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