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 ...
- 01-31-2008 #1Just Joined!
- Join Date
- Jan 2008
- Posts
- 3
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.
- 01-31-2008 #2
You don't really want to mess around with scratch files, do you? No, I thought not. Do this:
- Before the fork(), do a pipe() in the parent process.
- The parent should close the second descriptor in the array, because it won't be writing to the pipe.
- The child should close the first descriptor in the array, because it won't be reading from the pipe.
- 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:
- The child process should open() (not fopen()) the file where the standard error messages should go.
- 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.
- 01-31-2008 #3Just Joined!
- Join Date
- Jan 2008
- Posts
- 3
- 01-31-2008 #4
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
- 01-31-2008 #5
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.
- 02-01-2008 #6Just 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.


Reply With Quote
