bug in fdopendir

Corinna Vinschen corinna-cygwin@cygwin.com
Mon Jul 9 10:17:00 GMT 2007

On Jul  6 14:40, Eric Blake wrote:
> I'm looking at Corinna's new fdopendir implementation:
> http://cygwin.com/ml/cygwin-cvs/2007-q2/msg00064.html
> First, thanks for doing it.  Second, I think there may be a couple of bugs.
> The fd handed to fdopendir must be consumed by the DIR, therefore the
> closedir must close it.  POSIX (well, the draft POSIX 200x) states that
> behavior is undefined if a user calls close(fd) after calling
> fdopendir(fd); it also states that calling closedir() must close the fd
> originally passed in to fdopendir (if the implementation of DIR tracks an
> underlying fd, which it does in cygwin).  Your special treatment of

I didn't see the POSIX draft so far.  I was looking into the Solaris man
pages for my implementation and the statement referring to closedir in
this regard is:

  The directory file descriptor should not be used or closed following a
  successful function call, as this might cause undefined results from
  future operations on the directory stream obtained from the call. Use
  closedir(3C) to close a directory stream.

Looks like I misunderstood this.  It's clear that the directory stream
is closed by closedir but it doesn't explicitely state that the
underlying file descriptor is also closed when closedir is called on
this directory stream.  I interpreted the "not be used or closed" part
as referring to using or closing before closedir has been called, not
beyond the closedir call.  Other man pages on the net weren't any clearer.

I subscribed to the austin group list a couple of minutes ago, so that
I get access to the draft.  I'm curious how it's worded there...

> whether the DIR wraps an internal fd created by opendir, or whether it was
> an external fd handed through fdopendir, ends up being a leak of file
> descriptors in closedir in dir.cc when a compliant program uses fdopendir
> properly.


> Also, POSIX is clear that DIR streams are not inherited across processes.

Erm... not quite.  DIR streams are inherited through fork.  They just
don't survive exec calls.  Anyway, thanks for pointing that out.  I
missed this fact entirely.


Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

More information about the Cygwin-developers mailing list