Header And Logo

PostgreSQL
| The world's most advanced open source database.

postmaster.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * postmaster.c
00004  *    This program acts as a clearing house for requests to the
00005  *    POSTGRES system.  Frontend programs send a startup message
00006  *    to the Postmaster and the postmaster uses the info in the
00007  *    message to setup a backend process.
00008  *
00009  *    The postmaster also manages system-wide operations such as
00010  *    startup and shutdown. The postmaster itself doesn't do those
00011  *    operations, mind you --- it just forks off a subprocess to do them
00012  *    at the right times.  It also takes care of resetting the system
00013  *    if a backend crashes.
00014  *
00015  *    The postmaster process creates the shared memory and semaphore
00016  *    pools during startup, but as a rule does not touch them itself.
00017  *    In particular, it is not a member of the PGPROC array of backends
00018  *    and so it cannot participate in lock-manager operations.  Keeping
00019  *    the postmaster away from shared memory operations makes it simpler
00020  *    and more reliable.  The postmaster is almost always able to recover
00021  *    from crashes of individual backends by resetting shared memory;
00022  *    if it did much with shared memory then it would be prone to crashing
00023  *    along with the backends.
00024  *
00025  *    When a request message is received, we now fork() immediately.
00026  *    The child process performs authentication of the request, and
00027  *    then becomes a backend if successful.  This allows the auth code
00028  *    to be written in a simple single-threaded style (as opposed to the
00029  *    crufty "poor man's multitasking" code that used to be needed).
00030  *    More importantly, it ensures that blockages in non-multithreaded
00031  *    libraries like SSL or PAM cannot cause denial of service to other
00032  *    clients.
00033  *
00034  *
00035  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00036  * Portions Copyright (c) 1994, Regents of the University of California
00037  *
00038  *
00039  * IDENTIFICATION
00040  *    src/backend/postmaster/postmaster.c
00041  *
00042  * NOTES
00043  *
00044  * Initialization:
00045  *      The Postmaster sets up shared memory data structures
00046  *      for the backends.
00047  *
00048  * Synchronization:
00049  *      The Postmaster shares memory with the backends but should avoid
00050  *      touching shared memory, so as not to become stuck if a crashing
00051  *      backend screws up locks or shared memory.  Likewise, the Postmaster
00052  *      should never block on messages from frontend clients.
00053  *
00054  * Garbage Collection:
00055  *      The Postmaster cleans up after backends if they have an emergency
00056  *      exit and/or core dump.
00057  *
00058  * Error Reporting:
00059  *      Use write_stderr() only for reporting "interactive" errors
00060  *      (essentially, bogus arguments on the command line).  Once the
00061  *      postmaster is launched, use ereport().
00062  *
00063  *-------------------------------------------------------------------------
00064  */
00065 
00066 #include "postgres.h"
00067 
00068 #include <unistd.h>
00069 #include <signal.h>
00070 #include <time.h>
00071 #include <sys/wait.h>
00072 #include <ctype.h>
00073 #include <sys/stat.h>
00074 #include <sys/socket.h>
00075 #include <fcntl.h>
00076 #include <sys/param.h>
00077 #include <netinet/in.h>
00078 #include <arpa/inet.h>
00079 #include <netdb.h>
00080 #include <limits.h>
00081 
00082 #ifdef HAVE_SYS_SELECT_H
00083 #include <sys/select.h>
00084 #endif
00085 
00086 #ifdef HAVE_GETOPT_H
00087 #include <getopt.h>
00088 #endif
00089 
00090 #ifdef USE_BONJOUR
00091 #include <dns_sd.h>
00092 #endif
00093 
00094 #include "access/transam.h"
00095 #include "access/xlog.h"
00096 #include "bootstrap/bootstrap.h"
00097 #include "catalog/pg_control.h"
00098 #include "lib/ilist.h"
00099 #include "libpq/auth.h"
00100 #include "libpq/ip.h"
00101 #include "libpq/libpq.h"
00102 #include "libpq/pqsignal.h"
00103 #include "miscadmin.h"
00104 #include "pgstat.h"
00105 #include "postmaster/autovacuum.h"
00106 #include "postmaster/bgworker.h"
00107 #include "postmaster/fork_process.h"
00108 #include "postmaster/pgarch.h"
00109 #include "postmaster/postmaster.h"
00110 #include "postmaster/syslogger.h"
00111 #include "replication/walsender.h"
00112 #include "storage/fd.h"
00113 #include "storage/ipc.h"
00114 #include "storage/pg_shmem.h"
00115 #include "storage/pmsignal.h"
00116 #include "storage/proc.h"
00117 #include "tcop/tcopprot.h"
00118 #include "utils/builtins.h"
00119 #include "utils/datetime.h"
00120 #include "utils/memutils.h"
00121 #include "utils/ps_status.h"
00122 #include "utils/timeout.h"
00123 
00124 #ifdef EXEC_BACKEND
00125 #include "storage/spin.h"
00126 #endif
00127 
00128 
00129 /*
00130  * Possible types of a backend. Beyond being the possible bkend_type values in
00131  * struct bkend, these are OR-able request flag bits for SignalSomeChildren()
00132  * and CountChildren().
00133  */
00134 #define BACKEND_TYPE_NORMAL     0x0001  /* normal backend */
00135 #define BACKEND_TYPE_AUTOVAC    0x0002  /* autovacuum worker process */
00136 #define BACKEND_TYPE_WALSND     0x0004  /* walsender process */
00137 #define BACKEND_TYPE_BGWORKER   0x0008  /* bgworker process */
00138 #define BACKEND_TYPE_ALL        0x000F  /* OR of all the above */
00139 
00140 #define BACKEND_TYPE_WORKER     (BACKEND_TYPE_AUTOVAC | BACKEND_TYPE_BGWORKER)
00141 
00142 /*
00143  * List of active backends (or child processes anyway; we don't actually
00144  * know whether a given child has become a backend or is still in the
00145  * authorization phase).  This is used mainly to keep track of how many
00146  * children we have and send them appropriate signals when necessary.
00147  *
00148  * "Special" children such as the startup, bgwriter and autovacuum launcher
00149  * tasks are not in this list.  Autovacuum worker and walsender are in it.
00150  * Also, "dead_end" children are in it: these are children launched just for
00151  * the purpose of sending a friendly rejection message to a would-be client.
00152  * We must track them because they are attached to shared memory, but we know
00153  * they will never become live backends.  dead_end children are not assigned a
00154  * PMChildSlot.
00155  *
00156  * Background workers that request shared memory access during registration are
00157  * in this list, too.
00158  */
00159 typedef struct bkend
00160 {
00161     pid_t       pid;            /* process id of backend */
00162     long        cancel_key;     /* cancel key for cancels for this backend */
00163     int         child_slot;     /* PMChildSlot for this backend, if any */
00164 
00165     /*
00166      * Flavor of backend or auxiliary process.  Note that BACKEND_TYPE_WALSND
00167      * backends initially announce themselves as BACKEND_TYPE_NORMAL, so if
00168      * bkend_type is normal, you should check for a recent transition.
00169      */
00170     int         bkend_type;
00171     bool        dead_end;       /* is it going to send an error and quit? */
00172     dlist_node  elem;           /* list link in BackendList */
00173 } Backend;
00174 
00175 static dlist_head BackendList = DLIST_STATIC_INIT(BackendList);
00176 
00177 #ifdef EXEC_BACKEND
00178 static Backend *ShmemBackendArray;
00179 #endif
00180 
00181 
00182 /*
00183  * List of background workers.
00184  *
00185  * A worker that requests a database connection during registration will have
00186  * rw_backend set, and will be present in BackendList.  Note: do not rely on
00187  * rw_backend being non-NULL for shmem-connected workers!
00188  */
00189 typedef struct RegisteredBgWorker
00190 {
00191     BackgroundWorker rw_worker; /* its registry entry */
00192     Backend    *rw_backend;     /* its BackendList entry, or NULL */
00193     pid_t       rw_pid;         /* 0 if not running */
00194     int         rw_child_slot;
00195     TimestampTz rw_crashed_at;  /* if not 0, time it last crashed */
00196 #ifdef EXEC_BACKEND
00197     int         rw_cookie;
00198 #endif
00199     slist_node  rw_lnode;       /* list link */
00200 }   RegisteredBgWorker;
00201 
00202 static slist_head BackgroundWorkerList = SLIST_STATIC_INIT(BackgroundWorkerList);
00203 
00204 BackgroundWorker *MyBgworkerEntry = NULL;
00205 
00206 
00207 
00208 /* The socket number we are listening for connections on */
00209 int         PostPortNumber;
00210 /* The directory names for Unix socket(s) */
00211 char       *Unix_socket_directories;
00212 /* The TCP listen address(es) */
00213 char       *ListenAddresses;
00214 
00215 /*
00216  * ReservedBackends is the number of backends reserved for superuser use.
00217  * This number is taken out of the pool size given by MaxBackends so
00218  * number of backend slots available to non-superusers is
00219  * (MaxBackends - ReservedBackends).  Note what this really means is
00220  * "if there are <= ReservedBackends connections available, only superusers
00221  * can make new connections" --- pre-existing superuser connections don't
00222  * count against the limit.
00223  */
00224 int         ReservedBackends;
00225 
00226 /* The socket(s) we're listening to. */
00227 #define MAXLISTEN   64
00228 static pgsocket ListenSocket[MAXLISTEN];
00229 
00230 /*
00231  * Set by the -o option
00232  */
00233 static char ExtraOptions[MAXPGPATH];
00234 
00235 /*
00236  * These globals control the behavior of the postmaster in case some
00237  * backend dumps core.  Normally, it kills all peers of the dead backend
00238  * and reinitializes shared memory.  By specifying -s or -n, we can have
00239  * the postmaster stop (rather than kill) peers and not reinitialize
00240  * shared data structures.  (Reinit is currently dead code, though.)
00241  */
00242 static bool Reinit = true;
00243 static int  SendStop = false;
00244 
00245 /* still more option variables */
00246 bool        EnableSSL = false;
00247 
00248 int         PreAuthDelay = 0;
00249 int         AuthenticationTimeout = 60;
00250 
00251 bool        log_hostname;       /* for ps display and logging */
00252 bool        Log_connections = false;
00253 bool        Db_user_namespace = false;
00254 
00255 bool        enable_bonjour = false;
00256 char       *bonjour_name;
00257 bool        restart_after_crash = true;
00258 
00259 char       *output_config_variable = NULL;
00260 
00261 /* PIDs of special child processes; 0 when not running */
00262 static pid_t StartupPID = 0,
00263             BgWriterPID = 0,
00264             CheckpointerPID = 0,
00265             WalWriterPID = 0,
00266             WalReceiverPID = 0,
00267             AutoVacPID = 0,
00268             PgArchPID = 0,
00269             PgStatPID = 0,
00270             SysLoggerPID = 0;
00271 
00272 /* Startup/shutdown state */
00273 #define         NoShutdown      0
00274 #define         SmartShutdown   1
00275 #define         FastShutdown    2
00276 
00277 static int  Shutdown = NoShutdown;
00278 
00279 static bool FatalError = false; /* T if recovering from backend crash */
00280 static bool RecoveryError = false;      /* T if WAL recovery failed */
00281 
00282 /*
00283  * We use a simple state machine to control startup, shutdown, and
00284  * crash recovery (which is rather like shutdown followed by startup).
00285  *
00286  * After doing all the postmaster initialization work, we enter PM_STARTUP
00287  * state and the startup process is launched. The startup process begins by
00288  * reading the control file and other preliminary initialization steps.
00289  * In a normal startup, or after crash recovery, the startup process exits
00290  * with exit code 0 and we switch to PM_RUN state.  However, archive recovery
00291  * is handled specially since it takes much longer and we would like to support
00292  * hot standby during archive recovery.
00293  *
00294  * When the startup process is ready to start archive recovery, it signals the
00295  * postmaster, and we switch to PM_RECOVERY state. The background writer and
00296  * checkpointer are launched, while the startup process continues applying WAL.
00297  * If Hot Standby is enabled, then, after reaching a consistent point in WAL
00298  * redo, startup process signals us again, and we switch to PM_HOT_STANDBY
00299  * state and begin accepting connections to perform read-only queries.  When
00300  * archive recovery is finished, the startup process exits with exit code 0
00301  * and we switch to PM_RUN state.
00302  *
00303  * Normal child backends can only be launched when we are in PM_RUN or
00304  * PM_HOT_STANDBY state.  (We also allow launch of normal
00305  * child backends in PM_WAIT_BACKUP state, but only for superusers.)
00306  * In other states we handle connection requests by launching "dead_end"
00307  * child processes, which will simply send the client an error message and
00308  * quit.  (We track these in the BackendList so that we can know when they
00309  * are all gone; this is important because they're still connected to shared
00310  * memory, and would interfere with an attempt to destroy the shmem segment,
00311  * possibly leading to SHMALL failure when we try to make a new one.)
00312  * In PM_WAIT_DEAD_END state we are waiting for all the dead_end children
00313  * to drain out of the system, and therefore stop accepting connection
00314  * requests at all until the last existing child has quit (which hopefully
00315  * will not be very long).
00316  *
00317  * Notice that this state variable does not distinguish *why* we entered
00318  * states later than PM_RUN --- Shutdown and FatalError must be consulted
00319  * to find that out.  FatalError is never true in PM_RECOVERY_* or PM_RUN
00320  * states, nor in PM_SHUTDOWN states (because we don't enter those states
00321  * when trying to recover from a crash).  It can be true in PM_STARTUP state,
00322  * because we don't clear it until we've successfully started WAL redo.
00323  * Similarly, RecoveryError means that we have crashed during recovery, and
00324  * should not try to restart.
00325  */
00326 typedef enum
00327 {
00328     PM_INIT,                    /* postmaster starting */
00329     PM_STARTUP,                 /* waiting for startup subprocess */
00330     PM_RECOVERY,                /* in archive recovery mode */
00331     PM_HOT_STANDBY,             /* in hot standby mode */
00332     PM_RUN,                     /* normal "database is alive" state */
00333     PM_WAIT_BACKUP,             /* waiting for online backup mode to end */
00334     PM_WAIT_READONLY,           /* waiting for read only backends to exit */
00335     PM_WAIT_BACKENDS,           /* waiting for live backends to exit */
00336     PM_SHUTDOWN,                /* waiting for checkpointer to do shutdown
00337                                  * ckpt */
00338     PM_SHUTDOWN_2,              /* waiting for archiver and walsenders to
00339                                  * finish */
00340     PM_WAIT_DEAD_END,           /* waiting for dead_end children to exit */
00341     PM_NO_CHILDREN              /* all important children have exited */
00342 } PMState;
00343 
00344 static PMState pmState = PM_INIT;
00345 
00346 static bool ReachedNormalRunning = false;       /* T if we've reached PM_RUN */
00347 
00348 bool        ClientAuthInProgress = false;       /* T during new-client
00349                                                  * authentication */
00350 
00351 bool        redirection_done = false;   /* stderr redirected for syslogger? */
00352 
00353 /* received START_AUTOVAC_LAUNCHER signal */
00354 static volatile sig_atomic_t start_autovac_launcher = false;
00355 
00356 /* the launcher needs to be signalled to communicate some condition */
00357 static volatile bool avlauncher_needs_signal = false;
00358 
00359 /* set when there's a worker that needs to be started up */
00360 static volatile bool StartWorkerNeeded = true;
00361 static volatile bool HaveCrashedWorker = false;
00362 
00363 /*
00364  * State for assigning random salts and cancel keys.
00365  * Also, the global MyCancelKey passes the cancel key assigned to a given
00366  * backend from the postmaster to that backend (via fork).
00367  */
00368 static unsigned int random_seed = 0;
00369 static struct timeval random_start_time;
00370 
00371 extern char *optarg;
00372 extern int  optind,
00373             opterr;
00374 
00375 #ifdef HAVE_INT_OPTRESET
00376 extern int  optreset;           /* might not be declared by system headers */
00377 #endif
00378 
00379 #ifdef USE_BONJOUR
00380 static DNSServiceRef bonjour_sdref = NULL;
00381 #endif
00382 
00383 /*
00384  * postmaster.c - function prototypes
00385  */
00386 static void unlink_external_pid_file(int status, Datum arg);
00387 static void getInstallationPaths(const char *argv0);
00388 static void checkDataDir(void);
00389 static Port *ConnCreate(int serverFd);
00390 static void ConnFree(Port *port);
00391 static void reset_shared(int port);
00392 static void SIGHUP_handler(SIGNAL_ARGS);
00393 static void pmdie(SIGNAL_ARGS);
00394 static void reaper(SIGNAL_ARGS);
00395 static void sigusr1_handler(SIGNAL_ARGS);
00396 static void startup_die(SIGNAL_ARGS);
00397 static void dummy_handler(SIGNAL_ARGS);
00398 static int  GetNumRegisteredBackgroundWorkers(int flags);
00399 static void StartupPacketTimeoutHandler(void);
00400 static void CleanupBackend(int pid, int exitstatus);
00401 static bool CleanupBackgroundWorker(int pid, int exitstatus);
00402 static void do_start_bgworker(void);
00403 static void HandleChildCrash(int pid, int exitstatus, const char *procname);
00404 static void LogChildExit(int lev, const char *procname,
00405              int pid, int exitstatus);
00406 static void PostmasterStateMachine(void);
00407 static void BackendInitialize(Port *port);
00408 static void BackendRun(Port *port) __attribute__((noreturn));
00409 static void ExitPostmaster(int status) __attribute__((noreturn));
00410 static int  ServerLoop(void);
00411 static int  BackendStartup(Port *port);
00412 static int  ProcessStartupPacket(Port *port, bool SSLdone);
00413 static void processCancelRequest(Port *port, void *pkt);
00414 static int  initMasks(fd_set *rmask);
00415 static void report_fork_failure_to_client(Port *port, int errnum);
00416 static CAC_state canAcceptConnections(void);
00417 static long PostmasterRandom(void);
00418 static void RandomSalt(char *md5Salt);
00419 static void signal_child(pid_t pid, int signal);
00420 static bool SignalSomeChildren(int signal, int targets);
00421 static bool SignalUnconnectedWorkers(int signal);
00422 
00423 #define SignalChildren(sig)            SignalSomeChildren(sig, BACKEND_TYPE_ALL)
00424 
00425 static int  CountChildren(int target);
00426 static int  CountUnconnectedWorkers(void);
00427 static void StartOneBackgroundWorker(void);
00428 static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
00429 static pid_t StartChildProcess(AuxProcType type);
00430 static void StartAutovacuumWorker(void);
00431 static void InitPostmasterDeathWatchHandle(void);
00432 
00433 #ifdef EXEC_BACKEND
00434 
00435 #ifdef WIN32
00436 #define WNOHANG 0               /* ignored, so any integer value will do */
00437 
00438 static pid_t waitpid(pid_t pid, int *exitstatus, int options);
00439 static void WINAPI pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
00440 
00441 static HANDLE win32ChildQueue;
00442 
00443 typedef struct
00444 {
00445     HANDLE      waitHandle;
00446     HANDLE      procHandle;
00447     DWORD       procId;
00448 } win32_deadchild_waitinfo;
00449 #endif /* WIN32 */
00450 
00451 static pid_t backend_forkexec(Port *port);
00452 static pid_t internal_forkexec(int argc, char *argv[], Port *port);
00453 
00454 /* Type for a socket that can be inherited to a client process */
00455 #ifdef WIN32
00456 typedef struct
00457 {
00458     SOCKET      origsocket;     /* Original socket value, or PGINVALID_SOCKET
00459                                  * if not a socket */
00460     WSAPROTOCOL_INFO wsainfo;
00461 } InheritableSocket;
00462 #else
00463 typedef int InheritableSocket;
00464 #endif
00465 
00466 typedef struct LWLock LWLock;   /* ugly kluge */
00467 
00468 /*
00469  * Structure contains all variables passed to exec:ed backends
00470  */
00471 typedef struct
00472 {
00473     Port        port;
00474     InheritableSocket portsocket;
00475     char        DataDir[MAXPGPATH];
00476     pgsocket    ListenSocket[MAXLISTEN];
00477     long        MyCancelKey;
00478     int         MyPMChildSlot;
00479 #ifndef WIN32
00480     unsigned long UsedShmemSegID;
00481 #else
00482     HANDLE      UsedShmemSegID;
00483 #endif
00484     void       *UsedShmemSegAddr;
00485     slock_t    *ShmemLock;
00486     VariableCache ShmemVariableCache;
00487     Backend    *ShmemBackendArray;
00488     LWLock     *LWLockArray;
00489     slock_t    *ProcStructLock;
00490     PROC_HDR   *ProcGlobal;
00491     PGPROC     *AuxiliaryProcs;
00492     PGPROC     *PreparedXactProcs;
00493     PMSignalData *PMSignalState;
00494     InheritableSocket pgStatSock;
00495     pid_t       PostmasterPid;
00496     TimestampTz PgStartTime;
00497     TimestampTz PgReloadTime;
00498     pg_time_t   first_syslogger_file_time;
00499     bool        redirection_done;
00500     bool        IsBinaryUpgrade;
00501     int         max_safe_fds;
00502     int         MaxBackends;
00503 #ifdef WIN32
00504     HANDLE      PostmasterHandle;
00505     HANDLE      initial_signal_pipe;
00506     HANDLE      syslogPipe[2];
00507 #else
00508     int         postmaster_alive_fds[2];
00509     int         syslogPipe[2];
00510 #endif
00511     char        my_exec_path[MAXPGPATH];
00512     char        pkglib_path[MAXPGPATH];
00513     char        ExtraOptions[MAXPGPATH];
00514 } BackendParameters;
00515 
00516 static void read_backend_variables(char *id, Port *port);
00517 static void restore_backend_variables(BackendParameters *param, Port *port);
00518 
00519 #ifndef WIN32
00520 static bool save_backend_variables(BackendParameters *param, Port *port);
00521 #else
00522 static bool save_backend_variables(BackendParameters *param, Port *port,
00523                        HANDLE childProcess, pid_t childPid);
00524 #endif
00525 
00526 static void ShmemBackendArrayAdd(Backend *bn);
00527 static void ShmemBackendArrayRemove(Backend *bn);
00528 
00529 static BackgroundWorker *find_bgworker_entry(int cookie);
00530 #endif   /* EXEC_BACKEND */
00531 
00532 #define StartupDataBase()       StartChildProcess(StartupProcess)
00533 #define StartBackgroundWriter() StartChildProcess(BgWriterProcess)
00534 #define StartCheckpointer()     StartChildProcess(CheckpointerProcess)
00535 #define StartWalWriter()        StartChildProcess(WalWriterProcess)
00536 #define StartWalReceiver()      StartChildProcess(WalReceiverProcess)
00537 
00538 /* Macros to check exit status of a child process */
00539 #define EXIT_STATUS_0(st)  ((st) == 0)
00540 #define EXIT_STATUS_1(st)  (WIFEXITED(st) && WEXITSTATUS(st) == 1)
00541 
00542 #ifndef WIN32
00543 /*
00544  * File descriptors for pipe used to monitor if postmaster is alive.
00545  * First is POSTMASTER_FD_WATCH, second is POSTMASTER_FD_OWN.
00546  */
00547 int         postmaster_alive_fds[2] = {-1, -1};
00548 #else
00549 /* Process handle of postmaster used for the same purpose on Windows */
00550 HANDLE      PostmasterHandle;
00551 #endif
00552 
00553 /*
00554  * Postmaster main entry point
00555  */
00556 void
00557 PostmasterMain(int argc, char *argv[])
00558 {
00559     int         opt;
00560     int         status;
00561     char       *userDoption = NULL;
00562     bool        listen_addr_saved = false;
00563     int         i;
00564 
00565     MyProcPid = PostmasterPid = getpid();
00566 
00567     MyStartTime = time(NULL);
00568 
00569     IsPostmasterEnvironment = true;
00570 
00571     /*
00572      * for security, no dir or file created can be group or other accessible
00573      */
00574     umask(S_IRWXG | S_IRWXO);
00575 
00576     /*
00577      * Fire up essential subsystems: memory management
00578      */
00579     MemoryContextInit();
00580 
00581     /*
00582      * By default, palloc() requests in the postmaster will be allocated in
00583      * the PostmasterContext, which is space that can be recycled by backends.
00584      * Allocated data that needs to be available to backends should be
00585      * allocated in TopMemoryContext.
00586      */
00587     PostmasterContext = AllocSetContextCreate(TopMemoryContext,
00588                                               "Postmaster",
00589                                               ALLOCSET_DEFAULT_MINSIZE,
00590                                               ALLOCSET_DEFAULT_INITSIZE,
00591                                               ALLOCSET_DEFAULT_MAXSIZE);
00592     MemoryContextSwitchTo(PostmasterContext);
00593 
00594     /* Initialize paths to installation files */
00595     getInstallationPaths(argv[0]);
00596 
00597     /*
00598      * Options setup
00599      */
00600     InitializeGUCOptions();
00601 
00602     opterr = 1;
00603 
00604     /*
00605      * Parse command-line options.  CAUTION: keep this in sync with
00606      * tcop/postgres.c (the option sets should not conflict) and with the
00607      * common help() function in main/main.c.
00608      */
00609     while ((opt = getopt(argc, argv, "A:B:bc:C:D:d:EeFf:h:ijk:lN:nOo:Pp:r:S:sTt:W:-:")) != -1)
00610     {
00611         switch (opt)
00612         {
00613             case 'A':
00614                 SetConfigOption("debug_assertions", optarg, PGC_POSTMASTER, PGC_S_ARGV);
00615                 break;
00616 
00617             case 'B':
00618                 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
00619                 break;
00620 
00621             case 'b':
00622                 /* Undocumented flag used for binary upgrades */
00623                 IsBinaryUpgrade = true;
00624                 break;
00625 
00626             case 'C':
00627                 output_config_variable = strdup(optarg);
00628                 break;
00629 
00630             case 'D':
00631                 userDoption = strdup(optarg);
00632                 break;
00633 
00634             case 'd':
00635                 set_debug_options(atoi(optarg), PGC_POSTMASTER, PGC_S_ARGV);
00636                 break;
00637 
00638             case 'E':
00639                 SetConfigOption("log_statement", "all", PGC_POSTMASTER, PGC_S_ARGV);
00640                 break;
00641 
00642             case 'e':
00643                 SetConfigOption("datestyle", "euro", PGC_POSTMASTER, PGC_S_ARGV);
00644                 break;
00645 
00646             case 'F':
00647                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
00648                 break;
00649 
00650             case 'f':
00651                 if (!set_plan_disabling_options(optarg, PGC_POSTMASTER, PGC_S_ARGV))
00652                 {
00653                     write_stderr("%s: invalid argument for option -f: \"%s\"\n",
00654                                  progname, optarg);
00655                     ExitPostmaster(1);
00656                 }
00657                 break;
00658 
00659             case 'h':
00660                 SetConfigOption("listen_addresses", optarg, PGC_POSTMASTER, PGC_S_ARGV);
00661                 break;
00662 
00663             case 'i':
00664                 SetConfigOption("listen_addresses", "*", PGC_POSTMASTER, PGC_S_ARGV);
00665                 break;
00666 
00667             case 'j':
00668                 /* only used by interactive backend */
00669                 break;
00670 
00671             case 'k':
00672                 SetConfigOption("unix_socket_directories", optarg, PGC_POSTMASTER, PGC_S_ARGV);
00673                 break;
00674 
00675             case 'l':
00676                 SetConfigOption("ssl", "true", PGC_POSTMASTER, PGC_S_ARGV);
00677                 break;
00678 
00679             case 'N':
00680                 SetConfigOption("max_connections", optarg, PGC_POSTMASTER, PGC_S_ARGV);
00681                 break;
00682 
00683             case 'n':
00684                 /* Don't reinit shared mem after abnormal exit */
00685                 Reinit = false;
00686                 break;
00687 
00688             case 'O':
00689                 SetConfigOption("allow_system_table_mods", "true", PGC_POSTMASTER, PGC_S_ARGV);
00690                 break;
00691 
00692             case 'o':
00693                 /* Other options to pass to the backend on the command line */
00694                 snprintf(ExtraOptions + strlen(ExtraOptions),
00695                          sizeof(ExtraOptions) - strlen(ExtraOptions),
00696                          " %s", optarg);
00697                 break;
00698 
00699             case 'P':
00700                 SetConfigOption("ignore_system_indexes", "true", PGC_POSTMASTER, PGC_S_ARGV);
00701                 break;
00702 
00703             case 'p':
00704                 SetConfigOption("port", optarg, PGC_POSTMASTER, PGC_S_ARGV);
00705                 break;
00706 
00707             case 'r':
00708                 /* only used by single-user backend */
00709                 break;
00710 
00711             case 'S':
00712                 SetConfigOption("work_mem", optarg, PGC_POSTMASTER, PGC_S_ARGV);
00713                 break;
00714 
00715             case 's':
00716                 SetConfigOption("log_statement_stats", "true", PGC_POSTMASTER, PGC_S_ARGV);
00717                 break;
00718 
00719             case 'T':
00720 
00721                 /*
00722                  * In the event that some backend dumps core, send SIGSTOP,
00723                  * rather than SIGQUIT, to all its peers.  This lets the wily
00724                  * post_hacker collect core dumps from everyone.
00725                  */
00726                 SendStop = true;
00727                 break;
00728 
00729             case 't':
00730                 {
00731                     const char *tmp = get_stats_option_name(optarg);
00732 
00733                     if (tmp)
00734                     {
00735                         SetConfigOption(tmp, "true", PGC_POSTMASTER, PGC_S_ARGV);
00736                     }
00737                     else
00738                     {
00739                         write_stderr("%s: invalid argument for option -t: \"%s\"\n",
00740                                      progname, optarg);
00741                         ExitPostmaster(1);
00742                     }
00743                     break;
00744                 }
00745 
00746             case 'W':
00747                 SetConfigOption("post_auth_delay", optarg, PGC_POSTMASTER, PGC_S_ARGV);
00748                 break;
00749 
00750             case 'c':
00751             case '-':
00752                 {
00753                     char       *name,
00754                                *value;
00755 
00756                     ParseLongOption(optarg, &name, &value);
00757                     if (!value)
00758                     {
00759                         if (opt == '-')
00760                             ereport(ERROR,
00761                                     (errcode(ERRCODE_SYNTAX_ERROR),
00762                                      errmsg("--%s requires a value",
00763                                             optarg)));
00764                         else
00765                             ereport(ERROR,
00766                                     (errcode(ERRCODE_SYNTAX_ERROR),
00767                                      errmsg("-c %s requires a value",
00768                                             optarg)));
00769                     }
00770 
00771                     SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
00772                     free(name);
00773                     if (value)
00774                         free(value);
00775                     break;
00776                 }
00777 
00778             default:
00779                 write_stderr("Try \"%s --help\" for more information.\n",
00780                              progname);
00781                 ExitPostmaster(1);
00782         }
00783     }
00784 
00785     /*
00786      * Postmaster accepts no non-option switch arguments.
00787      */
00788     if (optind < argc)
00789     {
00790         write_stderr("%s: invalid argument: \"%s\"\n",
00791                      progname, argv[optind]);
00792         write_stderr("Try \"%s --help\" for more information.\n",
00793                      progname);
00794         ExitPostmaster(1);
00795     }
00796 
00797     /*
00798      * Locate the proper configuration files and data directory, and read
00799      * postgresql.conf for the first time.
00800      */
00801     if (!SelectConfigFiles(userDoption, progname))
00802         ExitPostmaster(2);
00803 
00804     if (output_config_variable != NULL)
00805     {
00806         /*
00807          * permission is handled because the user is reading inside the data
00808          * dir
00809          */
00810         puts(GetConfigOption(output_config_variable, false, false));
00811         ExitPostmaster(0);
00812     }
00813 
00814     /* Verify that DataDir looks reasonable */
00815     checkDataDir();
00816 
00817     /* And switch working directory into it */
00818     ChangeToDataDir();
00819 
00820     /*
00821      * Check for invalid combinations of GUC settings.
00822      */
00823     if (ReservedBackends >= MaxConnections)
00824     {
00825         write_stderr("%s: superuser_reserved_connections must be less than max_connections\n", progname);
00826         ExitPostmaster(1);
00827     }
00828     if (max_wal_senders >= MaxConnections)
00829     {
00830         write_stderr("%s: max_wal_senders must be less than max_connections\n", progname);
00831         ExitPostmaster(1);
00832     }
00833     if (XLogArchiveMode && wal_level == WAL_LEVEL_MINIMAL)
00834         ereport(ERROR,
00835                 (errmsg("WAL archival (archive_mode=on) requires wal_level \"archive\" or \"hot_standby\"")));
00836     if (max_wal_senders > 0 && wal_level == WAL_LEVEL_MINIMAL)
00837         ereport(ERROR,
00838                 (errmsg("WAL streaming (max_wal_senders > 0) requires wal_level \"archive\" or \"hot_standby\"")));
00839 
00840     /*
00841      * Other one-time internal sanity checks can go here, if they are fast.
00842      * (Put any slow processing further down, after postmaster.pid creation.)
00843      */
00844     if (!CheckDateTokenTables())
00845     {
00846         write_stderr("%s: invalid datetoken tables, please fix\n", progname);
00847         ExitPostmaster(1);
00848     }
00849 
00850     /*
00851      * Now that we are done processing the postmaster arguments, reset
00852      * getopt(3) library so that it will work correctly in subprocesses.
00853      */
00854     optind = 1;
00855 #ifdef HAVE_INT_OPTRESET
00856     optreset = 1;               /* some systems need this too */
00857 #endif
00858 
00859     /* For debugging: display postmaster environment */
00860     {
00861         extern char **environ;
00862         char      **p;
00863 
00864         ereport(DEBUG3,
00865             (errmsg_internal("%s: PostmasterMain: initial environment dump:",
00866                              progname)));
00867         ereport(DEBUG3,
00868              (errmsg_internal("-----------------------------------------")));
00869         for (p = environ; *p; ++p)
00870             ereport(DEBUG3,
00871                     (errmsg_internal("\t%s", *p)));
00872         ereport(DEBUG3,
00873              (errmsg_internal("-----------------------------------------")));
00874     }
00875 
00876     /*
00877      * Create lockfile for data directory.
00878      *
00879      * We want to do this before we try to grab the input sockets, because the
00880      * data directory interlock is more reliable than the socket-file
00881      * interlock (thanks to whoever decided to put socket files in /tmp :-().
00882      * For the same reason, it's best to grab the TCP socket(s) before the
00883      * Unix socket(s).
00884      */
00885     CreateDataDirLockFile(true);
00886 
00887     /*
00888      * Initialize SSL library, if specified.
00889      */
00890 #ifdef USE_SSL
00891     if (EnableSSL)
00892         secure_initialize();
00893 #endif
00894 
00895     /*
00896      * process any libraries that should be preloaded at postmaster start
00897      */
00898     process_shared_preload_libraries();
00899 
00900     /*
00901      * Now that loadable modules have had their chance to register background
00902      * workers, calculate MaxBackends.
00903      */
00904     InitializeMaxBackends();
00905 
00906     /*
00907      * Establish input sockets.
00908      */
00909     for (i = 0; i < MAXLISTEN; i++)
00910         ListenSocket[i] = PGINVALID_SOCKET;
00911 
00912     if (ListenAddresses)
00913     {
00914         char       *rawstring;
00915         List       *elemlist;
00916         ListCell   *l;
00917         int         success = 0;
00918 
00919         /* Need a modifiable copy of ListenAddresses */
00920         rawstring = pstrdup(ListenAddresses);
00921 
00922         /* Parse string into list of hostnames */
00923         if (!SplitIdentifierString(rawstring, ',', &elemlist))
00924         {
00925             /* syntax error in list */
00926             ereport(FATAL,
00927                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
00928                      errmsg("invalid list syntax for \"listen_addresses\"")));
00929         }
00930 
00931         foreach(l, elemlist)
00932         {
00933             char       *curhost = (char *) lfirst(l);
00934 
00935             if (strcmp(curhost, "*") == 0)
00936                 status = StreamServerPort(AF_UNSPEC, NULL,
00937                                           (unsigned short) PostPortNumber,
00938                                           NULL,
00939                                           ListenSocket, MAXLISTEN);
00940             else
00941                 status = StreamServerPort(AF_UNSPEC, curhost,
00942                                           (unsigned short) PostPortNumber,
00943                                           NULL,
00944                                           ListenSocket, MAXLISTEN);
00945 
00946             if (status == STATUS_OK)
00947             {
00948                 success++;
00949                 /* record the first successful host addr in lockfile */
00950                 if (!listen_addr_saved)
00951                 {
00952                     AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, curhost);
00953                     listen_addr_saved = true;
00954                 }
00955             }
00956             else
00957                 ereport(WARNING,
00958                         (errmsg("could not create listen socket for \"%s\"",
00959                                 curhost)));
00960         }
00961 
00962         if (!success && elemlist != NIL)
00963             ereport(FATAL,
00964                     (errmsg("could not create any TCP/IP sockets")));
00965 
00966         list_free(elemlist);
00967         pfree(rawstring);
00968     }
00969 
00970 #ifdef USE_BONJOUR
00971     /* Register for Bonjour only if we opened TCP socket(s) */
00972     if (enable_bonjour && ListenSocket[0] != PGINVALID_SOCKET)
00973     {
00974         DNSServiceErrorType err;
00975 
00976         /*
00977          * We pass 0 for interface_index, which will result in registering on
00978          * all "applicable" interfaces.  It's not entirely clear from the
00979          * DNS-SD docs whether this would be appropriate if we have bound to
00980          * just a subset of the available network interfaces.
00981          */
00982         err = DNSServiceRegister(&bonjour_sdref,
00983                                  0,
00984                                  0,
00985                                  bonjour_name,
00986                                  "_postgresql._tcp.",
00987                                  NULL,
00988                                  NULL,
00989                                  htons(PostPortNumber),
00990                                  0,
00991                                  NULL,
00992                                  NULL,
00993                                  NULL);
00994         if (err != kDNSServiceErr_NoError)
00995             elog(LOG, "DNSServiceRegister() failed: error code %ld",
00996                  (long) err);
00997 
00998         /*
00999          * We don't bother to read the mDNS daemon's reply, and we expect that
01000          * it will automatically terminate our registration when the socket is
01001          * closed at postmaster termination.  So there's nothing more to be
01002          * done here.  However, the bonjour_sdref is kept around so that
01003          * forked children can close their copies of the socket.
01004          */
01005     }
01006 #endif
01007 
01008 #ifdef HAVE_UNIX_SOCKETS
01009     if (Unix_socket_directories)
01010     {
01011         char       *rawstring;
01012         List       *elemlist;
01013         ListCell   *l;
01014         int         success = 0;
01015 
01016         /* Need a modifiable copy of Unix_socket_directories */
01017         rawstring = pstrdup(Unix_socket_directories);
01018 
01019         /* Parse string into list of directories */
01020         if (!SplitDirectoriesString(rawstring, ',', &elemlist))
01021         {
01022             /* syntax error in list */
01023             ereport(FATAL,
01024                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
01025                      errmsg("invalid list syntax for \"unix_socket_directories\"")));
01026         }
01027 
01028         foreach(l, elemlist)
01029         {
01030             char       *socketdir = (char *) lfirst(l);
01031 
01032             status = StreamServerPort(AF_UNIX, NULL,
01033                                       (unsigned short) PostPortNumber,
01034                                       socketdir,
01035                                       ListenSocket, MAXLISTEN);
01036 
01037             if (status == STATUS_OK)
01038             {
01039                 success++;
01040                 /* record the first successful Unix socket in lockfile */
01041                 if (success == 1)
01042                     AddToDataDirLockFile(LOCK_FILE_LINE_SOCKET_DIR, socketdir);
01043             }
01044             else
01045                 ereport(WARNING,
01046                         (errmsg("could not create Unix-domain socket in directory \"%s\"",
01047                                 socketdir)));
01048         }
01049 
01050         if (!success && elemlist != NIL)
01051             ereport(FATAL,
01052                     (errmsg("could not create any Unix-domain sockets")));
01053 
01054         list_free_deep(elemlist);
01055         pfree(rawstring);
01056     }
01057 #endif
01058 
01059     /*
01060      * check that we have some socket to listen on
01061      */
01062     if (ListenSocket[0] == PGINVALID_SOCKET)
01063         ereport(FATAL,
01064                 (errmsg("no socket created for listening")));
01065 
01066     /*
01067      * If no valid TCP ports, write an empty line for listen address,
01068      * indicating the Unix socket must be used.  Note that this line is not
01069      * added to the lock file until there is a socket backing it.
01070      */
01071     if (!listen_addr_saved)
01072         AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, "");
01073 
01074     /*
01075      * Set up shared memory and semaphores.
01076      */
01077     reset_shared(PostPortNumber);
01078 
01079     /*
01080      * Estimate number of openable files.  This must happen after setting up
01081      * semaphores, because on some platforms semaphores count as open files.
01082      */
01083     set_max_safe_fds();
01084 
01085     /*
01086      * Set reference point for stack-depth checking.
01087      */
01088     set_stack_base();
01089 
01090     /*
01091      * Initialize pipe (or process handle on Windows) that allows children to
01092      * wake up from sleep on postmaster death.
01093      */
01094     InitPostmasterDeathWatchHandle();
01095 
01096 #ifdef WIN32
01097 
01098     /*
01099      * Initialize I/O completion port used to deliver list of dead children.
01100      */
01101     win32ChildQueue = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
01102     if (win32ChildQueue == NULL)
01103         ereport(FATAL,
01104            (errmsg("could not create I/O completion port for child queue")));
01105 #endif
01106 
01107     /*
01108      * Record postmaster options.  We delay this till now to avoid recording
01109      * bogus options (eg, NBuffers too high for available memory).
01110      */
01111     if (!CreateOptsFile(argc, argv, my_exec_path))
01112         ExitPostmaster(1);
01113 
01114 #ifdef EXEC_BACKEND
01115     /* Write out nondefault GUC settings for child processes to use */
01116     write_nondefault_variables(PGC_POSTMASTER);
01117 #endif
01118 
01119     /*
01120      * Write the external PID file if requested
01121      */
01122     if (external_pid_file)
01123     {
01124         FILE       *fpidfile = fopen(external_pid_file, "w");
01125 
01126         if (fpidfile)
01127         {
01128             fprintf(fpidfile, "%d\n", MyProcPid);
01129             fclose(fpidfile);
01130 
01131             /* Make PID file world readable */
01132             if (chmod(external_pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0)
01133                 write_stderr("%s: could not change permissions of external PID file \"%s\": %s\n",
01134                              progname, external_pid_file, strerror(errno));
01135         }
01136         else
01137             write_stderr("%s: could not write external PID file \"%s\": %s\n",
01138                          progname, external_pid_file, strerror(errno));
01139 
01140         on_proc_exit(unlink_external_pid_file, 0);
01141     }
01142 
01143     /*
01144      * Set up signal handlers for the postmaster process.
01145      *
01146      * CAUTION: when changing this list, check for side-effects on the signal
01147      * handling setup of child processes.  See tcop/postgres.c,
01148      * bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
01149      * postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c,
01150      * postmaster/syslogger.c, postmaster/bgworker.c and
01151      * postmaster/checkpointer.c.
01152      */
01153     pqinitmask();
01154     PG_SETMASK(&BlockSig);
01155 
01156     pqsignal(SIGHUP, SIGHUP_handler);   /* reread config file and have
01157                                          * children do same */
01158     pqsignal(SIGINT, pmdie);    /* send SIGTERM and shut down */
01159     pqsignal(SIGQUIT, pmdie);   /* send SIGQUIT and die */
01160     pqsignal(SIGTERM, pmdie);   /* wait for children and shut down */
01161     pqsignal(SIGALRM, SIG_IGN); /* ignored */
01162     pqsignal(SIGPIPE, SIG_IGN); /* ignored */
01163     pqsignal(SIGUSR1, sigusr1_handler); /* message from child process */
01164     pqsignal(SIGUSR2, dummy_handler);   /* unused, reserve for children */
01165     pqsignal(SIGCHLD, reaper);  /* handle child termination */
01166     pqsignal(SIGTTIN, SIG_IGN); /* ignored */
01167     pqsignal(SIGTTOU, SIG_IGN); /* ignored */
01168     /* ignore SIGXFSZ, so that ulimit violations work like disk full */
01169 #ifdef SIGXFSZ
01170     pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
01171 #endif
01172 
01173     /*
01174      * If enabled, start up syslogger collection subprocess
01175      */
01176     SysLoggerPID = SysLogger_Start();
01177 
01178     /*
01179      * Reset whereToSendOutput from DestDebug (its starting state) to
01180      * DestNone. This stops ereport from sending log messages to stderr unless
01181      * Log_destination permits.  We don't do this until the postmaster is
01182      * fully launched, since startup failures may as well be reported to
01183      * stderr.
01184      */
01185     whereToSendOutput = DestNone;
01186 
01187     /*
01188      * Initialize stats collection subsystem (this does NOT start the
01189      * collector process!)
01190      */
01191     pgstat_init();
01192 
01193     /*
01194      * Initialize the autovacuum subsystem (again, no process start yet)
01195      */
01196     autovac_init();
01197 
01198     /*
01199      * Load configuration files for client authentication.
01200      */
01201     if (!load_hba())
01202     {
01203         /*
01204          * It makes no sense to continue if we fail to load the HBA file,
01205          * since there is no way to connect to the database in this case.
01206          */
01207         ereport(FATAL,
01208                 (errmsg("could not load pg_hba.conf")));
01209     }
01210     if (!load_ident())
01211     {
01212         /*
01213          * We can start up without the IDENT file, although it means that you
01214          * cannot log in using any of the authentication methods that need a
01215          * user name mapping. load_ident() already logged the details of
01216          * error to the log.
01217          */
01218     }
01219 
01220 
01221     /*
01222      * Remove old temporary files.  At this point there can be no other
01223      * Postgres processes running in this directory, so this should be safe.
01224      */
01225     RemovePgTempFiles();
01226 
01227     /*
01228      * Remember postmaster startup time
01229      */
01230     PgStartTime = GetCurrentTimestamp();
01231     /* PostmasterRandom wants its own copy */
01232     gettimeofday(&random_start_time, NULL);
01233 
01234     /*
01235      * We're ready to rock and roll...
01236      */
01237     StartupPID = StartupDataBase();
01238     Assert(StartupPID != 0);
01239     pmState = PM_STARTUP;
01240 
01241     /* Some workers may be scheduled to start now */
01242     StartOneBackgroundWorker();
01243 
01244     status = ServerLoop();
01245 
01246     /*
01247      * ServerLoop probably shouldn't ever return, but if it does, close down.
01248      */
01249     ExitPostmaster(status != STATUS_OK);
01250 
01251     abort();                    /* not reached */
01252 }
01253 
01254 
01255 /*
01256  * on_proc_exit callback to delete external_pid_file
01257  */
01258 static void
01259 unlink_external_pid_file(int status, Datum arg)
01260 {
01261     if (external_pid_file)
01262         unlink(external_pid_file);
01263 }
01264 
01265 
01266 /*
01267  * Compute and check the directory paths to files that are part of the
01268  * installation (as deduced from the postgres executable's own location)
01269  */
01270 static void
01271 getInstallationPaths(const char *argv0)
01272 {
01273     DIR        *pdir;
01274 
01275     /* Locate the postgres executable itself */
01276     if (find_my_exec(argv0, my_exec_path) < 0)
01277         elog(FATAL, "%s: could not locate my own executable path", argv0);
01278 
01279 #ifdef EXEC_BACKEND
01280     /* Locate executable backend before we change working directory */
01281     if (find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
01282                         postgres_exec_path) < 0)
01283         ereport(FATAL,
01284                 (errmsg("%s: could not locate matching postgres executable",
01285                         argv0)));
01286 #endif
01287 
01288     /*
01289      * Locate the pkglib directory --- this has to be set early in case we try
01290      * to load any modules from it in response to postgresql.conf entries.
01291      */
01292     get_pkglib_path(my_exec_path, pkglib_path);
01293 
01294     /*
01295      * Verify that there's a readable directory there; otherwise the Postgres
01296      * installation is incomplete or corrupt.  (A typical cause of this
01297      * failure is that the postgres executable has been moved or hardlinked to
01298      * some directory that's not a sibling of the installation lib/
01299      * directory.)
01300      */
01301     pdir = AllocateDir(pkglib_path);
01302     if (pdir == NULL)
01303         ereport(ERROR,
01304                 (errcode_for_file_access(),
01305                  errmsg("could not open directory \"%s\": %m",
01306                         pkglib_path),
01307                  errhint("This may indicate an incomplete PostgreSQL installation, or that the file \"%s\" has been moved away from its proper location.",
01308                          my_exec_path)));
01309     FreeDir(pdir);
01310 
01311     /*
01312      * XXX is it worth similarly checking the share/ directory?  If the lib/
01313      * directory is there, then share/ probably is too.
01314      */
01315 }
01316 
01317 
01318 /*
01319  * Validate the proposed data directory
01320  */
01321 static void
01322 checkDataDir(void)
01323 {
01324     char        path[MAXPGPATH];
01325     FILE       *fp;
01326     struct stat stat_buf;
01327 
01328     Assert(DataDir);
01329 
01330     if (stat(DataDir, &stat_buf) != 0)
01331     {
01332         if (errno == ENOENT)
01333             ereport(FATAL,
01334                     (errcode_for_file_access(),
01335                      errmsg("data directory \"%s\" does not exist",
01336                             DataDir)));
01337         else
01338             ereport(FATAL,
01339                     (errcode_for_file_access(),
01340                  errmsg("could not read permissions of directory \"%s\": %m",
01341                         DataDir)));
01342     }
01343 
01344     /* eventual chdir would fail anyway, but let's test ... */
01345     if (!S_ISDIR(stat_buf.st_mode))
01346         ereport(FATAL,
01347                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
01348                  errmsg("specified data directory \"%s\" is not a directory",
01349                         DataDir)));
01350 
01351     /*
01352      * Check that the directory belongs to my userid; if not, reject.
01353      *
01354      * This check is an essential part of the interlock that prevents two
01355      * postmasters from starting in the same directory (see CreateLockFile()).
01356      * Do not remove or weaken it.
01357      *
01358      * XXX can we safely enable this check on Windows?
01359      */
01360 #if !defined(WIN32) && !defined(__CYGWIN__)
01361     if (stat_buf.st_uid != geteuid())
01362         ereport(FATAL,
01363                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
01364                  errmsg("data directory \"%s\" has wrong ownership",
01365                         DataDir),
01366                  errhint("The server must be started by the user that owns the data directory.")));
01367 #endif
01368 
01369     /*
01370      * Check if the directory has group or world access.  If so, reject.
01371      *
01372      * It would be possible to allow weaker constraints (for example, allow
01373      * group access) but we cannot make a general assumption that that is
01374      * okay; for example there are platforms where nearly all users
01375      * customarily belong to the same group.  Perhaps this test should be
01376      * configurable.
01377      *
01378      * XXX temporarily suppress check when on Windows, because there may not
01379      * be proper support for Unix-y file permissions.  Need to think of a
01380      * reasonable check to apply on Windows.
01381      */
01382 #if !defined(WIN32) && !defined(__CYGWIN__)
01383     if (stat_buf.st_mode & (S_IRWXG | S_IRWXO))
01384         ereport(FATAL,
01385                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
01386                  errmsg("data directory \"%s\" has group or world access",
01387                         DataDir),
01388                  errdetail("Permissions should be u=rwx (0700).")));
01389 #endif
01390 
01391     /* Look for PG_VERSION before looking for pg_control */
01392     ValidatePgVersion(DataDir);
01393 
01394     snprintf(path, sizeof(path), "%s/global/pg_control", DataDir);
01395 
01396     fp = AllocateFile(path, PG_BINARY_R);
01397     if (fp == NULL)
01398     {
01399         write_stderr("%s: could not find the database system\n"
01400                      "Expected to find it in the directory \"%s\",\n"
01401                      "but could not open file \"%s\": %s\n",
01402                      progname, DataDir, path, strerror(errno));
01403         ExitPostmaster(2);
01404     }
01405     FreeFile(fp);
01406 }
01407 
01408 /*
01409  * Determine how long should we let ServerLoop sleep.
01410  *
01411  * In normal conditions we wait at most one minute, to ensure that the other
01412  * background tasks handled by ServerLoop get done even when no requests are
01413  * arriving.  However, if there are background workers waiting to be started,
01414  * we don't actually sleep so that they are quickly serviced.
01415  */
01416 static void
01417 DetermineSleepTime(struct timeval *timeout)
01418 {
01419     TimestampTz next_wakeup = 0;
01420 
01421     /*
01422      * Normal case: either there are no background workers at all, or we're in
01423      * a shutdown sequence (during which we ignore bgworkers altogether).
01424      */
01425     if (Shutdown > NoShutdown ||
01426         (!StartWorkerNeeded && !HaveCrashedWorker))
01427     {
01428         timeout->tv_sec = 60;
01429         timeout->tv_usec = 0;
01430         return;
01431     }
01432 
01433     if (StartWorkerNeeded)
01434     {
01435         timeout->tv_sec = 0;
01436         timeout->tv_usec = 0;
01437         return;
01438     }
01439 
01440     if (HaveCrashedWorker)
01441     {
01442         slist_iter  siter;
01443 
01444         /*
01445          * When there are crashed bgworkers, we sleep just long enough that
01446          * they are restarted when they request to be.  Scan the list to
01447          * determine the minimum of all wakeup times according to most recent
01448          * crash time and requested restart interval.
01449          */
01450         slist_foreach(siter, &BackgroundWorkerList)
01451         {
01452             RegisteredBgWorker *rw;
01453             TimestampTz this_wakeup;
01454 
01455             rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
01456 
01457             if (rw->rw_crashed_at == 0)
01458                 continue;
01459 
01460             if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
01461                 continue;
01462 
01463             this_wakeup = TimestampTzPlusMilliseconds(rw->rw_crashed_at,
01464                                      1000L * rw->rw_worker.bgw_restart_time);
01465             if (next_wakeup == 0 || this_wakeup < next_wakeup)
01466                 next_wakeup = this_wakeup;
01467         }
01468     }
01469 
01470     if (next_wakeup != 0)
01471     {
01472         int         microsecs;
01473 
01474         TimestampDifference(GetCurrentTimestamp(), next_wakeup,
01475                             &timeout->tv_sec, &microsecs);
01476         timeout->tv_usec = microsecs;
01477 
01478         /* Ensure we don't exceed one minute */
01479         if (timeout->tv_sec > 60)
01480         {
01481             timeout->tv_sec = 60;
01482             timeout->tv_usec = 0;
01483         }
01484     }
01485     else
01486     {
01487         timeout->tv_sec = 60;
01488         timeout->tv_usec = 0;
01489     }
01490 }
01491 
01492 /*
01493  * Main idle loop of postmaster
01494  */
01495 static int
01496 ServerLoop(void)
01497 {
01498     fd_set      readmask;
01499     int         nSockets;
01500     time_t      now,
01501                 last_touch_time;
01502 
01503     last_touch_time = time(NULL);
01504 
01505     nSockets = initMasks(&readmask);
01506 
01507     for (;;)
01508     {
01509         fd_set      rmask;
01510         int         selres;
01511 
01512         /*
01513          * Wait for a connection request to arrive.
01514          *
01515          * If we are in PM_WAIT_DEAD_END state, then we don't want to accept
01516          * any new connections, so we don't call select() at all; just sleep
01517          * for a little bit with signals unblocked.
01518          */
01519         memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
01520 
01521         PG_SETMASK(&UnBlockSig);
01522 
01523         if (pmState == PM_WAIT_DEAD_END)
01524         {
01525             pg_usleep(100000L); /* 100 msec seems reasonable */
01526             selres = 0;
01527         }
01528         else
01529         {
01530             /* must set timeout each time; some OSes change it! */
01531             struct timeval timeout;
01532 
01533             DetermineSleepTime(&timeout);
01534 
01535             selres = select(nSockets, &rmask, NULL, NULL, &timeout);
01536         }
01537 
01538         /*
01539          * Block all signals until we wait again.  (This makes it safe for our
01540          * signal handlers to do nontrivial work.)
01541          */
01542         PG_SETMASK(&BlockSig);
01543 
01544         /* Now check the select() result */
01545         if (selres < 0)
01546         {
01547             if (errno != EINTR && errno != EWOULDBLOCK)
01548             {
01549                 ereport(LOG,
01550                         (errcode_for_socket_access(),
01551                          errmsg("select() failed in postmaster: %m")));
01552                 return STATUS_ERROR;
01553             }
01554         }
01555 
01556         /*
01557          * New connection pending on any of our sockets? If so, fork a child
01558          * process to deal with it.
01559          */
01560         if (selres > 0)
01561         {
01562             int         i;
01563 
01564             for (i = 0; i < MAXLISTEN; i++)
01565             {
01566                 if (ListenSocket[i] == PGINVALID_SOCKET)
01567                     break;
01568                 if (FD_ISSET(ListenSocket[i], &rmask))
01569                 {
01570                     Port       *port;
01571 
01572                     port = ConnCreate(ListenSocket[i]);
01573                     if (port)
01574                     {
01575                         BackendStartup(port);
01576 
01577                         /*
01578                          * We no longer need the open socket or port structure
01579                          * in this process
01580                          */
01581                         StreamClose(port->sock);
01582                         ConnFree(port);
01583                     }
01584                 }
01585             }
01586         }
01587 
01588         /* If we have lost the log collector, try to start a new one */
01589         if (SysLoggerPID == 0 && Logging_collector)
01590             SysLoggerPID = SysLogger_Start();
01591 
01592         /*
01593          * If no background writer process is running, and we are not in a
01594          * state that prevents it, start one.  It doesn't matter if this
01595          * fails, we'll just try again later.  Likewise for the checkpointer.
01596          */
01597         if (pmState == PM_RUN || pmState == PM_RECOVERY ||
01598             pmState == PM_HOT_STANDBY)
01599         {
01600             if (CheckpointerPID == 0)
01601                 CheckpointerPID = StartCheckpointer();
01602             if (BgWriterPID == 0)
01603                 BgWriterPID = StartBackgroundWriter();
01604         }
01605 
01606         /*
01607          * Likewise, if we have lost the walwriter process, try to start a new
01608          * one.  But this is needed only in normal operation (else we cannot
01609          * be writing any new WAL).
01610          */
01611         if (WalWriterPID == 0 && pmState == PM_RUN)
01612             WalWriterPID = StartWalWriter();
01613 
01614         /*
01615          * If we have lost the autovacuum launcher, try to start a new one. We
01616          * don't want autovacuum to run in binary upgrade mode because
01617          * autovacuum might update relfrozenxid for empty tables before the
01618          * physical files are put in place.
01619          */
01620         if (!IsBinaryUpgrade && AutoVacPID == 0 &&
01621             (AutoVacuumingActive() || start_autovac_launcher) &&
01622             pmState == PM_RUN)
01623         {
01624             AutoVacPID = StartAutoVacLauncher();
01625             if (AutoVacPID != 0)
01626                 start_autovac_launcher = false; /* signal processed */
01627         }
01628 
01629         /* If we have lost the archiver, try to start a new one */
01630         if (XLogArchivingActive() && PgArchPID == 0 && pmState == PM_RUN)
01631             PgArchPID = pgarch_start();
01632 
01633         /* If we have lost the stats collector, try to start a new one */
01634         if (PgStatPID == 0 && pmState == PM_RUN)
01635             PgStatPID = pgstat_start();
01636 
01637         /* If we need to signal the autovacuum launcher, do so now */
01638         if (avlauncher_needs_signal)
01639         {
01640             avlauncher_needs_signal = false;
01641             if (AutoVacPID != 0)
01642                 kill(AutoVacPID, SIGUSR2);
01643         }
01644 
01645         /* Get other worker processes running, if needed */
01646         if (StartWorkerNeeded || HaveCrashedWorker)
01647             StartOneBackgroundWorker();
01648 
01649         /*
01650          * Touch Unix socket and lock files every 58 minutes, to ensure that
01651          * they are not removed by overzealous /tmp-cleaning tasks.  We assume
01652          * no one runs cleaners with cutoff times of less than an hour ...
01653          */
01654         now = time(NULL);
01655         if (now - last_touch_time >= 58 * SECS_PER_MINUTE)
01656         {
01657             TouchSocketFiles();
01658             TouchSocketLockFiles();
01659             last_touch_time = now;
01660         }
01661     }
01662 }
01663 
01664 /*
01665  * Initialise the masks for select() for the ports we are listening on.
01666  * Return the number of sockets to listen on.
01667  */
01668 static int
01669 initMasks(fd_set *rmask)
01670 {
01671     int         maxsock = -1;
01672     int         i;
01673 
01674     FD_ZERO(rmask);
01675 
01676     for (i = 0; i < MAXLISTEN; i++)
01677     {
01678         int         fd = ListenSocket[i];
01679 
01680         if (fd == PGINVALID_SOCKET)
01681             break;
01682         FD_SET(fd, rmask);
01683 
01684         if (fd > maxsock)
01685             maxsock = fd;
01686     }
01687 
01688     return maxsock + 1;
01689 }
01690 
01691 
01692 /*
01693  * Read a client's startup packet and do something according to it.
01694  *
01695  * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
01696  * not return at all.
01697  *
01698  * (Note that ereport(FATAL) stuff is sent to the client, so only use it
01699  * if that's what you want.  Return STATUS_ERROR if you don't want to
01700  * send anything to the client, which would typically be appropriate
01701  * if we detect a communications failure.)
01702  */
01703 static int
01704 ProcessStartupPacket(Port *port, bool SSLdone)
01705 {
01706     int32       len;
01707     void       *buf;
01708     ProtocolVersion proto;
01709     MemoryContext oldcontext;
01710 
01711     if (pq_getbytes((char *) &len, 4) == EOF)
01712     {
01713         /*
01714          * EOF after SSLdone probably means the client didn't like our
01715          * response to NEGOTIATE_SSL_CODE.  That's not an error condition, so
01716          * don't clutter the log with a complaint.
01717          */
01718         if (!SSLdone)
01719             ereport(COMMERROR,
01720                     (errcode(ERRCODE_PROTOCOL_VIOLATION),
01721                      errmsg("incomplete startup packet")));
01722         return STATUS_ERROR;
01723     }
01724 
01725     len = ntohl(len);
01726     len -= 4;
01727 
01728     if (len < (int32) sizeof(ProtocolVersion) ||
01729         len > MAX_STARTUP_PACKET_LENGTH)
01730     {
01731         ereport(COMMERROR,
01732                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
01733                  errmsg("invalid length of startup packet")));
01734         return STATUS_ERROR;
01735     }
01736 
01737     /*
01738      * Allocate at least the size of an old-style startup packet, plus one
01739      * extra byte, and make sure all are zeroes.  This ensures we will have
01740      * null termination of all strings, in both fixed- and variable-length
01741      * packet layouts.
01742      */
01743     if (len <= (int32) sizeof(StartupPacket))
01744         buf = palloc0(sizeof(StartupPacket) + 1);
01745     else
01746         buf = palloc0(len + 1);
01747 
01748     if (pq_getbytes(buf, len) == EOF)
01749     {
01750         ereport(COMMERROR,
01751                 (errcode(ERRCODE_PROTOCOL_VIOLATION),
01752                  errmsg("incomplete startup packet")));
01753         return STATUS_ERROR;
01754     }
01755 
01756     /*
01757      * The first field is either a protocol version number or a special
01758      * request code.
01759      */
01760     port->proto = proto = ntohl(*((ProtocolVersion *) buf));
01761 
01762     if (proto == CANCEL_REQUEST_CODE)
01763     {
01764         processCancelRequest(port, buf);
01765         /* Not really an error, but we don't want to proceed further */
01766         return STATUS_ERROR;
01767     }
01768 
01769     if (proto == NEGOTIATE_SSL_CODE && !SSLdone)
01770     {
01771         char        SSLok;
01772 
01773 #ifdef USE_SSL
01774         /* No SSL when disabled or on Unix sockets */
01775         if (!EnableSSL || IS_AF_UNIX(port->laddr.addr.ss_family))
01776             SSLok = 'N';
01777         else
01778             SSLok = 'S';        /* Support for SSL */
01779 #else
01780         SSLok = 'N';            /* No support for SSL */
01781 #endif
01782 
01783 retry1:
01784         if (send(port->sock, &SSLok, 1, 0) != 1)
01785         {
01786             if (errno == EINTR)
01787                 goto retry1;    /* if interrupted, just retry */
01788             ereport(COMMERROR,
01789                     (errcode_for_socket_access(),
01790                      errmsg("failed to send SSL negotiation response: %m")));
01791             return STATUS_ERROR;    /* close the connection */
01792         }
01793 
01794 #ifdef USE_SSL
01795         if (SSLok == 'S' && secure_open_server(port) == -1)
01796             return STATUS_ERROR;
01797 #endif
01798         /* regular startup packet, cancel, etc packet should follow... */
01799         /* but not another SSL negotiation request */
01800         return ProcessStartupPacket(port, true);
01801     }
01802 
01803     /* Could add additional special packet types here */
01804 
01805     /*
01806      * Set FrontendProtocol now so that ereport() knows what format to send if
01807      * we fail during startup.
01808      */
01809     FrontendProtocol = proto;
01810 
01811     /* Check we can handle the protocol the frontend is using. */
01812 
01813     if (PG_PROTOCOL_MAJOR(proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) ||
01814         PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) ||
01815         (PG_PROTOCOL_MAJOR(proto) == PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST) &&
01816          PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST)))
01817         ereport(FATAL,
01818                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
01819                  errmsg("unsupported frontend protocol %u.%u: server supports %u.0 to %u.%u",
01820                         PG_PROTOCOL_MAJOR(proto), PG_PROTOCOL_MINOR(proto),
01821                         PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST),
01822                         PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST),
01823                         PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST))));
01824 
01825     /*
01826      * Now fetch parameters out of startup packet and save them into the Port
01827      * structure.  All data structures attached to the Port struct must be
01828      * allocated in TopMemoryContext so that they will remain available in a
01829      * running backend (even after PostmasterContext is destroyed).  We need
01830      * not worry about leaking this storage on failure, since we aren't in the
01831      * postmaster process anymore.
01832      */
01833     oldcontext = MemoryContextSwitchTo(TopMemoryContext);
01834 
01835     if (PG_PROTOCOL_MAJOR(proto) >= 3)
01836     {
01837         int32       offset = sizeof(ProtocolVersion);
01838 
01839         /*
01840          * Scan packet body for name/option pairs.  We can assume any string
01841          * beginning within the packet body is null-terminated, thanks to
01842          * zeroing extra byte above.
01843          */
01844         port->guc_options = NIL;
01845 
01846         while (offset < len)
01847         {
01848             char       *nameptr = ((char *) buf) + offset;
01849             int32       valoffset;
01850             char       *valptr;
01851 
01852             if (*nameptr == '\0')
01853                 break;          /* found packet terminator */
01854             valoffset = offset + strlen(nameptr) + 1;
01855             if (valoffset >= len)
01856                 break;          /* missing value, will complain below */
01857             valptr = ((char *) buf) + valoffset;
01858 
01859             if (strcmp(nameptr, "database") == 0)
01860                 port->database_name = pstrdup(valptr);
01861             else if (strcmp(nameptr, "user") == 0)
01862                 port->user_name = pstrdup(valptr);
01863             else if (strcmp(nameptr, "options") == 0)
01864                 port->cmdline_options = pstrdup(valptr);
01865             else if (strcmp(nameptr, "replication") == 0)
01866             {
01867                 if (!parse_bool(valptr, &am_walsender))
01868                     ereport(FATAL,
01869                             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
01870                              errmsg("invalid value for boolean option \"replication\"")));
01871             }
01872             else
01873             {
01874                 /* Assume it's a generic GUC option */
01875                 port->guc_options = lappend(port->guc_options,
01876                                             pstrdup(nameptr));
01877                 port->guc_options = lappend(port->guc_options,
01878                                             pstrdup(valptr));
01879             }
01880             offset = valoffset + strlen(valptr) + 1;
01881         }
01882 
01883         /*
01884          * If we didn't find a packet terminator exactly at the end of the
01885          * given packet length, complain.
01886          */
01887         if (offset != len - 1)
01888             ereport(FATAL,
01889                     (errcode(ERRCODE_PROTOCOL_VIOLATION),
01890                      errmsg("invalid startup packet layout: expected terminator as last byte")));
01891     }
01892     else
01893     {
01894         /*
01895          * Get the parameters from the old-style, fixed-width-fields startup
01896          * packet as C strings.  The packet destination was cleared first so a
01897          * short packet has zeros silently added.  We have to be prepared to
01898          * truncate the pstrdup result for oversize fields, though.
01899          */
01900         StartupPacket *packet = (StartupPacket *) buf;
01901 
01902         port->database_name = pstrdup(packet->database);
01903         if (strlen(port->database_name) > sizeof(packet->database))
01904             port->database_name[sizeof(packet->database)] = '\0';
01905         port->user_name = pstrdup(packet->user);
01906         if (strlen(port->user_name) > sizeof(packet->user))
01907             port->user_name[sizeof(packet->user)] = '\0';
01908         port->cmdline_options = pstrdup(packet->options);
01909         if (strlen(port->cmdline_options) > sizeof(packet->options))
01910             port->cmdline_options[sizeof(packet->options)] = '\0';
01911         port->guc_options = NIL;
01912     }
01913 
01914     /* Check a user name was given. */
01915     if (port->user_name == NULL || port->user_name[0] == '\0')
01916         ereport(FATAL,
01917                 (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
01918              errmsg("no PostgreSQL user name specified in startup packet")));
01919 
01920     /* The database defaults to the user name. */
01921     if (port->database_name == NULL || port->database_name[0] == '\0')
01922         port->database_name = pstrdup(port->user_name);
01923 
01924     if (Db_user_namespace)
01925     {
01926         /*
01927          * If user@, it is a global user, remove '@'. We only want to do this
01928          * if there is an '@' at the end and no earlier in the user string or
01929          * they may fake as a local user of another database attaching to this
01930          * database.
01931          */
01932         if (strchr(port->user_name, '@') ==
01933             port->user_name + strlen(port->user_name) - 1)
01934             *strchr(port->user_name, '@') = '\0';
01935         else
01936         {
01937             /* Append '@' and dbname */
01938             char       *db_user;
01939 
01940             db_user = palloc(strlen(port->user_name) +
01941                              strlen(port->database_name) + 2);
01942             sprintf(db_user, "%s@%s", port->user_name, port->database_name);
01943             port->user_name = db_user;
01944         }
01945     }
01946 
01947     /*
01948      * Truncate given database and user names to length of a Postgres name.
01949      * This avoids lookup failures when overlength names are given.
01950      */
01951     if (strlen(port->database_name) >= NAMEDATALEN)
01952         port->database_name[NAMEDATALEN - 1] = '\0';
01953     if (strlen(port->user_name) >= NAMEDATALEN)
01954         port->user_name[NAMEDATALEN - 1] = '\0';
01955 
01956     /* Walsender is not related to a particular database */
01957     if (am_walsender)
01958         port->database_name[0] = '\0';
01959 
01960     /*
01961      * Done putting stuff in TopMemoryContext.
01962      */
01963     MemoryContextSwitchTo(oldcontext);
01964 
01965     /*
01966      * If we're going to reject the connection due to database state, say so
01967      * now instead of wasting cycles on an authentication exchange. (This also
01968      * allows a pg_ping utility to be written.)
01969      */
01970     switch (port->canAcceptConnections)
01971     {
01972         case CAC_STARTUP:
01973             ereport(FATAL,
01974                     (errcode(ERRCODE_CANNOT_CONNECT_NOW),
01975                      errmsg("the database system is starting up")));
01976             break;
01977         case CAC_SHUTDOWN:
01978             ereport(FATAL,
01979                     (errcode(ERRCODE_CANNOT_CONNECT_NOW),
01980                      errmsg("the database system is shutting down")));
01981             break;
01982         case CAC_RECOVERY:
01983             ereport(FATAL,
01984                     (errcode(ERRCODE_CANNOT_CONNECT_NOW),
01985                      errmsg("the database system is in recovery mode")));
01986             break;
01987         case CAC_TOOMANY:
01988             ereport(FATAL,
01989                     (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
01990                      errmsg("sorry, too many clients already")));
01991             break;
01992         case CAC_WAITBACKUP:
01993             /* OK for now, will check in InitPostgres */
01994             break;
01995         case CAC_OK:
01996             break;
01997     }
01998 
01999     return STATUS_OK;
02000 }
02001 
02002 
02003 /*
02004  * The client has sent a cancel request packet, not a normal
02005  * start-a-new-connection packet.  Perform the necessary processing.
02006  * Nothing is sent back to the client.
02007  */
02008 static void
02009 processCancelRequest(Port *port, void *pkt)
02010 {
02011     CancelRequestPacket *canc = (CancelRequestPacket *) pkt;
02012     int         backendPID;
02013     long        cancelAuthCode;
02014     Backend    *bp;
02015 
02016 #ifndef EXEC_BACKEND
02017     dlist_iter  iter;
02018 #else
02019     int         i;
02020 #endif
02021 
02022     backendPID = (int) ntohl(canc->backendPID);
02023     cancelAuthCode = (long) ntohl(canc->cancelAuthCode);
02024 
02025     /*
02026      * See if we have a matching backend.  In the EXEC_BACKEND case, we can no
02027      * longer access the postmaster's own backend list, and must rely on the
02028      * duplicate array in shared memory.
02029      */
02030 #ifndef EXEC_BACKEND
02031     dlist_foreach(iter, &BackendList)
02032     {
02033         bp = dlist_container(Backend, elem, iter.cur);
02034 #else
02035     for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
02036     {
02037         bp = (Backend *) &ShmemBackendArray[i];
02038 #endif
02039         if (bp->pid == backendPID)
02040         {
02041             if (bp->cancel_key == cancelAuthCode)
02042             {
02043                 /* Found a match; signal that backend to cancel current op */
02044                 ereport(DEBUG2,
02045                         (errmsg_internal("processing cancel request: sending SIGINT to process %d",
02046                                          backendPID)));
02047                 signal_child(bp->pid, SIGINT);
02048             }
02049             else
02050                 /* Right PID, wrong key: no way, Jose */
02051                 ereport(LOG,
02052                         (errmsg("wrong key in cancel request for process %d",
02053                                 backendPID)));
02054             return;
02055         }
02056     }
02057 
02058     /* No matching backend */
02059     ereport(LOG,
02060             (errmsg("PID %d in cancel request did not match any process",
02061                     backendPID)));
02062 }
02063 
02064 /*
02065  * canAcceptConnections --- check to see if database state allows connections.
02066  */
02067 static CAC_state
02068 canAcceptConnections(void)
02069 {
02070     CAC_state   result = CAC_OK;
02071 
02072     /*
02073      * Can't start backends when in startup/shutdown/inconsistent recovery
02074      * state.
02075      *
02076      * In state PM_WAIT_BACKUP only superusers can connect (this must be
02077      * allowed so that a superuser can end online backup mode); we return
02078      * CAC_WAITBACKUP code to indicate that this must be checked later. Note
02079      * that neither CAC_OK nor CAC_WAITBACKUP can safely be returned until we
02080      * have checked for too many children.
02081      */
02082     if (pmState != PM_RUN)
02083     {
02084         if (pmState == PM_WAIT_BACKUP)
02085             result = CAC_WAITBACKUP;    /* allow superusers only */
02086         else if (Shutdown > NoShutdown)
02087             return CAC_SHUTDOWN;    /* shutdown is pending */
02088         else if (!FatalError &&
02089                  (pmState == PM_STARTUP ||
02090                   pmState == PM_RECOVERY))
02091             return CAC_STARTUP; /* normal startup */
02092         else if (!FatalError &&
02093                  pmState == PM_HOT_STANDBY)
02094             result = CAC_OK;    /* connection OK during hot standby */
02095         else
02096             return CAC_RECOVERY;    /* else must be crash recovery */
02097     }
02098 
02099     /*
02100      * Don't start too many children.
02101      *
02102      * We allow more connections than we can have backends here because some
02103      * might still be authenticating; they might fail auth, or some existing
02104      * backend might exit before the auth cycle is completed. The exact
02105      * MaxBackends limit is enforced when a new backend tries to join the
02106      * shared-inval backend array.
02107      *
02108      * The limit here must match the sizes of the per-child-process arrays;
02109      * see comments for MaxLivePostmasterChildren().
02110      */
02111     if (CountChildren(BACKEND_TYPE_ALL) >= MaxLivePostmasterChildren())
02112         result = CAC_TOOMANY;
02113 
02114     return result;
02115 }
02116 
02117 
02118 /*
02119  * ConnCreate -- create a local connection data structure
02120  *
02121  * Returns NULL on failure, other than out-of-memory which is fatal.
02122  */
02123 static Port *
02124 ConnCreate(int serverFd)
02125 {
02126     Port       *port;
02127 
02128     if (!(port = (Port *) calloc(1, sizeof(Port))))
02129     {
02130         ereport(LOG,
02131                 (errcode(ERRCODE_OUT_OF_MEMORY),
02132                  errmsg("out of memory")));
02133         ExitPostmaster(1);
02134     }
02135 
02136     if (StreamConnection(serverFd, port) != STATUS_OK)
02137     {
02138         if (port->sock >= 0)
02139             StreamClose(port->sock);
02140         ConnFree(port);
02141         return NULL;
02142     }
02143 
02144     /*
02145      * Precompute password salt values to use for this connection. It's
02146      * slightly annoying to do this long in advance of knowing whether we'll
02147      * need 'em or not, but we must do the random() calls before we fork, not
02148      * after.  Else the postmaster's random sequence won't get advanced, and
02149      * all backends would end up using the same salt...
02150      */
02151     RandomSalt(port->md5Salt);
02152 
02153     /*
02154      * Allocate GSSAPI specific state struct
02155      */
02156 #ifndef EXEC_BACKEND
02157 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
02158     port->gss = (pg_gssinfo *) calloc(1, sizeof(pg_gssinfo));
02159     if (!port->gss)
02160     {
02161         ereport(LOG,
02162                 (errcode(ERRCODE_OUT_OF_MEMORY),
02163                  errmsg("out of memory")));
02164         ExitPostmaster(1);
02165     }
02166 #endif
02167 #endif
02168 
02169     return port;
02170 }
02171 
02172 
02173 /*
02174  * ConnFree -- free a local connection data structure
02175  */
02176 static void
02177 ConnFree(Port *conn)
02178 {
02179 #ifdef USE_SSL
02180     secure_close(conn);
02181 #endif
02182     if (conn->gss)
02183         free(conn->gss);
02184     free(conn);
02185 }
02186 
02187 
02188 /*
02189  * ClosePostmasterPorts -- close all the postmaster's open sockets
02190  *
02191  * This is called during child process startup to release file descriptors
02192  * that are not needed by that child process.  The postmaster still has
02193  * them open, of course.
02194  *
02195  * Note: we pass am_syslogger as a boolean because we don't want to set
02196  * the global variable yet when this is called.
02197  */
02198 void
02199 ClosePostmasterPorts(bool am_syslogger)
02200 {
02201     int         i;
02202 
02203 #ifndef WIN32
02204 
02205     /*
02206      * Close the write end of postmaster death watch pipe. It's important to
02207      * do this as early as possible, so that if postmaster dies, others won't
02208      * think that it's still running because we're holding the pipe open.
02209      */
02210     if (close(postmaster_alive_fds[POSTMASTER_FD_OWN]))
02211         ereport(FATAL,
02212                 (errcode_for_file_access(),
02213                  errmsg_internal("could not close postmaster death monitoring pipe in child process: %m")));
02214     postmaster_alive_fds[POSTMASTER_FD_OWN] = -1;
02215 #endif
02216 
02217     /* Close the listen sockets */
02218     for (i = 0; i < MAXLISTEN; i++)
02219     {
02220         if (ListenSocket[i] != PGINVALID_SOCKET)
02221         {
02222             StreamClose(ListenSocket[i]);
02223             ListenSocket[i] = PGINVALID_SOCKET;
02224         }
02225     }
02226 
02227     /* If using syslogger, close the read side of the pipe */
02228     if (!am_syslogger)
02229     {
02230 #ifndef WIN32
02231         if (syslogPipe[0] >= 0)
02232             close(syslogPipe[0]);
02233         syslogPipe[0] = -1;
02234 #else
02235         if (syslogPipe[0])
02236             CloseHandle(syslogPipe[0]);
02237         syslogPipe[0] = 0;
02238 #endif
02239     }
02240 
02241 #ifdef USE_BONJOUR
02242     /* If using Bonjour, close the connection to the mDNS daemon */
02243     if (bonjour_sdref)
02244         close(DNSServiceRefSockFD(bonjour_sdref));
02245 #endif
02246 }
02247 
02248 
02249 /*
02250  * reset_shared -- reset shared memory and semaphores
02251  */
02252 static void
02253 reset_shared(int port)
02254 {
02255     /*
02256      * Create or re-create shared memory and semaphores.
02257      *
02258      * Note: in each "cycle of life" we will normally assign the same IPC keys
02259      * (if using SysV shmem and/or semas), since the port number is used to
02260      * determine IPC keys.  This helps ensure that we will clean up dead IPC
02261      * objects if the postmaster crashes and is restarted.
02262      */
02263     CreateSharedMemoryAndSemaphores(false, port);
02264 }
02265 
02266 
02267 /*
02268  * SIGHUP -- reread config files, and tell children to do same
02269  */
02270 static void
02271 SIGHUP_handler(SIGNAL_ARGS)
02272 {
02273     int         save_errno = errno;
02274 
02275     PG_SETMASK(&BlockSig);
02276 
02277     if (Shutdown <= SmartShutdown)
02278     {
02279         ereport(LOG,
02280                 (errmsg("received SIGHUP, reloading configuration files")));
02281         ProcessConfigFile(PGC_SIGHUP);
02282         SignalChildren(SIGHUP);
02283         SignalUnconnectedWorkers(SIGHUP);
02284         if (StartupPID != 0)
02285             signal_child(StartupPID, SIGHUP);
02286         if (BgWriterPID != 0)
02287             signal_child(BgWriterPID, SIGHUP);
02288         if (CheckpointerPID != 0)
02289             signal_child(CheckpointerPID, SIGHUP);
02290         if (WalWriterPID != 0)
02291             signal_child(WalWriterPID, SIGHUP);
02292         if (WalReceiverPID != 0)
02293             signal_child(WalReceiverPID, SIGHUP);
02294         if (AutoVacPID != 0)
02295             signal_child(AutoVacPID, SIGHUP);
02296         if (PgArchPID != 0)
02297             signal_child(PgArchPID, SIGHUP);
02298         if (SysLoggerPID != 0)
02299             signal_child(SysLoggerPID, SIGHUP);
02300         if (PgStatPID != 0)
02301             signal_child(PgStatPID, SIGHUP);
02302 
02303         /* Reload authentication config files too */
02304         if (!load_hba())
02305             ereport(WARNING,
02306                     (errmsg("pg_hba.conf not reloaded")));
02307 
02308         if (!load_ident())
02309             ereport(WARNING,
02310                     (errmsg("pg_ident.conf not reloaded")));
02311 
02312 #ifdef EXEC_BACKEND
02313         /* Update the starting-point file for future children */
02314         write_nondefault_variables(PGC_SIGHUP);
02315 #endif
02316     }
02317 
02318     PG_SETMASK(&UnBlockSig);
02319 
02320     errno = save_errno;
02321 }
02322 
02323 
02324 /*
02325  * pmdie -- signal handler for processing various postmaster signals.
02326  */
02327 static void
02328 pmdie(SIGNAL_ARGS)
02329 {
02330     int         save_errno = errno;
02331 
02332     PG_SETMASK(&BlockSig);
02333 
02334     ereport(DEBUG2,
02335             (errmsg_internal("postmaster received signal %d",
02336                              postgres_signal_arg)));
02337 
02338     switch (postgres_signal_arg)
02339     {
02340         case SIGTERM:
02341 
02342             /*
02343              * Smart Shutdown:
02344              *
02345              * Wait for children to end their work, then shut down.
02346              */
02347             if (Shutdown >= SmartShutdown)
02348                 break;
02349             Shutdown = SmartShutdown;
02350             ereport(LOG,
02351                     (errmsg("received smart shutdown request")));
02352 
02353             if (pmState == PM_RUN || pmState == PM_RECOVERY ||
02354                 pmState == PM_HOT_STANDBY || pmState == PM_STARTUP)
02355             {
02356                 /* autovac workers are told to shut down immediately */
02357                 /* and bgworkers too; does this need tweaking? */
02358                 SignalSomeChildren(SIGTERM,
02359                                BACKEND_TYPE_AUTOVAC | BACKEND_TYPE_BGWORKER);
02360                 SignalUnconnectedWorkers(SIGTERM);
02361                 /* and the autovac launcher too */
02362                 if (AutoVacPID != 0)
02363                     signal_child(AutoVacPID, SIGTERM);
02364                 /* and the bgwriter too */
02365                 if (BgWriterPID != 0)
02366                     signal_child(BgWriterPID, SIGTERM);
02367                 /* and the walwriter too */
02368                 if (WalWriterPID != 0)
02369                     signal_child(WalWriterPID, SIGTERM);
02370 
02371                 /*
02372                  * If we're in recovery, we can't kill the startup process
02373                  * right away, because at present doing so does not release
02374                  * its locks.  We might want to change this in a future
02375                  * release.  For the time being, the PM_WAIT_READONLY state
02376                  * indicates that we're waiting for the regular (read only)
02377                  * backends to die off; once they do, we'll kill the startup
02378                  * and walreceiver processes.
02379                  */
02380                 pmState = (pmState == PM_RUN) ?
02381                     PM_WAIT_BACKUP : PM_WAIT_READONLY;
02382             }
02383 
02384             /*
02385              * Now wait for online backup mode to end and backends to exit. If
02386              * that is already the case, PostmasterStateMachine will take the
02387              * next step.
02388              */
02389             PostmasterStateMachine();
02390             break;
02391 
02392         case SIGINT:
02393 
02394             /*
02395              * Fast Shutdown:
02396              *
02397              * Abort all children with SIGTERM (rollback active transactions
02398              * and exit) and shut down when they are gone.
02399              */
02400             if (Shutdown >= FastShutdown)
02401                 break;
02402             Shutdown = FastShutdown;
02403             ereport(LOG,
02404                     (errmsg("received fast shutdown request")));
02405 
02406             if (StartupPID != 0)
02407                 signal_child(StartupPID, SIGTERM);
02408             if (BgWriterPID != 0)
02409                 signal_child(BgWriterPID, SIGTERM);
02410             if (WalReceiverPID != 0)
02411                 signal_child(WalReceiverPID, SIGTERM);
02412             SignalUnconnectedWorkers(SIGTERM);
02413             if (pmState == PM_RECOVERY)
02414             {
02415                 /*
02416                  * Only startup, bgwriter, walreceiver, unconnected bgworkers,
02417                  * and/or checkpointer should be active in this state; we just
02418                  * signaled the first four, and we don't want to kill
02419                  * checkpointer yet.
02420                  */
02421                 pmState = PM_WAIT_BACKENDS;
02422             }
02423             else if (pmState == PM_RUN ||
02424                      pmState == PM_WAIT_BACKUP ||
02425                      pmState == PM_WAIT_READONLY ||
02426                      pmState == PM_WAIT_BACKENDS ||
02427                      pmState == PM_HOT_STANDBY)
02428             {
02429                 ereport(LOG,
02430                         (errmsg("aborting any active transactions")));
02431                 /* shut down all backends and workers */
02432                 SignalSomeChildren(SIGTERM,
02433                                  BACKEND_TYPE_NORMAL | BACKEND_TYPE_AUTOVAC |
02434                                    BACKEND_TYPE_BGWORKER);
02435                 /* and the autovac launcher too */
02436                 if (AutoVacPID != 0)
02437                     signal_child(AutoVacPID, SIGTERM);
02438                 /* and the walwriter too */
02439                 if (WalWriterPID != 0)
02440                     signal_child(WalWriterPID, SIGTERM);
02441                 pmState = PM_WAIT_BACKENDS;
02442             }
02443 
02444             /*
02445              * Now wait for backends to exit.  If there are none,
02446              * PostmasterStateMachine will take the next step.
02447              */
02448             PostmasterStateMachine();
02449             break;
02450 
02451         case SIGQUIT:
02452 
02453             /*
02454              * Immediate Shutdown:
02455              *
02456              * abort all children with SIGQUIT and exit without attempt to
02457              * properly shut down data base system.
02458              */
02459             ereport(LOG,
02460                     (errmsg("received immediate shutdown request")));
02461             SignalChildren(SIGQUIT);
02462             if (StartupPID != 0)
02463                 signal_child(StartupPID, SIGQUIT);
02464             if (BgWriterPID != 0)
02465                 signal_child(BgWriterPID, SIGQUIT);
02466             if (CheckpointerPID != 0)
02467                 signal_child(CheckpointerPID, SIGQUIT);
02468             if (WalWriterPID != 0)
02469                 signal_child(WalWriterPID, SIGQUIT);
02470             if (WalReceiverPID != 0)
02471                 signal_child(WalReceiverPID, SIGQUIT);
02472             if (AutoVacPID != 0)
02473                 signal_child(AutoVacPID, SIGQUIT);
02474             if (PgArchPID != 0)
02475                 signal_child(PgArchPID, SIGQUIT);
02476             if (PgStatPID != 0)
02477                 signal_child(PgStatPID, SIGQUIT);
02478             SignalUnconnectedWorkers(SIGQUIT);
02479             ExitPostmaster(0);
02480             break;
02481     }
02482 
02483     PG_SETMASK(&UnBlockSig);
02484 
02485     errno = save_errno;
02486 }
02487 
02488 /*
02489  * Reaper -- signal handler to cleanup after a child process dies.
02490  */
02491 static void
02492 reaper(SIGNAL_ARGS)
02493 {
02494     int         save_errno = errno;
02495     int         pid;            /* process id of dead child process */
02496     int         exitstatus;     /* its exit status */
02497 
02498     PG_SETMASK(&BlockSig);
02499 
02500     ereport(DEBUG4,
02501             (errmsg_internal("reaping dead processes")));
02502 
02503     while ((pid = waitpid(-1, &exitstatus, WNOHANG)) > 0)
02504     {
02505         /*
02506          * Check if this child was a startup process.
02507          */
02508         if (pid == StartupPID)
02509         {
02510             StartupPID = 0;
02511 
02512             /*
02513              * Startup process exited in response to a shutdown request (or it
02514              * completed normally regardless of the shutdown request).
02515              */
02516             if (Shutdown > NoShutdown &&
02517                 (EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
02518             {
02519                 pmState = PM_WAIT_BACKENDS;
02520                 /* PostmasterStateMachine logic does the rest */
02521                 continue;
02522             }
02523 
02524             /*
02525              * Unexpected exit of startup process (including FATAL exit)
02526              * during PM_STARTUP is treated as catastrophic. There are no
02527              * other processes running yet, so we can just exit.
02528              */
02529             if (pmState == PM_STARTUP && !EXIT_STATUS_0(exitstatus))
02530             {
02531                 LogChildExit(LOG, _("startup process"),
02532                              pid, exitstatus);
02533                 ereport(LOG,
02534                 (errmsg("aborting startup due to startup process failure")));
02535                 ExitPostmaster(1);
02536             }
02537 
02538             /*
02539              * After PM_STARTUP, any unexpected exit (including FATAL exit) of
02540              * the startup process is catastrophic, so kill other children,
02541              * and set RecoveryError so we don't try to reinitialize after
02542              * they're gone.  Exception: if FatalError is already set, that
02543              * implies we previously sent the startup process a SIGQUIT, so
02544              * that's probably the reason it died, and we do want to try to
02545              * restart in that case.
02546              */
02547             if (!EXIT_STATUS_0(exitstatus))
02548             {
02549                 if (!FatalError)
02550                     RecoveryError = true;
02551                 HandleChildCrash(pid, exitstatus,
02552                                  _("startup process"));
02553                 continue;
02554             }
02555 
02556             /*
02557              * Startup succeeded, commence normal operations
02558              */
02559             FatalError = false;
02560             ReachedNormalRunning = true;
02561             pmState = PM_RUN;
02562 
02563             /*
02564              * Crank up the background tasks, if we didn't do that already
02565              * when we entered consistent recovery state.  It doesn't matter
02566              * if this fails, we'll just try again later.
02567              */
02568             if (CheckpointerPID == 0)
02569                 CheckpointerPID = StartCheckpointer();
02570             if (BgWriterPID == 0)
02571                 BgWriterPID = StartBackgroundWriter();
02572             if (WalWriterPID == 0)
02573                 WalWriterPID = StartWalWriter();
02574 
02575             /*
02576              * Likewise, start other special children as needed.  In a restart
02577              * situation, some of them may be alive already.
02578              */
02579             if (!IsBinaryUpgrade && AutoVacuumingActive() && AutoVacPID == 0)
02580                 AutoVacPID = StartAutoVacLauncher();
02581             if (XLogArchivingActive() && PgArchPID == 0)
02582                 PgArchPID = pgarch_start();
02583             if (PgStatPID == 0)
02584                 PgStatPID = pgstat_start();
02585 
02586             /* some workers may be scheduled to start now */
02587             StartOneBackgroundWorker();
02588 
02589             /* at this point we are really open for business */
02590             ereport(LOG,
02591                  (errmsg("database system is ready to accept connections")));
02592 
02593             continue;
02594         }
02595 
02596         /*
02597          * Was it the bgwriter?  Normal exit can be ignored; we'll start a new
02598          * one at the next iteration of the postmaster's main loop, if
02599          * necessary.  Any other exit condition is treated as a crash.
02600          */
02601         if (pid == BgWriterPID)
02602         {
02603             BgWriterPID = 0;
02604             if (!EXIT_STATUS_0(exitstatus))
02605                 HandleChildCrash(pid, exitstatus,
02606                                  _("background writer process"));
02607             continue;
02608         }
02609 
02610         /*
02611          * Was it the checkpointer?
02612          */
02613         if (pid == CheckpointerPID)
02614         {
02615             CheckpointerPID = 0;
02616             if (EXIT_STATUS_0(exitstatus) && pmState == PM_SHUTDOWN)
02617             {
02618                 /*
02619                  * OK, we saw normal exit of the checkpointer after it's been
02620                  * told to shut down.  We expect that it wrote a shutdown
02621                  * checkpoint.  (If for some reason it didn't, recovery will
02622                  * occur on next postmaster start.)
02623                  *
02624                  * At this point we should have no normal backend children
02625                  * left (else we'd not be in PM_SHUTDOWN state) but we might
02626                  * have dead_end children to wait for.
02627                  *
02628                  * If we have an archiver subprocess, tell it to do a last
02629                  * archive cycle and quit. Likewise, if we have walsender
02630                  * processes, tell them to send any remaining WAL and quit.
02631                  */
02632                 Assert(Shutdown > NoShutdown);
02633 
02634                 /* Waken archiver for the last time */
02635                 if (PgArchPID != 0)
02636                     signal_child(PgArchPID, SIGUSR2);
02637 
02638                 /*
02639                  * Waken walsenders for the last time. No regular backends
02640                  * should be around anymore.
02641                  */
02642                 SignalChildren(SIGUSR2);
02643 
02644                 pmState = PM_SHUTDOWN_2;
02645 
02646                 /*
02647                  * We can also shut down the stats collector now; there's
02648                  * nothing left for it to do.
02649                  */
02650                 if (PgStatPID != 0)
02651                     signal_child(PgStatPID, SIGQUIT);
02652             }
02653             else
02654             {
02655                 /*
02656                  * Any unexpected exit of the checkpointer (including FATAL
02657                  * exit) is treated as a crash.
02658                  */
02659                 HandleChildCrash(pid, exitstatus,
02660                                  _("checkpointer process"));
02661             }
02662 
02663             continue;
02664         }
02665 
02666         /*
02667          * Was it the wal writer?  Normal exit can be ignored; we'll start a
02668          * new one at the next iteration of the postmaster's main loop, if
02669          * necessary.  Any other exit condition is treated as a crash.
02670          */
02671         if (pid == WalWriterPID)
02672         {
02673             WalWriterPID = 0;
02674             if (!EXIT_STATUS_0(exitstatus))
02675                 HandleChildCrash(pid, exitstatus,
02676                                  _("WAL writer process"));
02677             continue;
02678         }
02679 
02680         /*
02681          * Was it the wal receiver?  If exit status is zero (normal) or one
02682          * (FATAL exit), we assume everything is all right just like normal
02683          * backends.
02684          */
02685         if (pid == WalReceiverPID)
02686         {
02687             WalReceiverPID = 0;
02688             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
02689                 HandleChildCrash(pid, exitstatus,
02690                                  _("WAL receiver process"));
02691             continue;
02692         }
02693 
02694         /*
02695          * Was it the autovacuum launcher?  Normal exit can be ignored; we'll
02696          * start a new one at the next iteration of the postmaster's main
02697          * loop, if necessary.  Any other exit condition is treated as a
02698          * crash.
02699          */
02700         if (pid == AutoVacPID)
02701         {
02702             AutoVacPID = 0;
02703             if (!EXIT_STATUS_0(exitstatus))
02704                 HandleChildCrash(pid, exitstatus,
02705                                  _("autovacuum launcher process"));
02706             continue;
02707         }
02708 
02709         /*
02710          * Was it the archiver?  If so, just try to start a new one; no need
02711          * to force reset of the rest of the system.  (If fail, we'll try
02712          * again in future cycles of the main loop.).  Unless we were waiting
02713          * for it to shut down; don't restart it in that case, and
02714          * PostmasterStateMachine() will advance to the next shutdown step.
02715          */
02716         if (pid == PgArchPID)
02717         {
02718             PgArchPID = 0;
02719             if (!EXIT_STATUS_0(exitstatus))
02720                 LogChildExit(LOG, _("archiver process"),
02721                              pid, exitstatus);
02722             if (XLogArchivingActive() && pmState == PM_RUN)
02723                 PgArchPID = pgarch_start();
02724             continue;
02725         }
02726 
02727         /*
02728          * Was it the statistics collector?  If so, just try to start a new
02729          * one; no need to force reset of the rest of the system.  (If fail,
02730          * we'll try again in future cycles of the main loop.)
02731          */
02732         if (pid == PgStatPID)
02733         {
02734             PgStatPID = 0;
02735             if (!EXIT_STATUS_0(exitstatus))
02736                 LogChildExit(LOG, _("statistics collector process"),
02737                              pid, exitstatus);
02738             if (pmState == PM_RUN)
02739                 PgStatPID = pgstat_start();
02740             continue;
02741         }
02742 
02743         /* Was it the system logger?  If so, try to start a new one */
02744         if (pid == SysLoggerPID)
02745         {
02746             SysLoggerPID = 0;
02747             /* for safety's sake, launch new logger *first* */
02748             SysLoggerPID = SysLogger_Start();
02749             if (!EXIT_STATUS_0(exitstatus))
02750                 LogChildExit(LOG, _("system logger process"),
02751                              pid, exitstatus);
02752             continue;
02753         }
02754 
02755         /* Was it one of our background workers? */
02756         if (CleanupBackgroundWorker(pid, exitstatus))
02757         {
02758             /* have it be restarted */
02759             HaveCrashedWorker = true;
02760             continue;
02761         }
02762 
02763         /*
02764          * Else do standard backend child cleanup.
02765          */
02766         CleanupBackend(pid, exitstatus);
02767     }                           /* loop over pending child-death reports */
02768 
02769     /*
02770      * After cleaning out the SIGCHLD queue, see if we have any state changes
02771      * or actions to make.
02772      */
02773     PostmasterStateMachine();
02774 
02775     /* Done with signal handler */
02776     PG_SETMASK(&UnBlockSig);
02777 
02778     errno = save_errno;
02779 }
02780 
02781 /*
02782  * Scan the bgworkers list and see if the given PID (which has just stopped
02783  * or crashed) is in it.  Handle its shutdown if so, and return true.  If not a
02784  * bgworker, return false.
02785  *
02786  * This is heavily based on CleanupBackend.  One important difference is that
02787  * we don't know yet that the dying process is a bgworker, so we must be silent
02788  * until we're sure it is.
02789  */
02790 static bool
02791 CleanupBackgroundWorker(int pid,
02792                         int exitstatus) /* child's exit status */
02793 {
02794     char        namebuf[MAXPGPATH];
02795     slist_iter  iter;
02796 
02797     slist_foreach(iter, &BackgroundWorkerList)
02798     {
02799         RegisteredBgWorker *rw;
02800 
02801         rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
02802 
02803         if (rw->rw_pid != pid)
02804             continue;
02805 
02806 #ifdef WIN32
02807         /* see CleanupBackend */
02808         if (exitstatus == ERROR_WAIT_NO_CHILDREN)
02809             exitstatus = 0;
02810 #endif
02811 
02812         snprintf(namebuf, MAXPGPATH, "%s: %s", _("worker process"),
02813                  rw->rw_worker.bgw_name);
02814 
02815         /* Delay restarting any bgworker that exits with a nonzero status. */
02816         if (!EXIT_STATUS_0(exitstatus))
02817             rw->rw_crashed_at = GetCurrentTimestamp();
02818         else
02819             rw->rw_crashed_at = 0;
02820 
02821         /*
02822          * Additionally, for shared-memory-connected workers, just like a
02823          * backend, any exit status other than 0 or 1 is considered a crash
02824          * and causes a system-wide restart.
02825          */
02826         if (rw->rw_worker.bgw_flags & BGWORKER_SHMEM_ACCESS)
02827         {
02828             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
02829             {
02830                 rw->rw_crashed_at = GetCurrentTimestamp();
02831                 HandleChildCrash(pid, exitstatus, namebuf);
02832                 return true;
02833             }
02834         }
02835 
02836         if (!ReleasePostmasterChildSlot(rw->rw_child_slot))
02837         {
02838             /*
02839              * Uh-oh, the child failed to clean itself up.  Treat as a crash
02840              * after all.
02841              */
02842             rw->rw_crashed_at = GetCurrentTimestamp();
02843             HandleChildCrash(pid, exitstatus, namebuf);
02844             return true;
02845         }
02846 
02847         /* Get it out of the BackendList and clear out remaining data */
02848         if (rw->rw_backend)
02849         {
02850             Assert(rw->rw_worker.bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION);
02851             dlist_delete(&rw->rw_backend->elem);
02852 #ifdef EXEC_BACKEND
02853             ShmemBackendArrayRemove(rw->rw_backend);
02854 #endif
02855             free(rw->rw_backend);
02856             rw->rw_backend = NULL;
02857         }
02858         rw->rw_pid = 0;
02859         rw->rw_child_slot = 0;
02860 
02861         LogChildExit(LOG, namebuf, pid, exitstatus);
02862 
02863         return true;
02864     }
02865 
02866     return false;
02867 }
02868 
02869 /*
02870  * CleanupBackend -- cleanup after terminated backend.
02871  *
02872  * Remove all local state associated with backend.
02873  *
02874  * If you change this, see also CleanupBackgroundWorker.
02875  */
02876 static void
02877 CleanupBackend(int pid,
02878                int exitstatus)  /* child's exit status. */
02879 {
02880     dlist_mutable_iter iter;
02881 
02882     LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
02883 
02884     /*
02885      * If a backend dies in an ugly way then we must signal all other backends
02886      * to quickdie.  If exit status is zero (normal) or one (FATAL exit), we
02887      * assume everything is all right and proceed to remove the backend from
02888      * the active backend list.
02889      */
02890 #ifdef WIN32
02891 
02892     /*
02893      * On win32, also treat ERROR_WAIT_NO_CHILDREN (128) as nonfatal case,
02894      * since that sometimes happens under load when the process fails to start
02895      * properly (long before it starts using shared memory). Microsoft reports
02896      * it is related to mutex failure:
02897      * http://archives.postgresql.org/pgsql-hackers/2010-09/msg00790.php
02898      */
02899     if (exitstatus == ERROR_WAIT_NO_CHILDREN)
02900     {
02901         LogChildExit(LOG, _("server process"), pid, exitstatus);
02902         exitstatus = 0;
02903     }
02904 #endif
02905 
02906     if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
02907     {
02908         HandleChildCrash(pid, exitstatus, _("server process"));
02909         return;
02910     }
02911 
02912     dlist_foreach_modify(iter, &BackendList)
02913     {
02914         Backend    *bp = dlist_container(Backend, elem, iter.cur);
02915 
02916         if (bp->pid == pid)
02917         {
02918             if (!bp->dead_end)
02919             {
02920                 if (!ReleasePostmasterChildSlot(bp->child_slot))
02921                 {
02922                     /*
02923                      * Uh-oh, the child failed to clean itself up.  Treat as a
02924                      * crash after all.
02925                      */
02926                     HandleChildCrash(pid, exitstatus, _("server process"));
02927                     return;
02928                 }
02929 #ifdef EXEC_BACKEND
02930                 ShmemBackendArrayRemove(bp);
02931 #endif
02932             }
02933             dlist_delete(iter.cur);
02934             free(bp);
02935             break;
02936         }
02937     }
02938 }
02939 
02940 /*
02941  * HandleChildCrash -- cleanup after failed backend, bgwriter, checkpointer,
02942  * walwriter, autovacuum, or background worker.
02943  *
02944  * The objectives here are to clean up our local state about the child
02945  * process, and to signal all other remaining children to quickdie.
02946  */
02947 static void
02948 HandleChildCrash(int pid, int exitstatus, const char *procname)
02949 {
02950     dlist_mutable_iter iter;
02951     slist_iter  siter;
02952     Backend    *bp;
02953 
02954     /*
02955      * Make log entry unless there was a previous crash (if so, nonzero exit
02956      * status is to be expected in SIGQUIT response; don't clutter log)
02957      */
02958     if (!FatalError)
02959     {
02960         LogChildExit(LOG, procname, pid, exitstatus);
02961         ereport(LOG,
02962                 (errmsg("terminating any other active server processes")));
02963     }
02964 
02965     /* Process background workers. */
02966     slist_foreach(siter, &BackgroundWorkerList)
02967     {
02968         RegisteredBgWorker *rw;
02969 
02970         rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
02971         if (rw->rw_pid == 0)
02972             continue;       /* not running */
02973         if (rw->rw_pid == pid)
02974         {
02975             /*
02976              * Found entry for freshly-dead worker, so remove it.
02977              */
02978             (void) ReleasePostmasterChildSlot(rw->rw_child_slot);
02979             if (rw->rw_backend)
02980             {
02981                 dlist_delete(&rw->rw_backend->elem);
02982 #ifdef EXEC_BACKEND
02983                 ShmemBackendArrayRemove(rw->rw_backend);
02984 #endif
02985                 free(rw->rw_backend);
02986                 rw->rw_backend = NULL;
02987             }
02988             rw->rw_pid = 0;
02989             rw->rw_child_slot = 0;
02990             /* don't reset crashed_at */
02991             /* Keep looping so we can signal remaining workers */
02992         }
02993         else
02994         {
02995             /*
02996              * This worker is still alive.  Unless we did so already, tell it
02997              * to commit hara-kiri.
02998              *
02999              * SIGQUIT is the special signal that says exit without proc_exit
03000              * and let the user know what's going on. But if SendStop is set
03001              * (-s on command line), then we send SIGSTOP instead, so that we
03002              * can get core dumps from all backends by hand.
03003              */
03004             if (!FatalError)
03005             {
03006                 ereport(DEBUG2,
03007                         (errmsg_internal("sending %s to process %d",
03008                                          (SendStop ? "SIGSTOP" : "SIGQUIT"),
03009                                          (int) rw->rw_pid)));
03010                 signal_child(rw->rw_pid, (SendStop ? SIGSTOP : SIGQUIT));
03011             }
03012         }
03013     }
03014 
03015     /* Process regular backends */
03016     dlist_foreach_modify(iter, &BackendList)
03017     {
03018         bp = dlist_container(Backend, elem, iter.cur);
03019 
03020         if (bp->pid == pid)
03021         {
03022             /*
03023              * Found entry for freshly-dead backend, so remove it.
03024              */
03025             if (!bp->dead_end)
03026             {
03027                 (void) ReleasePostmasterChildSlot(bp->child_slot);
03028 #ifdef EXEC_BACKEND
03029                 ShmemBackendArrayRemove(bp);
03030 #endif
03031             }
03032             dlist_delete(iter.cur);
03033             free(bp);
03034             /* Keep looping so we can signal remaining backends */
03035         }
03036         else
03037         {
03038             /*
03039              * This backend is still alive.  Unless we did so already, tell it
03040              * to commit hara-kiri.
03041              *
03042              * SIGQUIT is the special signal that says exit without proc_exit
03043              * and let the user know what's going on. But if SendStop is set
03044              * (-s on command line), then we send SIGSTOP instead, so that we
03045              * can get core dumps from all backends by hand.
03046              *
03047              * We could exclude dead_end children here, but at least in the
03048              * SIGSTOP case it seems better to include them.
03049              *
03050              * Background workers were already processed above; ignore them
03051              * here.
03052              */
03053             if (bp->bkend_type == BACKEND_TYPE_BGWORKER)
03054                 continue;
03055 
03056             if (!FatalError)
03057             {
03058                 ereport(DEBUG2,
03059                         (errmsg_internal("sending %s to process %d",
03060                                          (SendStop ? "SIGSTOP" : "SIGQUIT"),
03061                                          (int) bp->pid)));
03062                 signal_child(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
03063             }
03064         }
03065     }
03066 
03067     /* Take care of the startup process too */
03068     if (pid == StartupPID)
03069         StartupPID = 0;
03070     else if (StartupPID != 0 && !FatalError)
03071     {
03072         ereport(DEBUG2,
03073                 (errmsg_internal("sending %s to process %d",
03074                                  (SendStop ? "SIGSTOP" : "SIGQUIT"),
03075                                  (int) StartupPID)));
03076         signal_child(StartupPID, (SendStop ? SIGSTOP : SIGQUIT));
03077     }
03078 
03079     /* Take care of the bgwriter too */
03080     if (pid == BgWriterPID)
03081         BgWriterPID = 0;
03082     else if (BgWriterPID != 0 && !FatalError)
03083     {
03084         ereport(DEBUG2,
03085                 (errmsg_internal("sending %s to process %d",
03086                                  (SendStop ? "SIGSTOP" : "SIGQUIT"),
03087                                  (int) BgWriterPID)));
03088         signal_child(BgWriterPID, (SendStop ? SIGSTOP : SIGQUIT));
03089     }
03090 
03091     /* Take care of the checkpointer too */
03092     if (pid == CheckpointerPID)
03093         CheckpointerPID = 0;
03094     else if (CheckpointerPID != 0 && !FatalError)
03095     {
03096         ereport(DEBUG2,
03097                 (errmsg_internal("sending %s to process %d",
03098                                  (SendStop ? "SIGSTOP" : "SIGQUIT"),
03099                                  (int) CheckpointerPID)));
03100         signal_child(CheckpointerPID, (SendStop ? SIGSTOP : SIGQUIT));
03101     }
03102 
03103     /* Take care of the walwriter too */
03104     if (pid == WalWriterPID)
03105         WalWriterPID = 0;
03106     else if (WalWriterPID != 0 && !FatalError)
03107     {
03108         ereport(DEBUG2,
03109                 (errmsg_internal("sending %s to process %d",
03110                                  (SendStop ? "SIGSTOP" : "SIGQUIT"),
03111                                  (int) WalWriterPID)));
03112         signal_child(WalWriterPID, (SendStop ? SIGSTOP : SIGQUIT));
03113     }
03114 
03115     /* Take care of the walreceiver too */
03116     if (pid == WalReceiverPID)
03117         WalReceiverPID = 0;
03118     else if (WalReceiverPID != 0 && !FatalError)
03119     {
03120         ereport(DEBUG2,
03121                 (errmsg_internal("sending %s to process %d",
03122                                  (SendStop ? "SIGSTOP" : "SIGQUIT"),
03123                                  (int) WalReceiverPID)));
03124         signal_child(WalReceiverPID, (SendStop ? SIGSTOP : SIGQUIT));
03125     }
03126 
03127     /* Take care of the autovacuum launcher too */
03128     if (pid == AutoVacPID)
03129         AutoVacPID = 0;
03130     else if (AutoVacPID != 0 && !FatalError)
03131     {
03132         ereport(DEBUG2,
03133                 (errmsg_internal("sending %s to process %d",
03134                                  (SendStop ? "SIGSTOP" : "SIGQUIT"),
03135                                  (int) AutoVacPID)));
03136         signal_child(AutoVacPID, (SendStop ? SIGSTOP : SIGQUIT));
03137     }
03138 
03139     /*
03140      * Force a power-cycle of the pgarch process too.  (This isn't absolutely
03141      * necessary, but it seems like a good idea for robustness, and it
03142      * simplifies the state-machine logic in the case where a shutdown request
03143      * arrives during crash processing.)
03144      */
03145     if (PgArchPID != 0 && !FatalError)
03146     {
03147         ereport(DEBUG2,
03148                 (errmsg_internal("sending %s to process %d",
03149                                  "SIGQUIT",
03150                                  (int) PgArchPID)));
03151         signal_child(PgArchPID, SIGQUIT);
03152     }
03153 
03154     /*
03155      * Force a power-cycle of the pgstat process too.  (This isn't absolutely
03156      * necessary, but it seems like a good idea for robustness, and it
03157      * simplifies the state-machine logic in the case where a shutdown request
03158      * arrives during crash processing.)
03159      */
03160     if (PgStatPID != 0 && !FatalError)
03161     {
03162         ereport(DEBUG2,
03163                 (errmsg_internal("sending %s to process %d",
03164                                  "SIGQUIT",
03165                                  (int) PgStatPID)));
03166         signal_child(PgStatPID, SIGQUIT);
03167         allow_immediate_pgstat_restart();
03168     }
03169 
03170     /* We do NOT restart the syslogger */
03171 
03172     FatalError = true;
03173     /* We now transit into a state of waiting for children to die */
03174     if (pmState == PM_RECOVERY ||
03175         pmState == PM_HOT_STANDBY ||
03176         pmState == PM_RUN ||
03177         pmState == PM_WAIT_BACKUP ||
03178         pmState == PM_WAIT_READONLY ||
03179         pmState == PM_SHUTDOWN)
03180         pmState = PM_WAIT_BACKENDS;
03181 }
03182 
03183 /*
03184  * Log the death of a child process.
03185  */
03186 static void
03187 LogChildExit(int lev, const char *procname, int pid, int exitstatus)
03188 {
03189     /*
03190      * size of activity_buffer is arbitrary, but set equal to default
03191      * track_activity_query_size
03192      */
03193     char        activity_buffer[1024];
03194     const char *activity = NULL;
03195 
03196     if (!EXIT_STATUS_0(exitstatus))
03197         activity = pgstat_get_crashed_backend_activity(pid,
03198                                                        activity_buffer,
03199                                                     sizeof(activity_buffer));
03200 
03201     if (WIFEXITED(exitstatus))
03202         ereport(lev,
03203 
03204         /*------
03205           translator: %s is a noun phrase describing a child process, such as
03206           "server process" */
03207                 (errmsg("%s (PID %d) exited with exit code %d",
03208                         procname, pid, WEXITSTATUS(exitstatus)),
03209                  activity ? errdetail("Failed process was running: %s", activity) : 0));
03210     else if (WIFSIGNALED(exitstatus))
03211 #if defined(WIN32)
03212         ereport(lev,
03213 
03214         /*------
03215           translator: %s is a noun phrase describing a child process, such as
03216           "server process" */
03217                 (errmsg("%s (PID %d) was terminated by exception 0x%X",
03218                         procname, pid, WTERMSIG(exitstatus)),
03219                  errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
03220                  activity ? errdetail("Failed process was running: %s", activity) : 0));
03221 #elif defined(HAVE_DECL_SYS_SIGLIST) && HAVE_DECL_SYS_SIGLIST
03222     ereport(lev,
03223 
03224     /*------
03225       translator: %s is a noun phrase describing a child process, such as
03226       "server process" */
03227             (errmsg("%s (PID %d) was terminated by signal %d: %s",
03228                     procname, pid, WTERMSIG(exitstatus),
03229                     WTERMSIG(exitstatus) < NSIG ?
03230                     sys_siglist[WTERMSIG(exitstatus)] : "(unknown)"),
03231       activity ? errdetail("Failed process was running: %s", activity) : 0));
03232 #else
03233         ereport(lev,
03234 
03235         /*------
03236           translator: %s is a noun phrase describing a child process, such as
03237           "server process" */
03238                 (errmsg("%s (PID %d) was terminated by signal %d",
03239                         procname, pid, WTERMSIG(exitstatus)),
03240                  activity ? errdetail("Failed process was running: %s", activity) : 0));
03241 #endif
03242     else
03243         ereport(lev,
03244 
03245         /*------
03246           translator: %s is a noun phrase describing a child process, such as
03247           "server process" */
03248                 (errmsg("%s (PID %d) exited with unrecognized status %d",
03249                         procname, pid, exitstatus),
03250                  activity ? errdetail("Failed process was running: %s", activity) : 0));
03251 }
03252 
03253 /*
03254  * Advance the postmaster's state machine and take actions as appropriate
03255  *
03256  * This is common code for pmdie(), reaper() and sigusr1_handler(), which
03257  * receive the signals that might mean we need to change state.
03258  */
03259 static void
03260 PostmasterStateMachine(void)
03261 {
03262     if (pmState == PM_WAIT_BACKUP)
03263     {
03264         /*
03265          * PM_WAIT_BACKUP state ends when online backup mode is not active.
03266          */
03267         if (!BackupInProgress())
03268             pmState = PM_WAIT_BACKENDS;
03269     }
03270 
03271     if (pmState == PM_WAIT_READONLY)
03272     {
03273         /*
03274          * PM_WAIT_READONLY state ends when we have no regular backends that
03275          * have been started during recovery.  We kill the startup and
03276          * walreceiver processes and transition to PM_WAIT_BACKENDS.  Ideally,
03277          * we might like to kill these processes first and then wait for
03278          * backends to die off, but that doesn't work at present because
03279          * killing the startup process doesn't release its locks.
03280          */
03281         if (CountChildren(BACKEND_TYPE_NORMAL) == 0)
03282         {
03283             if (StartupPID != 0)
03284                 signal_child(StartupPID, SIGTERM);
03285             if (WalReceiverPID != 0)
03286                 signal_child(WalReceiverPID, SIGTERM);
03287             pmState = PM_WAIT_BACKENDS;
03288         }
03289     }
03290 
03291     /*
03292      * If we are in a state-machine state that implies waiting for backends to
03293      * exit, see if they're all gone, and change state if so.
03294      */
03295     if (pmState == PM_WAIT_BACKENDS)
03296     {
03297         /*
03298          * PM_WAIT_BACKENDS state ends when we have no regular backends
03299          * (including autovac workers), no bgworkers (including unconnected
03300          * ones), and no walwriter, autovac launcher or bgwriter.  If we are
03301          * doing crash recovery then we expect the checkpointer to exit as
03302          * well, otherwise not. The archiver, stats, and syslogger processes
03303          * are disregarded since they are not connected to shared memory; we
03304          * also disregard dead_end children here. Walsenders are also
03305          * disregarded, they will be terminated later after writing the
03306          * checkpoint record, like the archiver process.
03307          */
03308         if (CountChildren(BACKEND_TYPE_NORMAL | BACKEND_TYPE_WORKER) == 0 &&
03309             CountUnconnectedWorkers() == 0 &&
03310             StartupPID == 0 &&
03311             WalReceiverPID == 0 &&
03312             BgWriterPID == 0 &&
03313             (CheckpointerPID == 0 || !FatalError) &&
03314             WalWriterPID == 0 &&
03315             AutoVacPID == 0)
03316         {
03317             if (FatalError)
03318             {
03319                 /*
03320                  * Start waiting for dead_end children to die.  This state
03321                  * change causes ServerLoop to stop creating new ones.
03322                  */
03323                 pmState = PM_WAIT_DEAD_END;
03324 
03325                 /*
03326                  * We already SIGQUIT'd the archiver and stats processes, if
03327                  * any, when we entered FatalError state.
03328                  */
03329             }
03330             else
03331             {
03332                 /*
03333                  * If we get here, we are proceeding with normal shutdown. All
03334                  * the regular children are gone, and it's time to tell the
03335                  * checkpointer to do a shutdown checkpoint.
03336                  */
03337                 Assert(Shutdown > NoShutdown);
03338                 /* Start the checkpointer if not running */
03339                 if (CheckpointerPID == 0)
03340                     CheckpointerPID = StartCheckpointer();
03341                 /* And tell it to shut down */
03342                 if (CheckpointerPID != 0)
03343                 {
03344                     signal_child(CheckpointerPID, SIGUSR2);
03345                     pmState = PM_SHUTDOWN;
03346                 }
03347                 else
03348                 {
03349                     /*
03350                      * If we failed to fork a checkpointer, just shut down.
03351                      * Any required cleanup will happen at next restart. We
03352                      * set FatalError so that an "abnormal shutdown" message
03353                      * gets logged when we exit.
03354                      */
03355                     FatalError = true;
03356                     pmState = PM_WAIT_DEAD_END;
03357 
03358                     /* Kill the walsenders, archiver and stats collector too */
03359                     SignalChildren(SIGQUIT);
03360                     if (PgArchPID != 0)
03361                         signal_child(PgArchPID, SIGQUIT);
03362                     if (PgStatPID != 0)
03363                         signal_child(PgStatPID, SIGQUIT);
03364                 }
03365             }
03366         }
03367     }
03368 
03369     if (pmState == PM_SHUTDOWN_2)
03370     {
03371         /*
03372          * PM_SHUTDOWN_2 state ends when there's no other children than
03373          * dead_end children left. There shouldn't be any regular backends
03374          * left by now anyway; what we're really waiting for is walsenders and
03375          * archiver.
03376          *
03377          * Walreceiver should normally be dead by now, but not when a fast
03378          * shutdown is performed during recovery.
03379          */
03380         if (PgArchPID == 0 && CountChildren(BACKEND_TYPE_ALL) == 0 &&
03381             WalReceiverPID == 0)
03382         {
03383             pmState = PM_WAIT_DEAD_END;
03384         }
03385     }
03386 
03387     if (pmState == PM_WAIT_DEAD_END)
03388     {
03389         /*
03390          * PM_WAIT_DEAD_END state ends when the BackendList is entirely empty
03391          * (ie, no dead_end children remain), and the archiver and stats
03392          * collector are gone too.
03393          *
03394          * The reason we wait for those two is to protect them against a new
03395          * postmaster starting conflicting subprocesses; this isn't an
03396          * ironclad protection, but it at least helps in the
03397          * shutdown-and-immediately-restart scenario.  Note that they have
03398          * already been sent appropriate shutdown signals, either during a
03399          * normal state transition leading up to PM_WAIT_DEAD_END, or during
03400          * FatalError processing.
03401          */
03402         if (dlist_is_empty(&BackendList) &&
03403             PgArchPID == 0 && PgStatPID == 0)
03404         {
03405             /* These other guys should be dead already */
03406             Assert(StartupPID == 0);
03407             Assert(WalReceiverPID == 0);
03408             Assert(BgWriterPID == 0);
03409             Assert(CheckpointerPID == 0);
03410             Assert(WalWriterPID == 0);
03411             Assert(AutoVacPID == 0);
03412             /* syslogger is not considered here */
03413             pmState = PM_NO_CHILDREN;
03414         }
03415     }
03416 
03417     /*
03418      * If we've been told to shut down, we exit as soon as there are no
03419      * remaining children.  If there was a crash, cleanup will occur at the
03420      * next startup.  (Before PostgreSQL 8.3, we tried to recover from the
03421      * crash before exiting, but that seems unwise if we are quitting because
03422      * we got SIGTERM from init --- there may well not be time for recovery
03423      * before init decides to SIGKILL us.)
03424      *
03425      * Note that the syslogger continues to run.  It will exit when it sees
03426      * EOF on its input pipe, which happens when there are no more upstream
03427      * processes.
03428      */
03429     if (Shutdown > NoShutdown && pmState == PM_NO_CHILDREN)
03430     {
03431         if (FatalError)
03432         {
03433             ereport(LOG, (errmsg("abnormal database system shutdown")));
03434             ExitPostmaster(1);
03435         }
03436         else
03437         {
03438             /*
03439              * Terminate exclusive backup mode to avoid recovery after a clean
03440              * fast shutdown.  Since an exclusive backup can only be taken
03441              * during normal running (and not, for example, while running
03442              * under Hot Standby) it only makes sense to do this if we reached
03443              * normal running. If we're still in recovery, the backup file is
03444              * one we're recovering *from*, and we must keep it around so that
03445              * recovery restarts from the right place.
03446              */
03447             if (ReachedNormalRunning)
03448                 CancelBackup();
03449 
03450             /* Normal exit from the postmaster is here */
03451             ExitPostmaster(0);
03452         }
03453     }
03454 
03455     /*
03456      * If recovery failed, or the user does not want an automatic restart
03457      * after backend crashes, wait for all non-syslogger children to exit, and
03458      * then exit postmaster. We don't try to reinitialize when recovery fails,
03459      * because more than likely it will just fail again and we will keep
03460      * trying forever.
03461      */
03462     if (pmState == PM_NO_CHILDREN && (RecoveryError || !restart_after_crash))
03463         ExitPostmaster(1);
03464 
03465     /*
03466      * If we need to recover from a crash, wait for all non-syslogger children
03467      * to exit, then reset shmem and StartupDataBase.
03468      */
03469     if (FatalError && pmState == PM_NO_CHILDREN)
03470     {
03471         ereport(LOG,
03472                 (errmsg("all server processes terminated; reinitializing")));
03473 
03474         shmem_exit(1);
03475         reset_shared(PostPortNumber);
03476 
03477         StartupPID = StartupDataBase();
03478         Assert(StartupPID != 0);
03479         pmState = PM_STARTUP;
03480     }
03481 }
03482 
03483 
03484 /*
03485  * Send a signal to a postmaster child process
03486  *
03487  * On systems that have setsid(), each child process sets itself up as a
03488  * process group leader.  For signals that are generally interpreted in the
03489  * appropriate fashion, we signal the entire process group not just the
03490  * direct child process.  This allows us to, for example, SIGQUIT a blocked
03491  * archive_recovery script, or SIGINT a script being run by a backend via
03492  * system().
03493  *
03494  * There is a race condition for recently-forked children: they might not
03495  * have executed setsid() yet.  So we signal the child directly as well as
03496  * the group.  We assume such a child will handle the signal before trying
03497  * to spawn any grandchild processes.  We also assume that signaling the
03498  * child twice will not cause any problems.
03499  */
03500 static void
03501 signal_child(pid_t pid, int signal)
03502 {
03503     if (kill(pid, signal) < 0)
03504         elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) pid, signal);
03505 #ifdef HAVE_SETSID
03506     switch (signal)
03507     {
03508         case SIGINT:
03509         case SIGTERM:
03510         case SIGQUIT:
03511         case SIGSTOP:
03512             if (kill(-pid, signal) < 0)
03513                 elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) (-pid), signal);
03514             break;
03515         default:
03516             break;
03517     }
03518 #endif
03519 }
03520 
03521 /*
03522  * Send a signal to bgworkers that did not request backend connections
03523  *
03524  * The reason this is interesting is that workers that did request connections
03525  * are considered by SignalChildren; this function complements that one.
03526  */
03527 static bool
03528 SignalUnconnectedWorkers(int signal)
03529 {
03530     slist_iter  iter;
03531     bool        signaled = false;
03532 
03533     slist_foreach(iter, &BackgroundWorkerList)
03534     {
03535         RegisteredBgWorker *rw;
03536 
03537         rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
03538 
03539         if (rw->rw_pid == 0)
03540             continue;
03541         /* ignore connected workers */
03542         if (rw->rw_backend != NULL)
03543             continue;
03544 
03545         ereport(DEBUG4,
03546                 (errmsg_internal("sending signal %d to process %d",
03547                                  signal, (int) rw->rw_pid)));
03548         signal_child(rw->rw_pid, signal);
03549         signaled = true;
03550     }
03551     return signaled;
03552 }
03553 
03554 /*
03555  * Send a signal to the targeted children (but NOT special children;
03556  * dead_end children are never signaled, either).
03557  */
03558 static bool
03559 SignalSomeChildren(int signal, int target)
03560 {
03561     dlist_iter  iter;
03562     bool        signaled = false;
03563 
03564     dlist_foreach(iter, &BackendList)
03565     {
03566         Backend    *bp = dlist_container(Backend, elem, iter.cur);
03567 
03568         if (bp->dead_end)
03569             continue;
03570 
03571         /*
03572          * Since target == BACKEND_TYPE_ALL is the most common case, we test
03573          * it first and avoid touching shared memory for every child.
03574          */
03575         if (target != BACKEND_TYPE_ALL)
03576         {
03577             /*
03578              * Assign bkend_type for any recently announced WAL Sender
03579              * processes.
03580              */
03581             if (bp->bkend_type == BACKEND_TYPE_NORMAL &&
03582                 IsPostmasterChildWalSender(bp->child_slot))
03583                 bp->bkend_type = BACKEND_TYPE_WALSND;
03584 
03585             if (!(target & bp->bkend_type))
03586                 continue;
03587         }
03588 
03589         ereport(DEBUG4,
03590                 (errmsg_internal("sending signal %d to process %d",
03591                                  signal, (int) bp->pid)));
03592         signal_child(bp->pid, signal);
03593         signaled = true;
03594     }
03595     return signaled;
03596 }
03597 
03598 /*
03599  * BackendStartup -- start backend process
03600  *
03601  * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
03602  *
03603  * Note: if you change this code, also consider StartAutovacuumWorker.
03604  */
03605 static int
03606 BackendStartup(Port *port)
03607 {
03608     Backend    *bn;             /* for backend cleanup */
03609     pid_t       pid;
03610 
03611     /*
03612      * Create backend data structure.  Better before the fork() so we can
03613      * handle failure cleanly.
03614      */
03615     bn = (Backend *) malloc(sizeof(Backend));
03616     if (!bn)
03617     {
03618         ereport(LOG,
03619                 (errcode(ERRCODE_OUT_OF_MEMORY),
03620                  errmsg("out of memory")));
03621         return STATUS_ERROR;
03622     }
03623 
03624     /*
03625      * Compute the cancel key that will be assigned to this backend. The
03626      * backend will have its own copy in the forked-off process' value of
03627      * MyCancelKey, so that it can transmit the key to the frontend.
03628      */
03629     MyCancelKey = PostmasterRandom();
03630     bn->cancel_key = MyCancelKey;
03631 
03632     /* Pass down canAcceptConnections state */
03633     port->canAcceptConnections = canAcceptConnections();
03634     bn->dead_end = (port->canAcceptConnections != CAC_OK &&
03635                     port->canAcceptConnections != CAC_WAITBACKUP);
03636 
03637     /*
03638      * Unless it's a dead_end child, assign it a child slot number
03639      */
03640     if (!bn->dead_end)
03641         bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
03642     else
03643         bn->child_slot = 0;
03644 
03645 #ifdef EXEC_BACKEND
03646     pid = backend_forkexec(port);
03647 #else                           /* !EXEC_BACKEND */
03648     pid = fork_process();
03649     if (pid == 0)               /* child */
03650     {
03651         free(bn);
03652 
03653         /*
03654          * Let's clean up ourselves as the postmaster child, and close the
03655          * postmaster's listen sockets.  (In EXEC_BACKEND case this is all
03656          * done in SubPostmasterMain.)
03657          */
03658         IsUnderPostmaster = true;       /* we are a postmaster subprocess now */
03659 
03660         MyProcPid = getpid();   /* reset MyProcPid */
03661 
03662         MyStartTime = time(NULL);
03663 
03664         /* We don't want the postmaster's proc_exit() handlers */
03665         on_exit_reset();
03666 
03667         /* Close the postmaster's sockets */
03668         ClosePostmasterPorts(false);
03669 
03670         /* Perform additional initialization and collect startup packet */
03671         BackendInitialize(port);
03672 
03673         /* And run the backend */
03674         BackendRun(port);
03675     }
03676 #endif   /* EXEC_BACKEND */
03677 
03678     if (pid < 0)
03679     {
03680         /* in parent, fork failed */
03681         int         save_errno = errno;
03682 
03683         if (!bn->dead_end)
03684             (void) ReleasePostmasterChildSlot(bn->child_slot);
03685         free(bn);
03686         errno = save_errno;
03687         ereport(LOG,
03688                 (errmsg("could not fork new process for connection: %m")));
03689         report_fork_failure_to_client(port, save_errno);
03690         return STATUS_ERROR;
03691     }
03692 
03693     /* in parent, successful fork */
03694     ereport(DEBUG2,
03695             (errmsg_internal("forked new backend, pid=%d socket=%d",
03696                              (int) pid, (int) port->sock)));
03697 
03698     /*
03699      * Everything's been successful, it's safe to add this backend to our list
03700      * of backends.
03701      */
03702     bn->pid = pid;
03703     bn->bkend_type = BACKEND_TYPE_NORMAL;       /* Can change later to WALSND */
03704     dlist_push_head(&BackendList, &bn->elem);
03705 
03706 #ifdef EXEC_BACKEND
03707     if (!bn->dead_end)
03708         ShmemBackendArrayAdd(bn);
03709 #endif
03710 
03711     return STATUS_OK;
03712 }
03713 
03714 /*
03715  * Try to report backend fork() failure to client before we close the
03716  * connection.  Since we do not care to risk blocking the postmaster on
03717  * this connection, we set the connection to non-blocking and try only once.
03718  *
03719  * This is grungy special-purpose code; we cannot use backend libpq since
03720  * it's not up and running.
03721  */
03722 static void
03723 report_fork_failure_to_client(Port *port, int errnum)
03724 {
03725     char        buffer[1000];
03726     int         rc;
03727 
03728     /* Format the error message packet (always V2 protocol) */
03729     snprintf(buffer, sizeof(buffer), "E%s%s\n",
03730              _("could not fork new process for connection: "),
03731              strerror(errnum));
03732 
03733     /* Set port to non-blocking.  Don't do send() if this fails */
03734     if (!pg_set_noblock(port->sock))
03735         return;
03736 
03737     /* We'll retry after EINTR, but ignore all other failures */
03738     do
03739     {
03740         rc = send(port->sock, buffer, strlen(buffer) + 1, 0);
03741     } while (rc < 0 && errno == EINTR);
03742 }
03743 
03744 
03745 /*
03746  * BackendInitialize -- initialize an interactive (postmaster-child)
03747  *              backend process, and collect the client's startup packet.
03748  *
03749  * returns: nothing.  Will not return at all if there's any failure.
03750  *
03751  * Note: this code does not depend on having any access to shared memory.
03752  * In the EXEC_BACKEND case, we are physically attached to shared memory
03753  * but have not yet set up most of our local pointers to shmem structures.
03754  */
03755 static void
03756 BackendInitialize(Port *port)
03757 {
03758     int         status;
03759     int         ret;
03760     char        remote_host[NI_MAXHOST];
03761     char        remote_port[NI_MAXSERV];
03762     char        remote_ps_data[NI_MAXHOST];
03763 
03764     /* Save port etc. for ps status */
03765     MyProcPort = port;
03766 
03767     /*
03768      * PreAuthDelay is a debugging aid for investigating problems in the
03769      * authentication cycle: it can be set in postgresql.conf to allow time to
03770      * attach to the newly-forked backend with a debugger.  (See also
03771      * PostAuthDelay, which we allow clients to pass through PGOPTIONS, but it
03772      * is not honored until after authentication.)
03773      */
03774     if (PreAuthDelay > 0)
03775         pg_usleep(PreAuthDelay * 1000000L);
03776 
03777     /* This flag will remain set until InitPostgres finishes authentication */
03778     ClientAuthInProgress = true;    /* limit visibility of log messages */
03779 
03780     /* save process start time */
03781     port->SessionStartTime = GetCurrentTimestamp();
03782     MyStartTime = timestamptz_to_time_t(port->SessionStartTime);
03783 
03784     /* set these to empty in case they are needed before we set them up */
03785     port->remote_host = "";
03786     port->remote_port = "";
03787 
03788     /*
03789      * Initialize libpq and enable reporting of ereport errors to the client.
03790      * Must do this now because authentication uses libpq to send messages.
03791      */
03792     pq_init();                  /* initialize libpq to talk to client */
03793     whereToSendOutput = DestRemote;     /* now safe to ereport to client */
03794 
03795     /*
03796      * If possible, make this process a group leader, so that the postmaster
03797      * can signal any child processes too.  (We do this now on the off chance
03798      * that something might spawn a child process during authentication.)
03799      */
03800 #ifdef HAVE_SETSID
03801     if (setsid() < 0)
03802         elog(FATAL, "setsid() failed: %m");
03803 #endif
03804 
03805     /*
03806      * We arrange for a simple exit(1) if we receive SIGTERM or SIGQUIT or
03807      * timeout while trying to collect the startup packet.  Otherwise the
03808      * postmaster cannot shutdown the database FAST or IMMED cleanly if a
03809      * buggy client fails to send the packet promptly.
03810      */
03811     pqsignal(SIGTERM, startup_die);
03812     pqsignal(SIGQUIT, startup_die);
03813     InitializeTimeouts();       /* establishes SIGALRM handler */
03814     PG_SETMASK(&StartupBlockSig);
03815 
03816     /*
03817      * Get the remote host name and port for logging and status display.
03818      */
03819     remote_host[0] = '\0';
03820     remote_port[0] = '\0';
03821     if ((ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
03822                            remote_host, sizeof(remote_host),
03823                            remote_port, sizeof(remote_port),
03824                   (log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV)) != 0)
03825         ereport(WARNING,
03826                 (errmsg_internal("pg_getnameinfo_all() failed: %s",
03827                                  gai_strerror(ret))));
03828     if (remote_port[0] == '\0')
03829         snprintf(remote_ps_data, sizeof(remote_ps_data), "%s", remote_host);
03830     else
03831         snprintf(remote_ps_data, sizeof(remote_ps_data), "%s(%s)", remote_host, remote_port);
03832 
03833     if (Log_connections)
03834     {
03835         if (remote_port[0])
03836             ereport(LOG,
03837                     (errmsg("connection received: host=%s port=%s",
03838                             remote_host,
03839                             remote_port)));
03840         else
03841             ereport(LOG,
03842                     (errmsg("connection received: host=%s",
03843                             remote_host)));
03844     }
03845 
03846     /*
03847      * save remote_host and remote_port in port structure
03848      */
03849     port->remote_host = strdup(remote_host);
03850     port->remote_port = strdup(remote_port);
03851     if (log_hostname)
03852         port->remote_hostname = port->remote_host;
03853 
03854     /*
03855      * Ready to begin client interaction.  We will give up and exit(1) after a
03856      * time delay, so that a broken client can't hog a connection
03857      * indefinitely.  PreAuthDelay and any DNS interactions above don't count
03858      * against the time limit.
03859      *
03860      * Note: AuthenticationTimeout is applied here while waiting for the
03861      * startup packet, and then again in InitPostgres for the duration of any
03862      * authentication operations.  So a hostile client could tie up the
03863      * process for nearly twice AuthenticationTimeout before we kick him off.
03864      *
03865      * Note: because PostgresMain will call InitializeTimeouts again, the
03866      * registration of STARTUP_PACKET_TIMEOUT will be lost.  This is okay
03867      * since we never use it again after this function.
03868      */
03869     RegisterTimeout(STARTUP_PACKET_TIMEOUT, StartupPacketTimeoutHandler);
03870     enable_timeout_after(STARTUP_PACKET_TIMEOUT, AuthenticationTimeout * 1000);
03871 
03872     /*
03873      * Receive the startup packet (which might turn out to be a cancel request
03874      * packet).
03875      */
03876     status = ProcessStartupPacket(port, false);
03877 
03878     /*
03879      * Stop here if it was bad or a cancel packet.  ProcessStartupPacket
03880      * already did any appropriate error reporting.
03881      */
03882     if (status != STATUS_OK)
03883         proc_exit(0);
03884 
03885     /*
03886      * Now that we have the user and database name, we can set the process
03887      * title for ps.  It's good to do this as early as possible in startup.
03888      *
03889      * For a walsender, the ps display is set in the following form:
03890      *
03891      * postgres: wal sender process <user> <host> <activity>
03892      *
03893      * To achieve that, we pass "wal sender process" as username and username
03894      * as dbname to init_ps_display(). XXX: should add a new variant of
03895      * init_ps_display() to avoid abusing the parameters like this.
03896      */
03897     if (am_walsender)
03898         init_ps_display("wal sender process", port->user_name, remote_ps_data,
03899                         update_process_title ? "authentication" : "");
03900     else
03901         init_ps_display(port->user_name, port->database_name, remote_ps_data,
03902                         update_process_title ? "authentication" : "");
03903 
03904     /*
03905      * Disable the timeout, and prevent SIGTERM/SIGQUIT again.
03906      */
03907     disable_timeout(STARTUP_PACKET_TIMEOUT, false);
03908     PG_SETMASK(&BlockSig);
03909 }
03910 
03911 
03912 /*
03913  * BackendRun -- set up the backend's argument list and invoke PostgresMain()
03914  *
03915  * returns:
03916  *      Shouldn't return at all.
03917  *      If PostgresMain() fails, return status.
03918  */
03919 static void
03920 BackendRun(Port *port)
03921 {
03922     char      **av;
03923     int         maxac;
03924     int         ac;
03925     long        secs;
03926     int         usecs;
03927     int         i;
03928 
03929     /*
03930      * Don't want backend to be able to see the postmaster random number
03931      * generator state.  We have to clobber the static random_seed *and* start
03932      * a new random sequence in the random() library function.
03933      */
03934     random_seed = 0;
03935     random_start_time.tv_usec = 0;
03936     /* slightly hacky way to get integer microseconds part of timestamptz */
03937     TimestampDifference(0, port->SessionStartTime, &secs, &usecs);
03938     srandom((unsigned int) (MyProcPid ^ usecs));
03939 
03940     /*
03941      * Now, build the argv vector that will be given to PostgresMain.
03942      *
03943      * The maximum possible number of commandline arguments that could come
03944      * from ExtraOptions is (strlen(ExtraOptions) + 1) / 2; see
03945      * pg_split_opts().
03946      */
03947     maxac = 2;                  /* for fixed args supplied below */
03948     maxac += (strlen(ExtraOptions) + 1) / 2;
03949 
03950     av = (char **) MemoryContextAlloc(TopMemoryContext,
03951                                       maxac * sizeof(char *));
03952     ac = 0;
03953 
03954     av[ac++] = "postgres";
03955 
03956     /*
03957      * Pass any backend switches specified with -o on the postmaster's own
03958      * command line.  We assume these are secure.  (It's OK to mangle
03959      * ExtraOptions now, since we're safely inside a subprocess.)
03960      */
03961     pg_split_opts(av, &ac, ExtraOptions);
03962 
03963     av[ac] = NULL;
03964 
03965     Assert(ac < maxac);
03966 
03967     /*
03968      * Debug: print arguments being passed to backend
03969      */
03970     ereport(DEBUG3,
03971             (errmsg_internal("%s child[%d]: starting with (",
03972                              progname, (int) getpid())));
03973     for (i = 0; i < ac; ++i)
03974         ereport(DEBUG3,
03975                 (errmsg_internal("\t%s", av[i])));
03976     ereport(DEBUG3,
03977             (errmsg_internal(")")));
03978 
03979     /*
03980      * Make sure we aren't in PostmasterContext anymore.  (We can't delete it
03981      * just yet, though, because InitPostgres will need the HBA data.)
03982      */
03983     MemoryContextSwitchTo(TopMemoryContext);
03984 
03985     PostgresMain(ac, av, port->database_name, port->user_name);
03986 }
03987 
03988 
03989 #ifdef EXEC_BACKEND
03990 
03991 /*
03992  * postmaster_forkexec -- fork and exec a postmaster subprocess
03993  *
03994  * The caller must have set up the argv array already, except for argv[2]
03995  * which will be filled with the name of the temp variable file.
03996  *
03997  * Returns the child process PID, or -1 on fork failure (a suitable error
03998  * message has been logged on failure).
03999  *
04000  * All uses of this routine will dispatch to SubPostmasterMain in the
04001  * child process.
04002  */
04003 pid_t
04004 postmaster_forkexec(int argc, char *argv[])
04005 {
04006     Port        port;
04007 
04008     /* This entry point passes dummy values for the Port variables */
04009     memset(&port, 0, sizeof(port));
04010     return internal_forkexec(argc, argv, &port);
04011 }
04012 
04013 /*
04014  * backend_forkexec -- fork/exec off a backend process
04015  *
04016  * Some operating systems (WIN32) don't have fork() so we have to simulate
04017  * it by storing parameters that need to be passed to the child and
04018  * then create a new child process.
04019  *
04020  * returns the pid of the fork/exec'd process, or -1 on failure
04021  */
04022 static pid_t
04023 backend_forkexec(Port *port)
04024 {
04025     char       *av[4];
04026     int         ac = 0;
04027 
04028     av[ac++] = "postgres";
04029     av[ac++] = "--forkbackend";
04030     av[ac++] = NULL;            /* filled in by internal_forkexec */
04031 
04032     av[ac] = NULL;
04033     Assert(ac < lengthof(av));
04034 
04035     return internal_forkexec(ac, av, port);
04036 }
04037 
04038 #ifndef WIN32
04039 
04040 /*
04041  * internal_forkexec non-win32 implementation
04042  *
04043  * - writes out backend variables to the parameter file
04044  * - fork():s, and then exec():s the child process
04045  */
04046 static pid_t
04047 internal_forkexec(int argc, char *argv[], Port *port)
04048 {
04049     static unsigned long tmpBackendFileNum = 0;
04050     pid_t       pid;
04051     char        tmpfilename[MAXPGPATH];
04052     BackendParameters param;
04053     FILE       *fp;
04054 
04055     if (!save_backend_variables(&param, port))
04056         return -1;              /* log made by save_backend_variables */
04057 
04058     /* Calculate name for temp file */
04059     snprintf(tmpfilename, MAXPGPATH, "%s/%s.backend_var.%d.%lu",
04060              PG_TEMP_FILES_DIR, PG_TEMP_FILE_PREFIX,
04061              MyProcPid, ++tmpBackendFileNum);
04062 
04063     /* Open file */
04064     fp = AllocateFile(tmpfilename, PG_BINARY_W);
04065     if (!fp)
04066     {
04067         /*
04068          * As in OpenTemporaryFileInTablespace, try to make the temp-file
04069          * directory
04070          */
04071         mkdir(PG_TEMP_FILES_DIR, S_IRWXU);
04072 
04073         fp = AllocateFile(tmpfilename, PG_BINARY_W);
04074         if (!fp)
04075         {
04076             ereport(LOG,
04077                     (errcode_for_file_access(),
04078                      errmsg("could not create file \"%s\": %m",
04079                             tmpfilename)));
04080             return -1;
04081         }
04082     }
04083 
04084     if (fwrite(&param, sizeof(param), 1, fp) != 1)
04085     {
04086         ereport(LOG,
04087                 (errcode_for_file_access(),
04088                  errmsg("could not write to file \"%s\": %m", tmpfilename)));
04089         FreeFile(fp);
04090         return -1;
04091     }
04092 
04093     /* Release file */
04094     if (FreeFile(fp))
04095     {
04096         ereport(LOG,
04097                 (errcode_for_file_access(),
04098                  errmsg("could not write to file \"%s\": %m", tmpfilename)));
04099         return -1;
04100     }
04101 
04102     /* Make sure caller set up argv properly */
04103     Assert(argc >= 3);
04104     Assert(argv[argc] == NULL);
04105     Assert(strncmp(argv[1], "--fork", 6) == 0);
04106     Assert(argv[2] == NULL);
04107 
04108     /* Insert temp file name after --fork argument */
04109     argv[2] = tmpfilename;
04110 
04111     /* Fire off execv in child */
04112     if ((pid = fork_process()) == 0)
04113     {
04114         if (execv(postgres_exec_path, argv) < 0)
04115         {
04116             ereport(LOG,
04117                     (errmsg("could not execute server process \"%s\": %m",
04118                             postgres_exec_path)));
04119             /* We're already in the child process here, can't return */
04120             exit(1);
04121         }
04122     }
04123 
04124     return pid;                 /* Parent returns pid, or -1 on fork failure */
04125 }
04126 #else                           /* WIN32 */
04127 
04128 /*
04129  * internal_forkexec win32 implementation
04130  *
04131  * - starts backend using CreateProcess(), in suspended state
04132  * - writes out backend variables to the parameter file
04133  *  - during this, duplicates handles and sockets required for
04134  *    inheritance into the new process
04135  * - resumes execution of the new process once the backend parameter
04136  *   file is complete.
04137  */
04138 static pid_t
04139 internal_forkexec(int argc, char *argv[], Port *port)
04140 {
04141     STARTUPINFO si;
04142     PROCESS_INFORMATION pi;
04143     int         i;
04144     int         j;
04145     char        cmdLine[MAXPGPATH * 2];
04146     HANDLE      paramHandle;
04147     BackendParameters *param;
04148     SECURITY_ATTRIBUTES sa;
04149     char        paramHandleStr[32];
04150     win32_deadchild_waitinfo *childinfo;
04151 
04152     /* Make sure caller set up argv properly */
04153     Assert(argc >= 3);
04154     Assert(argv[argc] == NULL);
04155     Assert(strncmp(argv[1], "--fork", 6) == 0);
04156     Assert(argv[2] == NULL);
04157 
04158     /* Set up shared memory for parameter passing */
04159     ZeroMemory(&sa, sizeof(sa));
04160     sa.nLength = sizeof(sa);
04161     sa.bInheritHandle = TRUE;
04162     paramHandle = CreateFileMapping(INVALID_HANDLE_VALUE,
04163                                     &sa,
04164                                     PAGE_READWRITE,
04165                                     0,
04166                                     sizeof(BackendParameters),
04167                                     NULL);
04168     if (paramHandle == INVALID_HANDLE_VALUE)
04169     {
04170         elog(LOG, "could not create backend parameter file mapping: error code %lu",
04171              GetLastError());
04172         return -1;
04173     }
04174 
04175     param = MapViewOfFile(paramHandle, FILE_MAP_WRITE, 0, 0, sizeof(BackendParameters));
04176     if (!param)
04177     {
04178         elog(LOG, "could not map backend parameter memory: error code %lu",
04179              GetLastError());
04180         CloseHandle(paramHandle);
04181         return -1;
04182     }
04183 
04184     /* Insert temp file name after --fork argument */
04185 #ifdef _WIN64
04186     sprintf(paramHandleStr, "%llu", (LONG_PTR) paramHandle);
04187 #else
04188     sprintf(paramHandleStr, "%lu", (DWORD) paramHandle);
04189 #endif
04190     argv[2] = paramHandleStr;
04191 
04192     /* Format the cmd line */
04193     cmdLine[sizeof(cmdLine) - 1] = '\0';
04194     cmdLine[sizeof(cmdLine) - 2] = '\0';
04195     snprintf(cmdLine, sizeof(cmdLine) - 1, "\"%s\"", postgres_exec_path);
04196     i = 0;
04197     while (argv[++i] != NULL)
04198     {
04199         j = strlen(cmdLine);
04200         snprintf(cmdLine + j, sizeof(cmdLine) - 1 - j, " \"%s\"", argv[i]);
04201     }
04202     if (cmdLine[sizeof(cmdLine) - 2] != '\0')
04203     {
04204         elog(LOG, "subprocess command line too long");
04205         return -1;
04206     }
04207 
04208     memset(&pi, 0, sizeof(pi));
04209     memset(&si, 0, sizeof(si));
04210     si.cb = sizeof(si);
04211 
04212     /*
04213      * Create the subprocess in a suspended state. This will be resumed later,
04214      * once we have written out the parameter file.
04215      */
04216     if (!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, CREATE_SUSPENDED,
04217                        NULL, NULL, &si, &pi))
04218     {
04219         elog(LOG, "CreateProcess call failed: %m (error code %lu)",
04220              GetLastError());
04221         return -1;
04222     }
04223 
04224     if (!save_backend_variables(param, port, pi.hProcess, pi.dwProcessId))
04225     {
04226         /*
04227          * log made by save_backend_variables, but we have to clean up the
04228          * mess with the half-started process
04229          */
04230         if (!TerminateProcess(pi.hProcess, 255))
04231             ereport(LOG,
04232                     (errmsg_internal("could not terminate unstarted process: error code %lu",
04233                                      GetLastError())));
04234         CloseHandle(pi.hProcess);
04235         CloseHandle(pi.hThread);
04236         return -1;              /* log made by save_backend_variables */
04237     }
04238 
04239     /* Drop the parameter shared memory that is now inherited to the backend */
04240     if (!UnmapViewOfFile(param))
04241         elog(LOG, "could not unmap view of backend parameter file: error code %lu",
04242              GetLastError());
04243     if (!CloseHandle(paramHandle))
04244         elog(LOG, "could not close handle to backend parameter file: error code %lu",
04245              GetLastError());
04246 
04247     /*
04248      * Reserve the memory region used by our main shared memory segment before
04249      * we resume the child process.
04250      */
04251     if (!pgwin32_ReserveSharedMemoryRegion(pi.hProcess))
04252     {
04253         /*
04254          * Failed to reserve the memory, so terminate the newly created
04255          * process and give up.
04256          */
04257         if (!TerminateProcess(pi.hProcess, 255))
04258             ereport(LOG,
04259                     (errmsg_internal("could not terminate process that failed to reserve memory: error code %lu",
04260                                      GetLastError())));
04261         CloseHandle(pi.hProcess);
04262         CloseHandle(pi.hThread);
04263         return -1;              /* logging done made by
04264                                  * pgwin32_ReserveSharedMemoryRegion() */
04265     }
04266 
04267     /*
04268      * Now that the backend variables are written out, we start the child
04269      * thread so it can start initializing while we set up the rest of the
04270      * parent state.
04271      */
04272     if (ResumeThread(pi.hThread) == -1)
04273     {
04274         if (!TerminateProcess(pi.hProcess, 255))
04275         {
04276             ereport(LOG,
04277                     (errmsg_internal("could not terminate unstartable process: error code %lu",
04278                                      GetLastError())));
04279             CloseHandle(pi.hProcess);
04280             CloseHandle(pi.hThread);
04281             return -1;
04282         }
04283         CloseHandle(pi.hProcess);
04284         CloseHandle(pi.hThread);
04285         ereport(LOG,
04286                 (errmsg_internal("could not resume thread of unstarted process: error code %lu",
04287                                  GetLastError())));
04288         return -1;
04289     }
04290 
04291     /*
04292      * Queue a waiter for to signal when this child dies. The wait will be
04293      * handled automatically by an operating system thread pool.
04294      *
04295      * Note: use malloc instead of palloc, since it needs to be thread-safe.
04296      * Struct will be free():d from the callback function that runs on a
04297      * different thread.
04298      */
04299     childinfo = malloc(sizeof(win32_deadchild_waitinfo));
04300     if (!childinfo)
04301         ereport(FATAL,
04302                 (errcode(ERRCODE_OUT_OF_MEMORY),
04303                  errmsg("out of memory")));
04304 
04305     childinfo->procHandle = pi.hProcess;
04306     childinfo->procId = pi.dwProcessId;
04307 
04308     if (!RegisterWaitForSingleObject(&childinfo->waitHandle,
04309                                      pi.hProcess,
04310                                      pgwin32_deadchild_callback,
04311                                      childinfo,
04312                                      INFINITE,
04313                                 WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD))
04314         ereport(FATAL,
04315                 (errmsg_internal("could not register process for wait: error code %lu",
04316                                  GetLastError())));
04317 
04318     /* Don't close pi.hProcess here - the wait thread needs access to it */
04319 
04320     CloseHandle(pi.hThread);
04321 
04322     return pi.dwProcessId;
04323 }
04324 #endif   /* WIN32 */
04325 
04326 
04327 /*
04328  * SubPostmasterMain -- Get the fork/exec'd process into a state equivalent
04329  *          to what it would be if we'd simply forked on Unix, and then
04330  *          dispatch to the appropriate place.
04331  *
04332  * The first two command line arguments are expected to be "--forkFOO"
04333  * (where FOO indicates which postmaster child we are to become), and
04334  * the name of a variables file that we can read to load data that would
04335  * have been inherited by fork() on Unix.  Remaining arguments go to the
04336  * subprocess FooMain() routine.
04337  */
04338 void
04339 SubPostmasterMain(int argc, char *argv[])
04340 {
04341     Port        port;
04342 
04343     /* Do this sooner rather than later... */
04344     IsUnderPostmaster = true;   /* we are a postmaster subprocess now */
04345 
04346     MyProcPid = getpid();       /* reset MyProcPid */
04347 
04348     MyStartTime = time(NULL);
04349 
04350     /*
04351      * make sure stderr is in binary mode before anything can possibly be
04352      * written to it, in case it's actually the syslogger pipe, so the pipe
04353      * chunking protocol isn't disturbed. Non-logpipe data gets translated on
04354      * redirection (e.g. via pg_ctl -l) anyway.
04355      */
04356 #ifdef WIN32
04357     _setmode(fileno(stderr), _O_BINARY);
04358 #endif
04359 
04360     /* Lose the postmaster's on-exit routines (really a no-op) */
04361     on_exit_reset();
04362 
04363     /* In EXEC_BACKEND case we will not have inherited these settings */
04364     IsPostmasterEnvironment = true;
04365     whereToSendOutput = DestNone;
04366 
04367     /* Setup essential subsystems (to ensure elog() behaves sanely) */
04368     MemoryContextInit();
04369     InitializeGUCOptions();
04370 
04371     /* Read in the variables file */
04372     memset(&port, 0, sizeof(Port));
04373     read_backend_variables(argv[2], &port);
04374 
04375     /*
04376      * Set reference point for stack-depth checking
04377      */
04378     set_stack_base();
04379 
04380     /*
04381      * Set up memory area for GSS information. Mirrors the code in ConnCreate
04382      * for the non-exec case.
04383      */
04384 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
04385     port.gss = (pg_gssinfo *) calloc(1, sizeof(pg_gssinfo));
04386     if (!port.gss)
04387         ereport(FATAL,
04388                 (errcode(ERRCODE_OUT_OF_MEMORY),
04389                  errmsg("out of memory")));
04390 #endif
04391 
04392     /* Check we got appropriate args */
04393     if (argc < 3)
04394         elog(FATAL, "invalid subpostmaster invocation");
04395 
04396     /*
04397      * If appropriate, physically re-attach to shared memory segment. We want
04398      * to do this before going any further to ensure that we can attach at the
04399      * same address the postmaster used.
04400      */
04401     if (strcmp(argv[1], "--forkbackend") == 0 ||
04402         strcmp(argv[1], "--forkavlauncher") == 0 ||
04403         strcmp(argv[1], "--forkavworker") == 0 ||
04404         strcmp(argv[1], "--forkboot") == 0 ||
04405         strncmp(argv[1], "--forkbgworker=", 15) == 0)
04406         PGSharedMemoryReAttach();
04407 
04408     /* autovacuum needs this set before calling InitProcess */
04409     if (strcmp(argv[1], "--forkavlauncher") == 0)
04410         AutovacuumLauncherIAm();
04411     if (strcmp(argv[1], "--forkavworker") == 0)
04412         AutovacuumWorkerIAm();
04413 
04414     /*
04415      * Start our win32 signal implementation. This has to be done after we
04416      * read the backend variables, because we need to pick up the signal pipe
04417      * from the parent process.
04418      */
04419 #ifdef WIN32
04420     pgwin32_signal_initialize();
04421 #endif
04422 
04423     /* In EXEC_BACKEND case we will not have inherited these settings */
04424     pqinitmask();
04425     PG_SETMASK(&BlockSig);
04426 
04427     /* Read in remaining GUC variables */
04428     read_nondefault_variables();
04429 
04430     /*
04431      * Reload any libraries that were preloaded by the postmaster.  Since we
04432      * exec'd this process, those libraries didn't come along with us; but we
04433      * should load them into all child processes to be consistent with the
04434      * non-EXEC_BACKEND behavior.
04435      */
04436     process_shared_preload_libraries();
04437 
04438     /* Run backend or appropriate child */
04439     if (strcmp(argv[1], "--forkbackend") == 0)
04440     {
04441         Assert(argc == 3);      /* shouldn't be any more args */
04442 
04443         /* Close the postmaster's sockets */
04444         ClosePostmasterPorts(false);
04445 
04446         /*
04447          * Need to reinitialize the SSL library in the backend, since the
04448          * context structures contain function pointers and cannot be passed
04449          * through the parameter file.
04450          *
04451          * XXX should we do this in all child processes?  For the moment it's
04452          * enough to do it in backend children.
04453          */
04454 #ifdef USE_SSL
04455         if (EnableSSL)
04456             secure_initialize();
04457 #endif
04458 
04459         /*
04460          * Perform additional initialization and collect startup packet.
04461          *
04462          * We want to do this before InitProcess() for a couple of reasons: 1.
04463          * so that we aren't eating up a PGPROC slot while waiting on the
04464          * client. 2. so that if InitProcess() fails due to being out of
04465          * PGPROC slots, we have already initialized libpq and are able to
04466          * report the error to the client.
04467          */
04468         BackendInitialize(&port);
04469 
04470         /* Restore basic shared memory pointers */
04471         InitShmemAccess(UsedShmemSegAddr);
04472 
04473         /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
04474         InitProcess();
04475 
04476         /*
04477          * Attach process to shared data structures.  If testing EXEC_BACKEND
04478          * on Linux, you must run this as root before starting the postmaster:
04479          *
04480          * echo 0 >/proc/sys/kernel/randomize_va_space
04481          *
04482          * This prevents a randomized stack base address that causes child
04483          * shared memory to be at a different address than the parent, making
04484          * it impossible to attached to shared memory.  Return the value to
04485          * '1' when finished.
04486          */
04487         CreateSharedMemoryAndSemaphores(false, 0);
04488 
04489         /* And run the backend */
04490         BackendRun(&port);      /* does not return */
04491     }
04492     if (strcmp(argv[1], "--forkboot") == 0)
04493     {
04494         /* Close the postmaster's sockets */
04495         ClosePostmasterPorts(false);
04496 
04497         /* Restore basic shared memory pointers */
04498         InitShmemAccess(UsedShmemSegAddr);
04499 
04500         /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
04501         InitAuxiliaryProcess();
04502 
04503         /* Attach process to shared data structures */
04504         CreateSharedMemoryAndSemaphores(false, 0);
04505 
04506         AuxiliaryProcessMain(argc - 2, argv + 2); /* does not return */
04507     }
04508     if (strcmp(argv[1], "--forkavlauncher") == 0)
04509     {
04510         /* Close the postmaster's sockets */
04511         ClosePostmasterPorts(false);
04512 
04513         /* Restore basic shared memory pointers */
04514         InitShmemAccess(UsedShmemSegAddr);
04515 
04516         /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
04517         InitProcess();
04518 
04519         /* Attach process to shared data structures */
04520         CreateSharedMemoryAndSemaphores(false, 0);
04521 
04522         AutoVacLauncherMain(argc - 2, argv + 2); /* does not return */
04523     }
04524     if (strcmp(argv[1], "--forkavworker") == 0)
04525     {
04526         /* Close the postmaster's sockets */
04527         ClosePostmasterPorts(false);
04528 
04529         /* Restore basic shared memory pointers */
04530         InitShmemAccess(UsedShmemSegAddr);
04531 
04532         /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
04533         InitProcess();
04534 
04535         /* Attach process to shared data structures */
04536         CreateSharedMemoryAndSemaphores(false, 0);
04537 
04538         AutoVacWorkerMain(argc - 2, argv + 2); /* does not return */
04539     }
04540     if (strncmp(argv[1], "--forkbgworker=", 15) == 0)
04541     {
04542         int         cookie;
04543 
04544         /* Close the postmaster's sockets */
04545         ClosePostmasterPorts(false);
04546 
04547         /* Restore basic shared memory pointers */
04548         InitShmemAccess(UsedShmemSegAddr);
04549 
04550         /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
04551         InitProcess();
04552 
04553         /* Attach process to shared data structures */
04554         CreateSharedMemoryAndSemaphores(false, 0);
04555 
04556         cookie = atoi(argv[1] + 15);
04557         MyBgworkerEntry = find_bgworker_entry(cookie);
04558         do_start_bgworker();
04559     }
04560     if (strcmp(argv[1], "--forkarch") == 0)
04561     {
04562         /* Close the postmaster's sockets */
04563         ClosePostmasterPorts(false);
04564 
04565         /* Do not want to attach to shared memory */
04566 
04567         PgArchiverMain(argc, argv); /* does not return */
04568     }
04569     if (strcmp(argv[1], "--forkcol") == 0)
04570     {
04571         /* Close the postmaster's sockets */
04572         ClosePostmasterPorts(false);
04573 
04574         /* Do not want to attach to shared memory */
04575 
04576         PgstatCollectorMain(argc, argv); /* does not return */
04577     }
04578     if (strcmp(argv[1], "--forklog") == 0)
04579     {
04580         /* Close the postmaster's sockets */
04581         ClosePostmasterPorts(true);
04582 
04583         /* Do not want to attach to shared memory */
04584 
04585         SysLoggerMain(argc, argv); /* does not return */
04586     }
04587 
04588     abort();                    /* shouldn't get here */
04589 }
04590 #endif   /* EXEC_BACKEND */
04591 
04592 
04593 /*
04594  * ExitPostmaster -- cleanup
04595  *
04596  * Do NOT call exit() directly --- always go through here!
04597  */
04598 static void
04599 ExitPostmaster(int status)
04600 {
04601     /* should cleanup shared memory and kill all backends */
04602 
04603     /*
04604      * Not sure of the semantics here.  When the Postmaster dies, should the
04605      * backends all be killed? probably not.
04606      *
04607      * MUST     -- vadim 05-10-1999
04608      */
04609 
04610     proc_exit(status);
04611 }
04612 
04613 /*
04614  * sigusr1_handler - handle signal conditions from child processes
04615  */
04616 static void
04617 sigusr1_handler(SIGNAL_ARGS)
04618 {
04619     int         save_errno = errno;
04620 
04621     PG_SETMASK(&BlockSig);
04622 
04623     /*
04624      * RECOVERY_STARTED and BEGIN_HOT_STANDBY signals are ignored in
04625      * unexpected states. If the startup process quickly starts up, completes
04626      * recovery, exits, we might process the death of the startup process
04627      * first. We don't want to go back to recovery in that case.
04628      */
04629     if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_STARTED) &&
04630         pmState == PM_STARTUP && Shutdown == NoShutdown)
04631     {
04632         /* WAL redo has started. We're out of reinitialization. */
04633         FatalError = false;
04634 
04635         /*
04636          * Crank up the background tasks.  It doesn't matter if this fails,
04637          * we'll just try again later.
04638          */
04639         Assert(CheckpointerPID == 0);
04640         CheckpointerPID = StartCheckpointer();
04641         Assert(BgWriterPID == 0);
04642         BgWriterPID = StartBackgroundWriter();
04643 
04644         pmState = PM_RECOVERY;
04645     }
04646     if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) &&
04647         pmState == PM_RECOVERY && Shutdown == NoShutdown)
04648     {
04649         /*
04650          * Likewise, start other special children as needed.
04651          */
04652         Assert(PgStatPID == 0);
04653         PgStatPID = pgstat_start();
04654 
04655         ereport(LOG,
04656         (errmsg("database system is ready to accept read only connections")));
04657 
04658         pmState = PM_HOT_STANDBY;
04659 
04660         /* Some workers may be scheduled to start now */
04661         StartOneBackgroundWorker();
04662     }
04663 
04664     if (CheckPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER) &&
04665         PgArchPID != 0)
04666     {
04667         /*
04668          * Send SIGUSR1 to archiver process, to wake it up and begin archiving
04669          * next transaction log file.
04670          */
04671         signal_child(PgArchPID, SIGUSR1);
04672     }
04673 
04674     if (CheckPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE) &&
04675         SysLoggerPID != 0)
04676     {
04677         /* Tell syslogger to rotate logfile */
04678         signal_child(SysLoggerPID, SIGUSR1);
04679     }
04680 
04681     if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER) &&
04682         Shutdown == NoShutdown)
04683     {
04684         /*
04685          * Start one iteration of the autovacuum daemon, even if autovacuuming
04686          * is nominally not enabled.  This is so we can have an active defense
04687          * against transaction ID wraparound.  We set a flag for the main loop
04688          * to do it rather than trying to do it here --- this is because the
04689          * autovac process itself may send the signal, and we want to handle
04690          * that by launching another iteration as soon as the current one
04691          * completes.
04692          */
04693         start_autovac_launcher = true;
04694     }
04695 
04696     if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER) &&
04697         Shutdown == NoShutdown)
04698     {
04699         /* The autovacuum launcher wants us to start a worker process. */
04700         StartAutovacuumWorker();
04701     }
04702 
04703     if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER) &&
04704         WalReceiverPID == 0 &&
04705         (pmState == PM_STARTUP || pmState == PM_RECOVERY ||
04706          pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY) &&
04707         Shutdown == NoShutdown)
04708     {
04709         /* Startup Process wants us to start the walreceiver process. */
04710         WalReceiverPID = StartWalReceiver();
04711     }
04712 
04713     if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE) &&
04714         (pmState == PM_WAIT_BACKUP || pmState == PM_WAIT_BACKENDS))
04715     {
04716         /* Advance postmaster's state machine */
04717         PostmasterStateMachine();
04718     }
04719 
04720     if (CheckPromoteSignal() && StartupPID != 0 &&
04721         (pmState == PM_STARTUP || pmState == PM_RECOVERY ||
04722          pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY))
04723     {
04724         /* Tell startup process to finish recovery */
04725         signal_child(StartupPID, SIGUSR2);
04726     }
04727 
04728     PG_SETMASK(&UnBlockSig);
04729 
04730     errno = save_errno;
04731 }
04732 
04733 /*
04734  * SIGTERM or SIGQUIT while processing startup packet.
04735  * Clean up and exit(1).
04736  *
04737  * XXX: possible future improvement: try to send a message indicating
04738  * why we are disconnecting.  Problem is to be sure we don't block while
04739  * doing so, nor mess up SSL initialization.  In practice, if the client
04740  * has wedged here, it probably couldn't do anything with the message anyway.
04741  */
04742 static void
04743 startup_die(SIGNAL_ARGS)
04744 {
04745     proc_exit(1);
04746 }
04747 
04748 /*
04749  * Dummy signal handler
04750  *
04751  * We use this for signals that we don't actually use in the postmaster,
04752  * but we do use in backends.  If we were to SIG_IGN such signals in the
04753  * postmaster, then a newly started backend might drop a signal that arrives
04754  * before it's able to reconfigure its signal processing.  (See notes in
04755  * tcop/postgres.c.)
04756  */
04757 static void
04758 dummy_handler(SIGNAL_ARGS)
04759 {
04760 }
04761 
04762 /*
04763  * Timeout while processing startup packet.
04764  * As for startup_die(), we clean up and exit(1).
04765  */
04766 static void
04767 StartupPacketTimeoutHandler(void)
04768 {
04769     proc_exit(1);
04770 }
04771 
04772 
04773 /*
04774  * RandomSalt
04775  */
04776 static void
04777 RandomSalt(char *md5Salt)
04778 {
04779     long        rand;
04780 
04781     /*
04782      * We use % 255, sacrificing one possible byte value, so as to ensure that
04783      * all bits of the random() value participate in the result. While at it,
04784      * add one to avoid generating any null bytes.
04785      */
04786     rand = PostmasterRandom();
04787     md5Salt[0] = (rand % 255) + 1;
04788     rand = PostmasterRandom();
04789     md5Salt[1] = (rand % 255) + 1;
04790     rand = PostmasterRandom();
04791     md5Salt[2] = (rand % 255) + 1;
04792     rand = PostmasterRandom();
04793     md5Salt[3] = (rand % 255) + 1;
04794 }
04795 
04796 /*
04797  * PostmasterRandom
04798  */
04799 static long
04800 PostmasterRandom(void)
04801 {
04802     /*
04803      * Select a random seed at the time of first receiving a request.
04804      */
04805     if (random_seed == 0)
04806     {
04807         do
04808         {
04809             struct timeval random_stop_time;
04810 
04811             gettimeofday(&random_stop_time, NULL);
04812 
04813             /*
04814              * We are not sure how much precision is in tv_usec, so we swap
04815              * the high and low 16 bits of 'random_stop_time' and XOR them
04816              * with 'random_start_time'. On the off chance that the result is
04817              * 0, we loop until it isn't.
04818              */
04819             random_seed = random_start_time.tv_usec ^
04820                 ((random_stop_time.tv_usec << 16) |
04821                  ((random_stop_time.tv_usec >> 16) & 0xffff));
04822         }
04823         while (random_seed == 0);
04824 
04825         srandom(random_seed);
04826     }
04827 
04828     return random();
04829 }
04830 
04831 /*
04832  * Count up number of worker processes that did not request backend connections
04833  * See SignalUnconnectedWorkers for why this is interesting.
04834  */
04835 static int
04836 CountUnconnectedWorkers(void)
04837 {
04838     slist_iter  iter;
04839     int         cnt = 0;
04840 
04841     slist_foreach(iter, &BackgroundWorkerList)
04842     {
04843         RegisteredBgWorker *rw;
04844 
04845         rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
04846 
04847         if (rw->rw_pid == 0)
04848             continue;
04849         /* ignore connected workers */
04850         if (rw->rw_backend != NULL)
04851             continue;
04852 
04853         cnt++;
04854     }
04855     return cnt;
04856 }
04857 
04858 /*
04859  * Count up number of child processes of specified types (dead_end chidren
04860  * are always excluded).
04861  */
04862 static int
04863 CountChildren(int target)
04864 {
04865     dlist_iter  iter;
04866     int         cnt = 0;
04867 
04868     dlist_foreach(iter, &BackendList)
04869     {
04870         Backend    *bp = dlist_container(Backend, elem, iter.cur);
04871 
04872         if (bp->dead_end)
04873             continue;
04874 
04875         /*
04876          * Since target == BACKEND_TYPE_ALL is the most common case, we test
04877          * it first and avoid touching shared memory for every child.
04878          */
04879         if (target != BACKEND_TYPE_ALL)
04880         {
04881             /*
04882              * Assign bkend_type for any recently announced WAL Sender
04883              * processes.
04884              */
04885             if (bp->bkend_type == BACKEND_TYPE_NORMAL &&
04886                 IsPostmasterChildWalSender(bp->child_slot))
04887                 bp->bkend_type = BACKEND_TYPE_WALSND;
04888 
04889             if (!(target & bp->bkend_type))
04890                 continue;
04891         }
04892 
04893         cnt++;
04894     }
04895     return cnt;
04896 }
04897 
04898 
04899 /*
04900  * StartChildProcess -- start an auxiliary process for the postmaster
04901  *
04902  * xlop determines what kind of child will be started.  All child types
04903  * initially go to AuxiliaryProcessMain, which will handle common setup.
04904  *
04905  * Return value of StartChildProcess is subprocess' PID, or 0 if failed
04906  * to start subprocess.
04907  */
04908 static pid_t
04909 StartChildProcess(AuxProcType type)
04910 {
04911     pid_t       pid;
04912     char       *av[10];
04913     int         ac = 0;
04914     char        typebuf[32];
04915 
04916     /*
04917      * Set up command-line arguments for subprocess
04918      */
04919     av[ac++] = "postgres";
04920 
04921 #ifdef EXEC_BACKEND
04922     av[ac++] = "--forkboot";
04923     av[ac++] = NULL;            /* filled in by postmaster_forkexec */
04924 #endif
04925 
04926     snprintf(typebuf, sizeof(typebuf), "-x%d", type);
04927     av[ac++] = typebuf;
04928 
04929     av[ac] = NULL;
04930     Assert(ac < lengthof(av));
04931 
04932 #ifdef EXEC_BACKEND
04933     pid = postmaster_forkexec(ac, av);
04934 #else                           /* !EXEC_BACKEND */
04935     pid = fork_process();
04936 
04937     if (pid == 0)               /* child */
04938     {
04939         IsUnderPostmaster = true;       /* we are a postmaster subprocess now */
04940 
04941         /* Close the postmaster's sockets */
04942         ClosePostmasterPorts(false);
04943 
04944         /* Lose the postmaster's on-exit routines and port connections */
04945         on_exit_reset();
04946 
04947         /* Release postmaster's working memory context */
04948         MemoryContextSwitchTo(TopMemoryContext);
04949         MemoryContextDelete(PostmasterContext);
04950         PostmasterContext = NULL;
04951 
04952         AuxiliaryProcessMain(ac, av);
04953         ExitPostmaster(0);
04954     }
04955 #endif   /* EXEC_BACKEND */
04956 
04957     if (pid < 0)
04958     {
04959         /* in parent, fork failed */
04960         int         save_errno = errno;
04961 
04962         errno = save_errno;
04963         switch (type)
04964         {
04965             case StartupProcess:
04966                 ereport(LOG,
04967                         (errmsg("could not fork startup process: %m")));
04968                 break;
04969             case BgWriterProcess:
04970                 ereport(LOG,
04971                    (errmsg("could not fork background writer process: %m")));
04972                 break;
04973             case CheckpointerProcess:
04974                 ereport(LOG,
04975                         (errmsg("could not fork checkpointer process: %m")));
04976                 break;
04977             case WalWriterProcess:
04978                 ereport(LOG,
04979                         (errmsg("could not fork WAL writer process: %m")));
04980                 break;
04981             case WalReceiverProcess:
04982                 ereport(LOG,
04983                         (errmsg("could not fork WAL receiver process: %m")));
04984                 break;
04985             default:
04986                 ereport(LOG,
04987                         (errmsg("could not fork process: %m")));
04988                 break;
04989         }
04990 
04991         /*
04992          * fork failure is fatal during startup, but there's no need to choke
04993          * immediately if starting other child types fails.
04994          */
04995         if (type == StartupProcess)
04996             ExitPostmaster(1);
04997         return 0;
04998     }
04999 
05000     /*
05001      * in parent, successful fork
05002      */
05003     return pid;
05004 }
05005 
05006 /*
05007  * StartAutovacuumWorker
05008  *      Start an autovac worker process.
05009  *
05010  * This function is here because it enters the resulting PID into the
05011  * postmaster's private backends list.
05012  *
05013  * NB -- this code very roughly matches BackendStartup.
05014  */
05015 static void
05016 StartAutovacuumWorker(void)
05017 {
05018     Backend    *bn;
05019 
05020     /*
05021      * If not in condition to run a process, don't try, but handle it like a
05022      * fork failure.  This does not normally happen, since the signal is only
05023      * supposed to be sent by autovacuum launcher when it's OK to do it, but
05024      * we have to check to avoid race-condition problems during DB state
05025      * changes.
05026      */
05027     if (canAcceptConnections() == CAC_OK)
05028     {
05029         bn = (Backend *) malloc(sizeof(Backend));
05030         if (bn)
05031         {
05032             /*
05033              * Compute the cancel key that will be assigned to this session.
05034              * We probably don't need cancel keys for autovac workers, but
05035              * we'd better have something random in the field to prevent
05036              * unfriendly people from sending cancels to them.
05037              */
05038             MyCancelKey = PostmasterRandom();
05039             bn->cancel_key = MyCancelKey;
05040 
05041             /* Autovac workers are not dead_end and need a child slot */
05042             bn->dead_end = false;
05043             bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
05044 
05045             bn->pid = StartAutoVacWorker();
05046             if (bn->pid > 0)
05047             {
05048                 bn->bkend_type = BACKEND_TYPE_AUTOVAC;
05049                 dlist_push_head(&BackendList, &bn->elem);
05050 #ifdef EXEC_BACKEND
05051                 ShmemBackendArrayAdd(bn);
05052 #endif
05053                 /* all OK */
05054                 return;
05055             }
05056 
05057             /*
05058              * fork failed, fall through to report -- actual error message was
05059              * logged by StartAutoVacWorker
05060              */
05061             (void) ReleasePostmasterChildSlot(bn->child_slot);
05062             free(bn);
05063         }
05064         else
05065             ereport(LOG,
05066                     (errcode(ERRCODE_OUT_OF_MEMORY),
05067                      errmsg("out of memory")));
05068     }
05069 
05070     /*
05071      * Report the failure to the launcher, if it's running.  (If it's not, we
05072      * might not even be connected to shared memory, so don't try to call
05073      * AutoVacWorkerFailed.)  Note that we also need to signal it so that it
05074      * responds to the condition, but we don't do that here, instead waiting
05075      * for ServerLoop to do it.  This way we avoid a ping-pong signalling in
05076      * quick succession between the autovac launcher and postmaster in case
05077      * things get ugly.
05078      */
05079     if (AutoVacPID != 0)
05080     {
05081         AutoVacWorkerFailed();
05082         avlauncher_needs_signal = true;
05083     }
05084 }
05085 
05086 /*
05087  * Create the opts file
05088  */
05089 static bool
05090 CreateOptsFile(int argc, char *argv[], char *fullprogname)
05091 {
05092     FILE       *fp;
05093     int         i;
05094 
05095 #define OPTS_FILE   "postmaster.opts"
05096 
05097     if ((fp = fopen(OPTS_FILE, "w")) == NULL)
05098     {
05099         elog(LOG, "could not create file \"%s\": %m", OPTS_FILE);
05100         return false;
05101     }
05102 
05103     fprintf(fp, "%s", fullprogname);
05104     for (i = 1; i < argc; i++)
05105         fprintf(fp, " \"%s\"", argv[i]);
05106     fputs("\n", fp);
05107 
05108     if (fclose(fp))
05109     {
05110         elog(LOG, "could not write file \"%s\": %m", OPTS_FILE);
05111         return false;
05112     }
05113 
05114     return true;
05115 }
05116 
05117 
05118 /*
05119  * MaxLivePostmasterChildren
05120  *
05121  * This reports the number of entries needed in per-child-process arrays
05122  * (the PMChildFlags array, and if EXEC_BACKEND the ShmemBackendArray).
05123  * These arrays include regular backends, autovac workers, walsenders
05124  * and background workers, but not special children nor dead_end children.
05125  * This allows the arrays to have a fixed maximum size, to wit the same
05126  * too-many-children limit enforced by canAcceptConnections().  The exact value
05127  * isn't too critical as long as it's more than MaxBackends.
05128  */
05129 int
05130 MaxLivePostmasterChildren(void)
05131 {
05132     return 2 * (MaxConnections + autovacuum_max_workers + 1 +
05133                 GetNumRegisteredBackgroundWorkers(0));
05134 }
05135 
05136 /*
05137  * Register a new background worker.
05138  *
05139  * This can only be called in the _PG_init function of a module library
05140  * that's loaded by shared_preload_libraries; otherwise it has no effect.
05141  */
05142 void
05143 RegisterBackgroundWorker(BackgroundWorker *worker)
05144 {
05145     RegisteredBgWorker *rw;
05146     int         namelen = strlen(worker->bgw_name);
05147     static int  maxworkers;
05148     static int  numworkers = 0;
05149 
05150 #ifdef EXEC_BACKEND
05151 
05152     /*
05153      * Use 1 here, not 0, to avoid confusing a possible bogus cookie read by
05154      * atoi() in SubPostmasterMain.
05155      */
05156     static int  BackgroundWorkerCookie = 1;
05157 #endif
05158 
05159     /* initialize upper limit on first call */
05160     if (numworkers == 0)
05161         maxworkers = MAX_BACKENDS -
05162             (MaxConnections + autovacuum_max_workers + 1);
05163 
05164     if (!IsUnderPostmaster)
05165         ereport(LOG,
05166             (errmsg("registering background worker: %s", worker->bgw_name)));
05167 
05168     if (!process_shared_preload_libraries_in_progress)
05169     {
05170         if (!IsUnderPostmaster)
05171             ereport(LOG,
05172                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
05173                      errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
05174                             worker->bgw_name)));
05175         return;
05176     }
05177 
05178     /* sanity check for flags */
05179     if (worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)
05180     {
05181         if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
05182         {
05183             if (!IsUnderPostmaster)
05184                 ereport(LOG,
05185                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
05186                          errmsg("background worker \"%s\": must attach to shared memory in order to request a database connection",
05187                                 worker->bgw_name)));
05188             return;
05189         }
05190 
05191         if (worker->bgw_start_time == BgWorkerStart_PostmasterStart)
05192         {
05193             if (!IsUnderPostmaster)
05194                 ereport(LOG,
05195                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
05196                          errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
05197                                 worker->bgw_name)));
05198             return;
05199         }
05200 
05201         /* XXX other checks? */
05202     }
05203 
05204     if ((worker->bgw_restart_time < 0 &&
05205          worker->bgw_restart_time != BGW_NEVER_RESTART) ||
05206         (worker->bgw_restart_time > USECS_PER_DAY / 1000))
05207     {
05208         if (!IsUnderPostmaster)
05209             ereport(LOG,
05210                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
05211                  errmsg("background worker \"%s\": invalid restart interval",
05212                         worker->bgw_name)));
05213         return;
05214     }
05215 
05216     /*
05217      * Enforce maximum number of workers.  Note this is overly restrictive:
05218      * we could allow more non-shmem-connected workers, because these don't
05219      * count towards the MAX_BACKENDS limit elsewhere.  This doesn't really
05220      * matter for practical purposes; several million processes would need to
05221      * run on a single server.
05222      */
05223     if (++numworkers > maxworkers)
05224     {
05225         ereport(LOG,
05226                 (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
05227                  errmsg("too many background workers"),
05228                  errdetail("Up to %d background workers can be registered with the current settings.",
05229                            maxworkers)));
05230         return;
05231     }
05232 
05233     /*
05234      * Copy the registration data into the registered workers list.
05235      */
05236     rw = malloc(sizeof(RegisteredBgWorker) + namelen + 1);
05237     if (rw == NULL)
05238     {
05239         ereport(LOG,
05240                 (errcode(ERRCODE_OUT_OF_MEMORY),
05241                  errmsg("out of memory")));
05242         return;
05243     }
05244 
05245     rw->rw_worker = *worker;
05246     rw->rw_worker.bgw_name = ((char *) rw) + sizeof(RegisteredBgWorker);
05247     strlcpy(rw->rw_worker.bgw_name, worker->bgw_name, namelen + 1);
05248 
05249     rw->rw_backend = NULL;
05250     rw->rw_pid = 0;
05251     rw->rw_child_slot = 0;
05252     rw->rw_crashed_at = 0;
05253 #ifdef EXEC_BACKEND
05254     rw->rw_cookie = BackgroundWorkerCookie++;
05255 #endif
05256 
05257     slist_push_head(&BackgroundWorkerList, &rw->rw_lnode);
05258 }
05259 
05260 /*
05261  * Connect background worker to a database.
05262  */
05263 void
05264 BackgroundWorkerInitializeConnection(char *dbname, char *username)
05265 {
05266     BackgroundWorker *worker = MyBgworkerEntry;
05267 
05268     /* XXX is this the right errcode? */
05269     if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
05270         ereport(FATAL,
05271                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
05272                  errmsg("database connection requirement not indicated during registration")));
05273 
05274     InitPostgres(dbname, InvalidOid, username, NULL);
05275 
05276     /* it had better not gotten out of "init" mode yet */
05277     if (!IsInitProcessingMode())
05278         ereport(ERROR,
05279                 (errmsg("invalid processing mode in bgworker")));
05280     SetProcessingMode(NormalProcessing);
05281 }
05282 
05283 /*
05284  * Block/unblock signals in a background worker
05285  */
05286 void
05287 BackgroundWorkerBlockSignals(void)
05288 {
05289     PG_SETMASK(&BlockSig);
05290 }
05291 
05292 void
05293 BackgroundWorkerUnblockSignals(void)
05294 {
05295     PG_SETMASK(&UnBlockSig);
05296 }
05297 
05298 #ifdef EXEC_BACKEND
05299 static BackgroundWorker *
05300 find_bgworker_entry(int cookie)
05301 {
05302     slist_iter  iter;
05303 
05304     slist_foreach(iter, &BackgroundWorkerList)
05305     {
05306         RegisteredBgWorker *rw;
05307 
05308         rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
05309         if (rw->rw_cookie == cookie)
05310             return &rw->rw_worker;
05311     }
05312 
05313     return NULL;
05314 }
05315 #endif
05316 
05317 static void
05318 bgworker_quickdie(SIGNAL_ARGS)
05319 {
05320     sigaddset(&BlockSig, SIGQUIT);      /* prevent nested calls */
05321     PG_SETMASK(&BlockSig);
05322 
05323     /*
05324      * We DO NOT want to run proc_exit() callbacks -- we're here because
05325      * shared memory may be corrupted, so we don't want to try to clean up our
05326      * transaction.  Just nail the windows shut and get out of town.  Now that
05327      * there's an atexit callback to prevent third-party code from breaking
05328      * things by calling exit() directly, we have to reset the callbacks
05329      * explicitly to make this work as intended.
05330      */
05331     on_exit_reset();
05332 
05333     /*
05334      * Note we do exit(0) here, not exit(2) like quickdie.  The reason is that
05335      * we don't want to be seen this worker as independently crashed, because
05336      * then postmaster would delay restarting it again afterwards.  If some
05337      * idiot DBA manually sends SIGQUIT to a random bgworker, the "dead man
05338      * switch" will ensure that postmaster sees this as a crash.
05339      */
05340     exit(0);
05341 }
05342 
05343 /*
05344  * Standard SIGTERM handler for background workers
05345  */
05346 static void
05347 bgworker_die(SIGNAL_ARGS)
05348 {
05349     PG_SETMASK(&BlockSig);
05350 
05351     ereport(FATAL,
05352             (errcode(ERRCODE_ADMIN_SHUTDOWN),
05353              errmsg("terminating background worker \"%s\" due to administrator command",
05354                     MyBgworkerEntry->bgw_name)));
05355 }
05356 
05357 /*
05358  * Standard SIGUSR1 handler for unconnected workers
05359  *
05360  * Here, we want to make sure an unconnected worker will at least heed
05361  * latch activity.
05362  */
05363 static void
05364 bgworker_sigusr1_handler(SIGNAL_ARGS)
05365 {
05366     int         save_errno = errno;
05367 
05368     latch_sigusr1_handler();
05369 
05370     errno = save_errno;
05371 }
05372 
05373 static void
05374 do_start_bgworker(void)
05375 {
05376     sigjmp_buf  local_sigjmp_buf;
05377     char        buf[MAXPGPATH];
05378     BackgroundWorker *worker = MyBgworkerEntry;
05379 
05380     if (worker == NULL)
05381         elog(FATAL, "unable to find bgworker entry");
05382 
05383     /* we are a postmaster subprocess now */
05384     IsUnderPostmaster = true;
05385     IsBackgroundWorker = true;
05386 
05387     /* reset MyProcPid */
05388     MyProcPid = getpid();
05389 
05390     /* record Start Time for logging */
05391     MyStartTime = time(NULL);
05392 
05393     /* Identify myself via ps */
05394     snprintf(buf, MAXPGPATH, "bgworker: %s", worker->bgw_name);
05395     init_ps_display(buf, "", "", "");
05396 
05397     SetProcessingMode(InitProcessing);
05398 
05399     /* Apply PostAuthDelay */
05400     if (PostAuthDelay > 0)
05401         pg_usleep(PostAuthDelay * 1000000L);
05402 
05403     /*
05404      * If possible, make this process a group leader, so that the postmaster
05405      * can signal any child processes too.
05406      */
05407 #ifdef HAVE_SETSID
05408     if (setsid() < 0)
05409         elog(FATAL, "setsid() failed: %m");
05410 #endif
05411 
05412     /*
05413      * Set up signal handlers.
05414      */
05415     if (worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)
05416     {
05417         /*
05418          * SIGINT is used to signal canceling the current action
05419          */
05420         pqsignal(SIGINT, StatementCancelHandler);
05421         pqsignal(SIGUSR1, procsignal_sigusr1_handler);
05422         pqsignal(SIGFPE, FloatExceptionHandler);
05423 
05424         /* XXX Any other handlers needed here? */
05425     }
05426     else
05427     {
05428         pqsignal(SIGINT, SIG_IGN);
05429         pqsignal(SIGUSR1, bgworker_sigusr1_handler);
05430         pqsignal(SIGFPE, SIG_IGN);
05431     }
05432 
05433     /* SIGTERM and SIGHUP are configurable */
05434     if (worker->bgw_sigterm)
05435         pqsignal(SIGTERM, worker->bgw_sigterm);
05436     else
05437         pqsignal(SIGTERM, bgworker_die);
05438 
05439     if (worker->bgw_sighup)
05440         pqsignal(SIGHUP, worker->bgw_sighup);
05441     else
05442         pqsignal(SIGHUP, SIG_IGN);
05443 
05444     pqsignal(SIGQUIT, bgworker_quickdie);
05445     InitializeTimeouts();       /* establishes SIGALRM handler */
05446 
05447     pqsignal(SIGPIPE, SIG_IGN);
05448     pqsignal(SIGUSR2, SIG_IGN);
05449     pqsignal(SIGCHLD, SIG_DFL);
05450 
05451     /*
05452      * If an exception is encountered, processing resumes here.
05453      *
05454      * See notes in postgres.c about the design of this coding.
05455      */
05456     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
05457     {
05458         /* Since not using PG_TRY, must reset error stack by hand */
05459         error_context_stack = NULL;
05460 
05461         /* Prevent interrupts while cleaning up */
05462         HOLD_INTERRUPTS();
05463 
05464         /* Report the error to the server log */
05465         EmitErrorReport();
05466 
05467         /*
05468          * Do we need more cleanup here?  For shmem-connected bgworkers, we
05469          * will call InitProcess below, which will install ProcKill as exit
05470          * callback.  That will take care of releasing locks, etc.
05471          */
05472 
05473         /* and go away */
05474         proc_exit(1);
05475     }
05476 
05477     /* We can now handle ereport(ERROR) */
05478     PG_exception_stack = &local_sigjmp_buf;
05479 
05480     /* Early initialization */
05481     BaseInit();
05482 
05483     /*
05484      * If necessary, create a per-backend PGPROC struct in shared memory,
05485      * except in the EXEC_BACKEND case where this was done in
05486      * SubPostmasterMain. We must do this before we can use LWLocks (and in
05487      * the EXEC_BACKEND case we already had to do some stuff with LWLocks).
05488      */
05489 #ifndef EXEC_BACKEND
05490     if (worker->bgw_flags & BGWORKER_SHMEM_ACCESS)
05491         InitProcess();
05492 #endif
05493 
05494     /*
05495      * Note that in normal processes, we would call InitPostgres here.  For a
05496      * worker, however, we don't know what database to connect to, yet; so we
05497      * need to wait until the user code does it via
05498      * BackgroundWorkerInitializeConnection().
05499      */
05500 
05501     /*
05502      * Now invoke the user-defined worker code
05503      */
05504     worker->bgw_main(worker->bgw_main_arg);
05505 
05506     /* ... and if it returns, we're done */
05507     proc_exit(0);
05508 }
05509 
05510 /*
05511  * Return the number of background workers registered that have at least
05512  * one of the passed flag bits set.
05513  */
05514 static int
05515 GetNumRegisteredBackgroundWorkers(int flags)
05516 {
05517     slist_iter  iter;
05518     int         count = 0;
05519 
05520     slist_foreach(iter, &BackgroundWorkerList)
05521     {
05522         RegisteredBgWorker *rw;
05523 
05524         rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
05525 
05526         if (flags != 0 &&
05527             !(rw->rw_worker.bgw_flags & flags))
05528             continue;
05529 
05530         count++;
05531     }
05532 
05533     return count;
05534 }
05535 
05536 /*
05537  * Return the number of bgworkers that need to have PGPROC entries.
05538  */
05539 int
05540 GetNumShmemAttachedBgworkers(void)
05541 {
05542     return GetNumRegisteredBackgroundWorkers(BGWORKER_SHMEM_ACCESS);
05543 }
05544 
05545 #ifdef EXEC_BACKEND
05546 static pid_t
05547 bgworker_forkexec(int cookie)
05548 {
05549     char       *av[10];
05550     int         ac = 0;
05551     char        forkav[MAXPGPATH];
05552 
05553     snprintf(forkav, MAXPGPATH, "--forkbgworker=%d", cookie);
05554 
05555     av[ac++] = "postgres";
05556     av[ac++] = forkav;
05557     av[ac++] = NULL;            /* filled in by postmaster_forkexec */
05558     av[ac] = NULL;
05559 
05560     Assert(ac < lengthof(av));
05561 
05562     return postmaster_forkexec(ac, av);
05563 }
05564 #endif
05565 
05566 /*
05567  * Start a new bgworker.
05568  * Starting time conditions must have been checked already.
05569  *
05570  * This code is heavily based on autovacuum.c, q.v.
05571  */
05572 static void
05573 start_bgworker(RegisteredBgWorker *rw)
05574 {
05575     pid_t       worker_pid;
05576 
05577     ereport(LOG,
05578             (errmsg("starting background worker process \"%s\"",
05579                     rw->rw_worker.bgw_name)));
05580 
05581 #ifdef EXEC_BACKEND
05582     switch ((worker_pid = bgworker_forkexec(rw->rw_cookie)))
05583 #else
05584     switch ((worker_pid = fork_process()))
05585 #endif
05586     {
05587         case -1:
05588             ereport(LOG,
05589                     (errmsg("could not fork worker process: %m")));
05590             return;
05591 
05592 #ifndef EXEC_BACKEND
05593         case 0:
05594             /* in postmaster child ... */
05595             /* Close the postmaster's sockets */
05596             ClosePostmasterPorts(false);
05597 
05598             /* Lose the postmaster's on-exit routines */
05599             on_exit_reset();
05600 
05601             /* Do NOT release postmaster's working memory context */
05602 
05603             MyBgworkerEntry = &rw->rw_worker;
05604             do_start_bgworker();
05605             break;
05606 #endif
05607         default:
05608             rw->rw_pid = worker_pid;
05609             if (rw->rw_backend)
05610                 rw->rw_backend->pid = rw->rw_pid;
05611     }
05612 }
05613 
05614 /*
05615  * Does the current postmaster state require starting a worker with the
05616  * specified start_time?
05617  */
05618 static bool
05619 bgworker_should_start_now(BgWorkerStartTime start_time)
05620 {
05621     switch (pmState)
05622     {
05623         case PM_NO_CHILDREN:
05624         case PM_WAIT_DEAD_END:
05625         case PM_SHUTDOWN_2:
05626         case PM_SHUTDOWN:
05627         case PM_WAIT_BACKENDS:
05628         case PM_WAIT_READONLY:
05629         case PM_WAIT_BACKUP:
05630             break;
05631 
05632         case PM_RUN:
05633             if (start_time == BgWorkerStart_RecoveryFinished)
05634                 return true;
05635             /* fall through */
05636 
05637         case PM_HOT_STANDBY:
05638             if (start_time == BgWorkerStart_ConsistentState)
05639                 return true;
05640             /* fall through */
05641 
05642         case PM_RECOVERY:
05643         case PM_STARTUP:
05644         case PM_INIT:
05645             if (start_time == BgWorkerStart_PostmasterStart)
05646                 return true;
05647             /* fall through */
05648 
05649     }
05650 
05651     return false;
05652 }
05653 
05654 /*
05655  * Allocate the Backend struct for a connected background worker, but don't
05656  * add it to the list of backends just yet.
05657  *
05658  * Some info from the Backend is copied into the passed rw.
05659  */
05660 static bool
05661 assign_backendlist_entry(RegisteredBgWorker *rw)
05662 {
05663     Backend    *bn = malloc(sizeof(Backend));
05664 
05665     if (bn == NULL)
05666     {
05667         ereport(LOG,
05668                 (errcode(ERRCODE_OUT_OF_MEMORY),
05669                  errmsg("out of memory")));
05670 
05671         /*
05672          * The worker didn't really crash, but setting this nonzero makes
05673          * postmaster wait a bit before attempting to start it again; if it
05674          * tried again right away, most likely it'd find itself under the same
05675          * memory pressure.
05676          */
05677         rw->rw_crashed_at = GetCurrentTimestamp();
05678         return false;
05679     }
05680 
05681     /*
05682      * Compute the cancel key that will be assigned to this session. We
05683      * probably don't need cancel keys for background workers, but we'd better
05684      * have something random in the field to prevent unfriendly people from
05685      * sending cancels to them.
05686      */
05687     MyCancelKey = PostmasterRandom();
05688     bn->cancel_key = MyCancelKey;
05689 
05690     bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
05691     bn->bkend_type = BACKEND_TYPE_BGWORKER;
05692     bn->dead_end = false;
05693 
05694     rw->rw_backend = bn;
05695     rw->rw_child_slot = bn->child_slot;
05696 
05697     return true;
05698 }
05699 
05700 /*
05701  * If the time is right, start one background worker.
05702  *
05703  * As a side effect, the bgworker control variables are set or reset whenever
05704  * there are more workers to start after this one, and whenever the overall
05705  * system state requires it.
05706  */
05707 static void
05708 StartOneBackgroundWorker(void)
05709 {
05710     slist_iter  iter;
05711     TimestampTz now = 0;
05712 
05713     if (FatalError)
05714     {
05715         StartWorkerNeeded = false;
05716         HaveCrashedWorker = false;
05717         return;                 /* not yet */
05718     }
05719 
05720     HaveCrashedWorker = false;
05721 
05722     slist_foreach(iter, &BackgroundWorkerList)
05723     {
05724         RegisteredBgWorker *rw;
05725 
05726         rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
05727 
05728         /* already running? */
05729         if (rw->rw_pid != 0)
05730             continue;
05731 
05732         /*
05733          * If this worker has crashed previously, maybe it needs to be
05734          * restarted (unless on registration it specified it doesn't want to
05735          * be restarted at all).  Check how long ago did a crash last happen.
05736          * If the last crash is too recent, don't start it right away; let it
05737          * be restarted once enough time has passed.
05738          */
05739         if (rw->rw_crashed_at != 0)
05740         {
05741             if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
05742                 continue;
05743 
05744             if (now == 0)
05745                 now = GetCurrentTimestamp();
05746 
05747             if (!TimestampDifferenceExceeds(rw->rw_crashed_at, now,
05748                                       rw->rw_worker.bgw_restart_time * 1000))
05749             {
05750                 HaveCrashedWorker = true;
05751                 continue;
05752             }
05753         }
05754 
05755         if (bgworker_should_start_now(rw->rw_worker.bgw_start_time))
05756         {
05757             /* reset crash time before calling assign_backendlist_entry */
05758             rw->rw_crashed_at = 0;
05759 
05760             /*
05761              * If necessary, allocate and assign the Backend element.  Note we
05762              * must do this before forking, so that we can handle out of
05763              * memory properly.
05764              *
05765              * If not connected, we don't need a Backend element, but we still
05766              * need a PMChildSlot.
05767              */
05768             if (rw->rw_worker.bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)
05769             {
05770                 if (!assign_backendlist_entry(rw))
05771                     return;
05772             }
05773             else
05774                 rw->rw_child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
05775 
05776             start_bgworker(rw); /* sets rw->rw_pid */
05777 
05778             if (rw->rw_backend)
05779             {
05780                 dlist_push_head(&BackendList, &rw->rw_backend->elem);
05781 #ifdef EXEC_BACKEND
05782                 ShmemBackendArrayAdd(rw->rw_backend);
05783 #endif
05784             }
05785 
05786             /*
05787              * Have ServerLoop call us again.  Note that there might not
05788              * actually *be* another runnable worker, but we don't care all
05789              * that much; we will find out the next time we run.
05790              */
05791             StartWorkerNeeded = true;
05792             return;
05793         }
05794     }
05795 
05796     /* no runnable worker found */
05797     StartWorkerNeeded = false;
05798 }
05799 
05800 #ifdef EXEC_BACKEND
05801 
05802 /*
05803  * The following need to be available to the save/restore_backend_variables
05804  * functions.  They are marked NON_EXEC_STATIC in their home modules.
05805  */
05806 extern slock_t *ShmemLock;
05807 extern LWLock *LWLockArray;
05808 extern slock_t *ProcStructLock;
05809 extern PGPROC *AuxiliaryProcs;
05810 extern PMSignalData *PMSignalState;
05811 extern pgsocket pgStatSock;
05812 extern pg_time_t first_syslogger_file_time;
05813 
05814 #ifndef WIN32
05815 #define write_inheritable_socket(dest, src, childpid) ((*(dest) = (src)), true)
05816 #define read_inheritable_socket(dest, src) (*(dest) = *(src))
05817 #else
05818 static bool write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE child);
05819 static bool write_inheritable_socket(InheritableSocket *dest, SOCKET src,
05820                          pid_t childPid);
05821 static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
05822 #endif
05823 
05824 
05825 /* Save critical backend variables into the BackendParameters struct */
05826 #ifndef WIN32
05827 static bool
05828 save_backend_variables(BackendParameters *param, Port *port)
05829 #else
05830 static bool
05831 save_backend_variables(BackendParameters *param, Port *port,
05832                        HANDLE childProcess, pid_t childPid)
05833 #endif
05834 {
05835     memcpy(&param->port, port, sizeof(Port));
05836     if (!write_inheritable_socket(&param->portsocket, port->sock, childPid))
05837         return false;
05838 
05839     strlcpy(param->DataDir, DataDir, MAXPGPATH);
05840 
05841     memcpy(&param->ListenSocket, &ListenSocket, sizeof(ListenSocket));
05842 
05843     param->MyCancelKey = MyCancelKey;
05844     param->MyPMChildSlot = MyPMChildSlot;
05845 
05846     param->UsedShmemSegID = UsedShmemSegID;
05847     param->UsedShmemSegAddr = UsedShmemSegAddr;
05848 
05849     param->ShmemLock = ShmemLock;
05850     param->ShmemVariableCache = ShmemVariableCache;
05851     param->ShmemBackendArray = ShmemBackendArray;
05852 
05853     param->LWLockArray = LWLockArray;
05854     param->ProcStructLock = ProcStructLock;
05855     param->ProcGlobal = ProcGlobal;
05856     param->AuxiliaryProcs = AuxiliaryProcs;
05857     param->PreparedXactProcs = PreparedXactProcs;
05858     param->PMSignalState = PMSignalState;
05859     if (!write_inheritable_socket(&param->pgStatSock, pgStatSock, childPid))
05860         return false;
05861 
05862     param->PostmasterPid = PostmasterPid;
05863     param->PgStartTime = PgStartTime;
05864     param->PgReloadTime = PgReloadTime;
05865     param->first_syslogger_file_time = first_syslogger_file_time;
05866 
05867     param->redirection_done = redirection_done;
05868     param->IsBinaryUpgrade = IsBinaryUpgrade;
05869     param->max_safe_fds = max_safe_fds;
05870 
05871     param->MaxBackends = MaxBackends;
05872 
05873 #ifdef WIN32
05874     param->PostmasterHandle = PostmasterHandle;
05875     if (!write_duplicated_handle(&param->initial_signal_pipe,
05876                                  pgwin32_create_signal_listener(childPid),
05877                                  childProcess))
05878         return false;
05879 #else
05880     memcpy(&param->postmaster_alive_fds, &postmaster_alive_fds,
05881            sizeof(postmaster_alive_fds));
05882 #endif
05883 
05884     memcpy(&param->syslogPipe, &syslogPipe, sizeof(syslogPipe));
05885 
05886     strlcpy(param->my_exec_path, my_exec_path, MAXPGPATH);
05887 
05888     strlcpy(param->pkglib_path, pkglib_path, MAXPGPATH);
05889 
05890     strlcpy(param->ExtraOptions, ExtraOptions, MAXPGPATH);
05891 
05892     return true;
05893 }
05894 
05895 
05896 #ifdef WIN32
05897 /*
05898  * Duplicate a handle for usage in a child process, and write the child
05899  * process instance of the handle to the parameter file.
05900  */
05901 static bool
05902 write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE childProcess)
05903 {
05904     HANDLE      hChild = INVALID_HANDLE_VALUE;
05905 
05906     if (!DuplicateHandle(GetCurrentProcess(),
05907                          src,
05908                          childProcess,
05909                          &hChild,
05910                          0,
05911                          TRUE,
05912                          DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
05913     {
05914         ereport(LOG,
05915                 (errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %lu",
05916                                  GetLastError())));
05917         return false;
05918     }
05919 
05920     *dest = hChild;
05921     return true;
05922 }
05923 
05924 /*
05925  * Duplicate a socket for usage in a child process, and write the resulting
05926  * structure to the parameter file.
05927  * This is required because a number of LSPs (Layered Service Providers) very
05928  * common on Windows (antivirus, firewalls, download managers etc) break
05929  * straight socket inheritance.
05930  */
05931 static bool
05932 write_inheritable_socket(InheritableSocket *dest, SOCKET src, pid_t childpid)
05933 {
05934     dest->origsocket = src;
05935     if (src != 0 && src != PGINVALID_SOCKET)
05936     {
05937         /* Actual socket */
05938         if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
05939         {
05940             ereport(LOG,
05941                     (errmsg("could not duplicate socket %d for use in backend: error code %d",
05942                             (int) src, WSAGetLastError())));
05943             return false;
05944         }
05945     }
05946     return true;
05947 }
05948 
05949 /*
05950  * Read a duplicate socket structure back, and get the socket descriptor.
05951  */
05952 static void
05953 read_inheritable_socket(SOCKET *dest, InheritableSocket *src)
05954 {
05955     SOCKET      s;
05956 
05957     if (src->origsocket == PGINVALID_SOCKET || src->origsocket == 0)
05958     {
05959         /* Not a real socket! */
05960         *dest = src->origsocket;
05961     }
05962     else
05963     {
05964         /* Actual socket, so create from structure */
05965         s = WSASocket(FROM_PROTOCOL_INFO,
05966                       FROM_PROTOCOL_INFO,
05967                       FROM_PROTOCOL_INFO,
05968                       &src->wsainfo,
05969                       0,
05970                       0);
05971         if (s == INVALID_SOCKET)
05972         {
05973             write_stderr("could not create inherited socket: error code %d\n",
05974                          WSAGetLastError());
05975             exit(1);
05976         }
05977         *dest = s;
05978 
05979         /*
05980          * To make sure we don't get two references to the same socket, close
05981          * the original one. (This would happen when inheritance actually
05982          * works..
05983          */
05984         closesocket(src->origsocket);
05985     }
05986 }
05987 #endif
05988 
05989 static void
05990 read_backend_variables(char *id, Port *port)
05991 {
05992     BackendParameters param;
05993 
05994 #ifndef WIN32
05995     /* Non-win32 implementation reads from file */
05996     FILE       *fp;
05997 
05998     /* Open file */
05999     fp = AllocateFile(id, PG_BINARY_R);
06000     if (!fp)
06001     {
06002         write_stderr("could not read from backend variables file \"%s\": %s\n",
06003                      id, strerror(errno));
06004         exit(1);
06005     }
06006 
06007     if (fread(&param, sizeof(param), 1, fp) != 1)
06008     {
06009         write_stderr("could not read from backend variables file \"%s\": %s\n",
06010                      id, strerror(errno));
06011         exit(1);
06012     }
06013 
06014     /* Release file */
06015     FreeFile(fp);
06016     if (unlink(id) != 0)
06017     {
06018         write_stderr("could not remove file \"%s\": %s\n",
06019                      id, strerror(errno));
06020         exit(1);
06021     }
06022 #else
06023     /* Win32 version uses mapped file */
06024     HANDLE      paramHandle;
06025     BackendParameters *paramp;
06026 
06027 #ifdef _WIN64
06028     paramHandle = (HANDLE) _atoi64(id);
06029 #else
06030     paramHandle = (HANDLE) atol(id);
06031 #endif
06032     paramp = MapViewOfFile(paramHandle, FILE_MAP_READ, 0, 0, 0);
06033     if (!paramp)
06034     {
06035         write_stderr("could not map view of backend variables: error code %lu\n",
06036                      GetLastError());
06037         exit(1);
06038     }
06039 
06040     memcpy(&param, paramp, sizeof(BackendParameters));
06041 
06042     if (!UnmapViewOfFile(paramp))
06043     {
06044         write_stderr("could not unmap view of backend variables: error code %lu\n",
06045                      GetLastError());
06046         exit(1);
06047     }
06048 
06049     if (!CloseHandle(paramHandle))
06050     {
06051         write_stderr("could not close handle to backend parameter variables: error code %lu\n",
06052                      GetLastError());
06053         exit(1);
06054     }
06055 #endif
06056 
06057     restore_backend_variables(&param, port);
06058 }
06059 
06060 /* Restore critical backend variables from the BackendParameters struct */
06061 static void
06062 restore_backend_variables(BackendParameters *param, Port *port)
06063 {
06064     memcpy(port, &param->port, sizeof(Port));
06065     read_inheritable_socket(&port->sock, &param->portsocket);
06066 
06067     SetDataDir(param->DataDir);
06068 
06069     memcpy(&ListenSocket, &param->ListenSocket, sizeof(ListenSocket));
06070 
06071     MyCancelKey = param->MyCancelKey;
06072     MyPMChildSlot = param->MyPMChildSlot;
06073 
06074     UsedShmemSegID = param->UsedShmemSegID;
06075     UsedShmemSegAddr = param->UsedShmemSegAddr;
06076 
06077     ShmemLock = param->ShmemLock;
06078     ShmemVariableCache = param->ShmemVariableCache;
06079     ShmemBackendArray = param->ShmemBackendArray;
06080 
06081     LWLockArray = param->LWLockArray;
06082     ProcStructLock = param->ProcStructLock;
06083     ProcGlobal = param->ProcGlobal;
06084     AuxiliaryProcs = param->AuxiliaryProcs;
06085     PreparedXactProcs = param->PreparedXactProcs;
06086     PMSignalState = param->PMSignalState;
06087     read_inheritable_socket(&pgStatSock, &param->pgStatSock);
06088 
06089     PostmasterPid = param->PostmasterPid;
06090     PgStartTime = param->PgStartTime;
06091     PgReloadTime = param->PgReloadTime;
06092     first_syslogger_file_time = param->first_syslogger_file_time;
06093 
06094     redirection_done = param->redirection_done;
06095     IsBinaryUpgrade = param->IsBinaryUpgrade;
06096     max_safe_fds = param->max_safe_fds;
06097 
06098     MaxBackends = param->MaxBackends;
06099 
06100 #ifdef WIN32
06101     PostmasterHandle = param->PostmasterHandle;
06102     pgwin32_initial_signal_pipe = param->initial_signal_pipe;
06103 #else
06104     memcpy(&postmaster_alive_fds, &param->postmaster_alive_fds,
06105            sizeof(postmaster_alive_fds));
06106 #endif
06107 
06108     memcpy(&syslogPipe, &param->syslogPipe, sizeof(syslogPipe));
06109 
06110     strlcpy(my_exec_path, param->my_exec_path, MAXPGPATH);
06111 
06112     strlcpy(pkglib_path, param->pkglib_path, MAXPGPATH);
06113 
06114     strlcpy(ExtraOptions, param->ExtraOptions, MAXPGPATH);
06115 }
06116 
06117 
06118 Size
06119 ShmemBackendArraySize(void)
06120 {
06121     return mul_size(MaxLivePostmasterChildren(), sizeof(Backend));
06122 }
06123 
06124 void
06125 ShmemBackendArrayAllocation(void)
06126 {
06127     Size        size = ShmemBackendArraySize();
06128 
06129     ShmemBackendArray = (Backend *) ShmemAlloc(size);
06130     /* Mark all slots as empty */
06131     memset(ShmemBackendArray, 0, size);
06132 }
06133 
06134 static void
06135 ShmemBackendArrayAdd(Backend *bn)
06136 {
06137     /* The array slot corresponding to my PMChildSlot should be free */
06138     int         i = bn->child_slot - 1;
06139 
06140     Assert(ShmemBackendArray[i].pid == 0);
06141     ShmemBackendArray[i] = *bn;
06142 }
06143 
06144 static void
06145 ShmemBackendArrayRemove(Backend *bn)
06146 {
06147     int         i = bn->child_slot - 1;
06148 
06149     Assert(ShmemBackendArray[i].pid == bn->pid);
06150     /* Mark the slot as empty */
06151     ShmemBackendArray[i].pid = 0;
06152 }
06153 #endif   /* EXEC_BACKEND */
06154 
06155 
06156 #ifdef WIN32
06157 
06158 /*
06159  * Subset implementation of waitpid() for Windows.  We assume pid is -1
06160  * (that is, check all child processes) and options is WNOHANG (don't wait).
06161  */
06162 static pid_t
06163 waitpid(pid_t pid, int *exitstatus, int options)
06164 {
06165     DWORD       dwd;
06166     ULONG_PTR   key;
06167     OVERLAPPED *ovl;
06168 
06169     /*
06170      * Check if there are any dead children. If there are, return the pid of
06171      * the first one that died.
06172      */
06173     if (GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
06174     {
06175         *exitstatus = (int) key;
06176         return dwd;
06177     }
06178 
06179     return -1;
06180 }
06181 
06182 /*
06183  * Note! Code below executes on a thread pool! All operations must
06184  * be thread safe! Note that elog() and friends must *not* be used.
06185  */
06186 static void WINAPI
06187 pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
06188 {
06189     win32_deadchild_waitinfo *childinfo = (win32_deadchild_waitinfo *) lpParameter;
06190     DWORD       exitcode;
06191 
06192     if (TimerOrWaitFired)
06193         return;                 /* timeout. Should never happen, since we use
06194                                  * INFINITE as timeout value. */
06195 
06196     /*
06197      * Remove handle from wait - required even though it's set to wait only
06198      * once
06199      */
06200     UnregisterWaitEx(childinfo->waitHandle, NULL);
06201 
06202     if (!GetExitCodeProcess(childinfo->procHandle, &exitcode))
06203     {
06204         /*
06205          * Should never happen. Inform user and set a fixed exitcode.
06206          */
06207         write_stderr("could not read exit code for process\n");
06208         exitcode = 255;
06209     }
06210 
06211     if (!PostQueuedCompletionStatus(win32ChildQueue, childinfo->procId, (ULONG_PTR) exitcode, NULL))
06212         write_stderr("could not post child completion status\n");
06213 
06214     /*
06215      * Handle is per-process, so we close it here instead of in the
06216      * originating thread
06217      */
06218     CloseHandle(childinfo->procHandle);
06219 
06220     /*
06221      * Free struct that was allocated before the call to
06222      * RegisterWaitForSingleObject()
06223      */
06224     free(childinfo);
06225 
06226     /* Queue SIGCHLD signal */
06227     pg_queue_signal(SIGCHLD);
06228 }
06229 #endif   /* WIN32 */
06230 
06231 /*
06232  * Initialize one and only handle for monitoring postmaster death.
06233  *
06234  * Called once in the postmaster, so that child processes can subsequently
06235  * monitor if their parent is dead.
06236  */
06237 static void
06238 InitPostmasterDeathWatchHandle(void)
06239 {
06240 #ifndef WIN32
06241 
06242     /*
06243      * Create a pipe. Postmaster holds the write end of the pipe open
06244      * (POSTMASTER_FD_OWN), and children hold the read end. Children can pass
06245      * the read file descriptor to select() to wake up in case postmaster
06246      * dies, or check for postmaster death with a (read() == 0). Children must
06247      * close the write end as soon as possible after forking, because EOF
06248      * won't be signaled in the read end until all processes have closed the
06249      * write fd. That is taken care of in ClosePostmasterPorts().
06250      */
06251     Assert(MyProcPid == PostmasterPid);
06252     if (pipe(postmaster_alive_fds))
06253         ereport(FATAL,
06254                 (errcode_for_file_access(),
06255                  errmsg_internal("could not create pipe to monitor postmaster death: %m")));
06256 
06257     /*
06258      * Set O_NONBLOCK to allow testing for the fd's presence with a read()
06259      * call.
06260      */
06261     if (fcntl(postmaster_alive_fds[POSTMASTER_FD_WATCH], F_SETFL, O_NONBLOCK))
06262         ereport(FATAL,
06263                 (errcode_for_socket_access(),
06264                  errmsg_internal("could not set postmaster death monitoring pipe to nonblocking mode: %m")));
06265 #else
06266 
06267     /*
06268      * On Windows, we use a process handle for the same purpose.
06269      */
06270     if (DuplicateHandle(GetCurrentProcess(),
06271                         GetCurrentProcess(),
06272                         GetCurrentProcess(),
06273                         &PostmasterHandle,
06274                         0,
06275                         TRUE,
06276                         DUPLICATE_SAME_ACCESS) == 0)
06277         ereport(FATAL,
06278                 (errmsg_internal("could not duplicate postmaster handle: error code %lu",
06279                                  GetLastError())));
06280 #endif   /* WIN32 */
06281 }