diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-09-11 19:47:26 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-09-11 19:47:26 +0000 |
commit | 170480a068841398b5538ae0598fbfda0e7789b5 (patch) | |
tree | a7eaecd34451c99725fed8f967cd35a083f601d3 /src | |
parent | 1b0fce052f0adf2e4f3da7762cd9f4885a5d9da8 (diff) | |
download | skalibs-170480a068841398b5538ae0598fbfda0e7789b5.tar.xz |
Add gcspawn
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/headers/signed-template | 4 | ||||
-rw-r--r-- | src/include/skalibs/cspawn.h | 5 | ||||
-rw-r--r-- | src/libstddjb/gcspawn.c | 48 |
3 files changed, 55 insertions, 2 deletions
diff --git a/src/headers/signed-template b/src/headers/signed-template index b67b2bd..a3b85eb 100644 --- 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 index fa5c934..061fbfb 100644 --- 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 new file mode 100644 index 0000000..7e9e602 --- /dev/null +++ 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) ; +} |