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