-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
net::UnixListener: Allow binding/using an existing socket #5678
Comments
It sounds like you're looking for way to set an option on the socket. Which option is it? |
From which side, rust/tokio, or the OS? I've found some (potential - I haven't tried them yet) rust/tokio examples of how to set this up, but it's somewhat involved, needing to create a manual socket in std rust and then wiring up a stream manually. I'd like to avoid that, and feel that it's something that probably should be streamlined for other users. I'm not entirely sure what all needs to happen in terms of setting up a socket, but my guess is that some sort of |
For example, an example of doing this correctly in C would help me understand the requirements better. |
Alas, I don't know how to do this in C/C++. Generally what happens is that you end up with a systemd socket definition that looks like this: #Socket file (/etc/systemd/system/mysocket.socket)
[Unit]
Description=My Socket
[Socket]
ListenStream=/run/mysocket.sock
[Install]
WantedBy=sockets.target #Service file (/etc/systemd/system/mysocket.service)
[Unit]
Description=My Service
[Service]
ExecStart=/usr/bin/myprogram /run/mysocket.sock
[Install]
WantedBy=multi-user.target ... and then the calling program is supposed to "be able to accept connections on the socket", whatever that means. Um. |
What happens here is that systemd invokes your binary with more file descriptors (3+, rather than only the usual stdin/stdout/stderr). Those extra descriptors are the listening sockets. It tells you how many there are by means of the I think you can already do this, although it takes jumping through a few hoops:
Unfortunately Tokio doesn’t seem to have a “generic listener” which accepts sockets of any type, so to be properly general, you’d actually want to check the FD type (with Maybe there’s room for Tokio to add some support to make a few of these steps easier? |
@Hawk777 Thank you for explaining! We have a |
To use a trait method, wouldn’t you have to have already figured out whether the socket is TCP or UNIX so you can decide with trait impl to call the method on? Whereas the issue here is that you don’t know yet. Maybe a free function that takes a socket, internally calls |
Sorry, to clarify, I did not mean to add the function to the trait, but to add a free-standing method to the |
On a related note, it seems that |
@pronebird hopefully we have the same behavior as std. If there's an issue, then please open a new issue. Otherwise it will get lost. |
Note that from what I posted originally, I believe that in my case I'm not supposed to remove the socket file, since it would be maintained by systemd itself. |
After digging around I figured that it’s indeed how things work not only rust but in C too. So the behaviour is consistent. |
In case of a systemd socket, yes, you shouldn’t
The kernel doesn’t automatically unlink the socket. Userspace code—whether that’s the application or some library—is responsible for doing that sometime (whether on termination or at startup) before binding the new listener. |
Is your feature request related to a problem? Please describe.
UnixListener::bind(...)
fails if the socket file already exists, which will be the case for systemd-managed socket entries (eg, asome-systemd-service.socket
file). The purpose of creating such a socket entry is that the systemd service will not be started until after the first connection is made to the socket, which means that with ephemeral or intermittent services they are not required to be constantly running.Describe the solution you'd like
Provide a method to bind to an existing socket file.
Describe alternatives you've considered
Deleting the socket file is expressly forbidden by the systemd documentation.
There may be additional ways to configure systemd/tokio to work around this, but these will be arcane or require
unsafe
code. Otherwise, a constantly-running service will be required.Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: