]> arthur.barton.de Git - ngircd-alex.git/commitdiff
Test suite: simplify and enhance getpid.sh
authorAlexander Barton <alex@barton.de>
Mon, 1 Apr 2024 19:38:21 +0000 (21:38 +0200)
committerAlexander Barton <alex@barton.de>
Fri, 5 Apr 2024 21:16:07 +0000 (23:16 +0200)
- Try to only search for processes of the current user.
- Prefer using pgrep (in addition to pidof) when available.
- Streamline system overrides.
- Get rid of HEAD_FLAGS: all systems so far support "-1".
- Use "ps -o pid,comm" as the default, which is POSIX.1.
- Use "sort -r" to hopefully get the newest (=highest) PID, which is the
  case on older systems not using randomized PIDs at least.

src/testsuite/getpid.sh

index 55997ad825efffffb4b48db7fda577ff5e641797..465def64605356d0c74b45a6348f7a1bcde4a8cd 100755 (executable)
@@ -1,46 +1,57 @@
 #!/bin/sh
 # ngIRCd Test Suite
+#
+# Try to detect the PID of a running process of the current user.
+#
 
 set -u
 
 # did we get a name?
-[ $# -ne 1 ] && exit 1
-
-[ -x /bin/pidof ] && exec /bin/pidof -s "$1"
+if [ $# -ne 1 ]; then
+       echo "Usage: $0 <name>" >&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="-af"; PS_PIDCOL="2"; HEAD_FLAGS="-1"
-elif [ $UNAME = "GNU" ]; then
-       PS_FLAGS="-ax"; PS_PIDCOL="2"; HEAD_FLAGS="-n 1"
-elif [ $UNAME = "Haiku" ]; then
-       PS_FLAGS="-o Id -o Team"; PS_PIDCOL="1"; HEAD_FLAGS="-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
+       exec /usr/bin/pgrep $PGREP_FLAGS -n -u "$LOGNAME" "$1"
 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" | \
+grep -v "$$" procs.tmp | grep "$1" | \
        awk "{print \$$PS_PIDCOL}" | \
-       sort -n >pids.tmp
-pid=`head $HEAD_FLAGS pids.tmp`
+       sort -nr >pids.tmp
+pid=`head -1 pids.tmp`
 rm -rf procs.tmp pids.tmp
 
 # validate PID