Skip to content

Commit

Permalink
Take BorrowedFd as the argument for PollFd::new
Browse files Browse the repository at this point in the history
&AsFd didn't work because there are 'static types, like std::fs::File,
which implement AsFd.  The created BorrowedFd type within the
PollFd::new method would have a very brief lifetime, but the PhantomData
would capture the lifetime of the std::fs::File.  Taking BorrowFd<'fd>
argument makes the lifetime explicit.
  • Loading branch information
asomers committed Sep 30, 2023
1 parent 8b82c46 commit 818550b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
7 changes: 2 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

([#2137](https://github.com/nix-rust/nix/pull/2137))

## [Unreleased] - ReleaseDate

### Fixed

- Relaxed lifetime requirements for `PollFd::new`.
- `PollFd::new` now takes a `BorrowedFd` argument, with relaxed lifetime
requirements relative to the previous version.
([#2134](https://github.com/nix-rust/nix/pull/2134))

## [0.27.1] - 2023-08-28
Expand Down
19 changes: 8 additions & 11 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,34 @@ impl<'fd> PollFd<'fd> {
///
/// # Examples
/// ```no_run
/// # use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
/// # use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd};
/// # use nix::{
/// # poll::{PollFd, PollFlags, poll},
/// # unistd::{pipe, read}
/// # };
/// let (r, w) = pipe().unwrap();
/// let r = unsafe { OwnedFd::from_raw_fd(r) };
/// let pfd = PollFd::new(&r.as_fd(), PollFlags::POLLIN);
/// let pfd = PollFd::new(r.as_fd(), PollFlags::POLLIN);
/// let mut fds = [pfd];
/// poll(&mut fds, -1).unwrap();
/// let mut buf = [0u8; 80];
/// read(r.as_raw_fd(), &mut buf[..]);
/// ```
// Unlike I/O functions, constructors like this must take `AsFd` by
// reference. Otherwise, an `OwnedFd` argument would be dropped at the end
// of the method, leaving the structure referencing a closed file
// descriptor.
// Different from other I/O-safe interfaces, here, we have to take `AsFd`
// by reference to prevent the case where the `fd` is closed but it is
// still in use. For example:
// Unlike I/O functions, constructors like this must take `BorrowedFd`
// instead of AsFd or &AsFd. Otherwise, an `OwnedFd` argument would be
// dropped at the end of the method, leaving the structure referencing a
// closed file descriptor. For example:
//
// ```rust
// let (r, _) = pipe().unwrap();
// let reader: OwnedFd = unsafe { OwnedFd::from_raw_fd(r) };
// let pollfd = PollFd::new(reader, flag); // Drops the OwnedFd
// // Do something with `pollfd`, which uses the CLOSED fd.
// ```
pub fn new<Fd: AsFd + 'fd>(fd: &Fd, events: PollFlags) -> PollFd<'fd> {
pub fn new(fd: BorrowedFd<'fd>, events: PollFlags) -> PollFd<'fd> {
PollFd {
pollfd: libc::pollfd {
fd: fd.as_fd().as_raw_fd(),
fd: fd.as_raw_fd(),
events: events.bits(),
revents: PollFlags::empty().bits(),
},
Expand Down
8 changes: 4 additions & 4 deletions test/test_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nix::{
poll::{poll, PollFd, PollFlags},
unistd::{pipe, write},
};
use std::os::unix::io::BorrowedFd;
use std::os::unix::io::{AsFd, BorrowedFd};

macro_rules! loop_while_eintr {
($poll_expr: expr) => {
Expand All @@ -20,7 +20,7 @@ macro_rules! loop_while_eintr {
#[test]
fn test_poll() {
let (r, w) = pipe().unwrap();
let mut fds = [PollFd::new(&r, PollFlags::POLLIN)];
let mut fds = [PollFd::new(r.as_fd(), PollFlags::POLLIN)];

// Poll an idle pipe. Should timeout
let nfds = loop_while_eintr!(poll(&mut fds, 100));
Expand Down Expand Up @@ -52,7 +52,7 @@ fn test_ppoll() {

let timeout = TimeSpec::milliseconds(1);
let (r, w) = pipe().unwrap();
let mut fds = [PollFd::new(&r, PollFlags::POLLIN)];
let mut fds = [PollFd::new(r.as_fd(), PollFlags::POLLIN)];

// Poll an idle pipe. Should timeout
let sigset = SigSet::empty();
Expand All @@ -71,7 +71,7 @@ fn test_ppoll() {
#[test]
fn test_pollfd_events() {
let fd_zero = unsafe { BorrowedFd::borrow_raw(0) };
let mut pfd = PollFd::new(&fd_zero, PollFlags::POLLIN);
let mut pfd = PollFd::new(fd_zero.as_fd(), PollFlags::POLLIN);
assert_eq!(pfd.events(), PollFlags::POLLIN);
pfd.set_events(PollFlags::POLLOUT);
assert_eq!(pfd.events(), PollFlags::POLLOUT);
Expand Down

0 comments on commit 818550b

Please sign in to comment.