diff --git a/Cargo.lock b/Cargo.lock index 87451f53c..47ca7c60c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4664,6 +4664,7 @@ dependencies = [ "url", "wezterm-ssh", "wezterm-term", + "winapi 0.3.9", ] [[package]] diff --git a/wezterm-client/Cargo.toml b/wezterm-client/Cargo.toml index d32b8a657..3998b48d2 100644 --- a/wezterm-client/Cargo.toml +++ b/wezterm-client/Cargo.toml @@ -33,3 +33,6 @@ wezterm-term = { path = "../term", features=["use_serde"] } [target."cfg(windows)".dependencies] uds_windows = "1.0" +winapi = { version = "0.3", features = [ + "winuser", +]} diff --git a/wezterm-client/src/discovery.rs b/wezterm-client/src/discovery.rs new file mode 100644 index 000000000..31b514f52 --- /dev/null +++ b/wezterm-client/src/discovery.rs @@ -0,0 +1,280 @@ +use std::path::{Path, PathBuf}; + +/// There's a lot more code in this windows module than I thought I would need +/// to write. Ostensibly, we could get away with making a symlink by taking +/// the SessionName environment variable, combining it with the class name +/// and using a symlink to point to the actual path. +/// Symlinks are problematic on Windows, and the SessionName environment +/// variable may not be set. +/// It's a bit of a chore to resolve the name, and then it would be more +/// of a chore to manage the symlink. +/// What this module does is logically equivalent to the above, except +/// that it creates a piece of shared memory in the per-desktop namespace. +/// While there is a lot of code in here, it is simpler overall because +/// the naming is managed by the OS, as well as automatically removing +/// the name from the namespace when there are no more references to it. +#[cfg(windows)] +mod windows { + use anyhow::Context; + use super::*; + use std::io::Error as IoError; + use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE}; + use winapi::um::memoryapi::{ + CreateFileMappingW, MapViewOfFile, OpenFileMappingW, UnmapViewOfFile, FILE_MAP_ALL_ACCESS, + }; + use winapi::um::synchapi::{CreateMutexW, ReleaseMutex, WaitForSingleObject}; + use winapi::um::winbase::{INFINITE, WAIT_OBJECT_0}; + use winapi::um::winnt::{HANDLE, PAGE_READWRITE}; + + const MAX_NAME: usize = 1024; + + /// Keeps the published name alive for the duration of the process. + pub struct NameHolder { + _mapping: FileMapping, + _view: MappedView, + } + + /// A Windows file mapping + struct FileMapping { + name: String, + handle: HANDLE, + size: usize, + } + + impl Drop for FileMapping { + fn drop(&mut self) { + unsafe { CloseHandle(self.handle) }; + } + } + + impl FileMapping { + /// Create a new or open an existing mapping with the specified name/size + pub fn create(name: &str, size: usize) -> anyhow::Result { + let wide_name = wide_string(&name); + + let handle = unsafe { + CreateFileMappingW( + INVALID_HANDLE_VALUE, + std::ptr::null_mut(), + PAGE_READWRITE, + 0, + size as _, + wide_name.as_ptr(), + ) + }; + if handle.is_null() { + return Err(IoError::last_os_error()) + .with_context(|| format!("creating shared memory with name {}", name)); + } + Ok(Self { + name: name.to_string(), + handle, + size, + }) + } + + /// Attempt to open an existing mapping + pub fn open(name: &str, size: usize) -> anyhow::Result { + let wide_name = wide_string(&name); + + let handle = unsafe { OpenFileMappingW(FILE_MAP_ALL_ACCESS, 0, wide_name.as_ptr()) }; + if handle.is_null() { + return Err(IoError::last_os_error()) + .with_context(|| format!("creating shared memory with name {}", name)); + } + Ok(Self { + name: name.to_string(), + handle, + size, + }) + } + + /// Map the mapping into the process address space + pub fn map(&self) -> anyhow::Result { + let buf = + unsafe { MapViewOfFile(self.handle, FILE_MAP_ALL_ACCESS, 0, 0, self.size as _) }; + if buf.is_null() { + return Err(IoError::last_os_error()).with_context(|| { + format!("mapping view of shared memory with name {}", self.name) + }); + } + Ok(MappedView { + buf: buf as _, + size: self.size, + }) + } + } + + /// A mutex that can be used to coordinate between processes + struct NamedMutex { + handle: HANDLE, + } + impl Drop for NamedMutex { + fn drop(&mut self) { + unsafe { + CloseHandle(self.handle); + } + } + } + + impl NamedMutex { + /// Create a mutex with the specified name + pub fn new(name: &str) -> anyhow::Result { + let wide_name = wide_string(name); + let handle = unsafe { CreateMutexW(std::ptr::null_mut(), 0, wide_name.as_ptr()) }; + if handle.is_null() { + return Err(IoError::last_os_error()) + .with_context(|| format!("creating mutex name {}", name)); + } + Ok(Self { handle }) + } + + /// Acquire the mutex, and perform `func` while the mutex is held. + /// Once `func` returns, the mutex is released. + /// Returns the result of `func`. + pub fn with_lock(&self, func: F) -> anyhow::Result + where + F: FnOnce() -> anyhow::Result, + { + let res = unsafe { WaitForSingleObject(self.handle, INFINITE) }; + if res != WAIT_OBJECT_0 { + return Err(IoError::last_os_error()).context("acquire mutex"); + } + + let res = func(); + unsafe { ReleaseMutex(self.handle) }; + res + } + } + + /// A materialized view of a mapping + struct MappedView { + buf: *mut u8, + size: usize, + } + + impl Drop for MappedView { + fn drop(&mut self) { + unsafe { + UnmapViewOfFile(self.buf as _); + } + } + } + + impl MappedView { + fn slice_mut(&mut self) -> &mut [u8] { + unsafe { std::slice::from_raw_parts_mut(self.buf, self.size) } + } + + fn slice(&self) -> &[u8] { + unsafe { std::slice::from_raw_parts(self.buf, self.size) } + } + } + + impl NameHolder { + /// Computes the names of the objects; they use Local scoped + /// names so that we have one per desktop, rather than one + /// system wide. + fn compute_names(class_name: &str) -> (String, String) { + let mutex_name = format!("Local\\wezterm-sock-mutex-{}", class_name); + let map_name = format!("Local\\wezterm-sock-{}", class_name); + (mutex_name, map_name) + } + + /// Publish path as the path for class_name. + pub fn new(path: &Path, class_name: &str) -> anyhow::Result { + let (mutex_name, map_name) = Self::compute_names(class_name); + let path = path + .file_name() + .ok_or_else(|| anyhow::anyhow!("path has no file_name!?"))? + .to_str() + .ok_or_else(|| anyhow::anyhow!("path is not UTF8!"))? + .to_string(); + + let mutex = NamedMutex::new(&mutex_name)?; + mutex.with_lock(|| { + let mapping = FileMapping::create(&map_name, MAX_NAME)?; + let mut view = mapping.map()?; + + let target_slice = view.slice_mut(); + let len = path.len(); + + target_slice[0..len].copy_from_slice(path.as_bytes()); + target_slice[len] = 0; + + log::info!("published gui path as {}", path); + + Ok(Self { + _mapping: mapping, + _view: view, + }) + }) + } + + /// Resolve the existing path for class_name + pub fn resolve(class_name: &str) -> anyhow::Result { + let (mutex_name, map_name) = Self::compute_names(class_name); + let mutex = NamedMutex::new(&mutex_name)?; + mutex.with_lock(|| { + let mapping = FileMapping::open(&map_name, MAX_NAME)?; + let view = mapping.map()?; + + let source_slice = view.slice(); + let len = source_slice + .iter() + .position(|&c| c == 0) + .ok_or_else(|| anyhow::anyhow!("shared memory is not NUL terminated!"))?; + + let path = std::str::from_utf8(&source_slice[0..len]) + .context("reading path from shared memory")?; + + let path: PathBuf = path.into(); + + Ok(path) + }) + } + } + + /// Convert a rust string to a windows wide string + fn wide_string(s: &str) -> Vec { + use std::os::windows::ffi::OsStrExt; + std::ffi::OsStr::new(s) + .encode_wide() + .chain(std::iter::once(0)) + .collect() + } +} + +#[allow(dead_code)] +mod fallback { + use super::*; + pub struct NameHolder {} + impl NameHolder { + pub fn new(_path: &Path, _class_name: &str) -> anyhow::Result { + anyhow::bail!("Sock path publishing not implemented on this system"); + } + + pub fn resolve(_class_name: &str) -> anyhow::Result { + anyhow::bail!("Sock path publishing not implemented on this system"); + } + } +} + +#[cfg(windows)] +pub use self::windows::NameHolder; + +#[cfg(not(windows))] +pub use self::fallback::NameHolder; + +/// Unconditionally update the published path to match the provided path, +/// even if there is a running instance with a legitimate published path. +pub fn publish_gui_sock_path(path: &Path, class_name: &str) -> anyhow::Result { + NameHolder::new(path, class_name) +} + +/// Resolve the last published path for `class_name`. +/// If successful, there is NO guarantee that the returned path references +/// a running instance; it is just the last published path. +pub fn resolve_gui_sock_path(class_name: &str) -> anyhow::Result { + NameHolder::resolve(class_name) +} diff --git a/wezterm-client/src/lib.rs b/wezterm-client/src/lib.rs index 5a8307061..4f7c1d7df 100644 --- a/wezterm-client/src/lib.rs +++ b/wezterm-client/src/lib.rs @@ -4,5 +4,6 @@ use std::os::unix::net::UnixStream; use uds_windows::UnixStream; pub mod client; +pub mod discovery; pub mod domain; pub mod pane; diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index f61138a5c..3f85a4ae9 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -348,6 +348,14 @@ fn run_terminal_gui(opts: StartCommand) -> anyhow::Result<()> { config::RUNTIME_DIR.join(format!("gui-sock-{}", unsafe { libc::getpid() })); std::env::set_var("WEZTERM_UNIX_SOCKET", unix_socket_path.clone()); + let name_holder = wezterm_client::discovery::publish_gui_sock_path( + &unix_socket_path, + &crate::termwindow::get_window_class(), + ); + if let Err(err) = &name_holder { + log::warn!("{:#}", err); + } + if let Ok(mut listener) = wezterm_mux_server_impl::local::LocalListener::with_domain(&config::UnixDomain { socket_path: Some(unix_socket_path.clone()), diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 949a9fe3a..eb4ea1dd5 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -75,6 +75,10 @@ pub fn set_window_class(cls: &str) { *WINDOW_CLASS.lock().unwrap() = cls.to_owned(); } +pub fn get_window_class() -> String { + WINDOW_CLASS.lock().unwrap().clone() +} + /// Type used together with Window::notify to do something in the /// context of the window-specific event loop pub enum TermWindowNotif { @@ -720,7 +724,7 @@ impl TermWindow { let tw_event = Rc::clone(&tw); let window = Window::new_window( - &*WINDOW_CLASS.lock().unwrap(), + &get_window_class(), "wezterm", dimensions.pixel_width, dimensions.pixel_height,