PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
startup.c File Reference
#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include "access/xlog.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "postmaster/startup.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pmsignal.h"
#include "storage/standby.h"
#include "utils/guc.h"
#include "utils/timeout.h"
Include dependency graph for startup.c:

Go to the source code of this file.

Functions

static void startupproc_quickdie (SIGNAL_ARGS)
 
static void StartupProcSigUsr1Handler (SIGNAL_ARGS)
 
static void StartupProcTriggerHandler (SIGNAL_ARGS)
 
static void StartupProcSigHupHandler (SIGNAL_ARGS)
 
static void StartupProcShutdownHandler (SIGNAL_ARGS)
 
void HandleStartupProcInterrupts (void)
 
void StartupProcessMain (void)
 
void PreRestoreCommand (void)
 
void PostRestoreCommand (void)
 
bool IsPromoteTriggered (void)
 
void ResetPromoteTriggered (void)
 

Variables

static volatile sig_atomic_t got_SIGHUP = false
 
static volatile sig_atomic_t shutdown_requested = false
 
static volatile sig_atomic_t promote_triggered = false
 
static volatile sig_atomic_t in_restore_command = false
 

Function Documentation

void HandleStartupProcInterrupts ( void  )

Definition at line 147 of file startup.c.

References got_SIGHUP, IsUnderPostmaster, PGC_SIGHUP, PostmasterIsAlive(), proc_exit(), ProcessConfigFile(), and shutdown_requested.

Referenced by recoveryApplyDelay(), recoveryPausesHere(), ShutdownWalRcv(), StartupXLOG(), and WaitForWALToBecomeAvailable().

148 {
149  /*
150  * Check if we were requested to re-read config file.
151  */
152  if (got_SIGHUP)
153  {
154  got_SIGHUP = false;
156  }
157 
158  /*
159  * Check if we were requested to exit without finishing recovery.
160  */
161  if (shutdown_requested)
162  proc_exit(1);
163 
164  /*
165  * Emergency bailout if postmaster has died. This is to avoid the
166  * necessity for manual cleanup of all postmaster children.
167  */
169  exit(1);
170 }
void ProcessConfigFile(GucContext context)
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:99
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:40
bool IsUnderPostmaster
Definition: globals.c:100
bool PostmasterIsAlive(void)
Definition: pmsignal.c:272
Definition: guc.h:72
bool IsPromoteTriggered ( void  )

Definition at line 246 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

247 {
248  return promote_triggered;
249 }
static volatile sig_atomic_t promote_triggered
Definition: startup.c:42
void PostRestoreCommand ( void  )

Definition at line 240 of file startup.c.

References in_restore_command.

Referenced by RestoreArchivedFile().

241 {
242  in_restore_command = false;
243 }
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48
void PreRestoreCommand ( void  )

Definition at line 226 of file startup.c.

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

