PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ipc.c File Reference
#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include "miscadmin.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "tcop/tcopprot.h"
Include dependency graph for ipc.c:

Go to the source code of this file.

Data Structures

struct  ONEXIT
 

Macros

#define MAX_ON_EXITS   20
 

Functions

static void proc_exit_prepare (int code)
 
void proc_exit (int code)
 
void shmem_exit (int code)
 
static void atexit_callback (void)
 
void on_proc_exit (pg_on_exit_callback function, Datum arg)
 
void before_shmem_exit (pg_on_exit_callback function, Datum arg)
 
void on_shmem_exit (pg_on_exit_callback function, Datum arg)
 
void cancel_before_shmem_exit (pg_on_exit_callback function, Datum arg)
 
void on_exit_reset (void)
 

Variables

bool proc_exit_inprogress = false
 
static bool atexit_callback_setup = false
 
static struct ONEXIT on_proc_exit_list [MAX_ON_EXITS]
 
static struct ONEXIT on_shmem_exit_list [MAX_ON_EXITS]
 
static struct ONEXIT before_shmem_exit_list [MAX_ON_EXITS]
 
static int on_proc_exit_index
 
static int on_shmem_exit_index
 
static int before_shmem_exit_index
 

Macro Definition Documentation

#define MAX_ON_EXITS   20

Definition at line 66 of file ipc.c.

Referenced by before_shmem_exit(), on_proc_exit(), and on_shmem_exit().

Function Documentation

static void atexit_callback ( void  )
static

Definition at line 277 of file ipc.c.

References proc_exit_prepare().

Referenced by before_shmem_exit(), on_proc_exit(), and on_shmem_exit().

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 }
static void proc_exit_prepare(int code)
Definition: ipc.c:152
void before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 320 of file ipc.c.

References arg, ONEXIT::arg, atexit_callback(), atexit_callback_setup, before_shmem_exit_index, before_shmem_exit_list, ereport, errcode(), errmsg_internal(), FATAL, ONEXIT::function, and MAX_ON_EXITS.

Referenced by ApplyLauncherMain(), AtEOXact_Namespace(), AuxiliaryProcessMain(), Exec_ListenPreCommit(), InitPostgres(), LockGXact(), logicalrep_worker_attach(), MarkAsPreparing(), and pg_start_backup().

321 {
323  ereport(FATAL,
324  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
325  errmsg_internal("out of before_shmem_exit slots")));
326 
329 
331 
333  {
334  atexit(atexit_callback);
335  atexit_callback_setup = true;
336  }
337 }
#define MAX_ON_EXITS
Definition: ipc.c:66
int errcode(int sqlerrcode)
Definition: elog.c:575
pg_on_exit_callback function
Definition: ipc.c:70
static bool atexit_callback_setup
Definition: ipc.c:46
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
static int before_shmem_exit_index
Definition: ipc.c:78
Datum arg
Definition: ipc.c:71
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:76
static void atexit_callback(void)
Definition: ipc.c:277
void cancel_before_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 377 of file ipc.c.

References before_shmem_exit_index, and before_shmem_exit_list.

Referenced by pg_stop_backup_v2().

