diff options
author | June McEnroe <june@causal.agency> | 2020-08-14 15:56:10 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-08-14 16:00:35 -0400 |
commit | e0dab12078fa6dc9903c8a73616a84347a6b93b2 (patch) | |
tree | 04b63da065d8e5cb6d495259e807761c3e52355c /daemon.c | |
parent | Open syslog, daemonize, write PID (diff) | |
download | catsit-e0dab12078fa6dc9903c8a73616a84347a6b93b2.tar.gz catsit-e0dab12078fa6dc9903c8a73616a84347a6b93b2.zip |
Implement spawntab parsing
Diffstat (limited to '')
-rw-r--r-- | daemon.c | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/daemon.c b/daemon.c index 7030978..6cbf8eb 100644 --- a/daemon.c +++ b/daemon.c @@ -19,6 +19,7 @@ #include <fcntl.h> #include <grp.h> #include <pwd.h> +#include <stdarg.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> @@ -39,6 +40,72 @@ #define ETCDIR "/usr/local/etc" #endif +#define WS " \t" + +int restartInterval = 1000; +struct Set256 stopExits; + +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_ERR, format, ap); + } + va_end(ap); +} + +static void parseConfig(bool exit, const char *path) { + size_t cap = 0; + char *buf = NULL; + + FILE *file = fopen(path, "r"); + if (!file) { + configerr(exit, "%s: %s", path, strerror(errno)); + goto err; + } + + prependClear(); + + int line = 1; + for (ssize_t len; 0 <= (len = getline(&buf, &cap, file)); ++line) { + if (buf[len - 1] == '\n') buf[len - 1] = '\0'; + + char *ptr = &buf[strspn(buf, WS)]; + if (!ptr[0] || ptr[0] == '#') { + continue; + } else if (ptr[0] == '%') { + int error = prependAdd(&ptr[1]); + if (error) { + configerr( + exit, "cannot add prepend command: %s", strerror(errno) + ); + goto err; + } + } else { + char *name = strsep(&ptr, WS); + if (!ptr) { + configerr( + exit, "%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)); + goto err; + } + } + } + if (ferror(file)) configerr(exit, "%s: %s", path, strerror(errno)); + +err: + free(buf); + fclose(file); +} + static void parseExits(char *list) { setClear(&stopExits); while (*list) { @@ -52,6 +119,8 @@ static void parseExits(char *list) { } int main(int argc, char *argv[]) { + setprogname(argv[0]); + bool daemonize = true; setAdd(&stopExits, 127); setAdd(&stopExits, EX_USAGE); @@ -83,7 +152,7 @@ int main(int argc, char *argv[]) { } } - // TODO: Read config file. + parseConfig(true, configPath); int error = access(serviceDir, X_OK); if (error) err(EX_NOINPUT, "%s", serviceDir); @@ -116,7 +185,7 @@ int main(int argc, char *argv[]) { int fifo = open(fifoPath, O_RDONLY | O_NONBLOCK | O_CLOEXEC); if (fifo < 0) err(EX_CANTCREAT, "%s", fifoPath); - openlog("spawnd", LOG_NDELAY | LOG_PID | LOG_PERROR, LOG_DAEMON); + openlog(getprogname(), LOG_NDELAY | LOG_PID | LOG_PERROR, LOG_DAEMON); if (daemonize) { error = daemon(0, 0); |