1 /*-------------------------------------------------------------------------
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.
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.
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.
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
35 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
36 * Portions Copyright (c) 1994, Regents of the University of California
40 * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.439 2004/11/17 00:14:12 tgl Exp $
45 * The Postmaster sets up shared memory data structures
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.
55 * The Postmaster cleans up after backends if they have an emergency
56 * exit and/or core dump.
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.
64 *-------------------------------------------------------------------------
75 #include <sys/socket.h>
77 #include <sys/param.h>
78 #include <netinet/in.h>
79 #include <arpa/inet.h>
83 #ifdef HAVE_SYS_SELECT_H
84 #include <sys/select.h>
92 #include <DNSServiceDiscovery/DNSServiceDiscovery.h>
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"
124 #include "storage/spin.h"
129 * List of active backends (or child processes anyway; we don't actually
130 * know whether a given child has become a backend or is still in the
131 * authorization phase). This is used mainly to keep track of how many
132 * children we have and send them appropriate signals when necessary.
134 * "Special" children such as the startup and bgwriter tasks are not in
139 pid_t pid; /* process id of backend */
140 long cancel_key; /* cancel key for cancels for this backend */
143 static Dllist *BackendList;
146 #define NUM_BACKENDARRAY_ELEMS (2*MaxBackends)
147 static Backend *ShmemBackendArray;
150 /* The socket number we are listening for connections on */
153 char *ListenAddresses;
156 * ReservedBackends is the number of backends reserved for superuser use.
157 * This number is taken out of the pool size given by MaxBackends so
158 * number of backend slots available to non-superusers is
159 * (MaxBackends - ReservedBackends). Note what this really means is
160 * "if there are <= ReservedBackends connections available, only superusers
161 * can make new connections" --- pre-existing superuser connections don't
162 * count against the limit.
164 int ReservedBackends;
167 static const char *progname = NULL;
169 /* The socket(s) we're listening to. */
171 static int ListenSocket[MAXLISTEN];
174 * Set by the -o option
176 static char ExtraOptions[MAXPGPATH];
179 * These globals control the behavior of the postmaster in case some
180 * backend dumps core. Normally, it kills all peers of the dead backend
181 * and reinitializes shared memory. By specifying -s or -n, we can have
182 * the postmaster stop (rather than kill) peers and not reinitialize
183 * shared data structures.
185 static bool Reinit = true;
186 static int SendStop = false;
188 /* still more option variables */
189 bool EnableSSL = false;
190 bool SilentMode = false; /* silent mode (-S) */
192 int PreAuthDelay = 0;
193 int AuthenticationTimeout = 60;
195 bool log_hostname; /* for ps display and logging */
196 bool Log_connections = false;
197 bool Db_user_namespace = false;
199 char *rendezvous_name;
201 /* list of library:init-function to be preloaded */
202 char *preload_libraries_string = NULL;
204 /* PIDs of special child processes; 0 when not running */
205 static pid_t StartupPID = 0,
211 /* Startup/shutdown state */
213 #define SmartShutdown 1
214 #define FastShutdown 2
216 static int Shutdown = NoShutdown;
218 static bool FatalError = false; /* T if recovering from backend crash */
220 bool ClientAuthInProgress = false; /* T during new-client
224 * State for assigning random salts and cancel keys.
225 * Also, the global MyCancelKey passes the cancel key assigned to a given
226 * backend from the postmaster to that backend (via fork).
228 static unsigned int random_seed = 0;
234 #ifdef HAVE_INT_OPTRESET
239 * postmaster.c - function prototypes
241 static void checkDataDir(void);
243 #ifdef USE_RENDEZVOUS
244 static void reg_reply(DNSServiceRegistrationReplyErrorType errorCode,
247 static void pmdaemonize(void);
248 static Port *ConnCreate(int serverFd);
249 static void ConnFree(Port *port);
250 static void reset_shared(unsigned short port);
251 static void SIGHUP_handler(SIGNAL_ARGS);
252 static void pmdie(SIGNAL_ARGS);
253 static void reaper(SIGNAL_ARGS);
254 static void sigusr1_handler(SIGNAL_ARGS);
255 static void dummy_handler(SIGNAL_ARGS);
256 static void CleanupBackend(int pid, int exitstatus);
257 static void HandleChildCrash(int pid, int exitstatus, const char *procname);
258 static void LogChildExit(int lev, const char *procname,
259 int pid, int exitstatus);
260 static int BackendRun(Port *port);
261 static void ExitPostmaster(int status);
262 static void usage(const char *);
263 static int ServerLoop(void);
264 static int BackendStartup(Port *port);
265 static int ProcessStartupPacket(Port *port, bool SSLdone);
266 static void processCancelRequest(Port *port, void *pkt);
267 static int initMasks(fd_set *rmask);
268 static void report_fork_failure_to_client(Port *port, int errnum);
269 static enum CAC_state canAcceptConnections(void);
270 static long PostmasterRandom(void);
271 static void RandomSalt(char *cryptSalt, char *md5Salt);
272 static void SignalChildren(int signal);
273 static int CountChildren(void);
274 static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
275 static pid_t StartChildProcess(int xlop);
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);
285 static pid_t *win32_childPIDArray;
286 static HANDLE *win32_childHNDArray;
287 static unsigned long win32_numChildren = 0;
289 HANDLE PostmasterHandle;
292 static pid_t backend_forkexec(Port *port);
293 static pid_t internal_forkexec(int argc, char *argv[], Port *port);
295 /* Type for a socket that can be inherited to a client process */
299 SOCKET origsocket; /* Original socket value, or -1 if not a socket */
300 WSAPROTOCOL_INFO wsainfo;
303 typedef int InheritableSocket;
306 typedef struct LWLock LWLock; /* ugly kluge */
309 * Structure contains all variables passed to exec:ed backends
314 InheritableSocket portsocket;
315 char DataDir[MAXPGPATH];
316 int ListenSocket[MAXLISTEN];
318 unsigned long UsedShmemSegID;
319 void *UsedShmemSegAddr;
321 slock_t *ShmemIndexLock;
322 VariableCache ShmemVariableCache;
323 void *ShmemIndexAlloc;
324 Backend *ShmemBackendArray;
326 slock_t *ProcStructLock;
327 InheritableSocket pgStatSock;
328 InheritableSocket pgStatPipe0;
329 InheritableSocket pgStatPipe1;
332 HANDLE PostmasterHandle;
333 HANDLE initial_signal_pipe;
334 HANDLE syslogPipe[2];
338 char my_exec_path[MAXPGPATH];
339 char ExtraOptions[MAXPGPATH];
340 char lc_collate[MAXPGPATH];
341 char lc_ctype[MAXPGPATH];
344 static void read_backend_variables(char *id, Port *port);
345 static void restore_backend_variables(BackendParameters *param, Port *port);
347 static bool save_backend_variables(BackendParameters *param, Port *port);
349 static bool save_backend_variables(BackendParameters *param, Port *port,
350 HANDLE childProcess, pid_t childPid);
353 static void ShmemBackendArrayAdd(Backend *bn);
354 static void ShmemBackendArrayRemove(pid_t pid);
356 #endif /* EXEC_BACKEND */
358 #define StartupDataBase() StartChildProcess(BS_XLOG_STARTUP)
359 #define StartBackgroundWriter() StartChildProcess(BS_XLOG_BGWRITER)
363 * Postmaster main entry point
366 PostmasterMain(int argc, char *argv[])
370 char *userDoption = NULL;
373 /* This will call exit() if strdup() fails. */
374 progname = get_progname(argv[0]);
376 MyProcPid = PostmasterPid = getpid();
378 IsPostmasterEnvironment = true;
381 * Catch standard options before doing much else. This even works on
382 * systems without getopt_long.
386 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
391 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
393 puts("postmaster (PostgreSQL) " PG_VERSION);
399 /* Start our win32 signal implementation */
400 pgwin32_signal_initialize();
404 * for security, no dir or file created can be group or other
407 umask((mode_t) 0077);
410 * Fire up essential subsystems: memory management
415 * By default, palloc() requests in the postmaster will be allocated
416 * in the PostmasterContext, which is space that can be recycled by
417 * backends. Allocated data that needs to be available to backends
418 * should be allocated in TopMemoryContext.
420 PostmasterContext = AllocSetContextCreate(TopMemoryContext,
422 ALLOCSET_DEFAULT_MINSIZE,
423 ALLOCSET_DEFAULT_INITSIZE,
424 ALLOCSET_DEFAULT_MAXSIZE);
425 MemoryContextSwitchTo(PostmasterContext);
427 IgnoreSystemIndexes(false);
429 if (find_my_exec(argv[0], my_exec_path) < 0)
430 elog(FATAL, "%s: could not locate my own executable path",
433 get_pkglib_path(my_exec_path, pkglib_path);
438 InitializeGUCOptions();
442 while ((opt = getopt(argc, argv, "A:a:B:b:c:D:d:Fh:ik:lm:MN:no:p:Ss-:")) != -1)
447 #ifdef USE_ASSERT_CHECKING
448 SetConfigOption("debug_assertions", optarg, PGC_POSTMASTER, PGC_S_ARGV);
450 write_stderr("%s: assert checking is not compiled in\n", progname);
454 /* Can no longer set authentication method. */
457 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
460 /* Can no longer set the backend executable file to use. */
463 userDoption = optarg;
466 set_debug_options(atoi(optarg), PGC_POSTMASTER, PGC_S_ARGV);
469 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
472 SetConfigOption("listen_addresses", optarg, PGC_POSTMASTER, PGC_S_ARGV);
475 SetConfigOption("listen_addresses", "*", PGC_POSTMASTER, PGC_S_ARGV);
478 SetConfigOption("unix_socket_directory", optarg, PGC_POSTMASTER, PGC_S_ARGV);
482 SetConfigOption("ssl", "true", PGC_POSTMASTER, PGC_S_ARGV);
486 /* Multiplexed backends no longer supported. */
491 * ignore this flag. This may be passed in because the
492 * program was run as 'postgres -M' instead of
497 /* The max number of backends to start. */
498 SetConfigOption("max_connections", optarg, PGC_POSTMASTER, PGC_S_ARGV);
501 /* Don't reinit shared mem after abnormal exit */
507 * Other options to pass to the backend on the command
510 snprintf(ExtraOptions + strlen(ExtraOptions),
511 sizeof(ExtraOptions) - strlen(ExtraOptions),
515 SetConfigOption("port", optarg, PGC_POSTMASTER, PGC_S_ARGV);
520 * Start in 'S'ilent mode (disassociate from controlling
521 * tty). You may also think of this as 'S'ysV mode since
522 * it's most badly needed on SysV-derived systems like
525 SetConfigOption("silent_mode", "true", PGC_POSTMASTER, PGC_S_ARGV);
530 * In the event that some backend dumps core, send
531 * SIGSTOP, rather than SIGQUIT, to all its peers. This
532 * lets the wily post_hacker collect core dumps from
543 ParseLongOption(optarg, &name, &value);
548 (errcode(ERRCODE_SYNTAX_ERROR),
549 errmsg("--%s requires a value",
553 (errcode(ERRCODE_SYNTAX_ERROR),
554 errmsg("-c %s requires a value",
558 SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
566 write_stderr("Try \"%s --help\" for more information.\n",
573 * Postmaster accepts no non-option switch arguments.
577 write_stderr("%s: invalid argument: \"%s\"\n",
578 progname, argv[optind]);
579 write_stderr("Try \"%s --help\" for more information.\n",
585 * Locate the proper configuration files and data directory, and
586 * read postgresql.conf for the first time.
588 if (!SelectConfigFiles(userDoption, progname))
591 /* Verify that DataDir looks reasonable */
595 * Check for invalid combinations of GUC settings.
597 if (NBuffers < 2 * MaxBackends || NBuffers < 16)
600 * Do not accept -B so small that backends are likely to starve
601 * for lack of buffers. The specific choices here are somewhat
604 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);
608 if (ReservedBackends >= MaxBackends)
610 write_stderr("%s: superuser_reserved_connections must be less than max_connections\n", progname);
615 * Other one-time internal sanity checks can go here.
617 if (!CheckDateTokenTables())
619 write_stderr("%s: invalid datetoken tables, please fix\n", progname);
624 * Now that we are done processing the postmaster arguments, reset
625 * getopt(3) library so that it will work correctly in subprocesses.
628 #ifdef HAVE_INT_OPTRESET
629 optreset = 1; /* some systems need this too */
632 /* For debugging: display postmaster environment */
634 extern char **environ;
638 (errmsg_internal("%s: PostmasterMain: initial environ dump:",
641 (errmsg_internal("-----------------------------------------")));
642 for (p = environ; *p; ++p)
644 (errmsg_internal("\t%s", *p)));
646 (errmsg_internal("-----------------------------------------")));
650 if (find_other_exec(argv[0], "postgres", PG_VERSIONSTR,
651 postgres_exec_path) < 0)
653 (errmsg("%s: could not locate matching postgres executable",
658 * Initialize SSL library, if specified.
666 * process any libraries that should be preloaded and optionally
669 if (preload_libraries_string)
670 process_preload_libraries(preload_libraries_string);
673 * Fork away from controlling terminal, if -S specified.
675 * Must do this before we grab any interlock files, else the interlocks
676 * will show the wrong PID.
682 * Create lockfile for data directory.
684 * We want to do this before we try to grab the input sockets, because
685 * the data directory interlock is more reliable than the socket-file
686 * interlock (thanks to whoever decided to put socket files in /tmp
687 * :-(). For the same reason, it's best to grab the TCP socket(s)
688 * before the Unix socket.
690 CreateDataDirLockFile(DataDir, true);
693 * Remove old temporary files. At this point there can be no other
694 * Postgres processes running in this directory, so this should be
700 * Establish input sockets.
702 for (i = 0; i < MAXLISTEN; i++)
703 ListenSocket[i] = -1;
711 /* Need a modifiable copy of ListenAddresses */
712 rawstring = pstrdup(ListenAddresses);
714 /* Parse string into list of identifiers */
715 if (!SplitIdentifierString(rawstring, ',', &elemlist))
717 /* syntax error in list */
719 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
720 errmsg("invalid list syntax for \"listen_addresses\"")));
725 char *curhost = (char *) lfirst(l);
727 if (strcmp(curhost, "*") == 0)
728 status = StreamServerPort(AF_UNSPEC, NULL,
729 (unsigned short) PostPortNumber,
731 ListenSocket, MAXLISTEN);
733 status = StreamServerPort(AF_UNSPEC, curhost,
734 (unsigned short) PostPortNumber,
736 ListenSocket, MAXLISTEN);
737 if (status != STATUS_OK)
739 (errmsg("could not create listen socket for \"%s\"",
747 #ifdef USE_RENDEZVOUS
748 /* Register for Rendezvous only if we opened TCP socket(s) */
749 if (ListenSocket[0] != -1 && rendezvous_name != NULL)
751 DNSServiceRegistrationCreate(rendezvous_name,
754 htonl(PostPortNumber),
756 (DNSServiceRegistrationReply) reg_reply,
761 #ifdef HAVE_UNIX_SOCKETS
762 status = StreamServerPort(AF_UNIX, NULL,
763 (unsigned short) PostPortNumber,
765 ListenSocket, MAXLISTEN);
766 if (status != STATUS_OK)
768 (errmsg("could not create Unix-domain socket")));
772 * check that we have some socket to listen on
774 if (ListenSocket[0] == -1)
776 (errmsg("no socket created for listening")));
781 * Set up shared memory and semaphores.
783 reset_shared(PostPortNumber);
786 * Estimate number of openable files. This must happen after setting
787 * up semaphores, because on some platforms semaphores count as open
793 * Initialize the list of active backends.
795 BackendList = DLNewList();
800 * Initialize the child pid/HANDLE arrays for signal handling.
802 win32_childPIDArray = (pid_t *)
803 malloc(NUM_BACKENDARRAY_ELEMS * sizeof(pid_t));
804 win32_childHNDArray = (HANDLE *)
805 malloc(NUM_BACKENDARRAY_ELEMS * sizeof(HANDLE));
806 if (!win32_childPIDArray || !win32_childHNDArray)
808 (errcode(ERRCODE_OUT_OF_MEMORY),
809 errmsg("out of memory")));
812 * Set up a handle that child processes can use to check whether the
813 * postmaster is still running.
815 if (DuplicateHandle(GetCurrentProcess(),
821 DUPLICATE_SAME_ACCESS) == 0)
823 (errmsg_internal("could not duplicate postmaster handle: error code %d",
824 (int) GetLastError())));
828 * Record postmaster options. We delay this till now to avoid
829 * recording bogus options (eg, NBuffers too high for available
832 if (!CreateOptsFile(argc, argv, my_exec_path))
836 write_nondefault_variables(PGC_POSTMASTER);
840 * Write the external PID file if requested
842 if (external_pid_file)
844 FILE *fpidfile = fopen(external_pid_file, "w");
848 fprintf(fpidfile, "%d\n", MyProcPid);
850 /* Should we remove the pid file on postmaster exit? */
853 write_stderr("%s: could not write external PID file \"%s\": %s\n",
854 progname, external_pid_file, strerror(errno));
858 * Set up signal handlers for the postmaster process.
860 * CAUTION: when changing this list, check for side-effects on the signal
861 * handling setup of child processes. See tcop/postgres.c,
862 * bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/pgarch.c,
863 * postmaster/pgstat.c, and postmaster/syslogger.c.
866 PG_SETMASK(&BlockSig);
868 pqsignal(SIGHUP, SIGHUP_handler); /* reread config file and have
869 * children do same */
870 pqsignal(SIGINT, pmdie); /* send SIGTERM and shut down */
871 pqsignal(SIGQUIT, pmdie); /* send SIGQUIT and die */
872 pqsignal(SIGTERM, pmdie); /* wait for children and shut down */
873 pqsignal(SIGALRM, SIG_IGN); /* ignored */
874 pqsignal(SIGPIPE, SIG_IGN); /* ignored */
875 pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
876 pqsignal(SIGUSR2, dummy_handler); /* unused, reserve for children */
877 pqsignal(SIGCHLD, reaper); /* handle child termination */
878 pqsignal(SIGTTIN, SIG_IGN); /* ignored */
879 pqsignal(SIGTTOU, SIG_IGN); /* ignored */
880 /* ignore SIGXFSZ, so that ulimit violations work like disk full */
882 pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
886 * If enabled, start up syslogger collection subprocess
888 SysLoggerPID = SysLogger_Start();
891 * Reset whereToSendOutput from Debug (its starting state) to None.
892 * This stops ereport from sending log messages to stderr unless
893 * Log_destination permits. We don't do this until the postmaster is
894 * fully launched, since startup failures may as well be reported to
897 whereToSendOutput = None;
900 * Initialize the statistics collector stuff
905 * Load cached files for client authentication.
913 * We're ready to rock and roll...
915 StartupPID = StartupDataBase();
917 status = ServerLoop();
920 * ServerLoop probably shouldn't ever return, but if it does, close
923 ExitPostmaster(status != STATUS_OK);
925 return 0; /* not reached */
930 * Validate the proposed data directory
935 char path[MAXPGPATH];
937 struct stat stat_buf;
941 if (stat(DataDir, &stat_buf) != 0)
945 (errcode_for_file_access(),
946 errmsg("data directory \"%s\" does not exist",
950 (errcode_for_file_access(),
951 errmsg("could not read permissions of directory \"%s\": %m",
956 * Check if the directory has group or world access. If so, reject.
958 * XXX temporarily suppress check when on Windows, because there may not
959 * be proper support for Unix-y file permissions. Need to think of a
960 * reasonable check to apply on Windows.
962 #if !defined(WIN32) && !defined(__CYGWIN__)
963 if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
965 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
966 errmsg("data directory \"%s\" has group or world access",
968 errdetail("Permissions should be u=rwx (0700).")));
971 /* Look for PG_VERSION before looking for pg_control */
972 ValidatePgVersion(DataDir);
974 snprintf(path, sizeof(path), "%s/global/pg_control", DataDir);
976 fp = AllocateFile(path, PG_BINARY_R);
979 write_stderr("%s: could not find the database system\n"
980 "Expected to find it in the directory \"%s\",\n"
981 "but could not open file \"%s\": %s\n",
982 progname, DataDir, path, strerror(errno));
989 #ifdef USE_RENDEZVOUS
992 * empty callback function for DNSServiceRegistrationCreate()
995 reg_reply(DNSServiceRegistrationReplyErrorType errorCode, void *context)
999 #endif /* USE_RENDEZVOUS */
1003 * Fork away from the controlling terminal (-S option)
1012 #ifdef LINUX_PROFILE
1013 struct itimerval prof_itimer;
1016 #ifdef LINUX_PROFILE
1017 /* see comments in BackendStartup */
1018 getitimer(ITIMER_PROF, &prof_itimer);
1022 if (pid == (pid_t) -1)
1024 write_stderr("%s: could not fork background process: %s\n",
1025 progname, strerror(errno));
1030 /* Parent should just exit, without doing any atexit cleanup */
1034 #ifdef LINUX_PROFILE
1035 setitimer(ITIMER_PROF, &prof_itimer, NULL);
1038 MyProcPid = PostmasterPid = getpid(); /* reset PID vars to child */
1040 /* GH: If there's no setsid(), we hopefully don't need silent mode.
1041 * Until there's a better solution.
1046 write_stderr("%s: could not dissociate from controlling TTY: %s\n",
1047 progname, strerror(errno));
1051 i = open(NULL_DEV, O_RDWR);
1058 elog(FATAL, "SilentMode not supported under WIN32");
1064 * Print out help message
1067 usage(const char *progname)
1069 printf(gettext("%s is the PostgreSQL server.\n\n"), progname);
1070 printf(gettext("Usage:\n %s [OPTION]...\n\n"), progname);
1071 printf(gettext("Options:\n"));
1072 #ifdef USE_ASSERT_CHECKING
1073 printf(gettext(" -A 1|0 enable/disable run-time assert checking\n"));
1075 printf(gettext(" -B NBUFFERS number of shared buffers\n"));
1076 printf(gettext(" -c NAME=VALUE set run-time parameter\n"));
1077 printf(gettext(" -d 1-5 debugging level\n"));
1078 printf(gettext(" -D DATADIR database directory\n"));
1079 printf(gettext(" -F turn fsync off\n"));
1080 printf(gettext(" -h HOSTNAME host name or IP address to listen on\n"));
1081 printf(gettext(" -i enable TCP/IP connections\n"));
1082 printf(gettext(" -k DIRECTORY Unix-domain socket location\n"));
1084 printf(gettext(" -l enable SSL connections\n"));
1086 printf(gettext(" -N MAX-CONNECT maximum number of allowed connections\n"));
1087 printf(gettext(" -o OPTIONS pass \"OPTIONS\" to each server process\n"));
1088 printf(gettext(" -p PORT port number to listen on\n"));
1089 printf(gettext(" -S silent mode (start in background without logging output)\n"));
1090 printf(gettext(" --help show this help, then exit\n"));
1091 printf(gettext(" --version output version information, then exit\n"));
1093 printf(gettext("\nDeveloper options:\n"));
1094 printf(gettext(" -n do not reinitialize shared memory after abnormal exit\n"));
1095 printf(gettext(" -s send SIGSTOP to all backend servers if one dies\n"));
1097 printf(gettext("\nPlease read the documentation for the complete list of run-time\n"
1098 "configuration settings and how to set them on the command line or in\n"
1099 "the configuration file.\n\n"
1100 "Report bugs to <pgsql-bugs@postgresql.org>.\n"));
1105 * Main idle loop of postmaster
1114 struct timeval earlier,
1118 gettimeofday(&earlier, &tz);
1119 last_touch_time = time(NULL);
1121 nSockets = initMasks(&readmask);
1127 struct timeval timeout;
1132 * Wait for something to happen.
1134 * We wait at most one minute, to ensure that the other background
1135 * tasks handled below get done even when no requests are
1138 memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
1140 timeout.tv_sec = 60;
1141 timeout.tv_usec = 0;
1143 PG_SETMASK(&UnBlockSig);
1145 selres = select(nSockets, &rmask, NULL, NULL, &timeout);
1148 * Block all signals until we wait again. (This makes it safe for
1149 * our signal handlers to do nontrivial work.)
1151 PG_SETMASK(&BlockSig);
1155 if (errno != EINTR && errno != EWOULDBLOCK)
1158 (errcode_for_socket_access(),
1159 errmsg("select() failed in postmaster: %m")));
1160 return STATUS_ERROR;
1165 * New connection pending on any of our sockets? If so, fork a
1166 * child process to deal with it.
1171 * Select a random seed at the time of first receiving a
1174 while (random_seed == 0)
1176 gettimeofday(&later, &tz);
1179 * We are not sure how much precision is in tv_usec, so we
1180 * swap the high and low 16 bits of 'later' and XOR them with
1181 * 'earlier'. On the off chance that the result is 0, we
1182 * loop until it isn't.
1184 random_seed = earlier.tv_usec ^
1185 ((later.tv_usec << 16) |
1186 ((later.tv_usec >> 16) & 0xffff));
1189 for (i = 0; i < MAXLISTEN; i++)
1191 if (ListenSocket[i] == -1)
1193 if (FD_ISSET(ListenSocket[i], &rmask))
1195 port = ConnCreate(ListenSocket[i]);
1198 BackendStartup(port);
1201 * We no longer need the open socket or port
1202 * structure in this process
1204 StreamClose(port->sock);
1211 /* If we have lost the system logger, try to start a new one */
1212 if (SysLoggerPID == 0 && Redirect_stderr)
1213 SysLoggerPID = SysLogger_Start();
1216 * If no background writer process is running, and we are not in a
1217 * state that prevents it, start one. It doesn't matter if this
1218 * fails, we'll just try again later.
1220 if (BgWriterPID == 0 && StartupPID == 0 && !FatalError)
1222 BgWriterPID = StartBackgroundWriter();
1223 /* If shutdown is pending, set it going */
1224 if (Shutdown > NoShutdown && BgWriterPID != 0)
1225 kill(BgWriterPID, SIGUSR2);
1228 /* If we have lost the archiver, try to start a new one */
1229 if (XLogArchivingActive() && PgArchPID == 0 &&
1230 StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
1231 PgArchPID = pgarch_start();
1233 /* If we have lost the stats collector, try to start a new one */
1234 if (PgStatPID == 0 &&
1235 StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
1236 PgStatPID = pgstat_start();
1239 * Touch the socket and lock file at least every ten minutes, to
1240 * ensure that they are not removed by overzealous /tmp-cleaning
1244 if (now - last_touch_time >= 10 * 60)
1247 TouchSocketLockFile();
1248 last_touch_time = now;
1255 * Initialise the masks for select() for the ports we are listening on.
1256 * Return the number of sockets to listen on.
1259 initMasks(fd_set *rmask)
1266 for (i = 0; i < MAXLISTEN; i++)
1268 int fd = ListenSocket[i];
1282 * Read the startup packet and do something according to it.
1284 * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
1285 * not return at all.
1287 * (Note that ereport(FATAL) stuff is sent to the client, so only use it
1288 * if that's what you want. Return STATUS_ERROR if you don't want to
1289 * send anything to the client, which would typically be appropriate
1290 * if we detect a communications failure.)
1293 ProcessStartupPacket(Port *port, bool SSLdone)
1297 ProtocolVersion proto;
1298 MemoryContext oldcontext;
1300 if (pq_getbytes((char *) &len, 4) == EOF)
1303 * EOF after SSLdone probably means the client didn't like our
1304 * response to NEGOTIATE_SSL_CODE. That's not an error condition,
1305 * so don't clutter the log with a complaint.
1309 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1310 errmsg("incomplete startup packet")));
1311 return STATUS_ERROR;
1317 if (len < (int32) sizeof(ProtocolVersion) ||
1318 len > MAX_STARTUP_PACKET_LENGTH)
1321 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1322 errmsg("invalid length of startup packet")));
1323 return STATUS_ERROR;
1327 * Allocate at least the size of an old-style startup packet, plus one
1328 * extra byte, and make sure all are zeroes. This ensures we will
1329 * have null termination of all strings, in both fixed- and
1330 * variable-length packet layouts.
1332 if (len <= (int32) sizeof(StartupPacket))
1333 buf = palloc0(sizeof(StartupPacket) + 1);
1335 buf = palloc0(len + 1);
1337 if (pq_getbytes(buf, len) == EOF)
1340 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1341 errmsg("incomplete startup packet")));
1342 return STATUS_ERROR;
1346 * The first field is either a protocol version number or a special
1349 port->proto = proto = ntohl(*((ProtocolVersion *) buf));
1351 if (proto == CANCEL_REQUEST_CODE)
1353 processCancelRequest(port, buf);
1354 return 127; /* XXX */
1357 if (proto == NEGOTIATE_SSL_CODE && !SSLdone)
1362 /* No SSL when disabled or on Unix sockets */
1363 if (!EnableSSL || IS_AF_UNIX(port->laddr.addr.ss_family))
1366 SSLok = 'S'; /* Support for SSL */
1368 SSLok = 'N'; /* No support for SSL */
1370 if (send(port->sock, &SSLok, 1, 0) != 1)
1373 (errcode_for_socket_access(),
1374 errmsg("failed to send SSL negotiation response: %m")));
1375 return STATUS_ERROR; /* close the connection */
1379 if (SSLok == 'S' && secure_open_server(port) == -1)
1380 return STATUS_ERROR;
1382 /* regular startup packet, cancel, etc packet should follow... */
1383 /* but not another SSL negotiation request */
1384 return ProcessStartupPacket(port, true);
1387 /* Could add additional special packet types here */
1390 * Set FrontendProtocol now so that ereport() knows what format to
1391 * send if we fail during startup.
1393 FrontendProtocol = proto;
1395 /* Check we can handle the protocol the frontend is using. */
1397 if (PG_PROTOCOL_MAJOR(proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) ||
1398 PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) ||
1399 (PG_PROTOCOL_MAJOR(proto) == PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) &&
1400 PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST)))
1402 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1403 errmsg("unsupported frontend protocol %u.%u: server supports %u.0 to %u.%u",
1404 PG_PROTOCOL_MAJOR(proto), PG_PROTOCOL_MINOR(proto),
1405 PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST),
1406 PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST),
1407 PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST))));
1410 * Now fetch parameters out of startup packet and save them into the
1411 * Port structure. All data structures attached to the Port struct
1412 * must be allocated in TopMemoryContext so that they won't disappear
1413 * when we pass them to PostgresMain (see BackendRun). We need not
1414 * worry about leaking this storage on failure, since we aren't in the
1415 * postmaster process anymore.
1417 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
1419 if (PG_PROTOCOL_MAJOR(proto) >= 3)
1421 int32 offset = sizeof(ProtocolVersion);
1424 * Scan packet body for name/option pairs. We can assume any
1425 * string beginning within the packet body is null-terminated,
1426 * thanks to zeroing extra byte above.
1428 port->guc_options = NIL;
1430 while (offset < len)
1432 char *nameptr = ((char *) buf) + offset;
1436 if (*nameptr == '\0')
1437 break; /* found packet terminator */
1438 valoffset = offset + strlen(nameptr) + 1;
1439 if (valoffset >= len)
1440 break; /* missing value, will complain below */
1441 valptr = ((char *) buf) + valoffset;
1443 if (strcmp(nameptr, "database") == 0)
1444 port->database_name = pstrdup(valptr);
1445 else if (strcmp(nameptr, "user") == 0)
1446 port->user_name = pstrdup(valptr);
1447 else if (strcmp(nameptr, "options") == 0)
1448 port->cmdline_options = pstrdup(valptr);
1451 /* Assume it's a generic GUC option */
1452 port->guc_options = lappend(port->guc_options,
1454 port->guc_options = lappend(port->guc_options,
1457 offset = valoffset + strlen(valptr) + 1;
1461 * If we didn't find a packet terminator exactly at the end of the
1462 * given packet length, complain.
1464 if (offset != len - 1)
1466 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1467 errmsg("invalid startup packet layout: expected terminator as last byte")));
1472 * Get the parameters from the old-style, fixed-width-fields
1473 * startup packet as C strings. The packet destination was
1474 * cleared first so a short packet has zeros silently added. We
1475 * have to be prepared to truncate the pstrdup result for oversize
1478 StartupPacket *packet = (StartupPacket *) buf;
1480 port->database_name = pstrdup(packet->database);
1481 if (strlen(port->database_name) > sizeof(packet->database))
1482 port->database_name[sizeof(packet->database)] = '\0';
1483 port->user_name = pstrdup(packet->user);
1484 if (strlen(port->user_name) > sizeof(packet->user))
1485 port->user_name[sizeof(packet->user)] = '\0';
1486 port->cmdline_options = pstrdup(packet->options);
1487 if (strlen(port->cmdline_options) > sizeof(packet->options))
1488 port->cmdline_options[sizeof(packet->options)] = '\0';
1489 port->guc_options = NIL;
1492 /* Check a user name was given. */
1493 if (port->user_name == NULL || port->user_name[0] == '\0')
1495 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
1496 errmsg("no PostgreSQL user name specified in startup packet")));
1498 /* The database defaults to the user name. */
1499 if (port->database_name == NULL || port->database_name[0] == '\0')
1500 port->database_name = pstrdup(port->user_name);
1502 if (Db_user_namespace)
1505 * If user@, it is a global user, remove '@'. We only want to do
1506 * this if there is an '@' at the end and no earlier in the user
1507 * string or they may fake as a local user of another database
1508 * attaching to this database.
1510 if (strchr(port->user_name, '@') ==
1511 port->user_name + strlen(port->user_name) - 1)
1512 *strchr(port->user_name, '@') = '\0';
1515 /* Append '@' and dbname */
1518 db_user = palloc(strlen(port->user_name) +
1519 strlen(port->database_name) + 2);
1520 sprintf(db_user, "%s@%s", port->user_name, port->database_name);
1521 port->user_name = db_user;
1526 * Truncate given database and user names to length of a Postgres
1527 * name. This avoids lookup failures when overlength names are given.
1529 if (strlen(port->database_name) >= NAMEDATALEN)
1530 port->database_name[NAMEDATALEN - 1] = '\0';
1531 if (strlen(port->user_name) >= NAMEDATALEN)
1532 port->user_name[NAMEDATALEN - 1] = '\0';
1535 * Done putting stuff in TopMemoryContext.
1537 MemoryContextSwitchTo(oldcontext);
1540 * If we're going to reject the connection due to database state, say
1541 * so now instead of wasting cycles on an authentication exchange.
1542 * (This also allows a pg_ping utility to be written.)
1544 switch (port->canAcceptConnections)
1548 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1549 errmsg("the database system is starting up")));
1553 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1554 errmsg("the database system is shutting down")));
1558 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1559 errmsg("the database system is in recovery mode")));
1563 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
1564 errmsg("sorry, too many clients already")));
1576 * The client has sent a cancel request packet, not a normal
1577 * start-a-new-connection packet. Perform the necessary processing.
1578 * Nothing is sent back to the client.
1581 processCancelRequest(Port *port, void *pkt)
1583 CancelRequestPacket *canc = (CancelRequestPacket *) pkt;
1585 long cancelAuthCode;
1588 #ifndef EXEC_BACKEND
1595 backendPID = (int) ntohl(canc->backendPID);
1596 cancelAuthCode = (long) ntohl(canc->cancelAuthCode);
1599 * See if we have a matching backend. In the EXEC_BACKEND case, we
1600 * can no longer access the postmaster's own backend list, and must
1601 * rely on the duplicate array in shared memory.
1603 #ifndef EXEC_BACKEND
1604 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
1606 bp = (Backend *) DLE_VAL(curr);
1608 for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++)
1610 bp = (Backend *) &ShmemBackendArray[i];
1612 if (bp->pid == backendPID)
1614 if (bp->cancel_key == cancelAuthCode)
1616 /* Found a match; signal that backend to cancel current op */
1618 (errmsg_internal("processing cancel request: sending SIGINT to process %d",
1620 kill(bp->pid, SIGINT);
1623 /* Right PID, wrong key: no way, Jose */
1625 (errmsg_internal("bad key in cancel request for process %d",
1631 /* No matching backend */
1633 (errmsg_internal("bad pid in cancel request for process %d",
1638 * canAcceptConnections --- check to see if database state allows connections.
1640 static enum CAC_state
1641 canAcceptConnections(void)
1643 /* Can't start backends when in startup/shutdown/recovery state. */
1644 if (Shutdown > NoShutdown)
1645 return CAC_SHUTDOWN;
1649 return CAC_RECOVERY;
1652 * Don't start too many children.
1654 * We allow more connections than we can have backends here because some
1655 * might still be authenticating; they might fail auth, or some
1656 * existing backend might exit before the auth cycle is completed. The
1657 * exact MaxBackends limit is enforced when a new backend tries to
1658 * join the shared-inval backend array.
1660 if (CountChildren() >= 2 * MaxBackends)
1668 * ConnCreate -- create a local connection data structure
1671 ConnCreate(int serverFd)
1675 if (!(port = (Port *) calloc(1, sizeof(Port))))
1678 (errcode(ERRCODE_OUT_OF_MEMORY),
1679 errmsg("out of memory")));
1683 if (StreamConnection(serverFd, port) != STATUS_OK)
1685 StreamClose(port->sock);
1692 * Precompute password salt values to use for this connection.
1693 * It's slightly annoying to do this long in advance of knowing
1694 * whether we'll need 'em or not, but we must do the random()
1695 * calls before we fork, not after. Else the postmaster's random
1696 * sequence won't get advanced, and all backends would end up
1697 * using the same salt...
1699 RandomSalt(port->cryptSalt, port->md5Salt);
1707 * ConnFree -- free a local connection data structure
1710 ConnFree(Port *conn)
1720 * ClosePostmasterPorts -- close all the postmaster's open sockets
1722 * This is called during child process startup to release file descriptors
1723 * that are not needed by that child process. The postmaster still has
1724 * them open, of course.
1726 * Note: we pass am_syslogger as a boolean because we don't want to set
1727 * the global variable yet when this is called.
1730 ClosePostmasterPorts(bool am_syslogger)
1734 /* Close the listen sockets */
1735 for (i = 0; i < MAXLISTEN; i++)
1737 if (ListenSocket[i] != -1)
1739 StreamClose(ListenSocket[i]);
1740 ListenSocket[i] = -1;
1744 /* If using syslogger, close the read side of the pipe */
1748 if (syslogPipe[0] >= 0)
1749 close(syslogPipe[0]);
1753 CloseHandle(syslogPipe[0]);
1761 * reset_shared -- reset shared memory and semaphores
1764 reset_shared(unsigned short port)
1767 * Create or re-create shared memory and semaphores.
1769 * Note: in each "cycle of life" we will normally assign the same IPC
1770 * keys (if using SysV shmem and/or semas), since the port number is
1771 * used to determine IPC keys. This helps ensure that we will clean
1772 * up dead IPC objects if the postmaster crashes and is restarted.
1774 CreateSharedMemoryAndSemaphores(false, MaxBackends, port);
1779 * SIGHUP -- reread config files, and tell children to do same
1782 SIGHUP_handler(SIGNAL_ARGS)
1784 int save_errno = errno;
1786 PG_SETMASK(&BlockSig);
1788 if (Shutdown <= SmartShutdown)
1791 (errmsg("received SIGHUP, reloading configuration files")));
1792 ProcessConfigFile(PGC_SIGHUP);
1793 SignalChildren(SIGHUP);
1794 if (BgWriterPID != 0)
1795 kill(BgWriterPID, SIGHUP);
1797 kill(PgArchPID, SIGHUP);
1798 if (SysLoggerPID != 0)
1799 kill(SysLoggerPID, SIGHUP);
1800 /* PgStatPID does not currently need SIGHUP */
1805 /* Update the starting-point file for future children */
1806 write_nondefault_variables(PGC_SIGHUP);
1810 PG_SETMASK(&UnBlockSig);
1817 * pmdie -- signal handler for processing various postmaster signals.
1822 int save_errno = errno;
1824 PG_SETMASK(&BlockSig);
1827 (errmsg_internal("postmaster received signal %d",
1828 postgres_signal_arg)));
1830 switch (postgres_signal_arg)
1837 * Wait for children to end their work, then shut down.
1839 if (Shutdown >= SmartShutdown)
1841 Shutdown = SmartShutdown;
1843 (errmsg("received smart shutdown request")));
1845 if (DLGetHead(BackendList))
1846 break; /* let reaper() handle this */
1849 * No children left. Begin shutdown of data base system.
1851 if (StartupPID != 0 || FatalError)
1852 break; /* let reaper() handle this */
1853 /* Start the bgwriter if not running */
1854 if (BgWriterPID == 0)
1855 BgWriterPID = StartBackgroundWriter();
1856 /* And tell it to shut down */
1857 if (BgWriterPID != 0)
1858 kill(BgWriterPID, SIGUSR2);
1859 /* Tell pgarch to shut down too; nothing left for it to do */
1861 kill(PgArchPID, SIGQUIT);
1862 /* Tell pgstat to shut down too; nothing left for it to do */
1864 kill(PgStatPID, SIGQUIT);
1872 * Abort all children with SIGTERM (rollback active transactions
1873 * and exit) and shut down when they are gone.
1875 if (Shutdown >= FastShutdown)
1877 Shutdown = FastShutdown;
1879 (errmsg("received fast shutdown request")));
1881 if (DLGetHead(BackendList))
1886 (errmsg("aborting any active transactions")));
1887 SignalChildren(SIGTERM);
1888 /* reaper() does the rest */
1894 * No children left. Begin shutdown of data base system.
1896 * Note: if we previously got SIGTERM then we may send SIGUSR2 to
1897 * the bgwriter a second time here. This should be harmless.
1899 if (StartupPID != 0 || FatalError)
1900 break; /* let reaper() handle this */
1901 /* Start the bgwriter if not running */
1902 if (BgWriterPID == 0)
1903 BgWriterPID = StartBackgroundWriter();
1904 /* And tell it to shut down */
1905 if (BgWriterPID != 0)
1906 kill(BgWriterPID, SIGUSR2);
1907 /* Tell pgarch to shut down too; nothing left for it to do */
1909 kill(PgArchPID, SIGQUIT);
1910 /* Tell pgstat to shut down too; nothing left for it to do */
1912 kill(PgStatPID, SIGQUIT);
1918 * Immediate Shutdown:
1920 * abort all children with SIGQUIT and exit without attempt to
1921 * properly shut down data base system.
1924 (errmsg("received immediate shutdown request")));
1925 if (StartupPID != 0)
1926 kill(StartupPID, SIGQUIT);
1927 if (BgWriterPID != 0)
1928 kill(BgWriterPID, SIGQUIT);
1930 kill(PgArchPID, SIGQUIT);
1932 kill(PgStatPID, SIGQUIT);
1933 if (DLGetHead(BackendList))
1934 SignalChildren(SIGQUIT);
1939 PG_SETMASK(&UnBlockSig);
1945 * Reaper -- signal handler to cleanup after a backend (child) dies.
1950 int save_errno = errno;
1953 int status; /* backend exit status */
1957 union wait status; /* backend exit status */
1961 int pid; /* process id of dead backend */
1963 PG_SETMASK(&BlockSig);
1966 (errmsg_internal("reaping dead processes")));
1968 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1970 exitstatus = status;
1973 while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
1975 exitstatus = status.w_status;
1977 while ((pid = win32_waitpid(&exitstatus)) > 0)
1980 * We need to do this here, and not in CleanupBackend, since this
1981 * is to be called on all children when we are done with them.
1982 * Could move to LogChildExit, but that seems like asking for
1985 win32_RemoveChild(pid);
1987 #endif /* HAVE_WAITPID */
1990 * Check if this child was a startup process.
1992 if (StartupPID != 0 && pid == StartupPID)
1995 if (exitstatus != 0)
1997 LogChildExit(LOG, gettext("startup process"),
2000 (errmsg("aborting startup due to startup process failure")));
2005 * Startup succeeded - we are done with system startup or
2011 * Crank up the background writer. It doesn't matter if this
2012 * fails, we'll just try again later.
2014 Assert(BgWriterPID == 0);
2015 BgWriterPID = StartBackgroundWriter();
2018 * Go to shutdown mode if a shutdown request was pending.
2019 * Otherwise, try to start the archiver and stats collector
2022 if (Shutdown > NoShutdown && BgWriterPID != 0)
2023 kill(BgWriterPID, SIGUSR2);
2024 else if (Shutdown == NoShutdown)
2026 if (XLogArchivingActive() && PgArchPID == 0)
2027 PgArchPID = pgarch_start();
2029 PgStatPID = pgstat_start();
2036 * Was it the bgwriter?
2038 if (BgWriterPID != 0 && pid == BgWriterPID)
2041 if (exitstatus == 0 && Shutdown > NoShutdown &&
2042 !FatalError && !DLGetHead(BackendList))
2045 * Normal postmaster exit is here: we've seen normal exit
2046 * of the bgwriter after it's been told to shut down. We
2047 * expect that it wrote a shutdown checkpoint. (If for
2048 * some reason it didn't, recovery will occur on next
2049 * postmaster start.)
2051 * Note: we do not wait around for exit of the archiver or
2052 * stats processes. They've been sent SIGQUIT by this
2053 * point, and in any case contain logic to commit
2054 * hara-kiri if they notice the postmaster is gone.
2060 * Any unexpected exit of the bgwriter is treated as a crash.
2062 HandleChildCrash(pid, exitstatus,
2063 gettext("background writer process"));
2068 * Was it the archiver? If so, just try to start a new one; no
2069 * need to force reset of the rest of the system. (If fail, we'll
2070 * try again in future cycles of the main loop.)
2072 if (PgArchPID != 0 && pid == PgArchPID)
2075 if (exitstatus != 0)
2076 LogChildExit(LOG, gettext("archiver process"),
2078 if (XLogArchivingActive() &&
2079 StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
2080 PgArchPID = pgarch_start();
2085 * Was it the statistics collector? If so, just try to start a
2086 * new one; no need to force reset of the rest of the system. (If
2087 * fail, we'll try again in future cycles of the main loop.)
2089 if (PgStatPID != 0 && pid == PgStatPID)
2092 if (exitstatus != 0)
2093 LogChildExit(LOG, gettext("statistics collector process"),
2095 if (StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
2096 PgStatPID = pgstat_start();
2100 /* Was it the system logger? try to start a new one */
2101 if (SysLoggerPID != 0 && pid == SysLoggerPID)
2104 /* for safety's sake, launch new logger *first* */
2105 SysLoggerPID = SysLogger_Start();
2106 if (exitstatus != 0)
2107 LogChildExit(LOG, gettext("system logger process"),
2113 * Else do standard backend child cleanup.
2115 CleanupBackend(pid, exitstatus);
2116 } /* loop over pending child-death reports */
2121 * Wait for all important children to exit, then reset shmem and
2122 * StartupDataBase. (We can ignore the archiver and stats
2123 * processes here since they are not connected to shmem.)
2125 if (DLGetHead(BackendList) || StartupPID != 0 || BgWriterPID != 0)
2128 (errmsg("all server processes terminated; reinitializing")));
2131 reset_shared(PostPortNumber);
2133 StartupPID = StartupDataBase();
2138 if (Shutdown > NoShutdown)
2140 if (DLGetHead(BackendList) || StartupPID != 0)
2142 /* Start the bgwriter if not running */
2143 if (BgWriterPID == 0)
2144 BgWriterPID = StartBackgroundWriter();
2145 /* And tell it to shut down */
2146 if (BgWriterPID != 0)
2147 kill(BgWriterPID, SIGUSR2);
2148 /* Tell pgarch to shut down too; nothing left for it to do */
2150 kill(PgArchPID, SIGQUIT);
2151 /* Tell pgstat to shut down too; nothing left for it to do */
2153 kill(PgStatPID, SIGQUIT);
2157 PG_SETMASK(&UnBlockSig);
2164 * CleanupBackend -- cleanup after terminated backend.
2166 * Remove all local state associated with backend.
2169 CleanupBackend(int pid,
2170 int exitstatus) /* child's exit status. */
2174 LogChildExit(DEBUG2, gettext("server process"), pid, exitstatus);
2177 * If a backend dies in an ugly way (i.e. exit status not 0) then we
2178 * must signal all other backends to quickdie. If exit status is zero
2179 * we assume everything is hunky dory and simply remove the backend
2180 * from the active backend list.
2182 if (exitstatus != 0)
2184 HandleChildCrash(pid, exitstatus, gettext("server process"));
2188 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
2190 Backend *bp = (Backend *) DLE_VAL(curr);
2198 ShmemBackendArrayRemove(pid);
2200 /* Tell the collector about backend termination */
2208 * HandleChildCrash -- cleanup after failed backend or bgwriter.
2210 * The objectives here are to clean up our local state about the child
2211 * process, and to signal all other remaining children to quickdie.
2214 HandleChildCrash(int pid, int exitstatus, const char *procname)
2221 * Make log entry unless there was a previous crash (if so, nonzero
2222 * exit status is to be expected in SIGQUIT response; don't clutter
2227 LogChildExit(LOG, procname, pid, exitstatus);
2229 (errmsg("terminating any other active server processes")));
2232 /* Process regular backends */
2233 for (curr = DLGetHead(BackendList); curr; curr = next)
2235 next = DLGetSucc(curr);
2236 bp = (Backend *) DLE_VAL(curr);
2240 * Found entry for freshly-dead backend, so remove it.
2246 ShmemBackendArrayRemove(pid);
2248 /* Tell the collector about backend termination */
2250 /* Keep looping so we can signal remaining backends */
2255 * This backend is still alive. Unless we did so already,
2256 * tell it to commit hara-kiri.
2258 * SIGQUIT is the special signal that says exit without proc_exit
2259 * and let the user know what's going on. But if SendStop is
2260 * set (-s on command line), then we send SIGSTOP instead, so
2261 * that we can get core dumps from all backends by hand.
2266 (errmsg_internal("sending %s to process %d",
2267 (SendStop ? "SIGSTOP" : "SIGQUIT"),
2269 kill(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
2274 /* Take care of the bgwriter too */
2275 if (pid == BgWriterPID)
2277 else if (BgWriterPID != 0 && !FatalError)
2280 (errmsg_internal("sending %s to process %d",
2281 (SendStop ? "SIGSTOP" : "SIGQUIT"),
2282 (int) BgWriterPID)));
2283 kill(BgWriterPID, (SendStop ? SIGSTOP : SIGQUIT));
2286 /* Force a power-cycle of the pgarch process too */
2287 /* (Shouldn't be necessary, but just for luck) */
2288 if (PgArchPID != 0 && !FatalError)
2291 (errmsg_internal("sending %s to process %d",
2294 kill(PgArchPID, SIGQUIT);
2297 /* Force a power-cycle of the pgstat processes too */
2298 /* (Shouldn't be necessary, but just for luck) */
2299 if (PgStatPID != 0 && !FatalError)
2302 (errmsg_internal("sending %s to process %d",
2305 kill(PgStatPID, SIGQUIT);
2308 /* We do NOT restart the syslogger */
2314 * Log the death of a child process.
2317 LogChildExit(int lev, const char *procname, int pid, int exitstatus)
2319 if (WIFEXITED(exitstatus))
2323 * translator: %s is a noun phrase describing a child process,
2324 * such as "server process"
2326 (errmsg("%s (PID %d) exited with exit code %d",
2327 procname, pid, WEXITSTATUS(exitstatus))));
2328 else if (WIFSIGNALED(exitstatus))
2332 * translator: %s is a noun phrase describing a child process,
2333 * such as "server process"
2335 (errmsg("%s (PID %d) was terminated by signal %d",
2336 procname, pid, WTERMSIG(exitstatus))));
2341 * translator: %s is a noun phrase describing a child process,
2342 * such as "server process"
2344 (errmsg("%s (PID %d) exited with unexpected status %d",
2345 procname, pid, exitstatus)));
2349 * Send a signal to all backend children (but NOT special children)
2352 SignalChildren(int signal)
2356 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
2358 Backend *bp = (Backend *) DLE_VAL(curr);
2361 (errmsg_internal("sending signal %d to process %d",
2362 signal, (int) bp->pid)));
2363 kill(bp->pid, signal);
2368 * BackendStartup -- start backend process
2370 * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
2373 BackendStartup(Port *port)
2375 Backend *bn; /* for backend cleanup */
2378 #ifdef LINUX_PROFILE
2379 struct itimerval prof_itimer;
2383 * Compute the cancel key that will be assigned to this backend. The
2384 * backend will have its own copy in the forked-off process' value of
2385 * MyCancelKey, so that it can transmit the key to the frontend.
2387 MyCancelKey = PostmasterRandom();
2390 * Make room for backend data structure. Better before the fork() so
2391 * we can handle failure cleanly.
2393 bn = (Backend *) malloc(sizeof(Backend));
2397 (errcode(ERRCODE_OUT_OF_MEMORY),
2398 errmsg("out of memory")));
2399 return STATUS_ERROR;
2402 /* Pass down canAcceptConnections state (kluge for EXEC_BACKEND case) */
2403 port->canAcceptConnections = canAcceptConnections();
2406 * Flush stdio channels just before fork, to avoid double-output
2407 * problems. Ideally we'd use fflush(NULL) here, but there are still a
2408 * few non-ANSI stdio libraries out there (like SunOS 4.1.x) that
2409 * coredump if we do. Presently stdout and stderr are the only stdio
2410 * output channels used by the postmaster, so fflush'ing them should
2418 pid = backend_forkexec(port);
2420 #else /* !EXEC_BACKEND */
2422 #ifdef LINUX_PROFILE
2425 * Linux's fork() resets the profiling timer in the child process. If
2426 * we want to profile child processes then we need to save and restore
2427 * the timer setting. This is a waste of time if not profiling,
2428 * however, so only do it if commanded by specific -DLINUX_PROFILE
2431 getitimer(ITIMER_PROF, &prof_itimer);
2435 /* Specific beos actions before backend startup */
2436 beos_before_backend_startup();
2441 if (pid == 0) /* child */
2443 #ifdef LINUX_PROFILE
2444 setitimer(ITIMER_PROF, &prof_itimer, NULL);
2448 /* Specific beos backend startup actions */
2449 beos_backend_startup();
2453 proc_exit(BackendRun(port));
2455 #endif /* EXEC_BACKEND */
2459 /* in parent, fork failed */
2460 int save_errno = errno;
2463 /* Specific beos backend startup actions */
2464 beos_backend_startup_failed();
2469 (errmsg("could not fork new process for connection: %m")));
2470 report_fork_failure_to_client(port, save_errno);
2471 return STATUS_ERROR;
2474 /* in parent, successful fork */
2476 (errmsg_internal("forked new backend, pid=%d socket=%d",
2477 (int) pid, port->sock)));
2480 * Everything's been successful, it's safe to add this backend to our
2484 bn->cancel_key = MyCancelKey;
2485 DLAddHead(BackendList, DLNewElem(bn));
2487 ShmemBackendArrayAdd(bn);
2494 * Try to report backend fork() failure to client before we close the
2495 * connection. Since we do not care to risk blocking the postmaster on
2496 * this connection, we set the connection to non-blocking and try only once.
2498 * This is grungy special-purpose code; we cannot use backend libpq since
2499 * it's not up and running.
2502 report_fork_failure_to_client(Port *port, int errnum)
2506 /* Format the error message packet (always V2 protocol) */
2507 snprintf(buffer, sizeof(buffer), "E%s%s\n",
2508 gettext("could not fork new process for connection: "),
2511 /* Set port to non-blocking. Don't do send() if this fails */
2512 if (!set_noblock(port->sock))
2515 send(port->sock, buffer, strlen(buffer) + 1, 0);
2520 * split_opts -- split a string of options and append it to an argv array
2522 * NB: the string is destructively modified!
2524 * Since no current POSTGRES arguments require any quoting characters,
2525 * we can use the simple-minded tactic of assuming each set of space-
2526 * delimited characters is a separate argv element.
2528 * If you don't like that, well, we *used* to pass the whole option string
2529 * as ONE argument to execl(), which was even less intelligent...
2532 split_opts(char **argv, int *argcp, char *s)
2536 while (isspace((unsigned char) *s))
2540 argv[(*argcp)++] = s;
2541 while (*s && !isspace((unsigned char) *s))
2550 * BackendRun -- perform authentication, and if successful,
2551 * set up the backend's argument list and invoke PostgresMain()
2554 * Shouldn't return at all.
2555 * If PostgresMain() fails, return status.
2558 BackendRun(Port *port)
2561 char remote_host[NI_MAXHOST];
2562 char remote_port[NI_MAXSERV];
2563 char remote_ps_data[NI_MAXHOST];
2570 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
2573 * Let's clean up ourselves as the postmaster child, and close the
2574 * postmaster's listen sockets
2576 ClosePostmasterPorts(false);
2578 /* We don't want the postmaster's proc_exit() handlers */
2582 * Signal handlers setting is moved to tcop/postgres...
2585 /* Save port etc. for ps status */
2588 /* Reset MyProcPid to new backend's pid */
2589 MyProcPid = getpid();
2592 * PreAuthDelay is a debugging aid for investigating problems in the
2593 * authentication cycle: it can be set in postgresql.conf to allow
2594 * time to attach to the newly-forked backend with a debugger. (See
2595 * also the -W backend switch, which we allow clients to pass through
2596 * PGOPTIONS, but it is not honored until after authentication.)
2598 if (PreAuthDelay > 0)
2599 pg_usleep(PreAuthDelay * 1000000L);
2601 ClientAuthInProgress = true; /* limit visibility of log messages */
2603 /* save start time for end of session reporting */
2604 gettimeofday(&(port->session_start), NULL);
2606 /* set these to empty in case they are needed before we set them up */
2607 port->remote_host = "";
2608 port->remote_port = "";
2609 port->commandTag = "";
2612 * Initialize libpq and enable reporting of ereport errors to the
2613 * client. Must do this now because authentication uses libpq to send
2616 pq_init(); /* initialize libpq to talk to client */
2617 whereToSendOutput = Remote; /* now safe to ereport to client */
2620 * We arrange for a simple exit(0) if we receive SIGTERM or SIGQUIT
2621 * during any client authentication related communication. Otherwise
2622 * the postmaster cannot shutdown the database FAST or IMMED cleanly
2623 * if a buggy client blocks a backend during authentication.
2625 pqsignal(SIGTERM, authdie);
2626 pqsignal(SIGQUIT, authdie);
2627 pqsignal(SIGALRM, authdie);
2628 PG_SETMASK(&AuthBlockSig);
2631 * Get the remote host name and port for logging and status display.
2633 remote_host[0] = '\0';
2634 remote_port[0] = '\0';
2635 if (getnameinfo_all(&port->raddr.addr, port->raddr.salen,
2636 remote_host, sizeof(remote_host),
2637 remote_port, sizeof(remote_port),
2638 (log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV))
2640 int ret = getnameinfo_all(&port->raddr.addr, port->raddr.salen,
2641 remote_host, sizeof(remote_host),
2642 remote_port, sizeof(remote_port),
2643 NI_NUMERICHOST | NI_NUMERICSERV);
2647 (errmsg("getnameinfo_all() failed: %s",
2648 gai_strerror(ret))));
2650 snprintf(remote_ps_data, sizeof(remote_ps_data),
2651 remote_port[0] == '\0' ? "%s" : "%s(%s)",
2652 remote_host, remote_port);
2654 if (Log_connections)
2656 (errmsg("connection received: host=%s port=%s",
2657 remote_host, remote_port)));
2660 * save remote_host and remote_port in port stucture
2662 port->remote_host = strdup(remote_host);
2663 port->remote_port = strdup(remote_port);
2666 * In EXEC_BACKEND case, we didn't inherit the contents of pg_hba.c
2667 * etcetera from the postmaster, and have to load them ourselves.
2668 * Build the PostmasterContext (which didn't exist before, in this
2669 * process) to contain the data.
2671 * FIXME: [fork/exec] Ugh. Is there a way around this overhead?
2674 Assert(PostmasterContext == NULL);
2675 PostmasterContext = AllocSetContextCreate(TopMemoryContext,
2677 ALLOCSET_DEFAULT_MINSIZE,
2678 ALLOCSET_DEFAULT_INITSIZE,
2679 ALLOCSET_DEFAULT_MAXSIZE);
2680 MemoryContextSwitchTo(PostmasterContext);
2689 * Ready to begin client interaction. We will give up and exit(0)
2690 * after a time delay, so that a broken client can't hog a connection
2691 * indefinitely. PreAuthDelay doesn't count against the time limit.
2693 if (!enable_sig_alarm(AuthenticationTimeout * 1000, false))
2694 elog(FATAL, "could not set timer for authorization timeout");
2697 * Receive the startup packet (which might turn out to be a cancel
2700 status = ProcessStartupPacket(port, false);
2702 if (status != STATUS_OK)
2706 * Now that we have the user and database name, we can set the process
2707 * title for ps. It's good to do this as early as possible in
2710 init_ps_display(port->user_name, port->database_name, remote_ps_data);
2711 set_ps_display("authentication");
2714 * Now perform authentication exchange.
2716 ClientAuthentication(port); /* might not return, if failure */
2719 * Done with authentication. Disable timeout, and prevent
2720 * SIGTERM/SIGQUIT again until backend startup is complete.
2722 if (!disable_sig_alarm(false))
2723 elog(FATAL, "could not disable timer for authorization timeout");
2724 PG_SETMASK(&BlockSig);
2726 if (Log_connections)
2728 (errmsg("connection authorized: user=%s database=%s",
2729 port->user_name, port->database_name)));
2732 * Don't want backend to be able to see the postmaster random number
2733 * generator state. We have to clobber the static random_seed *and*
2734 * start a new random sequence in the random() library function.
2737 srandom((unsigned int) (MyProcPid ^ port->session_start.tv_usec));
2740 * Now, build the argv vector that will be given to PostgresMain.
2742 * The layout of the command line is
2743 * postgres [secure switches] -p databasename [insecure switches]
2744 * where the switches after -p come from the client request.
2746 * The maximum possible number of commandline arguments that could come
2747 * from ExtraOptions or port->cmdline_options is (strlen + 1) / 2; see
2751 maxac = 10; /* for fixed args supplied below */
2752 maxac += (strlen(ExtraOptions) + 1) / 2;
2753 if (port->cmdline_options)
2754 maxac += (strlen(port->cmdline_options) + 1) / 2;
2756 av = (char **) MemoryContextAlloc(TopMemoryContext,
2757 maxac * sizeof(char *));
2760 av[ac++] = "postgres";
2763 * Pass any backend switches specified with -o in the postmaster's own
2764 * command line. We assume these are secure. (It's OK to mangle
2765 * ExtraOptions now, since we're safely inside a subprocess.)
2767 split_opts(av, &ac, ExtraOptions);
2769 /* Tell the backend what protocol the frontend is using. */
2770 snprintf(protobuf, sizeof(protobuf), "-v%u", port->proto);
2771 av[ac++] = protobuf;
2774 * Tell the backend it is being called from the postmaster, and which
2775 * database to use. -p marks the end of secure switches.
2778 av[ac++] = port->database_name;
2781 * Pass the (insecure) option switches from the connection request.
2782 * (It's OK to mangle port->cmdline_options now.)
2784 if (port->cmdline_options)
2785 split_opts(av, &ac, port->cmdline_options);
2792 * Release postmaster's working memory context so that backend can
2793 * recycle the space. Note this does not trash *MyProcPort, because
2794 * ConnCreate() allocated that space with malloc() ... else we'd need
2795 * to copy the Port data here. Also, subsidiary data such as the
2796 * username isn't lost either; see ProcessStartupPacket().
2798 MemoryContextSwitchTo(TopMemoryContext);
2799 MemoryContextDelete(PostmasterContext);
2800 PostmasterContext = NULL;
2803 * Debug: print arguments being passed to backend
2806 (errmsg_internal("%s child[%d]: starting with (",
2807 progname, (int)getpid())));
2808 for (i = 0; i < ac; ++i)
2810 (errmsg_internal("\t%s", av[i])));
2812 (errmsg_internal(")")));
2814 ClientAuthInProgress = false; /* client_min_messages is active
2817 return (PostgresMain(ac, av, port->user_name));
2824 * postmaster_forkexec -- fork and exec a postmaster subprocess
2826 * The caller must have set up the argv array already, except for argv[2]
2827 * which will be filled with the name of the temp variable file.
2829 * Returns the child process PID, or -1 on fork failure (a suitable error
2830 * message has been logged on failure).
2832 * All uses of this routine will dispatch to SubPostmasterMain in the
2836 postmaster_forkexec(int argc, char *argv[])
2840 /* This entry point passes dummy values for the Port variables */
2841 memset(&port, 0, sizeof(port));
2842 return internal_forkexec(argc, argv, &port);
2846 * backend_forkexec -- fork/exec off a backend process
2848 * returns the pid of the fork/exec'd process, or -1 on failure
2851 backend_forkexec(Port *port)
2856 av[ac++] = "postgres";
2857 av[ac++] = "-forkbackend";
2858 av[ac++] = NULL; /* filled in by internal_forkexec */
2861 Assert(ac < lengthof(av));
2863 return internal_forkexec(ac, av, port);
2869 * internal_forkexec non-win32 implementation
2871 * - writes out backend variables to the parameter file
2872 * - fork():s, and then exec():s the child process
2875 internal_forkexec(int argc, char *argv[], Port *port)
2877 static unsigned long tmpBackendFileNum = 0;
2879 char tmpfilename[MAXPGPATH];
2880 BackendParameters param;
2883 if (!save_backend_variables(¶m, port))
2884 return -1; /* log made by save_backend_variables */
2886 /* Calculate name for temp file */
2888 snprintf(tmpfilename, MAXPGPATH, "%s/%s/%s.backend_var.%d.%lu",
2889 DataDir, PG_TEMP_FILES_DIR, PG_TEMP_FILE_PREFIX,
2890 MyProcPid, ++tmpBackendFileNum);
2893 fp = AllocateFile(tmpfilename, PG_BINARY_W);
2896 /* As per OpenTemporaryFile... */
2897 char dirname[MAXPGPATH];
2899 snprintf(dirname, MAXPGPATH, "%s/%s", DataDir, PG_TEMP_FILES_DIR);
2900 mkdir(dirname, S_IRWXU);
2902 fp = AllocateFile(tmpfilename, PG_BINARY_W);
2906 (errcode_for_file_access(),
2907 errmsg("could not create file \"%s\": %m",
2913 if (fwrite(¶m, sizeof(param), 1, fp) != 1)
2916 (errcode_for_file_access(),
2917 errmsg("could not write to file \"%s\": %m", tmpfilename)));
2926 (errcode_for_file_access(),
2927 errmsg("could not write to file \"%s\": %m", tmpfilename)));
2931 /* Make sure caller set up argv properly */
2933 Assert(argv[argc] == NULL);
2934 Assert(strncmp(argv[1], "-fork", 5) == 0);
2935 Assert(argv[2] == NULL);
2937 /* Insert temp file name after -fork argument */
2938 argv[2] = tmpfilename;
2940 /* Fire off execv in child */
2941 if ((pid = fork()) == 0)
2943 if (execv(postgres_exec_path, argv) < 0)
2946 (errmsg("could not execute server process \"%s\": %m",
2947 postgres_exec_path)));
2948 /* We're already in the child process here, can't return */
2953 return pid; /* Parent returns pid, or -1 on fork
2960 * internal_forkexec win32 implementation
2962 * - starts backend using CreateProcess(), in suspended state
2963 * - writes out backend variables to the parameter file
2964 * - during this, duplicates handles and sockets required for
2965 * inheritance into the new process
2966 * - resumes execution of the new process once the backend parameter
2970 internal_forkexec(int argc, char *argv[], Port *port)
2973 PROCESS_INFORMATION pi;
2976 char cmdLine[MAXPGPATH * 2];
2977 HANDLE childHandleCopy;
2978 HANDLE waiterThread;
2980 BackendParameters *param;
2981 SECURITY_ATTRIBUTES sa;
2982 char paramHandleStr[32];
2984 /* Make sure caller set up argv properly */
2986 Assert(argv[argc] == NULL);
2987 Assert(strncmp(argv[1], "-fork", 5) == 0);
2988 Assert(argv[2] == NULL);
2990 /* Set up shared memory for parameter passing */
2991 ZeroMemory(&sa,sizeof(sa));
2992 sa.nLength = sizeof(sa);
2993 sa.bInheritHandle = TRUE;
2994 paramHandle = CreateFileMapping(INVALID_HANDLE_VALUE,
2998 sizeof(BackendParameters),
3000 if (paramHandle == INVALID_HANDLE_VALUE)
3002 elog(LOG, "could not create backend parameter file mapping: error code %d",
3003 (int) GetLastError());
3007 param = MapViewOfFile(paramHandle, FILE_MAP_WRITE, 0, 0, sizeof(BackendParameters));
3010 elog(LOG, "could not map backend parameter memory: error code %d",
3011 (int) GetLastError());
3012 CloseHandle(paramHandle);
3016 /* Insert temp file name after -fork argument */
3017 sprintf(paramHandleStr, "%lu", (DWORD)paramHandle);
3018 argv[2] = paramHandleStr;
3020 /* Format the cmd line */
3021 cmdLine[sizeof(cmdLine) - 1] = '\0';
3022 cmdLine[sizeof(cmdLine) - 2] = '\0';
3023 snprintf(cmdLine, sizeof(cmdLine) - 1, "\"%s\"", postgres_exec_path);
3025 while (argv[++i] != NULL)
3027 j = strlen(cmdLine);
3028 snprintf(cmdLine + j, sizeof(cmdLine) - 1 - j, " \"%s\"", argv[i]);
3030 if (cmdLine[sizeof(cmdLine) - 2] != '\0')
3032 elog(LOG, "subprocess command line too long");
3036 memset(&pi, 0, sizeof(pi));
3037 memset(&si, 0, sizeof(si));
3040 * Create the subprocess in a suspended state. This will be resumed
3041 * later, once we have written out the parameter file.
3043 if (!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, CREATE_SUSPENDED,
3044 NULL, NULL, &si, &pi))
3046 elog(LOG, "CreateProcess call failed: %m (error code %d)",
3047 (int) GetLastError());
3051 if (!save_backend_variables(param, port, pi.hProcess, pi.dwProcessId))
3054 * log made by save_backend_variables, but we have to clean
3055 * up the mess with the half-started process
3057 if (!TerminateProcess(pi.hProcess, 255))
3059 (errmsg_internal("could not terminate unstarted process: error code %d",
3060 (int) GetLastError())));
3061 CloseHandle(pi.hProcess);
3062 CloseHandle(pi.hThread);
3063 return -1; /* log made by save_backend_variables */
3066 /* Drop the shared memory that is now inherited to the backend */
3067 if (!UnmapViewOfFile(param))
3068 elog(LOG, "could not unmap view of backend parameter file: error code %d",
3069 (int) GetLastError());
3070 if (!CloseHandle(paramHandle))
3071 elog(LOG, "could not close handle to backend parameter file: error code %d",
3072 (int) GetLastError());
3075 * Now that the backend variables are written out, we start the
3076 * child thread so it can start initializing while we set up
3077 * the rest of the parent state.
3079 if (ResumeThread(pi.hThread) == -1)
3081 if (!TerminateProcess(pi.hProcess, 255))
3084 (errmsg_internal("could not terminate unstartable process: error code %d",
3085 (int) GetLastError())));
3086 CloseHandle(pi.hProcess);
3087 CloseHandle(pi.hThread);
3090 CloseHandle(pi.hProcess);
3091 CloseHandle(pi.hThread);
3093 (errmsg_internal("could not resume thread of unstarted process: error code %d",
3094 (int) GetLastError())));
3098 if (!IsUnderPostmaster)
3100 /* We are the Postmaster creating a child... */
3101 win32_AddChild(pi.dwProcessId, pi.hProcess);
3104 /* Set up the thread to handle the SIGCHLD for this process */
3105 if (DuplicateHandle(GetCurrentProcess(),
3107 GetCurrentProcess(),
3111 DUPLICATE_SAME_ACCESS) == 0)
3113 (errmsg_internal("could not duplicate child handle: error code %d",
3114 (int) GetLastError())));
3116 waiterThread = CreateThread(NULL, 64 * 1024, win32_sigchld_waiter,
3117 (LPVOID) childHandleCopy, 0, NULL);
3120 (errmsg_internal("could not create sigchld waiter thread: error code %d",
3121 (int) GetLastError())));
3122 CloseHandle(waiterThread);
3124 if (IsUnderPostmaster)
3125 CloseHandle(pi.hProcess);
3126 CloseHandle(pi.hThread);
3128 return pi.dwProcessId;
3135 * SubPostmasterMain -- Get the fork/exec'd process into a state equivalent
3136 * to what it would be if we'd simply forked on Unix, and then
3137 * dispatch to the appropriate place.
3139 * The first two command line arguments are expected to be "-forkFOO"
3140 * (where FOO indicates which postmaster child we are to become), and
3141 * the name of a variables file that we can read to load data that would
3142 * have been inherited by fork() on Unix. Remaining arguments go to the
3143 * subprocess FooMain() routine.
3146 SubPostmasterMain(int argc, char *argv[])
3150 /* Do this sooner rather than later... */
3151 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
3153 MyProcPid = getpid(); /* reset MyProcPid */
3155 /* Read in file-based context */
3156 memset(&port, 0, sizeof(Port));
3157 read_backend_variables(argv[2], &port);
3160 * Start our win32 signal implementation. This has to be done
3161 * after we read the backend variables, because we need to pick
3162 * up the signal pipe from the parent process.
3165 pgwin32_signal_initialize();
3168 /* In EXEC_BACKEND case we will not have inherited these settings */
3169 IsPostmasterEnvironment = true;
3170 whereToSendOutput = None;
3172 PG_SETMASK(&BlockSig);
3174 /* Setup essential subsystems */
3175 MemoryContextInit();
3176 InitializeGUCOptions();
3178 /* Check we got appropriate args */
3180 elog(FATAL, "invalid subpostmaster invocation");
3182 /* Read in remaining GUC variables */
3183 read_nondefault_variables();
3185 /* Run backend or appropriate child */
3186 if (strcmp(argv[1], "-forkbackend") == 0)
3188 /* BackendRun will close sockets */
3190 /* Attach process to shared segments */
3191 CreateSharedMemoryAndSemaphores(false, MaxBackends, 0);
3195 * Need to reinitialize the SSL library in the backend,
3196 * since the context structures contain function pointers
3197 * and cannot be passed through the parameter file.
3200 secure_initialize();
3203 Assert(argc == 3); /* shouldn't be any more args */
3204 proc_exit(BackendRun(&port));
3206 if (strcmp(argv[1], "-forkboot") == 0)
3208 /* Close the postmaster's sockets */
3209 ClosePostmasterPorts(false);
3211 /* Attach process to shared segments */
3212 CreateSharedMemoryAndSemaphores(false, MaxBackends, 0);
3214 BootstrapMain(argc - 2, argv + 2);
3217 if (strcmp(argv[1], "-forkarch") == 0)
3219 /* Close the postmaster's sockets */
3220 ClosePostmasterPorts(false);
3222 /* Do not want to attach to shared memory */
3224 PgArchiverMain(argc, argv);
3227 if (strcmp(argv[1], "-forkbuf") == 0)
3229 /* Close the postmaster's sockets */
3230 ClosePostmasterPorts(false);
3232 /* Do not want to attach to shared memory */
3234 PgstatBufferMain(argc, argv);
3237 if (strcmp(argv[1], "-forkcol") == 0)
3240 * Do NOT close postmaster sockets here, because we are forking
3241 * from pgstat buffer process, which already did it.
3244 /* Do not want to attach to shared memory */
3246 PgstatCollectorMain(argc, argv);
3249 if (strcmp(argv[1], "-forklog") == 0)
3251 /* Close the postmaster's sockets */
3252 ClosePostmasterPorts(true);
3254 /* Do not want to attach to shared memory */
3256 SysLoggerMain(argc, argv);
3260 return 1; /* shouldn't get here */
3262 #endif /* EXEC_BACKEND */
3266 * ExitPostmaster -- cleanup
3268 * Do NOT call exit() directly --- always go through here!
3271 ExitPostmaster(int status)
3273 /* should cleanup shared memory and kill all backends */
3276 * Not sure of the semantics here. When the Postmaster dies, should
3277 * the backends all be killed? probably not.
3279 * MUST -- vadim 05-10-1999
3286 * sigusr1_handler - handle signal conditions from child processes
3289 sigusr1_handler(SIGNAL_ARGS)
3291 int save_errno = errno;
3293 PG_SETMASK(&BlockSig);
3295 if (CheckPostmasterSignal(PMSIGNAL_PASSWORD_CHANGE))
3298 * Password or group file has changed.
3304 if (CheckPostmasterSignal(PMSIGNAL_WAKEN_CHILDREN))
3307 * Send SIGUSR1 to all children (triggers
3308 * CatchupInterruptHandler). See storage/ipc/sinval[adt].c for the
3311 if (Shutdown <= SmartShutdown)
3312 SignalChildren(SIGUSR1);
3315 if (PgArchPID != 0 && Shutdown == NoShutdown)
3317 if (CheckPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER))
3320 * Send SIGUSR1 to archiver process, to wake it up and begin
3321 * archiving next transaction log file.
3323 kill(PgArchPID, SIGUSR1);
3327 PG_SETMASK(&UnBlockSig);
3334 * Dummy signal handler
3336 * We use this for signals that we don't actually use in the postmaster,
3337 * but we do use in backends. If we were to SIG_IGN such signals in the
3338 * postmaster, then a newly started backend might drop a signal that arrives
3339 * before it's able to reconfigure its signal processing. (See notes in
3343 dummy_handler(SIGNAL_ARGS)
3349 * CharRemap: given an int in range 0..61, produce textual encoding of it
3350 * per crypt(3) conventions.
3374 RandomSalt(char *cryptSalt, char *md5Salt)
3376 long rand = PostmasterRandom();
3378 cryptSalt[0] = CharRemap(rand % 62);
3379 cryptSalt[1] = CharRemap(rand / 62);
3382 * It's okay to reuse the first random value for one of the MD5 salt
3383 * bytes, since only one of the two salts will be sent to the client.
3384 * After that we need to compute more random bits.
3386 * We use % 255, sacrificing one possible byte value, so as to ensure
3387 * that all bits of the random() value participate in the result.
3388 * While at it, add one to avoid generating any null bytes.
3390 md5Salt[0] = (rand % 255) + 1;
3391 rand = PostmasterRandom();
3392 md5Salt[1] = (rand % 255) + 1;
3393 rand = PostmasterRandom();
3394 md5Salt[2] = (rand % 255) + 1;
3395 rand = PostmasterRandom();
3396 md5Salt[3] = (rand % 255) + 1;
3403 PostmasterRandom(void)
3405 static bool initialized = false;
3409 Assert(random_seed != 0);
3410 srandom(random_seed);
3418 * Count up number of child processes (regular backends only)
3426 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
3433 * StartChildProcess -- start a non-backend child process for the postmaster
3435 * xlog determines what kind of child will be started. All child types
3436 * initially go to BootstrapMain, which will handle common setup.
3438 * Return value of StartChildProcess is subprocess' PID, or 0 if failed
3439 * to start subprocess.
3442 StartChildProcess(int xlop)
3449 #ifdef LINUX_PROFILE
3450 struct itimerval prof_itimer;
3454 * Set up command-line arguments for subprocess
3456 av[ac++] = "postgres";
3459 av[ac++] = "-forkboot";
3460 av[ac++] = NULL; /* filled in by postmaster_forkexec */
3463 snprintf(xlbuf, sizeof(xlbuf), "-x%d", xlop);
3467 av[ac++] = "template1";
3470 Assert(ac < lengthof(av));
3473 * Flush stdio channels (see comments in BackendStartup)
3480 pid = postmaster_forkexec(ac, av);
3482 #else /* !EXEC_BACKEND */
3484 #ifdef LINUX_PROFILE
3485 /* see comments in BackendStartup */
3486 getitimer(ITIMER_PROF, &prof_itimer);
3490 /* Specific beos actions before backend startup */
3491 beos_before_backend_startup();
3496 if (pid == 0) /* child */
3498 #ifdef LINUX_PROFILE
3499 setitimer(ITIMER_PROF, &prof_itimer, NULL);
3503 /* Specific beos actions after backend startup */
3504 beos_backend_startup();
3507 IsUnderPostmaster = true; /* we are a postmaster subprocess
3510 /* Close the postmaster's sockets */
3511 ClosePostmasterPorts(false);
3513 /* Lose the postmaster's on-exit routines and port connections */
3516 /* Release postmaster's working memory context */
3517 MemoryContextSwitchTo(TopMemoryContext);
3518 MemoryContextDelete(PostmasterContext);
3519 PostmasterContext = NULL;
3521 BootstrapMain(ac, av);
3524 #endif /* EXEC_BACKEND */
3528 /* in parent, fork failed */
3529 int save_errno = errno;
3532 /* Specific beos actions before backend startup */
3533 beos_backend_startup_failed();
3538 case BS_XLOG_STARTUP:
3540 (errmsg("could not fork startup process: %m")));
3542 case BS_XLOG_BGWRITER:
3544 (errmsg("could not fork background writer process: %m")));
3548 (errmsg("could not fork process: %m")));
3553 * fork failure is fatal during startup, but there's no need to
3554 * choke immediately if starting other child types fails.
3556 if (xlop == BS_XLOG_STARTUP)
3562 * in parent, successful fork
3569 * Create the opts file
3572 CreateOptsFile(int argc, char *argv[], char *fullprogname)
3574 char filename[MAXPGPATH];
3578 snprintf(filename, sizeof(filename), "%s/postmaster.opts", DataDir);
3580 if ((fp = fopen(filename, "w")) == NULL)
3582 elog(LOG, "could not create file \"%s\": %m", filename);
3586 fprintf(fp, "%s", fullprogname);
3587 for (i = 1; i < argc; i++)
3588 fprintf(fp, " %s%s%s", SYSTEMQUOTE, argv[i], SYSTEMQUOTE);
3593 elog(LOG, "could not write file \"%s\": %m", filename);
3604 * The following need to be available to the save/restore_backend_variables
3607 extern slock_t *ShmemLock;
3608 extern slock_t *ShmemIndexLock;
3609 extern void *ShmemIndexAlloc;
3610 extern LWLock *LWLockArray;
3611 extern slock_t *ProcStructLock;
3612 extern int pgStatSock;
3613 extern int pgStatPipe[2];
3616 #define write_inheritable_socket(dest, src, childpid) (*(dest) = (src))
3617 #define read_inheritable_socket(dest, src) (*(dest) = *(src))
3619 static void write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE child);
3620 static void write_inheritable_socket(InheritableSocket *dest, SOCKET src,
3622 static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
3626 /* Save critical backend variables into the BackendParameters struct */
3629 save_backend_variables(BackendParameters *param, Port *port)
3632 save_backend_variables(BackendParameters *param, Port *port,
3633 HANDLE childProcess, pid_t childPid)
3636 memcpy(¶m->port, port, sizeof(Port));
3637 write_inheritable_socket(¶m->portsocket, port->sock, childPid);
3639 StrNCpy(param->DataDir, DataDir, MAXPGPATH);
3641 memcpy(¶m->ListenSocket, &ListenSocket, sizeof(ListenSocket));
3643 param->MyCancelKey = MyCancelKey;
3645 param->UsedShmemSegID = UsedShmemSegID;
3646 param->UsedShmemSegAddr = UsedShmemSegAddr;
3648 param->ShmemLock = ShmemLock;
3649 param->ShmemIndexLock = ShmemIndexLock;
3650 param->ShmemVariableCache = ShmemVariableCache;
3651 param->ShmemIndexAlloc = ShmemIndexAlloc;
3652 param->ShmemBackendArray = ShmemBackendArray;
3654 param->LWLockArray = LWLockArray;
3655 param->ProcStructLock = ProcStructLock;
3656 write_inheritable_socket(¶m->pgStatSock, pgStatSock, childPid);
3657 write_inheritable_socket(¶m->pgStatPipe0, pgStatPipe[0], childPid);
3658 write_inheritable_socket(¶m->pgStatPipe1, pgStatPipe[1], childPid);
3660 param->PostmasterPid = PostmasterPid;
3663 param->PostmasterHandle = PostmasterHandle;
3664 write_duplicated_handle(¶m->initial_signal_pipe,
3665 pgwin32_create_signal_listener(childPid),
3669 memcpy(¶m->syslogPipe, &syslogPipe, sizeof(syslogPipe));
3671 StrNCpy(param->my_exec_path, my_exec_path, MAXPGPATH);
3673 StrNCpy(param->ExtraOptions, ExtraOptions, MAXPGPATH);
3675 StrNCpy(param->lc_collate, setlocale(LC_COLLATE, NULL), MAXPGPATH);
3676 StrNCpy(param->lc_ctype, setlocale(LC_CTYPE, NULL), MAXPGPATH);
3684 * Duplicate a handle for usage in a child process, and write the child
3685 * process instance of the handle to the parameter file.
3688 write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE childProcess)
3690 HANDLE hChild = INVALID_HANDLE_VALUE;
3692 if (!DuplicateHandle(GetCurrentProcess(),
3698 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
3700 (errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %d",
3701 (int) GetLastError())));
3707 * Duplicate a socket for usage in a child process, and write the resulting
3708 * structure to the parameter file.
3709 * This is required because a number of LSPs (Layered Service Providers) very
3710 * common on Windows (antivirus, firewalls, download managers etc) break
3711 * straight socket inheritance.
3714 write_inheritable_socket(InheritableSocket *dest, SOCKET src, pid_t childpid)
3716 dest->origsocket = src;
3717 if (src != 0 && src != -1)
3720 if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
3722 (errmsg("could not duplicate socket %d for use in backend: error code %d",
3723 src, WSAGetLastError())));
3728 * Read a duplicate socket structure back, and get the socket descriptor.
3731 read_inheritable_socket(SOCKET *dest, InheritableSocket *src)
3735 if (src->origsocket == -1 || src->origsocket == 0)
3737 /* Not a real socket! */
3738 *dest = src->origsocket;
3742 /* Actual socket, so create from structure */
3743 s = WSASocket(FROM_PROTOCOL_INFO,
3749 if (s == INVALID_SOCKET)
3751 write_stderr("could not create inherited socket: error code %d\n",
3758 * To make sure we don't get two references to the same socket,
3759 * close the original one. (This would happen when inheritance
3762 closesocket(src->origsocket);
3768 read_backend_variables(char *id, Port *port)
3771 /* Non-win32 implementation reads from file */
3773 BackendParameters param;
3776 fp = AllocateFile(id, PG_BINARY_R);
3779 write_stderr("could not read from backend variables file \"%s\": %s\n",
3780 id, strerror(errno));
3784 if (fread(¶m, sizeof(param), 1, fp) != 1)
3786 write_stderr("could not read from backend variables file \"%s\": %s\n",
3787 id, strerror(errno));
3793 if (unlink(id) != 0)
3795 write_stderr("could not remove file \"%s\": %s\n",
3796 id, strerror(errno));
3800 restore_backend_variables(¶m, port);
3802 /* Win32 version uses mapped file */
3804 BackendParameters *param;
3806 paramHandle = (HANDLE)atol(id);
3807 param = MapViewOfFile(paramHandle, FILE_MAP_READ, 0, 0, 0);
3810 write_stderr("could not map view of backend variables: error code %d\n",
3811 (int) GetLastError());
3815 restore_backend_variables(param, port);
3817 if (!UnmapViewOfFile(param))
3819 write_stderr("could not unmap view of backend variables: error code %d\n",
3820 (int) GetLastError());
3824 if (!CloseHandle(paramHandle))
3826 write_stderr("could not close handle to backend parameter variables: error code %d\n",
3827 (int) GetLastError());
3833 /* Restore critical backend variables from the BackendParameters struct */
3835 restore_backend_variables(BackendParameters *param, Port *port)
3837 memcpy(port, ¶m->port, sizeof(Port));
3838 read_inheritable_socket(&port->sock, ¶m->portsocket);
3840 SetDataDir(param->DataDir);
3842 memcpy(&ListenSocket, ¶m->ListenSocket, sizeof(ListenSocket));
3844 MyCancelKey = param->MyCancelKey;
3846 UsedShmemSegID = param->UsedShmemSegID;
3847 UsedShmemSegAddr = param->UsedShmemSegAddr;
3849 ShmemLock = param->ShmemLock;
3850 ShmemIndexLock = param->ShmemIndexLock;
3851 ShmemVariableCache = param->ShmemVariableCache;
3852 ShmemIndexAlloc = param->ShmemIndexAlloc;
3853 ShmemBackendArray = param->ShmemBackendArray;
3855 LWLockArray = param->LWLockArray;
3856 ProcStructLock = param->ProcStructLock;
3857 read_inheritable_socket(&pgStatSock, ¶m->pgStatSock);
3858 read_inheritable_socket(&pgStatPipe[0], ¶m->pgStatPipe0);
3859 read_inheritable_socket(&pgStatPipe[1], ¶m->pgStatPipe1);
3861 PostmasterPid = param->PostmasterPid;
3864 PostmasterHandle = param->PostmasterHandle;
3865 pgwin32_initial_signal_pipe = param->initial_signal_pipe;
3868 memcpy(&syslogPipe, ¶m->syslogPipe, sizeof(syslogPipe));
3870 StrNCpy(my_exec_path, param->my_exec_path, MAXPGPATH);
3872 StrNCpy(ExtraOptions, param->ExtraOptions, MAXPGPATH);
3874 setlocale(LC_COLLATE, param->lc_collate);
3875 setlocale(LC_CTYPE, param->lc_ctype);
3880 ShmemBackendArraySize(void)
3882 return (NUM_BACKENDARRAY_ELEMS * sizeof(Backend));
3886 ShmemBackendArrayAllocation(void)
3888 size_t size = ShmemBackendArraySize();
3890 ShmemBackendArray = (Backend *) ShmemAlloc(size);
3891 /* Mark all slots as empty */
3892 memset(ShmemBackendArray, 0, size);
3896 ShmemBackendArrayAdd(Backend *bn)
3900 /* Find an empty slot */
3901 for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++)
3903 if (ShmemBackendArray[i].pid == 0)
3905 ShmemBackendArray[i] = *bn;
3911 (errmsg_internal("no free slots in shmem backend array")));
3915 ShmemBackendArrayRemove(pid_t pid)
3919 for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++)
3921 if (ShmemBackendArray[i].pid == pid)
3923 /* Mark the slot as empty */
3924 ShmemBackendArray[i].pid = 0;
3930 (errmsg_internal("could not find backend entry with pid %d",
3933 #endif /* EXEC_BACKEND */
3939 * Note: The following three functions must not be interrupted (eg. by
3940 * signals). As the Postgres Win32 signalling architecture (currently)
3941 * requires polling, or APC checking functions which aren't used here, this
3944 * We keep two separate arrays, instead of a single array of pid/HANDLE
3945 * structs, to avoid having to re-create a handle array for
3946 * WaitForMultipleObjects on each call to win32_waitpid.
3950 win32_AddChild(pid_t pid, HANDLE handle)
3952 Assert(win32_childPIDArray && win32_childHNDArray);
3953 if (win32_numChildren < NUM_BACKENDARRAY_ELEMS)
3955 win32_childPIDArray[win32_numChildren] = pid;
3956 win32_childHNDArray[win32_numChildren] = handle;
3957 ++win32_numChildren;
3961 (errmsg_internal("no room for child entry with pid %lu",
3962 (unsigned long) pid)));
3966 win32_RemoveChild(pid_t pid)
3970 Assert(win32_childPIDArray && win32_childHNDArray);
3972 for (i = 0; i < win32_numChildren; i++)
3974 if (win32_childPIDArray[i] == pid)
3976 CloseHandle(win32_childHNDArray[i]);
3978 /* Swap last entry into the "removed" one */
3979 --win32_numChildren;
3980 win32_childPIDArray[i] = win32_childPIDArray[win32_numChildren];
3981 win32_childHNDArray[i] = win32_childHNDArray[win32_numChildren];
3987 (errmsg_internal("could not find child entry with pid %lu",
3988 (unsigned long) pid)));
3992 win32_waitpid(int *exitstatus)
3995 * Note: Do NOT use WaitForMultipleObjectsEx, as we don't want to run
4001 unsigned long offset;
4003 Assert(win32_childPIDArray && win32_childHNDArray);
4004 elog(DEBUG3, "waiting on %lu children", win32_numChildren);
4006 for (offset = 0; offset < win32_numChildren; offset += MAXIMUM_WAIT_OBJECTS)
4008 unsigned long num = Min(MAXIMUM_WAIT_OBJECTS, win32_numChildren - offset);
4010 ret = WaitForMultipleObjects(num, &win32_childHNDArray[offset], FALSE, 0);
4015 (errmsg_internal("failed to wait on %lu of %lu children: error code %d",
4016 num, win32_numChildren, (int) GetLastError())));
4020 /* No children (in this chunk) have finished */
4026 * Get the exit code, and return the PID of, the
4027 * respective process
4029 index = offset + ret - WAIT_OBJECT_0;
4030 Assert(index >= 0 && index < win32_numChildren);
4031 if (!GetExitCodeProcess(win32_childHNDArray[index], &exitCode))
4034 * If we get this far, this should never happen, but,
4035 * then again... No choice other than to assume a
4036 * catastrophic failure.
4039 (errmsg_internal("failed to get exit code for child %lu",
4040 (DWORD)win32_childPIDArray[index])));
4042 *exitstatus = (int) exitCode;
4043 return win32_childPIDArray[index];
4047 /* No children have finished */
4052 * Note! Code below executes on separate threads, one for
4053 * each child process created
4056 win32_sigchld_waiter(LPVOID param)
4058 HANDLE procHandle = (HANDLE) param;
4060 DWORD r = WaitForSingleObject(procHandle, INFINITE);
4062 if (r == WAIT_OBJECT_0)
4063 pg_queue_signal(SIGCHLD);
4065 write_stderr("could not wait on child process handle: error code %d\n",
4066 (int) GetLastError());
4067 CloseHandle(procHandle);