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.
This commit is contained in:
earnestly 2021-07-04 15:12:30 +01:00
parent e3687899a2
commit 2ef0b3bf03
1 changed files with 6 additions and 6 deletions

12
sx
View File

@ -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