1use std::convert::TryFrom;
2
3#[test]
4fn test_signalfd() {
5    use nix::sys::signal::{self, raise, SigSet, Signal};
6    use nix::sys::signalfd::SignalFd;
7
8    // Grab the mutex for altering signals so we don't interfere with other tests.
9    let _m = crate::SIGNAL_MTX.lock();
10
11    // Block the SIGUSR1 signal from automatic processing for this thread
12    let mut mask = SigSet::empty();
13    mask.add(signal::SIGUSR1);
14    mask.thread_block().unwrap();
15
16    let mut fd = SignalFd::new(&mask).unwrap();
17
18    // Send a SIGUSR1 signal to the current process. Note that this uses `raise` instead of `kill`
19    // because `kill` with `getpid` isn't correct during multi-threaded execution like during a
20    // cargo test session. Instead use `raise` which does the correct thing by default.
21    raise(signal::SIGUSR1).expect("Error: raise(SIGUSR1) failed");
22
23    // And now catch that same signal.
24    let res = fd.read_signal().unwrap().unwrap();
25    let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
26    assert_eq!(signo, signal::SIGUSR1);
27}
28