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