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-2005, 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.444 2005/02/20 02:21:54 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_control.h"
96 #include "catalog/pg_database.h"
97 #include "commands/async.h"
98 #include "lib/dllist.h"
99 #include "libpq/auth.h"
100 #include "libpq/crypt.h"
101 #include "libpq/libpq.h"
102 #include "libpq/pqcomm.h"
103 #include "libpq/pqsignal.h"
104 #include "miscadmin.h"
105 #include "nodes/nodes.h"
106 #include "postmaster/postmaster.h"
107 #include "postmaster/pgarch.h"
108 #include "postmaster/syslogger.h"
109 #include "storage/fd.h"
110 #include "storage/ipc.h"
111 #include "storage/pg_shmem.h"
112 #include "storage/pmsignal.h"
113 #include "storage/proc.h"
114 #include "storage/bufmgr.h"
115 #include "access/xlog.h"
116 #include "tcop/tcopprot.h"
117 #include "utils/builtins.h"
118 #include "utils/guc.h"
119 #include "utils/memutils.h"
120 #include "utils/ps_status.h"
121 #include "bootstrap/bootstrap.h"
125 #include "storage/spin.h"
130 * List of active backends (or child processes anyway; we don't actually
131 * know whether a given child has become a backend or is still in the
132 * authorization phase). This is used mainly to keep track of how many
133 * children we have and send them appropriate signals when necessary.
135 * "Special" children such as the startup and bgwriter tasks are not in
140 pid_t pid; /* process id of backend */
141 long cancel_key; /* cancel key for cancels for this backend */
144 static Dllist *BackendList;
147 #define NUM_BACKENDARRAY_ELEMS (2*MaxBackends)
148 static Backend *ShmemBackendArray;
151 /* The socket number we are listening for connections on */
154 char *ListenAddresses;
157 * ReservedBackends is the number of backends reserved for superuser use.
158 * This number is taken out of the pool size given by MaxBackends so
159 * number of backend slots available to non-superusers is
160 * (MaxBackends - ReservedBackends). Note what this really means is
161 * "if there are <= ReservedBackends connections available, only superusers
162 * can make new connections" --- pre-existing superuser connections don't
163 * count against the limit.
165 int ReservedBackends;
168 static const char *progname = NULL;
170 /* The socket(s) we're listening to. */
172 static int ListenSocket[MAXLISTEN];
175 * Set by the -o option
177 static char ExtraOptions[MAXPGPATH];
180 * These globals control the behavior of the postmaster in case some
181 * backend dumps core. Normally, it kills all peers of the dead backend
182 * and reinitializes shared memory. By specifying -s or -n, we can have
183 * the postmaster stop (rather than kill) peers and not reinitialize
184 * shared data structures.
186 static bool Reinit = true;
187 static int SendStop = false;
189 /* still more option variables */
190 bool EnableSSL = false;
191 bool SilentMode = false; /* silent mode (-S) */
193 int PreAuthDelay = 0;
194 int AuthenticationTimeout = 60;
196 bool log_hostname; /* for ps display and logging */
197 bool Log_connections = false;
198 bool Db_user_namespace = false;
200 char *rendezvous_name;
202 /* list of library:init-function to be preloaded */
203 char *preload_libraries_string = NULL;
205 /* PIDs of special child processes; 0 when not running */
206 static pid_t StartupPID = 0,
212 /* Startup/shutdown state */
214 #define SmartShutdown 1
215 #define FastShutdown 2
217 static int Shutdown = NoShutdown;
219 static bool FatalError = false; /* T if recovering from backend crash */
221 bool ClientAuthInProgress = false; /* T during new-client
225 * State for assigning random salts and cancel keys.
226 * Also, the global MyCancelKey passes the cancel key assigned to a given
227 * backend from the postmaster to that backend (via fork).
229 static unsigned int random_seed = 0;
235 #ifdef HAVE_INT_OPTRESET
240 * postmaster.c - function prototypes
242 static void checkDataDir(void);
244 #ifdef USE_RENDEZVOUS
245 static void reg_reply(DNSServiceRegistrationReplyErrorType errorCode,
248 static void pmdaemonize(void);
249 static Port *ConnCreate(int serverFd);
250 static void ConnFree(Port *port);
251 static void reset_shared(unsigned short port);
252 static void SIGHUP_handler(SIGNAL_ARGS);
253 static void pmdie(SIGNAL_ARGS);
254 static void reaper(SIGNAL_ARGS);
255 static void sigusr1_handler(SIGNAL_ARGS);
256 static void dummy_handler(SIGNAL_ARGS);
257 static void CleanupBackend(int pid, int exitstatus);
258 static void HandleChildCrash(int pid, int exitstatus, const char *procname);
259 static void LogChildExit(int lev, const char *procname,
260 int pid, int exitstatus);
261 static int BackendRun(Port *port);
262 static void ExitPostmaster(int status);
263 static void usage(const char *);
264 static int ServerLoop(void);
265 static int BackendStartup(Port *port);
266 static int ProcessStartupPacket(Port *port, bool SSLdone);
267 static void processCancelRequest(Port *port, void *pkt);
268 static int initMasks(fd_set *rmask);
269 static void report_fork_failure_to_client(Port *port, int errnum);
270 static enum CAC_state canAcceptConnections(void);
271 static long PostmasterRandom(void);
272 static void RandomSalt(char *cryptSalt, char *md5Salt);
273 static void SignalChildren(int signal);
274 static int CountChildren(void);
275 static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
276 static pid_t StartChildProcess(int xlop);
281 static void win32_AddChild(pid_t pid, HANDLE handle);
282 static void win32_RemoveChild(pid_t pid);
283 static pid_t win32_waitpid(int *exitstatus);
284 static DWORD WINAPI win32_sigchld_waiter(LPVOID param);
286 static pid_t *win32_childPIDArray;
287 static HANDLE *win32_childHNDArray;
288 static unsigned long win32_numChildren = 0;
290 HANDLE PostmasterHandle;
293 static pid_t backend_forkexec(Port *port);
294 static pid_t internal_forkexec(int argc, char *argv[], Port *port);
296 /* Type for a socket that can be inherited to a client process */
300 SOCKET origsocket; /* Original socket value, or -1 if not a socket */
301 WSAPROTOCOL_INFO wsainfo;
304 typedef int InheritableSocket;
307 typedef struct LWLock LWLock; /* ugly kluge */
310 * Structure contains all variables passed to exec:ed backends
315 InheritableSocket portsocket;
316 char DataDir[MAXPGPATH];
317 int ListenSocket[MAXLISTEN];
319 unsigned long UsedShmemSegID;
320 void *UsedShmemSegAddr;
322 slock_t *ShmemIndexLock;
323 VariableCache ShmemVariableCache;
324 void *ShmemIndexAlloc;
325 Backend *ShmemBackendArray;
327 slock_t *ProcStructLock;
328 InheritableSocket pgStatSock;
329 InheritableSocket pgStatPipe0;
330 InheritableSocket pgStatPipe1;
333 HANDLE PostmasterHandle;
334 HANDLE initial_signal_pipe;
335 HANDLE syslogPipe[2];
339 char my_exec_path[MAXPGPATH];
340 char ExtraOptions[MAXPGPATH];
341 char lc_collate[LOCALE_NAME_BUFLEN];
342 char lc_ctype[LOCALE_NAME_BUFLEN];
345 static void read_backend_variables(char *id, Port *port);
346 static void restore_backend_variables(BackendParameters *param, Port *port);
348 static bool save_backend_variables(BackendParameters *param, Port *port);
350 static bool save_backend_variables(BackendParameters *param, Port *port,
351 HANDLE childProcess, pid_t childPid);
354 static void ShmemBackendArrayAdd(Backend *bn);
355 static void ShmemBackendArrayRemove(pid_t pid);
357 #endif /* EXEC_BACKEND */
359 #define StartupDataBase() StartChildProcess(BS_XLOG_STARTUP)
360 #define StartBackgroundWriter() StartChildProcess(BS_XLOG_BGWRITER)
364 * Postmaster main entry point
367 PostmasterMain(int argc, char *argv[])
371 char *userDoption = NULL;
374 /* This will call exit() if strdup() fails. */
375 progname = get_progname(argv[0]);
377 MyProcPid = PostmasterPid = getpid();
379 IsPostmasterEnvironment = true;
382 * Catch standard options before doing much else. This even works on
383 * systems without getopt_long.
387 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
392 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
394 puts("postmaster (PostgreSQL) " PG_VERSION);
400 /* Start our win32 signal implementation */
401 pgwin32_signal_initialize();
405 * for security, no dir or file created can be group or other
408 umask((mode_t) 0077);
411 * Fire up essential subsystems: memory management
416 * By default, palloc() requests in the postmaster will be allocated
417 * in the PostmasterContext, which is space that can be recycled by
418 * backends. Allocated data that needs to be available to backends
419 * should be allocated in TopMemoryContext.
421 PostmasterContext = AllocSetContextCreate(TopMemoryContext,
423 ALLOCSET_DEFAULT_MINSIZE,
424 ALLOCSET_DEFAULT_INITSIZE,
425 ALLOCSET_DEFAULT_MAXSIZE);
426 MemoryContextSwitchTo(PostmasterContext);
428 IgnoreSystemIndexes(false);
430 if (find_my_exec(argv[0], my_exec_path) < 0)
431 elog(FATAL, "%s: could not locate my own executable path",
434 get_pkglib_path(my_exec_path, pkglib_path);
439 InitializeGUCOptions();
443 while ((opt = getopt(argc, argv, "A:a:B:b:c:D:d:Fh:ik:lm:MN:no:p:Ss-:")) != -1)
448 #ifdef USE_ASSERT_CHECKING
449 SetConfigOption("debug_assertions", optarg, PGC_POSTMASTER, PGC_S_ARGV);
451 write_stderr("%s: assert checking is not compiled in\n", progname);
455 /* Can no longer set authentication method. */
458 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
461 /* Can no longer set the backend executable file to use. */
464 userDoption = optarg;
467 set_debug_options(atoi(optarg), PGC_POSTMASTER, PGC_S_ARGV);
470 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
473 SetConfigOption("listen_addresses", optarg, PGC_POSTMASTER, PGC_S_ARGV);
476 SetConfigOption("listen_addresses", "*", PGC_POSTMASTER, PGC_S_ARGV);
479 SetConfigOption("unix_socket_directory", optarg, PGC_POSTMASTER, PGC_S_ARGV);
483 SetConfigOption("ssl", "true", PGC_POSTMASTER, PGC_S_ARGV);
487 /* Multiplexed backends no longer supported. */
492 * ignore this flag. This may be passed in because the
493 * program was run as 'postgres -M' instead of
498 /* The max number of backends to start. */
499 SetConfigOption("max_connections", optarg, PGC_POSTMASTER, PGC_S_ARGV);
502 /* Don't reinit shared mem after abnormal exit */
508 * Other options to pass to the backend on the command
511 snprintf(ExtraOptions + strlen(ExtraOptions),
512 sizeof(ExtraOptions) - strlen(ExtraOptions),
516 SetConfigOption("port", optarg, PGC_POSTMASTER, PGC_S_ARGV);
521 * Start in 'S'ilent mode (disassociate from controlling
522 * tty). You may also think of this as 'S'ysV mode since
523 * it's most badly needed on SysV-derived systems like
526 SetConfigOption("silent_mode", "true", PGC_POSTMASTER, PGC_S_ARGV);
531 * In the event that some backend dumps core, send
532 * SIGSTOP, rather than SIGQUIT, to all its peers. This
533 * lets the wily post_hacker collect core dumps from
544 ParseLongOption(optarg, &name, &value);
549 (errcode(ERRCODE_SYNTAX_ERROR),
550 errmsg("--%s requires a value",
554 (errcode(ERRCODE_SYNTAX_ERROR),
555 errmsg("-c %s requires a value",
559 SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
567 write_stderr("Try \"%s --help\" for more information.\n",
574 * Postmaster accepts no non-option switch arguments.
578 write_stderr("%s: invalid argument: \"%s\"\n",
579 progname, argv[optind]);
580 write_stderr("Try \"%s --help\" for more information.\n",
586 * Locate the proper configuration files and data directory, and
587 * read postgresql.conf for the first time.
589 if (!SelectConfigFiles(userDoption, progname))
592 /* Verify that DataDir looks reasonable */
596 * Check for invalid combinations of GUC settings.
598 if (NBuffers < 2 * MaxBackends || NBuffers < 16)
601 * Do not accept -B so small that backends are likely to starve
602 * for lack of buffers. The specific choices here are somewhat
605 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);
609 if (ReservedBackends >= MaxBackends)
611 write_stderr("%s: superuser_reserved_connections must be less than max_connections\n", progname);
616 * Other one-time internal sanity checks can go here.
618 if (!CheckDateTokenTables())
620 write_stderr("%s: invalid datetoken tables, please fix\n", progname);
625 * Now that we are done processing the postmaster arguments, reset
626 * getopt(3) library so that it will work correctly in subprocesses.
629 #ifdef HAVE_INT_OPTRESET
630 optreset = 1; /* some systems need this too */
633 /* For debugging: display postmaster environment */
635 extern char **environ;
639 (errmsg_internal("%s: PostmasterMain: initial environ dump:",
642 (errmsg_internal("-----------------------------------------")));
643 for (p = environ; *p; ++p)
645 (errmsg_internal("\t%s", *p)));
647 (errmsg_internal("-----------------------------------------")));
651 if (find_other_exec(argv[0], "postgres", PG_VERSIONSTR,
652 postgres_exec_path) < 0)
654 (errmsg("%s: could not locate matching postgres executable",
659 * Initialize SSL library, if specified.
667 * process any libraries that should be preloaded and optionally
670 if (preload_libraries_string)
671 process_preload_libraries(preload_libraries_string);
674 * Fork away from controlling terminal, if -S specified.
676 * Must do this before we grab any interlock files, else the interlocks
677 * will show the wrong PID.
683 * Create lockfile for data directory.
685 * We want to do this before we try to grab the input sockets, because
686 * the data directory interlock is more reliable than the socket-file
687 * interlock (thanks to whoever decided to put socket files in /tmp
688 * :-(). For the same reason, it's best to grab the TCP socket(s)
689 * before the Unix socket.
691 CreateDataDirLockFile(DataDir, true);
694 * Remove old temporary files. At this point there can be no other
695 * Postgres processes running in this directory, so this should be
701 * Establish input sockets.
703 for (i = 0; i < MAXLISTEN; i++)
704 ListenSocket[i] = -1;
712 /* Need a modifiable copy of ListenAddresses */
713 rawstring = pstrdup(ListenAddresses);
715 /* Parse string into list of identifiers */
716 if (!SplitIdentifierString(rawstring, ',', &elemlist))
718 /* syntax error in list */
720 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
721 errmsg("invalid list syntax for \"listen_addresses\"")));
726 char *curhost = (char *) lfirst(l);
728 if (strcmp(curhost, "*") == 0)
729 status = StreamServerPort(AF_UNSPEC, NULL,
730 (unsigned short) PostPortNumber,
732 ListenSocket, MAXLISTEN);
734 status = StreamServerPort(AF_UNSPEC, curhost,
735 (unsigned short) PostPortNumber,
737 ListenSocket, MAXLISTEN);
738 if (status != STATUS_OK)
740 (errmsg("could not create listen socket for \"%s\"",
748 #ifdef USE_RENDEZVOUS
749 /* Register for Rendezvous only if we opened TCP socket(s) */
750 if (ListenSocket[0] != -1 && rendezvous_name != NULL)
752 DNSServiceRegistrationCreate(rendezvous_name,
755 htonl(PostPortNumber),
757 (DNSServiceRegistrationReply) reg_reply,
762 #ifdef HAVE_UNIX_SOCKETS
763 status = StreamServerPort(AF_UNIX, NULL,
764 (unsigned short) PostPortNumber,
766 ListenSocket, MAXLISTEN);
767 if (status != STATUS_OK)
769 (errmsg("could not create Unix-domain socket")));
773 * check that we have some socket to listen on
775 if (ListenSocket[0] == -1)
777 (errmsg("no socket created for listening")));
782 * Set up shared memory and semaphores.
784 reset_shared(PostPortNumber);
787 * Estimate number of openable files. This must happen after setting
788 * up semaphores, because on some platforms semaphores count as open
794 * Initialize the list of active backends.
796 BackendList = DLNewList();
801 * Initialize the child pid/HANDLE arrays for signal handling.
803 win32_childPIDArray = (pid_t *)
804 malloc(NUM_BACKENDARRAY_ELEMS * sizeof(pid_t));
805 win32_childHNDArray = (HANDLE *)
806 malloc(NUM_BACKENDARRAY_ELEMS * sizeof(HANDLE));
807 if (!win32_childPIDArray || !win32_childHNDArray)
809 (errcode(ERRCODE_OUT_OF_MEMORY),
810 errmsg("out of memory")));
813 * Set up a handle that child processes can use to check whether the
814 * postmaster is still running.
816 if (DuplicateHandle(GetCurrentProcess(),
822 DUPLICATE_SAME_ACCESS) == 0)
824 (errmsg_internal("could not duplicate postmaster handle: error code %d",
825 (int) GetLastError())));
829 * Record postmaster options. We delay this till now to avoid
830 * recording bogus options (eg, NBuffers too high for available
833 if (!CreateOptsFile(argc, argv, my_exec_path))
837 write_nondefault_variables(PGC_POSTMASTER);
841 * Write the external PID file if requested
843 if (external_pid_file)
845 FILE *fpidfile = fopen(external_pid_file, "w");
849 fprintf(fpidfile, "%d\n", MyProcPid);
851 /* Should we remove the pid file on postmaster exit? */
854 write_stderr("%s: could not write external PID file \"%s\": %s\n",
855 progname, external_pid_file, strerror(errno));
859 * Set up signal handlers for the postmaster process.
861 * CAUTION: when changing this list, check for side-effects on the signal
862 * handling setup of child processes. See tcop/postgres.c,
863 * bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/pgarch.c,
864 * postmaster/pgstat.c, and postmaster/syslogger.c.
867 PG_SETMASK(&BlockSig);
869 pqsignal(SIGHUP, SIGHUP_handler); /* reread config file and have
870 * children do same */
871 pqsignal(SIGINT, pmdie); /* send SIGTERM and shut down */
872 pqsignal(SIGQUIT, pmdie); /* send SIGQUIT and die */
873 pqsignal(SIGTERM, pmdie); /* wait for children and shut down */
874 pqsignal(SIGALRM, SIG_IGN); /* ignored */
875 pqsignal(SIGPIPE, SIG_IGN); /* ignored */
876 pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
877 pqsignal(SIGUSR2, dummy_handler); /* unused, reserve for children */
878 pqsignal(SIGCHLD, reaper); /* handle child termination */
879 pqsignal(SIGTTIN, SIG_IGN); /* ignored */
880 pqsignal(SIGTTOU, SIG_IGN); /* ignored */
881 /* ignore SIGXFSZ, so that ulimit violations work like disk full */
883 pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
887 * If enabled, start up syslogger collection subprocess
889 SysLoggerPID = SysLogger_Start();
892 * Reset whereToSendOutput from Debug (its starting state) to None.
893 * This stops ereport from sending log messages to stderr unless
894 * Log_destination permits. We don't do this until the postmaster is
895 * fully launched, since startup failures may as well be reported to
898 whereToSendOutput = None;
901 * Initialize the statistics collector stuff
906 * Load configuration files for client authentication.
912 * We're ready to rock and roll...
914 StartupPID = StartupDataBase();
916 status = ServerLoop();
919 * ServerLoop probably shouldn't ever return, but if it does, close
922 ExitPostmaster(status != STATUS_OK);
924 return 0; /* not reached */
929 * Validate the proposed data directory
934 char path[MAXPGPATH];
936 struct stat stat_buf;
940 if (stat(DataDir, &stat_buf) != 0)
944 (errcode_for_file_access(),
945 errmsg("data directory \"%s\" does not exist",
949 (errcode_for_file_access(),
950 errmsg("could not read permissions of directory \"%s\": %m",
955 * Check if the directory has group or world access. If so, reject.
957 * XXX temporarily suppress check when on Windows, because there may not
958 * be proper support for Unix-y file permissions. Need to think of a
959 * reasonable check to apply on Windows.
961 #if !defined(WIN32) && !defined(__CYGWIN__)
962 if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
964 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
965 errmsg("data directory \"%s\" has group or world access",
967 errdetail("Permissions should be u=rwx (0700).")));
970 /* Look for PG_VERSION before looking for pg_control */
971 ValidatePgVersion(DataDir);
973 snprintf(path, sizeof(path), "%s/global/pg_control", DataDir);
975 fp = AllocateFile(path, PG_BINARY_R);
978 write_stderr("%s: could not find the database system\n"
979 "Expected to find it in the directory \"%s\",\n"
980 "but could not open file \"%s\": %s\n",
981 progname, DataDir, path, strerror(errno));
988 #ifdef USE_RENDEZVOUS
991 * empty callback function for DNSServiceRegistrationCreate()
994 reg_reply(DNSServiceRegistrationReplyErrorType errorCode, void *context)
998 #endif /* USE_RENDEZVOUS */
1002 * Fork away from the controlling terminal (-S option)
1011 #ifdef LINUX_PROFILE
1012 struct itimerval prof_itimer;
1015 #ifdef LINUX_PROFILE
1016 /* see comments in BackendStartup */
1017 getitimer(ITIMER_PROF, &prof_itimer);
1021 if (pid == (pid_t) -1)
1023 write_stderr("%s: could not fork background process: %s\n",
1024 progname, strerror(errno));
1029 /* Parent should just exit, without doing any atexit cleanup */
1033 #ifdef LINUX_PROFILE
1034 setitimer(ITIMER_PROF, &prof_itimer, NULL);
1037 MyProcPid = PostmasterPid = getpid(); /* reset PID vars to child */
1039 /* GH: If there's no setsid(), we hopefully don't need silent mode.
1040 * Until there's a better solution.
1045 write_stderr("%s: could not dissociate from controlling TTY: %s\n",
1046 progname, strerror(errno));
1050 i = open(NULL_DEV, O_RDWR);
1057 elog(FATAL, "SilentMode not supported under WIN32");
1063 * Print out help message
1066 usage(const char *progname)
1068 printf(gettext("%s is the PostgreSQL server.\n\n"), progname);
1069 printf(gettext("Usage:\n %s [OPTION]...\n\n"), progname);
1070 printf(gettext("Options:\n"));
1071 #ifdef USE_ASSERT_CHECKING
1072 printf(gettext(" -A 1|0 enable/disable run-time assert checking\n"));
1074 printf(gettext(" -B NBUFFERS number of shared buffers\n"));
1075 printf(gettext(" -c NAME=VALUE set run-time parameter\n"));
1076 printf(gettext(" -d 1-5 debugging level\n"));
1077 printf(gettext(" -D DATADIR database directory\n"));
1078 printf(gettext(" -F turn fsync off\n"));
1079 printf(gettext(" -h HOSTNAME host name or IP address to listen on\n"));
1080 printf(gettext(" -i enable TCP/IP connections\n"));
1081 printf(gettext(" -k DIRECTORY Unix-domain socket location\n"));
1083 printf(gettext(" -l enable SSL connections\n"));
1085 printf(gettext(" -N MAX-CONNECT maximum number of allowed connections\n"));
1086 printf(gettext(" -o OPTIONS pass \"OPTIONS\" to each server process\n"));
1087 printf(gettext(" -p PORT port number to listen on\n"));
1088 printf(gettext(" -S silent mode (start in background without logging output)\n"));
1089 printf(gettext(" --help show this help, then exit\n"));
1090 printf(gettext(" --version output version information, then exit\n"));
1092 printf(gettext("\nDeveloper options:\n"));
1093 printf(gettext(" -n do not reinitialize shared memory after abnormal exit\n"));
1094 printf(gettext(" -s send SIGSTOP to all backend servers if one dies\n"));
1096 printf(gettext("\nPlease read the documentation for the complete list of run-time\n"
1097 "configuration settings and how to set them on the command line or in\n"
1098 "the configuration file.\n\n"
1099 "Report bugs to <pgsql-bugs@postgresql.org>.\n"));
1104 * Main idle loop of postmaster
1113 struct timeval earlier,
1117 gettimeofday(&earlier, &tz);
1118 last_touch_time = time(NULL);
1120 nSockets = initMasks(&readmask);
1126 struct timeval timeout;
1131 * Wait for something to happen.
1133 * We wait at most one minute, to ensure that the other background
1134 * tasks handled below get done even when no requests are
1137 memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
1139 timeout.tv_sec = 60;
1140 timeout.tv_usec = 0;
1142 PG_SETMASK(&UnBlockSig);
1144 selres = select(nSockets, &rmask, NULL, NULL, &timeout);
1147 * Block all signals until we wait again. (This makes it safe for
1148 * our signal handlers to do nontrivial work.)
1150 PG_SETMASK(&BlockSig);
1154 if (errno != EINTR && errno != EWOULDBLOCK)
1157 (errcode_for_socket_access(),
1158 errmsg("select() failed in postmaster: %m")));
1159 return STATUS_ERROR;
1164 * New connection pending on any of our sockets? If so, fork a
1165 * child process to deal with it.
1170 * Select a random seed at the time of first receiving a
1173 while (random_seed == 0)
1175 gettimeofday(&later, &tz);
1178 * We are not sure how much precision is in tv_usec, so we
1179 * swap the high and low 16 bits of 'later' and XOR them with
1180 * 'earlier'. On the off chance that the result is 0, we
1181 * loop until it isn't.
1183 random_seed = earlier.tv_usec ^
1184 ((later.tv_usec << 16) |
1185 ((later.tv_usec >> 16) & 0xffff));
1188 for (i = 0; i < MAXLISTEN; i++)
1190 if (ListenSocket[i] == -1)
1192 if (FD_ISSET(ListenSocket[i], &rmask))
1194 port = ConnCreate(ListenSocket[i]);
1197 BackendStartup(port);
1200 * We no longer need the open socket or port
1201 * structure in this process
1203 StreamClose(port->sock);
1210 /* If we have lost the system logger, try to start a new one */
1211 if (SysLoggerPID == 0 && Redirect_stderr)
1212 SysLoggerPID = SysLogger_Start();
1215 * If no background writer process is running, and we are not in a
1216 * state that prevents it, start one. It doesn't matter if this
1217 * fails, we'll just try again later.
1219 if (BgWriterPID == 0 && StartupPID == 0 && !FatalError)
1221 BgWriterPID = StartBackgroundWriter();
1222 /* If shutdown is pending, set it going */
1223 if (Shutdown > NoShutdown && BgWriterPID != 0)
1224 kill(BgWriterPID, SIGUSR2);
1227 /* If we have lost the archiver, try to start a new one */
1228 if (XLogArchivingActive() && PgArchPID == 0 &&
1229 StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
1230 PgArchPID = pgarch_start();
1232 /* If we have lost the stats collector, try to start a new one */
1233 if (PgStatPID == 0 &&
1234 StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
1235 PgStatPID = pgstat_start();
1238 * Touch the socket and lock file at least every ten minutes, to
1239 * ensure that they are not removed by overzealous /tmp-cleaning
1243 if (now - last_touch_time >= 10 * 60)
1246 TouchSocketLockFile();
1247 last_touch_time = now;
1254 * Initialise the masks for select() for the ports we are listening on.
1255 * Return the number of sockets to listen on.
1258 initMasks(fd_set *rmask)
1265 for (i = 0; i < MAXLISTEN; i++)
1267 int fd = ListenSocket[i];
1281 * Read the startup packet and do something according to it.
1283 * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
1284 * not return at all.
1286 * (Note that ereport(FATAL) stuff is sent to the client, so only use it
1287 * if that's what you want. Return STATUS_ERROR if you don't want to
1288 * send anything to the client, which would typically be appropriate
1289 * if we detect a communications failure.)
1292 ProcessStartupPacket(Port *port, bool SSLdone)
1296 ProtocolVersion proto;
1297 MemoryContext oldcontext;
1299 if (pq_getbytes((char *) &len, 4) == EOF)
1302 * EOF after SSLdone probably means the client didn't like our
1303 * response to NEGOTIATE_SSL_CODE. That's not an error condition,
1304 * so don't clutter the log with a complaint.
1308 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1309 errmsg("incomplete startup packet")));
1310 return STATUS_ERROR;
1316 if (len < (int32) sizeof(ProtocolVersion) ||
1317 len > MAX_STARTUP_PACKET_LENGTH)
1320 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1321 errmsg("invalid length of startup packet")));
1322 return STATUS_ERROR;
1326 * Allocate at least the size of an old-style startup packet, plus one
1327 * extra byte, and make sure all are zeroes. This ensures we will
1328 * have null termination of all strings, in both fixed- and
1329 * variable-length packet layouts.
1331 if (len <= (int32) sizeof(StartupPacket))
1332 buf = palloc0(sizeof(StartupPacket) + 1);
1334 buf = palloc0(len + 1);
1336 if (pq_getbytes(buf, len) == EOF)
1339 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1340 errmsg("incomplete startup packet")));
1341 return STATUS_ERROR;
1345 * The first field is either a protocol version number or a special
1348 port->proto = proto = ntohl(*((ProtocolVersion *) buf));
1350 if (proto == CANCEL_REQUEST_CODE)
1352 processCancelRequest(port, buf);
1353 return 127; /* XXX */
1356 if (proto == NEGOTIATE_SSL_CODE && !SSLdone)
1361 /* No SSL when disabled or on Unix sockets */
1362 if (!EnableSSL || IS_AF_UNIX(port->laddr.addr.ss_family))
1365 SSLok = 'S'; /* Support for SSL */
1367 SSLok = 'N'; /* No support for SSL */
1369 if (send(port->sock, &SSLok, 1, 0) != 1)
1372 (errcode_for_socket_access(),
1373 errmsg("failed to send SSL negotiation response: %m")));
1374 return STATUS_ERROR; /* close the connection */
1378 if (SSLok == 'S' && secure_open_server(port) == -1)
1379 return STATUS_ERROR;
1381 /* regular startup packet, cancel, etc packet should follow... */
1382 /* but not another SSL negotiation request */
1383 return ProcessStartupPacket(port, true);
1386 /* Could add additional special packet types here */
1389 * Set FrontendProtocol now so that ereport() knows what format to
1390 * send if we fail during startup.
1392 FrontendProtocol = proto;
1394 /* Check we can handle the protocol the frontend is using. */
1396 if (PG_PROTOCOL_MAJOR(proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) ||
1397 PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) ||
1398 (PG_PROTOCOL_MAJOR(proto) == PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) &&
1399 PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST)))
1401 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1402 errmsg("unsupported frontend protocol %u.%u: server supports %u.0 to %u.%u",
1403 PG_PROTOCOL_MAJOR(proto), PG_PROTOCOL_MINOR(proto),
1404 PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST),
1405 PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST),
1406 PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST))));
1409 * Now fetch parameters out of startup packet and save them into the
1410 * Port structure. All data structures attached to the Port struct
1411 * must be allocated in TopMemoryContext so that they won't disappear
1412 * when we pass them to PostgresMain (see BackendRun). We need not
1413 * worry about leaking this storage on failure, since we aren't in the
1414 * postmaster process anymore.
1416 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
1418 if (PG_PROTOCOL_MAJOR(proto) >= 3)
1420 int32 offset = sizeof(ProtocolVersion);
1423 * Scan packet body for name/option pairs. We can assume any
1424 * string beginning within the packet body is null-terminated,
1425 * thanks to zeroing extra byte above.
1427 port->guc_options = NIL;
1429 while (offset < len)
1431 char *nameptr = ((char *) buf) + offset;
1435 if (*nameptr == '\0')
1436 break; /* found packet terminator */
1437 valoffset = offset + strlen(nameptr) + 1;
1438 if (valoffset >= len)
1439 break; /* missing value, will complain below */
1440 valptr = ((char *) buf) + valoffset;
1442 if (strcmp(nameptr, "database") == 0)
1443 port->database_name = pstrdup(valptr);
1444 else if (strcmp(nameptr, "user") == 0)
1445 port->user_name = pstrdup(valptr);
1446 else if (strcmp(nameptr, "options") == 0)
1447 port->cmdline_options = pstrdup(valptr);
1450 /* Assume it's a generic GUC option */
1451 port->guc_options = lappend(port->guc_options,
1453 port->guc_options = lappend(port->guc_options,
1456 offset = valoffset + strlen(valptr) + 1;
1460 * If we didn't find a packet terminator exactly at the end of the
1461 * given packet length, complain.
1463 if (offset != len - 1)
1465 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1466 errmsg("invalid startup packet layout: expected terminator as last byte")));
1471 * Get the parameters from the old-style, fixed-width-fields
1472 * startup packet as C strings. The packet destination was
1473 * cleared first so a short packet has zeros silently added. We
1474 * have to be prepared to truncate the pstrdup result for oversize
1477 StartupPacket *packet = (StartupPacket *) buf;
1479 port->database_name = pstrdup(packet->database);
1480 if (strlen(port->database_name) > sizeof(packet->database))
1481 port->database_name[sizeof(packet->database)] = '\0';
1482 port->user_name = pstrdup(packet->user);
1483 if (strlen(port->user_name) > sizeof(packet->user))
1484 port->user_name[sizeof(packet->user)] = '\0';
1485 port->cmdline_options = pstrdup(packet->options);
1486 if (strlen(port->cmdline_options) > sizeof(packet->options))
1487 port->cmdline_options[sizeof(packet->options)] = '\0';
1488 port->guc_options = NIL;
1491 /* Check a user name was given. */
1492 if (port->user_name == NULL || port->user_name[0] == '\0')
1494 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
1495 errmsg("no PostgreSQL user name specified in startup packet")));
1497 /* The database defaults to the user name. */
1498 if (port->database_name == NULL || port->database_name[0] == '\0')
1499 port->database_name = pstrdup(port->user_name);
1501 if (Db_user_namespace)
1504 * If user@, it is a global user, remove '@'. We only want to do
1505 * this if there is an '@' at the end and no earlier in the user
1506 * string or they may fake as a local user of another database
1507 * attaching to this database.
1509 if (strchr(port->user_name, '@') ==
1510 port->user_name + strlen(port->user_name) - 1)
1511 *strchr(port->user_name, '@') = '\0';
1514 /* Append '@' and dbname */
1517 db_user = palloc(strlen(port->user_name) +
1518 strlen(port->database_name) + 2);
1519 sprintf(db_user, "%s@%s", port->user_name, port->database_name);
1520 port->user_name = db_user;
1525 * Truncate given database and user names to length of a Postgres
1526 * name. This avoids lookup failures when overlength names are given.
1528 if (strlen(port->database_name) >= NAMEDATALEN)
1529 port->database_name[NAMEDATALEN - 1] = '\0';
1530 if (strlen(port->user_name) >= NAMEDATALEN)
1531 port->user_name[NAMEDATALEN - 1] = '\0';
1534 * Done putting stuff in TopMemoryContext.
1536 MemoryContextSwitchTo(oldcontext);
1539 * If we're going to reject the connection due to database state, say
1540 * so now instead of wasting cycles on an authentication exchange.
1541 * (This also allows a pg_ping utility to be written.)
1543 switch (port->canAcceptConnections)
1547 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1548 errmsg("the database system is starting up")));
1552 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1553 errmsg("the database system is shutting down")));
1557 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1558 errmsg("the database system is in recovery mode")));
1562 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
1563 errmsg("sorry, too many clients already")));
1575 * The client has sent a cancel request packet, not a normal
1576 * start-a-new-connection packet. Perform the necessary processing.
1577 * Nothing is sent back to the client.
1580 processCancelRequest(Port *port, void *pkt)
1582 CancelRequestPacket *canc = (CancelRequestPacket *) pkt;
1584 long cancelAuthCode;
1586 #ifndef EXEC_BACKEND
1592 backendPID = (int) ntohl(canc->backendPID);
1593 cancelAuthCode = (long) ntohl(canc->cancelAuthCode);
1596 * See if we have a matching backend. In the EXEC_BACKEND case, we
1597 * can no longer access the postmaster's own backend list, and must
1598 * rely on the duplicate array in shared memory.
1600 #ifndef EXEC_BACKEND
1601 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
1603 bp = (Backend *) DLE_VAL(curr);
1605 for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++)
1607 bp = (Backend *) &ShmemBackendArray[i];
1609 if (bp->pid == backendPID)
1611 if (bp->cancel_key == cancelAuthCode)
1613 /* Found a match; signal that backend to cancel current op */
1615 (errmsg_internal("processing cancel request: sending SIGINT to process %d",
1617 kill(bp->pid, SIGINT);
1620 /* Right PID, wrong key: no way, Jose */
1622 (errmsg_internal("bad key in cancel request for process %d",
1628 /* No matching backend */
1630 (errmsg_internal("bad pid in cancel request for process %d",
1635 * canAcceptConnections --- check to see if database state allows connections.
1637 static enum CAC_state
1638 canAcceptConnections(void)
1640 /* Can't start backends when in startup/shutdown/recovery state. */
1641 if (Shutdown > NoShutdown)
1642 return CAC_SHUTDOWN;
1646 return CAC_RECOVERY;
1649 * Don't start too many children.
1651 * We allow more connections than we can have backends here because some
1652 * might still be authenticating; they might fail auth, or some
1653 * existing backend might exit before the auth cycle is completed. The
1654 * exact MaxBackends limit is enforced when a new backend tries to
1655 * join the shared-inval backend array.
1657 if (CountChildren() >= 2 * MaxBackends)
1665 * ConnCreate -- create a local connection data structure
1668 ConnCreate(int serverFd)
1672 if (!(port = (Port *) calloc(1, sizeof(Port))))
1675 (errcode(ERRCODE_OUT_OF_MEMORY),
1676 errmsg("out of memory")));
1680 if (StreamConnection(serverFd, port) != STATUS_OK)
1682 StreamClose(port->sock);
1689 * Precompute password salt values to use for this connection.
1690 * It's slightly annoying to do this long in advance of knowing
1691 * whether we'll need 'em or not, but we must do the random()
1692 * calls before we fork, not after. Else the postmaster's random
1693 * sequence won't get advanced, and all backends would end up
1694 * using the same salt...
1696 RandomSalt(port->cryptSalt, port->md5Salt);
1704 * ConnFree -- free a local connection data structure
1707 ConnFree(Port *conn)
1717 * ClosePostmasterPorts -- close all the postmaster's open sockets
1719 * This is called during child process startup to release file descriptors
1720 * that are not needed by that child process. The postmaster still has
1721 * them open, of course.
1723 * Note: we pass am_syslogger as a boolean because we don't want to set
1724 * the global variable yet when this is called.
1727 ClosePostmasterPorts(bool am_syslogger)
1731 /* Close the listen sockets */
1732 for (i = 0; i < MAXLISTEN; i++)
1734 if (ListenSocket[i] != -1)
1736 StreamClose(ListenSocket[i]);
1737 ListenSocket[i] = -1;
1741 /* If using syslogger, close the read side of the pipe */
1745 if (syslogPipe[0] >= 0)
1746 close(syslogPipe[0]);
1750 CloseHandle(syslogPipe[0]);
1758 * reset_shared -- reset shared memory and semaphores
1761 reset_shared(unsigned short port)
1764 * Create or re-create shared memory and semaphores.
1766 * Note: in each "cycle of life" we will normally assign the same IPC
1767 * keys (if using SysV shmem and/or semas), since the port number is
1768 * used to determine IPC keys. This helps ensure that we will clean
1769 * up dead IPC objects if the postmaster crashes and is restarted.
1771 CreateSharedMemoryAndSemaphores(false, MaxBackends, port);
1776 * SIGHUP -- reread config files, and tell children to do same
1779 SIGHUP_handler(SIGNAL_ARGS)
1781 int save_errno = errno;
1783 PG_SETMASK(&BlockSig);
1785 if (Shutdown <= SmartShutdown)
1788 (errmsg("received SIGHUP, reloading configuration files")));
1789 ProcessConfigFile(PGC_SIGHUP);
1790 SignalChildren(SIGHUP);
1791 if (BgWriterPID != 0)
1792 kill(BgWriterPID, SIGHUP);
1794 kill(PgArchPID, SIGHUP);
1795 if (SysLoggerPID != 0)
1796 kill(SysLoggerPID, SIGHUP);
1797 /* PgStatPID does not currently need SIGHUP */
1799 /* Reload authentication config files too */
1804 /* Update the starting-point file for future children */
1805 write_nondefault_variables(PGC_SIGHUP);
1809 PG_SETMASK(&UnBlockSig);
1816 * pmdie -- signal handler for processing various postmaster signals.
1821 int save_errno = errno;
1823 PG_SETMASK(&BlockSig);
1826 (errmsg_internal("postmaster received signal %d",
1827 postgres_signal_arg)));
1829 switch (postgres_signal_arg)
1836 * Wait for children to end their work, then shut down.
1838 if (Shutdown >= SmartShutdown)
1840 Shutdown = SmartShutdown;
1842 (errmsg("received smart shutdown request")));
1844 if (DLGetHead(BackendList))
1845 break; /* let reaper() handle this */
1848 * No children left. Begin shutdown of data base system.
1850 if (StartupPID != 0 || FatalError)
1851 break; /* let reaper() handle this */
1852 /* Start the bgwriter if not running */
1853 if (BgWriterPID == 0)
1854 BgWriterPID = StartBackgroundWriter();
1855 /* And tell it to shut down */
1856 if (BgWriterPID != 0)
1857 kill(BgWriterPID, SIGUSR2);
1858 /* Tell pgarch to shut down too; nothing left for it to do */
1860 kill(PgArchPID, SIGQUIT);
1861 /* Tell pgstat to shut down too; nothing left for it to do */
1863 kill(PgStatPID, SIGQUIT);
1871 * Abort all children with SIGTERM (rollback active transactions
1872 * and exit) and shut down when they are gone.
1874 if (Shutdown >= FastShutdown)
1876 Shutdown = FastShutdown;
1878 (errmsg("received fast shutdown request")));
1880 if (DLGetHead(BackendList))
1885 (errmsg("aborting any active transactions")));
1886 SignalChildren(SIGTERM);
1887 /* reaper() does the rest */
1893 * No children left. Begin shutdown of data base system.
1895 * Note: if we previously got SIGTERM then we may send SIGUSR2 to
1896 * the bgwriter a second time here. This should be harmless.
1898 if (StartupPID != 0 || FatalError)
1899 break; /* let reaper() handle this */
1900 /* Start the bgwriter if not running */
1901 if (BgWriterPID == 0)
1902 BgWriterPID = StartBackgroundWriter();
1903 /* And tell it to shut down */
1904 if (BgWriterPID != 0)
1905 kill(BgWriterPID, SIGUSR2);
1906 /* Tell pgarch to shut down too; nothing left for it to do */
1908 kill(PgArchPID, SIGQUIT);
1909 /* Tell pgstat to shut down too; nothing left for it to do */
1911 kill(PgStatPID, SIGQUIT);
1917 * Immediate Shutdown:
1919 * abort all children with SIGQUIT and exit without attempt to
1920 * properly shut down data base system.
1923 (errmsg("received immediate shutdown request")));
1924 if (StartupPID != 0)
1925 kill(StartupPID, SIGQUIT);
1926 if (BgWriterPID != 0)
1927 kill(BgWriterPID, SIGQUIT);
1929 kill(PgArchPID, SIGQUIT);
1931 kill(PgStatPID, SIGQUIT);
1932 if (DLGetHead(BackendList))
1933 SignalChildren(SIGQUIT);
1938 PG_SETMASK(&UnBlockSig);
1944 * Reaper -- signal handler to cleanup after a backend (child) dies.
1949 int save_errno = errno;
1952 int status; /* backend exit status */
1956 union wait status; /* backend exit status */
1960 int pid; /* process id of dead backend */
1962 PG_SETMASK(&BlockSig);
1965 (errmsg_internal("reaping dead processes")));
1967 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1969 exitstatus = status;
1972 while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
1974 exitstatus = status.w_status;
1976 while ((pid = win32_waitpid(&exitstatus)) > 0)
1979 * We need to do this here, and not in CleanupBackend, since this
1980 * is to be called on all children when we are done with them.
1981 * Could move to LogChildExit, but that seems like asking for
1984 win32_RemoveChild(pid);
1986 #endif /* HAVE_WAITPID */
1989 * Check if this child was a startup process.
1991 if (StartupPID != 0 && pid == StartupPID)
1994 if (exitstatus != 0)
1996 LogChildExit(LOG, gettext("startup process"),
1999 (errmsg("aborting startup due to startup process failure")));
2004 * Startup succeeded - we are done with system startup or
2010 * Load the flat user/group files into postmaster's caches.
2011 * The startup process has recomputed these from the database
2012 * contents, so we wait till it finishes before loading them.
2018 * Crank up the background writer. It doesn't matter if this
2019 * fails, we'll just try again later.
2021 Assert(BgWriterPID == 0);
2022 BgWriterPID = StartBackgroundWriter();
2025 * Go to shutdown mode if a shutdown request was pending.
2026 * Otherwise, try to start the archiver and stats collector
2029 if (Shutdown > NoShutdown && BgWriterPID != 0)
2030 kill(BgWriterPID, SIGUSR2);
2031 else if (Shutdown == NoShutdown)
2033 if (XLogArchivingActive() && PgArchPID == 0)
2034 PgArchPID = pgarch_start();
2036 PgStatPID = pgstat_start();
2043 * Was it the bgwriter?
2045 if (BgWriterPID != 0 && pid == BgWriterPID)
2048 if (exitstatus == 0 && Shutdown > NoShutdown &&
2049 !FatalError && !DLGetHead(BackendList))
2052 * Normal postmaster exit is here: we've seen normal exit
2053 * of the bgwriter after it's been told to shut down. We
2054 * expect that it wrote a shutdown checkpoint. (If for
2055 * some reason it didn't, recovery will occur on next
2056 * postmaster start.)
2058 * Note: we do not wait around for exit of the archiver or
2059 * stats processes. They've been sent SIGQUIT by this
2060 * point, and in any case contain logic to commit
2061 * hara-kiri if they notice the postmaster is gone.
2067 * Any unexpected exit of the bgwriter is treated as a crash.
2069 HandleChildCrash(pid, exitstatus,
2070 gettext("background writer process"));
2075 * Was it the archiver? If so, just try to start a new one; no
2076 * need to force reset of the rest of the system. (If fail, we'll
2077 * try again in future cycles of the main loop.)
2079 if (PgArchPID != 0 && pid == PgArchPID)
2082 if (exitstatus != 0)
2083 LogChildExit(LOG, gettext("archiver process"),
2085 if (XLogArchivingActive() &&
2086 StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
2087 PgArchPID = pgarch_start();
2092 * Was it the statistics collector? If so, just try to start a
2093 * new one; no need to force reset of the rest of the system. (If
2094 * fail, we'll try again in future cycles of the main loop.)
2096 if (PgStatPID != 0 && pid == PgStatPID)
2099 if (exitstatus != 0)
2100 LogChildExit(LOG, gettext("statistics collector process"),
2102 if (StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
2103 PgStatPID = pgstat_start();
2107 /* Was it the system logger? try to start a new one */
2108 if (SysLoggerPID != 0 && pid == SysLoggerPID)
2111 /* for safety's sake, launch new logger *first* */
2112 SysLoggerPID = SysLogger_Start();
2113 if (exitstatus != 0)
2114 LogChildExit(LOG, gettext("system logger process"),
2120 * Else do standard backend child cleanup.
2122 CleanupBackend(pid, exitstatus);
2123 } /* loop over pending child-death reports */
2128 * Wait for all important children to exit, then reset shmem and
2129 * StartupDataBase. (We can ignore the archiver and stats
2130 * processes here since they are not connected to shmem.)
2132 if (DLGetHead(BackendList) || StartupPID != 0 || BgWriterPID != 0)
2135 (errmsg("all server processes terminated; reinitializing")));
2138 reset_shared(PostPortNumber);
2140 StartupPID = StartupDataBase();
2145 if (Shutdown > NoShutdown)
2147 if (DLGetHead(BackendList) || StartupPID != 0)
2149 /* Start the bgwriter if not running */
2150 if (BgWriterPID == 0)
2151 BgWriterPID = StartBackgroundWriter();
2152 /* And tell it to shut down */
2153 if (BgWriterPID != 0)
2154 kill(BgWriterPID, SIGUSR2);
2155 /* Tell pgarch to shut down too; nothing left for it to do */
2157 kill(PgArchPID, SIGQUIT);
2158 /* Tell pgstat to shut down too; nothing left for it to do */
2160 kill(PgStatPID, SIGQUIT);
2164 PG_SETMASK(&UnBlockSig);
2171 * CleanupBackend -- cleanup after terminated backend.
2173 * Remove all local state associated with backend.
2176 CleanupBackend(int pid,
2177 int exitstatus) /* child's exit status. */
2181 LogChildExit(DEBUG2, gettext("server process"), pid, exitstatus);
2184 * If a backend dies in an ugly way (i.e. exit status not 0) then we
2185 * must signal all other backends to quickdie. If exit status is zero
2186 * we assume everything is hunky dory and simply remove the backend
2187 * from the active backend list.
2189 if (exitstatus != 0)
2191 HandleChildCrash(pid, exitstatus, gettext("server process"));
2195 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
2197 Backend *bp = (Backend *) DLE_VAL(curr);
2205 ShmemBackendArrayRemove(pid);
2207 /* Tell the collector about backend termination */
2215 * HandleChildCrash -- cleanup after failed backend or bgwriter.
2217 * The objectives here are to clean up our local state about the child
2218 * process, and to signal all other remaining children to quickdie.
2221 HandleChildCrash(int pid, int exitstatus, const char *procname)
2228 * Make log entry unless there was a previous crash (if so, nonzero
2229 * exit status is to be expected in SIGQUIT response; don't clutter
2234 LogChildExit(LOG, procname, pid, exitstatus);
2236 (errmsg("terminating any other active server processes")));
2239 /* Process regular backends */
2240 for (curr = DLGetHead(BackendList); curr; curr = next)
2242 next = DLGetSucc(curr);
2243 bp = (Backend *) DLE_VAL(curr);
2247 * Found entry for freshly-dead backend, so remove it.
2253 ShmemBackendArrayRemove(pid);
2255 /* Tell the collector about backend termination */
2257 /* Keep looping so we can signal remaining backends */
2262 * This backend is still alive. Unless we did so already,
2263 * tell it to commit hara-kiri.
2265 * SIGQUIT is the special signal that says exit without proc_exit
2266 * and let the user know what's going on. But if SendStop is
2267 * set (-s on command line), then we send SIGSTOP instead, so
2268 * that we can get core dumps from all backends by hand.
2273 (errmsg_internal("sending %s to process %d",
2274 (SendStop ? "SIGSTOP" : "SIGQUIT"),
2276 kill(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
2281 /* Take care of the bgwriter too */
2282 if (pid == BgWriterPID)
2284 else if (BgWriterPID != 0 && !FatalError)
2287 (errmsg_internal("sending %s to process %d",
2288 (SendStop ? "SIGSTOP" : "SIGQUIT"),
2289 (int) BgWriterPID)));
2290 kill(BgWriterPID, (SendStop ? SIGSTOP : SIGQUIT));
2293 /* Force a power-cycle of the pgarch process too */
2294 /* (Shouldn't be necessary, but just for luck) */
2295 if (PgArchPID != 0 && !FatalError)
2298 (errmsg_internal("sending %s to process %d",
2301 kill(PgArchPID, SIGQUIT);
2304 /* Force a power-cycle of the pgstat processes too */
2305 /* (Shouldn't be necessary, but just for luck) */
2306 if (PgStatPID != 0 && !FatalError)
2309 (errmsg_internal("sending %s to process %d",
2312 kill(PgStatPID, SIGQUIT);
2315 /* We do NOT restart the syslogger */
2321 * Log the death of a child process.
2324 LogChildExit(int lev, const char *procname, int pid, int exitstatus)
2326 if (WIFEXITED(exitstatus))
2330 * translator: %s is a noun phrase describing a child process,
2331 * such as "server process"
2333 (errmsg("%s (PID %d) exited with exit code %d",
2334 procname, pid, WEXITSTATUS(exitstatus))));
2335 else if (WIFSIGNALED(exitstatus))
2339 * translator: %s is a noun phrase describing a child process,
2340 * such as "server process"
2342 (errmsg("%s (PID %d) was terminated by signal %d",
2343 procname, pid, WTERMSIG(exitstatus))));
2348 * translator: %s is a noun phrase describing a child process,
2349 * such as "server process"
2351 (errmsg("%s (PID %d) exited with unexpected status %d",
2352 procname, pid, exitstatus)));
2356 * Send a signal to all backend children (but NOT special children)
2359 SignalChildren(int signal)
2363 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
2365 Backend *bp = (Backend *) DLE_VAL(curr);
2368 (errmsg_internal("sending signal %d to process %d",
2369 signal, (int) bp->pid)));
2370 kill(bp->pid, signal);
2375 * BackendStartup -- start backend process
2377 * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
2380 BackendStartup(Port *port)
2382 Backend *bn; /* for backend cleanup */
2385 #ifdef LINUX_PROFILE
2386 struct itimerval prof_itimer;
2390 * Compute the cancel key that will be assigned to this backend. The
2391 * backend will have its own copy in the forked-off process' value of
2392 * MyCancelKey, so that it can transmit the key to the frontend.
2394 MyCancelKey = PostmasterRandom();
2397 * Make room for backend data structure. Better before the fork() so
2398 * we can handle failure cleanly.
2400 bn = (Backend *) malloc(sizeof(Backend));
2404 (errcode(ERRCODE_OUT_OF_MEMORY),
2405 errmsg("out of memory")));
2406 return STATUS_ERROR;
2409 /* Pass down canAcceptConnections state (kluge for EXEC_BACKEND case) */
2410 port->canAcceptConnections = canAcceptConnections();
2413 * Flush stdio channels just before fork, to avoid double-output
2414 * problems. Ideally we'd use fflush(NULL) here, but there are still a
2415 * few non-ANSI stdio libraries out there (like SunOS 4.1.x) that
2416 * coredump if we do. Presently stdout and stderr are the only stdio
2417 * output channels used by the postmaster, so fflush'ing them should
2425 pid = backend_forkexec(port);
2427 #else /* !EXEC_BACKEND */
2429 #ifdef LINUX_PROFILE
2432 * Linux's fork() resets the profiling timer in the child process. If
2433 * we want to profile child processes then we need to save and restore
2434 * the timer setting. This is a waste of time if not profiling,
2435 * however, so only do it if commanded by specific -DLINUX_PROFILE
2438 getitimer(ITIMER_PROF, &prof_itimer);
2442 /* Specific beos actions before backend startup */
2443 beos_before_backend_startup();
2448 if (pid == 0) /* child */
2450 #ifdef LINUX_PROFILE
2451 setitimer(ITIMER_PROF, &prof_itimer, NULL);
2455 /* Specific beos backend startup actions */
2456 beos_backend_startup();
2460 proc_exit(BackendRun(port));
2462 #endif /* EXEC_BACKEND */
2466 /* in parent, fork failed */
2467 int save_errno = errno;
2470 /* Specific beos backend startup actions */
2471 beos_backend_startup_failed();
2476 (errmsg("could not fork new process for connection: %m")));
2477 report_fork_failure_to_client(port, save_errno);
2478 return STATUS_ERROR;
2481 /* in parent, successful fork */
2483 (errmsg_internal("forked new backend, pid=%d socket=%d",
2484 (int) pid, port->sock)));
2487 * Everything's been successful, it's safe to add this backend to our
2491 bn->cancel_key = MyCancelKey;
2492 DLAddHead(BackendList, DLNewElem(bn));
2494 ShmemBackendArrayAdd(bn);
2501 * Try to report backend fork() failure to client before we close the
2502 * connection. Since we do not care to risk blocking the postmaster on
2503 * this connection, we set the connection to non-blocking and try only once.
2505 * This is grungy special-purpose code; we cannot use backend libpq since
2506 * it's not up and running.
2509 report_fork_failure_to_client(Port *port, int errnum)
2513 /* Format the error message packet (always V2 protocol) */
2514 snprintf(buffer, sizeof(buffer), "E%s%s\n",
2515 gettext("could not fork new process for connection: "),
2518 /* Set port to non-blocking. Don't do send() if this fails */
2519 if (!set_noblock(port->sock))
2522 send(port->sock, buffer, strlen(buffer) + 1, 0);
2527 * split_opts -- split a string of options and append it to an argv array
2529 * NB: the string is destructively modified!
2531 * Since no current POSTGRES arguments require any quoting characters,
2532 * we can use the simple-minded tactic of assuming each set of space-
2533 * delimited characters is a separate argv element.
2535 * If you don't like that, well, we *used* to pass the whole option string
2536 * as ONE argument to execl(), which was even less intelligent...
2539 split_opts(char **argv, int *argcp, char *s)
2543 while (isspace((unsigned char) *s))
2547 argv[(*argcp)++] = s;
2548 while (*s && !isspace((unsigned char) *s))
2557 * BackendRun -- perform authentication, and if successful,
2558 * set up the backend's argument list and invoke PostgresMain()
2561 * Shouldn't return at all.
2562 * If PostgresMain() fails, return status.
2565 BackendRun(Port *port)
2568 char remote_host[NI_MAXHOST];
2569 char remote_port[NI_MAXSERV];
2570 char remote_ps_data[NI_MAXHOST];
2577 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
2580 * Let's clean up ourselves as the postmaster child, and close the
2581 * postmaster's listen sockets
2583 ClosePostmasterPorts(false);
2585 /* We don't want the postmaster's proc_exit() handlers */
2589 * Signal handlers setting is moved to tcop/postgres...
2592 /* Save port etc. for ps status */
2595 /* Reset MyProcPid to new backend's pid */
2596 MyProcPid = getpid();
2599 * PreAuthDelay is a debugging aid for investigating problems in the
2600 * authentication cycle: it can be set in postgresql.conf to allow
2601 * time to attach to the newly-forked backend with a debugger. (See
2602 * also the -W backend switch, which we allow clients to pass through
2603 * PGOPTIONS, but it is not honored until after authentication.)
2605 if (PreAuthDelay > 0)
2606 pg_usleep(PreAuthDelay * 1000000L);
2608 ClientAuthInProgress = true; /* limit visibility of log messages */
2610 /* save start time for end of session reporting */
2611 gettimeofday(&(port->session_start), NULL);
2613 /* set these to empty in case they are needed before we set them up */
2614 port->remote_host = "";
2615 port->remote_port = "";
2616 port->commandTag = "";
2619 * Initialize libpq and enable reporting of ereport errors to the
2620 * client. Must do this now because authentication uses libpq to send
2623 pq_init(); /* initialize libpq to talk to client */
2624 whereToSendOutput = Remote; /* now safe to ereport to client */
2627 * We arrange for a simple exit(0) if we receive SIGTERM or SIGQUIT
2628 * during any client authentication related communication. Otherwise
2629 * the postmaster cannot shutdown the database FAST or IMMED cleanly
2630 * if a buggy client blocks a backend during authentication.
2632 pqsignal(SIGTERM, authdie);
2633 pqsignal(SIGQUIT, authdie);
2634 pqsignal(SIGALRM, authdie);
2635 PG_SETMASK(&AuthBlockSig);
2638 * Get the remote host name and port for logging and status display.
2640 remote_host[0] = '\0';
2641 remote_port[0] = '\0';
2642 if (getnameinfo_all(&port->raddr.addr, port->raddr.salen,
2643 remote_host, sizeof(remote_host),
2644 remote_port, sizeof(remote_port),
2645 (log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV))
2647 int ret = getnameinfo_all(&port->raddr.addr, port->raddr.salen,
2648 remote_host, sizeof(remote_host),
2649 remote_port, sizeof(remote_port),
2650 NI_NUMERICHOST | NI_NUMERICSERV);
2654 (errmsg("getnameinfo_all() failed: %s",
2655 gai_strerror(ret))));
2657 snprintf(remote_ps_data, sizeof(remote_ps_data),
2658 remote_port[0] == '\0' ? "%s" : "%s(%s)",
2659 remote_host, remote_port);
2661 if (Log_connections)
2663 (errmsg("connection received: host=%s port=%s",
2664 remote_host, remote_port)));
2667 * save remote_host and remote_port in port stucture
2669 port->remote_host = strdup(remote_host);
2670 port->remote_port = strdup(remote_port);
2673 * In EXEC_BACKEND case, we didn't inherit the contents of pg_hba.conf
2674 * etcetera from the postmaster, and have to load them ourselves.
2675 * Build the PostmasterContext (which didn't exist before, in this
2676 * process) to contain the data.
2678 * FIXME: [fork/exec] Ugh. Is there a way around this overhead?
2681 Assert(PostmasterContext == NULL);
2682 PostmasterContext = AllocSetContextCreate(TopMemoryContext,
2684 ALLOCSET_DEFAULT_MINSIZE,
2685 ALLOCSET_DEFAULT_INITSIZE,
2686 ALLOCSET_DEFAULT_MAXSIZE);
2687 MemoryContextSwitchTo(PostmasterContext);
2696 * Ready to begin client interaction. We will give up and exit(0)
2697 * after a time delay, so that a broken client can't hog a connection
2698 * indefinitely. PreAuthDelay doesn't count against the time limit.
2700 if (!enable_sig_alarm(AuthenticationTimeout * 1000, false))
2701 elog(FATAL, "could not set timer for authorization timeout");
2704 * Receive the startup packet (which might turn out to be a cancel
2707 status = ProcessStartupPacket(port, false);
2709 if (status != STATUS_OK)
2713 * Now that we have the user and database name, we can set the process
2714 * title for ps. It's good to do this as early as possible in
2717 init_ps_display(port->user_name, port->database_name, remote_ps_data);
2718 set_ps_display("authentication");
2721 * Now perform authentication exchange.
2723 ClientAuthentication(port); /* might not return, if failure */
2726 * Done with authentication. Disable timeout, and prevent
2727 * SIGTERM/SIGQUIT again until backend startup is complete.
2729 if (!disable_sig_alarm(false))
2730 elog(FATAL, "could not disable timer for authorization timeout");
2731 PG_SETMASK(&BlockSig);
2733 if (Log_connections)
2735 (errmsg("connection authorized: user=%s database=%s",
2736 port->user_name, port->database_name)));
2739 * Don't want backend to be able to see the postmaster random number
2740 * generator state. We have to clobber the static random_seed *and*
2741 * start a new random sequence in the random() library function.
2744 srandom((unsigned int) (MyProcPid ^ port->session_start.tv_usec));
2747 * Now, build the argv vector that will be given to PostgresMain.
2749 * The layout of the command line is
2750 * postgres [secure switches] -p databasename [insecure switches]
2751 * where the switches after -p come from the client request.
2753 * The maximum possible number of commandline arguments that could come
2754 * from ExtraOptions or port->cmdline_options is (strlen + 1) / 2; see
2758 maxac = 10; /* for fixed args supplied below */
2759 maxac += (strlen(ExtraOptions) + 1) / 2;
2760 if (port->cmdline_options)
2761 maxac += (strlen(port->cmdline_options) + 1) / 2;
2763 av = (char **) MemoryContextAlloc(TopMemoryContext,
2764 maxac * sizeof(char *));
2767 av[ac++] = "postgres";
2770 * Pass any backend switches specified with -o in the postmaster's own
2771 * command line. We assume these are secure. (It's OK to mangle
2772 * ExtraOptions now, since we're safely inside a subprocess.)
2774 split_opts(av, &ac, ExtraOptions);
2776 /* Tell the backend what protocol the frontend is using. */
2777 snprintf(protobuf, sizeof(protobuf), "-v%u", port->proto);
2778 av[ac++] = protobuf;
2781 * Tell the backend it is being called from the postmaster, and which
2782 * database to use. -p marks the end of secure switches.
2785 av[ac++] = port->database_name;
2788 * Pass the (insecure) option switches from the connection request.
2789 * (It's OK to mangle port->cmdline_options now.)
2791 if (port->cmdline_options)
2792 split_opts(av, &ac, port->cmdline_options);
2799 * Release postmaster's working memory context so that backend can
2800 * recycle the space. Note this does not trash *MyProcPort, because
2801 * ConnCreate() allocated that space with malloc() ... else we'd need
2802 * to copy the Port data here. Also, subsidiary data such as the
2803 * username isn't lost either; see ProcessStartupPacket().
2805 MemoryContextSwitchTo(TopMemoryContext);
2806 MemoryContextDelete(PostmasterContext);
2807 PostmasterContext = NULL;
2810 * Debug: print arguments being passed to backend
2813 (errmsg_internal("%s child[%d]: starting with (",
2814 progname, (int)getpid())));
2815 for (i = 0; i < ac; ++i)
2817 (errmsg_internal("\t%s", av[i])));
2819 (errmsg_internal(")")));
2821 ClientAuthInProgress = false; /* client_min_messages is active
2824 return (PostgresMain(ac, av, port->user_name));
2831 * postmaster_forkexec -- fork and exec a postmaster subprocess
2833 * The caller must have set up the argv array already, except for argv[2]
2834 * which will be filled with the name of the temp variable file.
2836 * Returns the child process PID, or -1 on fork failure (a suitable error
2837 * message has been logged on failure).
2839 * All uses of this routine will dispatch to SubPostmasterMain in the
2843 postmaster_forkexec(int argc, char *argv[])
2847 /* This entry point passes dummy values for the Port variables */
2848 memset(&port, 0, sizeof(port));
2849 return internal_forkexec(argc, argv, &port);
2853 * backend_forkexec -- fork/exec off a backend process
2855 * returns the pid of the fork/exec'd process, or -1 on failure
2858 backend_forkexec(Port *port)
2863 av[ac++] = "postgres";
2864 av[ac++] = "-forkbackend";
2865 av[ac++] = NULL; /* filled in by internal_forkexec */
2868 Assert(ac < lengthof(av));
2870 return internal_forkexec(ac, av, port);
2876 * internal_forkexec non-win32 implementation
2878 * - writes out backend variables to the parameter file
2879 * - fork():s, and then exec():s the child process
2882 internal_forkexec(int argc, char *argv[], Port *port)
2884 static unsigned long tmpBackendFileNum = 0;
2886 char tmpfilename[MAXPGPATH];
2887 BackendParameters param;
2890 if (!save_backend_variables(¶m, port))
2891 return -1; /* log made by save_backend_variables */
2893 /* Calculate name for temp file */
2895 snprintf(tmpfilename, MAXPGPATH, "%s/%s/%s.backend_var.%d.%lu",
2896 DataDir, PG_TEMP_FILES_DIR, PG_TEMP_FILE_PREFIX,
2897 MyProcPid, ++tmpBackendFileNum);
2900 fp = AllocateFile(tmpfilename, PG_BINARY_W);
2903 /* As per OpenTemporaryFile... */
2904 char dirname[MAXPGPATH];
2906 snprintf(dirname, MAXPGPATH, "%s/%s", DataDir, PG_TEMP_FILES_DIR);
2907 mkdir(dirname, S_IRWXU);
2909 fp = AllocateFile(tmpfilename, PG_BINARY_W);
2913 (errcode_for_file_access(),
2914 errmsg("could not create file \"%s\": %m",
2920 if (fwrite(¶m, sizeof(param), 1, fp) != 1)
2923 (errcode_for_file_access(),
2924 errmsg("could not write to file \"%s\": %m", tmpfilename)));
2933 (errcode_for_file_access(),
2934 errmsg("could not write to file \"%s\": %m", tmpfilename)));
2938 /* Make sure caller set up argv properly */
2940 Assert(argv[argc] == NULL);
2941 Assert(strncmp(argv[1], "-fork", 5) == 0);
2942 Assert(argv[2] == NULL);
2944 /* Insert temp file name after -fork argument */
2945 argv[2] = tmpfilename;
2947 /* Fire off execv in child */
2948 if ((pid = fork()) == 0)
2950 if (execv(postgres_exec_path, argv) < 0)
2953 (errmsg("could not execute server process \"%s\": %m",
2954 postgres_exec_path)));
2955 /* We're already in the child process here, can't return */
2960 return pid; /* Parent returns pid, or -1 on fork
2967 * internal_forkexec win32 implementation
2969 * - starts backend using CreateProcess(), in suspended state
2970 * - writes out backend variables to the parameter file
2971 * - during this, duplicates handles and sockets required for
2972 * inheritance into the new process
2973 * - resumes execution of the new process once the backend parameter
2977 internal_forkexec(int argc, char *argv[], Port *port)
2980 PROCESS_INFORMATION pi;
2983 char cmdLine[MAXPGPATH * 2];
2984 HANDLE childHandleCopy;
2985 HANDLE waiterThread;
2987 BackendParameters *param;
2988 SECURITY_ATTRIBUTES sa;
2989 char paramHandleStr[32];
2991 /* Make sure caller set up argv properly */
2993 Assert(argv[argc] == NULL);
2994 Assert(strncmp(argv[1], "-fork", 5) == 0);
2995 Assert(argv[2] == NULL);
2997 /* Set up shared memory for parameter passing */
2998 ZeroMemory(&sa,sizeof(sa));
2999 sa.nLength = sizeof(sa);
3000 sa.bInheritHandle = TRUE;
3001 paramHandle = CreateFileMapping(INVALID_HANDLE_VALUE,
3005 sizeof(BackendParameters),
3007 if (paramHandle == INVALID_HANDLE_VALUE)
3009 elog(LOG, "could not create backend parameter file mapping: error code %d",
3010 (int) GetLastError());
3014 param = MapViewOfFile(paramHandle, FILE_MAP_WRITE, 0, 0, sizeof(BackendParameters));
3017 elog(LOG, "could not map backend parameter memory: error code %d",
3018 (int) GetLastError());
3019 CloseHandle(paramHandle);
3023 /* Insert temp file name after -fork argument */
3024 sprintf(paramHandleStr, "%lu", (DWORD)paramHandle);
3025 argv[2] = paramHandleStr;
3027 /* Format the cmd line */
3028 cmdLine[sizeof(cmdLine) - 1] = '\0';
3029 cmdLine[sizeof(cmdLine) - 2] = '\0';
3030 snprintf(cmdLine, sizeof(cmdLine) - 1, "\"%s\"", postgres_exec_path);
3032 while (argv[++i] != NULL)
3034 j = strlen(cmdLine);
3035 snprintf(cmdLine + j, sizeof(cmdLine) - 1 - j, " \"%s\"", argv[i]);
3037 if (cmdLine[sizeof(cmdLine) - 2] != '\0')
3039 elog(LOG, "subprocess command line too long");
3043 memset(&pi, 0, sizeof(pi));
3044 memset(&si, 0, sizeof(si));
3047 * Create the subprocess in a suspended state. This will be resumed
3048 * later, once we have written out the parameter file.
3050 if (!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, CREATE_SUSPENDED,
3051 NULL, NULL, &si, &pi))
3053 elog(LOG, "CreateProcess call failed: %m (error code %d)",
3054 (int) GetLastError());
3058 if (!save_backend_variables(param, port, pi.hProcess, pi.dwProcessId))
3061 * log made by save_backend_variables, but we have to clean
3062 * up the mess with the half-started process
3064 if (!TerminateProcess(pi.hProcess, 255))
3066 (errmsg_internal("could not terminate unstarted process: error code %d",
3067 (int) GetLastError())));
3068 CloseHandle(pi.hProcess);
3069 CloseHandle(pi.hThread);
3070 return -1; /* log made by save_backend_variables */
3073 /* Drop the shared memory that is now inherited to the backend */
3074 if (!UnmapViewOfFile(param))
3075 elog(LOG, "could not unmap view of backend parameter file: error code %d",
3076 (int) GetLastError());
3077 if (!CloseHandle(paramHandle))
3078 elog(LOG, "could not close handle to backend parameter file: error code %d",
3079 (int) GetLastError());
3082 * Now that the backend variables are written out, we start the
3083 * child thread so it can start initializing while we set up
3084 * the rest of the parent state.
3086 if (ResumeThread(pi.hThread) == -1)
3088 if (!TerminateProcess(pi.hProcess, 255))
3091 (errmsg_internal("could not terminate unstartable process: error code %d",
3092 (int) GetLastError())));
3093 CloseHandle(pi.hProcess);
3094 CloseHandle(pi.hThread);
3097 CloseHandle(pi.hProcess);
3098 CloseHandle(pi.hThread);
3100 (errmsg_internal("could not resume thread of unstarted process: error code %d",
3101 (int) GetLastError())));
3105 if (!IsUnderPostmaster)
3107 /* We are the Postmaster creating a child... */
3108 win32_AddChild(pi.dwProcessId, pi.hProcess);
3111 /* Set up the thread to handle the SIGCHLD for this process */
3112 if (DuplicateHandle(GetCurrentProcess(),
3114 GetCurrentProcess(),
3118 DUPLICATE_SAME_ACCESS) == 0)
3120 (errmsg_internal("could not duplicate child handle: error code %d",
3121 (int) GetLastError())));
3123 waiterThread = CreateThread(NULL, 64 * 1024, win32_sigchld_waiter,
3124 (LPVOID) childHandleCopy, 0, NULL);
3127 (errmsg_internal("could not create sigchld waiter thread: error code %d",
3128 (int) GetLastError())));
3129 CloseHandle(waiterThread);
3131 if (IsUnderPostmaster)
3132 CloseHandle(pi.hProcess);
3133 CloseHandle(pi.hThread);
3135 return pi.dwProcessId;
3142 * SubPostmasterMain -- Get the fork/exec'd process into a state equivalent
3143 * to what it would be if we'd simply forked on Unix, and then
3144 * dispatch to the appropriate place.
3146 * The first two command line arguments are expected to be "-forkFOO"
3147 * (where FOO indicates which postmaster child we are to become), and
3148 * the name of a variables file that we can read to load data that would
3149 * have been inherited by fork() on Unix. Remaining arguments go to the
3150 * subprocess FooMain() routine.
3153 SubPostmasterMain(int argc, char *argv[])
3157 /* Do this sooner rather than later... */
3158 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
3160 MyProcPid = getpid(); /* reset MyProcPid */
3162 /* In EXEC_BACKEND case we will not have inherited these settings */
3163 IsPostmasterEnvironment = true;
3164 whereToSendOutput = None;
3166 /* Setup essential subsystems (to ensure elog() behaves sanely) */
3167 MemoryContextInit();
3168 InitializeGUCOptions();
3170 /* Read in the variables file */
3171 memset(&port, 0, sizeof(Port));
3172 read_backend_variables(argv[2], &port);
3174 /* Check we got appropriate args */
3176 elog(FATAL, "invalid subpostmaster invocation");
3179 * If appropriate, physically re-attach to shared memory segment.
3180 * We want to do this before going any further to ensure that we
3181 * can attach at the same address the postmaster used.
3183 if (strcmp(argv[1], "-forkbackend") == 0 ||
3184 strcmp(argv[1], "-forkboot") == 0)
3185 PGSharedMemoryReAttach();
3188 * Start our win32 signal implementation. This has to be done
3189 * after we read the backend variables, because we need to pick
3190 * up the signal pipe from the parent process.
3193 pgwin32_signal_initialize();
3196 /* In EXEC_BACKEND case we will not have inherited these settings */
3198 PG_SETMASK(&BlockSig);
3200 /* Read in remaining GUC variables */
3201 read_nondefault_variables();
3203 /* Run backend or appropriate child */
3204 if (strcmp(argv[1], "-forkbackend") == 0)
3206 /* BackendRun will close sockets */
3208 /* Attach process to shared data structures */
3209 CreateSharedMemoryAndSemaphores(false, MaxBackends, 0);
3213 * Need to reinitialize the SSL library in the backend,
3214 * since the context structures contain function pointers
3215 * and cannot be passed through the parameter file.
3218 secure_initialize();
3221 Assert(argc == 3); /* shouldn't be any more args */
3222 proc_exit(BackendRun(&port));
3224 if (strcmp(argv[1], "-forkboot") == 0)
3226 /* Close the postmaster's sockets */
3227 ClosePostmasterPorts(false);
3229 /* Attach process to shared data structures */
3230 CreateSharedMemoryAndSemaphores(false, MaxBackends, 0);
3232 BootstrapMain(argc - 2, argv + 2);
3235 if (strcmp(argv[1], "-forkarch") == 0)
3237 /* Close the postmaster's sockets */
3238 ClosePostmasterPorts(false);
3240 /* Do not want to attach to shared memory */
3242 PgArchiverMain(argc, argv);
3245 if (strcmp(argv[1], "-forkbuf") == 0)
3247 /* Close the postmaster's sockets */
3248 ClosePostmasterPorts(false);
3250 /* Do not want to attach to shared memory */
3252 PgstatBufferMain(argc, argv);
3255 if (strcmp(argv[1], "-forkcol") == 0)
3258 * Do NOT close postmaster sockets here, because we are forking
3259 * from pgstat buffer process, which already did it.
3262 /* Do not want to attach to shared memory */
3264 PgstatCollectorMain(argc, argv);
3267 if (strcmp(argv[1], "-forklog") == 0)
3269 /* Close the postmaster's sockets */
3270 ClosePostmasterPorts(true);
3272 /* Do not want to attach to shared memory */
3274 SysLoggerMain(argc, argv);
3278 return 1; /* shouldn't get here */
3281 #endif /* EXEC_BACKEND */
3285 * ExitPostmaster -- cleanup
3287 * Do NOT call exit() directly --- always go through here!
3290 ExitPostmaster(int status)
3292 /* should cleanup shared memory and kill all backends */
3295 * Not sure of the semantics here. When the Postmaster dies, should
3296 * the backends all be killed? probably not.
3298 * MUST -- vadim 05-10-1999
3305 * sigusr1_handler - handle signal conditions from child processes
3308 sigusr1_handler(SIGNAL_ARGS)
3310 int save_errno = errno;
3312 PG_SETMASK(&BlockSig);
3314 if (CheckPostmasterSignal(PMSIGNAL_PASSWORD_CHANGE))
3317 * Password or group file has changed.
3323 if (CheckPostmasterSignal(PMSIGNAL_WAKEN_CHILDREN))
3326 * Send SIGUSR1 to all children (triggers
3327 * CatchupInterruptHandler). See storage/ipc/sinval[adt].c for the
3330 if (Shutdown <= SmartShutdown)
3331 SignalChildren(SIGUSR1);
3334 if (PgArchPID != 0 && Shutdown == NoShutdown)
3336 if (CheckPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER))
3339 * Send SIGUSR1 to archiver process, to wake it up and begin
3340 * archiving next transaction log file.
3342 kill(PgArchPID, SIGUSR1);
3346 PG_SETMASK(&UnBlockSig);
3353 * Dummy signal handler
3355 * We use this for signals that we don't actually use in the postmaster,
3356 * but we do use in backends. If we were to SIG_IGN such signals in the
3357 * postmaster, then a newly started backend might drop a signal that arrives
3358 * before it's able to reconfigure its signal processing. (See notes in
3362 dummy_handler(SIGNAL_ARGS)
3368 * CharRemap: given an int in range 0..61, produce textual encoding of it
3369 * per crypt(3) conventions.
3393 RandomSalt(char *cryptSalt, char *md5Salt)
3395 long rand = PostmasterRandom();
3397 cryptSalt[0] = CharRemap(rand % 62);
3398 cryptSalt[1] = CharRemap(rand / 62);
3401 * It's okay to reuse the first random value for one of the MD5 salt
3402 * bytes, since only one of the two salts will be sent to the client.
3403 * After that we need to compute more random bits.
3405 * We use % 255, sacrificing one possible byte value, so as to ensure
3406 * that all bits of the random() value participate in the result.
3407 * While at it, add one to avoid generating any null bytes.
3409 md5Salt[0] = (rand % 255) + 1;
3410 rand = PostmasterRandom();
3411 md5Salt[1] = (rand % 255) + 1;
3412 rand = PostmasterRandom();
3413 md5Salt[2] = (rand % 255) + 1;
3414 rand = PostmasterRandom();
3415 md5Salt[3] = (rand % 255) + 1;
3422 PostmasterRandom(void)
3424 static bool initialized = false;
3428 Assert(random_seed != 0);
3429 srandom(random_seed);
3437 * Count up number of child processes (regular backends only)
3445 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
3452 * StartChildProcess -- start a non-backend child process for the postmaster
3454 * xlog determines what kind of child will be started. All child types
3455 * initially go to BootstrapMain, which will handle common setup.
3457 * Return value of StartChildProcess is subprocess' PID, or 0 if failed
3458 * to start subprocess.
3461 StartChildProcess(int xlop)
3468 #ifdef LINUX_PROFILE
3469 struct itimerval prof_itimer;
3473 * Set up command-line arguments for subprocess
3475 av[ac++] = "postgres";
3478 av[ac++] = "-forkboot";
3479 av[ac++] = NULL; /* filled in by postmaster_forkexec */
3482 snprintf(xlbuf, sizeof(xlbuf), "-x%d", xlop);
3486 av[ac++] = "template1";
3489 Assert(ac < lengthof(av));
3492 * Flush stdio channels (see comments in BackendStartup)
3499 pid = postmaster_forkexec(ac, av);
3501 #else /* !EXEC_BACKEND */
3503 #ifdef LINUX_PROFILE
3504 /* see comments in BackendStartup */
3505 getitimer(ITIMER_PROF, &prof_itimer);
3509 /* Specific beos actions before backend startup */
3510 beos_before_backend_startup();
3515 if (pid == 0) /* child */
3517 #ifdef LINUX_PROFILE
3518 setitimer(ITIMER_PROF, &prof_itimer, NULL);
3522 /* Specific beos actions after backend startup */
3523 beos_backend_startup();
3526 IsUnderPostmaster = true; /* we are a postmaster subprocess
3529 /* Close the postmaster's sockets */
3530 ClosePostmasterPorts(false);
3532 /* Lose the postmaster's on-exit routines and port connections */
3535 /* Release postmaster's working memory context */
3536 MemoryContextSwitchTo(TopMemoryContext);
3537 MemoryContextDelete(PostmasterContext);
3538 PostmasterContext = NULL;
3540 BootstrapMain(ac, av);
3543 #endif /* EXEC_BACKEND */
3547 /* in parent, fork failed */
3548 int save_errno = errno;
3551 /* Specific beos actions before backend startup */
3552 beos_backend_startup_failed();
3557 case BS_XLOG_STARTUP:
3559 (errmsg("could not fork startup process: %m")));
3561 case BS_XLOG_BGWRITER:
3563 (errmsg("could not fork background writer process: %m")));
3567 (errmsg("could not fork process: %m")));
3572 * fork failure is fatal during startup, but there's no need to
3573 * choke immediately if starting other child types fails.
3575 if (xlop == BS_XLOG_STARTUP)
3581 * in parent, successful fork
3588 * Create the opts file
3591 CreateOptsFile(int argc, char *argv[], char *fullprogname)
3593 char filename[MAXPGPATH];
3597 snprintf(filename, sizeof(filename), "%s/postmaster.opts", DataDir);
3599 if ((fp = fopen(filename, "w")) == NULL)
3601 elog(LOG, "could not create file \"%s\": %m", filename);
3605 fprintf(fp, "%s", fullprogname);
3606 for (i = 1; i < argc; i++)
3607 fprintf(fp, " %s%s%s", SYSTEMQUOTE, argv[i], SYSTEMQUOTE);
3612 elog(LOG, "could not write file \"%s\": %m", filename);
3623 * The following need to be available to the save/restore_backend_variables
3626 extern slock_t *ShmemLock;
3627 extern slock_t *ShmemIndexLock;
3628 extern void *ShmemIndexAlloc;
3629 extern LWLock *LWLockArray;
3630 extern slock_t *ProcStructLock;
3631 extern int pgStatSock;
3632 extern int pgStatPipe[2];
3635 #define write_inheritable_socket(dest, src, childpid) (*(dest) = (src))
3636 #define read_inheritable_socket(dest, src) (*(dest) = *(src))
3638 static void write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE child);
3639 static void write_inheritable_socket(InheritableSocket *dest, SOCKET src,
3641 static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
3645 /* Save critical backend variables into the BackendParameters struct */
3648 save_backend_variables(BackendParameters *param, Port *port)
3651 save_backend_variables(BackendParameters *param, Port *port,
3652 HANDLE childProcess, pid_t childPid)
3655 memcpy(¶m->port, port, sizeof(Port));
3656 write_inheritable_socket(¶m->portsocket, port->sock, childPid);
3658 StrNCpy(param->DataDir, DataDir, MAXPGPATH);
3660 memcpy(¶m->ListenSocket, &ListenSocket, sizeof(ListenSocket));
3662 param->MyCancelKey = MyCancelKey;
3664 param->UsedShmemSegID = UsedShmemSegID;
3665 param->UsedShmemSegAddr = UsedShmemSegAddr;
3667 param->ShmemLock = ShmemLock;
3668 param->ShmemIndexLock = ShmemIndexLock;
3669 param->ShmemVariableCache = ShmemVariableCache;
3670 param->ShmemIndexAlloc = ShmemIndexAlloc;
3671 param->ShmemBackendArray = ShmemBackendArray;
3673 param->LWLockArray = LWLockArray;
3674 param->ProcStructLock = ProcStructLock;
3675 write_inheritable_socket(¶m->pgStatSock, pgStatSock, childPid);
3676 write_inheritable_socket(¶m->pgStatPipe0, pgStatPipe[0], childPid);
3677 write_inheritable_socket(¶m->pgStatPipe1, pgStatPipe[1], childPid);
3679 param->PostmasterPid = PostmasterPid;
3682 param->PostmasterHandle = PostmasterHandle;
3683 write_duplicated_handle(¶m->initial_signal_pipe,
3684 pgwin32_create_signal_listener(childPid),
3688 memcpy(¶m->syslogPipe, &syslogPipe, sizeof(syslogPipe));
3690 StrNCpy(param->my_exec_path, my_exec_path, MAXPGPATH);
3692 StrNCpy(param->ExtraOptions, ExtraOptions, MAXPGPATH);
3694 StrNCpy(param->lc_collate, setlocale(LC_COLLATE, NULL), LOCALE_NAME_BUFLEN);
3695 StrNCpy(param->lc_ctype, setlocale(LC_CTYPE, NULL), LOCALE_NAME_BUFLEN);
3703 * Duplicate a handle for usage in a child process, and write the child
3704 * process instance of the handle to the parameter file.
3707 write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE childProcess)
3709 HANDLE hChild = INVALID_HANDLE_VALUE;
3711 if (!DuplicateHandle(GetCurrentProcess(),
3717 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
3719 (errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %d",
3720 (int) GetLastError())));
3726 * Duplicate a socket for usage in a child process, and write the resulting
3727 * structure to the parameter file.
3728 * This is required because a number of LSPs (Layered Service Providers) very
3729 * common on Windows (antivirus, firewalls, download managers etc) break
3730 * straight socket inheritance.
3733 write_inheritable_socket(InheritableSocket *dest, SOCKET src, pid_t childpid)
3735 dest->origsocket = src;
3736 if (src != 0 && src != -1)
3739 if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
3741 (errmsg("could not duplicate socket %d for use in backend: error code %d",
3742 src, WSAGetLastError())));
3747 * Read a duplicate socket structure back, and get the socket descriptor.
3750 read_inheritable_socket(SOCKET *dest, InheritableSocket *src)
3754 if (src->origsocket == -1 || src->origsocket == 0)
3756 /* Not a real socket! */
3757 *dest = src->origsocket;
3761 /* Actual socket, so create from structure */
3762 s = WSASocket(FROM_PROTOCOL_INFO,
3768 if (s == INVALID_SOCKET)
3770 write_stderr("could not create inherited socket: error code %d\n",
3777 * To make sure we don't get two references to the same socket,
3778 * close the original one. (This would happen when inheritance
3781 closesocket(src->origsocket);
3787 read_backend_variables(char *id, Port *port)
3789 BackendParameters param;
3792 /* Non-win32 implementation reads from file */
3796 fp = AllocateFile(id, PG_BINARY_R);
3799 write_stderr("could not read from backend variables file \"%s\": %s\n",
3800 id, strerror(errno));
3804 if (fread(¶m, sizeof(param), 1, fp) != 1)
3806 write_stderr("could not read from backend variables file \"%s\": %s\n",
3807 id, strerror(errno));
3813 if (unlink(id) != 0)
3815 write_stderr("could not remove file \"%s\": %s\n",
3816 id, strerror(errno));
3820 /* Win32 version uses mapped file */
3822 BackendParameters *paramp;
3824 paramHandle = (HANDLE)atol(id);
3825 paramp = MapViewOfFile(paramHandle, FILE_MAP_READ, 0, 0, 0);
3828 write_stderr("could not map view of backend variables: error code %d\n",
3829 (int) GetLastError());
3833 memcpy(¶m, paramp, sizeof(BackendParameters));
3835 if (!UnmapViewOfFile(paramp))
3837 write_stderr("could not unmap view of backend variables: error code %d\n",
3838 (int) GetLastError());
3842 if (!CloseHandle(paramHandle))
3844 write_stderr("could not close handle to backend parameter variables: error code %d\n",
3845 (int) GetLastError());
3850 restore_backend_variables(¶m, port);
3853 /* Restore critical backend variables from the BackendParameters struct */
3855 restore_backend_variables(BackendParameters *param, Port *port)
3857 memcpy(port, ¶m->port, sizeof(Port));
3858 read_inheritable_socket(&port->sock, ¶m->portsocket);
3860 SetDataDir(param->DataDir);
3862 memcpy(&ListenSocket, ¶m->ListenSocket, sizeof(ListenSocket));
3864 MyCancelKey = param->MyCancelKey;
3866 UsedShmemSegID = param->UsedShmemSegID;
3867 UsedShmemSegAddr = param->UsedShmemSegAddr;
3869 ShmemLock = param->ShmemLock;
3870 ShmemIndexLock = param->ShmemIndexLock;
3871 ShmemVariableCache = param->ShmemVariableCache;
3872 ShmemIndexAlloc = param->ShmemIndexAlloc;
3873 ShmemBackendArray = param->ShmemBackendArray;
3875 LWLockArray = param->LWLockArray;
3876 ProcStructLock = param->ProcStructLock;
3877 read_inheritable_socket(&pgStatSock, ¶m->pgStatSock);
3878 read_inheritable_socket(&pgStatPipe[0], ¶m->pgStatPipe0);
3879 read_inheritable_socket(&pgStatPipe[1], ¶m->pgStatPipe1);
3881 PostmasterPid = param->PostmasterPid;
3884 PostmasterHandle = param->PostmasterHandle;
3885 pgwin32_initial_signal_pipe = param->initial_signal_pipe;
3888 memcpy(&syslogPipe, ¶m->syslogPipe, sizeof(syslogPipe));
3890 StrNCpy(my_exec_path, param->my_exec_path, MAXPGPATH);
3892 StrNCpy(ExtraOptions, param->ExtraOptions, MAXPGPATH);
3894 setlocale(LC_COLLATE, param->lc_collate);
3895 setlocale(LC_CTYPE, param->lc_ctype);
3900 ShmemBackendArraySize(void)
3902 return (NUM_BACKENDARRAY_ELEMS * sizeof(Backend));
3906 ShmemBackendArrayAllocation(void)
3908 size_t size = ShmemBackendArraySize();
3910 ShmemBackendArray = (Backend *) ShmemAlloc(size);
3911 /* Mark all slots as empty */
3912 memset(ShmemBackendArray, 0, size);
3916 ShmemBackendArrayAdd(Backend *bn)
3920 /* Find an empty slot */
3921 for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++)
3923 if (ShmemBackendArray[i].pid == 0)
3925 ShmemBackendArray[i] = *bn;
3931 (errmsg_internal("no free slots in shmem backend array")));
3935 ShmemBackendArrayRemove(pid_t pid)
3939 for (i = 0; i < NUM_BACKENDARRAY_ELEMS; i++)
3941 if (ShmemBackendArray[i].pid == pid)
3943 /* Mark the slot as empty */
3944 ShmemBackendArray[i].pid = 0;
3950 (errmsg_internal("could not find backend entry with pid %d",
3954 #endif /* EXEC_BACKEND */
3960 * Note: The following three functions must not be interrupted (eg. by
3961 * signals). As the Postgres Win32 signalling architecture (currently)
3962 * requires polling, or APC checking functions which aren't used here, this
3965 * We keep two separate arrays, instead of a single array of pid/HANDLE
3966 * structs, to avoid having to re-create a handle array for
3967 * WaitForMultipleObjects on each call to win32_waitpid.
3971 win32_AddChild(pid_t pid, HANDLE handle)
3973 Assert(win32_childPIDArray && win32_childHNDArray);
3974 if (win32_numChildren < NUM_BACKENDARRAY_ELEMS)
3976 win32_childPIDArray[win32_numChildren] = pid;
3977 win32_childHNDArray[win32_numChildren] = handle;
3978 ++win32_numChildren;
3982 (errmsg_internal("no room for child entry with pid %lu",
3983 (unsigned long) pid)));
3987 win32_RemoveChild(pid_t pid)
3991 Assert(win32_childPIDArray && win32_childHNDArray);
3993 for (i = 0; i < win32_numChildren; i++)
3995 if (win32_childPIDArray[i] == pid)
3997 CloseHandle(win32_childHNDArray[i]);
3999 /* Swap last entry into the "removed" one */
4000 --win32_numChildren;
4001 win32_childPIDArray[i] = win32_childPIDArray[win32_numChildren];
4002 win32_childHNDArray[i] = win32_childHNDArray[win32_numChildren];
4008 (errmsg_internal("could not find child entry with pid %lu",
4009 (unsigned long) pid)));
4013 win32_waitpid(int *exitstatus)
4016 * Note: Do NOT use WaitForMultipleObjectsEx, as we don't want to run
4022 unsigned long offset;
4024 Assert(win32_childPIDArray && win32_childHNDArray);
4025 elog(DEBUG3, "waiting on %lu children", win32_numChildren);
4027 for (offset = 0; offset < win32_numChildren; offset += MAXIMUM_WAIT_OBJECTS)
4029 unsigned long num = Min(MAXIMUM_WAIT_OBJECTS, win32_numChildren - offset);
4031 ret = WaitForMultipleObjects(num, &win32_childHNDArray[offset], FALSE, 0);
4036 (errmsg_internal("failed to wait on %lu of %lu children: error code %d",
4037 num, win32_numChildren, (int) GetLastError())));
4041 /* No children (in this chunk) have finished */
4047 * Get the exit code, and return the PID of, the
4048 * respective process
4050 index = offset + ret - WAIT_OBJECT_0;
4051 Assert(index >= 0 && index < win32_numChildren);
4052 if (!GetExitCodeProcess(win32_childHNDArray[index], &exitCode))
4055 * If we get this far, this should never happen, but,
4056 * then again... No choice other than to assume a
4057 * catastrophic failure.
4060 (errmsg_internal("failed to get exit code for child %lu",
4061 (unsigned long) win32_childPIDArray[index])));
4063 *exitstatus = (int) exitCode;
4064 return win32_childPIDArray[index];
4068 /* No children have finished */
4073 * Note! Code below executes on separate threads, one for
4074 * each child process created
4077 win32_sigchld_waiter(LPVOID param)
4079 HANDLE procHandle = (HANDLE) param;
4081 DWORD r = WaitForSingleObject(procHandle, INFINITE);
4083 if (r == WAIT_OBJECT_0)
4084 pg_queue_signal(SIGCHLD);
4086 write_stderr("could not wait on child process handle: error code %d\n",
4087 (int) GetLastError());
4088 CloseHandle(procHandle);