Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, this is my first day on linuxforums so... if I don't write smart questions please, be patient! I have a problem that maybe someone out there can help me ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    3

    Post Catching stderr of a child process

    Hi,

    this is my first day on linuxforums so... if I don't write smart
    questions please, be patient!

    I have a problem that maybe someone out there can help me to solve.
    From an application I wrote in C I launch unzip as a child process using
    execve.

    I need, in the parent, to catch the possible error output of the unzip
    process to see in everithing went right or if there has been some problems
    like a bad CRC in the zip file or something similar.

    How can I do this?

    How can I start the child process, for example, making its stderr to be
    redirected to a file that the parent can then parse?

    If I wasn't clear please tell me...

    Thanks in advance for every hint,
    I.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You don't really want to mess around with scratch files, do you? No, I thought not. Do this:

    1. Before the fork(), do a pipe() in the parent process.
    2. The parent should close the second descriptor in the array, because it won't be writing to the pipe.
    3. The child should close the first descriptor in the array, because it won't be reading from the pipe.
    4. The child process should also dup2(second descriptor in array,STDERR_FILENO). The constant STDERR_FILENO is defined in unistd.h.

    The parent can then read the child's standard error directly from the pipe.

    If you really want it sent to a file, then do this instead:

    1. The child process should open() (not fopen()) the file where the standard error messages should go.
    2. The child process should also dup2(file descriptor from that open(),STDERR_FILENO).


    Caution: I haven't actually done either of these, but (ahem) they should work. Come back here if something doesn't, and I'll actually try it out!

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by wje_lf View Post
    You don't really want to mess around with scratch files, do you? No, I thought not. Do this:

    1. Before the fork(), do a pipe() in the parent process.
    2. The parent should close the second descriptor in the array, because it won't be writing to the pipe.
    3. The child should close the first descriptor in the array, because it won't be reading from the pipe.
    4. The child process should also dup2(second descriptor in array,STDERR_FILENO). The constant STDERR_FILENO is defined in unistd.h.

    The parent can then read the child's standard error directly from the pipe.

    Hope this helps.

    Yes, it helped me and worked fine!
    Thanks a lot!

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    There may be a better way to do this, depending on what you need.

    If you need to actually see the error output, then wje's approach is the way to do it. However, if all you need to know is whether the child process errored or not, you don't need output at all.

    I assume that your parent process uses the waitpid() call to wait for the child to finish. If this is the case, then you can pass a pointer to an int into this function, and this will contain the exit status of the child process. You can then use the WEXITSTATUS(...) macro to find out the child's exit status. A 0 means success, anything else means some sort of error.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Cabhan makes an excellent point.

    Keep in mind, though the following (pulled from my copy of the waitpid() man page):

    Code:
    WIFEXITED(status)
           returns true if the child terminated normally, that
           is, by calling exit() or _exit(), or by returning
           from main().
    
    WEXITSTATUS(status)
           evaluates to the least significant eight bits of
           the return code of the child which terminated,
           which may have been set as the argument to a call
           to exit() or _exit() or as the argument for a
           return statement in the main program.  This macro
           can only be evaluated if WIFEXITED returned true.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Just Joined!
    Join Date
    Jan 2008
    Posts
    3
    Thanks wje_lf and Cabhan! You have been kind and clear.
    I learnt from you something useful and I could immediately
    apply what you told me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...