mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
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:
11 files changed
+274
-16
No files matched your search
+83
-11
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
@@ -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)) {
|
||||
|
||||
@@ -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; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user