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