From e9d5512e1ab0b93b8dfca1d6e8147a6792cff642 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 2 Jul 2017 12:38:35 +0200 Subject: [PATCH] Refactor, simplify ... print() APIs already check if silent Signed-off-by: Joachim Nilsson --- src/conf.c | 3 +-- src/exec.c | 4 ++-- src/plugin.c | 12 +++-------- src/service.c | 55 +++++++++++++++------------------------------------ 4 files changed, 22 insertions(+), 52 deletions(-) diff --git a/src/conf.c b/src/conf.c index 1bdd71ae..09dd88f0 100644 --- a/src/conf.c +++ b/src/conf.c @@ -450,8 +450,7 @@ static int parse_conf(char *file) strcat(line, " "); } - if (!silent) - print(0, "Loading %sconfiguration", line); + print(0, "Loading %sconfiguration", line); _d("Parsing %s", file); while (!feof(fp)) { diff --git a/src/exec.c b/src/exec.c index eb620951..2954da41 100644 --- a/src/exec.c +++ b/src/exec.c @@ -161,7 +161,7 @@ int run_interactive(char *cmd, char *fmt, ...) return 1; } - if (!silent && fmt) { + if (fmt) { va_start(ap, fmt); vsnprintf(line, sizeof(line), fmt, ap); va_end(ap); @@ -193,7 +193,7 @@ int run_interactive(char *cmd, char *fmt, ...) } } - if (!silent && fmt) + if (fmt) print_result(status); /* Dump any results of cmd on stderr after we've printed [ OK ] or [FAIL] */ diff --git a/src/plugin.c b/src/plugin.c index 69b57016..79e3cb84 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -311,8 +311,7 @@ static int load_plugins(char *path) DIR *dp; struct dirent *entry; - if (!silent) - print_desc("Loading plugins", NULL); + print_desc("Loading plugins", NULL); dp = opendir(path); if (!dp) { @@ -336,9 +335,7 @@ static int load_plugins(char *path) #else static int load_plugins(char *UNUSED(path)) { - if (!silent) - print_desc("Initializing plugins", NULL); - + print_desc("Initializing plugins", NULL); return 0; } #endif /* ENABLE_STATIC */ @@ -350,10 +347,7 @@ int plugin_init(uev_ctx_t *ctx, char *path) if (!load_plugins(path)) fail = init_plugins(ctx); - if (!silent) - print_result(fail); - - return fail; + return print_result(fail); } void plugin_exit(void) diff --git a/src/service.c b/src/service.c index e5897f02..3998025c 100644 --- a/src/service.c +++ b/src/service.c @@ -150,14 +150,7 @@ static int service_start(svc_t *svc) /* Don't try and start service if it doesn't exist. */ if (!fexist(svc->cmd) && !svc->inetd.cmd) { - if (!silent) { - char msg[80]; - - snprintf(msg, sizeof(msg), "Service %s does not exist!", svc->cmd); - print_desc("", msg); - print_result(1); - } - + print(1, "Service %s does not exist!", svc->cmd); svc_missing(svc); return 1; } @@ -166,20 +159,14 @@ static int service_start(svc_t *svc) if (is_norespawn()) return 1; - if (!silent) { - if (svc_is_daemon(svc) || svc_is_inetd(svc)) - print_desc("Starting ", svc->desc); - else - print_desc("", svc->desc); - } + if (svc_is_daemon(svc) || svc_is_inetd(svc)) + print_desc("Starting ", svc->desc); + else + print_desc("", svc->desc); #ifdef INETD_ENABLED - if (svc_is_inetd(svc)) { - result = inetd_start(&svc->inetd); - if (!silent) - print_result(result); - return result; - } + if (svc_is_inetd(svc)) + return print_result(inetd_start(&svc->inetd)); #endif /* Declare we're waiting for svc to create its pidfile */ @@ -316,7 +303,7 @@ static int service_start(svc_t *svc) #ifdef INETD_ENABLED if (svc_is_inetd_conn(svc) && svc->inetd.type == SOCK_STREAM) - close(svc->stdin_fd); + close(svc->stdin_fd); #endif plugin_run_hook(HOOK_SVC_START, (void *)(uintptr_t)pid); @@ -328,10 +315,7 @@ static int service_start(svc_t *svc) sigprocmask(SIG_SETMASK, &omask, NULL); - if (!silent) - print_result(result); - - return 0; + return print_result(result); } /** @@ -344,14 +328,14 @@ static void service_kill(svc_t *svc) { service_timeout_cancel(svc); - if (runlevel != 1 && !silent) + if (runlevel != 1) print_desc("Killing ", svc->desc); _d("Sending SIGKILL to pid:%d name:%s", svc->pid, pid_get_name(svc->pid, NULL, 0)); kill(svc->pid, SIGKILL); /* Let SIGKILLs stand out, show result as [WARN] */ - if (runlevel != 1 && !silent) + if (runlevel != 1) print(2, NULL); } @@ -371,7 +355,7 @@ static int service_stop(svc_t *svc) #ifdef INETD_ENABLED if (svc_is_inetd(svc)) { - int do_print = runlevel != 1 && !silent && !svc_is_busy(svc); + int do_print = runlevel != 1 && !svc_is_busy(svc); if (do_print) print_desc("Stopping ", svc->desc); @@ -392,13 +376,13 @@ static int service_stop(svc_t *svc) if (SVC_TYPE_SERVICE != svc->type) return 0; - if (runlevel != 1 && !silent) + if (runlevel != 1) print_desc("Stopping ", svc->desc); _d("Sending SIGTERM to pid:%d name:%s", svc->pid, pid_get_name(svc->pid, NULL, 0)); res = kill(svc->pid, SIGTERM); - if (runlevel != 1 && !silent) + if (runlevel != 1) print_result(res); return res; @@ -416,8 +400,6 @@ static int service_stop(svc_t *svc) */ static int service_restart(svc_t *svc) { - int err; - /* Ignore if finit is SIGSTOP'ed */ if (is_norespawn()) return 1; @@ -431,18 +413,13 @@ static int service_restart(svc_t *svc) return 1; } - if (!silent) - print_desc("Restarting ", svc->desc); + print_desc("Restarting ", svc->desc); /* Declare we're waiting for svc to re-assert/touch its pidfile */ svc_starting(svc); _d("Sending SIGHUP to PID %d", svc->pid); - err = kill(svc->pid, SIGHUP); - - if (!silent) - print_result(err); - return err; + return print_result(kill(svc->pid, SIGHUP)); } /**