aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/posix/spawni.c
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2018-09-19 12:14:34 -0700
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2019-01-03 14:38:01 -0200
commit805334b26c7e6e83557234f2008497c72176a6cd (patch)
tree465179b51941e1b06a2134381337d1ecf273b245 /sysdeps/posix/spawni.c
parent03992356e6fedc5a5e9d32df96c1a2c79ea28a8f (diff)
downloadglibc-805334b26c7e6e83557234f2008497c72176a6cd.zip
glibc-805334b26c7e6e83557234f2008497c72176a6cd.tar.gz
glibc-805334b26c7e6e83557234f2008497c72176a6cd.tar.bz2
posix: Clear close-on-exec for posix_spawn adddup2 (BZ#23640)
Austin Group issue #411 [1] proposes that posix_spawn file action posix_spawn_file_actions_adddup2 resets the close-on-exec when source and destination refer to same file descriptor. It solves the issue on multi-thread applications which uses close-on-exec as default, and want to hand-chose specifically file descriptor to purposefully inherited into a child process. Current approach to achieve this scenario is to use two adddup2 file actions and a temporary file description which do not conflict with any other, coupled with a close file action to avoid leaking the temporary file descriptor. This approach, besides being complex, may fail with EMFILE/ENFILE file descriptor exaustion. This can be more easily accomplished with an in-place removal of FD_CLOEXEC. Although the resulting adddup2 semantic is slight different than dup2 (equal file descriptors should be handled as no-op), the proposed possible solution are either more complex (fcntl action which a limited set of operations) or results in unrequired operations (dup3 which also returns EINVAL for same file descriptor). Checked on aarch64-linux-gnu. [BZ #23640] * posix/tst-spawn.c (do_prepare, handle_restart, do_test): Add posix_spawn_file_actions_adddup2 test to check O_CLOCEXEC reset. * sysdeps/unix/sysv/linux/spawni.c (__spawni_child): Add close-on-exec reset for adddup2 file action. * sysdeps/posix/spawni.c (__spawni_child): Likewise. [1] http://austingroupbugs.net/view.php?id=411
Diffstat (limited to 'sysdeps/posix/spawni.c')
-rw-r--r--sysdeps/posix/spawni.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/sysdeps/posix/spawni.c b/sysdeps/posix/spawni.c
index 483dc26..631a179 100644
--- a/sysdeps/posix/spawni.c
+++ b/sysdeps/posix/spawni.c
@@ -204,9 +204,21 @@ __spawni_child (void *arguments)
break;
case spawn_do_dup2:
- if (__dup2 (action->action.dup2_action.fd,
- action->action.dup2_action.newfd)
- != action->action.dup2_action.newfd)
+ /* Austin Group issue #411 requires adddup2 action with source
+ and destination being equal to remove close-on-exec flag. */
+ if (action->action.dup2_action.fd
+ == action->action.dup2_action.newfd)
+ {
+ int fd = action->action.dup2_action.newfd;
+ int flags = __fcntl (fd, F_GETFD, 0);
+ if (flags == -1)
+ goto fail;
+ if (__fcntl (fd, F_SETFD, flags & ~FD_CLOEXEC) == -1)
+ goto fail;
+ }
+ else if (__dup2 (action->action.dup2_action.fd,
+ action->action.dup2_action.newfd)
+ != action->action.dup2_action.newfd)
goto fail;
break;