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 REQUIRES
Notable requirements are for GNU timeout and GNU tail which is used for the * Xorg
--pid option. Beyond these the usual set of POSIX command-line tools along * xauth
with Xorg and xauth are required. * 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 PREFIX=/usr DESTDIR=staged install
make DESTDIR=staged PREFIX=/usr install

21
sx
View File

@ -1,7 +1,7 @@
#!/bin/sh -- #!/bin/sh --
# sx - start an xserver # 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 # 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. # 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 # Send SIGKILL after 10 seconds if the xserver is taking too long to
# terminate. # terminate.
timeout 10 tail --pid="$1" -f /dev/null interval=10
until [ "$interval" -le 0 ]; do
case $? in if ! kill -0 "$1"; then
124) kill -s KILL "$1" break
esac 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 fi
if ! stty "$stty"; then if ! stty "$stty"; then
@ -48,7 +55,7 @@ touch "$XAUTHORITY"
trap 'cleanup "$pid"' EXIT 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 # 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 # its own SIGUSR1 back to the parent process when it is ready to accept