Question about libjava/childproc.c

Thomas Stüfe thomas.stuefe at
Wed Sep 5 15:45:10 UTC 2018

Hi all,

I just saw that in childproc.c, between fork and exec, in the child
process, we attempt to close all file descriptors. Which is fine and
good practice.

See function "closeDecriptors()" in libjava/childproc.c.

That function attempts to read /proc/fd to get all open file
descriptors and then closes them. For that, it uses readdir().
However, it has to make some weird magic workarounds since readdir()
itself may be internally implemented using a file descriptor:

86    /* We're trying to close all file descriptors, but opendir() might
87     * itself be implemented using a file descriptor, and we certainly
88     * don't want to close that while it's in use.  We assume that if
89     * opendir() is implemented using a file descriptor, then it uses
90     * the lowest numbered file descriptor, just like open().  So we
91     * close a couple explicitly.  */
93    close(from_fd);          /* for possible use by opendir() */
94    close(from_fd + 1);      /* another one for good luck */

My question would be, could we not - instead of straight away closing
the file descriptor - set them all to FD_CLOEXEC instead?

for (every fd in /proc/fd)
   fcntl(fd, F_SETFD, FD_CLOEXEC)


That way we will close them once we do exec(), which is what we
actually want, no?

Thanks & Regards, Thomas

More information about the core-libs-dev mailing list