Skip to content

Commit

Permalink
♻️ Extract unix_addr() out of unix_stream()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jokeyrhyme committed Dec 14, 2024
1 parent f004d14 commit 36e3b1a
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/bus/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -130,14 +135,10 @@ impl Bus {
}

#[cfg(unix)]
async fn unix_stream(unix: &Unix) -> Result<Listener> {
// 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<std::os::unix::net::SocketAddr> {
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;
Expand Down Expand Up @@ -170,7 +171,15 @@ impl Bus {
addr
}
_ => bail!("Unsupported address."),
};
})
}

#[cfg(unix)]
async fn unix_stream(addr: std::os::unix::net::SocketAddr) -> Result<Listener> {
// 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??;
Expand Down

0 comments on commit 36e3b1a

Please sign in to comment.