3 use std
::marker
::PhantomData
;
4 use std
::os
::raw
::c_int
;
5 use std
::os
::unix
::io
::{AsRawFd, FromRawFd, IntoRawFd, RawFd}
;
9 const TYPE
: libc
::c_int
;
12 macro_rules
! define_ns_type
{
13 ($name
:ident
, $number
:expr
) => {
15 impl NsType
for $name
{
16 const TYPE
: libc
::c_int
= $number
;
21 define_ns_type
!(Mount
, libc
::CLONE_NEWNS
);
22 define_ns_type
!(User
, libc
::CLONE_NEWUSER
);
23 define_ns_type
!(Cgroup
, libc
::CLONE_NEWCGROUP
);
26 pub use ns_type
::NsType
;
28 file_descriptor_type
!(RawNsFd
);
31 pub fn open(path
: &CStr
) -> io
::Result
<Self> {
32 Self::openat(libc
::AT_FDCWD
, path
)
35 pub fn openat(fd
: RawFd
, path
: &CStr
) -> io
::Result
<Self> {
37 c_try
!(unsafe { libc::openat(fd, path.as_ptr(), libc::O_RDONLY | libc::O_CLOEXEC) }
);
42 pub fn setns(&self, ns_type
: c_int
) -> io
::Result
<()> {
43 c_try
!(unsafe { libc::setns(self.0, ns_type) }
);
49 pub struct NsFd
<T
: NsType
>(RawNsFd
, PhantomData
<T
>);
51 impl<T
: NsType
> std
::ops
::Deref
for NsFd
<T
> {
52 type Target
= RawNsFd
;
54 fn deref(&self) -> &Self::Target
{
59 impl<T
: NsType
> NsFd
<T
> {
60 pub fn open(path
: &CStr
) -> io
::Result
<Self> {
61 Ok(Self(RawNsFd
::open(path
)?
, PhantomData
))
64 pub fn openat(fd
: RawFd
, path
: &CStr
) -> io
::Result
<Self> {
65 Ok(Self(RawNsFd
::openat(fd
, path
)?
, PhantomData
))
68 pub fn setns(&self) -> io
::Result
<()> {