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