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-2016, 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/dsm.h"
31 #include "storage/ipc.h"
32 #include "tcop/tcopprot.h"
36 * This flag is set during proc_exit() to change ereport()'s behavior,
37 * so that an ereport() from an on_proc_exit routine cannot get us out
38 * of the exit procedure. We do NOT want to go back to the idle loop...
40 bool proc_exit_inprogress = false;
43 * This flag tracks whether we've called atexit() in the current process
44 * (or in the parent postmaster).
46 static bool atexit_callback_setup = false;
49 static void proc_exit_prepare(int code);
52 /* ----------------------------------------------------------------
53 * exit() handling stuff
55 * These functions are in generally the same spirit as atexit(),
56 * but provide some additional features we need --- in particular,
57 * we want to register callbacks to invoke when we are disconnecting
58 * from a broken shared-memory context but not exiting the postmaster.
60 * Callback functions can take zero, one, or two args: the first passed
61 * arg is the integer exitcode, the second is the Datum supplied when
62 * the callback was registered.
63 * ----------------------------------------------------------------
66 #define MAX_ON_EXITS 20
70 pg_on_exit_callback function;
74 static struct ONEXIT on_proc_exit_list[MAX_ON_EXITS];
75 static struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS];
76 static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS];
78 static int on_proc_exit_index,
80 before_shmem_exit_index;
83 /* ----------------------------------------------------------------
86 * this function calls all the callbacks registered
87 * for it (to free resources) and then calls exit.
89 * This should be the only function to call exit().
92 * Unfortunately, we can't really guarantee that add-on code
93 * obeys the rule of not calling exit() directly. So, while
94 * this is the preferred way out of the system, we also register
95 * an atexit callback that will make sure cleanup happens.
96 * ----------------------------------------------------------------
101 /* Clean up everything that must be cleaned up */
102 proc_exit_prepare(code);
104 #ifdef PROFILE_PID_DIR
107 * If we are profiling ourself then gprof's mcleanup() is about to
108 * write out a profile to ./gmon.out. Since mcleanup() always uses a
109 * fixed file name, each backend will overwrite earlier profiles. To
110 * fix that, we create a separate subdirectory for each backend
111 * (./gprof/pid) and 'cd' to that subdirectory before we exit() - that
112 * forces mcleanup() to write each profile into its own directory. We
113 * end up with something like: $PGDATA/gprof/8829/gmon.out
114 * $PGDATA/gprof/8845/gmon.out ...
116 * To avoid undesirable disk space bloat, autovacuum workers are
117 * discriminated against: all their gmon.out files go into the same
118 * subdirectory. Without this, an installation that is "just sitting
119 * there" nonetheless eats megabytes of disk space every few seconds.
121 * Note that we do this here instead of in an on_proc_exit() callback
122 * because we want to ensure that this code executes last - we don't
123 * want to interfere with any other on_proc_exit() callback. For the
124 * same reason, we do not include it in proc_exit_prepare ... so if
125 * you are exiting in the "wrong way" you won't drop your profile in a
128 char gprofDirName[32];
130 if (IsAutoVacuumWorkerProcess())
131 snprintf(gprofDirName, 32, "gprof/avworker");
133 snprintf(gprofDirName, 32, "gprof/%d", (int) getpid());
135 mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO);
136 mkdir(gprofDirName, S_IRWXU | S_IRWXG | S_IRWXO);
141 elog(DEBUG3, "exit(%d)", code);
147 * Code shared between proc_exit and the atexit handler. Note that in
148 * normal exit through proc_exit, this will actually be called twice ...
149 * but the second call will have nothing to do.
152 proc_exit_prepare(int code)
155 * Once we set this flag, we are committed to exit. Any ereport() will
156 * NOT send control back to the main loop, but right back here.
158 proc_exit_inprogress = true;
161 * Forget any pending cancel or die requests; we're doing our best to
162 * close up shop already. Note that the signal handlers will not set
163 * these flags again, now that proc_exit_inprogress is set.
165 InterruptPending = false;
166 ProcDiePending = false;
167 QueryCancelPending = false;
168 InterruptHoldoffCount = 1;
169 CritSectionCount = 0;
172 * Also clear the error context stack, to prevent error callbacks from
173 * being invoked by any elog/ereport calls made during proc_exit. Whatever
174 * context they might want to offer is probably not relevant, and in any
175 * case they are likely to fail outright after we've done things like
176 * aborting any open transaction. (In normal exit scenarios the context
177 * stack should be empty anyway, but it might not be in the case of
178 * elog(FATAL) for example.)
180 error_context_stack = NULL;
181 /* For the same reason, reset debug_query_string before it's clobbered */
182 debug_query_string = NULL;
184 /* do our shared memory exits first */
187 elog(DEBUG3, "proc_exit(%d): %d callbacks to make",
188 code, on_proc_exit_index);
191 * call all the registered callbacks.
193 * Note that since we decrement on_proc_exit_index each time, if a
194 * callback calls ereport(ERROR) or ereport(FATAL) then it won't be
195 * invoked again when control comes back here (nor will the
196 * previously-completed callbacks). So, an infinite loop should not be
199 while (--on_proc_exit_index >= 0)
200 (*on_proc_exit_list[on_proc_exit_index].function) (code,
201 on_proc_exit_list[on_proc_exit_index].arg);
203 on_proc_exit_index = 0;
206 /* ------------------
207 * Run all of the on_shmem_exit routines --- but don't actually exit.
208 * This is used by the postmaster to re-initialize shared memory and
209 * semaphores after a backend dies horribly. As with proc_exit(), we
210 * remove each callback from the list before calling it, to avoid
211 * infinite loop in case of error.
218 * Call before_shmem_exit callbacks.
220 * These should be things that need most of the system to still be up and
221 * working, such as cleanup of temp relations, which requires catalog
222 * access; or things that need to be completed because later cleanup steps
223 * depend on them, such as releasing lwlocks.
225 elog(DEBUG3, "shmem_exit(%d): %d before_shmem_exit callbacks to make",
226 code, before_shmem_exit_index);
227 while (--before_shmem_exit_index >= 0)
228 (*before_shmem_exit_list[before_shmem_exit_index].function) (code,
229 before_shmem_exit_list[before_shmem_exit_index].arg);
230 before_shmem_exit_index = 0;
233 * Call dynamic shared memory callbacks.
235 * These serve the same purpose as late callbacks, but for dynamic shared
236 * memory segments rather than the main shared memory segment.
237 * dsm_backend_shutdown() has the same kind of progressive logic we use
238 * for the main shared memory segment; namely, it unregisters each
239 * callback before invoking it, so that we don't get stuck in an infinite
240 * loop if one of those callbacks itself throws an ERROR or FATAL.
242 * Note that explicitly calling this function here is quite different from
243 * registering it as an on_shmem_exit callback for precisely this reason:
244 * if one dynamic shared memory callback errors out, the remaining
245 * callbacks will still be invoked. Thus, hard-coding this call puts it
246 * equal footing with callbacks for the main shared memory segment.
248 dsm_backend_shutdown();
251 * Call on_shmem_exit callbacks.
253 * These are generally releasing low-level shared memory resources. In
254 * some cases, this is a backstop against the possibility that the early
255 * callbacks might themselves fail, leading to re-entry to this routine;
256 * in other cases, it's cleanup that only happens at process exit.
258 elog(DEBUG3, "shmem_exit(%d): %d on_shmem_exit callbacks to make",
259 code, on_shmem_exit_index);
260 while (--on_shmem_exit_index >= 0)
261 (*on_shmem_exit_list[on_shmem_exit_index].function) (code,
262 on_shmem_exit_list[on_shmem_exit_index].arg);
263 on_shmem_exit_index = 0;
266 /* ----------------------------------------------------------------
269 * Backstop to ensure that direct calls of exit() don't mess us up.
271 * Somebody who was being really uncooperative could call _exit(),
272 * but for that case we have a "dead man switch" that will make the
273 * postmaster treat it as a crash --- see pmsignal.c.
274 * ----------------------------------------------------------------
277 atexit_callback(void)
279 /* Clean up everything that must be cleaned up */
280 /* ... too bad we don't know the real exit code ... */
281 proc_exit_prepare(-1);
284 /* ----------------------------------------------------------------
287 * this function adds a callback function to the list of
288 * functions invoked by proc_exit(). -cim 2/6/90
289 * ----------------------------------------------------------------
292 on_proc_exit(pg_on_exit_callback function, Datum arg)
294 if (on_proc_exit_index >= MAX_ON_EXITS)
296 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
297 errmsg_internal("out of on_proc_exit slots")));
299 on_proc_exit_list[on_proc_exit_index].function = function;
300 on_proc_exit_list[on_proc_exit_index].arg = arg;
302 ++on_proc_exit_index;
304 if (!atexit_callback_setup)
306 atexit(atexit_callback);
307 atexit_callback_setup = true;
311 /* ----------------------------------------------------------------
314 * Register early callback to perform user-level cleanup,
315 * e.g. transaction abort, before we begin shutting down
316 * low-level subsystems.
317 * ----------------------------------------------------------------
320 before_shmem_exit(pg_on_exit_callback function, Datum arg)
322 if (before_shmem_exit_index >= MAX_ON_EXITS)
324 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
325 errmsg_internal("out of before_shmem_exit slots")));
327 before_shmem_exit_list[before_shmem_exit_index].function = function;
328 before_shmem_exit_list[before_shmem_exit_index].arg = arg;
330 ++before_shmem_exit_index;
332 if (!atexit_callback_setup)
334 atexit(atexit_callback);
335 atexit_callback_setup = true;
339 /* ----------------------------------------------------------------
342 * Register ordinary callback to perform low-level shutdown
343 * (e.g. releasing our PGPROC); run after before_shmem_exit
344 * callbacks and before on_proc_exit callbacks.
345 * ----------------------------------------------------------------
348 on_shmem_exit(pg_on_exit_callback function, Datum arg)
350 if (on_shmem_exit_index >= MAX_ON_EXITS)
352 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
353 errmsg_internal("out of on_shmem_exit slots")));
355 on_shmem_exit_list[on_shmem_exit_index].function = function;
356 on_shmem_exit_list[on_shmem_exit_index].arg = arg;
358 ++on_shmem_exit_index;
360 if (!atexit_callback_setup)
362 atexit(atexit_callback);
363 atexit_callback_setup = true;
367 /* ----------------------------------------------------------------
368 * cancel_before_shmem_exit
370 * this function removes a previously-registed before_shmem_exit
371 * callback. For simplicity, only the latest entry can be
372 * removed. (We could work harder but there is no need for
374 * ----------------------------------------------------------------
377 cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg)
379 if (before_shmem_exit_index > 0 &&
380 before_shmem_exit_list[before_shmem_exit_index - 1].function
382 before_shmem_exit_list[before_shmem_exit_index - 1].arg == arg)
383 --before_shmem_exit_index;
386 /* ----------------------------------------------------------------
389 * this function clears all on_proc_exit() and on_shmem_exit()
390 * registered functions. This is used just after forking a backend,
391 * so that the backend doesn't believe it should call the postmaster's
392 * on-exit routines when it exits...
393 * ----------------------------------------------------------------
398 before_shmem_exit_index = 0;
399 on_shmem_exit_index = 0;
400 on_proc_exit_index = 0;
401 reset_on_dsm_detach();