skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit c61359571fd3549ed69cd2e3b80e31bc9f9f0514
parent eabd95d0385c9600ed5b75d37398498c19f64d9d
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Thu, 18 Dec 2014 21:08:14 +0000

 Fix fd management in new child_spawn1, make socket nb for skaclient

Diffstat:
Msrc/libstddjb/child_spawn1_internal.c | 30+++++++++++++++++-------------
Msrc/libstddjb/child_spawn1_socket.c | 2+-
Msrc/libunixonacid/skaclient_startf_async.c | 2+-
3 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/src/libstddjb/child_spawn1_internal.c b/src/libstddjb/child_spawn1_internal.c @@ -21,8 +21,7 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con int e ; pid_t pid ; int haspath = !!env_get("PATH") ; - to = !!to ; - if (coe(p[!to]) < 0) { e = errno ; goto err ; } + if (coe(p[!(to & 1)]) < 0) { e = errno ; goto err ; } e = posix_spawnattr_init(&attr) ; if (e) goto err ; { @@ -33,16 +32,21 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con if (e) goto errattr ; e = posix_spawn_file_actions_init(&actions) ; if (e) goto errattr ; - e = posix_spawn_file_actions_adddup2(&actions, p[to], to) ; + 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]) ; + e = posix_spawn_file_actions_addclose(&actions, p[to & 1]) ; if (e) goto erractions ; + if (to & 2) + { + 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 ; } 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]) ; + fd_close(p[to & 1]) ; if (e) goto errp ; return pid ; @@ -51,9 +55,9 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con errattr: posix_spawnattr_destroy(&attr) ; err: - fd_close(p[to]) ; + fd_close(p[to & 1]) ; errp: - fd_close(p[!to]) ; + fd_close(p[!(to & 1)]) ; errno = e ; return 0 ; } @@ -76,7 +80,6 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con errno = e ; return 0 ; } - to = !!to ; pid = fork() ; if (pid < 0) { @@ -91,8 +94,9 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con if (pid) { fd_close(syncp[0]) ; - fd_close(p[!to]) ; - if (fd_move(to, p[to]) < 0) goto err ; + 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 ; sig_blocknone() ; pathexec_run(prog, argv, envp) ; err: @@ -101,20 +105,20 @@ err: _exit(127) ; } fd_close(syncp[1]) ; - fd_close(p[to]) ; + fd_close(p[to & 1]) ; syncp[1] = fd_read(syncp[0], (char *)&e, sizeof(e)) ; if (syncp[1] < 0) { e = errno ; fd_close(syncp[0]) ; - fd_close(p[!to]) ; + fd_close(p[!(to & 1)]) ; errno = e ; return 0 ; } fd_close(syncp[0]) ; if (syncp[1] == sizeof(e)) { - fd_close(p[!to]) ; + fd_close(p[!(to & 1)]) ; wait_pid(pid, &syncp[1]) ; errno = e ; return 0 ; diff --git a/src/libstddjb/child_spawn1_socket.c b/src/libstddjb/child_spawn1_socket.c @@ -9,7 +9,7 @@ pid_t child_spawn1_socket (char const *prog, char const *const *argv, char const pid_t pid ; int p[2] ; if (ipc_pair_b(p) < 0) return 0 ; - pid = child_spawn1_internal(prog, argv, envp, p, 1) ; + pid = child_spawn1_internal(prog, argv, envp, p, 3) ; if (!pid) return 0 ; *fd = p[0] ; return pid ; diff --git a/src/libunixonacid/skaclient_startf_async.c b/src/libunixonacid/skaclient_startf_async.c @@ -34,7 +34,7 @@ int skaclient_startf_async ( int fd ; pid_t pid = child_spawn1_socket(prog, argv, envp, &fd) ; if (!pid) return 0 ; - if (!skaclient_init(a, fd, bufss, bufsn, auxbufss, auxbufsn, bufas, bufan, auxbufas, auxbufan, q, qlen, before, beforelen)) + if (ndelay_on(fd) < 0 || !skaclient_init(a, fd, bufss, bufsn, auxbufss, auxbufsn, bufas, bufan, auxbufas, auxbufan, q, qlen, before, beforelen)) { register int e = errno ; fd_close(fd) ;