diff options
Diffstat (limited to 'src/supervision')
-rw-r--r-- | src/supervision/deps-exe/s6-notifywhenup | 3 | ||||
-rw-r--r-- | src/supervision/s6-notifywhenup.c | 92 | ||||
-rw-r--r-- | src/supervision/s6-svstat.c | 31 |
3 files changed, 120 insertions, 6 deletions
diff --git a/src/supervision/deps-exe/s6-notifywhenup b/src/supervision/deps-exe/s6-notifywhenup new file mode 100644 index 0000000..077e863 --- /dev/null +++ b/src/supervision/deps-exe/s6-notifywhenup @@ -0,0 +1,3 @@ +${LIBS6} +-lskarnet +${TAINNOW_LIB} diff --git a/src/supervision/s6-notifywhenup.c b/src/supervision/s6-notifywhenup.c new file mode 100644 index 0000000..98f0401 --- /dev/null +++ b/src/supervision/s6-notifywhenup.c @@ -0,0 +1,92 @@ +/* ISC license. */ + +#include <unistd.h> +#include <errno.h> +#include <skalibs/uint.h> +#include <skalibs/bytestr.h> +#include <skalibs/sgetopt.h> +#include <skalibs/strerr2.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/tai.h> +#include <skalibs/iopause.h> +#include <skalibs/djbunix.h> +#include <s6/ftrigw.h> +#include <s6/s6-supervise.h> + +#define USAGE "s6-notifywhenup [ -d fd ] [ -e fifodir ] [ -f ] [ -t timeout ] prog..." +#define dieusage() strerr_dieusage(100, USAGE) + +static int run_child (int fd, char const *fifodir, unsigned int timeout) +{ + char dummy[4096] ; + iopause_fd x = { .fd = fd, .events = IOPAUSE_READ } ; + tain_t deadline ; + char pack[TAIN_PACK] ; + if (!tain_now_g()) strerr_diefu1sys(111, "tain_now") ; + if (timeout) tain_from_millisecs(&deadline, timeout) ; + else deadline = tain_infinite_relative ; + tain_add_g(&deadline, &deadline) ; + for (;;) + { + register int r = iopause_g(&x, 1, &deadline) ; + if (r < 0) strerr_diefu1sys(111, "iopause") ; + if (!r) return 99 ; + r = sanitize_read(fd_read(fd, dummy, 4096)) ; + if (r < 0) + if (errno == EPIPE) return 1 ; + else strerr_diefu1sys(111, "read from parent") ; + else if (r) + if (byte_chr(dummy, r, '\n') < r) break ; + } + close(fd) ; + tain_pack(pack, &STAMP) ; + if (!openwritenclose_suffix(S6_SUPERVISE_READY_FILENAME, pack, TAIN_PACK, ".new")) + strerr_warnwu1sys("open " S6_SUPERVISE_READY_FILENAME " for writing") ; + ftrigw_notify(fifodir, 'U') ; + return 0 ; +} + +int main (int argc, char const *const *argv, char const *const *envp) +{ + unsigned int fd = 1 ; + char const *fifodir = "event" ; + int df = 1 ; + unsigned int timeout = 0 ; + PROG = "s6-notifywhenup" ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + register int opt = subgetopt_r(argc, argv, "d:e:ft:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'd' : if (!uint0_scan(l.arg, &fd)) dieusage() ; break ; + case 'e' : fifodir = l.arg ; break ; + case 'f' : df = 0 ; break ; + case 't' : if (!uint0_scan(l.arg, &timeout)) dieusage() ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + if (!argc) dieusage() ; + + { + int p[2] ; + pid_t pid ; + if (pipe(p) < 0) strerr_diefu1sys(111, "pipe") ; + pid = df ? doublefork() : fork() ; + if (pid < 0) strerr_diefu1sys(111, df ? "doublefork" : "fork") ; + else if (!pid) + { + PROG = "s6-notifywhenup (child)" ; + close(p[1]) ; + return run_child(p[0], fifodir, timeout) ; + } + close(p[0]) ; + if (fd_move((int)fd, p[1]) < 0) strerr_diefu1sys(111, "fd_move") ; + } + pathexec_run(argv[0], argv, envp) ; + strerr_dieexec(111, argv[1]) ; +} diff --git a/src/supervision/s6-svstat.c b/src/supervision/s6-svstat.c index 4f95a0c..de8fe0d 100644 --- a/src/supervision/s6-svstat.c +++ b/src/supervision/s6-svstat.c @@ -23,6 +23,7 @@ int main (int argc, char const *const *argv) s6_svstatus_t status ; int flagnum = 0 ; int isup, normallyup ; + tain_t readytime = TAIN_ZERO ; char fmt[UINT_FMT] ; PROG = "s6-svstat" ; { @@ -44,22 +45,32 @@ int main (int argc, char const *const *argv) if (!s6_svstatus_read(*argv, &status)) strerr_diefu2sys(111, "read status for ", *argv) ; + tain_now_g() ; + if (tain_future(&status.stamp)) tain_copynow(&status.stamp) ; + { + char pack[TAIN_PACK] ; struct stat st ; unsigned int dirlen = str_len(*argv) ; - char fn[dirlen + 6] ; + char fn[dirlen + sizeof(S6_SUPERVISE_READY_FILENAME) + 1] ; byte_copy(fn, dirlen, *argv) ; byte_copy(fn + dirlen, 6, "/down") ; if (stat(fn, &st) == -1) if (errno != ENOENT) strerr_diefu2sys(111, "stat ", fn) ; else normallyup = 1 ; else normallyup = 0 ; + byte_copy(fn + dirlen, sizeof(S6_SUPERVISE_READY_FILENAME) + 1, "/" S6_SUPERVISE_READY_FILENAME) ; + if (openreadnclose(fn, pack, TAIN_PACK) < TAIN_PACK) + { + if (errno != ENOENT) strerr_warnwu2sys("read ", fn) ; + } + else + { + tain_unpack(pack, &readytime) ; + if (tain_future(&readytime)) tain_copynow(&readytime) ; + } } - tain_now_g() ; - if (tain_future(&status.stamp)) tain_copynow(&status.stamp) ; - tain_sub(&status.stamp, &STAMP, &status.stamp) ; - isup = status.pid && !status.flagfinishing ; if (isup) { @@ -89,8 +100,9 @@ int main (int argc, char const *const *argv) buffer_putnoflush(buffer_1small, ") ", 2) ; } + tain_sub(&status.stamp, &STAMP, &status.stamp) ; buffer_putnoflush(buffer_1small, fmt, uint64_fmt(fmt, status.stamp.sec.x)) ; - buffer_putnoflush(buffer_1small," seconds", 8) ; + buffer_putnoflush(buffer_1small, " seconds", 8) ; if (isup && !normallyup) buffer_putnoflush(buffer_1small, ", normally down", 15) ; @@ -103,6 +115,13 @@ int main (int argc, char const *const *argv) if (isup && (status.flagwant == 'd')) buffer_putnoflush(buffer_1small, ", want down", 12) ; + if (readytime.sec.x) + { + tain_sub(&readytime, &STAMP, &readytime) ; + buffer_putnoflush(buffer_1small, ", ready ", 8) ; + buffer_putnoflush(buffer_1small, fmt, uint64_fmt(fmt, readytime.sec.x)) ; + buffer_putnoflush(buffer_1small, " seconds", 8) ; + } if (buffer_putflush(buffer_1small, "\n", 1) < 0) strerr_diefu1sys(111, "write to stdout") ; return 0 ; |