• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Utils for dealing with processes
2 
3 use std::fs::{read, read_dir};
4 
5 /// return PID given name
get_pid(process_name: &str) -> Option<i32>6 pub(crate) fn get_pid(process_name: &str) -> Option<i32> {
7     for entry in read_dir("/proc").ok()? {
8         let entry = entry.ok()?;
9         let path = entry.path();
10 
11         if path.is_dir() {
12             let cmdline_path = path.join("cmdline");
13             if let Ok(cmdline_bytes) = read(cmdline_path) {
14                 let cmdline = String::from_utf8_lossy(&cmdline_bytes);
15                 if cmdline == process_name || cmdline.starts_with(process_name) {
16                     if let Some(pid_str) = path.file_name().and_then(|s| s.to_str()) {
17                         if let Ok(pid) = pid_str.parse::<i32>() {
18                             return Some(pid);
19                         }
20                     }
21                 }
22             }
23         }
24     }
25 
26     None
27 }
28