-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_command_launcher.c
162 lines (141 loc) · 3.65 KB
/
pg_command_launcher.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
#include "postgres.h"
#include <signal.h>
#include "miscadmin.h"
#include "storage/ipc.h"
#include "utils/guc.h"
PG_MODULE_MAGIC;
/* Saved hook value in case of unload */
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
/* GUC variables */
static char *command_at_startup = NULL;
static char *command_at_shutdown = NULL;
/* Function declarations */
void _PG_init(void);
void _PG_fini(void);
static void pgcl_shmem_startup(void);
static void pgcl_shmem_shutdown(int code, Datum arg);
static void ExecuteCommand(char *command, char *commandName);
/*
* Module load callback
*/
void
_PG_init(void)
{
if (!process_shared_preload_libraries_in_progress)
{
ereport(WARNING,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("pg_command_launcher must be registered in shared_preload_libraries")));
return;
}
/* Define (or redefine) custom GUC variables */
DefineCustomStringVariable("pg_command_launcher.command_at_startup",
"Sets the shell command that will be called at server startup.",
NULL,
&command_at_startup,
"",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomStringVariable("pg_command_launcher.command_at_shutdown",
"Sets the shell command that will be called at server shutdown.",
NULL,
&command_at_shutdown,
"",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
EmitWarningsOnPlaceholders("pg_command_launcher");
/* Install hook */
prev_shmem_startup_hook = shmem_startup_hook;
shmem_startup_hook = pgcl_shmem_startup;
}
/*
* Module unload callback
*/
void
_PG_fini(void)
{
/* Uninstall hook */
shmem_startup_hook = prev_shmem_startup_hook;
}
/*
* shmem_startup_hook
*/
static void
pgcl_shmem_startup(void)
{
if (prev_shmem_startup_hook)
prev_shmem_startup_hook();
/*
* If we're in the postmaster (or a standalone backend...),
* set up a shmem exit hook to call command_at_shutdown.
*/
if (!IsUnderPostmaster)
on_shmem_exit(pgcl_shmem_shutdown, (Datum) 0);
if (command_at_startup)
ExecuteCommand(command_at_startup, "command_at_startup");
}
static void
pgcl_shmem_shutdown(int code, Datum arg)
{
if (command_at_shutdown)
ExecuteCommand(command_at_shutdown, "command_at_shutdown");
}
static void
ExecuteCommand(char *command, char *commandName)
{
int rc;
if (!command || !commandName)
return;
ereport(DEBUG3,
(errmsg_internal("executing %s \"%s\"", commandName, command)));
rc = system(command);
if (rc != 0)
{
if (WIFEXITED(rc))
{
ereport(WARNING,
(errmsg("%s failed with exit code %d",
commandName, WEXITSTATUS(rc)),
errdetail("The failed command was: %s",
command)));
}
else if (WIFSIGNALED(rc))
{
#if defined(WIN32)
ereport(WARNING,
(errmsg("%s was terminated by exception 0x%X",
commandName, WTERMSIG(rc)),
errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
errdetail("The failed command was: %s",
command)));
#elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST
ereport(WARNING,
(errmsg("%s was terminated by signal %d: %s",
commandName, WTERMSIG(rc),
WTERMSIG(rc) < NSIG ? sys_siglist[WTERMSIG(rc)] : "(unknown)"),
errdetail("The failed command was: %s",
command)));
#else
ereport(WARNING,
(errmsg("%s was terminated by signal %d",
commandName, WTERMSIG(rc)),
errdetail("The failed command was: %s",
command)));
#endif
}
else
{
ereport(WARNING,
(errmsg("%s exited with unrecognized status %d",
commandName, rc),
errdetail("The failed command was: %s",
command)));
}
}
}