return ret * 1000;\r
}\r
\r
+/*\r
+ Wrapper to be called in a new thread so that we can acknowledge a STOP\r
+ control immediately.\r
+*/\r
+static unsigned long WINAPI shutdown_service(void *arg) {\r
+ return stop_service(0, true, true);\r
+}\r
+\r
/* Connect to the service manager */\r
SC_HANDLE open_service_manager() {\r
SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);\r
case SERVICE_CONTROL_SHUTDOWN:\r
case SERVICE_CONTROL_STOP:\r
log_service_control(service_name, control, true);\r
- stop_service(0, true, true);\r
+ /*\r
+ We MUST acknowledge the stop request promptly but we're committed to\r
+ waiting for the application to exit. Spawn a new thread to wait\r
+ while we acknowledge the request.\r
+ */\r
+ if (! CreateThread(NULL, 0, shutdown_service, (void *) service_name, 0, NULL)) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETHREAD_FAILED, error_string(GetLastError()), 0);\r
+\r
+ /*\r
+ We couldn't create a thread to tidy up so we'll have to force the tidyup\r
+ to complete in time in this thread.\r
+ */\r
+ kill_console_delay = NSSM_KILL_CONSOLE_GRACE_PERIOD;\r
+ kill_window_delay = NSSM_KILL_WINDOW_GRACE_PERIOD;\r
+ kill_threads_delay = NSSM_KILL_THREADS_GRACE_PERIOD;\r
+\r
+ stop_service(0, true, true);\r
+ }\r
return NO_ERROR;\r
\r
case SERVICE_CONTROL_CONTINUE:\r
if (graceful) {\r
service_status.dwCurrentState = SERVICE_STOP_PENDING;\r
service_status.dwWaitHint = NSSM_WAITHINT_MARGIN;\r
- if (stop_method & NSSM_STOP_METHOD_CONSOLE && imports.AttachConsole) service_status.dwWaitHint += kill_console_delay;\r
- if (stop_method & NSSM_STOP_METHOD_WINDOW) service_status.dwWaitHint += kill_window_delay;\r
- if (stop_method & NSSM_STOP_METHOD_THREADS) service_status.dwWaitHint += kill_threads_delay;\r
SetServiceStatus(service_handle, &service_status);\r
}\r
\r
if (pid) {\r
/* Shut down service */\r
log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_TERMINATEPROCESS, service_name, exe, 0);\r
- kill_process(service_name, stop_method, process_handle, pid, 0);\r
+ kill_process(service_name, service_handle, &service_status, stop_method, process_handle, pid, 0);\r
}\r
else log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_PROCESS_ALREADY_STOPPED, service_name, exe, 0);\r
\r
\r
/* Clean up. */\r
if (exitcode == STILL_ACTIVE) exitcode = 0;\r
- kill_process_tree(service_name, stop_method, pid, exitcode, pid, &creation_time, &exit_time);\r
+ kill_process_tree(service_name, service_handle, &service_status, stop_method, pid, exitcode, pid, &creation_time, &exit_time);\r
\r
/*\r
The why argument is true if our wait timed out or false otherwise.\r
else Sleep(ms);\r
}\r
}\r
+\r
+/*\r
+ When responding to a stop (or any other) request we need to set dwWaitHint to\r
+ the number of milliseconds we expect the operation to take, and optionally\r
+ increase dwCheckPoint. If dwWaitHint milliseconds elapses without the\r
+ operation completing or dwCheckPoint increasing, the system will consider the\r
+ service to be hung.\r
+\r
+ However the system will consider the service to be hung after 30000\r
+ milliseconds regardless of the value of dwWaitHint if dwCheckPoint has not\r
+ changed. Therefore if we want to wait longer than that we must periodically\r
+ increase dwCheckPoint.\r
+\r
+ Furthermore, it will consider the service to be hung after 60000 milliseconds\r
+ regardless of the value of dwCheckPoint unless dwWaitHint is increased every\r
+ time dwCheckPoint is also increased.\r
+\r
+ Our strategy then is to retrieve the initial dwWaitHint and wait for\r
+ NSSM_SHUTDOWN_CHECKPOINT milliseconds. If the process is still running and\r
+ we haven't finished waiting we increment dwCheckPoint and add whichever is\r
+ smaller of NSSM_SHUTDOWN_CHECKPOINT or the remaining timeout to dwWaitHint.\r
+\r
+ Only doing both these things will prevent the system from killing the service.\r
+\r
+ Returns: 1 if the wait timed out.\r
+ 0 if the wait completed.\r
+ -1 on error.\r
+*/\r
+int await_shutdown(char *function_name, char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVICE_STATUS *service_status, HANDLE process_handle, unsigned long timeout) {\r
+ unsigned long interval;\r
+ unsigned long waithint;\r
+ unsigned long ret;\r
+ unsigned long waited;\r
+ char interval_milliseconds[16];\r
+ char timeout_milliseconds[16];\r
+ char waited_milliseconds[16];\r
+ char *function = function_name;\r
+\r
+ /* Add brackets to function name. */\r
+ size_t funclen = strlen(function_name) + 3;\r
+ char *func = (char *) HeapAlloc(GetProcessHeap(), 0, funclen);\r
+ if (func) {\r
+ if (_snprintf_s(func, funclen, _TRUNCATE, "%s()", function_name) > -1) function = func;\r
+ }\r
+\r
+ _snprintf_s(timeout_milliseconds, sizeof(timeout_milliseconds), _TRUNCATE, "%lu", timeout);\r
+\r
+ waithint = service_status->dwWaitHint;\r
+ waited = 0;\r
+ while (waited < timeout) {\r
+ interval = timeout - waited;\r
+ if (interval > NSSM_SHUTDOWN_CHECKPOINT) interval = NSSM_SHUTDOWN_CHECKPOINT;\r
+\r
+ service_status->dwCurrentState = SERVICE_STOP_PENDING;\r
+ service_status->dwWaitHint += interval;\r
+ service_status->dwCheckPoint++;\r
+ SetServiceStatus(service_handle, service_status);\r
+\r
+ if (waited) {\r
+ _snprintf_s(waited_milliseconds, sizeof(waited_milliseconds), _TRUNCATE, "%lu", waited);\r
+ _snprintf_s(interval_milliseconds, sizeof(interval_milliseconds), _TRUNCATE, "%lu", interval);\r
+ log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_AWAITING_SHUTDOWN, function, service_name, waited_milliseconds, interval_milliseconds, timeout_milliseconds, 0);\r
+ }\r
+\r
+ switch (WaitForSingleObject(process_handle, interval)) {\r
+ case WAIT_OBJECT_0:\r
+ ret = 0;\r
+ goto awaited;\r
+\r
+ case WAIT_TIMEOUT:\r
+ ret = 1;\r
+ break;\r
+\r
+ default:\r
+ ret = -1;\r
+ goto awaited;\r
+ }\r
+\r
+ waited += interval;\r
+ }\r
+\r
+awaited:\r
+ if (func) HeapFree(GetProcessHeap(), 0, func);\r
+\r
+ return ret;\r
+}\r