-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c33ce0
commit 8ccfa21
Showing
2 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,66 @@ | ||
use anyhow::Result; | ||
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt}; | ||
|
||
pub fn name(_pid: i32) -> Result<String> { | ||
unimplemented!("Method unimplemented") | ||
} | ||
pub fn name(pid: i32) -> Result<String> { | ||
if !System::IS_SUPPORTED { | ||
return Err(anyhow::anyhow!("This OS isn't supported for process functions. | ||
Please see sysinfo docs for a full list of supported systems. | ||
https://docs.rs/sysinfo/0.23.5/sysinfo/index.html#supported-oses\n\n")); | ||
} | ||
|
||
let mut sys = System::new(); | ||
sys.refresh_processes(); | ||
|
||
let mut res = ""; | ||
|
||
if let Some(process) = sys.process(Pid::from_u32(pid as u32)) { | ||
res = process.name() | ||
} | ||
|
||
Ok(res.to_string()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::{process::Command}; | ||
|
||
#[test] | ||
fn test_process_name() -> anyhow::Result<()>{ | ||
let mut commandstring = ""; | ||
if cfg!(target_os = "linux") || | ||
cfg!(target_os = "ios") || | ||
cfg!(target_os = "macos") || | ||
cfg!(target_os = "android") || | ||
cfg!(target_os = "freebsd") || | ||
cfg!(target_os = "openbsd") || | ||
cfg!(target_os = "netbsd") { | ||
commandstring = "sleep"; | ||
} else if cfg!(target_os = "windows") { | ||
commandstring = "timeout"; | ||
} else { | ||
return Err(anyhow::anyhow!("OS Not supported please re run on Linux, Windows, or MacOS")); | ||
} | ||
|
||
let child = Command::new(commandstring) | ||
.arg("5") | ||
.spawn()?; | ||
|
||
let pname = name(child.id() as i32)?; | ||
if cfg!(target_os = "linux") || | ||
cfg!(target_os = "ios") || | ||
cfg!(target_os = "macos") || | ||
cfg!(target_os = "android") || | ||
cfg!(target_os = "freebsd") || | ||
cfg!(target_os = "openbsd") || | ||
cfg!(target_os = "netbsd") { | ||
//If linux or Mac, Process Name should be 'sleep' | ||
assert_eq!(pname, "sleep") | ||
}else if cfg!(target_os = "windows") { | ||
//If windows,Pprocess Name should be 'timeout.exe' | ||
assert_eq!(pname, "timeout.exe") | ||
} | ||
|
||
return Ok(()) | ||
} | ||
} |