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