From a38757ef3baae62ceee04288b376e6262faf1ae3 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 24 Dec 2021 07:21:00 -0700 Subject: [PATCH] mux: improve process leader heuristics on windows refs: #1417 --- .../lua/pane/get_foreground_process_name.md | 2 +- mux/src/localpane.rs | 55 +++++++++++++------ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/docs/config/lua/pane/get_foreground_process_name.md b/docs/config/lua/pane/get_foreground_process_name.md index df49875fc..dd6202a00 100644 --- a/docs/config/lua/pane/get_foreground_process_name.md +++ b/docs/config/lua/pane/get_foreground_process_name.md @@ -7,7 +7,7 @@ Returns the path to the executable image for the pane. This method has some restrictions and caveats: * This information is only available for local panes. Multiplexer panes do not report this information. Similarly, if you are using eg: `ssh` to connect to a remote host, you won't be able to access the name of the remote process that is running. -* On unix systems, the *process group leader* (the foreground process) will be queried, but that concept doesn't exist on Windows, so instead, the program that was used to launch the pane will be used +* On unix systems, the *process group leader* (the foreground process) will be queried, but that concept doesn't exist on Windows, so instead, the process tree of the originally spawned program is examined, and the most recently spawned descendant is assumed to be the foreground process * On Linux, macOS and Windows, the process can be queried to determine this path. Other operating systems (notably, FreeBSD and other unix systems) are not currently supported * Querying the path may fail for a variety of reasons outside of the control of WezTerm * Querying process information has some runtime overhead, which may cause wezterm to slow down if over-used. diff --git a/mux/src/localpane.rs b/mux/src/localpane.rs index ef1c29d27..53f12e232 100644 --- a/mux/src/localpane.rs +++ b/mux/src/localpane.rs @@ -327,27 +327,46 @@ impl Pane for LocalPane { #[cfg(any(windows, target_os = "linux", target_os = "macos"))] fn get_foreground_process_name(&self) -> Option { - use sysinfo::{Pid, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt}; - let leader; - #[cfg(unix)] { - leader = self.pty.borrow().process_group_leader()?; - } - #[cfg(windows)] - { - if let ProcessState::Running { pid: Some(pid), .. } = &*self.process.borrow() { - leader = *pid; - } else { - return None; - } + use sysinfo::{Pid, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt}; + let leader = self.pty.borrow().process_group_leader()?; + let system = System::new_with_specifics( + RefreshKind::new().with_processes(ProcessRefreshKind::new()), + ); + let proc = system.process(leader as Pid)?; + Some(proc.exe().to_string_lossy().to_string()) } - let system = System::new_with_specifics( - RefreshKind::new().with_processes(ProcessRefreshKind::new()), - ); - let proc = system.process(leader as Pid)?; - Some(proc.exe().to_string_lossy().to_string()) + #[cfg(windows)] + { + // Windows doesn't have any job control or session concept, + // so we infer that the equivalent to the process group + // leader is the most recently spawned program running + // in the console + if let Some(root_proc) = self.divine_process_list() { + let mut youngest = &root_proc; + + fn find_youngest<'a>( + proc: &'a LocalProcessInfo, + youngest: &mut &'a LocalProcessInfo, + ) { + if proc.start_time > youngest.start_time { + *youngest = proc; + } + + for child in proc.children.values() { + find_youngest(child, youngest); + } + } + + find_youngest(&root_proc, &mut youngest); + + Some(youngest.executable.to_string_lossy().to_string()) + } else { + None + } + } } fn can_close_without_prompting(&self, _reason: CloseReason) -> bool { @@ -882,6 +901,7 @@ pub struct LocalProcessInfo { pub cwd: PathBuf, pub status: LocalProcessStatus, pub children: HashMap, + pub start_time: u64, } luahelper::impl_lua_conversion!(LocalProcessInfo); @@ -932,6 +952,7 @@ impl LocalProcessInfo { executable: proc.exe().to_path_buf(), cwd: proc.cwd().to_path_buf(), argv: proc.cmd().to_vec(), + start_time: proc.start_time(), status: LocalProcessStatus::from_process_status(proc.status()), children, }