From 30457a3139379e02c60405becdd8b834ec46229d Mon Sep 17 00:00:00 2001 From: Earnestly Date: Thu, 7 Dec 2017 11:58:40 +0000 Subject: [PATCH] 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. --- README | 12 +++++------- sx | 21 ++++++++++++++------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README b/README index fb16d5c..b552be5 100644 --- a/README +++ b/README @@ -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 diff --git a/sx b/sx index bee0be9..e01c138 100755 --- a/sx +++ b/sx @@ -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