diff options
author | Marius Halden <marius.h@lden.org> | 2015-12-02 02:47:02 +0100 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2015-12-02 02:47:02 +0100 |
commit | 98f591956e853da6e305db0247b20a3821725cd5 (patch) | |
tree | fdb44220df7ad609bdc205e242f20014f4c20afa /svcsupervise.c | |
parent | fa9bee2fa3c44d9f03e7554da03545dade962c78 (diff) | |
download | svcmon-98f591956e853da6e305db0247b20a3821725cd5.tar.gz svcmon-98f591956e853da6e305db0247b20a3821725cd5.tar.bz2 svcmon-98f591956e853da6e305db0247b20a3821725cd5.tar.xz |
Mostly additions to the svcsupervise mainloop
Diffstat (limited to 'svcsupervise.c')
-rw-r--r-- | svcsupervise.c | 93 |
1 files changed, 86 insertions, 7 deletions
diff --git a/svcsupervise.c b/svcsupervise.c index 5872e1e..1fce356 100644 --- a/svcsupervise.c +++ b/svcsupervise.c @@ -23,7 +23,7 @@ start_proc() if (p == 0) { execv(run_path[0], run_path); } else if (p > 0) { - waitpid(p, NULL, 0); + /* XXX: Do something apropriate here */ } else { err(1, "fork()"); } @@ -79,6 +79,55 @@ update_status() perror("rename()"); } +int +handle_ctrl_command(int fd) +{ + char cmd; + int r; + + r = read(fd, &cmd, 1); + if (r == 0) { + return 1; + } else if (r == -1) { + if (errno != EAGAIN && errno != EINTR) + perror("read()"); + return 1; + } else { + switch (cmd) { + case 'd': + /* Down */ + break; + case 'u': + /* Up */ + break; + case 'o': + break; + case 'a': + break; + case 'h': + break; + case 'k': + break; + case 't': + break; + case 'i': + break; + case 'p': + break; + case 'c': + break; + case 'x': + return 0; + break; + default: + fprintf(stderr, "Unknown command \"%c\"", cmd); + break; + } + } + + return 1; +} + void setup_signals() { @@ -112,7 +161,7 @@ reset_signals() int main(int argc, char **argv) { - int ctrl_fd, ok_fd, lock_fd, kq; + int ctrl_fd, ok_fd, lock_fd, kq, e, i; struct kevent evt[6], revt[6]; if (argc != 2) @@ -149,18 +198,48 @@ main(int argc, char **argv) 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); + EV_SET(&evt[5], 1, EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_ONESHOT, NOTE_SECONDS, 5, 0); if (kevent(kq, evt, 5, NULL, 0, NULL) == -1) err(1, "kevent()"); - for (;;) { - start_proc(); - break; + start_proc(); - kevent(kq, NULL, 0, revt, 5, NULL); + for (;;) { + e = kevent(kq, NULL, 0, revt, 6, NULL); + if (e == -1) { + if (errno != EINTR) + perror("kevent()"); + continue; + } else if (e > 0) { + /* XXX: Do the things */ + for (i = 0; i < e; i++) { + if (revt[i].filter == EVFILT_SIGNAL) { + if (revt[i].ident == SIGCHLD) { + wait(NULL); /* XXX: We have to this better */ + if (kevent(kq, &evt[5], 1, NULL, 0, NULL) == -1) + perror("kevent()"); + } else if (revt[i].ident == SIGHUP || + revt[i].ident == SIGINT || + revt[i].ident == SIGTERM) { + goto end; + } + } else if (revt[i].filter == EVFILT_TIMER && revt[i].ident == 1) { + start_proc(); /* XXX: Handle some failures here */ + } else if (revt[i].filter == EVFILT_READ) { + if (revt[i].ident == ctrl_fd) { + if (handle_ctrl_command(ctrl_fd) == 0) + goto end; + } + } else { + fprintf(stderr, "Unknown event\n"); + } + } + } } +end: + fprintf(stderr, "Sup bye\n"); if (close(ctrl_fd) == -1) perror("close()"); |