home | list info | list archive | date index | thread index

[OCLUG-Tech] FD_CLOEXEC weirdness

I thought I'd post here before embarrasing myself on the gilbc list with
what is probably a simple oversight :)

FD_CLOEXEC should be off by default after open or dup, according to
http://www.gnu.org/software/libc/manual/html_node/Descriptor-Flags.html .
However, check this program out:

---
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int
main() {
    int fd1, fd2, i;

    if ((fd1 = open("/bin/ls", O_RDONLY)) < 0)
	perror("open");

    i = fcntl(fd1, F_GETFD);
    printf("fd1 FD_CLOEXEC: %d\n");

    if ((fd2 = dup(fd1)) < 0)
	perror("dup");

    i = fcntl(fd2, F_GETFD);
    printf("fd2 FD_CLOEXEC: %d\n");
}
--- (also http://modernduck.com/temp/fcntl.c )

scjody@gribbl:~$ gcc fcntl.c -o fcntl && ./fcntl
fd1 FD_CLOEXEC: 1
fd2 FD_CLOEXEC: 1

What the hell?

strace shows nothing unusual (no fcntl calls behind my back, for
example).  Just open, fcntl of F_GETFD, write, dup, fcntl of F_GETFD,
write.  What am I missing, or is glibc out to lunch?

Thanks,
Jody


replies