From 00e35a1031d58133f6de38573bf1b532c7c79641 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Mon, 17 Aug 2020 15:13:58 -0400 Subject: Simplify parseConfig error handling --- daemon.c | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/daemon.c b/daemon.c index 969675f..99b9fb9 100644 --- a/daemon.c +++ b/daemon.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -57,24 +56,14 @@ static void signalHandler(int signal) { signals[signal]++; } -static void configerr(bool exit, const char *format, ...) { - va_list ap; - va_start(ap, format); - if (exit) { - verrx(EX_DATAERR, format, ap); - } else { - vsyslog(LOG_WARNING, format, ap); - } - va_end(ap); -} - -static void parseConfig(bool exit, const char *path) { +static int parseConfig(const char *path) { + int ret = -1; size_t cap = 0; char *buf = NULL; FILE *file = fopen(path, "r"); if (!file) { - configerr(exit, "%s: %s", path, strerror(errno)); + syslog(LOG_WARNING, "%s: %m", path); goto err; } @@ -90,32 +79,35 @@ static void parseConfig(bool exit, const char *path) { } else if (ptr[0] == '%') { int error = prependAdd(&ptr[1]); if (error) { - configerr( - exit, "cannot add prepend command: %s", strerror(errno) - ); + syslog(LOG_WARNING, "cannot add prepend command: %m"); goto err; } } else { char *name = strsep(&ptr, WS); if (!ptr) { - configerr( - exit, "%s:%d: no command line for service %s", + syslog( + LOG_WARNING, "%s:%d: no command line for service %s", path, line, name ); goto err; } int error = serviceAdd(name, ptr); if (error) { - configerr(exit, "cannot add service: %s", strerror(errno)); + syslog(LOG_WARNING, "cannot add service: %m"); goto err; } } } - if (ferror(file)) configerr(exit, "%s: %s", path, strerror(errno)); + if (ferror(file)) { + syslog(LOG_WARNING, "%s: %m", path); + goto err; + } + ret = 0; err: free(buf); - fclose(file); + if (file) fclose(file); + return ret; } typedef void Action(struct Service *service); @@ -276,7 +268,8 @@ int main(int argc, char *argv[]) { openlog(getprogname(), LOG_NDELAY | LOG_PID | LOG_PERROR, LOG_DAEMON); - parseConfig(true, configPath); + error = parseConfig(configPath); + if (error) return EX_DATAERR; if (daemonize) { error = daemon(0, 0); @@ -392,7 +385,7 @@ int main(int argc, char *argv[]) { break; } if (signals[SIGHUP]) { - parseConfig(false, configPath); + parseConfig(configPath); setTitle(); signals[SIGHUP] = 0; } -- cgit 1.4.1