I have a parent process having some children which don't need the FIFO descriptor opened by the parent before forking. I have tried to do the following approaches that
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
after opening the fd.fd = open(/tmp/testfifo, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
To see whether it works correctly I do following steps but the first one doesn't work since it is decided as a bug. However, I don't understand what the wrong with the second is.
First approach,
if (fcntl(fd, F_GETFD) & FD_CLOEXEC) {
fprintf(stderr, "FD_CLOEXEC is set\n");
}
Second approach over the terminal,
lsof -n | grep /private/tmp/testfifo
With two children it prints,
program 17898 soner 3r FIFO 0t0 5274098 /private/tmp/testfifo
program 17898 soner 4w FIFO 0t0 5274098 /private/tmp/testfifo
program 17899 soner 3r FIFO 0t0 5274098 /private/tmp/testfifo
program 17899 soner 4w FIFO 0t0 5274098 /private/tmp/testfifo
program 17900 soner 3r FIFO 0t0 5274098 /private/tmp/testfifo
program 17900 soner 4w FIFO 0t0 5274098 /private/tmp/testfifo
Is my approach wrong? Or did I do something wrong? Or did I misunderstand rationale of the flag?