Since there seems no way to use already opened fd after exec
,
why isn't this flag the default?
File descriptors can be used past an exec
call; that's how the Unix utilities get their standard input/output/error fds from the shell, for example.
Close-on-exec is not the default because the POSIX standard (and Unix tradition) mandates the opposite behavior:
File descriptors open in the calling process image shall remain open in the new process image, except for those whose close-on- exec flag
FD_CLOEXEC
is set.
Because on UNIX one of the most used features is/was piping streams between processes - and you cannot do that if the CLOEXEC flag is set (child process cannot inherit file descriptor(s), e.g.: STDOUT_FILENO).
And no, it is not true that you cannot use inherited file descriptors after exec (example: standard streams). You also can use any inherited file descriptor as long as you know its value (it is an integer). This value is often passed to a child process by argument (quite a few UNIX programs do that), or you can do this any other way using any IPC (Inter-Process Communication) mechanism of your choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With