68 #define DEFAULT_WAIT 60
70 #define USEC_PER_SEC 1000000
72 #define WAITS_PER_SEC 10
79 static int sig = SIGINT;
105 static DWORD pgctl_start_type = SERVICE_AUTO_START;
106 static SERVICE_STATUS status;
107 static SERVICE_STATUS_HANDLE hStatus = (SERVICE_STATUS_HANDLE) 0;
108 static HANDLE shutdownHandles[2];
110 #define shutdownEvent shutdownHandles[0]
111 #define postmasterProcess shutdownHandles[1]
118 static
void set_mode(
char *modeopt);
119 static
void set_sig(
char *signame);
128 static
void do_kill(pid_t pid);
133 #include <versionhelpers.h>
134 static bool pgwin32_IsInstalled(SC_HANDLE);
135 static char *pgwin32_CommandLine(
bool);
136 static void pgwin32_doRegister(
void);
137 static void pgwin32_doUnregister(
void);
138 static void pgwin32_SetServiceStatus(DWORD);
139 static void WINAPI pgwin32_ServiceHandler(DWORD);
140 static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *);
141 static void pgwin32_doRunAsService(
void);
142 static int CreateRestrictedProcess(
char *cmd, PROCESS_INFORMATION *processInfo,
bool as_service);
143 static PTOKEN_PRIVILEGES GetPrivilegesToDelete(HANDLE hToken);
146 static pid_t
get_pgpid(
bool is_status_request);
147 static char **
readfile(
const char *path,
int *numlines);
157 #if defined(HAVE_GETRLIMIT)
166 write_eventlog(
int level,
const char *line)
168 static HANDLE evtHandle = INVALID_HANDLE_VALUE;
170 if (
silent_mode && level == EVENTLOG_INFORMATION_TYPE)
173 if (evtHandle == INVALID_HANDLE_VALUE)
175 evtHandle = RegisterEventSource(NULL,
177 if (evtHandle == NULL)
179 evtHandle = INVALID_HANDLE_VALUE;
184 ReportEvent(evtHandle,
221 write_eventlog(EVENTLOG_ERROR_TYPE, errbuf);
265 exit(is_status_request ? 4 : 1);
270 write_stderr(
_(
"%s: directory \"%s\" is not a database cluster directory\n"),
272 exit(is_status_request ? 4 : 1);
288 if (fscanf(pidf,
"%d", &pid) != 1)
291 if (ftell(pidf) == 0 && feof(pidf))
347 result = (
char **)
pg_malloc(
sizeof(
char *));
370 if (buffer[
i] ==
'\n')
375 result = (
char **)
pg_malloc((nlines + 1) *
sizeof(
char *));
383 if (buffer[
i] ==
'\n')
385 int slen = &buffer[
i] - linebegin;
388 memcpy(linebuf, linebegin, slen);
390 if (slen > 0 && linebuf[slen - 1] ==
'\r')
392 linebuf[slen] =
'\0';
393 result[n++] = linebuf;
394 linebegin = &buffer[
i + 1];
411 char *curr_line = NULL;
417 while ((curr_line = optlines[
i++]))
477 write_stderr(
_(
"%s: could not start server due to setsid() failure: %m\n"),
489 cmd =
psprintf(
"exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
493 cmd =
psprintf(
"exec \"%s\" %s%s < \"%s\" 2>&1",
496 (void) execl(
"/bin/sh",
"/bin/sh",
"-c", cmd, (
char *) NULL);
512 PROCESS_INFORMATION pi;
516 comspec = getenv(
"COMSPEC");
555 cmd =
psprintf(
"\"%s\" /D /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
559 cmd =
psprintf(
"\"%s\" /D /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
562 if (!CreateRestrictedProcess(cmd, &pi,
false))
564 write_stderr(
_(
"%s: could not start server: error code %lu\n"),
565 progname, (
unsigned long) GetLastError());
569 postmasterProcess = pi.hProcess;
570 CloseHandle(pi.hThread);
571 return pi.dwProcessId;
664 if (waitpid(pm_pid, &exitstatus, WNOHANG) == pm_pid)
668 if (WaitForSingleObject(postmasterProcess, 0) == WAIT_OBJECT_0)
685 status.dwWaitHint += 6000;
686 status.dwCheckPoint++;
687 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
720 if (
kill(pid, 0) != 0)
757 if (
kill(pid, 0) != 0)
772 #if defined(HAVE_GETRLIMIT)
778 getrlimit(RLIMIT_CORE, &lim);
779 if (lim.rlim_max == 0)
781 write_stderr(
_(
"%s: cannot set core file size limit; disallowed by hard limit\n"),
785 else if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
787 lim.rlim_cur = lim.rlim_max;
788 setrlimit(RLIMIT_CORE, &lim);
805 if (optlines == NULL)
810 else if (numlines != 1)
812 write_stderr(
_(
"%s: option file \"%s\" must have exactly one line\n"),
821 optline = optlines[0];
827 if ((arg1 = strstr(optline,
" \"")) != NULL)
854 write_stderr(
_(
"%s: could not send stop signal (PID: %d): %m\n"),
863 raise(postgres_signal_arg);
882 write_stderr(
_(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"\n"),
885 write_stderr(
_(
"program \"%s\" was found by \"%s\" but was not the same version as %s\n"),
911 cmd =
psprintf(
"\"%s\" %s%s > \"%s\"",
915 if (system(cmd) != 0)
933 "trying to start server anyway\n"),
946 #if defined(HAVE_GETRLIMIT)
960 snprintf(env_var,
sizeof(env_var),
"%d", (
int) getppid());
961 setenv(
"PG_GRANDPARENT_PID", env_var, 1);
980 print_msg(
_(
"waiting for server to start..."));
997 "Examine the log output.\n"),
1008 CloseHandle(postmasterProcess);
1009 postmasterProcess = INVALID_HANDLE_VALUE;
1031 "single-user server is running (PID: %d)\n"),
1049 print_msg(
_(
"waiting for server to shut down..."));
1057 write_stderr(
_(
"HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1058 "waiting for session-initiated disconnection.\n"));
1094 "single-user server is running (PID: %d)\n"),
1096 write_stderr(
_(
"Please terminate the single-user server and try again.\n"));
1109 print_msg(
_(
"waiting for server to shut down..."));
1118 write_stderr(
_(
"HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1119 "waiting for session-initiated disconnection.\n"));
1128 write_stderr(
_(
"%s: old server process (PID: %d) seems to be gone\n"),
1152 "single-user server is running (PID: %d)\n"),
1154 write_stderr(
_(
"Please terminate the single-user server and try again.\n"));
1160 write_stderr(
_(
"%s: could not send reload signal (PID: %d): %m\n"),
1191 "single-user server is running (PID: %d)\n"),
1199 "server is not in standby mode\n"),
1208 write_stderr(
_(
"%s: could not create promote signal file \"%s\": %m\n"),
1212 if (fclose(prmfile))
1214 write_stderr(
_(
"%s: could not write promote signal file \"%s\": %m\n"),
1222 write_stderr(
_(
"%s: could not send promote signal (PID: %d): %m\n"),
1225 write_stderr(
_(
"%s: could not remove promote signal file \"%s\": %m\n"),
1232 print_msg(
_(
"waiting for server to promote..."));
1257 FILE *logrotatefile;
1272 "single-user server is running (PID: %d)\n"),
1281 write_stderr(
_(
"%s: could not create log rotation signal file \"%s\": %m\n"),
1285 if (fclose(logrotatefile))
1287 write_stderr(
_(
"%s: could not write log rotation signal file \"%s\": %m\n"),
1295 write_stderr(
_(
"%s: could not send log rotation signal (PID: %d): %m\n"),
1298 write_stderr(
_(
"%s: could not remove log rotation signal file \"%s\": %m\n"),
1303 print_msg(
_(
"server signaled to rotate log file\n"));
1324 if (pid == getpid())
1327 if (pid == getppid())
1330 if (
kill(pid, 0) == 0)
1350 printf(
_(
"%s: single-user server is running (PID: %d)\n"),
1364 printf(
_(
"%s: server is running (PID: %d)\n"),
1368 if (optlines != NULL)
1370 for (curr_line = optlines; *curr_line != NULL; curr_line++)
1397 write_stderr(
_(
"%s: could not send signal %d (PID: %d): %m\n"),
1406 pgwin32_IsInstalled(SC_HANDLE hSCM)
1409 bool bResult = (hService != NULL);
1412 CloseServiceHandle(hService);
1417 pgwin32_CommandLine(
bool registration)
1444 if (strlen(cmdPath) < 4 ||
1446 snprintf(cmdPath + strlen(cmdPath),
sizeof(cmdPath) - strlen(cmdPath),
1497 return cmdLine->
data;
1501 pgwin32_doRegister(
void)
1504 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
1511 if (pgwin32_IsInstalled(hSCM))
1513 CloseServiceHandle(hSCM);
1519 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1520 pgctl_start_type, SERVICE_ERROR_NORMAL,
1521 pgwin32_CommandLine(
true),
1524 CloseServiceHandle(hSCM);
1525 write_stderr(
_(
"%s: could not register service \"%s\": error code %lu\n"),
1527 (
unsigned long) GetLastError());
1530 CloseServiceHandle(hService);
1531 CloseServiceHandle(hSCM);
1535 pgwin32_doUnregister(
void)
1538 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
1545 if (!pgwin32_IsInstalled(hSCM))
1547 CloseServiceHandle(hSCM);
1554 CloseServiceHandle(hSCM);
1555 write_stderr(
_(
"%s: could not open service \"%s\": error code %lu\n"),
1557 (
unsigned long) GetLastError());
1560 if (!DeleteService(hService))
1562 CloseServiceHandle(hService);
1563 CloseServiceHandle(hSCM);
1564 write_stderr(
_(
"%s: could not unregister service \"%s\": error code %lu\n"),
1566 (
unsigned long) GetLastError());
1569 CloseServiceHandle(hService);
1570 CloseServiceHandle(hSCM);
1574 pgwin32_SetServiceStatus(DWORD currentState)
1576 status.dwCurrentState = currentState;
1577 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
1581 pgwin32_ServiceHandler(DWORD request)
1585 case SERVICE_CONTROL_STOP:
1586 case SERVICE_CONTROL_SHUTDOWN:
1593 status.dwWaitHint = 10000;
1594 pgwin32_SetServiceStatus(SERVICE_STOP_PENDING);
1595 SetEvent(shutdownEvent);
1598 case SERVICE_CONTROL_PAUSE:
1600 status.dwWaitHint = 5000;
1605 case SERVICE_CONTROL_CONTINUE:
1606 case SERVICE_CONTROL_INTERROGATE:
1613 pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
1615 PROCESS_INFORMATION pi;
1619 status.dwWin32ExitCode = S_OK;
1620 status.dwCheckPoint = 0;
1621 status.dwWaitHint = 60000;
1622 status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1623 status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
1624 status.dwServiceSpecificExitCode = 0;
1625 status.dwCurrentState = SERVICE_START_PENDING;
1627 memset(&pi, 0,
sizeof(pi));
1632 if ((hStatus = RegisterServiceCtrlHandler(
register_servicename, pgwin32_ServiceHandler)) == (SERVICE_STATUS_HANDLE) 0)
1635 if ((shutdownEvent = CreateEvent(NULL,
true,
false, NULL)) == NULL)
1639 pgwin32_SetServiceStatus(SERVICE_START_PENDING);
1640 if (!CreateRestrictedProcess(pgwin32_CommandLine(
false), &pi,
true))
1642 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1646 postmasterProcess = pi.hProcess;
1647 CloseHandle(pi.hThread);
1651 write_eventlog(EVENTLOG_INFORMATION_TYPE,
_(
"Waiting for server startup...\n"));
1654 write_eventlog(EVENTLOG_ERROR_TYPE,
_(
"Timed out waiting for server startup\n"));
1655 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1658 write_eventlog(EVENTLOG_INFORMATION_TYPE,
_(
"Server started and accepting connections\n"));
1661 pgwin32_SetServiceStatus(SERVICE_RUNNING);
1664 ret = WaitForMultipleObjects(2, shutdownHandles, FALSE, INFINITE);
1666 pgwin32_SetServiceStatus(SERVICE_STOP_PENDING);
1675 int maxShutdownCheckPoint = status.dwCheckPoint + 12;
1683 while (WaitForSingleObject(postmasterProcess, 5000) == WAIT_TIMEOUT && status.dwCheckPoint < maxShutdownCheckPoint)
1685 status.dwCheckPoint++;
1686 SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
1691 case (WAIT_OBJECT_0 + 1):
1699 CloseHandle(shutdownEvent);
1700 CloseHandle(postmasterProcess);
1702 pgwin32_SetServiceStatus(SERVICE_STOPPED);
1706 pgwin32_doRunAsService(
void)
1711 if (StartServiceCtrlDispatcher(st) == 0)
1713 write_stderr(
_(
"%s: could not start service \"%s\": error code %lu\n"),
1715 (
unsigned long) GetLastError());
1732 InheritStdHandles(STARTUPINFO *si)
1734 si->dwFlags |= STARTF_USESTDHANDLES;
1735 si->hStdInput = GetStdHandle(STD_INPUT_HANDLE);
1736 if (si->hStdInput == NULL)
1737 si->hStdInput = INVALID_HANDLE_VALUE;
1738 si->hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
1739 if (si->hStdOutput == NULL)
1740 si->hStdOutput = INVALID_HANDLE_VALUE;
1741 si->hStdError = GetStdHandle(STD_ERROR_HANDLE);
1742 if (si->hStdError == NULL)
1743 si->hStdError = INVALID_HANDLE_VALUE;
1756 CreateRestrictedProcess(
char *cmd, PROCESS_INFORMATION *processInfo,
bool as_service)
1762 HANDLE restrictedToken;
1764 SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
1765 SID_AND_ATTRIBUTES dropSids[2];
1766 PTOKEN_PRIVILEGES delPrivs;
1768 ZeroMemory(&si,
sizeof(si));
1777 InheritStdHandles(&si);
1780 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &origToken))
1786 write_stderr(
_(
"%s: could not open process token: error code %lu\n"),
1787 progname, (
unsigned long) GetLastError());
1792 ZeroMemory(&dropSids,
sizeof(dropSids));
1793 if (!AllocateAndInitializeSid(&NtAuthority, 2,
1794 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
1795 0, &dropSids[0].Sid) ||
1796 !AllocateAndInitializeSid(&NtAuthority, 2,
1797 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
1798 0, &dropSids[1].Sid))
1800 write_stderr(
_(
"%s: could not allocate SIDs: error code %lu\n"),
1801 progname, (
unsigned long) GetLastError());
1806 delPrivs = GetPrivilegesToDelete(origToken);
1807 if (delPrivs == NULL)
1811 b = CreateRestrictedToken(origToken,
1813 sizeof(dropSids) /
sizeof(dropSids[0]),
1815 delPrivs->PrivilegeCount, delPrivs->Privileges,
1820 FreeSid(dropSids[1].Sid);
1821 FreeSid(dropSids[0].Sid);
1822 CloseHandle(origToken);
1826 write_stderr(
_(
"%s: could not create restricted token: error code %lu\n"),
1827 progname, (
unsigned long) GetLastError());
1832 r = CreateProcessAsUser(restrictedToken, NULL, cmd, NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, NULL, &si, processInfo);
1834 if (IsProcessInJob(processInfo->hProcess, NULL, &inJob))
1846 sprintf(jobname,
"PostgreSQL_%lu",
1847 (
unsigned long) processInfo->dwProcessId);
1849 job = CreateJobObject(NULL, jobname);
1852 JOBOBJECT_BASIC_LIMIT_INFORMATION basicLimit;
1853 JOBOBJECT_BASIC_UI_RESTRICTIONS uiRestrictions;
1854 JOBOBJECT_SECURITY_LIMIT_INFORMATION securityLimit;
1856 ZeroMemory(&basicLimit,
sizeof(basicLimit));
1857 ZeroMemory(&uiRestrictions,
sizeof(uiRestrictions));
1858 ZeroMemory(&securityLimit,
sizeof(securityLimit));
1860 basicLimit.LimitFlags = JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION | JOB_OBJECT_LIMIT_PRIORITY_CLASS;
1861 basicLimit.PriorityClass = NORMAL_PRIORITY_CLASS;
1862 SetInformationJobObject(job, JobObjectBasicLimitInformation, &basicLimit,
sizeof(basicLimit));
1864 uiRestrictions.UIRestrictionsClass = JOB_OBJECT_UILIMIT_DESKTOP | JOB_OBJECT_UILIMIT_DISPLAYSETTINGS |
1865 JOB_OBJECT_UILIMIT_EXITWINDOWS | JOB_OBJECT_UILIMIT_READCLIPBOARD |
1866 JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS | JOB_OBJECT_UILIMIT_WRITECLIPBOARD;
1868 SetInformationJobObject(job, JobObjectBasicUIRestrictions, &uiRestrictions,
sizeof(uiRestrictions));
1870 securityLimit.SecurityLimitFlags = JOB_OBJECT_SECURITY_NO_ADMIN | JOB_OBJECT_SECURITY_ONLY_TOKEN;
1871 securityLimit.JobToken = restrictedToken;
1872 SetInformationJobObject(job, JobObjectSecurityLimitInformation, &securityLimit,
sizeof(securityLimit));
1874 AssignProcessToJobObject(job, processInfo->hProcess);
1879 CloseHandle(restrictedToken);
1881 ResumeThread(processInfo->hThread);
1895 static PTOKEN_PRIVILEGES
1896 GetPrivilegesToDelete(HANDLE hToken)
1901 PTOKEN_PRIVILEGES tokenPrivs;
1903 LUID luidChangeNotify;
1905 if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &luidLockPages) ||
1906 !LookupPrivilegeValue(NULL, SE_CHANGE_NOTIFY_NAME, &luidChangeNotify))
1908 write_stderr(
_(
"%s: could not get LUIDs for privileges: error code %lu\n"),
1909 progname, (
unsigned long) GetLastError());
1913 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &length) &&
1914 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1916 write_stderr(
_(
"%s: could not get token information: error code %lu\n"),
1917 progname, (
unsigned long) GetLastError());
1923 if (tokenPrivs == NULL)
1929 if (!GetTokenInformation(hToken, TokenPrivileges, tokenPrivs, length, &length))
1931 write_stderr(
_(
"%s: could not get token information: error code %lu\n"),
1932 progname, (
unsigned long) GetLastError());
1937 for (
i = 0;
i < tokenPrivs->PrivilegeCount;
i++)
1939 if (memcmp(&tokenPrivs->Privileges[
i].Luid, &luidLockPages,
sizeof(LUID)) == 0 ||
1940 memcmp(&tokenPrivs->Privileges[
i].Luid, &luidChangeNotify,
sizeof(LUID)) == 0)
1942 for (
j =
i;
j < tokenPrivs->PrivilegeCount - 1;
j++)
1943 tokenPrivs->Privileges[
j] = tokenPrivs->Privileges[
j + 1];
1944 tokenPrivs->PrivilegeCount--;
1963 printf(
_(
"%s is a utility to initialize, start, stop, or control a PostgreSQL server.\n\n"),
progname);
1966 printf(
_(
" %s start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]\n"
1967 " [-o OPTIONS] [-p PATH] [-c]\n"),
progname);
1968 printf(
_(
" %s stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"),
progname);
1969 printf(
_(
" %s restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"
1970 " [-o OPTIONS] [-c]\n"),
progname);
1973 printf(
_(
" %s promote [-D DATADIR] [-W] [-t SECS] [-s]\n"),
progname);
1977 printf(
_(
" %s register [-D DATADIR] [-N SERVICENAME] [-U USERNAME] [-P PASSWORD]\n"
1978 " [-S START-TYPE] [-e SOURCE] [-W] [-t SECS] [-s] [-o OPTIONS]\n"),
progname);
1982 printf(
_(
"\nCommon options:\n"));
1983 printf(
_(
" -D, --pgdata=DATADIR location of the database storage area\n"));
1985 printf(
_(
" -e SOURCE event source for logging when running as a service\n"));
1987 printf(
_(
" -s, --silent only print errors, no informational messages\n"));
1988 printf(
_(
" -t, --timeout=SECS seconds to wait when using -w option\n"));
1989 printf(
_(
" -V, --version output version information, then exit\n"));
1990 printf(
_(
" -w, --wait wait until operation completes (default)\n"));
1991 printf(
_(
" -W, --no-wait do not wait until operation completes\n"));
1992 printf(
_(
" -?, --help show this help, then exit\n"));
1993 printf(
_(
"If the -D option is omitted, the environment variable PGDATA is used.\n"));
1995 printf(
_(
"\nOptions for start or restart:\n"));
1996 #if defined(HAVE_GETRLIMIT)
1997 printf(
_(
" -c, --core-files allow postgres to produce core files\n"));
1999 printf(
_(
" -c, --core-files not applicable on this platform\n"));
2001 printf(
_(
" -l, --log=FILENAME write (or append) server log to FILENAME\n"));
2002 printf(
_(
" -o, --options=OPTIONS command line options to pass to postgres\n"
2003 " (PostgreSQL server executable) or initdb\n"));
2004 printf(
_(
" -p PATH-TO-POSTGRES normally not necessary\n"));
2005 printf(
_(
"\nOptions for stop or restart:\n"));
2006 printf(
_(
" -m, --mode=MODE MODE can be \"smart\", \"fast\", or \"immediate\"\n"));
2008 printf(
_(
"\nShutdown modes are:\n"));
2009 printf(
_(
" smart quit after all clients have disconnected\n"));
2010 printf(
_(
" fast quit directly, with proper shutdown (default)\n"));
2011 printf(
_(
" immediate quit without complete shutdown; will lead to recovery on restart\n"));
2013 printf(
_(
"\nAllowed signal names for kill:\n"));
2014 printf(
" ABRT HUP INT KILL QUIT TERM USR1 USR2\n");
2017 printf(
_(
"\nOptions for register and unregister:\n"));
2018 printf(
_(
" -N SERVICENAME service name with which to register PostgreSQL server\n"));
2019 printf(
_(
" -P PASSWORD password of account to register PostgreSQL server\n"));
2020 printf(
_(
" -U USERNAME user name of account to register PostgreSQL server\n"));
2021 printf(
_(
" -S START-TYPE service start type to register PostgreSQL server\n"));
2023 printf(
_(
"\nStart types are:\n"));
2024 printf(
_(
" auto start service automatically during system startup (default)\n"));
2025 printf(
_(
" demand start service on demand\n"));
2028 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2029 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2037 if (strcmp(modeopt,
"s") == 0 || strcmp(modeopt,
"smart") == 0)
2042 else if (strcmp(modeopt,
"f") == 0 || strcmp(modeopt,
"fast") == 0)
2047 else if (strcmp(modeopt,
"i") == 0 || strcmp(modeopt,
"immediate") == 0)
2065 if (strcmp(signame,
"HUP") == 0)
2067 else if (strcmp(signame,
"INT") == 0)
2069 else if (strcmp(signame,
"QUIT") == 0)
2071 else if (strcmp(signame,
"ABRT") == 0)
2073 else if (strcmp(signame,
"KILL") == 0)
2075 else if (strcmp(signame,
"TERM") == 0)
2077 else if (strcmp(signame,
"USR1") == 0)
2079 else if (strcmp(signame,
"USR2") == 0)
2092 set_starttype(
char *starttypeopt)
2094 if (strcmp(starttypeopt,
"a") == 0 || strcmp(starttypeopt,
"auto") == 0)
2095 pgctl_start_type = SERVICE_AUTO_START;
2096 else if (strcmp(starttypeopt,
"d") == 0 || strcmp(starttypeopt,
"demand") == 0)
2097 pgctl_start_type = SERVICE_DEMAND_START;
2147 cmd =
psprintf(
"\"%s\" -C data_directory %s%s",
2153 fd = popen(cmd,
"r");
2156 write_stderr(
_(
"%s: could not determine the data directory using command \"%s\"\n"),
progname, cmd);
2183 ret = control_file_data->
state;
2184 pfree(control_file_data);
2192 static struct option long_options[] = {
2229 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
2234 else if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
2236 puts(
"pg_ctl (PostgreSQL) " PG_VERSION);
2248 "Please log in (using, e.g., \"su\") as the "
2249 "(unprivileged) user that will\n"
2250 "own the server process.\n"),
2256 env_wait = getenv(
"PGCTLTIMEOUT");
2257 if (env_wait != NULL)
2261 while ((
c =
getopt_long(argc, argv,
"cD:e:l:m:N:o:p:P:sS:t:U:wW",
2262 long_options, &option_index)) != -1)
2272 setenv(
"PGDATA", pgdata_D, 1);
2303 free(old_post_opts);
2319 write_stderr(
_(
"%s: -S option not supported on this platform\n"),
2329 if (strchr(
optarg,
'\\'))
2354 if (strcmp(argv[
optind],
"init") == 0
2355 || strcmp(argv[
optind],
"initdb") == 0)
2357 else if (strcmp(argv[
optind],
"start") == 0)
2359 else if (strcmp(argv[
optind],
"stop") == 0)
2361 else if (strcmp(argv[
optind],
"restart") == 0)
2363 else if (strcmp(argv[
optind],
"reload") == 0)
2365 else if (strcmp(argv[
optind],
"status") == 0)
2367 else if (strcmp(argv[
optind],
"promote") == 0)
2369 else if (strcmp(argv[
optind],
"logrotate") == 0)
2371 else if (strcmp(argv[
optind],
"kill") == 0)
2381 killproc = atol(argv[++
optind]);
2384 else if (strcmp(argv[
optind],
"register") == 0)
2386 else if (strcmp(argv[
optind],
"unregister") == 0)
2388 else if (strcmp(argv[
optind],
"runservice") == 0)
2430 write_stderr(
_(
"%s: no database directory specified and environment variable PGDATA unset\n"),
2490 pgwin32_doRegister();
2493 pgwin32_doUnregister();
2496 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 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)