OSDN Git Service

Another pgindent run. Fixes enum indenting, and improves #endif
[pg-rex/syncrep.git] / 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, shutdown, and periodic checkpoints.  The postmaster
11  *        itself doesn't do those operations, mind you --- it just forks
12  *        off a subprocess to do them at the right times.  It also takes
13  *        care of resetting the system 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 PROC 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-2001, PostgreSQL Global Development Group
36  * Portions Copyright (c) 1994, Regents of the University of California
37  *
38  *
39  * IDENTIFICATION
40  *        $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.253 2001/10/28 06:25:47 momjian Exp $
41  *
42  * NOTES
43  *
44  * Initialization:
45  *              The Postmaster sets up a few shared memory data structures
46  *              for the backends.  It should at the very least initialize the
47  *              lock manager.
48  *
49  * Synchronization:
50  *              The Postmaster shares memory with the backends but should avoid
51  *              touching shared memory, so as not to become stuck if a crashing
52  *              backend screws up locks or shared memory.  Likewise, the Postmaster
53  *              should never block on messages from frontend clients.
54  *
55  * Garbage Collection:
56  *              The Postmaster cleans up after backends if they have an emergency
57  *              exit and/or core dump.
58  *
59  *-------------------------------------------------------------------------
60  */
61
62 #include "postgres.h"
63
64 #include <unistd.h>
65 #include <signal.h>
66 #include <sys/wait.h>
67 #include <ctype.h>
68 #include <sys/types.h>
69 #include <sys/stat.h>
70 #include <sys/time.h>
71 #include <sys/socket.h>
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <time.h>
75 #include <sys/param.h>
76 #include <netinet/in.h>
77 #include <arpa/inet.h>
78 #include <netdb.h>
79 #include <limits.h>
80
81 #ifdef HAVE_SYS_SELECT_H
82 #include <sys/select.h>
83 #endif
84
85 #ifdef HAVE_GETOPT_H
86 #include <getopt.h>
87 #endif
88
89 #include "catalog/pg_database.h"
90 #include "commands/async.h"
91 #include "lib/dllist.h"
92 #include "libpq/auth.h"
93 #include "libpq/crypt.h"
94 #include "libpq/libpq.h"
95 #include "libpq/pqcomm.h"
96 #include "libpq/pqsignal.h"
97 #include "miscadmin.h"
98 #include "nodes/nodes.h"
99 #include "storage/fd.h"
100 #include "storage/ipc.h"
101 #include "storage/proc.h"
102 #include "access/xlog.h"
103 #include "tcop/tcopprot.h"
104 #include "utils/exc.h"
105 #include "utils/guc.h"
106 #include "utils/memutils.h"
107 #include "utils/ps_status.h"
108 #include "bootstrap/bootstrap.h"
109
110 #include "pgstat.h"
111
112 #define INVALID_SOCK    (-1)
113 #define ARGV_SIZE       64
114
115 #ifdef HAVE_SIGPROCMASK
116 sigset_t        UnBlockSig,
117                         BlockSig,
118                         AuthBlockSig;
119
120 #else
121 int                     UnBlockSig,
122                         BlockSig,
123                         AuthBlockSig;
124 #endif
125
126 /*
127  * List of active backends (or child processes anyway; we don't actually
128  * know whether a given child has become a backend or is still in the
129  * authorization phase).  This is used mainly to keep track of how many
130  * children we have and send them appropriate signals when necessary.
131  */
132 typedef struct bkend
133 {
134         pid_t           pid;                    /* process id of backend */
135         long            cancel_key;             /* cancel key for cancels for this backend */
136 } Backend;
137
138 static Dllist *BackendList;
139
140 /* The socket number we are listening for connections on */
141 int                     PostPortNumber;
142 char       *UnixSocketDir;
143 char       *VirtualHost;
144
145 /*
146  * MaxBackends is the limit on the number of backends we can start.
147  * The default is established by configure, but it can be altered at
148  * postmaster start with the postmaster's -N switch.  Note
149  * that a larger MaxBackends value will increase the size of the shared
150  * memory area as well as cause the postmaster to grab more kernel
151  * semaphores, even if you never actually use that many backends.
152  */
153 int                     MaxBackends = DEF_MAXBACKENDS;
154
155
156 static char *progname = (char *) NULL;
157
158 /* flag to indicate that SIGHUP arrived during server loop */
159 static volatile bool got_SIGHUP = false;
160
161 /*
162  * Default Values
163  */
164 static int      ServerSock_INET = INVALID_SOCK;         /* stream socket server */
165
166 #ifdef HAVE_UNIX_SOCKETS
167 static int      ServerSock_UNIX = INVALID_SOCK;         /* stream socket server */
168 #endif
169
170 #ifdef USE_SSL
171 static SSL_CTX *SSL_context = NULL;             /* Global SSL context */
172 #endif
173
174 /*
175  * Set by the -o option
176  */
177 static char ExtraOptions[MAXPGPATH];
178
179 /*
180  * These globals control the behavior of the postmaster in case some
181  * backend dumps core.  Normally, it kills all peers of the dead backend
182  * and reinitializes shared memory.  By specifying -s or -n, we can have
183  * the postmaster stop (rather than kill) peers and not reinitialize
184  * shared data structures.
185  */
186 static bool Reinit = true;
187 static int      SendStop = false;
188
189 /* still more option variables */
190 bool            NetServer = false;      /* listen on TCP/IP */
191 bool            EnableSSL = false;
192 bool            SilentMode = false; /* silent mode (-S) */
193
194 int                     PreAuthDelay = 0;
195 int                     AuthenticationTimeout = 60;
196 int                     CheckPointTimeout = 300;
197
198 bool            HostnameLookup;         /* for ps display */
199 bool            ShowPortNumber;
200 bool            Log_connections = false;
201
202 /* Startup/shutdown state */
203 static pid_t StartupPID = 0,
204                         ShutdownPID = 0,
205                         CheckPointPID = 0;
206 static time_t checkpointed = 0;
207
208 #define                 NoShutdown              0
209 #define                 SmartShutdown   1
210 #define                 FastShutdown    2
211
212 static int      Shutdown = NoShutdown;
213
214 static bool FatalError = false; /* T if recovering from backend crash */
215
216 /*
217  * State for assigning random salts and cancel keys.
218  * Also, the global MyCancelKey passes the cancel key assigned to a given
219  * backend from the postmaster to that backend (via fork).
220  */
221
222 static unsigned int random_seed = 0;
223
224 extern char *optarg;
225 extern int      optind,
226                         opterr;
227
228 #ifdef HAVE_INT_OPTRESET
229 extern int      optreset;
230 #endif
231
232 /*
233  * postmaster.c - function prototypes
234  */
235 static void pmdaemonize(int argc, char *argv[]);
236 static Port *ConnCreate(int serverFd);
237 static void ConnFree(Port *port);
238 static void reset_shared(unsigned short port);
239 static void SIGHUP_handler(SIGNAL_ARGS);
240 static void pmdie(SIGNAL_ARGS);
241 static void reaper(SIGNAL_ARGS);
242 static void schedule_checkpoint(SIGNAL_ARGS);
243 static void CleanupProc(int pid, int exitstatus);
244 static int      DoBackend(Port *port);
245 static void ExitPostmaster(int status);
246 static void usage(const char *);
247 static int      ServerLoop(void);
248 static int      BackendStartup(Port *port);
249 static int      ProcessStartupPacket(Port *port, bool SSLdone);
250 static void processCancelRequest(Port *port, void *pkt);
251 static int      initMasks(fd_set *rmask, fd_set *wmask);
252 enum CAC_state
253 {
254         CAC_OK, CAC_STARTUP, CAC_SHUTDOWN, CAC_RECOVERY, CAC_TOOMANY
255 };
256 static enum CAC_state canAcceptConnections(void);
257 static long PostmasterRandom(void);
258 static void RandomSalt(char *cryptSalt, char *md5Salt);
259 static void SignalChildren(int signal);
260 static int      CountChildren(void);
261 static bool CreateOptsFile(int argc, char *argv[]);
262 static pid_t SSDataBase(int xlop);
263 static void
264 postmaster_error(const char *fmt,...)
265 /* This lets gcc check the format string for consistency. */
266 __attribute__((format(printf, 1, 2)));
267
268 #define StartupDataBase()               SSDataBase(BS_XLOG_STARTUP)
269 #define CheckPointDataBase()    SSDataBase(BS_XLOG_CHECKPOINT)
270 #define ShutdownDataBase()              SSDataBase(BS_XLOG_SHUTDOWN)
271
272 #ifdef USE_SSL
273 static void InitSSL(void);
274 #endif
275
276
277 static void
278 checkDataDir(const char *checkdir)
279 {
280         char            path[MAXPGPATH];
281         FILE       *fp;
282         struct stat stat_buf;
283
284         if (checkdir == NULL)
285         {
286                 fprintf(stderr, gettext(
287                          "%s does not know where to find the database system data.\n"
288                                                                 "You must specify the directory that contains the database system\n"
289                                                                 "either by specifying the -D invocation option or by setting the\n"
290                                                                 "PGDATA environment variable.\n\n"),
291                                 progname);
292                 ExitPostmaster(2);
293         }
294
295         /*
296          * Check if the directory has group or world access.  If so, reject.
297          */
298         if (stat(checkdir, &stat_buf) == -1)
299         {
300                 if (errno == ENOENT)
301                         elog(FATAL, "data directory %s was not found", checkdir);
302                 else
303                         elog(FATAL, "could not read permissions of directory %s: %m",
304                                  checkdir);
305         }
306
307         if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
308                 elog(FATAL, "data directory %s has group or world access; permissions should be u=rwx (0700)",
309                          checkdir);
310
311         /* Look for PG_VERSION before looking for pg_control */
312         ValidatePgVersion(checkdir);
313
314         snprintf(path, sizeof(path), "%s/global/pg_control", checkdir);
315
316         fp = AllocateFile(path, PG_BINARY_R);
317         if (fp == NULL)
318         {
319                 fprintf(stderr, gettext(
320                                                                 "%s does not find the database system.\n"
321                                   "Expected to find it in the PGDATA directory \"%s\",\n"
322                                                                 "but unable to open file \"%s\": %s\n\n"),
323                                 progname, checkdir, path, strerror(errno));
324                 ExitPostmaster(2);
325         }
326         FreeFile(fp);
327 }
328
329
330 int
331 PostmasterMain(int argc, char *argv[])
332 {
333         int                     opt;
334         int                     status;
335         char            original_extraoptions[MAXPGPATH];
336         char       *potential_DataDir = NULL;
337
338         *original_extraoptions = '\0';
339
340         progname = argv[0];
341
342         /*
343          * Catch standard options before doing much else.  This even works on
344          * systems without getopt_long.
345          */
346         if (argc > 1)
347         {
348                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
349                 {
350                         usage(progname);
351                         ExitPostmaster(0);
352                 }
353                 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
354                 {
355                         puts("postmaster (PostgreSQL) " PG_VERSION);
356                         ExitPostmaster(0);
357                 }
358         }
359
360         /*
361          * for security, no dir or file created can be group or other
362          * accessible
363          */
364         umask((mode_t) 0077);
365
366         MyProcPid = getpid();
367
368         /*
369          * Fire up essential subsystems: error and memory management
370          */
371         EnableExceptionHandling(true);
372         MemoryContextInit();
373
374         /*
375          * By default, palloc() requests in the postmaster will be allocated
376          * in the PostmasterContext, which is space that can be recycled by
377          * backends.  Allocated data that needs to be available to backends
378          * should be allocated in TopMemoryContext.
379          */
380         PostmasterContext = AllocSetContextCreate(TopMemoryContext,
381                                                                                           "Postmaster",
382                                                                                           ALLOCSET_DEFAULT_MINSIZE,
383                                                                                           ALLOCSET_DEFAULT_INITSIZE,
384                                                                                           ALLOCSET_DEFAULT_MAXSIZE);
385         MemoryContextSwitchTo(PostmasterContext);
386
387         /*
388          * Options setup
389          */
390         ResetAllOptions(true);
391
392         /* PGPORT environment variable, if set, overrides GUC setting */
393         if (getenv("PGPORT"))
394                 SetConfigOption("port", getenv("PGPORT"), PGC_POSTMASTER, true);
395
396         potential_DataDir = getenv("PGDATA");           /* default value */
397
398         /*
399          * First we must scan for a -D argument to get the data dir. Then read
400          * the config file. Finally, scan all the other arguments. (Command
401          * line switches override config file.)
402          *
403          * Note: The two lists of options must be exactly the same, even though
404          * perhaps the first one would only have to be "D:" with opterr turned
405          * off. But some versions of getopt (notably GNU) are going to
406          * arbitrarily permute some "non-options" (according to the local
407          * world view) which will result in some switches being associated
408          * with the wrong argument. Death and destruction will occur.
409          */
410         opterr = 1;
411         while ((opt = getopt(argc, argv, "A:a:B:b:c:D:d:Fh:ik:lm:MN:no:p:Ss-:")) != EOF)
412         {
413                 switch (opt)
414                 {
415                         case 'D':
416                                 potential_DataDir = optarg;
417                                 break;
418
419                         case '?':
420                                 fprintf(stderr, gettext("Try '%s --help' for more information.\n"), progname);
421                                 ExitPostmaster(1);
422                 }
423         }
424
425         /*
426          * Postmaster accepts no non-option switch arguments.
427          */
428         if (optind < argc)
429         {
430                 postmaster_error("invalid argument -- %s", argv[optind]);
431                 fprintf(stderr, gettext("Try '%s --help' for more information.\n"),
432                                 progname);
433                 ExitPostmaster(1);
434         }
435
436         checkDataDir(potential_DataDir);        /* issues error messages */
437         SetDataDir(potential_DataDir);
438
439         ProcessConfigFile(PGC_POSTMASTER);
440
441         IgnoreSystemIndexes(false);
442
443         /* reset getopt(3) to rescan arguments */
444         optind = 1;
445 #ifdef HAVE_INT_OPTRESET
446         optreset = 1;                           /* some systems need this too */
447 #endif
448
449         while ((opt = getopt(argc, argv, "A:a:B:b:c:D:d:Fh:ik:lm:MN:no:p:Ss-:")) != EOF)
450         {
451                 switch (opt)
452                 {
453                         case 'A':
454 #ifdef USE_ASSERT_CHECKING
455                                 SetConfigOption("debug_assertions", optarg, PGC_POSTMASTER, true);
456 #else
457                                 postmaster_error("Assert checking is not compiled in.");
458 #endif
459                                 break;
460                         case 'a':
461                                 /* Can no longer set authentication method. */
462                                 break;
463                         case 'B':
464                                 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, true);
465                                 break;
466                         case 'b':
467                                 /* Can no longer set the backend executable file to use. */
468                                 break;
469                         case 'D':
470                                 /* already done above */
471                                 break;
472                         case 'd':
473
474                                 /*
475                                  * Turn on debugging for the postmaster and the backend
476                                  * servers descended from it.
477                                  */
478                                 SetConfigOption("debug_level", optarg, PGC_POSTMASTER, true);
479                                 break;
480                         case 'F':
481                                 SetConfigOption("fsync", "false", PGC_POSTMASTER, true);
482                                 break;
483                         case 'h':
484                                 SetConfigOption("virtual_host", optarg, PGC_POSTMASTER, true);
485                                 break;
486                         case 'i':
487                                 SetConfigOption("tcpip_socket", "true", PGC_POSTMASTER, true);
488                                 break;
489                         case 'k':
490                                 SetConfigOption("unix_socket_directory", optarg, PGC_POSTMASTER, true);
491                                 break;
492 #ifdef USE_SSL
493                         case 'l':
494                                 SetConfigOption("ssl", "true", PGC_POSTMASTER, true);
495                                 break;
496 #endif
497                         case 'm':
498                                 /* Multiplexed backends no longer supported. */
499                                 break;
500                         case 'M':
501
502                                 /*
503                                  * ignore this flag.  This may be passed in because the
504                                  * program was run as 'postgres -M' instead of
505                                  * 'postmaster'
506                                  */
507                                 break;
508                         case 'N':
509                                 /* The max number of backends to start. */
510                                 SetConfigOption("max_connections", optarg, PGC_POSTMASTER, true);
511                                 break;
512                         case 'n':
513                                 /* Don't reinit shared mem after abnormal exit */
514                                 Reinit = false;
515                                 break;
516                         case 'o':
517
518                                 /*
519                                  * Other options to pass to the backend on the command
520                                  * line -- useful only for debugging.
521                                  */
522                                 strcat(ExtraOptions, " ");
523                                 strcat(ExtraOptions, optarg);
524                                 strcpy(original_extraoptions, optarg);
525                                 break;
526                         case 'p':
527                                 SetConfigOption("port", optarg, PGC_POSTMASTER, true);
528                                 break;
529                         case 'S':
530
531                                 /*
532                                  * Start in 'S'ilent mode (disassociate from controlling
533                                  * tty). You may also think of this as 'S'ysV mode since
534                                  * it's most badly needed on SysV-derived systems like
535                                  * SVR4 and HP-UX.
536                                  */
537                                 SetConfigOption("silent_mode", "true", PGC_POSTMASTER, true);
538                                 break;
539                         case 's':
540
541                                 /*
542                                  * In the event that some backend dumps core, send
543                                  * SIGSTOP, rather than SIGQUIT, to all its peers.      This
544                                  * lets the wily post_hacker collect core dumps from
545                                  * everyone.
546                                  */
547                                 SendStop = true;
548                                 break;
549                         case 'c':
550                         case '-':
551                                 {
552                                         char       *name,
553                                                            *value;
554
555                                         ParseLongOption(optarg, &name, &value);
556                                         if (!value)
557                                         {
558                                                 if (opt == '-')
559                                                         elog(ERROR, "--%s requires argument", optarg);
560                                                 else
561                                                         elog(ERROR, "-c %s requires argument", optarg);
562                                         }
563
564                                         SetConfigOption(name, value, PGC_POSTMASTER, true);
565                                         free(name);
566                                         if (value)
567                                                 free(value);
568                                         break;
569                                 }
570
571                         default:
572                                 /* shouldn't get here */
573                                 fprintf(stderr, gettext("Try '%s --help' for more information.\n"), progname);
574                                 ExitPostmaster(1);
575                 }
576         }
577
578         /*
579          * Check for invalid combinations of switches
580          */
581         if (NBuffers < 2 * MaxBackends || NBuffers < 16)
582         {
583                 /*
584                  * Do not accept -B so small that backends are likely to starve
585                  * for lack of buffers.  The specific choices here are somewhat
586                  * arbitrary.
587                  */
588                 postmaster_error("The number of buffers (-B) must be at least twice the number of allowed connections (-N) and at least 16.");
589                 ExitPostmaster(1);
590         }
591
592         /*
593          * Now that we are done processing the postmaster arguments, reset
594          * getopt(3) library so that it will work correctly in subprocesses.
595          */
596         optind = 1;
597 #ifdef HAVE_INT_OPTRESET
598         optreset = 1;                           /* some systems need this too */
599 #endif
600
601         /* For debugging: display postmaster environment */
602         if (DebugLvl > 2)
603         {
604                 extern char **environ;
605                 char      **p;
606
607                 fprintf(stderr, "%s: PostmasterMain: initial environ dump:\n",
608                                 progname);
609                 fprintf(stderr, "-----------------------------------------\n");
610                 for (p = environ; *p; ++p)
611                         fprintf(stderr, "\t%s\n", *p);
612                 fprintf(stderr, "-----------------------------------------\n");
613         }
614
615         /*
616          * On some systems our dynloader code needs the executable's pathname.
617          */
618         if (FindExec(pg_pathname, progname, "postgres") < 0)
619                 elog(FATAL, "%s: could not locate executable, bailing out...",
620                          progname);
621
622         /*
623          * Initialize SSL library, if specified.
624          */
625 #ifdef USE_SSL
626         if (EnableSSL && !NetServer)
627         {
628                 postmaster_error("For SSL, TCP/IP connections must be enabled.");
629                 fprintf(stderr, gettext("Try '%s --help' for more information.\n"), progname);
630                 ExitPostmaster(1);
631         }
632         if (EnableSSL)
633                 InitSSL();
634 #endif
635
636         /*
637          * Fork away from controlling terminal, if -S specified.
638          *
639          * Must do this before we grab any interlock files, else the interlocks
640          * will show the wrong PID.
641          */
642         if (SilentMode)
643                 pmdaemonize(argc, argv);
644
645         /*
646          * Create lockfile for data directory.
647          *
648          * We want to do this before we try to grab the input sockets, because
649          * the data directory interlock is more reliable than the socket-file
650          * interlock (thanks to whoever decided to put socket files in /tmp
651          * :-(). For the same reason, it's best to grab the TCP socket before
652          * the Unix socket.
653          */
654         if (!CreateDataDirLockFile(DataDir, true))
655                 ExitPostmaster(1);
656
657         /*
658          * Remove old temporary files.  At this point there can be no other
659          * Postgres processes running in this directory, so this should be
660          * safe.
661          */
662         RemovePgTempFiles();
663
664         /*
665          * Establish input sockets.
666          */
667         if (NetServer)
668         {
669                 status = StreamServerPort(AF_INET, VirtualHost,
670                                                                   (unsigned short) PostPortNumber,
671                                                                   UnixSocketDir,
672                                                                   &ServerSock_INET);
673                 if (status != STATUS_OK)
674                 {
675                         postmaster_error("cannot create INET stream port");
676                         ExitPostmaster(1);
677                 }
678         }
679
680 #ifdef HAVE_UNIX_SOCKETS
681         status = StreamServerPort(AF_UNIX, VirtualHost,
682                                                           (unsigned short) PostPortNumber,
683                                                           UnixSocketDir,
684                                                           &ServerSock_UNIX);
685         if (status != STATUS_OK)
686         {
687                 postmaster_error("cannot create UNIX stream port");
688                 ExitPostmaster(1);
689         }
690 #endif
691
692         XLOGPathInit();
693
694         /*
695          * Set up shared memory and semaphores.
696          */
697         reset_shared(PostPortNumber);
698
699         /*
700          * Initialize the list of active backends.
701          */
702         BackendList = DLNewList();
703
704         /*
705          * Record postmaster options.  We delay this till now to avoid
706          * recording bogus options (eg, NBuffers too high for available
707          * memory).
708          */
709         if (!CreateOptsFile(argc, argv))
710                 ExitPostmaster(1);
711
712         /*
713          * Set up signal handlers for the postmaster process.
714          */
715         pqinitmask();
716         PG_SETMASK(&BlockSig);
717
718         pqsignal(SIGHUP, SIGHUP_handler);       /* reread config file and have
719                                                                                  * children do same */
720         pqsignal(SIGINT, pmdie);        /* send SIGTERM and ShutdownDataBase */
721         pqsignal(SIGQUIT, pmdie);       /* send SIGQUIT and die */
722         pqsignal(SIGTERM, pmdie);       /* wait for children and ShutdownDataBase */
723         pqsignal(SIGALRM, SIG_IGN); /* ignored */
724         pqsignal(SIGPIPE, SIG_IGN); /* ignored */
725         pqsignal(SIGUSR1, schedule_checkpoint);         /* start a background
726                                                                                                  * checkpoint */
727         pqsignal(SIGUSR2, pmdie);       /* send SIGUSR2, don't die */
728         pqsignal(SIGCHLD, reaper);      /* handle child termination */
729         pqsignal(SIGTTIN, SIG_IGN); /* ignored */
730         pqsignal(SIGTTOU, SIG_IGN); /* ignored */
731
732         /*
733          * Reset whereToSendOutput from Debug (its starting state) to None.
734          * This prevents elog from sending messages to stderr unless the
735          * syslog/stderr switch permits.  We don't do this until the
736          * postmaster is fully launched, since startup failures may as well be
737          * reported to stderr.
738          */
739         whereToSendOutput = None;
740
741         /*
742          * Initialize and startup the statistics collector process
743          */
744         if (pgstat_init() < 0)
745                 ExitPostmaster(1);
746         if (pgstat_start() < 0)
747                 ExitPostmaster(1);
748
749         /*
750          * We're ready to rock and roll...
751          */
752         StartupPID = StartupDataBase();
753
754         status = ServerLoop();
755
756         /*
757          * ServerLoop probably shouldn't ever return, but if it does, close
758          * down.
759          */
760         ExitPostmaster(status != STATUS_OK);
761
762         return 0;                                       /* not reached */
763 }
764
765 static void
766 pmdaemonize(int argc, char *argv[])
767 {
768         int                     i;
769         pid_t           pid;
770
771         pid = fork();
772         if (pid == (pid_t) -1)
773         {
774                 postmaster_error("fork failed: %s", strerror(errno));
775                 ExitPostmaster(1);
776                 return;                                 /* not reached */
777         }
778         else if (pid)
779         {                                                       /* parent */
780                 /* Parent should just exit, without doing any atexit cleanup */
781                 _exit(0);
782         }
783
784         MyProcPid = getpid();           /* reset MyProcPid to child */
785
786 /* GH: If there's no setsid(), we hopefully don't need silent mode.
787  * Until there's a better solution.
788  */
789 #ifdef HAVE_SETSID
790         if (setsid() < 0)
791         {
792                 postmaster_error("cannot disassociate from controlling TTY: %s",
793                                                  strerror(errno));
794                 ExitPostmaster(1);
795         }
796 #endif
797         i = open(NULL_DEV, O_RDWR | PG_BINARY);
798         dup2(i, 0);
799         dup2(i, 1);
800         dup2(i, 2);
801         close(i);
802 }
803
804
805
806 /*
807  * Print out help message
808  */
809 static void
810 usage(const char *progname)
811 {
812         printf(gettext("%s is the PostgreSQL server.\n\n"), progname);
813         printf(gettext("Usage:\n  %s [options...]\n\n"), progname);
814         printf(gettext("Options:\n"));
815 #ifdef USE_ASSERT_CHECKING
816         printf(gettext("  -A 1|0          enable/disable run-time assert checking\n"));
817 #endif
818         printf(gettext("  -B NBUFFERS     number of shared buffers (default %d)\n"), DEF_NBUFFERS);
819         printf(gettext("  -c NAME=VALUE   set run-time parameter\n"));
820         printf(gettext("  -d 1-5          debugging level\n"));
821         printf(gettext("  -D DATADIR      database directory\n"));
822         printf(gettext("  -F              turn fsync off\n"));
823         printf(gettext("  -h HOSTNAME     host name or IP address to listen on\n"));
824         printf(gettext("  -i              enable TCP/IP connections\n"));
825         printf(gettext("  -k DIRECTORY    Unix-domain socket location\n"));
826 #ifdef USE_SSL
827         printf(gettext("  -l              enable SSL connections\n"));
828 #endif
829         printf(gettext("  -N MAX-CONNECT  maximum number of allowed connections (default %d)\n"),
830                    DEF_MAXBACKENDS);
831         printf(gettext("  -o OPTIONS      pass 'OPTIONS' to each backend server\n"));
832         printf(gettext("  -p PORT         port number to listen on (default %d)\n"), DEF_PGPORT);
833         printf(gettext("  -S              silent mode (start in background without logging output)\n"));
834
835         printf(gettext("\nDeveloper options:\n"));
836         printf(gettext("  -n              do not reinitialize shared memory after abnormal exit\n"));
837         printf(gettext("  -s              send SIGSTOP to all backend servers if one dies\n"));
838
839         printf(gettext("\nPlease read the documentation for the complete list of run-time\n"
840                                    "configuration settings and how to set them on the command line or in\n"
841                                    "the configuration file.\n\n"
842                                    "Report bugs to <pgsql-bugs@postgresql.org>.\n"));
843 }
844
845 static int
846 ServerLoop(void)
847 {
848         fd_set          readmask,
849                                 writemask;
850         int                     nSockets;
851         struct timeval now,
852                                 later;
853         struct timezone tz;
854
855         load_hba_and_ident();
856
857         gettimeofday(&now, &tz);
858
859         nSockets = initMasks(&readmask, &writemask);
860
861         for (;;)
862         {
863                 Port       *port;
864                 fd_set          rmask,
865                                         wmask;
866                 struct timeval *timeout = NULL;
867                 struct timeval timeout_tv;
868
869                 if (CheckPointPID == 0 && checkpointed &&
870                         Shutdown == NoShutdown && !FatalError && random_seed != 0)
871                 {
872                         time_t          now = time(NULL);
873
874                         if (CheckPointTimeout + checkpointed > now)
875                         {
876                                 /*
877                                  * Not time for checkpoint yet, so set a timeout for
878                                  * select
879                                  */
880                                 timeout_tv.tv_sec = CheckPointTimeout + checkpointed - now;
881                                 timeout_tv.tv_usec = 0;
882                                 timeout = &timeout_tv;
883                         }
884                         else
885                         {
886                                 /* Time to make the checkpoint... */
887                                 CheckPointPID = CheckPointDataBase();
888
889                                 /*
890                                  * if fork failed, schedule another try at 0.1 normal
891                                  * delay
892                                  */
893                                 if (CheckPointPID == 0)
894                                         checkpointed = now - (9 * CheckPointTimeout) / 10;
895                         }
896                 }
897
898                 /*
899                  * Wait for something to happen.
900                  */
901                 memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
902                 memcpy((char *) &wmask, (char *) &writemask, sizeof(fd_set));
903
904                 PG_SETMASK(&UnBlockSig);
905
906                 if (select(nSockets, &rmask, &wmask, (fd_set *) NULL, timeout) < 0)
907                 {
908                         PG_SETMASK(&BlockSig);
909                         if (errno == EINTR || errno == EWOULDBLOCK)
910                                 continue;
911                         elog(DEBUG, "ServerLoop: select failed: %s", strerror(errno));
912                         return STATUS_ERROR;
913                 }
914
915                 /*
916                  * Block all signals until we wait again
917                  */
918                 PG_SETMASK(&BlockSig);
919
920                 /*
921                  * Respond to signals, if needed
922                  */
923                 if (got_SIGHUP)
924                 {
925                         got_SIGHUP = false;
926                         ProcessConfigFile(PGC_SIGHUP);
927                         load_hba_and_ident();
928                 }
929
930                 /*
931                  * Select a random seed at the time of first receiving a request.
932                  */
933                 while (random_seed == 0)
934                 {
935                         gettimeofday(&later, &tz);
936
937                         /*
938                          * We are not sure how much precision is in tv_usec, so we
939                          * swap the nibbles of 'later' and XOR them with 'now'. On the
940                          * off chance that the result is 0, we loop until it isn't.
941                          */
942                         random_seed = now.tv_usec ^
943                                 ((later.tv_usec << 16) |
944                                  ((later.tv_usec >> 16) & 0xffff));
945                 }
946
947                 /*
948                  * New connection pending on our well-known port's socket? If so,
949                  * fork a child process to deal with it.
950                  */
951
952 #ifdef HAVE_UNIX_SOCKETS
953                 if (ServerSock_UNIX != INVALID_SOCK
954                         && FD_ISSET(ServerSock_UNIX, &rmask))
955                 {
956                         port = ConnCreate(ServerSock_UNIX);
957                         if (port)
958                         {
959                                 BackendStartup(port);
960
961                                 /*
962                                  * We no longer need the open socket or port structure in
963                                  * this process
964                                  */
965                                 StreamClose(port->sock);
966                                 ConnFree(port);
967                         }
968                 }
969 #endif
970
971                 if (ServerSock_INET != INVALID_SOCK
972                         && FD_ISSET(ServerSock_INET, &rmask))
973                 {
974                         port = ConnCreate(ServerSock_INET);
975                         if (port)
976                         {
977                                 BackendStartup(port);
978
979                                 /*
980                                  * We no longer need the open socket or port structure in
981                                  * this process
982                                  */
983                                 StreamClose(port->sock);
984                                 ConnFree(port);
985                         }
986                 }
987         }
988 }
989
990
991 /*
992  * Initialise the read and write masks for select() for the well-known ports
993  * we are listening on.  Return the number of sockets to listen on.
994  */
995
996 static int
997 initMasks(fd_set *rmask, fd_set *wmask)
998 {
999         int                     nsocks = -1;
1000
1001         FD_ZERO(rmask);
1002         FD_ZERO(wmask);
1003
1004 #ifdef HAVE_UNIX_SOCKETS
1005         if (ServerSock_UNIX != INVALID_SOCK)
1006         {
1007                 FD_SET(ServerSock_UNIX, rmask);
1008
1009                 if (ServerSock_UNIX > nsocks)
1010                         nsocks = ServerSock_UNIX;
1011         }
1012 #endif
1013
1014         if (ServerSock_INET != INVALID_SOCK)
1015         {
1016                 FD_SET(ServerSock_INET, rmask);
1017                 if (ServerSock_INET > nsocks)
1018                         nsocks = ServerSock_INET;
1019         }
1020
1021         return nsocks + 1;
1022 }
1023
1024
1025 /*
1026  * Read the startup packet and do something according to it.
1027  *
1028  * Returns STATUS_OK or STATUS_ERROR, or might call elog(FATAL) and
1029  * not return at all.
1030  *
1031  * (Note that elog(FATAL) stuff is sent to the client, so only use it
1032  * if that's what you want.  Return STATUS_ERROR if you don't want to
1033  * send anything to the client, which would typically be appropriate
1034  * if we detect a communications failure.)
1035  */
1036 static int
1037 ProcessStartupPacket(Port *port, bool SSLdone)
1038 {
1039         StartupPacket *packet;
1040         enum CAC_state cac;
1041         int32           len;
1042         void       *buf;
1043
1044         if (pq_getbytes((char *) &len, 4) == EOF)
1045         {
1046                 elog(DEBUG, "incomplete startup packet");
1047                 return STATUS_ERROR;
1048         }
1049
1050         len = ntohl(len);
1051         len -= 4;
1052
1053         if (len < sizeof(len) || len > sizeof(len) + sizeof(StartupPacket))
1054                 elog(FATAL, "invalid length of startup packet");
1055
1056         buf = palloc(len);
1057
1058         if (pq_getbytes(buf, len) == EOF)
1059         {
1060                 elog(DEBUG, "incomplete startup packet");
1061                 return STATUS_ERROR;
1062         }
1063
1064         packet = buf;
1065
1066         /*
1067          * The first field is either a protocol version number or a special
1068          * request code.
1069          */
1070         port->proto = ntohl(packet->protoVersion);
1071
1072         if (port->proto == CANCEL_REQUEST_CODE)
1073         {
1074                 processCancelRequest(port, packet);
1075                 return 127;                             /* XXX */
1076         }
1077
1078         if (port->proto == NEGOTIATE_SSL_CODE && !SSLdone)
1079         {
1080                 char            SSLok;
1081
1082 #ifdef USE_SSL
1083                 /* No SSL when disabled or on Unix sockets */
1084                 if (!EnableSSL || port->laddr.sa.sa_family != AF_INET)
1085                         SSLok = 'N';
1086                 else
1087                         SSLok = 'S';            /* Support for SSL */
1088 #else
1089                 SSLok = 'N';                    /* No support for SSL */
1090 #endif
1091                 if (send(port->sock, &SSLok, 1, 0) != 1)
1092                 {
1093                         elog(DEBUG, "failed to send SSL negotiation response: %s",
1094                                  strerror(errno));
1095                         return STATUS_ERROR;    /* close the connection */
1096                 }
1097
1098 #ifdef USE_SSL
1099                 if (SSLok == 'S')
1100                 {
1101                         if (!(port->ssl = SSL_new(SSL_context)) ||
1102                                 !SSL_set_fd(port->ssl, port->sock) ||
1103                                 SSL_accept(port->ssl) <= 0)
1104                         {
1105                                 elog(DEBUG, "failed to initialize SSL connection: %s (%s)",
1106                                          ERR_reason_error_string(ERR_get_error()), strerror(errno));
1107                                 return STATUS_ERROR;
1108                         }
1109                 }
1110 #endif
1111                 /* regular startup packet, cancel, etc packet should follow... */
1112                 /* but not another SSL negotiation request */
1113                 return ProcessStartupPacket(port, true);
1114         }
1115
1116         /* Could add additional special packet types here */
1117
1118
1119         /* Check we can handle the protocol the frontend is using. */
1120
1121         if (PG_PROTOCOL_MAJOR(port->proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) ||
1122                 PG_PROTOCOL_MAJOR(port->proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) ||
1123                 (PG_PROTOCOL_MAJOR(port->proto) == PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) &&
1124                  PG_PROTOCOL_MINOR(port->proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST)))
1125                 elog(FATAL, "unsupported frontend protocol");
1126
1127         /*
1128          * Get the parameters from the startup packet as C strings.  The
1129          * packet destination was cleared first so a short packet has zeros
1130          * silently added and a long packet is silently truncated.
1131          */
1132         StrNCpy(port->database, packet->database, sizeof(port->database));
1133         StrNCpy(port->user, packet->user, sizeof(port->user));
1134         StrNCpy(port->options, packet->options, sizeof(port->options));
1135         StrNCpy(port->tty, packet->tty, sizeof(port->tty));
1136
1137         /* The database defaults to the user name. */
1138         if (port->database[0] == '\0')
1139                 StrNCpy(port->database, packet->user, sizeof(port->database));
1140
1141         /*
1142          * Truncate given database and user names to length of a Postgres
1143          * name.  This avoids lookup failures when overlength names are given.
1144          */
1145         if ((int) sizeof(port->database) >= NAMEDATALEN)
1146                 port->database[NAMEDATALEN - 1] = '\0';
1147         if ((int) sizeof(port->user) >= NAMEDATALEN)
1148                 port->user[NAMEDATALEN - 1] = '\0';
1149
1150         /* Check a user name was given. */
1151         if (port->user[0] == '\0')
1152                 elog(FATAL, "no PostgreSQL user name specified in startup packet");
1153
1154         /*
1155          * If we're going to reject the connection due to database state, say
1156          * so now instead of wasting cycles on an authentication exchange.
1157          * (This also allows a pg_ping utility to be written.)
1158          */
1159         cac = canAcceptConnections();
1160
1161         switch (cac)
1162         {
1163                 case CAC_STARTUP:
1164                         elog(FATAL, "The database system is starting up");
1165                         break;
1166                 case CAC_SHUTDOWN:
1167                         elog(FATAL, "The database system is shutting down");
1168                         break;
1169                 case CAC_RECOVERY:
1170                         elog(FATAL, "The database system is in recovery mode");
1171                         break;
1172                 case CAC_TOOMANY:
1173                         elog(FATAL, "Sorry, too many clients already");
1174                         break;
1175                 case CAC_OK:
1176                 default:
1177                         ;
1178         }
1179
1180         return STATUS_OK;
1181 }
1182
1183
1184 /*
1185  * The client has sent a cancel request packet, not a normal
1186  * start-a-new-connection packet.  Perform the necessary processing.
1187  * Nothing is sent back to the client.
1188  */
1189 static void
1190 processCancelRequest(Port *port, void *pkt)
1191 {
1192         CancelRequestPacket *canc = (CancelRequestPacket *) pkt;
1193         int                     backendPID;
1194         long            cancelAuthCode;
1195         Dlelem     *curr;
1196         Backend    *bp;
1197
1198         backendPID = (int) ntohl(canc->backendPID);
1199         cancelAuthCode = (long) ntohl(canc->cancelAuthCode);
1200
1201         if (backendPID == CheckPointPID)
1202         {
1203                 if (DebugLvl)
1204                         elog(DEBUG, "processCancelRequest: CheckPointPID in cancel request for process %d", backendPID);
1205                 return;
1206         }
1207
1208         /* See if we have a matching backend */
1209
1210         for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
1211         {
1212                 bp = (Backend *) DLE_VAL(curr);
1213                 if (bp->pid == backendPID)
1214                 {
1215                         if (bp->cancel_key == cancelAuthCode)
1216                         {
1217                                 /* Found a match; signal that backend to cancel current op */
1218                                 if (DebugLvl)
1219                                         elog(DEBUG, "processing cancel request: sending SIGINT to process %d",
1220                                                  backendPID);
1221                                 kill(bp->pid, SIGINT);
1222                         }
1223                         else
1224                         {
1225                                 /* Right PID, wrong key: no way, Jose */
1226                                 if (DebugLvl)
1227                                         elog(DEBUG, "bad key in cancel request for process %d",
1228                                                  backendPID);
1229                         }
1230                         return;
1231                 }
1232         }
1233
1234         /* No matching backend */
1235         if (DebugLvl)
1236                 elog(DEBUG, "bad pid in cancel request for process %d", backendPID);
1237 }
1238
1239 /*
1240  * canAcceptConnections --- check to see if database state allows connections.
1241  */
1242 static enum CAC_state
1243 canAcceptConnections(void)
1244 {
1245         /* Can't start backends when in startup/shutdown/recovery state. */
1246         if (Shutdown > NoShutdown)
1247                 return CAC_SHUTDOWN;
1248         if (StartupPID)
1249                 return CAC_STARTUP;
1250         if (FatalError)
1251                 return CAC_RECOVERY;
1252
1253         /*
1254          * Don't start too many children.
1255          *
1256          * We allow more connections than we can have backends here because some
1257          * might still be authenticating; they might fail auth, or some
1258          * existing backend might exit before the auth cycle is completed. The
1259          * exact MaxBackends limit is enforced when a new backend tries to
1260          * join the shared-inval backend array.
1261          */
1262         if (CountChildren() >= 2 * MaxBackends)
1263                 return CAC_TOOMANY;
1264
1265         return CAC_OK;
1266 }
1267
1268
1269 /*
1270  * ConnCreate -- create a local connection data structure
1271  */
1272 static Port *
1273 ConnCreate(int serverFd)
1274 {
1275         Port       *port;
1276
1277         if (!(port = (Port *) calloc(1, sizeof(Port))))
1278         {
1279                 elog(DEBUG, "ConnCreate: malloc failed");
1280                 SignalChildren(SIGQUIT);
1281                 ExitPostmaster(1);
1282         }
1283
1284         if (StreamConnection(serverFd, port) != STATUS_OK)
1285         {
1286                 StreamClose(port->sock);
1287                 ConnFree(port);
1288                 port = NULL;
1289         }
1290         else
1291         {
1292                 /*
1293                  * Precompute password salt values to use for this connection.
1294                  * It's slightly annoying to do this long in advance of knowing
1295                  * whether we'll need 'em or not, but we must do the random()
1296                  * calls before we fork, not after.  Else the postmaster's random
1297                  * sequence won't get advanced, and all backends would end up
1298                  * using the same salt...
1299                  */
1300                 RandomSalt(port->cryptSalt, port->md5Salt);
1301                 port->pktInfo.state = Idle;
1302         }
1303
1304         return port;
1305 }
1306
1307
1308 /*
1309  * ConnFree -- free a local connection data structure
1310  */
1311 static void
1312 ConnFree(Port *conn)
1313 {
1314 #ifdef USE_SSL
1315         if (conn->ssl)
1316                 SSL_free(conn->ssl);
1317 #endif
1318         free(conn);
1319 }
1320
1321
1322 /*
1323  * ClosePostmasterPorts -- close all the postmaster's open sockets
1324  *
1325  * This is called during child process startup to release file descriptors
1326  * that are not needed by that child process.  The postmaster still has
1327  * them open, of course.
1328  */
1329 void
1330 ClosePostmasterPorts(bool pgstat_too)
1331 {
1332         /* Close the listen sockets */
1333         if (NetServer)
1334                 StreamClose(ServerSock_INET);
1335         ServerSock_INET = INVALID_SOCK;
1336 #ifdef HAVE_UNIX_SOCKETS
1337         StreamClose(ServerSock_UNIX);
1338         ServerSock_UNIX = INVALID_SOCK;
1339 #endif
1340         /* Close pgstat control sockets, unless we're starting pgstat itself */
1341         if (pgstat_too)
1342                 pgstat_close_sockets();
1343 }
1344
1345
1346 /*
1347  * reset_shared -- reset shared memory and semaphores
1348  */
1349 static void
1350 reset_shared(unsigned short port)
1351 {
1352         /*
1353          * Reset assignment of shared mem and semaphore IPC keys. Doing this
1354          * means that in normal cases we'll assign the same keys on each
1355          * "cycle of life", and thereby avoid leaving dead IPC objects
1356          * floating around if the postmaster crashes and is restarted.
1357          */
1358         IpcInitKeyAssignment(port);
1359
1360         /*
1361          * Create or re-create shared memory and semaphores.
1362          */
1363         CreateSharedMemoryAndSemaphores(false, MaxBackends);
1364 }
1365
1366
1367 /*
1368  * Set flag if SIGHUP was detected so config file can be reread in
1369  * main loop
1370  */
1371 static void
1372 SIGHUP_handler(SIGNAL_ARGS)
1373 {
1374         int                     save_errno = errno;
1375
1376         PG_SETMASK(&BlockSig);
1377
1378         if (Shutdown <= SmartShutdown)
1379         {
1380                 got_SIGHUP = true;
1381                 SignalChildren(SIGHUP);
1382         }
1383
1384         errno = save_errno;
1385 }
1386
1387
1388
1389 /*
1390  * pmdie -- signal handler for processing various postmaster signals.
1391  */
1392 static void
1393 pmdie(SIGNAL_ARGS)
1394 {
1395         int                     save_errno = errno;
1396
1397         PG_SETMASK(&BlockSig);
1398
1399         if (DebugLvl >= 1)
1400                 elog(DEBUG, "pmdie %d", postgres_signal_arg);
1401
1402         switch (postgres_signal_arg)
1403         {
1404                 case SIGUSR2:
1405
1406                         /*
1407                          * Send SIGUSR2 to all children (AsyncNotifyHandler)
1408                          */
1409                         if (Shutdown > SmartShutdown)
1410                         {
1411                                 errno = save_errno;
1412                                 return;
1413                         }
1414                         SignalChildren(SIGUSR2);
1415                         errno = save_errno;
1416                         return;
1417
1418                 case SIGTERM:
1419
1420                         /*
1421                          * Smart Shutdown:
1422                          *
1423                          * Wait for children to end their work and ShutdownDataBase.
1424                          */
1425                         if (Shutdown >= SmartShutdown)
1426                         {
1427                                 errno = save_errno;
1428                                 return;
1429                         }
1430                         Shutdown = SmartShutdown;
1431                         elog(DEBUG, "smart shutdown request");
1432                         if (DLGetHead(BackendList)) /* let reaper() handle this */
1433                         {
1434                                 errno = save_errno;
1435                                 return;
1436                         }
1437
1438                         /*
1439                          * No children left. Shutdown data base system.
1440                          */
1441                         if (StartupPID > 0 || FatalError)       /* let reaper() handle
1442                                                                                                  * this */
1443                         {
1444                                 errno = save_errno;
1445                                 return;
1446                         }
1447                         if (ShutdownPID > 0)
1448                         {
1449                                 elog(REALLYFATAL, "shutdown process %d already running",
1450                                          ShutdownPID);
1451                                 abort();
1452                         }
1453
1454                         ShutdownPID = ShutdownDataBase();
1455                         errno = save_errno;
1456                         return;
1457
1458                 case SIGINT:
1459
1460                         /*
1461                          * Fast Shutdown:
1462                          *
1463                          * abort all children with SIGTERM (rollback active transactions
1464                          * and exit) and ShutdownDataBase when they are gone.
1465                          */
1466                         if (Shutdown >= FastShutdown)
1467                         {
1468                                 errno = save_errno;
1469                                 return;
1470                         }
1471                         elog(DEBUG, "fast shutdown request");
1472                         if (DLGetHead(BackendList)) /* let reaper() handle this */
1473                         {
1474                                 Shutdown = FastShutdown;
1475                                 if (!FatalError)
1476                                 {
1477                                         elog(DEBUG, "aborting any active transactions");
1478                                         SignalChildren(SIGTERM);
1479                                 }
1480                                 errno = save_errno;
1481                                 return;
1482                         }
1483                         if (Shutdown > NoShutdown)
1484                         {
1485                                 Shutdown = FastShutdown;
1486                                 errno = save_errno;
1487                                 return;
1488                         }
1489                         Shutdown = FastShutdown;
1490
1491                         /*
1492                          * No children left. Shutdown data base system.
1493                          */
1494                         if (StartupPID > 0 || FatalError)       /* let reaper() handle
1495                                                                                                  * this */
1496                         {
1497                                 errno = save_errno;
1498                                 return;
1499                         }
1500                         if (ShutdownPID > 0)
1501                         {
1502                                 elog(REALLYFATAL, "shutdown process %d already running",
1503                                          ShutdownPID);
1504                                 abort();
1505                         }
1506
1507                         ShutdownPID = ShutdownDataBase();
1508                         errno = save_errno;
1509                         return;
1510
1511                 case SIGQUIT:
1512
1513                         /*
1514                          * Immediate Shutdown:
1515                          *
1516                          * abort all children with SIGQUIT and exit without attempt to
1517                          * properly shutdown data base system.
1518                          */
1519                         elog(DEBUG, "immediate shutdown request");
1520                         if (ShutdownPID > 0)
1521                                 kill(ShutdownPID, SIGQUIT);
1522                         if (StartupPID > 0)
1523                                 kill(StartupPID, SIGQUIT);
1524                         if (DLGetHead(BackendList))
1525                                 SignalChildren(SIGQUIT);
1526                         break;
1527         }
1528
1529         /* exit postmaster */
1530         ExitPostmaster(0);
1531 }
1532
1533 /*
1534  * Reaper -- signal handler to cleanup after a backend (child) dies.
1535  */
1536 static void
1537 reaper(SIGNAL_ARGS)
1538 {
1539         int                     save_errno = errno;
1540
1541 #ifdef HAVE_WAITPID
1542         int                     status;                 /* backend exit status */
1543
1544 #else
1545         union wait      status;                 /* backend exit status */
1546 #endif
1547         int                     exitstatus;
1548         int                     pid;                    /* process id of dead backend */
1549
1550         PG_SETMASK(&BlockSig);
1551         /* It's not really necessary to reset the handler each time is it? */
1552         pqsignal(SIGCHLD, reaper);
1553
1554         if (DebugLvl)
1555                 elog(DEBUG, "reaping dead processes");
1556 #ifdef HAVE_WAITPID
1557         while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1558         {
1559                 exitstatus = status;
1560 #else
1561         while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
1562         {
1563                 exitstatus = status.w_status;
1564 #endif
1565
1566                 /*
1567                  * Check if this child was the statistics collector. If so, start
1568                  * a new one.
1569                  */
1570                 if (pgstat_ispgstat(pid))
1571                 {
1572                         if (WIFEXITED(exitstatus))
1573                                 elog(DEBUG, "statistics collector exited with status %d",
1574                                          WEXITSTATUS(exitstatus));
1575                         else if (WIFSIGNALED(exitstatus))
1576                                 elog(DEBUG, "statistics collector was terminated by signal %d",
1577                                          WTERMSIG(exitstatus));
1578                         pgstat_start();
1579                         continue;
1580                 }
1581
1582                 if (ShutdownPID > 0 && pid == ShutdownPID)
1583                 {
1584                         if (WIFEXITED(exitstatus) && WEXITSTATUS(exitstatus) != 0)
1585                         {
1586                                 elog(DEBUG, "shutdown process %d exited with status %d",
1587                                          pid, WEXITSTATUS(exitstatus));
1588                                 ExitPostmaster(1);
1589                         }
1590                         if (WIFSIGNALED(exitstatus))
1591                         {
1592                                 elog(DEBUG, "shutdown process %d was terminated by signal %d",
1593                                          pid, WTERMSIG(exitstatus));
1594                                 ExitPostmaster(1);
1595                         }
1596                         ExitPostmaster(0);
1597                 }
1598
1599                 if (StartupPID > 0 && pid == StartupPID)
1600                 {
1601                         if (WIFEXITED(exitstatus) && WEXITSTATUS(exitstatus) != 0)
1602                         {
1603                                 elog(DEBUG, "startup process %d exited with status %d; aborting startup",
1604                                          pid, WEXITSTATUS(exitstatus));
1605                                 ExitPostmaster(1);
1606                         }
1607                         if (WIFSIGNALED(exitstatus))
1608                         {
1609                                 elog(DEBUG, "shutdown process %d was terminated by signal %d; aborting startup",
1610                                          pid, WTERMSIG(exitstatus));
1611                                 ExitPostmaster(1);
1612                         }
1613
1614                         StartupPID = 0;
1615                         FatalError = false; /* done with recovery */
1616                         if (Shutdown > NoShutdown)
1617                         {
1618                                 if (ShutdownPID > 0)
1619                                 {
1620                                         elog(STOP, "startup process %d died while shutdown process %d already running",
1621                                                  pid, ShutdownPID);
1622                                         abort();
1623                                 }
1624                                 ShutdownPID = ShutdownDataBase();
1625                         }
1626
1627                         /*
1628                          * Startup succeeded - remember its ID and RedoRecPtr
1629                          */
1630                         SetThisStartUpID();
1631
1632                         /*
1633                          * Arrange for first checkpoint to occur after standard delay.
1634                          */
1635                         CheckPointPID = 0;
1636                         checkpointed = time(NULL);
1637
1638                         errno = save_errno;
1639                         return;
1640                 }
1641
1642                 CleanupProc(pid, exitstatus);
1643         }
1644
1645         if (FatalError)
1646         {
1647                 /*
1648                  * Wait for all children exit, then reset shmem and
1649                  * StartupDataBase.
1650                  */
1651                 if (DLGetHead(BackendList) || StartupPID > 0 || ShutdownPID > 0)
1652                 {
1653                         errno = save_errno;
1654                         return;
1655                 }
1656                 elog(DEBUG, "all server processes terminated; reinitializing shared memory and semaphores");
1657
1658                 shmem_exit(0);
1659                 reset_shared(PostPortNumber);
1660
1661                 StartupPID = StartupDataBase();
1662                 errno = save_errno;
1663                 return;
1664         }
1665
1666         if (Shutdown > NoShutdown)
1667         {
1668                 if (DLGetHead(BackendList))
1669                 {
1670                         errno = save_errno;
1671                         return;
1672                 }
1673                 if (StartupPID > 0 || ShutdownPID > 0)
1674                 {
1675                         errno = save_errno;
1676                         return;
1677                 }
1678                 ShutdownPID = ShutdownDataBase();
1679         }
1680
1681         errno = save_errno;
1682 }
1683
1684 /*
1685  * CleanupProc -- cleanup after terminated backend.
1686  *
1687  * Remove all local state associated with backend.
1688  *
1689  * Dillon's note: should log child's exit status in the system log.
1690  */
1691 static void
1692 CleanupProc(int pid,
1693                         int exitstatus)         /* child's exit status. */
1694 {
1695         Dlelem     *curr,
1696                            *next;
1697         Backend    *bp;
1698
1699         if (DebugLvl)
1700                 elog(DEBUG, "CleanupProc: pid %d exited with status %d",
1701                          pid, exitstatus);
1702
1703         /*
1704          * If a backend dies in an ugly way (i.e. exit status not 0) then we
1705          * must signal all other backends to quickdie.  If exit status is zero
1706          * we assume everything is hunky dory and simply remove the backend
1707          * from the active backend list.
1708          */
1709         if (exitstatus == 0)
1710         {
1711                 curr = DLGetHead(BackendList);
1712                 while (curr)
1713                 {
1714                         bp = (Backend *) DLE_VAL(curr);
1715                         if (bp->pid == pid)
1716                         {
1717                                 DLRemove(curr);
1718                                 free(bp);
1719                                 DLFreeElem(curr);
1720                                 break;
1721                         }
1722                         curr = DLGetSucc(curr);
1723                 }
1724
1725                 if (pid == CheckPointPID)
1726                 {
1727                         CheckPointPID = 0;
1728                         if (!FatalError)
1729                         {
1730                                 checkpointed = time(NULL);
1731                                 /* Update RedoRecPtr for future child backends */
1732                                 GetRedoRecPtr();
1733                         }
1734                 }
1735                 else
1736                         pgstat_beterm(pid);
1737
1738                 return;
1739         }
1740
1741         /* below here we're dealing with a non-normal exit */
1742
1743         /* Make log entry unless we did so already */
1744         if (!FatalError)
1745         {
1746                 if (WIFEXITED(exitstatus))
1747                         elog(DEBUG, "server process (pid %d) exited with status %d",
1748                                  pid, WEXITSTATUS(exitstatus));
1749                 else if (WIFSIGNALED(exitstatus))
1750                         elog(DEBUG, "server process (pid %d) was terminated by signal %d",
1751                                  pid, WTERMSIG(exitstatus));
1752                 elog(DEBUG, "terminating any other active server processes");
1753         }
1754
1755         curr = DLGetHead(BackendList);
1756         while (curr)
1757         {
1758                 next = DLGetSucc(curr);
1759                 bp = (Backend *) DLE_VAL(curr);
1760                 if (bp->pid != pid)
1761                 {
1762                         /*
1763                          * This backend is still alive.  Unless we did so already,
1764                          * tell it to commit hara-kiri.
1765                          *
1766                          * SIGQUIT is the special signal that says exit without proc_exit
1767                          * and let the user know what's going on. But if SendStop is
1768                          * set (-s on command line), then we send SIGSTOP instead, so
1769                          * that we can get core dumps from all backends by hand.
1770                          */
1771                         if (!FatalError)
1772                         {
1773                                 if (DebugLvl)
1774                                         elog(DEBUG, "CleanupProc: sending %s to process %d",
1775                                                  (SendStop ? "SIGSTOP" : "SIGQUIT"),
1776                                                  bp->pid);
1777                                 kill(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
1778                         }
1779                 }
1780                 else
1781                 {
1782                         /*
1783                          * Found entry for freshly-dead backend, so remove it.
1784                          */
1785                         DLRemove(curr);
1786                         free(bp);
1787                         DLFreeElem(curr);
1788                 }
1789                 curr = next;
1790         }
1791
1792         if (pid == CheckPointPID)
1793         {
1794                 CheckPointPID = 0;
1795                 checkpointed = 0;
1796         }
1797         else
1798         {
1799                 /*
1800                  * Tell the collector about backend termination
1801                  */
1802                 pgstat_beterm(pid);
1803         }
1804
1805         FatalError = true;
1806 }
1807
1808 /*
1809  * Send a signal to all backend children.
1810  */
1811 static void
1812 SignalChildren(int signal)
1813 {
1814         Dlelem     *curr,
1815                            *next;
1816         Backend    *bp;
1817         int                     mypid = getpid();
1818
1819         curr = DLGetHead(BackendList);
1820         while (curr)
1821         {
1822                 next = DLGetSucc(curr);
1823                 bp = (Backend *) DLE_VAL(curr);
1824
1825                 if (bp->pid != mypid)
1826                 {
1827                         if (DebugLvl >= 1)
1828                                 elog(DEBUG, "SignalChildren: sending signal %d to process %d",
1829                                          signal, bp->pid);
1830
1831                         kill(bp->pid, signal);
1832                 }
1833
1834                 curr = next;
1835         }
1836 }
1837
1838 /*
1839  * BackendStartup -- start backend process
1840  *
1841  * returns: STATUS_ERROR if the fork/exec failed, STATUS_OK otherwise.
1842  */
1843 static int
1844 BackendStartup(Port *port)
1845 {
1846         Backend    *bn;                         /* for backend cleanup */
1847         pid_t           pid;
1848
1849         /*
1850          * Compute the cancel key that will be assigned to this backend. The
1851          * backend will have its own copy in the forked-off process' value of
1852          * MyCancelKey, so that it can transmit the key to the frontend.
1853          */
1854         MyCancelKey = PostmasterRandom();
1855
1856         /*
1857          * Make room for backend data structure.  Better before the fork() so
1858          * we can handle failure cleanly.
1859          */
1860         bn = (Backend *) malloc(sizeof(Backend));
1861         if (!bn)
1862         {
1863                 elog(DEBUG, "out of memory; connection startup aborted");
1864                 return STATUS_ERROR;
1865         }
1866
1867         /*
1868          * Flush stdio channels just before fork, to avoid double-output
1869          * problems. Ideally we'd use fflush(NULL) here, but there are still a
1870          * few non-ANSI stdio libraries out there (like SunOS 4.1.x) that
1871          * coredump if we do. Presently stdout and stderr are the only stdio
1872          * output channels used by the postmaster, so fflush'ing them should
1873          * be sufficient.
1874          */
1875         fflush(stdout);
1876         fflush(stderr);
1877
1878 #ifdef __BEOS__
1879         /* Specific beos actions before backend startup */
1880         beos_before_backend_startup();
1881 #endif
1882
1883         pid = fork();
1884
1885         if (pid == 0)                           /* child */
1886         {
1887                 int                     status;
1888
1889                 free(bn);
1890 #ifdef __BEOS__
1891                 /* Specific beos backend startup actions */
1892                 beos_backend_startup();
1893 #endif
1894
1895                 status = DoBackend(port);
1896                 if (status != 0)
1897                 {
1898                         elog(DEBUG, "connection startup failed");
1899                         proc_exit(status);
1900                 }
1901                 else
1902                         proc_exit(0);
1903         }
1904
1905         /* in parent, error */
1906         if (pid < 0)
1907         {
1908                 free(bn);
1909 #ifdef __BEOS__
1910                 /* Specific beos backend startup actions */
1911                 beos_backend_startup_failed();
1912 #endif
1913                 elog(DEBUG, "connection startup failed (fork failure): %s",
1914                          strerror(errno));
1915                 return STATUS_ERROR;
1916         }
1917
1918         /* in parent, normal */
1919         if (DebugLvl >= 1)
1920                 elog(DEBUG, "BackendStartup: forked pid=%d socket=%d",
1921                          pid, port->sock);
1922
1923         /*
1924          * Everything's been successful, it's safe to add this backend to our
1925          * list of backends.
1926          */
1927         bn->pid = pid;
1928         bn->cancel_key = MyCancelKey;
1929         DLAddHead(BackendList, DLNewElem(bn));
1930
1931         return STATUS_OK;
1932 }
1933
1934
1935 /*
1936  * split_opts -- split a string of options and append it to an argv array
1937  *
1938  * NB: the string is destructively modified!
1939  *
1940  * Since no current POSTGRES arguments require any quoting characters,
1941  * we can use the simple-minded tactic of assuming each set of space-
1942  * delimited characters is a separate argv element.
1943  *
1944  * If you don't like that, well, we *used* to pass the whole option string
1945  * as ONE argument to execl(), which was even less intelligent...
1946  */
1947 static void
1948 split_opts(char **argv, int *argcp, char *s)
1949 {
1950         while (s && *s)
1951         {
1952                 while (isspace((unsigned char) *s))
1953                         ++s;
1954                 if (*s == '\0')
1955                         break;
1956                 argv[(*argcp)++] = s;
1957                 while (*s && !isspace((unsigned char) *s))
1958                         ++s;
1959                 if (*s)
1960                         *s++ = '\0';
1961         }
1962 }
1963
1964 /*
1965  * DoBackend -- perform authentication, and if successful, set up the
1966  *              backend's argument list and invoke backend main().
1967  *
1968  * This used to perform an execv() but we no longer exec the backend;
1969  * it's the same executable as the postmaster.
1970  *
1971  * returns:
1972  *              Shouldn't return at all.
1973  *              If PostgresMain() fails, return status.
1974  */
1975 static int
1976 DoBackend(Port *port)
1977 {
1978         char       *remote_host;
1979         char       *av[ARGV_SIZE * 2];
1980         int                     ac = 0;
1981         char            debugbuf[ARGV_SIZE];
1982         char            protobuf[ARGV_SIZE];
1983         char            dbbuf[ARGV_SIZE];
1984         char            optbuf[ARGV_SIZE];
1985         char            ttybuf[ARGV_SIZE];
1986         int                     i;
1987         int                     status;
1988         struct timeval now;
1989         struct timezone tz;
1990
1991         /*
1992          * Let's clean up ourselves as the postmaster child
1993          */
1994
1995         IsUnderPostmaster = true;       /* we are a postmaster subprocess now */
1996
1997         /* We don't want the postmaster's proc_exit() handlers */
1998         on_exit_reset();
1999
2000         /*
2001          * Signal handlers setting is moved to tcop/postgres...
2002          */
2003
2004         /* Close the postmaster's other sockets */
2005         ClosePostmasterPorts(true);
2006
2007         /* Save port etc. for ps status */
2008         MyProcPort = port;
2009
2010         /* Reset MyProcPid to new backend's pid */
2011         MyProcPid = getpid();
2012
2013         /*
2014          * Initialize libpq and enable reporting of elog errors to the client.
2015          * Must do this now because authentication uses libpq to send
2016          * messages.
2017          */
2018         pq_init();                                      /* initialize libpq to talk to client */
2019         whereToSendOutput = Remote; /* now safe to elog to client */
2020
2021         /*
2022          * We arrange for a simple exit(0) if we receive SIGTERM or SIGQUIT
2023          * during any client authentication related communication. Otherwise
2024          * the postmaster cannot shutdown the database FAST or IMMED cleanly
2025          * if a buggy client blocks a backend during authentication.  We also
2026          * will exit(0) after a time delay, so that a broken client can't hog
2027          * a connection indefinitely.
2028          *
2029          * PreAuthDelay is a debugging aid for investigating problems in the
2030          * authentication cycle: it can be set in postgresql.conf to allow
2031          * time to attach to the newly-forked backend with a debugger. (See
2032          * also the -W backend switch, which we allow clients to pass through
2033          * PGOPTIONS, but it is not honored until after authentication.)
2034          */
2035         pqsignal(SIGTERM, authdie);
2036         pqsignal(SIGQUIT, authdie);
2037         pqsignal(SIGALRM, authdie);
2038         PG_SETMASK(&AuthBlockSig);
2039
2040         if (PreAuthDelay > 0)
2041                 sleep(PreAuthDelay);
2042
2043         if (!enable_sigalrm_interrupt(AuthenticationTimeout * 1000))
2044                 elog(FATAL, "DoBackend: Unable to set timer for auth timeout");
2045
2046         /*
2047          * Receive the startup packet (which might turn out to be a cancel
2048          * request packet).
2049          */
2050         status = ProcessStartupPacket(port, false);
2051
2052         if (status != STATUS_OK)
2053                 return 0;                               /* cancel request processed, or error */
2054
2055         /*
2056          * Now that we have the user and database name, we can set the process
2057          * title for ps.  It's good to do this as early as possible in
2058          * startup.
2059          *
2060          * But first, we need the remote host name.
2061          */
2062         if (port->raddr.sa.sa_family == AF_INET)
2063         {
2064                 unsigned short remote_port;
2065                 char       *host_addr;
2066
2067                 remote_port = ntohs(port->raddr.in.sin_port);
2068                 host_addr = inet_ntoa(port->raddr.in.sin_addr);
2069
2070                 remote_host = NULL;
2071
2072                 if (HostnameLookup)
2073                 {
2074                         struct hostent *host_ent;
2075
2076                         host_ent = gethostbyaddr((char *) &port->raddr.in.sin_addr,
2077                                                                          sizeof(port->raddr.in.sin_addr),
2078                                                                          AF_INET);
2079
2080                         if (host_ent)
2081                         {
2082                                 remote_host = palloc(strlen(host_addr) + strlen(host_ent->h_name) + 3);
2083                                 sprintf(remote_host, "%s[%s]", host_ent->h_name, host_addr);
2084                         }
2085                 }
2086
2087                 if (remote_host == NULL)
2088                         remote_host = pstrdup(host_addr);
2089
2090                 if (ShowPortNumber)
2091                 {
2092                         char       *str = palloc(strlen(remote_host) + 7);
2093
2094                         sprintf(str, "%s:%hu", remote_host, remote_port);
2095                         pfree(remote_host);
2096                         remote_host = str;
2097                 }
2098         }
2099         else
2100         {
2101                 /* not AF_INET */
2102                 remote_host = "[local]";
2103         }
2104
2105         /*
2106          * Set process parameters for ps display.
2107          */
2108         init_ps_display(port->user, port->database, remote_host);
2109         set_ps_display("authentication");
2110
2111         /*
2112          * Now perform authentication exchange.
2113          */
2114         ClientAuthentication(port); /* might not return, if failure */
2115
2116         /*
2117          * Done with authentication.  Disable timeout, and prevent
2118          * SIGTERM/SIGQUIT again until backend startup is complete.
2119          */
2120         if (!disable_sigalrm_interrupt())
2121                 elog(FATAL, "DoBackend: Unable to disable timer for auth timeout");
2122         PG_SETMASK(&BlockSig);
2123
2124         if (Log_connections)
2125                 elog(DEBUG, "connection: host=%s user=%s database=%s",
2126                          remote_host, port->user, port->database);
2127
2128         /*
2129          * Don't want backend to be able to see the postmaster random number
2130          * generator state.  We have to clobber the static random_seed *and*
2131          * start a new random sequence in the random() library function.
2132          */
2133         random_seed = 0;
2134         gettimeofday(&now, &tz);
2135         srandom((unsigned int) now.tv_usec);
2136
2137         /* ----------------
2138          * Now, build the argv vector that will be given to PostgresMain.
2139          *
2140          * The layout of the command line is
2141          *              postgres [secure switches] -p databasename [insecure switches]
2142          * where the switches after -p come from the client request.
2143          * ----------------
2144          */
2145
2146         av[ac++] = "postgres";
2147
2148         /*
2149          * Pass the requested debugging level along to the backend. Level one
2150          * debugging in the postmaster traces postmaster connection activity,
2151          * and levels two and higher are passed along to the backend.  This
2152          * allows us to watch only the postmaster or the postmaster and the
2153          * backend.
2154          */
2155         if (DebugLvl > 1)
2156         {
2157                 sprintf(debugbuf, "-d%d", DebugLvl);
2158                 av[ac++] = debugbuf;
2159         }
2160
2161         /*
2162          * Pass any backend switches specified with -o in the postmaster's own
2163          * command line.  We assume these are secure. (It's OK to mangle
2164          * ExtraOptions since we are now in the child process; this won't
2165          * change the postmaster's copy.)
2166          */
2167         split_opts(av, &ac, ExtraOptions);
2168
2169         /* Tell the backend what protocol the frontend is using. */
2170         sprintf(protobuf, "-v%u", port->proto);
2171         av[ac++] = protobuf;
2172
2173         /*
2174          * Tell the backend it is being called from the postmaster, and which
2175          * database to use.  -p marks the end of secure switches.
2176          */
2177         av[ac++] = "-p";
2178
2179         StrNCpy(dbbuf, port->database, ARGV_SIZE);
2180         av[ac++] = dbbuf;
2181
2182         /*
2183          * Pass the (insecure) option switches from the connection request.
2184          */
2185         StrNCpy(optbuf, port->options, ARGV_SIZE);
2186         split_opts(av, &ac, optbuf);
2187
2188         /*
2189          * Pass the (insecure) debug output file request.
2190          *
2191          * NOTE: currently, this is useless code, since the backend will not
2192          * honor an insecure -o switch.  I left it here since the backend
2193          * could be modified to allow insecure -o, given adequate checking
2194          * that the specified filename is something safe to write on.
2195          */
2196         if (port->tty[0])
2197         {
2198                 StrNCpy(ttybuf, port->tty, ARGV_SIZE);
2199                 av[ac++] = "-o";
2200                 av[ac++] = ttybuf;
2201         }
2202
2203         av[ac] = (char *) NULL;
2204
2205         /*
2206          * Release postmaster's working memory context so that backend can
2207          * recycle the space.  Note this does not trash *MyProcPort, because
2208          * ConnCreate() allocated that space with malloc() ... else we'd need
2209          * to copy the Port data here.
2210          */
2211         MemoryContextSwitchTo(TopMemoryContext);
2212         MemoryContextDelete(PostmasterContext);
2213         PostmasterContext = NULL;
2214
2215         /*
2216          * Debug: print arguments being passed to backend
2217          */
2218         if (DebugLvl > 1)
2219         {
2220                 fprintf(stderr, "%s child[%d]: starting with (",
2221                                 progname, MyProcPid);
2222                 for (i = 0; i < ac; ++i)
2223                         fprintf(stderr, "%s ", av[i]);
2224                 fprintf(stderr, ")\n");
2225         }
2226
2227         return (PostgresMain(ac, av, port->user));
2228 }
2229
2230 /*
2231  * ExitPostmaster -- cleanup
2232  *
2233  * Do NOT call exit() directly --- always go through here!
2234  */
2235 static void
2236 ExitPostmaster(int status)
2237 {
2238         /* should cleanup shared memory and kill all backends */
2239
2240         /*
2241          * Not sure of the semantics here.      When the Postmaster dies, should
2242          * the backends all be killed? probably not.
2243          *
2244          * MUST         -- vadim 05-10-1999
2245          */
2246         if (ServerSock_INET != INVALID_SOCK)
2247                 StreamClose(ServerSock_INET);
2248         ServerSock_INET = INVALID_SOCK;
2249 #ifdef HAVE_UNIX_SOCKETS
2250         if (ServerSock_UNIX != INVALID_SOCK)
2251                 StreamClose(ServerSock_UNIX);
2252         ServerSock_UNIX = INVALID_SOCK;
2253 #endif
2254
2255         proc_exit(status);
2256 }
2257
2258 /* Request to schedule a checkpoint (no-op if one is currently running) */
2259 static void
2260 schedule_checkpoint(SIGNAL_ARGS)
2261 {
2262         int                     save_errno = errno;
2263
2264         PG_SETMASK(&BlockSig);
2265
2266         /* Ignore request if checkpointing is currently disabled */
2267         if (CheckPointPID == 0 && checkpointed &&
2268                 Shutdown == NoShutdown && !FatalError && random_seed != 0)
2269         {
2270                 CheckPointPID = CheckPointDataBase();
2271                 /* note: if fork fails, CheckPointPID stays 0; nothing happens */
2272         }
2273
2274         errno = save_errno;
2275 }
2276
2277
2278 /*
2279  * CharRemap: given an int in range 0..61, produce textual encoding of it
2280  * per crypt(3) conventions.
2281  */
2282 static char
2283 CharRemap(long ch)
2284 {
2285         if (ch < 0)
2286                 ch = -ch;
2287         ch = ch % 62;
2288
2289         if (ch < 26)
2290                 return 'A' + ch;
2291
2292         ch -= 26;
2293         if (ch < 26)
2294                 return 'a' + ch;
2295
2296         ch -= 26;
2297         return '0' + ch;
2298 }
2299
2300 /*
2301  * RandomSalt
2302  */
2303 static void
2304 RandomSalt(char *cryptSalt, char *md5Salt)
2305 {
2306         long            rand = PostmasterRandom();
2307
2308         cryptSalt[0] = CharRemap(rand % 62);
2309         cryptSalt[1] = CharRemap(rand / 62);
2310
2311         /*
2312          * It's okay to reuse the first random value for one of the MD5 salt
2313          * bytes, since only one of the two salts will be sent to the client.
2314          * After that we need to compute more random bits.
2315          *
2316          * We use % 255, sacrificing one possible byte value, so as to ensure
2317          * that all bits of the random() value participate in the result.
2318          * While at it, add one to avoid generating any null bytes.
2319          */
2320         md5Salt[0] = (rand % 255) + 1;
2321         rand = PostmasterRandom();
2322         md5Salt[1] = (rand % 255) + 1;
2323         rand = PostmasterRandom();
2324         md5Salt[2] = (rand % 255) + 1;
2325         rand = PostmasterRandom();
2326         md5Salt[3] = (rand % 255) + 1;
2327 }
2328
2329 /*
2330  * PostmasterRandom
2331  */
2332 static long
2333 PostmasterRandom(void)
2334 {
2335         static bool initialized = false;
2336
2337         if (!initialized)
2338         {
2339                 Assert(random_seed != 0);
2340                 srandom(random_seed);
2341                 initialized = true;
2342         }
2343
2344         return random();
2345 }
2346
2347 /*
2348  * Count up number of child processes.
2349  */
2350 static int
2351 CountChildren(void)
2352 {
2353         Dlelem     *curr;
2354         Backend    *bp;
2355         int                     mypid = getpid();
2356         int                     cnt = 0;
2357
2358         for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
2359         {
2360                 bp = (Backend *) DLE_VAL(curr);
2361                 if (bp->pid != mypid)
2362                         cnt++;
2363         }
2364         if (CheckPointPID != 0)
2365                 cnt--;
2366         return cnt;
2367 }
2368
2369 #ifdef USE_SSL
2370 /*
2371  * Initialize SSL library and structures
2372  */
2373 static void
2374 InitSSL(void)
2375 {
2376         char            fnbuf[2048];
2377
2378         SSL_load_error_strings();
2379         SSL_library_init();
2380         SSL_context = SSL_CTX_new(SSLv23_method());
2381         if (!SSL_context)
2382         {
2383                 postmaster_error("failed to create SSL context: %s",
2384                                                  ERR_reason_error_string(ERR_get_error()));
2385                 ExitPostmaster(1);
2386         }
2387         snprintf(fnbuf, sizeof(fnbuf), "%s/server.crt", DataDir);
2388         if (!SSL_CTX_use_certificate_file(SSL_context, fnbuf, SSL_FILETYPE_PEM))
2389         {
2390                 postmaster_error("failed to load server certificate (%s): %s",
2391                                                  fnbuf, ERR_reason_error_string(ERR_get_error()));
2392                 ExitPostmaster(1);
2393         }
2394         snprintf(fnbuf, sizeof(fnbuf), "%s/server.key", DataDir);
2395         if (!SSL_CTX_use_PrivateKey_file(SSL_context, fnbuf, SSL_FILETYPE_PEM))
2396         {
2397                 postmaster_error("failed to load private key file (%s): %s",
2398                                                  fnbuf, ERR_reason_error_string(ERR_get_error()));
2399                 ExitPostmaster(1);
2400         }
2401         if (!SSL_CTX_check_private_key(SSL_context))
2402         {
2403                 postmaster_error("check of private key failed: %s",
2404                                                  ERR_reason_error_string(ERR_get_error()));
2405                 ExitPostmaster(1);
2406         }
2407 }
2408 #endif
2409
2410 /*
2411  * Fire off a subprocess for startup/shutdown/checkpoint.
2412  *
2413  * Return value is subprocess' PID, or 0 if failed to start subprocess
2414  * (0 is returned only for checkpoint case).
2415  */
2416 static pid_t
2417 SSDataBase(int xlop)
2418 {
2419         pid_t           pid;
2420         Backend    *bn;
2421
2422         fflush(stdout);
2423         fflush(stderr);
2424
2425 #ifdef __BEOS__
2426         /* Specific beos actions before backend startup */
2427         beos_before_backend_startup();
2428 #endif
2429
2430         if ((pid = fork()) == 0)        /* child */
2431         {
2432                 const char *statmsg;
2433                 char       *av[ARGV_SIZE * 2];
2434                 int                     ac = 0;
2435                 char            nbbuf[ARGV_SIZE];
2436                 char            dbbuf[ARGV_SIZE];
2437                 char            xlbuf[ARGV_SIZE];
2438
2439 #ifdef __BEOS__
2440                 /* Specific beos actions after backend startup */
2441                 beos_backend_startup();
2442 #endif
2443
2444                 IsUnderPostmaster = true;               /* we are a postmaster subprocess
2445                                                                                  * now */
2446
2447                 /* Lose the postmaster's on-exit routines and port connections */
2448                 on_exit_reset();
2449
2450                 /* Close the postmaster's sockets */
2451                 ClosePostmasterPorts(true);
2452
2453                 /*
2454                  * Identify myself via ps
2455                  */
2456                 switch (xlop)
2457                 {
2458                         case BS_XLOG_STARTUP:
2459                                 statmsg = "startup subprocess";
2460                                 break;
2461                         case BS_XLOG_CHECKPOINT:
2462                                 statmsg = "checkpoint subprocess";
2463                                 break;
2464                         case BS_XLOG_SHUTDOWN:
2465                                 statmsg = "shutdown subprocess";
2466                                 break;
2467                         default:
2468                                 statmsg = "??? subprocess";
2469                                 break;
2470                 }
2471                 init_ps_display(statmsg, "", "");
2472                 set_ps_display("");
2473
2474                 /* Set up command-line arguments for subprocess */
2475                 av[ac++] = "postgres";
2476
2477                 av[ac++] = "-d";
2478
2479                 sprintf(nbbuf, "-B%d", NBuffers);
2480                 av[ac++] = nbbuf;
2481
2482                 sprintf(xlbuf, "-x%d", xlop);
2483                 av[ac++] = xlbuf;
2484
2485                 av[ac++] = "-p";
2486
2487                 StrNCpy(dbbuf, "template1", ARGV_SIZE);
2488                 av[ac++] = dbbuf;
2489
2490                 av[ac] = (char *) NULL;
2491
2492                 BootstrapMain(ac, av);
2493                 ExitPostmaster(0);
2494         }
2495
2496         /* in parent */
2497         if (pid < 0)
2498         {
2499 #ifdef __BEOS__
2500                 /* Specific beos actions before backend startup */
2501                 beos_backend_startup_failed();
2502 #endif
2503
2504                 switch (xlop)
2505                 {
2506                         case BS_XLOG_STARTUP:
2507                                 elog(DEBUG, "could not launch startup process (fork failure): %s",
2508                                          strerror(errno));
2509                                 break;
2510                         case BS_XLOG_CHECKPOINT:
2511                                 elog(DEBUG, "could not launch checkpoint process (fork failure): %s",
2512                                          strerror(errno));
2513                                 break;
2514                         case BS_XLOG_SHUTDOWN:
2515                         default:
2516                                 elog(DEBUG, "could not launch shutdown process (fork failure): %s",
2517                                          strerror(errno));
2518                                 break;
2519                 }
2520
2521                 /*
2522                  * fork failure is fatal during startup/shutdown, but there's no
2523                  * need to choke if a routine checkpoint fails.
2524                  */
2525                 if (xlop == BS_XLOG_CHECKPOINT)
2526                         return 0;
2527                 ExitPostmaster(1);
2528         }
2529
2530         /*
2531          * The startup and shutdown processes are not considered normal
2532          * backends, but the checkpoint process is.  Checkpoint must be added
2533          * to the list of backends.
2534          */
2535         if (xlop == BS_XLOG_CHECKPOINT)
2536         {
2537                 if (!(bn = (Backend *) calloc(1, sizeof(Backend))))
2538                 {
2539                         elog(DEBUG, "CheckPointDataBase: malloc failed");
2540                         ExitPostmaster(1);
2541                 }
2542
2543                 bn->pid = pid;
2544                 bn->cancel_key = PostmasterRandom();
2545                 DLAddHead(BackendList, DLNewElem(bn));
2546
2547                 /*
2548                  * Since this code is executed periodically, it's a fine place to
2549                  * do other actions that should happen every now and then on no
2550                  * particular schedule.  Such as...
2551                  */
2552                 TouchSocketLockFile();
2553         }
2554
2555         return pid;
2556 }
2557
2558
2559 /*
2560  * Create the opts file
2561  */
2562 static bool
2563 CreateOptsFile(int argc, char *argv[])
2564 {
2565         char            fullprogname[MAXPGPATH];
2566         char       *filename;
2567         FILE       *fp;
2568         unsigned        i;
2569
2570         if (FindExec(fullprogname, argv[0], "postmaster") < 0)
2571                 return false;
2572
2573         filename = palloc(strlen(DataDir) + 20);
2574         sprintf(filename, "%s/postmaster.opts", DataDir);
2575
2576         fp = fopen(filename, "w");
2577         if (fp == NULL)
2578         {
2579                 postmaster_error("cannot create file %s: %s",
2580                                                  filename, strerror(errno));
2581                 return false;
2582         }
2583
2584         fprintf(fp, "%s", fullprogname);
2585         for (i = 1; i < argc; i++)
2586                 fprintf(fp, " '%s'", argv[i]);
2587         fputs("\n", fp);
2588
2589         if (ferror(fp))
2590         {
2591                 postmaster_error("writing file %s failed", filename);
2592                 fclose(fp);
2593                 return false;
2594         }
2595
2596         fclose(fp);
2597         return true;
2598 }
2599
2600
2601 static void
2602 postmaster_error(const char *fmt,...)
2603 {
2604         va_list         ap;
2605
2606         fprintf(stderr, "%s: ", progname);
2607         va_start(ap, fmt);
2608         vfprintf(stderr, gettext(fmt), ap);
2609         va_end(ap);
2610         fprintf(stderr, "\n");
2611 }