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