-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Support creating files and sockets from file descriptors #46196
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
Comments
I would recommend against doing this. At the moment, it is by design that an isolate can't access file descriptors owned by other isolates. However with this change, an isolate could manipulate files owned by another isolate by forging the integer file descriptor. /cc @aam |
@zanderso is the intention of isolates that code cannot access resources of another isolate (i.e. a security requirement) or shouldn't access resources (i.e. a code structure requirement)? Some thoughts on this:
|
I think the intent would be keep file descriptors(perhaps |
We could consider adding |
A particular case might be a Dart process has been started with a pipe using file descriptor 5. This number might have been passed using an environment variable (e.g. Note that if any of these file descriptors were closed during the process lifetime they could be re-used. Not sure if there is a suitable method to guard against that. |
Hi, today I have realized about an interesting usecase for this feature. While I was trying to create a Dart-only wrapper of dns_sd.h through FFI, I noticed that all of the glue C code i'm using to abstract creating a service broadcast could be written in dart if I could just use RawSocket stream of RawSocketEvent notifications and a minimal amount of FFI glue code to send the fds to Dart. Also, that saves me from having to poll those fds constantly. I am also welcoming in that the Native API be expanded to be able to send a socket from C through a NativePort, but I imagine that one would be harder as there's an assumption that isolates can't share file descriptors. This is my POC of bonjour ffi |
About this issue. Well, on It would also enable integration of other event loops into Dart (at least on Unix systems). |
I have started with a WIP CL for this: https://dart-review.googlesource.com/c/sdk/+/360863 It adds support for constructing sockets from file descriptors. For me, a use case I'd like to see supported in Dart is using file descriptors inherited from a parent process. With systemd for instance, we can have a privileged daemon listen on sockets and start an unprivileged server inheriting that socket on the first connection. This improves startup time and helps with sandboxing, but it's a pain to use in Dart if we can't go from FDs to Obviously there's the question of whether this should be in |
Hello I reviewed your code and I don't know, maybe it is better to try to extend the ResourceHandle API instead? That one would allow you more flexibility rather than just ServerSockets. Something like a ResourceHandle.fromFileDescriptor that would allow it to convert to most IO classes in dart:io, like server sockets, files, etc... |
@brianquinlan Hello, sorry for the direct ping, but I noticed that you're responsible for |
I got around this issue using dart ffi. Far from adequate but for now it works see https://github.com/kingwill101/dart_native_socket |
On Unix systems (e.g. Linux) file descriptors are the handle used for files and sockets. These may be passed between processes, e.g. using Unix domain sockets. This gives the case where Dart code might receive such a file descriptor, but is not able to make use of it using standard Dart APIs.
A proposed API would be something like:
This new API would throw an exception on platforms that didn't support it. I'm aware this might be considered a bit special case, so it may be more desirable to have an API like
File.fromNativeHandle()
that could support other platforms such as Windows (as long as we can make a common handle type).Another option is to use an FFI based library to wrap the file descriptors, but this makes it much harder to cleanly and efficiently implement asynchronous reads from the file descriptors.
A specific case where this feature would be used is the IPC mechanism DBus, which allows file descriptors to be sent in messages. This is used in cases where large amounts of data need to be transferred between processes, and this is inefficient to do using IPC.
The text was updated successfully, but these errors were encountered: