diff --git a/src/cgutil.c b/src/cgutil.c index ccf68bb9..67a42d2e 100644 --- a/src/cgutil.c +++ b/src/cgutil.c @@ -23,7 +23,6 @@ #include "config.h" -#include #include #include #include @@ -244,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(1, "Cannot open %s", fn); while (fgets(buf, sizeof(buf), fp)) { uint64_t curr; @@ -280,13 +279,13 @@ static struct cg *append(char *path) if (access(fn, F_OK)) { /* older kernels, 4.19, don't have summary cpu.stat in root */ if (strcmp(path, FINIT_CGPATH)) - warn("not a cgroup path with cpu controller, %s", path); + WARN("not a cgroup path with cpu controller, %s", path); return NULL; } cg = calloc(1, sizeof(struct cg)); if (!cg) - err(1, "failed allocating struct cg"); + ERR(1, "failed allocating struct cg"); cg->cg_path = strdup(path); if (list) @@ -296,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(1, "failed adding to hash table"); return cg; } @@ -610,7 +609,7 @@ int show_cgtop(char *arg) } if (!hcreate(ttrows + 25)) - err(1, "failed creating hash table"); + ERR(1, "failed creating hash table"); sysinfo(&si); total_ram = si.totalram * si.mem_unit; diff --git a/src/client.c b/src/client.c index f8388132..a89ba8da 100644 --- a/src/client.c +++ b/src/client.c @@ -21,7 +21,6 @@ * THE SOFTWARE. */ -#include #include #include #include @@ -41,13 +40,13 @@ int client_connect(void) sd = socket(AF_UNIX, SOCK_SEQPACKET, 0); if (-1 == sd) { - warn("Failed creating UNIX domain socket"); + WARN("Failed creating UNIX domain socket"); return -1; } if (connect(sd, (struct sockaddr*)&sun, sizeof(sun)) == -1) { if (errno != ENOENT) - warnx("Failed connecting to finit"); + WARNX("Failed connecting to finit"); close(sd); return -1; } @@ -82,12 +81,12 @@ int client_send(struct init_request *rq, ssize_t len) pfd.fd = sd; pfd.events = POLLOUT; if (poll(&pfd, 1, 2000) <= 0) { - warn("Timed out waiting for Finit, errno %d", errno); + WARN("Timed out waiting for Finit, errno %d", errno); goto exit; } if (write(sd, rq, len) != len) { - warn("Failed communicating with Finit, errno %d", errno); + WARN("Failed communicating with Finit, errno %d", errno); goto exit; } @@ -97,14 +96,14 @@ int client_send(struct init_request *rq, ssize_t len) if (rc) { if (errno == EINTR) /* shutdown/reboot */ goto exit; - warn("poll(), errno %d", errno); + WARN("poll(), errno %d", errno); } else - warnx("Timed out waiting for reply from Finit."); + WARNX("Timed out waiting for reply from Finit."); goto exit; } if (read(sd, rq, len) != len) { - warn("Failed reading reply from Finit, errno %d", errno); + WARN("Failed reading reply from Finit, errno %d", errno); goto exit; } @@ -145,7 +144,7 @@ svc_t *client_svc_iterator(int first) return &svc; error: - perror("Failed communicating with finit"); + WARN("Failed communicating with finit, error %d", errno); client_disconnect(); sd = -1; @@ -176,7 +175,7 @@ svc_t *do_cmd(int cmd, const char *arg) return &svc; error: client_disconnect(); - perror("Failed communicating with finit"); + WARN("Failed communicating with finit, error %d", errno); return NULL; } diff --git a/src/initctl.c b/src/initctl.c index 3d516467..a35187bd 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -23,7 +23,6 @@ #include "config.h" -#include #include #include #include @@ -39,6 +38,7 @@ # include #endif +#include "initctl.h" #include "client.h" #include "cond.h" #include "serv.h" @@ -245,11 +245,11 @@ static int do_startstop(int cmd, char *arg) }; if (!arg || !arg[0]) - errx(1, "missing command argument"); + ERRX(1, "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(1, "No such task or service(s): %s", arg); return do_svc(cmd, arg); } @@ -281,7 +281,7 @@ static int do_restart(char *arg) } if (retries == 0) - errx(1, "failed stopping %s (restart)", arg); + ERRX(1, "failed stopping %s (restart)", arg); return do_startstop(INIT_CMD_RESTART_SVC, arg); } @@ -305,11 +305,11 @@ int do_signal(int argc, char *argv[]) int signo; if (argc != 2) - errx(1, "invalid number of arguments to signal"); + ERRX(1, "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(1, "no such task or service(s): %s", argv[0]); signo = str2sig(argv[1]); if (signo == -1) { @@ -317,7 +317,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(1, "%s signal: %s", errstr, argv[1]); } /* Reuse runlevel for signal number. */ @@ -379,7 +379,7 @@ static int do_cond_dump(char *arg) } if (nftw(_PATH_COND, dump_one_cond, 20, 0) == -1) { - warnx("Failed parsing %s", _PATH_COND); + WARNX("Failed parsing %s", _PATH_COND); return 1; } @@ -395,11 +395,11 @@ static int do_cond_act(char *arg, int op) arg += strlen(COND_USR); if (!arg || !arg[0]) - errx(1, "Invalid condition (empty)"); + ERRX(1, "Invalid condition (empty)"); if (strchr(arg, '/')) - errx(1, "Invalid condition (slashes)"); + ERRX(1, "Invalid condition (slashes)"); if (strchr(arg, '.')) - errx(1, "Invalid condition (periods)"); + ERRX(1, "Invalid condition (periods)"); snprintf(oneshot, sizeof(oneshot), _PATH_CONDUSR "%s", arg); off = strlen(_PATH_COND); @@ -412,14 +412,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(1, "Failed asserting condition <%s>", &oneshot[off]); break; case 0: if (erase(oneshot) && errno != ENOENT) - err(1, "Failed deasserting condition <%s>", &oneshot[off]); + ERR(1, "Failed deasserting condition <%s>", &oneshot[off]); break; default: - errx(1, "Unsupported operation (%d)", op); + ERRX(1, "Unsupported operation (%d)", op); break; } @@ -1104,7 +1104,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(1, "No such command. See 'initctl help' for an overview of available commands."); return command[0].cb(NULL); /* default cmd */ } @@ -1241,6 +1241,9 @@ void logit(int prio, const char *fmt, ...) { va_list ap; + if (quiet) + return; + va_start(ap, fmt); if (prio <= LOG_ERR) verrx(1, fmt, ap); diff --git a/src/initctl.h b/src/initctl.h index baa3d833..3d7a1ee9 100644 --- a/src/initctl.h +++ b/src/initctl.h @@ -24,6 +24,8 @@ #ifndef FINIT_INITCTL_H_ #define FINIT_INITCTL_H_ +#include + #include "finit.h" #include "util.h" @@ -33,6 +35,12 @@ extern int ionce; /* initctl -1 */ extern int heading; /* initctl -t */ extern int verbose; /* initctl -v */ extern int plain; /* initctl -p */ +extern int quiet; /* initctl -q */ + +#define ERR(rc, fmt, args...) do { if (!quiet) err(rc, fmt, ##args); else exit(rc); } while (0) +#define ERRX(rc, fmt, args...) do { if (!quiet) errx(rc, fmt, ##args); else exit(rc); } while (0) +#define WARN(fmt, args...) do { if (!quiet) warn(fmt, ##args); } while (0) +#define WARNX(fmt, args...) do { if (!quiet) warnx(fmt, ##args); } while (0) extern void print_header(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); diff --git a/src/serv.c b/src/serv.c index dd06896a..6a25960f 100644 --- a/src/serv.c +++ b/src/serv.c @@ -23,7 +23,6 @@ #include "config.h" -#include #include /* Definition of AT_* constants */ #include #include @@ -233,7 +232,7 @@ int serv_enable(char *arg) int ena; if (!arg || !arg[0]) { - warnx("missing argument to enable, may be one of:"); + WARNX("missing argument to enable, may be one of:"); return serv_list("available"); } @@ -243,18 +242,18 @@ int serv_enable(char *arg) } if (chdir(FINIT_RCSD)) - err(1, "failed cd %s", FINIT_RCSD); + ERR(1, "failed cd %s", FINIT_RCSD); if (icreate && mkdir("enabled", 0755) && EEXIST != errno) - err(1, "failed creating %s/enabled directory", FINIT_RCSD); + ERR(1, "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(1, "cannot find %s", conf(path, sizeof(path), arg, 0)); if (fexist(arg)) - errx(1, "%s already enabled", arg); + ERRX(1, "%s already enabled", arg); return symlink(path, arg) != 0; } @@ -265,7 +264,7 @@ int do_disable(char *arg, int check) char corr[40]; if (!arg || !arg[0]) { - warnx("missing argument to disable, may be one of:"); + WARNX("missing argument to disable, may be one of:"); return serv_list("enabled"); } @@ -275,15 +274,15 @@ int do_disable(char *arg, int check) } if (chdir(FINIT_RCSD)) - err(1, "failed cd %s", FINIT_RCSD); + ERR(1, "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(1, "%s not (an) enabled (service).", arg); if (check && (st.st_mode & S_IFMT) == S_IFLNK) - errx(1, "cannot disable %s, not a symlink.", arg); + ERRX(1, "cannot disable %s, not a symlink.", arg); return remove(arg) != 0; } @@ -299,16 +298,16 @@ int serv_touch(char *arg) char *fn; if (!arg || !arg[0]) { - warnx("missing argument to touch, may be one of:"); + WARNX("missing argument to touch, may be one of:"); return serv_list("enabled"); } fn = conf(path, sizeof(path), arg, 0); if (!fexist(fn)) { if (!strstr(arg, "finit.conf")) - errx(1, "%s not available.", arg); + ERRX(1, "%s not available.", arg); if (is_builtin(arg)) - errx(1, "%s is a built-in service.", arg); + ERRX(1, "%s is a built-in service.", arg); strlcpy(path, FINIT_CONF, sizeof(path)); fn = path; @@ -316,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(1, "failed marking %s for reload", fn); return 0; } @@ -329,9 +328,9 @@ 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(1, "%s is a built-in service.", arg); - warnx("Cannot find %s", arg); + WARNX("Cannot find %s", arg); return 1; } @@ -366,10 +365,10 @@ 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(1, "%s is a built-in service.", arg); if (!creat) { - warnx("Cannot find %s, use -c flag, create command, or select one of:", arg); + WARNX("Cannot find %s, use -c flag, create command, or select one of:", arg); return serv_list(NULL); } @@ -378,7 +377,7 @@ static int do_edit(char *arg, int creat) copyfile(SAMPLE_CONF, fn, 0, 0); #endif } else if (creat) - warnx("the file %s already exists, falling back to edit.", fn); + WARNX("the file %s already exists, falling back to edit.", fn); for (size_t i = 0; i < NELEMS(editor); i++) { if (systemf("%s %s 2>/dev/null", editor[i], path)) @@ -407,10 +406,10 @@ int serv_creat(char *arg) FILE *fp; if (!arg || !arg[0]) - errx(1, "missing argument to create"); + ERRX(1, "missing argument to create"); if (is_builtin(arg)) - errx(1, "%s is a built-in service.", arg); + ERRX(1, "%s is a built-in service.", arg); /* Input from a pipe or a proper TTY? */ if (isatty(STDIN_FILENO)) @@ -419,16 +418,16 @@ 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(1, "failed creating conf %s", arg); if (!icreate && fexist(fn)) { - warnx("%s already exists, skipping (use -c to override)", fn); + WARNX("%s already exists, skipping (use -c to override)", fn); fn = "/dev/null"; } fp = fopen(fn, "w"); if (!fp) - err(1, "failed opening %s for writing", fn); + ERR(1, "failed opening %s for writing", fn); while (fgets(buf, sizeof(buf), stdin)) fputs(buf, fp); @@ -442,24 +441,24 @@ int serv_delete(char *arg) char *fn; if (!arg || !arg[0]) { - warnx("missing argument to delete, may be one of:"); + WARNX("missing argument to delete, may be one of:"); return serv_list("available"); } 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(1, "%s is a built-in service.", arg); + ERRX(1, FINIT_RCSD " missing on system."); } if (!fexist(fn)) - warnx("cannot find %s", fn); + WARNX("cannot find %s", fn); if (iforce || yorn("Remove file and symlink(s) to %s (y/N)? ", fn)) { do_disable(arg, 0); if (remove(fn)) - err(1, "Failed removing %s", fn); + ERR(1, "Failed removing %s", fn); } return 0;