Add support for if:[!]ident and ifdef:[!]ident for run/task/services

Conditional loading of stanza depending on ident is already loaded.  The
if: checks as in-band while ifdef: checks out-of-band, i.e., post eval.

The optional leading '!' negates the comparison, if NOT foo then ...

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2023-02-28 00:49:55 +01:00
1 parent ed383eaaa6
commit fa71f97dcf
5 files changed
+72

No files matched your search

+40
View File
@@ -1395,6 +1395,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
char *id = NULL, *env = NULL, *cgroup = NULL;
char *pre_script = NULL, *post_script = NULL;
char *ready_script = NULL, *conflict = NULL;
char *ifdef = NULL, *ifident = NULL;
char *notify = NULL;
struct tty tty = { 0 };
char *dev = NULL;
@@ -1507,6 +1508,10 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
cgroup = arg; /* with group */
else if (MATCH_CMD(cmd, "conflict:", arg))
conflict = arg;
else if (MATCH_CMD(cmd, "if:", arg))
ifident = arg;
else if (MATCH_CMD(cmd, "ifdef:", arg))
ifdef = arg;
else
break;
@@ -1520,6 +1525,18 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
if (!id)
id = "";
if (ifident) {
char ident[MAX_IDENT_LEN];
if (id[0]) {
snprintf(ident, sizeof(ident), "%s:%s", name, id);
} else
strlcpy(ident, name, sizeof(ident));
if (!svc_ifthen(ident, ifident))
return 0;
}
levels = conf_parse_runlevels(runlevels);
if (runlevel > 0 && !ISOTHER(levels, 0)) {
dbg("Skipping %s%s%s, bootstrap is completed.",
@@ -1710,6 +1727,10 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
strlcpy(svc->conflict, conflict, sizeof(svc->conflict));
else
memset(svc->conflict, 0, sizeof(svc->conflict));
if (ifdef)
strlcpy(svc->ifdef, ifdef, sizeof(svc->ifdef));
else
memset(svc->ifdef, 0, sizeof(svc->ifdef));
svc->manual = manual;
svc->respawn = respawn;
svc->forking = forking;
@@ -1897,6 +1918,25 @@ void service_update_rdeps(void)
}
}
/*
* Drop services with 'if:otherserv' if otherserv does not exist.
* Drop services with 'if:!otherserv' if otherserv does exist.
*/
void service_mark_unavail(void)
{
svc_t *svc, *iter = NULL;
for (svc = svc_iterator(&iter, 1); svc; svc = svc_iterator(&iter, 0)) {
char buf[MAX_IDENT_LEN];
if (!svc->ifdef[0])
continue;
if (!svc_ifthen(svc_ident(svc, buf, sizeof(buf)), svc->ifdef))
svc_mark(svc);
}
}
static void service_kill_script(svc_t *svc)
{
if (svc->pid <= 1)