From 1c4fbda36cf071eca1ac29ee41f2440a3597fb3f Mon Sep 17 00:00:00 2001 From: Earnestly Date: Wed, 13 Dec 2017 13:53:44 +0000 Subject: [PATCH] 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. --- sx | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/sx b/sx index bad8d13..7c49290 100755 --- a/sx +++ b/sx @@ -4,11 +4,18 @@ # requires xauth Xorg cleanup() { + if [ "$pid" ] && kill -0 "$pid" 2> /dev/null; then + kill -s TERM "$pid" + wait "$pid" + r=$? + fi + if ! stty "$stty"; then stty sane fi xauth remove :"$tty" + exit "${r:-$?}" } stty=$(stty -g) @@ -24,8 +31,7 @@ export XAUTHORITY=${XAUTHORITY:-$cfgdir/xauthority} mkdir -p "$cfgdir" "${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 ' ')" # 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 # from the SIGUSR1 handler and avoid the need to poll for server readiness. 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" - -if kill -0 "$pid" 2> /dev/null; then - kill -s TERM "$pid" - wait "$pid" -fi