tech-kern archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: detect valid fd
In article <20140915232046.GA9227%quark.cable.rcn.com@localhost>,
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
>
>
>The motivation for the question is
>
> https://bugs.freedesktop.org/show_bug.cgi?id=83899
>
Libc can open and keep file descriptors open internally. For example
if you run gethostbyname("foo") in your program, there is a file
descriptor kept open for the /etc/resolv.conf kqueue and one for
/etc/resolv.conf itself. There was a bug and this was not marked
close-on-exec, which I just fixed. Same is true if you use NIS, or
other subsystems that use file descriptors. Eventually all should
be close-on-exec. Unfortunately there are other things that use
file descriptors internally (crypto, semaphores) on NetBSD that
are possibly more difficult to tackle.
christos
of them close-on-exec
Home |
Main Index |
Thread Index |
Old Index