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-2005, PostgreSQL Global Development Group
12
* Portions Copyright (c) 1994, Regents of the University of California
16
* $PostgreSQL: pgsql/src/backend/storage/ipc/ipc.c,v 1.90 2004-12-31 22:00:56 pgsql Exp $
18
*-------------------------------------------------------------------------
26
#include "miscadmin.h"
27
#include "storage/ipc.h"
31
* This flag is set during proc_exit() to change ereport()'s behavior,
32
* so that an ereport() from an on_proc_exit routine cannot get us out
33
* of the exit procedure. We do NOT want to go back to the idle loop...
35
bool proc_exit_inprogress = false;
38
/* ----------------------------------------------------------------
39
* exit() handling stuff
41
* These functions are in generally the same spirit as atexit(2),
42
* but provide some additional features we need --- in particular,
43
* we want to register callbacks to invoke when we are disconnecting
44
* from a broken shared-memory context but not exiting the postmaster.
46
* Callback functions can take zero, one, or two args: the first passed
47
* arg is the integer exitcode, the second is the Datum supplied when
48
* the callback was registered.
49
* ----------------------------------------------------------------
52
#define MAX_ON_EXITS 20
56
void (*function) (int code, Datum arg);
58
} on_proc_exit_list[MAX_ON_EXITS], on_shmem_exit_list[MAX_ON_EXITS];
60
static int on_proc_exit_index,
64
/* ----------------------------------------------------------------
67
* this function calls all the callbacks registered
68
* for it (to free resources) and then calls exit.
69
* This should be the only function to call exit().
71
* ----------------------------------------------------------------
77
* Once we set this flag, we are committed to exit. Any ereport()
78
* will NOT send control back to the main loop, but right back here.
80
proc_exit_inprogress = true;
83
* Forget any pending cancel or die requests; we're doing our best to
84
* close up shop already. Note that the signal handlers will not set
85
* these flags again, now that proc_exit_inprogress is set.
87
InterruptPending = false;
88
ProcDiePending = false;
89
QueryCancelPending = false;
90
/* And let's just make *sure* we're not interrupted ... */
91
ImmediateInterruptOK = false;
92
InterruptHoldoffCount = 1;
95
elog(DEBUG3, "proc_exit(%d)", code);
97
/* do our shared memory exits first */
101
* call all the callbacks registered before calling exit().
103
* Note that since we decrement on_proc_exit_index each time, if a
104
* callback calls ereport(ERROR) or ereport(FATAL) then it won't be
105
* invoked again when control comes back here (nor will the
106
* previously-completed callbacks). So, an infinite loop should not
109
while (--on_proc_exit_index >= 0)
110
(*on_proc_exit_list[on_proc_exit_index].function) (code,
111
on_proc_exit_list[on_proc_exit_index].arg);
113
elog(DEBUG3, "exit(%d)", code);
117
/* ------------------
118
* Run all of the on_shmem_exit routines --- but don't actually exit.
119
* This is used by the postmaster to re-initialize shared memory and
120
* semaphores after a backend dies horribly.
126
elog(DEBUG3, "shmem_exit(%d)", code);
129
* call all the registered callbacks.
131
* As with proc_exit(), we remove each callback from the list before
132
* calling it, to avoid infinite loop in case of error.
134
while (--on_shmem_exit_index >= 0)
135
(*on_shmem_exit_list[on_shmem_exit_index].function) (code,
136
on_shmem_exit_list[on_shmem_exit_index].arg);
138
on_shmem_exit_index = 0;
141
/* ----------------------------------------------------------------
144
* this function adds a callback function to the list of
145
* functions invoked by proc_exit(). -cim 2/6/90
146
* ----------------------------------------------------------------
149
on_proc_exit(void (*function) (int code, Datum arg), Datum arg)
151
if (on_proc_exit_index >= MAX_ON_EXITS)
153
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
154
errmsg_internal("out of on_proc_exit slots")));
156
on_proc_exit_list[on_proc_exit_index].function = function;
157
on_proc_exit_list[on_proc_exit_index].arg = arg;
159
++on_proc_exit_index;
162
/* ----------------------------------------------------------------
165
* this function adds a callback function to the list of
166
* functions invoked by shmem_exit(). -cim 2/6/90
167
* ----------------------------------------------------------------
170
on_shmem_exit(void (*function) (int code, Datum arg), Datum arg)
172
if (on_shmem_exit_index >= MAX_ON_EXITS)
174
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
175
errmsg_internal("out of on_shmem_exit slots")));
177
on_shmem_exit_list[on_shmem_exit_index].function = function;
178
on_shmem_exit_list[on_shmem_exit_index].arg = arg;
180
++on_shmem_exit_index;
183
/* ----------------------------------------------------------------
186
* this function clears all on_proc_exit() and on_shmem_exit()
187
* registered functions. This is used just after forking a backend,
188
* so that the backend doesn't believe it should call the postmaster's
189
* on-exit routines when it exits...
190
* ----------------------------------------------------------------
195
on_shmem_exit_index = 0;
196
on_proc_exit_index = 0;