X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=process.cpp;h=f060d725992a13dcff36241883e0d9f26eba73aa;hb=636b16d3702049145143511c6461ff20bf1f245d;hp=e6548119c380e4d72d9675836c5d585d99df87bb;hpb=23b8173ce06a843f18a9269fc6f7d4d0d224cd4c;p=nssm.git diff --git a/process.cpp b/process.cpp index e654811..f060d72 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(pid_string, sizeof(pid_string), "%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; } @@ -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 (! WaitForSingleObject(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 (! WaitForSingleObject(process_handle, kill_threads_delay)) return 1; } } @@ -186,8 +191,11 @@ int kill_process(char *service_name, unsigned long stop_method, HANDLE process_h int kill_console(char *service_name, 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,7 +236,7 @@ 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 (WaitForSingleObject(process_handle, kill_console_delay)) return 6; return ret; } @@ -238,8 +246,8 @@ void kill_process_tree(char *service_name, unsigned long stop_method, unsigned l if (! pid) return; char pid_string[16], code[16]; - _snprintf(pid_string, sizeof(pid_string), "%d", pid); - _snprintf(code, sizeof(code), "%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. */ @@ -285,7 +293,7 @@ void kill_process_tree(char *service_name, unsigned long stop_method, unsigned l } char ppid_string[16]; - _snprintf(ppid_string, sizeof(ppid_string), "%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)) { /* Maybe it already died. */