On 05/16/2011 01:05 AM, Laine Stump wrote:
This is in response to:
https://bugzilla.redhat.com/show_bug.cgi?id=616060
but will also be useful in an upcoming patch which needs to retrieve
the output of /etc/rc.d/init.d/netcf-transaction.
Before forking, we now create a pipe, then after forking, the child
dup2's both stdout and stderr to the write side of the pipe, and the
parent calls fdopen() on the read side of the pipe and then calls
gnulib's fread_file() before calling waitpid() to reap the process.
---
src/dutil_linux.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++----
src/dutil_linux.h | 2 +-
2 files changed, 72 insertions(+), 7 deletions(-)
@@ -133,6 +153,18 @@ exec_program(struct netcf *ncf,
_exit(1);
}
+ if (pipeout[1] >= 0) {
+ /* direct stdout and stderr to the pipe */
+ if (dup2(pipeout[1], fileno(stdout)) < 0
+ || dup2(pipeout[1], fileno(stderr)) < 0) {
+ /* don't report_error, as it will never be seen anyway */
+ _exit(1);
127 might be better than 1 here (basically, make the failed child look
like an exec failure).
ACK with that nit fixed.
--
Eric Blake eblake(a)redhat.com +1-801-349-2682
Libvirt virtualization library
http://libvirt.org