1use super::FdType;
2use anyhow::Result;
3
4use std::{
5 fs,
6 path::{Path, PathBuf},
7};
8
9#[derive(Debug)]
10pub struct PidFd {
11 fdtype: FdType,
12}
13
14impl PidFd {
15 pub fn from_file<P>(path: P) -> Result<PidFd>
16 where
17 P: AsRef<Path>,
18 {
19 let inf = fs::read_link(path)?;
20 let mut fdtype = FdType::Unknown;
21
22 if let Some(name) = inf.to_str() {
23 if name.starts_with("socket") {
25 fdtype = FdType::SocketFd(name[8..name.len() - 1].parse()?);
26 }
27 }
28 Ok(PidFd { fdtype })
29 }
30
31 pub fn fdtype(&self) -> FdType {
32 self.fdtype
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39 #[test]
40 fn test_pidfs_from_file() {
41 let pidfd = PidFd::from_file("/proc/1/fd/70");
42 assert_eq!(pidfd.is_ok(), true);
43 }
44}