pidfile: don't let a stopped service claim a PID file

A stopped service with a daemon-owned PID file came back as running
when another process wrote that file, e.g., a container started by
hand:

    sudo podman start dash

turned container:dash from stopped to running in initctl, with the
podman PID adopted, moved to the service's cgroup, and its conditions
set.  The same happened to a service waiting for its conditions.

The plugin acted on any new or changed PID file, only skipping
services stopping or running scripts, and service_forked() then
cleared the stop.

Only let a service that is starting or running claim its PID file.

Fixes #452

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-17 06:44:45 +01:00
1 parent a162551896
commit a1706e8ebd
4 files changed
+72

No files matched your search

+2
View File
@@ -55,6 +55,7 @@ EXTRA_DIST += keventd.sh
EXTRA_DIST += notify.sh
EXTRA_DIST += pam-session.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
@@ -126,6 +127,7 @@ endif
TESTS += notify.sh
TESTS += pam-session.sh
TESTS += pidfile.sh
TESTS += pidfile-stopped.sh
TESTS += stale-pidfile.sh
TESTS += pre-post-serv.sh
TESTS += pre-fail.sh
+60
View File
@@ -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 <usr/never> serv -p -P $PIDFN -- External' > $FINIT_CONF"
run "initctl reload"
retry 'assert_status ext waiting'
external
assert_status ext waiting
assert_nocond pid/ext