commit 8e8d652a5461a4616c680ae96ffc5b1fc0b40188
parent 970bbeb622573a80381f0f32e6f287195182c0e0
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Tue, 12 Sep 2023 03:36:09 +0000
Defork s6-fghack
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat:
4 files changed, 26 insertions(+), 53 deletions(-)
diff --git a/doc/s6-fghack.html b/doc/s6-fghack.html
@@ -30,12 +30,13 @@ s6-fghack is an anti-backgrounding tool.
<ul>
<li> s6-fghack opens a lot of file descriptors (all writing to a single pipe). </li>
- <li> Then it forks and executes <em>prog...</em>. </li>
+ <li> Then it spawns <em>prog...</em> as a child. </li>
<li> If something gets written on one of those descriptors, it's a bug in <em>prog</em>.
s6-fghack then complains and exits 102. </li>
<li> Unless <em>prog...</em> goes out of its way to close descriptors it does not know about,
-s6-fghack is able to detect when <em>prog...</em> exits. It exits with the same exit
-code (or 111 if <em>prog...</em> has crashed). </li>
+s6-fghack is able to detect when <em>prog...</em> exits. It exits with an
+<a href="//skarnet.org/software/execline/exitcodes.html">approximation</a> of the same exit
+code. </li>
</ul>
<h2> Notes </h2>
diff --git a/package/deps.mak b/package/deps.mak
@@ -127,7 +127,7 @@ src/pipe-tools/s6-ftrig-wait.o src/pipe-tools/s6-ftrig-wait.lo: src/pipe-tools/s
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-notifyoncheck.o src/supervision/s6-notifyoncheck.lo: src/supervision/s6-notifyoncheck.c src/include/s6/s6.h
src/supervision/s6-permafailon.o src/supervision/s6-permafailon.lo: src/supervision/s6-permafailon.c src/include/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/supervise.h
+src/supervision/s6-supervise.o src/supervision/s6-supervise.lo: src/supervision/s6-supervise.c src/include/s6/config.h src/include/s6/ftrigw.h src/include/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/supervise.h
src/supervision/s6-svdt-clear.o src/supervision/s6-svdt-clear.lo: src/supervision/s6-svdt-clear.c src/include/s6/supervise.h
src/supervision/s6-svdt.o src/supervision/s6-svdt.lo: src/supervision/s6-svdt.c src/include/s6/supervise.h
@@ -175,7 +175,7 @@ s6-envdir: EXTRA_LIBS := -lskarnet
s6-envdir: src/daemontools-extras/s6-envdir.o
s6-envuidgid: EXTRA_LIBS := -lskarnet ${MAYBEPTHREAD_LIB}
s6-envuidgid: src/daemontools-extras/s6-envuidgid.o ${LIBNSSS}
-s6-fghack: EXTRA_LIBS := -lskarnet
+s6-fghack: EXTRA_LIBS := -lskarnet ${SPAWN_LIB}
s6-fghack: src/daemontools-extras/s6-fghack.o
s6-log: EXTRA_LIBS := -lskarnet ${SYSCLOCK_LIB}
s6-log: src/daemontools-extras/s6-log.o
diff --git a/src/daemontools-extras/deps-exe/s6-fghack b/src/daemontools-extras/deps-exe/s6-fghack
@@ -1 +1,2 @@
-lskarnet
+${SPAWN_LIB}
diff --git a/src/daemontools-extras/s6-fghack.c b/src/daemontools-extras/s6-fghack.c
@@ -7,63 +7,34 @@
#include <skalibs/strerr.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/djbunix.h>
-#include <skalibs/exec.h>
+#include <skalibs/cspawn.h>
#define USAGE "s6-fghack prog..."
-int main (int argc, char const *const *argv)
+#define N 30
+
+int main (int argc, char const *const *argv, char const *const *envp)
{
int p[2] ;
- int pcoe[2] ;
+ int fds[N] ;
pid_t pid ;
- char dummy ;
+ cspawn_fileaction fa = { .type = CSPAWN_FA_CLOSE } ;
+ char c ;
+
PROG = "s6-fghack" ;
if (argc < 2) strerr_dieusage(100, USAGE) ;
- if (pipe(p) < 0) strerr_diefu1sys(111, "create hackpipe") ;
- if (pipe(pcoe) < 0) strerr_diefu1sys(111, "create coepipe") ;
-
- switch (pid = fork())
- {
- case -1 : strerr_diefu1sys(111, "fork") ;
- case 0 :
- {
- int i = 0 ;
- close(p[0]) ;
- close(pcoe[0]) ;
- if (coe(pcoe[1]) < 0) _exit(111) ;
- for (; i < 30 ; i++) dup(p[1]) ; /* hack. gcc's warning is justified. */
- exec(argv+1) ;
- i = errno ;
- if (fd_write(pcoe[1], "", 1) < 1) _exit(111) ;
- _exit(i) ;
- }
- }
-
+ if (pipe(p) == -1) strerr_diefu1sys(111, "create hackpipe") ;
+ for (size_t i = 0 ; i < N ; i++)
+ fds[i] = dup(p[1]) ;
+ fa.x.fd = p[0] ;
+ pid = cspawn(argv[1], argv + 1, envp, 0, &fa, 1) ;
+ if (!pid) strerr_diefu2sys(111, "spawn ", argv[1]) ;
close(p[1]) ;
- close(pcoe[1]) ;
+ for (size_t i = 0 ; i < N ; i++) close(fds[i]) ;
- switch (fd_read(pcoe[0], &dummy, 1))
- {
- case -1 : strerr_diefu1sys(111, "read on coepipe") ;
- case 1 :
- {
- int wstat ;
- if (wait_pid(pid, &wstat) < 0) strerr_diefu1sys(111, "wait_pid") ;
- errno = WEXITSTATUS(wstat) ;
- strerr_dieexec(111, argv[1]) ;
- }
- }
-
- fd_close(pcoe[0]) ;
-
- p[1] = fd_read(p[0], &dummy, 1) ;
- if (p[1] < 0) strerr_diefu1sys(111, "read on hackpipe") ;
+ p[1] = fd_read(p[0], &c, 1) ;
+ if (p[1] == -1) strerr_diefu1sys(111, "read on hackpipe") ;
if (p[1]) strerr_dief2x(102, argv[1], " wrote on hackpipe") ;
-
- {
- int wstat ;
- if (wait_pid(pid, &wstat) < 0) strerr_diefu1sys(111, "wait_pid") ;
- if (WIFSIGNALED(wstat)) strerr_dief2x(111, argv[2], " crashed") ;
- return WEXITSTATUS(wstat) ;
- }
+ if (wait_pid(pid, &p[1]) < 0) strerr_diefu1sys(111, "wait_pid") ;
+ return wait_estatus(p[1]) ;
}