Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't close_on_exec the default configuration?

Tags:

linux

exec

Since there seems no way to use already opened fd after exec,

why isn't this flag the default?

like image 994
new_perl Avatar asked Mar 06 '12 12:03

new_perl


2 Answers

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.

like image 165
Fred Foo Avatar answered Jan 09 '23 20:01

Fred Foo


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.

like image 29
sirgeorge Avatar answered Jan 09 '23 20:01

sirgeorge