initctl: use standardized exit codes for all fatal errors

This change diversifies the exit codes used by initctl on fatal errors.
Previously only Std C codes 0 and 1 were used, with the initctl specific
255 for some cases.

Both the LSB init script codes and BSD sysexits.h codes are used.  See
util.c exitcodes[] for the full list.  The idea is to complement the
change in 9a76f08, issue #266, so that initctl can be used silently for
scripting purposes.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2022-05-15 11:32:06 +02:00
parent 4dff8237f1
commit 0e93dfd640
3 changed files with 35 additions and 35 deletions
+4 -4
View File
@@ -243,7 +243,7 @@ static float cgroup_cpuload(struct cg *cg)
snprintf(fn, sizeof(fn), "%s/cpu.stat", cg->cg_path);
fp = fopen(fn, "r");
if (!fp)
ERR(1, "Cannot open %s", fn);
ERR(72, "Cannot open %s", fn);
while (fgets(buf, sizeof(buf), fp)) {
uint64_t curr;
@@ -285,7 +285,7 @@ static struct cg *append(char *path)
cg = calloc(1, sizeof(struct cg));
if (!cg)
ERR(1, "failed allocating struct cg");
ERR(71, "failed allocating struct cg");
cg->cg_path = strdup(path);
if (list)
@@ -295,7 +295,7 @@ static struct cg *append(char *path)
item.key = cg->cg_path;
item.data = cg;
if (!hsearch(item, ENTER))
ERR(1, "failed adding to hash table");
ERR(70, "failed adding to hash table");
return cg;
}
@@ -609,7 +609,7 @@ int show_cgtop(char *arg)
}
if (!hcreate(ttrows + 25))
ERR(1, "failed creating hash table");
ERR(70, "failed creating hash table");
sysinfo(&si);
total_ram = si.totalram * si.mem_unit;
+15 -15
View File
@@ -184,7 +184,7 @@ static int show_log(char *arg)
if (arg) {
svc = client_svc_find(arg);
if (!svc)
return 255;
ERRX(69, "no such task or service(s): %s", arg);
}
return do_log(svc, "");
@@ -249,11 +249,11 @@ static int do_startstop(int cmd, char *arg)
};
if (!arg || !arg[0])
ERRX(1, "missing command argument");
ERRX(2, "missing command argument");
strlcpy(rq.data, arg, sizeof(rq.data));
if (client_send(&rq, sizeof(rq)))
ERRX(1, "No such task or service(s): %s", arg);
ERRX(69, "no such task or service(s): %s", arg);
return do_svc(cmd, arg);
}
@@ -285,7 +285,7 @@ static int do_restart(char *arg)
}
if (retries == 0)
ERRX(1, "failed stopping %s (restart)", arg);
ERRX(7, "failed stopping %s (restart)", arg);
return do_startstop(INIT_CMD_RESTART_SVC, arg);
}
@@ -309,11 +309,11 @@ int do_signal(int argc, char *argv[])
int signo;
if (argc != 2)
ERRX(1, "invalid number of arguments to signal");
ERRX(2, "invalid number of arguments to signal");
strlcpy(rq.data, argv[0], sizeof(rq.data));
if (client_send(&rq, sizeof(rq)))
ERRX(1, "no such task or service(s): %s", argv[0]);
ERRX(69, "no such task or service(s): %s", argv[0]);
signo = str2sig(argv[1]);
if (signo == -1) {
@@ -321,7 +321,7 @@ int do_signal(int argc, char *argv[])
signo = (int)strtonum(argv[1], 1, 31, &errstr);
if (errstr)
ERRX(1, "%s signal: %s", errstr, argv[1]);
ERRX(65, "%s signal: %s", errstr, argv[1]);
}
/* Reuse runlevel for signal number. */
@@ -399,11 +399,11 @@ static int do_cond_act(char *arg, int op)
arg += strlen(COND_USR);
if (!arg || !arg[0])
ERRX(1, "Invalid condition (empty)");
ERRX(2, "Invalid condition (empty)");
if (strchr(arg, '/'))
ERRX(1, "Invalid condition (slashes)");
ERRX(2, "Invalid condition (slashes)");
if (strchr(arg, '.'))
ERRX(1, "Invalid condition (periods)");
ERRX(2, "Invalid condition (periods)");
snprintf(oneshot, sizeof(oneshot), _PATH_CONDUSR "%s", arg);
off = strlen(_PATH_COND);
@@ -416,14 +416,14 @@ static int do_cond_act(char *arg, int op)
return off;
case 1:
if (symlink(_PATH_RECONF, oneshot) && errno != EEXIST)
ERR(1, "Failed asserting condition <%s>", &oneshot[off]);
ERR(73, "Failed asserting condition <%s>", &oneshot[off]);
break;
case 0:
if (erase(oneshot) && errno != ENOENT)
ERR(1, "Failed deasserting condition <%s>", &oneshot[off]);
ERR(73, "Failed deasserting condition <%s>", &oneshot[off]);
break;
default:
ERRX(1, "Unsupported operation (%d)", op);
ERRX(3, "Unsupported operation (%d)", op);
break;
}
@@ -810,7 +810,7 @@ static int show_status(char *arg)
svc = client_svc_find(arg);
if (!svc)
return 255;
ERRX(69, "no such task or service(s): %s", arg);
if (quiet) {
if (svc_is_runtask(svc))
@@ -1108,7 +1108,7 @@ static int cmd_parse(int argc, char *argv[], struct cmd *command)
}
if (argv[0] && strlen(argv[0]) > 0)
ERRX(1, "No such command. See 'initctl help' for an overview of available commands.");
ERRX(3, "No such command. See 'initctl help' for an overview of available commands.");
return command[0].cb(NULL); /* default cmd */
}
+16 -16
View File
@@ -242,15 +242,15 @@ int serv_enable(char *arg)
}
if (chdir(FINIT_RCSD))
ERR(1, "failed cd %s", FINIT_RCSD);
ERR(72, "failed cd %s", FINIT_RCSD);
if (icreate && mkdir("enabled", 0755) && EEXIST != errno)
ERR(1, "failed creating %s/enabled directory", FINIT_RCSD);
ERR(73, "failed creating %s/enabled directory", FINIT_RCSD);
ena = !chdir("enabled"); /* System *may* have enabled/ dir. */
snprintf(path, sizeof(path), "%savailable/%s", ena ? "../" : "", arg);
if (!fexist(path))
ERRX(1, "cannot find %s", conf(path, sizeof(path), arg, 0));
ERRX(72, "cannot find %s", conf(path, sizeof(path), arg, 0));
if (fexist(arg))
ERRX(1, "%s already enabled", arg);
@@ -274,12 +274,12 @@ int do_disable(char *arg, int check)
}
if (chdir(FINIT_RCSD))
ERR(1, "failed cd %s", FINIT_RCSD);
ERR(72, "failed cd %s", FINIT_RCSD);
if (chdir("enabled")) /* System *may* have enabled/ dir. */
dbg("Failed changing to %s/enabled/: %s", FINIT_RCSD, strerror(errno));
if (check && stat(arg, &st))
ERRX(1, "%s not (an) enabled (service).", arg);
ERRX(6, "%s not (an) enabled (service).", arg);
if (check && (st.st_mode & S_IFMT) == S_IFLNK)
ERRX(1, "cannot disable %s, not a symlink.", arg);
@@ -305,9 +305,9 @@ int serv_touch(char *arg)
fn = conf(path, sizeof(path), arg, 0);
if (!fexist(fn)) {
if (!strstr(arg, "finit.conf"))
ERRX(1, "%s not available.", arg);
ERRX(72, "%s not available.", arg);
if (is_builtin(arg))
ERRX(1, "%s is a built-in service.", arg);
ERRX(4, "%s is a built-in service.", arg);
strlcpy(path, FINIT_CONF, sizeof(path));
fn = path;
@@ -315,7 +315,7 @@ int serv_touch(char *arg)
/* libite:touch() follows symlinks */
if (utimensat(AT_FDCWD, fn, NULL, AT_SYMLINK_NOFOLLOW))
ERR(1, "failed marking %s for reload", fn);
ERR(71, "failed marking %s for reload", fn);
return 0;
}
@@ -328,7 +328,7 @@ int serv_show(char *arg)
fn = conf(path, sizeof(path), arg, 0);
if (!fexist(fn)) {
if (is_builtin(arg))
ERRX(1, "%s is a built-in service.", arg);
ERRX(4, "%s is a built-in service.", arg);
WARNX("Cannot find %s", arg);
return 1;
@@ -365,7 +365,7 @@ static int do_edit(char *arg, int creat)
fn = conf(path, sizeof(path), arg, creat);
if (!fexist(fn)) {
if (is_builtin(arg))
ERRX(1, "%s is a built-in service.", arg);
ERRX(4, "%s is a built-in service.", arg);
if (!creat) {
WARNX("Cannot find %s, use -c flag, create command, or select one of:", arg);
@@ -406,10 +406,10 @@ int serv_creat(char *arg)
FILE *fp;
if (!arg || !arg[0])
ERRX(1, "missing argument to create");
ERRX(2, "missing argument to create");
if (is_builtin(arg))
ERRX(1, "%s is a built-in service.", arg);
ERRX(4, "%s is a built-in service.", arg);
/* Input from a pipe or a proper TTY? */
if (isatty(STDIN_FILENO))
@@ -418,7 +418,7 @@ int serv_creat(char *arg)
/* Open fn for writing from pipe */
fn = conf(buf, sizeof(buf), arg, 1);
if (!fn)
ERR(1, "failed creating conf %s", arg);
ERR(73, "failed creating conf %s", arg);
if (!icreate && fexist(fn)) {
WARNX("%s already exists, skipping (use -c to override)", fn);
@@ -427,7 +427,7 @@ int serv_creat(char *arg)
fp = fopen(fn, "w");
if (!fp)
ERR(1, "failed opening %s for writing", fn);
ERR(73, "failed opening %s for writing", fn);
while (fgets(buf, sizeof(buf), stdin))
fputs(buf, fp);
@@ -448,8 +448,8 @@ int serv_delete(char *arg)
fn = conf(buf, sizeof(buf), arg, 0);
if (!fn) {
if (is_builtin(arg))
ERRX(1, "%s is a built-in service.", arg);
ERRX(1, FINIT_RCSD " missing on system.");
ERRX(4, "%s is a built-in service.", arg);
ERRX(72, FINIT_RCSD " missing on system.");
}
if (!fexist(fn))