diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libstddjb/child_spawn.c | 47 | ||||
-rw-r--r-- | src/libstddjb/child_spawn0.c | 15 | ||||
-rw-r--r-- | src/libstddjb/child_spawn1_internal.c | 101 | ||||
-rw-r--r-- | src/libstddjb/child_spawn2.c | 39 | ||||
-rw-r--r-- | src/libstddjb/child_spawn3.c | 36 |
5 files changed, 119 insertions, 119 deletions
diff --git a/src/libstddjb/child_spawn.c b/src/libstddjb/child_spawn.c index 58bbfc8..4ac1dde 100644 --- a/src/libstddjb/child_spawn.c +++ b/src/libstddjb/child_spawn.c @@ -21,6 +21,7 @@ #include <skalibs/sig.h> #include <skalibs/strerr2.h> +#include <skalibs/exec.h> #endif @@ -37,25 +38,27 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const *envp, int *fds, unsigned int n) { + pid_t pid ; #ifdef SKALIBS_HASPOSIXSPAWN posix_spawn_file_actions_t actions ; posix_spawnattr_t attr ; + int e ; #else int syncpipe[2] ; #endif int p[n ? n : 1][2] ; - pid_t pid ; - int e ; size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; unsigned int i = 0 ; char modifs[m + 1 + n * UINT_FMT] ; memcpy(modifs, SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=", sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR)) ; - for (; i < n ; i++) if (pipe(p[i]) < 0) { e = errno ; goto errpi ; } - for (i = 0 ; i < n ; i++) + for (; i < n ; i++) + { + if (pipe(p[i]) < 0) goto errpi ; if ((ndelay_on(p[i][i & 1]) < 0) || (coe(p[i][i & 1]) < 0)) { - e = errno ; goto errp ; + i++ ; goto errpi ; } + } for (i = 2 ; i < n ; i++) { m += uint_fmt(modifs + m, p[i][!(i & 1)]) ; @@ -66,7 +69,7 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const #ifdef SKALIBS_HASPOSIXSPAWN e = posix_spawnattr_init(&attr) ; - if (e) goto errsp ; + if (e) goto erre ; { sigset_t set ; sigemptyset(&set) ; @@ -92,16 +95,16 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const if (e) goto erractions ; } { - int haspath = !!getenv("PATH") ; + int nopath = !getenv("PATH") ; size_t envlen = env_len(envp) ; char const *newenv[envlen + 2] ; - if (!env_merge(newenv, envlen+2, envp, envlen, modifs, m)) goto erractions ; - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) + 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; if (e) goto erractions ; } @@ -109,11 +112,10 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const posix_spawnattr_destroy(&attr) ; #else - if (pipe(syncpipe) < 0) { e = errno ; goto errp ; } - if (coe(syncpipe[1]) < 0) { e = errno ; goto errsp ; } + if (pipecoe(syncpipe) < 0) goto errp ; pid = fork() ; - if (pid < 0) { e = errno ; goto errsp ; } + if (pid < 0) goto errsp ; else if (!pid) { size_t len = strlen(PROG) ; @@ -121,7 +123,6 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const memcpy(name, PROG, len) ; memcpy(name + len, " (child)", 9) ; PROG = name ; - fd_close(syncpipe[0]) ; if (n >= 2) { if (fd_move2(0, p[1][0], 1, p[0][1]) < 0) goto syncdie ; @@ -131,7 +132,7 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const if (fd_move(1, p[0][1]) < 0) goto syncdie ; } sig_blocknone() ; - pathexec_r_name(prog, argv, envp, env_len(envp), modifs, m) ; + mexec_aen(prog, argv, envp, modifs, m, 1) ; syncdie: { @@ -147,13 +148,10 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; if (syncpipe[1]) { + int e = c ; if (syncpipe[1] < 0) e = errno ; - else - { - kill(pid, SIGKILL) ; - e = c ; - } - wait_pid(pid, &syncpipe[1]) ; + if (wait_pid(pid, &syncpipe[1]) < 0) e = errno ; + errno = e ; goto errsp0 ; } } @@ -172,14 +170,16 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const posix_spawn_file_actions_destroy(&actions) ; errattr: posix_spawnattr_destroy(&attr) ; + erre: + errno = e ; #endif - errsp: #ifndef SKALIBS_HASPOSIXSPAWN + errsp: fd_close(syncpipe[1]) ; errsp0: fd_close(syncpipe[0]) ; -#endif errp: +#endif i = n ; errpi: while (i--) @@ -187,6 +187,5 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const fd_close(p[i][1]) ; fd_close(p[i][0]) ; } - errno = e ; return 0 ; } diff --git a/src/libstddjb/child_spawn0.c b/src/libstddjb/child_spawn0.c index 31d44e2..7b95b9f 100644 --- a/src/libstddjb/child_spawn0.c +++ b/src/libstddjb/child_spawn0.c @@ -8,14 +8,15 @@ #include <signal.h> #include <spawn.h> #include <stdlib.h> + #include <skalibs/config.h> pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const *envp) { + pid_t pid ; posix_spawnattr_t attr ; int e ; - pid_t pid ; - int haspath = !!getenv("PATH") ; + int nopath = !getenv("PATH") ; e = posix_spawnattr_init(&attr) ; if (e) goto err ; { @@ -26,9 +27,9 @@ pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const e = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ; if (e) goto errattr ; } - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; posix_spawnattr_destroy(&attr) ; if (e) goto err ; return pid ; @@ -44,16 +45,18 @@ pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const #include <unistd.h> #include <string.h> + #include <skalibs/allreadwrite.h> #include <skalibs/strerr2.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 e ; int p[2] ; - pid_t pid ; if (pipecoe(p) < 0) return 0 ; pid = fork() ; if (pid < 0) @@ -71,7 +74,7 @@ pid_t child_spawn0 (char const *prog, char const *const *argv, char const *const PROG = name ; fd_close(p[0]) ; sig_blocknone() ; - pathexec_run(prog, argv, envp) ; + exec_ae(prog, argv, envp) ; e = errno ; fd_write(p[1], (char *)&e, sizeof(e)) ; _exit(127) ; diff --git a/src/libstddjb/child_spawn1_internal.c b/src/libstddjb/child_spawn1_internal.c index 116a65e..460bca0 100644 --- a/src/libstddjb/child_spawn1_internal.c +++ b/src/libstddjb/child_spawn1_internal.c @@ -9,19 +9,20 @@ #include <signal.h> #include <spawn.h> #include <stdlib.h> + #include <skalibs/config.h> #include <skalibs/djbunix.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 ; int e ; - pid_t pid ; - int haspath = !!getenv("PATH") ; - if (coe(p[!(to & 1)]) < 0) { e = errno ; goto err ; } + int nopath = !getenv("PATH") ; + if (coe(p[!(to & 1)]) < 0) goto err ; e = posix_spawnattr_init(&attr) ; - if (e) goto err ; + if (e) goto erre ; { sigset_t set ; sigemptyset(&set) ; @@ -44,55 +45,45 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con e = posix_spawn_file_actions_adddup2(&actions, to & 1, !(to & 1)) ; if (e) goto erractions ; } - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) { e = errno ; 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; + if (e) goto erractions ; posix_spawn_file_actions_destroy(&actions) ; posix_spawnattr_destroy(&attr) ; fd_close(p[to & 1]) ; - if (e) goto errp ; return pid ; erractions: posix_spawn_file_actions_destroy(&actions) ; errattr: posix_spawnattr_destroy(&attr) ; - err: - fd_close(p[to & 1]) ; - errp: - fd_close(p[!(to & 1)]) ; + erre: errno = e ; + err: + fd_close(p[1]) ; + fd_close(p[0]) ; return 0 ; } #else #include <string.h> + #include <skalibs/allreadwrite.h> #include <skalibs/strerr2.h> #include <skalibs/sig.h> #include <skalibs/djbunix.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) { - int e ; - int syncp[2] ; pid_t pid ; - if (coe(p[0]) < 0 || pipecoe(syncp) < 0) - { - fd_close(p[1]) ; - fd_close(p[0]) ; - return 0 ; - } + int syncpipe[2] ; + if (coe(p[0]) < 0 || pipecoe(syncpipe) < 0) goto err ; + pid = fork() ; - if (pid < 0) - { - fd_close(syncp[1]) ; - fd_close(syncp[0]) ; - fd_close(p[1]) ; - fd_close(p[0]) ; - return 0 ; - } + if (pid < 0) goto errsp ; if (!pid) { size_t len = strlen(PROG) ; @@ -100,35 +91,47 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con memcpy(name, PROG, len) ; memcpy(name + len, " (child)", 9) ; PROG = name ; - fd_close(syncp[0]) ; fd_close(p[!(to & 1)]) ; - if (fd_move(to & 1, p[to & 1]) < 0) goto err ; - if ((to & 2) && (fd_copy(!(to & 1), to & 1) < 0)) goto err ; + 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() ; - pathexec_run(prog, argv, envp) ; - err: - e = errno ; - fd_write(syncp[1], (char *)&e, sizeof(e)) ; + exec_ae(prog, argv, envp) ; + + syncdie: + { + char c = errno ; + fd_write(syncpipe[1], &c, 1) ; + } _exit(127) ; } - fd_close(syncp[1]) ; - fd_close(p[to & 1]) ; - syncp[1] = fd_read(syncp[0], (char *)&e, sizeof(e)) ; - if (syncp[1] < 0) + + fd_close(syncpipe[1]) ; + { - fd_close(syncp[0]) ; - fd_close(p[!(to & 1)]) ; - return 0 ; - } - fd_close(syncp[0]) ; - if (syncp[1] == sizeof(e)) - { - fd_close(p[!(to & 1)]) ; - wait_pid(pid, &syncp[1]) ; - errno = e ; - return 0 ; + char c ; + syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; + if (syncpipe[1]) + { + int e = c ; + if (syncpipe[1] < 0) e = errno ; + if (wait_pid(pid, &syncpipe[1]) < 0) 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_spawn2.c b/src/libstddjb/child_spawn2.c index 4e7d0a5..03db0d5 100644 --- a/src/libstddjb/child_spawn2.c +++ b/src/libstddjb/child_spawn2.c @@ -22,6 +22,7 @@ #include <skalibs/allreadwrite.h> #include <skalibs/sig.h> #include <skalibs/strerr2.h> +#include <skalibs/exec.h> #endif @@ -29,23 +30,23 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const *envp, int *fds) { + pid_t pid ; #ifdef SKALIBS_HASPOSIXSPAWN posix_spawn_file_actions_t actions ; posix_spawnattr_t attr ; + int e ; #else int syncpipe[2] ; #endif int p[2][2] ; - pid_t pid ; - int e ; if (pipe(p[0]) < 0) return 0 ; - if (ndelay_on(p[0][0]) < 0 || coe(p[0][0]) < 0 || pipe(p[1]) < 0) { e = errno ; goto errp ; } - if (ndelay_on(p[1][1]) < 0 || coe(p[1][1]) < 0) { e = errno ; goto errp1 ; } + 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 ; #ifdef SKALIBS_HASPOSIXSPAWN e = posix_spawnattr_init(&attr) ; - if (e) goto errsp ; + if (e) goto erre ; { sigset_t set ; sigemptyset(&set) ; @@ -71,13 +72,13 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const if (e) goto erractions ; } { - int haspath = !!getenv("PATH") ; - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) + int nopath = !getenv("PATH") ; + 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; if (e) goto erractions ; } @@ -85,11 +86,10 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const posix_spawnattr_destroy(&attr) ; #else - if (pipe(syncpipe) < 0) { e = errno ; goto errp1 ; } - if (coe(syncpipe[1]) < 0) { e = errno ; goto errsp ; } + if (pipecoe(syncpipe) < 0) goto errp1 ; pid = fork() ; - if (pid < 0) { e = errno ; goto errsp ; } + if (pid < 0) goto errsp ; else if (!pid) { size_t len = strlen(PROG) ; @@ -97,10 +97,9 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const memcpy(name, PROG, len) ; memcpy(name + len, " (child)", 9) ; PROG = name ; - fd_close(syncpipe[0]) ; if (fd_move2(fds[0], p[1][0], fds[1], p[0][1]) < 0) goto syncdie ; sig_blocknone() ; - pathexec_run(prog, argv, envp) ; + exec_ae(prog, argv, envp) ; syncdie: { @@ -116,13 +115,10 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; if (syncpipe[1]) { + int e = c ; if (syncpipe[1] < 0) e = errno ; - else - { - kill(pid, SIGKILL) ; - e = c ; - } - wait_pid(pid, &syncpipe[1]) ; + if (wait_pid(pid, &syncpipe[1]) < 0) e = errno ; + errno = e ; goto errsp0 ; } } @@ -138,9 +134,11 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const posix_spawn_file_actions_destroy(&actions) ; errattr: posix_spawnattr_destroy(&attr) ; + erre: + errno = e ; #endif - errsp: #ifndef SKALIBS_HASPOSIXSPAWN + errsp: fd_close(syncpipe[1]) ; errsp0: fd_close(syncpipe[0]) ; @@ -151,6 +149,5 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const errp: fd_close(p[0][1]) ; fd_close(p[0][0]) ; - errno = e ; return 0 ; } diff --git a/src/libstddjb/child_spawn3.c b/src/libstddjb/child_spawn3.c index 9dbed41..e1e433a 100644 --- a/src/libstddjb/child_spawn3.c +++ b/src/libstddjb/child_spawn3.c @@ -21,6 +21,7 @@ #include <string.h> #include <skalibs/sig.h> #include <skalibs/strerr2.h> +#include <skalibs/exec.h> #endif @@ -31,15 +32,15 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const *envp, int *fds) { + pid_t pid ; #ifdef SKALIBS_HASPOSIXSPAWN posix_spawn_file_actions_t actions ; posix_spawnattr_t attr ; + int e ; #else int syncpipe[2] ; #endif int p[3][2] ; - pid_t pid ; - int e ; size_t m = sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ; char modifs[sizeof(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) + UINT_FMT] = SKALIBS_CHILD_SPAWN_FDS_ENVVAR "=" ; if (pipe(p[0]) < 0 || ndelay_on(p[0][0]) < 0 || coe(p[0][0]) < 0) return 0 ; @@ -51,7 +52,7 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const #ifdef SKALIBS_HASPOSIXSPAWN e = posix_spawnattr_init(&attr) ; - if (e) goto errp2 ; + if (e) goto erre ; { sigset_t set ; sigemptyset(&set) ; @@ -77,16 +78,16 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const if (e) goto erractions ; } { - int haspath = !!getenv("PATH") ; + int nopath = !getenv("PATH") ; size_t envlen = env_len(envp) ; char const *newenv[envlen + 2] ; - if (!env_merge(newenv, envlen+2, envp, envlen, modifs, m)) goto erractions ; - if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) + 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 (!haspath) unsetenv("PATH") ; + if (nopath) unsetenv("PATH") ; if (e) goto erractions ; } @@ -94,11 +95,10 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const posix_spawnattr_destroy(&attr) ; #else - if (pipe(syncpipe) < 0) { e = errno ; goto errp2 ; } - if (coe(syncpipe[1]) < 0) { e = errno ; goto errsp ; } + if (pipecoe(syncpipe) < 0) goto errp2 ; pid = fork() ; - if (pid < 0) { e = errno ; goto errsp ; } + if (pid < 0) goto errsp ; else if (!pid) { size_t len = strlen(PROG) ; @@ -106,10 +106,9 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const memcpy(name, PROG, len) ; memcpy(name + len, " (child)", 9) ; PROG = name ; - fd_close(syncpipe[0]) ; if (fd_move2(fds[0], p[1][0], fds[1], p[0][1]) < 0) goto syncdie ; sig_blocknone() ; - pathexec_r_name(prog, argv, envp, env_len(envp), modifs, m) ; + mexec_aen(prog, argv, envp, modifs, m, 1) ; syncdie: { @@ -125,13 +124,10 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const syncpipe[1] = fd_read(syncpipe[0], &c, 1) ; if (syncpipe[1]) { + int e = c ; if (syncpipe[1] < 0) e = errno ; - else - { - kill(pid, SIGKILL) ; - e = c ; - } - wait_pid(pid, &syncpipe[1]) ; + if (wait_pid(pid, &syncpipe[1]) < 0) e = errno ; + errno = e ; goto errsp0 ; } } @@ -151,14 +147,16 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const posix_spawn_file_actions_destroy(&actions) ; errattr: posix_spawnattr_destroy(&attr) ; + erre: + errno = e ; #endif #ifndef SKALIBS_HASPOSIXSPAWN errsp: fd_close(syncpipe[1]) ; errsp0: fd_close(syncpipe[0]) ; -#endif errp2: +#endif fd_close(p[2][1]) ; fd_close(p[2][0]) ; errp1: |