//! The single-instance socket: one Hex process per user and data root. //! //! The running process binds `service.sock` next to its `service.lock`. A //! later `hex` forwards `Activate` (with the launcher's activation token) and //! exits; `hex dictation …`, `hex voice-action …`, `hex paste-last`, //! `hex status`, and `hex quit` forward over the same socket. Shortcut //! commands go straight from the socket worker to the listener's command //! queue, never through the window's thread. This module also owns the //! per-user systemd lifecycle (`Type=notify` readiness, the session //! environment, start, stop, and restart). use std::fs::{self, OpenOptions}; use std::io::{Read, Write}; use std::os::fd::AsRawFd; use std::os::unix::fs::{FileTypeExt, MetadataExt, OpenOptionsExt, PermissionsExt}; use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream}; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{self, SyncSender}; use std::sync::{Arc, Mutex}; use std::thread::{self, JoinHandle}; use std::time::{Duration, Instant}; use color_eyre::eyre::{Result, WrapErr, bail, eyre}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; use crate::desktop_host::DesktopSnapshot; use crate::linux_input::ShortcutCommand; /// Version 1 was the service/Settings-client protocol; its clients get a /// version error and exit. const VERSION: u32 = 2; const MAX_FRAME: usize = 128 * 1024; const IO_TIMEOUT: Duration = Duration::from_secs(2); const CLIENTS: usize = 8; const UNIT: &str = "hex.service"; /// How long a launch waits for a started unit's socket. `Type=notify` /// units are ready when `systemctl start` returns; units an older installer /// wrote are `Type=simple` and need the wait. const READY_TIMEOUT: Duration = Duration::from_secs(10); #[derive(Clone, Debug, Serialize, Deserialize)] pub enum Request { /// A relaunch: open or focus the window when `show`. A launcher's /// activation token rides along for the compositor. Activate { activation: Activation, show: bool, session: Session, }, /// A `hex dictation`, `hex voice-action`, or `hex paste-last` command. Shortcut(ShortcutCommand), Status, /// Drain accepted work and exit. Quit, } /// What a launcher handed the process that asked for the window. #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] pub struct Activation { /// Wayland `xdg_activation_v1` token (`XDG_ACTIVATION_TOKEN`). pub token: Option, /// X11 startup notification id (`DESKTOP_STARTUP_ID`). pub startup_id: Option, } impl Activation { /// Takes this process's activation environment so nothing it starts /// reuses a token meant for one window. pub fn take_from_environment() -> Self { let take = |key: &str| { let value = std::env::var(key).ok().filter(|value| !value.is_empty()); // Called on the main thread before Hex starts any other thread. unsafe { std::env::remove_var(key) }; value }; Self { token: take("XDG_ACTIVATION_TOKEN"), startup_id: take("DESKTOP_STARTUP_ID"), } } } /// A shortcut Settings records for the listener. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] pub enum ShortcutTarget { #[default] Dictation, PasteLast, VoiceAction, } /// What `hex status` prints and every reply carries. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Status { pub desktop: DesktopSnapshot, pub settings: crate::app_settings::AppSettings, pub editing: bool, pub hud_limitation: Option, pub window_open: bool, pub pid: u32, pub session: Session, } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Session { backend: String, display: String, runtime: String, } impl Session { pub fn current() -> Self { let session = crate::linux_session::LinuxSession::detect(); Self { backend: session.as_str().into(), display: std::env::var(if session.is_wayland() { "WAYLAND_DISPLAY" } else { "DISPLAY" }) .unwrap_or_default(), runtime: std::env::var("XDG_RUNTIME_DIR").unwrap_or_default(), } } } /// Where the socket hands a `hex` shortcut command: the running listener's /// queue, once it reports its session. #[derive(Clone)] pub struct ShortcutRoute { pub commands: SyncSender, pub voice_action: bool, } /// Routes one command, failing fast when nothing listens. pub fn route_shortcut(route: Option<&ShortcutRoute>, command: ShortcutCommand) -> Result<()> { let route = route .ok_or_else(|| eyre!("Hex is not listening; enable it in Settings or run `hex start`"))?; if command.is_voice_action() && !route.voice_action { bail!("Voice Action is off; turn it on in Settings"); } route .commands .try_send(command) .map_err(|_| eyre!("Hex is busy; try again")) } /// What the socket reads without waiting on the host or the window: the /// latest status and the listener's command queue, both published by the /// host thread. #[derive(Default)] pub struct Published { pub status: Mutex>, pub shortcuts: Mutex>, } /// Requests for the main thread, which owns the window. #[derive(Debug)] pub enum Control { Activate(Activation), Quit, } #[derive(Serialize, Deserialize)] struct Message { version: u32, request: Request, } #[derive(Serialize, Deserialize)] struct Reply { version: u32, #[serde(default)] status: Option, error: Option, } fn socket_path() -> Result { Ok(crate::app_paths::support_dir()?.join("service.sock")) } fn private_directory(path: &Path) -> Result<()> { fs::create_dir_all(path)?; let metadata = fs::symlink_metadata(path)?; if !metadata.is_dir() || metadata.uid() != unsafe { libc::geteuid() } { bail!("Hex requires a user-owned, non-symlink data directory"); } fs::set_permissions(path, fs::Permissions::from_mode(0o700))?; Ok(()) } fn check_peer(stream: &UnixStream) -> Result<()> { let mut credentials: libc::ucred = unsafe { std::mem::zeroed() }; let mut length = std::mem::size_of_val(&credentials) as libc::socklen_t; let result = unsafe { libc::getsockopt( stream.as_raw_fd(), libc::SOL_SOCKET, libc::SO_PEERCRED, (&mut credentials as *mut libc::ucred).cast(), &mut length, ) }; if result != 0 || credentials.uid != unsafe { libc::geteuid() } { bail!("Hex only accepts local IPC from the same user"); } Ok(()) } fn read_exact(stream: &mut UnixStream, mut bytes: &mut [u8], deadline: Instant) -> Result<()> { while !bytes.is_empty() { let remaining = deadline .checked_duration_since(Instant::now()) .ok_or_else(|| eyre!("Hex read timed out"))?; stream.set_read_timeout(Some(remaining))?; let count = stream.read(bytes)?; if count == 0 { bail!("Hex disconnected"); } bytes = &mut bytes[count..]; } Ok(()) } fn read_frame(stream: &mut UnixStream) -> Result { let deadline = Instant::now() + IO_TIMEOUT; let mut header = [0; 4]; read_exact(stream, &mut header, deadline)?; let length = u32::from_be_bytes(header) as usize; if length == 0 || length > MAX_FRAME { bail!("Invalid Hex frame size"); } let mut body = vec![0; length]; read_exact(stream, &mut body, deadline)?; Ok(serde_json::from_slice(&body)?) } fn write_frame(stream: &mut UnixStream, message: &T) -> Result<()> { let body = serde_json::to_vec(message)?; if body.len() > MAX_FRAME { bail!("Hex message exceeds the size limit"); } let deadline = Instant::now() + IO_TIMEOUT; write_all(stream, &(body.len() as u32).to_be_bytes(), deadline)?; write_all(stream, &body, deadline) } fn write_all(stream: &mut UnixStream, mut bytes: &[u8], deadline: Instant) -> Result<()> { while !bytes.is_empty() { let remaining = deadline .checked_duration_since(Instant::now()) .ok_or_else(|| eyre!("Hex write timed out"))?; stream.set_write_timeout(Some(remaining))?; let count = stream.write(bytes)?; if count == 0 { bail!("Hex disconnected"); } bytes = &bytes[count..]; } Ok(()) } /// Why a request did not reach a current instance. #[derive(Debug)] pub enum RequestError { /// Nothing listens on the socket. NotRunning, /// An older Hex (the service/Settings protocol) owns the socket. Outdated, Failed(color_eyre::Report), } impl std::fmt::Display for RequestError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::NotRunning => write!(f, "Hex is not running; run `hex start`"), Self::Outdated => write!( f, "An older Hex is running; quit it or run `hex restart`, then try again" ), Self::Failed(error) => write!(f, "{error:#}"), } } } impl std::error::Error for RequestError {} fn connect() -> std::result::Result { let path = socket_path().map_err(RequestError::Failed)?; let Ok(metadata) = fs::symlink_metadata(&path) else { return Err(RequestError::NotRunning); }; if !metadata.file_type().is_socket() || metadata.uid() != unsafe { libc::geteuid() } || metadata.mode() & 0o077 != 0 { return Err(RequestError::Failed(eyre!( "Refusing an insecure Hex socket" ))); } // A crashed instance leaves its socket behind. let stream = UnixStream::connect(path).map_err(|_| RequestError::NotRunning)?; check_peer(&stream).map_err(RequestError::Failed)?; Ok(stream) } /// Sends one request to the running instance. Never starts one. pub fn request(request: Request) -> std::result::Result { let mut stream = connect()?; write_frame( &mut stream, &Message { version: VERSION, request, }, ) .map_err(RequestError::Failed)?; let reply: Reply = read_frame(&mut stream).map_err(RequestError::Failed)?; if reply.version != VERSION { return Err(RequestError::Outdated); } if let Some(error) = reply.error { return Err(RequestError::Failed(eyre!(error))); } reply .status .ok_or_else(|| RequestError::Failed(eyre!("Invalid Hex reply"))) } pub struct Server { path: PathBuf, inode: u64, listener: Option, acceptor: Option>, workers: Vec>, stop: Arc, _lock: crate::instance::InstanceLock, } impl Server { /// Claims the data root. Fails when another instance holds it. pub fn bind() -> Result { if unsafe { libc::geteuid() } == 0 { bail!("Run Hex as a desktop user, never root"); } let path = socket_path()?; private_directory(path.parent().unwrap())?; let lock = crate::instance::acquire("service")?; if let Ok(metadata) = fs::symlink_metadata(&path) { if !metadata.file_type().is_socket() || metadata.uid() != unsafe { libc::geteuid() } { bail!("Refusing to replace an unmanaged Hex socket path"); } fs::remove_file(&path)?; } let listener = UnixListener::bind(&path)?; fs::set_permissions(&path, fs::Permissions::from_mode(0o600))?; let inode = fs::metadata(&path)?.ino(); Ok(Self { path, inode, listener: Some(listener), acceptor: None, workers: Vec::new(), stop: Arc::new(AtomicBool::new(false)), _lock: lock, }) } /// Starts answering once the host has published its first status. /// `Quit` sets `shutdown`, which the host thread drains on. pub fn serve( &mut self, published: Arc, control: SyncSender, shutdown: &'static AtomicBool, ) { let Some(listener) = self.listener.take() else { return; }; let (connections, pending) = mpsc::sync_channel::(CLIENTS); let pending = Arc::new(Mutex::new(pending)); let session = Session::current(); self.workers = (0..CLIENTS) .map(|_| { let pending = pending.clone(); let published = published.clone(); let control = control.clone(); let stop = self.stop.clone(); let session = session.clone(); thread::spawn(move || { loop { let Ok(stream) = pending.lock().unwrap().recv() else { break; }; if stop.load(Ordering::Acquire) { break; } serve_client(stream, &published, &control, &session, &stop, shutdown); } }) }) .collect(); let stop = self.stop.clone(); self.acceptor = Some(thread::spawn(move || { for stream in listener.incoming() { if stop.load(Ordering::Acquire) { break; } // A full pool drops the connection; its client fails fast. if let Ok(stream) = stream { let _ = connections.try_send(stream); } } })); } } fn serve_client( mut stream: UnixStream, published: &Published, control: &SyncSender, session: &Session, stop: &AtomicBool, shutdown: &AtomicBool, ) { if check_peer(&stream).is_err() { return; } while !stop.load(Ordering::Acquire) { // The version is read first: older clients send requests this // protocol no longer has. let Ok(frame) = read_frame::(&mut stream) else { break; }; let message = (frame.get("version").and_then(serde_json::Value::as_u64) == Some(VERSION.into())) .then(|| serde_json::from_value::(frame).ok()) .flatten(); let Some(message) = message else { let _ = write_frame( &mut stream, &Reply { version: VERSION, status: None, error: Some("Unsupported Hex request; restart Hex".into()), }, ); break; }; let result = match message.request { Request::Activate { activation, show, session: caller, } => { // A caller from another desktop session decides from the reply. if show && caller == *session { control .try_send(Control::Activate(activation)) .map_err(|_| eyre!("Hex is busy; try again")) } else { Ok(()) } } Request::Shortcut(command) => { route_shortcut(published.shortcuts.lock().unwrap().as_ref(), command) } Request::Status => Ok(()), Request::Quit => { shutdown.store(true, Ordering::Release); // The host thread also wakes the main thread when it sees the flag. let _ = control.try_send(Control::Quit); Ok(()) } }; let status = published.status.lock().unwrap().clone(); let error = match (&result, &status) { (Err(error), _) => Some(format!("{error:#}")), (Ok(()), None) => Some("Hex is starting; try again".into()), (Ok(()), Some(_)) => None, }; if write_frame( &mut stream, &Reply { version: VERSION, status, error, }, ) .is_err() { break; } } } impl Drop for Server { fn drop(&mut self) { self.stop.store(true, Ordering::Release); if let Some(acceptor) = self.acceptor.take() { // Wakes the blocking accept. let _ = UnixStream::connect(&self.path); let _ = acceptor.join(); } for worker in self.workers.drain(..) { let _ = worker.join(); } if fs::symlink_metadata(&self.path).is_ok_and(|m| m.ino() == self.inode) { let _ = fs::remove_file(&self.path); } } } /// Tells systemd (`Type=notify`) the socket answers. Without /// `NOTIFY_SOCKET` (no unit, or an older `Type=simple` unit) it does nothing. pub fn notify(state: &str) { let Some(path) = std::env::var_os("NOTIFY_SOCKET") else { return; }; let result = (|| -> std::io::Result<()> { let socket = UnixDatagram::unbound()?; let bytes = std::os::unix::ffi::OsStrExt::as_bytes(path.as_os_str()); if let Some(name) = bytes.strip_prefix(b"@") { use std::os::linux::net::SocketAddrExt; let address = std::os::unix::net::SocketAddr::from_abstract_name(name)?; socket.send_to_addr(state.as_bytes(), &address)?; } else { socket.send_to(state.as_bytes(), &path)?; } Ok(()) })(); if let Err(error) = result { tracing::warn!(%error, state, "could not notify systemd"); } } fn unit() -> String { std::env::var("HEX_SYSTEMD_UNIT") .ok() .filter(|unit| !unit.is_empty()) .unwrap_or_else(|| UNIT.into()) } /// Whether launches start Hex through its systemd user unit: the unit is /// installed (by the installer or Home Manager) and this process uses the /// default data root. `HEX_SYSTEMD_UNIT` names another unit, for isolated /// tests with a custom data root. pub fn supervised() -> bool { if std::env::var_os("HEX_APPLICATION_SUPPORT_DIR").is_some() && std::env::var_os("HEX_SYSTEMD_UNIT").is_none() { return false; } Command::new("systemctl") .args(["--user", "show", &unit(), "--property=LoadState", "--value"]) .stdin(Stdio::null()) .stderr(Stdio::null()) .output() .is_ok_and(|output| { output.status.success() && String::from_utf8_lossy(&output.stdout).trim() == "loaded" }) } fn systemctl(arguments: &[&str]) -> Result<()> { let unit = unit(); let mut child = Command::new("systemctl") .arg("--user") .args(arguments) .arg(&unit) .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn() .wrap_err("Could not run systemctl")?; // `start` waits for READY=1, which follows model-free startup. let deadline = Instant::now() + Duration::from_secs(40); loop { if let Some(status) = child.try_wait()? { if !status.success() { bail!( "systemctl --user {} {unit} failed; inspect `systemctl --user status {unit}`", arguments.join(" ") ); } return Ok(()); } if Instant::now() >= deadline { let _ = child.kill(); let _ = child.wait(); bail!("Timed out controlling {unit}"); } thread::sleep(Duration::from_millis(25)); } } /// Waits until the started unit answers. fn wait_ready() -> Result { let deadline = Instant::now() + READY_TIMEOUT; loop { match request(Request::Status) { Ok(status) => return Ok(status), Err(RequestError::Outdated) => return Err(RequestError::Outdated.into()), Err(_) if Instant::now() < deadline => thread::sleep(Duration::from_millis(50)), Err(error) => { return Err(eyre!( "Hex did not become ready ({error}); inspect `journalctl --user -u {}`", unit() )); } } } } /// What a launch found. pub enum Launch { /// The running instance took the request. Forwarded, /// Nothing runs and no unit supervises Hex: this process becomes it. BecomeInstance, } /// `hex`, `hex app`, and `hex start`: forward to the running instance, start /// the systemd unit, or tell the caller to become the instance. pub fn launch(activation: &Activation, show: bool, supervise: bool) -> Result { let activate = || Request::Activate { activation: activation.clone(), show, session: Session::current(), }; match request(activate()) { Ok(status) if status.session == Session::current() => return Ok(Launch::Forwarded), Ok(_) if supervise && supervised() => { // Hand the instance over to this desktop session. restart()?; } Ok(_) => { bail!("Hex is running in another desktop session; run `hex quit`, then launch it again") } Err(RequestError::Outdated) if supervise && supervised() => restart()?, Err(RequestError::NotRunning) if supervise && supervised() => { write_session_environment()?; systemctl(&["start"])?; } Err(RequestError::NotRunning) => return Ok(Launch::BecomeInstance), Err(error) => return Err(error.into()), } let status = wait_ready()?; if status.session != Session::current() { bail!( "{} did not load this desktop's environment; check its EnvironmentFile", unit() ); } request(activate())?; Ok(Launch::Forwarded) } /// `hex quit` and `hex stop`. pub fn quit() -> Result<()> { match request(Request::Quit) { Ok(_) => Ok(()), // An older service stops through its unit. Err(RequestError::Outdated) if supervised() => systemctl(&["stop"]), Err(RequestError::NotRunning) => Ok(()), Err(error) => Err(error.into()), } } pub fn restart() -> Result<()> { if !supervised() { bail!("Only Hex started by its systemd unit restarts; run `hex quit`, then launch it"); } // Without a desktop (over SSH), keep the one Hex last ran in. if graphical_session() || !session_environment_path()?.exists() { write_session_environment()?; } systemctl(&["restart"]) } pub fn schedule_restart() -> Result<()> { systemctl(&["--no-block", "restart"]) } /// The display the process connects to. Absent values are written empty /// so they override a stale user-manager environment from an earlier /// desktop. const SESSION_KEYS: &[&str] = &[ "DISPLAY", "WAYLAND_DISPLAY", "XAUTHORITY", "XDG_RUNTIME_DIR", "DBUS_SESSION_BUS_ADDRESS", "XDG_CURRENT_DESKTOP", "XDG_SESSION_TYPE", // The window's cursor theme and input method. "XCURSOR_THEME", "XCURSOR_SIZE", "XMODIFIERS", "GTK_IM_MODULE", "QT_IM_MODULE", ]; /// Written only when set: clearing them would break the user manager's own /// locale and data directories. const INHERITED_KEYS: &[&str] = &[ "XDG_DATA_DIRS", "LANG", "LANGUAGE", "LC_ALL", "LC_CTYPE", "LC_MESSAGES", ]; fn environment_line(key: &str, value: &str) -> Result { if value.contains(['\n', '\r', '\0']) { bail!("Invalid desktop environment value for {key}"); } Ok(format!( "{key}=\"{}\"\n", value.replace('\\', "\\\\").replace('"', "\\\"") )) } fn session_environment(get: impl Fn(&str) -> Option) -> Result { let mut body = String::new(); for key in SESSION_KEYS { body.push_str(&environment_line(key, &get(key).unwrap_or_default())?); } for key in INHERITED_KEYS { if let Some(value) = get(key).filter(|value| !value.is_empty()) { body.push_str(&environment_line(key, &value)?); } } Ok(body) } fn graphical_session() -> bool { ["DISPLAY", "WAYLAND_DISPLAY"] .iter() .any(|key| !std::env::var(key).unwrap_or_default().is_empty()) } fn session_environment_path() -> Result { Ok(crate::app_paths::support_dir()?.join("session.env")) } fn write_session_environment() -> Result<()> { if !graphical_session() { bail!("Start Hex from your graphical login session"); } let directory = crate::app_paths::support_dir()?; private_directory(&directory)?; let body = session_environment(|key| std::env::var(key).ok())?; let temporary = directory.join(format!(".session-{}.env", std::process::id())); let mut file = OpenOptions::new() .write(true) .create_new(true) .mode(0o600) .open(&temporary)?; let result = (|| -> Result<()> { file.write_all(body.as_bytes())?; file.sync_all()?; fs::rename(&temporary, session_environment_path()?)?; Ok(()) })(); if result.is_err() { let _ = fs::remove_file(temporary); } result } #[cfg(test)] mod tests { use super::*; #[test] fn frames_round_trip_and_reject_oversized_input() { let (mut client, mut server) = UnixStream::pair().unwrap(); check_peer(&client).unwrap(); write_frame( &mut client, &Message { version: VERSION, request: Request::Activate { activation: Activation { token: Some("token".into()), startup_id: None, }, show: true, session: Session::current(), }, }, ) .unwrap(); let message: Message = read_frame(&mut server).unwrap(); assert_eq!(message.version, VERSION); assert!(matches!( message.request, Request::Activate { activation, show: true, .. } if activation.token.as_deref() == Some("token") )); client .write_all(&((MAX_FRAME + 1) as u32).to_be_bytes()) .unwrap(); assert!(read_frame::(&mut server).is_err()); } #[test] fn incomplete_frames_and_disconnections_are_bounded() { let (mut client, mut server) = UnixStream::pair().unwrap(); client.write_all(&100_u32.to_be_bytes()).unwrap(); drop(client); assert!(read_frame::(&mut server).is_err()); let (_client, mut server) = UnixStream::pair().unwrap(); let started = Instant::now(); assert!( read_exact( &mut server, &mut [0; 4], started + Duration::from_millis(25) ) .is_err() ); assert!(started.elapsed() < Duration::from_secs(1)); } #[test] fn version_one_replies_parse_as_outdated() { let reply: Reply = serde_json::from_str( r#"{"version":1,"state":null,"error":"HEX service version mismatch; restart the service"}"#, ) .unwrap(); assert_eq!(reply.version, 1); assert!(reply.status.is_none()); } #[test] fn commands_reach_only_a_listening_route() { let error = route_shortcut(None, ShortcutCommand::DictationToggle).unwrap_err(); assert!(error.to_string().contains("not listening"), "{error}"); let (commands, received) = mpsc::sync_channel(2); let mut route = ShortcutRoute { commands, voice_action: false, }; route_shortcut(Some(&route), ShortcutCommand::DictationStart).unwrap(); let error = route_shortcut(Some(&route), ShortcutCommand::VoiceActionStart).unwrap_err(); assert!(error.to_string().contains("Voice Action is off"), "{error}"); route.voice_action = true; route_shortcut(Some(&route), ShortcutCommand::VoiceActionToggle).unwrap(); let error = route_shortcut(Some(&route), ShortcutCommand::PasteLast).unwrap_err(); assert!(error.to_string().contains("busy"), "{error}"); assert_eq!( received.try_iter().collect::>(), [ ShortcutCommand::DictationStart, ShortcutCommand::VoiceActionToggle ] ); } #[test] fn session_environment_clears_display_keys_and_keeps_locale_unset() { let body = session_environment(|key| match key { "DISPLAY" => Some(":0".into()), "XCURSOR_THEME" => Some("Adwaita".into()), "LANG" => Some("de_DE.UTF-8".into()), _ => None, }) .unwrap(); assert!(body.contains("DISPLAY=\":0\"\n")); assert!(body.contains("WAYLAND_DISPLAY=\"\"\n")); assert!(body.contains("XCURSOR_THEME=\"Adwaita\"\n")); assert!(body.contains("LANG=\"de_DE.UTF-8\"\n")); assert!(!body.contains("XDG_DATA_DIRS")); assert!(!body.contains("LC_ALL")); assert!(environment_line("DISPLAY", ":0\nEVIL=yes").is_err()); } }