@matheusbsbs / matheus-bagliano / commits / ce2cf27adc
Fork 0

run-command: Redirect stderr to a pipe before redirecting stdout to stderr

With this patch, in the 'start_command' function after forking we now take care of stderr in the child process before stdout. This way if 'start_command' is called with a 'child_process' argument like this: .err = -1; .stdout_to_stderr = 1; then stderr will be redirected to a pipe before stdout is redirected to stderr. So we can now get the process' stdout from the pipe (as well as its stderr). Earlier such a call would have redirected stdout to stderr before stderr was itself redirected, and therefore stdout would not have followed stderr, which would not have been very useful anyway. Update documentation in 'api-run-command.txt' accordingly. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Acked-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Mar 5, 2008 at 08:35 UTC ce2cf27adc434c11cd1e91bcacf00297efd8cc92
2 files changed +11 -10
Documentation/technical/api-run-command.txt
+4 -3
@@ -111,9 +111,10 @@ stderr as follows:
111 .no_stdin, .no_stdout, .no_stderr: The respective channel is
112 redirected to /dev/null.
113
114 - .stdout_to_stderr: stdout of the child is redirected to the
115 - parent's stderr (i.e. *not* to what .err or
116 - .no_stderr specify).
114 + .stdout_to_stderr: stdout of the child is redirected to its
115 + stderr. This happens after stderr is itself redirected.
116 + So stdout will follow stderr to wherever it is
117 + redirected.
118
119 To modify the environment of the sub-process, specify an array of
120 string pointers (NULL terminated) in .env:
run-command.c
+7 -7
@@ -91,6 +91,13 @@ int start_command(struct child_process *cmd)
91 close(cmd->in);
92 }
93
94 + if (cmd->no_stderr)
95 + dup_devnull(2);
96 + else if (need_err) {
97 + dup2(fderr[1], 2);
98 + close_pair(fderr);
99 + }
100 +
101 if (cmd->no_stdout)
102 dup_devnull(1);
103 else if (cmd->stdout_to_stderr)
@@ -103,13 +110,6 @@ int start_command(struct child_process *cmd)
110 close(cmd->out);
111 }
112
106 - if (cmd->no_stderr)
107 - dup_devnull(2);
108 - else if (need_err) {
109 - dup2(fderr[1], 2);
110 - close_pair(fderr);
111 - }
112 -
113 if (cmd->dir && chdir(cmd->dir))
114 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
115 cmd->dir, strerror(errno));