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