From 7c43d8523cadf8cd0483581fb8a50138b7fc4d9f Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 30 Oct 2011 09:46:56 +0000 Subject: [PATCH] * ruby.c (fill_standard_fds): new function to open closed standard file descriptors. (ruby_sysinit): call fill_standard_fds. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33567 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ ruby.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/ChangeLog b/ChangeLog index 1729cd5eec..eb365eb75c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sun Oct 30 18:45:50 2011 Tanaka Akira + + * ruby.c (fill_standard_fds): new function to open closed standard + file descriptors. + (ruby_sysinit): call fill_standard_fds. + Sun Oct 30 10:50:36 2011 Nobuyoshi Nakada * tool/rbinstall.rb (install_recursive, bin-comm): split mere diff --git a/ruby.c b/ruby.c index 2e157c0708..3371772671 100644 --- a/ruby.c +++ b/ruby.c @@ -1815,6 +1815,35 @@ ruby_process_options(int argc, char **argv) return (void*)(struct RData*)iseq; } +static void +fill_standard_fds(void) +{ + int f0, f1, f2, fds[2]; + f0 = fcntl(0, F_GETFD) == -1 && errno == EBADF; + f1 = fcntl(1, F_GETFD) == -1 && errno == EBADF; + f2 = fcntl(2, F_GETFD) == -1 && errno == EBADF; + if (f0) { + if (pipe(fds) == 0) { + close(fds[1]); + if (fds[0] != 0) { + dup2(fds[0], 0); + close(fds[0]); + } + } + } + if (f1 || f2) { + if (pipe(fds) == 0) { + close(fds[0]); + if (f1 && fds[1] != 1) + dup2(fds[1], 1); + if (f2 && fds[1] != 2) + dup2(fds[1], 2); + if (fds[1] != 1 && fds[1] != 2) + close(fds[1]); + } + } +} + void ruby_sysinit(int *argc, char ***argv) { @@ -1827,4 +1856,5 @@ ruby_sysinit(int *argc, char ***argv) #if defined(USE_DLN_A_OUT) dln_argv0 = origarg.argv[0]; #endif + fill_standard_fds(); }