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, shutdown, and periodic checkpoints. The postmaster
11 * itself doesn't do those operations, mind you --- it just forks
12 * off a subprocess to do them at the right times. It also takes
13 * care of resetting the system if a backend crashes.
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-2003, PostgreSQL Global Development Group
36 * Portions Copyright (c) 1994, Regents of the University of California
40 * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.348 2003/11/11 01:09:42 momjian Exp $
45 * The Postmaster sets up a few shared memory data structures
46 * for the backends. It should at the very least initialize the
50 * The Postmaster shares memory with the backends but should avoid
51 * touching shared memory, so as not to become stuck if a crashing
52 * backend screws up locks or shared memory. Likewise, the Postmaster
53 * should never block on messages from frontend clients.
56 * The Postmaster cleans up after backends if they have an emergency
57 * exit and/or core dump.
59 *-------------------------------------------------------------------------
70 #include <sys/socket.h>
74 #include <sys/param.h>
75 #include <netinet/in.h>
76 #include <arpa/inet.h>
80 #ifdef HAVE_SYS_SELECT_H
81 #include <sys/select.h>
89 #include <DNSServiceDiscovery/DNSServiceDiscovery.h>
92 #include "catalog/pg_database.h"
93 #include "commands/async.h"
94 #include "lib/dllist.h"
95 #include "libpq/auth.h"
96 #include "libpq/crypt.h"
97 #include "libpq/libpq.h"
98 #include "libpq/pqcomm.h"
99 #include "libpq/pqsignal.h"
100 #include "miscadmin.h"
101 #include "nodes/nodes.h"
102 #include "storage/fd.h"
103 #include "storage/ipc.h"
104 #include "storage/pg_shmem.h"
105 #include "storage/pmsignal.h"
106 #include "storage/proc.h"
107 #include "access/xlog.h"
108 #include "tcop/tcopprot.h"
109 #include "utils/guc.h"
110 #include "utils/memutils.h"
111 #include "utils/ps_status.h"
112 #include "bootstrap/bootstrap.h"
116 #define INVALID_SOCK (-1)
118 #ifdef HAVE_SIGPROCMASK
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.
137 pid_t pid; /* process id of backend */
138 long cancel_key; /* cancel key for cancels for this backend */
141 static Dllist *BackendList;
143 /* The socket number we are listening for connections on */
149 * MaxBackends is the limit on the number of backends we can start.
150 * Note that a larger MaxBackends value will increase the size of the
151 * shared memory area as well as cause the postmaster to grab more
152 * kernel semaphores, even if you never actually use that many
158 * ReservedBackends is the number of backends reserved for superuser use.
159 * This number is taken out of the pool size given by MaxBackends so
160 * number of backend slots available to non-superusers is
161 * (MaxBackends - ReservedBackends). Note what this really means is
162 * "if there are <= ReservedBackends connections available, only superusers
163 * can make new connections" --- pre-existing superuser connections don't
164 * count against the limit.
166 int ReservedBackends;
169 static char *progname = (char *) NULL;
171 /* The socket(s) we're listening to. */
173 static int ListenSocket[MAXLISTEN];
175 /* Used to reduce macros tests */
177 const bool ExecBackend = true;
180 const bool ExecBackend = false;
184 * Set by the -o option
186 static char ExtraOptions[MAXPGPATH];
189 * These globals control the behavior of the postmaster in case some
190 * backend dumps core. Normally, it kills all peers of the dead backend
191 * and reinitializes shared memory. By specifying -s or -n, we can have
192 * the postmaster stop (rather than kill) peers and not reinitialize
193 * shared data structures.
195 static bool Reinit = true;
196 static int SendStop = false;
198 /* still more option variables */
199 bool NetServer = false; /* listen on TCP/IP */
200 bool EnableSSL = false;
201 bool SilentMode = false; /* silent mode (-S) */
203 int PreAuthDelay = 0;
204 int AuthenticationTimeout = 60;
205 int CheckPointTimeout = 300;
206 int CheckPointWarning = 30;
207 time_t LastSignalledCheckpoint = 0;
209 bool log_hostname; /* for ps display */
211 bool Log_connections = false;
212 bool Db_user_namespace = false;
214 char *rendezvous_name;
216 /* For FNCTL_NONBLOCK */
217 #if defined(WIN32) || defined(__BEOS__)
218 long ioctlsocket_ret=1;
221 /* list of library:init-function to be preloaded */
222 char *preload_libraries_string = NULL;
224 /* Startup/shutdown state */
225 static pid_t StartupPID = 0,
228 static time_t checkpointed = 0;
231 #define SmartShutdown 1
232 #define FastShutdown 2
234 static int Shutdown = NoShutdown;
236 static bool FatalError = false; /* T if recovering from backend crash */
238 bool ClientAuthInProgress = false; /* T during new-client
242 * State for assigning random salts and cancel keys.
243 * Also, the global MyCancelKey passes the cancel key assigned to a given
244 * backend from the postmaster to that backend (via fork).
247 static unsigned int random_seed = 0;
249 static int debug_flag = 0;
255 #ifdef HAVE_INT_OPTRESET
260 * postmaster.c - function prototypes
262 static void pmdaemonize(int argc, char *argv[]);
263 static Port *ConnCreate(int serverFd);
264 static void ConnFree(Port *port);
265 static void reset_shared(unsigned short port);
266 static void SIGHUP_handler(SIGNAL_ARGS);
267 static void pmdie(SIGNAL_ARGS);
268 static void reaper(SIGNAL_ARGS);
269 static void sigusr1_handler(SIGNAL_ARGS);
270 static void dummy_handler(SIGNAL_ARGS);
271 static void CleanupProc(int pid, int exitstatus);
272 static void LogChildExit(int lev, const char *procname,
273 int pid, int exitstatus);
274 static int BackendFork(Port *port);
275 static void ExitPostmaster(int status);
276 static void usage(const char *);
277 static int ServerLoop(void);
278 static int BackendStartup(Port *port);
279 static int ProcessStartupPacket(Port *port, bool SSLdone);
280 static void processCancelRequest(Port *port, void *pkt);
281 static int initMasks(fd_set *rmask);
282 static void report_fork_failure_to_client(Port *port, int errnum);
285 CAC_OK, CAC_STARTUP, CAC_SHUTDOWN, CAC_RECOVERY, CAC_TOOMANY
287 static enum CAC_state canAcceptConnections(void);
288 static long PostmasterRandom(void);
289 static void RandomSalt(char *cryptSalt, char *md5Salt);
290 static void SignalChildren(int signal);
291 static int CountChildren(void);
292 static bool CreateOptsFile(int argc, char *argv[]);
293 static pid_t SSDataBase(int xlop);
295 postmaster_error(const char *fmt,...)
296 /* This lets gcc check the format string for consistency. */
297 __attribute__((format(printf, 1, 2)));
299 #define StartupDataBase() SSDataBase(BS_XLOG_STARTUP)
300 #define CheckPointDataBase() SSDataBase(BS_XLOG_CHECKPOINT)
301 #define ShutdownDataBase() SSDataBase(BS_XLOG_SHUTDOWN)
305 checkDataDir(const char *checkdir)
307 char path[MAXPGPATH];
309 struct stat stat_buf;
311 if (checkdir == NULL)
314 gettext("%s does not know where to find the database system data.\n"
315 "You must specify the directory that contains the database system\n"
316 "either by specifying the -D invocation option or by setting the\n"
317 "PGDATA environment variable.\n"),
322 if (stat(checkdir, &stat_buf) == -1)
326 (errcode_for_file_access(),
327 errmsg("data directory \"%s\" does not exist",
331 (errcode_for_file_access(),
332 errmsg("could not read permissions of directory \"%s\": %m",
337 * Check if the directory has group or world access. If so, reject.
339 * XXX temporarily suppress check when on Windows, because there may not
340 * be proper support for Unix-y file permissions. Need to think of a
341 * reasonable check to apply on Windows.
343 #if !defined(__CYGWIN__) && !defined(WIN32)
344 if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
346 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
347 errmsg("data directory \"%s\" has group or world access",
349 errdetail("Permissions should be u=rwx (0700).")));
352 /* Look for PG_VERSION before looking for pg_control */
353 ValidatePgVersion(checkdir);
355 snprintf(path, sizeof(path), "%s/global/pg_control", checkdir);
357 fp = AllocateFile(path, PG_BINARY_R);
361 gettext("%s: could not find the database system\n"
362 "Expected to find it in the directory \"%s\",\n"
363 "but could not open file \"%s\": %s\n"),
364 progname, checkdir, path, strerror(errno));
371 #ifdef USE_RENDEZVOUS
373 /* reg_reply -- empty callback function for DNSServiceRegistrationCreate() */
375 reg_reply(DNSServiceRegistrationReplyErrorType errorCode, void *context)
382 PostmasterMain(int argc, char *argv[])
386 char original_extraoptions[MAXPGPATH];
387 char *potential_DataDir = NULL;
390 *original_extraoptions = '\0';
394 IsPostmasterEnvironment = true;
397 * Catch standard options before doing much else. This even works on
398 * systems without getopt_long.
402 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
407 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
409 puts("postmaster (PostgreSQL) " PG_VERSION);
415 * for security, no dir or file created can be group or other
418 umask((mode_t) 0077);
420 MyProcPid = getpid();
423 * Fire up essential subsystems: memory management
428 * By default, palloc() requests in the postmaster will be allocated
429 * in the PostmasterContext, which is space that can be recycled by
430 * backends. Allocated data that needs to be available to backends
431 * should be allocated in TopMemoryContext.
433 PostmasterContext = AllocSetContextCreate(TopMemoryContext,
435 ALLOCSET_DEFAULT_MINSIZE,
436 ALLOCSET_DEFAULT_INITSIZE,
437 ALLOCSET_DEFAULT_MAXSIZE);
438 MemoryContextSwitchTo(PostmasterContext);
440 IgnoreSystemIndexes(false);
445 InitializeGUCOptions();
447 potential_DataDir = getenv("PGDATA"); /* default value */
451 while ((opt = getopt(argc, argv, "A:a:B:b:c:D:d:Fh:ik:lm:MN:no:p:Ss-:")) != -1)
456 #ifdef USE_ASSERT_CHECKING
457 SetConfigOption("debug_assertions", optarg, PGC_POSTMASTER, PGC_S_ARGV);
459 postmaster_error("assert checking is not compiled in");
463 /* Can no longer set authentication method. */
466 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
469 /* Can no longer set the backend executable file to use. */
472 potential_DataDir = optarg;
476 /* Turn on debugging for the postmaster. */
477 char *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
479 sprintf(debugstr, "debug%s", optarg);
480 SetConfigOption("log_min_messages", debugstr,
481 PGC_POSTMASTER, PGC_S_ARGV);
483 debug_flag = atoi(optarg);
487 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
490 SetConfigOption("virtual_host", optarg, PGC_POSTMASTER, PGC_S_ARGV);
493 SetConfigOption("tcpip_socket", "true", PGC_POSTMASTER, PGC_S_ARGV);
496 SetConfigOption("unix_socket_directory", optarg, PGC_POSTMASTER, PGC_S_ARGV);
500 SetConfigOption("ssl", "true", PGC_POSTMASTER, PGC_S_ARGV);
504 /* Multiplexed backends no longer supported. */
509 * ignore this flag. This may be passed in because the
510 * program was run as 'postgres -M' instead of
515 /* The max number of backends to start. */
516 SetConfigOption("max_connections", optarg, PGC_POSTMASTER, PGC_S_ARGV);
519 /* Don't reinit shared mem after abnormal exit */
525 * Other options to pass to the backend on the command
526 * line -- useful only for debugging.
528 strcat(ExtraOptions, " ");
529 strcat(ExtraOptions, optarg);
530 strcpy(original_extraoptions, optarg);
533 SetConfigOption("port", optarg, PGC_POSTMASTER, PGC_S_ARGV);
538 * Start in 'S'ilent mode (disassociate from controlling
539 * tty). You may also think of this as 'S'ysV mode since
540 * it's most badly needed on SysV-derived systems like
543 SetConfigOption("silent_mode", "true", PGC_POSTMASTER, PGC_S_ARGV);
548 * In the event that some backend dumps core, send
549 * SIGSTOP, rather than SIGQUIT, to all its peers. This
550 * lets the wily post_hacker collect core dumps from
561 ParseLongOption(optarg, &name, &value);
566 (errcode(ERRCODE_SYNTAX_ERROR),
567 errmsg("--%s requires a value",
571 (errcode(ERRCODE_SYNTAX_ERROR),
572 errmsg("-c %s requires a value",
576 SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
585 gettext("Try \"%s --help\" for more information.\n"),
592 * Postmaster accepts no non-option switch arguments.
596 postmaster_error("invalid argument: \"%s\"", argv[optind]);
598 gettext("Try \"%s --help\" for more information.\n"),
604 * Now we can set the data directory, and then read postgresql.conf.
606 checkDataDir(potential_DataDir); /* issues error messages */
607 SetDataDir(potential_DataDir);
609 ProcessConfigFile(PGC_POSTMASTER);
611 write_nondefault_variables(PGC_POSTMASTER);
615 * Check for invalid combinations of GUC settings.
617 if (NBuffers < 2 * MaxBackends || NBuffers < 16)
620 * Do not accept -B so small that backends are likely to starve
621 * for lack of buffers. The specific choices here are somewhat
624 postmaster_error("the number of buffers (-B) must be at least twice the number of allowed connections (-N) and at least 16");
628 if (ReservedBackends >= MaxBackends)
630 postmaster_error("superuser_reserved_connections must be less than max_connections");
635 * Other one-time internal sanity checks can go here.
637 if (!CheckDateTokenTables())
639 postmaster_error("invalid datetoken tables, please fix");
644 * Now that we are done processing the postmaster arguments, reset
645 * getopt(3) library so that it will work correctly in subprocesses.
648 #ifdef HAVE_INT_OPTRESET
649 optreset = 1; /* some systems need this too */
652 /* For debugging: display postmaster environment */
654 extern char **environ;
658 (errmsg_internal("%s: PostmasterMain: initial environ dump:",
661 (errmsg_internal("-----------------------------------------")));
662 for (p = environ; *p; ++p)
664 (errmsg_internal("\t%s", *p)));
666 (errmsg_internal("-----------------------------------------")));
670 * On some systems our dynloader code needs the executable's pathname.
672 if (FindExec(pg_pathname, progname, "postgres") < 0)
674 (errmsg("%s: could not locate postgres executable",
678 * Initialize SSL library, if specified.
681 if (EnableSSL && !NetServer)
683 postmaster_error("TCP/IP connections must be enabled for SSL");
691 * process any libraries that should be preloaded and optionally
694 if (preload_libraries_string)
695 process_preload_libraries(preload_libraries_string);
698 * Fork away from controlling terminal, if -S specified.
700 * Must do this before we grab any interlock files, else the interlocks
701 * will show the wrong PID.
704 pmdaemonize(argc, argv);
707 * Create lockfile for data directory.
709 * We want to do this before we try to grab the input sockets, because
710 * the data directory interlock is more reliable than the socket-file
711 * interlock (thanks to whoever decided to put socket files in /tmp
712 * :-(). For the same reason, it's best to grab the TCP socket before
715 CreateDataDirLockFile(DataDir, true);
718 * Remove old temporary files. At this point there can be no other
719 * Postgres processes running in this directory, so this should be
725 * Establish input sockets.
727 for (i = 0; i < MAXLISTEN; i++)
728 ListenSocket[i] = -1;
732 if (VirtualHost && VirtualHost[0])
738 curhost = VirtualHost;
741 while (*curhost == ' ') /* skip any extra spaces */
743 if (*curhost == '\0')
745 endptr = strchr(curhost, ' ');
751 status = StreamServerPort(AF_UNSPEC, curhost,
752 (unsigned short) PostPortNumber,
754 ListenSocket, MAXLISTEN);
755 if (status != STATUS_OK)
757 (errmsg("could not create listen socket for \"%s\"",
762 curhost = endptr + 1;
770 status = StreamServerPort(AF_UNSPEC, NULL,
771 (unsigned short) PostPortNumber,
773 ListenSocket, MAXLISTEN);
774 if (status != STATUS_OK)
776 (errmsg("could not create TCP/IP listen socket")));
779 #ifdef USE_RENDEZVOUS
780 if (rendezvous_name != NULL)
782 DNSServiceRegistrationCreate(rendezvous_name,
785 htonl(PostPortNumber),
787 (DNSServiceRegistrationReply) reg_reply,
793 #ifdef HAVE_UNIX_SOCKETS
794 status = StreamServerPort(AF_UNIX, NULL,
795 (unsigned short) PostPortNumber,
797 ListenSocket, MAXLISTEN);
798 if (status != STATUS_OK)
800 (errmsg("could not create Unix-domain socket")));
806 * Set up shared memory and semaphores.
808 reset_shared(PostPortNumber);
811 * Initialize the list of active backends.
813 BackendList = DLNewList();
816 * Record postmaster options. We delay this till now to avoid
817 * recording bogus options (eg, NBuffers too high for available
820 if (!CreateOptsFile(argc, argv))
824 * Set up signal handlers for the postmaster process.
826 * CAUTION: when changing this list, check for side-effects on the signal
827 * handling setup of child processes. See tcop/postgres.c,
828 * bootstrap/bootstrap.c, and postmaster/pgstat.c.
831 PG_SETMASK(&BlockSig);
833 pqsignal(SIGHUP, SIGHUP_handler); /* reread config file and have
834 * children do same */
835 pqsignal(SIGINT, pmdie); /* send SIGTERM and ShutdownDataBase */
836 pqsignal(SIGQUIT, pmdie); /* send SIGQUIT and die */
837 pqsignal(SIGTERM, pmdie); /* wait for children and ShutdownDataBase */
838 pqsignal(SIGALRM, SIG_IGN); /* ignored */
839 pqsignal(SIGPIPE, SIG_IGN); /* ignored */
840 pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
841 pqsignal(SIGUSR2, dummy_handler); /* unused, reserve for children */
842 pqsignal(SIGCHLD, reaper); /* handle child termination */
843 pqsignal(SIGTTIN, SIG_IGN); /* ignored */
844 pqsignal(SIGTTOU, SIG_IGN); /* ignored */
845 /* ignore SIGXFSZ, so that ulimit violations work like disk full */
847 pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
851 * Reset whereToSendOutput from Debug (its starting state) to None.
852 * This prevents ereport from sending log messages to stderr unless
853 * the syslog/stderr switch permits. We don't do this until the
854 * postmaster is fully launched, since startup failures may as well be
855 * reported to stderr.
857 whereToSendOutput = None;
860 * On many platforms, the first call of localtime() incurs significant
861 * overhead to load timezone info from the system configuration files.
862 * By doing it once in the postmaster, we avoid having to do it in
863 * every started child process. The savings are not huge, but they
867 time_t now = time(NULL);
869 (void) localtime(&now);
873 * Initialize and try to startup the statistics collector process
879 * Load cached files for client authentication.
887 * We're ready to rock and roll...
889 StartupPID = StartupDataBase();
891 status = ServerLoop();
894 * ServerLoop probably shouldn't ever return, but if it does, close
897 ExitPostmaster(status != STATUS_OK);
899 return 0; /* not reached */
903 pmdaemonize(int argc, char *argv[])
909 struct itimerval prof_itimer;
913 /* see comments in BackendStartup */
914 getitimer(ITIMER_PROF, &prof_itimer);
918 if (pid == (pid_t) -1)
920 postmaster_error("could not fork background process: %s",
926 /* Parent should just exit, without doing any atexit cleanup */
931 setitimer(ITIMER_PROF, &prof_itimer, NULL);
934 MyProcPid = getpid(); /* reset MyProcPid to child */
936 /* GH: If there's no setsid(), we hopefully don't need silent mode.
937 * Until there's a better solution.
942 postmaster_error("could not dissociate from controlling TTY: %s",
947 i = open(NULL_DEV, O_RDWR | PG_BINARY);
957 * Print out help message
960 usage(const char *progname)
962 printf(gettext("%s is the PostgreSQL server.\n\n"), progname);
963 printf(gettext("Usage:\n %s [OPTION]...\n\n"), progname);
964 printf(gettext("Options:\n"));
965 #ifdef USE_ASSERT_CHECKING
966 printf(gettext(" -A 1|0 enable/disable run-time assert checking\n"));
968 printf(gettext(" -B NBUFFERS number of shared buffers\n"));
969 printf(gettext(" -c NAME=VALUE set run-time parameter\n"));
970 printf(gettext(" -d 1-5 debugging level\n"));
971 printf(gettext(" -D DATADIR database directory\n"));
972 printf(gettext(" -F turn fsync off\n"));
973 printf(gettext(" -h HOSTNAME host name or IP address to listen on\n"));
974 printf(gettext(" -i enable TCP/IP connections\n"));
975 printf(gettext(" -k DIRECTORY Unix-domain socket location\n"));
977 printf(gettext(" -l enable SSL connections\n"));
979 printf(gettext(" -N MAX-CONNECT maximum number of allowed connections\n"));
980 printf(gettext(" -o OPTIONS pass \"OPTIONS\" to each server process\n"));
981 printf(gettext(" -p PORT port number to listen on\n"));
982 printf(gettext(" -S silent mode (start in background without logging output)\n"));
983 printf(gettext(" --help show this help, then exit\n"));
984 printf(gettext(" --version output version information, then exit\n"));
986 printf(gettext("\nDeveloper options:\n"));
987 printf(gettext(" -n do not reinitialize shared memory after abnormal exit\n"));
988 printf(gettext(" -s send SIGSTOP to all backend servers if one dies\n"));
990 printf(gettext("\nPlease read the documentation for the complete list of run-time\n"
991 "configuration settings and how to set them on the command line or in\n"
992 "the configuration file.\n\n"
993 "Report bugs to <pgsql-bugs@postgresql.org>.\n"));
1006 gettimeofday(&now, &tz);
1008 nSockets = initMasks(&readmask);
1014 struct timeval timeout;
1017 * The timeout for the select() below is normally set on the basis
1018 * of the time to the next checkpoint. However, if for some
1019 * reason we don't have a next-checkpoint time, time out after 60
1020 * seconds. This keeps checkpoint scheduling from locking up when
1021 * we get new connection requests infrequently (since we are
1022 * likely to detect checkpoint completion just after enabling
1023 * signals below, after we've already made the decision about how
1024 * long to wait this time).
1026 timeout.tv_sec = 60;
1027 timeout.tv_usec = 0;
1029 if (CheckPointPID == 0 && checkpointed &&
1030 Shutdown == NoShutdown && !FatalError && random_seed != 0)
1032 time_t now = time(NULL);
1034 if (CheckPointTimeout + checkpointed > now)
1037 * Not time for checkpoint yet, so set select timeout
1039 timeout.tv_sec = CheckPointTimeout + checkpointed - now;
1043 /* Time to make the checkpoint... */
1044 CheckPointPID = CheckPointDataBase();
1047 * if fork failed, schedule another try at 0.1 normal
1050 if (CheckPointPID == 0)
1052 timeout.tv_sec = CheckPointTimeout / 10;
1053 checkpointed = now + timeout.tv_sec - CheckPointTimeout;
1059 * Wait for something to happen.
1061 memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
1063 PG_SETMASK(&UnBlockSig);
1065 if (select(nSockets, &rmask, (fd_set *) NULL,
1066 (fd_set *) NULL, &timeout) < 0)
1068 PG_SETMASK(&BlockSig);
1069 if (errno == EINTR || errno == EWOULDBLOCK)
1072 (errcode_for_socket_access(),
1073 errmsg("select() failed in postmaster: %m")));
1074 return STATUS_ERROR;
1078 * Block all signals until we wait again. (This makes it safe for
1079 * our signal handlers to do nontrivial work.)
1081 PG_SETMASK(&BlockSig);
1084 * Select a random seed at the time of first receiving a request.
1086 while (random_seed == 0)
1088 gettimeofday(&later, &tz);
1091 * We are not sure how much precision is in tv_usec, so we
1092 * swap the nibbles of 'later' and XOR them with 'now'. On the
1093 * off chance that the result is 0, we loop until it isn't.
1095 random_seed = now.tv_usec ^
1096 ((later.tv_usec << 16) |
1097 ((later.tv_usec >> 16) & 0xffff));
1101 * New connection pending on any of our sockets? If so, fork a
1102 * child process to deal with it.
1104 for (i = 0; i < MAXLISTEN; i++)
1106 if (ListenSocket[i] == -1)
1108 if (FD_ISSET(ListenSocket[i], &rmask))
1110 port = ConnCreate(ListenSocket[i]);
1113 BackendStartup(port);
1116 * We no longer need the open socket or port structure
1119 StreamClose(port->sock);
1125 /* If we have lost the stats collector, try to start a new one */
1126 if (!pgstat_is_running)
1133 * Initialise the masks for select() for the ports
1134 * we are listening on. Return the number of sockets to listen on.
1138 initMasks(fd_set *rmask)
1145 for (i = 0; i < MAXLISTEN; i++)
1147 int fd = ListenSocket[i];
1161 * Read the startup packet and do something according to it.
1163 * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
1164 * not return at all.
1166 * (Note that ereport(FATAL) stuff is sent to the client, so only use it
1167 * if that's what you want. Return STATUS_ERROR if you don't want to
1168 * send anything to the client, which would typically be appropriate
1169 * if we detect a communications failure.)
1172 ProcessStartupPacket(Port *port, bool SSLdone)
1177 ProtocolVersion proto;
1178 MemoryContext oldcontext;
1180 if (pq_getbytes((char *) &len, 4) == EOF)
1183 * EOF after SSLdone probably means the client didn't like our
1184 * response to NEGOTIATE_SSL_CODE. That's not an error condition,
1185 * so don't clutter the log with a complaint.
1189 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1190 errmsg("incomplete startup packet")));
1191 return STATUS_ERROR;
1197 if (len < (int32) sizeof(ProtocolVersion) ||
1198 len > MAX_STARTUP_PACKET_LENGTH)
1201 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1202 errmsg("invalid length of startup packet")));
1203 return STATUS_ERROR;
1207 * Allocate at least the size of an old-style startup packet, plus one
1208 * extra byte, and make sure all are zeroes. This ensures we will
1209 * have null termination of all strings, in both fixed- and
1210 * variable-length packet layouts.
1212 if (len <= (int32) sizeof(StartupPacket))
1213 buf = palloc0(sizeof(StartupPacket) + 1);
1215 buf = palloc0(len + 1);
1217 if (pq_getbytes(buf, len) == EOF)
1220 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1221 errmsg("incomplete startup packet")));
1222 return STATUS_ERROR;
1226 * The first field is either a protocol version number or a special
1229 port->proto = proto = ntohl(*((ProtocolVersion *) buf));
1231 if (proto == CANCEL_REQUEST_CODE)
1233 processCancelRequest(port, buf);
1234 return 127; /* XXX */
1237 if (proto == NEGOTIATE_SSL_CODE && !SSLdone)
1242 /* No SSL when disabled or on Unix sockets */
1243 if (!EnableSSL || IS_AF_UNIX(port->laddr.addr.ss_family))
1246 SSLok = 'S'; /* Support for SSL */
1248 SSLok = 'N'; /* No support for SSL */
1250 if (send(port->sock, &SSLok, 1, 0) != 1)
1253 (errcode_for_socket_access(),
1254 errmsg("failed to send SSL negotiation response: %m")));
1255 return STATUS_ERROR; /* close the connection */
1259 if (SSLok == 'S' && secure_open_server(port) == -1)
1260 return STATUS_ERROR;
1262 /* regular startup packet, cancel, etc packet should follow... */
1263 /* but not another SSL negotiation request */
1264 return ProcessStartupPacket(port, true);
1267 /* Could add additional special packet types here */
1270 * Set FrontendProtocol now so that ereport() knows what format to
1271 * send if we fail during startup.
1273 FrontendProtocol = proto;
1275 /* Check we can handle the protocol the frontend is using. */
1277 if (PG_PROTOCOL_MAJOR(proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) ||
1278 PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) ||
1279 (PG_PROTOCOL_MAJOR(proto) == PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) &&
1280 PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST)))
1282 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1283 errmsg("unsupported frontend protocol %u.%u: server supports %u.0 to %u.%u",
1284 PG_PROTOCOL_MAJOR(proto), PG_PROTOCOL_MINOR(proto),
1285 PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST),
1286 PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST),
1287 PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST))));
1290 * Now fetch parameters out of startup packet and save them into the
1291 * Port structure. All data structures attached to the Port struct
1292 * must be allocated in TopMemoryContext so that they won't disappear
1293 * when we pass them to PostgresMain (see BackendFork). We need not
1294 * worry about leaking this storage on failure, since we aren't in the
1295 * postmaster process anymore.
1297 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
1299 if (PG_PROTOCOL_MAJOR(proto) >= 3)
1301 int32 offset = sizeof(ProtocolVersion);
1304 * Scan packet body for name/option pairs. We can assume any
1305 * string beginning within the packet body is null-terminated,
1306 * thanks to zeroing extra byte above.
1308 port->guc_options = NIL;
1310 while (offset < len)
1312 char *nameptr = ((char *) buf) + offset;
1316 if (*nameptr == '\0')
1317 break; /* found packet terminator */
1318 valoffset = offset + strlen(nameptr) + 1;
1319 if (valoffset >= len)
1320 break; /* missing value, will complain below */
1321 valptr = ((char *) buf) + valoffset;
1323 if (strcmp(nameptr, "database") == 0)
1324 port->database_name = pstrdup(valptr);
1325 else if (strcmp(nameptr, "user") == 0)
1326 port->user_name = pstrdup(valptr);
1327 else if (strcmp(nameptr, "options") == 0)
1328 port->cmdline_options = pstrdup(valptr);
1331 /* Assume it's a generic GUC option */
1332 port->guc_options = lappend(port->guc_options,
1334 port->guc_options = lappend(port->guc_options,
1337 offset = valoffset + strlen(valptr) + 1;
1341 * If we didn't find a packet terminator exactly at the end of the
1342 * given packet length, complain.
1344 if (offset != len - 1)
1346 (errcode(ERRCODE_PROTOCOL_VIOLATION),
1347 errmsg("invalid startup packet layout: expected terminator as last byte")));
1352 * Get the parameters from the old-style, fixed-width-fields
1353 * startup packet as C strings. The packet destination was
1354 * cleared first so a short packet has zeros silently added. We
1355 * have to be prepared to truncate the pstrdup result for oversize
1358 StartupPacket *packet = (StartupPacket *) buf;
1360 port->database_name = pstrdup(packet->database);
1361 if (strlen(port->database_name) > sizeof(packet->database))
1362 port->database_name[sizeof(packet->database)] = '\0';
1363 port->user_name = pstrdup(packet->user);
1364 if (strlen(port->user_name) > sizeof(packet->user))
1365 port->user_name[sizeof(packet->user)] = '\0';
1366 port->cmdline_options = pstrdup(packet->options);
1367 if (strlen(port->cmdline_options) > sizeof(packet->options))
1368 port->cmdline_options[sizeof(packet->options)] = '\0';
1369 port->guc_options = NIL;
1372 /* Check a user name was given. */
1373 if (port->user_name == NULL || port->user_name[0] == '\0')
1375 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
1376 errmsg("no PostgreSQL user name specified in startup packet")));
1378 /* The database defaults to the user name. */
1379 if (port->database_name == NULL || port->database_name[0] == '\0')
1380 port->database_name = pstrdup(port->user_name);
1382 if (Db_user_namespace)
1385 * If user@, it is a global user, remove '@'. We only want to do
1386 * this if there is an '@' at the end and no earlier in the user
1387 * string or they may fake as a local user of another database
1388 * attaching to this database.
1390 if (strchr(port->user_name, '@') ==
1391 port->user_name + strlen(port->user_name) - 1)
1392 *strchr(port->user_name, '@') = '\0';
1395 /* Append '@' and dbname */
1398 db_user = palloc(strlen(port->user_name) +
1399 strlen(port->database_name) + 2);
1400 sprintf(db_user, "%s@%s", port->user_name, port->database_name);
1401 port->user_name = db_user;
1406 * Truncate given database and user names to length of a Postgres
1407 * name. This avoids lookup failures when overlength names are given.
1409 if (strlen(port->database_name) >= NAMEDATALEN)
1410 port->database_name[NAMEDATALEN - 1] = '\0';
1411 if (strlen(port->user_name) >= NAMEDATALEN)
1412 port->user_name[NAMEDATALEN - 1] = '\0';
1415 * Done putting stuff in TopMemoryContext.
1417 MemoryContextSwitchTo(oldcontext);
1420 * If we're going to reject the connection due to database state, say
1421 * so now instead of wasting cycles on an authentication exchange.
1422 * (This also allows a pg_ping utility to be written.)
1424 cac = canAcceptConnections();
1430 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1431 errmsg("the database system is starting up")));
1435 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1436 errmsg("the database system is shutting down")));
1440 (errcode(ERRCODE_CANNOT_CONNECT_NOW),
1441 errmsg("the database system is in recovery mode")));
1445 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
1446 errmsg("sorry, too many clients already")));
1458 * The client has sent a cancel request packet, not a normal
1459 * start-a-new-connection packet. Perform the necessary processing.
1460 * Nothing is sent back to the client.
1463 processCancelRequest(Port *port, void *pkt)
1465 CancelRequestPacket *canc = (CancelRequestPacket *) pkt;
1467 long cancelAuthCode;
1471 backendPID = (int) ntohl(canc->backendPID);
1472 cancelAuthCode = (long) ntohl(canc->cancelAuthCode);
1474 if (backendPID == CheckPointPID)
1477 (errmsg_internal("ignoring cancel request for checkpoint process %d",
1481 else if (ExecBackend)
1482 AttachSharedMemoryAndSemaphores();
1484 /* See if we have a matching backend */
1486 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
1488 bp = (Backend *) DLE_VAL(curr);
1489 if (bp->pid == backendPID)
1491 if (bp->cancel_key == cancelAuthCode)
1493 /* Found a match; signal that backend to cancel current op */
1495 (errmsg_internal("processing cancel request: sending SIGINT to process %d",
1497 kill(bp->pid, SIGINT);
1500 /* Right PID, wrong key: no way, Jose */
1502 (errmsg_internal("bad key in cancel request for process %d",
1508 /* No matching backend */
1510 (errmsg_internal("bad pid in cancel request for process %d",
1515 * canAcceptConnections --- check to see if database state allows connections.
1517 static enum CAC_state
1518 canAcceptConnections(void)
1520 /* Can't start backends when in startup/shutdown/recovery state. */
1521 if (Shutdown > NoShutdown)
1522 return CAC_SHUTDOWN;
1526 return CAC_RECOVERY;
1529 * Don't start too many children.
1531 * We allow more connections than we can have backends here because some
1532 * might still be authenticating; they might fail auth, or some
1533 * existing backend might exit before the auth cycle is completed. The
1534 * exact MaxBackends limit is enforced when a new backend tries to
1535 * join the shared-inval backend array.
1537 if (CountChildren() >= 2 * MaxBackends)
1545 * ConnCreate -- create a local connection data structure
1548 ConnCreate(int serverFd)
1552 if (!(port = (Port *) calloc(1, sizeof(Port))))
1555 (errcode(ERRCODE_OUT_OF_MEMORY),
1556 errmsg("out of memory")));
1560 if (StreamConnection(serverFd, port) != STATUS_OK)
1562 StreamClose(port->sock);
1569 * Precompute password salt values to use for this connection.
1570 * It's slightly annoying to do this long in advance of knowing
1571 * whether we'll need 'em or not, but we must do the random()
1572 * calls before we fork, not after. Else the postmaster's random
1573 * sequence won't get advanced, and all backends would end up
1574 * using the same salt...
1576 RandomSalt(port->cryptSalt, port->md5Salt);
1584 * ConnFree -- free a local connection data structure
1587 ConnFree(Port *conn)
1597 * ClosePostmasterPorts -- close all the postmaster's open sockets
1599 * This is called during child process startup to release file descriptors
1600 * that are not needed by that child process. The postmaster still has
1601 * them open, of course.
1604 ClosePostmasterPorts(bool pgstat_too)
1608 /* Close the listen sockets */
1609 for (i = 0; i < MAXLISTEN; i++)
1611 if (ListenSocket[i] != -1)
1613 StreamClose(ListenSocket[i]);
1614 ListenSocket[i] = -1;
1618 /* Close pgstat control sockets, unless we're starting pgstat itself */
1620 pgstat_close_sockets();
1625 * reset_shared -- reset shared memory and semaphores
1628 reset_shared(unsigned short port)
1631 * Create or re-create shared memory and semaphores.
1633 * Note: in each "cycle of life" we will normally assign the same IPC
1634 * keys (if using SysV shmem and/or semas), since the port number is
1635 * used to determine IPC keys. This helps ensure that we will clean
1636 * up dead IPC objects if the postmaster crashes and is restarted.
1638 CreateSharedMemoryAndSemaphores(false, MaxBackends, port);
1643 * SIGHUP -- reread config files, and tell children to do same
1646 SIGHUP_handler(SIGNAL_ARGS)
1648 int save_errno = errno;
1650 PG_SETMASK(&BlockSig);
1652 if (Shutdown <= SmartShutdown)
1655 (errmsg("received SIGHUP, reloading configuration files")));
1656 ProcessConfigFile(PGC_SIGHUP);
1658 write_nondefault_variables(PGC_SIGHUP);
1660 SignalChildren(SIGHUP);
1665 PG_SETMASK(&UnBlockSig);
1673 * pmdie -- signal handler for processing various postmaster signals.
1678 int save_errno = errno;
1680 PG_SETMASK(&BlockSig);
1683 (errmsg_internal("postmaster received signal %d",
1684 postgres_signal_arg)));
1686 switch (postgres_signal_arg)
1693 * Wait for children to end their work and ShutdownDataBase.
1695 if (Shutdown >= SmartShutdown)
1697 Shutdown = SmartShutdown;
1699 (errmsg("received smart shutdown request")));
1700 if (DLGetHead(BackendList)) /* let reaper() handle this */
1704 * No children left. Shutdown data base system.
1706 if (StartupPID > 0 || FatalError) /* let reaper() handle
1709 if (ShutdownPID > 0)
1711 elog(PANIC, "shutdown process %d already running",
1716 ShutdownPID = ShutdownDataBase();
1724 * abort all children with SIGTERM (rollback active transactions
1725 * and exit) and ShutdownDataBase when they are gone.
1727 if (Shutdown >= FastShutdown)
1730 (errmsg("received fast shutdown request")));
1731 if (DLGetHead(BackendList)) /* let reaper() handle this */
1733 Shutdown = FastShutdown;
1737 (errmsg("aborting any active transactions")));
1738 SignalChildren(SIGTERM);
1742 if (Shutdown > NoShutdown)
1744 Shutdown = FastShutdown;
1747 Shutdown = FastShutdown;
1750 * No children left. Shutdown data base system.
1752 if (StartupPID > 0 || FatalError) /* let reaper() handle
1755 if (ShutdownPID > 0)
1757 elog(PANIC, "shutdown process %d already running",
1762 ShutdownPID = ShutdownDataBase();
1768 * Immediate Shutdown:
1770 * abort all children with SIGQUIT and exit without attempt to
1771 * properly shutdown data base system.
1774 (errmsg("received immediate shutdown request")));
1775 if (ShutdownPID > 0)
1776 kill(ShutdownPID, SIGQUIT);
1778 kill(StartupPID, SIGQUIT);
1779 if (DLGetHead(BackendList))
1780 SignalChildren(SIGQUIT);
1785 PG_SETMASK(&UnBlockSig);
1791 * Reaper -- signal handler to cleanup after a backend (child) dies.
1796 int save_errno = errno;
1799 #warning fix waidpid for Win32
1802 int status; /* backend exit status */
1805 union wait status; /* backend exit status */
1808 int pid; /* process id of dead backend */
1810 PG_SETMASK(&BlockSig);
1813 (errmsg_internal("reaping dead processes")));
1815 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1817 exitstatus = status;
1819 while ((pid = wait3(&status, WNOHANG, NULL)) > 0)
1821 exitstatus = status.w_status;
1825 * Check if this child was the statistics collector. If so, try to
1826 * start a new one. (If fail, we'll try again in future cycles of
1829 if (pgstat_ispgstat(pid))
1831 LogChildExit(LOG, gettext("statistics collector process"),
1838 * Check if this child was a shutdown or startup process.
1840 if (ShutdownPID > 0 && pid == ShutdownPID)
1842 if (exitstatus != 0)
1844 LogChildExit(LOG, gettext("shutdown process"),
1848 /* Normal postmaster exit is here */
1852 if (StartupPID > 0 && pid == StartupPID)
1854 if (exitstatus != 0)
1856 LogChildExit(LOG, gettext("startup process"),
1859 (errmsg("aborting startup due to startup process failure")));
1865 * Startup succeeded - remember its ID and RedoRecPtr.
1867 * NB: this MUST happen before we fork a checkpoint or shutdown
1868 * subprocess, else they will have wrong local ThisStartUpId.
1872 FatalError = false; /* done with recovery */
1875 * Arrange for first checkpoint to occur after standard delay.
1878 checkpointed = time(NULL);
1881 * Go to shutdown mode if a shutdown request was pending.
1883 if (Shutdown > NoShutdown)
1885 if (ShutdownPID > 0)
1887 elog(PANIC, "startup process %d died while shutdown process %d already running",
1888 pid, (int) ShutdownPID);
1891 ShutdownPID = ShutdownDataBase();
1898 * Else do standard child cleanup.
1900 CleanupProc(pid, exitstatus);
1902 } /* loop over pending child-death reports */
1908 * Wait for all children exit, then reset shmem and
1911 if (DLGetHead(BackendList) || StartupPID > 0 || ShutdownPID > 0)
1914 (errmsg("all server processes terminated; reinitializing")));
1917 reset_shared(PostPortNumber);
1919 StartupPID = StartupDataBase();
1924 if (Shutdown > NoShutdown)
1926 if (DLGetHead(BackendList))
1928 if (StartupPID > 0 || ShutdownPID > 0)
1930 ShutdownPID = ShutdownDataBase();
1934 PG_SETMASK(&UnBlockSig);
1940 * CleanupProc -- cleanup after terminated backend.
1942 * Remove all local state associated with backend.
1945 CleanupProc(int pid,
1946 int exitstatus) /* child's exit status. */
1952 LogChildExit(DEBUG2, gettext("child process"), pid, exitstatus);
1955 * If a backend dies in an ugly way (i.e. exit status not 0) then we
1956 * must signal all other backends to quickdie. If exit status is zero
1957 * we assume everything is hunky dory and simply remove the backend
1958 * from the active backend list.
1960 if (exitstatus == 0)
1962 curr = DLGetHead(BackendList);
1965 bp = (Backend *) DLE_VAL(curr);
1973 curr = DLGetSucc(curr);
1976 if (pid == CheckPointPID)
1981 checkpointed = time(NULL);
1982 /* Update RedoRecPtr for future child backends */
1983 GetSavedRedoRecPtr();
1992 /* below here we're dealing with a non-normal exit */
1994 /* Make log entry unless we did so already */
1998 (pid == CheckPointPID) ? gettext("checkpoint process") :
1999 gettext("server process"),
2002 (errmsg("terminating any other active server processes")));
2005 curr = DLGetHead(BackendList);
2008 next = DLGetSucc(curr);
2009 bp = (Backend *) DLE_VAL(curr);
2013 * This backend is still alive. Unless we did so already,
2014 * tell it to commit hara-kiri.
2016 * SIGQUIT is the special signal that says exit without proc_exit
2017 * and let the user know what's going on. But if SendStop is
2018 * set (-s on command line), then we send SIGSTOP instead, so
2019 * that we can get core dumps from all backends by hand.
2024 (errmsg_internal("sending %s to process %d",
2025 (SendStop ? "SIGSTOP" : "SIGQUIT"),
2027 kill(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
2033 * Found entry for freshly-dead backend, so remove it.
2042 if (pid == CheckPointPID)
2050 * Tell the collector about backend termination
2059 * Log the death of a child process.
2062 LogChildExit(int lev, const char *procname, int pid, int exitstatus)
2064 if (WIFEXITED(exitstatus))
2068 * translator: %s is a noun phrase describing a child process,
2069 * such as "server process"
2071 (errmsg("%s (PID %d) exited with exit code %d",
2072 procname, pid, WEXITSTATUS(exitstatus))));
2073 else if (WIFSIGNALED(exitstatus))
2077 * translator: %s is a noun phrase describing a child process,
2078 * such as "server process"
2080 (errmsg("%s (PID %d) was terminated by signal %d",
2081 procname, pid, WTERMSIG(exitstatus))));
2086 * translator: %s is a noun phrase describing a child process,
2087 * such as "server process"
2089 (errmsg("%s (PID %d) exited with unexpected status %d",
2090 procname, pid, exitstatus)));
2094 * Send a signal to all backend children.
2097 SignalChildren(int signal)
2103 curr = DLGetHead(BackendList);
2106 next = DLGetSucc(curr);
2107 bp = (Backend *) DLE_VAL(curr);
2109 if (bp->pid != MyProcPid)
2112 (errmsg_internal("sending signal %d to process %d",
2115 kill(bp->pid, signal);
2123 * BackendStartup -- start backend process
2125 * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
2128 BackendStartup(Port *port)
2130 Backend *bn; /* for backend cleanup */
2133 #ifdef LINUX_PROFILE
2134 struct itimerval prof_itimer;
2138 * Compute the cancel key that will be assigned to this backend. The
2139 * backend will have its own copy in the forked-off process' value of
2140 * MyCancelKey, so that it can transmit the key to the frontend.
2142 MyCancelKey = PostmasterRandom();
2145 * Make room for backend data structure. Better before the fork() so
2146 * we can handle failure cleanly.
2148 bn = (Backend *) malloc(sizeof(Backend));
2152 (errcode(ERRCODE_OUT_OF_MEMORY),
2153 errmsg("out of memory")));
2154 return STATUS_ERROR;
2158 * Flush stdio channels just before fork, to avoid double-output
2159 * problems. Ideally we'd use fflush(NULL) here, but there are still a
2160 * few non-ANSI stdio libraries out there (like SunOS 4.1.x) that
2161 * coredump if we do. Presently stdout and stderr are the only stdio
2162 * output channels used by the postmaster, so fflush'ing them should
2168 #ifdef LINUX_PROFILE
2171 * Linux's fork() resets the profiling timer in the child process. If
2172 * we want to profile child processes then we need to save and restore
2173 * the timer setting. This is a waste of time if not profiling,
2174 * however, so only do it if commanded by specific -DLINUX_PROFILE
2177 getitimer(ITIMER_PROF, &prof_itimer);
2181 /* Specific beos actions before backend startup */
2182 beos_before_backend_startup();
2187 if (pid == 0) /* child */
2191 #ifdef LINUX_PROFILE
2192 setitimer(ITIMER_PROF, &prof_itimer, NULL);
2196 /* Specific beos backend startup actions */
2197 beos_backend_startup();
2201 status = BackendFork(port);
2205 (errmsg("connection startup failed")));
2209 /* in parent, error */
2212 int save_errno = errno;
2215 /* Specific beos backend startup actions */
2216 beos_backend_startup_failed();
2221 (errmsg("could not fork new process for connection: %m")));
2222 report_fork_failure_to_client(port, save_errno);
2223 return STATUS_ERROR;
2226 /* in parent, normal */
2228 (errmsg_internal("forked new backend, pid=%d socket=%d",
2229 (int) pid, port->sock)));
2232 * Everything's been successful, it's safe to add this backend to our
2236 bn->cancel_key = MyCancelKey;
2237 DLAddHead(BackendList, DLNewElem(bn));
2243 * Try to report backend fork() failure to client before we close the
2244 * connection. Since we do not care to risk blocking the postmaster on
2245 * this connection, we set the connection to non-blocking and try only once.
2247 * This is grungy special-purpose code; we cannot use backend libpq since
2248 * it's not up and running.
2251 report_fork_failure_to_client(Port *port, int errnum)
2255 /* Format the error message packet (always V2 protocol) */
2256 snprintf(buffer, sizeof(buffer), "E%s%s\n",
2257 gettext("could not fork new process for connection: "),
2260 /* Set port to non-blocking. Don't do send() if this fails */
2261 if (FCNTL_NONBLOCK(port->sock) < 0)
2264 send(port->sock, buffer, strlen(buffer) + 1, 0);
2269 * split_opts -- split a string of options and append it to an argv array
2271 * NB: the string is destructively modified!
2273 * Since no current POSTGRES arguments require any quoting characters,
2274 * we can use the simple-minded tactic of assuming each set of space-
2275 * delimited characters is a separate argv element.
2277 * If you don't like that, well, we *used* to pass the whole option string
2278 * as ONE argument to execl(), which was even less intelligent...
2281 split_opts(char **argv, int *argcp, char *s)
2285 while (isspace((unsigned char) *s))
2289 argv[(*argcp)++] = s;
2290 while (*s && !isspace((unsigned char) *s))
2298 * BackendFork -- perform authentication, and if successful, set up the
2299 * backend's argument list and invoke backend main().
2301 * This used to perform an execv() but we no longer exec the backend;
2302 * it's the same executable as the postmaster.
2305 * Shouldn't return at all.
2306 * If PostgresMain() fails, return status.
2309 BackendFork(Port *port)
2318 char pbuf[NAMEDATALEN + 256];
2324 char remote_host[NI_MAXHOST];
2325 char remote_port[NI_MAXSERV];
2328 * Let's clean up ourselves as the postmaster child
2331 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
2333 ClientAuthInProgress = true; /* limit visibility of log messages */
2335 /* We don't want the postmaster's proc_exit() handlers */
2339 * Signal handlers setting is moved to tcop/postgres...
2342 /* Close the postmaster's other sockets */
2343 ClosePostmasterPorts(true);
2345 /* Save port etc. for ps status */
2348 /* Reset MyProcPid to new backend's pid */
2349 MyProcPid = getpid();
2352 * Initialize libpq and enable reporting of ereport errors to the
2353 * client. Must do this now because authentication uses libpq to send
2356 pq_init(); /* initialize libpq to talk to client */
2357 whereToSendOutput = Remote; /* now safe to ereport to client */
2360 * We arrange for a simple exit(0) if we receive SIGTERM or SIGQUIT
2361 * during any client authentication related communication. Otherwise
2362 * the postmaster cannot shutdown the database FAST or IMMED cleanly
2363 * if a buggy client blocks a backend during authentication.
2365 pqsignal(SIGTERM, authdie);
2366 pqsignal(SIGQUIT, authdie);
2367 pqsignal(SIGALRM, authdie);
2368 PG_SETMASK(&AuthBlockSig);
2371 * Get the remote host name and port for logging and status display.
2373 remote_host[0] = '\0';
2374 remote_port[0] = '\0';
2375 if (getnameinfo_all(&port->raddr.addr, port->raddr.salen,
2376 remote_host, sizeof(remote_host),
2377 remote_port, sizeof(remote_port),
2378 (log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV))
2380 getnameinfo_all(&port->raddr.addr, port->raddr.salen,
2381 remote_host, sizeof(remote_host),
2382 remote_port, sizeof(remote_port),
2383 NI_NUMERICHOST | NI_NUMERICSERV);
2386 if (Log_connections)
2388 (errmsg("connection received: host=%s port=%s",
2389 remote_host, remote_port)));
2393 /* modify remote_host for use in ps status */
2394 char tmphost[NI_MAXHOST];
2396 snprintf(tmphost, sizeof(tmphost), "%s:%s", remote_host, remote_port);
2397 StrNCpy(remote_host, tmphost, sizeof(remote_host));
2401 * PreAuthDelay is a debugging aid for investigating problems in the
2402 * authentication cycle: it can be set in postgresql.conf to allow
2403 * time to attach to the newly-forked backend with a debugger. (See
2404 * also the -W backend switch, which we allow clients to pass through
2405 * PGOPTIONS, but it is not honored until after authentication.)
2407 if (PreAuthDelay > 0)
2408 sleep(PreAuthDelay);
2411 * Ready to begin client interaction. We will give up and exit(0)
2412 * after a time delay, so that a broken client can't hog a connection
2413 * indefinitely. PreAuthDelay doesn't count against the time limit.
2415 if (!enable_sig_alarm(AuthenticationTimeout * 1000, false))
2416 elog(FATAL, "could not set timer for authorization timeout");
2419 * Receive the startup packet (which might turn out to be a cancel
2422 status = ProcessStartupPacket(port, false);
2424 if (status != STATUS_OK)
2425 return 0; /* cancel request processed, or error */
2428 * Now that we have the user and database name, we can set the process
2429 * title for ps. It's good to do this as early as possible in
2432 init_ps_display(port->user_name, port->database_name, remote_host);
2433 set_ps_display("authentication");
2436 * Now perform authentication exchange.
2438 ClientAuthentication(port); /* might not return, if failure */
2441 * Done with authentication. Disable timeout, and prevent
2442 * SIGTERM/SIGQUIT again until backend startup is complete.
2444 if (!disable_sig_alarm(false))
2445 elog(FATAL, "could not disable timer for authorization timeout");
2446 PG_SETMASK(&BlockSig);
2448 if (Log_connections)
2450 (errmsg("connection authorized: user=%s database=%s",
2451 port->user_name, port->database_name)));
2454 * Don't want backend to be able to see the postmaster random number
2455 * generator state. We have to clobber the static random_seed *and*
2456 * start a new random sequence in the random() library function.
2459 gettimeofday(&now, &tz);
2460 srandom((unsigned int) now.tv_usec);
2463 * Now, build the argv vector that will be given to PostgresMain.
2465 * The layout of the command line is
2466 * postgres [secure switches] -p databasename [insecure switches]
2467 * where the switches after -p come from the client request.
2469 * The maximum possible number of commandline arguments that could come
2470 * from ExtraOptions or port->cmdline_options is (strlen + 1) / 2; see
2474 maxac = 10; /* for fixed args supplied below */
2475 maxac += (strlen(ExtraOptions) + 1) / 2;
2476 if (port->cmdline_options)
2477 maxac += (strlen(port->cmdline_options) + 1) / 2;
2479 av = (char **) MemoryContextAlloc(TopMemoryContext,
2480 maxac * sizeof(char *));
2483 av[ac++] = "postgres";
2486 * Pass the requested debugging level along to the backend.
2490 snprintf(debugbuf, sizeof(debugbuf), "-d%d", debug_flag);
2491 av[ac++] = debugbuf;
2495 * Pass any backend switches specified with -o in the postmaster's own
2496 * command line. We assume these are secure. (It's OK to mangle
2497 * ExtraOptions since we are now in the child process; this won't
2498 * change the postmaster's copy.)
2500 split_opts(av, &ac, ExtraOptions);
2502 /* Tell the backend what protocol the frontend is using. */
2503 snprintf(protobuf, sizeof(protobuf), "-v%u", port->proto);
2504 av[ac++] = protobuf;
2507 * Tell the backend it is being called from the postmaster, and which
2508 * database to use. -p marks the end of secure switches.
2512 Assert(UsedShmemSegID != 0 && UsedShmemSegAddr != NULL);
2513 /* database name at the end because it might contain commas */
2514 snprintf(pbuf, NAMEDATALEN + 256, "%d,%d,%d,%p,%s", port->sock, canAcceptConnections(),
2515 UsedShmemSegID, UsedShmemSegAddr, port->database_name);
2518 av[ac++] = port->database_name;
2522 * Pass the (insecure) option switches from the connection request.
2523 * (It's OK to mangle port->cmdline_options now.)
2525 if (port->cmdline_options)
2526 split_opts(av, &ac, port->cmdline_options);
2528 av[ac] = (char *) NULL;
2533 * Release postmaster's working memory context so that backend can
2534 * recycle the space. Note this does not trash *MyProcPort, because
2535 * ConnCreate() allocated that space with malloc() ... else we'd need
2536 * to copy the Port data here. Also, subsidiary data such as the
2537 * username isn't lost either; see ProcessStartupPacket().
2539 MemoryContextSwitchTo(TopMemoryContext);
2540 MemoryContextDelete(PostmasterContext);
2541 PostmasterContext = NULL;
2544 * Debug: print arguments being passed to backend
2547 (errmsg_internal("%s child[%d]: starting with (",
2548 progname, MyProcPid)));
2549 for (i = 0; i < ac; ++i)
2551 (errmsg_internal("\t%s", av[i])));
2553 (errmsg_internal(")")));
2555 ClientAuthInProgress = false; /* client_min_messages is active
2558 return (PostgresMain(ac, av, port->user_name));
2562 * ExitPostmaster -- cleanup
2564 * Do NOT call exit() directly --- always go through here!
2567 ExitPostmaster(int status)
2569 /* should cleanup shared memory and kill all backends */
2572 * Not sure of the semantics here. When the Postmaster dies, should
2573 * the backends all be killed? probably not.
2575 * MUST -- vadim 05-10-1999
2577 /* Should I use true instead? */
2578 ClosePostmasterPorts(false);
2584 * sigusr1_handler - handle signal conditions from child processes
2587 sigusr1_handler(SIGNAL_ARGS)
2589 int save_errno = errno;
2591 PG_SETMASK(&BlockSig);
2593 if (CheckPostmasterSignal(PMSIGNAL_DO_CHECKPOINT))
2595 if (CheckPointWarning != 0)
2598 * This only times checkpoints forced by running out of
2599 * segment files. Other checkpoints could reduce the
2600 * frequency of forced checkpoints.
2602 time_t now = time(NULL);
2604 if (LastSignalledCheckpoint != 0)
2606 int elapsed_secs = now - LastSignalledCheckpoint;
2608 if (elapsed_secs < CheckPointWarning)
2610 (errmsg("checkpoints are occurring too frequently (%d seconds apart)",
2612 errhint("Consider increasing the configuration parameter \"checkpoint_segments\".")));
2614 LastSignalledCheckpoint = now;
2618 * Request to schedule a checkpoint
2620 * Ignore request if checkpoint is already running or checkpointing
2621 * is currently disabled
2623 if (CheckPointPID == 0 && checkpointed &&
2624 Shutdown == NoShutdown && !FatalError && random_seed != 0)
2626 CheckPointPID = CheckPointDataBase();
2627 /* note: if fork fails, CheckPointPID stays 0; nothing happens */
2631 if (CheckPostmasterSignal(PMSIGNAL_PASSWORD_CHANGE))
2634 * Password or group file has changed.
2640 if (CheckPostmasterSignal(PMSIGNAL_WAKEN_CHILDREN))
2643 * Send SIGUSR2 to all children (triggers AsyncNotifyHandler). See
2644 * storage/ipc/sinvaladt.c for the use of this.
2646 if (Shutdown == NoShutdown)
2647 SignalChildren(SIGUSR2);
2650 PG_SETMASK(&UnBlockSig);
2657 * Dummy signal handler
2659 * We use this for signals that we don't actually use in the postmaster,
2660 * but we do use in backends. If we SIG_IGN such signals in the postmaster,
2661 * then a newly started backend might drop a signal that arrives before it's
2662 * able to reconfigure its signal processing. (See notes in postgres.c.)
2665 dummy_handler(SIGNAL_ARGS)
2671 * CharRemap: given an int in range 0..61, produce textual encoding of it
2672 * per crypt(3) conventions.
2696 RandomSalt(char *cryptSalt, char *md5Salt)
2698 long rand = PostmasterRandom();
2700 cryptSalt[0] = CharRemap(rand % 62);
2701 cryptSalt[1] = CharRemap(rand / 62);
2704 * It's okay to reuse the first random value for one of the MD5 salt
2705 * bytes, since only one of the two salts will be sent to the client.
2706 * After that we need to compute more random bits.
2708 * We use % 255, sacrificing one possible byte value, so as to ensure
2709 * that all bits of the random() value participate in the result.
2710 * While at it, add one to avoid generating any null bytes.
2712 md5Salt[0] = (rand % 255) + 1;
2713 rand = PostmasterRandom();
2714 md5Salt[1] = (rand % 255) + 1;
2715 rand = PostmasterRandom();
2716 md5Salt[2] = (rand % 255) + 1;
2717 rand = PostmasterRandom();
2718 md5Salt[3] = (rand % 255) + 1;
2725 PostmasterRandom(void)
2727 static bool initialized = false;
2731 Assert(random_seed != 0);
2732 srandom(random_seed);
2740 * Count up number of child processes.
2749 for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
2751 bp = (Backend *) DLE_VAL(curr);
2752 if (bp->pid != MyProcPid)
2755 if (CheckPointPID != 0)
2761 * Fire off a subprocess for startup/shutdown/checkpoint.
2763 * Return value is subprocess' PID, or 0 if failed to start subprocess
2764 * (0 is returned only for checkpoint case).
2767 SSDataBase(int xlop)
2772 #ifdef LINUX_PROFILE
2773 struct itimerval prof_itimer;
2779 #ifdef LINUX_PROFILE
2780 /* see comments in BackendStartup */
2781 getitimer(ITIMER_PROF, &prof_itimer);
2785 /* Specific beos actions before backend startup */
2786 beos_before_backend_startup();
2789 if ((pid = fork()) == 0) /* child */
2791 const char *statmsg;
2798 char pbuf[NAMEDATALEN + 256];
2801 #ifdef LINUX_PROFILE
2802 setitimer(ITIMER_PROF, &prof_itimer, NULL);
2806 /* Specific beos actions after backend startup */
2807 beos_backend_startup();
2810 IsUnderPostmaster = true; /* we are a postmaster subprocess
2813 /* Lose the postmaster's on-exit routines and port connections */
2816 /* Close the postmaster's sockets */
2817 ClosePostmasterPorts(true);
2820 * Identify myself via ps
2824 case BS_XLOG_STARTUP:
2825 statmsg = "startup subprocess";
2827 case BS_XLOG_CHECKPOINT:
2828 statmsg = "checkpoint subprocess";
2830 case BS_XLOG_SHUTDOWN:
2831 statmsg = "shutdown subprocess";
2834 statmsg = "??? subprocess";
2837 init_ps_display(statmsg, "", "");
2840 /* Set up command-line arguments for subprocess */
2841 av[ac++] = "postgres";
2843 snprintf(nbbuf, sizeof(nbbuf), "-B%d", NBuffers);
2846 snprintf(xlbuf, sizeof(xlbuf), "-x%d", xlop);
2851 Assert(UsedShmemSegID != 0 && UsedShmemSegAddr != NULL);
2852 /* database name at the end because it might contain commas */
2853 snprintf(pbuf, NAMEDATALEN + 256, "%d,%p,%s", UsedShmemSegID,
2854 UsedShmemSegAddr, "template1");
2857 av[ac++] = "template1";
2860 av[ac] = (char *) NULL;
2862 Assert(ac < lengthof(av));
2864 BootstrapMain(ac, av);
2872 /* Specific beos actions before backend startup */
2873 beos_backend_startup_failed();
2878 case BS_XLOG_STARTUP:
2880 (errmsg("could not fork startup process: %m")));
2882 case BS_XLOG_CHECKPOINT:
2884 (errmsg("could not fork checkpoint process: %m")));
2886 case BS_XLOG_SHUTDOWN:
2888 (errmsg("could not fork shutdown process: %m")));
2892 (errmsg("could not fork process: %m")));
2897 * fork failure is fatal during startup/shutdown, but there's no
2898 * need to choke if a routine checkpoint fails.
2900 if (xlop == BS_XLOG_CHECKPOINT)
2906 * The startup and shutdown processes are not considered normal
2907 * backends, but the checkpoint process is. Checkpoint must be added
2908 * to the list of backends.
2910 if (xlop == BS_XLOG_CHECKPOINT)
2912 if (!(bn = (Backend *) malloc(sizeof(Backend))))
2915 (errcode(ERRCODE_OUT_OF_MEMORY),
2916 errmsg("out of memory")));
2921 bn->cancel_key = PostmasterRandom();
2922 DLAddHead(BackendList, DLNewElem(bn));
2925 * Since this code is executed periodically, it's a fine place to
2926 * do other actions that should happen every now and then on no
2927 * particular schedule. Such as...
2930 TouchSocketLockFile();
2938 * Create the opts file
2941 CreateOptsFile(int argc, char *argv[])
2943 char fullprogname[MAXPGPATH];
2944 char filename[MAXPGPATH];
2948 if (FindExec(fullprogname, argv[0], "postmaster") < 0)
2951 snprintf(filename, sizeof(filename), "%s/postmaster.opts", DataDir);
2953 if ((fp = fopen(filename, "w")) == NULL)
2955 elog(LOG, "could not create file \"%s\": %m", filename);
2959 fprintf(fp, "%s", fullprogname);
2960 for (i = 1; i < argc; i++)
2961 fprintf(fp, " '%s'", argv[i]);
2967 elog(LOG, "could not write file \"%s\": %m", filename);
2977 * This should be used only for reporting "interactive" errors (essentially,
2978 * bogus arguments on the command line). Once the postmaster is launched,
2979 * use ereport. In particular, don't use this for anything that occurs
2980 * after pmdaemonize.
2983 postmaster_error(const char *fmt,...)
2987 fprintf(stderr, "%s: ", progname);
2989 vfprintf(stderr, gettext(fmt), ap);
2991 fprintf(stderr, "\n");