diff --git a/ChangeLog b/ChangeLog index 98397912f9..cd2ccdbf7e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Sat Oct 29 20:00:26 2011 Tanaka Akira + + * include/ruby/intern.h (rb_cloexec_dup): declared. + + * io.c (rb_cloexec_dup): new function. + (ruby_dup): use rb_cloexec_dup. + + * ext/pty/pty.c (pty_getpty): use rb_cloexec_dup. + + * ext/openssl/ossl_bio.c (ossl_obj2bio): ditto. + Sat Oct 29 16:11:34 2011 Tanaka Akira * ext/sdbm/_sdbm.c (sdbm_prep): use O_CLOEXEC if available. diff --git a/ext/openssl/ossl_bio.c b/ext/openssl/ossl_bio.c index da63c722e5..a11c08c1a3 100644 --- a/ext/openssl/ossl_bio.c +++ b/ext/openssl/ossl_bio.c @@ -25,10 +25,10 @@ ossl_obj2bio(VALUE obj) GetOpenFile(obj, fptr); rb_io_check_readable(fptr); - if ((fd = dup(FPTR_TO_FD(fptr))) < 0){ + if ((fd = rb_cloexec_dup(FPTR_TO_FD(fptr))) < 0){ rb_sys_fail(0); } - rb_fd_set_cloexec(fd); + rb_update_max_fd(fd); if (!(fp = fdopen(fd, "r"))){ close(fd); rb_sys_fail(0); diff --git a/ext/pty/pty.c b/ext/pty/pty.c index 032b2462d0..ac023f70c3 100644 --- a/ext/pty/pty.c +++ b/ext/pty/pty.c @@ -603,10 +603,10 @@ pty_getpty(int argc, VALUE *argv, VALUE self) rfptr->pathv = rb_obj_freeze(rb_str_new_cstr(SlaveName)); wfptr->mode = rb_io_mode_flags("w") | FMODE_SYNC; - wfptr->fd = dup(info.fd); + wfptr->fd = rb_cloexec_dup(info.fd); if (wfptr->fd == -1) rb_sys_fail("dup()"); - rb_fd_set_cloexec(wfptr->fd); + rb_update_max_fd(wfptr->fd); wfptr->pathv = rfptr->pathv; res = rb_ary_new2(3); diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 5f8d1ae929..b1a86586a5 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -503,6 +503,7 @@ void rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds); int rb_pipe(int *pipes); int rb_reserved_fd_p(int fd); int rb_cloexec_open(const char *pathname, int flags, mode_t mode); +int rb_cloexec_dup(int oldfd); #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 98484e8958..ddc89cc67e 100644 --- a/io.c +++ b/io.c @@ -200,6 +200,15 @@ rb_cloexec_open(const char *pathname, int flags, mode_t mode) return ret; } +int +rb_cloexec_dup(int oldfd) +{ + int ret; + ret = dup(oldfd); + 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) @@ -561,17 +570,17 @@ ruby_dup(int orig) { int fd; - fd = dup(orig); + fd = rb_cloexec_dup(orig); if (fd < 0) { if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) { rb_gc(); - fd = dup(orig); + fd = rb_cloexec_dup(orig); } if (fd < 0) { rb_sys_fail(0); } } - rb_fd_set_cloexec(fd); + rb_update_max_fd(fd); return fd; }