378 {
379  if (before_shmem_exit_index > 0 &&
381  == function &&
384 }
static int before_shmem_exit_index
Definition: ipc.c:78
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:76
void on_exit_reset ( void  )

Definition at line 396 of file ipc.c.

References before_shmem_exit_index, on_proc_exit_index, on_shmem_exit_index, and reset_on_dsm_detach().

Referenced by bg_quickdie(), bgworker_quickdie(), chkpt_quickdie(), InitPostmasterChild(), quickdie(), startupproc_quickdie(), wal_quickdie(), and WalRcvQuickDieHandler().

397 {
400  on_proc_exit_index = 0;
402 }
void reset_on_dsm_detach(void)
Definition: dsm.c:1075
static int on_shmem_exit_index
Definition: ipc.c:78
static int on_proc_exit_index
Definition: ipc.c:78
static int before_shmem_exit_index
Definition: ipc.c:78
void on_proc_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 292 of file ipc.c.

References arg, ONEXIT::arg, atexit_callback(), atexit_callback_setup, ereport, errcode(), errmsg_internal(), FATAL, ONEXIT::function, MAX_ON_EXITS, on_proc_exit_index, and on_proc_exit_list.

Referenced by CreateLockFile(), InitCatCache(), InitFileAccess(), PostgresMain(), PostmasterMain(), pq_init(), select_perl_context(), sepgsql_avc_init(), and smgrinit().

293 {
295  ereport(FATAL,
296  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
297  errmsg_internal("out of on_proc_exit slots")));
298 
301 
303 
305  {
306  atexit(atexit_callback);
307  atexit_callback_setup = true;
308  }
309 }
static struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
Definition: ipc.c:74
#define MAX_ON_EXITS
Definition: ipc.c:66
int errcode(int sqlerrcode)
Definition: elog.c:575
pg_on_exit_callback function
Definition: ipc.c:70
static bool atexit_callback_setup
Definition: ipc.c:46
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
static int on_proc_exit_index
Definition: ipc.c:78
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
Datum arg
Definition: ipc.c:71
void * arg
static void atexit_callback(void)
Definition: ipc.c:277
void on_shmem_exit ( pg_on_exit_callback  function,
Datum  arg 
)

Definition at line 348 of file ipc.c.

References arg, ONEXIT::arg, atexit_callback(), atexit_callback_setup, ereport, errcode(), errmsg_internal(), FATAL, ONEXIT::function, MAX_ON_EXITS, on_shmem_exit_index, and on_shmem_exit_list.

Referenced by AutoVacWorkerMain(), dsm_postmaster_startup(), InitAuxiliaryProcess(), InitBufferPoolBackend(), InitPostgres(), InitProcess(), InitProcessPhase2(), InitWalSenderSlot(), InternalIpcMemoryCreate(), PGReserveSemaphores(), PGSharedMemoryCreate(), pgss_shmem_startup(), pgstat_initialize(), ProcSignalInit(), replorigin_session_setup(), SharedInvalBackendInit(), and WalReceiverMain().

349 {
351  ereport(FATAL,
352  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
353  errmsg_internal("out of on_shmem_exit slots")));
354 
357 
359 
361  {
362  atexit(atexit_callback);
363  atexit_callback_setup = true;
364  }
365 }
#define MAX_ON_EXITS
Definition: ipc.c:66
int errcode(int sqlerrcode)
Definition: elog.c:575
static int on_shmem_exit_index
Definition: ipc.c:78
pg_on_exit_callback function
Definition: ipc.c:70
static bool atexit_callback_setup
Definition: ipc.c:46
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
static struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:75
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
Datum arg
Definition: ipc.c:71
void * arg
static void atexit_callback(void)
Definition: ipc.c:277
void proc_exit ( int  code)

Definition at line 99 of file ipc.c.

References DEBUG3, elog, IsAutoVacuumWorkerProcess(), mkdir, proc_exit_prepare(), S_IRWXG, S_IRWXO, and snprintf().

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), auth_failed(), AutoVacLauncherMain(), AutoVacWorkerMain(), AuxiliaryProcessMain(), BackendInitialize(), BackgroundWriterMain(), BootstrapModeMain(), CheckerModeMain(), CheckpointerMain(), copy_read_data(), errfinish(), ExitPostmaster(), HandleStartupProcInterrupts(), logicalrep_worker_stop(), LogicalRepApplyLoop(), maybe_reread_subscription(), pg_attribute_noreturn(), pgstat_bestart(), PostgresMain(), PreRestoreCommand(), ProcessInterrupts(), ProcessRepliesIfAny(), ProcessStandbyMessage(), RestoreArchivedFile(), StartBackgroundWorker(), StartLogicalReplication(), StartReplication(), startup_die(), StartupPacketTimeoutHandler(), StartupProcessMain(), StartupProcShutdownHandler(), StartupXLOG(), SysLoggerMain(), test_shm_mq_main(), wait_for_relation_state_change(), wait_for_worker_state_change(), WaitForReplicationWorkerAttach(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndDone(), WalSndErrorCleanup(), and WalWriterMain().

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 
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 }
#define DEBUG3
Definition: elog.h:23
#define mkdir(a, b)
Definition: win32.h:57
static void proc_exit_prepare(int code)
Definition: ipc.c:152
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3361
#define S_IRWXO
Definition: win32.h:455
#define S_IRWXG
Definition: win32.h:451
#define elog
Definition: elog.h:219
static void proc_exit_prepare ( int  code)
static

