From 2ef0b3bf037ac0a3489df33615399e685348c579 Mon Sep 17 00:00:00 2001 From: earnestly Date: Sun, 4 Jul 2021 15:12:30 +0100 Subject: [PATCH] sx: The signal saga concludes This ought to be the last effort on signal handling. Prior to this design the resending of the signals was ineffective due to the cleanup function explicitly calling exit. As a result this would prevent the subsequent commands in the trap from executing. Instead a variable is employed to guard the exit on condition of not receiving an INT signal. On UNIX systems this particular signal needs to be resent such that the calling program knows the process was interrupted. See https://www.cons.org/cracauer/sigint.html This situation exists because, besides bash, all shells treat EXIT traps as a mechanism to execute code after the program ends, as if the code it runs was pasted at the end of the file, rather than to run it however the program terminated. --- sx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sx b/sx index c789759..b4e357f 100755 --- a/sx +++ b/sx @@ -3,7 +3,7 @@ # requires xauth Xorg /dev/urandom cleanup() { - if kill -0 "$pid" 2> /dev/null; then + if [ "$pid" ] && kill -0 "$pid" 2> /dev/null; then kill "$pid" wait "$pid" xorg=$? @@ -15,7 +15,9 @@ cleanup() { xauth remove :"$tty" - exit "${xorg:-0}" + if [ "$1" = exit ]; then + exit "${xorg:-0}" + fi } stty=$(stty -g) @@ -31,9 +33,8 @@ touch -- "$XAUTHORITY" xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(od -An -N16 -tx /dev/urandom | tr -d ' ')" -for signal in HUP INT TERM QUIT; do - trap 'cleanup; trap - "$signal"; kill -s "$signal" "$$"' "$signal" -done +trap 'cleanup; trap - INT; kill -INT "$$"' INT +trap 'cleanup exit' EXIT HUP TERM QUIT # Xorg will check whether it inherited a USR1 with a disposition of SIG_IGN and # use this state to reply back to the parent process with its own USR1. @@ -44,4 +45,3 @@ trap 'DISPLAY=:$tty exec "${@:-$cfgdir/sxrc}" & wait "$!"' USR1 (trap '' USR1 && exec Xorg :"$tty" -keeptty vt"$tty" -noreset -auth "$XAUTHORITY") & pid=$! wait "$pid" -cleanup