diff --git a/src/common/remoteio.c b/src/common/remoteio.c --- a/src/common/remoteio.c +++ b/src/common/remoteio.c @@ -36,7 +36,9 @@ int remoteio_open(const char *spec, stru pid_t child; /* for wait(2) if needed */ - int childstatus; + int childstatus; + + int counter; /* create two pipes to facilitate communication with child */ if(pipe(pipe_write)) @@ -91,8 +93,30 @@ int remoteio_open(const char *spec, stru close(pipe_write[1]); close(pipe_read[0]); - /* reset stdin, stdout, and stderr to the appropriate streams. OK, not stderr :-) */ + /* + reset stdin, stdout, and stderr to the appropriate files. OK, not stderr :-) + */ + dup2(pipe_read[1], 0); + dup2(pipe_write[0], 1); + /* + close all other file descriptors. We want to keep 0, 1, and 2 open. We don't know that the last open() or pipe() always gives the highest fd number. However, I will assume that it does. Maybe this is a bad idea: + */ + counter = pipe_write[0]; + if(counter < pipe_write[1]) + counter = pipe_write[1]; + if(counter < pipe_read[0]) + counter = pipe_read[0]; + if(counter < pipe_read[1]) + counter = pipe_read[1]; + while(counter > 2) + { + close(counter); + counter --; + } + /* + now exec + */ return 1; /* this line should never be reached because we exec*/ }