Should be true for close_on_exec flag
MODIFY
mrbgems/mruby-io/src/io.c
mrbgems/mruby-io/src/io.c
@@ -180,7 +180,7 @@ mrb_io_flags_to_modenum(mrb_state *mrb, int flags)
return modenum;
}
-void
+static void
mrb_fd_cloexec(mrb_state *mrb, int fd)
{
#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
@@ -188,7 +188,8 @@ mrb_fd_cloexec(mrb_state *mrb, int fd)
flags = fcntl(fd, F_GETFD);
if (flags == -1) {
- mrb_sys_fail(mrb, "fcntl");
+ mrb_bug(mrb, "mrb_fd_cloexec: fcntl(%S, F_GETFD) failed: %S",
+ mrb_fixnum_value(fd), mrb_fixnum_value(errno));
}
if (fd <= 2) {
flags2 = flags & ~FD_CLOEXEC; /* Clear CLOEXEC for standard file descriptors: 0, 1, 2. */
@@ -198,7 +199,8 @@ mrb_fd_cloexec(mrb_state *mrb, int fd)
}
if (flags != flags2) {
if (fcntl(fd, F_SETFD, flags2) == -1) {
- mrb_sys_fail(mrb, "fcntl");
+ mrb_bug(mrb, "mrb_fd_cloexec: fcntl(%S, F_SETFD, %S) failed: %S",
+ mrb_fixnum_value(fd), mrb_fixnum_value(flags2), mrb_fixnum_value(errno));
}
}
#endif
@@ -577,11 +579,17 @@ mrb_io_initialize_copy(mrb_state *mrb, mrb_value copy)
if (failed) {
mrb_sys_fail(mrb, 0);
}
+ mrb_fd_cloexec(mrb, fptr_copy->fd);
+
+ if (fptr_orig->fd2 != -1) {
fptr_copy->fd2 = mrb_dup(mrb, fptr_orig->fd2, &failed);
if (failed) {
close(fptr_copy->fd);
mrb_sys_fail(mrb, 0);
}
+ mrb_fd_cloexec(mrb, fptr_copy->fd2);
+ }
+
fptr_copy->pid = fptr_orig->pid;
fptr_copy->readable = fptr_orig->readable;
fptr_copy->writable = fptr_orig->writable;

MODIFY
mrbgems/mruby-io/test/io.rb
mrbgems/mruby-io/test/io.rb
@@ -216,6 +216,7 @@ assert('IO#dup for readable') do
dup = io.dup
assert_true io != dup
assert_true io.fileno != dup.fileno
+ assert_true dup.close_on_exec?
assert_equal 'm', dup.sysread(1)
assert_equal 'r', io.sysread(1)
assert_equal 'u', dup.sysread(1)

Should be true for close_on_exec flag