Skip to content

Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.

A notification server the bus starts on demand is a desktop too

Lars Taliand7406fe8bf80
diff --git a/src/daemon/ask.rs b/src/daemon/ask.rs
index 41f965e..99a6ecc 100644
--- a/src/daemon/ask.rs
+++ b/src/daemon/ask.rs
@@ -297,8 +297,8 @@ impl Drop for Waiting<'_> {
}
}
-/// What notify-send says when it couldn't show the question, in libnotify 0.7.10 to 0.8.9; it exits 1 then, but only
-/// once its expiry (an hour) is out. With no notification server it says both, the first in every version.
+/// What notify-send says when it couldn't show the question: the first since libnotify 0.7.10, the second since
+/// 0.8.8. It exits 1 then, but only once its expiry (an hour) is out. With no notification server, all say the first.
const NOT_SHOWN: [&str; 2] = ["Actions are not supported", "Failed to show notification"];
/// Runs the pop-up and returns what it printed, or why it couldn't be shown: at once when notify-send says so.
diff --git a/src/daemon/notify.rs b/src/daemon/notify.rs
index 91e02bb..aff3705 100644
--- a/src/daemon/notify.rs
+++ b/src/daemon/notify.rs
@@ -82,7 +82,7 @@ pub(super) fn notify_send_at(bin: std::path::PathBuf) -> Option<Command> {
}
};
// Linger keeps a user bus up with nobody at the screen (a server, a box reached over SSH): only a notification
- // server on it makes a desktop. One started on demand counts once it runs.
+ // server on it makes a desktop, running or one the bus starts on demand.
if !notifications_on(&bus) {
return None;
}
@@ -90,32 +90,43 @@ pub(super) fn notify_send_at(bin: std::path::PathBuf) -> Option<Command> {
Some(cmd)
}
-/// Whether the bus at `bus` has a notification server. Only a clear no counts: without busctl, or on a bus it can't
-/// reach, notify-send finds out. It's asked under the daemon's lock, so a bus that hangs gets a second in all
-/// (`--timeout` doesn't cover connecting).
+/// Whether the bus at `bus` has a notification server: one running, or one it starts on demand (dunst, mako, MATE's).
+/// Only a clear no counts: without busctl, or on a bus it can't reach, notify-send finds out. It's asked under the
+/// daemon's lock, so a bus that hangs gets a second in all (`--timeout` doesn't cover connecting).
#[cfg(all(unix, not(target_os = "macos")))]
fn notifications_on(bus: &std::ffi::OsStr) -> bool {
+ const NAME: &str = "org.freedesktop.Notifications";
let Some(busctl) = super::ask::asker_bin("busctl", &["/usr/bin/busctl", "/run/current-system/sw/bin/busctl"])
else {
return true;
};
- let Ok(mut child) = Command::new(busctl)
- .env("DBUS_SESSION_BUS_ADDRESS", bus)
- .args(["--user", "--timeout=1", "call", "org.freedesktop.DBus", "/org/freedesktop/DBus"])
- .args(["org.freedesktop.DBus", "NameHasOwner", "s", "org.freedesktop.Notifications"])
- .stdin(Stdio::null())
- .stdout(Stdio::piped())
- .stderr(Stdio::null())
- .spawn()
- else {
- return true;
+ let call = |args: &[&str]| {
+ Command::new(&busctl)
+ .env("DBUS_SESSION_BUS_ADDRESS", bus)
+ .args(["--user", "--timeout=1", "call", "org.freedesktop.DBus", "/org/freedesktop/DBus"])
+ .arg("org.freedesktop.DBus")
+ .args(args)
+ .stdin(Stdio::null())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::null())
+ .spawn()
+ .ok()
};
+ let calls = [call(&["NameHasOwner", "s", NAME]), call(&["ListActivatableNames"])];
let until = Instant::now() + Duration::from_secs(1);
- while matches!(child.try_wait(), Ok(None)) && Instant::now() < until {
- std::thread::sleep(Duration::from_millis(2));
- }
- let _ = child.kill();
- !matches!(child.wait_with_output(), Ok(o) if o.status.success() && o.stdout.trim_ascii() == b"b false")
+ let [owned, activatable] = calls.map(|child| {
+ let mut child = child?;
+ while matches!(child.try_wait(), Ok(None)) && Instant::now() < until {
+ std::thread::sleep(Duration::from_millis(2));
+ }
+ let _ = child.kill();
+ child.wait_with_output().ok().filter(|o| o.status.success()).map(|o| o.stdout)
+ });
+ // `as 2 "org.freedesktop.DBus" "org.freedesktop.Notifications"`
+ let quoted = format!("\"{NAME}\"");
+ let unowned = owned.is_some_and(|o| o.trim_ascii() == b"b false");
+ let unlisted = activatable.is_some_and(|o| !o.windows(quoted.len()).any(|w| w == quoted.as_bytes()));
+ !(unowned && unlisted)
}
/// Most notification servers read markup.
@@ -159,25 +170,59 @@ mod tests {
use super::*;
use std::io::BufRead;
- #[test]
- fn a_bus_without_a_notification_server_is_no_desktop() {
- let Some(daemon) = agent::find_bin("dbus-daemon").filter(|_| agent::find_bin("busctl").is_some()) else {
- return;
- };
+ /// A private session bus whose only service directory is `services`, so what the host has installed doesn't count.
+ fn bus(services: &[&str]) -> Option<(std::process::Child, String, std::path::PathBuf)> {
+ let daemon = agent::find_bin("dbus-daemon").filter(|_| agent::find_bin("busctl").is_some())?;
+ let dir = std::env::temp_dir().join(format!("reason-network-bus-{}", ulid::Ulid::new()));
+ std::fs::create_dir_all(dir.join("services")).unwrap();
+ for name in services {
+ let service = format!("[D-BUS Service]\nName={name}\nExec=/nonexistent\n");
+ std::fs::write(dir.join("services").join(format!("{name}.service")), service).unwrap();
+ }
+ let config = format!(
+ r#"<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
+ "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+ <type>session</type>
+ <listen>unix:tmpdir={tmp}</listen>
+ <servicedir>{tmp}/services</servicedir>
+ <policy context="default"><allow send_destination="*" eavesdrop="true"/><allow eavesdrop="true"/><allow own="*"/></policy>
+</busconfig>"#,
+ tmp = dir.display()
+ );
+ std::fs::write(dir.join("bus.conf"), config).unwrap();
let mut bus = Command::new(daemon)
- .args(["--session", "--nofork", "--print-address"])
- .arg(format!("--address=unix:tmpdir={}", std::env::temp_dir().display()))
+ .arg(format!("--config-file={}", dir.join("bus.conf").display()))
+ .args(["--nofork", "--print-address"])
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.unwrap();
let mut address = String::new();
std::io::BufReader::new(bus.stdout.take().unwrap()).read_line(&mut address).unwrap();
- let on = notifications_on(address.trim().as_ref());
+ Some((bus, address.trim().to_string(), dir))
+ }
+
+ fn notifications_on_bus(services: &[&str]) -> Option<bool> {
+ let (mut bus, address, dir) = bus(services)?;
+ let on = notifications_on(address.as_ref());
bus.kill().unwrap();
bus.wait().unwrap();
+ std::fs::remove_dir_all(dir).unwrap();
+ Some(on)
+ }
+
+ #[test]
+ fn a_bus_without_a_notification_server_is_no_desktop() {
+ let Some(on) = notifications_on_bus(&["org.example.Other"]) else { return };
assert!(!on);
// A bus it can't reach is left to notify-send.
assert!(notifications_on("unix:path=/nonexistent".as_ref()));
}
+
+ #[test]
+ fn a_notification_server_the_bus_starts_on_demand_is_a_desktop() {
+ let Some(on) = notifications_on_bus(&["org.freedesktop.Notifications"]) else { return };
+ assert!(on);
+ }
}