mirror of
https://github.com/troglobit/finit.git
synced 2026-11-17 19:30:29 +00:00
service: keep a stop of a forking service waiting for its PID file
Stopping a forking service after its start script had exited, but
before the daemon had written its PID file, did not hold. When the
PID file showed up, the service was reported running again and the
daemon was left alone:
initctl stop ext
service_forked() cleared any block on the service, the user's stop
included.
Keep a user stop, and let the state machine stop the late daemon of
a service no longer enabled. The service is briefly RUNNING on its
way to STOPPING, so service/NAME/running blinks on and off.
Follow-up to #452
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
(cherry picked from commit 35c313c2fa)
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
5 files changed
+47
-9
No files matched your search
@@ -27,6 +27,9 @@ All relevant changes are documented in this 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
|
||||
- Fix `initctl stop` of a forking service before it had written its
|
||||
PID file. The PID file showing up later undid the stop and the
|
||||
service was reported running. Now the late daemon is stopped instead
|
||||
- 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
|
||||
|
||||
+4
-3
@@ -139,9 +139,6 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask)
|
||||
return;
|
||||
}
|
||||
|
||||
/* Service is not crashing, let's tell state machine! */
|
||||
service_forked(svc);
|
||||
|
||||
if (pid != svc->pid) {
|
||||
dbg("Forking service %s (cmd %s) changed PID from %d to %d",
|
||||
svc_ident(svc, NULL, 0), svc->cmd, svc->pid, pid);
|
||||
@@ -151,6 +148,10 @@ static void pidfile_update_conds(char *dir, char *name, uint32_t mask)
|
||||
logit(LOG_CONSOLE | LOG_NOTICE, "Started %s[%d]", svc_ident(svc, NULL, 0), pid);
|
||||
cgroup_move_svc(svc);
|
||||
}
|
||||
|
||||
/* Service is not crashing, let's tell state machine! */
|
||||
if (!service_forked(svc))
|
||||
return;
|
||||
}
|
||||
|
||||
if (svc->notify == SVC_NOTIFY_PID)
|
||||
|
||||
+14
-4
@@ -2918,16 +2918,26 @@ static void svc_set_state(svc_t *svc, svc_state_t new_state)
|
||||
/*
|
||||
* Called by pidfile plugin for forking services that have successfully started.
|
||||
* We get here when the service_monitor() collects the PID, sets svc->pid to 0,
|
||||
* and the state machine prepares for the worst bu setting state to HALTED.
|
||||
* and the state machine prepares for the worst by setting state to HALTED.
|
||||
*
|
||||
* Returns 1 if the service is now running, 0 if it was stopped or disabled
|
||||
* while waiting for its PID file, then the late daemon is stopped.
|
||||
*/
|
||||
void service_forked(svc_t *svc)
|
||||
int service_forked(svc_t *svc)
|
||||
{
|
||||
/* Stop forking_retry() timer */
|
||||
service_timeout_cancel(svc);
|
||||
|
||||
/* Not crashing, restore RUNNING state */
|
||||
svc_unblock(svc);
|
||||
/* Not crashing, restore RUNNING state, but keep a user stop */
|
||||
if (!svc_is_stopped(svc))
|
||||
svc_unblock(svc);
|
||||
svc_set_state(svc, SVC_RUNNING_STATE);
|
||||
|
||||
/* Follow-up to issue #452, RUNNING and not enabled -> service_stop() */
|
||||
if (!svc_enabled(svc))
|
||||
service_step(svc);
|
||||
|
||||
return svc_is_running(svc);
|
||||
}
|
||||
|
||||
/* Set or clear service/foo/ready condition for services and call optional ready:script */
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ void service_ready_script (svc_t *svc); /* XXX: only for pidfile plugin b
|
||||
int service_timeout_after (svc_t *svc, int timeout, void (*cb)(svc_t *svc));
|
||||
int service_timeout_cancel (svc_t *svc);
|
||||
|
||||
void service_forked (svc_t *svc);
|
||||
int service_forked (svc_t *svc);
|
||||
void service_ready (svc_t *svc, int ready);
|
||||
|
||||
int service_stop (svc_t *svc);
|
||||
|
||||
+25
-1
@@ -21,7 +21,7 @@ external()
|
||||
cleanup()
|
||||
{
|
||||
run "kill \$(cat $PIDFN 2>/dev/null) 2>/dev/null || true"
|
||||
run "rm -f $PIDFN"
|
||||
run "rm -f $PIDFN /bin/late.sh /tmp/late"
|
||||
}
|
||||
|
||||
test_teardown()
|
||||
@@ -58,3 +58,27 @@ retry 'assert_status ext waiting'
|
||||
external
|
||||
assert_status ext waiting
|
||||
assert_nocond pid/ext
|
||||
cleanup
|
||||
|
||||
# A forking daemon whose launcher exits before the daemon has written
|
||||
# its PID file. A stop in that window must hold, and the daemon that
|
||||
# shows up late must be stopped, not adopted as running.
|
||||
sep
|
||||
say 'A forking service stopped before its PID file appears stays stopped'
|
||||
# Unquoted END, $PIDFN expands here. /tmp/late marks the daemon start
|
||||
cat > "$SYSROOT/bin/late.sh" <<END
|
||||
#!/bin/sh
|
||||
( sleep 1; touch /tmp/late; exec serv -n -p -P $PIDFN ) &
|
||||
END
|
||||
chmod +x "$SYSROOT/bin/late.sh"
|
||||
run "echo 'service name:ext pid:!$PIDFN late.sh -- Late' > $FINIT_CONF"
|
||||
run "initctl reload"
|
||||
run "initctl stop ext"
|
||||
|
||||
retry 'texec test -e /tmp/late'
|
||||
|
||||
# Negative check, give the pidfile plugin time to (not) react
|
||||
sleep 1
|
||||
assert_status ext stopped
|
||||
assert_nocond pid/ext
|
||||
assert_num_children 0 serv
|
||||
Reference in new issue
Block a user