diff --git a/sx b/sx index 45371cb..c789759 100755 --- a/sx +++ b/sx @@ -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 diff --git a/sx.1 b/sx.1 index b631844..8863635 100644 --- a/sx.1 +++ b/sx.1 @@ -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