pty_info.rs

  1use alacritty_terminal::tty::Pty;
  2#[cfg(target_os = "windows")]
  3use std::num::NonZeroU32;
  4#[cfg(unix)]
  5use std::os::fd::AsRawFd;
  6use std::path::PathBuf;
  7
  8#[cfg(target_os = "windows")]
  9use windows::Win32::{Foundation::HANDLE, System::Threading::GetProcessId};
 10
 11use sysinfo::{Pid, Process, ProcessRefreshKind, RefreshKind, System, UpdateKind};
 12
 13struct ProcessIdGetter {
 14    handle: i32,
 15    fallback_pid: u32,
 16}
 17
 18#[cfg(unix)]
 19impl ProcessIdGetter {
 20    fn new(pty: &Pty) -> ProcessIdGetter {
 21        ProcessIdGetter {
 22            handle: pty.file().as_raw_fd(),
 23            fallback_pid: pty.child().id(),
 24        }
 25    }
 26
 27    fn pid(&self) -> Option<Pid> {
 28        let pid = unsafe { libc::tcgetpgrp(self.handle) };
 29        if pid < 0 {
 30            return Some(Pid::from_u32(self.fallback_pid));
 31        }
 32        Some(Pid::from_u32(pid as u32))
 33    }
 34}
 35
 36#[cfg(windows)]
 37impl ProcessIdGetter {
 38    fn new(pty: &Pty) -> ProcessIdGetter {
 39        let child = pty.child_watcher();
 40        let handle = child.raw_handle();
 41        let fallback_pid = child
 42            .pid()
 43            .unwrap_or_else(|| unsafe { NonZeroU32::new_unchecked(GetProcessId(HANDLE(handle))) });
 44
 45        ProcessIdGetter {
 46            handle: handle as i32,
 47            fallback_pid: u32::from(fallback_pid),
 48        }
 49    }
 50
 51    fn pid(&self) -> Option<Pid> {
 52        let pid = unsafe { GetProcessId(HANDLE(self.handle as _)) };
 53        // the GetProcessId may fail and returns zero, which will lead to a stack overflow issue
 54        if pid == 0 {
 55            // in the builder process, there is a small chance, almost negligible,
 56            // that this value could be zero, which means child_watcher returns None,
 57            // GetProcessId returns 0.
 58            if self.fallback_pid == 0 {
 59                return None;
 60            }
 61            return Some(Pid::from_u32(self.fallback_pid));
 62        }
 63        Some(Pid::from_u32(pid))
 64    }
 65}
 66
 67#[derive(Clone, Debug)]
 68pub struct ProcessInfo {
 69    pub name: String,
 70    pub cwd: PathBuf,
 71    pub argv: Vec<String>,
 72}
 73
 74/// Fetches Zed-relevant Pseudo-Terminal (PTY) process information
 75pub struct PtyProcessInfo {
 76    system: System,
 77    refresh_kind: ProcessRefreshKind,
 78    pid_getter: ProcessIdGetter,
 79    pub current: Option<ProcessInfo>,
 80}
 81
 82impl PtyProcessInfo {
 83    pub fn new(pty: &Pty) -> PtyProcessInfo {
 84        let process_refresh_kind = ProcessRefreshKind::new()
 85            .with_cmd(UpdateKind::Always)
 86            .with_cwd(UpdateKind::Always)
 87            .with_exe(UpdateKind::Always);
 88        let refresh_kind = RefreshKind::new().with_processes(process_refresh_kind);
 89        let system = System::new_with_specifics(refresh_kind);
 90
 91        PtyProcessInfo {
 92            system,
 93            refresh_kind: process_refresh_kind,
 94            pid_getter: ProcessIdGetter::new(pty),
 95            current: None,
 96        }
 97    }
 98
 99    fn refresh(&mut self) -> Option<&Process> {
100        let pid = self.pid_getter.pid()?;
101        if self
102            .system
103            .refresh_process_specifics(pid, self.refresh_kind)
104        {
105            self.system.process(pid)
106        } else {
107            None
108        }
109    }
110
111    fn load(&mut self) -> Option<ProcessInfo> {
112        let process = self.refresh()?;
113        let cwd = process
114            .cwd()
115            .take()
116            .map_or(PathBuf::new(), |p| p.to_owned());
117
118        let info = ProcessInfo {
119            name: process.name().to_owned(),
120            cwd,
121            argv: process.cmd().to_vec(),
122        };
123        self.current = Some(info.clone());
124        Some(info)
125    }
126
127    /// Updates the cached process info, returns whether the Zed-relevant info has changed
128    pub fn has_changed(&mut self) -> bool {
129        let current = self.load();
130        let has_changed = match (self.current.as_ref(), current.as_ref()) {
131            (None, None) => false,
132            (Some(prev), Some(now)) => prev.cwd != now.cwd || prev.name != now.name,
133            _ => true,
134        };
135        if has_changed {
136            self.current = current;
137        }
138        has_changed
139    }
140}