X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=f14390f717e3020ae19a0468e348c0a0514f382a;hb=cb571db509c239c3c465907a718479612fe5bb15;hp=d0cba9f966776cbc1280c5dd192aad95d1885e5c;hpb=4550eb2281a16b698c6a68d8de3552fe13807590;p=nssm.git diff --git a/service.cpp b/service.cpp index d0cba9f..f14390f 100644 --- a/service.cpp +++ b/service.cpp @@ -14,6 +14,9 @@ bool stopping; bool allow_restart; unsigned long throttle_delay; unsigned long stop_method; +unsigned long kill_console_delay; +unsigned long kill_window_delay; +unsigned long kill_threads_delay; CRITICAL_SECTION throttle_section; CONDITION_VARIABLE throttle_condition; HANDLE throttle_timer; @@ -280,7 +283,7 @@ int monitor_service() { } log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_STARTED_SERVICE, exe, flags, service_name, dir, 0); - /* Monitor service service */ + /* Monitor service */ if (! RegisterWaitForSingleObject(&wait_handle, process_handle, end_service, (void *) pid, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) { log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_REGISTERWAITFORSINGLEOBJECT_FAILED, service_name, exe, error_string(GetLastError()), 0); } @@ -309,11 +312,11 @@ void log_service_control(char *service_name, unsigned long control, bool handled /* "0x" + 8 x hex + NULL */ text = (char *) HeapAlloc(GetProcessHeap(), 0, 11); if (! text) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control", 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control()", 0); return; } if (_snprintf_s(text, 11, _TRUNCATE, "0x%08x", control) < 0) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control", 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control()", 0); HeapFree(GetProcessHeap(), 0, text); return; } @@ -390,7 +393,7 @@ int start_service() { /* Get startup parameters */ char *env = 0; - int ret = get_parameters(service_name, exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir), &env, &throttle_delay, &stop_method, &si); + int ret = get_parameters(service_name, exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir), &env, &throttle_delay, &stop_method, &kill_console_delay, &kill_window_delay, &kill_threads_delay, &si); if (ret) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, service_name, 0); return stop_service(2, true, true); @@ -445,7 +448,10 @@ int stop_service(unsigned long exitcode, bool graceful, bool default_action) { /* Signal we are stopping */ if (graceful) { service_status.dwCurrentState = SERVICE_STOP_PENDING; - service_status.dwWaitHint = NSSM_KILL_WINDOW_GRACE_PERIOD + NSSM_KILL_THREADS_GRACE_PERIOD + NSSM_WAITHINT_MARGIN; + service_status.dwWaitHint = NSSM_WAITHINT_MARGIN; + if (stop_method & NSSM_STOP_METHOD_CONSOLE && imports.AttachConsole) service_status.dwWaitHint += kill_console_delay; + if (stop_method & NSSM_STOP_METHOD_WINDOW) service_status.dwWaitHint += kill_window_delay; + if (stop_method & NSSM_STOP_METHOD_THREADS) service_status.dwWaitHint += kill_threads_delay; SetServiceStatus(service_handle, &service_status); } @@ -497,11 +503,12 @@ void CALLBACK end_service(void *arg, unsigned char why) { tree. See below for the possible values of the why argument. */ if (! why) { - _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", exitcode); + _snprintf_s(code, sizeof(code), _TRUNCATE, "%lu", exitcode); log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0); } /* Clean up. */ + if (exitcode == STILL_ACTIVE) exitcode = 0; kill_process_tree(service_name, stop_method, pid, exitcode, pid, &creation_time, &exit_time); /* @@ -569,8 +576,8 @@ void throttle_restart() { if (throttle > 7) throttle = 8; char threshold[8], milliseconds[8]; - _snprintf_s(threshold, sizeof(threshold), _TRUNCATE, "%d", throttle_delay); - _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%d", ms); + _snprintf_s(threshold, sizeof(threshold), _TRUNCATE, "%lu", throttle_delay); + _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%lu", ms); log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service_name, threshold, milliseconds, 0); if (use_critical_section) EnterCriticalSection(&throttle_section);