diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2024-04-30 19:09:43 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2024-04-30 19:09:43 +0000 |
commit | 02926ee3447b1ea0d04b53b8fcb08d8b1a4deec5 (patch) | |
tree | f0cffb716e9ba61382795b460203df506e13ed4b /src/libenvexec/child_spawn.c | |
parent | 944a9d260a41b30f32730ccb12b3f5dafb507b7d (diff) | |
download | skalibs-02926ee3447b1ea0d04b53b8fcb08d8b1a4deec5.tar.xz |
Add mspawn functions to cspawn.h; move everything to libenvexec
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libenvexec/child_spawn.c')
-rw-r--r-- | src/libenvexec/child_spawn.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/libenvexec/child_spawn.c b/src/libenvexec/child_spawn.c new file mode 100644 index 0000000..164f1b2 --- /dev/null +++ b/src/libenvexec/child_spawn.c @@ -0,0 +1,61 @@ +/* ISC license. */ + +#include <string.h> +#include <unistd.h> + +#include <skalibs/types.h> +#include <skalibs/djbunix.h> +#include <skalibs/env.h> +#include <skalibs/cspawn.h> + +pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, size_t n) +{ + pid_t pid ; + cspawn_fileaction fa[2] = + { + [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 1 } } }, + [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 0 } } } + } ; + size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; + size_t envlen = env_len(envp) ; + size_t i = 0 ; + int p[n ? n : 1][2] ; + char const *newenv[envlen + 2] ; + char modifs[m + 1 + n * UINT_FMT] ; + + for (; i < n ; i++) + { + if (pipe(p[i]) == -1) goto errpi ; + if ((ndelay_on(p[i][i & 1]) == -1) || (coe(p[i][i & 1]) == -1)) goto errpip ; + } + + memcpy(modifs, SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=", sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR)) ; + for (i = 2 ; i < n ; i++) + { + m += uint_fmt(modifs + m, p[i][!(i & 1)]) ; + if (i+1 < n) modifs[m++] = ',' ; + } + modifs[m++] = 0 ; + env_mergen(newenv, envlen + 2, envp, envlen, modifs, m, 1) ; + if (n) fa[0].x.fd2[1] = p[0][1] ; + if (n >= 2) fa[1].x.fd2[1] = p[1][0] ; + pid = cspawn(prog, argv, newenv, CSPAWN_FLAGS_SIGBLOCKNONE, fa, n < 2 ? n : 2) ; + if (!pid) goto errpi ; + + for (i = 0 ; i < n ; i++) + { + fd_close(p[i][!(i & 1)]) ; + fds[i] = p[i][i & 1] ; + } + return pid ; + + errpip: + i++ ; + errpi: + while (i--) + { + fd_close(p[i][1]) ; + fd_close(p[i][0]) ; + } + return 0 ; +} |