69#define DEFAULT_WAIT 60
71#define USEC_PER_SEC 1000000
73#define WAITS_PER_SEC 10
80static int sig = SIGINT;
106static DWORD pgctl_start_type = SERVICE_AUTO_START;
107static SERVICE_STATUS status;
108static SERVICE_STATUS_HANDLE hStatus = (SERVICE_STATUS_HANDLE) 0;
109static HANDLE shutdownHandles[2];
111#define shutdownEvent shutdownHandles[0]
112#define postmasterProcess shutdownHandles[1]
120static
void set_sig(
char *signame);
134#include <versionhelpers.h>
135static bool pgwin32_IsInstalled(SC_HANDLE);
136static char *pgwin32_CommandLine(
bool);
137static void pgwin32_doRegister(
void);
138static void pgwin32_doUnregister(
void);
139static void pgwin32_SetServiceStatus(DWORD);
140static void WINAPI pgwin32_ServiceHandler(DWORD);
141static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *);
142static void pgwin32_doRunAsService(
void);
143static int CreateRestrictedProcess(
char *cmd, PROCESS_INFORMATION *processInfo,
bool as_service);
144static PTOKEN_PRIVILEGES GetPrivilegesToDelete(HANDLE hToken);
147static pid_t
get_pgpid(
bool is_status_request);
148static char **
readfile(
const char *path,
int *numlines);
158#if defined(HAVE_GETRLIMIT)
167write_eventlog(
int level,
const char *line)
169 static HANDLE evtHandle = INVALID_HANDLE_VALUE;
171 if (
silent_mode && level == EVENTLOG_INFORMATION_TYPE)
174 if (evtHandle == INVALID_HANDLE_VALUE)
176 evtHandle = RegisterEventSource(NULL,
178 if (evtHandle == NULL)
180 evtHandle = INVALID_HANDLE_VALUE;
185 ReportEvent(evtHandle,
220 vsnprintf(errbuf,
sizeof(errbuf), fmt, ap);
222 write_eventlog(EVENTLOG_ERROR_TYPE, errbuf);
266 exit(is_status_request ? 4 : 1);
271 write_stderr(
_(
"%s: directory \"%s\" is not a database cluster directory\n"),
273 exit(is_status_request ? 4 : 1);
289 if (fscanf(pidf,
"%d", &pid) != 1)
292 if (ftell(pidf) == 0 && feof(pidf))
348 result = (
char **)
pg_malloc(
sizeof(
char *));
371 if (buffer[
i] ==
'\n')
376 result = (
char **)
pg_malloc((nlines + 1) *
sizeof(
char *));
384 if (buffer[
i] ==
'\n')
386 int slen = &buffer[
i] - linebegin;
389 memcpy(linebuf, linebegin, slen);
391 if (slen > 0 && linebuf[slen - 1] ==
'\r')
393 linebuf[slen] =
'\0';
394 result[n++] = linebuf;
395 linebegin = &buffer[
i + 1];
412 char *curr_line = NULL;
418 while ((curr_line = optlines[
i++]))
478 write_stderr(
_(
"%s: could not start server due to setsid() failure: %m\n"),
490 cmd =
psprintf(
"exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
494 cmd =
psprintf(
"exec \"%s\" %s%s < \"%s\" 2>&1",
497 (void) execl(
"/bin/sh",
"/bin/sh",
"-c", cmd, (
char *) NULL);
513 PROCESS_INFORMATION pi;
517 comspec = getenv(
"COMSPEC");
556 cmd =
psprintf(
"\"%s\" /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
560 cmd =
psprintf(
"\"%s\" /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
563 if (!CreateRestrictedProcess(cmd, &pi,
false))
565 write_stderr(
_(
"%s: could not start server: error code %lu\n"),
566 progname, (
unsigned long) GetLastError());
570 postmasterProcess = pi.hProcess;
571 CloseHandle(pi.hThread);
572 return pi.dwProcessId;
666 pm_died = (waitpid(pm_pid, &exitstatus, WNOHANG) == pm_pid);
668 pm_died = (WaitForSingleObject(postmasterProcess, 0) == WAIT_OBJECT_0);
693 status.dwWaitHint += 6000;
694 status.dwCheckPoint++;
695 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
728 if (
kill(pid, 0) != 0)
765 if (
kill(pid, 0) != 0)
780#if defined(HAVE_GETRLIMIT)
786 getrlimit(RLIMIT_CORE, &lim);
787 if (lim.rlim_max == 0)
789 write_stderr(
_(
"%s: cannot set core file size limit; disallowed by hard limit\n"),
793 else if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
795 lim.rlim_cur = lim.rlim_max;
796 setrlimit(RLIMIT_CORE, &lim);
813 if (optlines == NULL)
818 else if (numlines != 1)
820 write_stderr(
_(
"%s: option file \"%s\" must have exactly one line\n"),
829 optline = optlines[0];
835 if ((arg1 = strstr(optline,
" \"")) != NULL)
862 write_stderr(
_(
"%s: could not send stop signal (PID: %d): %m\n"),
870 pqsignal(postgres_signal_arg, SIG_DFL);
871 raise(postgres_signal_arg);
890 write_stderr(
_(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"\n"),
893 write_stderr(
_(
"program \"%s\" was found by \"%s\" but was not the same version as %s\n"),
919 cmd =
psprintf(
"\"%s\" %s%s > \"%s\"",
923 if (system(cmd) != 0)
941 "trying to start server anyway\n"),
954#if defined(HAVE_GETRLIMIT)
968 snprintf(env_var,
sizeof(env_var),
"%d", (
int) getppid());
969 setenv(
"PG_GRANDPARENT_PID", env_var, 1);
988 print_msg(
_(
"waiting for server to start..."));
1004 print_msg(
_(
"server shut down because of recovery target settings\n"));
1009 "Examine the log output.\n"),
1020 CloseHandle(postmasterProcess);
1021 postmasterProcess = INVALID_HANDLE_VALUE;
1043 "single-user server is running (PID: %d)\n"),
1061 print_msg(
_(
"waiting for server to shut down..."));
1069 write_stderr(
_(
"HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1070 "waiting for session-initiated disconnection.\n"));
1106 "single-user server is running (PID: %d)\n"),
1108 write_stderr(
_(
"Please terminate the single-user server and try again.\n"));
1121 print_msg(
_(
"waiting for server to shut down..."));
1130 write_stderr(
_(
"HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1131 "waiting for session-initiated disconnection.\n"));
1140 write_stderr(
_(
"%s: old server process (PID: %d) seems to be gone\n"),
1164 "single-user server is running (PID: %d)\n"),
1166 write_stderr(
_(
"Please terminate the single-user server and try again.\n"));
1172 write_stderr(
_(
"%s: could not send reload signal (PID: %d): %m\n"),
1203 "single-user server is running (PID: %d)\n"),
1211 "server is not in standby mode\n"),
1220 write_stderr(
_(
"%s: could not create promote signal file \"%s\": %m\n"),
1224 if (fclose(prmfile))
1226 write_stderr(
_(
"%s: could not write promote signal file \"%s\": %m\n"),
1234 write_stderr(
_(
"%s: could not send promote signal (PID: %d): %m\n"),
1237 write_stderr(
_(
"%s: could not remove promote signal file \"%s\": %m\n"),
1244 print_msg(
_(
"waiting for server to promote..."));
1269 FILE *logrotatefile;
1284 "single-user server is running (PID: %d)\n"),
1293 write_stderr(
_(
"%s: could not create log rotation signal file \"%s\": %m\n"),
1297 if (fclose(logrotatefile))
1299 write_stderr(
_(
"%s: could not write log rotation signal file \"%s\": %m\n"),
1307 write_stderr(
_(
"%s: could not send log rotation signal (PID: %d): %m\n"),
1310 write_stderr(
_(
"%s: could not remove log rotation signal file \"%s\": %m\n"),
1315 print_msg(
_(
"server signaled to rotate log file\n"));
1336 if (pid == getpid())
1339 if (pid == getppid())
1342 if (
kill(pid, 0) == 0)
1362 printf(
_(
"%s: single-user server is running (PID: %d)\n"),
1376 printf(
_(
"%s: server is running (PID: %d)\n"),
1380 if (optlines != NULL)
1382 for (curr_line = optlines; *curr_line != NULL; curr_line++)
1409 write_stderr(
_(
"%s: could not send signal %d (PID: %d): %m\n"),
1418pgwin32_IsInstalled(SC_HANDLE hSCM)
1421 bool bResult = (hService != NULL);
1424 CloseServiceHandle(hService);
1429pgwin32_CommandLine(
bool registration)
1456 if (strlen(cmdPath) < 4 ||
1458 snprintf(cmdPath + strlen(cmdPath),
sizeof(cmdPath) - strlen(cmdPath),
1509 return cmdLine->
data;
1513pgwin32_doRegister(
void)
1516 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
1523 if (pgwin32_IsInstalled(hSCM))
1525 CloseServiceHandle(hSCM);
1531 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1532 pgctl_start_type, SERVICE_ERROR_NORMAL,
1533 pgwin32_CommandLine(
true),
1536 CloseServiceHandle(hSCM);
1537 write_stderr(
_(
"%s: could not register service \"%s\": error code %lu\n"),
1539 (
unsigned long) GetLastError());
1542 CloseServiceHandle(hService);
1543 CloseServiceHandle(hSCM);
1547pgwin32_doUnregister(
void)
1550 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
1557 if (!pgwin32_IsInstalled(hSCM))
1559 CloseServiceHandle(hSCM);
1566 CloseServiceHandle(hSCM);
1567 write_stderr(
_(
"%s: could not open service \"%s\": error code %lu\n"),
1569 (
unsigned long) GetLastError());
1572 if (!DeleteService(hService))
1574 CloseServiceHandle(hService);
1575 CloseServiceHandle(hSCM);
1576 write_stderr(
_(
"%s: could not unregister service \"%s\": error code %lu\n"),
1578 (
unsigned long) GetLastError());
1581 CloseServiceHandle(hService);
1582 CloseServiceHandle(hSCM);
1586pgwin32_SetServiceStatus(DWORD currentState)
1588 status.dwCurrentState = currentState;
1589 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
1593pgwin32_ServiceHandler(DWORD request)
1597 case SERVICE_CONTROL_STOP:
1598 case SERVICE_CONTROL_SHUTDOWN:
1605 status.dwWaitHint = 10000;
1606 pgwin32_SetServiceStatus(SERVICE_STOP_PENDING);
1607 SetEvent(shutdownEvent);
1610 case SERVICE_CONTROL_PAUSE:
1612 status.dwWaitHint = 5000;
1617 case SERVICE_CONTROL_CONTINUE:
1618 case SERVICE_CONTROL_INTERROGATE:
1625pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
1627 PROCESS_INFORMATION pi;
1631 status.dwWin32ExitCode = S_OK;
1632 status.dwCheckPoint = 0;
1633 status.dwWaitHint = 60000;
1634 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1635 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
1636 status.dwServiceSpecificExitCode = 0;
1637 status.dwCurrentState = SERVICE_START_PENDING;
1639 memset(&pi, 0,
sizeof(pi));
1644 if ((hStatus = RegisterServiceCtrlHandler(
register_servicename, pgwin32_ServiceHandler)) == (SERVICE_STATUS_HANDLE) 0)
1647 if ((shutdownEvent = CreateEvent(NULL,
true,
false, NULL)) == NULL)
1651 pgwin32_SetServiceStatus(SERVICE_START_PENDING);
1652 if (!CreateRestrictedProcess(pgwin32_CommandLine(
false), &pi,
true))
1654 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1658 postmasterProcess = pi.hProcess;
1659 CloseHandle(pi.hThread);
1663 write_eventlog(EVENTLOG_INFORMATION_TYPE,
_(
"Waiting for server startup...\n"));
1666 write_eventlog(EVENTLOG_ERROR_TYPE,
_(
"Timed out waiting for server startup\n"));
1667 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1670 write_eventlog(EVENTLOG_INFORMATION_TYPE,
_(
"Server started and accepting connections\n"));
1673 pgwin32_SetServiceStatus(SERVICE_RUNNING);
1676 ret = WaitForMultipleObjects(2, shutdownHandles, FALSE, INFINITE);
1678 pgwin32_SetServiceStatus(SERVICE_STOP_PENDING);
1687 int maxShutdownCheckPoint = status.dwCheckPoint + 12;
1695 while (WaitForSingleObject(postmasterProcess, 5000) == WAIT_TIMEOUT && status.dwCheckPoint < maxShutdownCheckPoint)
1697 status.dwCheckPoint++;
1698 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
1703 case (WAIT_OBJECT_0 + 1):
1711 CloseHandle(shutdownEvent);
1712 CloseHandle(postmasterProcess);
1714 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1718pgwin32_doRunAsService(
void)
1723 if (StartServiceCtrlDispatcher(st) == 0)
1725 write_stderr(
_(
"%s: could not start service \"%s\": error code %lu\n"),
1727 (
unsigned long) GetLastError());
1744InheritStdHandles(STARTUPINFO *si)
1746 si->dwFlags |= STARTF_USESTDHANDLES;
1747 si->hStdInput = GetStdHandle(STD_INPUT_HANDLE);
1748 if (si->hStdInput == NULL)
1749 si->hStdInput = INVALID_HANDLE_VALUE;
1750 si->hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
1751 if (si->hStdOutput == NULL)
1752 si->hStdOutput = INVALID_HANDLE_VALUE;
1753 si->hStdError = GetStdHandle(STD_ERROR_HANDLE);
1754 if (si->hStdError == NULL)
1755 si->hStdError = INVALID_HANDLE_VALUE;
1768CreateRestrictedProcess(
char *cmd, PROCESS_INFORMATION *processInfo,
bool as_service)
1774 HANDLE restrictedToken;
1776 SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
1777 SID_AND_ATTRIBUTES dropSids[2];
1778 PTOKEN_PRIVILEGES delPrivs;
1780 ZeroMemory(&si,
sizeof(si));
1789 InheritStdHandles(&si);
1792 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &origToken))
1798 write_stderr(
_(
"%s: could not open process token: error code %lu\n"),
1799 progname, (
unsigned long) GetLastError());
1804 ZeroMemory(&dropSids,
sizeof(dropSids));
1805 if (!AllocateAndInitializeSid(&NtAuthority, 2,
1806 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
1807 0, &dropSids[0].Sid) ||
1808 !AllocateAndInitializeSid(&NtAuthority, 2,
1809 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
1810 0, &dropSids[1].Sid))
1812 write_stderr(
_(
"%s: could not allocate SIDs: error code %lu\n"),
1813 progname, (
unsigned long) GetLastError());
1818 delPrivs = GetPrivilegesToDelete(origToken);
1819 if (delPrivs == NULL)
1823 b = CreateRestrictedToken(origToken,
1825 sizeof(dropSids) /
sizeof(dropSids[0]),
1827 delPrivs->PrivilegeCount, delPrivs->Privileges,
1832 FreeSid(dropSids[1].Sid);
1833 FreeSid(dropSids[0].Sid);
1834 CloseHandle(origToken);
1838 write_stderr(
_(
"%s: could not create restricted token: error code %lu\n"),
1839 progname, (
unsigned long) GetLastError());
1844 r = CreateProcessAsUser(restrictedToken, NULL, cmd, NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, NULL, &si, processInfo);
1846 if (IsProcessInJob(processInfo->hProcess, NULL, &inJob))
1858 sprintf(jobname,
"PostgreSQL_%lu",
1859 (
unsigned long) processInfo->dwProcessId);
1861 job = CreateJobObject(NULL, jobname);
1864 JOBOBJECT_BASIC_LIMIT_INFORMATION basicLimit;
1865 JOBOBJECT_BASIC_UI_RESTRICTIONS uiRestrictions;
1866 JOBOBJECT_SECURITY_LIMIT_INFORMATION securityLimit;
1868 ZeroMemory(&basicLimit,
sizeof(basicLimit));
1869 ZeroMemory(&uiRestrictions,
sizeof(uiRestrictions));
1870 ZeroMemory(&securityLimit,
sizeof(securityLimit));
1872 basicLimit.LimitFlags = JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION | JOB_OBJECT_LIMIT_PRIORITY_CLASS;
1873 basicLimit.PriorityClass = NORMAL_PRIORITY_CLASS;
1874 SetInformationJobObject(job, JobObjectBasicLimitInformation, &basicLimit,
sizeof(basicLimit));
1876 uiRestrictions.UIRestrictionsClass = JOB_OBJECT_UILIMIT_DESKTOP | JOB_OBJECT_UILIMIT_DISPLAYSETTINGS |
1877 JOB_OBJECT_UILIMIT_EXITWINDOWS | JOB_OBJECT_UILIMIT_READCLIPBOARD |
1878 JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS | JOB_OBJECT_UILIMIT_WRITECLIPBOARD;
1880 SetInformationJobObject(job, JobObjectBasicUIRestrictions, &uiRestrictions,
sizeof(uiRestrictions));
1882 securityLimit.SecurityLimitFlags = JOB_OBJECT_SECURITY_NO_ADMIN | JOB_OBJECT_SECURITY_ONLY_TOKEN;
1883 securityLimit.JobToken = restrictedToken;
1884 SetInformationJobObject(job, JobObjectSecurityLimitInformation, &securityLimit,
sizeof(securityLimit));
1886 AssignProcessToJobObject(job, processInfo->hProcess);
1891 CloseHandle(restrictedToken);
1893 ResumeThread(processInfo->hThread);
1907static PTOKEN_PRIVILEGES
1908GetPrivilegesToDelete(HANDLE hToken)
1913 PTOKEN_PRIVILEGES tokenPrivs;
1915 LUID luidChangeNotify;
1917 if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &luidLockPages) ||
1918 !LookupPrivilegeValue(NULL, SE_CHANGE_NOTIFY_NAME, &luidChangeNotify))
1920 write_stderr(
_(
"%s: could not get LUIDs for privileges: error code %lu\n"),
1921 progname, (
unsigned long) GetLastError());
1925 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &length) &&
1926 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1928 write_stderr(
_(
"%s: could not get token information: error code %lu\n"),
1929 progname, (
unsigned long) GetLastError());
1935 if (tokenPrivs == NULL)
1941 if (!GetTokenInformation(hToken, TokenPrivileges, tokenPrivs, length, &length))
1943 write_stderr(
_(
"%s: could not get token information: error code %lu\n"),
1944 progname, (
unsigned long) GetLastError());
1949 for (
i = 0;
i < tokenPrivs->PrivilegeCount;
i++)
1951 if (memcmp(&tokenPrivs->Privileges[
i].Luid, &luidLockPages,
sizeof(LUID)) == 0 ||
1952 memcmp(&tokenPrivs->Privileges[
i].Luid, &luidChangeNotify,
sizeof(LUID)) == 0)
1954 for (
j =
i;
j < tokenPrivs->PrivilegeCount - 1;
j++)
1955 tokenPrivs->Privileges[
j] = tokenPrivs->Privileges[
j + 1];
1956 tokenPrivs->PrivilegeCount--;
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"));
2040 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2041 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2049 if (strcmp(modeopt,
"s") == 0 || strcmp(modeopt,
"smart") == 0)
2054 else if (strcmp(modeopt,
"f") == 0 || strcmp(modeopt,
"fast") == 0)
2059 else if (strcmp(modeopt,
"i") == 0 || strcmp(modeopt,
"immediate") == 0)
2077 if (strcmp(signame,
"HUP") == 0)
2079 else if (strcmp(signame,
"INT") == 0)
2081 else if (strcmp(signame,
"QUIT") == 0)
2083 else if (strcmp(signame,
"ABRT") == 0)
2085 else if (strcmp(signame,
"KILL") == 0)
2087 else if (strcmp(signame,
"TERM") == 0)
2089 else if (strcmp(signame,
"USR1") == 0)
2091 else if (strcmp(signame,
"USR2") == 0)
2104set_starttype(
char *starttypeopt)
2106 if (strcmp(starttypeopt,
"a") == 0 || strcmp(starttypeopt,
"auto") == 0)
2107 pgctl_start_type = SERVICE_AUTO_START;
2108 else if (strcmp(starttypeopt,
"d") == 0 || strcmp(starttypeopt,
"demand") == 0)
2109 pgctl_start_type = SERVICE_DEMAND_START;
2159 cmd =
psprintf(
"\"%s\" -C data_directory %s%s",
2165 fd = popen(cmd,
"r");
2168 write_stderr(
_(
"%s: could not determine the data directory using command \"%s\"\n"),
progname, cmd);
2195 ret = control_file_data->
state;
2196 pfree(control_file_data);
2204 static struct option long_options[] = {
2241 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
2246 else if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
2248 puts(
"pg_ctl (PostgreSQL) " PG_VERSION);
2260 "Please log in (using, e.g., \"su\") as the "
2261 "(unprivileged) user that will\n"
2262 "own the server process.\n"),
2268 env_wait = getenv(
"PGCTLTIMEOUT");
2269 if (env_wait != NULL)
2273 while ((
c =
getopt_long(argc, argv,
"cD:e:l:m:N:o:p:P:sS:t:U:wW",
2274 long_options, &option_index)) != -1)
2284 setenv(
"PGDATA", pgdata_D, 1);
2315 free(old_post_opts);
2331 write_stderr(
_(
"%s: -S option not supported on this platform\n"),
2341 if (strchr(
optarg,
'\\'))
2366 if (strcmp(argv[
optind],
"init") == 0
2367 || strcmp(argv[
optind],
"initdb") == 0)
2369 else if (strcmp(argv[
optind],
"start") == 0)
2371 else if (strcmp(argv[
optind],
"stop") == 0)
2373 else if (strcmp(argv[
optind],
"restart") == 0)
2375 else if (strcmp(argv[
optind],
"reload") == 0)
2377 else if (strcmp(argv[
optind],
"status") == 0)
2379 else if (strcmp(argv[
optind],
"promote") == 0)
2381 else if (strcmp(argv[
optind],
"logrotate") == 0)
2383 else if (strcmp(argv[
optind],
"kill") == 0)
2393 killproc = atol(argv[++
optind]);
2396 else if (strcmp(argv[
optind],
"register") == 0)
2398 else if (strcmp(argv[
optind],
"unregister") == 0)
2400 else if (strcmp(argv[
optind],
"runservice") == 0)
2442 write_stderr(
_(
"%s: no database directory specified and environment variable PGDATA unset\n"),
2502 pgwin32_doRegister();
2505 pgwin32_doUnregister();
2508 pgwin32_doRunAsService();
#define PG_TEXTDOMAIN(domain)
#define pg_attribute_printf(f, a)
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)
int main(int argc, char **argv)
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 write_stderr(const char *fmt,...) pg_attribute_printf(1
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)