From 89aa9d783c535670c6aed1d32c8740b7474cca00 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 23 May 2023 10:57:56 +0100 Subject: win32: changes to signal handling Use an exit code of the form (signal << 24) when a process exits due to a signal. This replaces the previous use of (signal + 128). This makes it easier to distinguish exit codes from signals. Allow kill(2) to handle all defined signals, not just EXIT, TERM and KILL. The kill and timeout applets now accept any defined signals. Convert certain Windows status codes Unix-style signal codes. In ash: - Exit as if with SIGINT in raise_interrupt() rather than call raise(SIGINT). The latter returns an exit code of 3. - Detect if a child process exits as if with SIGINT. If not and if the parent is an interactive top-level shell, reset pending_int. This prevents the parent from seeing an INT if the child hasn't reported it exited due to INT. (Probably due to it being an interactive shell.) Costs 132-136 bytes. --- coreutils/timeout.c | 4 ++-- findutils/xargs.c | 2 +- include/mingw.h | 3 ++- shell/ash.c | 31 +++++++++++++++++++++++++------ win32/process.c | 21 +++++++++++++-------- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/coreutils/timeout.c b/coreutils/timeout.c index 3565c005d..8f306529f 100644 --- a/coreutils/timeout.c +++ b/coreutils/timeout.c @@ -54,7 +54,7 @@ static HANDLE child = INVALID_HANDLE_VALUE; static void kill_child(void) { if (child != INVALID_HANDLE_VALUE) { - kill_SIGTERM_by_handle(child); + kill_signal_by_handle(child, SIGTERM); } } @@ -130,7 +130,7 @@ int timeout_main(int argc UNUSED_PARAM, char **argv) #if !ENABLE_PLATFORM_MINGW32 if (signo < 0) #else - if (signo != SIGTERM && signo != SIGKILL && signo != 0) + if (!is_valid_signal(signo)) #endif bb_error_msg_and_die("unknown signal '%s'", opt_s); diff --git a/findutils/xargs.c b/findutils/xargs.c index bc47a869f..1e7d8de02 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c @@ -216,7 +216,7 @@ static BOOL WINAPI ctrl_handler(DWORD dwCtrlType) } } # endif - exit(128 + SIGINT); + exit(SIGINT << 24); return TRUE; } return FALSE; diff --git a/include/mingw.h b/include/mingw.h index 6204ded6d..f94804d91 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -540,7 +540,8 @@ int mingw_execve(const char *cmd, char *const *argv, char *const *envp); #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':') -int kill_SIGTERM_by_handle(HANDLE process); +int kill_signal_by_handle(HANDLE process, int sig); +int FAST_FUNC is_valid_signal(int number); #define find_mount_point(n, s) find_mount_point(n) diff --git a/shell/ash.c b/shell/ash.c index 93c402a8b..87df555dd 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -884,9 +884,13 @@ raise_interrupt(void) /* pending_sig = 0; - now done in signal_handler() */ if (!(rootshell && iflag)) { +#if !ENABLE_PLATFORM_MINGW32 /* Kill ourself with SIGINT */ signal(SIGINT, SIG_DFL); raise(SIGINT); +#else + exit(SIGINT << 24); +#endif } #if ENABLE_PLATFORM_MINGW32 if (iflag) @@ -4122,8 +4126,10 @@ struct job { #define JOBDONE 2 /* all procs are completed */ unsigned state: 8, -#if JOBS +#if JOBS || ENABLE_PLATFORM_MINGW32 sigint: 1, /* job was killed by SIGINT */ +#endif +#if JOBS jobctl: 1, /* job running under job control */ #endif waited: 1, /* true if this entry has been waited for */ @@ -4840,7 +4846,7 @@ waitpid_child(int *status, int wait_flags) HANDLE *proclist; pid_t pid = -1; DWORD win_status, idx; - int i; + int i, sig; for (jb = curjob; jb; jb = jb->prev_job) { if (jb->state != JOBDONE) @@ -4872,9 +4878,18 @@ waitpid_child(int *status, int wait_flags) wait_flags&WNOHANG ? 1 : INFINITE); if (idx < pid_nr) { GetExitCodeProcess(proclist[idx], &win_status); - *status = (int)win_status << 8; - if (win_status == 128 + SIGTERM || win_status == 128 + SIGKILL) - *status += win_status - 128; + if (win_status == 0xc0000005) + win_status = SIGSEGV << 24; + else if (win_status == 0xc000013a) + win_status = SIGINT << 24; + + // When a process is terminated as if by a signal the exit + // code is zero apart from the signal in its topmost byte. + sig = win_status >> 24; + if (sig != 0 && win_status == sig << 24 && is_valid_signal(sig)) + *status = sig; + else + *status = (int)win_status << 8; pid = pidlist[idx]; } done: @@ -5241,7 +5256,7 @@ getstatus(struct job *job) { /* XXX: limits number of signals */ retval = WTERMSIG(status); -#if JOBS +#if JOBS || ENABLE_PLATFORM_MINGW32 if (retval == SIGINT) job->sigint = 1; #endif @@ -6051,6 +6066,10 @@ waitforjob(struct job *jp) return exitstatus; st = getstatus(jp); +#if ENABLE_PLATFORM_MINGW32 + if (!jp->sigint && iflag && rootshell) + pending_int = 0; +#endif #if JOBS if (jp->jobctl) { xtcsetpgrp(ttyfd, rootpid); diff --git a/win32/process.c b/win32/process.c index 228b0b30e..ebc006d4e 100644 --- a/win32/process.c +++ b/win32/process.c @@ -705,14 +705,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 kill_signal_by_handle(HANDLE process, int sig) { DWORD code; int ret = 0; if (GetExitCodeProcess(process, &code) && code == STILL_ACTIVE) { DECLARE_PROC_ADDR(DWORD, ExitProcess, LPVOID); - PVOID arg = (PVOID)(intptr_t)(128 + SIGTERM); + PVOID arg = (PVOID)(intptr_t)(sig << 24); DWORD thread_id; HANDLE thread; @@ -734,7 +734,7 @@ int kill_SIGTERM_by_handle(HANDLE process) return ret; } -static int kill_SIGTERM(pid_t pid, int sig UNUSED_PARAM) +static int kill_signal(pid_t pid, int sig) { HANDLE process; @@ -745,7 +745,7 @@ static int kill_SIGTERM(pid_t pid, int sig UNUSED_PARAM) return -1; } - return kill_SIGTERM_by_handle(process); + return kill_signal_by_handle(process, sig); } /* @@ -765,18 +765,23 @@ static int kill_SIGKILL(pid_t pid, int sig) } if (sig == SIGKILL) - ret = !TerminateProcess(process, 128 + SIGKILL); + ret = !TerminateProcess(process, SIGKILL << 24); CloseHandle(process); return ret; } +int FAST_FUNC is_valid_signal(int number) +{ + return isalpha(*get_signame(number)); +} + int kill(pid_t pid, int sig) { - if (sig == SIGTERM) - return kill_pids(pid, sig, kill_SIGTERM); - else if (sig == SIGKILL || sig == 0) + if (sig == SIGKILL || sig == 0) return kill_pids(pid, sig, kill_SIGKILL); + else if (is_valid_signal(sig)) + return kill_pids(pid, sig, kill_signal); errno = EINVAL; return -1; -- cgit v1.2.3-55-g6feb