commit 170480a068841398b5538ae0598fbfda0e7789b5
parent 1b0fce052f0adf2e4f3da7762cd9f4885a5d9da8
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Mon, 11 Sep 2023 19:47:26 +0000
Add gcspawn
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat:
5 files changed, 69 insertions(+), 9 deletions(-)
diff --git a/doc/libstddjb/cspawn.html b/doc/libstddjb/cspawn.html
@@ -41,7 +41,7 @@ otherwise.
<h3> Primitive </h3>
<p>
-<code> pid_t cspawn (char const *file, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) </code> <br />
+<code> pid_t cspawn (char const *file, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) </code> <br>
Forks and execs a child as with <tt>exec_ae(file, argv, envp)</tt>.
Returns 0 if it fails, and the pid of the child if it succeeds.
Before execing, the following operations are performed in the child:
@@ -70,7 +70,7 @@ descriptor <tt>fa[i].x.openinfo.fd</tt> points to it. </li>
<h3> Higher level interfaces </h3>
<p>
-<code> pid_t child_spawn1_pipe (char const *file, char const *const *argv, char const *const *envp, int *fd, int w) </code> <br />
+<code> pid_t child_spawn1_pipe (char const *file, char const *const *argv, char const *const *envp, int *fd, int w) </code> <br>
Spawns <em>file</em> as with <tt>cspawn()</tt> with a <em>flags</em> value of CSPAWN_FLAGS_SIGBLOCKNONE;
a pipe is created between the child's
stdin (if <em>w</em> is 0) or stdout (if <em>w</em> is nonzero) and the parent.
@@ -78,14 +78,14 @@ The parent's end of the pipe will be stored in *<em>fd</em>.
</p>
<p>
-<code> pid_t child_spawn1_socket (char const *file, char const *const *argv, char const *const *envp, int *fd) </code> <br />
+<code> pid_t child_spawn1_socket (char const *file, char const *const *argv, char const *const *envp, int *fd) </code> <br>
Like <tt>child_spawn1</tt>, except that a socket, not a pipe is created between the parent
and the child. Both the child's stdin and stdout point to the socket; the parent has
its end of the socket available in *<em>fd</em>.
</p>
<p>
-<code> pid_t child_spawn2 (char const *file, char const *const *argv, char const *const *envp, int *fds) </code> <br />
+<code> pid_t child_spawn2 (char const *file, char const *const *argv, char const *const *envp, int *fds) </code> <br>
Two pipes are created between the
parent and the child. <em>fds</em> must point to an array of 2 ints: this
array is read as well as written by <tt>child_spawn2()</tt>. On function
@@ -97,7 +97,7 @@ descriptors for the pipes that read from / write to the child.
</p>
<p>
-<code> pid_t child_spawn3 (char const *file, char const *const *argv, char const *const *envp, int *fds) </code> <br />
+<code> pid_t child_spawn3 (char const *file, char const *const *argv, char const *const *envp, int *fds) </code> <br>
Three pipes are created between the
parent and the child. <em>fds</em> must point to an array of 3 ints: this
array is read as well as written by <tt>child_spawn2()</tt>. On function
@@ -109,9 +109,8 @@ the reading end of a pipe; the writing end is present in the child, and its numb
is available as the value of the <tt>SKALIBS_CHILD_SPAWN_FDS</tt> environment variable.
</p>
-
<p>
-<code> pid_t child_spawn (char const *file, char const *const *argv, char const *const *envp, int *fds, unsigned int nfds) </code> <br />
+<code> pid_t child_spawn (char const *file, char const *const *argv, char const *const *envp, int *fds, unsigned int nfds) </code> <br>
More generic spawning function. <em>fds</em> must point to an array of at least <em>nfds</em> ints;
file descriptors reading from or writing to the child will be stored there. The function returns
0 on failure or the pid of the child on success.
@@ -126,5 +125,12 @@ then any additional fds are available to it in the <tt>SKALIBS_CHILD_SPAWN_FDS</
variable as a comma-separated list of integers. </li>
</ul>
+<p>
+<code> pid_t gcspawn (char const *file, char const *const *argv, char const *const *envp, uint32_t flags, cspawn_fileaction const *fa, size_t n) </code> <br>
+Like <tt>cspawn</tt>, but <em>argv</em> will be running as a grandchild of the
+current process. The function forks once, invokes <tt>cspawn</tt> from the child,
+and the child exits after passing the grandchild's pid to the parent.
+</p>
+
</body>
</html>
diff --git a/package/deps.mak b/package/deps.mak
@@ -342,6 +342,7 @@ src/libstddjb/filecopy_suffix.o src/libstddjb/filecopy_suffix.lo: src/libstddjb/
src/libstddjb/filecopy_unsafe.o src/libstddjb/filecopy_unsafe.lo: src/libstddjb/filecopy_unsafe.c src/include/skalibs/djbunix.h
src/libstddjb/fmtscan_asc.o src/libstddjb/fmtscan_asc.lo: src/libstddjb/fmtscan_asc.c src/include/skalibs/fmtscan.h
src/libstddjb/fmtscan_num.o src/libstddjb/fmtscan_num.lo: src/libstddjb/fmtscan_num.c src/include/skalibs/fmtscan.h
+src/libstddjb/gcspawn.o src/libstddjb/gcspawn.lo: src/libstddjb/gcspawn.c src/include/skalibs/allreadwrite.h src/include/skalibs/cspawn.h src/include/skalibs/djbunix.h src/include/skalibs/types.h
src/libstddjb/genalloc_deepfree.o src/libstddjb/genalloc_deepfree.lo: src/libstddjb/genalloc_deepfree.c src/include/skalibs/genalloc.h src/include/skalibs/stralloc.h
src/libstddjb/genwrite_flush_bufalloc.o src/libstddjb/genwrite_flush_bufalloc.lo: src/libstddjb/genwrite_flush_bufalloc.c src/include/skalibs/bufalloc.h src/include/skalibs/genwrite.h
src/libstddjb/genwrite_flush_buffer.o src/libstddjb/genwrite_flush_buffer.lo: src/libstddjb/genwrite_flush_buffer.c src/include/skalibs/buffer.h src/include/skalibs/genwrite.h
diff --git a/src/headers/signed-template b/src/headers/signed-template
@@ -1,8 +1,8 @@
#define @TYPE@_PACK @BYTES@
#define @type@_pack uint@BITS@_pack
#define @type@_pack_big uint@BITS@_pack_big
-#define @type@_unpack uint@BITS@_unpack
-#define @type@_unpack_big uint@BITS@_unpack_big
+#define @type@_unpack(pack, p) uint@BITS@_unpack(pack, (uint@BITS@_t *)(p))
+#define @type@_unpack_big(pack, p) uint@BITS@_unpack_big(pack, (uint@BITS@_t *)(p))
#define @TYPE@_FMT (1+UINT@BITS@_FMT)
#define @type@_fmt int@BITS@_fmt
diff --git a/src/include/skalibs/cspawn.h b/src/include/skalibs/cspawn.h
@@ -85,4 +85,9 @@ extern pid_t child_spawn3 (char const *, char const *const *, char const *const
extern pid_t child_spawn (char const *, char const *const *, char const *const *, int *, size_t) ;
+
+ /* cspawn, but running as a grandchild. Uses one fork(). */
+
+extern pid_t gcspawn (char const *, char const *const *, char const *const *, uint16_t, cspawn_fileaction const *, size_t) ;
+
#endif
diff --git a/src/libstddjb/gcspawn.c b/src/libstddjb/gcspawn.c
@@ -0,0 +1,48 @@
+/* ISC license. */
+
+#include <sys/wait.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <skalibs/types.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/cspawn.h>
+
+pid_t gcspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n)
+{
+ pid_t pid = 0 ;
+ int wstat ;
+ int p[2] ;
+ char pack[PID_PACK] ;
+ if (pipecoe(p) == -1) return 0 ;
+ pid = fork() ;
+ switch (pid)
+ {
+ case -1:
+ {
+ fd_close(p[1]) ;
+ fd_close(p[0]) ;
+ return 0 ;
+ }
+ case 0:
+ {
+ fd_close(p[0]) ;
+ pid = cspawn(prog, argv, envp, flags, fa, n) ;
+ if (!pid) _exit(errno) ;
+ pid_pack_big(pack, pid) ;
+ _exit(fd_write(p[1], pack, PID_PACK) < PID_PACK ? errno : 0) ;
+ }
+ }
+ fd_close(p[1]) ;
+ if (fd_read(p[0], pack, PID_PACK) < PID_PACK) goto err ;
+ fd_close(p[0]) ;
+ wait_pid(pid, &wstat) ;
+ pid_unpack_big(pack, &pid) ;
+ return pid ;
+
+ err:
+ fd_close(p[0]) ;
+ wait_pid(pid, &wstat) ;
+ return (errno = WIFSIGNALED(wstat) ? EINTR : WEXITSTATUS(wstat), 0) ;
+}