sx: implement more robust signal handling
This ought to address the majority of the issues presented in https://github.com/Earnestly/sx/issues/15
This commit is contained in:
parent
72ae82f648
commit
93f50006dc
38
sx
38
sx
|
@ -1,21 +1,20 @@
|
||||||
#!/bin/sh --
|
#!/bin/sh --
|
||||||
# sx - start an xserver
|
# sx - start an xserver
|
||||||
|
# requires xauth Xorg /dev/urandom
|
||||||
# requires xauth Xorg
|
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
if [ "$pid" ] && kill -0 "$pid" 2> /dev/null; then
|
for pid; do
|
||||||
kill -s TERM "$pid"
|
if kill -0 "$pid" 2> /dev/null; then
|
||||||
|
kill "$pid"
|
||||||
wait "$pid"
|
wait "$pid"
|
||||||
r=$?
|
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
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)
|
||||||
|
@ -29,14 +28,23 @@ mkdir -p -- "$cfgdir" "$datadir"
|
||||||
export XAUTHORITY="${XAUTHORITY:-$datadir/xauthority}"
|
export XAUTHORITY="${XAUTHORITY:-$datadir/xauthority}"
|
||||||
touch -- "$XAUTHORITY"
|
touch -- "$XAUTHORITY"
|
||||||
|
|
||||||
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
|
for signal in HUP INT TERM; do
|
||||||
# reply back to the parent process with its own SIGUSR1 as an indication it is
|
# The client variable is set by the USR1 signal trap and contains the
|
||||||
# ready to accept connections.
|
# client's PID.
|
||||||
# Taking advantage of this feature allows us to launch our client directly
|
# shellcheck disable=SC2154
|
||||||
# from a SIGUSR1 handler and avoid the need to poll for server readiness.
|
trap 'cleanup "$client" "$server"; trap - "$signal"; kill -s "$signal" "$$"' "$signal"
|
||||||
trap 'DISPLAY=:$tty "${@:-$cfgdir/sxrc}"' USR1
|
done
|
||||||
(trap '' USR1 && exec Xorg :"$tty" -keeptty vt"$tty" -noreset -auth "$XAUTHORITY") & pid=$!
|
trap cleanup EXIT
|
||||||
wait "$pid"
|
|
||||||
|
# Xorg will check if its USR1 disposition is SIG_IGN and use this state to
|
||||||
|
# reply back to the parent process with a SIGUSR1 of its own as indication it
|
||||||
|
# is ready to accept connections.
|
||||||
|
# Taking advantage of this feature allows launching the client directly from a
|
||||||
|
# USR1 signal trap which obviates the need to poll for server readiness.
|
||||||
|
trap 'DISPLAY=:$tty "${@:-$cfgdir/sxrc}" & client=$!; wait' USR1
|
||||||
|
|
||||||
|
(trap '' USR1 && exec Xorg :"$tty" -keeptty vt"$tty" -noreset -auth "$XAUTHORITY") &
|
||||||
|
server=$!
|
||||||
|
wait
|
||||||
|
|
Loading…
Reference in New Issue