diff options
-rw-r--r-- | src/tls/s6tls_io_spawn.c | 105 |
1 files changed, 10 insertions, 95 deletions
diff --git a/src/tls/s6tls_io_spawn.c b/src/tls/s6tls_io_spawn.c index e2d589a..8e26807 100644 --- a/src/tls/s6tls_io_spawn.c +++ b/src/tls/s6tls_io_spawn.c @@ -1,104 +1,19 @@ /* ISC license. */ -#include <skalibs/sysdeps.h> - -#include <skalibs/djbunix.h> - -#include "s6tls-internal.h" - -#ifdef SKALIBS_HASPOSIXSPAWN - -#include <errno.h> -#include <signal.h> -#include <stdlib.h> -#include <spawn.h> - -#include <skalibs/config.h> #include <skalibs/posixplz.h> +#include <skalibs/cspawn.h> -pid_t s6tls_io_spawn (char const *const *argv, int const p[4][2]) -{ - pid_t pid ; - posix_spawn_file_actions_t actions ; - int e ; - int nopath = !getenv("PATH") ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - int eep[2] ; - if (pipecoe(eep) == -1) return 0 ; -#endif - e = posix_spawn_file_actions_init(&actions) ; - if (e) goto err ; - e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[2][0]) ; - if (e) goto erractions ; - if (p[3][0] >= 0) - { - e = posix_spawn_file_actions_adddup2(&actions, p[3][0], 0) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[3][0]) ; - if (e) goto erractions ; - } - if (p[3][1] >= 0) - { - e = posix_spawn_file_actions_adddup2(&actions, p[3][1], 1) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[3][1]) ; - if (e) goto erractions ; - } - - if (nopath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto erractions ; } - e = posix_spawnp(&pid, argv[0], &actions, 0, (char *const *)argv, environ) ; - if (nopath) unsetenv("PATH") ; - if (e) goto erractions ; - - posix_spawn_file_actions_destroy(&actions) ; -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - return child_spawn_workaround(pid, eep) ; -#else - return pid ; -#endif - - erractions: - posix_spawn_file_actions_destroy(&actions) ; - err: -#ifdef SKALIBS_HASPOSIXSPAWNEARLYRETURN - fd_close(eep[1]) ; - fd_close(eep[0]) ; -#endif - errno = e ; - return 0 ; -} - -#else - -#include <string.h> - -#include <skalibs/strerr.h> -#include <skalibs/exec.h> +#include "s6tls-internal.h" pid_t s6tls_io_spawn (char const *const *argv, int const p[4][2]) { - pid_t pid = fork() ; - if (pid == -1) return 0 ; - if (!pid) + cspawn_fileaction fa[5] = { - size_t proglen = strlen(PROG) ; - char newprog[proglen + 9] ; - memcpy(newprog, PROG, proglen) ; - memcpy(newprog, " (child)", 9) ; - PROG = newprog ; - fd_close(p[0][1]) ; - fd_close(p[1][0]) ; - fd_close(p[2][0]) ; - if ((p[3][0] >= 0 && fd_move(0, p[3][0]) == -1) - || (p[3][1] >= 0 && fd_move(1, p[3][1]) == -1)) - strerr_diefu1sys(111, "move network fds to stdin/stdout") ; - xexec(argv) ; - } - return pid ; + [0] = { .type = CSPAWN_FA_CLOSE, .x = { .fd = p[0][1] } }, + [1] = { .type = CSPAWN_FA_CLOSE, .x = { .fd = p[1][0] } }, + [2] = { .type = CSPAWN_FA_CLOSE, .x = { .fd = p[2][1] } }, + [3] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 0, [1] = p[3][0] } } }, + [4] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 1, [1] = p[3][1] } } } + } ; + return cspawn(argv[0], argv, (char const *const *)environ, 0, fa, p[3][0] >= 0 ? 5 : 3) ; } - -#endif |