1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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())
}
}