- /* set up stdin, stdout, and stderr */
- dup2(elt->upstream->output_fd, STDIN_FILENO);
- dup2(elt->downstream->input_fd, STDOUT_FILENO);
- debug_dup_stderr_to_debug();
+ /* first, copy our fd's out of the stdio range */
+ while (rfd <= STDERR_FILENO)
+ rfd = dup(rfd);
+ while (wfd <= STDERR_FILENO)
+ wfd = dup(wfd);
+
+ /* set up stdin, stdout, and stderr, overwriting anything already open
+ * on those fd's */
+ dup2(rfd, STDIN_FILENO);
+ dup2(wfd, STDOUT_FILENO);
+ if (!self->log_stderr)
+ debug_dup_stderr_to_debug();