summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 477822c)
raw | patch | inline | side by side (parent: 477822c)
author | Johannes Sixt <johannes.sixt@telecom.at> | |
Fri, 19 Oct 2007 19:47:58 +0000 (21:47 +0200) | ||
committer | Shawn O. Pearce <spearce@spearce.org> | |
Sun, 21 Oct 2007 05:30:40 +0000 (01:30 -0400) |
This adds another stanza that allocates a pipe that is connected to the
child's stderr and that the caller can read from. In order to request this
pipe, the caller sets cmd->err to -1.
The implementation is not exactly modeled after the stdout case: For stdout
the caller can supply an existing file descriptor, but this facility is
nowhere needed in the stderr case. Additionally, the caller is required to
close cmd->err.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
child's stderr and that the caller can read from. In order to request this
pipe, the caller sets cmd->err to -1.
The implementation is not exactly modeled after the stdout case: For stdout
the caller can supply an existing file descriptor, but this facility is
nowhere needed in the stderr case. Additionally, the caller is required to
close cmd->err.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
run-command.c | patch | blob | history | |
run-command.h | patch | blob | history |
diff --git a/run-command.c b/run-command.c
index 7e779d33ee9ea5f7d2e6aedc8c3a0a0476e87135..d00c03bbdf6c699596e22cf7f08e8b6860a146a9 100644 (file)
--- a/run-command.c
+++ b/run-command.c
int start_command(struct child_process *cmd)
{
- int need_in, need_out;
- int fdin[2], fdout[2];
+ int need_in, need_out, need_err;
+ int fdin[2], fdout[2], fderr[2];
need_in = !cmd->no_stdin && cmd->in < 0;
if (need_in) {
cmd->close_out = 1;
}
+ need_err = cmd->err < 0;
+ if (need_err) {
+ if (pipe(fderr) < 0) {
+ if (need_in)
+ close_pair(fdin);
+ if (need_out)
+ close_pair(fdout);
+ return -ERR_RUN_COMMAND_PIPE;
+ }
+ cmd->err = fderr[0];
+ }
+
cmd->pid = fork();
if (cmd->pid < 0) {
if (need_in)
close_pair(fdin);
if (need_out)
close_pair(fdout);
+ if (need_err)
+ close_pair(fderr);
return -ERR_RUN_COMMAND_FORK;
}
close(cmd->out);
}
+ if (need_err) {
+ dup2(fderr[1], 2);
+ close_pair(fderr);
+ }
+
if (cmd->dir && chdir(cmd->dir))
die("exec %s: cd to %s failed (%s)", cmd->argv[0],
cmd->dir, strerror(errno));
else if (cmd->out > 1)
close(cmd->out);
+ if (need_err)
+ close(fderr[1]);
+
return 0;
}
diff --git a/run-command.h b/run-command.h
index 7958eb1e0b7a927019460e06d7a01622eddf81df..35b9fb61f17e6b178f76c05ccd761b4a34c661bf 100644 (file)
--- a/run-command.h
+++ b/run-command.h
pid_t pid;
int in;
int out;
+ int err;
const char *dir;
const char *const *env;
unsigned close_in:1;