X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=52937cffb399d19f82dd436b5ec117bf91c85130;hb=b5286398f850b432edbddc6d602ab3f33ab086be;hp=4327a012a98d94e6c6f03e557cd175255c78dcca;hpb=4798232daef193da47e021879f1f6653137be8bb;p=nssm.git diff --git a/service.cpp b/service.cpp index 4327a01..52937cf 100644 --- a/service.cpp +++ b/service.cpp @@ -689,6 +689,7 @@ void set_nssm_service_defaults(nssm_service_t *service) { service->kill_console_delay = NSSM_KILL_CONSOLE_GRACE_PERIOD; service->kill_window_delay = NSSM_KILL_WINDOW_GRACE_PERIOD; service->kill_threads_delay = NSSM_KILL_THREADS_GRACE_PERIOD; + service->kill_process_tree = 1; } /* Allocate and zero memory for a service. */ @@ -709,7 +710,7 @@ void cleanup_nssm_service(nssm_service_t *service) { if (service->dependencies) HeapFree(GetProcessHeap(), 0, service->dependencies); if (service->env) HeapFree(GetProcessHeap(), 0, service->env); if (service->env_extra) HeapFree(GetProcessHeap(), 0, service->env_extra); - if (service->handle) CloseHandle(service->handle); + if (service->handle) CloseServiceHandle(service->handle); if (service->process_handle) CloseHandle(service->process_handle); if (service->wait_handle) UnregisterWait(service->wait_handle); if (service->throttle_section_initialised) DeleteCriticalSection(&service->throttle_section); @@ -856,7 +857,7 @@ int pre_edit_service(int argc, TCHAR **argv) { /* Get system details. */ QUERY_SERVICE_CONFIG *qsc = query_service_config(service->name, service->handle); if (! qsc) { - CloseHandle(service->handle); + CloseServiceHandle(service->handle); CloseServiceHandle(services); return 4; } @@ -865,7 +866,7 @@ int pre_edit_service(int argc, TCHAR **argv) { if (! (service->type & SERVICE_WIN32_OWN_PROCESS)) { if (mode != MODE_GETTING) { HeapFree(GetProcessHeap(), 0, qsc); - CloseHandle(service->handle); + CloseServiceHandle(service->handle); CloseServiceHandle(services); print_message(stderr, NSSM_MESSAGE_CANNOT_EDIT, service->name, NSSM_WIN32_OWN_PROCESS, 0); return 3; @@ -875,7 +876,7 @@ int pre_edit_service(int argc, TCHAR **argv) { if (get_service_startup(service->name, service->handle, qsc, &service->startup)) { if (mode != MODE_GETTING) { HeapFree(GetProcessHeap(), 0, qsc); - CloseHandle(service->handle); + CloseServiceHandle(service->handle); CloseServiceHandle(services); return 4; } @@ -884,7 +885,7 @@ int pre_edit_service(int argc, TCHAR **argv) { if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) { if (mode != MODE_GETTING) { HeapFree(GetProcessHeap(), 0, qsc); - CloseHandle(service->handle); + CloseServiceHandle(service->handle); CloseServiceHandle(services); return 5; } @@ -903,7 +904,7 @@ int pre_edit_service(int argc, TCHAR **argv) { /* Get extended system details. */ if (get_service_description(service->name, service->handle, _countof(service->description), service->description)) { if (mode != MODE_GETTING) { - CloseHandle(service->handle); + CloseServiceHandle(service->handle); CloseServiceHandle(services); return 6; } @@ -911,7 +912,7 @@ int pre_edit_service(int argc, TCHAR **argv) { if (get_service_dependencies(service->name, service->handle, &service->dependencies, &service->dependencieslen)) { if (mode != MODE_GETTING) { - CloseHandle(service->handle); + CloseServiceHandle(service->handle); CloseServiceHandle(services); return 7; } @@ -935,7 +936,7 @@ int pre_edit_service(int argc, TCHAR **argv) { /* Trying to manage App* parameters for a non-NSSM service. */ if (! setting->native && service->native) { - CloseHandle(service->handle); + CloseServiceHandle(service->handle); print_message(stderr, NSSM_MESSAGE_NATIVE_PARAMETER, setting->name, NSSM); return 1; } @@ -953,7 +954,7 @@ int pre_edit_service(int argc, TCHAR **argv) { if (setting->native) ret = get_setting(service->name, service->handle, setting, &value, additional); else ret = get_setting(service->name, key, setting, &value, additional); if (ret < 0) { - CloseHandle(service->handle); + CloseServiceHandle(service->handle); return 5; } @@ -971,7 +972,7 @@ int pre_edit_service(int argc, TCHAR **argv) { } if (! service->native) RegCloseKey(key); - CloseHandle(service->handle); + CloseServiceHandle(service->handle); return 0; } @@ -993,7 +994,7 @@ int pre_edit_service(int argc, TCHAR **argv) { value.string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR)); if (! value.string) { print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("value"), _T("edit_service()")); - CloseHandle(service->handle); + CloseServiceHandle(service->handle); return 2; } @@ -1026,12 +1027,12 @@ int pre_edit_service(int argc, TCHAR **argv) { if (value.string) HeapFree(GetProcessHeap(), 0, value.string); if (ret < 0) { if (! service->native) RegCloseKey(key); - CloseHandle(service->handle); + CloseServiceHandle(service->handle); return 6; } if (! service->native) RegCloseKey(key); - CloseHandle(service->handle); + CloseServiceHandle(service->handle); return 0; } @@ -1241,7 +1242,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) { if (ret) { int response = await_service_control_response(control, service_handle, &service_status, initial_status); - CloseHandle(service_handle); + CloseServiceHandle(service_handle); if (response) { print_message(stderr, NSSM_MESSAGE_BAD_CONTROL_RESPONSE, canonical_name, service_status_text(service_status.dwCurrentState), service_control_text(control)); @@ -1251,7 +1252,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) { return 0; } else { - CloseHandle(service_handle); + CloseServiceHandle(service_handle); _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error)); return 1; } @@ -1287,7 +1288,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) { if (ret) { int response = await_service_control_response(control, service_handle, &service_status, initial_status); - CloseHandle(service_handle); + CloseServiceHandle(service_handle); if (response) { print_message(stderr, NSSM_MESSAGE_BAD_CONTROL_RESPONSE, canonical_name, service_status_text(service_status.dwCurrentState), service_control_text(control)); @@ -1297,7 +1298,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) { return 0; } else { - CloseHandle(service_handle); + CloseServiceHandle(service_handle); _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error)); if (error == ERROR_SERVICE_NOT_ACTIVE) { if (control == SERVICE_CONTROL_SHUTDOWN || control == SERVICE_CONTROL_STOP) return 0; @@ -1362,7 +1363,7 @@ void WINAPI service_main(unsigned long argc, TCHAR **argv) { /* Initialise status */ ZeroMemory(&service->status, sizeof(service->status)); service->status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS; - service->status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; + service->status.dwControlsAccepted = SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; service->status.dwWin32ExitCode = NO_ERROR; service->status.dwServiceSpecificExitCode = 0; service->status.dwCheckPoint = 0; @@ -1460,6 +1461,7 @@ TCHAR *service_control_text(unsigned long control) { case SERVICE_CONTROL_CONTINUE: return _T("CONTINUE"); case SERVICE_CONTROL_INTERROGATE: return _T("INTERROGATE"); case NSSM_SERVICE_CONTROL_ROTATE: return _T("ROTATE"); + case SERVICE_CONTROL_POWEREVENT: return _T("POWEREVENT"); default: return 0; } } @@ -1567,6 +1569,15 @@ unsigned long WINAPI service_control_handler(unsigned long control, unsigned lon if (service->rotate_stdout_online == NSSM_ROTATE_ONLINE) service->rotate_stdout_online = NSSM_ROTATE_ONLINE_ASAP; if (service->rotate_stderr_online == NSSM_ROTATE_ONLINE) service->rotate_stderr_online = NSSM_ROTATE_ONLINE_ASAP; return NO_ERROR; + + case SERVICE_CONTROL_POWEREVENT: + if (event != PBT_APMRESUMEAUTOMATIC) { + log_service_control(service->name, control, false); + return NO_ERROR; + } + log_service_control(service->name, control, true); + end_service((void *) service, false); + return NO_ERROR; } /* Unknown control */ @@ -1790,7 +1801,7 @@ void CALLBACK end_service(void *arg, unsigned char why) { /* Clean up. */ if (exitcode == STILL_ACTIVE) exitcode = 0; - if (service->pid) kill_process_tree(service, service->pid, exitcode, service->pid); + if (service->pid && service->kill_process_tree) kill_process_tree(service, service->pid, exitcode, service->pid); service->pid = 0; /*