summaryrefslogtreecommitdiff
path: root/process.c
diff options
context:
space:
mode:
authorngoto <ngoto@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-15 14:26:22 +0000
committerngoto <ngoto@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-15 14:26:22 +0000
commitd1bce9ec5576598a5d943de10da2162ae8a22f94 (patch)
tree815ae3e106a6a6741fc7dfdb93215a05d9c0f875 /process.c
parentae406d9e09ed3518b39363c21cd8af1e18167cb6 (diff)
* process.c (redirect_close, parent_redirect_close): should not close
reserved FD. It should be closed in the exec system call due to the O_CLOEXEC or FD_CLOEXEC flag. [Bug #11353] [ruby-core:69977] * process.c (close_unless_reserved): new function to close FD unless it is reserved for internal communication. * thread_pthread.c (rb_reserved_fd_p): should check owner_process pid to avoid false positive in forked child process. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51255 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'process.c')
-rw-r--r--process.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/process.c b/process.c
index 12eb624..ac72463 100644
--- a/process.c
+++ b/process.c
@@ -294,6 +294,16 @@ extern ID ruby_static_id_status;
#define ALWAYS_NEED_ENVP 0
#endif
+static inline int close_unless_reserved(fd)
+{
+ /* Do nothing to the reserved fd because it should be closed in exec(2)
+ due to the O_CLOEXEC or FD_CLOEXEC flag. */
+ if (rb_reserved_fd_p(fd)) { /* async-signal-safe */
+ return 0;
+ }
+ return close(fd); /* async-signal-safe */
+}
+
/*#define DEBUG_REDIRECT*/
#if defined(DEBUG_REDIRECT)
@@ -342,7 +352,7 @@ static int
redirect_close(int fd)
{
int ret;
- ret = close(fd);
+ ret = close_unless_reserved(fd);
ttyprintf("close(%d) => %d\n", fd, ret);
return ret;
}
@@ -360,7 +370,7 @@ static int
parent_redirect_close(int fd)
{
int ret;
- ret = close(fd);
+ ret = close_unless_reserved(fd);
ttyprintf("parent_close(%d) => %d\n", fd, ret);
return ret;
}
@@ -368,9 +378,9 @@ parent_redirect_close(int fd)
#else
#define redirect_dup(oldfd) dup(oldfd)
#define redirect_dup2(oldfd, newfd) dup2((oldfd), (newfd))
-#define redirect_close(fd) close(fd)
+#define redirect_close(fd) close_unless_reserved(fd)
#define parent_redirect_open(pathname, flags, perm) rb_cloexec_open((pathname), (flags), (perm))
-#define parent_redirect_close(fd) close(fd)
+#define parent_redirect_close(fd) close_unless_reserved(fd)
#endif
/*