tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: detect valid fd



On Sep 15, 2014, at 4:59 PM, Patrick Welche <prlw1%cam.ac.uk@localhost> wrote:

> On Tue, Sep 16, 2014 at 12:51:24AM +0100, Justin Cormack wrote:
>> On Tue, Sep 16, 2014 at 12:20 AM, Patrick Welche <prlw1%cam.ac.uk@localhost> 
>> wrote:
>>> Given a filedescriptor, how can you tell that it is valid and has been
>>> opened?
>>> 
>>> In the attached simple program, a file and a directory are opened
>>> (with CLOEXEC set). I then call fcntl(fd, F_GETFD) on the range
>>> fd = [3..15].  fd = {3,4} correspond to the open file and directory.
>>> Why don't I get fcntl(4):
>>> 
>>>     [EBADF]            fildes is not a valid open file descriptor.
>>> 
>>> for fd = [5..15], but only for some of them?
>>> 
>>> $ ./cloexec
>>> fd  3 testfile.txt flags = 0x1 (0x1)
>>> fd  4 testdir flags = 0x1 (0x1)
>>> fd  3's flags = 0x1 (0x1)
>>> fd  4's flags = 0x1 (0x1)
>>> fd  5's flags = 0x0 (0x0)
>>> fd  6's flags = 0x0 (0x0)
>>> fd  7's flags = 0x0 (0x0)
>>> fd  8's flags = 0x0 (0x0)
>>> fd  9's flags = 0x0 (0x0)
>>> fd 10's flags = 0x0 (0x0)
>>> cloexec: fcntl 11: Bad file descriptor
>>> cloexec: fcntl 12: Bad file descriptor
>>> fd 13's flags = 0x0 (0x0)
>>> fd 14's flags = 0x0 (0x0)
>>> cloexec: fcntl 15: Bad file descriptor
>> 
>> I get
>> 
>> fd  3 testfile.txt flags = 0x1 (0x1)
>> fd  4 testdir flags = 0x1 (0x1)
>> fd  3's flags = 0x1 (0x1)
>> fd  4's flags = 0x1 (0x1)
>> cloexec: fcntl 5: Bad file descriptor
>> cloexec: fcntl 6: Bad file descriptor
>> cloexec: fcntl 7: Bad file descriptor
>> cloexec: fcntl 8: Bad file descriptor
>> cloexec: fcntl 9: Bad file descriptor
>> cloexec: fcntl 10: Bad file descriptor
>> cloexec: fcntl 11: Bad file descriptor
>> cloexec: fcntl 12: Bad file descriptor
>> cloexec: fcntl 13: Bad file descriptor
>> cloexec: fcntl 14: Bad file descriptor
>> cloexec: fcntl 15: Bad file descriptor
>> 
>> Which looks fine, on netbsd6.1.4 and 7-pre, both on amd64.
>> 
>> What NetBSD version are you testing on?
> 
> So for both of you, things look correct!
> 
> This is on Sunday's NetBSD 7.99.1 amd64, but this is an old problem for
> me...

What does fstat show for your shell or add a pause to the program and fstat it?

fstat -p $$ 




Home | Main Index | Thread Index | Old Index