From 2a62fa3d85add03f1891d45b9ecc4dfabfe5b7ad Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 7 Jan 2023 11:42:04 +0100 Subject: [PATCH] Fix #329: add support for multiple args to initctl cond set/clr Signed-off-by: Joachim Wiberg --- README.md | 4 +-- man/initctl.8 | 4 +-- src/initctl.c | 77 +++++++++++++++++++++++++++++---------------------- 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 517942c0..0ee9e402 100644 --- a/README.md +++ b/README.md @@ -535,9 +535,9 @@ Commands: disable Disable .conf in /etc/finit.d/enabled reload Reload *.conf in /etc/finit.d (activate changes) - cond set Set (assert) user-defined condition +usr/COND + cond set Set (assert) user-defined conditions +usr/COND cond get Get status of user-defined condition, see $? and -v - cond clear Clear (deassert) user-defined condition -usr/COND + cond clear Clear (deassert) user-defined conditions -usr/COND cond status Show condition status, default cond command cond dump [TYPE] Dump all, or a type of, conditions and their status diff --git a/man/initctl.8 b/man/initctl.8 index c5ef7ea0..e50860a5 100644 --- a/man/initctl.8 +++ b/man/initctl.8 @@ -127,7 +127,7 @@ Reload .Cm *.conf in .Pa /etc/finit.d , i.e., activates changes. -.It Nm Ar cond set Ar COND +.It Nm Ar cond set Ar COND Op COND ... Set (assert) user-defined condition, .Cm +usr/COND .It Nm Ar cond get Ar COND @@ -144,7 +144,7 @@ status of the condition, or give the .Fl v option for a more verbose output. -.It Nm Ar cond clr | clear Ar COND +.It Nm Ar cond clr | clear Ar COND Op COND ... Clear (deassert) user-defined condition, .Cm -usr/COND .It Nm Ar cond status diff --git a/src/initctl.c b/src/initctl.c index 9f0420b0..3b5e7827 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -408,45 +408,56 @@ static int do_cond_dump(char *arg) typedef enum { COND_CLR, COND_SET, COND_GET } condop_t; -static int do_cond_act(char *arg, condop_t op) +/* + * cond get allows only one argument + * cond set|clr iterate over multiple args + */ +static int do_cond_act(char *args, condop_t op) { char path[256]; - size_t off; + char *arg; - if (arg && strncmp(arg, COND_USR, strlen(COND_USR)) == 0) - arg += strlen(COND_USR); - - if (!arg || !arg[0]) + if (!args || !args[0]) ERRX(2, "Invalid condition (empty)"); - /* allowed to read any condition, but not set/clr */ - if (op != COND_GET) { + arg = strtok(args, " \t"); + while (arg) { + size_t off; + + if (strncmp(arg, COND_USR, strlen(COND_USR)) == 0) + arg += strlen(COND_USR); + + /* allowed to read any condition, but not set/clr */ + if (op != COND_GET) { + if (strchr(arg, '/')) + ERRX(2, "Invalid condition (slashes)"); + if (strchr(arg, '.')) + ERRX(2, "Invalid condition (periods)"); + } + if (strchr(arg, '/')) - ERRX(2, "Invalid condition (slashes)"); - if (strchr(arg, '.')) - ERRX(2, "Invalid condition (periods)"); - } + snprintf(path, sizeof(path), _PATH_COND "%s", arg); + else + snprintf(path, sizeof(path), _PATH_CONDUSR "%s", arg); + off = strlen(_PATH_COND); - if (strchr(arg, '/')) - snprintf(path, sizeof(path), _PATH_COND "%s", arg); - else - snprintf(path, sizeof(path), _PATH_CONDUSR "%s", arg); - off = strlen(_PATH_COND); + switch (op) { + case COND_GET: + off = !fexist(path); + if (verbose) + puts(off ? "off" : "on"); + return off; + case COND_SET: + if (symlink(_PATH_RECONF, path) && errno != EEXIST) + ERR(73, "Failed asserting condition <%s>", &path[off]); + break; + case COND_CLR: + if (erase(path) && errno != ENOENT) + ERR(73, "Failed deasserting condition <%s>", &path[off]); + break; + } - switch (op) { - case COND_GET: - off = !fexist(path); - if (verbose) - puts(off ? "off" : "on"); - return off; - case COND_SET: - if (symlink(_PATH_RECONF, path) && errno != EEXIST) - ERR(73, "Failed asserting condition <%s>", &path[off]); - break; - case COND_CLR: - if (erase(path) && errno != ENOENT) - ERR(73, "Failed deasserting condition <%s>", &path[off]); - break; + arg = strtok(NULL, " \t"); } return 0; @@ -1263,9 +1274,9 @@ static int usage(int rc) fprintf(stderr, "\n" - " cond set Set (assert) user-defined condition +usr/COND\n" + " cond set Set (assert) user-defined conditions +usr/COND\n" " cond get Get status of user-defined condition, see $? and -v\n" - " cond clear Clear (deassert) user-defined condition -usr/COND\n" + " cond clear Clear (deassert) user-defined conditions -usr/COND\n" " cond status Show condition status, default cond command\n" " cond dump [TYPE] Dump all, or a type of, conditions and their status\n" "\n"