Skip to content

Commit 7fa6f90

Browse files
signalfd optional file descriptor
1 parent ae34683 commit 7fa6f90

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66
## [Unreleased] - ReleaseDate
77
### Added
88

9+
- The `fd` argument to `sys::signalfd::signalfd` is now of type `Option<BorrowedFd>`.
10+
([#1874](https://github.com/nix-rust/nix/pull/1874))
911
- Add `MntFlags` and `unmount` on all of the BSDs.
1012
([#1849](https://github.com/nix-rust/nix/pull/1849))
1113
- Added a 'Statfs::flags' method.

src/sys/signalfd.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use crate::Result;
2222
pub use libc::signalfd_siginfo as siginfo;
2323

2424
use std::mem;
25-
use std::os::unix::io::{AsRawFd, RawFd};
25+
use std::os::unix::io::{AsRawFd, RawFd, BorrowedFd, FromRawFd, OwnedFd, AsFd};
26+
use std::mem::ManuallyDrop;
2627

2728
libc_bitflags! {
2829
pub struct SfdFlags: libc::c_int {
@@ -31,7 +32,6 @@ libc_bitflags! {
3132
}
3233
}
3334

34-
pub const SIGNALFD_NEW: RawFd = -1;
3535
#[deprecated(since = "0.23.0", note = "use mem::size_of::<siginfo>() instead")]
3636
pub const SIGNALFD_SIGINFO_SIZE: usize = mem::size_of::<siginfo>();
3737

@@ -46,13 +46,14 @@ pub const SIGNALFD_SIGINFO_SIZE: usize = mem::size_of::<siginfo>();
4646
/// signalfd (the default handler will be invoked instead).
4747
///
4848
/// See [the signalfd man page for more information](https://man7.org/linux/man-pages/man2/signalfd.2.html)
49-
pub fn signalfd(fd: RawFd, mask: &SigSet, flags: SfdFlags) -> Result<RawFd> {
49+
pub fn signalfd(fd: Option<BorrowedFd>, mask: &SigSet, flags: SfdFlags) -> Result<OwnedFd> {
50+
let raw_fd = fd.as_ref().map_or(-1,AsRawFd::as_raw_fd);
5051
unsafe {
5152
Errno::result(libc::signalfd(
52-
fd as libc::c_int,
53+
raw_fd,
5354
mask.as_ref(),
5455
flags.bits(),
55-
))
56+
)).map(|raw_fd|FromRawFd::from_raw_fd(raw_fd))
5657
}
5758
}
5859

@@ -82,30 +83,30 @@ pub fn signalfd(fd: RawFd, mask: &SigSet, flags: SfdFlags) -> Result<RawFd> {
8283
/// Err(err) => (), // some error happend
8384
/// }
8485
/// ```
85-
#[derive(Debug, Eq, Hash, PartialEq)]
86-
pub struct SignalFd(RawFd);
86+
#[derive(Debug)]
87+
pub struct SignalFd(ManuallyDrop<OwnedFd>);
8788

8889
impl SignalFd {
8990
pub fn new(mask: &SigSet) -> Result<SignalFd> {
9091
Self::with_flags(mask, SfdFlags::empty())
9192
}
9293

9394
pub fn with_flags(mask: &SigSet, flags: SfdFlags) -> Result<SignalFd> {
94-
let fd = signalfd(SIGNALFD_NEW, mask, flags)?;
95+
let fd = signalfd(None, mask, flags)?;
9596

96-
Ok(SignalFd(fd))
97+
Ok(SignalFd(ManuallyDrop::new(fd)))
9798
}
9899

99100
pub fn set_mask(&mut self, mask: &SigSet) -> Result<()> {
100-
signalfd(self.0, mask, SfdFlags::empty()).map(drop)
101+
signalfd(Some(self.0.as_fd()), mask, SfdFlags::empty()).map(drop)
101102
}
102103

103104
pub fn read_signal(&mut self) -> Result<Option<siginfo>> {
104105
let mut buffer = mem::MaybeUninit::<siginfo>::uninit();
105106

106107
let size = mem::size_of_val(&buffer);
107108
let res = Errno::result(unsafe {
108-
libc::read(self.0, buffer.as_mut_ptr() as *mut libc::c_void, size)
109+
libc::read(self.0.as_raw_fd(), buffer.as_mut_ptr() as *mut libc::c_void, size)
109110
})
110111
.map(|r| r as usize);
111112
match res {
@@ -119,7 +120,7 @@ impl SignalFd {
119120

120121
impl Drop for SignalFd {
121122
fn drop(&mut self) {
122-
let e = unistd::close(self.0);
123+
let e = unistd::close(self.0.as_raw_fd());
123124
if !std::thread::panicking() && e == Err(Errno::EBADF) {
124125
panic!("Closing an invalid file descriptor!");
125126
};
@@ -128,7 +129,7 @@ impl Drop for SignalFd {
128129

129130
impl AsRawFd for SignalFd {
130131
fn as_raw_fd(&self) -> RawFd {
131-
self.0
132+
self.0.as_raw_fd()
132133
}
133134
}
134135

src/unistd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::ffi::{CString, OsStr};
3434
#[cfg(not(target_os = "redox"))]
3535
use std::os::unix::ffi::OsStrExt;
3636
use std::os::unix::ffi::OsStringExt;
37-
use std::os::unix::io::RawFd;
37+
use std::os::unix::io::{IntoRawFd, RawFd};
3838
use std::path::PathBuf;
3939
use std::{fmt, mem, ptr};
4040

@@ -1060,8 +1060,8 @@ pub fn gethostname() -> Result<OsString> {
10601060
/// let f = tempfile::tempfile().unwrap();
10611061
/// close(f.into_raw_fd()).unwrap(); // Good. into_raw_fd consumes f
10621062
/// ```
1063-
pub fn close(fd: RawFd) -> Result<()> {
1064-
let res = unsafe { libc::close(fd) };
1063+
pub fn close<F: IntoRawFd>(fd: F) -> Result<()> {
1064+
let res = unsafe { libc::close(fd.into_raw_fd()) };
10651065
Errno::result(res).map(drop)
10661066
}
10671067

0 commit comments

Comments
 (0)