sx: drop dependencies on coreutils and util-linux

Upon review of GNU tail I discovered that the implemention of --pid was
the same method as polling kill -0 on a sleep interval, which defaults
to 1 second unless -s is used.

This is perhaps expected due to the lack of a pwait(2) system call, I
decided to implement the same mechanic myself and drop both the
requirement on GNU tail and GNU timeout at the same time.

This commit also replaces mcookie from util-linux with openssl rand
which is used to generate a random MD5 checksum for use in the
MIT-MAGIC-COOKIE-1 value.
This commit is contained in:
Earnestly 2017-12-07 11:58:40 +00:00
parent 39702615b7
commit 30457a3139
2 changed files with 19 additions and 14 deletions

12
README
View File

@ -34,12 +34,10 @@ INTRODUCTION
REQUIRES
Notable requirements are for GNU timeout and GNU tail which is used for the
--pid option. Beyond these the usual set of POSIX command-line tools along
with Xorg and xauth are required.
* Xorg
* xauth
* openssl (used to generate the MIT-MAGIC-COOKIE-1 value)
BUILD
INSTALL
As there's nothing to build, simply install using a prefered PREFIX.
make DESTDIR=staged PREFIX=/usr install
make PREFIX=/usr DESTDIR=staged install

21
sx
View File

@ -1,7 +1,7 @@
#!/bin/sh --
# sx - start an xserver
# requires timeout tail xauth Xorg
# requires xauth Xorg
# I'm willing to take advantage of errexit for this script as roughly 85% of
# the error checking would just be exiting on command failure.
@ -17,12 +17,19 @@ cleanup() {
# Send SIGKILL after 10 seconds if the xserver is taking too long to
# terminate.
timeout 10 tail --pid="$1" -f /dev/null
case $? in
124) kill -s KILL "$1"
esac
interval=10
until [ "$interval" -le 0 ]; do
if ! kill -0 "$1"; then
break
fi
sleep 1
interval=$((interval - 1))
done
# Make sure that the PID still refers to an Xorg process
if test "$interval" -le 0 -a "$(ps -o comm= "$1")" = Xorg; then
kill -s KILL "$1"
fi
fi
if ! stty "$stty"; then
@ -48,7 +55,7 @@ touch "$XAUTHORITY"
trap 'cleanup "$pid"' EXIT
xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(mcookie)"
xauth add :"$tty" MIT-MAGIC-COOKIE-1 "$(openssl rand -hex 16)"
# Xorg will check if SIGUSR1 was set to SIG_IGN in its environment and issue
# its own SIGUSR1 back to the parent process when it is ready to accept