+ switch (action) {\r
+ /* Try to restart the service or return failure code to service manager */\r
+ case NSSM_EXIT_RESTART:\r
+ log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_RESTART, service_name, code, exit_action_strings[action], exe, 0);\r
+ while (monitor_service()) {\r
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_RESTART_SERVICE_FAILED, exe, service_name, 0);\r
+ Sleep(30000);\r
+ }\r
+ break;\r
+\r
+ /* Do nothing, just like srvany would */\r
+ case NSSM_EXIT_IGNORE:\r
+ log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_IGNORE, service_name, code, exit_action_strings[action], exe, 0);\r
+ Sleep(INFINITE);\r
+ break;\r
+\r
+ /* Tell the service manager we are finished */\r
+ case NSSM_EXIT_REALLY:\r
+ log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_REALLY, service_name, code, exit_action_strings[action], 0);\r
+ stop_service(exitcode, true, default_action);\r
+ break;\r
+\r
+ /* Fake a crash so pre-Vista service managers will run recovery actions. */\r
+ case NSSM_EXIT_UNCLEAN:\r
+ log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_UNCLEAN, service_name, code, exit_action_strings[action], 0);\r
+ stop_service(exitcode, false, default_action);\r
+ free_imports();\r
+ exit(exitcode);\r
+ break;\r
+ }\r
+}\r
+\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