68 #define DEFAULT_WAIT 60
70 #define USEC_PER_SEC 1000000
72 #define WAITS_PER_SEC 10
79 static int sig = SIGINT;
106 static DWORD pgctl_start_type = SERVICE_AUTO_START;
107 static SERVICE_STATUS status;
108 static SERVICE_STATUS_HANDLE hStatus = (SERVICE_STATUS_HANDLE) 0;
109 static HANDLE shutdownHandles[2];
111 #define shutdownEvent shutdownHandles[0]
112 #define postmasterProcess shutdownHandles[1]
119 static
void set_mode(
char *modeopt);
120 static
void set_sig(
char *signame);
129 static
void do_kill(pid_t pid);
134 #include <versionhelpers.h>
135 static bool pgwin32_IsInstalled(SC_HANDLE);
136 static char *pgwin32_CommandLine(
bool);
137 static void pgwin32_doRegister(
void);
138 static void pgwin32_doUnregister(
void);
139 static void pgwin32_SetServiceStatus(DWORD);
140 static void WINAPI pgwin32_ServiceHandler(DWORD);
141 static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *);
142 static void pgwin32_doRunAsService(
void);
143 static int CreateRestrictedProcess(
char *cmd, PROCESS_INFORMATION *processInfo,
bool as_service);
144 static PTOKEN_PRIVILEGES GetPrivilegesToDelete(HANDLE hToken);
147 static pid_t
get_pgpid(
bool is_status_request);
148 static char **
readfile(
const char *path,
int *numlines);
158 #if defined(HAVE_GETRLIMIT)
167 write_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,
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: %s\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;
665 if (waitpid(pm_pid, &exitstatus, WNOHANG) == pm_pid)
669 if (WaitForSingleObject(postmasterProcess, 0) == WAIT_OBJECT_0)
686 status.dwWaitHint += 6000;
687 status.dwCheckPoint++;
688 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
721 if (
kill(pid, 0) != 0)
758 if (
kill(pid, 0) != 0)
773 #if defined(HAVE_GETRLIMIT)
779 getrlimit(RLIMIT_CORE, &lim);
780 if (lim.rlim_max == 0)
782 write_stderr(
_(
"%s: cannot set core file size limit; disallowed by hard limit\n"),
786 else if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
788 lim.rlim_cur = lim.rlim_max;
789 setrlimit(RLIMIT_CORE, &lim);
806 if (optlines == NULL)
811 else if (numlines != 1)
813 write_stderr(
_(
"%s: option file \"%s\" must have exactly one line\n"),
822 optline = optlines[0];
828 if ((arg1 = strstr(optline,
" \"")) != NULL)
855 write_stderr(
_(
"%s: could not send stop signal (PID: %d): %s\n"),
864 raise(postgres_signal_arg);
883 write_stderr(
_(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"\n"),
886 write_stderr(
_(
"program \"%s\" was found by \"%s\" but was not the same version as %s\n"),
912 cmd =
psprintf(
"\"%s\" %s%s > \"%s\"",
916 if (system(cmd) != 0)
934 "trying to start server anyway\n"),
947 #if defined(HAVE_GETRLIMIT)
961 snprintf(env_var,
sizeof(env_var),
"%d", (
int) getppid());
962 setenv(
"PG_GRANDPARENT_PID", env_var, 1);
981 print_msg(
_(
"waiting for server to start..."));
998 "Examine the log output.\n"),
1009 CloseHandle(postmasterProcess);
1010 postmasterProcess = INVALID_HANDLE_VALUE;
1032 "single-user server is running (PID: %d)\n"),
1051 print_msg(
_(
"waiting for server to shut down..."));
1059 write_stderr(
_(
"HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1060 "waiting for session-initiated disconnection.\n"));
1096 "single-user server is running (PID: %d)\n"),
1098 write_stderr(
_(
"Please terminate the single-user server and try again.\n"));
1112 print_msg(
_(
"waiting for server to shut down..."));
1121 write_stderr(
_(
"HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1122 "waiting for session-initiated disconnection.\n"));
1131 write_stderr(
_(
"%s: old server process (PID: %d) seems to be gone\n"),
1155 "single-user server is running (PID: %d)\n"),
1157 write_stderr(
_(
"Please terminate the single-user server and try again.\n"));
1163 write_stderr(
_(
"%s: could not send reload signal (PID: %d): %s\n"),
1194 "single-user server is running (PID: %d)\n"),
1202 "server is not in standby mode\n"),
1211 write_stderr(
_(
"%s: could not create promote signal file \"%s\": %s\n"),
1215 if (fclose(prmfile))
1217 write_stderr(
_(
"%s: could not write promote signal file \"%s\": %s\n"),
1225 write_stderr(
_(
"%s: could not send promote signal (PID: %d): %s\n"),
1228 write_stderr(
_(
"%s: could not remove promote signal file \"%s\": %s\n"),
1235 print_msg(
_(
"waiting for server to promote..."));
1260 FILE *logrotatefile;
1275 "single-user server is running (PID: %d)\n"),
1284 write_stderr(
_(
"%s: could not create log rotation signal file \"%s\": %s\n"),
1288 if (fclose(logrotatefile))
1290 write_stderr(
_(
"%s: could not write log rotation signal file \"%s\": %s\n"),
1298 write_stderr(
_(
"%s: could not send log rotation signal (PID: %d): %s\n"),
1301 write_stderr(
_(
"%s: could not remove log rotation signal file \"%s\": %s\n"),
1306 print_msg(
_(
"server signaled to rotate log file\n"));
1327 if (pid == getpid())
1330 if (pid == getppid())
1333 if (
kill(pid, 0) == 0)
1353 printf(
_(
"%s: single-user server is running (PID: %d)\n"),
1367 printf(
_(
"%s: server is running (PID: %d)\n"),
1371 if (optlines != NULL)
1373 for (curr_line = optlines; *curr_line != NULL; curr_line++)
1400 write_stderr(
_(
"%s: could not send signal %d (PID: %d): %s\n"),
1409 pgwin32_IsInstalled(SC_HANDLE hSCM)
1412 bool bResult = (hService != NULL);
1415 CloseServiceHandle(hService);
1420 pgwin32_CommandLine(
bool registration)
1447 if (strlen(cmdPath) < 4 ||
1449 snprintf(cmdPath + strlen(cmdPath),
sizeof(cmdPath) - strlen(cmdPath),
1500 return cmdLine->
data;
1504 pgwin32_doRegister(
void)
1507 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
1514 if (pgwin32_IsInstalled(hSCM))
1516 CloseServiceHandle(hSCM);
1522 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1523 pgctl_start_type, SERVICE_ERROR_NORMAL,
1524 pgwin32_CommandLine(
true),
1527 CloseServiceHandle(hSCM);
1528 write_stderr(
_(
"%s: could not register service \"%s\": error code %lu\n"),
1530 (
unsigned long) GetLastError());
1533 CloseServiceHandle(hService);
1534 CloseServiceHandle(hSCM);
1538 pgwin32_doUnregister(
void)
1541 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
1548 if (!pgwin32_IsInstalled(hSCM))
1550 CloseServiceHandle(hSCM);
1557 CloseServiceHandle(hSCM);
1558 write_stderr(
_(
"%s: could not open service \"%s\": error code %lu\n"),
1560 (
unsigned long) GetLastError());
1563 if (!DeleteService(hService))
1565 CloseServiceHandle(hService);
1566 CloseServiceHandle(hSCM);
1567 write_stderr(
_(
"%s: could not unregister service \"%s\": error code %lu\n"),
1569 (
unsigned long) GetLastError());
1572 CloseServiceHandle(hService);
1573 CloseServiceHandle(hSCM);
1577 pgwin32_SetServiceStatus(DWORD currentState)
1579 status.dwCurrentState = currentState;
1580 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
1584 pgwin32_ServiceHandler(DWORD request)
1588 case SERVICE_CONTROL_STOP:
1589 case SERVICE_CONTROL_SHUTDOWN:
1596 status.dwWaitHint = 10000;
1597 pgwin32_SetServiceStatus(SERVICE_STOP_PENDING);
1598 SetEvent(shutdownEvent);
1601 case SERVICE_CONTROL_PAUSE:
1603 status.dwWaitHint = 5000;
1608 case SERVICE_CONTROL_CONTINUE:
1609 case SERVICE_CONTROL_INTERROGATE:
1616 pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
1618 PROCESS_INFORMATION pi;
1622 status.dwWin32ExitCode = S_OK;
1623 status.dwCheckPoint = 0;
1624 status.dwWaitHint = 60000;
1625 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1626 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
1627 status.dwServiceSpecificExitCode = 0;
1628 status.dwCurrentState = SERVICE_START_PENDING;
1630 memset(&pi, 0,
sizeof(pi));
1635 if ((hStatus = RegisterServiceCtrlHandler(
register_servicename, pgwin32_ServiceHandler)) == (SERVICE_STATUS_HANDLE) 0)
1638 if ((shutdownEvent = CreateEvent(NULL,
true,
false, NULL)) == NULL)
1642 pgwin32_SetServiceStatus(SERVICE_START_PENDING);
1643 if (!CreateRestrictedProcess(pgwin32_CommandLine(
false), &pi,
true))
1645 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1649 postmasterProcess = pi.hProcess;
1650 CloseHandle(pi.hThread);
1654 write_eventlog(EVENTLOG_INFORMATION_TYPE,
_(
"Waiting for server startup...\n"));
1657 write_eventlog(EVENTLOG_ERROR_TYPE,
_(
"Timed out waiting for server startup\n"));
1658 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1661 write_eventlog(EVENTLOG_INFORMATION_TYPE,
_(
"Server started and accepting connections\n"));
1664 pgwin32_SetServiceStatus(SERVICE_RUNNING);
1667 ret = WaitForMultipleObjects(2, shutdownHandles, FALSE, INFINITE);
1669 pgwin32_SetServiceStatus(SERVICE_STOP_PENDING);
1678 int maxShutdownCheckPoint = status.dwCheckPoint + 12;
1686 while (WaitForSingleObject(postmasterProcess, 5000) == WAIT_TIMEOUT && status.dwCheckPoint < maxShutdownCheckPoint)
1688 status.dwCheckPoint++;
1689 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
1694 case (WAIT_OBJECT_0 + 1):
1702 CloseHandle(shutdownEvent);
1703 CloseHandle(postmasterProcess);
1705 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1709 pgwin32_doRunAsService(
void)
1714 if (StartServiceCtrlDispatcher(st) == 0)
1716 write_stderr(
_(
"%s: could not start service \"%s\": error code %lu\n"),
1718 (
unsigned long) GetLastError());
1735 InheritStdHandles(STARTUPINFO *si)
1737 si->dwFlags |= STARTF_USESTDHANDLES;
1738 si->hStdInput = GetStdHandle(STD_INPUT_HANDLE);
1739 if (si->hStdInput == NULL)
1740 si->hStdInput = INVALID_HANDLE_VALUE;
1741 si->hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
1742 if (si->hStdOutput == NULL)
1743 si->hStdOutput = INVALID_HANDLE_VALUE;
1744 si->hStdError = GetStdHandle(STD_ERROR_HANDLE);
1745 if (si->hStdError == NULL)
1746 si->hStdError = INVALID_HANDLE_VALUE;
1759 CreateRestrictedProcess(
char *cmd, PROCESS_INFORMATION *processInfo,
bool as_service)
1765 HANDLE restrictedToken;
1767 SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
1768 SID_AND_ATTRIBUTES dropSids[2];
1769 PTOKEN_PRIVILEGES delPrivs;
1771 ZeroMemory(&si,
sizeof(si));
1780 InheritStdHandles(&si);
1783 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &origToken))
1789 write_stderr(
_(
"%s: could not open process token: error code %lu\n"),
1790 progname, (
unsigned long) GetLastError());
1795 ZeroMemory(&dropSids,
sizeof(dropSids));
1796 if (!AllocateAndInitializeSid(&NtAuthority, 2,
1797 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
1798 0, &dropSids[0].Sid) ||
1799 !AllocateAndInitializeSid(&NtAuthority, 2,
1800 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
1801 0, &dropSids[1].Sid))
1803 write_stderr(
_(
"%s: could not allocate SIDs: error code %lu\n"),
1804 progname, (
unsigned long) GetLastError());
1809 delPrivs = GetPrivilegesToDelete(origToken);
1810 if (delPrivs == NULL)
1814 b = CreateRestrictedToken(origToken,
1816 sizeof(dropSids) /
sizeof(dropSids[0]),
1818 delPrivs->PrivilegeCount, delPrivs->Privileges,
1823 FreeSid(dropSids[1].Sid);
1824 FreeSid(dropSids[0].Sid);
1825 CloseHandle(origToken);
1829 write_stderr(
_(
"%s: could not create restricted token: error code %lu\n"),
1830 progname, (
unsigned long) GetLastError());
1835 r = CreateProcessAsUser(restrictedToken, NULL, cmd, NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, NULL, &si, processInfo);
1837 if (IsProcessInJob(processInfo->hProcess, NULL, &inJob))
1849 sprintf(jobname,
"PostgreSQL_%lu",
1850 (
unsigned long) processInfo->dwProcessId);
1852 job = CreateJobObject(NULL, jobname);
1855 JOBOBJECT_BASIC_LIMIT_INFORMATION basicLimit;
1856 JOBOBJECT_BASIC_UI_RESTRICTIONS uiRestrictions;
1857 JOBOBJECT_SECURITY_LIMIT_INFORMATION securityLimit;
1859 ZeroMemory(&basicLimit,
sizeof(basicLimit));
1860 ZeroMemory(&uiRestrictions,
sizeof(uiRestrictions));
1861 ZeroMemory(&securityLimit,
sizeof(securityLimit));
1863 basicLimit.LimitFlags = JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION | JOB_OBJECT_LIMIT_PRIORITY_CLASS;
1864 basicLimit.PriorityClass = NORMAL_PRIORITY_CLASS;
1865 SetInformationJobObject(job, JobObjectBasicLimitInformation, &basicLimit,
sizeof(basicLimit));
1867 uiRestrictions.UIRestrictionsClass = JOB_OBJECT_UILIMIT_DESKTOP | JOB_OBJECT_UILIMIT_DISPLAYSETTINGS |
1868 JOB_OBJECT_UILIMIT_EXITWINDOWS | JOB_OBJECT_UILIMIT_READCLIPBOARD |
1869 JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS | JOB_OBJECT_UILIMIT_WRITECLIPBOARD;
1871 SetInformationJobObject(job, JobObjectBasicUIRestrictions, &uiRestrictions,
sizeof(uiRestrictions));
1873 securityLimit.SecurityLimitFlags = JOB_OBJECT_SECURITY_NO_ADMIN | JOB_OBJECT_SECURITY_ONLY_TOKEN;
1874 securityLimit.JobToken = restrictedToken;
1875 SetInformationJobObject(job, JobObjectSecurityLimitInformation, &securityLimit,
sizeof(securityLimit));
1877 AssignProcessToJobObject(job, processInfo->hProcess);
1882 CloseHandle(restrictedToken);
1884 ResumeThread(processInfo->hThread);
1898 static PTOKEN_PRIVILEGES
1899 GetPrivilegesToDelete(HANDLE hToken)
1904 PTOKEN_PRIVILEGES tokenPrivs;
1906 LUID luidChangeNotify;
1908 if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &luidLockPages) ||
1909 !LookupPrivilegeValue(NULL, SE_CHANGE_NOTIFY_NAME, &luidChangeNotify))
1911 write_stderr(
_(
"%s: could not get LUIDs for privileges: error code %lu\n"),
1912 progname, (
unsigned long) GetLastError());
1916 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &length) &&
1917 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1919 write_stderr(
_(
"%s: could not get token information: error code %lu\n"),
1920 progname, (
unsigned long) GetLastError());
1926 if (tokenPrivs == NULL)
1932 if (!GetTokenInformation(hToken, TokenPrivileges, tokenPrivs, length, &length))
1934 write_stderr(
_(
"%s: could not get token information: error code %lu\n"),
1935 progname, (
unsigned long) GetLastError());
1940 for (
i = 0;
i < tokenPrivs->PrivilegeCount;
i++)
1942 if (memcmp(&tokenPrivs->Privileges[
i].Luid, &luidLockPages,
sizeof(LUID)) == 0 ||
1943 memcmp(&tokenPrivs->Privileges[
i].Luid, &luidChangeNotify,
sizeof(LUID)) == 0)
1945 for (
j =
i;
j < tokenPrivs->PrivilegeCount - 1;
j++)
1946 tokenPrivs->Privileges[
j] = tokenPrivs->Privileges[
j + 1];
1947 tokenPrivs->PrivilegeCount--;
1966 printf(
_(
"%s is a utility to initialize, start, stop, or control a PostgreSQL server.\n\n"),
progname);
1969 printf(
_(
" %s start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]\n"
1970 " [-o OPTIONS] [-p PATH] [-c]\n"),
progname);
1971 printf(
_(
" %s stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"),
progname);
1972 printf(
_(
" %s restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"
1973 " [-o OPTIONS] [-c]\n"),
progname);
1976 printf(
_(
" %s promote [-D DATADIR] [-W] [-t SECS] [-s]\n"),
progname);
1980 printf(
_(
" %s register [-D DATADIR] [-N SERVICENAME] [-U USERNAME] [-P PASSWORD]\n"
1981 " [-S START-TYPE] [-e SOURCE] [-W] [-t SECS] [-s] [-o OPTIONS]\n"),
progname);
1985 printf(
_(
"\nCommon options:\n"));
1986 printf(
_(
" -D, --pgdata=DATADIR location of the database storage area\n"));
1988 printf(
_(
" -e SOURCE event source for logging when running as a service\n"));
1990 printf(
_(
" -s, --silent only print errors, no informational messages\n"));
1991 printf(
_(
" -t, --timeout=SECS seconds to wait when using -w option\n"));
1992 printf(
_(
" -V, --version output version information, then exit\n"));
1993 printf(
_(
" -w, --wait wait until operation completes (default)\n"));
1994 printf(
_(
" -W, --no-wait do not wait until operation completes\n"));
1995 printf(
_(
" -?, --help show this help, then exit\n"));
1996 printf(
_(
"If the -D option is omitted, the environment variable PGDATA is used.\n"));
1998 printf(
_(
"\nOptions for start or restart:\n"));
1999 #if defined(HAVE_GETRLIMIT)
2000 printf(
_(
" -c, --core-files allow postgres to produce core files\n"));
2002 printf(
_(
" -c, --core-files not applicable on this platform\n"));
2004 printf(
_(
" -l, --log=FILENAME write (or append) server log to FILENAME\n"));
2005 printf(
_(
" -o, --options=OPTIONS command line options to pass to postgres\n"
2006 " (PostgreSQL server executable) or initdb\n"));
2007 printf(
_(
" -p PATH-TO-POSTGRES normally not necessary\n"));
2008 printf(
_(
"\nOptions for stop or restart:\n"));
2009 printf(
_(
" -m, --mode=MODE MODE can be \"smart\", \"fast\", or \"immediate\"\n"));
2011 printf(
_(
"\nShutdown modes are:\n"));
2012 printf(
_(
" smart quit after all clients have disconnected\n"));
2013 printf(
_(
" fast quit directly, with proper shutdown (default)\n"));
2014 printf(
_(
" immediate quit without complete shutdown; will lead to recovery on restart\n"));
2016 printf(
_(
"\nAllowed signal names for kill:\n"));
2017 printf(
" ABRT HUP INT KILL QUIT TERM USR1 USR2\n");
2020 printf(
_(
"\nOptions for register and unregister:\n"));
2021 printf(
_(
" -N SERVICENAME service name with which to register PostgreSQL server\n"));
2022 printf(
_(
" -P PASSWORD password of account to register PostgreSQL server\n"));
2023 printf(
_(
" -U USERNAME user name of account to register PostgreSQL server\n"));
2024 printf(
_(
" -S START-TYPE service start type to register PostgreSQL server\n"));
2026 printf(
_(
"\nStart types are:\n"));
2027 printf(
_(
" auto start service automatically during system startup (default)\n"));
2028 printf(
_(
" demand start service on demand\n"));
2031 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2032 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2040 if (strcmp(modeopt,
"s") == 0 || strcmp(modeopt,
"smart") == 0)
2045 else if (strcmp(modeopt,
"f") == 0 || strcmp(modeopt,
"fast") == 0)
2050 else if (strcmp(modeopt,
"i") == 0 || strcmp(modeopt,
"immediate") == 0)
2068 if (strcmp(signame,
"HUP") == 0)
2070 else if (strcmp(signame,
"INT") == 0)
2072 else if (strcmp(signame,
"QUIT") == 0)
2074 else if (strcmp(signame,
"ABRT") == 0)
2076 else if (strcmp(signame,
"KILL") == 0)
2078 else if (strcmp(signame,
"TERM") == 0)
2080 else if (strcmp(signame,
"USR1") == 0)
2082 else if (strcmp(signame,
"USR2") == 0)
2095 set_starttype(
char *starttypeopt)
2097 if (strcmp(starttypeopt,
"a") == 0 || strcmp(starttypeopt,
"auto") == 0)
2098 pgctl_start_type = SERVICE_AUTO_START;
2099 else if (strcmp(starttypeopt,
"d") == 0 || strcmp(starttypeopt,
"demand") == 0)
2100 pgctl_start_type = SERVICE_DEMAND_START;
2150 cmd =
psprintf(
"\"%s\" -C data_directory %s%s",
2156 fd = popen(cmd,
"r");
2159 write_stderr(
_(
"%s: could not determine the data directory using command \"%s\"\n"),
progname, cmd);
2186 ret = control_file_data->
state;
2187 pfree(control_file_data);
2195 static struct option long_options[] = {
2232 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
2237 else if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
2239 puts(
"pg_ctl (PostgreSQL) " PG_VERSION);
2251 "Please log in (using, e.g., \"su\") as the "
2252 "(unprivileged) user that will\n"
2253 "own the server process.\n"),
2259 env_wait = getenv(
"PGCTLTIMEOUT");
2260 if (env_wait != NULL)
2264 while ((
c =
getopt_long(argc, argv,
"cD:e:l:m:N:o:p:P:sS:t:U:wW",
2265 long_options, &option_index)) != -1)
2275 setenv(
"PGDATA", pgdata_D, 1);
2306 free(old_post_opts);
2322 write_stderr(
_(
"%s: -S option not supported on this platform\n"),
2332 if (strchr(
optarg,
'\\'))
2357 if (strcmp(argv[
optind],
"init") == 0
2358 || strcmp(argv[
optind],
"initdb") == 0)
2360 else if (strcmp(argv[
optind],
"start") == 0)
2362 else if (strcmp(argv[
optind],
"stop") == 0)
2364 else if (strcmp(argv[
optind],
"restart") == 0)
2366 else if (strcmp(argv[
optind],
"reload") == 0)
2368 else if (strcmp(argv[
optind],
"status") == 0)
2370 else if (strcmp(argv[
optind],
"promote") == 0)
2372 else if (strcmp(argv[
optind],
"logrotate") == 0)
2374 else if (strcmp(argv[
optind],
"kill") == 0)
2384 killproc = atol(argv[++
optind]);
2387 else if (strcmp(argv[
optind],
"register") == 0)
2389 else if (strcmp(argv[
optind],
"unregister") == 0)
2391 else if (strcmp(argv[
optind],
"runservice") == 0)
2433 write_stderr(
_(
"%s: no database directory specified and environment variable PGDATA unset\n"),
2494 pgwin32_doRegister();
2497 pgwin32_doUnregister();
2500 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_extended(size_t size, int flags)
char * pg_strdup(const char *in)
void * pg_malloc(size_t size)
#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]
static void const char * fmt
static void const char fflush(stdout)
vfprintf(stderr, fmt, args)
void pg_logging_init(const char *argv0)
void pfree(void *pointer)
#define DEFAULT_EVENT_SOURCE
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 * find_other_exec_or_die(const char *argv0, const char *target, const char *versionstr)
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
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_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 char backup_file[MAXPGPATH]
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
char * make_absolute_path(const char *path)
int pg_strcasecmp(const char *s1, const char *s2)
void canonicalize_path(char *path)
const char * get_progname(const char *argv0)
pqsigfunc pqsignal(int signo, pqsigfunc func)
void make_native_path(char *filename)
#define PG_BACKEND_VERSIONSTR
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)