1 /*-------------------------------------------------------------------------
2  *
3  * postmaster.c
4  *	  This program acts as a clearing house for requests to the
5  *	  POSTGRES system.  Frontend programs send a startup message
6  *	  to the Postmaster and the postmaster uses the info in the
7  *	  message to setup a backend process.
8  *
9  *	  The postmaster also manages system-wide operations such as
10  *	  startup and shutdown. The postmaster itself doesn't do those
11  *	  operations, mind you --- it just forks off a subprocess to do them
12  *	  at the right times.  It also takes care of resetting the system
13  *	  if a backend crashes.
14  *
15  *	  The postmaster process creates the shared memory and semaphore
16  *	  pools during startup, but as a rule does not touch them itself.
17  *	  In particular, it is not a member of the PGPROC array of backends
18  *	  and so it cannot participate in lock-manager operations.  Keeping
19  *	  the postmaster away from shared memory operations makes it simpler
20  *	  and more reliable.  The postmaster is almost always able to recover
21  *	  from crashes of individual backends by resetting shared memory;
22  *	  if it did much with shared memory then it would be prone to crashing
23  *	  along with the backends.
24  *
25  *	  When a request message is received, we now fork() immediately.
26  *	  The child process performs authentication of the request, and
27  *	  then becomes a backend if successful.  This allows the auth code
28  *	  to be written in a simple single-threaded style (as opposed to the
29  *	  crufty "poor man's multitasking" code that used to be needed).
30  *	  More importantly, it ensures that blockages in non-multithreaded
31  *	  libraries like SSL or PAM cannot cause denial of service to other
32  *	  clients.
33  *
34  *
35  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
36  * Portions Copyright (c) 1994, Regents of the University of California
37  *
38  *
39  * IDENTIFICATION
40  *	  src/backend/postmaster/postmaster.c
41  *
42  * NOTES
43  *
44  * Initialization:
45  *		The Postmaster sets up shared memory data structures
46  *		for the backends.
47  *
48  * Synchronization:
49  *		The Postmaster shares memory with the backends but should avoid
50  *		touching shared memory, so as not to become stuck if a crashing
51  *		backend screws up locks or shared memory.  Likewise, the Postmaster
52  *		should never block on messages from frontend clients.
53  *
54  * Garbage Collection:
55  *		The Postmaster cleans up after backends if they have an emergency
56  *		exit and/or core dump.
57  *
58  * Error Reporting:
59  *		Use write_stderr() only for reporting "interactive" errors
60  *		(essentially, bogus arguments on the command line).  Once the
61  *		postmaster is launched, use ereport().
62  *
63  *-------------------------------------------------------------------------
64  */
65 
66 #include "postgres.h"
67 
68 #include <unistd.h>
69 #include <signal.h>
70 #include <time.h>
71 #include <sys/wait.h>
72 #include <ctype.h>
73 #include <sys/stat.h>
74 #include <sys/socket.h>
75 #include <fcntl.h>
76 #include <sys/param.h>
77 #include <netinet/in.h>
78 #include <arpa/inet.h>
79 #include <netdb.h>
80 #include <limits.h>
81 
82 #ifdef HAVE_SYS_SELECT_H
83 #include <sys/select.h>
84 #endif
85 
86 #ifdef USE_BONJOUR
87 #include <dns_sd.h>
88 #endif
89 
90 #ifdef USE_SYSTEMD
91 #include <systemd/sd-daemon.h>
92 #endif
93 
94 #ifdef HAVE_PTHREAD_IS_THREADED_NP
95 #include <pthread.h>
96 #endif
97 
98 #include "access/transam.h"
99 #include "access/xlog.h"
100 #include "bootstrap/bootstrap.h"
101 #include "catalog/pg_control.h"
102 #include "lib/ilist.h"
103 #include "libpq/auth.h"
104 #include "libpq/ip.h"
105 #include "libpq/libpq.h"
106 #include "libpq/pqformat.h"
107 #include "libpq/pqsignal.h"
108 #include "miscadmin.h"
109 #include "pg_getopt.h"
110 #include "pgstat.h"
111 #include "postmaster/autovacuum.h"
112 #include "postmaster/bgworker_internals.h"
113 #include "postmaster/fork_process.h"
114 #include "postmaster/pgarch.h"
115 #include "postmaster/postmaster.h"
116 #include "postmaster/syslogger.h"
117 #include "replication/walsender.h"
118 #include "storage/fd.h"
119 #include "storage/ipc.h"
120 #include "storage/pg_shmem.h"
121 #include "storage/pmsignal.h"
122 #include "storage/proc.h"
123 #include "tcop/tcopprot.h"
124 #include "utils/builtins.h"
125 #include "utils/datetime.h"
126 #include "utils/dynamic_loader.h"
127 #include "utils/memutils.h"
128 #include "utils/ps_status.h"
129 #include "utils/timeout.h"
130 
131 #ifdef EXEC_BACKEND
132 #include "storage/spin.h"
133 #endif
134 
135 
136 /*
137  * Possible types of a backend. Beyond being the possible bkend_type values in
138  * struct bkend, these are OR-able request flag bits for SignalSomeChildren()
139  * and CountChildren().
140  */
141 #define BACKEND_TYPE_NORMAL		0x0001	/* normal backend */
142 #define BACKEND_TYPE_AUTOVAC	0x0002	/* autovacuum worker process */
143 #define BACKEND_TYPE_WALSND		0x0004	/* walsender process */
144 #define BACKEND_TYPE_BGWORKER	0x0008	/* bgworker process */
145 #define BACKEND_TYPE_ALL		0x000F	/* OR of all the above */
146 
147 /*
148  * List of active backends (or child processes anyway; we don't actually
149  * know whether a given child has become a backend or is still in the
150  * authorization phase).  This is used mainly to keep track of how many
151  * children we have and send them appropriate signals when necessary.
152  *
153  * "Special" children such as the startup, bgwriter and autovacuum launcher
154  * tasks are not in this list.  Autovacuum worker and walsender are in it.
155  * Also, "dead_end" children are in it: these are children launched just for
156  * the purpose of sending a friendly rejection message to a would-be client.
157  * We must track them because they are attached to shared memory, but we know
158  * they will never become live backends.  dead_end children are not assigned a
159  * PMChildSlot.
160  *
161  * Background workers are in this list, too.
162  */
163 typedef struct bkend
164 {
165 	pid_t		pid;			/* process id of backend */
166 	long		cancel_key;		/* cancel key for cancels for this backend */
167 	int			child_slot;		/* PMChildSlot for this backend, if any */
168 
169 	/*
170 	 * Flavor of backend or auxiliary process.  Note that BACKEND_TYPE_WALSND
171 	 * backends initially announce themselves as BACKEND_TYPE_NORMAL, so if
172 	 * bkend_type is normal, you should check for a recent transition.
173 	 */
174 	int			bkend_type;
175 	bool		dead_end;		/* is it going to send an error and quit? */
176 	bool		bgworker_notify;	/* gets bgworker start/stop notifications */
177 	dlist_node	elem;			/* list link in BackendList */
178 } Backend;
179 
180 static dlist_head BackendList = DLIST_STATIC_INIT(BackendList);
181 
182 #ifdef EXEC_BACKEND
183 static Backend *ShmemBackendArray;
184 #endif
185 
186 BackgroundWorker *MyBgworkerEntry = NULL;
187 
188 
189 
190 /* The socket number we are listening for connections on */
191 int			PostPortNumber;
192 
193 /* The directory names for Unix socket(s) */
194 char	   *Unix_socket_directories;
195 
196 /* The TCP listen address(es) */
197 char	   *ListenAddresses;
198 
199 /*
200  * ReservedBackends is the number of backends reserved for superuser use.
201  * This number is taken out of the pool size given by MaxBackends so
202  * number of backend slots available to non-superusers is
203  * (MaxBackends - ReservedBackends).  Note what this really means is
204  * "if there are <= ReservedBackends connections available, only superusers
205  * can make new connections" --- pre-existing superuser connections don't
206  * count against the limit.
207  */
208 int			ReservedBackends;
209 
210 /* The socket(s) we're listening to. */
211 #define MAXLISTEN	64
212 static pgsocket ListenSocket[MAXLISTEN];
213 
214 /*
215  * Set by the -o option
216  */
217 static char ExtraOptions[MAXPGPATH];
218 
219 /*
220  * These globals control the behavior of the postmaster in case some
221  * backend dumps core.  Normally, it kills all peers of the dead backend
222  * and reinitializes shared memory.  By specifying -s or -n, we can have
223  * the postmaster stop (rather than kill) peers and not reinitialize
224  * shared data structures.  (Reinit is currently dead code, though.)
225  */
226 static bool Reinit = true;
227 static int	SendStop = false;
228 
229 /* still more option variables */
230 bool		EnableSSL = false;
231 
232 int			PreAuthDelay = 0;
233 int			AuthenticationTimeout = 60;
234 
235 bool		log_hostname;		/* for ps display and logging */
236 bool		Log_connections = false;
237 bool		Db_user_namespace = false;
238 
239 bool		enable_bonjour = false;
240 char	   *bonjour_name;
241 bool		restart_after_crash = true;
242 
243 /* PIDs of special child processes; 0 when not running */
244 static pid_t StartupPID = 0,
245 			BgWriterPID = 0,
246 			CheckpointerPID = 0,
247 			WalWriterPID = 0,
248 			WalReceiverPID = 0,
249 			AutoVacPID = 0,
250 			PgArchPID = 0,
251 			PgStatPID = 0,
252 			SysLoggerPID = 0;
253 
254 /* Startup process's status */
255 typedef enum
256 {
257 	STARTUP_NOT_RUNNING,
258 	STARTUP_RUNNING,
259 	STARTUP_SIGNALED,			/* we sent it a SIGQUIT or SIGKILL */
260 	STARTUP_CRASHED
261 } StartupStatusEnum;
262 
263 static StartupStatusEnum StartupStatus = STARTUP_NOT_RUNNING;
264 
265 /* Startup/shutdown state */
266 #define			NoShutdown		0
267 #define			SmartShutdown	1
268 #define			FastShutdown	2
269 #define			ImmediateShutdown	3
270 
271 static int	Shutdown = NoShutdown;
272 
273 static bool FatalError = false; /* T if recovering from backend crash */
274 
275 /*
276  * We use a simple state machine to control startup, shutdown, and
277  * crash recovery (which is rather like shutdown followed by startup).
278  *
279  * After doing all the postmaster initialization work, we enter PM_STARTUP
280  * state and the startup process is launched. The startup process begins by
281  * reading the control file and other preliminary initialization steps.
282  * In a normal startup, or after crash recovery, the startup process exits
283  * with exit code 0 and we switch to PM_RUN state.  However, archive recovery
284  * is handled specially since it takes much longer and we would like to support
285  * hot standby during archive recovery.
286  *
287  * When the startup process is ready to start archive recovery, it signals the
288  * postmaster, and we switch to PM_RECOVERY state. The background writer and
289  * checkpointer are launched, while the startup process continues applying WAL.
290  * If Hot Standby is enabled, then, after reaching a consistent point in WAL
291  * redo, startup process signals us again, and we switch to PM_HOT_STANDBY
292  * state and begin accepting connections to perform read-only queries.  When
293  * archive recovery is finished, the startup process exits with exit code 0
294  * and we switch to PM_RUN state.
295  *
296  * Normal child backends can only be launched when we are in PM_RUN or
297  * PM_HOT_STANDBY state.  (connsAllowed can also restrict launching.)
298  * In other states we handle connection requests by launching "dead_end"
299  * child processes, which will simply send the client an error message and
300  * quit.  (We track these in the BackendList so that we can know when they
301  * are all gone; this is important because they're still connected to shared
302  * memory, and would interfere with an attempt to destroy the shmem segment,
303  * possibly leading to SHMALL failure when we try to make a new one.)
304  * In PM_WAIT_DEAD_END state we are waiting for all the dead_end children
305  * to drain out of the system, and therefore stop accepting connection
306  * requests at all until the last existing child has quit (which hopefully
307  * will not be very long).
308  *
309  * Notice that this state variable does not distinguish *why* we entered
310  * states later than PM_RUN --- Shutdown and FatalError must be consulted
311  * to find that out.  FatalError is never true in PM_RECOVERY, PM_HOT_STANDBY,
312  * or PM_RUN states, nor in PM_SHUTDOWN states (because we don't enter those
313  * states when trying to recover from a crash).  It can be true in PM_STARTUP
314  * state, because we don't clear it until we've successfully started WAL redo.
315  */
316 typedef enum
317 {
318 	PM_INIT,					/* postmaster starting */
319 	PM_STARTUP,					/* waiting for startup subprocess */
320 	PM_RECOVERY,				/* in archive recovery mode */
321 	PM_HOT_STANDBY,				/* in hot standby mode */
322 	PM_RUN,						/* normal "database is alive" state */
323 	PM_STOP_BACKENDS,			/* need to stop remaining backends */
324 	PM_WAIT_BACKENDS,			/* waiting for live backends to exit */
325 	PM_SHUTDOWN,				/* waiting for checkpointer to do shutdown
326 								 * ckpt */
327 	PM_SHUTDOWN_2,				/* waiting for archiver and walsenders to
328 								 * finish */
329 	PM_WAIT_DEAD_END,			/* waiting for dead_end children to exit */
330 	PM_NO_CHILDREN				/* all important children have exited */
331 } PMState;
332 
333 static PMState pmState = PM_INIT;
334 
335 /*
336  * While performing a "smart shutdown", we restrict new connections but stay
337  * in PM_RUN or PM_HOT_STANDBY state until all the client backends are gone.
338  * connsAllowed is a sub-state indicator showing the active restriction.
339  * It is of no interest unless pmState is PM_RUN or PM_HOT_STANDBY.
340  */
341 typedef enum
342 {
343 	ALLOW_ALL_CONNS,			/* normal not-shutting-down state */
344 	ALLOW_SUPERUSER_CONNS,		/* only superusers can connect */
345 	ALLOW_NO_CONNS				/* no new connections allowed, period */
346 } ConnsAllowedState;
347 
348 static ConnsAllowedState connsAllowed = ALLOW_ALL_CONNS;
349 
350 /* Start time of SIGKILL timeout during immediate shutdown or child crash */
351 /* Zero means timeout is not running */
352 static time_t AbortStartTime = 0;
353 /* Length of said timeout */
354 #define SIGKILL_CHILDREN_AFTER_SECS		5
355 
356 static bool ReachedNormalRunning = false;		/* T if we've reached PM_RUN */
357 
358 bool		ClientAuthInProgress = false;		/* T during new-client
359 												 * authentication */
360 
361 bool		redirection_done = false;	/* stderr redirected for syslogger? */
362 
363 /* received START_AUTOVAC_LAUNCHER signal */
364 static volatile sig_atomic_t start_autovac_launcher = false;
365 
366 /* the launcher needs to be signalled to communicate some condition */
367 static volatile bool avlauncher_needs_signal = false;
368 
369 /* received START_WALRECEIVER signal */
370 static volatile sig_atomic_t WalReceiverRequested = false;
371 
372 /* set when there's a worker that needs to be started up */
373 static volatile bool StartWorkerNeeded = true;
374 static volatile bool HaveCrashedWorker = false;
375 
376 /*
377  * State for assigning random salts and cancel keys.
378  * Also, the global MyCancelKey passes the cancel key assigned to a given
379  * backend from the postmaster to that backend (via fork).
380  */
381 static unsigned int random_seed = 0;
382 static struct timeval random_start_time;
383 
384 #ifdef USE_BONJOUR
385 static DNSServiceRef bonjour_sdref = NULL;
386 #endif
387 
388 /*
389  * postmaster.c - function prototypes
390  */
391 static void CloseServerPorts(int status, Datum arg);
392 static void unlink_external_pid_file(int status, Datum arg);
393 static void getInstallationPaths(const char *argv0);
394 static void checkDataDir(void);
395 static Port *ConnCreate(int serverFd);
396 static void ConnFree(Port *port);
397 static void reset_shared(int port);
398 static void SIGHUP_handler(SIGNAL_ARGS);
399 static void pmdie(SIGNAL_ARGS);
400 static void reaper(SIGNAL_ARGS);
401 static void sigusr1_handler(SIGNAL_ARGS);
402 static void process_startup_packet_die(SIGNAL_ARGS);
403 static void process_startup_packet_quickdie(SIGNAL_ARGS);
404 static void dummy_handler(SIGNAL_ARGS);
405 static void StartupPacketTimeoutHandler(void);
406 static void CleanupBackend(int pid, int exitstatus);
407 static bool CleanupBackgroundWorker(int pid, int exitstatus);
408 static void HandleChildCrash(int pid, int exitstatus, const char *procname);
409 static void LogChildExit(int lev, const char *procname,
410 			 int pid, int exitstatus);
411 static void PostmasterStateMachine(void);
412 static void BackendInitialize(Port *port);
413 static void BackendRun(Port *port) pg_attribute_noreturn();
414 static void ExitPostmaster(int status) pg_attribute_noreturn();
415 static int	ServerLoop(void);
416 static int	BackendStartup(Port *port);
417 static int	ProcessStartupPacket(Port *port, bool SSLdone);
418 static void SendNegotiateProtocolVersion(List *unrecognized_protocol_options);
419 static void processCancelRequest(Port *port, void *pkt);
420 static int	initMasks(fd_set *rmask);
421 static void report_fork_failure_to_client(Port *port, int errnum);
422 static CAC_state canAcceptConnections(int backend_type);
423 static long PostmasterRandom(void);
424 static void RandomSalt(char *md5Salt);
425 static void signal_child(pid_t pid, int signal);
426 static bool SignalSomeChildren(int signal, int targets);
427 static void TerminateChildren(int signal);
428 
429 #define SignalChildren(sig)			   SignalSomeChildren(sig, BACKEND_TYPE_ALL)
430 
431 static int	CountChildren(int target);
432 static bool assign_backendlist_entry(RegisteredBgWorker *rw);
433 static void maybe_start_bgworkers(void);
434 static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
435 static pid_t StartChildProcess(AuxProcType type);
436 static void StartAutovacuumWorker(void);
437 static void MaybeStartWalReceiver(void);
438 static void InitPostmasterDeathWatchHandle(void);
439 
440 /*
441  * Archiver is allowed to start up at the current postmaster state?
442  *
443  * If WAL archiving is enabled always, we are allowed to start archiver
444  * even during recovery.
445  */
446 #define PgArchStartupAllowed()	\
447 	((XLogArchivingActive() && pmState == PM_RUN) ||	\
448 	 (XLogArchivingAlways() &&	\
449 	  (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY)))
450 
451 #ifdef EXEC_BACKEND
452 
453 #ifdef WIN32
454 #define WNOHANG 0				/* ignored, so any integer value will do */
455 
456 static pid_t waitpid(pid_t pid, int *exitstatus, int options);
457 static void WINAPI pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
458 
459 static HANDLE win32ChildQueue;
460 
461 typedef struct
462 {
463 	HANDLE		waitHandle;
464 	HANDLE		procHandle;
465 	DWORD		procId;
466 } win32_deadchild_waitinfo;
467 #endif   /* WIN32 */
468 
469 static pid_t backend_forkexec(Port *port);
470 static pid_t internal_forkexec(int argc, char *argv[], Port *port);
471 
472 /* Type for a socket that can be inherited to a client process */
473 #ifdef WIN32
474 typedef struct
475 {
476 	SOCKET		origsocket;		/* Original socket value, or PGINVALID_SOCKET
477 								 * if not a socket */
478 	WSAPROTOCOL_INFO wsainfo;
479 } InheritableSocket;
480 #else
481 typedef int InheritableSocket;
482 #endif
483 
484 /*
485  * Structure contains all variables passed to exec:ed backends
486  */
487 typedef struct
488 {
489 	Port		port;
490 	InheritableSocket portsocket;
491 	char		DataDir[MAXPGPATH];
492 	pgsocket	ListenSocket[MAXLISTEN];
493 	long		MyCancelKey;
494 	int			MyPMChildSlot;
495 #ifndef WIN32
496 	unsigned long UsedShmemSegID;
497 #else
498 	void	   *ShmemProtectiveRegion;
499 	HANDLE		UsedShmemSegID;
500 #endif
501 	void	   *UsedShmemSegAddr;
502 	slock_t    *ShmemLock;
503 	VariableCache ShmemVariableCache;
504 	Backend    *ShmemBackendArray;
505 #ifndef HAVE_SPINLOCKS
506 	PGSemaphore SpinlockSemaArray;
507 #endif
508 	int			NamedLWLockTrancheRequests;
509 	NamedLWLockTranche *NamedLWLockTrancheArray;
510 	LWLockPadded *MainLWLockArray;
511 	slock_t    *ProcStructLock;
512 	PROC_HDR   *ProcGlobal;
513 	PGPROC	   *AuxiliaryProcs;
514 	PGPROC	   *PreparedXactProcs;
515 	PMSignalData *PMSignalState;
516 	InheritableSocket pgStatSock;
517 	pid_t		PostmasterPid;
518 	TimestampTz PgStartTime;
519 	TimestampTz PgReloadTime;
520 	pg_time_t	first_syslogger_file_time;
521 	bool		redirection_done;
522 	bool		IsBinaryUpgrade;
523 	int			max_safe_fds;
524 	int			MaxBackends;
525 #ifdef WIN32
526 	HANDLE		PostmasterHandle;
527 	HANDLE		initial_signal_pipe;
528 	HANDLE		syslogPipe[2];
529 #else
530 	int			postmaster_alive_fds[2];
531 	int			syslogPipe[2];
532 #endif
533 	char		my_exec_path[MAXPGPATH];
534 	char		pkglib_path[MAXPGPATH];
535 	char		ExtraOptions[MAXPGPATH];
536 } BackendParameters;
537 
538 static void read_backend_variables(char *id, Port *port);
539 static void restore_backend_variables(BackendParameters *param, Port *port);
540 
541 #ifndef WIN32
542 static bool save_backend_variables(BackendParameters *param, Port *port);
543 #else
544 static bool save_backend_variables(BackendParameters *param, Port *port,
545 					   HANDLE childProcess, pid_t childPid);
546 #endif
547 
548 static void ShmemBackendArrayAdd(Backend *bn);
549 static void ShmemBackendArrayRemove(Backend *bn);
550 #endif   /* EXEC_BACKEND */
551 
552 #define StartupDataBase()		StartChildProcess(StartupProcess)
553 #define StartBackgroundWriter() StartChildProcess(BgWriterProcess)
554 #define StartCheckpointer()		StartChildProcess(CheckpointerProcess)
555 #define StartWalWriter()		StartChildProcess(WalWriterProcess)
556 #define StartWalReceiver()		StartChildProcess(WalReceiverProcess)
557 
558 /* Macros to check exit status of a child process */
559 #define EXIT_STATUS_0(st)  ((st) == 0)
560 #define EXIT_STATUS_1(st)  (WIFEXITED(st) && WEXITSTATUS(st) == 1)
561 #define EXIT_STATUS_3(st)  (WIFEXITED(st) && WEXITSTATUS(st) == 3)
562 
563 #ifndef WIN32
564 /*
565  * File descriptors for pipe used to monitor if postmaster is alive.
566  * First is POSTMASTER_FD_WATCH, second is POSTMASTER_FD_OWN.
567  */
568 int			postmaster_alive_fds[2] = {-1, -1};
569 #else
570 /* Process handle of postmaster used for the same purpose on Windows */
571 HANDLE		PostmasterHandle;
572 #endif
573 
574 /*
575  * Postmaster main entry point
576  */
577 void
PostmasterMain(int argc,char * argv[])578 PostmasterMain(int argc, char *argv[])
579 {
580 	int			opt;
581 	int			status;
582 	char	   *userDoption = NULL;
583 	bool		listen_addr_saved = false;
584 	int			i;
585 	char	   *output_config_variable = NULL;
586 
587 	MyProcPid = PostmasterPid = getpid();
588 
589 	MyStartTime = time(NULL);
590 
591 	IsPostmasterEnvironment = true;
592 
593 	/*
594 	 * for security, no dir or file created can be group or other accessible
595 	 */
596 	umask(S_IRWXG | S_IRWXO);
597 
598 	/*
599 	 * Initialize random(3) so we don't get the same values in every run.
600 	 *
601 	 * Note: the seed is pretty predictable from externally-visible facts such
602 	 * as postmaster start time, so avoid using random() for security-critical
603 	 * random values during postmaster startup.  At the time of first
604 	 * connection, PostmasterRandom will select a hopefully-more-random seed.
605 	 */
606 	srandom((unsigned int) (MyProcPid ^ MyStartTime));
607 
608 	/*
609 	 * By default, palloc() requests in the postmaster will be allocated in
610 	 * the PostmasterContext, which is space that can be recycled by backends.
611 	 * Allocated data that needs to be available to backends should be
612 	 * allocated in TopMemoryContext.
613 	 */
614 	PostmasterContext = AllocSetContextCreate(TopMemoryContext,
615 											  "Postmaster",
616 											  ALLOCSET_DEFAULT_SIZES);
617 	MemoryContextSwitchTo(PostmasterContext);
618 
619 	/* Initialize paths to installation files */
620 	getInstallationPaths(argv[0]);
621 
622 	/*
623 	 * Set up signal handlers for the postmaster process.
624 	 *
625 	 * In the postmaster, we use pqsignal_pm() rather than pqsignal() (which
626 	 * is used by all child processes and client processes).  That has a
627 	 * couple of special behaviors:
628 	 *
629 	 * 1. Except on Windows, we tell sigaction() to block all signals for the
630 	 * duration of the signal handler.  This is faster than our old approach
631 	 * of blocking/unblocking explicitly in the signal handler, and it should
632 	 * also prevent excessive stack consumption if signals arrive quickly.
633 	 *
634 	 * 2. We do not set the SA_RESTART flag.  This is because signals will be
635 	 * blocked at all times except when ServerLoop is waiting for something to
636 	 * happen, and during that window, we want signals to exit the select(2)
637 	 * wait so that ServerLoop can respond if anything interesting happened.
638 	 * On some platforms, signals marked SA_RESTART would not cause the
639 	 * select() wait to end.
640 	 *
641 	 * Child processes will generally want SA_RESTART, so pqsignal() sets that
642 	 * flag.  We expect children to set up their own handlers before
643 	 * unblocking signals.
644 	 *
645 	 * CAUTION: when changing this list, check for side-effects on the signal
646 	 * handling setup of child processes.  See tcop/postgres.c,
647 	 * bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
648 	 * postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c,
649 	 * postmaster/syslogger.c, postmaster/bgworker.c and
650 	 * postmaster/checkpointer.c.
651 	 */
652 	pqinitmask();
653 	PG_SETMASK(&BlockSig);
654 
655 	pqsignal_pm(SIGHUP, SIGHUP_handler);	/* reread config file and have
656 											 * children do same */
657 	pqsignal_pm(SIGINT, pmdie); /* send SIGTERM and shut down */
658 	pqsignal_pm(SIGQUIT, pmdie);	/* send SIGQUIT and die */
659 	pqsignal_pm(SIGTERM, pmdie);	/* wait for children and shut down */
660 	pqsignal_pm(SIGALRM, SIG_IGN);	/* ignored */
661 	pqsignal_pm(SIGPIPE, SIG_IGN);	/* ignored */
662 	pqsignal_pm(SIGUSR1, sigusr1_handler);	/* message from child process */
663 	pqsignal_pm(SIGUSR2, dummy_handler);	/* unused, reserve for children */
664 	pqsignal_pm(SIGCHLD, reaper);	/* handle child termination */
665 	pqsignal_pm(SIGTTIN, SIG_IGN);	/* ignored */
666 	pqsignal_pm(SIGTTOU, SIG_IGN);	/* ignored */
667 	/* ignore SIGXFSZ, so that ulimit violations work like disk full */
668 #ifdef SIGXFSZ
669 	pqsignal_pm(SIGXFSZ, SIG_IGN);	/* ignored */
670 #endif
671 
672 	/*
673 	 * Options setup
674 	 */
675 	InitializeGUCOptions();
676 
677 	opterr = 1;
678 
679 	/*
680 	 * Parse command-line options.  CAUTION: keep this in sync with
681 	 * tcop/postgres.c (the option sets should not conflict) and with the
682 	 * common help() function in main/main.c.
683 	 */
684 	while ((opt = getopt(argc, argv, "B:bc:C:D:d:EeFf:h:ijk:lN:nOo:Pp:r:S:sTt:W:-:")) != -1)
685 	{
686 		switch (opt)
687 		{
688 			case 'B':
689 				SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
690 				break;
691 
692 			case 'b':
693 				/* Undocumented flag used for binary upgrades */
694 				IsBinaryUpgrade = true;
695 				break;
696 
697 			case 'C':
698 				output_config_variable = strdup(optarg);
699 				break;
700 
701 			case 'D':
702 				userDoption = strdup(optarg);
703 				break;
704 
705 			case 'd':
706 				set_debug_options(atoi(optarg), PGC_POSTMASTER, PGC_S_ARGV);
707 				break;
708 
709 			case 'E':
710 				SetConfigOption("log_statement", "all", PGC_POSTMASTER, PGC_S_ARGV);
711 				break;
712 
713 			case 'e':
714 				SetConfigOption("datestyle", "euro", PGC_POSTMASTER, PGC_S_ARGV);
715 				break;
716 
717 			case 'F':
718 				SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
719 				break;
720 
721 			case 'f':
722 				if (!set_plan_disabling_options(optarg, PGC_POSTMASTER, PGC_S_ARGV))
723 				{
724 					write_stderr("%s: invalid argument for option -f: \"%s\"\n",
725 								 progname, optarg);
726 					ExitPostmaster(1);
727 				}
728 				break;
729 
730 			case 'h':
731 				SetConfigOption("listen_addresses", optarg, PGC_POSTMASTER, PGC_S_ARGV);
732 				break;
733 
734 			case 'i':
735 				SetConfigOption("listen_addresses", "*", PGC_POSTMASTER, PGC_S_ARGV);
736 				break;
737 
738 			case 'j':
739 				/* only used by interactive backend */
740 				break;
741 
742 			case 'k':
743 				SetConfigOption("unix_socket_directories", optarg, PGC_POSTMASTER, PGC_S_ARGV);
744 				break;
745 
746 			case 'l':
747 				SetConfigOption("ssl", "true", PGC_POSTMASTER, PGC_S_ARGV);
748 				break;
749 
750 			case 'N':
751 				SetConfigOption("max_connections", optarg, PGC_POSTMASTER, PGC_S_ARGV);
752 				break;
753 
754 			case 'n':
755 				/* Don't reinit shared mem after abnormal exit */
756 				Reinit = false;
757 				break;
758 
759 			case 'O':
760 				SetConfigOption("allow_system_table_mods", "true", PGC_POSTMASTER, PGC_S_ARGV);
761 				break;
762 
763 			case 'o':
764 				/* Other options to pass to the backend on the command line */
765 				snprintf(ExtraOptions + strlen(ExtraOptions),
766 						 sizeof(ExtraOptions) - strlen(ExtraOptions),
767 						 " %s", optarg);
768 				break;
769 
770 			case 'P':
771 				SetConfigOption("ignore_system_indexes", "true", PGC_POSTMASTER, PGC_S_ARGV);
772 				break;
773 
774 			case 'p':
775 				SetConfigOption("port", optarg, PGC_POSTMASTER, PGC_S_ARGV);
776 				break;
777 
778 			case 'r':
779 				/* only used by single-user backend */
780 				break;
781 
782 			case 'S':
783 				SetConfigOption("work_mem", optarg, PGC_POSTMASTER, PGC_S_ARGV);
784 				break;
785 
786 			case 's':
787 				SetConfigOption("log_statement_stats", "true", PGC_POSTMASTER, PGC_S_ARGV);
788 				break;
789 
790 			case 'T':
791 
792 				/*
793 				 * In the event that some backend dumps core, send SIGSTOP,
794 				 * rather than SIGQUIT, to all its peers.  This lets the wily
795 				 * post_hacker collect core dumps from everyone.
796 				 */
797 				SendStop = true;
798 				break;
799 
800 			case 't':
801 				{
802 					const char *tmp = get_stats_option_name(optarg);
803 
804 					if (tmp)
805 					{
806 						SetConfigOption(tmp, "true", PGC_POSTMASTER, PGC_S_ARGV);
807 					}
808 					else
809 					{
810 						write_stderr("%s: invalid argument for option -t: \"%s\"\n",
811 									 progname, optarg);
812 						ExitPostmaster(1);
813 					}
814 					break;
815 				}
816 
817 			case 'W':
818 				SetConfigOption("post_auth_delay", optarg, PGC_POSTMASTER, PGC_S_ARGV);
819 				break;
820 
821 			case 'c':
822 			case '-':
823 				{
824 					char	   *name,
825 							   *value;
826 
827 					ParseLongOption(optarg, &name, &value);
828 					if (!value)
829 					{
830 						if (opt == '-')
831 							ereport(ERROR,
832 									(errcode(ERRCODE_SYNTAX_ERROR),
833 									 errmsg("--%s requires a value",
834 											optarg)));
835 						else
836 							ereport(ERROR,
837 									(errcode(ERRCODE_SYNTAX_ERROR),
838 									 errmsg("-c %s requires a value",
839 											optarg)));
840 					}
841 
842 					SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
843 					free(name);
844 					if (value)
845 						free(value);
846 					break;
847 				}
848 
849 			default:
850 				write_stderr("Try \"%s --help\" for more information.\n",
851 							 progname);
852 				ExitPostmaster(1);
853 		}
854 	}
855 
856 	/*
857 	 * Postmaster accepts no non-option switch arguments.
858 	 */
859 	if (optind < argc)
860 	{
861 		write_stderr("%s: invalid argument: \"%s\"\n",
862 					 progname, argv[optind]);
863 		write_stderr("Try \"%s --help\" for more information.\n",
864 					 progname);
865 		ExitPostmaster(1);
866 	}
867 
868 	/*
869 	 * Locate the proper configuration files and data directory, and read
870 	 * postgresql.conf for the first time.
871 	 */
872 	if (!SelectConfigFiles(userDoption, progname))
873 		ExitPostmaster(2);
874 
875 	if (output_config_variable != NULL)
876 	{
877 		/*
878 		 * "-C guc" was specified, so print GUC's value and exit.  No extra
879 		 * permission check is needed because the user is reading inside the
880 		 * data dir.
881 		 */
882 		const char *config_val = GetConfigOption(output_config_variable,
883 												 false, false);
884 
885 		puts(config_val ? config_val : "");
886 		ExitPostmaster(0);
887 	}
888 
889 	/* Verify that DataDir looks reasonable */
890 	checkDataDir();
891 
892 	/* And switch working directory into it */
893 	ChangeToDataDir();
894 
895 	/*
896 	 * Check for invalid combinations of GUC settings.
897 	 */
898 	if (ReservedBackends >= MaxConnections)
899 	{
900 		write_stderr("%s: superuser_reserved_connections must be less than max_connections\n", progname);
901 		ExitPostmaster(1);
902 	}
903 	if (max_wal_senders >= MaxConnections)
904 	{
905 		write_stderr("%s: max_wal_senders must be less than max_connections\n", progname);
906 		ExitPostmaster(1);
907 	}
908 	if (XLogArchiveMode > ARCHIVE_MODE_OFF && wal_level == WAL_LEVEL_MINIMAL)
909 		ereport(ERROR,
910 				(errmsg("WAL archival cannot be enabled when wal_level is \"minimal\"")));
911 	if (max_wal_senders > 0 && wal_level == WAL_LEVEL_MINIMAL)
912 		ereport(ERROR,
913 				(errmsg("WAL streaming (max_wal_senders > 0) requires wal_level \"replica\" or \"logical\"")));
914 
915 	/*
916 	 * Other one-time internal sanity checks can go here, if they are fast.
917 	 * (Put any slow processing further down, after postmaster.pid creation.)
918 	 */
919 	if (!CheckDateTokenTables())
920 	{
921 		write_stderr("%s: invalid datetoken tables, please fix\n", progname);
922 		ExitPostmaster(1);
923 	}
924 
925 	/*
926 	 * Now that we are done processing the postmaster arguments, reset
927 	 * getopt(3) library so that it will work correctly in subprocesses.
928 	 */
929 	optind = 1;
930 #ifdef HAVE_INT_OPTRESET
931 	optreset = 1;				/* some systems need this too */
932 #endif
933 
934 	/* For debugging: display postmaster environment */
935 	{
936 		extern char **environ;
937 		char	  **p;
938 
939 		ereport(DEBUG3,
940 			(errmsg_internal("%s: PostmasterMain: initial environment dump:",
941 							 progname)));
942 		ereport(DEBUG3,
943 			 (errmsg_internal("-----------------------------------------")));
944 		for (p = environ; *p; ++p)
945 			ereport(DEBUG3,
946 					(errmsg_internal("\t%s", *p)));
947 		ereport(DEBUG3,
948 			 (errmsg_internal("-----------------------------------------")));
949 	}
950 
951 	/*
952 	 * Create lockfile for data directory.
953 	 *
954 	 * We want to do this before we try to grab the input sockets, because the
955 	 * data directory interlock is more reliable than the socket-file
956 	 * interlock (thanks to whoever decided to put socket files in /tmp :-().
957 	 * For the same reason, it's best to grab the TCP socket(s) before the
958 	 * Unix socket(s).
959 	 *
960 	 * Also note that this internally sets up the on_proc_exit function that
961 	 * is responsible for removing both data directory and socket lockfiles;
962 	 * so it must happen before opening sockets so that at exit, the socket
963 	 * lockfiles go away after CloseServerPorts runs.
964 	 */
965 	CreateDataDirLockFile(true);
966 
967 	/*
968 	 * Initialize SSL library, if specified.
969 	 */
970 #ifdef USE_SSL
971 	if (EnableSSL)
972 		secure_initialize();
973 #endif
974 
975 	/*
976 	 * process any libraries that should be preloaded at postmaster start
977 	 */
978 	process_shared_preload_libraries();
979 
980 	/*
981 	 * Now that loadable modules have had their chance to register background
982 	 * workers, calculate MaxBackends.
983 	 */
984 	InitializeMaxBackends();
985 
986 	/*
987 	 * Establish input sockets.
988 	 *
989 	 * First, mark them all closed, and set up an on_proc_exit function that's
990 	 * charged with closing the sockets again at postmaster shutdown.
991 	 */
992 	for (i = 0; i < MAXLISTEN; i++)
993 		ListenSocket[i] = PGINVALID_SOCKET;
994 
995 	on_proc_exit(CloseServerPorts, 0);
996 
997 	if (ListenAddresses)
998 	{
999 		char	   *rawstring;
1000 		List	   *elemlist;
1001 		ListCell   *l;
1002 		int			success = 0;
1003 
1004 		/* Need a modifiable copy of ListenAddresses */
1005 		rawstring = pstrdup(ListenAddresses);
1006 
1007 		/* Parse string into list of hostnames */
1008 		if (!SplitIdentifierString(rawstring, ',', &elemlist))
1009 		{
1010 			/* syntax error in list */
1011 			ereport(FATAL,
1012 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1013 					 errmsg("invalid list syntax in parameter \"%s\"",
1014 							"listen_addresses")));
1015 		}
1016 
1017 		foreach(l, elemlist)
1018 		{
1019 			char	   *curhost = (char *) lfirst(l);
1020 
1021 			if (strcmp(curhost, "*") == 0)
1022 				status = StreamServerPort(AF_UNSPEC, NULL,
1023 										  (unsigned short) PostPortNumber,
1024 										  NULL,
1025 										  ListenSocket, MAXLISTEN);
1026 			else
1027 				status = StreamServerPort(AF_UNSPEC, curhost,
1028 										  (unsigned short) PostPortNumber,
1029 										  NULL,
1030 										  ListenSocket, MAXLISTEN);
1031 
1032 			if (status == STATUS_OK)
1033 			{
1034 				success++;
1035 				/* record the first successful host addr in lockfile */
1036 				if (!listen_addr_saved)
1037 				{
1038 					AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, curhost);
1039 					listen_addr_saved = true;
1040 				}
1041 			}
1042 			else
1043 				ereport(WARNING,
1044 						(errmsg("could not create listen socket for \"%s\"",
1045 								curhost)));
1046 		}
1047 
1048 		if (!success && elemlist != NIL)
1049 			ereport(FATAL,
1050 					(errmsg("could not create any TCP/IP sockets")));
1051 
1052 		list_free(elemlist);
1053 		pfree(rawstring);
1054 	}
1055 
1056 #ifdef USE_BONJOUR
1057 	/* Register for Bonjour only if we opened TCP socket(s) */
1058 	if (enable_bonjour && ListenSocket[0] != PGINVALID_SOCKET)
1059 	{
1060 		DNSServiceErrorType err;
1061 
1062 		/*
1063 		 * We pass 0 for interface_index, which will result in registering on
1064 		 * all "applicable" interfaces.  It's not entirely clear from the
1065 		 * DNS-SD docs whether this would be appropriate if we have bound to
1066 		 * just a subset of the available network interfaces.
1067 		 */
1068 		err = DNSServiceRegister(&bonjour_sdref,
1069 								 0,
1070 								 0,
1071 								 bonjour_name,
1072 								 "_postgresql._tcp.",
1073 								 NULL,
1074 								 NULL,
1075 								 htons(PostPortNumber),
1076 								 0,
1077 								 NULL,
1078 								 NULL,
1079 								 NULL);
1080 		if (err != kDNSServiceErr_NoError)
1081 			elog(LOG, "DNSServiceRegister() failed: error code %ld",
1082 				 (long) err);
1083 
1084 		/*
1085 		 * We don't bother to read the mDNS daemon's reply, and we expect that
1086 		 * it will automatically terminate our registration when the socket is
1087 		 * closed at postmaster termination.  So there's nothing more to be
1088 		 * done here.  However, the bonjour_sdref is kept around so that
1089 		 * forked children can close their copies of the socket.
1090 		 */
1091 	}
1092 #endif
1093 
1094 #ifdef HAVE_UNIX_SOCKETS
1095 	if (Unix_socket_directories)
1096 	{
1097 		char	   *rawstring;
1098 		List	   *elemlist;
1099 		ListCell   *l;
1100 		int			success = 0;
1101 
1102 		/* Need a modifiable copy of Unix_socket_directories */
1103 		rawstring = pstrdup(Unix_socket_directories);
1104 
1105 		/* Parse string into list of directories */
1106 		if (!SplitDirectoriesString(rawstring, ',', &elemlist))
1107 		{
1108 			/* syntax error in list */
1109 			ereport(FATAL,
1110 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1111 					 errmsg("invalid list syntax in parameter \"%s\"",
1112 							"unix_socket_directories")));
1113 		}
1114 
1115 		foreach(l, elemlist)
1116 		{
1117 			char	   *socketdir = (char *) lfirst(l);
1118 
1119 			status = StreamServerPort(AF_UNIX, NULL,
1120 									  (unsigned short) PostPortNumber,
1121 									  socketdir,
1122 									  ListenSocket, MAXLISTEN);
1123 
1124 			if (status == STATUS_OK)
1125 			{
1126 				success++;
1127 				/* record the first successful Unix socket in lockfile */
1128 				if (success == 1)
1129 					AddToDataDirLockFile(LOCK_FILE_LINE_SOCKET_DIR, socketdir);
1130 			}
1131 			else
1132 				ereport(WARNING,
1133 						(errmsg("could not create Unix-domain socket in directory \"%s\"",
1134 								socketdir)));
1135 		}
1136 
1137 		if (!success && elemlist != NIL)
1138 			ereport(FATAL,
1139 					(errmsg("could not create any Unix-domain sockets")));
1140 
1141 		list_free_deep(elemlist);
1142 		pfree(rawstring);
1143 	}
1144 #endif
1145 
1146 	/*
1147 	 * check that we have some socket to listen on
1148 	 */
1149 	if (ListenSocket[0] == PGINVALID_SOCKET)
1150 		ereport(FATAL,
1151 				(errmsg("no socket created for listening")));
1152 
1153 	/*
1154 	 * If no valid TCP ports, write an empty line for listen address,
1155 	 * indicating the Unix socket must be used.  Note that this line is not
1156 	 * added to the lock file until there is a socket backing it.
1157 	 */
1158 	if (!listen_addr_saved)
1159 		AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, "");
1160 
1161 	/*
1162 	 * Set up shared memory and semaphores.
1163 	 */
1164 	reset_shared(PostPortNumber);
1165 
1166 	/*
1167 	 * Estimate number of openable files.  This must happen after setting up
1168 	 * semaphores, because on some platforms semaphores count as open files.
1169 	 */
1170 	set_max_safe_fds();
1171 
1172 	/*
1173 	 * Set reference point for stack-depth checking.
1174 	 */
1175 	set_stack_base();
1176 
1177 	/*
1178 	 * Initialize pipe (or process handle on Windows) that allows children to
1179 	 * wake up from sleep on postmaster death.
1180 	 */
1181 	InitPostmasterDeathWatchHandle();
1182 
1183 #ifdef WIN32
1184 
1185 	/*
1186 	 * Initialize I/O completion port used to deliver list of dead children.
1187 	 */
1188 	win32ChildQueue = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
1189 	if (win32ChildQueue == NULL)
1190 		ereport(FATAL,
1191 		   (errmsg("could not create I/O completion port for child queue")));
1192 #endif
1193 
1194 	/*
1195 	 * Record postmaster options.  We delay this till now to avoid recording
1196 	 * bogus options (eg, NBuffers too high for available memory).
1197 	 */
1198 	if (!CreateOptsFile(argc, argv, my_exec_path))
1199 		ExitPostmaster(1);
1200 
1201 #ifdef EXEC_BACKEND
1202 	/* Write out nondefault GUC settings for child processes to use */
1203 	write_nondefault_variables(PGC_POSTMASTER);
1204 #endif
1205 
1206 	/*
1207 	 * Write the external PID file if requested
1208 	 */
1209 	if (external_pid_file)
1210 	{
1211 		FILE	   *fpidfile = fopen(external_pid_file, "w");
1212 
1213 		if (fpidfile)
1214 		{
1215 			fprintf(fpidfile, "%d\n", MyProcPid);
1216 			fclose(fpidfile);
1217 
1218 			/* Make PID file world readable */
1219 			if (chmod(external_pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0)
1220 				write_stderr("%s: could not change permissions of external PID file \"%s\": %s\n",
1221 							 progname, external_pid_file, strerror(errno));
1222 		}
1223 		else
1224 			write_stderr("%s: could not write external PID file \"%s\": %s\n",
1225 						 progname, external_pid_file, strerror(errno));
1226 
1227 		on_proc_exit(unlink_external_pid_file, 0);
1228 	}
1229 
1230 	/*
1231 	 * Remove old temporary files.  At this point there can be no other
1232 	 * Postgres processes running in this directory, so this should be safe.
1233 	 */
1234 	RemovePgTempFiles();
1235 
1236 	/*
1237 	 * Forcibly remove the files signaling a standby promotion request.
1238 	 * Otherwise, the existence of those files triggers a promotion too early,
1239 	 * whether a user wants that or not.
1240 	 *
1241 	 * This removal of files is usually unnecessary because they can exist
1242 	 * only during a few moments during a standby promotion. However there is
1243 	 * a race condition: if pg_ctl promote is executed and creates the files
1244 	 * during a promotion, the files can stay around even after the server is
1245 	 * brought up to new master. Then, if new standby starts by using the
1246 	 * backup taken from that master, the files can exist at the server
1247 	 * startup and should be removed in order to avoid an unexpected
1248 	 * promotion.
1249 	 *
1250 	 * Note that promotion signal files need to be removed before the startup
1251 	 * process is invoked. Because, after that, they can be used by
1252 	 * postmaster's SIGUSR1 signal handler.
1253 	 */
1254 	RemovePromoteSignalFiles();
1255 
1256 	/*
1257 	 * If enabled, start up syslogger collection subprocess
1258 	 */
1259 	SysLoggerPID = SysLogger_Start();
1260 
1261 	/*
1262 	 * Reset whereToSendOutput from DestDebug (its starting state) to
1263 	 * DestNone. This stops ereport from sending log messages to stderr unless
1264 	 * Log_destination permits.  We don't do this until the postmaster is
1265 	 * fully launched, since startup failures may as well be reported to
1266 	 * stderr.
1267 	 *
1268 	 * If we are in fact disabling logging to stderr, first emit a log message
1269 	 * saying so, to provide a breadcrumb trail for users who may not remember
1270 	 * that their logging is configured to go somewhere else.
1271 	 */
1272 	if (!(Log_destination & LOG_DESTINATION_STDERR))
1273 		ereport(LOG,
1274 				(errmsg("ending log output to stderr"),
1275 			  errhint("Future log output will go to log destination \"%s\".",
1276 					  Log_destination_string)));
1277 
1278 	whereToSendOutput = DestNone;
1279 
1280 	/*
1281 	 * Initialize stats collection subsystem (this does NOT start the
1282 	 * collector process!)
1283 	 */
1284 	pgstat_init();
1285 
1286 	/*
1287 	 * Initialize the autovacuum subsystem (again, no process start yet)
1288 	 */
1289 	autovac_init();
1290 
1291 	/*
1292 	 * Load configuration files for client authentication.
1293 	 */
1294 	if (!load_hba())
1295 	{
1296 		/*
1297 		 * It makes no sense to continue if we fail to load the HBA file,
1298 		 * since there is no way to connect to the database in this case.
1299 		 */
1300 		ereport(FATAL,
1301 				(errmsg("could not load pg_hba.conf")));
1302 	}
1303 	if (!load_ident())
1304 	{
1305 		/*
1306 		 * We can start up without the IDENT file, although it means that you
1307 		 * cannot log in using any of the authentication methods that need a
1308 		 * user name mapping. load_ident() already logged the details of error
1309 		 * to the log.
1310 		 */
1311 	}
1312 
1313 #ifdef HAVE_PTHREAD_IS_THREADED_NP
1314 
1315 	/*
1316 	 * On Darwin, libintl replaces setlocale() with a version that calls
1317 	 * CFLocaleCopyCurrent() when its second argument is "" and every relevant
1318 	 * environment variable is unset or empty.  CFLocaleCopyCurrent() makes
1319 	 * the process multithreaded.  The postmaster calls sigprocmask() and
1320 	 * calls fork() without an immediate exec(), both of which have undefined
1321 	 * behavior in a multithreaded program.  A multithreaded postmaster is the
1322 	 * normal case on Windows, which offers neither fork() nor sigprocmask().
1323 	 */
1324 	if (pthread_is_threaded_np() != 0)
1325 		ereport(FATAL,
1326 				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1327 				 errmsg("postmaster became multithreaded during startup"),
1328 		 errhint("Set the LC_ALL environment variable to a valid locale.")));
1329 #endif
1330 
1331 	/*
1332 	 * Remember postmaster startup time
1333 	 */
1334 	PgStartTime = GetCurrentTimestamp();
1335 	/* PostmasterRandom wants its own copy */
1336 	gettimeofday(&random_start_time, NULL);
1337 
1338 	/*
1339 	 * We're ready to rock and roll...
1340 	 */
1341 	StartupPID = StartupDataBase();
1342 	Assert(StartupPID != 0);
1343 	StartupStatus = STARTUP_RUNNING;
1344 	pmState = PM_STARTUP;
1345 
1346 	/* Some workers may be scheduled to start now */
1347 	maybe_start_bgworkers();
1348 
1349 	status = ServerLoop();
1350 
1351 	/*
1352 	 * ServerLoop probably shouldn't ever return, but if it does, close down.
1353 	 */
1354 	ExitPostmaster(status != STATUS_OK);
1355 
1356 	abort();					/* not reached */
1357 }
1358 
1359 
1360 /*
1361  * on_proc_exit callback to close server's listen sockets
1362  */
1363 static void
CloseServerPorts(int status,Datum arg)1364 CloseServerPorts(int status, Datum arg)
1365 {
1366 	int			i;
1367 
1368 	/*
1369 	 * First, explicitly close all the socket FDs.  We used to just let this
1370 	 * happen implicitly at postmaster exit, but it's better to close them
1371 	 * before we remove the postmaster.pid lockfile; otherwise there's a race
1372 	 * condition if a new postmaster wants to re-use the TCP port number.
1373 	 */
1374 	for (i = 0; i < MAXLISTEN; i++)
1375 	{
1376 		if (ListenSocket[i] != PGINVALID_SOCKET)
1377 		{
1378 			StreamClose(ListenSocket[i]);
1379 			ListenSocket[i] = PGINVALID_SOCKET;
1380 		}
1381 	}
1382 
1383 	/*
1384 	 * Next, remove any filesystem entries for Unix sockets.  To avoid race
1385 	 * conditions against incoming postmasters, this must happen after closing
1386 	 * the sockets and before removing lock files.
1387 	 */
1388 	RemoveSocketFiles();
1389 
1390 	/*
1391 	 * We don't do anything about socket lock files here; those will be
1392 	 * removed in a later on_proc_exit callback.
1393 	 */
1394 }
1395 
1396 /*
1397  * on_proc_exit callback to delete external_pid_file
1398  */
1399 static void
unlink_external_pid_file(int status,Datum arg)1400 unlink_external_pid_file(int status, Datum arg)
1401 {
1402 	if (external_pid_file)
1403 		unlink(external_pid_file);
1404 }
1405 
1406 
1407 /*
1408  * Compute and check the directory paths to files that are part of the
1409  * installation (as deduced from the postgres executable's own location)
1410  */
1411 static void
getInstallationPaths(const char * argv0)1412 getInstallationPaths(const char *argv0)
1413 {
1414 	DIR		   *pdir;
1415 
1416 	/* Locate the postgres executable itself */
1417 	if (find_my_exec(argv0, my_exec_path) < 0)
1418 		elog(FATAL, "%s: could not locate my own executable path", argv0);
1419 
1420 #ifdef EXEC_BACKEND
1421 	/* Locate executable backend before we change working directory */
1422 	if (find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
1423 						postgres_exec_path) < 0)
1424 		ereport(FATAL,
1425 				(errmsg("%s: could not locate matching postgres executable",
1426 						argv0)));
1427 #endif
1428 
1429 	/*
1430 	 * Locate the pkglib directory --- this has to be set early in case we try
1431 	 * to load any modules from it in response to postgresql.conf entries.
1432 	 */
1433 	get_pkglib_path(my_exec_path, pkglib_path);
1434 
1435 	/*
1436 	 * Verify that there's a readable directory there; otherwise the Postgres
1437 	 * installation is incomplete or corrupt.  (A typical cause of this
1438 	 * failure is that the postgres executable has been moved or hardlinked to
1439 	 * some directory that's not a sibling of the installation lib/
1440 	 * directory.)
1441 	 */
1442 	pdir = AllocateDir(pkglib_path);
1443 	if (pdir == NULL)
1444 		ereport(ERROR,
1445 				(errcode_for_file_access(),
1446 				 errmsg("could not open directory \"%s\": %m",
1447 						pkglib_path),
1448 				 errhint("This may indicate an incomplete PostgreSQL installation, or that the file \"%s\" has been moved away from its proper location.",
1449 						 my_exec_path)));
1450 	FreeDir(pdir);
1451 
1452 	/*
1453 	 * XXX is it worth similarly checking the share/ directory?  If the lib/
1454 	 * directory is there, then share/ probably is too.
1455 	 */
1456 }
1457 
1458 
1459 /*
1460  * Validate the proposed data directory
1461  */
1462 static void
checkDataDir(void)1463 checkDataDir(void)
1464 {
1465 	char		path[MAXPGPATH];
1466 	FILE	   *fp;
1467 	struct stat stat_buf;
1468 
1469 	Assert(DataDir);
1470 
1471 	if (stat(DataDir, &stat_buf) != 0)
1472 	{
1473 		if (errno == ENOENT)
1474 			ereport(FATAL,
1475 					(errcode_for_file_access(),
1476 					 errmsg("data directory \"%s\" does not exist",
1477 							DataDir)));
1478 		else
1479 			ereport(FATAL,
1480 					(errcode_for_file_access(),
1481 				 errmsg("could not read permissions of directory \"%s\": %m",
1482 						DataDir)));
1483 	}
1484 
1485 	/* eventual chdir would fail anyway, but let's test ... */
1486 	if (!S_ISDIR(stat_buf.st_mode))
1487 		ereport(FATAL,
1488 				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1489 				 errmsg("specified data directory \"%s\" is not a directory",
1490 						DataDir)));
1491 
1492 	/*
1493 	 * Check that the directory belongs to my userid; if not, reject.
1494 	 *
1495 	 * This check is an essential part of the interlock that prevents two
1496 	 * postmasters from starting in the same directory (see CreateLockFile()).
1497 	 * Do not remove or weaken it.
1498 	 *
1499 	 * XXX can we safely enable this check on Windows?
1500 	 */
1501 #if !defined(WIN32) && !defined(__CYGWIN__)
1502 	if (stat_buf.st_uid != geteuid())
1503 		ereport(FATAL,
1504 				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1505 				 errmsg("data directory \"%s\" has wrong ownership",
1506 						DataDir),
1507 				 errhint("The server must be started by the user that owns the data directory.")));
1508 #endif
1509 
1510 	/*
1511 	 * Check if the directory has group or world access.  If so, reject.
1512 	 *
1513 	 * It would be possible to allow weaker constraints (for example, allow
1514 	 * group access) but we cannot make a general assumption that that is
1515 	 * okay; for example there are platforms where nearly all users
1516 	 * customarily belong to the same group.  Perhaps this test should be
1517 	 * configurable.
1518 	 *
1519 	 * XXX temporarily suppress check when on Windows, because there may not
1520 	 * be proper support for Unix-y file permissions.  Need to think of a
1521 	 * reasonable check to apply on Windows.
1522 	 */
1523 #if !defined(WIN32) && !defined(__CYGWIN__)
1524 	if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
1525 		ereport(FATAL,
1526 				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1527 				 errmsg("data directory \"%s\" has group or world access",
1528 						DataDir),
1529 				 errdetail("Permissions should be u=rwx (0700).")));
1530 #endif
1531 
1532 	/* Look for PG_VERSION before looking for pg_control */
1533 	ValidatePgVersion(DataDir);
1534 
1535 	snprintf(path, sizeof(path), "%s/global/pg_control", DataDir);
1536 
1537 	fp = AllocateFile(path, PG_BINARY_R);
1538 	if (fp == NULL)
1539 	{
1540 		write_stderr("%s: could not find the database system\n"
1541 					 "Expected to find it in the directory \"%s\",\n"
1542 					 "but could not open file \"%s\": %s\n",
1543 					 progname, DataDir, path, strerror(errno));
1544 		ExitPostmaster(2);
1545 	}
1546 	FreeFile(fp);
1547 }
1548 
1549 /*
1550  * Determine how long should we let ServerLoop sleep.
1551  *
1552  * In normal conditions we wait at most one minute, to ensure that the other
1553  * background tasks handled by ServerLoop get done even when no requests are
1554  * arriving.  However, if there are background workers waiting to be started,
1555  * we don't actually sleep so that they are quickly serviced.  Other exception
1556  * cases are as shown in the code.
1557  */
1558 static void
DetermineSleepTime(struct timeval * timeout)1559 DetermineSleepTime(struct timeval * timeout)
1560 {
1561 	TimestampTz next_wakeup = 0;
1562 
1563 	/*
1564 	 * Normal case: either there are no background workers at all, or we're in
1565 	 * a shutdown sequence (during which we ignore bgworkers altogether).
1566 	 */
1567 	if (Shutdown > NoShutdown ||
1568 		(!StartWorkerNeeded && !HaveCrashedWorker))
1569 	{
1570 		if (AbortStartTime != 0)
1571 		{
1572 			/* time left to abort; clamp to 0 in case it already expired */
1573 			timeout->tv_sec = SIGKILL_CHILDREN_AFTER_SECS -
1574 				(time(NULL) - AbortStartTime);
1575 			timeout->tv_sec = Max(timeout->tv_sec, 0);
1576 			timeout->tv_usec = 0;
1577 		}
1578 		else
1579 		{
1580 			timeout->tv_sec = 60;
1581 			timeout->tv_usec = 0;
1582 		}
1583 		return;
1584 	}
1585 
1586 	if (StartWorkerNeeded)
1587 	{
1588 		timeout->tv_sec = 0;
1589 		timeout->tv_usec = 0;
1590 		return;
1591 	}
1592 
1593 	if (HaveCrashedWorker)
1594 	{
1595 		slist_mutable_iter siter;
1596 
1597 		/*
1598 		 * When there are crashed bgworkers, we sleep just long enough that
1599 		 * they are restarted when they request to be.  Scan the list to
1600 		 * determine the minimum of all wakeup times according to most recent
1601 		 * crash time and requested restart interval.
1602 		 */
1603 		slist_foreach_modify(siter, &BackgroundWorkerList)
1604 		{
1605 			RegisteredBgWorker *rw;
1606 			TimestampTz this_wakeup;
1607 
1608 			rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
1609 
1610 			if (rw->rw_crashed_at == 0)
1611 				continue;
1612 
1613 			if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART
1614 				|| rw->rw_terminate)
1615 			{
1616 				ForgetBackgroundWorker(&siter);
1617 				continue;
1618 			}
1619 
1620 			this_wakeup = TimestampTzPlusMilliseconds(rw->rw_crashed_at,
1621 									 1000L * rw->rw_worker.bgw_restart_time);
1622 			if (next_wakeup == 0 || this_wakeup < next_wakeup)
1623 				next_wakeup = this_wakeup;
1624 		}
1625 	}
1626 
1627 	if (next_wakeup != 0)
1628 	{
1629 		long		secs;
1630 		int			microsecs;
1631 
1632 		TimestampDifference(GetCurrentTimestamp(), next_wakeup,
1633 							&secs, &microsecs);
1634 		timeout->tv_sec = secs;
1635 		timeout->tv_usec = microsecs;
1636 
1637 		/* Ensure we don't exceed one minute */
1638 		if (timeout->tv_sec > 60)
1639 		{
1640 			timeout->tv_sec = 60;
1641 			timeout->tv_usec = 0;
1642 		}
1643 	}
1644 	else
1645 	{
1646 		timeout->tv_sec = 60;
1647 		timeout->tv_usec = 0;
1648 	}
1649 }
1650 
1651 /*
1652  * Main idle loop of postmaster
1653  *
1654  * NB: Needs to be called with signals blocked
1655  */
1656 static int
ServerLoop(void)1657 ServerLoop(void)
1658 {
1659 	fd_set		readmask;
1660 	int			nSockets;
1661 	time_t		last_lockfile_recheck_time,
1662 				last_touch_time;
1663 
1664 	last_lockfile_recheck_time = last_touch_time = time(NULL);
1665 
1666 	nSockets = initMasks(&readmask);
1667 
1668 	for (;;)
1669 	{
1670 		fd_set		rmask;
1671 		int			selres;
1672 		time_t		now;
1673 
1674 		/*
1675 		 * Wait for a connection request to arrive.
1676 		 *
1677 		 * We block all signals except while sleeping. That makes it safe for
1678 		 * signal handlers, which again block all signals while executing, to
1679 		 * do nontrivial work.
1680 		 *
1681 		 * If we are in PM_WAIT_DEAD_END state, then we don't want to accept
1682 		 * any new connections, so we don't call select(), and just sleep.
1683 		 */
1684 		memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
1685 
1686 		if (pmState == PM_WAIT_DEAD_END)
1687 		{
1688 			PG_SETMASK(&UnBlockSig);
1689 
1690 			pg_usleep(100000L); /* 100 msec seems reasonable */
1691 			selres = 0;
1692 
1693 			PG_SETMASK(&BlockSig);
1694 		}
1695 		else
1696 		{
1697 			/* must set timeout each time; some OSes change it! */
1698 			struct timeval timeout;
1699 
1700 			/* Needs to run with blocked signals! */
1701 			DetermineSleepTime(&timeout);
1702 
1703 			PG_SETMASK(&UnBlockSig);
1704 
1705 			selres = select(nSockets, &rmask, NULL, NULL, &timeout);
1706 
1707 			PG_SETMASK(&BlockSig);
1708 		}
1709 
1710 		/* Now check the select() result */
1711 		if (selres < 0)
1712 		{
1713 			if (errno != EINTR && errno != EWOULDBLOCK)
1714 			{
1715 				ereport(LOG,
1716 						(errcode_for_socket_access(),
1717 						 errmsg("select() failed in postmaster: %m")));
1718 				return STATUS_ERROR;
1719 			}
1720 		}
1721 
1722 		/*
1723 		 * New connection pending on any of our sockets? If so, fork a child
1724 		 * process to deal with it.
1725 		 */
1726 		if (selres > 0)
1727 		{
1728 			int			i;
1729 
1730 			for (i = 0; i < MAXLISTEN; i++)
1731 			{
1732 				if (ListenSocket[i] == PGINVALID_SOCKET)
1733 					break;
1734 				if (FD_ISSET(ListenSocket[i], &rmask))
1735 				{
1736 					Port	   *port;
1737 
1738 					port = ConnCreate(ListenSocket[i]);
1739 					if (port)
1740 					{
1741 						BackendStartup(port);
1742 
1743 						/*
1744 						 * We no longer need the open socket or port structure
1745 						 * in this process
1746 						 */
1747 						StreamClose(port->sock);
1748 						ConnFree(port);
1749 					}
1750 				}
1751 			}
1752 		}
1753 
1754 		/* If we have lost the log collector, try to start a new one */
1755 		if (SysLoggerPID == 0 && Logging_collector)
1756 			SysLoggerPID = SysLogger_Start();
1757 
1758 		/*
1759 		 * If no background writer process is running, and we are not in a
1760 		 * state that prevents it, start one.  It doesn't matter if this
1761 		 * fails, we'll just try again later.  Likewise for the checkpointer.
1762 		 */
1763 		if (pmState == PM_RUN || pmState == PM_RECOVERY ||
1764 			pmState == PM_HOT_STANDBY)
1765 		{
1766 			if (CheckpointerPID == 0)
1767 				CheckpointerPID = StartCheckpointer();
1768 			if (BgWriterPID == 0)
1769 				BgWriterPID = StartBackgroundWriter();
1770 		}
1771 
1772 		/*
1773 		 * Likewise, if we have lost the walwriter process, try to start a new
1774 		 * one.  But this is needed only in normal operation (else we cannot
1775 		 * be writing any new WAL).
1776 		 */
1777 		if (WalWriterPID == 0 && pmState == PM_RUN)
1778 			WalWriterPID = StartWalWriter();
1779 
1780 		/*
1781 		 * If we have lost the autovacuum launcher, try to start a new one. We
1782 		 * don't want autovacuum to run in binary upgrade mode because
1783 		 * autovacuum might update relfrozenxid for empty tables before the
1784 		 * physical files are put in place.
1785 		 */
1786 		if (!IsBinaryUpgrade && AutoVacPID == 0 &&
1787 			(AutoVacuumingActive() || start_autovac_launcher) &&
1788 			pmState == PM_RUN)
1789 		{
1790 			AutoVacPID = StartAutoVacLauncher();
1791 			if (AutoVacPID != 0)
1792 				start_autovac_launcher = false; /* signal processed */
1793 		}
1794 
1795 		/* If we have lost the stats collector, try to start a new one */
1796 		if (PgStatPID == 0 &&
1797 			(pmState == PM_RUN || pmState == PM_HOT_STANDBY))
1798 			PgStatPID = pgstat_start();
1799 
1800 		/* If we have lost the archiver, try to start a new one. */
1801 		if (PgArchPID == 0 && PgArchStartupAllowed())
1802 			PgArchPID = pgarch_start();
1803 
1804 		/* If we need to signal the autovacuum launcher, do so now */
1805 		if (avlauncher_needs_signal)
1806 		{
1807 			avlauncher_needs_signal = false;
1808 			if (AutoVacPID != 0)
1809 				kill(AutoVacPID, SIGUSR2);
1810 		}
1811 
1812 		/* If we need to start a WAL receiver, try to do that now */
1813 		if (WalReceiverRequested)
1814 			MaybeStartWalReceiver();
1815 
1816 		/* Get other worker processes running, if needed */
1817 		if (StartWorkerNeeded || HaveCrashedWorker)
1818 			maybe_start_bgworkers();
1819 
1820 #ifdef HAVE_PTHREAD_IS_THREADED_NP
1821 
1822 		/*
1823 		 * With assertions enabled, check regularly for appearance of
1824 		 * additional threads.  All builds check at start and exit.
1825 		 */
1826 		Assert(pthread_is_threaded_np() == 0);
1827 #endif
1828 
1829 		/*
1830 		 * Lastly, check to see if it's time to do some things that we don't
1831 		 * want to do every single time through the loop, because they're a
1832 		 * bit expensive.  Note that there's up to a minute of slop in when
1833 		 * these tasks will be performed, since DetermineSleepTime() will let
1834 		 * us sleep at most that long; except for SIGKILL timeout which has
1835 		 * special-case logic there.
1836 		 */
1837 		now = time(NULL);
1838 
1839 		/*
1840 		 * If we already sent SIGQUIT to children and they are slow to shut
1841 		 * down, it's time to send them SIGKILL.  This doesn't happen
1842 		 * normally, but under certain conditions backends can get stuck while
1843 		 * shutting down.  This is a last measure to get them unwedged.
1844 		 *
1845 		 * Note we also do this during recovery from a process crash.
1846 		 */
1847 		if ((Shutdown >= ImmediateShutdown || (FatalError && !SendStop)) &&
1848 			AbortStartTime != 0 &&
1849 			(now - AbortStartTime) >= SIGKILL_CHILDREN_AFTER_SECS)
1850 		{
1851 			/* We were gentle with them before. Not anymore */
1852 			TerminateChildren(SIGKILL);
1853 			/* reset flag so we don't SIGKILL again */
1854 			AbortStartTime = 0;
1855 		}
1856 
1857 		/*
1858 		 * Once a minute, verify that postmaster.pid hasn't been removed or
1859 		 * overwritten.  If it has, we force a shutdown.  This avoids having
1860 		 * postmasters and child processes hanging around after their database
1861 		 * is gone, and maybe causing problems if a new database cluster is
1862 		 * created in the same place.  It also provides some protection
1863 		 * against a DBA foolishly removing postmaster.pid and manually
1864 		 * starting a new postmaster.  Data corruption is likely to ensue from
1865 		 * that anyway, but we can minimize the damage by aborting ASAP.
1866 		 */
1867 		if (now - last_lockfile_recheck_time >= 1 * SECS_PER_MINUTE)
1868 		{
1869 			if (!RecheckDataDirLockFile())
1870 			{
1871 				ereport(LOG,
1872 						(errmsg("performing immediate shutdown because data directory lock file is invalid")));
1873 				kill(MyProcPid, SIGQUIT);
1874 			}
1875 			last_lockfile_recheck_time = now;
1876 		}
1877 
1878 		/*
1879 		 * Touch Unix socket and lock files every 58 minutes, to ensure that
1880 		 * they are not removed by overzealous /tmp-cleaning tasks.  We assume
1881 		 * no one runs cleaners with cutoff times of less than an hour ...
1882 		 */
1883 		if (now - last_touch_time >= 58 * SECS_PER_MINUTE)
1884 		{
1885 			TouchSocketFiles();
1886 			TouchSocketLockFiles();
1887 			last_touch_time = now;
1888 		}
1889 	}
1890 }
1891 
1892 /*
1893  * Initialise the masks for select() for the ports we are listening on.
1894  * Return the number of sockets to listen on.
1895  */
1896 static int
initMasks(fd_set * rmask)1897 initMasks(fd_set *rmask)
1898 {
1899 	int			maxsock = -1;
1900 	int			i;
1901 
1902 	FD_ZERO(rmask);
1903 
1904 	for (i = 0; i < MAXLISTEN; i++)
1905 	{
1906 		int			fd = ListenSocket[i];
1907 
1908 		if (fd == PGINVALID_SOCKET)
1909 			break;
1910 		FD_SET(fd, rmask);
1911 
1912 		if (fd > maxsock)
1913 			maxsock = fd;
1914 	}
1915 
1916 	return maxsock + 1;
1917 }
1918 
1919 
1920 /*
1921  * Read a client's startup packet and do something according to it.
1922  *
1923  * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
1924  * not return at all.
1925  *
1926  * (Note that ereport(FATAL) stuff is sent to the client, so only use it
1927  * if that's what you want.  Return STATUS_ERROR if you don't want to
1928  * send anything to the client, which would typically be appropriate
1929  * if we detect a communications failure.)
1930  */
1931 static int
ProcessStartupPacket(Port * port,bool SSLdone)1932 ProcessStartupPacket(Port *port, bool SSLdone)
1933 {
1934 	int32		len;
1935 	void	   *buf;
1936 	ProtocolVersion proto;
1937 	MemoryContext oldcontext;
1938 
1939 	pq_startmsgread();
1940 	if (pq_getbytes((char *) &len, 4) == EOF)
1941 	{
1942 		/*
1943 		 * EOF after SSLdone probably means the client didn't like our
1944 		 * response to NEGOTIATE_SSL_CODE.  That's not an error condition, so
1945 		 * don't clutter the log with a complaint.
1946 		 */
1947 		if (!SSLdone)
1948 			ereport(COMMERROR,
1949 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
1950 					 errmsg("incomplete startup packet")));
1951 		return STATUS_ERROR;
1952 	}
1953 
1954 	len = ntohl(len);
1955 	len -= 4;
1956 
1957 	if (len < (int32) sizeof(ProtocolVersion) ||
1958 		len > MAX_STARTUP_PACKET_LENGTH)
1959 	{
1960 		ereport(COMMERROR,
1961 				(errcode(ERRCODE_PROTOCOL_VIOLATION),
1962 				 errmsg("invalid length of startup packet")));
1963 		return STATUS_ERROR;
1964 	}
1965 
1966 	/*
1967 	 * Allocate at least the size of an old-style startup packet, plus one
1968 	 * extra byte, and make sure all are zeroes.  This ensures we will have
1969 	 * null termination of all strings, in both fixed- and variable-length
1970 	 * packet layouts.
1971 	 */
1972 	if (len <= (int32) sizeof(StartupPacket))
1973 		buf = palloc0(sizeof(StartupPacket) + 1);
1974 	else
1975 		buf = palloc0(len + 1);
1976 
1977 	if (pq_getbytes(buf, len) == EOF)
1978 	{
1979 		ereport(COMMERROR,
1980 				(errcode(ERRCODE_PROTOCOL_VIOLATION),
1981 				 errmsg("incomplete startup packet")));
1982 		return STATUS_ERROR;
1983 	}
1984 	pq_endmsgread();
1985 
1986 	/*
1987 	 * The first field is either a protocol version number or a special
1988 	 * request code.
1989 	 */
1990 	port->proto = proto = ntohl(*((ProtocolVersion *) buf));
1991 
1992 	if (proto == CANCEL_REQUEST_CODE)
1993 	{
1994 		processCancelRequest(port, buf);
1995 		/* Not really an error, but we don't want to proceed further */
1996 		return STATUS_ERROR;
1997 	}
1998 
1999 	if (proto == NEGOTIATE_SSL_CODE && !SSLdone)
2000 	{
2001 		char		SSLok;
2002 
2003 #ifdef USE_SSL
2004 		/* No SSL when disabled or on Unix sockets */
2005 		if (!EnableSSL || IS_AF_UNIX(port->laddr.addr.ss_family))
2006 			SSLok = 'N';
2007 		else
2008 			SSLok = 'S';		/* Support for SSL */
2009 #else
2010 		SSLok = 'N';			/* No support for SSL */
2011 #endif
2012 
2013 retry1:
2014 		if (send(port->sock, &SSLok, 1, 0) != 1)
2015 		{
2016 			if (errno == EINTR)
2017 				goto retry1;	/* if interrupted, just retry */
2018 			ereport(COMMERROR,
2019 					(errcode_for_socket_access(),
2020 					 errmsg("failed to send SSL negotiation response: %m")));
2021 			return STATUS_ERROR;	/* close the connection */
2022 		}
2023 
2024 #ifdef USE_SSL
2025 		if (SSLok == 'S' && secure_open_server(port) == -1)
2026 			return STATUS_ERROR;
2027 #endif
2028 
2029 		/*
2030 		 * At this point we should have no data already buffered.  If we do,
2031 		 * it was received before we performed the SSL handshake, so it wasn't
2032 		 * encrypted and indeed may have been injected by a man-in-the-middle.
2033 		 * We report this case to the client.
2034 		 */
2035 		if (pq_buffer_has_data())
2036 			ereport(FATAL,
2037 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
2038 					 errmsg("received unencrypted data after SSL request"),
2039 					 errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
2040 
2041 		/* regular startup packet, cancel, etc packet should follow... */
2042 		/* but not another SSL negotiation request */
2043 		return ProcessStartupPacket(port, true);
2044 	}
2045 
2046 	/* Could add additional special packet types here */
2047 
2048 	/*
2049 	 * Set FrontendProtocol now so that ereport() knows what format to send if
2050 	 * we fail during startup.
2051 	 */
2052 	FrontendProtocol = proto;
2053 
2054 	/* Check that the major protocol version is in range. */
2055 	if (PG_PROTOCOL_MAJOR(proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) ||
2056 		PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST))
2057 		ereport(FATAL,
2058 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2059 				 errmsg("unsupported frontend protocol %u.%u: server supports %u.0 to %u.%u",
2060 						PG_PROTOCOL_MAJOR(proto), PG_PROTOCOL_MINOR(proto),
2061 						PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST),
2062 						PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST),
2063 						PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST))));
2064 
2065 	/*
2066 	 * Now fetch parameters out of startup packet and save them into the Port
2067 	 * structure.  All data structures attached to the Port struct must be
2068 	 * allocated in TopMemoryContext so that they will remain available in a
2069 	 * running backend (even after PostmasterContext is destroyed).  We need
2070 	 * not worry about leaking this storage on failure, since we aren't in the
2071 	 * postmaster process anymore.
2072 	 */
2073 	oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2074 
2075 	if (PG_PROTOCOL_MAJOR(proto) >= 3)
2076 	{
2077 		int32		offset = sizeof(ProtocolVersion);
2078 		List	   *unrecognized_protocol_options = NIL;
2079 
2080 		/*
2081 		 * Scan packet body for name/option pairs.  We can assume any string
2082 		 * beginning within the packet body is null-terminated, thanks to
2083 		 * zeroing extra byte above.
2084 		 */
2085 		port->guc_options = NIL;
2086 
2087 		while (offset < len)
2088 		{
2089 			char	   *nameptr = ((char *) buf) + offset;
2090 			int32		valoffset;
2091 			char	   *valptr;
2092 
2093 			if (*nameptr == '\0')
2094 				break;			/* found packet terminator */
2095 			valoffset = offset + strlen(nameptr) + 1;
2096 			if (valoffset >= len)
2097 				break;			/* missing value, will complain below */
2098 			valptr = ((char *) buf) + valoffset;
2099 
2100 			if (strcmp(nameptr, "database") == 0)
2101 				port->database_name = pstrdup(valptr);
2102 			else if (strcmp(nameptr, "user") == 0)
2103 				port->user_name = pstrdup(valptr);
2104 			else if (strcmp(nameptr, "options") == 0)
2105 				port->cmdline_options = pstrdup(valptr);
2106 			else if (strcmp(nameptr, "replication") == 0)
2107 			{
2108 				/*
2109 				 * Due to backward compatibility concerns the replication
2110 				 * parameter is a hybrid beast which allows the value to be
2111 				 * either boolean or the string 'database'. The latter
2112 				 * connects to a specific database which is e.g. required for
2113 				 * logical decoding while.
2114 				 */
2115 				if (strcmp(valptr, "database") == 0)
2116 				{
2117 					am_walsender = true;
2118 					am_db_walsender = true;
2119 				}
2120 				else if (!parse_bool(valptr, &am_walsender))
2121 					ereport(FATAL,
2122 							(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2123 						 errmsg("invalid value for parameter \"%s\": \"%s\"",
2124 								"replication",
2125 								valptr),
2126 							 errhint("Valid values are: \"false\", 0, \"true\", 1, \"database\".")));
2127 			}
2128 			else if (strncmp(nameptr, "_pq_.", 5) == 0)
2129 			{
2130 				/*
2131 				 * Any option beginning with _pq_. is reserved for use as a
2132 				 * protocol-level option, but at present no such options are
2133 				 * defined.
2134 				 */
2135 				unrecognized_protocol_options =
2136 					lappend(unrecognized_protocol_options, pstrdup(nameptr));
2137 			}
2138 			else
2139 			{
2140 				/* Assume it's a generic GUC option */
2141 				port->guc_options = lappend(port->guc_options,
2142 											pstrdup(nameptr));
2143 				port->guc_options = lappend(port->guc_options,
2144 											pstrdup(valptr));
2145 			}
2146 			offset = valoffset + strlen(valptr) + 1;
2147 		}
2148 
2149 		/*
2150 		 * If we didn't find a packet terminator exactly at the end of the
2151 		 * given packet length, complain.
2152 		 */
2153 		if (offset != len - 1)
2154 			ereport(FATAL,
2155 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
2156 					 errmsg("invalid startup packet layout: expected terminator as last byte")));
2157 
2158 		/*
2159 		 * If the client requested a newer protocol version or if the client
2160 		 * requested any protocol options we didn't recognize, let them know
2161 		 * the newest minor protocol version we do support and the names of any
2162 		 * unrecognized options.
2163 		 */
2164 		if (PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST) ||
2165 			unrecognized_protocol_options != NIL)
2166 			SendNegotiateProtocolVersion(unrecognized_protocol_options);
2167 	}
2168 	else
2169 	{
2170 		/*
2171 		 * Get the parameters from the old-style, fixed-width-fields startup
2172 		 * packet as C strings.  The packet destination was cleared first so a
2173 		 * short packet has zeros silently added.  We have to be prepared to
2174 		 * truncate the pstrdup result for oversize fields, though.
2175 		 */
2176 		StartupPacket *packet = (StartupPacket *) buf;
2177 
2178 		port->database_name = pstrdup(packet->database);
2179 		if (strlen(port->database_name) > sizeof(packet->database))
2180 			port->database_name[sizeof(packet->database)] = '\0';
2181 		port->user_name = pstrdup(packet->user);
2182 		if (strlen(port->user_name) > sizeof(packet->user))
2183 			port->user_name[sizeof(packet->user)] = '\0';
2184 		port->cmdline_options = pstrdup(packet->options);
2185 		if (strlen(port->cmdline_options) > sizeof(packet->options))
2186 			port->cmdline_options[sizeof(packet->options)] = '\0';
2187 		port->guc_options = NIL;
2188 	}
2189 
2190 	/* Check a user name was given. */
2191 	if (port->user_name == NULL || port->user_name[0] == '\0')
2192 		ereport(FATAL,
2193 				(errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
2194 			 errmsg("no PostgreSQL user name specified in startup packet")));
2195 
2196 	/* The database defaults to the user name. */
2197 	if (port->database_name == NULL || port->database_name[0] == '\0')
2198 		port->database_name = pstrdup(port->user_name);
2199 
2200 	if (Db_user_namespace)
2201 	{
2202 		/*
2203 		 * If user@, it is a global user, remove '@'. We only want to do this
2204 		 * if there is an '@' at the end and no earlier in the user string or
2205 		 * they may fake as a local user of another database attaching to this
2206 		 * database.
2207 		 */
2208 		if (strchr(port->user_name, '@') ==
2209 			port->user_name + strlen(port->user_name) - 1)
2210 			*strchr(port->user_name, '@') = '\0';
2211 		else
2212 		{
2213 			/* Append '@' and dbname */
2214 			port->user_name = psprintf("%s@%s", port->user_name, port->database_name);
2215 		}
2216 	}
2217 
2218 	/*
2219 	 * Truncate given database and user names to length of a Postgres name.
2220 	 * This avoids lookup failures when overlength names are given.
2221 	 */
2222 	if (strlen(port->database_name) >= NAMEDATALEN)
2223 		port->database_name[NAMEDATALEN - 1] = '\0';
2224 	if (strlen(port->user_name) >= NAMEDATALEN)
2225 		port->user_name[NAMEDATALEN - 1] = '\0';
2226 
2227 	/*
2228 	 * Normal walsender backends, e.g. for streaming replication, are not
2229 	 * connected to a particular database. But walsenders used for logical
2230 	 * replication need to connect to a specific database. We allow streaming
2231 	 * replication commands to be issued even if connected to a database as it
2232 	 * can make sense to first make a basebackup and then stream changes
2233 	 * starting from that.
2234 	 */
2235 	if (am_walsender && !am_db_walsender)
2236 		port->database_name[0] = '\0';
2237 
2238 	/*
2239 	 * Done putting stuff in TopMemoryContext.
2240 	 */
2241 	MemoryContextSwitchTo(oldcontext);
2242 
2243 	/*
2244 	 * If we're going to reject the connection due to database state, say so
2245 	 * now instead of wasting cycles on an authentication exchange. (This also
2246 	 * allows a pg_ping utility to be written.)
2247 	 */
2248 	switch (port->canAcceptConnections)
2249 	{
2250 		case CAC_STARTUP:
2251 			ereport(FATAL,
2252 					(errcode(ERRCODE_CANNOT_CONNECT_NOW),
2253 					 errmsg("the database system is starting up")));
2254 			break;
2255 		case CAC_SHUTDOWN:
2256 			ereport(FATAL,
2257 					(errcode(ERRCODE_CANNOT_CONNECT_NOW),
2258 					 errmsg("the database system is shutting down")));
2259 			break;
2260 		case CAC_RECOVERY:
2261 			ereport(FATAL,
2262 					(errcode(ERRCODE_CANNOT_CONNECT_NOW),
2263 					 errmsg("the database system is in recovery mode")));
2264 			break;
2265 		case CAC_TOOMANY:
2266 			ereport(FATAL,
2267 					(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
2268 					 errmsg("sorry, too many clients already")));
2269 			break;
2270 		case CAC_SUPERUSER:
2271 			/* OK for now, will check in InitPostgres */
2272 			break;
2273 		case CAC_OK:
2274 			break;
2275 	}
2276 
2277 	return STATUS_OK;
2278 }
2279 
2280 /*
2281  * Send a NegotiateProtocolVersion to the client.  This lets the client know
2282  * that they have requested a newer minor protocol version than we are able
2283  * to speak.  We'll speak the highest version we know about; the client can,
2284  * of course, abandon the connection if that's a problem.
2285  *
2286  * We also include in the response a list of protocol options we didn't
2287  * understand.  This allows clients to include optional parameters that might
2288  * be present either in newer protocol versions or third-party protocol
2289  * extensions without fear of having to reconnect if those options are not
2290  * understood, while at the same time making certain that the client is aware
2291  * of which options were actually accepted.
2292  */
2293 static void
SendNegotiateProtocolVersion(List * unrecognized_protocol_options)2294 SendNegotiateProtocolVersion(List *unrecognized_protocol_options)
2295 {
2296 	StringInfoData buf;
2297 	ListCell   *lc;
2298 
2299 	pq_beginmessage(&buf, 'v'); /* NegotiateProtocolVersion */
2300 	pq_sendint(&buf, PG_PROTOCOL_LATEST, 4);
2301 	pq_sendint(&buf, list_length(unrecognized_protocol_options), 4);
2302 	foreach(lc, unrecognized_protocol_options)
2303 		pq_sendstring(&buf, lfirst(lc));
2304 	pq_endmessage(&buf);
2305 
2306 	/* no need to flush, some other message will follow */
2307 }
2308 
2309 /*
2310  * The client has sent a cancel request packet, not a normal
2311  * start-a-new-connection packet.  Perform the necessary processing.
2312  * Nothing is sent back to the client.
2313  */
2314 static void
processCancelRequest(Port * port,void * pkt)2315 processCancelRequest(Port *port, void *pkt)
2316 {
2317 	CancelRequestPacket *canc = (CancelRequestPacket *) pkt;
2318 	int			backendPID;
2319 	long		cancelAuthCode;
2320 	Backend    *bp;
2321 
2322 #ifndef EXEC_BACKEND
2323 	dlist_iter	iter;
2324 #else
2325 	int			i;
2326 #endif
2327 
2328 	backendPID = (int) ntohl(canc->backendPID);
2329 	cancelAuthCode = (long) ntohl(canc->cancelAuthCode);
2330 
2331 	/*
2332 	 * See if we have a matching backend.  In the EXEC_BACKEND case, we can no
2333 	 * longer access the postmaster's own backend list, and must rely on the
2334 	 * duplicate array in shared memory.
2335 	 */
2336 #ifndef EXEC_BACKEND
2337 	dlist_foreach(iter, &BackendList)
2338 	{
2339 		bp = dlist_container(Backend, elem, iter.cur);
2340 #else
2341 	for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
2342 	{
2343 		bp = (Backend *) &ShmemBackendArray[i];
2344 #endif
2345 		if (bp->pid == backendPID)
2346 		{
2347 			if (bp->cancel_key == cancelAuthCode)
2348 			{
2349 				/* Found a match; signal that backend to cancel current op */
2350 				ereport(DEBUG2,
2351 						(errmsg_internal("processing cancel request: sending SIGINT to process %d",
2352 										 backendPID)));
2353 				signal_child(bp->pid, SIGINT);
2354 			}
2355 			else
2356 				/* Right PID, wrong key: no way, Jose */
2357 				ereport(LOG,
2358 						(errmsg("wrong key in cancel request for process %d",
2359 								backendPID)));
2360 			return;
2361 		}
2362 	}
2363 
2364 	/* No matching backend */
2365 	ereport(LOG,
2366 			(errmsg("PID %d in cancel request did not match any process",
2367 					backendPID)));
2368 }
2369 
2370 /*
2371  * canAcceptConnections --- check to see if database state allows connections
2372  * of the specified type.  backend_type can be BACKEND_TYPE_NORMAL,
2373  * BACKEND_TYPE_AUTOVAC, or BACKEND_TYPE_BGWORKER.  (Note that we don't yet
2374  * know whether a NORMAL connection might turn into a walsender.)
2375  */
2376 static CAC_state
2377 canAcceptConnections(int backend_type)
2378 {
2379 	CAC_state	result = CAC_OK;
2380 
2381 	/*
2382 	 * Can't start backends when in startup/shutdown/inconsistent recovery
2383 	 * state.  We treat autovac workers the same as user backends for this
2384 	 * purpose.  However, bgworkers are excluded from this test; we expect
2385 	 * bgworker_should_start_now() decided whether the DB state allows them.
2386 	 */
2387 	if (pmState != PM_RUN && pmState != PM_HOT_STANDBY &&
2388 		backend_type != BACKEND_TYPE_BGWORKER)
2389 	{
2390 		if (Shutdown > NoShutdown)
2391 			return CAC_SHUTDOWN;	/* shutdown is pending */
2392 		else if (!FatalError &&
2393 				 (pmState == PM_STARTUP ||
2394 				  pmState == PM_RECOVERY))
2395 			return CAC_STARTUP; /* normal startup */
2396 		else
2397 			return CAC_RECOVERY;	/* else must be crash recovery */
2398 	}
2399 
2400 	/*
2401 	 * "Smart shutdown" restrictions are applied only to normal connections,
2402 	 * not to autovac workers or bgworkers.  When only superusers can connect,
2403 	 * we return CAC_SUPERUSER to indicate that superuserness must be checked
2404 	 * later.  Note that neither CAC_OK nor CAC_SUPERUSER can safely be
2405 	 * returned until we have checked for too many children.
2406 	 */
2407 	if (connsAllowed != ALLOW_ALL_CONNS &&
2408 		backend_type == BACKEND_TYPE_NORMAL)
2409 	{
2410 		if (connsAllowed == ALLOW_SUPERUSER_CONNS)
2411 			result = CAC_SUPERUSER; /* allow superusers only */
2412 		else
2413 			return CAC_SHUTDOWN;	/* shutdown is pending */
2414 	}
2415 
2416 	/*
2417 	 * Don't start too many children.
2418 	 *
2419 	 * We allow more connections here than we can have backends because some
2420 	 * might still be authenticating; they might fail auth, or some existing
2421 	 * backend might exit before the auth cycle is completed.  The exact
2422 	 * MaxBackends limit is enforced when a new backend tries to join the
2423 	 * shared-inval backend array.
2424 	 *
2425 	 * The limit here must match the sizes of the per-child-process arrays;
2426 	 * see comments for MaxLivePostmasterChildren().
2427 	 */
2428 	if (CountChildren(BACKEND_TYPE_ALL) >= MaxLivePostmasterChildren())
2429 		result = CAC_TOOMANY;
2430 
2431 	return result;
2432 }
2433 
2434 
2435 /*
2436  * ConnCreate -- create a local connection data structure
2437  *
2438  * Returns NULL on failure, other than out-of-memory which is fatal.
2439  */
2440 static Port *
2441 ConnCreate(int serverFd)
2442 {
2443 	Port	   *port;
2444 
2445 	if (!(port = (Port *) calloc(1, sizeof(Port))))
2446 	{
2447 		ereport(LOG,
2448 				(errcode(ERRCODE_OUT_OF_MEMORY),
2449 				 errmsg("out of memory")));
2450 		ExitPostmaster(1);
2451 	}
2452 
2453 	if (StreamConnection(serverFd, port) != STATUS_OK)
2454 	{
2455 		if (port->sock != PGINVALID_SOCKET)
2456 			StreamClose(port->sock);
2457 		ConnFree(port);
2458 		return NULL;
2459 	}
2460 
2461 	/*
2462 	 * Precompute password salt values to use for this connection. It's
2463 	 * slightly annoying to do this long in advance of knowing whether we'll
2464 	 * need 'em or not, but we must do the random() calls before we fork, not
2465 	 * after.  Else the postmaster's random sequence won't get advanced, and
2466 	 * all backends would end up using the same salt...
2467 	 */
2468 	RandomSalt(port->md5Salt);
2469 
2470 	/*
2471 	 * Allocate GSSAPI specific state struct
2472 	 */
2473 #ifndef EXEC_BACKEND
2474 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
2475 	port->gss = (pg_gssinfo *) calloc(1, sizeof(pg_gssinfo));
2476 	if (!port->gss)
2477 	{
2478 		ereport(LOG,
2479 				(errcode(ERRCODE_OUT_OF_MEMORY),
2480 				 errmsg("out of memory")));
2481 		ExitPostmaster(1);
2482 	}
2483 #endif
2484 #endif
2485 
2486 	return port;
2487 }
2488 
2489 
2490 /*
2491  * ConnFree -- free a local connection data structure
2492  */
2493 static void
2494 ConnFree(Port *conn)
2495 {
2496 #ifdef USE_SSL
2497 	secure_close(conn);
2498 #endif
2499 	if (conn->gss)
2500 		free(conn->gss);
2501 	free(conn);
2502 }
2503 
2504 
2505 /*
2506  * ClosePostmasterPorts -- close all the postmaster's open sockets
2507  *
2508  * This is called during child process startup to release file descriptors
2509  * that are not needed by that child process.  The postmaster still has
2510  * them open, of course.
2511  *
2512  * Note: we pass am_syslogger as a boolean because we don't want to set
2513  * the global variable yet when this is called.
2514  */
2515 void
2516 ClosePostmasterPorts(bool am_syslogger)
2517 {
2518 	int			i;
2519 
2520 #ifndef WIN32
2521 
2522 	/*
2523 	 * Close the write end of postmaster death watch pipe. It's important to
2524 	 * do this as early as possible, so that if postmaster dies, others won't
2525 	 * think that it's still running because we're holding the pipe open.
2526 	 */
2527 	if (close(postmaster_alive_fds[POSTMASTER_FD_OWN]))
2528 		ereport(FATAL,
2529 				(errcode_for_file_access(),
2530 				 errmsg_internal("could not close postmaster death monitoring pipe in child process: %m")));
2531 	postmaster_alive_fds[POSTMASTER_FD_OWN] = -1;
2532 #endif
2533 
2534 	/* Close the listen sockets */
2535 	for (i = 0; i < MAXLISTEN; i++)
2536 	{
2537 		if (ListenSocket[i] != PGINVALID_SOCKET)
2538 		{
2539 			StreamClose(ListenSocket[i]);
2540 			ListenSocket[i] = PGINVALID_SOCKET;
2541 		}
2542 	}
2543 
2544 	/* If using syslogger, close the read side of the pipe */
2545 	if (!am_syslogger)
2546 	{
2547 #ifndef WIN32
2548 		if (syslogPipe[0] >= 0)
2549 			close(syslogPipe[0]);
2550 		syslogPipe[0] = -1;
2551 #else
2552 		if (syslogPipe[0])
2553 			CloseHandle(syslogPipe[0]);
2554 		syslogPipe[0] = 0;
2555 #endif
2556 	}
2557 
2558 #ifdef USE_BONJOUR
2559 	/* If using Bonjour, close the connection to the mDNS daemon */
2560 	if (bonjour_sdref)
2561 		close(DNSServiceRefSockFD(bonjour_sdref));
2562 #endif
2563 }
2564 
2565 
2566 /*
2567  * reset_shared -- reset shared memory and semaphores
2568  */
2569 static void
2570 reset_shared(int port)
2571 {
2572 	/*
2573 	 * Create or re-create shared memory and semaphores.
2574 	 *
2575 	 * Note: in each "cycle of life" we will normally assign the same IPC keys
2576 	 * (if using SysV shmem and/or semas), since the port number is used to
2577 	 * determine IPC keys.  This helps ensure that we will clean up dead IPC
2578 	 * objects if the postmaster crashes and is restarted.
2579 	 */
2580 	CreateSharedMemoryAndSemaphores(port);
2581 }
2582 
2583 
2584 /*
2585  * SIGHUP -- reread config files, and tell children to do same
2586  */
2587 static void
2588 SIGHUP_handler(SIGNAL_ARGS)
2589 {
2590 	int			save_errno = errno;
2591 
2592 	/*
2593 	 * We rely on the signal mechanism to have blocked all signals ... except
2594 	 * on Windows, which lacks sigaction(), so we have to do it manually.
2595 	 */
2596 #ifdef WIN32
2597 	PG_SETMASK(&BlockSig);
2598 #endif
2599 
2600 	if (Shutdown <= SmartShutdown)
2601 	{
2602 		ereport(LOG,
2603 				(errmsg("received SIGHUP, reloading configuration files")));
2604 		ProcessConfigFile(PGC_SIGHUP);
2605 		SignalChildren(SIGHUP);
2606 		if (StartupPID != 0)
2607 			signal_child(StartupPID, SIGHUP);
2608 		if (BgWriterPID != 0)
2609 			signal_child(BgWriterPID, SIGHUP);
2610 		if (CheckpointerPID != 0)
2611 			signal_child(CheckpointerPID, SIGHUP);
2612 		if (WalWriterPID != 0)
2613 			signal_child(WalWriterPID, SIGHUP);
2614 		if (WalReceiverPID != 0)
2615 			signal_child(WalReceiverPID, SIGHUP);
2616 		if (AutoVacPID != 0)
2617 			signal_child(AutoVacPID, SIGHUP);
2618 		if (PgArchPID != 0)
2619 			signal_child(PgArchPID, SIGHUP);
2620 		if (SysLoggerPID != 0)
2621 			signal_child(SysLoggerPID, SIGHUP);
2622 		if (PgStatPID != 0)
2623 			signal_child(PgStatPID, SIGHUP);
2624 
2625 		/* Reload authentication config files too */
2626 		if (!load_hba())
2627 			ereport(WARNING,
2628 					(errmsg("pg_hba.conf not reloaded")));
2629 
2630 		if (!load_ident())
2631 			ereport(WARNING,
2632 					(errmsg("pg_ident.conf not reloaded")));
2633 
2634 #ifdef EXEC_BACKEND
2635 		/* Update the starting-point file for future children */
2636 		write_nondefault_variables(PGC_SIGHUP);
2637 #endif
2638 	}
2639 
2640 #ifdef WIN32
2641 	PG_SETMASK(&UnBlockSig);
2642 #endif
2643 
2644 	errno = save_errno;
2645 }
2646 
2647 
2648 /*
2649  * pmdie -- signal handler for processing various postmaster signals.
2650  */
2651 static void
2652 pmdie(SIGNAL_ARGS)
2653 {
2654 	int			save_errno = errno;
2655 
2656 	/*
2657 	 * We rely on the signal mechanism to have blocked all signals ... except
2658 	 * on Windows, which lacks sigaction(), so we have to do it manually.
2659 	 */
2660 #ifdef WIN32
2661 	PG_SETMASK(&BlockSig);
2662 #endif
2663 
2664 	ereport(DEBUG2,
2665 			(errmsg_internal("postmaster received signal %d",
2666 							 postgres_signal_arg)));
2667 
2668 	switch (postgres_signal_arg)
2669 	{
2670 		case SIGTERM:
2671 
2672 			/*
2673 			 * Smart Shutdown:
2674 			 *
2675 			 * Wait for children to end their work, then shut down.
2676 			 */
2677 			if (Shutdown >= SmartShutdown)
2678 				break;
2679 			Shutdown = SmartShutdown;
2680 			ereport(LOG,
2681 					(errmsg("received smart shutdown request")));
2682 #ifdef USE_SYSTEMD
2683 			sd_notify(0, "STOPPING=1");
2684 #endif
2685 
2686 			/*
2687 			 * If we reached normal running, we have to wait for any online
2688 			 * backup mode to end; otherwise go straight to waiting for client
2689 			 * backends to exit.  (The difference is that in the former state,
2690 			 * we'll still let in new superuser clients, so that somebody can
2691 			 * end the online backup mode.)  If already in PM_STOP_BACKENDS or
2692 			 * a later state, do not change it.
2693 			 */
2694 			if (pmState == PM_RUN)
2695 				connsAllowed = ALLOW_SUPERUSER_CONNS;
2696 			else if (pmState == PM_HOT_STANDBY)
2697 				connsAllowed = ALLOW_NO_CONNS;
2698 			else if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
2699 			{
2700 				/* There should be no clients, so proceed to stop children */
2701 				pmState = PM_STOP_BACKENDS;
2702 			}
2703 
2704 			/*
2705 			 * Now wait for online backup mode to end and backends to exit. If
2706 			 * that is already the case, PostmasterStateMachine will take the
2707 			 * next step.
2708 			 */
2709 			PostmasterStateMachine();
2710 			break;
2711 
2712 		case SIGINT:
2713 
2714 			/*
2715 			 * Fast Shutdown:
2716 			 *
2717 			 * Abort all children with SIGTERM (rollback active transactions
2718 			 * and exit) and shut down when they are gone.
2719 			 */
2720 			if (Shutdown >= FastShutdown)
2721 				break;
2722 			Shutdown = FastShutdown;
2723 			ereport(LOG,
2724 					(errmsg("received fast shutdown request")));
2725 #ifdef USE_SYSTEMD
2726 			sd_notify(0, "STOPPING=1");
2727 #endif
2728 
2729 			if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
2730 			{
2731 				/* Just shut down background processes silently */
2732 				pmState = PM_STOP_BACKENDS;
2733 			}
2734 			else if (pmState == PM_RUN ||
2735 					 pmState == PM_HOT_STANDBY)
2736 			{
2737 				/* Report that we're about to zap live client sessions */
2738 				ereport(LOG,
2739 						(errmsg("aborting any active transactions")));
2740 				pmState = PM_STOP_BACKENDS;
2741 			}
2742 
2743 			/*
2744 			 * PostmasterStateMachine will issue any necessary signals, or
2745 			 * take the next step if no child processes need to be killed.
2746 			 */
2747 			PostmasterStateMachine();
2748 			break;
2749 
2750 		case SIGQUIT:
2751 
2752 			/*
2753 			 * Immediate Shutdown:
2754 			 *
2755 			 * abort all children with SIGQUIT, wait for them to exit,
2756 			 * terminate remaining ones with SIGKILL, then exit without
2757 			 * attempt to properly shut down the data base system.
2758 			 */
2759 			if (Shutdown >= ImmediateShutdown)
2760 				break;
2761 			Shutdown = ImmediateShutdown;
2762 			ereport(LOG,
2763 					(errmsg("received immediate shutdown request")));
2764 #ifdef USE_SYSTEMD
2765 			sd_notify(0, "STOPPING=1");
2766 #endif
2767 
2768 			TerminateChildren(SIGQUIT);
2769 			pmState = PM_WAIT_BACKENDS;
2770 
2771 			/* set stopwatch for them to die */
2772 			AbortStartTime = time(NULL);
2773 
2774 			/*
2775 			 * Now wait for backends to exit.  If there are none,
2776 			 * PostmasterStateMachine will take the next step.
2777 			 */
2778 			PostmasterStateMachine();
2779 			break;
2780 	}
2781 
2782 #ifdef WIN32
2783 	PG_SETMASK(&UnBlockSig);
2784 #endif
2785 
2786 	errno = save_errno;
2787 }
2788 
2789 /*
2790  * Reaper -- signal handler to cleanup after a child process dies.
2791  */
2792 static void
2793 reaper(SIGNAL_ARGS)
2794 {
2795 	int			save_errno = errno;
2796 	int			pid;			/* process id of dead child process */
2797 	int			exitstatus;		/* its exit status */
2798 
2799 	/*
2800 	 * We rely on the signal mechanism to have blocked all signals ... except
2801 	 * on Windows, which lacks sigaction(), so we have to do it manually.
2802 	 */
2803 #ifdef WIN32
2804 	PG_SETMASK(&BlockSig);
2805 #endif
2806 
2807 	ereport(DEBUG4,
2808 			(errmsg_internal("reaping dead processes")));
2809 
2810 	while ((pid = waitpid(-1, &exitstatus, WNOHANG)) > 0)
2811 	{
2812 		/*
2813 		 * Check if this child was a startup process.
2814 		 */
2815 		if (pid == StartupPID)
2816 		{
2817 			StartupPID = 0;
2818 
2819 			/*
2820 			 * Startup process exited in response to a shutdown request (or it
2821 			 * completed normally regardless of the shutdown request).
2822 			 */
2823 			if (Shutdown > NoShutdown &&
2824 				(EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
2825 			{
2826 				StartupStatus = STARTUP_NOT_RUNNING;
2827 				pmState = PM_WAIT_BACKENDS;
2828 				/* PostmasterStateMachine logic does the rest */
2829 				continue;
2830 			}
2831 
2832 			if (EXIT_STATUS_3(exitstatus))
2833 			{
2834 				ereport(LOG,
2835 						(errmsg("shutdown at recovery target")));
2836 				StartupStatus = STARTUP_NOT_RUNNING;
2837 				Shutdown = Max(Shutdown, SmartShutdown);
2838 				TerminateChildren(SIGTERM);
2839 				pmState = PM_WAIT_BACKENDS;
2840 				/* PostmasterStateMachine logic does the rest */
2841 				continue;
2842 			}
2843 
2844 			/*
2845 			 * Unexpected exit of startup process (including FATAL exit)
2846 			 * during PM_STARTUP is treated as catastrophic. There are no
2847 			 * other processes running yet, so we can just exit.
2848 			 */
2849 			if (pmState == PM_STARTUP && !EXIT_STATUS_0(exitstatus))
2850 			{
2851 				LogChildExit(LOG, _("startup process"),
2852 							 pid, exitstatus);
2853 				ereport(LOG,
2854 				(errmsg("aborting startup due to startup process failure")));
2855 				ExitPostmaster(1);
2856 			}
2857 
2858 			/*
2859 			 * After PM_STARTUP, any unexpected exit (including FATAL exit) of
2860 			 * the startup process is catastrophic, so kill other children,
2861 			 * and set StartupStatus so we don't try to reinitialize after
2862 			 * they're gone.  Exception: if StartupStatus is STARTUP_SIGNALED,
2863 			 * then we previously sent the startup process a SIGQUIT; so
2864 			 * that's probably the reason it died, and we do want to try to
2865 			 * restart in that case.
2866 			 */
2867 			if (!EXIT_STATUS_0(exitstatus))
2868 			{
2869 				if (StartupStatus == STARTUP_SIGNALED)
2870 					StartupStatus = STARTUP_NOT_RUNNING;
2871 				else
2872 					StartupStatus = STARTUP_CRASHED;
2873 				HandleChildCrash(pid, exitstatus,
2874 								 _("startup process"));
2875 				continue;
2876 			}
2877 
2878 			/*
2879 			 * Startup succeeded, commence normal operations
2880 			 */
2881 			StartupStatus = STARTUP_NOT_RUNNING;
2882 			FatalError = false;
2883 			Assert(AbortStartTime == 0);
2884 			ReachedNormalRunning = true;
2885 			pmState = PM_RUN;
2886 			connsAllowed = ALLOW_ALL_CONNS;
2887 
2888 			/*
2889 			 * Crank up the background tasks, if we didn't do that already
2890 			 * when we entered consistent recovery state.  It doesn't matter
2891 			 * if this fails, we'll just try again later.
2892 			 */
2893 			if (CheckpointerPID == 0)
2894 				CheckpointerPID = StartCheckpointer();
2895 			if (BgWriterPID == 0)
2896 				BgWriterPID = StartBackgroundWriter();
2897 			if (WalWriterPID == 0)
2898 				WalWriterPID = StartWalWriter();
2899 
2900 			/*
2901 			 * Likewise, start other special children as needed.  In a restart
2902 			 * situation, some of them may be alive already.
2903 			 */
2904 			if (!IsBinaryUpgrade && AutoVacuumingActive() && AutoVacPID == 0)
2905 				AutoVacPID = StartAutoVacLauncher();
2906 			if (PgArchStartupAllowed() && PgArchPID == 0)
2907 				PgArchPID = pgarch_start();
2908 			if (PgStatPID == 0)
2909 				PgStatPID = pgstat_start();
2910 
2911 			/* workers may be scheduled to start now */
2912 			maybe_start_bgworkers();
2913 
2914 			/* at this point we are really open for business */
2915 			ereport(LOG,
2916 				 (errmsg("database system is ready to accept connections")));
2917 
2918 #ifdef USE_SYSTEMD
2919 			sd_notify(0, "READY=1");
2920 #endif
2921 
2922 			continue;
2923 		}
2924 
2925 		/*
2926 		 * Was it the bgwriter?  Normal exit can be ignored; we'll start a new
2927 		 * one at the next iteration of the postmaster's main loop, if
2928 		 * necessary.  Any other exit condition is treated as a crash.
2929 		 */
2930 		if (pid == BgWriterPID)
2931 		{
2932 			BgWriterPID = 0;
2933 			if (!EXIT_STATUS_0(exitstatus))
2934 				HandleChildCrash(pid, exitstatus,
2935 								 _("background writer process"));
2936 			continue;
2937 		}
2938 
2939 		/*
2940 		 * Was it the checkpointer?
2941 		 */
2942 		if (pid == CheckpointerPID)
2943 		{
2944 			CheckpointerPID = 0;
2945 			if (EXIT_STATUS_0(exitstatus) && pmState == PM_SHUTDOWN)
2946 			{
2947 				/*
2948 				 * OK, we saw normal exit of the checkpointer after it's been
2949 				 * told to shut down.  We expect that it wrote a shutdown
2950 				 * checkpoint.  (If for some reason it didn't, recovery will
2951 				 * occur on next postmaster start.)
2952 				 *
2953 				 * At this point we should have no normal backend children
2954 				 * left (else we'd not be in PM_SHUTDOWN state) but we might
2955 				 * have dead_end children to wait for.
2956 				 *
2957 				 * If we have an archiver subprocess, tell it to do a last
2958 				 * archive cycle and quit. Likewise, if we have walsender
2959 				 * processes, tell them to send any remaining WAL and quit.
2960 				 */
2961 				Assert(Shutdown > NoShutdown);
2962 
2963 				/* Waken archiver for the last time */
2964 				if (PgArchPID != 0)
2965 					signal_child(PgArchPID, SIGUSR2);
2966 
2967 				/*
2968 				 * Waken walsenders for the last time. No regular backends
2969 				 * should be around anymore.
2970 				 */
2971 				SignalChildren(SIGUSR2);
2972 
2973 				pmState = PM_SHUTDOWN_2;
2974 
2975 				/*
2976 				 * We can also shut down the stats collector now; there's
2977 				 * nothing left for it to do.
2978 				 */
2979 				if (PgStatPID != 0)
2980 					signal_child(PgStatPID, SIGQUIT);
2981 			}
2982 			else
2983 			{
2984 				/*
2985 				 * Any unexpected exit of the checkpointer (including FATAL
2986 				 * exit) is treated as a crash.
2987 				 */
2988 				HandleChildCrash(pid, exitstatus,
2989 								 _("checkpointer process"));
2990 			}
2991 
2992 			continue;
2993 		}
2994 
2995 		/*
2996 		 * Was it the wal writer?  Normal exit can be ignored; we'll start a
2997 		 * new one at the next iteration of the postmaster's main loop, if
2998 		 * necessary.  Any other exit condition is treated as a crash.
2999 		 */
3000 		if (pid == WalWriterPID)
3001 		{
3002 			WalWriterPID = 0;
3003 			if (!EXIT_STATUS_0(exitstatus))
3004 				HandleChildCrash(pid, exitstatus,
3005 								 _("WAL writer process"));
3006 			continue;
3007 		}
3008 
3009 		/*
3010 		 * Was it the wal receiver?  If exit status is zero (normal) or one
3011 		 * (FATAL exit), we assume everything is all right just like normal
3012 		 * backends.  (If we need a new wal receiver, we'll start one at the
3013 		 * next iteration of the postmaster's main loop.)
3014 		 */
3015 		if (pid == WalReceiverPID)
3016 		{
3017 			WalReceiverPID = 0;
3018 			if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
3019 				HandleChildCrash(pid, exitstatus,
3020 								 _("WAL receiver process"));
3021 			continue;
3022 		}
3023 
3024 		/*
3025 		 * Was it the autovacuum launcher?	Normal exit can be ignored; we'll
3026 		 * start a new one at the next iteration of the postmaster's main
3027 		 * loop, if necessary.  Any other exit condition is treated as a
3028 		 * crash.
3029 		 */
3030 		if (pid == AutoVacPID)
3031 		{
3032 			AutoVacPID = 0;
3033 			if (!EXIT_STATUS_0(exitstatus))
3034 				HandleChildCrash(pid, exitstatus,
3035 								 _("autovacuum launcher process"));
3036 			continue;
3037 		}
3038 
3039 		/*
3040 		 * Was it the archiver?  If so, just try to start a new one; no need
3041 		 * to force reset of the rest of the system.  (If fail, we'll try
3042 		 * again in future cycles of the main loop.).  Unless we were waiting
3043 		 * for it to shut down; don't restart it in that case, and
3044 		 * PostmasterStateMachine() will advance to the next shutdown step.
3045 		 */
3046 		if (pid == PgArchPID)
3047 		{
3048 			PgArchPID = 0;
3049 			if (!EXIT_STATUS_0(exitstatus))
3050 				LogChildExit(LOG, _("archiver process"),
3051 							 pid, exitstatus);
3052 			if (PgArchStartupAllowed())
3053 				PgArchPID = pgarch_start();
3054 			continue;
3055 		}
3056 
3057 		/*
3058 		 * Was it the statistics collector?  If so, just try to start a new
3059 		 * one; no need to force reset of the rest of the system.  (If fail,
3060 		 * we'll try again in future cycles of the main loop.)
3061 		 */
3062 		if (pid == PgStatPID)
3063 		{
3064 			PgStatPID = 0;
3065 			if (!EXIT_STATUS_0(exitstatus))
3066 				LogChildExit(LOG, _("statistics collector process"),
3067 							 pid, exitstatus);
3068 			if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
3069 				PgStatPID = pgstat_start();
3070 			continue;
3071 		}
3072 
3073 		/* Was it the system logger?  If so, try to start a new one */
3074 		if (pid == SysLoggerPID)
3075 		{
3076 			SysLoggerPID = 0;
3077 			/* for safety's sake, launch new logger *first* */
3078 			SysLoggerPID = SysLogger_Start();
3079 			if (!EXIT_STATUS_0(exitstatus))
3080 				LogChildExit(LOG, _("system logger process"),
3081 							 pid, exitstatus);
3082 			continue;
3083 		}
3084 
3085 		/* Was it one of our background workers? */
3086 		if (CleanupBackgroundWorker(pid, exitstatus))
3087 		{
3088 			/* have it be restarted */
3089 			HaveCrashedWorker = true;
3090 			continue;
3091 		}
3092 
3093 		/*
3094 		 * Else do standard backend child cleanup.
3095 		 */
3096 		CleanupBackend(pid, exitstatus);
3097 	}							/* loop over pending child-death reports */
3098 
3099 	/*
3100 	 * After cleaning out the SIGCHLD queue, see if we have any state changes
3101 	 * or actions to make.
3102 	 */
3103 	PostmasterStateMachine();
3104 
3105 	/* Done with signal handler */
3106 #ifdef WIN32
3107 	PG_SETMASK(&UnBlockSig);
3108 #endif
3109 
3110 	errno = save_errno;
3111 }
3112 
3113 /*
3114  * Scan the bgworkers list and see if the given PID (which has just stopped
3115  * or crashed) is in it.  Handle its shutdown if so, and return true.  If not a
3116  * bgworker, return false.
3117  *
3118  * This is heavily based on CleanupBackend.  One important difference is that
3119  * we don't know yet that the dying process is a bgworker, so we must be silent
3120  * until we're sure it is.
3121  */
3122 static bool
3123 CleanupBackgroundWorker(int pid,
3124 						int exitstatus) /* child's exit status */
3125 {
3126 	char		namebuf[MAXPGPATH];
3127 	slist_iter	iter;
3128 
3129 	slist_foreach(iter, &BackgroundWorkerList)
3130 	{
3131 		RegisteredBgWorker *rw;
3132 
3133 		rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
3134 
3135 		if (rw->rw_pid != pid)
3136 			continue;
3137 
3138 #ifdef WIN32
3139 		/* see CleanupBackend */
3140 		if (exitstatus == ERROR_WAIT_NO_CHILDREN)
3141 			exitstatus = 0;
3142 #endif
3143 
3144 		snprintf(namebuf, MAXPGPATH, "%s: %s", _("worker process"),
3145 				 rw->rw_worker.bgw_name);
3146 
3147 		if (!EXIT_STATUS_0(exitstatus))
3148 		{
3149 			/* Record timestamp, so we know when to restart the worker. */
3150 			rw->rw_crashed_at = GetCurrentTimestamp();
3151 		}
3152 		else
3153 		{
3154 			/* Zero exit status means terminate */
3155 			rw->rw_crashed_at = 0;
3156 			rw->rw_terminate = true;
3157 		}
3158 
3159 		/*
3160 		 * Additionally, for shared-memory-connected workers, just like a
3161 		 * backend, any exit status other than 0 or 1 is considered a crash
3162 		 * and causes a system-wide restart.
3163 		 */
3164 		if ((rw->rw_worker.bgw_flags & BGWORKER_SHMEM_ACCESS) != 0)
3165 		{
3166 			if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
3167 			{
3168 				HandleChildCrash(pid, exitstatus, namebuf);
3169 				return true;
3170 			}
3171 		}
3172 
3173 		/*
3174 		 * We must release the postmaster child slot whether this worker is
3175 		 * connected to shared memory or not, but we only treat it as a crash
3176 		 * if it is in fact connected.
3177 		 */
3178 		if (!ReleasePostmasterChildSlot(rw->rw_child_slot) &&
3179 			(rw->rw_worker.bgw_flags & BGWORKER_SHMEM_ACCESS) != 0)
3180 		{
3181 			HandleChildCrash(pid, exitstatus, namebuf);
3182 			return true;
3183 		}
3184 
3185 		/* Get it out of the BackendList and clear out remaining data */
3186 		dlist_delete(&rw->rw_backend->elem);
3187 #ifdef EXEC_BACKEND
3188 		ShmemBackendArrayRemove(rw->rw_backend);
3189 #endif
3190 
3191 		/*
3192 		 * It's possible that this background worker started some OTHER
3193 		 * background worker and asked to be notified when that worker started
3194 		 * or stopped.  If so, cancel any notifications destined for the
3195 		 * now-dead backend.
3196 		 */
3197 		if (rw->rw_backend->bgworker_notify)
3198 			BackgroundWorkerStopNotifications(rw->rw_pid);
3199 		free(rw->rw_backend);
3200 		rw->rw_backend = NULL;
3201 		rw->rw_pid = 0;
3202 		rw->rw_child_slot = 0;
3203 		ReportBackgroundWorkerPID(rw);	/* report child death */
3204 
3205 		LogChildExit(EXIT_STATUS_0(exitstatus) ? DEBUG1 : LOG,
3206 					 namebuf, pid, exitstatus);
3207 
3208 		return true;
3209 	}
3210 
3211 	return false;
3212 }
3213 
3214 /*
3215  * CleanupBackend -- cleanup after terminated backend.
3216  *
3217  * Remove all local state associated with backend.
3218  *
3219  * If you change this, see also CleanupBackgroundWorker.
3220  */
3221 static void
3222 CleanupBackend(int pid,
3223 			   int exitstatus)	/* child's exit status. */
3224 {
3225 	dlist_mutable_iter iter;
3226 
3227 	LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
3228 
3229 	/*
3230 	 * If a backend dies in an ugly way then we must signal all other backends
3231 	 * to quickdie.  If exit status is zero (normal) or one (FATAL exit), we
3232 	 * assume everything is all right and proceed to remove the backend from
3233 	 * the active backend list.
3234 	 */
3235 
3236 #ifdef WIN32
3237 
3238 	/*
3239 	 * On win32, also treat ERROR_WAIT_NO_CHILDREN (128) as nonfatal case,
3240 	 * since that sometimes happens under load when the process fails to start
3241 	 * properly (long before it starts using shared memory). Microsoft reports
3242 	 * it is related to mutex failure:
3243 	 * http://archives.postgresql.org/pgsql-hackers/2010-09/msg00790.php
3244 	 */
3245 	if (exitstatus == ERROR_WAIT_NO_CHILDREN)
3246 	{
3247 		LogChildExit(LOG, _("server process"), pid, exitstatus);
3248 		exitstatus = 0;
3249 	}
3250 #endif
3251 
3252 	if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
3253 	{
3254 		HandleChildCrash(pid, exitstatus, _("server process"));
3255 		return;
3256 	}
3257 
3258 	dlist_foreach_modify(iter, &BackendList)
3259 	{
3260 		Backend    *bp = dlist_container(Backend, elem, iter.cur);
3261 
3262 		if (bp->pid == pid)
3263 		{
3264 			if (!bp->dead_end)
3265 			{
3266 				if (!ReleasePostmasterChildSlot(bp->child_slot))
3267 				{
3268 					/*
3269 					 * Uh-oh, the child failed to clean itself up.  Treat as a
3270 					 * crash after all.
3271 					 */
3272 					HandleChildCrash(pid, exitstatus, _("server process"));
3273 					return;
3274 				}
3275 #ifdef EXEC_BACKEND
3276 				ShmemBackendArrayRemove(bp);
3277 #endif
3278 			}
3279 			if (bp->bgworker_notify)
3280 			{
3281 				/*
3282 				 * This backend may have been slated to receive SIGUSR1 when
3283 				 * some background worker started or stopped.  Cancel those
3284 				 * notifications, as we don't want to signal PIDs that are not
3285 				 * PostgreSQL backends.  This gets skipped in the (probably
3286 				 * very common) case where the backend has never requested any
3287 				 * such notifications.
3288 				 */
3289 				BackgroundWorkerStopNotifications(bp->pid);
3290 			}
3291 			dlist_delete(iter.cur);
3292 			free(bp);
3293 			break;
3294 		}
3295 	}
3296 }
3297 
3298 /*
3299  * HandleChildCrash -- cleanup after failed backend, bgwriter, checkpointer,
3300  * walwriter, autovacuum, or background worker.
3301  *
3302  * The objectives here are to clean up our local state about the child
3303  * process, and to signal all other remaining children to quickdie.
3304  */
3305 static void
3306 HandleChildCrash(int pid, int exitstatus, const char *procname)
3307 {
3308 	dlist_mutable_iter iter;
3309 	slist_iter	siter;
3310 	Backend    *bp;
3311 	bool		take_action;
3312 
3313 	/*
3314 	 * We only log messages and send signals if this is the first process
3315 	 * crash and we're not doing an immediate shutdown; otherwise, we're only
3316 	 * here to update postmaster's idea of live processes.  If we have already
3317 	 * signalled children, nonzero exit status is to be expected, so don't
3318 	 * clutter log.
3319 	 */
3320 	take_action = !FatalError && Shutdown != ImmediateShutdown;
3321 
3322 	if (take_action)
3323 	{
3324 		LogChildExit(LOG, procname, pid, exitstatus);
3325 		ereport(LOG,
3326 				(errmsg("terminating any other active server processes")));
3327 	}
3328 
3329 	/* Process background workers. */
3330 	slist_foreach(siter, &BackgroundWorkerList)
3331 	{
3332 		RegisteredBgWorker *rw;
3333 
3334 		rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
3335 		if (rw->rw_pid == 0)
3336 			continue;			/* not running */
3337 		if (rw->rw_pid == pid)
3338 		{
3339 			/*
3340 			 * Found entry for freshly-dead worker, so remove it.
3341 			 */
3342 			(void) ReleasePostmasterChildSlot(rw->rw_child_slot);
3343 			dlist_delete(&rw->rw_backend->elem);
3344 #ifdef EXEC_BACKEND
3345 			ShmemBackendArrayRemove(rw->rw_backend);
3346 #endif
3347 			free(rw->rw_backend);
3348 			rw->rw_backend = NULL;
3349 			rw->rw_pid = 0;
3350 			rw->rw_child_slot = 0;
3351 			/* don't reset crashed_at */
3352 			/* don't report child stop, either */
3353 			/* Keep looping so we can signal remaining workers */
3354 		}
3355 		else
3356 		{
3357 			/*
3358 			 * This worker is still alive.  Unless we did so already, tell it
3359 			 * to commit hara-kiri.
3360 			 *
3361 			 * SIGQUIT is the special signal that says exit without proc_exit
3362 			 * and let the user know what's going on. But if SendStop is set
3363 			 * (-s on command line), then we send SIGSTOP instead, so that we
3364 			 * can get core dumps from all backends by hand.
3365 			 */
3366 			if (take_action)
3367 			{
3368 				ereport(DEBUG2,
3369 						(errmsg_internal("sending %s to process %d",
3370 										 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3371 										 (int) rw->rw_pid)));
3372 				signal_child(rw->rw_pid, (SendStop ? SIGSTOP : SIGQUIT));
3373 			}
3374 		}
3375 	}
3376 
3377 	/* Process regular backends */
3378 	dlist_foreach_modify(iter, &BackendList)
3379 	{
3380 		bp = dlist_container(Backend, elem, iter.cur);
3381 
3382 		if (bp->pid == pid)
3383 		{
3384 			/*
3385 			 * Found entry for freshly-dead backend, so remove it.
3386 			 */
3387 			if (!bp->dead_end)
3388 			{
3389 				(void) ReleasePostmasterChildSlot(bp->child_slot);
3390 #ifdef EXEC_BACKEND
3391 				ShmemBackendArrayRemove(bp);
3392 #endif
3393 			}
3394 			dlist_delete(iter.cur);
3395 			free(bp);
3396 			/* Keep looping so we can signal remaining backends */
3397 		}
3398 		else
3399 		{
3400 			/*
3401 			 * This backend is still alive.  Unless we did so already, tell it
3402 			 * to commit hara-kiri.
3403 			 *
3404 			 * SIGQUIT is the special signal that says exit without proc_exit
3405 			 * and let the user know what's going on. But if SendStop is set
3406 			 * (-s on command line), then we send SIGSTOP instead, so that we
3407 			 * can get core dumps from all backends by hand.
3408 			 *
3409 			 * We could exclude dead_end children here, but at least in the
3410 			 * SIGSTOP case it seems better to include them.
3411 			 *
3412 			 * Background workers were already processed above; ignore them
3413 			 * here.
3414 			 */
3415 			if (bp->bkend_type == BACKEND_TYPE_BGWORKER)
3416 				continue;
3417 
3418 			if (take_action)
3419 			{
3420 				ereport(DEBUG2,
3421 						(errmsg_internal("sending %s to process %d",
3422 										 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3423 										 (int) bp->pid)));
3424 				signal_child(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
3425 			}
3426 		}
3427 	}
3428 
3429 	/* Take care of the startup process too */
3430 	if (pid == StartupPID)
3431 	{
3432 		StartupPID = 0;
3433 		StartupStatus = STARTUP_CRASHED;
3434 	}
3435 	else if (StartupPID != 0 && take_action)
3436 	{
3437 		ereport(DEBUG2,
3438 				(errmsg_internal("sending %s to process %d",
3439 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3440 								 (int) StartupPID)));
3441 		signal_child(StartupPID, (SendStop ? SIGSTOP : SIGQUIT));
3442 		StartupStatus = STARTUP_SIGNALED;
3443 	}
3444 
3445 	/* Take care of the bgwriter too */
3446 	if (pid == BgWriterPID)
3447 		BgWriterPID = 0;
3448 	else if (BgWriterPID != 0 && take_action)
3449 	{
3450 		ereport(DEBUG2,
3451 				(errmsg_internal("sending %s to process %d",
3452 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3453 								 (int) BgWriterPID)));
3454 		signal_child(BgWriterPID, (SendStop ? SIGSTOP : SIGQUIT));
3455 	}
3456 
3457 	/* Take care of the checkpointer too */
3458 	if (pid == CheckpointerPID)
3459 		CheckpointerPID = 0;
3460 	else if (CheckpointerPID != 0 && take_action)
3461 	{
3462 		ereport(DEBUG2,
3463 				(errmsg_internal("sending %s to process %d",
3464 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3465 								 (int) CheckpointerPID)));
3466 		signal_child(CheckpointerPID, (SendStop ? SIGSTOP : SIGQUIT));
3467 	}
3468 
3469 	/* Take care of the walwriter too */
3470 	if (pid == WalWriterPID)
3471 		WalWriterPID = 0;
3472 	else if (WalWriterPID != 0 && take_action)
3473 	{
3474 		ereport(DEBUG2,
3475 				(errmsg_internal("sending %s to process %d",
3476 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3477 								 (int) WalWriterPID)));
3478 		signal_child(WalWriterPID, (SendStop ? SIGSTOP : SIGQUIT));
3479 	}
3480 
3481 	/* Take care of the walreceiver too */
3482 	if (pid == WalReceiverPID)
3483 		WalReceiverPID = 0;
3484 	else if (WalReceiverPID != 0 && take_action)
3485 	{
3486 		ereport(DEBUG2,
3487 				(errmsg_internal("sending %s to process %d",
3488 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3489 								 (int) WalReceiverPID)));
3490 		signal_child(WalReceiverPID, (SendStop ? SIGSTOP : SIGQUIT));
3491 	}
3492 
3493 	/* Take care of the autovacuum launcher too */
3494 	if (pid == AutoVacPID)
3495 		AutoVacPID = 0;
3496 	else if (AutoVacPID != 0 && take_action)
3497 	{
3498 		ereport(DEBUG2,
3499 				(errmsg_internal("sending %s to process %d",
3500 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3501 								 (int) AutoVacPID)));
3502 		signal_child(AutoVacPID, (SendStop ? SIGSTOP : SIGQUIT));
3503 	}
3504 
3505 	/*
3506 	 * Force a power-cycle of the pgarch process too.  (This isn't absolutely
3507 	 * necessary, but it seems like a good idea for robustness, and it
3508 	 * simplifies the state-machine logic in the case where a shutdown request
3509 	 * arrives during crash processing.)
3510 	 */
3511 	if (PgArchPID != 0 && take_action)
3512 	{
3513 		ereport(DEBUG2,
3514 				(errmsg_internal("sending %s to process %d",
3515 								 "SIGQUIT",
3516 								 (int) PgArchPID)));
3517 		signal_child(PgArchPID, SIGQUIT);
3518 	}
3519 
3520 	/*
3521 	 * Force a power-cycle of the pgstat process too.  (This isn't absolutely
3522 	 * necessary, but it seems like a good idea for robustness, and it
3523 	 * simplifies the state-machine logic in the case where a shutdown request
3524 	 * arrives during crash processing.)
3525 	 */
3526 	if (PgStatPID != 0 && take_action)
3527 	{
3528 		ereport(DEBUG2,
3529 				(errmsg_internal("sending %s to process %d",
3530 								 "SIGQUIT",
3531 								 (int) PgStatPID)));
3532 		signal_child(PgStatPID, SIGQUIT);
3533 		allow_immediate_pgstat_restart();
3534 	}
3535 
3536 	/* We do NOT restart the syslogger */
3537 
3538 	if (Shutdown != ImmediateShutdown)
3539 		FatalError = true;
3540 
3541 	/* We now transit into a state of waiting for children to die */
3542 	if (pmState == PM_RECOVERY ||
3543 		pmState == PM_HOT_STANDBY ||
3544 		pmState == PM_RUN ||
3545 		pmState == PM_STOP_BACKENDS ||
3546 		pmState == PM_SHUTDOWN)
3547 		pmState = PM_WAIT_BACKENDS;
3548 
3549 	/*
3550 	 * .. and if this doesn't happen quickly enough, now the clock is ticking
3551 	 * for us to kill them without mercy.
3552 	 */
3553 	if (AbortStartTime == 0)
3554 		AbortStartTime = time(NULL);
3555 }
3556 
3557 /*
3558  * Log the death of a child process.
3559  */
3560 static void
3561 LogChildExit(int lev, const char *procname, int pid, int exitstatus)
3562 {
3563 	/*
3564 	 * size of activity_buffer is arbitrary, but set equal to default
3565 	 * track_activity_query_size
3566 	 */
3567 	char		activity_buffer[1024];
3568 	const char *activity = NULL;
3569 
3570 	if (!EXIT_STATUS_0(exitstatus))
3571 		activity = pgstat_get_crashed_backend_activity(pid,
3572 													   activity_buffer,
3573 													sizeof(activity_buffer));
3574 
3575 	if (WIFEXITED(exitstatus))
3576 		ereport(lev,
3577 
3578 		/*------
3579 		  translator: %s is a noun phrase describing a child process, such as
3580 		  "server process" */
3581 				(errmsg("%s (PID %d) exited with exit code %d",
3582 						procname, pid, WEXITSTATUS(exitstatus)),
3583 				 activity ? errdetail("Failed process was running: %s", activity) : 0));
3584 	else if (WIFSIGNALED(exitstatus))
3585 	{
3586 #if defined(WIN32)
3587 		ereport(lev,
3588 
3589 		/*------
3590 		  translator: %s is a noun phrase describing a child process, such as
3591 		  "server process" */
3592 				(errmsg("%s (PID %d) was terminated by exception 0x%X",
3593 						procname, pid, WTERMSIG(exitstatus)),
3594 				 errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
3595 				 activity ? errdetail("Failed process was running: %s", activity) : 0));
3596 #else
3597 	ereport(lev,
3598 
3599 	/*------
3600 	  translator: %s is a noun phrase describing a child process, such as
3601 	  "server process" */
3602 			(errmsg("%s (PID %d) was terminated by signal %d: %s",
3603 					procname, pid, WTERMSIG(exitstatus),
3604 						pg_strsignal(WTERMSIG(exitstatus))),
3605 				 activity ? errdetail("Failed process was running: %s", activity) : 0));
3606 #endif
3607 	}
3608 	else
3609 		ereport(lev,
3610 
3611 		/*------
3612 		  translator: %s is a noun phrase describing a child process, such as
3613 		  "server process" */
3614 				(errmsg("%s (PID %d) exited with unrecognized status %d",
3615 						procname, pid, exitstatus),
3616 				 activity ? errdetail("Failed process was running: %s", activity) : 0));
3617 }
3618 
3619 /*
3620  * Advance the postmaster's state machine and take actions as appropriate
3621  *
3622  * This is common code for pmdie(), reaper() and sigusr1_handler(), which
3623  * receive the signals that might mean we need to change state.
3624  */
3625 static void
3626 PostmasterStateMachine(void)
3627 {
3628 	/* If we're doing a smart shutdown, try to advance that state. */
3629 	if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
3630 	{
3631 		if (connsAllowed == ALLOW_SUPERUSER_CONNS)
3632 		{
3633 			/*
3634 			 * ALLOW_SUPERUSER_CONNS state ends as soon as online backup mode
3635 			 * is not active.
3636 			 */
3637 			if (!BackupInProgress())
3638 				connsAllowed = ALLOW_NO_CONNS;
3639 		}
3640 
3641 		if (connsAllowed == ALLOW_NO_CONNS)
3642 		{
3643 			/*
3644 			 * ALLOW_NO_CONNS state ends when we have no normal client
3645 			 * backends running.  Then we're ready to stop other children.
3646 			 */
3647 			if (CountChildren(BACKEND_TYPE_NORMAL) == 0)
3648 				pmState = PM_STOP_BACKENDS;
3649 		}
3650 	}
3651 
3652 	/*
3653 	 * If we're ready to do so, signal child processes to shut down.  (This
3654 	 * isn't a persistent state, but treating it as a distinct pmState allows
3655 	 * us to share this code across multiple shutdown code paths.)
3656 	 */
3657 	if (pmState == PM_STOP_BACKENDS)
3658 	{
3659 		/* Signal all backend children except walsenders */
3660 		SignalSomeChildren(SIGTERM,
3661 						   BACKEND_TYPE_ALL - BACKEND_TYPE_WALSND);
3662 		/* and the autovac launcher too */
3663 		if (AutoVacPID != 0)
3664 			signal_child(AutoVacPID, SIGTERM);
3665 		/* and the bgwriter too */
3666 		if (BgWriterPID != 0)
3667 			signal_child(BgWriterPID, SIGTERM);
3668 		/* and the walwriter too */
3669 		if (WalWriterPID != 0)
3670 			signal_child(WalWriterPID, SIGTERM);
3671 		/* If we're in recovery, also stop startup and walreceiver procs */
3672 		if (StartupPID != 0)
3673 			signal_child(StartupPID, SIGTERM);
3674 		if (WalReceiverPID != 0)
3675 			signal_child(WalReceiverPID, SIGTERM);
3676 		/* checkpointer, archiver, stats, and syslogger may continue for now */
3677 
3678 		/* Now transition to PM_WAIT_BACKENDS state to wait for them to die */
3679 		pmState = PM_WAIT_BACKENDS;
3680 	}
3681 
3682 	/*
3683 	 * If we are in a state-machine state that implies waiting for backends to
3684 	 * exit, see if they're all gone, and change state if so.
3685 	 */
3686 	if (pmState == PM_WAIT_BACKENDS)
3687 	{
3688 		/*
3689 		 * PM_WAIT_BACKENDS state ends when we have no regular backends
3690 		 * (including autovac workers), no bgworkers (including unconnected
3691 		 * ones), and no walwriter, autovac launcher or bgwriter.  If we are
3692 		 * doing crash recovery or an immediate shutdown then we expect the
3693 		 * checkpointer to exit as well, otherwise not. The archiver, stats,
3694 		 * and syslogger processes are disregarded since they are not
3695 		 * connected to shared memory; we also disregard dead_end children
3696 		 * here. Walsenders are also disregarded, they will be terminated
3697 		 * later after writing the checkpoint record, like the archiver
3698 		 * process.
3699 		 */
3700 		if (CountChildren(BACKEND_TYPE_ALL - BACKEND_TYPE_WALSND) == 0 &&
3701 			StartupPID == 0 &&
3702 			WalReceiverPID == 0 &&
3703 			BgWriterPID == 0 &&
3704 			(CheckpointerPID == 0 ||
3705 			 (!FatalError && Shutdown < ImmediateShutdown)) &&
3706 			WalWriterPID == 0 &&
3707 			AutoVacPID == 0)
3708 		{
3709 			if (Shutdown >= ImmediateShutdown || FatalError)
3710 			{
3711 				/*
3712 				 * Start waiting for dead_end children to die.  This state
3713 				 * change causes ServerLoop to stop creating new ones.
3714 				 */
3715 				pmState = PM_WAIT_DEAD_END;
3716 
3717 				/*
3718 				 * We already SIGQUIT'd the archiver and stats processes, if
3719 				 * any, when we started immediate shutdown or entered
3720 				 * FatalError state.
3721 				 */
3722 			}
3723 			else
3724 			{
3725 				/*
3726 				 * If we get here, we are proceeding with normal shutdown. All
3727 				 * the regular children are gone, and it's time to tell the
3728 				 * checkpointer to do a shutdown checkpoint.
3729 				 */
3730 				Assert(Shutdown > NoShutdown);
3731 				/* Start the checkpointer if not running */
3732 				if (CheckpointerPID == 0)
3733 					CheckpointerPID = StartCheckpointer();
3734 				/* And tell it to shut down */
3735 				if (CheckpointerPID != 0)
3736 				{
3737 					signal_child(CheckpointerPID, SIGUSR2);
3738 					pmState = PM_SHUTDOWN;
3739 				}
3740 				else
3741 				{
3742 					/*
3743 					 * If we failed to fork a checkpointer, just shut down.
3744 					 * Any required cleanup will happen at next restart. We
3745 					 * set FatalError so that an "abnormal shutdown" message
3746 					 * gets logged when we exit.
3747 					 */
3748 					FatalError = true;
3749 					pmState = PM_WAIT_DEAD_END;
3750 
3751 					/* Kill the walsenders, archiver and stats collector too */
3752 					SignalChildren(SIGQUIT);
3753 					if (PgArchPID != 0)
3754 						signal_child(PgArchPID, SIGQUIT);
3755 					if (PgStatPID != 0)
3756 						signal_child(PgStatPID, SIGQUIT);
3757 				}
3758 			}
3759 		}
3760 	}
3761 
3762 	if (pmState == PM_SHUTDOWN_2)
3763 	{
3764 		/*
3765 		 * PM_SHUTDOWN_2 state ends when there's no other children than
3766 		 * dead_end children left. There shouldn't be any regular backends
3767 		 * left by now anyway; what we're really waiting for is walsenders and
3768 		 * archiver.
3769 		 */
3770 		if (PgArchPID == 0 && CountChildren(BACKEND_TYPE_ALL) == 0)
3771 		{
3772 			pmState = PM_WAIT_DEAD_END;
3773 		}
3774 	}
3775 
3776 	if (pmState == PM_WAIT_DEAD_END)
3777 	{
3778 		/*
3779 		 * PM_WAIT_DEAD_END state ends when the BackendList is entirely empty
3780 		 * (ie, no dead_end children remain), and the archiver and stats
3781 		 * collector are gone too.
3782 		 *
3783 		 * The reason we wait for those two is to protect them against a new
3784 		 * postmaster starting conflicting subprocesses; this isn't an
3785 		 * ironclad protection, but it at least helps in the
3786 		 * shutdown-and-immediately-restart scenario.  Note that they have
3787 		 * already been sent appropriate shutdown signals, either during a
3788 		 * normal state transition leading up to PM_WAIT_DEAD_END, or during
3789 		 * FatalError processing.
3790 		 */
3791 		if (dlist_is_empty(&BackendList) &&
3792 			PgArchPID == 0 && PgStatPID == 0)
3793 		{
3794 			/* These other guys should be dead already */
3795 			Assert(StartupPID == 0);
3796 			Assert(WalReceiverPID == 0);
3797 			Assert(BgWriterPID == 0);
3798 			Assert(CheckpointerPID == 0);
3799 			Assert(WalWriterPID == 0);
3800 			Assert(AutoVacPID == 0);
3801 			/* syslogger is not considered here */
3802 			pmState = PM_NO_CHILDREN;
3803 		}
3804 	}
3805 
3806 	/*
3807 	 * If we've been told to shut down, we exit as soon as there are no
3808 	 * remaining children.  If there was a crash, cleanup will occur at the
3809 	 * next startup.  (Before PostgreSQL 8.3, we tried to recover from the
3810 	 * crash before exiting, but that seems unwise if we are quitting because
3811 	 * we got SIGTERM from init --- there may well not be time for recovery
3812 	 * before init decides to SIGKILL us.)
3813 	 *
3814 	 * Note that the syslogger continues to run.  It will exit when it sees
3815 	 * EOF on its input pipe, which happens when there are no more upstream
3816 	 * processes.
3817 	 */
3818 	if (Shutdown > NoShutdown && pmState == PM_NO_CHILDREN)
3819 	{
3820 		if (FatalError)
3821 		{
3822 			ereport(LOG, (errmsg("abnormal database system shutdown")));
3823 			ExitPostmaster(1);
3824 		}
3825 		else
3826 		{
3827 			/*
3828 			 * Terminate exclusive backup mode to avoid recovery after a clean
3829 			 * fast shutdown.  Since an exclusive backup can only be taken
3830 			 * during normal running (and not, for example, while running
3831 			 * under Hot Standby) it only makes sense to do this if we reached
3832 			 * normal running. If we're still in recovery, the backup file is
3833 			 * one we're recovering *from*, and we must keep it around so that
3834 			 * recovery restarts from the right place.
3835 			 */
3836 			if (ReachedNormalRunning)
3837 				CancelBackup();
3838 
3839 			/* Normal exit from the postmaster is here */
3840 			ExitPostmaster(0);
3841 		}
3842 	}
3843 
3844 	/*
3845 	 * If the startup process failed, or the user does not want an automatic
3846 	 * restart after backend crashes, wait for all non-syslogger children to
3847 	 * exit, and then exit postmaster.  We don't try to reinitialize when the
3848 	 * startup process fails, because more than likely it will just fail again
3849 	 * and we will keep trying forever.
3850 	 */
3851 	if (pmState == PM_NO_CHILDREN &&
3852 		(StartupStatus == STARTUP_CRASHED || !restart_after_crash))
3853 		ExitPostmaster(1);
3854 
3855 	/*
3856 	 * If we need to recover from a crash, wait for all non-syslogger children
3857 	 * to exit, then reset shmem and StartupDataBase.
3858 	 */
3859 	if (FatalError && pmState == PM_NO_CHILDREN)
3860 	{
3861 		ereport(LOG,
3862 				(errmsg("all server processes terminated; reinitializing")));
3863 
3864 		/* allow background workers to immediately restart */
3865 		ResetBackgroundWorkerCrashTimes();
3866 
3867 		shmem_exit(1);
3868 		reset_shared(PostPortNumber);
3869 
3870 		StartupPID = StartupDataBase();
3871 		Assert(StartupPID != 0);
3872 		StartupStatus = STARTUP_RUNNING;
3873 		pmState = PM_STARTUP;
3874 		/* crash recovery started, reset SIGKILL flag */
3875 		AbortStartTime = 0;
3876 	}
3877 }
3878 
3879 
3880 /*
3881  * Send a signal to a postmaster child process
3882  *
3883  * On systems that have setsid(), each child process sets itself up as a
3884  * process group leader.  For signals that are generally interpreted in the
3885  * appropriate fashion, we signal the entire process group not just the
3886  * direct child process.  This allows us to, for example, SIGQUIT a blocked
3887  * archive_recovery script, or SIGINT a script being run by a backend via
3888  * system().
3889  *
3890  * There is a race condition for recently-forked children: they might not
3891  * have executed setsid() yet.  So we signal the child directly as well as
3892  * the group.  We assume such a child will handle the signal before trying
3893  * to spawn any grandchild processes.  We also assume that signaling the
3894  * child twice will not cause any problems.
3895  */
3896 static void
3897 signal_child(pid_t pid, int signal)
3898 {
3899 	if (kill(pid, signal) < 0)
3900 		elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) pid, signal);
3901 #ifdef HAVE_SETSID
3902 	switch (signal)
3903 	{
3904 		case SIGINT:
3905 		case SIGTERM:
3906 		case SIGQUIT:
3907 		case SIGSTOP:
3908 		case SIGKILL:
3909 			if (kill(-pid, signal) < 0)
3910 				elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) (-pid), signal);
3911 			break;
3912 		default:
3913 			break;
3914 	}
3915 #endif
3916 }
3917 
3918 /*
3919  * Send a signal to the targeted children (but NOT special children;
3920  * dead_end children are never signaled, either).
3921  */
3922 static bool
3923 SignalSomeChildren(int signal, int target)
3924 {
3925 	dlist_iter	iter;
3926 	bool		signaled = false;
3927 
3928 	dlist_foreach(iter, &BackendList)
3929 	{
3930 		Backend    *bp = dlist_container(Backend, elem, iter.cur);
3931 
3932 		if (bp->dead_end)
3933 			continue;
3934 
3935 		/*
3936 		 * Since target == BACKEND_TYPE_ALL is the most common case, we test
3937 		 * it first and avoid touching shared memory for every child.
3938 		 */
3939 		if (target != BACKEND_TYPE_ALL)
3940 		{
3941 			/*
3942 			 * Assign bkend_type for any recently announced WAL Sender
3943 			 * processes.
3944 			 */
3945 			if (bp->bkend_type == BACKEND_TYPE_NORMAL &&
3946 				IsPostmasterChildWalSender(bp->child_slot))
3947 				bp->bkend_type = BACKEND_TYPE_WALSND;
3948 
3949 			if (!(target & bp->bkend_type))
3950 				continue;
3951 		}
3952 
3953 		ereport(DEBUG4,
3954 				(errmsg_internal("sending signal %d to process %d",
3955 								 signal, (int) bp->pid)));
3956 		signal_child(bp->pid, signal);
3957 		signaled = true;
3958 	}
3959 	return signaled;
3960 }
3961 
3962 /*
3963  * Send a termination signal to children.  This considers all of our children
3964  * processes, except syslogger and dead_end backends.
3965  */
3966 static void
3967 TerminateChildren(int signal)
3968 {
3969 	SignalChildren(signal);
3970 	if (StartupPID != 0)
3971 	{
3972 		signal_child(StartupPID, signal);
3973 		if (signal == SIGQUIT || signal == SIGKILL)
3974 			StartupStatus = STARTUP_SIGNALED;
3975 	}
3976 	if (BgWriterPID != 0)
3977 		signal_child(BgWriterPID, signal);
3978 	if (CheckpointerPID != 0)
3979 		signal_child(CheckpointerPID, signal);
3980 	if (WalWriterPID != 0)
3981 		signal_child(WalWriterPID, signal);
3982 	if (WalReceiverPID != 0)
3983 		signal_child(WalReceiverPID, signal);
3984 	if (AutoVacPID != 0)
3985 		signal_child(AutoVacPID, signal);
3986 	if (PgArchPID != 0)
3987 		signal_child(PgArchPID, signal);
3988 	if (PgStatPID != 0)
3989 		signal_child(PgStatPID, signal);
3990 }
3991 
3992 /*
3993  * BackendStartup -- start backend process
3994  *
3995  * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
3996  *
3997  * Note: if you change this code, also consider StartAutovacuumWorker.
3998  */
3999 static int
4000 BackendStartup(Port *port)
4001 {
4002 	Backend    *bn;				/* for backend cleanup */
4003 	pid_t		pid;
4004 
4005 	/*
4006 	 * Create backend data structure.  Better before the fork() so we can
4007 	 * handle failure cleanly.
4008 	 */
4009 	bn = (Backend *) malloc(sizeof(Backend));
4010 	if (!bn)
4011 	{
4012 		ereport(LOG,
4013 				(errcode(ERRCODE_OUT_OF_MEMORY),
4014 				 errmsg("out of memory")));
4015 		return STATUS_ERROR;
4016 	}
4017 
4018 	/*
4019 	 * Compute the cancel key that will be assigned to this backend. The
4020 	 * backend will have its own copy in the forked-off process' value of
4021 	 * MyCancelKey, so that it can transmit the key to the frontend.
4022 	 */
4023 	MyCancelKey = PostmasterRandom();
4024 	bn->cancel_key = MyCancelKey;
4025 
4026 	/* Pass down canAcceptConnections state */
4027 	port->canAcceptConnections = canAcceptConnections(BACKEND_TYPE_NORMAL);
4028 	bn->dead_end = (port->canAcceptConnections != CAC_OK &&
4029 					port->canAcceptConnections != CAC_SUPERUSER);
4030 
4031 	/*
4032 	 * Unless it's a dead_end child, assign it a child slot number
4033 	 */
4034 	if (!bn->dead_end)
4035 		bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
4036 	else
4037 		bn->child_slot = 0;
4038 
4039 	/* Hasn't asked to be notified about any bgworkers yet */
4040 	bn->bgworker_notify = false;
4041 
4042 #ifdef EXEC_BACKEND
4043 	pid = backend_forkexec(port);
4044 #else							/* !EXEC_BACKEND */
4045 	pid = fork_process();
4046 	if (pid == 0)				/* child */
4047 	{
4048 		free(bn);
4049 
4050 		/* Detangle from postmaster */
4051 		InitPostmasterChild();
4052 
4053 		/* Close the postmaster's sockets */
4054 		ClosePostmasterPorts(false);
4055 
4056 		/* Perform additional initialization and collect startup packet */
4057 		BackendInitialize(port);
4058 
4059 		/* And run the backend */
4060 		BackendRun(port);
4061 	}
4062 #endif   /* EXEC_BACKEND */
4063 
4064 	if (pid < 0)
4065 	{
4066 		/* in parent, fork failed */
4067 		int			save_errno = errno;
4068 
4069 		if (!bn->dead_end)
4070 			(void) ReleasePostmasterChildSlot(bn->child_slot);
4071 		free(bn);
4072 		errno = save_errno;
4073 		ereport(LOG,
4074 				(errmsg("could not fork new process for connection: %m")));
4075 		report_fork_failure_to_client(port, save_errno);
4076 		return STATUS_ERROR;
4077 	}
4078 
4079 	/* in parent, successful fork */
4080 	ereport(DEBUG2,
4081 			(errmsg_internal("forked new backend, pid=%d socket=%d",
4082 							 (int) pid, (int) port->sock)));
4083 
4084 	/*
4085 	 * Everything's been successful, it's safe to add this backend to our list
4086 	 * of backends.
4087 	 */
4088 	bn->pid = pid;
4089 	bn->bkend_type = BACKEND_TYPE_NORMAL;		/* Can change later to WALSND */
4090 	dlist_push_head(&BackendList, &bn->elem);
4091 
4092 #ifdef EXEC_BACKEND
4093 	if (!bn->dead_end)
4094 		ShmemBackendArrayAdd(bn);
4095 #endif
4096 
4097 	return STATUS_OK;
4098 }
4099 
4100 /*
4101  * Try to report backend fork() failure to client before we close the
4102  * connection.  Since we do not care to risk blocking the postmaster on
4103  * this connection, we set the connection to non-blocking and try only once.
4104  *
4105  * This is grungy special-purpose code; we cannot use backend libpq since
4106  * it's not up and running.
4107  */
4108 static void
4109 report_fork_failure_to_client(Port *port, int errnum)
4110 {
4111 	char		buffer[1000];
4112 	int			rc;
4113 
4114 	/* Format the error message packet (always V2 protocol) */
4115 	snprintf(buffer, sizeof(buffer), "E%s%s\n",
4116 			 _("could not fork new process for connection: "),
4117 			 strerror(errnum));
4118 
4119 	/* Set port to non-blocking.  Don't do send() if this fails */
4120 	if (!pg_set_noblock(port->sock))
4121 		return;
4122 
4123 	/* We'll retry after EINTR, but ignore all other failures */
4124 	do
4125 	{
4126 		rc = send(port->sock, buffer, strlen(buffer) + 1, 0);
4127 	} while (rc < 0 && errno == EINTR);
4128 }
4129 
4130 
4131 /*
4132  * BackendInitialize -- initialize an interactive (postmaster-child)
4133  *				backend process, and collect the client's startup packet.
4134  *
4135  * returns: nothing.  Will not return at all if there's any failure.
4136  *
4137  * Note: this code does not depend on having any access to shared memory.
4138  * In the EXEC_BACKEND case, we are physically attached to shared memory
4139  * but have not yet set up most of our local pointers to shmem structures.
4140  */
4141 static void
4142 BackendInitialize(Port *port)
4143 {
4144 	int			status;
4145 	int			ret;
4146 	char		remote_host[NI_MAXHOST];
4147 	char		remote_port[NI_MAXSERV];
4148 	char		remote_ps_data[NI_MAXHOST];
4149 
4150 	/* Save port etc. for ps status */
4151 	MyProcPort = port;
4152 
4153 	/*
4154 	 * PreAuthDelay is a debugging aid for investigating problems in the
4155 	 * authentication cycle: it can be set in postgresql.conf to allow time to
4156 	 * attach to the newly-forked backend with a debugger.  (See also
4157 	 * PostAuthDelay, which we allow clients to pass through PGOPTIONS, but it
4158 	 * is not honored until after authentication.)
4159 	 */
4160 	if (PreAuthDelay > 0)
4161 		pg_usleep(PreAuthDelay * 1000000L);
4162 
4163 	/* This flag will remain set until InitPostgres finishes authentication */
4164 	ClientAuthInProgress = true;	/* limit visibility of log messages */
4165 
4166 	/* save process start time */
4167 	port->SessionStartTime = GetCurrentTimestamp();
4168 	MyStartTime = timestamptz_to_time_t(port->SessionStartTime);
4169 
4170 	/* set these to empty in case they are needed before we set them up */
4171 	port->remote_host = "";
4172 	port->remote_port = "";
4173 
4174 	/*
4175 	 * Initialize libpq and enable reporting of ereport errors to the client.
4176 	 * Must do this now because authentication uses libpq to send messages.
4177 	 */
4178 	pq_init();					/* initialize libpq to talk to client */
4179 	whereToSendOutput = DestRemote;		/* now safe to ereport to client */
4180 
4181 	/*
4182 	 * We arrange to do proc_exit(1) if we receive SIGTERM or timeout while
4183 	 * trying to collect the startup packet; while SIGQUIT results in
4184 	 * _exit(2).  Otherwise the postmaster cannot shutdown the database FAST
4185 	 * or IMMED cleanly if a buggy client fails to send the packet promptly.
4186 	 *
4187 	 * XXX this is pretty dangerous; signal handlers should not call anything
4188 	 * as complex as proc_exit() directly.  We minimize the hazard by not
4189 	 * keeping these handlers active for longer than we must.  However, it
4190 	 * seems necessary to be able to escape out of DNS lookups as well as the
4191 	 * startup packet reception proper, so we can't narrow the scope further
4192 	 * than is done here.
4193 	 *
4194 	 * XXX it follows that the remainder of this function must tolerate losing
4195 	 * control at any instant.  Likewise, any pg_on_exit_callback registered
4196 	 * before or during this function must be prepared to execute at any
4197 	 * instant between here and the end of this function.  Furthermore,
4198 	 * affected callbacks execute partially or not at all when a second
4199 	 * exit-inducing signal arrives after proc_exit_prepare() decrements
4200 	 * on_proc_exit_index.  (Thanks to that mechanic, callbacks need not
4201 	 * anticipate more than one call.)  This is fragile; it ought to instead
4202 	 * follow the norm of handling interrupts at selected, safe opportunities.
4203 	 */
4204 	pqsignal(SIGTERM, process_startup_packet_die);
4205 	pqsignal(SIGQUIT, process_startup_packet_quickdie);
4206 	InitializeTimeouts();		/* establishes SIGALRM handler */
4207 	PG_SETMASK(&StartupBlockSig);
4208 
4209 	/*
4210 	 * Get the remote host name and port for logging and status display.
4211 	 */
4212 	remote_host[0] = '\0';
4213 	remote_port[0] = '\0';
4214 	if ((ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
4215 								  remote_host, sizeof(remote_host),
4216 								  remote_port, sizeof(remote_port),
4217 				 (log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV)) != 0)
4218 		ereport(WARNING,
4219 				(errmsg_internal("pg_getnameinfo_all() failed: %s",
4220 								 gai_strerror(ret))));
4221 	if (remote_port[0] == '\0')
4222 		snprintf(remote_ps_data, sizeof(remote_ps_data), "%s", remote_host);
4223 	else
4224 		snprintf(remote_ps_data, sizeof(remote_ps_data), "%s(%s)", remote_host, remote_port);
4225 
4226 	/*
4227 	 * Save remote_host and remote_port in port structure (after this, they
4228 	 * will appear in log_line_prefix data for log messages).
4229 	 */
4230 	port->remote_host = strdup(remote_host);
4231 	port->remote_port = strdup(remote_port);
4232 
4233 	/* And now we can issue the Log_connections message, if wanted */
4234 	if (Log_connections)
4235 	{
4236 		if (remote_port[0])
4237 			ereport(LOG,
4238 					(errmsg("connection received: host=%s port=%s",
4239 							remote_host,
4240 							remote_port)));
4241 		else
4242 			ereport(LOG,
4243 					(errmsg("connection received: host=%s",
4244 							remote_host)));
4245 	}
4246 
4247 	/*
4248 	 * If we did a reverse lookup to name, we might as well save the results
4249 	 * rather than possibly repeating the lookup during authentication.
4250 	 *
4251 	 * Note that we don't want to specify NI_NAMEREQD above, because then we'd
4252 	 * get nothing useful for a client without an rDNS entry.  Therefore, we
4253 	 * must check whether we got a numeric IPv4 or IPv6 address, and not save
4254 	 * it into remote_hostname if so.  (This test is conservative and might
4255 	 * sometimes classify a hostname as numeric, but an error in that
4256 	 * direction is safe; it only results in a possible extra lookup.)
4257 	 */
4258 	if (log_hostname &&
4259 		ret == 0 &&
4260 		strspn(remote_host, "0123456789.") < strlen(remote_host) &&
4261 		strspn(remote_host, "0123456789ABCDEFabcdef:") < strlen(remote_host))
4262 		port->remote_hostname = strdup(remote_host);
4263 
4264 	/*
4265 	 * Ready to begin client interaction.  We will give up and proc_exit(1)
4266 	 * after a time delay, so that a broken client can't hog a connection
4267 	 * indefinitely.  PreAuthDelay and any DNS interactions above don't count
4268 	 * against the time limit.
4269 	 *
4270 	 * Note: AuthenticationTimeout is applied here while waiting for the
4271 	 * startup packet, and then again in InitPostgres for the duration of any
4272 	 * authentication operations.  So a hostile client could tie up the
4273 	 * process for nearly twice AuthenticationTimeout before we kick him off.
4274 	 *
4275 	 * Note: because PostgresMain will call InitializeTimeouts again, the
4276 	 * registration of STARTUP_PACKET_TIMEOUT will be lost.  This is okay
4277 	 * since we never use it again after this function.
4278 	 */
4279 	RegisterTimeout(STARTUP_PACKET_TIMEOUT, StartupPacketTimeoutHandler);
4280 	enable_timeout_after(STARTUP_PACKET_TIMEOUT, AuthenticationTimeout * 1000);
4281 
4282 	/*
4283 	 * Receive the startup packet (which might turn out to be a cancel request
4284 	 * packet).
4285 	 */
4286 	status = ProcessStartupPacket(port, false);
4287 
4288 	/*
4289 	 * Disable the timeout, and prevent SIGTERM/SIGQUIT again.
4290 	 */
4291 	disable_timeout(STARTUP_PACKET_TIMEOUT, false);
4292 	PG_SETMASK(&BlockSig);
4293 
4294 	/*
4295 	 * Stop here if it was bad or a cancel packet.  ProcessStartupPacket
4296 	 * already did any appropriate error reporting.
4297 	 */
4298 	if (status != STATUS_OK)
4299 		proc_exit(0);
4300 
4301 	/*
4302 	 * Now that we have the user and database name, we can set the process
4303 	 * title for ps.  It's good to do this as early as possible in startup.
4304 	 *
4305 	 * For a walsender, the ps display is set in the following form:
4306 	 *
4307 	 * postgres: wal sender process <user> <host> <activity>
4308 	 *
4309 	 * To achieve that, we pass "wal sender process" as username and username
4310 	 * as dbname to init_ps_display(). XXX: should add a new variant of
4311 	 * init_ps_display() to avoid abusing the parameters like this.
4312 	 */
4313 	if (am_walsender)
4314 		init_ps_display("wal sender process", port->user_name, remote_ps_data,
4315 						update_process_title ? "authentication" : "");
4316 	else
4317 		init_ps_display(port->user_name, port->database_name, remote_ps_data,
4318 						update_process_title ? "authentication" : "");
4319 }
4320 
4321 
4322 /*
4323  * BackendRun -- set up the backend's argument list and invoke PostgresMain()
4324  *
4325  * returns:
4326  *		Shouldn't return at all.
4327  *		If PostgresMain() fails, return status.
4328  */
4329 static void
4330 BackendRun(Port *port)
4331 {
4332 	char	  **av;
4333 	int			maxac;
4334 	int			ac;
4335 	long		secs;
4336 	int			usecs;
4337 	int			i;
4338 
4339 	/*
4340 	 * Don't want backend to be able to see the postmaster random number
4341 	 * generator state.  We have to clobber the static random_seed *and* start
4342 	 * a new random sequence in the random() library function.
4343 	 */
4344 	random_seed = 0;
4345 	random_start_time.tv_usec = 0;
4346 	/* slightly hacky way to convert timestamptz into integers */
4347 	TimestampDifference(0, port->SessionStartTime, &secs, &usecs);
4348 	srandom((unsigned int) (MyProcPid ^ (usecs << 12) ^ secs));
4349 
4350 	/*
4351 	 * Now, build the argv vector that will be given to PostgresMain.
4352 	 *
4353 	 * The maximum possible number of commandline arguments that could come
4354 	 * from ExtraOptions is (strlen(ExtraOptions) + 1) / 2; see
4355 	 * pg_split_opts().
4356 	 */
4357 	maxac = 2;					/* for fixed args supplied below */
4358 	maxac += (strlen(ExtraOptions) + 1) / 2;
4359 
4360 	av = (char **) MemoryContextAlloc(TopMemoryContext,
4361 									  maxac * sizeof(char *));
4362 	ac = 0;
4363 
4364 	av[ac++] = "postgres";
4365 
4366 	/*
4367 	 * Pass any backend switches specified with -o on the postmaster's own
4368 	 * command line.  We assume these are secure.
4369 	 */
4370 	pg_split_opts(av, &ac, ExtraOptions);
4371 
4372 	av[ac] = NULL;
4373 
4374 	Assert(ac < maxac);
4375 
4376 	/*
4377 	 * Debug: print arguments being passed to backend
4378 	 */
4379 	ereport(DEBUG3,
4380 			(errmsg_internal("%s child[%d]: starting with (",
4381 							 progname, (int) getpid())));
4382 	for (i = 0; i < ac; ++i)
4383 		ereport(DEBUG3,
4384 				(errmsg_internal("\t%s", av[i])));
4385 	ereport(DEBUG3,
4386 			(errmsg_internal(")")));
4387 
4388 	/*
4389 	 * Make sure we aren't in PostmasterContext anymore.  (We can't delete it
4390 	 * just yet, though, because InitPostgres will need the HBA data.)
4391 	 */
4392 	MemoryContextSwitchTo(TopMemoryContext);
4393 
4394 	PostgresMain(ac, av, port->database_name, port->user_name);
4395 }
4396 
4397 
4398 #ifdef EXEC_BACKEND
4399 
4400 /*
4401  * postmaster_forkexec -- fork and exec a postmaster subprocess
4402  *
4403  * The caller must have set up the argv array already, except for argv[2]
4404  * which will be filled with the name of the temp variable file.
4405  *
4406  * Returns the child process PID, or -1 on fork failure (a suitable error
4407  * message has been logged on failure).
4408  *
4409  * All uses of this routine will dispatch to SubPostmasterMain in the
4410  * child process.
4411  */
4412 pid_t
4413 postmaster_forkexec(int argc, char *argv[])
4414 {
4415 	Port		port;
4416 
4417 	/* This entry point passes dummy values for the Port variables */
4418 	memset(&port, 0, sizeof(port));
4419 	return internal_forkexec(argc, argv, &port);
4420 }
4421 
4422 /*
4423  * backend_forkexec -- fork/exec off a backend process
4424  *
4425  * Some operating systems (WIN32) don't have fork() so we have to simulate
4426  * it by storing parameters that need to be passed to the child and
4427  * then create a new child process.
4428  *
4429  * returns the pid of the fork/exec'd process, or -1 on failure
4430  */
4431 static pid_t
4432 backend_forkexec(Port *port)
4433 {
4434 	char	   *av[4];
4435 	int			ac = 0;
4436 
4437 	av[ac++] = "postgres";
4438 	av[ac++] = "--forkbackend";
4439 	av[ac++] = NULL;			/* filled in by internal_forkexec */
4440 
4441 	av[ac] = NULL;
4442 	Assert(ac < lengthof(av));
4443 
4444 	return internal_forkexec(ac, av, port);
4445 }
4446 
4447 #ifndef WIN32
4448 
4449 /*
4450  * internal_forkexec non-win32 implementation
4451  *
4452  * - writes out backend variables to the parameter file
4453  * - fork():s, and then exec():s the child process
4454  */
4455 static pid_t
4456 internal_forkexec(int argc, char *argv[], Port *port)
4457 {
4458 	static unsigned long tmpBackendFileNum = 0;
4459 	pid_t		pid;
4460 	char		tmpfilename[MAXPGPATH];
4461 	BackendParameters param;
4462 	FILE	   *fp;
4463 
4464 	if (!save_backend_variables(&param, port))
4465 		return -1;				/* log made by save_backend_variables */
4466 
4467 	/* Calculate name for temp file */
4468 	snprintf(tmpfilename, MAXPGPATH, "%s/%s.backend_var.%d.%lu",
4469 			 PG_TEMP_FILES_DIR, PG_TEMP_FILE_PREFIX,
4470 			 MyProcPid, ++tmpBackendFileNum);
4471 
4472 	/* Open file */
4473 	fp = AllocateFile(tmpfilename, PG_BINARY_W);
4474 	if (!fp)
4475 	{
4476 		/*
4477 		 * As in OpenTemporaryFileInTablespace, try to make the temp-file
4478 		 * directory
4479 		 */
4480 		mkdir(PG_TEMP_FILES_DIR, S_IRWXU);
4481 
4482 		fp = AllocateFile(tmpfilename, PG_BINARY_W);
4483 		if (!fp)
4484 		{
4485 			ereport(LOG,
4486 					(errcode_for_file_access(),
4487 					 errmsg("could not create file \"%s\": %m",
4488 							tmpfilename)));
4489 			return -1;
4490 		}
4491 	}
4492 
4493 	if (fwrite(&param, sizeof(param), 1, fp) != 1)
4494 	{
4495 		ereport(LOG,
4496 				(errcode_for_file_access(),
4497 				 errmsg("could not write to file \"%s\": %m", tmpfilename)));
4498 		FreeFile(fp);
4499 		return -1;
4500 	}
4501 
4502 	/* Release file */
4503 	if (FreeFile(fp))
4504 	{
4505 		ereport(LOG,
4506 				(errcode_for_file_access(),
4507 				 errmsg("could not write to file \"%s\": %m", tmpfilename)));
4508 		return -1;
4509 	}
4510 
4511 	/* Make sure caller set up argv properly */
4512 	Assert(argc >= 3);
4513 	Assert(argv[argc] == NULL);
4514 	Assert(strncmp(argv[1], "--fork", 6) == 0);
4515 	Assert(argv[2] == NULL);
4516 
4517 	/* Insert temp file name after --fork argument */
4518 	argv[2] = tmpfilename;
4519 
4520 	/* Fire off execv in child */
4521 	if ((pid = fork_process()) == 0)
4522 	{
4523 		if (execv(postgres_exec_path, argv) < 0)
4524 		{
4525 			ereport(LOG,
4526 					(errmsg("could not execute server process \"%s\": %m",
4527 							postgres_exec_path)));
4528 			/* We're already in the child process here, can't return */
4529 			exit(1);
4530 		}
4531 	}
4532 
4533 	return pid;					/* Parent returns pid, or -1 on fork failure */
4534 }
4535 #else							/* WIN32 */
4536 
4537 /*
4538  * internal_forkexec win32 implementation
4539  *
4540  * - starts backend using CreateProcess(), in suspended state
4541  * - writes out backend variables to the parameter file
4542  *	- during this, duplicates handles and sockets required for
4543  *	  inheritance into the new process
4544  * - resumes execution of the new process once the backend parameter
4545  *	 file is complete.
4546  */
4547 static pid_t
4548 internal_forkexec(int argc, char *argv[], Port *port)
4549 {
4550 	int			retry_count = 0;
4551 	STARTUPINFO si;
4552 	PROCESS_INFORMATION pi;
4553 	int			i;
4554 	int			j;
4555 	char		cmdLine[MAXPGPATH * 2];
4556 	HANDLE		paramHandle;
4557 	BackendParameters *param;
4558 	SECURITY_ATTRIBUTES sa;
4559 	char		paramHandleStr[32];
4560 	win32_deadchild_waitinfo *childinfo;
4561 
4562 	/* Make sure caller set up argv properly */
4563 	Assert(argc >= 3);
4564 	Assert(argv[argc] == NULL);
4565 	Assert(strncmp(argv[1], "--fork", 6) == 0);
4566 	Assert(argv[2] == NULL);
4567 
4568 	/* Resume here if we need to retry */
4569 retry:
4570 
4571 	/* Set up shared memory for parameter passing */
4572 	ZeroMemory(&sa, sizeof(sa));
4573 	sa.nLength = sizeof(sa);
4574 	sa.bInheritHandle = TRUE;
4575 	paramHandle = CreateFileMapping(INVALID_HANDLE_VALUE,
4576 									&sa,
4577 									PAGE_READWRITE,
4578 									0,
4579 									sizeof(BackendParameters),
4580 									NULL);
4581 	if (paramHandle == INVALID_HANDLE_VALUE)
4582 	{
4583 		elog(LOG, "could not create backend parameter file mapping: error code %lu",
4584 			 GetLastError());
4585 		return -1;
4586 	}
4587 
4588 	param = MapViewOfFile(paramHandle, FILE_MAP_WRITE, 0, 0, sizeof(BackendParameters));
4589 	if (!param)
4590 	{
4591 		elog(LOG, "could not map backend parameter memory: error code %lu",
4592 			 GetLastError());
4593 		CloseHandle(paramHandle);
4594 		return -1;
4595 	}
4596 
4597 	/* Insert temp file name after --fork argument */
4598 #ifdef _WIN64
4599 	sprintf(paramHandleStr, "%llu", (LONG_PTR) paramHandle);
4600 #else
4601 	sprintf(paramHandleStr, "%lu", (DWORD) paramHandle);
4602 #endif
4603 	argv[2] = paramHandleStr;
4604 
4605 	/* Format the cmd line */
4606 	cmdLine[sizeof(cmdLine) - 1] = '\0';
4607 	cmdLine[sizeof(cmdLine) - 2] = '\0';
4608 	snprintf(cmdLine, sizeof(cmdLine) - 1, "\"%s\"", postgres_exec_path);
4609 	i = 0;
4610 	while (argv[++i] != NULL)
4611 	{
4612 		j = strlen(cmdLine);
4613 		snprintf(cmdLine + j, sizeof(cmdLine) - 1 - j, " \"%s\"", argv[i]);
4614 	}
4615 	if (cmdLine[sizeof(cmdLine) - 2] != '\0')
4616 	{
4617 		elog(LOG, "subprocess command line too long");
4618 		return -1;
4619 	}
4620 
4621 	memset(&pi, 0, sizeof(pi));
4622 	memset(&si, 0, sizeof(si));
4623 	si.cb = sizeof(si);
4624 
4625 	/*
4626 	 * Create the subprocess in a suspended state. This will be resumed later,
4627 	 * once we have written out the parameter file.
4628 	 */
4629 	if (!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, CREATE_SUSPENDED,
4630 					   NULL, NULL, &si, &pi))
4631 	{
4632 		elog(LOG, "CreateProcess call failed: %m (error code %lu)",
4633 			 GetLastError());
4634 		return -1;
4635 	}
4636 
4637 	if (!save_backend_variables(param, port, pi.hProcess, pi.dwProcessId))
4638 	{
4639 		/*
4640 		 * log made by save_backend_variables, but we have to clean up the
4641 		 * mess with the half-started process
4642 		 */
4643 		if (!TerminateProcess(pi.hProcess, 255))
4644 			ereport(LOG,
4645 					(errmsg_internal("could not terminate unstarted process: error code %lu",
4646 									 GetLastError())));
4647 		CloseHandle(pi.hProcess);
4648 		CloseHandle(pi.hThread);
4649 		return -1;				/* log made by save_backend_variables */
4650 	}
4651 
4652 	/* Drop the parameter shared memory that is now inherited to the backend */
4653 	if (!UnmapViewOfFile(param))
4654 		elog(LOG, "could not unmap view of backend parameter file: error code %lu",
4655 			 GetLastError());
4656 	if (!CloseHandle(paramHandle))
4657 		elog(LOG, "could not close handle to backend parameter file: error code %lu",
4658 			 GetLastError());
4659 
4660 	/*
4661 	 * Reserve the memory region used by our main shared memory segment before
4662 	 * we resume the child process.  Normally this should succeed, but if ASLR
4663 	 * is active then it might sometimes fail due to the stack or heap having
4664 	 * gotten mapped into that range.  In that case, just terminate the
4665 	 * process and retry.
4666 	 */
4667 	if (!pgwin32_ReserveSharedMemoryRegion(pi.hProcess))
4668 	{
4669 		/* pgwin32_ReserveSharedMemoryRegion already made a log entry */
4670 		if (!TerminateProcess(pi.hProcess, 255))
4671 			ereport(LOG,
4672 					(errmsg_internal("could not terminate process that failed to reserve memory: error code %lu",
4673 									 GetLastError())));
4674 		CloseHandle(pi.hProcess);
4675 		CloseHandle(pi.hThread);
4676 		if (++retry_count < 100)
4677 			goto retry;
4678 		ereport(LOG,
4679 				(errmsg("giving up after too many tries to reserve shared memory"),
4680 				 errhint("This might be caused by ASLR or antivirus software.")));
4681 		return -1;
4682 	}
4683 
4684 	/*
4685 	 * Now that the backend variables are written out, we start the child
4686 	 * thread so it can start initializing while we set up the rest of the
4687 	 * parent state.
4688 	 */
4689 	if (ResumeThread(pi.hThread) == -1)
4690 	{
4691 		if (!TerminateProcess(pi.hProcess, 255))
4692 		{
4693 			ereport(LOG,
4694 					(errmsg_internal("could not terminate unstartable process: error code %lu",
4695 									 GetLastError())));
4696 			CloseHandle(pi.hProcess);
4697 			CloseHandle(pi.hThread);
4698 			return -1;
4699 		}
4700 		CloseHandle(pi.hProcess);
4701 		CloseHandle(pi.hThread);
4702 		ereport(LOG,
4703 				(errmsg_internal("could not resume thread of unstarted process: error code %lu",
4704 								 GetLastError())));
4705 		return -1;
4706 	}
4707 
4708 	/*
4709 	 * Queue a waiter for to signal when this child dies. The wait will be
4710 	 * handled automatically by an operating system thread pool.
4711 	 *
4712 	 * Note: use malloc instead of palloc, since it needs to be thread-safe.
4713 	 * Struct will be free():d from the callback function that runs on a
4714 	 * different thread.
4715 	 */
4716 	childinfo = malloc(sizeof(win32_deadchild_waitinfo));
4717 	if (!childinfo)
4718 		ereport(FATAL,
4719 				(errcode(ERRCODE_OUT_OF_MEMORY),
4720 				 errmsg("out of memory")));
4721 
4722 	childinfo->procHandle = pi.hProcess;
4723 	childinfo->procId = pi.dwProcessId;
4724 
4725 	if (!RegisterWaitForSingleObject(&childinfo->waitHandle,
4726 									 pi.hProcess,
4727 									 pgwin32_deadchild_callback,
4728 									 childinfo,
4729 									 INFINITE,
4730 								WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD))
4731 		ereport(FATAL,
4732 				(errmsg_internal("could not register process for wait: error code %lu",
4733 								 GetLastError())));
4734 
4735 	/* Don't close pi.hProcess here - the wait thread needs access to it */
4736 
4737 	CloseHandle(pi.hThread);
4738 
4739 	return pi.dwProcessId;
4740 }
4741 #endif   /* WIN32 */
4742 
4743 
4744 /*
4745  * SubPostmasterMain -- Get the fork/exec'd process into a state equivalent
4746  *			to what it would be if we'd simply forked on Unix, and then
4747  *			dispatch to the appropriate place.
4748  *
4749  * The first two command line arguments are expected to be "--forkFOO"
4750  * (where FOO indicates which postmaster child we are to become), and
4751  * the name of a variables file that we can read to load data that would
4752  * have been inherited by fork() on Unix.  Remaining arguments go to the
4753  * subprocess FooMain() routine.
4754  */
4755 void
4756 SubPostmasterMain(int argc, char *argv[])
4757 {
4758 	Port		port;
4759 
4760 	/* In EXEC_BACKEND case we will not have inherited these settings */
4761 	IsPostmasterEnvironment = true;
4762 	whereToSendOutput = DestNone;
4763 
4764 	/* Setup as postmaster child */
4765 	InitPostmasterChild();
4766 
4767 	/* Setup essential subsystems (to ensure elog() behaves sanely) */
4768 	InitializeGUCOptions();
4769 
4770 	/* Check we got appropriate args */
4771 	if (argc < 3)
4772 		elog(FATAL, "invalid subpostmaster invocation");
4773 
4774 	/* Read in the variables file */
4775 	memset(&port, 0, sizeof(Port));
4776 	read_backend_variables(argv[2], &port);
4777 
4778 	/* Close the postmaster's sockets (as soon as we know them) */
4779 	ClosePostmasterPorts(strcmp(argv[1], "--forklog") == 0);
4780 
4781 	/*
4782 	 * Set reference point for stack-depth checking
4783 	 */
4784 	set_stack_base();
4785 
4786 	/*
4787 	 * Set up memory area for GSS information. Mirrors the code in ConnCreate
4788 	 * for the non-exec case.
4789 	 */
4790 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
4791 	port.gss = (pg_gssinfo *) calloc(1, sizeof(pg_gssinfo));
4792 	if (!port.gss)
4793 		ereport(FATAL,
4794 				(errcode(ERRCODE_OUT_OF_MEMORY),
4795 				 errmsg("out of memory")));
4796 #endif
4797 
4798 	/*
4799 	 * If appropriate, physically re-attach to shared memory segment. We want
4800 	 * to do this before going any further to ensure that we can attach at the
4801 	 * same address the postmaster used.  On the other hand, if we choose not
4802 	 * to re-attach, we may have other cleanup to do.
4803 	 *
4804 	 * If testing EXEC_BACKEND on Linux, you should run this as root before
4805 	 * starting the postmaster:
4806 	 *
4807 	 * echo 0 >/proc/sys/kernel/randomize_va_space
4808 	 *
4809 	 * This prevents using randomized stack and code addresses that cause the
4810 	 * child process's memory map to be different from the parent's, making it
4811 	 * sometimes impossible to attach to shared memory at the desired address.
4812 	 * Return the setting to its old value (usually '1' or '2') when finished.
4813 	 */
4814 	if (strcmp(argv[1], "--forkbackend") == 0 ||
4815 		strcmp(argv[1], "--forkavlauncher") == 0 ||
4816 		strcmp(argv[1], "--forkavworker") == 0 ||
4817 		strcmp(argv[1], "--forkboot") == 0 ||
4818 		strncmp(argv[1], "--forkbgworker=", 15) == 0)
4819 		PGSharedMemoryReAttach();
4820 	else
4821 		PGSharedMemoryNoReAttach();
4822 
4823 	/* autovacuum needs this set before calling InitProcess */
4824 	if (strcmp(argv[1], "--forkavlauncher") == 0)
4825 		AutovacuumLauncherIAm();
4826 	if (strcmp(argv[1], "--forkavworker") == 0)
4827 		AutovacuumWorkerIAm();
4828 
4829 	/*
4830 	 * Start our win32 signal implementation. This has to be done after we
4831 	 * read the backend variables, because we need to pick up the signal pipe
4832 	 * from the parent process.
4833 	 */
4834 #ifdef WIN32
4835 	pgwin32_signal_initialize();
4836 #endif
4837 
4838 	/* In EXEC_BACKEND case we will not have inherited these settings */
4839 	pqinitmask();
4840 	PG_SETMASK(&BlockSig);
4841 
4842 	/* Read in remaining GUC variables */
4843 	read_nondefault_variables();
4844 
4845 	/*
4846 	 * Reload any libraries that were preloaded by the postmaster.  Since we
4847 	 * exec'd this process, those libraries didn't come along with us; but we
4848 	 * should load them into all child processes to be consistent with the
4849 	 * non-EXEC_BACKEND behavior.
4850 	 */
4851 	process_shared_preload_libraries();
4852 
4853 	/* Run backend or appropriate child */
4854 	if (strcmp(argv[1], "--forkbackend") == 0)
4855 	{
4856 		Assert(argc == 3);		/* shouldn't be any more args */
4857 
4858 		/*
4859 		 * Need to reinitialize the SSL library in the backend, since the
4860 		 * context structures contain function pointers and cannot be passed
4861 		 * through the parameter file.
4862 		 *
4863 		 * XXX should we do this in all child processes?  For the moment it's
4864 		 * enough to do it in backend children.
4865 		 */
4866 #ifdef USE_SSL
4867 		if (EnableSSL)
4868 			secure_initialize();
4869 #endif
4870 
4871 		/*
4872 		 * Perform additional initialization and collect startup packet.
4873 		 *
4874 		 * We want to do this before InitProcess() for a couple of reasons: 1.
4875 		 * so that we aren't eating up a PGPROC slot while waiting on the
4876 		 * client. 2. so that if InitProcess() fails due to being out of
4877 		 * PGPROC slots, we have already initialized libpq and are able to
4878 		 * report the error to the client.
4879 		 */
4880 		BackendInitialize(&port);
4881 
4882 		/* Restore basic shared memory pointers */
4883 		InitShmemAccess(UsedShmemSegAddr);
4884 
4885 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4886 		InitProcess();
4887 
4888 		/* Attach process to shared data structures */
4889 		CreateSharedMemoryAndSemaphores(0);
4890 
4891 		/* And run the backend */
4892 		BackendRun(&port);		/* does not return */
4893 	}
4894 	if (strcmp(argv[1], "--forkboot") == 0)
4895 	{
4896 		/* Restore basic shared memory pointers */
4897 		InitShmemAccess(UsedShmemSegAddr);
4898 
4899 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4900 		InitAuxiliaryProcess();
4901 
4902 		/* Attach process to shared data structures */
4903 		CreateSharedMemoryAndSemaphores(0);
4904 
4905 		AuxiliaryProcessMain(argc - 2, argv + 2);		/* does not return */
4906 	}
4907 	if (strcmp(argv[1], "--forkavlauncher") == 0)
4908 	{
4909 		/* Restore basic shared memory pointers */
4910 		InitShmemAccess(UsedShmemSegAddr);
4911 
4912 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4913 		InitProcess();
4914 
4915 		/* Attach process to shared data structures */
4916 		CreateSharedMemoryAndSemaphores(0);
4917 
4918 		AutoVacLauncherMain(argc - 2, argv + 2);		/* does not return */
4919 	}
4920 	if (strcmp(argv[1], "--forkavworker") == 0)
4921 	{
4922 		/* Restore basic shared memory pointers */
4923 		InitShmemAccess(UsedShmemSegAddr);
4924 
4925 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4926 		InitProcess();
4927 
4928 		/* Attach process to shared data structures */
4929 		CreateSharedMemoryAndSemaphores(0);
4930 
4931 		AutoVacWorkerMain(argc - 2, argv + 2);	/* does not return */
4932 	}
4933 	if (strncmp(argv[1], "--forkbgworker=", 15) == 0)
4934 	{
4935 		int			shmem_slot;
4936 
4937 		/* do this as early as possible; in particular, before InitProcess() */
4938 		IsBackgroundWorker = true;
4939 
4940 		/* Restore basic shared memory pointers */
4941 		InitShmemAccess(UsedShmemSegAddr);
4942 
4943 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
4944 		InitProcess();
4945 
4946 		/* Attach process to shared data structures */
4947 		CreateSharedMemoryAndSemaphores(0);
4948 
4949 		/* Fetch MyBgworkerEntry from shared memory */
4950 		shmem_slot = atoi(argv[1] + 15);
4951 		MyBgworkerEntry = BackgroundWorkerEntry(shmem_slot);
4952 
4953 		StartBackgroundWorker();
4954 	}
4955 	if (strcmp(argv[1], "--forkarch") == 0)
4956 	{
4957 		/* Do not want to attach to shared memory */
4958 
4959 		PgArchiverMain(argc, argv);		/* does not return */
4960 	}
4961 	if (strcmp(argv[1], "--forkcol") == 0)
4962 	{
4963 		/* Do not want to attach to shared memory */
4964 
4965 		PgstatCollectorMain(argc, argv);		/* does not return */
4966 	}
4967 	if (strcmp(argv[1], "--forklog") == 0)
4968 	{
4969 		/* Do not want to attach to shared memory */
4970 
4971 		SysLoggerMain(argc, argv);		/* does not return */
4972 	}
4973 
4974 	abort();					/* shouldn't get here */
4975 }
4976 #endif   /* EXEC_BACKEND */
4977 
4978 
4979 /*
4980  * ExitPostmaster -- cleanup
4981  *
4982  * Do NOT call exit() directly --- always go through here!
4983  */
4984 static void
4985 ExitPostmaster(int status)
4986 {
4987 #ifdef HAVE_PTHREAD_IS_THREADED_NP
4988 
4989 	/*
4990 	 * There is no known cause for a postmaster to become multithreaded after
4991 	 * startup.  Recheck to account for the possibility of unknown causes.
4992 	 * This message uses LOG level, because an unclean shutdown at this point
4993 	 * would usually not look much different from a clean shutdown.
4994 	 */
4995 	if (pthread_is_threaded_np() != 0)
4996 		ereport(LOG,
4997 				(errcode(ERRCODE_INTERNAL_ERROR),
4998 				 errmsg_internal("postmaster became multithreaded"),
4999 		   errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
5000 #endif
5001 
5002 	/* should cleanup shared memory and kill all backends */
5003 
5004 	/*
5005 	 * Not sure of the semantics here.  When the Postmaster dies, should the
5006 	 * backends all be killed? probably not.
5007 	 *
5008 	 * MUST		-- vadim 05-10-1999
5009 	 */
5010 
5011 	proc_exit(status);
5012 }
5013 
5014 /*
5015  * sigusr1_handler - handle signal conditions from child processes
5016  */
5017 static void
5018 sigusr1_handler(SIGNAL_ARGS)
5019 {
5020 	int			save_errno = errno;
5021 
5022 	/*
5023 	 * We rely on the signal mechanism to have blocked all signals ... except
5024 	 * on Windows, which lacks sigaction(), so we have to do it manually.
5025 	 */
5026 #ifdef WIN32
5027 	PG_SETMASK(&BlockSig);
5028 #endif
5029 
5030 	/* Process background worker state change. */
5031 	if (CheckPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE))
5032 	{
5033 		BackgroundWorkerStateChange();
5034 		StartWorkerNeeded = true;
5035 	}
5036 
5037 	/*
5038 	 * RECOVERY_STARTED and BEGIN_HOT_STANDBY signals are ignored in
5039 	 * unexpected states. If the startup process quickly starts up, completes
5040 	 * recovery, exits, we might process the death of the startup process
5041 	 * first. We don't want to go back to recovery in that case.
5042 	 */
5043 	if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_STARTED) &&
5044 		pmState == PM_STARTUP && Shutdown == NoShutdown)
5045 	{
5046 		/* WAL redo has started. We're out of reinitialization. */
5047 		FatalError = false;
5048 		Assert(AbortStartTime == 0);
5049 
5050 		/*
5051 		 * Crank up the background tasks.  It doesn't matter if this fails,
5052 		 * we'll just try again later.
5053 		 */
5054 		Assert(CheckpointerPID == 0);
5055 		CheckpointerPID = StartCheckpointer();
5056 		Assert(BgWriterPID == 0);
5057 		BgWriterPID = StartBackgroundWriter();
5058 
5059 		/*
5060 		 * Start the archiver if we're responsible for (re-)archiving received
5061 		 * files.
5062 		 */
5063 		Assert(PgArchPID == 0);
5064 		if (XLogArchivingAlways())
5065 			PgArchPID = pgarch_start();
5066 
5067 #ifdef USE_SYSTEMD
5068 		if (!EnableHotStandby)
5069 			sd_notify(0, "READY=1");
5070 #endif
5071 
5072 		pmState = PM_RECOVERY;
5073 	}
5074 	if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) &&
5075 		pmState == PM_RECOVERY && Shutdown == NoShutdown)
5076 	{
5077 		/*
5078 		 * Likewise, start other special children as needed.
5079 		 */
5080 		Assert(PgStatPID == 0);
5081 		PgStatPID = pgstat_start();
5082 
5083 		ereport(LOG,
5084 		(errmsg("database system is ready to accept read only connections")));
5085 
5086 #ifdef USE_SYSTEMD
5087 		sd_notify(0, "READY=1");
5088 #endif
5089 
5090 		pmState = PM_HOT_STANDBY;
5091 		connsAllowed = ALLOW_ALL_CONNS;
5092 
5093 		/* Some workers may be scheduled to start now */
5094 		StartWorkerNeeded = true;
5095 	}
5096 
5097 	if (StartWorkerNeeded || HaveCrashedWorker)
5098 		maybe_start_bgworkers();
5099 
5100 	if (CheckPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER) &&
5101 		PgArchPID != 0)
5102 	{
5103 		/*
5104 		 * Send SIGUSR1 to archiver process, to wake it up and begin archiving
5105 		 * next transaction log file.
5106 		 */
5107 		signal_child(PgArchPID, SIGUSR1);
5108 	}
5109 
5110 	if (CheckPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE) &&
5111 		SysLoggerPID != 0)
5112 	{
5113 		/* Tell syslogger to rotate logfile */
5114 		signal_child(SysLoggerPID, SIGUSR1);
5115 	}
5116 
5117 	if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER) &&
5118 		Shutdown <= SmartShutdown && pmState < PM_STOP_BACKENDS)
5119 	{
5120 		/*
5121 		 * Start one iteration of the autovacuum daemon, even if autovacuuming
5122 		 * is nominally not enabled.  This is so we can have an active defense
5123 		 * against transaction ID wraparound.  We set a flag for the main loop
5124 		 * to do it rather than trying to do it here --- this is because the
5125 		 * autovac process itself may send the signal, and we want to handle
5126 		 * that by launching another iteration as soon as the current one
5127 		 * completes.
5128 		 */
5129 		start_autovac_launcher = true;
5130 	}
5131 
5132 	if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER) &&
5133 		Shutdown <= SmartShutdown && pmState < PM_STOP_BACKENDS)
5134 	{
5135 		/* The autovacuum launcher wants us to start a worker process. */
5136 		StartAutovacuumWorker();
5137 	}
5138 
5139 	if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER))
5140 	{
5141 		/* Startup Process wants us to start the walreceiver process. */
5142 		/* Start immediately if possible, else remember request for later. */
5143 		WalReceiverRequested = true;
5144 		MaybeStartWalReceiver();
5145 	}
5146 
5147 	/*
5148 	 * Try to advance postmaster's state machine, if a child requests it.
5149 	 *
5150 	 * Be careful about the order of this action relative to sigusr1_handler's
5151 	 * other actions.  Generally, this should be after other actions, in case
5152 	 * they have effects PostmasterStateMachine would need to know about.
5153 	 * However, we should do it before the CheckPromoteSignal step, which
5154 	 * cannot have any (immediate) effect on the state machine, but does
5155 	 * depend on what state we're in now.
5156 	 */
5157 	if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE))
5158 	{
5159 		PostmasterStateMachine();
5160 	}
5161 
5162 	if (StartupPID != 0 &&
5163 		(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
5164 		 pmState == PM_HOT_STANDBY) &&
5165 		CheckPromoteSignal())
5166 	{
5167 		/* Tell startup process to finish recovery */
5168 		signal_child(StartupPID, SIGUSR2);
5169 	}
5170 
5171 #ifdef WIN32
5172 	PG_SETMASK(&UnBlockSig);
5173 #endif
5174 
5175 	errno = save_errno;
5176 }
5177 
5178 /*
5179  * SIGTERM while processing startup packet.
5180  * Clean up and exit(1).
5181  *
5182  * Running proc_exit() from a signal handler is pretty unsafe, since we
5183  * can't know what code we've interrupted.  But the alternative of using
5184  * _exit(2) is also unpalatable, since it'd mean that a "fast shutdown"
5185  * would cause a database crash cycle (forcing WAL replay at restart)
5186  * if any sessions are in authentication.  So we live with it for now.
5187  *
5188  * One might be tempted to try to send a message indicating why we are
5189  * disconnecting.  However, that would make this even more unsafe.  Also,
5190  * it seems undesirable to provide clues about the database's state to
5191  * a client that has not yet completed authentication.
5192  */
5193 static void
5194 process_startup_packet_die(SIGNAL_ARGS)
5195 {
5196 	proc_exit(1);
5197 }
5198 
5199 /*
5200  * SIGQUIT while processing startup packet.
5201  *
5202  * Some backend has bought the farm,
5203  * so we need to stop what we're doing and exit.
5204  */
5205 static void
5206 process_startup_packet_quickdie(SIGNAL_ARGS)
5207 {
5208 	/*
5209 	 * We DO NOT want to run proc_exit() or atexit() callbacks; they wouldn't
5210 	 * be safe to run from a signal handler.  Just nail the windows shut and
5211 	 * get out of town.
5212 	 *
5213 	 * Note we do _exit(2) not _exit(1).  This is to force the postmaster into
5214 	 * a system reset cycle if someone sends a manual SIGQUIT to a random
5215 	 * backend.  (While it might be safe to do _exit(1), since this session
5216 	 * shouldn't have touched shared memory yet, there seems little point in
5217 	 * taking any risks.)
5218 	 */
5219 	_exit(2);
5220 }
5221 
5222 /*
5223  * Dummy signal handler
5224  *
5225  * We use this for signals that we don't actually use in the postmaster,
5226  * but we do use in backends.  If we were to SIG_IGN such signals in the
5227  * postmaster, then a newly started backend might drop a signal that arrives
5228  * before it's able to reconfigure its signal processing.  (See notes in
5229  * tcop/postgres.c.)
5230  */
5231 static void
5232 dummy_handler(SIGNAL_ARGS)
5233 {
5234 }
5235 
5236 /*
5237  * Timeout while processing startup packet.
5238  * As for process_startup_packet_die(), we clean up and exit(1).
5239  *
5240  * This is theoretically just as hazardous as in process_startup_packet_die(),
5241  * although in practice we're almost certainly waiting for client input,
5242  * which greatly reduces the risk.
5243  */
5244 static void
5245 StartupPacketTimeoutHandler(void)
5246 {
5247 	proc_exit(1);
5248 }
5249 
5250 
5251 /*
5252  * RandomSalt
5253  */
5254 static void
5255 RandomSalt(char *md5Salt)
5256 {
5257 	long		rand;
5258 
5259 	/*
5260 	 * We use % 255, sacrificing one possible byte value, so as to ensure that
5261 	 * all bits of the random() value participate in the result. While at it,
5262 	 * add one to avoid generating any null bytes.
5263 	 */
5264 	rand = PostmasterRandom();
5265 	md5Salt[0] = (rand % 255) + 1;
5266 	rand = PostmasterRandom();
5267 	md5Salt[1] = (rand % 255) + 1;
5268 	rand = PostmasterRandom();
5269 	md5Salt[2] = (rand % 255) + 1;
5270 	rand = PostmasterRandom();
5271 	md5Salt[3] = (rand % 255) + 1;
5272 }
5273 
5274 /*
5275  * PostmasterRandom
5276  *
5277  * Caution: use this only for values needed during connection-request
5278  * processing.  Otherwise, the intended property of having an unpredictable
5279  * delay between random_start_time and random_stop_time will be broken.
5280  */
5281 static long
5282 PostmasterRandom(void)
5283 {
5284 	/*
5285 	 * Select a random seed at the time of first receiving a request.
5286 	 */
5287 	if (random_seed == 0)
5288 	{
5289 		do
5290 		{
5291 			struct timeval random_stop_time;
5292 
5293 			gettimeofday(&random_stop_time, NULL);
5294 
5295 			/*
5296 			 * We are not sure how much precision is in tv_usec, so we swap
5297 			 * the high and low 16 bits of 'random_stop_time' and XOR them
5298 			 * with 'random_start_time'. On the off chance that the result is
5299 			 * 0, we loop until it isn't.
5300 			 */
5301 			random_seed = random_start_time.tv_usec ^
5302 				((random_stop_time.tv_usec << 16) |
5303 				 ((random_stop_time.tv_usec >> 16) & 0xffff));
5304 		}
5305 		while (random_seed == 0);
5306 
5307 		srandom(random_seed);
5308 	}
5309 
5310 	return random();
5311 }
5312 
5313 /*
5314  * Count up number of child processes of specified types (dead_end children
5315  * are always excluded).
5316  */
5317 static int
5318 CountChildren(int target)
5319 {
5320 	dlist_iter	iter;
5321 	int			cnt = 0;
5322 
5323 	dlist_foreach(iter, &BackendList)
5324 	{
5325 		Backend    *bp = dlist_container(Backend, elem, iter.cur);
5326 
5327 		if (bp->dead_end)
5328 			continue;
5329 
5330 		/*
5331 		 * Since target == BACKEND_TYPE_ALL is the most common case, we test
5332 		 * it first and avoid touching shared memory for every child.
5333 		 */
5334 		if (target != BACKEND_TYPE_ALL)
5335 		{
5336 			/*
5337 			 * Assign bkend_type for any recently announced WAL Sender
5338 			 * processes.
5339 			 */
5340 			if (bp->bkend_type == BACKEND_TYPE_NORMAL &&
5341 				IsPostmasterChildWalSender(bp->child_slot))
5342 				bp->bkend_type = BACKEND_TYPE_WALSND;
5343 
5344 			if (!(target & bp->bkend_type))
5345 				continue;
5346 		}
5347 
5348 		cnt++;
5349 	}
5350 	return cnt;
5351 }
5352 
5353 
5354 /*
5355  * StartChildProcess -- start an auxiliary process for the postmaster
5356  *
5357  * "type" determines what kind of child will be started.  All child types
5358  * initially go to AuxiliaryProcessMain, which will handle common setup.
5359  *
5360  * Return value of StartChildProcess is subprocess' PID, or 0 if failed
5361  * to start subprocess.
5362  */
5363 static pid_t
5364 StartChildProcess(AuxProcType type)
5365 {
5366 	pid_t		pid;
5367 	char	   *av[10];
5368 	int			ac = 0;
5369 	char		typebuf[32];
5370 
5371 	/*
5372 	 * Set up command-line arguments for subprocess
5373 	 */
5374 	av[ac++] = "postgres";
5375 
5376 #ifdef EXEC_BACKEND
5377 	av[ac++] = "--forkboot";
5378 	av[ac++] = NULL;			/* filled in by postmaster_forkexec */
5379 #endif
5380 
5381 	snprintf(typebuf, sizeof(typebuf), "-x%d", type);
5382 	av[ac++] = typebuf;
5383 
5384 	av[ac] = NULL;
5385 	Assert(ac < lengthof(av));
5386 
5387 #ifdef EXEC_BACKEND
5388 	pid = postmaster_forkexec(ac, av);
5389 #else							/* !EXEC_BACKEND */
5390 	pid = fork_process();
5391 
5392 	if (pid == 0)				/* child */
5393 	{
5394 		InitPostmasterChild();
5395 
5396 		/* Close the postmaster's sockets */
5397 		ClosePostmasterPorts(false);
5398 
5399 		/* Release postmaster's working memory context */
5400 		MemoryContextSwitchTo(TopMemoryContext);
5401 		MemoryContextDelete(PostmasterContext);
5402 		PostmasterContext = NULL;
5403 
5404 		AuxiliaryProcessMain(ac, av);
5405 		ExitPostmaster(0);
5406 	}
5407 #endif   /* EXEC_BACKEND */
5408 
5409 	if (pid < 0)
5410 	{
5411 		/* in parent, fork failed */
5412 		int			save_errno = errno;
5413 
5414 		errno = save_errno;
5415 		switch (type)
5416 		{
5417 			case StartupProcess:
5418 				ereport(LOG,
5419 						(errmsg("could not fork startup process: %m")));
5420 				break;
5421 			case BgWriterProcess:
5422 				ereport(LOG,
5423 				   (errmsg("could not fork background writer process: %m")));
5424 				break;
5425 			case CheckpointerProcess:
5426 				ereport(LOG,
5427 						(errmsg("could not fork checkpointer process: %m")));
5428 				break;
5429 			case WalWriterProcess:
5430 				ereport(LOG,
5431 						(errmsg("could not fork WAL writer process: %m")));
5432 				break;
5433 			case WalReceiverProcess:
5434 				ereport(LOG,
5435 						(errmsg("could not fork WAL receiver process: %m")));
5436 				break;
5437 			default:
5438 				ereport(LOG,
5439 						(errmsg("could not fork process: %m")));
5440 				break;
5441 		}
5442 
5443 		/*
5444 		 * fork failure is fatal during startup, but there's no need to choke
5445 		 * immediately if starting other child types fails.
5446 		 */
5447 		if (type == StartupProcess)
5448 			ExitPostmaster(1);
5449 		return 0;
5450 	}
5451 
5452 	/*
5453 	 * in parent, successful fork
5454 	 */
5455 	return pid;
5456 }
5457 
5458 /*
5459  * StartAutovacuumWorker
5460  *		Start an autovac worker process.
5461  *
5462  * This function is here because it enters the resulting PID into the
5463  * postmaster's private backends list.
5464  *
5465  * NB -- this code very roughly matches BackendStartup.
5466  */
5467 static void
5468 StartAutovacuumWorker(void)
5469 {
5470 	Backend    *bn;
5471 
5472 	/*
5473 	 * If not in condition to run a process, don't try, but handle it like a
5474 	 * fork failure.  This does not normally happen, since the signal is only
5475 	 * supposed to be sent by autovacuum launcher when it's OK to do it, but
5476 	 * we have to check to avoid race-condition problems during DB state
5477 	 * changes.
5478 	 */
5479 	if (canAcceptConnections(BACKEND_TYPE_AUTOVAC) == CAC_OK)
5480 	{
5481 		bn = (Backend *) malloc(sizeof(Backend));
5482 		if (bn)
5483 		{
5484 			/*
5485 			 * Compute the cancel key that will be assigned to this session.
5486 			 * We probably don't need cancel keys for autovac workers, but
5487 			 * we'd better have something random in the field to prevent
5488 			 * unfriendly people from sending cancels to them.
5489 			 */
5490 			MyCancelKey = PostmasterRandom();
5491 			bn->cancel_key = MyCancelKey;
5492 
5493 			/* Autovac workers are not dead_end and need a child slot */
5494 			bn->dead_end = false;
5495 			bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
5496 			bn->bgworker_notify = false;
5497 
5498 			bn->pid = StartAutoVacWorker();
5499 			if (bn->pid > 0)
5500 			{
5501 				bn->bkend_type = BACKEND_TYPE_AUTOVAC;
5502 				dlist_push_head(&BackendList, &bn->elem);
5503 #ifdef EXEC_BACKEND
5504 				ShmemBackendArrayAdd(bn);
5505 #endif
5506 				/* all OK */
5507 				return;
5508 			}
5509 
5510 			/*
5511 			 * fork failed, fall through to report -- actual error message was
5512 			 * logged by StartAutoVacWorker
5513 			 */
5514 			(void) ReleasePostmasterChildSlot(bn->child_slot);
5515 			free(bn);
5516 		}
5517 		else
5518 			ereport(LOG,
5519 					(errcode(ERRCODE_OUT_OF_MEMORY),
5520 					 errmsg("out of memory")));
5521 	}
5522 
5523 	/*
5524 	 * Report the failure to the launcher, if it's running.  (If it's not, we
5525 	 * might not even be connected to shared memory, so don't try to call
5526 	 * AutoVacWorkerFailed.)  Note that we also need to signal it so that it
5527 	 * responds to the condition, but we don't do that here, instead waiting
5528 	 * for ServerLoop to do it.  This way we avoid a ping-pong signalling in
5529 	 * quick succession between the autovac launcher and postmaster in case
5530 	 * things get ugly.
5531 	 */
5532 	if (AutoVacPID != 0)
5533 	{
5534 		AutoVacWorkerFailed();
5535 		avlauncher_needs_signal = true;
5536 	}
5537 }
5538 
5539 /*
5540  * MaybeStartWalReceiver
5541  *		Start the WAL receiver process, if not running and our state allows.
5542  *
5543  * Note: if WalReceiverPID is already nonzero, it might seem that we should
5544  * clear WalReceiverRequested.  However, there's a race condition if the
5545  * walreceiver terminates and the startup process immediately requests a new
5546  * one: it's quite possible to get the signal for the request before reaping
5547  * the dead walreceiver process.  Better to risk launching an extra
5548  * walreceiver than to miss launching one we need.  (The walreceiver code
5549  * has logic to recognize that it should go away if not needed.)
5550  */
5551 static void
5552 MaybeStartWalReceiver(void)
5553 {
5554 	if (WalReceiverPID == 0 &&
5555 		(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
5556 		 pmState == PM_HOT_STANDBY) &&
5557 		Shutdown <= SmartShutdown)
5558 	{
5559 		WalReceiverPID = StartWalReceiver();
5560 		if (WalReceiverPID != 0)
5561 			WalReceiverRequested = false;
5562 		/* else leave the flag set, so we'll try again later */
5563 	}
5564 }
5565 
5566 
5567 /*
5568  * Create the opts file
5569  */
5570 static bool
5571 CreateOptsFile(int argc, char *argv[], char *fullprogname)
5572 {
5573 	FILE	   *fp;
5574 	int			i;
5575 
5576 #define OPTS_FILE	"postmaster.opts"
5577 
5578 	if ((fp = fopen(OPTS_FILE, "w")) == NULL)
5579 	{
5580 		elog(LOG, "could not create file \"%s\": %m", OPTS_FILE);
5581 		return false;
5582 	}
5583 
5584 	fprintf(fp, "%s", fullprogname);
5585 	for (i = 1; i < argc; i++)
5586 		fprintf(fp, " \"%s\"", argv[i]);
5587 	fputs("\n", fp);
5588 
5589 	if (fclose(fp))
5590 	{
5591 		elog(LOG, "could not write file \"%s\": %m", OPTS_FILE);
5592 		return false;
5593 	}
5594 
5595 	return true;
5596 }
5597 
5598 
5599 /*
5600  * MaxLivePostmasterChildren
5601  *
5602  * This reports the number of entries needed in per-child-process arrays
5603  * (the PMChildFlags array, and if EXEC_BACKEND the ShmemBackendArray).
5604  * These arrays include regular backends, autovac workers, walsenders
5605  * and background workers, but not special children nor dead_end children.
5606  * This allows the arrays to have a fixed maximum size, to wit the same
5607  * too-many-children limit enforced by canAcceptConnections().  The exact value
5608  * isn't too critical as long as it's more than MaxBackends.
5609  */
5610 int
5611 MaxLivePostmasterChildren(void)
5612 {
5613 	return 2 * (MaxConnections + autovacuum_max_workers + 1 +
5614 				max_worker_processes);
5615 }
5616 
5617 /*
5618  * Connect background worker to a database.
5619  */
5620 void
5621 BackgroundWorkerInitializeConnection(char *dbname, char *username)
5622 {
5623 	BackgroundWorker *worker = MyBgworkerEntry;
5624 
5625 	/* XXX is this the right errcode? */
5626 	if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
5627 		ereport(FATAL,
5628 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5629 				 errmsg("database connection requirement not indicated during registration")));
5630 
5631 	InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL);
5632 
5633 	/* it had better not gotten out of "init" mode yet */
5634 	if (!IsInitProcessingMode())
5635 		ereport(ERROR,
5636 				(errmsg("invalid processing mode in background worker")));
5637 	SetProcessingMode(NormalProcessing);
5638 }
5639 
5640 /*
5641  * Connect background worker to a database using OIDs.
5642  */
5643 void
5644 BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
5645 {
5646 	BackgroundWorker *worker = MyBgworkerEntry;
5647 
5648 	/* XXX is this the right errcode? */
5649 	if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
5650 		ereport(FATAL,
5651 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5652 				 errmsg("database connection requirement not indicated during registration")));
5653 
5654 	InitPostgres(NULL, dboid, NULL, useroid, NULL);
5655 
5656 	/* it had better not gotten out of "init" mode yet */
5657 	if (!IsInitProcessingMode())
5658 		ereport(ERROR,
5659 				(errmsg("invalid processing mode in background worker")));
5660 	SetProcessingMode(NormalProcessing);
5661 }
5662 
5663 /*
5664  * Block/unblock signals in a background worker
5665  */
5666 void
5667 BackgroundWorkerBlockSignals(void)
5668 {
5669 	PG_SETMASK(&BlockSig);
5670 }
5671 
5672 void
5673 BackgroundWorkerUnblockSignals(void)
5674 {
5675 	PG_SETMASK(&UnBlockSig);
5676 }
5677 
5678 #ifdef EXEC_BACKEND
5679 static pid_t
5680 bgworker_forkexec(int shmem_slot)
5681 {
5682 	char	   *av[10];
5683 	int			ac = 0;
5684 	char		forkav[MAXPGPATH];
5685 
5686 	snprintf(forkav, MAXPGPATH, "--forkbgworker=%d", shmem_slot);
5687 
5688 	av[ac++] = "postgres";
5689 	av[ac++] = forkav;
5690 	av[ac++] = NULL;			/* filled in by postmaster_forkexec */
5691 	av[ac] = NULL;
5692 
5693 	Assert(ac < lengthof(av));
5694 
5695 	return postmaster_forkexec(ac, av);
5696 }
5697 #endif
5698 
5699 /*
5700  * Start a new bgworker.
5701  * Starting time conditions must have been checked already.
5702  *
5703  * Returns true on success, false on failure.
5704  * In either case, update the RegisteredBgWorker's state appropriately.
5705  *
5706  * This code is heavily based on autovacuum.c, q.v.
5707  */
5708 static bool
5709 do_start_bgworker(RegisteredBgWorker *rw)
5710 {
5711 	pid_t		worker_pid;
5712 
5713 	Assert(rw->rw_pid == 0);
5714 
5715 	/*
5716 	 * Allocate and assign the Backend element.  Note we must do this before
5717 	 * forking, so that we can handle failures (out of memory or child-process
5718 	 * slots) cleanly.
5719 	 *
5720 	 * Treat failure as though the worker had crashed.  That way, the
5721 	 * postmaster will wait a bit before attempting to start it again; if we
5722 	 * tried again right away, most likely we'd find ourselves hitting the
5723 	 * same resource-exhaustion condition.
5724 	 */
5725 	if (!assign_backendlist_entry(rw))
5726 	{
5727 		rw->rw_crashed_at = GetCurrentTimestamp();
5728 		return false;
5729 	}
5730 
5731 	ereport(DEBUG1,
5732 			(errmsg("starting background worker process \"%s\"",
5733 					rw->rw_worker.bgw_name)));
5734 
5735 #ifdef EXEC_BACKEND
5736 	switch ((worker_pid = bgworker_forkexec(rw->rw_shmem_slot)))
5737 #else
5738 	switch ((worker_pid = fork_process()))
5739 #endif
5740 	{
5741 		case -1:
5742 			/* in postmaster, fork failed ... */
5743 			ereport(LOG,
5744 					(errmsg("could not fork worker process: %m")));
5745 			/* undo what assign_backendlist_entry did */
5746 			ReleasePostmasterChildSlot(rw->rw_child_slot);
5747 			rw->rw_child_slot = 0;
5748 			free(rw->rw_backend);
5749 			rw->rw_backend = NULL;
5750 			/* mark entry as crashed, so we'll try again later */
5751 			rw->rw_crashed_at = GetCurrentTimestamp();
5752 			break;
5753 
5754 #ifndef EXEC_BACKEND
5755 		case 0:
5756 			/* in postmaster child ... */
5757 			InitPostmasterChild();
5758 
5759 			/* Close the postmaster's sockets */
5760 			ClosePostmasterPorts(false);
5761 
5762 			/*
5763 			 * Before blowing away PostmasterContext, save this bgworker's
5764 			 * data where it can find it.
5765 			 */
5766 			MyBgworkerEntry = (BackgroundWorker *)
5767 				MemoryContextAlloc(TopMemoryContext, sizeof(BackgroundWorker));
5768 			memcpy(MyBgworkerEntry, &rw->rw_worker, sizeof(BackgroundWorker));
5769 
5770 			/* Release postmaster's working memory context */
5771 			MemoryContextSwitchTo(TopMemoryContext);
5772 			MemoryContextDelete(PostmasterContext);
5773 			PostmasterContext = NULL;
5774 
5775 			StartBackgroundWorker();
5776 
5777 			exit(1);			/* should not get here */
5778 			break;
5779 #endif
5780 		default:
5781 			/* in postmaster, fork successful ... */
5782 			rw->rw_pid = worker_pid;
5783 			rw->rw_backend->pid = rw->rw_pid;
5784 			ReportBackgroundWorkerPID(rw);
5785 			/* add new worker to lists of backends */
5786 			dlist_push_head(&BackendList, &rw->rw_backend->elem);
5787 #ifdef EXEC_BACKEND
5788 			ShmemBackendArrayAdd(rw->rw_backend);
5789 #endif
5790 			return true;
5791 	}
5792 
5793 	return false;
5794 }
5795 
5796 /*
5797  * Does the current postmaster state require starting a worker with the
5798  * specified start_time?
5799  */
5800 static bool
5801 bgworker_should_start_now(BgWorkerStartTime start_time)
5802 {
5803 	switch (pmState)
5804 	{
5805 		case PM_NO_CHILDREN:
5806 		case PM_WAIT_DEAD_END:
5807 		case PM_SHUTDOWN_2:
5808 		case PM_SHUTDOWN:
5809 		case PM_WAIT_BACKENDS:
5810 		case PM_STOP_BACKENDS:
5811 			break;
5812 
5813 		case PM_RUN:
5814 			if (start_time == BgWorkerStart_RecoveryFinished)
5815 				return true;
5816 			/* fall through */
5817 
5818 		case PM_HOT_STANDBY:
5819 			if (start_time == BgWorkerStart_ConsistentState)
5820 				return true;
5821 			/* fall through */
5822 
5823 		case PM_RECOVERY:
5824 		case PM_STARTUP:
5825 		case PM_INIT:
5826 			if (start_time == BgWorkerStart_PostmasterStart)
5827 				return true;
5828 			/* fall through */
5829 
5830 	}
5831 
5832 	return false;
5833 }
5834 
5835 /*
5836  * Allocate the Backend struct for a connected background worker, but don't
5837  * add it to the list of backends just yet.
5838  *
5839  * On failure, return false without changing any worker state.
5840  *
5841  * Some info from the Backend is copied into the passed rw.
5842  */
5843 static bool
5844 assign_backendlist_entry(RegisteredBgWorker *rw)
5845 {
5846 	Backend    *bn;
5847 
5848 	/*
5849 	 * Check that database state allows another connection.  Currently the
5850 	 * only possible failure is CAC_TOOMANY, so we just log an error message
5851 	 * based on that rather than checking the error code precisely.
5852 	 */
5853 	if (canAcceptConnections(BACKEND_TYPE_BGWORKER) != CAC_OK)
5854 	{
5855 		ereport(LOG,
5856 				(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
5857 				 errmsg("no slot available for new worker process")));
5858 		return false;
5859 	}
5860 
5861 	bn = malloc(sizeof(Backend));
5862 	if (bn == NULL)
5863 	{
5864 		ereport(LOG,
5865 				(errcode(ERRCODE_OUT_OF_MEMORY),
5866 				 errmsg("out of memory")));
5867 		return false;
5868 	}
5869 
5870 	/*
5871 	 * Compute the cancel key that will be assigned to this session. We
5872 	 * probably don't need cancel keys for background workers, but we'd better
5873 	 * have something random in the field to prevent unfriendly people from
5874 	 * sending cancels to them.
5875 	 */
5876 	MyCancelKey = PostmasterRandom();
5877 	bn->cancel_key = MyCancelKey;
5878 
5879 	bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
5880 	bn->bkend_type = BACKEND_TYPE_BGWORKER;
5881 	bn->dead_end = false;
5882 	bn->bgworker_notify = false;
5883 
5884 	rw->rw_backend = bn;
5885 	rw->rw_child_slot = bn->child_slot;
5886 
5887 	return true;
5888 }
5889 
5890 /*
5891  * If the time is right, start background worker(s).
5892  *
5893  * As a side effect, the bgworker control variables are set or reset
5894  * depending on whether more workers may need to be started.
5895  *
5896  * We limit the number of workers started per call, to avoid consuming the
5897  * postmaster's attention for too long when many such requests are pending.
5898  * As long as StartWorkerNeeded is true, ServerLoop will not block and will
5899  * call this function again after dealing with any other issues.
5900  */
5901 static void
5902 maybe_start_bgworkers(void)
5903 {
5904 #define MAX_BGWORKERS_TO_LAUNCH 100
5905 	int			num_launched = 0;
5906 	TimestampTz now = 0;
5907 	slist_mutable_iter iter;
5908 
5909 	/*
5910 	 * During crash recovery, we have no need to be called until the state
5911 	 * transition out of recovery.
5912 	 */
5913 	if (FatalError)
5914 	{
5915 		StartWorkerNeeded = false;
5916 		HaveCrashedWorker = false;
5917 		return;
5918 	}
5919 
5920 	/* Don't need to be called again unless we find a reason for it below */
5921 	StartWorkerNeeded = false;
5922 	HaveCrashedWorker = false;
5923 
5924 	slist_foreach_modify(iter, &BackgroundWorkerList)
5925 	{
5926 		RegisteredBgWorker *rw;
5927 
5928 		rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
5929 
5930 		/* ignore if already running */
5931 		if (rw->rw_pid != 0)
5932 			continue;
5933 
5934 		/* if marked for death, clean up and remove from list */
5935 		if (rw->rw_terminate)
5936 		{
5937 			ForgetBackgroundWorker(&iter);
5938 			continue;
5939 		}
5940 
5941 		/*
5942 		 * If this worker has crashed previously, maybe it needs to be
5943 		 * restarted (unless on registration it specified it doesn't want to
5944 		 * be restarted at all).  Check how long ago did a crash last happen.
5945 		 * If the last crash is too recent, don't start it right away; let it
5946 		 * be restarted once enough time has passed.
5947 		 */
5948 		if (rw->rw_crashed_at != 0)
5949 		{
5950 			if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
5951 			{
5952 				int			notify_pid;
5953 
5954 				notify_pid = rw->rw_worker.bgw_notify_pid;
5955 
5956 				ForgetBackgroundWorker(&iter);
5957 
5958 				/* Report worker is gone now. */
5959 				if (notify_pid != 0)
5960 					kill(notify_pid, SIGUSR1);
5961 
5962 				continue;
5963 			}
5964 
5965 			/* read system time only when needed */
5966 			if (now == 0)
5967 				now = GetCurrentTimestamp();
5968 
5969 			if (!TimestampDifferenceExceeds(rw->rw_crashed_at, now,
5970 									  rw->rw_worker.bgw_restart_time * 1000))
5971 			{
5972 				/* Set flag to remember that we have workers to start later */
5973 				HaveCrashedWorker = true;
5974 				continue;
5975 			}
5976 		}
5977 
5978 		if (bgworker_should_start_now(rw->rw_worker.bgw_start_time))
5979 		{
5980 			/* reset crash time before trying to start worker */
5981 			rw->rw_crashed_at = 0;
5982 
5983 			/*
5984 			 * Try to start the worker.
5985 			 *
5986 			 * On failure, give up processing workers for now, but set
5987 			 * StartWorkerNeeded so we'll come back here on the next iteration
5988 			 * of ServerLoop to try again.  (We don't want to wait, because
5989 			 * there might be additional ready-to-run workers.)  We could set
5990 			 * HaveCrashedWorker as well, since this worker is now marked
5991 			 * crashed, but there's no need because the next run of this
5992 			 * function will do that.
5993 			 */
5994 			if (!do_start_bgworker(rw))
5995 			{
5996 				StartWorkerNeeded = true;
5997 				return;
5998 			}
5999 
6000 			/*
6001 			 * If we've launched as many workers as allowed, quit, but have
6002 			 * ServerLoop call us again to look for additional ready-to-run
6003 			 * workers.  There might not be any, but we'll find out the next
6004 			 * time we run.
6005 			 */
6006 			if (++num_launched >= MAX_BGWORKERS_TO_LAUNCH)
6007 			{
6008 				StartWorkerNeeded = true;
6009 				return;
6010 			}
6011 		}
6012 	}
6013 }
6014 
6015 /*
6016  * When a backend asks to be notified about worker state changes, we
6017  * set a flag in its backend entry.  The background worker machinery needs
6018  * to know when such backends exit.
6019  */
6020 bool
6021 PostmasterMarkPIDForWorkerNotify(int pid)
6022 {
6023 	dlist_iter	iter;
6024 	Backend    *bp;
6025 
6026 	dlist_foreach(iter, &BackendList)
6027 	{
6028 		bp = dlist_container(Backend, elem, iter.cur);
6029 		if (bp->pid == pid)
6030 		{
6031 			bp->bgworker_notify = true;
6032 			return true;
6033 		}
6034 	}
6035 	return false;
6036 }
6037 
6038 #ifdef EXEC_BACKEND
6039 
6040 /*
6041  * The following need to be available to the save/restore_backend_variables
6042  * functions.  They are marked NON_EXEC_STATIC in their home modules.
6043  */
6044 extern slock_t *ShmemLock;
6045 extern slock_t *ProcStructLock;
6046 extern PGPROC *AuxiliaryProcs;
6047 extern PMSignalData *PMSignalState;
6048 extern pgsocket pgStatSock;
6049 extern pg_time_t first_syslogger_file_time;
6050 
6051 #ifndef WIN32
6052 #define write_inheritable_socket(dest, src, childpid) ((*(dest) = (src)), true)
6053 #define read_inheritable_socket(dest, src) (*(dest) = *(src))
6054 #else
6055 static bool write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE child);
6056 static bool write_inheritable_socket(InheritableSocket *dest, SOCKET src,
6057 						 pid_t childPid);
6058 static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
6059 #endif
6060 
6061 
6062 /* Save critical backend variables into the BackendParameters struct */
6063 #ifndef WIN32
6064 static bool
6065 save_backend_variables(BackendParameters *param, Port *port)
6066 #else
6067 static bool
6068 save_backend_variables(BackendParameters *param, Port *port,
6069 					   HANDLE childProcess, pid_t childPid)
6070 #endif
6071 {
6072 	memcpy(&param->port, port, sizeof(Port));
6073 	if (!write_inheritable_socket(&param->portsocket, port->sock, childPid))
6074 		return false;
6075 
6076 	strlcpy(param->DataDir, DataDir, MAXPGPATH);
6077 
6078 	memcpy(&param->ListenSocket, &ListenSocket, sizeof(ListenSocket));
6079 
6080 	param->MyCancelKey = MyCancelKey;
6081 	param->MyPMChildSlot = MyPMChildSlot;
6082 
6083 #ifdef WIN32
6084 	param->ShmemProtectiveRegion = ShmemProtectiveRegion;
6085 #endif
6086 	param->UsedShmemSegID = UsedShmemSegID;
6087 	param->UsedShmemSegAddr = UsedShmemSegAddr;
6088 
6089 	param->ShmemLock = ShmemLock;
6090 	param->ShmemVariableCache = ShmemVariableCache;
6091 	param->ShmemBackendArray = ShmemBackendArray;
6092 
6093 #ifndef HAVE_SPINLOCKS
6094 	param->SpinlockSemaArray = SpinlockSemaArray;
6095 #endif
6096 	param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests;
6097 	param->NamedLWLockTrancheArray = NamedLWLockTrancheArray;
6098 	param->MainLWLockArray = MainLWLockArray;
6099 	param->ProcStructLock = ProcStructLock;
6100 	param->ProcGlobal = ProcGlobal;
6101 	param->AuxiliaryProcs = AuxiliaryProcs;
6102 	param->PreparedXactProcs = PreparedXactProcs;
6103 	param->PMSignalState = PMSignalState;
6104 	if (!write_inheritable_socket(&param->pgStatSock, pgStatSock, childPid))
6105 		return false;
6106 
6107 	param->PostmasterPid = PostmasterPid;
6108 	param->PgStartTime = PgStartTime;
6109 	param->PgReloadTime = PgReloadTime;
6110 	param->first_syslogger_file_time = first_syslogger_file_time;
6111 
6112 	param->redirection_done = redirection_done;
6113 	param->IsBinaryUpgrade = IsBinaryUpgrade;
6114 	param->max_safe_fds = max_safe_fds;
6115 
6116 	param->MaxBackends = MaxBackends;
6117 
6118 #ifdef WIN32
6119 	param->PostmasterHandle = PostmasterHandle;
6120 	if (!write_duplicated_handle(&param->initial_signal_pipe,
6121 								 pgwin32_create_signal_listener(childPid),
6122 								 childProcess))
6123 		return false;
6124 #else
6125 	memcpy(&param->postmaster_alive_fds, &postmaster_alive_fds,
6126 		   sizeof(postmaster_alive_fds));
6127 #endif
6128 
6129 	memcpy(&param->syslogPipe, &syslogPipe, sizeof(syslogPipe));
6130 
6131 	strlcpy(param->my_exec_path, my_exec_path, MAXPGPATH);
6132 
6133 	strlcpy(param->pkglib_path, pkglib_path, MAXPGPATH);
6134 
6135 	strlcpy(param->ExtraOptions, ExtraOptions, MAXPGPATH);
6136 
6137 	return true;
6138 }
6139 
6140 
6141 #ifdef WIN32
6142 /*
6143  * Duplicate a handle for usage in a child process, and write the child
6144  * process instance of the handle to the parameter file.
6145  */
6146 static bool
6147 write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE childProcess)
6148 {
6149 	HANDLE		hChild = INVALID_HANDLE_VALUE;
6150 
6151 	if (!DuplicateHandle(GetCurrentProcess(),
6152 						 src,
6153 						 childProcess,
6154 						 &hChild,
6155 						 0,
6156 						 TRUE,
6157 						 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
6158 	{
6159 		ereport(LOG,
6160 				(errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %lu",
6161 								 GetLastError())));
6162 		return false;
6163 	}
6164 
6165 	*dest = hChild;
6166 	return true;
6167 }
6168 
6169 /*
6170  * Duplicate a socket for usage in a child process, and write the resulting
6171  * structure to the parameter file.
6172  * This is required because a number of LSPs (Layered Service Providers) very
6173  * common on Windows (antivirus, firewalls, download managers etc) break
6174  * straight socket inheritance.
6175  */
6176 static bool
6177 write_inheritable_socket(InheritableSocket *dest, SOCKET src, pid_t childpid)
6178 {
6179 	dest->origsocket = src;
6180 	if (src != 0 && src != PGINVALID_SOCKET)
6181 	{
6182 		/* Actual socket */
6183 		if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
6184 		{
6185 			ereport(LOG,
6186 					(errmsg("could not duplicate socket %d for use in backend: error code %d",
6187 							(int) src, WSAGetLastError())));
6188 			return false;
6189 		}
6190 	}
6191 	return true;
6192 }
6193 
6194 /*
6195  * Read a duplicate socket structure back, and get the socket descriptor.
6196  */
6197 static void
6198 read_inheritable_socket(SOCKET *dest, InheritableSocket *src)
6199 {
6200 	SOCKET		s;
6201 
6202 	if (src->origsocket == PGINVALID_SOCKET || src->origsocket == 0)
6203 	{
6204 		/* Not a real socket! */
6205 		*dest = src->origsocket;
6206 	}
6207 	else
6208 	{
6209 		/* Actual socket, so create from structure */
6210 		s = WSASocket(FROM_PROTOCOL_INFO,
6211 					  FROM_PROTOCOL_INFO,
6212 					  FROM_PROTOCOL_INFO,
6213 					  &src->wsainfo,
6214 					  0,
6215 					  0);
6216 		if (s == INVALID_SOCKET)
6217 		{
6218 			write_stderr("could not create inherited socket: error code %d\n",
6219 						 WSAGetLastError());
6220 			exit(1);
6221 		}
6222 		*dest = s;
6223 
6224 		/*
6225 		 * To make sure we don't get two references to the same socket, close
6226 		 * the original one. (This would happen when inheritance actually
6227 		 * works..
6228 		 */
6229 		closesocket(src->origsocket);
6230 	}
6231 }
6232 #endif
6233 
6234 static void
6235 read_backend_variables(char *id, Port *port)
6236 {
6237 	BackendParameters param;
6238 
6239 #ifndef WIN32
6240 	/* Non-win32 implementation reads from file */
6241 	FILE	   *fp;
6242 
6243 	/* Open file */
6244 	fp = AllocateFile(id, PG_BINARY_R);
6245 	if (!fp)
6246 	{
6247 		write_stderr("could not open backend variables file \"%s\": %s\n",
6248 					 id, strerror(errno));
6249 		exit(1);
6250 	}
6251 
6252 	if (fread(&param, sizeof(param), 1, fp) != 1)
6253 	{
6254 		write_stderr("could not read from backend variables file \"%s\": %s\n",
6255 					 id, strerror(errno));
6256 		exit(1);
6257 	}
6258 
6259 	/* Release file */
6260 	FreeFile(fp);
6261 	if (unlink(id) != 0)
6262 	{
6263 		write_stderr("could not remove file \"%s\": %s\n",
6264 					 id, strerror(errno));
6265 		exit(1);
6266 	}
6267 #else
6268 	/* Win32 version uses mapped file */
6269 	HANDLE		paramHandle;
6270 	BackendParameters *paramp;
6271 
6272 #ifdef _WIN64
6273 	paramHandle = (HANDLE) _atoi64(id);
6274 #else
6275 	paramHandle = (HANDLE) atol(id);
6276 #endif
6277 	paramp = MapViewOfFile(paramHandle, FILE_MAP_READ, 0, 0, 0);
6278 	if (!paramp)
6279 	{
6280 		write_stderr("could not map view of backend variables: error code %lu\n",
6281 					 GetLastError());
6282 		exit(1);
6283 	}
6284 
6285 	memcpy(&param, paramp, sizeof(BackendParameters));
6286 
6287 	if (!UnmapViewOfFile(paramp))
6288 	{
6289 		write_stderr("could not unmap view of backend variables: error code %lu\n",
6290 					 GetLastError());
6291 		exit(1);
6292 	}
6293 
6294 	if (!CloseHandle(paramHandle))
6295 	{
6296 		write_stderr("could not close handle to backend parameter variables: error code %lu\n",
6297 					 GetLastError());
6298 		exit(1);
6299 	}
6300 #endif
6301 
6302 	restore_backend_variables(&param, port);
6303 }
6304 
6305 /* Restore critical backend variables from the BackendParameters struct */
6306 static void
6307 restore_backend_variables(BackendParameters *param, Port *port)
6308 {
6309 	memcpy(port, &param->port, sizeof(Port));
6310 	read_inheritable_socket(&port->sock, &param->portsocket);
6311 
6312 	SetDataDir(param->DataDir);
6313 
6314 	memcpy(&ListenSocket, &param->ListenSocket, sizeof(ListenSocket));
6315 
6316 	MyCancelKey = param->MyCancelKey;
6317 	MyPMChildSlot = param->MyPMChildSlot;
6318 
6319 #ifdef WIN32
6320 	ShmemProtectiveRegion = param->ShmemProtectiveRegion;
6321 #endif
6322 	UsedShmemSegID = param->UsedShmemSegID;
6323 	UsedShmemSegAddr = param->UsedShmemSegAddr;
6324 
6325 	ShmemLock = param->ShmemLock;
6326 	ShmemVariableCache = param->ShmemVariableCache;
6327 	ShmemBackendArray = param->ShmemBackendArray;
6328 
6329 #ifndef HAVE_SPINLOCKS
6330 	SpinlockSemaArray = param->SpinlockSemaArray;
6331 #endif
6332 	NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests;
6333 	NamedLWLockTrancheArray = param->NamedLWLockTrancheArray;
6334 	MainLWLockArray = param->MainLWLockArray;
6335 	ProcStructLock = param->ProcStructLock;
6336 	ProcGlobal = param->ProcGlobal;
6337 	AuxiliaryProcs = param->AuxiliaryProcs;
6338 	PreparedXactProcs = param->PreparedXactProcs;
6339 	PMSignalState = param->PMSignalState;
6340 	read_inheritable_socket(&pgStatSock, &param->pgStatSock);
6341 
6342 	PostmasterPid = param->PostmasterPid;
6343 	PgStartTime = param->PgStartTime;
6344 	PgReloadTime = param->PgReloadTime;
6345 	first_syslogger_file_time = param->first_syslogger_file_time;
6346 
6347 	redirection_done = param->redirection_done;
6348 	IsBinaryUpgrade = param->IsBinaryUpgrade;
6349 	max_safe_fds = param->max_safe_fds;
6350 
6351 	MaxBackends = param->MaxBackends;
6352 
6353 #ifdef WIN32
6354 	PostmasterHandle = param->PostmasterHandle;
6355 	pgwin32_initial_signal_pipe = param->initial_signal_pipe;
6356 #else
6357 	memcpy(&postmaster_alive_fds, &param->postmaster_alive_fds,
6358 		   sizeof(postmaster_alive_fds));
6359 #endif
6360 
6361 	memcpy(&syslogPipe, &param->syslogPipe, sizeof(syslogPipe));
6362 
6363 	strlcpy(my_exec_path, param->my_exec_path, MAXPGPATH);
6364 
6365 	strlcpy(pkglib_path, param->pkglib_path, MAXPGPATH);
6366 
6367 	strlcpy(ExtraOptions, param->ExtraOptions, MAXPGPATH);
6368 }
6369 
6370 
6371 Size
6372 ShmemBackendArraySize(void)
6373 {
6374 	return mul_size(MaxLivePostmasterChildren(), sizeof(Backend));
6375 }
6376 
6377 void
6378 ShmemBackendArrayAllocation(void)
6379 {
6380 	Size		size = ShmemBackendArraySize();
6381 
6382 	ShmemBackendArray = (Backend *) ShmemAlloc(size);
6383 	/* Mark all slots as empty */
6384 	memset(ShmemBackendArray, 0, size);
6385 }
6386 
6387 static void
6388 ShmemBackendArrayAdd(Backend *bn)
6389 {
6390 	/* The array slot corresponding to my PMChildSlot should be free */
6391 	int			i = bn->child_slot - 1;
6392 
6393 	Assert(ShmemBackendArray[i].pid == 0);
6394 	ShmemBackendArray[i] = *bn;
6395 }
6396 
6397 static void
6398 ShmemBackendArrayRemove(Backend *bn)
6399 {
6400 	int			i = bn->child_slot - 1;
6401 
6402 	Assert(ShmemBackendArray[i].pid == bn->pid);
6403 	/* Mark the slot as empty */
6404 	ShmemBackendArray[i].pid = 0;
6405 }
6406 #endif   /* EXEC_BACKEND */
6407 
6408 
6409 #ifdef WIN32
6410 
6411 /*
6412  * Subset implementation of waitpid() for Windows.  We assume pid is -1
6413  * (that is, check all child processes) and options is WNOHANG (don't wait).
6414  */
6415 static pid_t
6416 waitpid(pid_t pid, int *exitstatus, int options)
6417 {
6418 	DWORD		dwd;
6419 	ULONG_PTR	key;
6420 	OVERLAPPED *ovl;
6421 
6422 	/*
6423 	 * Check if there are any dead children. If there are, return the pid of
6424 	 * the first one that died.
6425 	 */
6426 	if (GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
6427 	{
6428 		*exitstatus = (int) key;
6429 		return dwd;
6430 	}
6431 
6432 	return -1;
6433 }
6434 
6435 /*
6436  * Note! Code below executes on a thread pool! All operations must
6437  * be thread safe! Note that elog() and friends must *not* be used.
6438  */
6439 static void WINAPI
6440 pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
6441 {
6442 	win32_deadchild_waitinfo *childinfo = (win32_deadchild_waitinfo *) lpParameter;
6443 	DWORD		exitcode;
6444 
6445 	if (TimerOrWaitFired)
6446 		return;					/* timeout. Should never happen, since we use
6447 								 * INFINITE as timeout value. */
6448 
6449 	/*
6450 	 * Remove handle from wait - required even though it's set to wait only
6451 	 * once
6452 	 */
6453 	UnregisterWaitEx(childinfo->waitHandle, NULL);
6454 
6455 	if (!GetExitCodeProcess(childinfo->procHandle, &exitcode))
6456 	{
6457 		/*
6458 		 * Should never happen. Inform user and set a fixed exitcode.
6459 		 */
6460 		write_stderr("could not read exit code for process\n");
6461 		exitcode = 255;
6462 	}
6463 
6464 	if (!PostQueuedCompletionStatus(win32ChildQueue, childinfo->procId, (ULONG_PTR) exitcode, NULL))
6465 		write_stderr("could not post child completion status\n");
6466 
6467 	/*
6468 	 * Handle is per-process, so we close it here instead of in the
6469 	 * originating thread
6470 	 */
6471 	CloseHandle(childinfo->procHandle);
6472 
6473 	/*
6474 	 * Free struct that was allocated before the call to
6475 	 * RegisterWaitForSingleObject()
6476 	 */
6477 	free(childinfo);
6478 
6479 	/* Queue SIGCHLD signal */
6480 	pg_queue_signal(SIGCHLD);
6481 }
6482 #endif   /* WIN32 */
6483 
6484 /*
6485  * Initialize one and only handle for monitoring postmaster death.
6486  *
6487  * Called once in the postmaster, so that child processes can subsequently
6488  * monitor if their parent is dead.
6489  */
6490 static void
6491 InitPostmasterDeathWatchHandle(void)
6492 {
6493 #ifndef WIN32
6494 
6495 	/*
6496 	 * Create a pipe. Postmaster holds the write end of the pipe open
6497 	 * (POSTMASTER_FD_OWN), and children hold the read end. Children can pass
6498 	 * the read file descriptor to select() to wake up in case postmaster
6499 	 * dies, or check for postmaster death with a (read() == 0). Children must
6500 	 * close the write end as soon as possible after forking, because EOF
6501 	 * won't be signaled in the read end until all processes have closed the
6502 	 * write fd. That is taken care of in ClosePostmasterPorts().
6503 	 */
6504 	Assert(MyProcPid == PostmasterPid);
6505 	if (pipe(postmaster_alive_fds) < 0)
6506 		ereport(FATAL,
6507 				(errcode_for_file_access(),
6508 				 errmsg_internal("could not create pipe to monitor postmaster death: %m")));
6509 
6510 	/*
6511 	 * Set O_NONBLOCK to allow testing for the fd's presence with a read()
6512 	 * call.
6513 	 */
6514 	if (fcntl(postmaster_alive_fds[POSTMASTER_FD_WATCH], F_SETFL, O_NONBLOCK) == -1)
6515 		ereport(FATAL,
6516 				(errcode_for_socket_access(),
6517 				 errmsg_internal("could not set postmaster death monitoring pipe to nonblocking mode: %m")));
6518 #else
6519 
6520 	/*
6521 	 * On Windows, we use a process handle for the same purpose.
6522 	 */
6523 	if (DuplicateHandle(GetCurrentProcess(),
6524 						GetCurrentProcess(),
6525 						GetCurrentProcess(),
6526 						&PostmasterHandle,
6527 						0,
6528 						TRUE,
6529 						DUPLICATE_SAME_ACCESS) == 0)
6530 		ereport(FATAL,
6531 				(errmsg_internal("could not duplicate postmaster handle: error code %lu",
6532 								 GetLastError())));
6533 #endif   /* WIN32 */
6534 }
6535