]>
Commit | Line | Data |
---|---|---|
e420f6f9 WB |
1 | //! pidfd helper functionality |
2 | ||
3 | use std::ffi::CString; | |
4 | use std::io; | |
5 | use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; | |
6 | ||
7 | use crate::nsfd::{ns_type, NsFd}; | |
8 | use crate::tools::Fd; | |
9 | use crate::{file_descriptor_type, libc_try}; | |
10 | ||
11 | file_descriptor_type!(PidFd); | |
12 | ||
13 | impl PidFd { | |
14 | pub fn open(pid: libc::pid_t) -> io::Result<Self> { | |
15 | let path = CString::new(format!("/proc/{}", pid)).unwrap(); | |
16 | ||
17 | let fd = | |
18 | libc_try!(unsafe { libc::open(path.as_ptr(), libc::O_DIRECTORY | libc::O_CLOEXEC) }); | |
19 | ||
20 | Ok(Self(fd)) | |
21 | } | |
22 | ||
23 | pub fn mount_namespace(&self) -> io::Result<NsFd<ns_type::Mount>> { | |
24 | NsFd::openat(self.0, "ns/mnt") | |
25 | } | |
26 | ||
27 | pub fn cgroup_namespace(&self) -> io::Result<NsFd<ns_type::Cgroup>> { | |
28 | NsFd::openat(self.0, "ns/cgroup") | |
29 | } | |
30 | ||
31 | pub fn user_namespace(&self) -> io::Result<NsFd<ns_type::User>> { | |
32 | NsFd::openat(self.0, "ns/user") | |
33 | } | |
34 | ||
35 | pub fn cwd_fd(&self) -> io::Result<Fd> { | |
36 | Ok(Fd(libc_try!(unsafe { | |
37 | libc::openat( | |
38 | self.as_raw_fd(), | |
39 | b"cwd".as_ptr() as *const _, | |
40 | libc::O_DIRECTORY, | |
41 | ) | |
42 | }))) | |
43 | } | |
44 | } |