Re: dup2() Problem
Re: dup2() Problem
- Subject: Re: dup2() Problem
- From: Mark Rhoads <email@hidden>
- Date: Fri, 06 Jul 2007 12:20:42 -0700
- Organization: WiredRoads
Hello,
Norm Green wrote:
Hi,
I'm having a problem on Darwin i386 using dup2() in a child process.
This is after a fork() call but before execve() call.
After the fork I do:
int logfd = open(logName, O_WRONLY|O_APPEND|O_CREAT, 0644);
dup2(logfd, STDOUT_FILENO);
The open() call succeeds but the dup2() call always fails with errno=9,
which suggests one of the args is not an open fd. I'm sure I'm not
closing either in my code.
I presume that the above code snippet is in the child process, e.g.:
if ( 0 == fork() )
{
...
}
Given the EBADF errno, I'd guess that either logfd or STDOUT_FILENO does
not contain or refer to an expected value for an fd. As well as
following other advice given in other posts, you might check whether
some bit of custom kit in your 'old as dirt' and 'works fine elsewhere'
code is (re)defining STDOUT_FILENO, or even 'dup2' or 'fork' to
something unexpected. Also, check that some bug or side-effect is not
changing the value of logName or what it references after your 'fork',
and thus causing open() to intermittently fail (even though you might
have successfully tested it before).
If your code snippet happens to be in the parent process space instead,
then it is also possible that the child process of a vfork() could be
mucking things up for you.
This bit of advice is worth repeating from other posts:
Build and run a small test case.
Best Regards
--Mark Rhoads
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden