resources/mruby/mrbgems/mruby-io/src/io.c in esruby-0.0.9 vs resources/mruby/mrbgems/mruby-io/src/io.c in esruby-0.0.10
- old
+ new
@@ -178,29 +178,31 @@
#endif
return modenum;
}
-void
+static void
mrb_fd_cloexec(mrb_state *mrb, int fd)
{
#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
int flags, flags2;
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. */
}
else {
flags2 = flags | FD_CLOEXEC; /* Set CLOEXEC for non-standard file descriptors: 3, 4, 5, ... */
}
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
}
@@ -575,15 +577,21 @@
fptr_copy->fd = mrb_dup(mrb, fptr_orig->fd, &failed);
if (failed) {
mrb_sys_fail(mrb, 0);
}
- 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->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;
fptr_copy->sync = fptr_orig->sync;
fptr_copy->is_socket = fptr_orig->is_socket;
@@ -674,10 +682,10 @@
} while (pid == -1 && errno == EINTR);
if (!quiet && pid == fptr->pid) {
io_set_process_status(mrb, pid, status);
}
#else
- HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, fptr->pid);
+ HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, fptr->pid);
DWORD status;
if (WaitForSingleObject(h, INFINITE) && GetExitCodeProcess(h, &status))
if (!quiet)
io_set_process_status(mrb, fptr->pid, (int)status);
CloseHandle(h);