From: ahoward Date: 2003-05-13T08:27:23+09:00 Subject: Re: RCR for child execution On Tue, 13 May 2003, Brian Candler wrote: > > The problem is fairly simple, but I doubt anyone understands me ??? > > If I do understand you, then you are asking for something impossible. I > think you would realise this if you understood the Unix execution > environment a little better. perhaps i too am misunderstanding, but it does not seem like what simon is asking for is impossible - there is another option: a process can redirect fd 1 and 2, call some methods (possibly which spawn sub-processes), and restore fd 1 and 2. i quite realize the entire broken-pipe argument against this but considering simon's application - it seems like this is moot (or at least not cared about). AFAICT ruby does something odd which prevents this - 50 'i love ruby' stickers to whomever can figure out why this short c program does not work for ruby! it works for other types of output routines - what is ruby doing here?: file: redirect2.c ----CUT---- #include #include #include #include #include #include #include #include "ruby.h" /* gcc redirect2.c -I /usr/local/ruby-1.6.6/ /usr/local/ruby-1.6.6/libruby.a -lm -ldl -lcrypt */ int main (argc, argv) int argc; char **argv; { int fd[2]; int f; int __out, __err; pid_t pid; char *script; int c; assert ((script = argv[1]) != NULL || (script = "redirected.rb")); /* save */ assert ((__out = dup (STDOUT_FILENO)) != -1); assert ((__err = dup (STDERR_FILENO)) != -1); /* redirect */ assert (pipe (fd) == 0); assert (dup2 (fd[1], STDOUT_FILENO) != -1); assert (dup2 (fd[1], STDERR_FILENO) != -1); /* sandboxed */ /* THIS WORKS... */ system ("ls "__FILE__); system ("ls no-exist"); write (STDOUT_FILENO, "redirected out\n", 15); write (STDERR_FILENO, "redirected err\n", 15); /* THIS WILL NOT... */ /* ruby_init (); ruby_script (script); rb_load_file (script); ruby_run (); */ /* restore */ assert (dup2 (__out, STDOUT_FILENO) != -1); assert (dup2 (__err, STDERR_FILENO) != -1); /* display */ write (STDOUT_FILENO, "REDIRECTED:\n", 12); assert (close (fd[1]) != -1); while (read (fd[0], &c, 1) && write (STDOUT_FILENO, &c, 1)); assert (close (fd[0]) != -1); return (EXIT_SUCCESS); } /* * * file: redirected.rb ** * `ls #{__FILE__}` `ls non-existent-file` system 'ls #{__FILE__}' system 'ls non-existent-file' STDOUT.puts 42 STDERR.puts 42 $defout.puts 42 $stdout.puts 42 $stderr.puts 42 puts 42 */ ----CUT---- notice the part which does not work has been commented out. -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ara.t.howard@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================