-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Darwin: Only set FD_CLOEXEC when creating a socket FD #14650
Conversation
Not when a FD is passed to Socket.new
Follow up on #14632 (review) |
I notice some issues with this change: when we create the socket the |
Yeah I think we need to use
Yes, that's the current behaviour. But I believe it's wrong. We probably shouldn't autoclose a file descriptor that's coming from outside the process. Only close what we opened ourselves. However, I'm wondering if this isn't too bad a silent breaking change in behaviour... 🤔 |
I agree: it may not be the best API, but security-wise it doesn't sound like a bad practice to always close file descriptors on exec... and maybe always calling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following methods (at least) must also be fixed to set FD_CLOEXEC
for darwin targets:
Socket#accept?
UNIXSocket#accept?
UNIXSocket.pair
Maybe there should be a system method to set FD_CLOEXEC
on a given fd
to avoid repeating the same LibC.fcntl
call? Something like Crystal::System::FileDescriptor.set_close_on_exec(fd : Int32) : Nil
.
Why Apple wouldn't implement the O_CLOEXEC
SOCK_CLOEXEC
flag is beyond me 😮💨
Yeah, I suppose we should also setup specs for those methods. @carlhoerberg If you want to go on, please continue. But we'll be happy to take over these final steps if you prefer. Just say the word. |
Go for it! :) |
Harmonizes the implementations between Darwin and other POSIX platforms for the "close on exec" behavior. When `SOCK_CLOEXEC` is available, we always use it in the `socket`, `socketpair` and `accept4` syscalls. When `SOCK_CLOEXEC` isn't available, we don't delay to `Socket#initialize_handle` anymore to set `FD_CLOEXEC` for Darwin only, but immediately call `fcntl` to set it after the above syscalls. The `accept4` syscall is non-standard but widely available: Linux, all supported BSD, except for Darwin (obviously). The patch also fixes an issue where TCP and UNIX client sockets didn't have `FD_CLOEXEC` on POSIX platforms... except for Darwin. closes #14650
Not when an existing FD is passed to Socket.new