Third-party libraries
Third-party libraries
Posted Aug 27, 2009 20:24 UTC (Thu) by quotemstr (subscriber, #45331)In reply to: O_NOSTD by foom
Parent article: In brief
The problem is then that a third-party library might not be notified between a call to fork() and exec(). That's what pthread_atfork is for, but not everyone links against libpthread.
Even with pthread_atfork, however, you can race. Consider:
[library code]
static int fd = -1;
void mylib_atfork() {
close(fd);
}
void mylib_dosomething() {
fd = open(...);
do_something_with_fd(fd);
}
If the fork happens between the return from open() and the assignment to fd, then you race and leak the file descriptor.
The real userspace solution would be for programs to just close unknown file descriptors between fork and exec. But they don't, so O_CLOEXEC is a decent facility for defensive library programing.
Now, on the other hand, this O_NOSTD business is pure junk that will uselessly take up a valuable flag bit for all eternity.