The question is quite simple: how do I capture and redirect to a file the stderr/stdout output produced by child program, running in a new process, created using Apache Portable Runtime’s apr_proc_create.
The ultimate task is to run an external command with some arguments in a separate process (preferably, but not mandatory) and redirect it’s output to a file.
So far I’ve got the following code (removed checks for clarity):
apr_procattr_t *attr;
apr_proc_t newproc;
const char *progname;
const char *args[100];
// progname and args are populated with data here
apr_procattr_create(&attr, p);
apr_procattr_io_set(attr, APR_CHILD_BLOCK, APR_CHILD_BLOCK, APR_CHILD_BLOCK);
apr_procattr_cmdtype_set(attr, APR_PROGRAM_PATH);
apr_proc_create(&newproc, progname, args, NULL, attr, p);
Using this code, the output of child process are suppressed (at least I don’t see it in the console), but it’s not redirected to a file. Using APR_NO_PIPE instead of APR_CHILD_BLOCK results in flushing the child’s output into parent stdout/stderr
APR appears to have very little ‘cookbook’ recipes, so I was unable to google it.
Any hints or suggestions? Non-APR solutions are welcome, provided they are as simple and clear as the code I’ve posted.
P.S. running on Debian 6(Linux 2.6.32-5-686), gcc 4.4.5, but I think it’s irrelevant. 🙂
The test cases for APR contain a lot of useful examples. In your case, you should have a look into testproc.c:
COPYRIGHT NOTICE: This code is protected by the APL 2.0