diff options
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/child_spawn.c | 73 | ||||
-rw-r--r-- | src/libstddjb/child_spawn1_internal.c (renamed from src/libstddjb/child_spawn1.c) | 32 | ||||
-rw-r--r-- | src/libstddjb/child_spawn1_pipe.c | 16 | ||||
-rw-r--r-- | src/libstddjb/child_spawn1_socket.c | 16 | ||||
-rw-r--r-- | src/libstddjb/djbunix-internal.h | 10 |
5 files changed, 84 insertions, 63 deletions
diff --git a/src/libstddjb/child_spawn.c b/src/libstddjb/child_spawn.c index 5d696e4..a9003e5 100644 --- a/src/libstddjb/child_spawn.c +++ b/src/libstddjb/child_spawn.c @@ -15,7 +15,6 @@ #include <skalibs/env.h> #include <skalibs/djbunix.h> #include <skalibs/uint.h> -#include <skalibs/webipc.h> #ifdef SKALIBS_HASPOSIXSPAWN @@ -35,9 +34,7 @@ /* If n = 0 : child's stdin and stdout are the same as the parent's - If n = 1 : Unix socket between parent and child. - Additional canals, if needed, may be fd-passed through it. - If n >= 2 : pipes between parent and child. + If n >= 1 : pipes between parent and child. Parent reads on even ones, writes on odd ones. */ @@ -53,17 +50,10 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const pid_t pid ; int e ; unsigned int m = sizeof(NOFDVAR) ; - unsigned int i ; + unsigned int i = 0 ; char modifs[m + 1 + n * UINT_FMT] ; byte_copy(modifs, sizeof(NOFDVAR), NOFDVAR "=") ; - if (n == 1) - { - if (ipc_pair_b(p[0]) < 0) return 0 ; - } - else if (n >= 2) - { - for (i = 0 ; i < n ; i++) if (pipe(p[i]) < 0) { e = errno ; goto errp ; } - } + for (; i < n ; i++) if (pipe(p[i]) < 0) { e = errno ; goto errp ; } for (i = 0 ; i < n ; i++) if ((ndelay_on(p[i][i & 1]) < 0) || (coe(p[i][i & 1]) < 0)) { @@ -88,28 +78,19 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const if (e) goto errattr ; e = posix_spawn_file_actions_init(&actions) ; if (e) goto errattr ; - switch (n) + if (n >= 2) + { + 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) { - case 0 : - break ; - case 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 ; - e = posix_spawn_file_actions_adddup2(&actions, 1, 0) ; - if (e) goto erractions ; - break ; - default : - 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 ; - 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 ; - break ; + 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 haspath = !!env_get("PATH") ; @@ -142,25 +123,13 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const byte_copy(name + len, 9, " (child)") ; PROG = name ; fd_close(syncpipe[0]) ; - switch (n) + if (n >= 2) { - case 0 : - { - int fd = open2("/dev/null", O_RDONLY) ; - if (fd < 0) goto syncdie ; - if (fd_move(0, fd) < 0) goto syncdie ; - fd = open2("/dev/null", O_WRONLY) ; - if (fd < 0) goto syncdie ; - if (fd_move(1, fd) < 0) goto syncdie ; - break ; - } - case 1 : - if (fd_move(1, p[0][1]) < 0) goto syncdie ; - if (fd_copy(0, 1) < 0) goto syncdie ; - break ; - default : - if (fd_move2(0, p[1][0], 1, p[0][1]) < 0) goto syncdie ; - break ; + if (fd_move2(0, p[1][0], 1, p[0][1]) < 0) goto syncdie ; + } + else if (n) + { + if (fd_move(1, p[0][1]) < 0) goto syncdie ; } sig_blocknone() ; pathexec_r_name(prog, argv, envp, env_len(envp), modifs, m) ; diff --git a/src/libstddjb/child_spawn1.c b/src/libstddjb/child_spawn1_internal.c index bb51ab9..68b0242 100644 --- a/src/libstddjb/child_spawn1.c +++ b/src/libstddjb/child_spawn1_internal.c @@ -8,37 +8,48 @@ #ifdef SKALIBS_HASPOSIXSPAWN +#include <signal.h> #include <spawn.h> #include <stdlib.h> #include <skalibs/config.h> #include <skalibs/env.h> -pid_t child_spawn1 (char const *prog, char const *const *argv, char const *const *envp, int *fd, int to) +pid_t child_spawn1_internal (char const *prog, char const *const *argv, char const *const *envp, int *p, int to) { posix_spawn_file_actions_t actions ; + posix_spawnattr_t attr ; int e ; - int p[2] ; pid_t pid ; int haspath = !!env_get("PATH") ; - if (pipe(p) < 0) return 0 ; to = !!to ; - e = posix_spawn_file_actions_init(&actions) ; + if (coe(p[!to]) < 0) { e = errno ; goto err ; } + e = posix_spawnattr_init(&attr) ; if (e) goto err ; + { + sigset_t set ; + sigemptyset(&set) ; + e = posix_spawnattr_setsigmask(&attr, &set) ; + } + if (e) goto errattr ; + e = posix_spawn_file_actions_init(&actions) ; + if (e) goto errattr ; e = posix_spawn_file_actions_addclose(&actions, p[!to]) ; if (e) goto erractions ; e = posix_spawn_file_actions_adddup2(&actions, p[to], to) ; if (e) goto erractions ; if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; goto erractions ; } - e = posix_spawnp(&pid, prog, &actions, 0, (char *const *)argv, (char *const *)envp) ; + e = posix_spawnp(&pid, prog, &actions, &attr, (char *const *)argv, (char *const *)envp) ; if (!haspath) unsetenv("PATH") ; posix_spawn_file_actions_destroy(&actions) ; + posix_spawnattr_destroy(&attr) ; fd_close(p[to]) ; if (e) goto errp ; - *fd = p[!to] ; return pid ; erractions: posix_spawn_file_actions_destroy(&actions) ; + errattr: + posix_spawnattr_destroy(&attr) ; err: fd_close(p[to]) ; errp: @@ -50,15 +61,14 @@ pid_t child_spawn1 (char const *prog, char const *const *argv, char const *const #else #include <skalibs/allreadwrite.h> +#include <skalibs/sig.h> -pid_t child_spawn1 (char const *prog, char const *const *argv, char const *const *envp, int *fd, int to) +pid_t child_spawn1_internal (char const *prog, char const *const *argv, char const *const *envp, int *p, int to) { int e ; int syncp[2] ; - int p[2] ; pid_t pid ; - if (pipe(p) < 0) return 0 ; - if (pipecoe(syncp) < 0) + if (coe(p[0]) < 0 || pipecoe(syncp) < 0) { e = errno ; fd_close(p[1]) ; @@ -83,6 +93,7 @@ pid_t child_spawn1 (char const *prog, char const *const *argv, char const *const fd_close(syncp[0]) ; fd_close(p[!to]) ; if (fd_move(to, p[to]) < 0) goto err ; + sig_blocknone() ; pathexec_run(prog, argv, envp) ; err: e = errno ; @@ -108,7 +119,6 @@ err: errno = e ; return 0 ; } - *fd = p[!to] ; return pid ; } diff --git a/src/libstddjb/child_spawn1_pipe.c b/src/libstddjb/child_spawn1_pipe.c new file mode 100644 index 0000000..a436f6e --- /dev/null +++ b/src/libstddjb/child_spawn1_pipe.c @@ -0,0 +1,16 @@ +/* ISC license. */ + +#include <unistd.h> +#include <skalibs/djbunix.h> +#include "djbunix-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 new file mode 100644 index 0000000..46c900f --- /dev/null +++ b/src/libstddjb/child_spawn1_socket.c @@ -0,0 +1,16 @@ +/* ISC license. */ + +#include <skalibs/webipc.h> +#include <skalibs/djbunix.h> +#include "djbunix-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, 1) ; + if (!pid) return 0 ; + *fd = p[0] ; + return pid ; +} diff --git a/src/libstddjb/djbunix-internal.h b/src/libstddjb/djbunix-internal.h new file mode 100644 index 0000000..775cb80 --- /dev/null +++ b/src/libstddjb/djbunix-internal.h @@ -0,0 +1,10 @@ +/* ISC license. */ + +#ifndef DJBUNIX_INTERNAL_H +#define DJBUNIX_INTERNAL_H + +#include <sys/types.h> + +extern pid_t child_spawn1_internal (char const *, char const *const *, char const *const *, int *, int) ; + +#endif |