diff options
author | Marius Halden <marius.h@lden.org> | 2015-12-02 01:45:00 +0100 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2015-12-02 01:45:00 +0100 |
commit | fa9bee2fa3c44d9f03e7554da03545dade962c78 (patch) | |
tree | ec6287774739f16051409b5b5ccb3956b252d22f /svcsupervise.c | |
parent | b25b860f80276f6d0ada60a677baed81488c2f08 (diff) | |
download | svcmon-fa9bee2fa3c44d9f03e7554da03545dade962c78.tar.gz svcmon-fa9bee2fa3c44d9f03e7554da03545dade962c78.tar.bz2 svcmon-fa9bee2fa3c44d9f03e7554da03545dade962c78.tar.xz |
Add some more stuff
Diffstat (limited to 'svcsupervise.c')
-rw-r--r-- | svcsupervise.c | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/svcsupervise.c b/svcsupervise.c index 95ede21..5872e1e 100644 --- a/svcsupervise.c +++ b/svcsupervise.c @@ -3,11 +3,17 @@ #include <unistd.h> #include <errno.h> #include <err.h> +#include <string.h> +#include <signal.h> #include <sys/types.h> +#include <sys/event.h> +#include <sys/time.h> #include <sys/stat.h> #include <sys/file.h> #include <sys/wait.h> +#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 <dir>\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; } |