xsession-daemon: More robust proc detection
authorStefan Huber <shuber@sthu.org>
Sat, 30 Dec 2023 08:52:31 +0000 (09:52 +0100)
committerStefan Huber <shuber@sthu.org>
Sat, 30 Dec 2023 08:52:31 +0000 (09:52 +0100)
Test for running process with given PID was instable. Replace with
pid-file feature of pgrep.

xsession-daemon

index f7edb638380256dc39a0c4346b2d3bd315807a4c..247c9036d2d5de89d0642627a21aeb9c98f0f14b 100755 (executable)
@@ -22,33 +22,29 @@ PIDDIR="${PIDFILE%/*}"
 LOGDIR="${LOGFILE%/*}"
 
 mkdir -p $PIDDIR
-if [ $? -ne 0 ];
-then
+if [ $? -ne 0 ]; then
     echo "Session daemon PIDDIR not there: $PIDDIR" >&2
     exit 2
 fi
 
 mkdir -p $LOGDIR
-if [ $? -ne 0 ];
-then
+if [ $? -ne 0 ]; then
     echo "Session daemon LOGDIR not there: $LOGDIR" >&2
     exit 2
 fi
 
-if [ -e "$PIDFILE" ] ; 
-then 
-    echo "Session daemon '$NAME' PIDFILE found: $PIDFILE"
-    PID=`cat "$PIDFILE"` 
+if [ -e "$PIDFILE" ] ; then
+    PID=`cat "$PIDFILE"`
     echo "Session daemon '$NAME' PID found: $PID"
+
+    if pgrep -F "$PIDFILE" "${NAME}" >/dev/null ; then
+        echo "Session daemon '$NAME' was already running with PID $PID" >&2
+        exit 3
+    fi
 else
     echo "Session daemon '$NAME' PIDFILE NOT found: $PIDFILE"
 fi
 
-if [ "$PID" ] && kill -0 "$PID" 2>/dev/null ; 
-then
-    echo "Session daemon '$NAME' was already running with PID $PID" >&2
-    exit 3
-fi
 
 echo "Session daemon running $COMMAND $* > $LOGFILE 2>&1"
 $COMMAND $* > "$LOGFILE" 2>&1 &
@@ -56,8 +52,7 @@ PID=$!
 ERR=$?
 sleep 1
 
-if [ "$PID" ] && [  $ERR == 0 ] && kill -0 "$PID" 2>/dev/null;
-then
+if [ "$PID" ] && [  $ERR == 0 ] && kill -0 "$PID" 2>/dev/null; then
     echo -n $PID > "$PIDFILE"
     echo "Session daemon '$NAME' started with PID $PID"
 else