Skip to content

Commit

Permalink
run-command: Allow stderr to be a caller supplied pipe
Browse files Browse the repository at this point in the history
Like .out, .err may now be set to a file descriptor > 0, which
is a writable pipe/socket/file that the child's stderr will be
redirected into.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Shawn O. Pearce authored and Junio C Hamano committed Feb 6, 2010
1 parent 2b26e0e commit 4f41b61
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Documentation/technical/api-run-command.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ stderr as follows:

.in: The FD must be readable; it becomes child's stdin.
.out: The FD must be writable; it becomes child's stdout.
.err > 0 is not supported.
.err: The FD must be writable; it becomes child's stderr.

The specified FD is closed by start_command(), even if it fails to
run the sub-process!
Expand Down
8 changes: 8 additions & 0 deletions run-command.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ int start_command(struct child_process *cmd)
else if (need_err) {
dup2(fderr[1], 2);
close_pair(fderr);
} else if (cmd->err > 1) {
dup2(cmd->err, 2);
close(cmd->err);
}

if (cmd->no_stdout)
Expand Down Expand Up @@ -156,6 +159,9 @@ int start_command(struct child_process *cmd)
} else if (need_err) {
s2 = dup(2);
dup2(fderr[1], 2);
} else if (cmd->err > 2) {
s2 = dup(2);
dup2(cmd->err, 2);
}

if (cmd->no_stdout) {
Expand Down Expand Up @@ -228,6 +234,8 @@ int start_command(struct child_process *cmd)

if (need_err)
close(fderr[1]);
else if (cmd->err)
close(cmd->err);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion run-command.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct child_process {
* - Specify > 0 to set a channel to a particular FD as follows:
* .in: a readable FD, becomes child's stdin
* .out: a writable FD, becomes child's stdout/stderr
* .err > 0 not supported
* .err: a writable FD, becomes child's stderr
* The specified FD is closed by start_command(), even in case
* of errors!
*/
Expand Down

0 comments on commit 4f41b61

Please sign in to comment.