Commit 1ab7dc06 authored by Magnus Hagander's avatar Magnus Hagander

Support running as a service on Windows 7, by not specifying

the JOB_OBJECT_UILIMIT_HANDLES flag.

Dave Page & Magnus Hagander
parent 6b733665
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* *
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.107 2009/01/15 22:33:19 petere Exp $ * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.108 2009/01/28 11:19:37 mha Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -121,7 +121,7 @@ static void pgwin32_SetServiceStatus(DWORD); ...@@ -121,7 +121,7 @@ static void pgwin32_SetServiceStatus(DWORD);
static void WINAPI pgwin32_ServiceHandler(DWORD); static void WINAPI pgwin32_ServiceHandler(DWORD);
static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *); static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *);
static void pgwin32_doRunAsService(void); static void pgwin32_doRunAsService(void);
static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo); static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo, bool as_service);
static SERVICE_STATUS status; static SERVICE_STATUS status;
static SERVICE_STATUS_HANDLE hStatus = (SERVICE_STATUS_HANDLE) 0; static SERVICE_STATUS_HANDLE hStatus = (SERVICE_STATUS_HANDLE) 0;
...@@ -385,7 +385,7 @@ start_postmaster(void) ...@@ -385,7 +385,7 @@ start_postmaster(void)
snprintf(cmd, MAXPGPATH, "CMD /C " SYSTEMQUOTE "\"%s\" %s%s < \"%s\" 2>&1" SYSTEMQUOTE, snprintf(cmd, MAXPGPATH, "CMD /C " SYSTEMQUOTE "\"%s\" %s%s < \"%s\" 2>&1" SYSTEMQUOTE,
postgres_path, pgdata_opt, post_opts, DEVNULL); postgres_path, pgdata_opt, post_opts, DEVNULL);
if (!CreateRestrictedProcess(cmd, &pi)) if (!CreateRestrictedProcess(cmd, &pi, false))
return GetLastError(); return GetLastError();
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
...@@ -1210,7 +1210,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR * argv) ...@@ -1210,7 +1210,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR * argv)
/* Start the postmaster */ /* Start the postmaster */
pgwin32_SetServiceStatus(SERVICE_START_PENDING); pgwin32_SetServiceStatus(SERVICE_START_PENDING);
if (!CreateRestrictedProcess(pgwin32_CommandLine(false), &pi)) if (!CreateRestrictedProcess(pgwin32_CommandLine(false), &pi, true))
{ {
pgwin32_SetServiceStatus(SERVICE_STOPPED); pgwin32_SetServiceStatus(SERVICE_STOPPED);
return; return;
...@@ -1313,7 +1313,7 @@ typedef BOOL(WINAPI * __QueryInformationJobObject) (HANDLE, JOBOBJECTINFOCLASS, ...@@ -1313,7 +1313,7 @@ typedef BOOL(WINAPI * __QueryInformationJobObject) (HANDLE, JOBOBJECTINFOCLASS,
* automatically destroyed when pg_ctl exits. * automatically destroyed when pg_ctl exits.
*/ */
static int static int
CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo) CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo, bool as_service)
{ {
int r; int r;
BOOL b; BOOL b;
...@@ -1449,6 +1449,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo) ...@@ -1449,6 +1449,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo)
JOBOBJECT_BASIC_LIMIT_INFORMATION basicLimit; JOBOBJECT_BASIC_LIMIT_INFORMATION basicLimit;
JOBOBJECT_BASIC_UI_RESTRICTIONS uiRestrictions; JOBOBJECT_BASIC_UI_RESTRICTIONS uiRestrictions;
JOBOBJECT_SECURITY_LIMIT_INFORMATION securityLimit; JOBOBJECT_SECURITY_LIMIT_INFORMATION securityLimit;
OSVERSIONINFO osv;
ZeroMemory(&basicLimit, sizeof(basicLimit)); ZeroMemory(&basicLimit, sizeof(basicLimit));
ZeroMemory(&uiRestrictions, sizeof(uiRestrictions)); ZeroMemory(&uiRestrictions, sizeof(uiRestrictions));
...@@ -1459,8 +1460,23 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo) ...@@ -1459,8 +1460,23 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo)
_SetInformationJobObject(job, JobObjectBasicLimitInformation, &basicLimit, sizeof(basicLimit)); _SetInformationJobObject(job, JobObjectBasicLimitInformation, &basicLimit, sizeof(basicLimit));
uiRestrictions.UIRestrictionsClass = JOB_OBJECT_UILIMIT_DESKTOP | JOB_OBJECT_UILIMIT_DISPLAYSETTINGS | uiRestrictions.UIRestrictionsClass = JOB_OBJECT_UILIMIT_DESKTOP | JOB_OBJECT_UILIMIT_DISPLAYSETTINGS |
JOB_OBJECT_UILIMIT_EXITWINDOWS | JOB_OBJECT_UILIMIT_HANDLES | JOB_OBJECT_UILIMIT_READCLIPBOARD | JOB_OBJECT_UILIMIT_EXITWINDOWS | JOB_OBJECT_UILIMIT_READCLIPBOARD |
JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS | JOB_OBJECT_UILIMIT_WRITECLIPBOARD; JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS | JOB_OBJECT_UILIMIT_WRITECLIPBOARD;
if (as_service)
{
osv.dwOSVersionInfoSize = sizeof(osv);
if (!GetVersionEx(&osv) ||
osv.dwMajorVersion < 6 ||
(osv.dwMajorVersion == 6 && osv.dwMinorVersion == 0))
{
/*
* On Windows 7 (and presumably later), JOB_OBJECT_UILIMIT_HANDLES prevents us from
* starting as a service. So we only enable it on Vista and earlier (version <= 6.0)
*/
uiRestrictions.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_HANDLES;
}
}
_SetInformationJobObject(job, JobObjectBasicUIRestrictions, &uiRestrictions, sizeof(uiRestrictions)); _SetInformationJobObject(job, JobObjectBasicUIRestrictions, &uiRestrictions, sizeof(uiRestrictions));
securityLimit.SecurityLimitFlags = JOB_OBJECT_SECURITY_NO_ADMIN | JOB_OBJECT_SECURITY_ONLY_TOKEN; securityLimit.SecurityLimitFlags = JOB_OBJECT_SECURITY_NO_ADMIN | JOB_OBJECT_SECURITY_ONLY_TOKEN;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment