RE: dup2() Problem
RE: dup2() Problem
- Subject: RE: dup2() Problem
- From: Norm Green <email@hidden>
- Date: Fri, 6 Jul 2007 15:39:11 -0700
Title: RE: dup2() Problem
Thanks Mark. Yes the code below is short snipet.
I ran the program under ktrace and see that the system() call causes a call to dup2(), which is failing with errno EBADF:
5738 sh CALL open(0x303f50,0x601,0x1b6)
5738 sh NAMI "testfile"
5738 sh RET open 5
5738 sh CALL dup2(0x5,0x1)
5738 sh RET dup2 -1 errno 9 Bad file descriptor
5738 sh CALL write(0x2,0x1800400,0x28)
5738 sh GIO fd 2 wrote 40 bytes
"/bin/sh: line 1: 1: Bad file descriptor
"
5738 sh RET write 40/0x28
5738 sh CALL exit(0x1)
This seems to indicate that all is well with the new file I opened since it returned fd 5. So does that mean something's wrong with stdout? I have put traces in to check the value of fileno(stdout) and it's always 1. Is it worth calling:
fstat(fileno(stdout), &sb)
?
I was assuming that people who have access to the Darwin source code monitor this list. If so, then I'd like to know under what circumstances (beyond what's in the man page) dup2() sets errno == EBADF? I've been doing this a long time and have seen most flavors of UNIX set an errno to indicate an error condition not documented in the man page. It's just a guess, but I suspect that's happening here.
If I had a trivial test case I would have sent it. I wrote a trivial test case and it works just fine of course :-( ). I'd be happy to send the entire ktrace log file and/or a "debug" executable with symbols that shows the problem if anyone is interested.
Thanks again to everyone for the suggestions, I do appreciate it.
Norm Green
GemStone Systems Inc.
-----Original Message-----
From: Mark Rhoads [mailto:email@hidden]
Sent: Friday, July 06, 2007 12:21 PM
To: email@hidden
Cc: Norm Green
Subject: Re: dup2() Problem
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