From cfa888eb571d92d4de02b406f0dad15e4fa26d19 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 25 Jul 2020 12:48:13 +0100 Subject: win32: code shrink kill(2) - Drop exit_code argument from kill_SIGTERM_by_handle() - Pass signal number rather than exit code to other functions - Merge kill_SIGKILL() and kill_SIGTEST() Saves 112 bytes. --- coreutils/timeout.c | 2 +- include/mingw.h | 2 +- win32/process.c | 48 +++++++++++++++++------------------------------- 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/coreutils/timeout.c b/coreutils/timeout.c index 1a9660183..06c134a37 100644 --- a/coreutils/timeout.c +++ b/coreutils/timeout.c @@ -52,7 +52,7 @@ HANDLE child = INVALID_HANDLE_VALUE; static void kill_child(void) { if (child != INVALID_HANDLE_VALUE) { - kill_SIGTERM_by_handle(child, 128+SIGTERM); + kill_SIGTERM_by_handle(child); } } #endif diff --git a/include/mingw.h b/include/mingw.h index c75b06330..a67b161c7 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -502,7 +502,7 @@ int mingw_execve(const char *cmd, char *const *argv, char *const *envp); #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); +int kill_SIGTERM_by_handle(HANDLE process); #define find_mount_point(n, s) find_mount_point(n) diff --git a/win32/process.c b/win32/process.c index 6224ce362..ac63a9c58 100644 --- a/win32/process.c +++ b/win32/process.c @@ -624,9 +624,9 @@ void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm) * of the indicated process. Zero indicates the current process; negative * indicates the process with process ID -pid. */ -typedef int (*kill_callback)(pid_t pid, int exit_code); +typedef int (*kill_callback)(pid_t pid, int sig); -static int kill_pids(pid_t pid, int exit_code, kill_callback killer) +static int kill_pids(pid_t pid, int sig, kill_callback killer) { DWORD pids[16384]; int max_len = sizeof(pids) / sizeof(*pids), i, len, ret = 0; @@ -684,7 +684,7 @@ static int kill_pids(pid_t pid, int exit_code, kill_callback killer) for (i = len - 1; i >= 0; i--) { SetLastError(0); - if (killer(pids[i], exit_code)) { + if (killer(pids[i], sig)) { errno = err_win_to_posix(); ret = -1; } @@ -725,14 +725,14 @@ static inline int process_architecture_matches_current(HANDLE process) * http://www.drdobbs.com/a-safer-alternative-to-terminateprocess/184416547 * */ -int kill_SIGTERM_by_handle(HANDLE process, int exit_code) +int kill_SIGTERM_by_handle(HANDLE process) { DWORD code; int ret = 0; if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) { DECLARE_PROC_ADDR(DWORD, ExitProcess, LPVOID); - PVOID arg = (PVOID)(intptr_t)exit_code; + PVOID arg = (PVOID)(intptr_t)(128 + SIGTERM); DWORD thread_id; HANDLE thread; @@ -754,7 +754,7 @@ int kill_SIGTERM_by_handle(HANDLE process, int exit_code) return ret; } -static int kill_SIGTERM(pid_t pid, int exit_code) +static int kill_SIGTERM(pid_t pid, int sig UNUSED_PARAM) { HANDLE process; @@ -765,52 +765,38 @@ static int kill_SIGTERM(pid_t pid, int exit_code) return -1; } - return kill_SIGTERM_by_handle(process, exit_code); + return kill_SIGTERM_by_handle(process); } /* * 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). + * + * If the signal isn't SIGKILL just check if the target process exists. */ -static int kill_SIGKILL(pid_t pid, int exit_code) +static int kill_SIGKILL(pid_t pid, int sig) { HANDLE process; - int ret; + int ret = 0; if (!(process=OpenProcess(PROCESS_TERMINATE, FALSE, pid))) { return -1; } - ret = !TerminateProcess(process, exit_code); + if (sig == SIGKILL) + ret = !TerminateProcess(process, 128 + SIGKILL); CloseHandle(process); return ret; } -static int kill_SIGTEST(pid_t pid, int exit_code UNUSED_PARAM) -{ - HANDLE process; - - if (!(process=OpenProcess(PROCESS_TERMINATE, FALSE, pid))) { - return -1; - } - - CloseHandle(process); - return 0; -} - int kill(pid_t pid, int sig) { - if (sig == SIGTERM) { - return kill_pids(pid, 128+sig, kill_SIGTERM); - } - else if (sig == SIGKILL) { - return kill_pids(pid, 128+sig, kill_SIGKILL); - } - else if (sig == 0) { - return kill_pids(pid, 128+sig, kill_SIGTEST); - } + if (sig == SIGTERM) + return kill_pids(pid, sig, kill_SIGTERM); + else if (sig == SIGKILL || sig == 0) + return kill_pids(pid, sig, kill_SIGKILL); errno = EINVAL; return -1; -- cgit v1.2.3-55-g6feb