[PATCH 1/2] ash: Set the CLOEXEC bit in fcntl_F_DUPFD

mark.marshall at omicronenergy.com mark.marshall at omicronenergy.com
Mon Mar 19 09:31:30 UTC 2018


From: Mark Marshall <mark.marshall at omicronenergy.com>

The function fcntl_F_DUPFD was introduced as some re-factoring at commit
035486c7500c09616a6c1040d8e70923532a5c2d
"ash: significant overhaul of redirect saving logic".
The old code used to set the CLOEXEC bit, but this got lost in the
re-factoring, so this change makes the new code do the same.

This was found because if a script was run via ssh, and this script ran
a second script, redirecting its stdin and stdout, and putting it into
the background, the ssh would hang waiting for the backgrounded script
to terminate.  From /proc/999/fd it can be seen that the background script
still has the stdin pipe from sshd open (at file handle 10+).

Signed-off-by: Mark Marshall <mark.marshall at omicronenergy.com>
---
 shell/ash.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/shell/ash.c b/shell/ash.c
index 5e281b5..6cf980c 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -5452,13 +5452,20 @@ fcntl_F_DUPFD(int fd, int avoid_fd)
 {
 	int newfd;
  repeat:
+#if defined(F_DUPFD_CLOEXEC)
+	newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
+#else
 	newfd = fcntl(fd, F_DUPFD, avoid_fd + 1);
+#endif
 	if (newfd < 0) {
 		if (errno == EBUSY)
 			goto repeat;
 		if (errno == EINTR)
 			goto repeat;
 	}
+#if !defined(F_DUPFD_CLOEXEC)
+	close_on_exec_on(newfd);
+#endif
 	return newfd;
 }
 static int
-- 
2.7.4



More information about the busybox mailing list