ZIm/crates/terminal/src/pty_info.rs
renovate[bot] 7814dd0301
Update Rust crate sysinfo to 0.31.0 (#17733)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [sysinfo](https://redirect.github.com/GuillaumeGomez/sysinfo) |
workspace.dependencies | minor | `0.30.7` -> `0.31.0` |

---

### Release Notes

<details>
<summary>GuillaumeGomez/sysinfo (sysinfo)</summary>

###
[`v0.31.4`](https://redirect.github.com/GuillaumeGomez/sysinfo/blob/HEAD/CHANGELOG.md#0314)

[Compare
Source](https://redirect.github.com/GuillaumeGomez/sysinfo/compare/v0.31.3...v0.31.4)

-   macOS: Force memory cleanup in disk list retrieval.

###
[`v0.31.3`](https://redirect.github.com/GuillaumeGomez/sysinfo/blob/HEAD/CHANGELOG.md#0313)

[Compare
Source](https://redirect.github.com/GuillaumeGomez/sysinfo/compare/v0.31.2...v0.31.3)

-   Raspberry Pi: Fix temperature retrieval.

###
[`v0.31.2`](https://redirect.github.com/GuillaumeGomez/sysinfo/blob/HEAD/CHANGELOG.md#0312)

[Compare
Source](https://redirect.github.com/GuillaumeGomez/sysinfo/compare/v0.31.1...v0.31.2)

-   Remove `bstr` dependency (needed for rustc development).

###
[`v0.31.1`](https://redirect.github.com/GuillaumeGomez/sysinfo/blob/HEAD/CHANGELOG.md#0311)

[Compare
Source](https://redirect.github.com/GuillaumeGomez/sysinfo/compare/v0.31.0...v0.31.1)

-   Downgrade version of `memchr` (needed for rustc development).

###
[`v0.31.0`](https://redirect.github.com/GuillaumeGomez/sysinfo/blob/HEAD/CHANGELOG.md#0310)

[Compare
Source](https://redirect.github.com/GuillaumeGomez/sysinfo/compare/v0.30.13...v0.31.0)

-   Split crate in features to only enable what you need.
- Remove `System::refresh_process`, `System::refresh_process_specifics`
and `System::refresh_pids`
    methods.
- Add new argument of type `ProcessesToUpdate` to
`System::refresh_processes` and `System::refresh_processes_specifics`
methods.
-   Add new `NetworkData::ip_networks` method.
-   Add new `System::refresh_cpu_list` method.
-   Global CPU now only contains CPU usage.
-   Rename `TermalSensorType` to `ThermalSensorType`.
-   Process names is now an `OsString`.
-   Remove `System::global_cpu_info`.
-   Add `System::global_cpu_usage`.
- macOS: Fix invalid CPU computation when single processes are refreshed
one after the other.
-   Windows: Fix virtual memory computation.
-   Windows: Fix WoW64 parent process refresh.
-   Linux: Retrieve RSS (Resident Set Size) memory for cgroups.

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 3pm on Wednesday" in timezone
America/New_York, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

Release Notes:

- N/A

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC43NC4xIiwidXBkYXRlZEluVmVyIjoiMzguODAuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
2024-09-17 21:50:37 +02:00

145 lines
4.2 KiB
Rust

use alacritty_terminal::tty::Pty;
#[cfg(target_os = "windows")]
use std::num::NonZeroU32;
#[cfg(unix)]
use std::os::fd::AsRawFd;
use std::path::PathBuf;
#[cfg(target_os = "windows")]
use windows::Win32::{Foundation::HANDLE, System::Threading::GetProcessId};
use sysinfo::{Pid, Process, ProcessRefreshKind, RefreshKind, System, UpdateKind};
struct ProcessIdGetter {
handle: i32,
fallback_pid: u32,
}
#[cfg(unix)]
impl ProcessIdGetter {
fn new(pty: &Pty) -> ProcessIdGetter {
ProcessIdGetter {
handle: pty.file().as_raw_fd(),
fallback_pid: pty.child().id(),
}
}
fn pid(&self) -> Option<Pid> {
let pid = unsafe { libc::tcgetpgrp(self.handle) };
if pid < 0 {
return Some(Pid::from_u32(self.fallback_pid));
}
Some(Pid::from_u32(pid as u32))
}
}
#[cfg(windows)]
impl ProcessIdGetter {
fn new(pty: &Pty) -> ProcessIdGetter {
let child = pty.child_watcher();
let handle = child.raw_handle();
let fallback_pid = child.pid().unwrap_or_else(|| unsafe {
NonZeroU32::new_unchecked(GetProcessId(HANDLE(handle as _)))
});
ProcessIdGetter {
handle: handle as i32,
fallback_pid: u32::from(fallback_pid),
}
}
fn pid(&self) -> Option<Pid> {
let pid = unsafe { GetProcessId(HANDLE(self.handle as _)) };
// the GetProcessId may fail and returns zero, which will lead to a stack overflow issue
if pid == 0 {
// in the builder process, there is a small chance, almost negligible,
// that this value could be zero, which means child_watcher returns None,
// GetProcessId returns 0.
if self.fallback_pid == 0 {
return None;
}
return Some(Pid::from_u32(self.fallback_pid));
}
Some(Pid::from_u32(pid))
}
}
#[derive(Clone, Debug)]
pub struct ProcessInfo {
pub name: String,
pub cwd: PathBuf,
pub argv: Vec<String>,
}
/// Fetches Zed-relevant Pseudo-Terminal (PTY) process information
pub struct PtyProcessInfo {
system: System,
refresh_kind: ProcessRefreshKind,
pid_getter: ProcessIdGetter,
pub current: Option<ProcessInfo>,
}
impl PtyProcessInfo {
pub fn new(pty: &Pty) -> PtyProcessInfo {
let process_refresh_kind = ProcessRefreshKind::new()
.with_cmd(UpdateKind::Always)
.with_cwd(UpdateKind::Always)
.with_exe(UpdateKind::Always);
let refresh_kind = RefreshKind::new().with_processes(process_refresh_kind);
let system = System::new_with_specifics(refresh_kind);
PtyProcessInfo {
system,
refresh_kind: process_refresh_kind,
pid_getter: ProcessIdGetter::new(pty),
current: None,
}
}
fn refresh(&mut self) -> Option<&Process> {
let pid = self.pid_getter.pid()?;
if self.system.refresh_processes_specifics(
sysinfo::ProcessesToUpdate::Some(&[pid]),
self.refresh_kind,
) == 1
{
self.system.process(pid)
} else {
None
}
}
fn load(&mut self) -> Option<ProcessInfo> {
let process = self.refresh()?;
let cwd = process
.cwd()
.take()
.map_or(PathBuf::new(), |p| p.to_owned());
let info = ProcessInfo {
name: process.name().to_str()?.to_owned(),
cwd,
argv: process
.cmd()
.iter()
.filter_map(|s| s.to_str().map(ToOwned::to_owned))
.collect(),
};
self.current = Some(info.clone());
Some(info)
}
/// Updates the cached process info, returns whether the Zed-relevant info has changed
pub fn has_changed(&mut self) -> bool {
let current = self.load();
let has_changed = match (self.current.as_ref(), current.as_ref()) {
(None, None) => false,
(Some(prev), Some(now)) => prev.cwd != now.cwd || prev.name != now.name,
_ => true,
};
if has_changed {
self.current = current;
}
has_changed
}
}