PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
startup.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * startup.c
4  *
5  * The Startup process initialises the server and performs any recovery
6  * actions that have been specified. Notice that there is no "main loop"
7  * since the Startup process ends as soon as initialisation is complete.
8  * (in standby mode, one can think of the replay loop as a main loop,
9  * though.)
10  *
11  *
12  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
13  *
14  *
15  * IDENTIFICATION
16  * src/backend/postmaster/startup.c
17  *
18  *-------------------------------------------------------------------------
19  */
20 #include "postgres.h"
21 
22 #include <signal.h>
23 #include <unistd.h>
24 
25 #include "access/xlog.h"
26 #include "libpq/pqsignal.h"
27 #include "miscadmin.h"
28 #include "postmaster/startup.h"
29 #include "storage/ipc.h"
30 #include "storage/latch.h"
31 #include "storage/pmsignal.h"
32 #include "storage/standby.h"
33 #include "utils/guc.h"
34 #include "utils/timeout.h"
35 
36 
37 /*
38  * Flags set by interrupt handlers for later service in the redo loop.
39  */
40 static volatile sig_atomic_t got_SIGHUP = false;
41 static volatile sig_atomic_t shutdown_requested = false;
42 static volatile sig_atomic_t promote_triggered = false;
43 
44 /*
45  * Flag set when executing a restore command, to tell SIGTERM signal handler
46  * that it's safe to just proc_exit.
47  */
48 static volatile sig_atomic_t in_restore_command = false;
49 
50 /* Signal handlers */
55 
56 
57 /* --------------------------------
58  * signal handler routines
59  * --------------------------------
60  */
61 
62 /*
63  * startupproc_quickdie() occurs when signalled SIGQUIT by the postmaster.
64  *
65  * Some backend has bought the farm,
66  * so we need to stop what we're doing and exit.
67  */
68 static void
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 }
93 
94 
95 /* SIGUSR1: let latch facility handle the signal */
96 static void
98 {
99  int save_errno = errno;
100 
102 
103  errno = save_errno;
104 }
105 
106 /* SIGUSR2: set flag to finish recovery */
107 static void
109 {
110  int save_errno = errno;
111 
112  promote_triggered = true;
113  WakeupRecovery();
114 
115  errno = save_errno;
116 }
117 
118 /* SIGHUP: set flag to re-read config file at next convenient time */
119 static void
121 {
122  int save_errno = errno;
123 
124  got_SIGHUP = true;
125  WakeupRecovery();
126 
127  errno = save_errno;
128 }
129 
130 /* SIGTERM: set flag to abort redo and exit */
131 static void
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 }
144 
145 /* Handle SIGHUP and SIGTERM signals of startup process */
146 void
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 }
171 
172 
173 /* ----------------------------------
174  * Startup Process main entry point
175  * ----------------------------------
176  */
177 void
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 }
224 
225 void
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 }
238 
239 void
241 {
242  in_restore_command = false;
243 }
244 
245 bool
247 {
248  return promote_triggered;
249 }
250 
251 void
253 {
254  promote_triggered = false;
255 }
void InitializeTimeouts(void)
Definition: timeout.c:340
#define SIGUSR1
Definition: win32.h:211
void ResetPromoteTriggered(void)
Definition: startup.c:252
void StandbyTimeoutHandler(void)
Definition: standby.c:558
static void startupproc_quickdie(SIGNAL_ARGS)
Definition: startup.c:69
#define SIGCONT
Definition: win32.h:205
void ProcessConfigFile(GucContext context)
#define SIGWINCH
Definition: win32.h:209
void StartupProcessMain(void)
Definition: startup.c:178
#define SIGTTIN
Definition: win32.h:207
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:99
#define SIGQUIT
Definition: win32.h:197
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:40
#define SIG_IGN
Definition: win32.h:193
void PostRestoreCommand(void)
Definition: startup.c:240
void on_exit_reset(void)
Definition: ipc.c:396
void WakeupRecovery(void)
Definition: xlog.c:11975
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48
static volatile sig_atomic_t promote_triggered
Definition: startup.c:42
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:120
bool IsUnderPostmaster
Definition: globals.c:100
bool PostmasterIsAlive(void)
Definition: pmsignal.c:272
void StandbyLockTimeoutHandler(void)
Definition: standby.c:571
void StandbyDeadLockHandler(void)
Definition: standby.c:547
bool IsPromoteTriggered(void)
Definition: startup.c:246
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void StartupProcSigUsr1Handler(SIGNAL_ARGS)
Definition: startup.c:97
void StartupXLOG(void)
Definition: xlog.c:6132
Definition: guc.h:72
sigset_t BlockSig
Definition: pqsignal.c:22
#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
#define SIGNAL_ARGS
Definition: c.h:1079
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:132
#define SIGTTOU
Definition: win32.h:208
void PreRestoreCommand(void)
Definition: startup.c:226
void HandleStartupProcInterrupts(void)
Definition: startup.c:147
void latch_sigusr1_handler(void)
Definition: latch.c:1540
#define SIGCHLD
Definition: win32.h:206
#define SIGUSR2
Definition: win32.h:212