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