13da5c369Sopenharmony_ciuse std::convert::TryFrom;
23da5c369Sopenharmony_ci
33da5c369Sopenharmony_ci#[test]
43da5c369Sopenharmony_cifn test_signalfd() {
53da5c369Sopenharmony_ci    use nix::sys::signal::{self, raise, SigSet, Signal};
63da5c369Sopenharmony_ci    use nix::sys::signalfd::SignalFd;
73da5c369Sopenharmony_ci
83da5c369Sopenharmony_ci    // Grab the mutex for altering signals so we don't interfere with other tests.
93da5c369Sopenharmony_ci    let _m = crate::SIGNAL_MTX.lock();
103da5c369Sopenharmony_ci
113da5c369Sopenharmony_ci    // Block the SIGUSR1 signal from automatic processing for this thread
123da5c369Sopenharmony_ci    let mut mask = SigSet::empty();
133da5c369Sopenharmony_ci    mask.add(signal::SIGUSR1);
143da5c369Sopenharmony_ci    mask.thread_block().unwrap();
153da5c369Sopenharmony_ci
163da5c369Sopenharmony_ci    let mut fd = SignalFd::new(&mask).unwrap();
173da5c369Sopenharmony_ci
183da5c369Sopenharmony_ci    // Send a SIGUSR1 signal to the current process. Note that this uses `raise` instead of `kill`
193da5c369Sopenharmony_ci    // because `kill` with `getpid` isn't correct during multi-threaded execution like during a
203da5c369Sopenharmony_ci    // cargo test session. Instead use `raise` which does the correct thing by default.
213da5c369Sopenharmony_ci    raise(signal::SIGUSR1).expect("Error: raise(SIGUSR1) failed");
223da5c369Sopenharmony_ci
233da5c369Sopenharmony_ci    // And now catch that same signal.
243da5c369Sopenharmony_ci    let res = fd.read_signal().unwrap().unwrap();
253da5c369Sopenharmony_ci    let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
263da5c369Sopenharmony_ci    assert_eq!(signo, signal::SIGUSR1);
273da5c369Sopenharmony_ci}
28