Restrict acceptance of service controls.
authorIain Patterson <me@iain.cx>
Wed, 31 Dec 2014 13:16:24 +0000 (13:16 +0000)
committerIain Patterson <me@iain.cx>
Wed, 31 Dec 2014 14:12:27 +0000 (14:12 +0000)
Don't accept a pause/continue control unless the service is marked as
paused.
Don't accept any control until startup is pending.
Don't accept any control after processing a stop control.

service.cpp

index 0a8773a..c6ac099 100644 (file)
@@ -1363,7 +1363,7 @@ void WINAPI service_main(unsigned long argc, TCHAR **argv) {
   /* Initialise status */\r
   ZeroMemory(&service->status, sizeof(service->status));\r
   service->status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;\r
-  service->status.dwControlsAccepted = SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;\r
+  service->status.dwControlsAccepted = 0;\r
   service->status.dwWin32ExitCode = NO_ERROR;\r
   service->status.dwServiceSpecificExitCode = 0;\r
   service->status.dwCheckPoint = 0;\r
@@ -1689,10 +1689,12 @@ int start_service(nssm_service_t *service) {
     so abandon the wait before too much time has elapsed.\r
   */\r
   service->status.dwCurrentState = SERVICE_START_PENDING;\r
+  service->status.dwControlsAccepted = SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;\r
   if (await_single_handle(service->status_handle, &service->status, service->process_handle, service->name, _T("start_service"), service->throttle_delay) == 1) service->throttle = 0;\r
 \r
   /* Signal successful start */\r
   service->status.dwCurrentState = SERVICE_RUNNING;\r
+  service->status.dwControlsAccepted &= ~SERVICE_ACCEPT_PAUSE_CONTINUE;\r
   SetServiceStatus(service->status_handle, &service->status);\r
 \r
   /* Continue waiting for a clean startup. */\r
@@ -1728,8 +1730,9 @@ int stop_service(nssm_service_t *service, unsigned long exitcode, bool graceful,
   if (graceful) {\r
     service->status.dwCurrentState = SERVICE_STOP_PENDING;\r
     service->status.dwWaitHint = NSSM_WAITHINT_MARGIN;\r
-    SetServiceStatus(service->status_handle, &service->status);\r
   }\r
+  service->status.dwControlsAccepted = 0;\r
+  SetServiceStatus(service->status_handle, &service->status);\r
 \r
   /* Nothing to do if service isn't running */\r
   if (service->pid) {\r
@@ -1887,6 +1890,7 @@ void throttle_restart(nssm_service_t *service) {
   }\r
 \r
   service->status.dwCurrentState = SERVICE_PAUSED;\r
+  service->status.dwControlsAccepted |= SERVICE_ACCEPT_PAUSE_CONTINUE;\r
   SetServiceStatus(service->status_handle, &service->status);\r
 \r
   if (use_critical_section) {\r