From b7e36c72a604796209f7f7b11d88aa3b75c1b44f Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 15 Aug 2020 15:16:50 -0400 Subject: Parse control commands --- daemon.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- daemon.h | 3 ++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/daemon.c b/daemon.c index c759f20..13098af 100644 --- a/daemon.c +++ b/daemon.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -115,6 +117,50 @@ err: fclose(file); } +typedef void Action(struct Service *service); +static void parseControl(char *command) { + char *action = strsep(&command, WS); + if (!command) { + syslog(LOG_NOTICE, "no service names for %s", action); + return; + } + + Action *fn = NULL; + int signal = 0; + if (!strcmp(action, "start")) { + fn = serviceStart; + } else if (!strcmp(action, "stop")) { + fn = serviceStop; + } else if (!strcmp(action, "restart")) { + fn = serviceRestart; + } else if (!strcmp(action, "status")) { + // TODO + } else { + for (int i = 1; i < NSIG; ++i) { + if (strcasecmp(action, sys_signame[i])) continue; + signal = i; + break; + } + } + if (!fn && !signal) { + syslog(LOG_NOTICE, "unknown action or signal %s", action); + return; + } + + while (command) { + char *pattern = strsep(&command, WS); + for (size_t i = 0; i < services.len; ++i) { + struct Service *service = &services.ptr[i]; + if (fnmatch(pattern, service->name, 0)) continue; + if (signal) { + serviceSignal(service, signal); + } else { + fn(service); + } + } + } +} + static void parseExits(char *list) { setClear(&stopExits); while (*list) { @@ -288,7 +334,9 @@ int main(int argc, char *argv[]) { if (nfds > 0 && fds[0].revents) { const char *line; while (NULL != (line = lineRead(&fifoLine, fifo))) { - syslog(LOG_INFO, "control: %s", line); + char buf[LineCap]; + snprintf(buf, sizeof(buf), "%s", line); + parseControl(buf); } if (errno != EAGAIN) syslog(LOG_ERR, "read: %m"); } @@ -331,7 +379,8 @@ int main(int argc, char *argv[]) { signals[SIGHUP] = 0; } if (signals[SIGINFO]) { - // TODO: status * + char command[] = "status *"; + parseControl(command); signals[SIGINFO] = 0; } } diff --git a/daemon.h b/daemon.h index 17b1c7f..b8b288b 100644 --- a/daemon.h +++ b/daemon.h @@ -51,9 +51,10 @@ static inline int prependAdd(const char *command) { return 0; } +enum { LineCap = 512 }; struct Line { size_t len; - char buf[512]; + char buf[LineCap]; }; static inline const char *lineFlush(struct Line *line) { -- cgit 1.4.1