1use super::{PidFd, PidNs};
2use anyhow::Result;
3use std::{
4 fs,
5 path::{Path, PathBuf},
6};
7
8#[derive(Debug)]
9pub struct Pid {
10 pub fds: Vec<PidFd>,
12
13 pub nss: Vec<PidNs>,
15}
16
17impl Pid {
18 pub fn from_file<P>(path: P) -> Pid
19 where
20 P: AsRef<Path>,
21 {
22 let mut pb = PathBuf::new();
23 let mut fds = Vec::default();
24 pb.push(path);
25
26 pb.push("fd");
28 match fs::read_dir(&pb) {
29 Ok(entrys) => {
30 for entry in entrys {
31 match entry {
32 Ok(ent) => match PidFd::from_file(ent.path()) {
33 Ok(fd) => {
34 log::debug!("{:?}: {:?}", ent.path(), fd);
35 fds.push(fd);
36 }
37 Err(e) => {
38 println!("failed to parse file: {:?}, error: {}", ent.path(), e);
39 }
40 },
41 Err(e) => {
42 println!("failed to get entry: {}", e);
43 }
44 }
45 }
46 }
47 Err(e) => {
48 println!("failed to readdir: directory-{:?}, error-{}", pb, e);
49 }
50 }
51
52 pb.pop();
53
54 pb.push("ns");
56 for entry in fs::read_dir(&pb) {}
57 pb.pop();
58
59 Pid {
60 fds,
61 nss: Vec::default(),
62 }
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69 #[test]
70 fn test_pid_from_file() {
71 let pid = Pid::from_file("/proc/1/");
72 }
74}