Skip to content

Commit cb6a921

Browse files
committed
Auto merge of #309 - alex-gulyas:update-signalfd, r=fiveop
Update signalfd Update `signalfd` to use types and functions from `libc`. It is a breaking change, because: - renamed pub const `CREATE_NEW_FD` to `SIGNALFD_NEW` - renamed pub const `SIGINFO_SIZE` to `SIGNALFD_SIGINFO_SIZE` - removed pub const `SIGINFO_PADDING` Fixes #307
2 parents 29aaccb + 19ad8ca commit cb6a921

File tree

2 files changed

+24
-65
lines changed

2 files changed

+24
-65
lines changed

src/sys/signalfd.rs

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,25 @@
1515
//!
1616
//! Please note that signal discarding is not specific to `signalfd`, but also happens with regular
1717
//! signal handlers.
18-
use libc::{c_int, pid_t, uid_t};
18+
use libc;
1919
use unistd;
20-
use {Errno, Result};
21-
use sys::signal::signal::siginfo as signal_siginfo;
20+
use {Error, Errno, Result};
2221
pub use sys::signal::{self, SigSet};
22+
pub use libc::signalfd_siginfo as siginfo;
2323

2424
use std::os::unix::io::{RawFd, AsRawFd};
2525
use std::mem;
2626

27-
mod ffi {
28-
use libc::c_int;
29-
use sys::signal::sigset_t;
30-
31-
extern {
32-
pub fn signalfd(fd: c_int, mask: *const sigset_t, flags: c_int) -> c_int;
33-
}
34-
}
3527

