use std::io::{BufRead, BufReader};
use std::process::{Child, Command, Stdio};
pub struct Bus {
child: Child,
pub address: String,
}
impl Bus {
pub fn spawn() -> Option<Self> {
let mut child = Command::new("dbus-daemon")
.args(["--session", "--print-address=1", "--nofork"])
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.ok()?;
let stdout = child.stdout.take()?;
let mut address = String::new();
BufReader::new(stdout).read_line(&mut address).ok()?;
let address = address.trim().to_string();
if address.is_empty() {
let _ = child.kill();
return None;
}
Some(Self { child, address })
}
}
impl Drop for Bus {
fn drop(&mut self) {
let _ = self.child.kill();
let _ = self.child.wait();
}
}