diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 1c875f98..105a0889 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -23,6 +23,10 @@ All relevant changes are documented in this file. ### Fixes +- Fix #452: a stopped or waiting service with a daemon-owned PID file + was reported as running, with its conditions set, when another + process wrote that PID file, e.g., a container started by hand with + podman. Found by Mattias Walström - Remove stale daemon-owned (`pid:!`) pidfiles after unclean exits. When a daemon dies via SIGKILL/OOM/segfault, or exits early during startup, its pidfile lingers and can prevent the next instance from diff --git a/plugins/pidfile.c b/plugins/pidfile.c index f1232ab6..4f9ac137 100644 --- a/plugins/pidfile.c +++ b/plugins/pidfile.c @@ -109,6 +109,12 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask) } if (mask & (IN_CLOSE_WRITE | IN_ATTRIB | IN_MODIFY | IN_MOVED_TO)) { + /* Only a starting or running service may claim a PID file, issue #452 */ + if (!svc_is_starting(svc) && !svc_is_running(svc)) { + dbg("%s is %s, ignoring %s", nm, svc_status(svc), fn); + return; + } + /* * only mark native services as started, the s6 and * systemd style services rely on their respective diff --git a/test/Makefile.am b/test/Makefile.am index be29f1d1..57dda818 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -42,6 +42,7 @@ EXTRA_DIST += global-envs.sh EXTRA_DIST += initctl-status-subset.sh EXTRA_DIST += notify.sh EXTRA_DIST += pidfile.sh +EXTRA_DIST += pidfile-stopped.sh EXTRA_DIST += stale-pidfile.sh EXTRA_DIST += pre-post-serv.sh EXTRA_DIST += pre-fail.sh @@ -88,6 +89,7 @@ TESTS += global-envs.sh TESTS += initctl-status-subset.sh TESTS += notify.sh TESTS += pidfile.sh +TESTS += pidfile-stopped.sh TESTS += stale-pidfile.sh TESTS += pre-post-serv.sh TESTS += pre-fail.sh diff --git a/test/pidfile-stopped.sh b/test/pidfile-stopped.sh new file mode 100755 index 00000000..d38f1c4f --- /dev/null +++ b/test/pidfile-stopped.sh @@ -0,0 +1,60 @@ +#!/bin/sh +# Verify that a PID file written by someone else does not make a stopped +# or waiting service look started, see issue #452. E.g., a container +# started by hand with podman, outside of Finit. +set -eu + +TEST_DIR=$(dirname "$0") + +PIDFN=/run/ext.pid + +# Stand-in for the process started outside of Finit +external() +{ + say "Starting a process outside of Finit, it writes $PIDFN" + run "sh -c 'sleep 100 & echo \$! > $PIDFN'" + + # Negative check, give the pidfile plugin time to (not) react + sleep 2 +} + +cleanup() +{ + run "kill \$(cat $PIDFN 2>/dev/null) 2>/dev/null || true" + run "rm -f $PIDFN" +} + +test_teardown() +{ + say "Running test teardown." + cleanup + run "rm -f $FINIT_CONF" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +sep +say 'A stopped forking service stays stopped' +run "echo 'service name:ext pid:!$PIDFN serv -p -P $PIDFN -- External' > $FINIT_CONF" +run "initctl reload" +retry 'assert_status ext running' + +run "initctl stop ext" +retry 'assert_status ext stopped' +run "rm -f $PIDFN" + +external +assert_status ext stopped +assert_nocond pid/ext +cleanup + +sep +say 'A service waiting for its condition stays waiting' +run "echo 'service name:ext pid:!$PIDFN serv -p -P $PIDFN -- External' > $FINIT_CONF" +run "initctl reload" +retry 'assert_status ext waiting' + +external +assert_status ext waiting +assert_nocond pid/ext