PageRenderTime 82ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/backend/postmaster/postmaster.c

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