clan-sidebus/sidebus-broker/src/main.rs

56 lines
1.7 KiB
Rust
Raw Normal View History

mod bus;
mod vsock;
use clap::Parser;
use std::sync::Arc;
use tokio::sync::Mutex;
// https://github.com/rust-lang/rfcs/issues/2407#issuecomment-385291238
macro_rules! enclose {
( ($( $x:ident ),*) $y:expr ) => {
{
$(let $x = $x.clone();)*
$y
}
};
}
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct BrokerCli {}
#[tokio::main]
async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt::init();
let _cli = BrokerCli::parse();
let vm_bus = bus::HostedBus::new().await?;
let vm_bus_guid: zbus::OwnedGuid = vm_bus.server_guid().to_owned().into();
let vm_bus = Arc::new(Mutex::new(vm_bus));
// Direct access for the host (just trying things out)
let unix_listener = tokio::net::UnixListener::bind("vmbus.sock")?;
tokio::spawn(enclose! { (vm_bus) async move {
while let Ok((socket, _remote_addr)) = unix_listener.accept().await {
vm_bus.lock().await.connect_unix(socket).await.unwrap()
}
} });
// NOTE: Every individual D-Bus client inside of the VM is a new client here!
vsock::ListenerBuilder::new(vsock::VsockAddr::new(vsock::VMADDR_CID_HOST, 4269))
.with_label("VM Bus")
.listen(move |client| {
enclose! { (vm_bus, vm_bus_guid) async move {
// TODO: Not necessary to go through the channel, add vsock support to the Peer too
let client_conn = client.build((&vm_bus_guid).into()).await?;
let vmbus_conn = vm_bus.lock().await.connect_channel(true).await?;
sidebus_common::raw::splice_conns(client_conn, vmbus_conn).await;
Ok(())
} }
})
.await?;
Ok(())
}