Skip to content

Commit

Permalink
cli: cleanup some process querying code (microsoft#170306)
Browse files Browse the repository at this point in the history
Avoid pulling system info we don't have to.

Minor cleanups while working on microsoft#170305
  • Loading branch information
connor4312 authored Dec 30, 2022
1 parent 6106980 commit b5ad508
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
9 changes: 0 additions & 9 deletions cli/src/tunnels/code_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,6 @@ pub enum AnyCodeServer {
Port(PortCodeServer),
}

// impl AnyCodeServer {
// pub fn origin(&mut self) -> &mut CodeServerOrigin {
// match self {
// AnyCodeServer::Socket(p) => &mut p.origin,
// AnyCodeServer::Port(p) => &mut p.origin,
// }
// }
// }

pub enum CodeServerOrigin {
/// A new code server, that opens the barrier when it exits.
New(Box<Child>),
Expand Down
4 changes: 1 addition & 3 deletions cli/src/tunnels/control_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ impl HandlerContext {
async fn dispose(self) {
let bridges: ServerBridgeList = {
let mut lock = self.server_bridges.lock().await;
let bridges = lock.take();
*lock = None;
bridges
lock.take()
};

if let Some(b) = bridges {
Expand Down
29 changes: 12 additions & 17 deletions cli/src/util/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit b5ad508

Please sign in to comment.