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
cleanup() {
for pid; do
if kill -0 "$pid" 2> /dev/null; then
kill "$pid"
wait "$pid"
fi
done
if kill -0 "$pid" 2> /dev/null; then
kill "$pid"
wait "$pid"
xorg=$?
fi
if ! stty "$stty"; then
stty sane
fi
xauth remove :"$tty"
exit "${xorg:-0}"
}
stty=$(stty -g)
@ -30,21 +31,17 @@ touch -- "$XAUTHORITY"
xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(od -An -N16 -tx /dev/urandom | tr -d ' ')"
for signal in HUP INT TERM; do
# The client variable is set by the USR1 signal trap and contains the
# client's PID.
# shellcheck disable=SC2154
trap 'cleanup "$client" "$server"; trap - "$signal"; kill -s "$signal" "$$"' "$signal"
for signal in HUP INT TERM QUIT; do
trap 'cleanup; trap - "$signal"; kill -s "$signal" "$$"' "$signal"
done
trap cleanup EXIT
# 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.
# 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.
# 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
# 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") &
server=$!
wait
(trap '' USR1 && exec Xorg :"$tty" -keeptty vt"$tty" -noreset -auth "$XAUTHORITY") & pid=$!
wait "$pid"
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
sx \- start an xorg server
.SH SYNOPSIS
@ -10,7 +10,9 @@ can be used to replace both
.BR xinit (1)
and
.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
will attempt to execute
.I \%XDG_CONFIG_HOME/sx/sxrc
@ -20,6 +22,11 @@ is provided. The
.I command
may have additional
.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
.TP
.B XAUTHORITY
@ -75,7 +82,7 @@ Use an existing
.I .Xinitrc
by specifying an appropriate interpreter such as
.BR sh (1)
or marking it executable with a correct interpreter line:
or making it executable with a correct interpreter line:
.IP
.EX
.B sx sh ~/.Xinitrc