-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhook_objacc.c
167 lines (143 loc) · 3.68 KB
/
hook_objacc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* ObjectAccess hook and associated functions.
*
*
* pg_schema_triggers/hook_objacc.c
*/
#include "postgres.h"
#include "catalog/dependency.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_class.h"
#include "catalog/pg_trigger.h"
#include "utils/builtins.h"
#include "events.h"
#include "hook_objacc.h"
static object_access_hook_type old_objectaccess_hook = NULL;
static void objectaccess_hook(ObjectAccessType access,
Oid classId,
Oid objectId,
int subId,
void *arg);
static void on_create(Oid classId, Oid objectId, int subId, ObjectAccessPostCreate *args);
static void on_alter(Oid classId, Oid objectId, int subId, ObjectAccessPostAlter *args);
static void on_drop(Oid classId, Oid objectId, int subId, ObjectAccessDrop *args);
/*
* Install our objectaccess hook.
*
* We react poorly if we discover another module has also installed an
* objectaccess hook.
*/
void
install_objacc_hook()
{
if (object_access_hook != NULL)
elog(FATAL, "an object_access hook is already installed.");
old_objectaccess_hook = object_access_hook;
object_access_hook = objectaccess_hook;
}
/*
* Remove our objectaccess hook.
*
* We react poorly if we discover another module has already removed
* our hook.
*/
void
remove_objacc_hook()
{
if (object_access_hook != objectaccess_hook)
elog(FATAL, "hook conflict, our object_access hook has been removed.");
object_access_hook = old_objectaccess_hook;
}
/*
* We abuse the OAT_* hooks (which are intended for access control frameworks
* such as sepgsql) for getting notified on relation create, alter, and drop
* actions. This is more reliable than digging around in the utility command
* parse trees, as these hooks are called from the internal functions which
* actually update the system catalogs, regardless of how those functions were
* invoked.
*/
static void
objectaccess_hook(ObjectAccessType access,
Oid classId,
Oid objectId,
int subId,
void *arg)
{
switch (access)
{
case OAT_POST_CREATE:
on_create(classId, objectId, subId, (ObjectAccessPostCreate *)arg);
break;
/*
* The OAT_POST_ALTER hook is called from the following functions:
*
* [func] [class] [obj] [subobj]
* renameatt_internal RelationRelationId pg_class.oid attnum
* RenameRelationInternal RelationRelationId pg_class.oid 0
*/
case OAT_POST_ALTER:
on_alter(classId, objectId, subId, (ObjectAccessPostAlter *)arg);
break;
case OAT_DROP:
on_drop(classId, objectId, subId, (ObjectAccessDrop *)arg);
break;
case OAT_NAMESPACE_SEARCH:
case OAT_FUNCTION_EXECUTE:
/* Ignore these events. */
break;
}
}
static void
on_create(Oid classId, Oid objectId, int subId, ObjectAccessPostCreate *args)
{
if (args->is_internal)
return;
switch (classId)
{
case RelationRelationId:
if (subId == 0)
relation_create_event(objectId);
else
column_add_event(objectId, subId);
break;
case TriggerRelationId:
trigger_create_event(objectId, args->is_internal);
break;
}
}
static void
on_alter(Oid classId, Oid objectId, int subId, ObjectAccessPostAlter *args)
{
if (args->is_internal)
return;
switch (classId)
{
case RelationRelationId:
if (subId == 0)
relation_alter_event(objectId);
else
column_alter_event(objectId, subId);
break;
case TriggerRelationId:
trigger_alter_event(objectId);
break;
}
}
static void
on_drop(Oid classId, Oid objectId, int subId, ObjectAccessDrop *args)
{
if (args->dropflags & PERFORM_DELETION_INTERNAL)
return;
switch (classId)
{
case RelationRelationId:
if (subId == 0)
relation_drop_event(objectId);
else
column_drop_event(objectId, subId);
break;
case TriggerRelationId:
trigger_drop_event(objectId);
break;
}
}