diff options
| author | Laurent Bercot <ska-skaware@skarnet.org> | 2026-04-09 02:31:43 +0000 |
|---|---|---|
| committer | Laurent Bercot <ska-skaware@skarnet.org> | 2026-04-09 02:31:43 +0000 |
| commit | 8a5a621baa9e756b34c507c3d8e2119dabe7f630 (patch) | |
| tree | 7a04e091de081a764ffc76ddde7cb84ee93662d0 /src | |
| parent | 1863ed9af17071fb733905534eb9c9433e86715a (diff) | |
| download | s6-8a5a621baa9e756b34c507c3d8e2119dabe7f630.tar.gz | |
Add s6-notify-fd-from-socket and s6-notify-socket-from-fd
Also add .gitkeep files, remove old include-local/s6lockd.h,
update a little bit of doc, make s6-ftrigrd pipe-only
Diffstat (limited to 'src')
| -rw-r--r-- | src/alias/deps-lib/.gitkeep | 0 | ||||
| -rw-r--r-- | src/conn-tools/deps-lib/.gitkeep | 0 | ||||
| -rw-r--r-- | src/daemontools-extras/deps-lib/.gitkeep | 0 | ||||
| -rw-r--r-- | src/fdholder/deps-lib/.gitkeep | 0 | ||||
| -rw-r--r-- | src/include-local/.gitkeep | 0 | ||||
| -rw-r--r-- | src/include-local/s6lockd.h | 8 | ||||
| -rw-r--r-- | src/instance/deps-lib/.gitkeep | 0 | ||||
| -rw-r--r-- | src/libs6/s6-ftrigrd.c | 2 | ||||
| -rw-r--r-- | src/pipe-tools/deps-lib/.gitkeep | 0 | ||||
| -rw-r--r-- | src/supervision/deps-exe/s6-notify-fd-from-socket | 3 | ||||
| -rw-r--r-- | src/supervision/deps-exe/s6-notify-socket-from-fd | 3 | ||||
| -rw-r--r-- | src/supervision/deps-lib/.gitkeep | 0 | ||||
| -rw-r--r-- | src/supervision/s6-notify-fd-from-socket.c | 145 | ||||
| -rw-r--r-- | src/supervision/s6-notify-socket-from-fd.c | 138 | ||||
| -rw-r--r-- | src/usertree/deps-lib/.gitkeep | 0 |
15 files changed, 290 insertions, 9 deletions
diff --git a/src/alias/deps-lib/.gitkeep b/src/alias/deps-lib/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/alias/deps-lib/.gitkeep diff --git a/src/conn-tools/deps-lib/.gitkeep b/src/conn-tools/deps-lib/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/conn-tools/deps-lib/.gitkeep diff --git a/src/daemontools-extras/deps-lib/.gitkeep b/src/daemontools-extras/deps-lib/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/daemontools-extras/deps-lib/.gitkeep diff --git a/src/fdholder/deps-lib/.gitkeep b/src/fdholder/deps-lib/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/fdholder/deps-lib/.gitkeep diff --git a/src/include-local/.gitkeep b/src/include-local/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/include-local/.gitkeep diff --git a/src/include-local/s6lockd.h b/src/include-local/s6lockd.h deleted file mode 100644 index 61a23b8..0000000 --- a/src/include-local/s6lockd.h +++ /dev/null @@ -1,8 +0,0 @@ -/* ISC license. */ - -#ifndef S6LOCKD_H -#define S6LOCKD_H - -extern int s6lockd_openandlock (char const *, int, int) ; - -#endif diff --git a/src/instance/deps-lib/.gitkeep b/src/instance/deps-lib/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/instance/deps-lib/.gitkeep diff --git a/src/libs6/s6-ftrigrd.c b/src/libs6/s6-ftrigrd.c index a037e31..cee705d 100644 --- a/src/libs6/s6-ftrigrd.c +++ b/src/libs6/s6-ftrigrd.c @@ -159,7 +159,7 @@ int main (void) { tain deadline ; tain_addsec_g(&deadline, 5) ; - sassserver_init_g(&a, FTRIGR_BANNER1, FTRIGR_BANNER2, &ftrigio_add, &ftrigio_remove, sizeof(ftrigio), &cleanup, 0, &deadline) ; + sassserver_init_frompipe_g(&a, FTRIGR_BANNER1, FTRIGR_BANNER2, &ftrigio_add, &ftrigio_remove, sizeof(ftrigio), &cleanup, 0, &deadline) ; } while (!r) diff --git a/src/pipe-tools/deps-lib/.gitkeep b/src/pipe-tools/deps-lib/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/pipe-tools/deps-lib/.gitkeep diff --git a/src/supervision/deps-exe/s6-notify-fd-from-socket b/src/supervision/deps-exe/s6-notify-fd-from-socket new file mode 100644 index 0000000..720fe7d --- /dev/null +++ b/src/supervision/deps-exe/s6-notify-fd-from-socket @@ -0,0 +1,3 @@ +-lskarnet +${SOCKET_LIB} +${SYSCLOCK_LIB} diff --git a/src/supervision/deps-exe/s6-notify-socket-from-fd b/src/supervision/deps-exe/s6-notify-socket-from-fd new file mode 100644 index 0000000..720fe7d --- /dev/null +++ b/src/supervision/deps-exe/s6-notify-socket-from-fd @@ -0,0 +1,3 @@ +-lskarnet +${SOCKET_LIB} +${SYSCLOCK_LIB} diff --git a/src/supervision/deps-lib/.gitkeep b/src/supervision/deps-lib/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/supervision/deps-lib/.gitkeep diff --git a/src/supervision/s6-notify-fd-from-socket.c b/src/supervision/s6-notify-fd-from-socket.c new file mode 100644 index 0000000..b5a6233 --- /dev/null +++ b/src/supervision/s6-notify-fd-from-socket.c @@ -0,0 +1,145 @@ +/* ISC license. */ + +#include <skalibs/nonposix.h> + +#include <sys/types.h> +#include <fcntl.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <sys/socket.h> +#include <sys/un.h> + +#include <skalibs/posixplz.h> +#include <skalibs/uint64.h> +#include <skalibs/types.h> +#include <skalibs/bytestr.h> +#include <skalibs/envexec.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/tai.h> +#include <skalibs/iopause.h> +#include <skalibs/djbunix.h> +#include <skalibs/socket.h> + +#define USAGE "s6-notify-fd-from-socket [ -f ] [ -3 fd ] [ -t timeout ] prog..." +#define dieusage() strerr_dieusage(100, USAGE) + +enum golb_e +{ + GOLB_SINGLEFORK = 0x01, +} ; + +enum gola_e +{ + GOLA_NOTIF, + GOLA_TIMEOUT, + GOLA_N +} ; + +static void bindit (int sock, char *name) +{ + struct sockaddr_un addr = { .sun_family = AF_UNIX, .sun_path = "" } ; + socklen_t addrlen = sizeof(struct sockaddr_un) ; + if (bind(sock, (struct sockaddr *)&addr, sizeof(sa_family_t)) == -1) /* autobind */ + strerr_diefu1sys(111, "bind") ; + if (getsockname(sock, (struct sockaddr *)&addr, &addrlen) == -1) + strerr_diefu1sys(111, "getsockname") ; + if (addr.sun_path[0]) + strerr_diefu1x(111, "autobind to an abstract socket") ; + memcpy(name, addr.sun_path + 1, 5) ; + name[5] = 0 ; +} + +static inline int run_child (int sock, int fd, unsigned int timeout) +{ + char buf[8192] ; + iopause_fd x = { .fd = fd, .events = IOPAUSE_READ } ; + int found = 0 ; + tain deadline = TAIN_INFINITE_RELATIVE ; + tain_now_g() ; + if (timeout) tain_from_millisecs(&deadline, timeout) ; + tain_add_g(&deadline, &deadline) ; + while (!found) + { + int r = iopause_g(&x, 1, &deadline) ; + if (r == -1) strerr_diefu1sys(111, "iopause") ; + if (!r) strerr_dief1x(99, "timed out waiting for notification") ; + r = sanitize_read(fd_recv(sock, buf, 8191, 0)) ; + if (r == -1) + { + if (errno == EPIPE) _exit(0) ; + else strerr_diefu1sys(111, "recv") ; + } + if (r) + { + buf[r++] = 0 ; + if (!strncmp(buf, "READY=1\n", 8) || strstr(buf, "\nREADY=1\n")) + found = 1 ; + } + } + fd_write(fd, "\n", 1) ; + _exit(0) ; +} + +static inline int read_uint (char const *file, unsigned int *fd) +{ + char buf[UINT_FMT + 1] ; + ssize_t r = openreadnclose_nb(file, buf, UINT_FMT) ; + if (r == -1) return -1 ; + buf[byte_chr(buf, r, '\n')] = 0 ; + return !!uint0_scan(buf, fd) ; +} + +int main (int argc, char const *const *argv) +{ + static gol_bool const rgolb[] = + { + { .so = 'f', .lo = "no-doublefork", .clear = 0, .set = GOLB_SINGLEFORK }, + { .so = 0, .lo = "doublefork", .clear = GOLB_SINGLEFORK, .set = 0 }, + } ; + static gol_arg const rgola[] = + { + { .so = '3', .lo = "notification-fd", .i = GOLA_NOTIF }, + { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT }, + } ; + unsigned int fd = 0 ; + unsigned int timeout = 0 ; + uint64_t wgolb = 0 ; + char const *wgola[GOLA_N] = { 0 } ; + pid_t pid ; + int sock ; + unsigned int golc ; + char modif[21] = "NOTIFY_SOCKET=@XXXXX" ; + PROG = "s6-notify-fd-from-socket" ; + + golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ; + argc -= golc ; argv += golc ; + if (!argc) dieusage() ; + + if (wgola[GOLA_TIMEOUT] && !uint0_scan(wgola[GOLA_TIMEOUT], &timeout)) + strerr_dief2x(100, "timeout", " must be an unsigned integer") ; + if (wgola[GOLA_NOTIF]) + { + if (!uint0_scan(wgola[GOLA_NOTIF], &fd)) + strerr_dief2x(100, "notification-fd", " must be an unsigned integer") ; + } + else + { + int r = read_uint("notification-fd", &fd) ; + if (r == -1) strerr_diefu2sys(111, "read ", "./notification-fd") ; + if (!r) strerr_dief2x(100, "invalid ", "./notification-fd") ; + } + if (fcntl(fd, F_GETFD) == -1) + strerr_dief2sys(111, "notification-fd", " sanity check failed") ; + + sock = ipc_datagram_nbcoe() ; + if (sock == -1) strerr_diefu1sys(111, "create socket") ; + bindit(sock, modif + 15) ; + + pid = wgolb & GOLB_SINGLEFORK ? fork() : doublefork() ; + if (pid == -1) strerr_diefu1sys(111, wgolb & GOLB_SINGLEFORK ? "fork" : "doublefork") ; + if (!pid) run_child(sock, fd, timeout) ; + fd_close(sock) ; + fd_close(fd) ; + xmexec_n(argv, modif, 21, 1) ; +} diff --git a/src/supervision/s6-notify-socket-from-fd.c b/src/supervision/s6-notify-socket-from-fd.c new file mode 100644 index 0000000..c443b40 --- /dev/null +++ b/src/supervision/s6-notify-socket-from-fd.c @@ -0,0 +1,138 @@ +/* ISC license. */ + +#include <sys/types.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/socket.h> +#include <sys/un.h> + +#include <skalibs/uint64.h> +#include <skalibs/types.h> +#include <skalibs/bytestr.h> +#include <skalibs/envexec.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/tai.h> +#include <skalibs/iopause.h> +#include <skalibs/djbunix.h> +#include <skalibs/socket.h> + +#define USAGE "s6-notify-socket-from-fd [ -d fd ] [ -f ] [ -t timeout ] [ -k ] prog..." +#define dieusage() strerr_dieusage(100, USAGE) + +#define VAR "NOTIFY_SOCKET" + +enum golb_e +{ + GOLB_SINGLEFORK = 0x01, + GOLB_KEEP = 0x02, +} ; + +enum gola_e +{ + GOLA_NOTIF, + GOLA_TIMEOUT, + GOLA_N +} ; + +static inline int ipc_sendto (int fd, char const *s, size_t len, char const *path) +{ + struct sockaddr_un sa ; + size_t l = strlen(path) ; + if (l > IPCPATH_MAX) return (errno = ENAMETOOLONG, 0) ; + memset(&sa, 0, sizeof sa) ; + sa.sun_family = AF_UNIX ; + memcpy(sa.sun_path, path, l+1) ; + if (path[0] == '@') sa.sun_path[0] = 0 ; + return sendto(fd, s, len, MSG_NOSIGNAL, (struct sockaddr *)&sa, sizeof sa) >= 0 ; +} + +static inline void notify_systemd (pid_t pid, char const *socketpath) +{ + size_t n = 16 ; + char fmt[16 + PID_FMT] = "READY=1\nMAINPID=" ; + int fd = ipc_datagram_b() ; + if (fd == -1) strerr_diefu1sys(111, "create socket") ; + n += pid_fmt(fmt + n, pid) ; + fmt[n++] = '\n' ; + if (!ipc_sendto(fd, fmt, n, socketpath)) + strerr_diefu2sys(111, "send notification message to ", socketpath) ; + fd_close(fd) ; +} + +static inline int run_child (int fd, unsigned int timeout, pid_t pid, char const *s) +{ + char dummy[4096] ; + iopause_fd x = { .fd = fd, .events = IOPAUSE_READ } ; + tain deadline = TAIN_INFINITE_RELATIVE ; + tain_now_g() ; + if (timeout) tain_from_millisecs(&deadline, timeout) ; + tain_add_g(&deadline, &deadline) ; + for (;;) + { + int r = iopause_g(&x, 1, &deadline) ; + if (r == -1) 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 && memchr(dummy, '\n', r)) break ; + } + fd_close(fd) ; + notify_systemd(pid, s) ; + return 0 ; +} + +int main (int argc, char const *const *argv) +{ + static gol_bool const rgolb[] = + { + { .so = 0, .lo = "doublefork", .clear = GOLB_SINGLEFORK, .set = 0 }, + { .so = 'f', .lo = "no-doublefork", .clear = 0, .set = GOLB_SINGLEFORK }, + { .so = 'k', .lo = "keep-environment", .clear = 0, .set = GOLB_KEEP }, + } ; + static gol_arg const rgola[] = + { + { .so = 'd', .lo = "notification-fd", .i = GOLA_NOTIF }, + { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT }, + } ; + char const *s = getenv(VAR) ; + unsigned int fd = 1 ; + unsigned int timeout = 0 ; + uint64_t wgolb = 0 ; + char const *wgola[GOLA_N] = { 0 } ; + unsigned int golc ; + PROG = "s6-notify-socket-from-fd" ; + + golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ; + argc -= golc ; argv += golc ; + if (!argc) dieusage() ; + + if (wgola[GOLA_NOTIF] && !uint0_scan(wgola[GOLA_NOTIF], &fd)) + strerr_dief2x(100, "notification-fd", " must be an unsigned integer") ; + if (wgola[GOLA_TIMEOUT] && !uint0_scan(wgola[GOLA_TIMEOUT], &timeout)) + strerr_dief2x(100, "timeout", " must be an unsigned integer") ; + + if (!s) xexec(argv) ; + else + { + pid_t parent = getpid() ; + pid_t child ; + int p[2] ; + if (pipe(p) == -1) strerr_diefu1sys(111, "pipe") ; + child = wgolb & GOLB_SINGLEFORK ? fork() : doublefork() ; + if (child == -1) strerr_diefu1sys(111, wgolb & GOLB_SINGLEFORK ? "fork" : "doublefork") ; + else if (!child) + { + PROG = "s6-notify-socket-from-fd (child)" ; + fd_close(p[1]) ; + return run_child(p[0], timeout, parent, s) ; + } + fd_close(p[0]) ; + if (fd_move(fd, p[1]) == -1) strerr_diefu1sys(111, "fd_move") ; + if (wgolb & GOLB_KEEP) xexec(argv) ; + else xmexec_n(argv, VAR, sizeof(VAR), 1) ; + } +} diff --git a/src/usertree/deps-lib/.gitkeep b/src/usertree/deps-lib/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/usertree/deps-lib/.gitkeep |
