The following program demonstrates a bug in APR: when a process is
created, it explicitly runs pool cleanups. I'm not sure what the
motivation for that is, but it's a bad idea. Note how "testdata\n" is
written twice to the file "test" when the test program is run, because
the apr_file_t output buffer is flushed twice.
A separate but similar bug is that, if the exec() fails in
threadproc/proc.c, the "exit(1)" call will cause regular atexit
handlers to run, including the handler which flushes stdio data. This
can cause stdio data to be flushed twice. "_exit(1)" should be called
instead.
(I'm not on this list; please cc me on replies.)
#include <apr_general.h>
#include <apr_file_io.h>
#include <apr_thread_proc.h>
int main()
{
apr_pool_t *pool;
apr_file_t *file;
apr_size_t nbytes;
apr_proc_t proc;
apr_procattr_t *procattr;
const char *args[] = { "/bin/echo", "blah", NULL };
apr_status_t err;
err = apr_initialize();
err = apr_pool_create(&pool, NULL);
err = apr_file_open(&file, "test",
APR_CREATE|APR_APPEND|APR_BUFFERED|APR_READ|APR_WRITE,
APR_OS_DEFAULT, pool);
nbytes = 9;
err = apr_file_write(file, "testdata\n", &nbytes);
err = apr_procattr_create(&procattr, pool);
err = apr_procattr_cmdtype_set(procattr, APR_PROGRAM_ENV);
err = apr_proc_create(&proc, "/bin/echo", args, NULL, procattr, pool);
err = apr_proc_wait(&proc, NULL, NULL, APR_WAIT);
apr_terminate();
return 0;
}