s6

Mirror/fork of https://skarnet.org/software/s6/
git clone https://ccx.te2000.cz/git/s6
Log | Files | Refs | README | LICENSE

commit 5af73b17714076498a98b6a5679a4700399eaaa0
parent 2844097114fe7b36e03ff863ce8daaaf9d6583c7
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Tue, 20 Jan 2015 22:35:45 +0000

 Add readiness support to s6-svstat

Diffstat:
Mpackage/deps.mak | 8++++----
Dsrc/daemontools-extras/s6-notifywhenup.c | 91-------------------------------------------------------------------------------
Msrc/include/s6/s6-fdholder.h | 1+
Msrc/libs6/s6_fdholder_store_async.c | 4++--
Rsrc/daemontools-extras/deps-exe/s6-notifywhenup -> src/supervision/deps-exe/s6-notifywhenup | 0
Asrc/supervision/s6-notifywhenup.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/supervision/s6-svstat.c | 31+++++++++++++++++++++++++------
7 files changed, 124 insertions(+), 103 deletions(-)

diff --git a/package/deps.mak b/package/deps.mak @@ -8,7 +8,7 @@ src/include/s6/s6lock.h: src/include/s6/config.h src/conn-tools/s6-accessrules-cdb-from-fs.o src/conn-tools/s6-accessrules-cdb-from-fs.lo: src/conn-tools/s6-accessrules-cdb-from-fs.c src/conn-tools/s6-accessrules-fs-from-cdb.o src/conn-tools/s6-accessrules-fs-from-cdb.lo: src/conn-tools/s6-accessrules-fs-from-cdb.c src/conn-tools/s6-connlimit.o src/conn-tools/s6-connlimit.lo: src/conn-tools/s6-connlimit.c -src/conn-tools/s6-fdholderd.o src/conn-tools/s6-fdholderd.lo: src/conn-tools/s6-fdholderd.c src/include/s6/accessrules.h src/include/s6/s6-fdholder.h src/include/s6/s6-supervise.h +src/conn-tools/s6-fdholderd.o src/conn-tools/s6-fdholderd.lo: src/conn-tools/s6-fdholderd.c src/include/s6/accessrules.h src/include/s6/s6-fdholder.h src/conn-tools/s6-ioconnect.o src/conn-tools/s6-ioconnect.lo: src/conn-tools/s6-ioconnect.c src/conn-tools/s6-ipcclient.o src/conn-tools/s6-ipcclient.lo: src/conn-tools/s6-ipcclient.c src/conn-tools/s6-ipcserver-access.o src/conn-tools/s6-ipcserver-access.lo: src/conn-tools/s6-ipcserver-access.c src/include/s6/accessrules.h @@ -23,7 +23,6 @@ src/daemontools-extras/s6-envdir.o src/daemontools-extras/s6-envdir.lo: src/daem src/daemontools-extras/s6-envuidgid.o src/daemontools-extras/s6-envuidgid.lo: src/daemontools-extras/s6-envuidgid.c src/daemontools-extras/s6-fghack.o src/daemontools-extras/s6-fghack.lo: src/daemontools-extras/s6-fghack.c src/daemontools-extras/s6-log.o src/daemontools-extras/s6-log.lo: src/daemontools-extras/s6-log.c -src/daemontools-extras/s6-notifywhenup.o src/daemontools-extras/s6-notifywhenup.lo: src/daemontools-extras/s6-notifywhenup.c src/include/s6/ftrigw.h src/include/s6/s6-supervise.h src/daemontools-extras/s6-setlock.o src/daemontools-extras/s6-setlock.lo: src/daemontools-extras/s6-setlock.c src/include/s6/config.h src/daemontools-extras/s6-setsid.o src/daemontools-extras/s6-setsid.lo: src/daemontools-extras/s6-setsid.c src/daemontools-extras/s6-setuidgid.o src/daemontools-extras/s6-setuidgid.lo: src/daemontools-extras/s6-setuidgid.c src/include/s6/config.h @@ -95,6 +94,7 @@ src/pipe-tools/s6-ftrig-listen1.o src/pipe-tools/s6-ftrig-listen1.lo: src/pipe-t src/pipe-tools/s6-ftrig-notify.o src/pipe-tools/s6-ftrig-notify.lo: src/pipe-tools/s6-ftrig-notify.c src/include/s6/ftrigw.h src/pipe-tools/s6-ftrig-wait.o src/pipe-tools/s6-ftrig-wait.lo: src/pipe-tools/s6-ftrig-wait.c src/include/s6/ftrigr.h src/pipe-tools/s6-mkfifodir.o src/pipe-tools/s6-mkfifodir.lo: src/pipe-tools/s6-mkfifodir.c src/include/s6/ftrigw.h +src/supervision/s6-notifywhenup.o src/supervision/s6-notifywhenup.lo: src/supervision/s6-notifywhenup.c src/include/s6/ftrigw.h src/include/s6/s6-supervise.h src/supervision/s6-supervise.o src/supervision/s6-supervise.lo: src/supervision/s6-supervise.c src/include/s6/ftrigw.h src/include/s6/s6-supervise.h src/supervision/s6-svc.o src/supervision/s6-svc.lo: src/supervision/s6-svc.c src/include/s6/config.h src/include/s6/s6-supervise.h src/supervision/s6-svlisten.o src/supervision/s6-svlisten.lo: src/supervision/s6-svlisten.c src/include/s6/ftrigr.h src/include/s6/s6-supervise.h @@ -139,8 +139,6 @@ s6-fghack: private EXTRA_LIBS := s6-fghack: src/daemontools-extras/s6-fghack.o -lskarnet s6-log: private EXTRA_LIBS := ${TAINNOW_LIB} s6-log: src/daemontools-extras/s6-log.o -lskarnet -s6-notifywhenup: private EXTRA_LIBS := ${TAINNOW_LIB} -s6-notifywhenup: src/daemontools-extras/s6-notifywhenup.o ${LIBS6} -lskarnet s6-setlock: private EXTRA_LIBS := ${TAINNOW_LIB} s6-setlock: src/daemontools-extras/s6-setlock.o -lskarnet s6-setsid: private EXTRA_LIBS := @@ -175,6 +173,8 @@ s6-ftrig-wait: private EXTRA_LIBS := ${SOCKET_LIB} ${TAINNOW_LIB} s6-ftrig-wait: src/pipe-tools/s6-ftrig-wait.o ${LIBS6} -lskarnet s6-mkfifodir: private EXTRA_LIBS := s6-mkfifodir: src/pipe-tools/s6-mkfifodir.o ${LIBS6} -lskarnet +s6-notifywhenup: private EXTRA_LIBS := ${TAINNOW_LIB} +s6-notifywhenup: src/supervision/s6-notifywhenup.o ${LIBS6} -lskarnet s6-supervise: private EXTRA_LIBS := ${TAINNOW_LIB} s6-supervise: src/supervision/s6-supervise.o ${LIBS6} -lskarnet s6-svc: private EXTRA_LIBS := diff --git a/src/daemontools-extras/s6-notifywhenup.c b/src/daemontools-extras/s6-notifywhenup.c @@ -1,91 +0,0 @@ -/* 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 ; - 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) ; - fd = open_create(S6_SUPERVISE_READY_FILENAME) ; - if (fd < 0) strerr_warnwu1sys("touch " S6_SUPERVISE_READY_FILENAME) ; - else close(fd) ; - 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/include/s6/s6-fdholder.h b/src/include/s6/s6-fdholder.h @@ -10,6 +10,7 @@ #include <skalibs/unixconnection.h> #define S6_FDHOLDER_ID_SIZE 255 +#define S6_FDHOLDER_MAX 16 typedef struct s6_fdholder_s s6_fdholder_t, *s6_fdholder_t_ref ; struct s6_fdholder_s diff --git a/src/libs6/s6_fdholder_store_async.c b/src/libs6/s6_fdholder_store_async.c @@ -14,7 +14,7 @@ int s6_fdholder_store_async (s6_fdholder_t *a, int fd, char const *id, tain_t co siovec_t v[2] = { { .s = pack, .len = 2 + TAIN_PACK }, { .s = id, .len = idlen + 1 } } ; unixmessage_v_t m = { .v = v, .vlen = 2, .fds = &fd, .nfds = 1 } ; if (idlen > S6_FDHOLDER_ID_SIZE) return (errno = ENAMETOOLONG, 0) ; - pack[1] = (unsigned char)idlen ; - tain_pack(pack + 2, limit) ; + tain_pack(pack + 1, limit) ; + pack[1+TAIN_PACK] = (unsigned char)idlen ; return unixmessage_putv(&a->connection.out, &m) ; } diff --git a/src/daemontools-extras/deps-exe/s6-notifywhenup b/src/supervision/deps-exe/s6-notifywhenup diff --git a/src/supervision/s6-notifywhenup.c 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 @@ -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 ;