Skip to content

Commit 4f41b61

Browse files
spearcegitster
authored andcommitted
run-command: Allow stderr to be a caller supplied pipe
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2b26e0e commit 4f41b61

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Documentation/technical/api-run-command.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ stderr as follows:
135135

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

140140
The specified FD is closed by start_command(), even if it fails to
141141
run the sub-process!

run-command.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ int start_command(struct child_process *cmd)
9494
else if (need_err) {
9595
dup2(fderr[1], 2);
9696
close_pair(fderr);
97+
} else if (cmd->err > 1) {
98+
dup2(cmd->err, 2);
99+
close(cmd->err);
97100
}
98101

99102
if (cmd->no_stdout)
@@ -156,6 +159,9 @@ int start_command(struct child_process *cmd)
156159
} else if (need_err) {
157160
s2 = dup(2);
158161
dup2(fderr[1], 2);
162+
} else if (cmd->err > 2) {
163+
s2 = dup(2);
164+
dup2(cmd->err, 2);
159165
}
160166

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

229235
if (need_err)
230236
close(fderr[1]);
237+
else if (cmd->err)
238+
close(cmd->err);
231239

232240
return 0;
233241
}

run-command.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct child_process {
1818
* - Specify > 0 to set a channel to a particular FD as follows:
1919
* .in: a readable FD, becomes child's stdin
2020
* .out: a writable FD, becomes child's stdout/stderr
21-
* .err > 0 not supported
21+
* .err: a writable FD, becomes child's stderr
2222
* The specified FD is closed by start_command(), even in case
2323
* of errors!
2424
*/

0 commit comments

Comments
 (0)