initctl: Follow priciple of least surprise for "cond get"

The accepted standard in Unix is to report successful executions with
exitcode 0. Therefore, map a "initctl cond get" of a condition to the
following exitcodes:

- On: 0
- Off: 1
- Flux: 255

Fixes: c3c662fe64 ("initctl: ensure 'cond get' support flux state")
Signed-off-by: Tobias Waldekranz <tobias@waldekranz.com>
This commit is contained in:
Tobias Waldekranz
2023-02-06 16:01:18 +01:00
parent 4f8a5a6531
commit c23a867485
2 changed files with 20 additions and 16 deletions
+1 -1
View File
@@ -145,7 +145,7 @@ or
then any condition can be read.
.Pp
The command is geared for scripting, check the exit code to get the
status of the condition: 0 - off, 1 - on, 255 - flux. For a more
status of the condition: 0 - on, 1 - off, 255 - flux. For a more
verbose output, use the
.Fl v
option.
+19 -15
View File
@@ -409,24 +409,24 @@ static int do_cond_dump(char *arg)
typedef enum { COND_CLR, COND_SET, COND_GET } condop_t;
static int cond_read(char *path)
static cond_state_t cond_read(char *path)
{
int now, gen;
if (fngetint(path, &gen) == -1)
return 0;
return COND_OFF;
/*
* if we cannot read the reconf generation, then either sth is
* very wrong, or we are called very early/late boot/shutdown.
*/
if (fngetint(_PATH_RECONF, &now) == -1)
return 255; /* classify as flux */
return COND_FLUX; /* classify as flux */
if (now != gen)
return 255;
return COND_FLUX;
return 1;
return COND_ON;
}
/*
@@ -435,9 +435,9 @@ static int cond_read(char *path)
*/
static int do_cond_act(char *args, condop_t op)
{
cond_state_t cstate;
char path[256];
char *arg;
int rc;
if (!args || !args[0])
ERRX(2, "Invalid condition (empty)");
@@ -465,16 +465,20 @@ static int do_cond_act(char *args, condop_t op)
switch (op) {
case COND_GET:
rc = cond_read(path);
if (verbose) {
if (rc == 255)
puts("flux");
else if (rc == 0)
puts("off");
else
puts("on");
cstate = cond_read(path);
if (verbose)
puts(condstr(cstate));
switch (cstate) {
case COND_ON:
return 0;
case COND_OFF:
return 1;
case COND_FLUX:
default:
break;
}
return rc;
return 255;
case COND_SET:
if (symlink(_PATH_RECONF, path) && errno != EEXIST)