diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-22 09:58:15 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-22 09:58:15 +0000 |
commit | b574a4d4a1f1d77fa0224b02774456107cdbc932 (patch) | |
tree | 000945919a3486eb709a026a09688017e73b2ce8 /io.c | |
parent | 00715f4c5aa12eec0dd88fb08bc7f46d23de8d40 (diff) | |
download | ruby-b574a4d4a1f1d77fa0224b02774456107cdbc932.tar.gz |
* include/ruby/intern.h (rb_fd_set_cloexec): declared.
* io.c (rb_fd_set_cloexec): new function.
(ruby_dup): call rb_fd_set_cloexec to set close-on-exec flag.
(rb_sysopen_internal): ditto.
(rb_pipe): ditto.
(io_reopen): ditto.
(io_cntl): ditto.
* process.c (rb_f_exec): change the default :close_others option to
true.
(rb_f_system): ditto.
(move_fds_to_avoid_crash): call rb_fd_set_cloexec to set
close-on-exec flag.
(ruby_setsid): ditto.
(rb_daemon): ditto.
* thread_pthread.c (rb_thread_create_timer_thread): call
rb_fd_set_cloexec to set close-on-exec flag.
* ruby.c (load_file_internal): ditto.
* file.c (rb_file_s_truncate): ditto.
(file_load_ok): ditto.
* random.c (fill_random_seed): ditto.
* ext/pty/pty.c (chfunc): ditto.
(get_device_once): ditto.
* ext/openssl/ossl_bio.c (ossl_obj2bio): ditto.
* ext/socket/init.c (rsock_socket): ditto.
(rsock_s_accept_nonblock): ditto.
(rsock_s_accept): ditto.
* ext/socket/socket.c (rsock_sock_s_socketpair): ditto.
* ext/socket/ancdata.c (discard_cmsg): ditto.
(make_io_for_unix_rights): ditto.
* ext/socket/unixsocket.c (unix_recv_io): ditto.
* ext/io/console/console.c (console_dev): ditto.
[ruby-core:38140] [Feature #5041]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33507 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r-- | io.c | 33 |
1 files changed, 26 insertions, 7 deletions
@@ -157,6 +157,25 @@ rb_update_max_fd(int fd) if (max_file_descriptor < fd) max_file_descriptor = fd; } +void rb_fd_set_cloexec(int fd) +{ + int flags, ret; + flags = fcntl(fd, F_GETFD); /* should not fail except EBADF. */ + if (flags == -1) { + rb_bug("rb_fd_set_cloexec: fcntl(%d, F_GETFD) failed: %s", fd, strerror(errno)); + } + if (2 < fd) { + if (!(flags & FD_CLOEXEC)) { + flags |= FD_CLOEXEC; + ret = fcntl(fd, F_SETFD, flags); + if (ret == -1) { + rb_bug("rb_fd_set_cloexec: fcntl(%d, F_SETFD, %d) failed: %s", fd, flags, strerror(errno)); + } + } + } + if (max_file_descriptor < fd) max_file_descriptor = fd; +} + #define argf_of(obj) (*(struct argf *)DATA_PTR(obj)) #define ARGF argf_of(argf) @@ -527,7 +546,7 @@ ruby_dup(int orig) rb_sys_fail(0); } } - rb_update_max_fd(fd); + rb_fd_set_cloexec(fd); return fd; } @@ -4591,7 +4610,7 @@ rb_sysopen_internal(struct sysopen_struct *data) int fd; fd = (int)rb_thread_blocking_region(sysopen_func, data, RUBY_UBF_IO, 0); if (0 <= fd) - rb_update_max_fd(fd); + rb_fd_set_cloexec(fd); return fd; } @@ -4919,8 +4938,8 @@ rb_pipe(int *pipes) } #endif if (ret == 0) { - rb_update_max_fd(pipes[0]); - rb_update_max_fd(pipes[1]); + rb_fd_set_cloexec(pipes[0]); + rb_fd_set_cloexec(pipes[1]); } return ret; } @@ -5802,7 +5821,7 @@ io_reopen(VALUE io, VALUE nfile) /* need to keep FILE objects of stdin, stdout and stderr */ if (dup2(fd2, fd) < 0) rb_sys_fail_path(orig->pathv); - rb_update_max_fd(fd); + rb_fd_set_cloexec(fd); } else { fclose(fptr->stdio_file); @@ -5810,7 +5829,7 @@ io_reopen(VALUE io, VALUE nfile) fptr->fd = -1; if (dup2(fd2, fd) < 0) rb_sys_fail_path(orig->pathv); - rb_update_max_fd(fd); + rb_fd_set_cloexec(fd); fptr->fd = fd; } rb_thread_fd_close(fd); @@ -7710,7 +7729,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_update_max_fd(retval); + rb_fd_set_cloexec(retval); } #endif |