mux: improve process leader heuristics on windows

refs: #1417
This commit is contained in:
Wez Furlong committed 2021-12-24 07:21:00 -07:00
1 parent bd6072b320
commit a38757ef3b
2 files changed
+39 -18

No files matched your search

@@ -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.
+38 -17
View File
@@ -327,27 +327,46 @@ impl Pane for LocalPane {
#[cfg(any(windows, target_os = "linux", target_os = "macos"))]
fn get_foreground_process_name(&self) -> Option<String> {
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<u32, LocalProcessInfo>,
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,
}