atomiccloseonexec

This is an old revision of the document!


Add various APIs that set the close-on-exec flag atomically with allocating a file descriptor. These can be used to avoid undesirably passing file descriptors to child processes if threads or signal handlers call fork and exec. Some existing software will use the new features if present or will even refuse to compile without them.

A few are already in POSIX.1-2008. Many are proposed in http://austingroupbugs.net/view.php?id=411 (enough for all POSIX functionality). A few more are in glibc, which appears to have a complete implementation, summarized in http://udrepper.livejournal.com/20407.html. Solaris 11 added one new API but does not appear to have a particularly complete implementation of the rest. NetBSD has a fairly complete implementation but no accept4().

Some of the new APIs also allow enabling non-blocking mode with a flag.

New API Defined by Done?
open() O_CLOEXEC POSIX.1-2008 yes
posix_openpt() O_CLOEXEC Austin group #411 yes
fopen() 'e' mode Austin group #411 yes but too strict about order of letters
freopen() 'e' mode Austin group #411 yes but too strict about order of letters
fcntl() F_DUPFD_CLOEXEC POSIX.1-2008 yes
fcntl() F_DUP2FD_CLOEXEC Solaris 11 yes
dup3() Austin group #411 no
posix_spawn_file_actions_adddup2() can clear FD_CLOEXEC Austin group #411 yes
pipe2() Austin group #411 yes
popen() 'e' mode Austin group #411 yes
socket() SOCK_CLOEXEC and SOCK_NONBLOCK Austin group #411 yes
socketpair() SOCK_CLOEXEC and SOCK_NONBLOCK Austin group #411 yes
recvmsg() MSG_CMSG_CLOEXEC Austin group #411 yes
accept4() Austin group #411 yes
mkostemp() Austin group #411 no
mkostemps() glibc no
kqueue1() NetBSD no
extension for sctp_peeloff() noone no
extension for /dev/crypto noone no

There is no new API for dup() and tmpfile(). Code based on fcntl(F_DUPFD_CLOEXEC) and mkostemp() should be used instead.

The related http://austingroupbugs.net/view.php?id=368 basically requires that libc uses these new APIs for internal file descriptors. This is partially done.

  • atomiccloseonexec.1369073810.txt.gz
  • Last modified: 2013/05/20 18:16
  • by JillesTjoelker