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