+\r
+void throttle_restart() {\r
+ /* This can't be a restart if the service is already running. */\r
+ if (! throttle++) return;\r
+\r
+ int ms = throttle_milliseconds();\r
+\r
+ if (throttle > 7) throttle = 8;\r
+\r
+ char threshold[8], milliseconds[8];\r
+ _snprintf_s(threshold, sizeof(threshold), _TRUNCATE, "%lu", throttle_delay);\r
+ _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%lu", ms);\r
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service_name, threshold, milliseconds, 0);\r
+\r
+ if (use_critical_section) EnterCriticalSection(&throttle_section);\r
+ else if (throttle_timer) {\r
+ ZeroMemory(&throttle_duetime, sizeof(throttle_duetime));\r
+ throttle_duetime.QuadPart = 0 - (ms * 10000LL);\r
+ SetWaitableTimer(throttle_timer, &throttle_duetime, 0, 0, 0, 0);\r
+ }\r
+\r
+ service_status.dwCurrentState = SERVICE_PAUSED;\r
+ SetServiceStatus(service_handle, &service_status);\r
+\r
+ if (use_critical_section) {\r
+ imports.SleepConditionVariableCS(&throttle_condition, &throttle_section, ms);\r
+ LeaveCriticalSection(&throttle_section);\r
+ }\r
+ else {\r
+ if (throttle_timer) WaitForSingleObject(throttle_timer, INFINITE);\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_SERVICE_STATUS_DEADLINE milliseconds. If the process is still running\r
+ and we haven't finished waiting we increment dwCheckPoint and add whichever is\r
+ smaller of NSSM_SERVICE_STATUS_DEADLINE or the remaining timeout to\r
+ 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_SERVICE_STATUS_DEADLINE) interval = NSSM_SERVICE_STATUS_DEADLINE;\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