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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::signal::Signal;
use _core::convert::TryFrom;
use bitflags::*;
pub const SIG_ERR: usize = usize::max_value() - 1;
pub const SIG_DFL: usize = 0;
pub const SIG_IGN: usize = 1;
#[derive(Default, Clone, Copy, Debug)]
#[repr(C)]
pub struct Sigset(u64);
impl Sigset {
pub fn new(val: u64) -> Self {
Sigset(val)
}
pub fn empty() -> Self {
Sigset(0)
}
pub fn val(&self) -> u64 {
self.0
}
pub fn contains(&self, sig: Signal) -> bool {
(self.0 & sig.as_bit()) != 0
}
pub fn insert(&mut self, sig: Signal) {
self.0 |= sig.as_bit()
}
pub fn insert_set(&mut self, sigset: &Sigset) {
self.0 |= sigset.0;
}
pub fn remove(&mut self, sig: Signal) {
self.0 ^= self.0 & sig.as_bit();
}
pub fn remove_set(&mut self, sigset: &Sigset) {
self.0 ^= self.0 & sigset.0;
}
pub fn mask_with(&self, mask: &Sigset) -> Sigset {
Sigset(self.0 & (!mask.0))
}
pub fn find_first_signal(&self) -> Option<Signal> {
let tz = self.0.trailing_zeros() as u8;
if tz >= 64 {
None
} else {
Some(Signal::try_from(tz + 1).unwrap())
}
}
pub fn is_empty(&self) -> bool {
self.0 == 0
}
pub fn is_not_empty(&self) -> bool {
self.0 != 0
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct SignalAction {
pub handler: usize,
pub flags: SignalActionFlags,
pub restorer: usize,
pub mask: Sigset,
}
#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct SiginfoFields {
pad: [u8; Self::PAD_SIZE],
}
impl SiginfoFields {
const PAD_SIZE: usize = 128 - 2 * core::mem::size_of::<i32>() - core::mem::size_of::<usize>();
}
impl Default for SiginfoFields {
fn default() -> Self {
SiginfoFields {
pad: [0; Self::PAD_SIZE],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct SigInfo {
pub signo: i32,
pub errno: i32,
pub code: SignalCode,
pub field: SiginfoFields,
}
impl Default for SigInfo {
fn default() -> Self {
Self {
signo: 0,
errno: 0,
code: SignalCode::USER,
field: Default::default(),
}
}
}
#[repr(i32)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SignalCode {
ASYNCNL = -60,
TKILL = -6,
SIGIO = -5,
ASYNCIO = -4,
MESGQ = -3,
TIMER = -2,
QUEUE = -1,
USER = 0,
KERNEL = 128,
}
bitflags! {
#[derive(Default)]
pub struct SignalActionFlags : usize {
const NOCLDSTOP = 1;
const NOCLDWAIT = 2;
const SIGINFO = 4;
const ONSTACK = 0x08000000;
const RESTART = 0x10000000;
const NODEFER = 0x40000000;
const RESETHAND = 0x80000000;
const RESTORER = 0x04000000;
}
}