From fa9bee2fa3c44d9f03e7554da03545dade962c78 Mon Sep 17 00:00:00 2001 From: Marius Halden Date: Wed, 2 Dec 2015 01:45:00 +0100 Subject: Add some more stuff --- svcsupervise.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) (limited to 'svcsupervise.c') diff --git a/svcsupervise.c b/svcsupervise.c index 95ede21..5872e1e 100644 --- a/svcsupervise.c +++ b/svcsupervise.c @@ -3,11 +3,17 @@ #include #include #include +#include +#include #include +#include +#include #include #include #include +#include "status.h" + char *const run_path[] = { "./run", NULL }; void @@ -73,10 +79,41 @@ update_status() perror("rename()"); } +void +setup_signals() +{ + struct sigaction act; + memset(&act, 0, sizeof(act)); + + act.sa_handler = SIG_IGN; + if (sigaction(SIGHUP, &act, NULL) == -1) + err(1, "sigaction()"); + if (sigaction(SIGINT, &act, NULL) == -1) + err(1, "sigaction()"); + if (sigaction(SIGTERM, &act, NULL) == -1) + err(1, "sigaction()"); +} + +void +reset_signals() +{ + struct sigaction act; + memset(&act, 0, sizeof(act)); + + act.sa_handler = SIG_DFL; + if (sigaction(SIGHUP, &act, NULL) == -1) + err(1, "sigaction()"); + if (sigaction(SIGINT, &act, NULL) == -1) + err(1, "sigaction()"); + if (sigaction(SIGTERM, &act, NULL) == -1) + err(1, "sigaction()"); +} + int main(int argc, char **argv) { - int lock_fd; + int ctrl_fd, ok_fd, lock_fd, kq; + struct kevent evt[6], revt[6]; if (argc != 2) errx(1, "Usage: %s \n", argv[0]); @@ -95,10 +132,42 @@ main(int argc, char **argv) if (mkfifo("supervise/ok", 0600) == -1 && errno != EEXIST) err(1, "mkfifo()"); - start_proc(); /* XXX: Main loop goes here */ + setup_signals(); + + kq = kqueue(); + if (kq == -1) + err(1, "kqueue()"); + + if ((ctrl_fd = open("supervise/control", O_RDONLY | O_NONBLOCK | O_CLOEXEC)) == -1) + err(1, "open()"); + + if ((ok_fd = open("supervise/ok", O_RDONLY | O_NONBLOCK | O_CLOEXEC)) == -1) + err(1, "open()"); + + EV_SET(&evt[0], SIGHUP, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, 0); + EV_SET(&evt[1], SIGINT, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, 0); + EV_SET(&evt[2], SIGTERM, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, 0); + EV_SET(&evt[3], SIGCHLD, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, 0); + EV_SET(&evt[4], ctrl_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0); + //EV_SET(&evt[5], ok_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0); + + if (kevent(kq, evt, 5, NULL, 0, NULL) == -1) + err(1, "kevent()"); + + for (;;) { + start_proc(); + break; + + kevent(kq, NULL, 0, revt, 5, NULL); + } + + if (close(ctrl_fd) == -1) + perror("close()"); if (close(lock_fd) == -1) perror("close()"); + reset_signals(); + return 0; } -- cgit v1.2.3