3628
bitflags!{
37-
flags SfdFlags: c_int {
38-
const SFD_NONBLOCK = 0o00004000, // O_NONBLOCK
39-
const SFD_CLOEXEC = 0o02000000, // O_CLOEXEC
29+
flags SfdFlags: libc::c_int {
30+
const SFD_NONBLOCK = libc::SFD_NONBLOCK,
31+
const SFD_CLOEXEC = libc::SFD_CLOEXEC,
4032
}
4133
}
4234

43-
pub const CREATE_NEW_FD: RawFd = -1;
35+
pub const SIGNALFD_NEW: RawFd = -1;
36+
pub const SIGNALFD_SIGINFO_SIZE: usize = 128;
4437

4538
/// Creates a new file descriptor for reading signals.
4639
///
@@ -55,7 +48,7 @@ pub const CREATE_NEW_FD: RawFd = -1;
5548
/// See [the signalfd man page for more information](http://man7.org/linux/man-pages/man2/signalfd.2.html)
5649
pub fn signalfd(fd: RawFd, mask: &SigSet, flags: SfdFlags) -> Result<RawFd> {
5750
unsafe {
58-
Errno::result(ffi::signalfd(fd as c_int, mask.as_ref(), flags.bits()))
51+
Errno::result(libc::signalfd(fd as libc::c_int, mask.as_ref(), flags.bits()))
5952
}
6053
}
6154

@@ -98,7 +91,7 @@ impl SignalFd {
9891
}
9992

10093
pub fn with_flags(mask: &SigSet, flags: SfdFlags) -> Result<SignalFd> {
101-
let fd = try!(signalfd(CREATE_NEW_FD, mask, flags));
94+
let fd = try!(signalfd(SIGNALFD_NEW, mask, flags));
10295

10396
Ok(SignalFd(fd))
10497
}
@@ -108,10 +101,10 @@ impl SignalFd {
108101
}
109102

110103
pub fn read_signal(&mut self) -> Result<Option<siginfo>> {
111-
let mut buffer: [u8; SIGINFO_SIZE] = unsafe { mem::uninitialized() };
104+
let mut buffer: [u8; SIGNALFD_SIGINFO_SIZE] = unsafe { mem::uninitialized() };
112105

113106
match unistd::read(self.0, &mut buffer) {
114-
Ok(SIGINFO_SIZE) => Ok(Some(unsafe { mem::transmute_copy(&buffer) })),
107+
Ok(SIGNALFD_SIGINFO_SIZE) => Ok(Some(unsafe { mem::transmute(buffer) })),
115108
Ok(_) => unreachable!("partial read on signalfd"),
116109
Err(Error::Sys(Errno::EAGAIN)) => Ok(None),
117110
Err(error) => Err(error)
@@ -143,51 +136,17 @@ impl Iterator for SignalFd {
143136
}
144137
}
145138

146-
pub const SIGINFO_SIZE: usize = 128;
147-
pub const SIGINFO_PADDING: usize = 48;
148-
149-
#[derive(Debug, Clone, PartialEq)]
150-
#[repr(C, packed)]
151-
pub struct siginfo {
152-
pub ssi_signo: u32,
153-
pub ssi_errno: i32,
154-
pub ssi_code: i32,
155-
pub ssi_pid: u32,
156-
pub ssi_uid: u32,
157-
pub ssi_fd: i32,
158-
pub ssi_tid: u32,
159-
pub ssi_band: u32,
160-
pub ssi_overrun: u32,
161-
pub ssi_trapno: u32,
162-
pub ssi_status: i32,
163-
pub ssi_int: i32,
164-
pub ssi_ptr: u64,
165-
pub ssi_utime: u64,
166-
pub ssi_stime: u64,
167-
pub ssi_addr: u64,
168-
}
169-
170-
impl Into<signal_siginfo> for siginfo {
171-
fn into(self) -> signal_siginfo {
172-
signal_siginfo {
173-
si_signo: self.ssi_signo as c_int,
174-
si_errno: self.ssi_errno as c_int,
175-
si_code: self.ssi_code as c_int,
176-
pid: self.ssi_pid as pid_t,
177-
uid: self.ssi_uid as uid_t,
178-
status: self.ssi_status as c_int,
179-
}
180-
}
181-
}
182139

183140
#[cfg(test)]
184141
mod tests {
185142
use super::*;
186143
use std::mem;
144+
use libc;
145+
187146

188147
#[test]
189148
fn check_siginfo_size() {
190-
assert_eq!(mem::size_of::<siginfo>() + SIGINFO_PADDING, SIGINFO_SIZE);
149+
assert_eq!(mem::size_of::<libc::signalfd_siginfo>(), SIGNALFD_SIGINFO_SIZE);
191150
}
192151

193152
#[test]
@@ -210,6 +169,6 @@ mod tests {
210169
let mut fd = SignalFd::with_flags(&mask, SFD_NONBLOCK).unwrap();
211170

212171
let res = fd.read_signal();
213-
assert_eq!(res, Ok(None));
172+
assert!(res.unwrap().is_none());
214173
}
215174
}

test/test_signalfd.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ extern crate nix;
22

33
#[cfg(feature = "signalfd")]
44

5-
use nix::sys::signalfd::*;
5+
use nix::sys::signalfd::SignalFd;
6+
use nix::sys::signal;
7+
use nix::unistd;
68

79
#[cfg(feature = "signalfd")]
810
fn main() {
9-
let mut mask = SigSet::empty();
11+
print!("test test_signalfd ... ");
12+
13+
let mut mask = signal::SigSet::empty();
1014
mask.add(signal::SIGUSR1).unwrap();
1115
mask.thread_block().unwrap();
1216

@@ -16,13 +20,9 @@ fn main() {
1620
signal::kill(pid, signal::SIGUSR1).unwrap();
1721

1822
let res = fd.read_signal();
19-
assert!(res.is_ok());
20-
21-
let opt = res.ok().unwrap();
22-
assert!(opt.is_some());
2323

24-
let info = opt.unwrap();
25-
assert_eq!(info.ssi_signo as i32, signal::SIGUSR1);
24+
assert_eq!(res.unwrap().unwrap().ssi_signo as i32, signal::SIGUSR1);
25+
println!("ok");
2626
}
2727

2828
#[cfg(not(feature = "signalfd"))]

0 commit comments

Comments
 (0)