1 use libc;
2 #[cfg(not(target_os = "redox"))]
3 use nix::Error;
4 use nix::sys::signal::*;
5 use nix::unistd::*;
6 use std::convert::TryFrom;
7 use std::sync::atomic::{AtomicBool, Ordering};
8 
9 #[test]
test_kill_none()10 fn test_kill_none() {
11     kill(getpid(), None).expect("Should be able to send signal to myself.");
12 }
13 
14 #[test]
15 #[cfg(not(target_os = "fuchsia"))]
test_killpg_none()16 fn test_killpg_none() {
17     killpg(getpgrp(), None)
18         .expect("Should be able to send signal to my process group.");
19 }
20 
21 #[test]
test_old_sigaction_flags()22 fn test_old_sigaction_flags() {
23     let _m = crate::SIGNAL_MTX.lock().expect("Mutex got poisoned by another test");
24 
25     extern "C" fn handler(_: ::libc::c_int) {}
26     let act = SigAction::new(
27         SigHandler::Handler(handler),
28         SaFlags::empty(),
29         SigSet::empty(),
30     );
31     let oact = unsafe { sigaction(SIGINT, &act) }.unwrap();
32     let _flags = oact.flags();
33     let oact = unsafe { sigaction(SIGINT, &act) }.unwrap();
34     let _flags = oact.flags();
35 }
36 
37 #[test]
test_sigprocmask_noop()38 fn test_sigprocmask_noop() {
39     sigprocmask(SigmaskHow::SIG_BLOCK, None, None)
40         .expect("this should be an effective noop");
41 }
42 
43 #[test]
test_sigprocmask()44 fn test_sigprocmask() {
45     let _m = crate::SIGNAL_MTX.lock().expect("Mutex got poisoned by another test");
46 
47     // This needs to be a signal that rust doesn't use in the test harness.
48     const SIGNAL: Signal = Signal::SIGCHLD;
49 
50     let mut old_signal_set = SigSet::empty();
51     sigprocmask(SigmaskHow::SIG_BLOCK, None, Some(&mut old_signal_set))
52         .expect("expect to be able to retrieve old signals");
53 
54     // Make sure the old set doesn't contain the signal, otherwise the following
55     // test don't make sense.
56     assert_eq!(old_signal_set.contains(SIGNAL), false,
57                "the {:?} signal is already blocked, please change to a \
58                 different one", SIGNAL);
59 
60     // Now block the signal.
61     let mut signal_set = SigSet::empty();
62     signal_set.add(SIGNAL);
63     sigprocmask(SigmaskHow::SIG_BLOCK, Some(&signal_set), None)
64         .expect("expect to be able to block signals");
65 
66     // And test it again, to make sure the change was effective.
67     old_signal_set.clear();
68     sigprocmask(SigmaskHow::SIG_BLOCK, None, Some(&mut old_signal_set))
69         .expect("expect to be able to retrieve old signals");
70     assert_eq!(old_signal_set.contains(SIGNAL), true,
71                "expected the {:?} to be blocked", SIGNAL);
72 
73     // Reset the signal.
74     sigprocmask(SigmaskHow::SIG_UNBLOCK, Some(&signal_set), None)
75         .expect("expect to be able to block signals");
76 }
77 
78 lazy_static! {
79     static ref SIGNALED: AtomicBool = AtomicBool::new(false);
80 }
81 
test_sigaction_handler(signal: libc::c_int)82 extern fn test_sigaction_handler(signal: libc::c_int) {
83     let signal = Signal::try_from(signal).unwrap();
84     SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
85 }
86 
87 #[cfg(not(target_os = "redox"))]
test_sigaction_action(_: libc::c_int, _: *mut libc::siginfo_t, _: *mut libc::c_void)88 extern fn test_sigaction_action(_: libc::c_int, _: *mut libc::siginfo_t, _: *mut libc::c_void) {}
89 
90 #[test]
91 #[cfg(not(target_os = "redox"))]
test_signal_sigaction()92 fn test_signal_sigaction() {
93     let _m = crate::SIGNAL_MTX.lock().expect("Mutex got poisoned by another test");
94 
95     let action_handler = SigHandler::SigAction(test_sigaction_action);
96     assert_eq!(unsafe { signal(Signal::SIGINT, action_handler) }.unwrap_err(), Error::UnsupportedOperation);
97 }
98 
99 #[test]
test_signal()100 fn test_signal() {
101     let _m = crate::SIGNAL_MTX.lock().expect("Mutex got poisoned by another test");
102 
103     unsafe { signal(Signal::SIGINT, SigHandler::SigIgn) }.unwrap();
104     raise(Signal::SIGINT).unwrap();
105     assert_eq!(unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap(), SigHandler::SigIgn);
106 
107     let handler = SigHandler::Handler(test_sigaction_handler);
108     assert_eq!(unsafe { signal(Signal::SIGINT, handler) }.unwrap(), SigHandler::SigDfl);
109     raise(Signal::SIGINT).unwrap();
110     assert!(SIGNALED.load(Ordering::Relaxed));
111     assert_eq!(unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap(), handler);
112 
113     // Restore default signal handler
114     unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap();
115 }
116