From 02f11dc1f9c2fc180a83aaa3f323e05c759a013f Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Mon, 27 Nov 2023 15:42:23 +0800 Subject: [PATCH 1/3] refactor: make Epoll take BorrowedFd --- src/sys/epoll.rs | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 88314ef926..c11c9e0b8e 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -3,7 +3,8 @@ pub use crate::poll_timeout::PollTimeout as EpollTimeout; use crate::Result; use libc::{self, c_int}; use std::mem; -use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd, RawFd}; +use std::os::fd::{AsFd, BorrowedFd}; +use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd, RawFd}; libc_bitflags!( pub struct EpollFlags: c_int { @@ -85,7 +86,7 @@ impl EpollEvent { /// /// // Create eventfd & Add event /// let eventfd = eventfd(0, EfdFlags::empty())?; -/// epoll.add(&eventfd, EpollEvent::new(EpollFlags::EPOLLIN,DATA))?; +/// epoll.add(eventfd.as_fd(), EpollEvent::new(EpollFlags::EPOLLIN,DATA))?; /// /// // Arm eventfd & Time wait /// write(&eventfd, &1u64.to_ne_bytes())?; @@ -102,8 +103,13 @@ impl EpollEvent { /// # } /// ``` #[derive(Debug)] -pub struct Epoll(pub OwnedFd); -impl Epoll { +#[repr(transparent)] +pub struct Epoll<'fd> { + epoll_fd: OwnedFd, + _fds: std::marker::PhantomData<*mut &'fd ()>, +} + +impl<'fd> Epoll<'fd> { /// Creates a new epoll instance and returns a file descriptor referring to that instance. /// /// [`epoll_create1`](https://man7.org/linux/man-pages/man2/epoll_create1.2.html). @@ -111,28 +117,36 @@ impl Epoll { let res = unsafe { libc::epoll_create1(flags.bits()) }; let fd = Errno::result(res)?; let owned_fd = unsafe { OwnedFd::from_raw_fd(fd) }; - Ok(Self(owned_fd)) + + Ok(Self { + epoll_fd: owned_fd, + _fds: std::marker::PhantomData, + }) } /// Add an entry to the interest list of the epoll file descriptor for /// specified in events. /// /// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_ADD`. - pub fn add(&self, fd: Fd, mut event: EpollEvent) -> Result<()> { + pub fn add( + &self, + fd: BorrowedFd<'fd>, + mut event: EpollEvent, + ) -> Result<()> { self.epoll_ctl(EpollOp::EpollCtlAdd, fd, &mut event) } /// Remove (deregister) the target file descriptor `fd` from the interest list. /// /// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_DEL` . - pub fn delete(&self, fd: Fd) -> Result<()> { + pub fn delete(&self, fd: BorrowedFd<'fd>) -> Result<()> { self.epoll_ctl(EpollOp::EpollCtlDel, fd, None) } /// Change the settings associated with `fd` in the interest list to the new settings specified /// in `event`. /// /// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_MOD`. - pub fn modify( + pub fn modify( &self, - fd: Fd, + fd: BorrowedFd<'fd>, event: &mut EpollEvent, ) -> Result<()> { self.epoll_ctl(EpollOp::EpollCtlMod, fd, event) @@ -148,7 +162,7 @@ impl Epoll { ) -> Result { let res = unsafe { libc::epoll_wait( - self.0.as_raw_fd(), + self.epoll_fd.as_raw_fd(), events.as_mut_ptr().cast(), events.len() as c_int, timeout.into().into(), @@ -164,10 +178,10 @@ impl Epoll { /// When possible prefer [`Epoll::add`], [`Epoll::delete`] and [`Epoll::modify`]. /// /// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) - fn epoll_ctl<'a, Fd: AsFd, T>( + fn epoll_ctl<'a, T>( &self, op: EpollOp, - fd: Fd, + fd: BorrowedFd<'fd>, event: T, ) -> Result<()> where @@ -179,9 +193,9 @@ impl Epoll { .unwrap_or(std::ptr::null_mut()); unsafe { Errno::result(libc::epoll_ctl( - self.0.as_raw_fd(), + self.epoll_fd.as_raw_fd(), op as c_int, - fd.as_fd().as_raw_fd(), + fd.as_raw_fd(), ptr, )) .map(drop) @@ -189,6 +203,12 @@ impl Epoll { } } +impl AsFd for Epoll<'_> { + fn as_fd(&self) -> BorrowedFd<'_> { + self.epoll_fd.as_fd() + } +} + #[deprecated(since = "0.27.0", note = "Use Epoll::new() instead")] #[inline] pub fn epoll_create() -> Result { From 23e6f4c2a918585b675be6ace33e421eded000b7 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Tue, 28 Nov 2023 10:27:13 +0800 Subject: [PATCH 2/3] update test and changelog --- changelog/2232.changed.md | 3 +++ test/sys/test_epoll.rs | 31 ++++++++++++++----------------- 2 files changed, 17 insertions(+), 17 deletions(-) create mode 100644 changelog/2232.changed.md diff --git a/changelog/2232.changed.md b/changelog/2232.changed.md new file mode 100644 index 0000000000..cad869b25c --- /dev/null +++ b/changelog/2232.changed.md @@ -0,0 +1,3 @@ +`Epoll` and its functions now take `fd: BorrowedFd` rather than `fd: &Fd` + +The `OwnedFd` field of `Epoll` is no longer exposed, please use the `AsFd` trait diff --git a/test/sys/test_epoll.rs b/test/sys/test_epoll.rs index 84b100c1e9..e5fd65b7d0 100644 --- a/test/sys/test_epoll.rs +++ b/test/sys/test_epoll.rs @@ -1,26 +1,23 @@ -#![allow(deprecated)] - -use nix::errno::Errno; -use nix::sys::epoll::{epoll_create1, epoll_ctl}; -use nix::sys::epoll::{EpollCreateFlags, EpollEvent, EpollFlags, EpollOp}; +use nix::{ + errno::Errno, + sys::epoll::{Epoll, EpollCreateFlags, EpollEvent, EpollFlags}, +}; +use std::os::fd::BorrowedFd; #[test] pub fn test_epoll_errno() { - let efd = epoll_create1(EpollCreateFlags::empty()).unwrap(); - let result = epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None); - result.expect_err("assertion failed"); + let epoll = Epoll::new(EpollCreateFlags::empty()).unwrap(); + let fd_1 = unsafe { BorrowedFd::borrow_raw(1) }; + let result = epoll.delete(fd_1); assert_eq!(result.unwrap_err(), Errno::ENOENT); - - let result = epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, None); - result.expect_err("assertion failed"); - assert_eq!(result.unwrap_err(), Errno::EINVAL); } #[test] pub fn test_epoll_ctl() { - let efd = epoll_create1(EpollCreateFlags::empty()).unwrap(); - let mut event = - EpollEvent::new(EpollFlags::EPOLLIN | EpollFlags::EPOLLERR, 1); - epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, &mut event).unwrap(); - epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None).unwrap(); + let epoll = Epoll::new(EpollCreateFlags::empty()).unwrap(); + let event = EpollEvent::new(EpollFlags::EPOLLIN | EpollFlags::EPOLLERR, 1); + let fd_1 = unsafe { BorrowedFd::borrow_raw(1) }; + + epoll.add(fd_1, event).unwrap(); + epoll.delete(fd_1).unwrap(); } From de313b36b63cb23e7b8b1285174b38b3cbcde532 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Tue, 28 Nov 2023 10:28:33 +0800 Subject: [PATCH 3/3] update test func name --- test/sys/test_epoll.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sys/test_epoll.rs b/test/sys/test_epoll.rs index e5fd65b7d0..c660b0e76e 100644 --- a/test/sys/test_epoll.rs +++ b/test/sys/test_epoll.rs @@ -13,7 +13,7 @@ pub fn test_epoll_errno() { } #[test] -pub fn test_epoll_ctl() { +pub fn test_epoll_add_delete() { let epoll = Epoll::new(EpollCreateFlags::empty()).unwrap(); let event = EpollEvent::new(EpollFlags::EPOLLIN | EpollFlags::EPOLLERR, 1); let fd_1 = unsafe { BorrowedFd::borrow_raw(1) };