summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-10-22 09:58:15 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-10-22 09:58:15 +0000
commitb574a4d4a1f1d77fa0224b02774456107cdbc932 (patch)
tree000945919a3486eb709a026a09688017e73b2ce8
parent00715f4c5aa12eec0dd88fb08bc7f46d23de8d40 (diff)
* 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
-rw-r--r--ChangeLog49
-rw-r--r--NEWS9
-rw-r--r--ext/io/console/console.c4
-rw-r--r--ext/openssl/ossl_bio.c2
-rw-r--r--ext/pty/pty.c26
-rw-r--r--ext/socket/ancdata.c4
-rw-r--r--ext/socket/init.c6
-rw-r--r--ext/socket/socket.c4
-rw-r--r--ext/socket/unixsocket.c2
-rw-r--r--file.c4
-rw-r--r--include/ruby/intern.h1
-rw-r--r--io.c33
-rw-r--r--process.c13
-rw-r--r--random.c2
-rw-r--r--ruby.c2
-rw-r--r--test/ruby/test_io.rb6
-rw-r--r--test/ruby/test_process.rb8
-rw-r--r--thread_pthread.c4
18 files changed, 133 insertions, 46 deletions
diff --git a/ChangeLog b/ChangeLog
index e5ddf9ca2c..56f3bb58af 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,52 @@
+Sat Oct 22 18:49:24 2011 Tanaka Akira <akr@fsij.org>
+
+ * 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]
+
Sat Oct 22 17:46:27 2011 Tanaka Akira <akr@fsij.org>
* lib/resolv.rb: fix a exception name in previous patch.
diff --git a/NEWS b/NEWS
index 9d96f5e86a..b52a7d14a2 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,9 @@ with all sufficient information, see the ChangeLog file.
* Kernel
* extended method:
* Kernel#warn accepts multiple args in like puts.
+ * incompatible changes:
+ * system() and exec() closes non-standard file descriptors
+ (The default of :close_others option is changed to true by default.)
* Signal
* incompatible changes:
@@ -48,3 +51,9 @@ with all sufficient information, see the ChangeLog file.
* Signal.trap
See above.
+
+ * incompatible changes:
+ The :close_others option is true by default for system() and exec().
+ Also, the close-on-exec flag is set by default for all new file descriptors.
+ This means file descriptors doesn't inherit to spawned process unless
+ explicitly requested such as system(..., fd=>fd).
diff --git a/ext/io/console/console.c b/ext/io/console/console.c
index e7983bbcb9..72f507210c 100644
--- a/ext/io/console/console.c
+++ b/ext/io/console/console.c
@@ -564,7 +564,7 @@ console_dev(VALUE klass)
#ifdef CONSOLE_DEVICE_FOR_WRITING
fd = open(CONSOLE_DEVICE_FOR_WRITING, O_WRONLY);
if (fd < 0) return Qnil;
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
args[1] = INT2FIX(O_WRONLY);
args[0] = INT2NUM(fd);
out = rb_class_new_instance(2, args, klass);
@@ -576,7 +576,7 @@ console_dev(VALUE klass)
#endif
return Qnil;
}
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
args[1] = INT2FIX(O_RDWR);
args[0] = INT2NUM(fd);
con = rb_class_new_instance(2, args, klass);
diff --git a/ext/openssl/ossl_bio.c b/ext/openssl/ossl_bio.c
index ed7c0a71a5..da63c722e5 100644
--- a/ext/openssl/ossl_bio.c
+++ b/ext/openssl/ossl_bio.c
@@ -28,7 +28,7 @@ ossl_obj2bio(VALUE obj)
if ((fd = dup(FPTR_TO_FD(fptr))) < 0){
rb_sys_fail(0);
}
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(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 b18eb68f77..8a6994dea0 100644
--- a/ext/pty/pty.c
+++ b/ext/pty/pty.c
@@ -177,7 +177,7 @@ chfunc(void *data, char *errbuf, size_t errbuf_len)
{
int i = open("/dev/tty", O_RDONLY);
if (i < 0) ERROR_EXIT("/dev/tty");
- rb_update_max_fd(i);
+ rb_fd_set_cloexec(i);
if (ioctl(i, TIOCNOTTY, (char *)0))
ERROR_EXIT("ioctl(TIOCNOTTY)");
close(i);
@@ -199,7 +199,7 @@ chfunc(void *data, char *errbuf, size_t errbuf_len)
if (slave < 0) {
ERROR_EXIT("open: pty slave");
}
- rb_update_max_fd(slave);
+ rb_fd_set_cloexec(slave);
close(master);
#endif
dup2(slave,0);
@@ -291,7 +291,7 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
sigemptyset(&dfl.sa_mask);
if ((masterfd = posix_openpt(O_RDWR|O_NOCTTY)) == -1) goto error;
- rb_update_max_fd(masterfd);
+ rb_fd_set_cloexec(masterfd);
if (sigaction(SIGCHLD, &dfl, &old) == -1) goto error;
if (grantpt(masterfd) == -1) goto grantpt_error;
if (sigaction(SIGCHLD, &old, NULL) == -1) goto error;
@@ -299,7 +299,7 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
if ((slavedevice = ptsname(masterfd)) == NULL) goto error;
if (no_mesg(slavedevice, nomesg) == -1) goto error;
if ((slavefd = open(slavedevice, O_RDWR|O_NOCTTY, 0)) == -1) goto error;
- rb_update_max_fd(slavefd);
+ rb_fd_set_cloexec(slavefd);
#if defined I_PUSH && !defined linux
if (ioctl(slavefd, I_PUSH, "ptem") == -1) goto error;
@@ -331,8 +331,8 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
if (!fail) return -1;
rb_raise(rb_eRuntimeError, "openpty() failed");
}
- rb_update_max_fd(*master);
- rb_update_max_fd(*slave);
+ rb_fd_set_cloexec(*master);
+ rb_fd_set_cloexec(*slave);
if (no_mesg(SlaveName, nomesg) == -1) {
if (!fail) return -1;
rb_raise(rb_eRuntimeError, "can't chmod slave pty");
@@ -348,11 +348,11 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
if (!fail) return -1;
rb_raise(rb_eRuntimeError, "_getpty() failed");
}
- rb_update_max_fd(*master);
+ rb_fd_set_cloexec(*master);
*slave = open(name, O_RDWR);
/* error check? */
- rb_update_max_fd(*slave);
+ rb_fd_set_cloexec(*slave);
strlcpy(SlaveName, name, DEVICELEN);
return 0;
@@ -366,7 +366,7 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
extern int grantpt(int);
if((masterfd = open("/dev/ptmx", O_RDWR, 0)) == -1) goto error;
- rb_update_max_fd(masterfd);
+ rb_fd_set_cloexec(masterfd);
s = signal(SIGCHLD, SIG_DFL);
if(grantpt(masterfd) == -1) goto error;
signal(SIGCHLD, s);
@@ -374,7 +374,7 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
if((slavedevice = ptsname(masterfd)) == NULL) goto error;
if (no_mesg(slavedevice, nomesg) == -1) goto error;
if((slavefd = open(slavedevice, O_RDWR, 0)) == -1) goto error;
- rb_update_max_fd(slavefd);
+ rb_fd_set_cloexec(slavefd);
#if defined I_PUSH && !defined linux
if(ioctl(slavefd, I_PUSH, "ptem") == -1) goto error;
if(ioctl(slavefd, I_PUSH, "ldterm") == -1) goto error;
@@ -398,11 +398,11 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
for (p = deviceNo; *p != NULL; p++) {
snprintf(MasterName, sizeof MasterName, MasterDevice, *p);
if ((masterfd = open(MasterName,O_RDWR,0)) >= 0) {
- rb_update_max_fd(masterfd);
+ rb_fd_set_cloexec(masterfd);
*master = masterfd;
snprintf(SlaveName, DEVICELEN, SlaveDevice, *p);
if ((slavefd = open(SlaveName,O_RDWR,0)) >= 0) {
- rb_update_max_fd(slavefd);
+ rb_fd_set_cloexec(slavefd);
*slave = slavefd;
if (chown(SlaveName, getuid(), getgid()) != 0) goto error;
if (chmod(SlaveName, nomesg ? 0600 : 0622) != 0) goto error;
@@ -590,7 +590,7 @@ pty_getpty(int argc, VALUE *argv, VALUE self)
wfptr->fd = dup(info.fd);
if (wfptr->fd == -1)
rb_sys_fail("dup()");
- rb_update_max_fd(wfptr->fd);
+ rb_fd_set_cloexec(wfptr->fd);
wfptr->pathv = rfptr->pathv;
res = rb_ary_new2(3);
diff --git a/ext/socket/ancdata.c b/ext/socket/ancdata.c
index 9db4426a80..848b332181 100644
--- a/ext/socket/ancdata.c
+++ b/ext/socket/ancdata.c
@@ -1396,7 +1396,7 @@ discard_cmsg(struct cmsghdr *cmh, char *msg_end, int msg_peek_p)
int *end = (int *)((char *)cmh + cmh->cmsg_len);
while ((char *)fdp + sizeof(int) <= (char *)end &&
(char *)fdp + sizeof(int) <= msg_end) {
- rb_update_max_fd(*fdp);
+ rb_fd_set_cloexec(*fdp);
close(*fdp);
fdp++;
}
@@ -1439,7 +1439,7 @@ make_io_for_unix_rights(VALUE ctl, struct cmsghdr *cmh, char *msg_end)
VALUE io;
if (fstat(fd, &stbuf) == -1)
rb_raise(rb_eSocket, "invalid fd in SCM_RIGHTS");
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
if (S_ISSOCK(stbuf.st_mode))
io = rsock_init_sock(rb_obj_alloc(rb_cSocket), fd);
else
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 0a2365850f..ca271f3b5a 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -252,7 +252,7 @@ rsock_socket(int domain, int type, int proto)
}
}
if (0 <= fd)
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
return fd;
}
@@ -466,7 +466,7 @@ rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, s
}
rb_sys_fail("accept(2)");
}
- rb_update_max_fd(fd2);
+ rb_fd_set_cloexec(fd2);
make_fd_nonblock(fd2);
return rsock_init_sock(rb_obj_alloc(klass), fd2);
}
@@ -513,7 +513,7 @@ rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
}
rb_sys_fail(0);
}
- rb_update_max_fd(fd2);
+ rb_fd_set_cloexec(fd2);
if (!klass) return INT2NUM(fd2);
return rsock_init_sock(rb_obj_alloc(klass), fd2);
}
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index 4613892e7d..92407185fc 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -119,8 +119,8 @@ rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
if (ret < 0) {
rb_sys_fail("socketpair(2)");
}
- rb_update_max_fd(sp[0]);
- rb_update_max_fd(sp[1]);
+ rb_fd_set_cloexec(sp[0]);
+ rb_fd_set_cloexec(sp[1]);
s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c
index 15196e7b1f..7f2d628167 100644
--- a/ext/socket/unixsocket.c
+++ b/ext/socket/unixsocket.c
@@ -383,7 +383,7 @@ unix_recv_io(int argc, VALUE *argv, VALUE sock)
#if FD_PASSING_BY_MSG_CONTROL
memcpy(&fd, CMSG_DATA(&cmsg.hdr), sizeof(int));
#endif
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
if (klass == Qnil)
return INT2FIX(fd);
diff --git a/file.c b/file.c
index 81f28ffa03..bd96ffee35 100644
--- a/file.c
+++ b/file.c
@@ -3914,7 +3914,7 @@ rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
if ((tmpfd = open(StringValueCStr(path), 0)) < 0) {
rb_sys_fail(RSTRING_PTR(path));
}
- rb_update_max_fd(tmpfd);
+ rb_fd_set_cloexec(tmpfd);
if (chsize(tmpfd, pos) < 0) {
close(tmpfd);
rb_sys_fail(RSTRING_PTR(path));
@@ -5062,7 +5062,7 @@ file_load_ok(const char *path)
int ret = 1;
int fd = open(path, O_RDONLY);
if (fd == -1) return 0;
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
#if !defined DOSISH
{
struct stat st;
diff --git a/include/ruby/intern.h b/include/ruby/intern.h
index cd1fd04948..cf31b8e3ab 100644
--- a/include/ruby/intern.h
+++ b/include/ruby/intern.h
@@ -504,6 +504,7 @@ int rb_pipe(int *pipes);
int rb_reserved_fd_p(int fd);
#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);
/* marshal.c */
VALUE rb_marshal_dump(VALUE, VALUE);
VALUE rb_marshal_load(VALUE);
diff --git a/io.c b/io.c
index 2ffe0aebf6..c4980a2c8b 100644
--- a/io.c
+++ b/io.c
@@ -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
diff --git a/process.c b/process.c
index a2cd4c6867..10020f6095 100644
--- a/process.c
+++ b/process.c
@@ -1870,7 +1870,7 @@ rb_f_exec(int argc, VALUE *argv)
rb_exec_arg_init(argc, argv, TRUE, &earg);
if (NIL_P(rb_ary_entry(earg.options, EXEC_OPTION_CLOSE_OTHERS)))
- rb_exec_arg_addopt(&earg, ID2SYM(rb_intern("close_others")), Qfalse);
+ rb_exec_arg_addopt(&earg, ID2SYM(rb_intern("close_others")), Qtrue);
rb_exec_arg_fixup(&earg);
rb_exec_err(&earg, errmsg, sizeof(errmsg));
@@ -2511,7 +2511,7 @@ move_fds_to_avoid_crash(int *fdp, int n, VALUE fds)
ret = fcntl(fdp[i], F_DUPFD, min);
if (ret == -1)
return -1;
- rb_update_max_fd(ret);
+ rb_fd_set_cloexec(ret);
close(fdp[i]);
fdp[i] = ret;
}
@@ -3089,7 +3089,7 @@ rb_f_system(int argc, VALUE *argv)
chfunc = signal(SIGCHLD, SIG_DFL);
#endif
- pid = rb_spawn_internal(argc, argv, FALSE, NULL, 0);
+ pid = rb_spawn_internal(argc, argv, TRUE, NULL, 0);
#if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
if (pid > 0) {
rb_syswait(pid);
@@ -3164,8 +3164,7 @@ rb_f_system(int argc, VALUE *argv)
* integer : the file descriptor of specified the integer
* io : the file descriptor specified as io.fileno
* file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not
- * :close_others => false : inherit fds (default for system and exec)
- * :close_others => true : don't inherit (default for spawn and IO.popen)
+ * :close_others => true : don't inherit
*
* If a hash is given as +env+, the environment is
* updated by +env+ before <code>exec(2)</code> in the child process.
@@ -3560,7 +3559,7 @@ ruby_setsid(void)
if (ret == -1) return -1;
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
ioctl(fd, TIOCNOTTY, NULL);
close(fd);
}
@@ -4851,7 +4850,7 @@ rb_daemon(int nochdir, int noclose)
err = chdir("/");
if (!noclose && (n = open("/dev/null", O_RDWR, 0)) != -1) {
- rb_update_max_fd(n);
+ rb_fd_set_cloexec(n);
(void)dup2(n, 0);
(void)dup2(n, 1);
(void)dup2(n, 2);
diff --git a/random.c b/random.c
index 5b5ae7f273..e55e4e7ad4 100644
--- a/random.c
+++ b/random.c
@@ -506,7 +506,7 @@ fill_random_seed(unsigned int seed[DEFAULT_SEED_CNT])
|O_NOCTTY
#endif
)) >= 0) {
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) {
if (read(fd, seed, DEFAULT_SEED_LEN) < DEFAULT_SEED_LEN) {
/* abandon */;
diff --git a/ruby.c b/ruby.c
index 2e6751f0f2..ef6454aa3e 100644
--- a/ruby.c
+++ b/ruby.c
@@ -1527,7 +1527,7 @@ load_file_internal(VALUE arg)
if ((fd = open(fname, mode)) < 0) {
rb_load_fail(fname);
}
- rb_update_max_fd(fd);
+ rb_fd_set_cloexec(fd);
f = rb_io_fdopen(fd, mode, fname);
}
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index d4787c75b4..e3bbd245ed 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -1261,6 +1261,8 @@ class TestIO < Test::Unit::TestCase
def test_close_on_exec
skip "IO\#close_on_exec is not implemented." unless have_close_on_exec?
ruby do |f|
+ assert_equal(true, f.close_on_exec?)
+ f.close_on_exec = false
assert_equal(false, f.close_on_exec?)
f.close_on_exec = true
assert_equal(true, f.close_on_exec?)
@@ -1269,12 +1271,16 @@ class TestIO < Test::Unit::TestCase
end
with_pipe do |r, w|
+ assert_equal(true, r.close_on_exec?)
+ r.close_on_exec = false
assert_equal(false, r.close_on_exec?)
r.close_on_exec = true
assert_equal(true, r.close_on_exec?)
r.close_on_exec = false
assert_equal(false, r.close_on_exec?)
+ assert_equal(true, w.close_on_exec?)
+ w.close_on_exec = false
assert_equal(false, w.close_on_exec?)
w.close_on_exec = true
assert_equal(true, w.close_on_exec?)
diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb
index c68b6008fe..5c05a47af5 100644
--- a/test/ruby/test_process.rb
+++ b/test/ruby/test_process.rb
@@ -603,7 +603,7 @@ class TestProcess < Test::Unit::TestCase
def test_fd_inheritance
skip "inheritance of fd other than stdin,stdout and stderr is not supported" if windows?
with_pipe {|r, w|
- system(RUBY, '-e', 'IO.new(ARGV[0].to_i, "w").puts(:ba)', w.fileno.to_s)
+ system(RUBY, '-e', 'IO.new(ARGV[0].to_i, "w").puts(:ba)', w.fileno.to_s, w=>w)
w.close
assert_equal("ba\n", r.read)
}
@@ -619,8 +619,9 @@ class TestProcess < Test::Unit::TestCase
write_file("s", <<-"End")
exec(#{RUBY.dump}, '-e',
'IO.new(ARGV[0].to_i, "w").puts("bu") rescue nil',
- #{w.fileno.to_s.dump})
+ #{w.fileno.to_s.dump}, :close_others=>false)
End
+ w.close_on_exec = false
Process.wait spawn(RUBY, "s", :close_others=>false)
w.close
assert_equal("bu\n", r.read)
@@ -660,6 +661,7 @@ class TestProcess < Test::Unit::TestCase
File.unlink("err")
}
with_pipe {|r, w|
+ w.close_on_exec = false
Process.wait spawn(RUBY, '-e', 'IO.new(ARGV[0].to_i, "w").puts("bi")', w.fileno.to_s, :close_others=>false)
w.close
assert_equal("bi\n", r.read)
@@ -686,6 +688,7 @@ class TestProcess < Test::Unit::TestCase
Process.wait
}
with_pipe {|r, w|
+ w.close_on_exec = false
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('mo')", :close_others=>false])
w.close
errmsg = io.read
@@ -694,6 +697,7 @@ class TestProcess < Test::Unit::TestCase
Process.wait
}
with_pipe {|r, w|
+ w.close_on_exec = false
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('mo')", :close_others=>nil])
w.close
errmsg = io.read
diff --git a/thread_pthread.c b/thread_pthread.c
index de730f10ff..be3148dc30 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -1214,8 +1214,8 @@ rb_thread_create_timer_thread(void)
if (err != 0) {
rb_bug_errno("thread_timer: Failed to create communication pipe for timer thread", errno);
}
- rb_update_max_fd(timer_thread_pipe[0]);
- rb_update_max_fd(timer_thread_pipe[1]);
+ rb_fd_set_cloexec(timer_thread_pipe[0]);
+ rb_fd_set_cloexec(timer_thread_pipe[1]);
#if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL)
{
int oflags;