diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-09-09 03:55:38 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-09-09 03:55:38 +0000 |
commit | 4cce87557feb6933c284d234c448bd8bde4facae (patch) | |
tree | 7417e8558b0770da2ae24df8abd755dbc913855f /src/libstddjb | |
parent | a646c96d5f7ec4b4e0bb538a41fc202c2cdb3309 (diff) | |
download | skalibs-4cce87557feb6933c284d234c448bd8bde4facae.tar.xz |
cspawn revamp, part 1. Prepare for 2.14.0.0.
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/child_spawn.c | 198 | ||||
-rw-r--r-- | src/libstddjb/child_spawn0.c | 101 | ||||
-rw-r--r-- | src/libstddjb/child_spawn1_internal.c | 131 | ||||
-rw-r--r-- | src/libstddjb/child_spawn1_pipe.c | 5 | ||||
-rw-r--r-- | src/libstddjb/child_spawn1_socket.c | 4 | ||||
-rw-r--r-- | src/libstddjb/child_spawn2.c | 149 | ||||
-rw-r--r-- | src/libstddjb/child_spawn3.c | 175 | ||||
-rw-r--r-- | src/libstddjb/cspawn-internal.h (renamed from src/libstddjb/djbunix-internal.h) | 4 | ||||
-rw-r--r-- | src/libstddjb/cspawn.c | 220 | ||||
-rw-r--r-- | src/libstddjb/cspawn_workaround.c (renamed from src/libstddjb/child_spawn_workaround.c) | 4 | ||||
-rw-r--r-- | src/libstddjb/openslurpclose.c | 8 | ||||
-rw-r--r-- | src/libstddjb/slurp.c | 8 |
12 files changed, 290 insertions, 717 deletions
diff --git a/src/libstddjb/child_spawn.c b/src/libstddjb/child_spawn.c index 7c59fba..fb5be27 100644 --- a/src/libstddjb/child_spawn.c +++ b/src/libstddjb/child_spawn.c @@ -1,208 +1,56 @@ /* ISC license. */ -/* MT-unsafe */ - -#include <skalibs/sysdeps.h> - #include <string.h> -#include <errno.h> #include <unistd.h> #include <skalibs/types.h> #include <skalibs/djbunix.h> - -#ifdef SKALIBS_HASPOSIXSPAWN - -#include <signal.h> -#include <stdlib.h> -#include <spawn.h> - -#include <skalibs/config.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, unsigned int n) +pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, size_t n) { pid_t pid ; - posix_spawn_file_actions_t actions ; - posix_spawnattr_t attr ; - sigset_t set ; - int e ; - int p[n ? n : 1][2] ; + 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) ; - unsigned int i = 0 ; + 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] ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - int syncpipe[2] ; - if (pipecoe(syncpipe) == -1) return 0 ; -#endif - memcpy(modifs, SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=", sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR)) ; 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)) - { - i++ ; goto errpi ; - } - } - for (i = 2 ; i < n ; i++) - { - m += uint_fmt(modifs + m, p[i][!(i & 1)]) ; - if (i+1 < n) modifs[m++] = ',' ; - } - modifs[m++] = 0 ; - - e = posix_spawnattr_init(&attr) ; - if (e) goto erre ; - sigemptyset(&set) ; - e = posix_spawnattr_setsigmask(&attr, &set) ; - if (e) goto errattr ; - e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ; - if (e) goto errattr ; - e = posix_spawn_file_actions_init(&actions) ; - if (e) goto errattr ; - if (n >= 2 && p[1][0]) - { - e = posix_spawn_file_actions_adddup2(&actions, p[1][0], 0) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; - if (e) goto erractions ; - } - if (n && p[0][1] != 1) - { - e = posix_spawn_file_actions_adddup2(&actions, p[0][1], 1) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ; - if (e) goto erractions ; - } - { - int nopath = !getenv("PATH") ; - size_t envlen = env_len(envp) ; - char const *newenv[envlen + 2] ; - if (!env_mergen(newenv, envlen+2, envp, envlen, modifs, m, 1)) goto erractions ; - if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) - { - e = errno ; goto erractions ; - } - e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)newenv) ; - if (nopath) unsetenv("PATH") ; - if (e) goto erractions ; - } - - posix_spawn_file_actions_destroy(&actions) ; - posix_spawnattr_destroy(&attr) ; - for (i = 0 ; i < n ; i++) fd_close(p[i][!(i & 1)]) ; - -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - pid = child_spawn_workaround(pid, syncpipe) ; - if (!pid) - { - for (i = 0 ; i < n ; i++) fd_close(p[i][i & 1]) ; - return 0 ; - } -#endif - for (i = 0 ; i < n ; i++) fds[i] = p[i][i & 1] ; - return pid ; - - erractions: - posix_spawn_file_actions_destroy(&actions) ; - errattr: - posix_spawnattr_destroy(&attr) ; - erre: - errno = e ; - i = n ; - errpi: - while (i--) - { - fd_close(p[i][1]) ; - fd_close(p[i][0]) ; + if ((ndelay_on(p[i][i & 1]) == -1) || (coe(p[i][i & 1]) == -1)) goto errpip ; } -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - fd_close(syncpipe[1]) ; - fd_close(syncpipe[0]) ; -#endif - return 0 ; -} - -#else - -#include <skalibs/allreadwrite.h> -#include <skalibs/sig.h> -#include <skalibs/exec.h> - -pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, unsigned int n) -{ - pid_t pid ; - int syncpipe[2] ; - int p[n ? n : 1][2] ; - size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; - unsigned int i = 0 ; - char modifs[m + 1 + n * UINT_FMT] ; - char c ; memcpy(modifs, SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=", sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR)) ; - 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)) - { - i++ ; goto errpi ; - } - } for (i = 2 ; i < n ; i++) { m += uint_fmt(modifs + m, p[i][!(i & 1)]) ; if (i+1 < n) modifs[m++] = ',' ; } modifs[m++] = 0 ; + if (!env_mergen(newenv, envlen + 2, envp, envlen, modifs, m, 1)) goto errpi ; + 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 ; - if (pipecoe(syncpipe) < 0) goto errp ; - - pid = fork() ; - if (pid < 0) goto errsp ; - else if (!pid) + for (i = 0 ; i < n ; i++) { - if (n >= 2) - { - if (fd_move2(0, p[1][0], 1, p[0][1]) == -1) goto syncdie ; - } - else if (n) - { - if (fd_move(1, p[0][1]) == -1) goto syncdie ; - } - sig_blocknone() ; - mexec_aen(prog, argv, envp, modifs, m, 1) ; - syncdie: - c = errno ; - fd_write(syncpipe[1], &c, 1) ; - _exit(127) ; - } - - fd_close(syncpipe[1]) ; - syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; - if (syncpipe[1]) - { - int e = (unsigned char)c ; - if (syncpipe[1] == -1) e = errno ; - if (wait_pid(pid, 0) == -1) e = errno ; - errno = e ; - goto errsp0 ; - } - fd_close(syncpipe[0]) ; - - for (i = n ; i ; i--) - { - fd_close(p[i-1][i & 1]) ; - fds[i-1] = p[i-1][!(i & 1)] ; + fd_close(p[i][!(i & 1)]) ; + fds[i] = p[i][i & 1] ; } return pid ; - errsp: - fd_close(syncpipe[1]) ; - errsp0: - fd_close(syncpipe[0]) ; - errp: - i = n ; + errpip: + i++ ; errpi: while (i--) { @@ -211,5 +59,3 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const } return 0 ; } - -#endif diff --git a/src/libstddjb/child_spawn0.c b/src/libstddjb/child_spawn0.c index ea7a5b1..b1353b6 100644 --- a/src/libstddjb/child_spawn0.c +++ b/src/libstddjb/child_spawn0.c @@ -1,105 +1,8 @@ /* ISC license. */ -#include <skalibs/sysdeps.h> - -#include <errno.h> - -#ifdef SKALIBS_HASPOSIXSPAWN - -#include <signal.h> -#include <stdlib.h> -#include <spawn.h> - -#include <skalibs/config.h> -#include <skalibs/djbunix.h> +#include <skalibs/cspawn.h> pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp) { - pid_t pid ; - posix_spawnattr_t attr ; - sigset_t set ; - int e ; - int nopath = !getenv("PATH") ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - int p[2] ; - if (pipecoe(p) == -1) return 0 ; -#endif - e = posix_spawnattr_init(&attr) ; - if (e) goto err ; - sigemptyset(&set) ; - e = posix_spawnattr_setsigmask(&attr, &set) ; - if (e) goto errattr ; - e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ; - if (e) goto errattr ; - if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto errattr ; } - e = posix_spawnp(&pid, prog, 0, &attr, (char *const *)argv, (char *const *)envp) ; - if (nopath) unsetenv("PATH") ; - if (e) goto errattr ; - - posix_spawnattr_destroy(&attr) ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - return child_spawn_workaround(pid, p) ; -#else - return pid ; -#endif - - errattr: - posix_spawnattr_destroy(&attr) ; - err: -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - fd_close(p[1]) ; - fd_close(p[0]) ; -#endif - errno = e ; - return 0 ; + return cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, 0, 0) ; } - -#else - -#include <unistd.h> - -#include <skalibs/allreadwrite.h> -#include <skalibs/sig.h> -#include <skalibs/djbunix.h> -#include <skalibs/exec.h> - -pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp) -{ - pid_t pid ; - int p[2] ; - char c ; - - if (pipecoe(p) == -1) return 0 ; - pid = fork() ; - if (pid < 0) - { - fd_close(p[1]) ; - fd_close(p[0]) ; - return 0 ; - } - if (!pid) - { - sig_blocknone() ; - exec_ae(prog, argv, envp) ; - 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 ; -} - -#endif diff --git a/src/libstddjb/child_spawn1_internal.c b/src/libstddjb/child_spawn1_internal.c index 9e6eeff..e65b588 100644 --- a/src/libstddjb/child_spawn1_internal.c +++ b/src/libstddjb/child_spawn1_internal.c @@ -1,140 +1,27 @@ /* ISC license. */ -#include <skalibs/sysdeps.h> - -#include <errno.h> - #include <skalibs/djbunix.h> - -#ifdef SKALIBS_HASPOSIXSPAWN - -#include <signal.h> -#include <spawn.h> -#include <stdlib.h> - -#include <skalibs/config.h> +#include <skalibs/cspawn.h> +#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 ; - posix_spawn_file_actions_t actions ; - posix_spawnattr_t attr ; - sigset_t set ; - int e ; - int nopath = !getenv("PATH") ; - -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - int syncpipe[2] ; - if (pipecoe(syncpipe) == -1) goto err ; -#endif - - if (coe(p[!(to & 1)]) == -1) goto errp ; - e = posix_spawnattr_init(&attr) ; - if (e) goto erre ; - sigemptyset(&set) ; - e = posix_spawnattr_setsigmask(&attr, &set) ; - if (e) goto errattr ; - e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ; - if (e) goto errattr ; - e = posix_spawn_file_actions_init(&actions) ; - if (e) goto errattr ; - if (p[to & 1] != (to & 1)) - { - e = posix_spawn_file_actions_adddup2(&actions, p[to & 1], to & 1) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[to & 1]) ; - if (e) goto erractions ; - } - if (to & 2) + cspawn_fileaction fa[3] = { - e = posix_spawn_file_actions_adddup2(&actions, to & 1, !(to & 1)) ; - if (e) goto erractions ; - } - if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto erractions ; } - e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)envp) ; - if (nopath) unsetenv("PATH") ; - if (e) goto erractions ; + [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 } } } + } ; - posix_spawn_file_actions_destroy(&actions) ; - posix_spawnattr_destroy(&attr) ; - fd_close(p[to & 1]) ; - -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - return child_spawn_workaround(pid, syncpipe) ; -#else - return pid ; -#endif + pid = cspawn(prog, argv, envp, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2 + !!(to & 2)) ; + if (!pid) goto err ; - erractions: - posix_spawn_file_actions_destroy(&actions) ; - errattr: - posix_spawnattr_destroy(&attr) ; - erre: - errno = e ; - errp: -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - fd_close(syncpipe[1]) ; - fd_close(syncpipe[0]) ; - err: -#endif - fd_close(p[1]) ; - fd_close(p[0]) ; - return 0 ; -} - -#else - -#include <unistd.h> - -#include <skalibs/allreadwrite.h> -#include <skalibs/sig.h> -#include <skalibs/exec.h> - -pid_t child_spawn1_internal (char const *prog, char const *const *argv, char const *const *envp, int *p, int to) -{ - pid_t pid ; - int syncpipe[2] ; - char c ; - - if (coe(p[0]) < 0 || pipecoe(syncpipe) < 0) goto err ; - - pid = fork() ; - if (pid < 0) goto errsp ; - if (!pid) - { - fd_close(p[!(to & 1)]) ; - if (fd_move(to & 1, p[to & 1]) < 0) goto syncdie ; - if ((to & 2) && (fd_copy(!(to & 1), to & 1) < 0)) goto syncdie ; - sig_blocknone() ; - exec_ae(prog, argv, envp) ; - syncdie: - c = errno ; - fd_write(syncpipe[1], &c, 1) ; - _exit(127) ; - } - - fd_close(syncpipe[1]) ; - syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; - if (syncpipe[1]) - { - int e = (unsigned char)c ; - if (syncpipe[1] == -1) e = errno ; - if (wait_pid(pid, 0) == -1) e = errno ; - errno = e ; - goto errsp0 ; - } - fd_close(syncpipe[0]) ; fd_close(p[to & 1]) ; return pid ; - errsp: - fd_close(syncpipe[1]) ; - errsp0: - fd_close(syncpipe[0]) ; err: fd_close(p[1]) ; fd_close(p[0]) ; return 0 ; } - -#endif diff --git a/src/libstddjb/child_spawn1_pipe.c b/src/libstddjb/child_spawn1_pipe.c index a436f6e..a31e091 100644 --- a/src/libstddjb/child_spawn1_pipe.c +++ b/src/libstddjb/child_spawn1_pipe.c @@ -1,8 +1,9 @@ /* ISC license. */ #include <unistd.h> -#include <skalibs/djbunix.h> -#include "djbunix-internal.h" + +#include <skalibs/cspawn.h> +#include "cspawn-internal.h" pid_t child_spawn1_pipe (char const *prog, char const *const *argv, char const *const *envp, int *fd, int to) { diff --git a/src/libstddjb/child_spawn1_socket.c b/src/libstddjb/child_spawn1_socket.c index 0bb2da9..684842e 100644 --- a/src/libstddjb/child_spawn1_socket.c +++ b/src/libstddjb/child_spawn1_socket.c @@ -1,8 +1,8 @@ /* ISC license. */ #include <skalibs/socket.h> -#include <skalibs/djbunix.h> -#include "djbunix-internal.h" +#include <skalibs/cspawn.h> +#include "cspawn-internal.h" pid_t child_spawn1_socket (char const *prog, char const *const *argv, char const *const *envp, int *fd) { diff --git a/src/libstddjb/child_spawn2.c b/src/libstddjb/child_spawn2.c index 986fd15..f287c0b 100644 --- a/src/libstddjb/child_spawn2.c +++ b/src/libstddjb/child_spawn2.c @@ -1,162 +1,35 @@ /* ISC license. */ -/* MT-unsafe */ - -#include <skalibs/sysdeps.h> - #include <unistd.h> -#include <errno.h> #include <skalibs/djbunix.h> - -#ifdef SKALIBS_HASPOSIXSPAWN - -#include <signal.h> -#include <stdlib.h> -#include <spawn.h> - -#include <skalibs/config.h> +#include <skalibs/cspawn.h> pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds) { pid_t pid ; - posix_spawn_file_actions_t actions ; - posix_spawnattr_t attr ; int p[2][2] ; - int e ; - int nopath = !getenv("PATH") ; - sigset_t set ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - int syncpipe[2] ; - if (pipecoe(syncpipe) == -1) return 0 ; -#endif - if (pipe(p[0]) == -1) goto err ; + 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 errp1 ; + if (ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1) goto err ; - e = posix_spawnattr_init(&attr) ; - if (e) goto erre ; - sigemptyset(&set) ; - e = posix_spawnattr_setsigmask(&attr, &set) ; - if (e) goto errattr ; - e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ; - if (e) goto errattr ; - e = posix_spawn_file_actions_init(&actions) ; - if (e) goto errattr ; - if (p[1][0] != fds[0]) { - e = posix_spawn_file_actions_adddup2(&actions, p[1][0], fds[0]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; - if (e) goto erractions ; + 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 ; } - if (p[0][1] != fds[1]) - { - e = posix_spawn_file_actions_adddup2(&actions, p[0][1], fds[1]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ; - if (e) goto erractions ; - } - if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) == -1)) - { - e = errno ; goto erractions ; - } - e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)envp) ; - if (nopath) unsetenv("PATH") ; - if (e) goto erractions ; - posix_spawn_file_actions_destroy(&actions) ; - posix_spawnattr_destroy(&attr) ; fd_close(p[0][1]) ; fd_close(p[1][0]) ; - -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - pid = child_spawn_workaround(pid, syncpipe) ; - if (!pid) - { - fd_close(p[1][1]) ; - fd_close(p[0][0]) ; - return 0 ; - } -#endif fds[0] = p[0][0] ; fds[1] = p[1][1] ; return pid ; - erractions: - posix_spawn_file_actions_destroy(&actions) ; - errattr: - posix_spawnattr_destroy(&attr) ; - erre: - errno = e ; - errp1: - fd_close(p[1][1]) ; - fd_close(p[1][0]) ; - errp: - fd_close(p[0][1]) ; - fd_close(p[0][0]) ; err: -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - fd_close(syncpipe[1]) ; - fd_close(syncpipe[0]) ; -#endif - return 0 ; -} - -#else - -#include <skalibs/allreadwrite.h> -#include <skalibs/sig.h> -#include <skalibs/exec.h> - -pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds) -{ - pid_t pid ; - int syncpipe[2] ; - int p[2][2] ; - char c ; - - if (pipe(p[0]) < 0) return 0 ; - if (ndelay_on(p[0][0]) < 0 || coe(p[0][0]) < 0 || pipe(p[1]) < 0) goto errp ; - if (ndelay_on(p[1][1]) < 0 || coe(p[1][1]) < 0) goto errp1 ; - if (pipecoe(syncpipe) < 0) goto errp1 ; - - pid = fork() ; - if (pid == -1) goto errsp ; - else if (!pid) - { - if (fd_move2(fds[0], p[1][0], fds[1], p[0][1]) < 0) goto syncdie ; - sig_blocknone() ; - exec_ae(prog, argv, envp) ; - syncdie: - c = errno ; - fd_write(syncpipe[1], &c, 1) ; - _exit(127) ; - } - - fd_close(syncpipe[1]) ; - syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; - if (syncpipe[1]) - { - int e = (unsigned char)c ; - if (syncpipe[1] == -1) e = errno ; - if (wait_pid(pid, &syncpipe[1]) == -1) e = errno ; - errno = e ; - goto errsp0 ; - } - fd_close(syncpipe[0]) ; - fd_close(p[0][1]) ; - fd_close(p[1][0]) ; - - fds[0] = p[0][0] ; - fds[1] = p[1][1] ; - return pid ; - - errsp: - fd_close(syncpipe[1]) ; - errsp0: - fd_close(syncpipe[0]) ; - errp1: fd_close(p[1][1]) ; fd_close(p[1][0]) ; errp: @@ -164,5 +37,3 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const fd_close(p[0][0]) ; return 0 ; } - -#endif diff --git a/src/libstddjb/child_spawn3.c b/src/libstddjb/child_spawn3.c index 6eb7519..932e5ab 100644 --- a/src/libstddjb/child_spawn3.c +++ b/src/libstddjb/child_spawn3.c @@ -1,185 +1,48 @@ /* ISC license. */ -/* MT-unsafe */ - -#include <skalibs/sysdeps.h> - #include <unistd.h> -#include <errno.h> #include <skalibs/types.h> -#include <skalibs/djbunix.h> - -#ifdef SKALIBS_HASPOSIXSPAWN - -#include <signal.h> -#include <stdlib.h> -#include <spawn.h> - -#include <skalibs/config.h> #include <skalibs/env.h> +#include <skalibs/djbunix.h> +#include <skalibs/cspawn.h> pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const *envp, int *fds) { pid_t pid ; - posix_spawn_file_actions_t actions ; - posix_spawnattr_t attr ; - sigset_t set ; - int e ; int p[3][2] ; - size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; - char modifs[sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) + UINT_FMT] = SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=" ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - int syncpipe[2] ; - if (pipecoe(syncpipe) == -1) return 0 ; -#endif - if (pipe(p[0]) < 0 || ndelay_on(p[0][0]) < 0 || coe(p[0][0]) < 0) goto err ; - if (pipe(p[1]) < 0 || ndelay_on(p[1][1]) < 0 || coe(p[1][1]) < 0) goto errp0 ; - if (pipe(p[2]) < 0 || ndelay_on(p[2][0]) < 0 || coe(p[2][0]) < 0) goto errp1 ; - m += uint_fmt(modifs + sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR), p[2][1]) ; - modifs[m++] = 0 ; + 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 ; - e = posix_spawnattr_init(&attr) ; - if (e) goto erre ; - sigemptyset(&set) ; - e = posix_spawnattr_setsigmask(&attr, &set) ; - if (e) goto errattr ; - e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ; - if (e) goto errattr ; - e = posix_spawn_file_actions_init(&actions) ; - if (e) goto errattr ; - if (p[1][0] != fds[0]) { - e = posix_spawn_file_actions_adddup2(&actions, p[1][0], fds[0]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; - if (e) goto erractions ; - } - if (p[0][1] != fds[1]) - { - e = posix_spawn_file_actions_adddup2(&actions, p[0][1], fds[1]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ; - if (e) goto erractions ; - } - { - int nopath = !getenv("PATH") ; + 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] ; - if (!env_mergen(newenv, envlen+2, envp, envlen, modifs, m, 1)) goto erractions ; - if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) - { - e = errno ; goto erractions ; - } - e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)newenv) ; - if (nopath) unsetenv("PATH") ; - if (e) goto erractions ; + m += uint_fmt(modifs + sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR), p[2][1]) ; + modifs[m++] = 0 ; + if (!env_mergen(newenv, envlen + 2, envp, envlen, modifs, m, 1)) goto err ; + pid = cspawn(prog, argv, newenv, CSPAWN_FLAGS_SIGBLOCKNONE, fa, 2) ; + if (!pid) goto err ; } - posix_spawn_file_actions_destroy(&actions) ; - posix_spawnattr_destroy(&attr) ; fd_close(p[2][1]) ; fd_close(p[1][0]) ; fd_close(p[0][1]) ; - -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - pid = child_spawn_workaround(pid, syncpipe) ; - if (!pid) - { - fd_close(p[2][0]) ; - fd_close(p[1][1]) ; - fd_close(p[0][0]) ; - return 0 ; - } -#endif fds[0] = p[0][0] ; fds[1] = p[1][1] ; fds[2] = p[2][0] ; return pid ; - erractions: - posix_spawn_file_actions_destroy(&actions) ; - errattr: - posix_spawnattr_destroy(&attr) ; - erre: - errno = e ; - 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]) ; err: -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - fd_close(syncpipe[1]) ; - fd_close(syncpipe[0]) ; -#endif - return 0 ; -} - -#else - -#include <skalibs/allreadwrite.h> -#include <skalibs/sig.h> -#include <skalibs/exec.h> - -pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const *envp, int *fds) -{ - pid_t pid ; - int syncpipe[2] ; - int p[3][2] ; - 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 c ; - - if (pipe(p[0]) == -1 || ndelay_on(p[0][0]) == -1 || coe(p[0][0]) == -1) return 0 ; - if (pipe(p[1]) == -1 || ndelay_on(p[1][1]) == -1 || coe(p[1][1]) == -1) goto errp0 ; - if (pipe(p[2]) == -1 || ndelay_on(p[2][0]) == -1 || coe(p[2][0]) == -1) goto errp1 ; - m += uint_fmt(modifs + sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR), p[2][1]) ; - modifs[m++] = 0 ; - if (pipecoe(syncpipe) < 0) goto errp2 ; - - pid = fork() ; - if (pid < 0) goto errsp ; - else if (!pid) - { - if (fd_move2(fds[0], p[1][0], fds[1], p[0][1]) == -1) goto syncdie ; - sig_blocknone() ; - mexec_aen(prog, argv, envp, modifs, m, 1) ; - syncdie: - c = errno ; - fd_write(syncpipe[1], &c, 1) ; - _exit(127) ; - } - - fd_close(syncpipe[1]) ; - syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; - if (syncpipe[1]) - { - int e = (unsigned char)c ; - if (syncpipe[1] == -1) e = errno ; - if (wait_pid(pid, &syncpipe[1]) == -1) e = errno ; - errno = e ; - goto errsp0 ; - } - fd_close(syncpipe[0]) ; - - 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 ; - - errsp: - fd_close(syncpipe[1]) ; - errsp0: - fd_close(syncpipe[0]) ; - errp2: fd_close(p[2][1]) ; fd_close(p[2][0]) ; errp1: @@ -190,5 +53,3 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const fd_close(p[0][0]) ; return 0 ; } - -#endif diff --git a/src/libstddjb/djbunix-internal.h b/src/libstddjb/cspawn-internal.h index 775cb80..0e5efc7 100644 --- a/src/libstddjb/djbunix-internal.h +++ b/src/libstddjb/cspawn-internal.h @@ -1,7 +1,7 @@ /* ISC license. */ -#ifndef DJBUNIX_INTERNAL_H -#define DJBUNIX_INTERNAL_H +#ifndef CSPAWN_INTERNAL_H +#define CSPAWN_INTERNAL_H #include <sys/types.h> diff --git a/src/libstddjb/cspawn.c b/src/libstddjb/cspawn.c new file mode 100644 index 0000000..4db86e0 --- /dev/null +++ b/src/libstddjb/cspawn.c @@ -0,0 +1,220 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#include <errno.h> + +#include <skalibs/cspawn.h> + +#ifdef SKALIBS_HASPOSIXSPAWN + +#include <signal.h> +#include <stdlib.h> +#include <spawn.h> + +#include <skalibs/config.h> +#include <skalibs/djbunix.h> + +#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN + +#include <sys/wait.h> + +#include <skalibs/allreadwrite.h> + +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 + +pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_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) + { + e = posix_spawnattr_init(&attr) ; + if (e) goto err ; + if (flags & 3) + { + sigset_t set ; + sigemptyset(&set) ; + e = posix_spawnattr_setsigmask(&attr, &set) ; + if (e) goto errattr ; + e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ; + 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 ; + default : + e = EINVAL ; + goto erractions ; + } + } + } + + if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) == -1)) { e = errno ; goto erractions ; } + e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)envp) ; + if (nopath) unsetenv("PATH") ; + if (e) goto errattr ; + + 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 ; +} + +#else + +#include <fcntl.h> +#include <unistd.h> + +#include <skalibs/allreadwrite.h> +#include <skalibs/sig.h> +#include <skalibs/selfpipe.h> +#include <skalibs/exec.h> + +static inline void cspawn_child_exec (char const *prog, char const *const *argv, char const *const *envp, uint32_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 ; + break ; + case CSPAWN_FA_OPEN : + { + int fd = open(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 ; + } + default : + errno = EINVAL ; return ; + } + } + + if (flags & CSPAWN_FLAGS_SELFPIPE_FINISH) selfpipe_finish() ; + if (flags & CSPAWN_FLAGS_SIGBLOCKNONE) sig_blocknone() ; +} + +pid_t cspawn (char const *prog, char const *const *argv, char const *const *envp, uint32_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 ; +} + +#endif diff --git a/src/libstddjb/child_spawn_workaround.c b/src/libstddjb/cspawn_workaround.c index b5fb4f0..da9bd89 100644 --- a/src/libstddjb/child_spawn_workaround.c +++ b/src/libstddjb/cspawn_workaround.c @@ -13,7 +13,7 @@ /* when posix_spawn returns too early, you need this */ -pid_t child_spawn_workaround (pid_t pid, int const *p) +pid_t cspawn_workaround (pid_t pid, int const *p) { siginfo_t si ; int e ; @@ -42,7 +42,7 @@ pid_t child_spawn_workaround (pid_t pid, int const *p) #else -pid_t child_spawn_workaround (pid_t pid, int const *p) +pid_t cspawn_workaround (pid_t pid, int const *p) { if (p) { diff --git a/src/libstddjb/openslurpclose.c b/src/libstddjb/openslurpclose.c deleted file mode 100644 index e6b0570..0000000 --- a/src/libstddjb/openslurpclose.c +++ /dev/null @@ -1,8 +0,0 @@ -/* ISC license. */ - -#include <skalibs/djbunix.h> - -int openslurpclose (stralloc *sa, char const *fn) -{ - return openslurpnclose(fn, sa, 0) ; -} diff --git a/src/libstddjb/slurp.c b/src/libstddjb/slurp.c deleted file mode 100644 index eccd833..0000000 --- a/src/libstddjb/slurp.c +++ /dev/null @@ -1,8 +0,0 @@ -/* ISC license. */ - -#include <skalibs/djbunix.h> - -int slurp (stralloc *sa, int fd) -{ - return slurpn(fd, sa, 0) ; -} |