Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:08:05+00:00 2026-06-03T16:08:05+00:00

The question is quite simple: how do I capture and redirect to a file

  • 0

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. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:08:08+00:00Added an answer on June 3, 2026 at 4:08 pm

    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

    /* Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    static void test_file_redir(abts_case *tc, void *data)
    {
        apr_file_t *testout = NULL;
        apr_file_t *testerr = NULL;
        apr_off_t offset;
        apr_status_t rv;
        const char *args[2];
        apr_procattr_t *attr;
        apr_file_t *testfile = NULL;
        apr_size_t length;
        char *buf;
    
        testfile = NULL;
        rv = apr_file_open(&testfile, "data/stdin",
                           APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
                           APR_OS_DEFAULT, p);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_file_open(&testout, "data/stdout",
                           APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
                           APR_OS_DEFAULT, p);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_file_open(&testerr, "data/stderr",
                           APR_FOPEN_READ | APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_EXCL,
                           APR_OS_DEFAULT, p);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    
        length = strlen(TESTSTR);
        apr_file_write(testfile, TESTSTR, &length);
        offset = 0;
        rv = apr_file_seek(testfile, APR_SET, &offset);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        ABTS_ASSERT(tc, "File position mismatch, expected 0", offset == 0);
    
        rv = apr_procattr_create(&attr, p);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_procattr_child_in_set(attr, testfile, NULL);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_procattr_child_out_set(attr, testout, NULL);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_procattr_child_err_set(attr, testerr, NULL);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_procattr_dir_set(attr, "data");
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_procattr_cmdtype_set(attr, APR_PROGRAM_ENV);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    
        args[0] = "proc_child";
        args[1] = NULL;
    
        rv = apr_proc_create(&newproc, proc_child, args, NULL, 
                             attr, p);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    
        rv = apr_proc_wait(&newproc, NULL, NULL, APR_WAIT);
        ABTS_INT_EQUAL(tc, APR_CHILD_DONE, rv);
    
        offset = 0;
        rv = apr_file_seek(testout, APR_SET, &offset);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    
        length = 256;
        buf = apr_pcalloc(p, length);
        rv = apr_file_read(testout, buf, &length);
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        ABTS_STR_EQUAL(tc, TESTSTR, buf);
    
    
        apr_file_close(testfile);
        apr_file_close(testout);
        apr_file_close(testerr);
    
        rv = apr_file_remove("data/stdin", p);;
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_file_remove("data/stdout", p);;
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
        rv = apr_file_remove("data/stderr", p);;
        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question is quite simple: When I create a new API or Service Class
The question is quite simple: How to read a .pgm image file into a
My question is quite simple: I want to store the FILE NAME that I
I'm new to Symfony2. And my question quite simple. I would use 2 connections
My question is quite simple. Is it possible to inactivate all toasts of an
The question is quite simple : I have a controller plugin for Zend Framework.
My question is quite simple and with the SharpSvn Api, it should be easy
I just gave an answer to a quite simple question by using an extension
This is probably a really simple question but one I've never quite worked out
This might be a simple question for some people since it was quite hard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.