diff --git a/crates/grove-gui/src/instance.rs b/crates/grove-gui/src/instance.rs index d3a45c5..eec1aa4 100644 --- a/crates/grove-gui/src/instance.rs +++ b/crates/grove-gui/src/instance.rs @@ -2,16 +2,47 @@ //! its window and exits. use std::io::{BufRead, BufReader, Write}; +use std::os::unix::fs::{DirBuilderExt, MetadataExt}; use std::os::unix::net::{UnixListener, UnixStream}; use std::path::{Path, PathBuf}; use std::time::Duration; -/// `$XDG_RUNTIME_DIR/grove/gui.sock` (same private directory as the daemon socket). +/// `$XDG_RUNTIME_DIR/grove/gui.sock` (same private directory as the daemon socket); without a +/// runtime dir, a private per-user directory under the temp dir. pub fn default_socket_path() -> PathBuf { - let base = std::env::var_os("XDG_RUNTIME_DIR") - .map(PathBuf::from) - .unwrap_or_else(std::env::temp_dir); - base.join("grove").join("gui.sock") + match std::env::var_os("XDG_RUNTIME_DIR") { + Some(base) => PathBuf::from(base).join("grove").join("gui.sock"), + None => std::env::temp_dir() + .join(format!("grove-gui-{}", own_uid())) + .join("gui.sock"), + } +} + +fn own_uid() -> u32 { + std::fs::metadata("/proc/self") + .map(|m| m.uid()) + .unwrap_or(0) +} + +/// Creates `dir` with mode 0700, or checks that an existing one is ours and not accessible to +/// others (a pre-created directory in a shared /tmp must not be trusted). +pub fn ensure_private_dir(dir: &Path) -> std::io::Result<()> { + if let Some(parent) = dir.parent() { + std::fs::create_dir_all(parent)?; + } + match std::fs::DirBuilder::new().mode(0o700).create(dir) { + Ok(()) => return Ok(()), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {} + Err(e) => return Err(e), + } + let m = std::fs::symlink_metadata(dir)?; + if !m.is_dir() || m.uid() != own_uid() || m.mode() & 0o077 != 0 { + return Err(std::io::Error::other(format!( + "{} is not a private directory owned by you", + dir.display() + ))); + } + Ok(()) } pub enum Role { @@ -23,15 +54,30 @@ pub enum Role { /// Tries to hand over to a running instance; otherwise becomes the primary one. pub fn acquire(path: &Path, show: bool) -> std::io::Result { + acquire_with(path, show, Duration::from_secs(2)) +} + +pub fn acquire_with(path: &Path, show: bool, timeout: Duration) -> std::io::Result { if let Some(dir) = path.parent() { - std::fs::create_dir_all(dir)?; + ensure_private_dir(dir)?; } for _ in 0..3 { - if notify(path, if show { "show" } else { "ping" }).is_ok() { - return Ok(Role::Secondary); + match notify(path, if show { "show" } else { "ping" }, timeout) { + Ok(()) => return Ok(Role::Secondary), + // Nobody listens: a stale socket file from a crash is replaced. + Err(e) + if matches!( + e.kind(), + std::io::ErrorKind::ConnectionRefused | std::io::ErrorKind::NotFound + ) => + { + if e.kind() == std::io::ErrorKind::ConnectionRefused { + let _ = std::fs::remove_file(path); + } + } + // A live instance that does not answer in time is never evicted. + Err(e) => return Err(e), } - // Nobody answers: a stale socket from a crash is replaced. - let _ = std::fs::remove_file(path); match UnixListener::bind(path) { Ok(l) => return Ok(Role::Primary(l)), Err(e) if e.kind() == std::io::ErrorKind::AddrInUse => continue, @@ -41,9 +87,9 @@ pub fn acquire(path: &Path, show: bool) -> std::io::Result { Err(std::io::Error::other("cannot acquire the instance socket")) } -fn notify(path: &Path, msg: &str) -> std::io::Result<()> { +fn notify(path: &Path, msg: &str, timeout: Duration) -> std::io::Result<()> { let mut c = UnixStream::connect(path)?; - c.set_read_timeout(Some(Duration::from_secs(2)))?; + c.set_read_timeout(Some(timeout))?; writeln!(c, "{msg}")?; let mut line = String::new(); BufReader::new(c).read_line(&mut line)?; @@ -73,11 +119,13 @@ pub fn serve(listener: UnixListener, on_show: impl Fn() + Send + 'static) { #[cfg(test)] mod tests { use super::*; + use std::os::unix::fs::PermissionsExt; use std::sync::mpsc; fn tmp_sock(tag: &str) -> PathBuf { let d = std::env::temp_dir().join(format!("gg-{tag}-{}", std::process::id())); std::fs::create_dir_all(&d).unwrap(); + std::fs::set_permissions(&d, std::fs::Permissions::from_mode(0o700)).unwrap(); d.join("gui.sock") } @@ -97,6 +145,34 @@ mod tests { assert!(rx.recv_timeout(Duration::from_millis(200)).is_err()); } + #[test] + fn unresponsive_instance_is_not_evicted() { + let p = tmp_sock("busy"); + let l = UnixListener::bind(&p).unwrap(); + // Accepts but never answers (a primary stuck in startup). + let t = std::thread::spawn(move || { + let c = l.accept().unwrap(); + std::thread::sleep(Duration::from_millis(600)); + drop(c); + }); + let r = acquire_with(&p, true, Duration::from_millis(200)); + assert!(r.is_err(), "must not become primary"); + assert!(p.exists(), "socket file must stay"); + t.join().unwrap(); + } + + #[test] + fn private_dir_is_enforced() { + let base = std::env::temp_dir().join(format!("gg-priv-{}", std::process::id())); + let d = base.join("x"); + ensure_private_dir(&d).unwrap(); + assert_eq!(std::fs::metadata(&d).unwrap().mode() & 0o777, 0o700); + ensure_private_dir(&d).unwrap(); // idempotent + std::fs::set_permissions(&d, std::fs::Permissions::from_mode(0o755)).unwrap(); + assert!(ensure_private_dir(&d).is_err()); + let _ = std::fs::remove_dir_all(&base); + } + #[test] fn stale_socket_is_replaced() { let p = tmp_sock("stale");