X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=process.cpp;h=7bb6dac819245e8f2db513f05d0beb63c4ad77d5;hb=99c5c2868f1d351d4d0569fda9199dda1aadad07;hp=4c00e64266431c1acb554df9755e2c07e6ea9ce7;hpb=ce9eb5d4646a0e279eae2909a18fccb1197e15fb;p=nssm.git diff --git a/process.cpp b/process.cpp index 4c00e64..7bb6dac 100644 --- a/process.cpp +++ b/process.cpp @@ -1,5 +1,10 @@ #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; @@ -39,7 +44,7 @@ int check_parent(char *service_name, PROCESSENTRY32 *pe, unsigned long ppid, FIL HANDLE process_handle = OpenProcess(PROCESS_QUERY_INFORMATION, false, pe->th32ProcessID); if (! process_handle) { char pid_string[16]; - _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%d", pe->th32ProcessID); + _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); return 2; } @@ -134,7 +139,7 @@ int kill_threads(char *service_name, kill_t *k) { } /* Give the process a chance to die gracefully. */ -int kill_process(char *service_name, unsigned long stop_method, HANDLE process_handle, unsigned long pid, unsigned long exitcode) { +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) { /* Shouldn't happen. */ if (! pid) return 1; if (! process_handle) return 1; @@ -148,7 +153,7 @@ int kill_process(char *service_name, unsigned long stop_method, HANDLE process_h /* Try to send a Control-C event to the console. */ if (stop_method & NSSM_STOP_METHOD_CONSOLE) { - if (! kill_console(service_name, process_handle, pid)) return 1; + if (! kill_console(service_name, service_handle, service_status, process_handle, pid)) return 1; } /* @@ -159,7 +164,7 @@ int kill_process(char *service_name, unsigned long stop_method, HANDLE process_h if (stop_method & NSSM_STOP_METHOD_WINDOW) { EnumWindows((WNDENUMPROC) kill_window, (LPARAM) &k); if (k.signalled) { - if (! WaitForSingleObject(process_handle, NSSM_KILL_WINDOW_GRACE_PERIOD)) return 1; + if (! await_shutdown(__FUNCTION__, service_name, service_handle, service_status, process_handle, kill_window_delay)) return 1; } } @@ -170,7 +175,7 @@ int kill_process(char *service_name, unsigned long stop_method, HANDLE process_h */ if (stop_method & NSSM_STOP_METHOD_THREADS) { if (kill_threads(service_name, &k)) { - if (! WaitForSingleObject(process_handle, NSSM_KILL_THREADS_GRACE_PERIOD)) return 1; + if (! await_shutdown(__FUNCTION__, service_name, service_handle, service_status, process_handle, kill_threads_delay)) return 1; } } @@ -183,11 +188,14 @@ int kill_process(char *service_name, unsigned long stop_method, HANDLE process_h } /* Simulate a Control-C event to our console (shared with the app). */ -int kill_console(char *service_name, HANDLE process_handle, unsigned long pid) { +int kill_console(char *service_name, SERVICE_STATUS_HANDLE service_handle, SERVICE_STATUS *service_status, HANDLE process_handle, unsigned long pid) { unsigned long ret; + /* Check we loaded AttachConsole(). */ + if (! imports.AttachConsole) return 4; + /* Try to attach to the process's console. */ - if (! AttachConsole(pid)) { + if (! imports.AttachConsole(pid)) { ret = GetLastError(); switch (ret) { @@ -228,18 +236,18 @@ int kill_console(char *service_name, HANDLE process_handle, unsigned long pid) { } /* Wait for process to exit. */ - if (WaitForSingleObject(process_handle, NSSM_KILL_CONSOLE_GRACE_PERIOD)) return 6; + if (await_shutdown(__FUNCTION__, service_name, service_handle, service_status, process_handle, kill_console_delay)) ret = 6; return ret; } -void kill_process_tree(char *service_name, 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(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) { /* 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, "%d", pid); - _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", exitcode); + _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); /* Get a snapshot of all processes in the system. */ @@ -260,7 +268,7 @@ void kill_process_tree(char *service_name, unsigned long stop_method, unsigned l } /* 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, stop_method, pe.th32ProcessID, exitcode, ppid, parent_creation_time, parent_exit_time); + 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); while (true) { /* Try to get the next process. */ @@ -272,7 +280,7 @@ void kill_process_tree(char *service_name, unsigned long stop_method, unsigned l return; } - if (! check_parent(service_name, &pe, pid, parent_creation_time, parent_exit_time)) kill_process_tree(service_name, stop_method, pe.th32ProcessID, exitcode, ppid, parent_creation_time, parent_exit_time); + 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); } CloseHandle(snapshot); @@ -285,9 +293,9 @@ void kill_process_tree(char *service_name, unsigned long stop_method, unsigned l } char ppid_string[16]; - _snprintf_s(ppid_string, sizeof(ppid_string), _TRUNCATE, "%d", ppid); + _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, stop_method, process_handle, pid, exitcode)) { + if (! kill_process(service_name, service_handle, service_status, stop_method, process_handle, pid, exitcode)) { /* Maybe it already died. */ unsigned long ret; if (! GetExitCodeProcess(process_handle, &ret) || ret == STILL_ACTIVE) {