1
/*-------------------------------------------------------------------------
4
* POSTGRES inter-process communication definitions.
6
* This file is misnamed, as it no longer has much of anything directly
7
* to do with IPC. The functionality here is concerned with managing
8
* exit-time cleanup for either a postmaster or a backend.
11
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
12
* Portions Copyright (c) 1994, Regents of the University of California
16
* src/backend/storage/ipc/ipc.c
18
*-------------------------------------------------------------------------
26
#include "miscadmin.h"
27
#ifdef PROFILE_PID_DIR
28
#include "postmaster/autovacuum.h"
30
#include "storage/ipc.h"
31
#include "tcop/tcopprot.h"
35
* This flag is set during proc_exit() to change ereport()'s behavior,
36
* so that an ereport() from an on_proc_exit routine cannot get us out
37
* of the exit procedure. We do NOT want to go back to the idle loop...
39
bool proc_exit_inprogress = false;
42
* This flag tracks whether we've called atexit(2) in the current process
43
* (or in the parent postmaster).
45
static bool atexit_callback_setup = false;
48
static void proc_exit_prepare(int code);
51
/* ----------------------------------------------------------------
52
* exit() handling stuff
54
* These functions are in generally the same spirit as atexit(2),
55
* but provide some additional features we need --- in particular,
56
* we want to register callbacks to invoke when we are disconnecting
57
* from a broken shared-memory context but not exiting the postmaster.
59
* Callback functions can take zero, one, or two args: the first passed
60
* arg is the integer exitcode, the second is the Datum supplied when
61
* the callback was registered.
62
* ----------------------------------------------------------------
65
#define MAX_ON_EXITS 20
69
pg_on_exit_callback function;
71
} on_proc_exit_list[MAX_ON_EXITS], on_shmem_exit_list[MAX_ON_EXITS];
73
static int on_proc_exit_index,
77
/* ----------------------------------------------------------------
80
* this function calls all the callbacks registered
81
* for it (to free resources) and then calls exit.
83
* This should be the only function to call exit().
86
* Unfortunately, we can't really guarantee that add-on code
87
* obeys the rule of not calling exit() directly. So, while
88
* this is the preferred way out of the system, we also register
89
* an atexit callback that will make sure cleanup happens.
90
* ----------------------------------------------------------------
95
/* Clean up everything that must be cleaned up */
96
proc_exit_prepare(code);
98
#ifdef PROFILE_PID_DIR
101
* If we are profiling ourself then gprof's mcleanup() is about to
102
* write out a profile to ./gmon.out. Since mcleanup() always uses a
103
* fixed file name, each backend will overwrite earlier profiles. To
104
* fix that, we create a separate subdirectory for each backend
105
* (./gprof/pid) and 'cd' to that subdirectory before we exit() - that
106
* forces mcleanup() to write each profile into its own directory. We
107
* end up with something like: $PGDATA/gprof/8829/gmon.out
108
* $PGDATA/gprof/8845/gmon.out ...
110
* To avoid undesirable disk space bloat, autovacuum workers are
111
* discriminated against: all their gmon.out files go into the same
112
* subdirectory. Without this, an installation that is "just sitting
113
* there" nonetheless eats megabytes of disk space every few seconds.
115
* Note that we do this here instead of in an on_proc_exit() callback
116
* because we want to ensure that this code executes last - we don't
117
* want to interfere with any other on_proc_exit() callback. For the
118
* same reason, we do not include it in proc_exit_prepare ... so if
119
* you are exiting in the "wrong way" you won't drop your profile in a
122
char gprofDirName[32];
124
if (IsAutoVacuumWorkerProcess())
125
snprintf(gprofDirName, 32, "gprof/avworker");
127
snprintf(gprofDirName, 32, "gprof/%d", (int) getpid());
129
mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO);
130
mkdir(gprofDirName, S_IRWXU | S_IRWXG | S_IRWXO);
135
elog(DEBUG3, "exit(%d)", code);
141
* Code shared between proc_exit and the atexit handler. Note that in
142
* normal exit through proc_exit, this will actually be called twice ...
143
* but the second call will have nothing to do.
146
proc_exit_prepare(int code)
149
* Once we set this flag, we are committed to exit. Any ereport() will
150
* NOT send control back to the main loop, but right back here.
152
proc_exit_inprogress = true;
155
* Forget any pending cancel or die requests; we're doing our best to
156
* close up shop already. Note that the signal handlers will not set
157
* these flags again, now that proc_exit_inprogress is set.
159
InterruptPending = false;
160
ProcDiePending = false;
161
QueryCancelPending = false;
162
/* And let's just make *sure* we're not interrupted ... */
163
ImmediateInterruptOK = false;
164
InterruptHoldoffCount = 1;
165
CritSectionCount = 0;
168
* Also clear the error context stack, to prevent error callbacks from
169
* being invoked by any elog/ereport calls made during proc_exit. Whatever
170
* context they might want to offer is probably not relevant, and in any
171
* case they are likely to fail outright after we've done things like
172
* aborting any open transaction. (In normal exit scenarios the context
173
* stack should be empty anyway, but it might not be in the case of
174
* elog(FATAL) for example.)
176
error_context_stack = NULL;
177
/* For the same reason, reset debug_query_string before it's clobbered */
178
debug_query_string = NULL;
180
/* do our shared memory exits first */
183
elog(DEBUG3, "proc_exit(%d): %d callbacks to make",
184
code, on_proc_exit_index);
187
* call all the registered callbacks.
189
* Note that since we decrement on_proc_exit_index each time, if a
190
* callback calls ereport(ERROR) or ereport(FATAL) then it won't be
191
* invoked again when control comes back here (nor will the
192
* previously-completed callbacks). So, an infinite loop should not be
195
while (--on_proc_exit_index >= 0)
196
(*on_proc_exit_list[on_proc_exit_index].function) (code,
197
on_proc_exit_list[on_proc_exit_index].arg);
199
on_proc_exit_index = 0;
202
/* ------------------
203
* Run all of the on_shmem_exit routines --- but don't actually exit.
204
* This is used by the postmaster to re-initialize shared memory and
205
* semaphores after a backend dies horribly.
211
elog(DEBUG3, "shmem_exit(%d): %d callbacks to make",
212
code, on_shmem_exit_index);
215
* call all the registered callbacks.
217
* As with proc_exit(), we remove each callback from the list before
218
* calling it, to avoid infinite loop in case of error.
220
while (--on_shmem_exit_index >= 0)
221
(*on_shmem_exit_list[on_shmem_exit_index].function) (code,
222
on_shmem_exit_list[on_shmem_exit_index].arg);
224
on_shmem_exit_index = 0;
227
/* ----------------------------------------------------------------
230
* Backstop to ensure that direct calls of exit() don't mess us up.
232
* Somebody who was being really uncooperative could call _exit(),
233
* but for that case we have a "dead man switch" that will make the
234
* postmaster treat it as a crash --- see pmsignal.c.
235
* ----------------------------------------------------------------
240
atexit_callback(void)
242
/* Clean up everything that must be cleaned up */
243
/* ... too bad we don't know the real exit code ... */
244
proc_exit_prepare(-1);
246
#else /* assume we have on_exit instead */
249
atexit_callback(int exitstatus, void *arg)
251
/* Clean up everything that must be cleaned up */
252
proc_exit_prepare(exitstatus);
254
#endif /* HAVE_ATEXIT */
256
/* ----------------------------------------------------------------
259
* this function adds a callback function to the list of
260
* functions invoked by proc_exit(). -cim 2/6/90
261
* ----------------------------------------------------------------
264
on_proc_exit(pg_on_exit_callback function, Datum arg)
266
if (on_proc_exit_index >= MAX_ON_EXITS)
268
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
269
errmsg_internal("out of on_proc_exit slots")));
271
on_proc_exit_list[on_proc_exit_index].function = function;
272
on_proc_exit_list[on_proc_exit_index].arg = arg;
274
++on_proc_exit_index;
276
if (!atexit_callback_setup)
279
atexit(atexit_callback);
281
on_exit(atexit_callback, NULL);
283
atexit_callback_setup = true;
287
/* ----------------------------------------------------------------
290
* this function adds a callback function to the list of
291
* functions invoked by shmem_exit(). -cim 2/6/90
292
* ----------------------------------------------------------------
295
on_shmem_exit(pg_on_exit_callback function, Datum arg)
297
if (on_shmem_exit_index >= MAX_ON_EXITS)
299
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
300
errmsg_internal("out of on_shmem_exit slots")));
302
on_shmem_exit_list[on_shmem_exit_index].function = function;
303
on_shmem_exit_list[on_shmem_exit_index].arg = arg;
305
++on_shmem_exit_index;
307
if (!atexit_callback_setup)
310
atexit(atexit_callback);
312
on_exit(atexit_callback, NULL);
314
atexit_callback_setup = true;
318
/* ----------------------------------------------------------------
321
* this function removes an entry, if present, from the list of
322
* functions to be invoked by shmem_exit(). For simplicity,
323
* only the latest entry can be removed. (We could work harder
324
* but there is no need for current uses.)
325
* ----------------------------------------------------------------
328
cancel_shmem_exit(pg_on_exit_callback function, Datum arg)
330
if (on_shmem_exit_index > 0 &&
331
on_shmem_exit_list[on_shmem_exit_index - 1].function == function &&
332
on_shmem_exit_list[on_shmem_exit_index - 1].arg == arg)
333
--on_shmem_exit_index;
336
/* ----------------------------------------------------------------
339
* this function clears all on_proc_exit() and on_shmem_exit()
340
* registered functions. This is used just after forking a backend,
341
* so that the backend doesn't believe it should call the postmaster's
342
* on-exit routines when it exits...
343
* ----------------------------------------------------------------
348
on_shmem_exit_index = 0;
349
on_proc_exit_index = 0;