aboutsummaryrefslogtreecommitdiff
path: root/shell/ash.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-03-28 18:35:07 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-03-28 18:35:07 +0200
commit9acd63c92cbc4bd0a3ddbb6a61e512eadc29239d (patch)
tree9bf53c82f5fccd4a5ed13915b8039080c563ce07 /shell/ash.c
parente4defe826be49b8ef19912ba4cb291bfe9166e0f (diff)
downloadbusybox-9acd63c92cbc4bd0a3ddbb6a61e512eadc29239d.tar.gz
ash,hush: fix "saved" redirected fds still visible in children
Based on a patch by Mark Marshall <mark.marshall@omicronenergy.com> function old new delta dup_CLOEXEC - 49 +49 fcntl_F_DUPFD 46 - -46 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/ash.c')
-rw-r--r--shell/ash.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 5e281b5ce..85690e555 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -255,6 +255,9 @@ typedef long arith_t;
# define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
#endif
+#ifndef F_DUPFD_CLOEXEC
+# define F_DUPFD_CLOEXEC F_DUPFD
+#endif
#ifndef PIPE_BUF
# define PIPE_BUF 4096 /* amount of buffering in a pipe */
#endif
@@ -5448,12 +5451,15 @@ dup2_or_raise(int from, int to)
return newfd;
}
static int
-fcntl_F_DUPFD(int fd, int avoid_fd)
+dup_CLOEXEC(int fd, int avoid_fd)
{
int newfd;
repeat:
- newfd = fcntl(fd, F_DUPFD, avoid_fd + 1);
- if (newfd < 0) {
+ newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
+ if (newfd >= 0) {
+ if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
+ fcntl(newfd, F_SETFD, FD_CLOEXEC);
+ } else { /* newfd < 0 */
if (errno == EBUSY)
goto repeat;
if (errno == EINTR)
@@ -5569,7 +5575,7 @@ save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq)
for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) {
/* If we collide with an already moved fd... */
if (fd == sq->two_fd[i].moved_to) {
- new_fd = fcntl_F_DUPFD(fd, avoid_fd);
+ new_fd = dup_CLOEXEC(fd, avoid_fd);
sq->two_fd[i].moved_to = new_fd;
TRACE(("redirect_fd %d: already busy, moving to %d\n", fd, new_fd));
if (new_fd < 0) /* what? */
@@ -5584,7 +5590,7 @@ save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq)
}
/* If this fd is open, we move and remember it; if it's closed, new_fd = CLOSED (-1) */
- new_fd = fcntl_F_DUPFD(fd, avoid_fd);
+ new_fd = dup_CLOEXEC(fd, avoid_fd);
TRACE(("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, new_fd));
if (new_fd < 0) {
if (errno != EBADF)