Skip to content

Commit 9f3aafc

Browse files
bors[bot]SteveLauCasomers
authored
Merge #1928 #1938
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]>
3 parents 6c8ff7b + 27d1844 + 9797e0c commit 9f3aafc

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1919
- With I/O-safe type applied in `pty::OpenptyResult` and `pty::ForkptyResult`,
2020
users no longer need to manually close the file descriptors in these types.
2121
([#1921](https://github.com/nix-rust/nix/pull/1921))
22-
- The `fd` argument to `sys::signalfd::signalfd` is now of type `Option<impl AsFd>`.
23-
([#1874](https://github.com/nix-rust/nix/pull/1874))
2422

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

3334
## [0.26.1] - 2022-11-29
3435
### Fixed

src/sys/epoll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl EpollEvent {
8383
/// let epoll = Epoll::new(EpollCreateFlags::empty())?;
8484
///
8585
/// // Create eventfd & Add event
86-
/// let eventfd = unsafe { OwnedFd::from_raw_fd(eventfd(0, EfdFlags::empty())?) };
86+
/// let eventfd = eventfd(0, EfdFlags::empty())?;
8787
/// epoll.add(&eventfd, EpollEvent::new(EpollFlags::EPOLLIN,DATA))?;
8888
///
8989
/// // Arm eventfd & Time wait

src/sys/event.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use libc::{c_int, c_long, intptr_t, time_t, timespec, uintptr_t};
88
use libc::{c_long, intptr_t, size_t, time_t, timespec, uintptr_t};
99
use std::convert::TryInto;
1010
use std::mem;
11-
use std::os::unix::io::RawFd;
11+
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd};
1212
use std::ptr;
1313

1414
// Redefine kevent in terms of programmer-friendly enums and bitfields.
@@ -207,10 +207,10 @@ libc_bitflags!(
207207
}
208208
);
209209

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

213-
Errno::result(res)
213+
Errno::result(res).map(|fd| unsafe { OwnedFd::from_raw_fd(fd) })
214214
}
215215

216216
// KEvent can't derive Send because on some operating systems, udata is defined
@@ -267,8 +267,8 @@ impl KEvent {
267267
}
268268
}
269269

270-
pub fn kevent(
271-
kq: RawFd,
270+
pub fn kevent<Fd: AsFd>(
271+
kq: Fd,
272272
changelist: &[KEvent],
273273
eventlist: &mut [KEvent],
274274
timeout_ms: usize,
@@ -293,15 +293,15 @@ type type_of_nchanges = c_int;
293293
#[cfg(target_os = "netbsd")]
294294
type type_of_nchanges = size_t;
295295

296-
pub fn kevent_ts(
297-
kq: RawFd,
296+
pub fn kevent_ts<Fd: AsFd>(
297+
kq: Fd,
298298
changelist: &[KEvent],
299299
eventlist: &mut [KEvent],
300300
timeout_opt: Option<timespec>,
301301
) -> Result<usize> {
302302
let res = unsafe {
303303
libc::kevent(
304-
kq,
304+
kq.as_fd().as_raw_fd(),
305305
changelist.as_ptr() as *const libc::kevent,
306306
changelist.len() as type_of_nchanges,
307307
eventlist.as_mut_ptr() as *mut libc::kevent,

src/sys/eventfd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::errno::Errno;
22
use crate::Result;
3-
use std::os::unix::io::RawFd;
3+
use std::os::unix::io::{FromRawFd, OwnedFd};
44

55
libc_bitflags! {
66
pub struct EfdFlags: libc::c_int {
@@ -10,8 +10,8 @@ libc_bitflags! {
1010
}
1111
}
1212

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

16-
Errno::result(res).map(|r| r as RawFd)
16+
Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r) })
1717
}

src/sys/memfd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Interfaces for managing memory-backed files.
22
33
use cfg_if::cfg_if;
4-
use std::os::unix::io::RawFd;
4+
use std::os::unix::io::{FromRawFd, OwnedFd, RawFd};
55

66
use crate::errno::Errno;
77
use crate::Result;
@@ -40,7 +40,7 @@ libc_bitflags!(
4040
/// For more information, see [`memfd_create(2)`].
4141
///
4242
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
43-
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
43+
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<OwnedFd> {
4444
let res = unsafe {
4545
cfg_if! {
4646
if #[cfg(all(
@@ -60,5 +60,5 @@ pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
6060
}
6161
};
6262

63-
Errno::result(res).map(|r| r as RawFd)
63+
Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r as RawFd) })
6464
}

src/sys/signalfd.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ pub const SIGNALFD_SIGINFO_SIZE: usize = mem::size_of::<siginfo>();
4444
/// signalfd (the default handler will be invoked instead).
4545
///
4646
/// See [the signalfd man page for more information](https://man7.org/linux/man-pages/man2/signalfd.2.html)
47+
#[deprecated(since = "0.27.0", note = "Use SignalFd instead")]
4748
pub fn signalfd<F: AsFd>(fd: Option<F>, mask: &SigSet, flags: SfdFlags) -> Result<OwnedFd> {
49+
_signalfd(fd, mask, flags)
50+
}
51+
52+
fn _signalfd<F: AsFd>(fd: Option<F>, mask: &SigSet, flags: SfdFlags) -> Result<OwnedFd> {
4853
let raw_fd = fd.map_or(-1, |x|x.as_fd().as_raw_fd());
4954
unsafe {
5055
Errno::result(libc::signalfd(
@@ -90,13 +95,13 @@ impl SignalFd {
9095
}
9196

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

95100
Ok(SignalFd(fd))
96101
}
97102

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

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

0 commit comments

Comments
 (0)