From 976d9a418125f70f7b1020c615ebfbd65565a8d6 Mon Sep 17 00:00:00 2001 From: Ron Waldon-Howe Date: Sat, 14 Dec 2024 17:07:28 +1100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20`unix=5Faddr()`?= =?UTF-8?q?=20out=20of=20`unix=5Fstream()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, we'd take the `--address` or default address, resolve it into the socket address, and start listening on that address. But we need to be able to expose that address to clients, too. So we break `Bus::unix_stream()` apart giving us a separate `Bus::unix_addr()` and access to the intermediate socket address. --- src/bus/mod.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/bus/mod.rs b/src/bus/mod.rs index 958f725..82c429b 100644 --- a/src/bus/mod.rs +++ b/src/bus/mod.rs @@ -1,5 +1,6 @@ -use anyhow::{bail, Ok, Result}; use std::{str::FromStr, sync::Arc}; + +use anyhow::{bail, Ok, Result}; #[cfg(unix)] use tokio::fs::remove_file; use tokio::spawn; @@ -56,7 +57,11 @@ impl Bus { }; let (listener, auth_mechanism) = match address.transport() { #[cfg(unix)] - Transport::Unix(unix) => (Self::unix_stream(unix).await?, AuthMechanism::External), + Transport::Unix(unix) => { + let addr = Self::unix_addr(unix)?; + + (Self::unix_stream(addr).await?, AuthMechanism::External) + } Transport::Tcp(tcp) => { #[cfg(not(windows))] let auth_mechanism = AuthMechanism::Anonymous; @@ -130,14 +135,10 @@ impl Bus { } #[cfg(unix)] - async fn unix_stream(unix: &Unix) -> Result { - // TODO: Use tokio::net::UnixListener directly once it supports abstract sockets: - // - // https://github.com/tokio-rs/tokio/issues/4610 - + fn unix_addr(unix: &Unix) -> Result { use std::os::unix::net::SocketAddr; - let addr = match unix.path() { + Ok(match unix.path() { #[cfg(target_os = "linux")] UnixSocket::Abstract(name) => { use std::os::linux::net::SocketAddrExt; @@ -170,7 +171,15 @@ impl Bus { addr } _ => bail!("Unsupported address."), - }; + }) + } + + #[cfg(unix)] + async fn unix_stream(addr: std::os::unix::net::SocketAddr) -> Result { + // TODO: Use tokio::net::UnixListener directly once it supports abstract sockets: + // + // https://github.com/tokio-rs/tokio/issues/4610 + let std_listener = tokio::task::spawn_blocking(move || std::os::unix::net::UnixListener::bind_addr(&addr)) .await??;