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
+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))