summaryrefslogtreecommitdiffstats
path: root/third_party/rust/nix/test/sys/test_signalfd.rs
blob: 4e0971aba7843c6f3df4253f2695a49f286b53ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::convert::TryFrom;

#[test]
fn create_signalfd() {
    use nix::sys::{signal::SigSet, signalfd::SignalFd};

    let mask = SigSet::empty();
    SignalFd::new(&mask).unwrap();
}

#[test]
fn create_signalfd_with_opts() {
    use nix::sys::{
        signal::SigSet,
        signalfd::{SfdFlags, SignalFd},
    };

    let mask = SigSet::empty();
    SignalFd::with_flags(&mask, SfdFlags::SFD_CLOEXEC | SfdFlags::SFD_NONBLOCK)
        .unwrap();
}

#[test]
fn read_empty_signalfd() {
    use nix::sys::{
        signal::SigSet,
        signalfd::{SfdFlags, SignalFd},
    };

    let mask = SigSet::empty();
    let mut fd = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK).unwrap();

    let res = fd.read_signal();
    assert!(res.unwrap().is_none());
}

#[test]
fn test_signalfd() {
    use nix::sys::signal::{self, raise, SigSet, Signal};
    use nix::sys::signalfd::SignalFd;

    // Grab the mutex for altering signals so we don't interfere with other tests.
    let _m = crate::SIGNAL_MTX.lock();

    // Block the SIGUSR1 signal from automatic processing for this thread
    let mut mask = SigSet::empty();
    mask.add(signal::SIGUSR1);
    mask.thread_block().unwrap();

    let mut fd = SignalFd::new(&mask).unwrap();

    // Send a SIGUSR1 signal to the current process. Note that this uses `raise` instead of `kill`
    // because `kill` with `getpid` isn't correct during multi-threaded execution like during a
    // cargo test session. Instead use `raise` which does the correct thing by default.
    raise(signal::SIGUSR1).expect("Error: raise(SIGUSR1) failed");

    // And now catch that same signal.
    let res = fd.read_signal().unwrap().unwrap();
    let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
    assert_eq!(signo, signal::SIGUSR1);
}

/// Update the signal mask of an already existing signalfd.
#[test]
fn test_signalfd_setmask() {
    use nix::sys::signal::{self, raise, SigSet, Signal};
    use nix::sys::signalfd::SignalFd;

    // Grab the mutex for altering signals so we don't interfere with other tests.
    let _m = crate::SIGNAL_MTX.lock();

    // Block the SIGUSR1 signal from automatic processing for this thread
    let mut mask = SigSet::empty();

    let mut fd = SignalFd::new(&mask).unwrap();

    mask.add(signal::SIGUSR1);
    mask.thread_block().unwrap();
    fd.set_mask(&mask).unwrap();

    // Send a SIGUSR1 signal to the current process. Note that this uses `raise` instead of `kill`
    // because `kill` with `getpid` isn't correct during multi-threaded execution like during a
    // cargo test session. Instead use `raise` which does the correct thing by default.
    raise(signal::SIGUSR1).expect("Error: raise(SIGUSR1) failed");

    // And now catch that same signal.
    let res = fd.read_signal().unwrap().unwrap();
    let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
    assert_eq!(signo, signal::SIGUSR1);
}