Definition at line 152 of file ipc.c.

References arg, CritSectionCount, DEBUG3, debug_query_string, elog, error_context_stack, ONEXIT::function, InterruptHoldoffCount, InterruptPending, NULL, on_proc_exit_index, on_proc_exit_list, proc_exit_inprogress, ProcDiePending, QueryCancelPending, and shmem_exit().

Referenced by atexit_callback(), and proc_exit().

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;
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  */
181  /* For the same reason, reset debug_query_string before it's clobbered */
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)
202 
203  on_proc_exit_index = 0;
204 }
volatile uint32 InterruptHoldoffCount
Definition: globals.c:35
static struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
Definition: ipc.c:74
#define DEBUG3
Definition: elog.h:23
void shmem_exit(int code)
Definition: ipc.c:215
volatile bool QueryCancelPending
Definition: globals.c:30
ErrorContextCallback * error_context_stack
Definition: elog.c:88
pg_on_exit_callback function
Definition: ipc.c:70
volatile uint32 CritSectionCount
Definition: globals.c:37
const char * debug_query_string
Definition: postgres.c:85
static int on_proc_exit_index
Definition: ipc.c:78
volatile bool InterruptPending
Definition: globals.c:29
#define NULL
Definition: c.h:229
bool proc_exit_inprogress
Definition: ipc.c:40
volatile bool ProcDiePending
Definition: globals.c:31
void * arg
#define elog
Definition: elog.h:219
void shmem_exit ( int  code)

Definition at line 215 of file ipc.c.

References arg, before_shmem_exit_index, before_shmem_exit_list, DEBUG3, dsm_backend_shutdown(), elog, ONEXIT::function, on_shmem_exit_index, and on_shmem_exit_list.

Referenced by PostmasterStateMachine(), and proc_exit_prepare().

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",
227  while (--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  */
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)
264 }
#define DEBUG3
Definition: elog.h:23
static int on_shmem_exit_index
Definition: ipc.c:78
pg_on_exit_callback function
Definition: ipc.c:70
void dsm_backend_shutdown(void)
Definition: dsm.c:648
static struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:75
static int before_shmem_exit_index
Definition: ipc.c:78
void * arg
static struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
Definition: ipc.c:76
#define elog
Definition: elog.h:219

Variable Documentation

bool atexit_callback_setup = false
static

Definition at line 46 of file ipc.c.

Referenced by before_shmem_exit(), on_proc_exit(), and on_shmem_exit().

int before_shmem_exit_index
static

Definition at line 78 of file ipc.c.

Referenced by before_shmem_exit(), cancel_before_shmem_exit(), on_exit_reset(), and shmem_exit().

struct ONEXIT before_shmem_exit_list[MAX_ON_EXITS]
static

Definition at line 76 of file ipc.c.

Referenced by before_shmem_exit(), cancel_before_shmem_exit(), and shmem_exit().

int on_proc_exit_index
static

Definition at line 78 of file ipc.c.

Referenced by on_exit_reset(), on_proc_exit(), and proc_exit_prepare().

struct ONEXIT on_proc_exit_list[MAX_ON_EXITS]
static

Definition at line 74 of file ipc.c.

Referenced by on_proc_exit(), and proc_exit_prepare().

int on_shmem_exit_index
static

Definition at line 78 of file ipc.c.

Referenced by on_exit_reset(), on_shmem_exit(), and shmem_exit().

struct ONEXIT on_shmem_exit_list[MAX_ON_EXITS]
static

Definition at line 75 of file ipc.c.

Referenced by on_shmem_exit(), and shmem_exit().