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