Why is close_on_exec not used by default?

Since there is no way to use the already open fd after exec ,

Why is this flag not set by default?

+6
source share
3 answers

File descriptors can be used after exec ; that, for example, Unix utilities get their standard I / O / error frames from the shell.

Close-on-exec is not the default because the POSIX standard (and the Unix tradition) sets the opposite behavior:

File descriptors opened in the image of the calling process remain open in the new process image, except for those that have the close-on-exec FD_CLOEXEC .

+3
source

On UNIX, one of the most used functions is thread flows between processes, and you cannot do this if the CLOEXEC flag is set (a child process cannot inherit file descriptors, for example: STDOUT_FILENO).

And no, it’s not true that you cannot use inherited file descriptors after exec (example: standard streams). You can also use any inherited file descriptor if you know its value (this is an integer). This value is often passed to the child process by arguments (this is quite a lot of UNIX programs), or you can do it in any other way using any IPC (Inter-Process Communication) mechanism of your choice.

+2
source

I would not mind getting a more complete answer to this, but it's pretty easy to guess it for backward compatibility. the close-on-exec flag should have been entered sometime. The code that existed before this time did not know about it and would not work correctly if it had not been changed. Therefore, it is disabled by default.

Unfortunately, errors occur because of this, the daemon process that opens cgi can leave the socket open for listening, and if cgi does not shut down or close it, the daemon cannot be restarted. Therefore, I agree with you that this is not a very good default.

0
source

Source: https://habr.com/ru/post/910073/


All Articles