From 10c9f6594bea17d865d87b9cc587dfc13d8ce60f Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Sat, 9 Sep 2023 11:12:12 +0000 Subject: Use cspawn (skalibs-2.14) Signed-off-by: Laurent Bercot --- src/tls/s6tls_io_spawn.c | 105 +++++------------------------------------------ 1 file 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 - -#include - -#include "s6tls-internal.h" - -#ifdef SKALIBS_HASPOSIXSPAWN - -#include -#include -#include -#include - -#include #include +#include -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 - -#include -#include +#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 -- cgit v1.2.3