PageRenderTime 64ms CodeModel.GetById 18ms 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

Large files files are truncated, but you can click here to view the full file

  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;

Large files files are truncated, but you can click here to view the full file