diff --git a/ChangeLog b/ChangeLog index 8013d07d45..7fe86b4ae8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat May 28 19:30:17 2011 KOSAKI Motohiro + + * process.c (before_exec, after_exec): change SIGPIPE handler to SIG_DFL + before calling execve(). Because r31760 reintroduced an issue that + system() may hang up (i.e. [ruby-dev:12261]). + * process.c (save_sigpipe, restore_sigpipe): new. + Sat May 28 16:08:16 2011 KOSAKI Motohiro * signal.c (Init_signal, default_handler): change default SIGPIPE handler diff --git a/process.c b/process.c index 0f60cfb940..af55a0e412 100644 --- a/process.c +++ b/process.c @@ -984,14 +984,41 @@ void rb_thread_reset_timer_thread(void); static int forked_child = 0; +#ifdef SIGPIPE +static RETSIGTYPE (*saved_sigpipe_handler)(int) = 0; +#endif + +#if defined(POSIX_SIGNAL) +# define signal(a,b) posix_signal((a),(b)) +#endif + +static void save_sigpipe(void) +{ +#ifdef SIGPIPE + /* + * Some OS commands don't initialize signal handler properly. Thus we have to + * reset signal handler before exec(). Otherwise, system() and similar child process + * interaction might fail. (e.g. ruby -e "system 'yes | ls'") [ruby-dev:12261] + */ + saved_sigpipe_handler = signal(SIGPIPE, SIG_DFL); +#endif +} + +static void restore_sigpipe(void) +{ +#ifdef SIGPIPE + signal(SIGPIPE, saved_sigpipe_handler); +#endif +} + /* * On old MacOS X, exec() may return ENOTSUPP if the process have multiple threads. * Therefore we have to kill internal threads at once. [ruby-core: 10583] */ #define before_exec() \ - (rb_enable_interrupt(), (void)(forked_child ? 0 : (rb_thread_stop_timer_thread(), 1))) + (rb_enable_interrupt(), save_sigpipe(), (void)(forked_child ? 0 : (rb_thread_stop_timer_thread(), 1))) #define after_exec() \ - (rb_thread_reset_timer_thread(), rb_thread_start_timer_thread(), forked_child = 0, rb_disable_interrupt()) + (rb_thread_reset_timer_thread(), rb_thread_start_timer_thread(), forked_child = 0, restore_sigpipe(), rb_disable_interrupt()) #define before_fork() before_exec() #define after_fork() (GET_THREAD()->thrown_errinfo = 0, after_exec()) @@ -2907,11 +2934,6 @@ rb_f_abort(int argc, VALUE *argv) return Qnil; /* not reached */ } - -#if defined(POSIX_SIGNAL) -# define signal(a,b) posix_signal((a),(b)) -#endif - void rb_syswait(rb_pid_t pid) { diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb index de1dcd0bd6..4df883fb53 100644 --- a/test/ruby/test_process.rb +++ b/test/ruby/test_process.rb @@ -1,6 +1,7 @@ require 'test/unit' require 'tmpdir' require 'pathname' +require 'timeout' require_relative 'envutil' require 'rbconfig' @@ -1250,4 +1251,21 @@ class TestProcess < Test::Unit::TestCase exs << Errno::E2BIG if defined?(Errno::E2BIG) assert_raise(*exs, bug4315) {Process.spawn('"a"|'*10_000_000)} end + + def test_system_sigpipe + return if /mswin|mingw/ =~ RUBY_PLATFORM + + pid = 0 + + with_tmpchdir do + assert_nothing_raised('[ruby-dev:12261]') do + timeout(3) do + pid = spawn('yes | ls') + Process.waitpid pid + end + end + end + ensure + Process.kill(:KILL, pid) if (pid != 0) rescue false + end end