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 Jan 8, 2025
1 parent 27b74b2 commit 2e7f073
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/bus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{bail, Ok, Result};
#[cfg(unix)]
use std::{env, path::Path};
use std::{str::FromStr, sync::Arc};

#[cfg(unix)]
use tokio::fs::remove_file;
use tokio::spawn;
Expand Down Expand Up @@ -61,7 +62,12 @@ impl Bus {
};
let (listener, auth_mechanism) = match address.transport() {
#[cfg(unix)]
Transport::Unix(unix) => (Self::unix_stream(unix).await?, AuthMechanism::External),
Transport::Unix(unix) => {
// Resolve address specification into address that clients can use.
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 @@ -135,14 +141,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 @@ -175,7 +177,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 2e7f073

Please sign in to comment.