summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-08-17 15:13:58 -0400
committerJune McEnroe <june@causal.agency>2020-08-17 15:49:19 -0400
commit00e35a1031d58133f6de38573bf1b532c7c79641 (patch)
tree040605a22bf534946b5d07a0285f36c3f9cf7c10
parentProperly handle command line truncation (diff)
downloadcatsit-00e35a1031d58133f6de38573bf1b532c7c79641.tar.gz
catsit-00e35a1031d58133f6de38573bf1b532c7c79641.zip
Simplify parseConfig error handling
-rw-r--r--daemon.c41
1 files 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 <poll.h>
 #include <pwd.h>
 #include <signal.h>
-#include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -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;
 		}