]> git.proxmox.com Git - pve-lxc-syscalld.git/commitdiff
use OwnedFd for all remaining custom file descriptors
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 16 Oct 2023 11:23:31 +0000 (13:23 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 16 Oct 2023 11:23:52 +0000 (13:23 +0200)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/macros.rs
src/nsfd.rs
src/process/pid_fd.rs

index 1184eb063bf6fb572bf88f99f5871c3e6a9f5551..8b4cc867d837b057bd99b43386e98b2e46572e53 100644 (file)
@@ -10,13 +10,13 @@ macro_rules! c_str {
 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) })
             }
         }
     };
@@ -24,33 +24,21 @@ macro_rules! file_descriptor_type {
 
 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)
             }
         }
     };
index 18f5e7d8d122bc60ceca34df3278701cfedc9d29..7a587d79386a492dfdfc96fe274af0f14207441f 100644 (file)
@@ -4,7 +4,7 @@ use std::ffi::CStr;
 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 {
@@ -38,11 +38,11 @@ impl RawNsFd {
         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(())
     }
 }
index 869c2b694b92e1fa09e1ad514cac700a27358873..c684e1868bbeb67fe7eaa4a207559a6f3457b301 100644 (file)
@@ -15,7 +15,7 @@ use crate::nsfd::{ns_type, NsFd};
 
 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 {
@@ -27,6 +27,7 @@ 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))
     }
@@ -39,22 +40,22 @@ impl PidFd {
     /// 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> {