command: allow merging stdout and stderr in string capture

Sometimes, its easier to run children with 2>&1 in shell notation,
and just deal with stdout and stderr interleaved.  This was already
possible for fd handling; extend it to also work when doing string
capture of a child process.

* docs/internals/command.html.in: Document this.
* src/util/command.c (virCommandSetErrorBuffer): Likewise.
(virCommandRun, virExecWithHook): Implement it.
* tests/commandtest.c (test14): Test it.
* daemon/remote.c (remoteDispatchAuthPolkit): Use new command
feature.
This commit is contained in:
Eric Blake
2012-01-27 15:40:20 -07:00
parent 9a3fc7f3f7
commit c9ace552eb
4 changed files with 46 additions and 12 deletions

View File

@@ -464,7 +464,9 @@ virExecWithHook(const char *const*argv,
}
if (errfd != NULL) {
if (*errfd == -1) {
if (errfd == outfd) {
childerr = childout;
} else if (*errfd == -1) {
if (pipe2(pipeerr, O_CLOEXEC) < 0) {
virReportSystemError(errno,
"%s", _("Failed to create pipe"));
@@ -1428,7 +1430,10 @@ virCommandSetOutputBuffer(virCommandPtr cmd, char **outbuf)
* guaranteed to be allocated after successful virCommandRun or
* virCommandWait, and is best-effort allocated after failed
* virCommandRun; caller is responsible for freeing *errbuf.
* This requires the use of virCommandRun.
* This requires the use of virCommandRun. It is possible to
* pass the same pointer as for virCommandSetOutputBuffer(), in
* which case the child process will interleave all output into
* a single string.
*/
void
virCommandSetErrorBuffer(virCommandPtr cmd, char **errbuf)
@@ -1940,6 +1945,13 @@ virCommandRun(virCommandPtr cmd, int *exitstatus)
cmd->inpipe = infd[1];
}
/* If caller requested the same string for stdout and stderr, then
* merge those into one string. */
if (cmd->outbuf && cmd->outbuf == cmd->errbuf) {
cmd->errfdptr = &cmd->outfd;
cmd->errbuf = NULL;
}
/* If caller hasn't requested capture of stdout/err, then capture
* it ourselves so we can log it. But the intermediate child for
* a daemon has no expected output, and we don't want our