|
1 | 1 | #include "postgres.h"
|
2 | 2 | #include "fmgr.h"
|
| 3 | +#include "nodes/nodes.h" |
| 4 | +#include "miscadmin.h" |
| 5 | +#include "storage/ipc.h" |
| 6 | +#include "storage/shmem.h" |
| 7 | +#include "storage/lwlock.h" |
| 8 | +#include "utils/builtins.h" |
3 | 9 |
|
| 10 | +/* Constants and Macros */ |
4 | 11 | PG_MODULE_MAGIC;
|
5 | 12 |
|
6 | 13 | /* Abost compilation against unsupported versions. */
|
7 | 14 | #if PG_VERSION_NUM < 130000
|
8 | 15 | #error "Unsupported PostgreSQL Version"
|
9 | 16 | #endif
|
10 | 17 |
|
| 18 | +/* Is there a better size candidate? */ |
| 19 | +#define DENY_LIST_SIZE 128 |
| 20 | +/* CMD_NOTHING tells us about enum size as it is the last enum member. */ |
| 21 | +#define CMD_TYPE_COUNT CMD_NOTHING+1 |
| 22 | + |
| 23 | +typedef struct pgswUserPerm { |
| 24 | + Oid userId; |
| 25 | + /* Lists SQL command types the user is NOT allowed to run. |
| 26 | + * CmdType enum members are indexes for this array, i.e. |
| 27 | + * denyCmd[CMD_DELETE] = true to prohibit SQL DELETE statements. */ |
| 28 | + bool denyCmd[CMD_TYPE_COUNT]; |
| 29 | +} pgswUserPerm; |
| 30 | + |
| 31 | +typedef struct pgswSharedState { |
| 32 | + LWLock* lock; |
| 33 | + pgswUserPerm denyList[DENY_LIST_SIZE]; |
| 34 | + int denyListSize; |
| 35 | +} pgswSharedState; |
| 36 | + |
| 37 | +/* Function Prototypes */ |
| 38 | +void _PG_init(void); |
| 39 | +static void pgsw_shmem_request(void); |
| 40 | +static void pgsw_shmem_startup(void); |
| 41 | + |
11 | 42 | Datum my_function(PG_FUNCTION_ARGS);
|
12 | 43 | PG_FUNCTION_INFO_V1(my_function);
|
13 | 44 |
|
| 45 | +/* Global variables */ |
| 46 | +static shmem_request_hook_type prev_shmem_request_hook = NULL; |
| 47 | +static shmem_startup_hook_type prev_shmem_startup_hook = NULL; |
| 48 | +pgswSharedState *pgsw_shmem = NULL; |
| 49 | + |
| 50 | +void |
| 51 | +_PG_init(void) |
| 52 | +{ |
| 53 | + if(!process_shared_preload_libraries_in_progress) |
| 54 | + elog(FATAL, "Please use shared_preload_libraries"); |
| 55 | + |
| 56 | + prev_shmem_request_hook = shmem_request_hook; |
| 57 | + shmem_request_hook = pgsw_shmem_request; |
| 58 | + |
| 59 | + prev_shmem_startup_hook = shmem_startup_hook; |
| 60 | + shmem_startup_hook = pgsw_shmem_startup; |
| 61 | +} |
| 62 | + |
| 63 | +void |
| 64 | +pgsw_shmem_request(void) |
| 65 | +{ |
| 66 | + if(prev_shmem_request_hook) |
| 67 | + prev_shmem_request_hook(); |
| 68 | + |
| 69 | + RequestAddinShmemSpace(MAXALIGN(sizeof(pgswSharedState))); |
| 70 | + RequestNamedLWLockTranche("pg_sqlwall", 1); |
| 71 | +} |
| 72 | + |
| 73 | +void |
| 74 | +pgsw_shmem_startup(void) |
| 75 | +{ |
| 76 | + bool found; |
| 77 | + |
| 78 | + if(prev_shmem_startup_hook) |
| 79 | + prev_shmem_startup_hook(); |
| 80 | + |
| 81 | + LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE); |
| 82 | + |
| 83 | + pgsw_shmem = ShmemInitStruct("pg_sqlwall", sizeof(pgswSharedState), &found); |
| 84 | + if(!found) { |
| 85 | + pgsw_shmem->denyListSize = 0; |
| 86 | + pgsw_shmem->lock = &(GetNamedLWLockTranche("pg_sqlwall"))->lock; |
| 87 | + } |
| 88 | + |
| 89 | + LWLockRelease(AddinShmemInitLock); |
| 90 | +} |
| 91 | + |
14 | 92 | Datum
|
15 | 93 | my_function(PG_FUNCTION_ARGS)
|
16 | 94 | {
|
|
0 commit comments