macro_rules! file_descriptor_type {
($type:ident) => {
#[repr(transparent)]
- pub struct $type(::std::os::unix::io::RawFd);
+ pub struct $type(::std::os::unix::io::OwnedFd);
file_descriptor_impl!($type);
impl ::std::os::unix::io::FromRawFd for $type {
unsafe fn from_raw_fd(fd: ::std::os::unix::io::RawFd) -> Self {
- Self(fd)
+ Self(unsafe { ::std::os::unix::io::FromRawFd::from_raw_fd(fd) })
}
}
};
macro_rules! file_descriptor_impl {
($type:ty) => {
- impl Drop for $type {
- fn drop(&mut self) {
- if self.0 >= 0 {
- unsafe {
- libc::close(self.0);
- }
- }
- }
- }
-
impl ::std::os::unix::io::AsFd for $type {
fn as_fd(&self) -> ::std::os::unix::io::BorrowedFd<'_> {
- unsafe { ::std::os::unix::io::BorrowedFd::borrow_raw(self.0) }
+ ::std::os::unix::io::AsFd::as_fd(&self.0)
}
}
impl ::std::os::unix::io::AsRawFd for $type {
fn as_raw_fd(&self) -> ::std::os::unix::io::RawFd {
- self.0
+ ::std::os::unix::io::AsRawFd::as_raw_fd(&self.0)
}
}
impl ::std::os::unix::io::IntoRawFd for $type {
- fn into_raw_fd(mut self) -> ::std::os::unix::io::RawFd {
- let fd = self.0;
- self.0 = -libc::EBADF;
- fd
+ fn into_raw_fd(self) -> ::std::os::unix::io::RawFd {
+ ::std::os::unix::io::IntoRawFd::into_raw_fd(self.0)
}
}
};
use std::io;
use std::marker::PhantomData;
use std::os::raw::c_int;
-use std::os::unix::io::RawFd;
+use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
pub mod ns_type {
pub trait NsType {
let fd =
c_try!(unsafe { libc::openat(fd, path.as_ptr(), libc::O_RDONLY | libc::O_CLOEXEC) });
- Ok(Self(fd))
+ Ok(unsafe { Self::from_raw_fd(fd) })
}
pub fn setns(&self, ns_type: c_int) -> io::Result<()> {
- c_try!(unsafe { libc::setns(self.0, ns_type) });
+ c_try!(unsafe { libc::setns(self.as_raw_fd(), ns_type) });
Ok(())
}
}
use super::{CGroups, IdMap, IdMapEntry, ProcStatus, Uids, UserCaps};
-pub struct PidFd(RawFd, pid_t);
+pub struct PidFd(OwnedFd, pid_t);
file_descriptor_impl!(PidFd);
impl PidFd {
let path = CString::new(format!("/proc/{pid}")).unwrap();
let fd = c_try!(unsafe { libc::open(path.as_ptr(), libc::O_DIRECTORY | libc::O_CLOEXEC) });
+ let fd = unsafe { OwnedFd::from_raw_fd(fd) };
Ok(Self(fd, pid))
}
/// fails if reading the pid from the pidfd's proc entry fails.
pub unsafe fn try_from_fd(fd: OwnedFd) -> io::Result<Self> {
#[allow(clippy::unnecessary_cast)] // pid_t is a type alias
- let mut this = Self(fd.into_raw_fd(), -1 as pid_t);
+ let mut this = Self(fd, -1 as pid_t);
let pid = this.read_pid()?;
this.1 = pid;
Ok(this)
}
pub fn mount_namespace(&self) -> io::Result<NsFd<ns_type::Mount>> {
- NsFd::openat(self.0, c_str!("ns/mnt"))
+ NsFd::openat(self.0.as_raw_fd(), c_str!("ns/mnt"))
}
pub fn cgroup_namespace(&self) -> io::Result<NsFd<ns_type::Cgroup>> {
- NsFd::openat(self.0, c_str!("ns/cgroup"))
+ NsFd::openat(self.0.as_raw_fd(), c_str!("ns/cgroup"))
}
pub fn user_namespace(&self) -> io::Result<NsFd<ns_type::User>> {
- NsFd::openat(self.0, c_str!("ns/user"))
+ NsFd::openat(self.0.as_raw_fd(), c_str!("ns/user"))
}
fn fd(&self, path: &CStr, flags: c_int, mode: c_int) -> io::Result<OwnedFd> {