xwayland: Make abstract socket optional and Linux-only
Browse files at this commit4ac4cb4a44a208898db3f80334258b2ed719f62cParent: 365274e2 changed files +58 -36
- src/utils/xwayland/mod.rs +14 -5
- src/utils/xwayland/satellite.rs +44 -31
src/utils/xwayland/mod.rsmodified+14 -5
| @@ -1,5 +1,4 @@ | |||
| 1 | use std::os::fd::OwnedFd; | 1 | use std::os::fd::OwnedFd; |
| 2 | -use std::os::linux::net::SocketAddrExt; | ||
| 3 | use std::os::unix::net::{SocketAddr, UnixListener}; | 2 | use std::os::unix::net::{SocketAddr, UnixListener}; |
| 4 | 3 | ||
| 5 | use anyhow::{anyhow, ensure, Context as _}; | 4 | use anyhow::{anyhow, ensure, Context as _}; |
| @@ -17,7 +16,8 @@ const X11_TMP_UNIX_DIR: &str = "/tmp/.X11-unix"; | |||
| 17 | 16 | ||
| 18 | struct X11Connection { | 17 | struct X11Connection { |
| 19 | display_name: String, | 18 | display_name: String, |
| 20 | - abstract_fd: OwnedFd, | 19 | + // Optional because there are no abstract sockets on FreeBSD. |
| 20 | + abstract_fd: Option<OwnedFd>, | ||
| 21 | unix_fd: OwnedFd, | 21 | unix_fd: OwnedFd, |
| 22 | _unix_guard: Unlink, | 22 | _unix_guard: Unlink, |
| 23 | _lock_guard: Unlink, | 23 | _lock_guard: Unlink, |
| @@ -82,7 +82,10 @@ fn bind_to_socket(addr: &SocketAddr) -> anyhow::Result<UnixListener> { | |||
| 82 | Ok(listener) | 82 | Ok(listener) |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | +#[cfg(target_os = "linux")] | ||
| 85 | fn bind_to_abstract_socket(display: u32) -> anyhow::Result<UnixListener> { | 86 | fn bind_to_abstract_socket(display: u32) -> anyhow::Result<UnixListener> { |
| 87 | + use std::os::linux::net::SocketAddrExt; | ||
| 88 | + | ||
| 86 | let name = format!("/tmp/.X11-unix/X{display}"); | 89 | let name = format!("/tmp/.X11-unix/X{display}"); |
| 87 | let addr = SocketAddr::from_abstract_name(name).unwrap(); | 90 | let addr = SocketAddr::from_abstract_name(name).unwrap(); |
| 88 | bind_to_socket(&addr) | 91 | bind_to_socket(&addr) |
| @@ -97,8 +100,14 @@ fn bind_to_unix_socket(display: u32) -> anyhow::Result<(UnixListener, Unlink)> { | |||
| 97 | bind_to_socket(&addr).map(|listener| (listener, guard)) | 100 | bind_to_socket(&addr).map(|listener| (listener, guard)) |
| 98 | } | 101 | } |
| 99 | 102 | ||
| 100 | -fn open_display_sockets(display: u32) -> anyhow::Result<(UnixListener, UnixListener, Unlink)> { | 103 | +fn open_display_sockets( |
| 101 | - let a = bind_to_abstract_socket(display).context("error binding to abstract socket")?; | 104 | + display: u32, |
| 105 | +) -> anyhow::Result<(Option<UnixListener>, UnixListener, Unlink)> { | ||
| 106 | + #[cfg(target_os = "linux")] | ||
| 107 | + let a = Some(bind_to_abstract_socket(display).context("error binding to abstract socket")?); | ||
| 108 | + #[cfg(not(target_os = "linux"))] | ||
| 109 | + let a = None; | ||
| 110 | + | ||
| 102 | let (u, g) = bind_to_unix_socket(display).context("error binding to unix socket")?; | 111 | let (u, g) = bind_to_unix_socket(display).context("error binding to unix socket")?; |
| 103 | Ok((a, u, g)) | 112 | Ok((a, u, g)) |
| 104 | } | 113 | } |
| @@ -138,7 +147,7 @@ fn setup_connection() -> anyhow::Result<X11Connection> { | |||
| 138 | }; | 147 | }; |
| 139 | 148 | ||
| 140 | let display_name = format!(":{display}"); | 149 | let display_name = format!(":{display}"); |
| 141 | - let abstract_fd = OwnedFd::from(a); | 150 | + let abstract_fd = a.map(OwnedFd::from); |
| 142 | let unix_fd = OwnedFd::from(u); | 151 | let unix_fd = OwnedFd::from(u); |
| 143 | 152 | ||
| 144 | Ok(X11Connection { | 153 | Ok(X11Connection { |
src/utils/xwayland/satellite.rsmodified+44 -31
| @@ -163,27 +163,29 @@ fn setup_watch(state: &mut State) { | |||
| 163 | event_loop.remove(token); | 163 | event_loop.remove(token); |
| 164 | } | 164 | } |
| 165 | 165 | ||
| 166 | - let fd = satellite.x11.abstract_fd.try_clone().unwrap(); | 166 | + if let Some(fd) = &satellite.x11.abstract_fd { |
| 167 | - let fd = clear_out_pending_connections(fd); | 167 | + let fd = fd.try_clone().unwrap(); |
| 168 | - let source = Generic::new(fd, Interest::READ, Mode::Level); | 168 | + let fd = clear_out_pending_connections(fd); |
| 169 | - let token = event_loop | 169 | + let source = Generic::new(fd, Interest::READ, Mode::Level); |
| 170 | - .insert_source(source, move |_, _, state| { | 170 | + let token = event_loop |
| 171 | - if let Some(satellite) = &mut state.niri.satellite { | 171 | + .insert_source(source, move |_, _, state| { |
| 172 | - // Remove the other source. | 172 | + if let Some(satellite) = &mut state.niri.satellite { |
| 173 | - if let Some(token) = satellite.unix_token.take() { | 173 | + // Remove the other source. |
| 174 | - state.niri.event_loop.remove(token); | 174 | + if let Some(token) = satellite.unix_token.take() { |
| 175 | + state.niri.event_loop.remove(token); | ||
| 176 | + } | ||
| 177 | + // Clear this source. | ||
| 178 | + satellite.abstract_token = None; | ||
| 179 | + | ||
| 180 | + debug!("connection to X11 abstract socket; spawning xwayland-satellite"); | ||
| 181 | + let path = state.niri.config.borrow().xwayland_satellite.path.clone(); | ||
| 182 | + spawn(path, satellite); | ||
| 175 | } | 183 | } |
| 176 | - // Clear this source. | 184 | + Ok(PostAction::Remove) |
| 177 | - satellite.abstract_token = None; | 185 | + }) |
| 178 | - | 186 | + .unwrap(); |
| 179 | - debug!("connection to X11 abstract socket; spawning xwayland-satellite"); | 187 | + satellite.abstract_token = Some(token); |
| 180 | - let path = state.niri.config.borrow().xwayland_satellite.path.clone(); | 188 | + } |
| 181 | - spawn(path, satellite); | ||
| 182 | - } | ||
| 183 | - Ok(PostAction::Remove) | ||
| 184 | - }) | ||
| 185 | - .unwrap(); | ||
| 186 | - satellite.abstract_token = Some(token); | ||
| 187 | 189 | ||
| 188 | let fd = satellite.x11.unix_fd.try_clone().unwrap(); | 190 | let fd = satellite.x11.unix_fd.try_clone().unwrap(); |
| 189 | let fd = clear_out_pending_connections(fd); | 191 | let fd = clear_out_pending_connections(fd); |
| @@ -211,7 +213,11 @@ fn setup_watch(state: &mut State) { | |||
| 211 | fn spawn(path: String, xwl: &Satellite) { | 213 | fn spawn(path: String, xwl: &Satellite) { |
| 212 | let _span = tracy_client::span!("satellite::spawn"); | 214 | let _span = tracy_client::span!("satellite::spawn"); |
| 213 | 215 | ||
| 214 | - let abstract_fd = xwl.x11.abstract_fd.try_clone().unwrap(); | 216 | + let abstract_fd = xwl |
| 217 | + .x11 | ||
| 218 | + .abstract_fd | ||
| 219 | + .as_ref() | ||
| 220 | + .map(|fd| fd.try_clone().unwrap()); | ||
| 215 | let unix_fd = xwl.x11.unix_fd.try_clone().unwrap(); | 221 | let unix_fd = xwl.x11.unix_fd.try_clone().unwrap(); |
| 216 | let to_main = xwl.to_main.clone(); | 222 | let to_main = xwl.to_main.clone(); |
| 217 | 223 | ||
| @@ -257,15 +263,20 @@ fn spawn(path: String, xwl: &Satellite) { | |||
| 257 | } | 263 | } |
| 258 | } | 264 | } |
| 259 | 265 | ||
| 260 | -fn spawn_and_wait(path: &Path, mut process: Command, abstract_fd: OwnedFd, unix_fd: OwnedFd) { | 266 | +fn spawn_and_wait( |
| 261 | - let abstract_raw = abstract_fd.as_raw_fd(); | 267 | + path: &Path, |
| 268 | + mut process: Command, | ||
| 269 | + abstract_fd: Option<OwnedFd>, | ||
| 270 | + unix_fd: OwnedFd, | ||
| 271 | +) { | ||
| 272 | + let abstract_raw = abstract_fd.as_ref().map(|fd| fd.as_raw_fd()); | ||
| 262 | let unix_raw = unix_fd.as_raw_fd(); | 273 | let unix_raw = unix_fd.as_raw_fd(); |
| 263 | 274 | ||
| 264 | - process | 275 | + process.arg("-listenfd").arg(unix_raw.to_string()); |
| 265 | - .arg("-listenfd") | 276 | + |
| 266 | - .arg(abstract_raw.to_string()) | 277 | + if let Some(abstract_raw) = abstract_raw { |
| 267 | - .arg("-listenfd") | 278 | + process.arg("-listenfd").arg(abstract_raw.to_string()); |
| 268 | - .arg(unix_raw.to_string()); | 279 | + } |
| 269 | 280 | ||
| 270 | unsafe { | 281 | unsafe { |
| 271 | process.pre_exec(move || { | 282 | process.pre_exec(move || { |
| @@ -273,12 +284,14 @@ fn spawn_and_wait(path: &Path, mut process: Command, abstract_fd: OwnedFd, unix_ | |||
| 273 | // that we want to pass it. | 284 | // that we want to pass it. |
| 274 | 285 | ||
| 275 | // We're not dropping these until after spawn(). | 286 | // We're not dropping these until after spawn(). |
| 276 | - let abstract_fd = BorrowedFd::borrow_raw(abstract_raw); | ||
| 277 | let unix_fd = BorrowedFd::borrow_raw(unix_raw); | 287 | let unix_fd = BorrowedFd::borrow_raw(unix_raw); |
| 278 | - | ||
| 279 | - fcntl_setfd(abstract_fd, FdFlags::empty())?; | ||
| 280 | fcntl_setfd(unix_fd, FdFlags::empty())?; | 288 | fcntl_setfd(unix_fd, FdFlags::empty())?; |
| 281 | 289 | ||
| 290 | + if let Some(abstract_raw) = abstract_raw { | ||
| 291 | + let abstract_fd = BorrowedFd::borrow_raw(abstract_raw); | ||
| 292 | + fcntl_setfd(abstract_fd, FdFlags::empty())?; | ||
| 293 | + } | ||
| 294 | + | ||
| 282 | Ok(()) | 295 | Ok(()) |
| 283 | }) | 296 | }) |
| 284 | }; | 297 | }; |