70#define DEFAULT_WAIT 60
72#define WAITS_PER_SEC 10
74 "WAITS_PER_SEC must divide USECS_PER_SEC evenly");
112#define shutdownEvent shutdownHandles[0]
113#define postmasterProcess shutdownHandles[1]
135#include <versionhelpers.h>
159#if defined(HAVE_GETRLIMIT)
272 write_stderr(
_(
"%s: directory \"%s\" is not a database cluster directory\n"),
349 result = (
char **)
pg_malloc(
sizeof(
char *));
372 if (buffer[
i] ==
'\n')
385 if (buffer[
i] ==
'\n')
479 write_stderr(
_(
"%s: could not start server due to setsid() failure: %m\n"),
491 cmd =
psprintf(
"exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
495 cmd =
psprintf(
"exec \"%s\" %s%s < \"%s\" 2>&1",
498 (
void)
execl(
"/bin/sh",
"/bin/sh",
"-c", cmd, (
char *)
NULL);
557 cmd =
psprintf(
"\"%s\" /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
561 cmd =
psprintf(
"\"%s\" /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
566 write_stderr(
_(
"%s: could not start server: error code %lu\n"),
573 return pi.dwProcessId;
694 status.dwWaitHint += 6000;
695 status.dwCheckPoint++;
729 if (
kill(pid, 0) != 0)
766 if (
kill(pid, 0) != 0)
781#if defined(HAVE_GETRLIMIT)
788 if (
lim.rlim_max == 0)
790 write_stderr(
_(
"%s: cannot set core file size limit; disallowed by hard limit\n"),
796 lim.rlim_cur =
lim.rlim_max;
821 write_stderr(
_(
"%s: option file \"%s\" must have exactly one line\n"),
863 write_stderr(
_(
"%s: could not send stop signal (PID: %d): %m\n"),
891 write_stderr(
_(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"\n"),
894 write_stderr(
_(
"program \"%s\" was found by \"%s\" but was not the same version as %s\n"),
920 cmd =
psprintf(
"\"%s\" %s%s > \"%s\"",
942 "trying to start server anyway\n"),
955#if defined(HAVE_GETRLIMIT)
989 print_msg(
_(
"waiting for server to start..."));
1005 print_msg(
_(
"server shut down because of recovery target settings\n"));
1010 "Examine the log output.\n"),
1044 "single-user server is running (PID: %d)\n"),
1062 print_msg(
_(
"waiting for server to shut down..."));
1070 write_stderr(
_(
"HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1071 "waiting for session-initiated disconnection.\n"));
1107 "single-user server is running (PID: %d)\n"),
1109 write_stderr(
_(
"Please terminate the single-user server and try again.\n"));
1122 print_msg(
_(
"waiting for server to shut down..."));
1131 write_stderr(
_(
"HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1132 "waiting for session-initiated disconnection.\n"));
1141 write_stderr(
_(
"%s: old server process (PID: %d) seems to be gone\n"),
1165 "single-user server is running (PID: %d)\n"),
1167 write_stderr(
_(
"Please terminate the single-user server and try again.\n"));
1173 write_stderr(
_(
"%s: could not send reload signal (PID: %d): %m\n"),
1204 "single-user server is running (PID: %d)\n"),
1212 "server is not in standby mode\n"),
1221 write_stderr(
_(
"%s: could not create promote signal file \"%s\": %m\n"),
1227 write_stderr(
_(
"%s: could not write promote signal file \"%s\": %m\n"),
1235 write_stderr(
_(
"%s: could not send promote signal (PID: %d): %m\n"),
1238 write_stderr(
_(
"%s: could not remove promote signal file \"%s\": %m\n"),
1245 print_msg(
_(
"waiting for server to promote..."));
1285 "single-user server is running (PID: %d)\n"),
1294 write_stderr(
_(
"%s: could not create log rotation signal file \"%s\": %m\n"),
1300 write_stderr(
_(
"%s: could not write log rotation signal file \"%s\": %m\n"),
1308 write_stderr(
_(
"%s: could not send log rotation signal (PID: %d): %m\n"),
1311 write_stderr(
_(
"%s: could not remove log rotation signal file \"%s\": %m\n"),
1316 print_msg(
_(
"server signaled to rotate log file\n"));
1343 if (
kill(pid, 0) == 0)
1363 printf(
_(
"%s: single-user server is running (PID: %d)\n"),
1377 printf(
_(
"%s: server is running (PID: %d)\n"),
1410 write_stderr(
_(
"%s: could not send signal %d (PID: %d): %m\n"),
1538 write_stderr(
_(
"%s: could not register service \"%s\": error code %lu\n"),
1568 write_stderr(
_(
"%s: could not open service \"%s\": error code %lu\n"),
1577 write_stderr(
_(
"%s: could not unregister service \"%s\": error code %lu\n"),
1606 status.dwWaitHint = 10000;
1613 status.dwWaitHint = 5000;
1632 status.dwWin32ExitCode =
S_OK;
1633 status.dwCheckPoint = 0;
1634 status.dwWaitHint = 60000;
1637 status.dwServiceSpecificExitCode = 0;
1698 status.dwCheckPoint++;
1726 write_stderr(
_(
"%s: could not start service \"%s\": error code %lu\n"),
1749 if (
si->hStdInput ==
NULL)
1752 if (
si->hStdOutput ==
NULL)
1755 if (
si->hStdError ==
NULL)
1799 write_stderr(
_(
"%s: could not open process token: error code %lu\n"),
1813 write_stderr(
_(
"%s: could not allocate SIDs: error code %lu\n"),
1839 write_stderr(
_(
"%s: could not create restricted token: error code %lu\n"),
1845 r =
CreateProcessAsUser(
restrictedToken,
NULL, cmd,
NULL,
NULL,
TRUE,
CREATE_SUSPENDED,
NULL,
NULL, &
si,
processInfo);
1920 write_stderr(
_(
"%s: could not get LUIDs for privileges: error code %lu\n"),
1928 write_stderr(
_(
"%s: could not get token information: error code %lu\n"),
1943 write_stderr(
_(
"%s: could not get token information: error code %lu\n"),
1975 printf(
_(
"%s is a utility to initialize, start, stop, or control a PostgreSQL server.\n\n"),
progname);
1978 printf(
_(
" %s start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]\n"
1979 " [-o OPTIONS] [-p PATH] [-c]\n"),
progname);
1980 printf(
_(
" %s stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"),
progname);
1981 printf(
_(
" %s restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"
1982 " [-o OPTIONS] [-c]\n"),
progname);
1985 printf(
_(
" %s promote [-D DATADIR] [-W] [-t SECS] [-s]\n"),
progname);
1989 printf(
_(
" %s register [-D DATADIR] [-N SERVICENAME] [-U USERNAME] [-P PASSWORD]\n"
1990 " [-S START-TYPE] [-e SOURCE] [-W] [-t SECS] [-s] [-o OPTIONS]\n"),
progname);
1994 printf(
_(
"\nCommon options:\n"));
1995 printf(
_(
" -D, --pgdata=DATADIR location of the database storage area\n"));
1997 printf(
_(
" -e SOURCE event source for logging when running as a service\n"));
1999 printf(
_(
" -s, --silent only print errors, no informational messages\n"));
2000 printf(
_(
" -t, --timeout=SECS seconds to wait when using -w option\n"));
2001 printf(
_(
" -V, --version output version information, then exit\n"));
2002 printf(
_(
" -w, --wait wait until operation completes (default)\n"));
2003 printf(
_(
" -W, --no-wait do not wait until operation completes\n"));
2004 printf(
_(
" -?, --help show this help, then exit\n"));
2005 printf(
_(
"If the -D option is omitted, the environment variable PGDATA is used.\n"));
2007 printf(
_(
"\nOptions for start or restart:\n"));
2008#if defined(HAVE_GETRLIMIT)
2009 printf(
_(
" -c, --core-files allow postgres to produce core files\n"));
2011 printf(
_(
" -c, --core-files not applicable on this platform\n"));
2013 printf(
_(
" -l, --log=FILENAME write (or append) server log to FILENAME\n"));
2014 printf(
_(
" -o, --options=OPTIONS command line options to pass to postgres\n"
2015 " (PostgreSQL server executable) or initdb\n"));
2016 printf(
_(
" -p PATH-TO-POSTGRES normally not necessary\n"));
2017 printf(
_(
"\nOptions for stop or restart:\n"));
2018 printf(
_(
" -m, --mode=MODE MODE can be \"smart\", \"fast\", or \"immediate\"\n"));
2020 printf(
_(
"\nShutdown modes are:\n"));
2021 printf(
_(
" smart quit after all clients have disconnected\n"));
2022 printf(
_(
" fast quit directly, with proper shutdown (default)\n"));
2023 printf(
_(
" immediate quit without complete shutdown; will lead to recovery on restart\n"));
2025 printf(
_(
"\nAllowed signal names for kill:\n"));
2026 printf(
" ABRT HUP INT KILL QUIT TERM USR1 USR2\n");
2029 printf(
_(
"\nOptions for register and unregister:\n"));
2030 printf(
_(
" -N SERVICENAME service name with which to register PostgreSQL server\n"));
2031 printf(
_(
" -P PASSWORD password of account to register PostgreSQL server\n"));
2032 printf(
_(
" -U USERNAME user name of account to register PostgreSQL server\n"));
2033 printf(
_(
" -S START-TYPE service start type to register PostgreSQL server\n"));
2035 printf(
_(
"\nStart types are:\n"));
2036 printf(
_(
" auto start service automatically during system startup (default)\n"));
2037 printf(
_(
" demand start service on demand\n"));
2159 cmd =
psprintf(
"\"%s\" -C data_directory %s%s",
2168 write_stderr(
_(
"%s: could not determine the data directory using command \"%s\"\n"),
progname, cmd);
2241 if (
strcmp(argv[1],
"--help") == 0 ||
strcmp(argv[1],
"-?") == 0)
2246 else if (
strcmp(argv[1],
"--version") == 0 ||
strcmp(argv[1],
"-V") == 0)
2260 "Please log in (using, e.g., \"su\") as the "
2261 "(unprivileged) user that will\n"
2262 "own the server process.\n"),
2273 while ((
c =
getopt_long(argc, argv,
"cD:e:l:m:N:o:p:P:sS:t:U:wW",
2331 write_stderr(
_(
"%s: -S option not supported on this platform\n"),
2442 write_stderr(
_(
"%s: no database directory specified and environment variable PGDATA unset\n"),
#define write_stderr(str)
#define PG_TEXTDOMAIN(domain)
#define pg_attribute_printf(f, a)
#define StaticAssertDecl(condition, errmessage)
int find_my_exec(const char *argv0, char *retpath)
void set_pglocale_pgservice(const char *argv0, const char *app)
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
void * pg_malloc(size_t size)
void * pg_malloc_extended(size_t size, int flags)
char * pg_strdup(const char *in)
#define MCXT_ALLOC_NO_OOM
bool GetDataDirectoryCreatePerm(const char *dataDir)
#define PG_MODE_MASK_OWNER
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
char my_exec_path[MAXPGPATH]
void pg_logging_init(const char *argv0)
void pfree(void *pointer)
#define DEFAULT_EVENT_SOURCE
@ DB_SHUTDOWNED_IN_RECOVERY
static void read_post_opts(void)
static void static void do_advice(void)
static void do_reload(void)
static WaitPMResult wait_for_postmaster_start(pid_t pm_pid, bool do_checkpoint)
static void do_help(void)
static void free_readfile(char **optlines)
static char * register_username
static char * event_source
static void trap_sigint_during_startup(SIGNAL_ARGS)
static volatile pid_t postmasterPID
static void adjust_data_dir(void)
static char * register_servicename
static char version_file[MAXPGPATH]
static pid_t get_pgpid(bool is_status_request)
static bool allow_core_files
static char * find_other_exec_or_die(const char *argv0, const char *target, const char *versionstr)
static char ** readfile(const char *path, int *numlines)
static bool wait_for_postmaster_promote(void)
static char postopts_file[MAXPGPATH]
static void unlimit_core_size(void)
@ POSTMASTER_SHUTDOWN_IN_RECOVERY
@ POSTMASTER_STILL_STARTING
static void set_mode(char *modeopt)
static char promote_file[MAXPGPATH]
static void do_start(void)
static DBState get_control_dbstate(void)
static void do_logrotate(void)
static void do_stop(void)
static char pid_file[MAXPGPATH]
static char * register_password
static CtlCommand ctl_command
static void do_init(void)
static void print_msg(const char *msg)
static bool postmaster_is_alive(pid_t pid)
static pid_t start_postmaster(void)
static void do_restart(void)
static bool wait_seconds_arg
static bool wait_for_postmaster_stop(void)
static void do_promote(void)
static void set_sig(char *signame)
static const char * progname
static void do_status(void)
static char logrotate_file[MAXPGPATH]
static void do_kill(pid_t pid)
static ShutdownMode shutdown_mode
PGDLLIMPORT char * optarg
#define PM_STATUS_STANDBY
#define LOCK_FILE_LINE_START_TIME
#define LOCK_FILE_LINE_PM_STATUS
#define LOCK_FILE_LINE_PID
int pg_strcasecmp(const char *s1, const char *s2)
void canonicalize_path(char *path)
void make_native_path(char *filename)
#define PG_BACKEND_VERSIONSTR
char * make_absolute_path(const char *path)
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
PQExpBuffer createPQExpBuffer(void)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
static int fd(const char *x, int i)
char * psprintf(const char *fmt,...)
void pg_usleep(long microsec)
int pg_strip_crlf(char *str)
BOOL AddUserToTokenDacl(HANDLE hToken)
int pgwin32_is_service(void)