Parse control commands

This commit is contained in:
C. McEnroe 2020-08-15 15:16:50 -04:00
parent 34c39437f2
commit b7e36c72a6
2 changed files with 53 additions and 3 deletions

View File

@ -17,6 +17,7 @@
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <grp.h>
#include <poll.h>
#include <pwd.h>
@ -27,6 +28,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
@ -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;
}
}

View File

@ -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) {