X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=process.cpp;h=34b07d378ef0ebab65c8975954c368478b6857fd;hb=fce252d07bdf443e3b283c26a940d3b0f26fd440;hp=7bb6dac819245e8f2db513f05d0beb63c4ad77d5;hpb=0f64692bef62a3be6f9352cfdee05d73130d06f4;p=nssm.git diff --git a/process.cpp b/process.cpp index 7bb6dac..34b07d3 100644 --- a/process.cpp +++ b/process.cpp @@ -1,9 +1,6 @@ #include "nssm.h" extern imports_t imports; -extern unsigned long kill_console_delay; -extern unsigned long kill_window_delay; -extern unsigned long kill_threads_delay; int get_process_creation_time(HANDLE process_handle, FILETIME *ft) { FILETIME creation_time, exit_time, kernel_time, user_time; @@ -31,7 +28,7 @@ int get_process_exit_time(HANDLE process_handle, FILETIME *ft) { return 0; } -int check_parent(char *service_name, PROCESSENTRY32 *pe, unsigned long ppid, FILETIME *pft, FILETIME *exit_time) { +int check_parent(nssm_service_t *service, PROCESSENTRY32 *pe, unsigned long ppid) { /* Check parent process ID matches. */ if (pe->th32ParentProcessID != ppid) return 1; @@ -45,7 +42,7 @@ int check_parent(char *service_name, PROCESSENTRY32 *pe, unsigned long ppid, FIL if (! process_handle) { char pid_string[16]; _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%lu", pe->th32ProcessID); - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENPROCESS_FAILED, pid_string, service_name, error_string(GetLastError()), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENPROCESS_FAILED, pid_string, service->name, error_string(GetLastError()), 0); return 2; } @@ -58,10 +55,10 @@ int check_parent(char *service_name, PROCESSENTRY32 *pe, unsigned long ppid, FIL CloseHandle(process_handle); /* Verify that the parent's creation time is not later. */ - if (CompareFileTime(pft, &ft) > 0) return 4; + if (CompareFileTime(&service->creation_time, &ft) > 0) return 4; /* Verify that the parent's exit time is not earlier. */ - if (CompareFileTime(exit_time, &ft) < 0) return 5; + if (CompareFileTime(&service->exit_time, &ft) < 0) return 5; return 0; } @@ -139,8 +136,9 @@ int kill_threads(char *service_name, kill_t *k) { } /* Give the process a chance to die gracefully. */ -int kill_process(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long stop_method, HANDLE process_handle, unsigned long pid, unsigned long exitcode) { +int kill_process(nssm_service_t *service, HANDLE process_handle, unsigned long pid, unsigned long exitcode) { /* Shouldn't happen. */ + if (! service) return 1; if (! pid) return 1; if (! process_handle) return 1; @@ -152,8 +150,8 @@ int kill_process(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVI kill_t k = { pid, exitcode, 0 }; /* Try to send a Control-C event to the console. */ - if (stop_method & NSSM_STOP_METHOD_CONSOLE) { - if (! kill_console(service_name, service_handle, service_status, process_handle, pid)) return 1; + if (service->stop_method & NSSM_STOP_METHOD_CONSOLE) { + if (! kill_console(service)) return 1; } /* @@ -161,10 +159,10 @@ int kill_process(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVI If the process is a console application it won't have any windows so there's no guarantee of success. */ - if (stop_method & NSSM_STOP_METHOD_WINDOW) { + if (service->stop_method & NSSM_STOP_METHOD_WINDOW) { EnumWindows((WNDENUMPROC) kill_window, (LPARAM) &k); if (k.signalled) { - if (! await_shutdown(__FUNCTION__, service_name, service_handle, service_status, process_handle, kill_window_delay)) return 1; + if (! await_shutdown(service, __FUNCTION__, service->kill_window_delay)) return 1; } } @@ -173,29 +171,31 @@ int kill_process(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVI process. Console applications might have them (but probably won't) so there's still no guarantee of success. */ - if (stop_method & NSSM_STOP_METHOD_THREADS) { - if (kill_threads(service_name, &k)) { - if (! await_shutdown(__FUNCTION__, service_name, service_handle, service_status, process_handle, kill_threads_delay)) return 1; + if (service->stop_method & NSSM_STOP_METHOD_THREADS) { + if (kill_threads(service->name, &k)) { + if (! await_shutdown(service, __FUNCTION__, service->kill_threads_delay)) return 1; } } /* We tried being nice. Time for extreme prejudice. */ - if (stop_method & NSSM_STOP_METHOD_TERMINATE) { - return TerminateProcess(process_handle, exitcode); + if (service->stop_method & NSSM_STOP_METHOD_TERMINATE) { + return TerminateProcess(service->process_handle, exitcode); } return 0; } /* Simulate a Control-C event to our console (shared with the app). */ -int kill_console(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVICE_STATUS *service_status, HANDLE process_handle, unsigned long pid) { +int kill_console(nssm_service_t *service) { unsigned long ret; + if (! service) return 1; + /* Check we loaded AttachConsole(). */ if (! imports.AttachConsole) return 4; /* Try to attach to the process's console. */ - if (! imports.AttachConsole(pid)) { + if (! imports.AttachConsole(service->pid)) { ret = GetLastError(); switch (ret) { @@ -210,7 +210,7 @@ int kill_console(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVI case ERROR_ACCESS_DENIED: default: /* We already have a console. */ - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ATTACHCONSOLE_FAILED, service_name, error_string(ret), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_ATTACHCONSOLE_FAILED, service->name, error_string(ret), 0); return 3; } } @@ -218,42 +218,42 @@ int kill_console(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVI /* Ignore the event ourselves. */ ret = 0; if (! SetConsoleCtrlHandler(0, TRUE)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETCONSOLECTRLHANDLER_FAILED, service_name, error_string(GetLastError()), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETCONSOLECTRLHANDLER_FAILED, service->name, error_string(GetLastError()), 0); ret = 4; } /* Send the event. */ if (! ret) { if (! GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GENERATECONSOLECTRLEVENT_FAILED, service_name, error_string(GetLastError()), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GENERATECONSOLECTRLEVENT_FAILED, service->name, error_string(GetLastError()), 0); ret = 5; } } /* Detach from the console. */ if (! FreeConsole()) { - log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_FREECONSOLE_FAILED, service_name, error_string(GetLastError()), 0); + log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_FREECONSOLE_FAILED, service->name, error_string(GetLastError()), 0); } /* Wait for process to exit. */ - if (await_shutdown(__FUNCTION__, service_name, service_handle, service_status, process_handle, kill_console_delay)) ret = 6; + if (await_shutdown(service, __FUNCTION__, service->kill_console_delay)) ret = 6; return ret; } -void kill_process_tree(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long stop_method, unsigned long pid, unsigned long exitcode, unsigned long ppid, FILETIME *parent_creation_time, FILETIME *parent_exit_time) { +void kill_process_tree(nssm_service_t *service, unsigned long pid, unsigned long exitcode, unsigned long ppid) { /* Shouldn't happen unless the service failed to start. */ if (! pid) return; char pid_string[16], code[16]; _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%lu", pid); _snprintf_s(code, sizeof(code), _TRUNCATE, "%lu", exitcode); - log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILLING, service_name, pid_string, code, 0); + log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILLING, service->name, pid_string, code, 0); /* Get a snapshot of all processes in the system. */ HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (! snapshot) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETOOLHELP32SNAPSHOT_PROCESS_FAILED, service_name, error_string(GetLastError()), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETOOLHELP32SNAPSHOT_PROCESS_FAILED, service->name, error_string(GetLastError()), 0); return; } @@ -262,25 +262,25 @@ void kill_process_tree(char *service_name, SERVICE_STATUS_HANDLE service_handle, pe.dwSize = sizeof(pe); if (! Process32First(snapshot, &pe)) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_PROCESS_ENUMERATE_FAILED, service_name, error_string(GetLastError()), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_PROCESS_ENUMERATE_FAILED, service->name, error_string(GetLastError()), 0); CloseHandle(snapshot); return; } /* This is a child of the doomed process so kill it. */ - if (! check_parent(service_name, &pe, pid, parent_creation_time, parent_exit_time)) kill_process_tree(service_name, service_handle, service_status, stop_method, pe.th32ProcessID, exitcode, ppid, parent_creation_time, parent_exit_time); + if (! check_parent(service, &pe, pid)) kill_process_tree(service, pe.th32ProcessID, exitcode, ppid); while (true) { /* Try to get the next process. */ if (! Process32Next(snapshot, &pe)) { unsigned long ret = GetLastError(); if (ret == ERROR_NO_MORE_FILES) break; - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_PROCESS_ENUMERATE_FAILED, service_name, error_string(GetLastError()), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_PROCESS_ENUMERATE_FAILED, service->name, error_string(GetLastError()), 0); CloseHandle(snapshot); return; } - if (! check_parent(service_name, &pe, pid, parent_creation_time, parent_exit_time)) kill_process_tree(service_name, service_handle, service_status, stop_method, pe.th32ProcessID, exitcode, ppid, parent_creation_time, parent_exit_time); + if (! check_parent(service, &pe, pid)) kill_process_tree(service, pe.th32ProcessID, exitcode, ppid); } CloseHandle(snapshot); @@ -288,19 +288,19 @@ void kill_process_tree(char *service_name, SERVICE_STATUS_HANDLE service_handle, /* We will need a process handle in order to call TerminateProcess() later. */ HANDLE process_handle = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, false, pid); if (! process_handle) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENPROCESS_FAILED, pid_string, service_name, error_string(GetLastError()), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENPROCESS_FAILED, pid_string, service->name, error_string(GetLastError()), 0); return; } char ppid_string[16]; _snprintf_s(ppid_string, sizeof(ppid_string), _TRUNCATE, "%lu", ppid); - log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILL_PROCESS_TREE, pid_string, ppid_string, service_name, 0); - if (! kill_process(service_name, service_handle, service_status, stop_method, process_handle, pid, exitcode)) { + log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILL_PROCESS_TREE, pid_string, ppid_string, service->name, 0); + if (! kill_process(service, process_handle, pid, exitcode)) { /* Maybe it already died. */ unsigned long ret; if (! GetExitCodeProcess(process_handle, &ret) || ret == STILL_ACTIVE) { - if (stop_method & NSSM_STOP_METHOD_TERMINATE) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_TERMINATEPROCESS_FAILED, pid_string, service_name, error_string(GetLastError()), 0); - else log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_PROCESS_STILL_ACTIVE, service_name, pid_string, NSSM, NSSM_REG_STOP_METHOD_SKIP, 0); + if (service->stop_method & NSSM_STOP_METHOD_TERMINATE) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_TERMINATEPROCESS_FAILED, pid_string, service->name, error_string(GetLastError()), 0); + else log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_PROCESS_STILL_ACTIVE, service->name, pid_string, NSSM, NSSM_REG_STOP_METHOD_SKIP, 0); } }