use std::path::PathBuf;
type Result<T> = std::result::Result<T, crate::ProcErr>;
pub fn _self() -> Result<PathBuf> {
Ok(std::fs::read_link("/proc/self")?)
}
pub fn self_pid() -> Result<u32> {
let path = std::fs::read_link("/proc/self")?;
let pid_str = path.display().to_string();
let pid = pid_str.parse::<u32>()?;
Ok(pid)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_self_self_pid() {
println!("/proc/self point to {:?}", _self().unwrap());
println!("current pid is {:?}", self_pid().unwrap());
}
#[test]
fn test_self_pid() {
let pid = unsafe { libc::getpid() } as u32;
assert_eq!(pid, self_pid().unwrap())
}
}