Skip to content

Commit 8cda34d

Browse files
committed
Auto merge of #460 - chaosagent:sys_signal, r=fiveop
Change SigFlags into an enum. Addresses #459. This is a breaking change. Should SigFlags be renamed to something more sensible?
2 parents 169c1e4 + e19f142 commit 8cda34d

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4646
accessible with the new method `events()` of `EpollEvent`. Instances of
4747
`EpollEvent` can be constructed using the new method `new()` of EpollEvent.
4848
([#410](https://github.com/nix-rust/nix/pull/410))
49+
- `SigFlags` in `::nix::sys::signal` has be renamed to `SigmaskHow` and its type
50+
has changed from `bitflags` to `enum` in order to conform to our conventions.
51+
([#410](https://github.com/nix-rust/nix/pull/460))
4952

5053
### Fixed
5154
- Fixed using kqueue with `EVFILT_USER` on FreeBSD

src/sys/signal.rs

+52-16
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ bitflags!{
206206
}
207207
}
208208

209-
bitflags!{
210-
flags SigFlags: libc::c_int {
211-
const SIG_BLOCK = libc::SIG_BLOCK,
212-
const SIG_UNBLOCK = libc::SIG_UNBLOCK,
213-
const SIG_SETMASK = libc::SIG_SETMASK,
214-
}
209+
#[repr(i32)]
210+
#[derive(Clone, Copy, PartialEq)]
211+
pub enum SigmaskHow {
212+
SIG_BLOCK = libc::SIG_BLOCK,
213+
SIG_UNBLOCK = libc::SIG_UNBLOCK,
214+
SIG_SETMASK = libc::SIG_SETMASK,
215215
}
216216

217217
#[derive(Clone, Copy)]
@@ -268,27 +268,27 @@ impl SigSet {
268268
/// Gets the currently blocked (masked) set of signals for the calling thread.
269269
pub fn thread_get_mask() -> Result<SigSet> {
270270
let mut oldmask: SigSet = unsafe { mem::uninitialized() };
271-
try!(pthread_sigmask(SigFlags::empty(), None, Some(&mut oldmask)));
271+
try!(pthread_sigmask(SigmaskHow::SIG_SETMASK, None, Some(&mut oldmask)));
272272
Ok(oldmask)
273273
}
274274

275275
/// Sets the set of signals as the signal mask for the calling thread.
276276
pub fn thread_set_mask(&self) -> Result<()> {
277-
pthread_sigmask(SIG_SETMASK, Some(self), None)
277+
pthread_sigmask(SigmaskHow::SIG_SETMASK, Some(self), None)
278278
}
279279

280280
/// Adds the set of signals to the signal mask for the calling thread.
281281
pub fn thread_block(&self) -> Result<()> {
282-
pthread_sigmask(SIG_BLOCK, Some(self), None)
282+
pthread_sigmask(SigmaskHow::SIG_BLOCK, Some(self), None)
283283
}
284284

285285
/// Removes the set of signals from the signal mask for the calling thread.
286286
pub fn thread_unblock(&self) -> Result<()> {
287-
pthread_sigmask(SIG_UNBLOCK, Some(self), None)
287+
pthread_sigmask(SigmaskHow::SIG_UNBLOCK, Some(self), None)
288288
}
289289

290290
/// Sets the set of signals as the signal mask, and returns the old mask.
291-
pub fn thread_swap_mask(&self, how: SigFlags) -> Result<SigSet> {
291+
pub fn thread_swap_mask(&self, how: SigmaskHow) -> Result<SigSet> {
292292
let mut oldmask: SigSet = unsafe { mem::uninitialized() };
293293
try!(pthread_sigmask(how, Some(self), Some(&mut oldmask)));
294294
Ok(oldmask)
@@ -368,7 +368,7 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result<SigActi
368368
///
369369
/// For more information, visit the [pthread_sigmask](http://man7.org/linux/man-pages/man3/pthread_sigmask.3.html),
370370
/// or [sigprocmask](http://man7.org/linux/man-pages/man2/sigprocmask.2.html) man pages.
371-
pub fn pthread_sigmask(how: SigFlags,
371+
pub fn pthread_sigmask(how: SigmaskHow,
372372
set: Option<&SigSet>,
373373
oldset: Option<&mut SigSet>) -> Result<()> {
374374
if set.is_none() && oldset.is_none() {
@@ -377,7 +377,7 @@ pub fn pthread_sigmask(how: SigFlags,
377377

378378
let res = unsafe {
379379
// if set or oldset is None, pass in null pointers instead
380-
libc::pthread_sigmask(how.bits(),
380+
libc::pthread_sigmask(how as libc::c_int,
381381
set.map_or_else(|| ptr::null::<libc::sigset_t>(),
382382
|s| &s.sigset as *const libc::sigset_t),
383383
oldset.map_or_else(|| ptr::null_mut::<libc::sigset_t>(),
@@ -442,12 +442,46 @@ mod tests {
442442
assert!(two_signals.contains(SIGUSR2));
443443
}
444444

445+
// This test doesn't actually test get_mask functionality, see the set_mask test for that.
446+
#[test]
447+
fn test_thread_signal_get_mask() {
448+
assert!(SigSet::thread_get_mask().is_ok());
449+
}
450+
451+
#[test]
452+
fn test_thread_signal_set_mask() {
453+
let prev_mask = SigSet::thread_get_mask().expect("Failed to get existing signal mask!");
454+
455+
let mut test_mask = prev_mask;
456+
test_mask.add(SIGUSR1);
457+
458+
assert!(test_mask.thread_set_mask().is_ok());
459+
let new_mask = SigSet::thread_get_mask().expect("Failed to get new mask!");
460+
461+
assert!(new_mask.contains(SIGUSR1));
462+
assert!(!new_mask.contains(SIGUSR2));
463+
464+
prev_mask.thread_set_mask().expect("Failed to revert signal mask!");
465+
}
466+
445467
#[test]
446468
fn test_thread_signal_block() {
447469
let mut mask = SigSet::empty();
448470
mask.add(SIGUSR1);
449471

450472
assert!(mask.thread_block().is_ok());
473+
474+
assert!(SigSet::thread_get_mask().unwrap().contains(SIGUSR1));
475+
}
476+
477+
#[test]
478+
fn test_thread_signal_unblock() {
479+
let mut mask = SigSet::empty();
480+
mask.add(SIGUSR1);
481+
482+
assert!(mask.thread_unblock().is_ok());
483+
484+
assert!(!SigSet::thread_get_mask().unwrap().contains(SIGUSR1));
451485
}
452486

453487
#[test]
@@ -458,13 +492,15 @@ mod tests {
458492

459493
assert!(SigSet::thread_get_mask().unwrap().contains(SIGUSR1));
460494

461-
let mask2 = SigSet::empty();
462-
mask.add(SIGUSR2);
495+
let mut mask2 = SigSet::empty();
496+
mask2.add(SIGUSR2);
463497

464-
let oldmask = mask2.thread_swap_mask(SIG_SETMASK).unwrap();
498+
let oldmask = mask2.thread_swap_mask(SigmaskHow::SIG_SETMASK).unwrap();
465499

466500
assert!(oldmask.contains(SIGUSR1));
467501
assert!(!oldmask.contains(SIGUSR2));
502+
503+
assert!(SigSet::thread_get_mask().unwrap().contains(SIGUSR2));
468504
}
469505

470506
// TODO(#251): Re-enable after figuring out flakiness.

0 commit comments

Comments
 (0)