-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpg_launchd.c
396 lines (334 loc) · 7.84 KB
/
pg_launchd.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Synopsis:
* MAC/OSX launchd wrapper around postgres for fast halt with SIGINT
* Description:
* pg_launchd only exists because launchd on MAC/OSX can not send a
* SIGINT to do a fast and safe shutdown.
*
* pg_launchd is a wrapper program that invokes the postgres master
* with a fast shutdown using SIGINT after receiving a SIGTERM from
* launchd. The time to pause before Also, the pid file is forcibly
* removed.
* Usage:
* pg_launchd \
* --SIGINT-pause 5 \
* --pid-path /usr/local/pgsql/data/postmaster.pid \
* /usr/local/pgsql/bin/postgres \
* -D /usr/local/pgsql/data
*
* # contents of /Library/LaunchDaemons/org.postgresql.plist
* <key>ProgramArguments</key>
* <array>
* <string>/Users/postgres/sbin/pg_launchd</string>
* <string>--SIGINT-pause</string>
* <string>30</string>
* <string>--pid-path</string>
* <string>/usr/local/pgsql/data/postmaster.pid</string>
* <string>/usr/local/pgsql/bin/postgres</string>
* <string>-D</string>
* <string>/usr/local/pgsql/data</string>
* </array>
*
* Exit Status:
* 0 clean shutdown of postmaster with SIGINT
* 1 unclean shutdown of postmaster with SIGKILL
* 2 unexpected error
* 3 unexpected exit of postmaster process
* Note:
* Convert to libjmscott.a
*
* A simpler method must exist to send a SIGINT to a process started
* by launchd.
*/
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "jmscott/libjmscott.h"
extern int errno;
static char *pid_path = 0;
static int SIGINT_pause = 0;
static pid_t postmaster_pid = 0;
static int exit_status = -1;
/*
* Synopsis:
* Fast, safe and simple string concatenator
* Usage:
* buf[0] = 0
* _strcat(buf, sizeof buf, "hello, world");
* _strcat(buf, sizeof buf, ": ");
* _strcat(buf, sizeof buf, "good bye, cruel world");
*/
static void
_strcat(char *tgt, int tgtsize, char *src)
{
// find null terminated end of target buffer
while (*tgt++)
--tgtsize;
--tgt;
// copy non-null src bytes, leaving room for trailing null
while (--tgtsize > 0 && *src)
*tgt++ = *src++;
// target always null terminated
*tgt = 0;
}
/*
* Write info messages to standard error.
*/
static void
info(char *msg1)
{
char msg[JMSCOTT_ATOMIC_WRITE_SIZE];
static char nl[] = "\n";
msg[0] = 0;
snprintf(msg, sizeof msg, "pg_launchd: #%d: ", getpid());
_strcat(msg, sizeof msg, msg1);
_strcat(msg, sizeof msg, nl);
again:
if (write(2, msg, strlen(msg)) < 0 && errno == EINTR)
goto again;
}
static void
info2(char *msg1, char *msg2)
{
char msg[JMSCOTT_ATOMIC_WRITE_SIZE];
msg[0] = 0;
_strcat(msg, sizeof msg, msg1);
_strcat(msg, sizeof msg, ": ");
_strcat(msg, sizeof msg, msg2);
info(msg);
}
static void die2(char *msg1, char *msg2);
static void
leave(int status)
{
if (pid_path) {
char *path = pid_path;
pid_path = 0;
info2("unlinking pid file", path);
if (unlink(path) && errno != ENOENT)
die2("unlink(pid path) failed", strerror(errno));
}
if (postmaster_pid > 0)
info("good bye, cruel world");
exit(status);
}
static void
ERROR(char *msg)
{
info2("ERROR", msg);
}
static void
ERROR2(char *msg1, char *msg2)
{
char msg[JMSCOTT_ATOMIC_WRITE_SIZE];
msg[0] = 0;
_strcat(msg, sizeof msg, msg1);
_strcat(msg, sizeof msg, ": ");
_strcat(msg, sizeof msg, msg2);
ERROR(msg);
}
/*
* Write error message to standard error and exit process with status code.
*/
static void
die(char *msg)
{
ERROR(msg);
leave(1);
}
static void
die2(char *msg1, char *msg2)
{
char msg[JMSCOTT_ATOMIC_WRITE_SIZE];
msg[0] = 0;
_strcat(msg, sizeof msg, msg1);
_strcat(msg, sizeof msg, ": ");
_strcat(msg, sizeof msg, msg2);
die(msg);
}
static void
die3(char *msg1, char *msg2, char *msg3)
{
char msg[JMSCOTT_ATOMIC_WRITE_SIZE];
msg[0] = 0;
_strcat(msg, sizeof msg, msg1);
_strcat(msg, sizeof msg, ": ");
_strcat(msg, sizeof msg, msg2);
die2(msg, msg3);
}
static void
info_wait_status(int status)
{
info("postmaster has stopped");
/*
* Unexpected exit of postmaster
*/
char what[JMSCOTT_ATOMIC_WRITE_SIZE];
if (WIFSIGNALED(status))
snprintf(what, sizeof what, "signaled: sig #%d", status);
else if (WIFEXITED(status))
snprintf(
what,
sizeof what,
"exit status: %d",
WEXITSTATUS(status)
);
else if (WIFSTOPPED(status))
snprintf(what, sizeof what, "stopped: #%d", status);
else
snprintf(what, sizeof what, "unknown exit status: %d", status);
info2("postmaster process terminated", what);
}
static void
WARN(char *msg)
{
info2("WARN", msg);
}
static void
catch_TERM(int sig)
{
(void)sig;
info("signal TERM caught");
if (!postmaster_pid) {
WARN("postmaster not running");
exit_status = 0;
return;
}
// fast shutdown with SIGINT to postmaster
info("sending fast shutdown signal INT to postmaster ...");
if (kill(postmaster_pid, SIGINT) != 0) {
exit_status = 2;
ERROR2("kill(postmaster, INT) failed", strerror(errno));
return;
}
info("postmaster got signal");
int pause = SIGINT_pause;
SIGINT_pause = 0;
while (pause > 1) {
info("pausing 1 second as postmaster stops gracefully");
sleep(1);
--pause;
int status;
pid_t pid = waitpid(postmaster_pid, &status, WNOHANG);
if (pid < 0) {
exit_status = 2;
ERROR2(
"waitpid(postmaster TERM) failed",
strerror(errno)
);
return;
}
// postmaster exited
if (pid > 0) {
// cheap sanity test
if (pid != postmaster_pid) {
exit_status = 2;
ERROR("non postmaster process exited");
return;
}
info_wait_status(status);
exit_status = 0;
return;
}
}
// nuke the postmaster
WARN("postmaster will not stop");
WARN("sending KILL signal to postmaster to force stop");
if (kill(postmaster_pid, SIGKILL) && errno != ESRCH) {
ERROR2("kill(postmaster, KILL) failed", strerror(errno));
exit_status = 2;
return;
}
}
static void
eSIGINT(char *msg)
{
die2("option: SIGINT-pause", msg);
}
static void
eSIGINT2(char *msg1, char *msg2)
{
die3("option: SIGINT-pause", msg1, msg2);
}
static void
epid(char *msg)
{
die2("option: pid-path", msg);
}
static void
postmaster(int pargc, char **pargv)
{
pargv[pargc] = 0;
postmaster_pid = fork();
if (postmaster_pid < 0)
die2("fork() failed", strerror(errno));
if (postmaster_pid == 0) {
info2("execv postmaster", pargv[0]);
execv(pargv[0], pargv);
die2("execv(postmaster) failed", strerror(errno));
}
signal(SIGTERM, catch_TERM);
char buf[JMSCOTT_ATOMIC_WRITE_SIZE];
snprintf(buf, sizeof buf, "postmaster pid: #%d", postmaster_pid);
info(buf);
}
int
main(int argc, char **argv)
{
int i;
if (argc < 6)
die("wrong number of command line arguments");
char **pargv = (char **)malloc(argc * sizeof *argv);
if (!pargv)
die("malloc(argv) failed");
int pargc = 0;
for (i = 1; i < argc; i++)
if (strcmp("--SIGINT-pause", argv[i]) == 0) {
if (++i == argc)
eSIGINT("missing seconds");
if (SIGINT_pause)
eSIGINT("option given more than once");
char *pa = argv[i];
errno = 0;
SIGINT_pause = strtoul(pa, (char **)0, 10);
if (SIGINT_pause == 0) {
if (errno > 0)
eSIGINT2(
"can not parse seconds",
strerror(errno)
);
else
eSIGINT2(
"can not parse seconds",
argv[i]
);
}
} else if (strcmp("--pid-path", argv[i]) == 0) {
if (++i == argc)
epid("missing path to pid file");
if (pid_path)
epid("option given more than once");
pid_path = strdup(argv[i]);
} else
pargv[pargc++] = strdup(argv[i]);
if (SIGINT_pause == 0)
eSIGINT("missing required option");
if (!pid_path)
epid("missing required option");
info("hello, world");
postmaster(pargc, pargv);
signal(SIGTERM, catch_TERM);
int status = 0;
if (waitpid(postmaster_pid, &status, 0) < 0 && errno != ECHILD)
die2("waitpid(postmaster) failed", strerror(errno));
// error occured during TERM signal
if (exit_status > -1)
leave(exit_status);
info_wait_status(status);
leave(3);
return 0;
}