1 /*-------------------------------------------------------------------------
2  *
3  * ipc.c
4  *	  POSTGRES inter-process communication definitions.
5  *
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.
9  *
10  *
11  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  *
15  * IDENTIFICATION
16  *	  src/backend/storage/ipc/ipc.c
17  *
18  *-------------------------------------------------------------------------
19  */
20 #include "postgres.h"
21 
22 #include <signal.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 
26 #include "miscadmin.h"
27 #ifdef PROFILE_PID_DIR
28 #include "postmaster/autovacuum.h"
29 #endif
30 #include "storage/dsm.h"
31 #include "storage/ipc.h"
32 #include "tcop/tcopprot.h"
33 
34 
35 /*
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...
39  */
40 bool		proc_exit_inprogress = false;
41 
42 /*
43  * This flag tracks whether we've called atexit() in the current process
44  * (or in the parent postmaster).
45  */
46 static bool atexit_callback_setup = false;
47 
48 /* local functions */
49 static void proc_exit_prepare(int code);
50 
51 
52 /* ----------------------------------------------------------------
53  *						exit() handling stuff
54  *
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.
59  *
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  * ----------------------------------------------------------------
64  */
65 
66 #define MAX_ON_EXITS 20
67 
68 struct ONEXIT
69 {
70 	pg_on_exit_callback function;
71 	Datum		arg;
72 };
73 
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];
77 
78 static int	on_proc_exit_index,
79 			on_shmem_exit_index,
80 			before_shmem_exit_index;
81 
82 
83 /* ----------------------------------------------------------------
84  *		proc_exit
85  *
86  *		this function calls all the callbacks registered
87  *		for it (to free resources) and then calls exit.
88  *
89  *		This should be the only function to call exit().
90  *		-cim 2/6/90
91  *
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  * ----------------------------------------------------------------
97  */
98 void
proc_exit(int code)99 proc_exit(int code)
100 {
101 	/* Clean up everything that must be cleaned up */
102 	proc_exit_prepare(code);
103 
104 #ifdef PROFILE_PID_DIR
105 	{
106 		/*
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 ...
115 		 *
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.
120 		 *
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
126 		 * nice place.
127 		 */
128 		char		gprofDirName[32];
129 
130 		if (IsAutoVacuumWorkerProcess())
131 			snprintf(gprofDirName, 32, "gprof/avworker");
132 		else
133 			snprintf(gprofDirName, 32, "gprof/%d", (int) getpid());
134 
135 		mkdir("gprof", S_IRWXU | S_IRWXG | S_IRWXO);
136 		mkdir(gprofDirName, S_IRWXU | S_IRWXG | S_IRWXO);
137 		chdir(gprofDirName);
138 	}
139 #endif
140 
141 	elog(DEBUG3, "exit(%d)", code);
142 
143 	exit(code);
144 }
145 
146 /*
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.
150  */
151 static void
proc_exit_prepare(int code)152 proc_exit_prepare(int code)
153 {
154 	/*
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.
157 	 */
158 	proc_exit_inprogress = true;
159 
160 	/*
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.
164 	 */
165 	InterruptPending = false;
166 	ProcDiePending = false;
167 	QueryCancelPending = false;
168 	InterruptHoldoffCount = 1;
169 	CritSectionCount = 0;
170 
171 	/*
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.)
179 	 */
180 	error_context_stack = NULL;
181 	/* For the same reason, reset debug_query_string before it's clobbered */
182 	debug_query_string = NULL;
183 
184 	/* do our shared memory exits first */
185 	shmem_exit(code);
186 
187 	elog(DEBUG3, "proc_exit(%d): %d callbacks to make",
188 		 code, on_proc_exit_index);
189 
190 	/*
191 	 * call all the registered callbacks.
192 	 *
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
197 	 * possible.
198 	 */
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);
202 
203 	on_proc_exit_index = 0;
204 }
205 
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.
212  * ------------------
213  */
214 void
shmem_exit(int code)215 shmem_exit(int code)
216 {
217 	/*
218 	 * Call before_shmem_exit callbacks.
219 	 *
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.
224 	 */
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;
231 
232 	/*
233 	 * Call dynamic shared memory callbacks.
234 	 *
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.
241 	 *
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.
247 	 */
248 	dsm_backend_shutdown();
249 
250 	/*
251 	 * Call on_shmem_exit callbacks.
252 	 *
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.
257 	 */
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;
264 }
265 
266 /* ----------------------------------------------------------------
267  *		atexit_callback
268  *
269  *		Backstop to ensure that direct calls of exit() don't mess us up.
270  *
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  * ----------------------------------------------------------------
275  */
276 static void
atexit_callback(void)277 atexit_callback(void)
278 {
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);
282 }
283 
284 /* ----------------------------------------------------------------
285  *		on_proc_exit
286  *
287  *		this function adds a callback function to the list of
288  *		functions invoked by proc_exit().   -cim 2/6/90
289  * ----------------------------------------------------------------
290  */
291 void
on_proc_exit(pg_on_exit_callback function,Datum arg)292 on_proc_exit(pg_on_exit_callback function, Datum arg)
293 {
294 	if (on_proc_exit_index >= MAX_ON_EXITS)
295 		ereport(FATAL,
296 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
297 				 errmsg_internal("out of on_proc_exit slots")));
298 
299 	on_proc_exit_list[on_proc_exit_index].function = function;
300 	on_proc_exit_list[on_proc_exit_index].arg = arg;
301 
302 	++on_proc_exit_index;
303 
304 	if (!atexit_callback_setup)
305 	{
306 		atexit(atexit_callback);
307 		atexit_callback_setup = true;
308 	}
309 }
310 
311 /* ----------------------------------------------------------------
312  *		before_shmem_exit
313  *
314  *		Register early callback to perform user-level cleanup,
315  *		e.g. transaction abort, before we begin shutting down
316  *		low-level subsystems.
317  * ----------------------------------------------------------------
318  */
319 void
before_shmem_exit(pg_on_exit_callback function,Datum arg)320 before_shmem_exit(pg_on_exit_callback function, Datum arg)
321 {
322 	if (before_shmem_exit_index >= MAX_ON_EXITS)
323 		ereport(FATAL,
324 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
325 				 errmsg_internal("out of before_shmem_exit slots")));
326 
327 	before_shmem_exit_list[before_shmem_exit_index].function = function;
328 	before_shmem_exit_list[before_shmem_exit_index].arg = arg;
329 
330 	++before_shmem_exit_index;
331 
332 	if (!atexit_callback_setup)
333 	{
334 		atexit(atexit_callback);
335 		atexit_callback_setup = true;
336 	}
337 }
338 
339 /* ----------------------------------------------------------------
340  *		on_shmem_exit
341  *
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  * ----------------------------------------------------------------
346  */
347 void
on_shmem_exit(pg_on_exit_callback function,Datum arg)348 on_shmem_exit(pg_on_exit_callback function, Datum arg)
349 {
350 	if (on_shmem_exit_index >= MAX_ON_EXITS)
351 		ereport(FATAL,
352 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
353 				 errmsg_internal("out of on_shmem_exit slots")));
354 
355 	on_shmem_exit_list[on_shmem_exit_index].function = function;
356 	on_shmem_exit_list[on_shmem_exit_index].arg = arg;
357 
358 	++on_shmem_exit_index;
359 
360 	if (!atexit_callback_setup)
361 	{
362 		atexit(atexit_callback);
363 		atexit_callback_setup = true;
364 	}
365 }
366 
367 /* ----------------------------------------------------------------
368  *		cancel_before_shmem_exit
369  *
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
373  *		current uses.)
374  * ----------------------------------------------------------------
375  */
376 void
cancel_before_shmem_exit(pg_on_exit_callback function,Datum arg)377 cancel_before_shmem_exit(pg_on_exit_callback function, Datum arg)
378 {
379 	if (before_shmem_exit_index > 0 &&
380 		before_shmem_exit_list[before_shmem_exit_index - 1].function
381 		== function &&
382 		before_shmem_exit_list[before_shmem_exit_index - 1].arg == arg)
383 		--before_shmem_exit_index;
384 }
385 
386 /* ----------------------------------------------------------------
387  *		on_exit_reset
388  *
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  * ----------------------------------------------------------------
394  */
395 void
on_exit_reset(void)396 on_exit_reset(void)
397 {
398 	before_shmem_exit_index = 0;
399 	on_shmem_exit_index = 0;
400 	on_proc_exit_index = 0;
401 	reset_on_dsm_detach();
402 }
403