diff --git a/src/bin/busd.rs b/src/bin/busd.rs index fdd1424..bcc593f 100644 --- a/src/bin/busd.rs +++ b/src/bin/busd.rs @@ -1,9 +1,10 @@ extern crate busd; +use std::path::PathBuf; #[cfg(unix)] use std::{fs::File, io::Write, os::fd::FromRawFd}; -use busd::bus; +use busd::{bus, config::Config}; use anyhow::Result; use clap::Parser; @@ -18,9 +19,14 @@ use tracing::{info, warn}; #[clap(author, version, about, long_about = None)] struct Args { /// The address to listen on. + /// Takes precedence over any `` element in the configuration file. #[clap(short = 'a', long, value_parser)] address: Option, + /// Use the given configuration file. + #[clap(long)] + config: Option, + /// Print the address of the message bus to standard output. #[clap(long)] print_address: bool, @@ -36,6 +42,15 @@ struct Args { #[cfg(unix)] #[clap(long)] ready_fd: Option, + + /// Equivalent to `--config /usr/share/dbus-1/session.conf`. + /// This is the default if `--config` and `--system` are unspecified. + #[clap(long)] + session: bool, + + /// Equivalent to `--config /usr/share/dbus-1/system.conf`. + #[clap(long)] + system: bool, } #[tokio::main] @@ -44,7 +59,23 @@ async fn main() -> Result<()> { let args = Args::parse(); - let mut bus = bus::Bus::for_address(args.address.as_deref()).await?; + let config_path = if args.system { + PathBuf::from("/usr/share/dbus-1/system.conf") + } else if let Some(config_path) = args.config { + config_path + } else { + PathBuf::from("/usr/share/dbus-1/session.conf") + }; + eprintln!("reading configuration file {} ...", config_path.display()); + let config = Config::read_file(&config_path)?; + + let address = if let Some(address) = args.address { + Some(address) + } else { + config.listen.map(|address| format!("{address}")) + }; + + let mut bus = bus::Bus::for_address(address.as_deref()).await?; #[cfg(unix)] if let Some(fd) = args.ready_fd {