tech-kern archive

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

Re: detect valid fd



On Mon, Sep 15, 2014 at 05:11:12PM -0700, Matt Thomas wrote:
> 
> 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
> 
> What does fstat show for your shell or add a pause to the program and fstat 
> it?

prlw1    cloexec     2336   wd /home    1424735 drwxr-xr-x     512 r 
prlw1    cloexec     2336    0 /dev/pts      13 crw--w----   pts/5 rw
prlw1    cloexec     2336    1 /dev/pts      13 crw--w----   pts/5 rw
prlw1    cloexec     2336    2 /dev/pts      13 crw--w----   pts/5 rw
prlw1    cloexec     2336    3 /home    1424738 -rw-r--r--       0 r 
prlw1    cloexec     2336    4 /home    1424739 drwxr-xr-x     512 r 
prlw1    cloexec     2336    5 /usr     7438302 -r--r--r--    5852 r 
prlw1    cloexec     2336    6 /usr     7431819 -r--r--r--    5879 r 
prlw1    cloexec     2336    7 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
prlw1    cloexec     2336    8 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
prlw1    cloexec     2336    9* pipe 0xfffffe814d38cdc0 -> 0x0 w
prlw1    cloexec     2336   10 /usr     7439186 -r--r--r--   58736 r 
prlw1    cloexec     2336   13 /home    1375614 -rw-r--r--      18 r 
prlw1    cloexec     2336   14 flags 0x80024<ISTTY,LOCKSWORK,CLEAN>

and simple guesses for /home and /usr didn't find a -inum match. Trying
a long find now...

Cheers,

Patrick


Home | Main Index | Thread Index | Old Index