X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Ftestsuite%2Fgetpid.sh;h=04708e6c27ad7d6ccb9bad9a78786ee1976e1b75;hb=HEAD;hp=19ced7620f13d9befb989eae635ab1fcdfc2e6a2;hpb=257fe922d2c2ee822c372bc5b979075271915553;p=ngircd-alex.git diff --git a/src/testsuite/getpid.sh b/src/testsuite/getpid.sh index 19ced762..7a3dbe37 100755 --- a/src/testsuite/getpid.sh +++ b/src/testsuite/getpid.sh @@ -1,48 +1,67 @@ #!/bin/sh # ngIRCd Test Suite -# $Id: getpid.sh,v 1.5 2006/08/05 00:15:28 alex Exp $ +# +# Try to detect the PID of a running process of the current user. +# + +set -u # did we get a name? -[ $# -ne 1 ] && exit 1 +if [ $# -ne 1 ]; then + echo "Usage: $0 " >&2 + exit 1 +fi -# detect flags for "ps" and "head" UNAME=`uname` -if [ $UNAME = "FreeBSD" ]; then - PS_FLAGS="-a"; PS_PIDCOL="1"; HEAD_FLAGS="-n 1" -elif [ $UNAME = "A/UX" ]; then - PS_FLAGS="-ae"; PS_PIDCOL="1"; HEAD_FLAGS="-1" -elif [ $UNAME = "GNU" ]; then - PS_FLAGS="-ax"; PS_PIDCOL="2"; HEAD_FLAGS="-n 1" -elif [ $UNAME = "Linux" ]; then - PS_FLAGS="ax"; PS_PIDCOL="1"; HEAD_FLAGS="-n 1" -elif [ $UNAME = "SunOS" ]; then - PS_FLAGS="-af"; PS_PIDCOL=2; HEAD_FLAGS="-n 1" -else - PS_FLAGS="-af"; PS_PIDCOL="2"; HEAD_FLAGS="-n 1" - ps $PS_FLAGS > /dev/null 2>&1 - if [ $? -ne 0 ]; then PS_FLAGS="a"; PS_PIDCOL="1"; fi + +# Use pgrep(1) whenever possible +if [ -x /usr/bin/pgrep ]; then + case "$UNAME" in + "FreeBSD") + PGREP_FLAGS="-a" + ;; + *) + PGREP_FLAGS="" + esac + if [ -n "${LOGNAME:-}" ] || [ -n "${USER:-}" ]; then + # Try to narrow the search down to the current user ... + exec /usr/bin/pgrep $PGREP_FLAGS -n -u "${LOGNAME:-$USER}" "$1" + else + # ... but neither LOGNAME nor USER were set! + exec /usr/bin/pgrep $PGREP_FLAGS -n "$1" + fi fi -# debug output -#echo "$0: UNAME=$UNAME" -#echo "$0: PS_FLAGS=$PS_FLAGS" -#echo "$0: PS_PIDCOL=$PS_PIDCOL" -#echo "$0: HEAD_FLAGS=$HEAD_FLAGS" +# pidof(1) could be a good alternative on elder Linux systems +if [ -x /bin/pidof ]; then + exec /bin/pidof -s "$1" +fi + +# fall back to ps(1) and parse its output: +# detect flags for "ps" and "head" +PS_PIDCOL=1 +case "$UNAME" in + "A/UX"|"GNU"|"SunOS") + PS_FLAGS="-a"; PS_PIDCOL=2 + ;; + "Haiku") + PS_FLAGS="-o Id -o Team" + ;; + *) + # Linux (GNU coreutils), Free/Net/OpenBSD, ... + PS_FLAGS="-o pid,comm" +esac # search PID -ps $PS_FLAGS > procs.tmp -cat procs.tmp | \ - grep -v "$0" | grep "$1" | \ - awk "{print \$$PS_PIDCOL}" | \ - sort -n > pids.tmp -pid=`head $HEAD_FLAGS pids.tmp` +ps $PS_FLAGS >procs.tmp +grep -v "$$" procs.tmp | grep "$1" | \ + awk "{print \$$PS_PIDCOL}" | \ + sort -nr >pids.tmp +pid=`head -1 pids.tmp` rm -rf procs.tmp pids.tmp # validate PID -[ "$pid" -gt 1 ] > /dev/null 2>&1 -[ $? -ne 0 ] && exit 1 +[ "$pid" -gt 1 ] >/dev/null 2>&1 || exit 1 echo $pid exit 0 - -# -eof-