Skip to content

Commit

Permalink
Merge #1928 #1938
Browse files Browse the repository at this point in the history
1928: feat: I/O safety for 'sys/memfd' & 'sys/event' & 'sys/eventfd' r=asomers a=SteveLauC

#### What this PR does:

Adds I/O safety for moduels:
1. `sys/memfd`
2. `sys/event`
3. `sys/eventfd`

-----

BYW, I called `rustfmt` on these 4 files, which introduces some noise, sorry about this.

1938: Deprecate the signalfd function. r=asomers a=asomers

The SignalFd type is just as capable and easier to use.

CC `@JonathanWoollett-Light` 

Co-authored-by: Steve Lau <[email protected]>
Co-authored-by: Alan Somers <[email protected]>
  • Loading branch information
3 people authored Dec 11, 2022
3 parents 6c8ff7b + 27d1844 + 9797e0c commit 9f3aafc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- With I/O-safe type applied in `pty::OpenptyResult` and `pty::ForkptyResult`,
users no longer need to manually close the file descriptors in these types.
([#1921](https://github.com/nix-rust/nix/pull/1921))
- The `fd` argument to `sys::signalfd::signalfd` is now of type `Option<impl AsFd>`.
([#1874](https://github.com/nix-rust/nix/pull/1874))

### Fixed
### Removed
Expand All @@ -29,6 +27,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#1855](https://github.com/nix-rust/nix/pull/1855))
- Removed deprecated net APIs.
([#1861](https://github.com/nix-rust/nix/pull/1861))
- `nix::sys::signalfd::signalfd` is deprecated. Use
`nix::sys::signalfd::SignalFd` instead.
([#1938](https://github.com/nix-rust/nix/pull/1938))

## [0.26.1] - 2022-11-29
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/sys/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl EpollEvent {
/// let epoll = Epoll::new(EpollCreateFlags::empty())?;
///
/// // Create eventfd & Add event
/// let eventfd = unsafe { OwnedFd::from_raw_fd(eventfd(0, EfdFlags::empty())?) };
/// let eventfd = eventfd(0, EfdFlags::empty())?;
/// epoll.add(&eventfd, EpollEvent::new(EpollFlags::EPOLLIN,DATA))?;
///
/// // Arm eventfd & Time wait
Expand Down
16 changes: 8 additions & 8 deletions src/sys/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use libc::{c_int, c_long, intptr_t, time_t, timespec, uintptr_t};
use libc::{c_long, intptr_t, size_t, time_t, timespec, uintptr_t};
use std::convert::TryInto;
use std::mem;
use std::os::unix::io::RawFd;
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd};
use std::ptr;

// Redefine kevent in terms of programmer-friendly enums and bitfields.
Expand Down Expand Up @@ -207,10 +207,10 @@ libc_bitflags!(
}
);

pub fn kqueue() -> Result<RawFd> {
pub fn kqueue() -> Result<OwnedFd> {
let res = unsafe { libc::kqueue() };

Errno::result(res)
Errno::result(res).map(|fd| unsafe { OwnedFd::from_raw_fd(fd) })
}

// KEvent can't derive Send because on some operating systems, udata is defined
Expand Down Expand Up @@ -267,8 +267,8 @@ impl KEvent {
}
}

pub fn kevent(
kq: RawFd,
pub fn kevent<Fd: AsFd>(
kq: Fd,
changelist: &[KEvent],
eventlist: &mut [KEvent],
timeout_ms: usize,
Expand All @@ -293,15 +293,15 @@ type type_of_nchanges = c_int;
#[cfg(target_os = "netbsd")]
type type_of_nchanges = size_t;

pub fn kevent_ts(
kq: RawFd,
pub fn kevent_ts<Fd: AsFd>(
kq: Fd,
changelist: &[KEvent],
eventlist: &mut [KEvent],
timeout_opt: Option<timespec>,
) -> Result<usize> {
let res = unsafe {
libc::kevent(
kq,
kq.as_fd().as_raw_fd(),
changelist.as_ptr() as *const libc::kevent,
changelist.len() as type_of_nchanges,
eventlist.as_mut_ptr() as *mut libc::kevent,
Expand Down
6 changes: 3 additions & 3 deletions src/sys/eventfd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::errno::Errno;
use crate::Result;
use std::os::unix::io::RawFd;
use std::os::unix::io::{FromRawFd, OwnedFd};

libc_bitflags! {
pub struct EfdFlags: libc::c_int {
Expand All @@ -10,8 +10,8 @@ libc_bitflags! {
}
}

pub fn eventfd(initval: libc::c_uint, flags: EfdFlags) -> Result<RawFd> {
pub fn eventfd(initval: libc::c_uint, flags: EfdFlags) -> Result<OwnedFd> {
let res = unsafe { libc::eventfd(initval, flags.bits()) };

Errno::result(res).map(|r| r as RawFd)
Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r) })
}
6 changes: 3 additions & 3 deletions src/sys/memfd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Interfaces for managing memory-backed files.
use cfg_if::cfg_if;
use std::os::unix::io::RawFd;
use std::os::unix::io::{FromRawFd, OwnedFd, RawFd};

use crate::errno::Errno;
use crate::Result;
Expand Down Expand Up @@ -40,7 +40,7 @@ libc_bitflags!(
/// For more information, see [`memfd_create(2)`].
///
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<OwnedFd> {
let res = unsafe {
cfg_if! {
if #[cfg(all(
Expand All @@ -60,5 +60,5 @@ pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
}
};

Errno::result(res).map(|r| r as RawFd)
Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r as RawFd) })
}
9 changes: 7 additions & 2 deletions src/sys/signalfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ pub const SIGNALFD_SIGINFO_SIZE: usize = mem::size_of::<siginfo>();
/// signalfd (the default handler will be invoked instead).
///
/// See [the signalfd man page for more information](https://man7.org/linux/man-pages/man2/signalfd.2.html)
#[deprecated(since = "0.27.0", note = "Use SignalFd instead")]
pub fn signalfd<F: AsFd>(fd: Option<F>, mask: &SigSet, flags: SfdFlags) -> Result<OwnedFd> {
_signalfd(fd, mask, flags)
}

fn _signalfd<F: AsFd>(fd: Option<F>, mask: &SigSet, flags: SfdFlags) -> Result<OwnedFd> {
let raw_fd = fd.map_or(-1, |x|x.as_fd().as_raw_fd());
unsafe {
Errno::result(libc::signalfd(
Expand Down Expand Up @@ -90,13 +95,13 @@ impl SignalFd {
}

pub fn with_flags(mask: &SigSet, flags: SfdFlags) -> Result<SignalFd> {
let fd = signalfd(None::<OwnedFd>, mask, flags)?;
let fd = _signalfd(None::<OwnedFd>, mask, flags)?;

Ok(SignalFd(fd))
}

pub fn set_mask(&mut self, mask: &SigSet) -> Result<()> {
signalfd(Some(self.0.as_fd()), mask, SfdFlags::empty()).map(drop)
_signalfd(Some(self.0.as_fd()), mask, SfdFlags::empty()).map(drop)
}

pub fn read_signal(&mut self) -> Result<Option<siginfo>> {
Expand Down

0 comments on commit 9f3aafc

Please sign in to comment.