Skip to content

Commit

Permalink
Use /proc opportunistically only if it exists
Browse files Browse the repository at this point in the history
This improves accuracy for systems with /proc and doesn't affect other
systems.

This leaves in place a race condition if many watchdog processes are
started in very short succession, since the pidfile is not atomically
checked and updated.  For purposes of the watchdog this is probably good
enough.  (See https://stackoverflow.com/a/688365/5419599 for more on
this.)
  • Loading branch information
mikeweilgart committed Feb 22, 2024
1 parent efcefac commit 70eb84a
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cfe_internal/core/watchdog/templates/watchdog.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,25 @@ LOGFILE="/var/cfengine/watchdog.log"
echo "$(date) Initiating watchdog $$" >> ${LOGFILE}

if [ -s $PIDFILE ]; then
# We have a pidfile
if ps -p $(cat $PIDFILE) > /dev/null 2>&1 ; then
# There is a process with the PID in the file, but is it stale?
if [ /proc/"$(cat "$PIDFILE")" = "$(ls -1dtr "$PIDFILE" /proc/"$(cat "$PIDFILE")" | head -n 1)" ]; then
if [ -d /proc ]; then
# We can know for sure if it's stale
actual_process="/proc/$(cat "$PIDFILE")"
newer="$(ls -1dt "$PIDFILE" "$actual_process" | head -n 1)"
if [ "$actual_process" = "$newer" ];
# Pidfile is stale, ignore it
echo $$ > $PIDFILE
else
# Pidfile is definitely correct
echo "$(date) Aborting execution of watchdog $$, existing watchdog process $(cat $PIDFILE) running" >> ${LOGFILE}
exit 1
fi
else
# No /proc, pidfile shows a running process, we'll assume it's valid
echo "$(date) Aborting execution of watchdog $$, existing watchdog process $(cat $PIDFILE) running" >> ${LOGFILE}
exit 1
else
# pidfile is older than the process with that pid, ignore it
echo $$ > $PIDFILE
fi
else
# No current process matching pid in file
Expand Down

0 comments on commit 70eb84a

Please sign in to comment.