From bfffcaeec7c726166e87c801b4a60e3adf88dfd4 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 27 Mar 2018 14:29:09 +0100 Subject: win32: changes to kill(2) The names of the callbacks to kill processes with a given signal were confusing because they referred to the implementation rather than the intent. Create the new function kill_SIGTERM_by_handle which is more efficient when a handle to the process to be killed is already available. --- include/mingw.h | 2 ++ win32/process.c | 34 ++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index bc0772574..2dba6c402 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -438,6 +438,8 @@ const char * next_path_sep(const char *path); #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':') #define is_absolute_path(path) ((path)[0] == '/' || (path)[0] == '\\' || has_dos_drive_prefix(path)) +int kill_SIGTERM_by_handle(HANDLE process, int exit_code); + #define find_mount_point(n, s) find_mount_point(n) /* diff --git a/win32/process.c b/win32/process.c index 8842e0dee..84613cfdb 100644 --- a/win32/process.c +++ b/win32/process.c @@ -580,19 +580,11 @@ static inline int process_architecture_matches_current(HANDLE process) * http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547 * */ -static int exit_process(pid_t pid, int exit_code) +int kill_SIGTERM_by_handle(HANDLE process, int exit_code) { - HANDLE process; DWORD code; int ret = 0; - if (!(process = OpenProcess(SYNCHRONIZE | PROCESS_CREATE_THREAD | - PROCESS_QUERY_INFORMATION | - PROCESS_VM_OPERATION | PROCESS_VM_WRITE | - PROCESS_VM_READ, FALSE, pid))) { - return -1; - } - if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) { static int initialized; static LPTHREAD_START_ROUTINE exit_process_address; @@ -629,12 +621,26 @@ static int exit_process(pid_t pid, int exit_code) return ret; } +static int kill_SIGTERM(pid_t pid, int exit_code) +{ + HANDLE process; + + if (!(process = OpenProcess(SYNCHRONIZE | PROCESS_CREATE_THREAD | + PROCESS_QUERY_INFORMATION | + PROCESS_VM_OPERATION | PROCESS_VM_WRITE | + PROCESS_VM_READ, FALSE, pid))) { + return -1; + } + + return kill_SIGTERM_by_handle(process, exit_code); +} + /* * This way of terminating processes is not gentle: they get no chance to * clean up after themselves (closing file handles, removing .lock files, * terminating spawned processes (if any), etc). */ -static int terminate_process(pid_t pid, int exit_code) +static int kill_SIGKILL(pid_t pid, int exit_code) { HANDLE process; int ret; @@ -649,7 +655,7 @@ static int terminate_process(pid_t pid, int exit_code) return ret; } -static int test_process(pid_t pid, int exit_code UNUSED_PARAM) +static int kill_SIGTEST(pid_t pid, int exit_code UNUSED_PARAM) { HANDLE process; @@ -664,13 +670,13 @@ static int test_process(pid_t pid, int exit_code UNUSED_PARAM) int kill(pid_t pid, int sig) { if (sig == SIGTERM) { - return kill_pids(pid, 128+sig, exit_process); + return kill_pids(pid, 128+sig, kill_SIGTERM); } else if (sig == SIGKILL) { - return kill_pids(pid, 128+sig, terminate_process); + return kill_pids(pid, 128+sig, kill_SIGKILL); } else if (sig == 0) { - return kill_pids(pid, 128+sig, test_process); + return kill_pids(pid, 128+sig, kill_SIGTEST); } errno = EINVAL; -- cgit v1.2.3-55-g6feb