From 5c19f077008f6e6633e15a8d15fceb4b17d37208 Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 30 Oct 2011 15:17:27 +0000 Subject: [PATCH] * include/ruby/intern.h (rb_cloexec_fcntl_dupfd): declared. * io.c (rb_cloexec_fcntl_dupfd): new function. (nogvl_io_cntl): use rb_cloexec_fcntl_dupfd. * process.c (move_fds_to_avoid_crash): use rb_cloexec_fcntl_dupfd. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++++ include/ruby/intern.h | 1 + io.c | 17 +++++++++++++++-- process.c | 4 ++-- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6ca2f49c90..07c9188bd6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Mon Oct 31 00:16:11 2011 Tanaka Akira + + * include/ruby/intern.h (rb_cloexec_fcntl_dupfd): declared. + + * io.c (rb_cloexec_fcntl_dupfd): new function. + (nogvl_io_cntl): use rb_cloexec_fcntl_dupfd. + + * process.c (move_fds_to_avoid_crash): use rb_cloexec_fcntl_dupfd. + Sun Oct 30 22:46:46 2011 Tanaka Akira * configure.in: check pipe2. diff --git a/include/ruby/intern.h b/include/ruby/intern.h index de2fdbd57b..aa24d435f6 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -506,6 +506,7 @@ int rb_cloexec_open(const char *pathname, int flags, mode_t mode); int rb_cloexec_dup(int oldfd); int rb_cloexec_dup2(int oldfd, int newfd); int rb_cloexec_pipe(int fildes[2]); +int rb_cloexec_fcntl_dupfd(int fd, int minfd); #define RB_RESERVED_FD_P(fd) rb_reserved_fd_p(fd) void rb_update_max_fd(int fd); void rb_fd_set_cloexec(int fd); diff --git a/io.c b/io.c index 2fb9ee5b63..7d31dce478 100644 --- a/io.c +++ b/io.c @@ -299,6 +299,16 @@ rb_cloexec_pipe(int fildes[2]) return ret; } +int +rb_cloexec_fcntl_dupfd(int fd, int minfd) +{ + int ret; + ret = fcntl(fd, F_DUPFD, minfd); + if (ret == -1) return -1; + fd_set_cloexec(ret); + return ret; +} + #define argf_of(obj) (*(struct argf *)DATA_PTR(obj)) #define ARGF argf_of(argf) @@ -7821,7 +7831,10 @@ static VALUE nogvl_io_cntl(void *ptr) if (arg->io_p) return (VALUE)ioctl(arg->fd, arg->cmd, arg->narg); else - return (VALUE)fcntl(arg->fd, arg->cmd, arg->narg); + if (arg->cmd == F_DUPFD) + return (VALUE)rb_cloexec_fcntl_dupfd(arg->fd, arg->narg); + else + return (VALUE)fcntl(arg->fd, arg->cmd, arg->narg); } static int @@ -7844,7 +7857,7 @@ io_cntl(int fd, int cmd, long narg, int io_p) retval = (int)rb_thread_io_blocking_region(nogvl_io_cntl, &arg, fd); #if defined(F_DUPFD) if (!io_p && retval != -1 && cmd == F_DUPFD) { - rb_fd_set_cloexec(retval); + rb_update_max_fd(retval); } #endif diff --git a/process.c b/process.c index 1e91743648..b0f7ae85a2 100644 --- a/process.c +++ b/process.c @@ -2508,10 +2508,10 @@ move_fds_to_avoid_crash(int *fdp, int n, VALUE fds) min = fdp[i]+1; while (RTEST(rb_hash_lookup(fds, INT2FIX(min)))) min++; - ret = fcntl(fdp[i], F_DUPFD, min); + ret = rb_cloexec_fcntl_dupfd(fdp[i], min); if (ret == -1) return -1; - rb_fd_set_cloexec(ret); + rb_update_max_fd(ret); close(fdp[i]); fdp[i] = ret; }