From 02926ee3447b1ea0d04b53b8fcb08d8b1a4deec5 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Tue, 30 Apr 2024 19:09:43 +0000 Subject: Add mspawn functions to cspawn.h; move everything to libenvexec Signed-off-by: Laurent Bercot --- src/include/skalibs/cspawn.h | 55 ++++++ src/include/skalibs/env.h | 4 +- src/include/skalibs/stddjb.h | 2 +- src/libenvexec/child_spawn.c | 61 +++++++ src/libenvexec/child_spawn0.c | 8 + src/libenvexec/child_spawn1_internal.c | 27 +++ src/libenvexec/child_spawn1_pipe.c | 17 ++ src/libenvexec/child_spawn1_socket.c | 16 ++ src/libenvexec/child_spawn2.c | 39 +++++ src/libenvexec/child_spawn3.c | 55 ++++++ src/libenvexec/cspawn-internal.h | 10 ++ src/libenvexec/cspawn.c | 297 +++++++++++++++++++++++++++++++++ src/libenvexec/env_merge.c | 24 ++- src/libenvexec/env_mergen.c | 31 ---- src/libenvexec/gcspawn.c | 48 ++++++ src/libenvexec/gmspawn_afn.c | 11 ++ src/libenvexec/mspawn_af.c | 17 ++ src/libenvexec/mspawn_afm.c | 9 + src/libenvexec/mspawn_afn.c | 11 ++ src/libenvexec/xmspawn_af.c | 13 ++ src/libenvexec/xmspawn_afm.c | 13 ++ src/libenvexec/xmspawn_afn.c | 13 ++ src/libstddjb/child_spawn.c | 61 ------- src/libstddjb/child_spawn0.c | 8 - src/libstddjb/child_spawn1_internal.c | 27 --- src/libstddjb/child_spawn1_pipe.c | 17 -- src/libstddjb/child_spawn1_socket.c | 16 -- src/libstddjb/child_spawn2.c | 39 ----- src/libstddjb/child_spawn3.c | 55 ------ src/libstddjb/cspawn-internal.h | 10 -- src/libstddjb/cspawn.c | 297 --------------------------------- src/libstddjb/gcspawn.c | 48 ------ 32 files changed, 746 insertions(+), 613 deletions(-) create mode 100644 src/libenvexec/child_spawn.c create mode 100644 src/libenvexec/child_spawn0.c create mode 100644 src/libenvexec/child_spawn1_internal.c create mode 100644 src/libenvexec/child_spawn1_pipe.c create mode 100644 src/libenvexec/child_spawn1_socket.c create mode 100644 src/libenvexec/child_spawn2.c create mode 100644 src/libenvexec/child_spawn3.c create mode 100644 src/libenvexec/cspawn-internal.h create mode 100644 src/libenvexec/cspawn.c delete mode 100644 src/libenvexec/env_mergen.c create mode 100644 src/libenvexec/gcspawn.c create mode 100644 src/libenvexec/gmspawn_afn.c create mode 100644 src/libenvexec/mspawn_af.c create mode 100644 src/libenvexec/mspawn_afm.c create mode 100644 src/libenvexec/mspawn_afn.c create mode 100644 src/libenvexec/xmspawn_af.c create mode 100644 src/libenvexec/xmspawn_afm.c create mode 100644 src/libenvexec/xmspawn_afn.c delete mode 100644 src/libstddjb/child_spawn.c delete mode 100644 src/libstddjb/child_spawn0.c delete mode 100644 src/libstddjb/child_spawn1_internal.c delete mode 100644 src/libstddjb/child_spawn1_pipe.c delete mode 100644 src/libstddjb/child_spawn1_socket.c delete mode 100644 src/libstddjb/child_spawn2.c delete mode 100644 src/libstddjb/child_spawn3.c delete mode 100644 src/libstddjb/cspawn-internal.h delete mode 100644 src/libstddjb/cspawn.c delete mode 100644 src/libstddjb/gcspawn.c (limited to 'src') diff --git a/src/include/skalibs/cspawn.h b/src/include/skalibs/cspawn.h index 061fbfb..0d8dd50 100644 --- a/src/include/skalibs/cspawn.h +++ b/src/include/skalibs/cspawn.h @@ -90,4 +90,59 @@ extern pid_t child_spawn (char const *, char const *const *, char const *const * extern pid_t gcspawn (char const *, char const *const *, char const *const *, uint16_t, cspawn_fileaction const *, size_t) ; + + /* mexec (see skalibs/exec.h), but with cspawn instead */ + +extern int env_mspawn (char const *, char const *) ; + +extern pid_t mspawn_afn (char const *, char const *const *, char const *const *, size_t, char const *, size_t, size_t, uint16_t, cspawn_fileaction const *, size_t) ; +extern pid_t mspawn_afm (char const *, char const *const *, char const *const *, size_t, char const *, size_t, uint16_t, cspawn_fileaction const *, size_t) ; +extern pid_t mspawn_af (char const *, char const *const *, char const *const *, size_t, uint16_t, cspawn_fileaction const *, size_t) ; + +#define mspawn_aen(file, argv, envp, modif, modiflen, modifn, flags, fa, n) mspawn_afn(file, argv, envp, env_len(envp), modif, modiflen, modifn, flags, fa, n) +#define mspawn_aem(file, argv, envp, modif, modiflen, flags, fa, n) mspawn_afm(file, argv, envp, env_len(envp), modif, modiflen, flags, fa, n) +#define mspawn_ae(file, argv, envp, flags, fa, n) mspawn_af(file, argv, (envp), env_len(envp), flags, fa, n) + +#define mspawn_an(file, argv, modif, modiflen, modifn, flags, fa, n) mspawn_aen(file, argv, (char const *const *)environ, modif, modiflen, modifn, flags, fa, n) +#define mspawn_am(file, argv, modif, modiflen, flags, fa, n) mspawn_aem(file, argv, (char const *const *)environ, modif, modiflen, flaga, fa, n) +#define mspawn_a(file, argv, flags, fa, n) mspawn_ae(file, (argv), (char const *const *)environ, flags, fa, n) + +#define mspawn_fn(argv, envp, envlen, modif, modiflen, modifn, flags, fa, n) mspawn_afn((argv)[0], (argv), envp, envlen, modif, modiflen, modifn, flags, fa, n) +#define mspawn_fm(argv, envp, envlen, modif, modiflen, flags, fa, n) mspawn_afm((argv)[0], (argv), envp, envlen, modif, modiflen, flags, fa, n) +#define mspawn_f(argv, envp, envlen, flags, fa, n) mspawn_af((argv)[0], (argv), envp, envlen, flags, fa, n) + +#define mspawn_en(argv, envp, modif, modiflen, modifn, flags, fa, n) mspawn_aen((argv)[0], (argv), envp, modif, modiflen, modifn, flags, fa, n) +#define mspawn_em(argv, envp, modif, modiflen, flags, fa, n) mspawn_aem((argv)[0], (argv), envp, modif, modiflen, flags, fa, n) +#define mspawn_e(argv, envp, flags, fa, n) mspawn_ae((argv)[0], (argv), envp, flags, fa, n) + +#define mspawn_n(argv, modif, modiflen, modifn, flags, fa, n) mspawn_an((argv)[0], (argv), modif, modiflen, modifn, flags, fa, n) +#define mspawn_m(argv, modif, modiflen, flags, fa, n) mspawn_am((argv)[0], (argv), modif, modiflen, flags, fa, n) +#define mspawn(argv, flags, fa, n) mspawn_a((argv)[0], (argv), flags, fa, n) + +extern pid_t xmspawn_afn (char const *, char const *const *, char const *const *, size_t, char const *, size_t, size_t, uint16_t, cspawn_fileaction const *, size_t) ; +extern pid_t xmspawn_afm (char const *, char const *const *, char const *const *, size_t, char const *, size_t, uint16_t, cspawn_fileaction const *, size_t) ; +extern pid_t xmspawn_af (char const *, char const *const *, char const *const *, size_t, uint16_t, cspawn_fileaction const *, size_t) ; + +#define xmspawn_aen(file, argv, envp, modif, modiflen, modifn, flags, fa, n) xmspawn_afn(file, argv, envp, env_len(envp), modif, modiflen, modifn, flags, fa, n) +#define xmspawn_aem(file, argv, envp, modif, modiflen, flags, fa, n) xmspawn_afm(file, argv, envp, env_len(envp), modif, modiflen, flags, fa, n) +#define xmspawn_ae(file, argv, envp, flags, fa, n) xmspawn_af(file, argv, envp, env_len(envp), flags, fa, n) + +#define xmspawn_an(file, argv, modif, modiflen, modifn, flags, fa, n) xmspawn_aen(file, argv, (char const *const *)environ, modif, modiflen, modifn, flags, fa, n) +#define xmspawn_am(file, argv, modif, modiflen, flags, fa, n) xmspawn_aem(file, argv, (char const *const *)environ, modif, modiflen, flags, fa, n) +#define xmspawn_a(file, argv, flags, fa, n) xmspawn_ae(file, argv, (char const *const *)environ, flags, fa, n) + +#define xmspawn_fn(argv, envp, envlen, modif, modiflen, modifn, flags, fa, n) xmspawn_afn((argv)[0], (argv), envp, envlen, modif, modiflen, modifn, flags, fa, n) +#define xmspawn_fm(argv, envp, envlen, modif, modiflen, flags, fa, n) xmspawn_afm((argv)[0], (argv), envp, envlen, modif, modiflen, flags, fa, n) +#define xmspawn_f(argv, envp, envlen, flags, fa, n) xmspawn_af((argv)[0], (argv), envp, envlen, flags, fa, n) + +#define xmspawn_en(argv, envp, modif, modiflen, modifn, flags, fa, n) xmspawn_aen((argv)[0], (argv), envp, modif, modiflen, modifn, flags, fa, n) +#define xmspawn_em(argv, envp, modif, modiflen, flags, fa, n) xmspawn_aem((argv)[0], (argv), envp, modif, modiflen, flags, fa, n) +#define xmspawn_e(argv, envp, flags, fa, n) xmspawn_ae((argv)[0], (argv), envp, flags, fa, n) + +#define xmspawn_n(argv, modif, modiflen, modifn, flags, fa, n) xmspawn_an((argv)[0], (argv), modif, modiflen, modifn, flags, fa, n) +#define xmspawn_m(argv, modif, modiflen, flags, fa, n) xmspawn_am((argv)[0], (argv), modif, modiflen, flags, fa, n) +#define xmspawn(argv, flags, fa, n) xmspawn_a((argv)[0], (argv), flags, fa, n) + +extern pid_t gmspawn_afn (char const *, char const *const *, char const *const *, size_t, char const *, size_t, size_t, uint16_t, cspawn_fileaction const *, size_t) ; + #endif diff --git a/src/include/skalibs/env.h b/src/include/skalibs/env.h index 5e17dfa..e5dfd1d 100644 --- a/src/include/skalibs/env.h +++ b/src/include/skalibs/env.h @@ -17,10 +17,10 @@ extern int env_addmodif (stralloc *, char const *, char const *) ; extern int env_make (char const **, size_t, char const *, size_t) ; extern int env_string (stralloc *, char const *const *, size_t) ; -extern size_t env_mergen (char const **, size_t, char const *const *, size_t, char const *, size_t, size_t) ; +extern size_t env_merg (char const **, size_t, char const *const *, char const *, size_t) ; extern size_t env_merge (char const **, size_t, char const *const *, size_t, char const *, size_t) ; +#define env_mergen(v, vmax, envp, envlen, modifs, modiflen, modifn) env_merge(v, vmax, envp, envlen, modifs, modiflen) extern size_t env_mergn (char const **, size_t, char const *const *, char const *, size_t, size_t) ; -extern size_t env_merg (char const **, size_t, char const *const *, char const *, size_t) ; #define SKALIBS_ENVDIR_VERBATIM 0x01 #define SKALIBS_ENVDIR_NOCHOMP 0x02 diff --git a/src/include/skalibs/stddjb.h b/src/include/skalibs/stddjb.h index d241de8..ab5bcaf 100644 --- a/src/include/skalibs/stddjb.h +++ b/src/include/skalibs/stddjb.h @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -31,6 +30,7 @@ #include #include #include +#include #include #include #include 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 +#include + +#include +#include +#include +#include + +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 ; +} diff --git a/src/libenvexec/child_spawn0.c b/src/libenvexec/child_spawn0.c new file mode 100644 index 0000000..b1353b6 --- /dev/null +++ b/src/libenvexec/child_spawn0.c @@ -0,0 +1,8 @@ +/* ISC license. */ + +#include + +pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp) +{ + return cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, 0, 0) ; +} diff --git a/src/libenvexec/child_spawn1_internal.c b/src/libenvexec/child_spawn1_internal.c new file mode 100644 index 0000000..e65b588 --- /dev/null +++ b/src/libenvexec/child_spawn1_internal.c @@ -0,0 +1,27 @@ +/* ISC license. */ + +#include +#include +#include "cspawn-internal.h" + +pid_t child_spawn1_internal (char const *prog, char const *const *argv, char const *const *envp, int *p, int to) +{ + pid_t pid ; + cspawn_fileaction fa[3] = + { + [0] = { .type = CSPAWN_FA_CLOSE, .x = { .fd = p[!(to & 1)]} }, + [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = to & 1, [1] = p[to & 1] } } }, + [2] = { .type = CSPAWN_FA_COPY, .x = { .fd2 = { [0] = !(to & 1), [1] = to & 1 } } } + } ; + + pid = cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2 + !!(to & 2)) ; + if (!pid) goto err ; + + fd_close(p[to & 1]) ; + return pid ; + + err: + fd_close(p[1]) ; + fd_close(p[0]) ; + return 0 ; +} diff --git a/src/libenvexec/child_spawn1_pipe.c b/src/libenvexec/child_spawn1_pipe.c new file mode 100644 index 0000000..a31e091 --- /dev/null +++ b/src/libenvexec/child_spawn1_pipe.c @@ -0,0 +1,17 @@ +/* ISC license. */ + +#include + +#include +#include "cspawn-internal.h" + +pid_t child_spawn1_pipe (char const *prog, char const *const *argv, char const *const *envp, int *fd, int to) +{ + pid_t pid ; + int p[2] ; + if (pipe(p) < 0) return 0 ; + pid = child_spawn1_internal(prog, argv, envp, p, !!to) ; + if (!pid) return 0 ; + *fd = p[!to] ; + return pid ; +} diff --git a/src/libenvexec/child_spawn1_socket.c b/src/libenvexec/child_spawn1_socket.c new file mode 100644 index 0000000..684842e --- /dev/null +++ b/src/libenvexec/child_spawn1_socket.c @@ -0,0 +1,16 @@ +/* ISC license. */ + +#include +#include +#include "cspawn-internal.h" + +pid_t child_spawn1_socket (char const *prog, char const *const *argv, char const *const *envp, int *fd) +{ + pid_t pid ; + int p[2] ; + if (ipc_pair_b(p) < 0) return 0 ; + pid = child_spawn1_internal(prog, argv, envp, p, 3) ; + if (!pid) return 0 ; + *fd = p[0] ; + return pid ; +} diff --git a/src/libenvexec/child_spawn2.c b/src/libenvexec/child_spawn2.c new file mode 100644 index 0000000..f287c0b --- /dev/null +++ b/src/libenvexec/child_spawn2.c @@ -0,0 +1,39 @@ +/* ISC license. */ + +#include + +#include +#include + +pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds) +{ + pid_t pid ; + int p[2][2] ; + if (pipe(p[0]) == -1) return 0 ; + if (ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1 || pipe(p[1]) == -1) goto errp ; + if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1) goto err ; + + { + cspawn_fileaction fa[2] = + { + [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[0], [1] = p[1][0] } } }, + [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[1], [1] = p[0][1] } } } + } ; + pid = cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2) ; + if (!pid) goto err ; + } + + fd_close(p[0][1]) ; + fd_close(p[1][0]) ; + fds[0] = p[0][0] ; + fds[1] = p[1][1] ; + return pid ; + + err: + fd_close(p[1][1]) ; + fd_close(p[1][0]) ; + errp: + fd_close(p[0][1]) ; + fd_close(p[0][0]) ; + return 0 ; +} diff --git a/src/libenvexec/child_spawn3.c b/src/libenvexec/child_spawn3.c new file mode 100644 index 0000000..75551d2 --- /dev/null +++ b/src/libenvexec/child_spawn3.c @@ -0,0 +1,55 @@ +/* ISC license. */ + +#include + +#include +#include +#include +#include + +pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const *envp, int *fds) +{ + pid_t pid ; + int p[3][2] ; + + if (pipe(p[0]) == -1) return 0 ; + if (ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1 || pipe(p[1]) == -1) goto errp0 ; + if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1 || pipe(p[2]) == -1) goto errp1 ; + if (ndelay_on(p[2][0]) == -1 || coe(p[2][0]) == -1) goto err ; + + { + cspawn_fileaction fa[2] = + { + [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[0], [1] = p[1][0] } } }, + [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[1], [1] = p[0][1] } } } + } ; + size_t envlen = env_len(envp) ; + size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; + char modifs[sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) + UINT_FMT] = SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=" ; + char const *newenv[envlen + 2] ; + m += uint_fmt(modifs + sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR), p[2][1]) ; + modifs[m++] = 0 ; + env_mergen(newenv, envlen + 2, envp, envlen, modifs, m, 1) ; + pid = cspawn(prog, argv, newenv, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2) ; + if (!pid) goto err ; + } + + fd_close(p[2][1]) ; + fd_close(p[1][0]) ; + fd_close(p[0][1]) ; + fds[0] = p[0][0] ; + fds[1] = p[1][1] ; + fds[2] = p[2][0] ; + return pid ; + + err: + fd_close(p[2][1]) ; + fd_close(p[2][0]) ; + errp1: + fd_close(p[1][1]) ; + fd_close(p[1][0]) ; + errp0: + fd_close(p[0][1]) ; + fd_close(p[0][0]) ; + return 0 ; +} diff --git a/src/libenvexec/cspawn-internal.h b/src/libenvexec/cspawn-internal.h new file mode 100644 index 0000000..0e5efc7 --- /dev/null +++ b/src/libenvexec/cspawn-internal.h @@ -0,0 +1,10 @@ +/* ISC license. */ + +#ifndef CSPAWN_INTERNAL_H +#define CSPAWN_INTERNAL_H + +#include + +extern pid_t child_spawn1_internal (char const *, char const *const *, char const *const *, int *, int) ; + +#endif diff --git a/src/libenvexec/cspawn.c b/src/libenvexec/cspawn.c new file mode 100644 index 0000000..683a0a3 --- /dev/null +++ b/src/libenvexec/cspawn.c @@ -0,0 +1,297 @@ +/* ISC license. */ + +#include + +#ifdef SKALIBS_HASPOSIXSPAWN +#include +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include + +static inline void cspawn_child_exec (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + for (size_t i = 0 ; i < n ; i++) + { + switch (fa[i].type) + { + case CSPAWN_FA_CLOSE : fd_close(fa[i].x.fd) ; break ; + case CSPAWN_FA_COPY : + if (fd_copy(fa[i].x.fd2[0], fa[i].x.fd2[1]) == -1) return ; + break ; + case CSPAWN_FA_MOVE : + if (fd_move(fa[i].x.fd2[0], fa[i].x.fd2[1]) == -1) return ; + if (fa[i].x.fd2[0] == fa[i].x.fd2[1] && uncoe(fa[i].x.fd2[0]) == -1) return ; + break ; + case CSPAWN_FA_OPEN : + { + int fd = open3(fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ; + if (fd == -1) return ; + if (fd_move(fa[i].x.openinfo.fd, fd) == -1) return ; + break ; + } + case CSPAWN_FA_CHDIR : + if (chdir(fa[i].x.path) == -1) return ; + break ; + case CSPAWN_FA_FCHDIR : + if (fchdir(fa[i].x.fd) == -1) return ; + break ; + default : + errno = EINVAL ; return ; + } + } + + if (flags & CSPAWN_FLAGS_SELFPIPE_FINISH) selfpipe_finish() ; + if (flags & CSPAWN_FLAGS_SIGBLOCKNONE) sig_blocknone() ; + if (flags & CSPAWN_FLAGS_SETSID) setsid() ; + + exec_ae(prog, argv, envp) ; +} + +static inline pid_t cspawn_fork (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + pid_t pid ; + int p[2] ; + char c ; + + if (pipecoe(p) == -1) return 0 ; + pid = fork() ; + if (pid == -1) + { + fd_close(p[1]) ; + fd_close(p[0]) ; + return 0 ; + } + if (!pid) + { + cspawn_child_exec(prog, argv, envp, flags, fa, n) ; + c = errno ; + fd_write(p[1], &c, 1) ; + _exit(127) ; + } + + fd_close(p[1]) ; + p[1] = fd_read(p[0], &c, 1) ; + if (p[1] < 0) + { + fd_close(p[0]) ; + return 0 ; + } + fd_close(p[0]) ; + if (p[1]) + { + wait_pid(pid, &p[0]) ; + errno = (unsigned char)c ; + return 0 ; + } + return pid ; +} + + /* + guess who has a buggy posix_spawn() *and* doesn't have waitid() to work around it? + if you guessed OpenBSD, you're right! + */ + +#if defined(SKALIBS_HASPOSIXSPAWN) && (!defined(SKALIBS_HASPOSIXSPAWNEARLYRETURN) || defined(SKALIBS_HASWAITID)) + +#include +#include +#include + +#include +#include + +#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN + +#include + +static inline pid_t cspawn_workaround (pid_t pid, int const *p) +{ + siginfo_t si ; + int e ; + ssize_t r ; + char c ; + + fd_close(p[1]) ; + r = fd_read(p[0], &c, 1) ; + fd_close(p[0]) ; + if (r == -1) return 0 ; + if (r) return (errno = EILSEQ, 0) ; /* child wrote, wtf */ + + do e = waitid(P_PID, pid, &si, WEXITED | WNOHANG | WNOWAIT) ; + while (e == -1 && errno == EINTR) ; + if (e == -1) return pid ; /* we're in trouble, but don't leak a child */ + if (!si.si_pid) return pid ; /* child is running */ + if (si.si_code != CLD_EXITED || si.si_status != 127) return pid ; /* child died after execve(), let caller handle it */ + /* + child exited 127, so either execve() failed, which is what we want to catch, + or it raced like a mofo, execve()d and then exited 127 on its own, in which + case, tough luck, it never existed. + */ + wait_pid(pid, 0) ; + return (errno = 0, 0) ; +} + +#endif + +static inline pid_t cspawn_pspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + pid_t pid ; + posix_spawnattr_t attr ; + posix_spawn_file_actions_t actions ; + int e ; + int nopath = !getenv("PATH") ; +#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN + int p[2] ; + if (pipecoe(p) == -1) return 0 ; +#endif + + if (flags) + { + short pfff = 0 ; + e = posix_spawnattr_init(&attr) ; + if (e) goto err ; + if (flags & (CSPAWN_FLAGS_SIGBLOCKNONE | CSPAWN_FLAGS_SELFPIPE_FINISH)) + { + sigset_t set ; + sigemptyset(&set) ; + e = posix_spawnattr_setsigmask(&attr, &set) ; + if (e) goto errattr ; + pfff |= POSIX_SPAWN_SETSIGMASK ; + } +#ifdef SKALIBS_HASPOSIXSPAWNSETSID + if (flags & CSPAWN_FLAGS_SETSID) pfff |= POSIX_SPAWN_SETSID ; +#else +#ifdef SKALIBS_HASPOSIXSPAWNSETSIDNP + if (flags & CSPAWN_FLAGS_SETSID) pfff |= POSIX_SPAWN_SETSID_NP ; +#endif +#endif + e = posix_spawnattr_setflags(&attr, pfff) ; + if (e) goto errattr ; + } + + if (n) + { + e = posix_spawn_file_actions_init(&actions) ; + if (e) goto errattr ; + for (size_t i = 0 ; i < n ; i++) + { + switch (fa[i].type) + { + case CSPAWN_FA_CLOSE : + e = posix_spawn_file_actions_addclose(&actions, fa[i].x.fd) ; + if (e) goto erractions ; + break ; + case CSPAWN_FA_COPY : + e = posix_spawn_file_actions_adddup2(&actions, fa[i].x.fd2[1], fa[i].x.fd2[0]) ; + if (e) goto erractions ; + break ; + case CSPAWN_FA_MOVE : + e = posix_spawn_file_actions_adddup2(&actions, fa[i].x.fd2[1], fa[i].x.fd2[0]) ; + if (e) goto erractions ; + if (fa[i].x.fd2[0] != fa[i].x.fd2[1]) + { + e = posix_spawn_file_actions_addclose(&actions, fa[i].x.fd2[1]) ; + if (e) goto erractions ; + } + break ; + case CSPAWN_FA_OPEN : + e = posix_spawn_file_actions_addopen(&actions, fa[i].x.openinfo.fd, fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ; + if (e) goto erractions ; + break ; +#ifdef SKALIBS_HASPOSIXSPAWNCHDIR + case CSPAWN_FA_CHDIR : + e = posix_spawn_file_actions_addchdir(&actions, fa[i].x.path) ; + if (e) goto erractions ; + break ; + case CSPAWN_FA_FCHDIR : + e = posix_spawn_file_actions_addfchdir(&actions, fa[i].x.fd) ; + if (e) goto erractions ; + break ; +#else +#ifdef SKALIBS_HASPOSIXSPAWNCHDIRNP + case CSPAWN_FA_CHDIR : + e = posix_spawn_file_actions_addchdir_np(&actions, fa[i].x.path) ; + if (e) goto erractions ; + break ; + case CSPAWN_FA_FCHDIR : + e = posix_spawn_file_actions_addfchdir_np(&actions, fa[i].x.fd) ; + if (e) goto erractions ; + break ; +#endif +#endif + default : + e = EINVAL ; + goto erractions ; + } + } + } + + if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) == -1)) { e = errno ; goto erractions ; } + e = posix_spawnp(&pid, prog, n ? &actions : 0, flags ? &attr : 0, (char *const *)argv, (char *const *)envp) ; + if (nopath) unsetenv("PATH") ; + if (e) goto erractions ; + + if (n) posix_spawn_file_actions_destroy(&actions) ; + if (flags) posix_spawnattr_destroy(&attr) ; +#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN + return cspawn_workaround(pid, p) ; +#else + return pid ; +#endif + + erractions: + if (n) posix_spawn_file_actions_destroy(&actions) ; + errattr: + if (flags) posix_spawnattr_destroy(&attr) ; + err: +#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN + fd_close(p[1]) ; + fd_close(p[0]) ; +#endif + errno = e ; + return 0 ; +} + +#if (defined(SKALIBS_HASPOSIXSPAWNSETSID) || defined(SKALIBS_HASPOSIXSPAWNSETSIDNP)) && (defined(SKALIBS_HASPOSIXSPAWNCHDIR) || defined(SKALIBS_HASPOSIXSPAWNCHDIRNP)) + +pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + return cspawn_pspawn(prog, argv, envp, flags, fa, n) ; +} + +#else + +pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ +#if !defined(SKALIBS_HASPOSIXSPAWNSETSID) && !defined(SKALIBS_HASPOSIXSPAWNSETSIDNP) + if (flags & CSPAWN_FLAGS_SETSID) goto dofork ; +#endif +#if !defined(SKALIBS_HASPOSIXSPAWNCHDIR) && !defined(SKALIBS_HASPOSIXSPAWNCHDIRNP) + for (size_t i = 0 ; i < n ; i++) + if (fa[i].type == CSPAWN_FA_CHDIR || fa[i].type == CSPAWN_FA_FCHDIR) + goto dofork ; +#endif + return cspawn_pspawn(prog, argv, envp, flags, fa, n) ; + + dofork: + return cspawn_fork(prog, argv, envp, flags, fa, n) ; +} + +#endif + +#else + +pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + return cspawn_fork(prog, argv, envp, flags, fa, n) ; +} + +#endif diff --git a/src/libenvexec/env_merge.c b/src/libenvexec/env_merge.c index ac3e17c..b6281ee 100644 --- a/src/libenvexec/env_merge.c +++ b/src/libenvexec/env_merge.c @@ -1,9 +1,31 @@ /* ISC license. */ +#include +#include + #include #include size_t env_merge (char const **v, size_t vmax, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen) { - return env_mergen(v, vmax, envp, envlen, modifs, modiflen, byte_count(modifs, modiflen, '\0')) ; + size_t vlen = envlen ; + size_t i = 0 ; + if (envlen >= vmax) return 0 ; + for (; i < envlen ; i++) v[i] = envp[i] ; + for (i = 0 ; i < modiflen ; i += strlen(modifs + i) + 1) + { + size_t split = str_chr(modifs + i, '=') ; + size_t j = 0 ; + for (; j < vlen ; j++) + if (!strncmp(modifs + i, v[j], split) && (v[j][split] == '=')) break ; + if (j < vlen) v[j] = v[--vlen] ; + if (modifs[i + split]) + { + if (vlen >= vmax) return 0 ; + v[vlen++] = modifs + i ; + } + } + if (vlen >= vmax) return 0 ; + v[vlen++] = 0 ; + return vlen ; } diff --git a/src/libenvexec/env_mergen.c b/src/libenvexec/env_mergen.c deleted file mode 100644 index 64a77cb..0000000 --- a/src/libenvexec/env_mergen.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ISC license. */ - -#include -#include - -#include -#include - -size_t env_mergen (char const **v, size_t vmax, char const *const *envp, size_t envlen, char const *modifs, size_t modiflen, size_t n) -{ - size_t vlen = envlen ; - size_t i = 0 ; - if (envlen >= vmax) return 0 ; - for (; i < envlen ; i++) v[i] = envp[i] ; - for (i = 0 ; i < modiflen ; i += strlen(modifs + i) + 1) - { - size_t split = str_chr(modifs + i, '=') ; - size_t j = 0 ; - for (; j < vlen ; j++) - if (!strncmp(modifs + i, v[j], split) && (v[j][split] == '=')) break ; - if (j < vlen) v[j] = v[--vlen] ; - if (modifs[i + split]) - { - if (vlen >= vmax) return 0 ; - v[vlen++] = modifs + i ; - } - } - if (vlen >= vmax) return 0 ; - v[vlen++] = 0 ; - return vlen ; -} diff --git a/src/libenvexec/gcspawn.c b/src/libenvexec/gcspawn.c new file mode 100644 index 0000000..7e9e602 --- /dev/null +++ b/src/libenvexec/gcspawn.c @@ -0,0 +1,48 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include +#include +#include + +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) ; +} diff --git a/src/libenvexec/gmspawn_afn.c b/src/libenvexec/gmspawn_afn.c new file mode 100644 index 0000000..dc49017 --- /dev/null +++ b/src/libenvexec/gmspawn_afn.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#include +#include + +pid_t gmspawn_afn (char const *file, char const *const *argv, char const *const *envp, size_t envlen, char const *modif, size_t modiflen, size_t modifn, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + char const *newenvp[envlen + modifn + 1] ; + env_merge(newenvp, envlen + modifn + 1, envp, envlen, modif, modiflen) ; + return gcspawn(file, argv, newenvp, flags, fa, n) ; +} diff --git a/src/libenvexec/mspawn_af.c b/src/libenvexec/mspawn_af.c new file mode 100644 index 0000000..97dc877 --- /dev/null +++ b/src/libenvexec/mspawn_af.c @@ -0,0 +1,17 @@ +/* ISC license. */ + +#include +#include +#include + +static stralloc modifsa = STRALLOC_ZERO ; + +int env_mspawn (char const *key, char const *value) +{ + return env_addmodif(&modifsa, key, value) ; +} + +pid_t mspawn_af (char const *file, char const *const *argv, char const *const *envp, size_t envlen, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + return mspawn_afm(file, argv, envp, envlen, modifsa.s, modifsa.len, flags, fa, n) ; +} diff --git a/src/libenvexec/mspawn_afm.c b/src/libenvexec/mspawn_afm.c new file mode 100644 index 0000000..4b3711e --- /dev/null +++ b/src/libenvexec/mspawn_afm.c @@ -0,0 +1,9 @@ +/* ISC license. */ + +#include +#include + +pid_t mspawn_afm (char const *file, char const *const *argv, char const *const *envp, size_t envlen, char const *modif, size_t modiflen, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + return mspawn_afn(file, argv, envp, envlen, modif, modiflen, byte_count(modif, modiflen, '\0'), flags, fa, n) ; +} diff --git a/src/libenvexec/mspawn_afn.c b/src/libenvexec/mspawn_afn.c new file mode 100644 index 0000000..fe36fca --- /dev/null +++ b/src/libenvexec/mspawn_afn.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#include +#include + +pid_t mspawn_afn (char const *file, char const *const *argv, char const *const *envp, size_t envlen, char const *modif, size_t modiflen, size_t modifn, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + char const *newenvp[envlen + modifn + 1] ; + env_merge(newenvp, envlen + modifn + 1, envp, envlen, modif, modiflen) ; + return cspawn(file, argv, newenvp, flags, fa, n) ; +} diff --git a/src/libenvexec/xmspawn_af.c b/src/libenvexec/xmspawn_af.c new file mode 100644 index 0000000..3e79114 --- /dev/null +++ b/src/libenvexec/xmspawn_af.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include + +#include +#include + +pid_t xmspawn_af (char const *file, char const *const *argv, char const *const *envp, size_t envlen, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + pid_t pid = mspawn_af(file, argv, envp, envlen, flags, fa, n) ; + if (!pid) strerr_diefu2sys(errno == ENOENT ? 127 : 126, "spawn ", file) ; + return pid ; +} diff --git a/src/libenvexec/xmspawn_afm.c b/src/libenvexec/xmspawn_afm.c new file mode 100644 index 0000000..5335fb1 --- /dev/null +++ b/src/libenvexec/xmspawn_afm.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include + +#include +#include + +pid_t xmspawn_afm (char const *file, char const *const *argv, char const *const *envp, size_t envlen, char const *modif, size_t modiflen, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + pid_t pid = mspawn_afm(file, argv, envp, envlen, modif, modiflen, flags, fa, n) ; + if (!pid) strerr_diefu2sys(errno == ENOENT ? 127 : 126, "spawn ", file) ; + return pid ; +} diff --git a/src/libenvexec/xmspawn_afn.c b/src/libenvexec/xmspawn_afn.c new file mode 100644 index 0000000..5af7c2e --- /dev/null +++ b/src/libenvexec/xmspawn_afn.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include + +#include +#include + +pid_t xmspawn_afn (char const *file, char const *const *argv, char const *const *envp, size_t envlen, char const *modif, size_t modiflen, size_t modifn, uint16_t flags, cspawn_fileaction const *fa, size_t n) +{ + pid_t pid = mspawn_afn(file, argv, envp, envlen, modif, modiflen, modifn, flags, fa, n) ; + if (!pid) strerr_diefu2sys(errno == ENOENT ? 127 : 126, "spawn ", file) ; + return pid ; +} diff --git a/src/libstddjb/child_spawn.c b/src/libstddjb/child_spawn.c deleted file mode 100644 index 164f1b2..0000000 --- a/src/libstddjb/child_spawn.c +++ /dev/null @@ -1,61 +0,0 @@ -/* ISC license. */ - -#include -#include - -#include -#include -#include -#include - -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 ; -} diff --git a/src/libstddjb/child_spawn0.c b/src/libstddjb/child_spawn0.c deleted file mode 100644 index b1353b6..0000000 --- a/src/libstddjb/child_spawn0.c +++ /dev/null @@ -1,8 +0,0 @@ -/* ISC license. */ - -#include - -pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp) -{ - return cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, 0, 0) ; -} diff --git a/src/libstddjb/child_spawn1_internal.c b/src/libstddjb/child_spawn1_internal.c deleted file mode 100644 index e65b588..0000000 --- a/src/libstddjb/child_spawn1_internal.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ISC license. */ - -#include -#include -#include "cspawn-internal.h" - -pid_t child_spawn1_internal (char const *prog, char const *const *argv, char const *const *envp, int *p, int to) -{ - pid_t pid ; - cspawn_fileaction fa[3] = - { - [0] = { .type = CSPAWN_FA_CLOSE, .x = { .fd = p[!(to & 1)]} }, - [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = to & 1, [1] = p[to & 1] } } }, - [2] = { .type = CSPAWN_FA_COPY, .x = { .fd2 = { [0] = !(to & 1), [1] = to & 1 } } } - } ; - - pid = cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2 + !!(to & 2)) ; - if (!pid) goto err ; - - fd_close(p[to & 1]) ; - return pid ; - - err: - fd_close(p[1]) ; - fd_close(p[0]) ; - return 0 ; -} diff --git a/src/libstddjb/child_spawn1_pipe.c b/src/libstddjb/child_spawn1_pipe.c deleted file mode 100644 index a31e091..0000000 --- a/src/libstddjb/child_spawn1_pipe.c +++ /dev/null @@ -1,17 +0,0 @@ -/* ISC license. */ - -#include - -#include -#include "cspawn-internal.h" - -pid_t child_spawn1_pipe (char const *prog, char const *const *argv, char const *const *envp, int *fd, int to) -{ - pid_t pid ; - int p[2] ; - if (pipe(p) < 0) return 0 ; - pid = child_spawn1_internal(prog, argv, envp, p, !!to) ; - if (!pid) return 0 ; - *fd = p[!to] ; - return pid ; -} diff --git a/src/libstddjb/child_spawn1_socket.c b/src/libstddjb/child_spawn1_socket.c deleted file mode 100644 index 684842e..0000000 --- a/src/libstddjb/child_spawn1_socket.c +++ /dev/null @@ -1,16 +0,0 @@ -/* ISC license. */ - -#include -#include -#include "cspawn-internal.h" - -pid_t child_spawn1_socket (char const *prog, char const *const *argv, char const *const *envp, int *fd) -{ - pid_t pid ; - int p[2] ; - if (ipc_pair_b(p) < 0) return 0 ; - pid = child_spawn1_internal(prog, argv, envp, p, 3) ; - if (!pid) return 0 ; - *fd = p[0] ; - return pid ; -} diff --git a/src/libstddjb/child_spawn2.c b/src/libstddjb/child_spawn2.c deleted file mode 100644 index f287c0b..0000000 --- a/src/libstddjb/child_spawn2.c +++ /dev/null @@ -1,39 +0,0 @@ -/* ISC license. */ - -#include - -#include -#include - -pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds) -{ - pid_t pid ; - int p[2][2] ; - if (pipe(p[0]) == -1) return 0 ; - if (ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1 || pipe(p[1]) == -1) goto errp ; - if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1) goto err ; - - { - cspawn_fileaction fa[2] = - { - [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[0], [1] = p[1][0] } } }, - [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[1], [1] = p[0][1] } } } - } ; - pid = cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2) ; - if (!pid) goto err ; - } - - fd_close(p[0][1]) ; - fd_close(p[1][0]) ; - fds[0] = p[0][0] ; - fds[1] = p[1][1] ; - return pid ; - - err: - fd_close(p[1][1]) ; - fd_close(p[1][0]) ; - errp: - fd_close(p[0][1]) ; - fd_close(p[0][0]) ; - return 0 ; -} diff --git a/src/libstddjb/child_spawn3.c b/src/libstddjb/child_spawn3.c deleted file mode 100644 index 75551d2..0000000 --- a/src/libstddjb/child_spawn3.c +++ /dev/null @@ -1,55 +0,0 @@ -/* ISC license. */ - -#include - -#include -#include -#include -#include - -pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const *envp, int *fds) -{ - pid_t pid ; - int p[3][2] ; - - if (pipe(p[0]) == -1) return 0 ; - if (ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1 || pipe(p[1]) == -1) goto errp0 ; - if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1 || pipe(p[2]) == -1) goto errp1 ; - if (ndelay_on(p[2][0]) == -1 || coe(p[2][0]) == -1) goto err ; - - { - cspawn_fileaction fa[2] = - { - [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[0], [1] = p[1][0] } } }, - [1] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = fds[1], [1] = p[0][1] } } } - } ; - size_t envlen = env_len(envp) ; - size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; - char modifs[sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) + UINT_FMT] = SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=" ; - char const *newenv[envlen + 2] ; - m += uint_fmt(modifs + sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR), p[2][1]) ; - modifs[m++] = 0 ; - env_mergen(newenv, envlen + 2, envp, envlen, modifs, m, 1) ; - pid = cspawn(prog, argv, newenv, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2) ; - if (!pid) goto err ; - } - - fd_close(p[2][1]) ; - fd_close(p[1][0]) ; - fd_close(p[0][1]) ; - fds[0] = p[0][0] ; - fds[1] = p[1][1] ; - fds[2] = p[2][0] ; - return pid ; - - err: - fd_close(p[2][1]) ; - fd_close(p[2][0]) ; - errp1: - fd_close(p[1][1]) ; - fd_close(p[1][0]) ; - errp0: - fd_close(p[0][1]) ; - fd_close(p[0][0]) ; - return 0 ; -} diff --git a/src/libstddjb/cspawn-internal.h b/src/libstddjb/cspawn-internal.h deleted file mode 100644 index 0e5efc7..0000000 --- a/src/libstddjb/cspawn-internal.h +++ /dev/null @@ -1,10 +0,0 @@ -/* ISC license. */ - -#ifndef CSPAWN_INTERNAL_H -#define CSPAWN_INTERNAL_H - -#include - -extern pid_t child_spawn1_internal (char const *, char const *const *, char const *const *, int *, int) ; - -#endif diff --git a/src/libstddjb/cspawn.c b/src/libstddjb/cspawn.c deleted file mode 100644 index 683a0a3..0000000 --- a/src/libstddjb/cspawn.c +++ /dev/null @@ -1,297 +0,0 @@ -/* ISC license. */ - -#include - -#ifdef SKALIBS_HASPOSIXSPAWN -#include -#endif - -#include -#include - -#include -#include -#include -#include -#include -#include - -static inline void cspawn_child_exec (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) -{ - for (size_t i = 0 ; i < n ; i++) - { - switch (fa[i].type) - { - case CSPAWN_FA_CLOSE : fd_close(fa[i].x.fd) ; break ; - case CSPAWN_FA_COPY : - if (fd_copy(fa[i].x.fd2[0], fa[i].x.fd2[1]) == -1) return ; - break ; - case CSPAWN_FA_MOVE : - if (fd_move(fa[i].x.fd2[0], fa[i].x.fd2[1]) == -1) return ; - if (fa[i].x.fd2[0] == fa[i].x.fd2[1] && uncoe(fa[i].x.fd2[0]) == -1) return ; - break ; - case CSPAWN_FA_OPEN : - { - int fd = open3(fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ; - if (fd == -1) return ; - if (fd_move(fa[i].x.openinfo.fd, fd) == -1) return ; - break ; - } - case CSPAWN_FA_CHDIR : - if (chdir(fa[i].x.path) == -1) return ; - break ; - case CSPAWN_FA_FCHDIR : - if (fchdir(fa[i].x.fd) == -1) return ; - break ; - default : - errno = EINVAL ; return ; - } - } - - if (flags & CSPAWN_FLAGS_SELFPIPE_FINISH) selfpipe_finish() ; - if (flags & CSPAWN_FLAGS_SIGBLOCKNONE) sig_blocknone() ; - if (flags & CSPAWN_FLAGS_SETSID) setsid() ; - - exec_ae(prog, argv, envp) ; -} - -static inline pid_t cspawn_fork (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) -{ - pid_t pid ; - int p[2] ; - char c ; - - if (pipecoe(p) == -1) return 0 ; - pid = fork() ; - if (pid == -1) - { - fd_close(p[1]) ; - fd_close(p[0]) ; - return 0 ; - } - if (!pid) - { - cspawn_child_exec(prog, argv, envp, flags, fa, n) ; - c = errno ; - fd_write(p[1], &c, 1) ; - _exit(127) ; - } - - fd_close(p[1]) ; - p[1] = fd_read(p[0], &c, 1) ; - if (p[1] < 0) - { - fd_close(p[0]) ; - return 0 ; - } - fd_close(p[0]) ; - if (p[1]) - { - wait_pid(pid, &p[0]) ; - errno = (unsigned char)c ; - return 0 ; - } - return pid ; -} - - /* - guess who has a buggy posix_spawn() *and* doesn't have waitid() to work around it? - if you guessed OpenBSD, you're right! - */ - -#if defined(SKALIBS_HASPOSIXSPAWN) && (!defined(SKALIBS_HASPOSIXSPAWNEARLYRETURN) || defined(SKALIBS_HASWAITID)) - -#include -#include -#include - -#include -#include - -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - -#include - -static inline pid_t cspawn_workaround (pid_t pid, int const *p) -{ - siginfo_t si ; - int e ; - ssize_t r ; - char c ; - - fd_close(p[1]) ; - r = fd_read(p[0], &c, 1) ; - fd_close(p[0]) ; - if (r == -1) return 0 ; - if (r) return (errno = EILSEQ, 0) ; /* child wrote, wtf */ - - do e = waitid(P_PID, pid, &si, WEXITED | WNOHANG | WNOWAIT) ; - while (e == -1 && errno == EINTR) ; - if (e == -1) return pid ; /* we're in trouble, but don't leak a child */ - if (!si.si_pid) return pid ; /* child is running */ - if (si.si_code != CLD_EXITED || si.si_status != 127) return pid ; /* child died after execve(), let caller handle it */ - /* - child exited 127, so either execve() failed, which is what we want to catch, - or it raced like a mofo, execve()d and then exited 127 on its own, in which - case, tough luck, it never existed. - */ - wait_pid(pid, 0) ; - return (errno = 0, 0) ; -} - -#endif - -static inline pid_t cspawn_pspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) -{ - pid_t pid ; - posix_spawnattr_t attr ; - posix_spawn_file_actions_t actions ; - int e ; - int nopath = !getenv("PATH") ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - int p[2] ; - if (pipecoe(p) == -1) return 0 ; -#endif - - if (flags) - { - short pfff = 0 ; - e = posix_spawnattr_init(&attr) ; - if (e) goto err ; - if (flags & (CSPAWN_FLAGS_SIGBLOCKNONE | CSPAWN_FLAGS_SELFPIPE_FINISH)) - { - sigset_t set ; - sigemptyset(&set) ; - e = posix_spawnattr_setsigmask(&attr, &set) ; - if (e) goto errattr ; - pfff |= POSIX_SPAWN_SETSIGMASK ; - } -#ifdef SKALIBS_HASPOSIXSPAWNSETSID - if (flags & CSPAWN_FLAGS_SETSID) pfff |= POSIX_SPAWN_SETSID ; -#else -#ifdef SKALIBS_HASPOSIXSPAWNSETSIDNP - if (flags & CSPAWN_FLAGS_SETSID) pfff |= POSIX_SPAWN_SETSID_NP ; -#endif -#endif - e = posix_spawnattr_setflags(&attr, pfff) ; - if (e) goto errattr ; - } - - if (n) - { - e = posix_spawn_file_actions_init(&actions) ; - if (e) goto errattr ; - for (size_t i = 0 ; i < n ; i++) - { - switch (fa[i].type) - { - case CSPAWN_FA_CLOSE : - e = posix_spawn_file_actions_addclose(&actions, fa[i].x.fd) ; - if (e) goto erractions ; - break ; - case CSPAWN_FA_COPY : - e = posix_spawn_file_actions_adddup2(&actions, fa[i].x.fd2[1], fa[i].x.fd2[0]) ; - if (e) goto erractions ; - break ; - case CSPAWN_FA_MOVE : - e = posix_spawn_file_actions_adddup2(&actions, fa[i].x.fd2[1], fa[i].x.fd2[0]) ; - if (e) goto erractions ; - if (fa[i].x.fd2[0] != fa[i].x.fd2[1]) - { - e = posix_spawn_file_actions_addclose(&actions, fa[i].x.fd2[1]) ; - if (e) goto erractions ; - } - break ; - case CSPAWN_FA_OPEN : - e = posix_spawn_file_actions_addopen(&actions, fa[i].x.openinfo.fd, fa[i].x.openinfo.file, fa[i].x.openinfo.oflag, fa[i].x.openinfo.mode) ; - if (e) goto erractions ; - break ; -#ifdef SKALIBS_HASPOSIXSPAWNCHDIR - case CSPAWN_FA_CHDIR : - e = posix_spawn_file_actions_addchdir(&actions, fa[i].x.path) ; - if (e) goto erractions ; - break ; - case CSPAWN_FA_FCHDIR : - e = posix_spawn_file_actions_addfchdir(&actions, fa[i].x.fd) ; - if (e) goto erractions ; - break ; -#else -#ifdef SKALIBS_HASPOSIXSPAWNCHDIRNP - case CSPAWN_FA_CHDIR : - e = posix_spawn_file_actions_addchdir_np(&actions, fa[i].x.path) ; - if (e) goto erractions ; - break ; - case CSPAWN_FA_FCHDIR : - e = posix_spawn_file_actions_addfchdir_np(&actions, fa[i].x.fd) ; - if (e) goto erractions ; - break ; -#endif -#endif - default : - e = EINVAL ; - goto erractions ; - } - } - } - - if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) == -1)) { e = errno ; goto erractions ; } - e = posix_spawnp(&pid, prog, n ? &actions : 0, flags ? &attr : 0, (char *const *)argv, (char *const *)envp) ; - if (nopath) unsetenv("PATH") ; - if (e) goto erractions ; - - if (n) posix_spawn_file_actions_destroy(&actions) ; - if (flags) posix_spawnattr_destroy(&attr) ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - return cspawn_workaround(pid, p) ; -#else - return pid ; -#endif - - erractions: - if (n) posix_spawn_file_actions_destroy(&actions) ; - errattr: - if (flags) posix_spawnattr_destroy(&attr) ; - err: -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - fd_close(p[1]) ; - fd_close(p[0]) ; -#endif - errno = e ; - return 0 ; -} - -#if (defined(SKALIBS_HASPOSIXSPAWNSETSID) || defined(SKALIBS_HASPOSIXSPAWNSETSIDNP)) && (defined(SKALIBS_HASPOSIXSPAWNCHDIR) || defined(SKALIBS_HASPOSIXSPAWNCHDIRNP)) - -pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) -{ - return cspawn_pspawn(prog, argv, envp, flags, fa, n) ; -} - -#else - -pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) -{ -#if !defined(SKALIBS_HASPOSIXSPAWNSETSID) && !defined(SKALIBS_HASPOSIXSPAWNSETSIDNP) - if (flags & CSPAWN_FLAGS_SETSID) goto dofork ; -#endif -#if !defined(SKALIBS_HASPOSIXSPAWNCHDIR) && !defined(SKALIBS_HASPOSIXSPAWNCHDIRNP) - for (size_t i = 0 ; i < n ; i++) - if (fa[i].type == CSPAWN_FA_CHDIR || fa[i].type == CSPAWN_FA_FCHDIR) - goto dofork ; -#endif - return cspawn_pspawn(prog, argv, envp, flags, fa, n) ; - - dofork: - return cspawn_fork(prog, argv, envp, flags, fa, n) ; -} - -#endif - -#else - -pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint16_t flags, cspawn_fileaction const *fa, size_t n) -{ - return cspawn_fork(prog, argv, envp, flags, fa, n) ; -} - -#endif diff --git a/src/libstddjb/gcspawn.c b/src/libstddjb/gcspawn.c deleted file mode 100644 index 7e9e602..0000000 --- a/src/libstddjb/gcspawn.c +++ /dev/null @@ -1,48 +0,0 @@ -/* ISC license. */ - -#include -#include -#include - -#include -#include -#include -#include - -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) ; -} -- cgit v1.2.3