Skip to content

Commit 2a62fa3

Browse files
committed
Fix #329: add support for multiple args to initctl cond set/clr
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
1 parent 4542fdb commit 2a62fa3

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,9 @@ Commands:
535535
disable <CONF> Disable .conf in /etc/finit.d/enabled
536536
reload Reload *.conf in /etc/finit.d (activate changes)
537537
538-
cond set <COND> Set (assert) user-defined condition +usr/COND
538+
cond set <COND> Set (assert) user-defined conditions +usr/COND
539539
cond get <COND> Get status of user-defined condition, see $? and -v
540-
cond clear <COND> Clear (deassert) user-defined condition -usr/COND
540+
cond clear <COND> Clear (deassert) user-defined conditions -usr/COND
541541
cond status Show condition status, default cond command
542542
cond dump [TYPE] Dump all, or a type of, conditions and their status
543543

man/initctl.8

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Reload
127127
.Cm *.conf in
128128
.Pa /etc/finit.d ,
129129
i.e., activates changes.
130-
.It Nm Ar cond set Ar COND
130+
.It Nm Ar cond set Ar COND Op COND ...
131131
Set (assert) user-defined condition,
132132
.Cm +usr/COND
133133
.It Nm Ar cond get Ar COND
@@ -144,7 +144,7 @@ status of the condition, or give
144144
the
145145
.Fl v
146146
option for a more verbose output.
147-
.It Nm Ar cond clr | clear Ar COND
147+
.It Nm Ar cond clr | clear Ar COND Op COND ...
148148
Clear (deassert) user-defined condition,
149149
.Cm -usr/COND
150150
.It Nm Ar cond status

src/initctl.c

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -408,45 +408,56 @@ static int do_cond_dump(char *arg)
408408

409409
typedef enum { COND_CLR, COND_SET, COND_GET } condop_t;
410410

411-
static int do_cond_act(char *arg, condop_t op)
411+
/*
412+
* cond get allows only one argument
413+
* cond set|clr iterate over multiple args
414+
*/
415+
static int do_cond_act(char *args, condop_t op)
412416
{
413417
char path[256];
414-
size_t off;
418+
char *arg;
415419

416-
if (arg && strncmp(arg, COND_USR, strlen(COND_USR)) == 0)
417-
arg += strlen(COND_USR);
418-
419-
if (!arg || !arg[0])
420+
if (!args || !args[0])
420421
ERRX(2, "Invalid condition (empty)");
421422

422-
/* allowed to read any condition, but not set/clr */
423-
if (op != COND_GET) {
423+
arg = strtok(args, " \t");
424+
while (arg) {
425+
size_t off;
426+
427+
if (strncmp(arg, COND_USR, strlen(COND_USR)) == 0)
428+
arg += strlen(COND_USR);
429+
430+
/* allowed to read any condition, but not set/clr */
431+
if (op != COND_GET) {
432+
if (strchr(arg, '/'))
433+
ERRX(2, "Invalid condition (slashes)");
434+
if (strchr(arg, '.'))
435+
ERRX(2, "Invalid condition (periods)");
436+
}
437+
424438
if (strchr(arg, '/'))
425-
ERRX(2, "Invalid condition (slashes)");
426-
if (strchr(arg, '.'))
427-
ERRX(2, "Invalid condition (periods)");
428-
}
439+
snprintf(path, sizeof(path), _PATH_COND "%s", arg);
440+
else
441+
snprintf(path, sizeof(path), _PATH_CONDUSR "%s", arg);
442+
off = strlen(_PATH_COND);
443+
444+
switch (op) {
445+
case COND_GET:
446+
off = !fexist(path);
447+
if (verbose)
448+
puts(off ? "off" : "on");
449+
return off;
450+
case COND_SET:
451+
if (symlink(_PATH_RECONF, path) && errno != EEXIST)
452+
ERR(73, "Failed asserting condition <%s>", &path[off]);
453+
break;
454+
case COND_CLR:
455+
if (erase(path) && errno != ENOENT)
456+
ERR(73, "Failed deasserting condition <%s>", &path[off]);
457+
break;
458+
}
429459

430-
if (strchr(arg, '/'))
431-
snprintf(path, sizeof(path), _PATH_COND "%s", arg);
432-
else
433-
snprintf(path, sizeof(path), _PATH_CONDUSR "%s", arg);
434-
off = strlen(_PATH_COND);
435-
436-
switch (op) {
437-
case COND_GET:
438-
off = !fexist(path);
439-
if (verbose)
440-
puts(off ? "off" : "on");
441-
return off;
442-
case COND_SET:
443-
if (symlink(_PATH_RECONF, path) && errno != EEXIST)
444-
ERR(73, "Failed asserting condition <%s>", &path[off]);
445-
break;
446-
case COND_CLR:
447-
if (erase(path) && errno != ENOENT)
448-
ERR(73, "Failed deasserting condition <%s>", &path[off]);
449-
break;
460+
arg = strtok(NULL, " \t");
450461
}
451462

452463
return 0;
@@ -1263,9 +1274,9 @@ static int usage(int rc)
12631274

12641275
fprintf(stderr,
12651276
"\n"
1266-
" cond set <COND> Set (assert) user-defined condition +usr/COND\n"
1277+
" cond set <COND> Set (assert) user-defined conditions +usr/COND\n"
12671278
" cond get <COND> Get status of user-defined condition, see $? and -v\n"
1268-
" cond clear <COND> Clear (deassert) user-defined condition -usr/COND\n"
1279+
" cond clear <COND> Clear (deassert) user-defined conditions -usr/COND\n"
12691280
" cond status Show condition status, default cond command\n"
12701281
" cond dump [TYPE] Dump all, or a type of, conditions and their status\n"
12711282
"\n"

0 commit comments

Comments
 (0)