service: add stop-scope, which processes a stop applies to

When the main process of a service exits, Finit sends SIGKILL to its
process group.  That killed the action script of avahi-autoipd, which
it runs on SIGTERM to deconfigure the interface, before it was done.

Add stop-scope, in the block format and as the stop-scope: token:

    service avahi-autoipd {
        stop-scope = "process"
        command    = "avahi-autoipd eth0"
    }

  - pgroup: the stop signal to the main process, SIGKILL to its
    process group when it exits, as before and the default
  - process: only the main process
  - cgroup: the stop signal to all of the cgroup, SIGKILL to the rest
  - mixed: the stop signal to the main process, SIGKILL to the rest of
    the cgroup, like systemd's KillMode=mixed

SIGKILL to a cgroup uses cgroup.kill when the kernel has it.  Without
a cgroup, cgroup and mixed fall back to the process group.  Services
in the same .conf file share a cgroup, Finit warns when one of them
uses a cgroup scope.

Fixes #413

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-17 11:00:48 +01:00
1 parent 35c313c2fa
commit d51f521bf8
11 files changed
+274 -16

No files matched your search

+3
View File
@@ -8,6 +8,9 @@ All relevant changes are documented in this file.
### Changes
- Add `stop-scope`, which processes a stop applies to: `pgroup`, the process
group of the main process (default), `process`, only the main process,
`cgroup`, the cgroup of the service, or `mixed` like systemd. Issue #413
- keventd is now a full device manager, promoted from the power supply
monitor it was in v4, and the device management defaults have swapped:
keventd is enabled by default (`--without-keventd` opts out, libblkid is
+1
View File
@@ -69,6 +69,7 @@ Service options
| `oncrash:reboot` | `oncrash = "reboot"` |
| `halt:SIG` | `stop-signal = "SIG"` |
| `kill:SEC` | `stop-timeout = SEC` |
| `stop-scope:SCOPE` | `stop-scope = "SCOPE"` |
| `pre:[TMO,]/script` | `exec-start-pre`, `exec-start-pre-timeout` |
| `ready:[TMO,]/script` | `exec-start-ready`, and its `-timeout` |
| `stop:[TMO,]/script` | `exec-stop`, and its `-timeout` |
+21
View File
@@ -259,6 +259,27 @@ down gracefully. If it has not been collected within 3 seconds,
* `stop-signal = "SIGPWR"` -- send this instead of `SIGTERM`
* `stop-timeout = 10` -- seconds to wait before `SIGKILL`, 1-300
* `stop-scope = "process"` -- which processes are stopped, see below
The stop signal goes to the main process of the service. When that
exits, Finit sends `SIGKILL` to any processes it left behind in its
process group. This is the default, `stop-scope = "pgroup"`. Other
scopes:
* `process` -- only the main process, anything it leaves behind is
left alone, e.g. a script it started on its way down
* `cgroup` -- the stop signal goes to all processes in the cgroup of
the service, and `SIGKILL` to those still there when the main
process exits
* `mixed` -- the stop signal goes to the main process, and `SIGKILL`
to the rest of the cgroup when it exits
All services declared in the same .conf file share a cgroup, unless
given a [name of their own](cgroups.md#overriding-cgroup-leaf-names),
so `cgroup` and `mixed` reach them all, Finit warns about this. Without
cgroups, these two fall back to the process group. A forking daemon
usually leaves the process group of its start command, so only `cgroup`
and `mixed` reach what it leaves behind.
Some services need more than a signal:
+12
View File
@@ -470,6 +470,18 @@ modifier
e.g.,
.Cm kill:10
to wait 10 seconds before sending SIGKILL.
When the process exits, Finit sends SIGKILL to any processes left in
its process group. The command modifier
.Cm stop-scope:SCOPE
changes this:
.Cm process
leaves them alone,
.Cm cgroup
sends the halt signal to all processes in the cgroup of the service and
SIGKILL to the rest when the process exits, and
.Cm mixed
sends only SIGKILL to the rest of the cgroup. The default is
.Cm pgroup .
.Pp
Services, including the
.Cm sysv
+83 -11
View File
@@ -222,6 +222,86 @@ int cgroup_move_svc(svc_t *svc)
svc->pid, svc->cgroup.delegate);
}
/*
* The cgroup @svc runs in, as set up by cgroup_prepare(). NULL for
* the root cgroup, which is not the service's own.
*/
static char *cgroup_svc_path(svc_t *svc, const char *name, char *buf, size_t len)
{
const char *group;
if (svc_is_tty(svc))
group = "user";
else if (!strcmp(svc->cgroup.name, "root"))
return NULL;
else
group = svc->cgroup.name[0] ? svc->cgroup.name : "system";
snprintf(buf, len, FINIT_CGPATH "/%s/%s", group, name);
return buf;
}
static void cgroup_signal_path(const char *path, int sig)
{
char fn[PATH_MAX];
struct dirent *d;
DIR *dir;
FILE *fp;
int pid;
fp = fopenf("r", "%s/cgroup.procs", path);
if (fp) {
/* A PID outside our PID namespace reads as 0, our own group */
while (fscanf(fp, "%d", &pid) == 1) {
if (pid > 1)
kill(pid, sig);
}
fclose(fp);
}
/* Delegated cgroups have their processes in sub-cgroups */
dir = opendir(path);
if (!dir)
return;
while ((d = readdir(dir))) {
if (d->d_type != DT_DIR || !strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
snprintf(fn, sizeof(fn), "%s/%s", path, d->d_name);
cgroup_signal_path(fn, sig);
}
closedir(dir);
}
/* True if @svc runs in a cgroup of its own, not the root cgroup */
int cgroup_svc_has(svc_t *svc)
{
return avail && svc && strcmp(svc->cgroup.name, "root");
}
/*
* Send @sig to every process in the cgroup of @svc. Returns -1 if
* it has no cgroup of its own, e.g. cgroups are not available.
*/
int cgroup_signal_svc(svc_t *svc, int sig)
{
char name[MAX_ARG_LEN];
char path[PATH_MAX];
if (!cgroup_svc_has(svc))
return -1;
if (!cgroup_svc_path(svc, cgroup_svc_name(svc, name, sizeof(name)), path, sizeof(path)) ||
!fisdir(path))
return -1;
/* Atomic and recursive, Linux 5.14 and later */
if (sig == SIGKILL && !fnwrite("1", "%s/cgroup.kill", path))
return 0;
cgroup_signal_path(path, sig);
return 0;
}
static void cgset(const char *path, char *ctrl, char *prop)
{
char *val;
@@ -798,23 +878,15 @@ int cgroup_del(char *dir)
*/
int cgroup_del_svc(svc_t *svc, const char *name)
{
const char *group;
char path[256];
char path[PATH_MAX];
if (!avail)
return 0;
/* Determine group from service */
if (svc_is_tty(svc)) {
group = "user";
} else if (svc->cgroup.name[0] && !strcmp(svc->cgroup.name, "root")) {
/* Root cgroup - nothing to delete */
/* Root cgroup - nothing to delete */
if (!cgroup_svc_path(svc, name, path, sizeof(path)))
return 0;
} else {
group = svc->cgroup.name[0] ? svc->cgroup.name : "system";
}
snprintf(path, sizeof(path), "/sys/fs/cgroup/%s/%s", group, name);
return cgroup_del(path);
}
+2
View File
@@ -58,6 +58,8 @@ int cgroup_watch (const char *group, const char *name);
int cgroup_move_pid(const char *group, const char *name, int pid, int delegate);
int cgroup_move_svc(svc_t *svc);
int cgroup_svc_has (svc_t *svc);
int cgroup_signal_svc(svc_t *svc, int sig);
void cgroup_prune (void);
+3
View File
@@ -268,6 +268,7 @@ static cfg_opt_t svc_opts[] = {
CFG_STR ("halt", NULL, CFGF_NODEFAULT), /* alias */
CFG_INT ("stop-timeout", 0, CFGF_NODEFAULT),
CFG_INT ("kill", 0, CFGF_NODEFAULT), /* alias */
CFG_STR ("stop-scope", NULL, CFGF_NODEFAULT),
/* lifecycle scripts, each with its own bound */
EXEC_OPTS("exec-start-pre"),
@@ -1674,6 +1675,8 @@ static void svc_translate(cfg_t *sec, int type, struct rlimit rlimit[], char *fi
addtok(line, sizeof(line), "halt:%s", str);
if (sec_getint(sec, "stop-timeout", "kill", &num))
addtok(line, sizeof(line), "kill:%ld", num);
if ((str = sec_getstr(sec, "stop-scope", NULL)))
addtok(line, sizeof(line), "stop-scope:%s", str);
addscript(line, sizeof(line), sec, "exec-start-pre", "pre:");
addscript(line, sizeof(line), sec, "exec-start-ready", "ready:");
+75 -5
View File
@@ -1252,6 +1252,15 @@ fail:
return result;
}
/* The cgroup of the service, or the process group of its main PID */
static int service_signal_all(svc_t *svc, int sig)
{
if (svc_is_cgroup_scope(svc) && !cgroup_signal_svc(svc, sig))
return 0;
return kill(-svc->pid, sig);
}
/**
* service_kill - Forcefully terminate a service
* @param svc Service to kill
@@ -1495,8 +1504,11 @@ int service_stop(svc_t *svc)
* entire group. This gives the process time to properly stop
* and/or forward TERM to its children. If it does not respond
* in within a reasonable timeout we SIGKILL the entire group.
* With stop-scope cgroup, all of the cgroup gets SIGTERM,
* if the service has one.
*/
rc = kill(svc->pid, svc->sighalt);
if (svc->stop_scope != SVC_SCOPE_CGROUP || cgroup_signal_svc(svc, svc->sighalt))
rc = kill(svc->pid, svc->sighalt);
dbg("kill(%d, %d) => rc %d, errno %d", svc->pid, svc->sighalt, rc, errno);
/* PID lost or forking process never really started */
if (rc == -1 && (errno == ESRCH || errno == ENOENT))
@@ -2001,6 +2013,58 @@ static void parse_sighalt(svc_t *svc, char *arg)
svc->sighalt = signo;
}
static const char *scopes[] = {
[SVC_SCOPE_PGROUP] = "pgroup",
[SVC_SCOPE_PROCESS] = "process",
[SVC_SCOPE_CGROUP] = "cgroup",
[SVC_SCOPE_MIXED] = "mixed",
};
static svc_scope_t parse_scope(svc_t *svc, const char *arg)
{
size_t i;
if (!arg)
return SVC_SCOPE_PGROUP;
for (i = 0; i < NELEMS(scopes); i++) {
if (!strcmp(arg, scopes[i]))
return (svc_scope_t)i;
}
warnx("%s: unknown stop-scope '%s', using pgroup", svc_ident(svc, NULL, 0), arg);
return SVC_SCOPE_PGROUP;
}
/*
* A cgroup scope also reaches other services sharing the cgroup.
* Only checked for a new or changed service, not on every reload.
*/
static void scope_check(svc_t *svc)
{
char a[MAX_ARG_LEN], b[MAX_ARG_LEN];
svc_t *s, *iter = NULL;
if (!svc_is_changed(svc) || !cgroup_svc_has(svc))
return;
cgroup_svc_name(svc, a, sizeof(a));
for (s = svc_iterator(&iter, 1); s; s = svc_iterator(&iter, 0)) {
svc_t *scoped = svc_is_cgroup_scope(svc) ? svc : s;
if (s == svc || !svc_is_cgroup_scope(scoped))
continue;
if (svc_is_tty(s) != svc_is_tty(svc) || strcmp(s->cgroup.name, svc->cgroup.name) ||
strcmp(cgroup_svc_name(s, b, sizeof(b)), a))
continue;
logit(LOG_WARNING, "%s: stop-scope %s also reaches %s, give it a cgroup name of its own",
svc_ident(scoped, a, sizeof(a)), scopes[scoped->stop_scope],
svc_ident(scoped == svc ? s : svc, b, sizeof(b)));
return;
}
}
static void parse_killdelay(svc_t *svc, char *delay)
{
const char *errstr;
@@ -2250,7 +2314,7 @@ svc_t *service_register_desc(int type, char *cfg, const char *desc,
{
char *cmd, *runlevels = NULL, *cond = NULL;
char *username = NULL, *log = NULL, *pid = NULL;
char *name = NULL, *halt = NULL, *delay = NULL;
char *name = NULL, *halt = NULL, *delay = NULL, *scope = NULL;
char *id = NULL, *env = NULL, *cgroup = NULL;
char *pre_script = NULL, *post_script = NULL;
char *ready_script = NULL, *conflict = NULL;
@@ -2351,6 +2415,8 @@ svc_t *service_register_desc(int type, char *cfg, const char *desc,
halt = arg;
else if (MATCH_CMD(cmd, "kill:", arg))
delay = arg;
else if (MATCH_CMD(cmd, "stop-scope:", arg))
scope = arg;
else if (MATCH_CMD(cmd, "pre:", arg))
pre_script = arg;
else if (MATCH_CMD(cmd, "post:", arg))
@@ -2571,6 +2637,7 @@ svc_t *service_register_desc(int type, char *cfg, const char *desc,
parse_killdelay(svc, delay);
else
svc->killdelay = SVC_TERM_TIMEOUT;
svc->stop_scope = parse_scope(svc, scope);
if (pre_script)
parse_script(svc, "pre", pre_script, &svc->pre_tmo, svc->pre_script, sizeof(svc->pre_script));
else
@@ -2736,6 +2803,7 @@ svc_t *service_register_desc(int type, char *cfg, const char *desc,
svc_mark_dirty(svc);
svc_enable(svc);
scope_check(svc);
/* for finit native services only, e.g. plugins/hotplug.c */
if (!file)
@@ -2878,9 +2946,11 @@ void service_monitor(pid_t lost, int status)
logit(LOG_CONSOLE | LOG_NOTICE, "Stopped %s[%d]", svc_ident(svc, NULL, 0), lost);
}
/* Terminate any children in the same proess group, e.g. logit */
dbg("Killing lingering children in same process group ...");
kill(-svc->pid, SIGKILL);
/* Terminate lingering children, e.g. logit, unless stop-scope says not to */
if (svc->stop_scope != SVC_SCOPE_PROCESS) {
dbg("Killing lingering children ...");
service_signal_all(svc, SIGKILL);
}
/* Try removing PID file (in case service does not clean up after itself) */
if (svc_is_daemon(svc) || svc_is_tty(svc)) {
+10
View File
@@ -112,6 +112,14 @@ typedef enum {
/* Prevent endless respawn of faulty services. */
#define SVC_RESPAWN_MAX 10
/* stop-scope, issue #413 */
typedef enum {
SVC_SCOPE_PGROUP = 0, /* Signal main PID, SIGKILL its process group */
SVC_SCOPE_PROCESS, /* Main PID only */
SVC_SCOPE_CGROUP, /* Signal all in cgroup, SIGKILL the rest */
SVC_SCOPE_MIXED, /* Signal main PID, SIGKILL rest of cgroup */
} svc_scope_t;
/*
* Default enable for all services, can be stopped by means
* of issuing an initctl call. E.g.
@@ -131,6 +139,7 @@ typedef struct svc {
/* Service details */
int sighalt; /* Signal to stop process, default: SIGTERM */
int killdelay; /* Delay in msec before sending SIGKILL */
svc_scope_t stop_scope; /* Which processes stop applies to */
pid_t oldpid, pid;
char pidfile[MAX_CMD_LEN];
long start_time; /* Start time, as seconds since boot, from sysinfo() */
@@ -324,6 +333,7 @@ static inline void svc_starting (svc_t *svc) { if (svc) svc->starting = 1;
static inline void svc_started (svc_t *svc) { if (svc) svc->starting = 0; }
static inline int svc_is_starting (svc_t *svc) { return svc && 0 != svc->starting; }
static inline int svc_is_running (svc_t *svc) { return svc && svc->state == SVC_RUNNING_STATE; }
static inline int svc_is_cgroup_scope(svc_t *svc) { return svc->stop_scope == SVC_SCOPE_CGROUP || svc->stop_scope == SVC_SCOPE_MIXED; }
static inline int svc_is_stopping (svc_t *svc) { return svc && svc->state == SVC_STOPPING_STATE; }
static inline int svc_is_stopped (svc_t *svc) { return svc && svc->block == SVC_BLOCK_USER; }
+2
View File
@@ -57,6 +57,7 @@ EXTRA_DIST += pam-session.sh
EXTRA_DIST += pidfile.sh
EXTRA_DIST += pidfile-stopped.sh
EXTRA_DIST += stale-pidfile.sh
EXTRA_DIST += stop-scope.sh
EXTRA_DIST += pre-post-serv.sh
EXTRA_DIST += pre-fail.sh
EXTRA_DIST += process-depends.sh
@@ -129,6 +130,7 @@ TESTS += pam-session.sh
TESTS += pidfile.sh
TESTS += pidfile-stopped.sh
TESTS += stale-pidfile.sh
TESTS += stop-scope.sh
TESTS += pre-post-serv.sh
TESTS += pre-fail.sh
TESTS += process-depends.sh
+62
View File
@@ -0,0 +1,62 @@
#!/bin/sh
# Verify stop-scope, which processes a stop applies to, see issue #413.
# The service leaves a child behind in its process group, like the
# action script of avahi-autoipd. Cgroups are not available in the
# test namespace, so cgroup and mixed fall back to the process group.
set -eu
TEST_DIR=$(dirname "$0")
CHILD=/tmp/scope-child
# Start the service, stop it, and check if its child is still there.
# Optional second argument is the stop-scope.
stop_one()
{
sep
say "stop-scope ${2:-default}, child is left $1"
run "rm -f $CHILD"
run "cat > $FINIT_CONF" <<EOF
service scope {
${2:+stop-scope = "$2"}
command = "scope.sh"
}
EOF
run "initctl reload"
run "initctl start scope"
retry 'assert_status scope running'
retry "texec test -s $CHILD"
run "initctl stop scope"
retry 'assert_status scope stopped'
if [ "$1" = alive ]; then
assert "Child is alive" "$(texec sh -c "kill -0 \$(cat $CHILD) && echo yes")" = yes
run "kill \$(cat $CHILD)"
else
retry "texec sh -c '! kill -0 \$(cat $CHILD) 2>/dev/null'"
fi
}
test_teardown()
{
say "Running test teardown."
run "kill \$(cat $CHILD 2>/dev/null) 2>/dev/null || true"
run "rm -f $FINIT_CONF $CHILD /bin/scope.sh"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
cat > "$SYSROOT/bin/scope.sh" <<EOF
#!/bin/sh
sleep 100 &
echo \$! > $CHILD
exec serv -np -i scope
EOF
chmod +x "$SYSROOT/bin/scope.sh"
stop_one dead
stop_one dead pgroup
stop_one alive process
stop_one dead cgroup
stop_one dead mixed