Extend conditional load support with if:<cond> and if:<!cond>

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-03-05 09:47:51 +01:00
parent b65773d508
commit c4dcf568c8
2 changed files with 30 additions and 4 deletions
+4 -4
View File
@@ -1395,7 +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 *ifdef = NULL, *ifstmt = NULL;
char *notify = NULL;
struct tty tty = { 0 };
char *dev = NULL;
@@ -1509,7 +1509,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
else if (MATCH_CMD(cmd, "conflict:", arg))
conflict = arg;
else if (MATCH_CMD(cmd, "if:", arg))
ifident = arg;
ifstmt = arg;
else if (MATCH_CMD(cmd, "ifdef:", arg))
ifdef = arg;
else
@@ -1525,7 +1525,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
if (!id)
id = "";
if (ifident) {
if (ifstmt) {
char ident[MAX_IDENT_LEN];
if (id[0]) {
@@ -1533,7 +1533,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
} else
strlcpy(ident, name, sizeof(ident));
if (!svc_ifthen(ident, ifident))
if (!svc_ifthen(ident, ifstmt))
return 0;
}
+26
View File
@@ -694,6 +694,32 @@ int svc_ifthen(const char *ident, const char *stmt)
if (!stmt || !stmt[0])
return 1;
if (stmt[0] == '<') {
enum cond_state cond;
char *ptr = ++stmt;
int i = 0;
if (stmt[0] == '!') {
stmt++;
not++;
}
while (ptr[i] != '>' && ptr[i] != 0)
i++;
ptr[i] = 0;
cond = cond_get(stmt);
if (not && cond == COND_ON) {
dbg("skipping %s, cond %s is on", ident, stmt);
return 0;
}
if (!not && cond == COND_OFF) {
dbg("skipping %s, cond %s is off", ident, stmt);
return 0;
}
return 1;
}
if (stmt[0] == '!') {
stmt++;
not++;