sx: traps can explicitly set an exit status

The prior commit 666b87e would still fail if the sx program itself was
killed leaving an xorg server behind.  While trap handlers aren't
typically allowed to change the exit status, if we explicitly exit with
a value then it is honoured.

This works around the subshell setting an exit status of 138 (due to
SIGUSR1) by explicitly using the return value from the last wait or the
last command if the server never started at all.
This commit is contained in:
Earnestly 2017-12-13 13:53:44 +00:00
parent 666b87ef26
commit 1c4fbda36c
1 changed files with 9 additions and 12 deletions

21
sx
View File

@ -4,11 +4,18 @@
# requires xauth Xorg # requires xauth Xorg
cleanup() { cleanup() {
if [ "$pid" ] && kill -0 "$pid" 2> /dev/null; then
kill -s TERM "$pid"
wait "$pid"
r=$?
fi
if ! stty "$stty"; then if ! stty "$stty"; then
stty sane stty sane
fi fi
xauth remove :"$tty" xauth remove :"$tty"
exit "${r:-$?}"
} }
stty=$(stty -g) stty=$(stty -g)
@ -24,8 +31,7 @@ export XAUTHORITY=${XAUTHORITY:-$cfgdir/xauthority}
mkdir -p "$cfgdir" "${XAUTHORITY%/*}" mkdir -p "$cfgdir" "${XAUTHORITY%/*}"
touch "$XAUTHORITY" touch "$XAUTHORITY"
trap 'cleanup "$pid"' EXIT trap 'cleanup' EXIT
xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(od -An -N16 -tx /dev/urandom | tr -d ' ')" xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(od -An -N16 -tx /dev/urandom | tr -d ' ')"
# Xorg will check if its SIGUSR1 disposition is SIG_IGN and use this state to # Xorg will check if its SIGUSR1 disposition is SIG_IGN and use this state to
@ -34,14 +40,5 @@ xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(od -An -N16 -tx /dev/urandom | tr -d ' '
# Taking advantage of this feature allows us to launch our client directly # Taking advantage of this feature allows us to launch our client directly
# from the SIGUSR1 handler and avoid the need to poll for server readiness. # from the SIGUSR1 handler and avoid the need to poll for server readiness.
trap 'DISPLAY=:$tty "${@:-$cfgdir/sxrc}"' USR1 trap 'DISPLAY=:$tty "${@:-$cfgdir/sxrc}"' USR1
( (trap '' USR1 && exec /usr/lib/xorg-server/Xorg :"$tty" -keeptty vt"$tty" -noreset -auth "$XAUTHORITY") & pid=$!
trap '' USR1
exec /usr/lib/xorg-server/Xorg :"$tty" -keeptty vt"$tty" -noreset -auth "$XAUTHORITY"
) & pid=$!
wait "$pid" wait "$pid"
if kill -0 "$pid" 2> /dev/null; then
kill -s TERM "$pid"
wait "$pid"
fi