forge
Explore
Log in

Don't you dare go Hollow

dbus: Reacquire replaced interface names

Browse files at this commit

aaf9c6059d178497e2320cf5f84bc1e7b57a7cd9Ivan MolodetskikhParent: 9144e92
docs/wiki/Configuration:-Debug-Options.mdmodified+2 -1
@@ -182,7 +182,8 @@ Make niri create its D-Bus interfaces even if it's not running as a `--session`.
182 182
183 Useful for testing screencasting changes without having to relogin.183 Useful for testing screencasting changes without having to relogin.
184 184
185-The main niri instance will *not* currently take back the interfaces when you close the test instance, so you will need to relogin in the end to make screencasting work again.185+<sup>Since: next release</sup>
186+The main niri instance will automatically take back the interfaces once the new instance quits.
186 187
187 ```kdl188 ```kdl
188 debug {189 debug {
src/dbus/freedesktop_a11y.rsmodified+2 -2
@@ -563,7 +563,7 @@ impl Manager {
563 }563 }
564 }564 }
565 565
566- pub fn start(&self) -> anyhow::Result<zbus::blocking::Connection> {566+ pub fn start(&self, monitor: bool) -> anyhow::Result<zbus::blocking::Connection> {
567 let conn = zbus::blocking::Connection::session()?;567 let conn = zbus::blocking::Connection::session()?;
568 conn.object_server().at(568 conn.object_server().at(
569 "/org/freedesktop/a11y/Manager",569 "/org/freedesktop/a11y/Manager",
@@ -573,7 +573,7 @@ impl Manager {
573 "/org/freedesktop/a11y/Manager",573 "/org/freedesktop/a11y/Manager",
574 self.pointer_locator.clone(),574 self.pointer_locator.clone(),
575 )?;575 )?;
576- request_name(&conn, "org.freedesktop.a11y.Manager")?;576+ request_name(&conn, "org.freedesktop.a11y.Manager", monitor)?;
577 577
578 let iface = conn578 let iface = conn
579 .object_server()579 .object_server()
src/dbus/freedesktop_screensaver.rsmodified+2 -2
@@ -128,7 +128,7 @@ async fn monitor_disappeared_clients(
128 }128 }
129 129
130 impl Start for ScreenSaver {130 impl Start for ScreenSaver {
131- fn start(self) -> anyhow::Result<zbus::blocking::Connection> {131+ fn start(self, monitor: bool) -> anyhow::Result<zbus::blocking::Connection> {
132 let is_inhibited = self.is_inhibited.clone();132 let is_inhibited = self.is_inhibited.clone();
133 let is_broken = self.is_broken.clone();133 let is_broken = self.is_broken.clone();
134 let inhibitors = self.inhibitors.clone();134 let inhibitors = self.inhibitors.clone();
@@ -144,7 +144,7 @@ impl Start for ScreenSaver {
144 anyhow::bail!("failed to register any org.freedesktop.ScreenSaver interface")144 anyhow::bail!("failed to register any org.freedesktop.ScreenSaver interface")
145 }145 }
146 146
147- request_name(&conn, "org.freedesktop.ScreenSaver")?;147+ request_name(&conn, "org.freedesktop.ScreenSaver", monitor)?;
148 148
149 let async_conn = conn.inner();149 let async_conn = conn.inner();
150 let future = {150 let future = {
src/dbus/gnome_shell_introspect.rsmodified+2 -2
@@ -66,11 +66,11 @@ impl Introspect {
66 }66 }
67 67
68 impl Start for Introspect {68 impl Start for Introspect {
69- fn start(self) -> anyhow::Result<zbus::blocking::Connection> {69+ fn start(self, monitor: bool) -> anyhow::Result<zbus::blocking::Connection> {
70 let conn = zbus::blocking::Connection::session()?;70 let conn = zbus::blocking::Connection::session()?;
71 conn.object_server()71 conn.object_server()
72 .at("/org/gnome/Shell/Introspect", self)?;72 .at("/org/gnome/Shell/Introspect", self)?;
73- request_name(&conn, "org.gnome.Shell.Introspect")?;73+ request_name(&conn, "org.gnome.Shell.Introspect", monitor)?;
74 74
75 Ok(conn)75 Ok(conn)
76 }76 }
src/dbus/gnome_shell_screenshot.rsmodified+2 -2
@@ -90,11 +90,11 @@ impl Screenshot {
90 }90 }
91 91
92 impl Start for Screenshot {92 impl Start for Screenshot {
93- fn start(self) -> anyhow::Result<zbus::blocking::Connection> {93+ fn start(self, monitor: bool) -> anyhow::Result<zbus::blocking::Connection> {
94 let conn = zbus::blocking::Connection::session()?;94 let conn = zbus::blocking::Connection::session()?;
95 conn.object_server()95 conn.object_server()
96 .at("/org/gnome/Shell/Screenshot", self)?;96 .at("/org/gnome/Shell/Screenshot", self)?;
97- request_name(&conn, "org.gnome.Shell.Screenshot")?;97+ request_name(&conn, "org.gnome.Shell.Screenshot", monitor)?;
98 98
99 Ok(conn)99 Ok(conn)
100 }100 }
src/dbus/mod.rsmodified+81 -12
@@ -1,6 +1,10 @@
1+use anyhow::Context as _;
2+use futures_util::StreamExt as _;
1 use zbus::blocking::Connection;3 use zbus::blocking::Connection;
2-use zbus::fdo::RequestNameFlags;4+use zbus::fdo::{self, RequestNameFlags, RequestNameReply};
5+use zbus::names::{UniqueName, WellKnownName};
3 use zbus::object_server::Interface;6 use zbus::object_server::Interface;
7+use zbus::zvariant::NoneValue as _;
4 8
5 use crate::niri::State;9 use crate::niri::State;
6 10
@@ -24,7 +28,7 @@ use self::mutter_display_config::DisplayConfig;
24 use self::mutter_service_channel::ServiceChannel;28 use self::mutter_service_channel::ServiceChannel;
25 29
26 trait Start: Interface {30 trait Start: Interface {
27- fn start(self) -> anyhow::Result<zbus::blocking::Connection>;31+ fn start(self, monitor: bool) -> anyhow::Result<zbus::blocking::Connection>;
28 }32 }
29 33
30 #[derive(Default)]34 #[derive(Default)]
@@ -62,7 +66,7 @@ impl DBusServers {
62 calloop::channel::Event::Closed => (),66 calloop::channel::Event::Closed => (),
63 })67 })
64 .unwrap();68 .unwrap();
65- dbus.conn_service_channel = try_start(service_channel);69+ dbus.conn_service_channel = try_start(service_channel, false);
66 }70 }
67 71
68 if is_session_instance || config.debug.dbus_interfaces_in_non_session_instances {72 if is_session_instance || config.debug.dbus_interfaces_in_non_session_instances {
@@ -85,10 +89,10 @@ impl DBusServers {
85 calloop::channel::Event::Closed => (),89 calloop::channel::Event::Closed => (),
86 })90 })
87 .unwrap();91 .unwrap();
88- dbus.conn_display_config = try_start(display_config);92+ dbus.conn_display_config = try_start(display_config, is_session_instance);
89 93
90 let screen_saver = ScreenSaver::new(niri.is_fdo_idle_inhibited.clone());94 let screen_saver = ScreenSaver::new(niri.is_fdo_idle_inhibited.clone());
91- dbus.conn_screen_saver = try_start(screen_saver);95+ dbus.conn_screen_saver = try_start(screen_saver, is_session_instance);
92 96
93 let (to_niri, from_screenshot) = calloop::channel::channel();97 let (to_niri, from_screenshot) = calloop::channel::channel();
94 let (to_screenshot, from_niri) = async_channel::unbounded();98 let (to_screenshot, from_niri) = async_channel::unbounded();
@@ -101,7 +105,7 @@ impl DBusServers {
101 })105 })
102 .unwrap();106 .unwrap();
103 let screenshot = gnome_shell_screenshot::Screenshot::new(to_niri, from_niri);107 let screenshot = gnome_shell_screenshot::Screenshot::new(to_niri, from_niri);
104- dbus.conn_screen_shot = try_start(screenshot);108+ dbus.conn_screen_shot = try_start(screenshot, is_session_instance);
105 109
106 let (to_niri, from_introspect) = calloop::channel::channel();110 let (to_niri, from_introspect) = calloop::channel::channel();
107 let (to_introspect, from_niri) = async_channel::unbounded();111 let (to_introspect, from_niri) = async_channel::unbounded();
@@ -114,7 +118,7 @@ impl DBusServers {
114 })118 })
115 .unwrap();119 .unwrap();
116 let introspect = Introspect::new(to_niri, from_niri);120 let introspect = Introspect::new(to_niri, from_niri);
117- dbus.conn_introspect = try_start(introspect);121+ dbus.conn_introspect = try_start(introspect, is_session_instance);
118 122
119 #[cfg(feature = "xdp-gnome-screencast")]123 #[cfg(feature = "xdp-gnome-screencast")]
120 {124 {
@@ -128,7 +132,7 @@ impl DBusServers {
128 })132 })
129 .unwrap();133 .unwrap();
130 let screen_cast = ScreenCast::new(backend.ipc_outputs(), to_niri);134 let screen_cast = ScreenCast::new(backend.ipc_outputs(), to_niri);
131- dbus.conn_screen_cast = try_start(screen_cast);135+ dbus.conn_screen_cast = try_start(screen_cast, is_session_instance);
132 }136 }
133 137
134 let (to_niri, from_a11y) = calloop::channel::channel();138 let (to_niri, from_a11y) = calloop::channel::channel();
@@ -140,7 +144,7 @@ impl DBusServers {
140 })144 })
141 .unwrap();145 .unwrap();
142 let a11y_manager = freedesktop_a11y::Manager::new(to_niri, from_niri);146 let a11y_manager = freedesktop_a11y::Manager::new(to_niri, from_niri);
143- match a11y_manager.start() {147+ match a11y_manager.start(is_session_instance) {
144 Ok(conn) => {148 Ok(conn) => {
145 dbus.conn_a11y_manager = Some(conn);149 dbus.conn_a11y_manager = Some(conn);
146 niri.a11y_manager = Some(a11y_manager);150 niri.a11y_manager = Some(a11y_manager);
@@ -187,7 +191,13 @@ impl DBusServers {
187 }191 }
188 }192 }
189 193
190-fn request_name(conn: &Connection, name: &'static str) -> zbus::Result<()> {194+fn request_name(conn: &Connection, name: &'static str, monitor: bool) -> zbus::Result<()> {
195+ if monitor {
196+ if let Err(err) = start_name_monitor(conn, name) {
197+ warn!("error monitoring D-Bus name {name}: {err:?}");
198+ }
199+ }
200+
191 let flags = RequestNameFlags::AllowReplacement201 let flags = RequestNameFlags::AllowReplacement
192 | RequestNameFlags::ReplaceExisting202 | RequestNameFlags::ReplaceExisting
193 | RequestNameFlags::DoNotQueue;203 | RequestNameFlags::DoNotQueue;
@@ -196,8 +206,67 @@ fn request_name(conn: &Connection, name: &'static str) -> zbus::Result<()> {
196 Ok(())206 Ok(())
197 }207 }
198 208
199-fn try_start<I: Start>(iface: I) -> Option<Connection> {209+fn start_name_monitor(conn: &Connection, name: &'static str) -> anyhow::Result<()> {
200- match iface.start() {210+ let conn = conn.inner().clone();
211+ let monitor = async_io::block_on(async {
212+ let proxy = fdo::DBusProxy::new(&conn)
213+ .await
214+ .context("error creating a DBusProxy")?;
215+ let stream = proxy
216+ .receive_name_owner_changed_with_args(&[(0, name), (2, UniqueName::null_value())])
217+ .await
218+ .context("error creating a NameOwnerChanged stream")?;
219+
220+ let monitor = async move {
221+ if let Err(err) = monitor_name(proxy, stream, name).await {
222+ warn!("error monitoring D-Bus name {name}: {err:?}");
223+ }
224+ };
225+ anyhow::Ok(monitor)
226+ })?;
227+
228+ let task = conn.executor().spawn(monitor, "monitor D-Bus name");
229+ task.detach();
230+
231+ Ok(())
232+}
233+
234+async fn monitor_name(
235+ proxy: fdo::DBusProxy<'static>,
236+ mut stream: fdo::NameOwnerChangedStream,
237+ name: &'static str,
238+) -> anyhow::Result<()> {
239+ let name = WellKnownName::try_from(name).unwrap();
240+ let flags = RequestNameFlags::AllowReplacement | RequestNameFlags::DoNotQueue;
241+
242+ while let Some(signal) = stream.next().await {
243+ let args = signal
244+ .args()
245+ .context("error retrieving NameOwnerChanged args")?;
246+
247+ if args.new_owner().is_some() {
248+ error!("non-null new_owner should've been filtered out");
249+ continue;
250+ }
251+
252+ match proxy.request_name(name.clone(), flags).await? {
253+ RequestNameReply::PrimaryOwner | RequestNameReply::AlreadyOwner => {
254+ debug!("reacquired D-Bus name {name}");
255+ }
256+ RequestNameReply::Exists => {
257+ trace!("another process acquired D-Bus name {name}");
258+ }
259+ RequestNameReply::InQueue => {
260+ error!("D-Bus name request should not have been queued");
261+ }
262+ }
263+ }
264+
265+ Ok(())
266+}
267+
268+fn try_start<I: Start>(iface: I, monitor: bool) -> Option<Connection> {
269+ match iface.start(monitor) {
201 Ok(conn) => Some(conn),270 Ok(conn) => Some(conn),
202 Err(err) => {271 Err(err) => {
203 warn!("error starting {}: {err:?}", I::name());272 warn!("error starting {}: {err:?}", I::name());
src/dbus/mutter_display_config.rsmodified+2 -2
@@ -293,11 +293,11 @@ impl DisplayConfig {
293 }293 }
294 294
295 impl Start for DisplayConfig {295 impl Start for DisplayConfig {
296- fn start(self) -> anyhow::Result<zbus::blocking::Connection> {296+ fn start(self, monitor: bool) -> anyhow::Result<zbus::blocking::Connection> {
297 let conn = zbus::blocking::Connection::session()?;297 let conn = zbus::blocking::Connection::session()?;
298 conn.object_server()298 conn.object_server()
299 .at("/org/gnome/Mutter/DisplayConfig", self)?;299 .at("/org/gnome/Mutter/DisplayConfig", self)?;
300- request_name(&conn, "org.gnome.Mutter.DisplayConfig")?;300+ request_name(&conn, "org.gnome.Mutter.DisplayConfig", monitor)?;
301 301
302 Ok(conn)302 Ok(conn)
303 }303 }
src/dbus/mutter_screen_cast.rsmodified+2 -2
@@ -318,11 +318,11 @@ impl ScreenCast {
318 }318 }
319 319
320 impl Start for ScreenCast {320 impl Start for ScreenCast {
321- fn start(self) -> anyhow::Result<zbus::blocking::Connection> {321+ fn start(self, monitor: bool) -> anyhow::Result<zbus::blocking::Connection> {
322 let conn = zbus::blocking::Connection::session()?;322 let conn = zbus::blocking::Connection::session()?;
323 conn.object_server()323 conn.object_server()
324 .at("/org/gnome/Mutter/ScreenCast", self)?;324 .at("/org/gnome/Mutter/ScreenCast", self)?;
325- request_name(&conn, "org.gnome.Mutter.ScreenCast")?;325+ request_name(&conn, "org.gnome.Mutter.ScreenCast", monitor)?;
326 326
327 Ok(conn)327 Ok(conn)
328 }328 }
src/dbus/mutter_service_channel.rsmodified+1 -1
@@ -44,7 +44,7 @@ impl ServiceChannel {
44 }44 }
45 45
46 impl Start for ServiceChannel {46 impl Start for ServiceChannel {
47- fn start(self) -> anyhow::Result<zbus::blocking::Connection> {47+ fn start(self, _monitor: bool) -> anyhow::Result<zbus::blocking::Connection> {
48 let conn = zbus::blocking::connection::Builder::session()?48 let conn = zbus::blocking::connection::Builder::session()?
49 .name("org.gnome.Mutter.ServiceChannel")?49 .name("org.gnome.Mutter.ServiceChannel")?
50 .serve_at("/org/gnome/Mutter/ServiceChannel", self)?50 .serve_at("/org/gnome/Mutter/ServiceChannel", self)?