cli: cleanup some process querying code (#170306)

Avoid pulling system info we don't have to.

Minor cleanups while working on https://github.com/microsoft/vscode/pull/170305
This commit is contained in:
Connor Peet
2022-12-30 12:55:57 -08:00
committed by GitHub
parent 6106980525
commit b5ad508dfb
3 changed files with 13 additions and 29 deletions

View File

@@ -7,35 +7,30 @@ use std::path::Path;
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};
pub fn process_at_path_exists(pid: u32, name: &Path) -> bool {
// TODO https://docs.rs/sysinfo/latest/sysinfo/index.html#usage
let mut sys = System::new_all();
sys.refresh_processes();
let mut sys = System::new();
let pid = Pid::from_u32(pid);
if !sys.refresh_process(pid) {
return false;
}
let name_str = format!("{}", name.display());
match sys.process(Pid::from_u32(pid)) {
Some(process) => {
for cmd in process.cmd() {
if cmd.contains(&name_str) {
return true;
}
if let Some(process) = sys.process(pid) {
for cmd in process.cmd() {
if cmd.contains(&name_str) {
return true;
}
}
None => {
return false;
}
}
false
}
pub fn process_exists(pid: u32) -> bool {
let mut sys = System::new_all();
sys.refresh_processes();
sys.process(Pid::from_u32(pid)).is_some()
let mut sys = System::new();
sys.refresh_process(Pid::from_u32(pid))
}
pub fn find_running_process(name: &Path) -> Option<u32> {
// TODO https://docs.rs/sysinfo/latest/sysinfo/index.html#usage
let mut sys = System::new_all();
let mut sys = System::new();
sys.refresh_processes();
let name_str = format!("{}", name.display());