sx: further tuning the signal situation

This commit removes the complication of trying to handle client PIDs as
doing so didn't provide any noticable gain.  The main issue was that
both `wait' commands didn't wait on their respective PIDs while the
final cleanup wasn't called with both client and server PIDs and so
the server remained running due to -noreset.

The exit status of `sx' should again reflect that of the Xorg's
which may be useful for scripts.

Also slightly improves the comments on how Xorg handles USR1 signals.

Fixes https://github.com/Earnestly/sx/issues/19
This commit is contained in:
Earnestly 2020-10-06 18:51:39 +01:00
parent 93f50006dc
commit 2742daede4
2 changed files with 26 additions and 22 deletions

35
sx
View File

@ -3,18 +3,19 @@
# requires xauth Xorg /dev/urandom # requires xauth Xorg /dev/urandom
cleanup() { cleanup() {
for pid; do if kill -0 "$pid" 2> /dev/null; then
if kill -0 "$pid" 2> /dev/null; then kill "$pid"
kill "$pid" wait "$pid"
wait "$pid" xorg=$?
fi fi
done
if ! stty "$stty"; then if ! stty "$stty"; then
stty sane stty sane
fi fi
xauth remove :"$tty" xauth remove :"$tty"
exit "${xorg:-0}"
} }
stty=$(stty -g) stty=$(stty -g)
@ -30,21 +31,17 @@ touch -- "$XAUTHORITY"
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 ' ')"
for signal in HUP INT TERM; do for signal in HUP INT TERM QUIT; do
# The client variable is set by the USR1 signal trap and contains the trap 'cleanup; trap - "$signal"; kill -s "$signal" "$$"' "$signal"
# client's PID.
# shellcheck disable=SC2154
trap 'cleanup "$client" "$server"; trap - "$signal"; kill -s "$signal" "$$"' "$signal"
done done
trap cleanup EXIT
# Xorg will check if its USR1 disposition is SIG_IGN and use this state to # Xorg will check whether it inherited a USR1 with a disposition of SIG_IGN and
# reply back to the parent process with a SIGUSR1 of its own as indication it # use this state to reply back to the parent process with its own USR1.
# is ready to accept connections. # This is done to indicate that the server is ready to accept connections.
# Taking advantage of this feature allows launching the client directly from a # Taking advantage of this feature allows launching the client directly from a
# USR1 signal trap which obviates the need to poll for server readiness. # USR1 signal trap which obviates the need to poll for server readiness.
trap 'DISPLAY=:$tty "${@:-$cfgdir/sxrc}" & client=$!; wait' USR1 trap 'DISPLAY=:$tty exec "${@:-$cfgdir/sxrc}" & wait "$!"' USR1
(trap '' USR1 && exec Xorg :"$tty" -keeptty vt"$tty" -noreset -auth "$XAUTHORITY") & (trap '' USR1 && exec Xorg :"$tty" -keeptty vt"$tty" -noreset -auth "$XAUTHORITY") & pid=$!
server=$! wait "$pid"
wait cleanup

13
sx.1
View File

@ -1,4 +1,4 @@
.TH SX 1 "6 February 2019" 2.1.1 .TH SX 1 "6 October 2020" 2.1.6
.SH NAME .SH NAME
sx \- start an xorg server sx \- start an xorg server
.SH SYNOPSIS .SH SYNOPSIS
@ -10,7 +10,9 @@ can be used to replace both
.BR xinit (1) .BR xinit (1)
and and
.BR startx (1) .BR startx (1)
for starting an Xorg server with an initial client. By default for starting an
.BR Xorg (1)
server with an initial client. By default
.B sx .B sx
will attempt to execute will attempt to execute
.I \%XDG_CONFIG_HOME/sx/sxrc .I \%XDG_CONFIG_HOME/sx/sxrc
@ -20,6 +22,11 @@ is provided. The
.I command .I command
may have additional may have additional
.IR arguments . .IR arguments .
.SH EXIT STATUS
.B sx
will attempt to inherit the exit status from the
.BR Xorg (1)
server. Otherwise it is 0.
.SH ENVIRONMENT .SH ENVIRONMENT
.TP .TP
.B XAUTHORITY .B XAUTHORITY
@ -75,7 +82,7 @@ Use an existing
.I .Xinitrc .I .Xinitrc
by specifying an appropriate interpreter such as by specifying an appropriate interpreter such as
.BR sh (1) .BR sh (1)
or marking it executable with a correct interpreter line: or making it executable with a correct interpreter line:
.IP .IP
.EX .EX
.B sx sh ~/.Xinitrc .B sx sh ~/.Xinitrc