227 {
228  /*
229  * Set in_restore_command to tell the signal handler that we should exit
230  * right away on SIGTERM. We know that we're at a safe point to do that.
231  * Check if we had already received the signal, so that we don't miss a
232  * shutdown request received just before this.
233  */
234  in_restore_command = true;
235  if (shutdown_requested)
236  proc_exit(1);
237 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:99
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48
void ResetPromoteTriggered ( void  )

Definition at line 252 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

253 {
254  promote_triggered = false;
255 }
static volatile sig_atomic_t promote_triggered
Definition: startup.c:42
static void startupproc_quickdie ( SIGNAL_ARGS  )
static

Definition at line 69 of file startup.c.

References BlockSig, on_exit_reset(), and PG_SETMASK.

Referenced by StartupProcessMain().

70 {
72 
73  /*
74  * We DO NOT want to run proc_exit() callbacks -- we're here because
75  * shared memory may be corrupted, so we don't want to try to clean up our
76  * transaction. Just nail the windows shut and get out of town. Now that
77  * there's an atexit callback to prevent third-party code from breaking
78  * things by calling exit() directly, we have to reset the callbacks
79  * explicitly to make this work as intended.
80  */
81  on_exit_reset();
82 
83  /*
84  * Note we do exit(2) not exit(0). This is to force the postmaster into a
85  * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
86  * backend. This is necessary precisely because we don't clean up our
87  * shared memory state. (The "dead man switch" mechanism in pmsignal.c
88  * should ensure the postmaster sees this as a crash, too, but no harm in
89  * being doubly sure.)
90  */
91  exit(2);
92 }
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void on_exit_reset(void)
Definition: ipc.c:396
sigset_t BlockSig
Definition: pqsignal.c:22
void StartupProcessMain ( void  )

Definition at line 178 of file startup.c.

References InitializeTimeouts(), PG_SETMASK, pqsignal(), proc_exit(), RegisterTimeout(), SIG_DFL, SIG_IGN, SIGCHLD, SIGCONT, SIGHUP, SIGPIPE, SIGQUIT, SIGTTIN, SIGTTOU, SIGUSR1, SIGUSR2, SIGWINCH, STANDBY_DEADLOCK_TIMEOUT, STANDBY_LOCK_TIMEOUT, STANDBY_TIMEOUT, StandbyDeadLockHandler(), StandbyLockTimeoutHandler(), StandbyTimeoutHandler(), startupproc_quickdie(), StartupProcShutdownHandler(), StartupProcSigHupHandler(), StartupProcSigUsr1Handler(), StartupProcTriggerHandler(), StartupXLOG(), and UnBlockSig.

Referenced by AuxiliaryProcessMain().

179 {
180  /*
181  * Properly accept or ignore signals the postmaster might send us.
182  */
183  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
184  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
185  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
186  pqsignal(SIGQUIT, startupproc_quickdie); /* hard crash time */
187  InitializeTimeouts(); /* establishes SIGALRM handler */
191 
192  /*
193  * Reset some signals that are accepted by postmaster but not here
194  */
200 
201  /*
202  * Register timeouts needed for standby mode
203  */
207 
208  /*
209  * Unblock signals (they were blocked when the postmaster forked us)
210  */
212 
213  /*
214  * Do what we came for.
215  */
216  StartupXLOG();
217 
218  /*
219  * Exit normally. Exit code 0 tells postmaster that we completed recovery
220  * successfully.
221  */
222  proc_exit(0);
223 }
void InitializeTimeouts(void)
Definition: timeout.c:340
#define SIGUSR1
Definition: win32.h:211
void StandbyTimeoutHandler(void)
Definition: standby.c:558
static void startupproc_quickdie(SIGNAL_ARGS)
Definition: startup.c:69
#define SIGCONT
Definition: win32.h:205
#define SIGWINCH
Definition: win32.h:209
#define SIGTTIN
Definition: win32.h:207
void proc_exit(int code)
Definition: ipc.c:99
#define SIGQUIT
Definition: win32.h:197
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
#define SIG_IGN
Definition: win32.h:193
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:120
void StandbyLockTimeoutHandler(void)
Definition: standby.c:571
void StandbyDeadLockHandler(void)
Definition: standby.c:547
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void StartupProcSigUsr1Handler(SIGNAL_ARGS)
Definition: startup.c:97
void StartupXLOG(void)
Definition: xlog.c:6182
#define SIGPIPE
Definition: win32.h:201
#define SIGHUP
Definition: win32.h:196
#define SIG_DFL
Definition: win32.h:191
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:373
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:108
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:132
#define SIGTTOU
Definition: win32.h:208
#define SIGCHLD
Definition: win32.h:206
#define SIGUSR2
Definition: win32.h:212
static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 132 of file startup.c.

References in_restore_command, proc_exit(), shutdown_requested, and WakeupRecovery().

Referenced by StartupProcessMain().

133 {
134  int save_errno = errno;
135 
136  if (in_restore_command)
137  proc_exit(1);
138  else
139  shutdown_requested = true;
140  WakeupRecovery();
141 
142  errno = save_errno;
143 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:99
void WakeupRecovery(void)
Definition: xlog.c:12030
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48
static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 120 of file startup.c.

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

121 {
122  int save_errno = errno;
123 
124  got_SIGHUP = true;
125  WakeupRecovery();
126 
127  errno = save_errno;
128 }
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:40
void WakeupRecovery(void)
Definition: xlog.c:12030
static void StartupProcSigUsr1Handler ( SIGNAL_ARGS  )
static

Definition at line 97 of file startup.c.

References latch_sigusr1_handler().

Referenced by StartupProcessMain().

98 {
99  int save_errno = errno;
100 
102 
103  errno = save_errno;
104 }
void latch_sigusr1_handler(void)
Definition: latch.c:1572
static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 108 of file startup.c.

References promote_triggered, and WakeupRecovery().

Referenced by StartupProcessMain().

109 {
110  int save_errno = errno;
111 
112  promote_triggered = true;
113  WakeupRecovery();
114 
115  errno = save_errno;
116 }
void WakeupRecovery(void)
Definition: xlog.c:12030
static volatile sig_atomic_t promote_triggered
Definition: startup.c:42

Variable Documentation

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 40 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

volatile sig_atomic_t in_restore_command = false
static

Definition at line 48 of file startup.c.

Referenced by PostRestoreCommand(), PreRestoreCommand(), and StartupProcShutdownHandler().

volatile sig_atomic_t promote_triggered = false
static

Definition at line 42 of file startup.c.

Referenced by IsPromoteTriggered(), ResetPromoteTriggered(), and StartupProcTriggerHandler().

volatile sig_atomic_t shutdown_requested = false
static