1use core::{
7 ffi::{c_char, c_int, c_long, c_uint, c_ulong, c_ulonglong, c_void},
8 fmt::Debug,
9};
10
11use bitflags::bitflags;
12
13use crate::{
14 arch::trap::TrapFrame,
15 uapi::types::{ClockT, PidT, SigSetT, SizeT, UidT},
16};
17
18pub const SIGSET_SIZE: usize = core::mem::size_of::<SigSetT>();
20
21#[repr(C)]
22#[derive(Clone, Copy)]
23pub union __SaHandler {
24 pub sa_handler: SaHandlerPtr,
27
28 pub sa_sigaction: SaSigactionPtr,
31}
32
33impl Debug for __SaHandler {
34 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
35 write!(f, "__SaHandler {{ ... }}")
36 }
37}
38
39#[repr(C)]
40#[derive(Clone, Copy, Debug)]
41pub struct SignalAction {
43 pub __sa_handler: __SaHandler,
46
47 pub sa_mask: SigSetT,
50
51 pub sa_flags: c_int,
55 pub sa_restorer: SaRestorerPtr,
58}
59
60impl SignalAction {
61 pub unsafe fn sa_handler(&self) -> SaHandlerPtr {
64 unsafe { self.__sa_handler.sa_handler }
65 }
66
67 pub unsafe fn sa_sigaction(&self) -> SaSigactionPtr {
70 unsafe { self.__sa_handler.sa_sigaction }
71 }
72
73 pub fn is_siginfo(&self) -> bool {
75 (self.sa_flags as u32) & SaFlags::SIGINFO.bits() != 0
77 }
78
79 pub fn new(handler: SaHandlerPtr, flags: SaFlags, mask: SignalFlags) -> Self {
86 Self {
87 __sa_handler: __SaHandler {
88 sa_handler: handler,
89 },
90 sa_mask: mask.bits() as SigSetT,
91 sa_flags: flags.bits() as c_int,
92 sa_restorer: core::ptr::null_mut(),
93 }
94 }
95}
96
97impl Default for SignalAction {
98 fn default() -> Self {
100 Self::new(SIG_DFL as *mut _, SaFlags::empty(), SignalFlags::empty())
101 }
102}
103
104pub const NSIG: usize = 31;
106
107pub const NUM_SIGHUP: usize = 1;
108pub const NUM_SIGINT: usize = 2;
109pub const NUM_SIGQUIT: usize = 3;
110pub const NUM_SIGILL: usize = 4;
111pub const NUM_SIGTRAP: usize = 5;
112pub const NUM_SIGABRT: usize = 6;
113pub const NUM_SIGBUS: usize = 7;
114pub const NUM_SIGFPE: usize = 8;
115pub const NUM_SIGKILL: usize = 9;
116pub const NUM_SIGUSR1: usize = 10;
117pub const NUM_SIGSEGV: usize = 11;
118pub const NUM_SIGUSR2: usize = 12;
119pub const NUM_SIGPIPE: usize = 13;
120pub const NUM_SIGALRM: usize = 14;
121pub const NUM_SIGTERM: usize = 15;
122pub const NUM_SIGSTKFLT: usize = 16;
123pub const NUM_SIGCHLD: usize = 17;
124pub const NUM_SIGCONT: usize = 18;
125pub const NUM_SIGSTOP: usize = 19;
126pub const NUM_SIGTSTP: usize = 20;
127pub const NUM_SIGTTIN: usize = 21;
128pub const NUM_SIGTTOU: usize = 22;
129pub const NUM_SIGURG: usize = 23;
130pub const NUM_SIGXCPU: usize = 24;
131pub const NUM_SIGXFSZ: usize = 25;
132pub const NUM_SIGVTALRM: usize = 26;
133pub const NUM_SIGPROF: usize = 27;
134pub const NUM_SIGWINCH: usize = 28;
135pub const NUM_SIGIO: usize = 29;
136pub const NUM_SIGPWR: usize = 30;
137pub const NUM_SIGSYS: usize = 31;
138
139bitflags! {
140 #[derive(Clone, Debug, Copy)]
141 #[repr(transparent)]
142 pub struct SignalFlags: usize {
144 const SIGHUP = 1 << (NUM_SIGHUP - 1);
145 const SIGINT = 1 << (NUM_SIGINT - 1);
146 const SIGQUIT = 1 << (NUM_SIGQUIT - 1);
147 const SIGILL = 1 << (NUM_SIGILL - 1);
148 const SIGTRAP = 1 << (NUM_SIGTRAP - 1);
149 const SIGABRT = 1 << (NUM_SIGABRT - 1);
150 const SIGBUS = 1 << (NUM_SIGBUS - 1);
151 const SIGFPE = 1 << (NUM_SIGFPE - 1);
152 const SIGKILL = 1 << (NUM_SIGKILL - 1);
153 const SIGUSR1 = 1 << (NUM_SIGUSR1 - 1);
154 const SIGSEGV = 1 << (NUM_SIGSEGV - 1);
155 const SIGUSR2 = 1 << (NUM_SIGUSR2 - 1);
156 const SIGPIPE = 1 << (NUM_SIGPIPE - 1);
157 const SIGALRM = 1 << (NUM_SIGALRM - 1);
158 const SIGTERM = 1 << (NUM_SIGTERM - 1);
159 const SIGSTKFLT = 1 << (NUM_SIGSTKFLT - 1);
160 const SIGCHLD = 1 << (NUM_SIGCHLD - 1);
161 const SIGCONT = 1 << (NUM_SIGCONT - 1);
162 const SIGSTOP = 1 << (NUM_SIGSTOP - 1);
163 const SIGTSTP = 1 << (NUM_SIGTSTP - 1);
164 const SIGTTIN = 1 << (NUM_SIGTTIN - 1);
165 const SIGTTOU = 1 << (NUM_SIGTTOU - 1);
166 const SIGURG = 1 << (NUM_SIGURG - 1);
167 const SIGXCPU = 1 << (NUM_SIGXCPU - 1);
168 const SIGXFSZ = 1 << (NUM_SIGXFSZ - 1);
169 const SIGVTALRM = 1 << (NUM_SIGVTALRM - 1);
170 const SIGPROF = 1 << (NUM_SIGPROF - 1);
171 const SIGWINCH = 1 << (NUM_SIGWINCH - 1);
172 const SIGIO = 1 << (NUM_SIGIO - 1);
173 const SIGPWR = 1 << (NUM_SIGPWR - 1);
174 const SIGSYS = 1 << (NUM_SIGSYS - 1);
175 }
176}
177
178impl SignalFlags {
179 pub fn from_signal_num(sig_num: usize) -> Option<Self> {
180 if sig_num == 0 || sig_num > NSIG {
181 return None;
182 }
183 Some(SignalFlags::from_bits(1 << (sig_num - 1)).unwrap())
184 }
185
186 pub fn to_signal_number(&self) -> usize {
190 for sig_num in 1..=NSIG {
191 if self.contains(SignalFlags::from_bits(1 << (sig_num - 1)).unwrap()) {
192 return sig_num;
193 }
194 }
195 0 }
197
198 pub fn from_sigset_t(set: SigSetT) -> Self {
199 SignalFlags::from_bits(set as usize).unwrap()
200 }
201
202 pub fn to_sigset_t(&self) -> SigSetT {
203 self.bits() as SigSetT
204 }
205}
206
207pub type SaHandlerFn = extern "C" fn(c_int);
210pub type SaSigactionFn = extern "C" fn(c_int, *const SigInfoT, *mut c_void);
211pub type SaRestorerFn = extern "C" fn();
212
213pub type SaHandlerPtr = *mut SaHandlerFn;
215pub type SaSigactionPtr = *mut SaSigactionFn;
216pub type SaRestorerPtr = *mut SaRestorerFn;
217
218bitflags! {
221 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
223 #[repr(transparent)]
224 pub struct SaFlags: u32 {
225 const NOCLDSTOP = 0x0000_0001;
227
228 const NOCLDWAIT = 0x0000_0002;
230
231 const SIGINFO = 0x0000_0004;
233
234 const ONSTACK = 0x0800_0000;
236
237 const RESTART = 0x1000_0000;
239
240 const NODEFER = 0x4000_0000;
242
243 const RESETHAND = 0x8000_0000;
245
246 const UNSUPPORTED = 0x0000_0400;
248
249 const EXPOSE_TAGBITS = 0x0000_0800;
251
252 const NOMASK = Self::NODEFER.bits();
255
256 const ONESHOT = Self::RESETHAND.bits();
258 }
259}
260
261const ALL_KNOWN_FLAGS: SaFlags = SaFlags::all();
262
263const NOW_SUPPORTED_FLAGS: SaFlags = SaFlags::UNSUPPORTED;
264
265impl SaFlags {
266 pub fn is_supported(&self) -> bool {
268 self.bits() & !NOW_SUPPORTED_FLAGS.bits() == 0
269 }
270
271 pub fn is_known(&self) -> bool {
273 self.bits() & !ALL_KNOWN_FLAGS.bits() == 0
274 }
275}
276
277pub const SIG_BLOCK: i32 = 0;
282
283pub const SIG_UNBLOCK: i32 = 1;
285
286pub const SIG_SETMASK: i32 = 2;
288
289pub const SIG_DFL: isize = 0;
296
297pub const SIG_IGN: isize = 1;
299
300pub const SIG_ERR: isize = -1;
302
303#[repr(C)]
304#[derive(Clone, Copy)]
305pub struct SigInfoT {
307 pub si_signo: c_int,
310 pub si_errno: c_int,
311 pub si_code: c_int,
312
313 pub __si_fields: __SiFields,
315}
316
317impl SigInfoT {
318 pub fn new() -> Self {
320 Self {
321 si_signo: 0,
322 si_errno: 0,
323 si_code: 0,
324 __si_fields: __SiFields {
325 __pad: [0; __SIGINFO_PAD_SIZE],
326 },
327 }
328 }
329}
330
331#[repr(C)]
332#[derive(Clone, Copy)]
333pub union Sigval {
334 pub sival_int: c_int,
335 pub sival_ptr: *mut c_void,
336}
337
338const __SIGINFO_PAD_SIZE: usize =
339 128 - 2 * core::mem::size_of::<c_int>() - core::mem::size_of::<c_long>();
340
341#[repr(C)]
342#[derive(Clone, Copy)]
343pub union __SiFields {
344 pub __pad: [c_char; __SIGINFO_PAD_SIZE], pub __si_common: __SiCommon,
348 pub __sigfault: __SigFault,
349 pub __sigpoll: __SigPoll,
350 pub __sigsys: __SigSys,
351}
352
353#[repr(C)]
354#[derive(Clone, Copy)]
355pub struct __SiCommon {
356 pub __first: __FirstCommon,
357 pub __second: __SecondCommon,
358}
359
360#[repr(C)]
361#[derive(Clone, Copy)]
362pub struct __SigFault {
363 pub si_addr: *mut c_void,
364 pub si_addr_lsb: i16, pub __first: __FirstFault,
366}
367
368#[repr(C)]
369#[derive(Clone, Copy)]
370pub struct __SigPoll {
371 pub si_band: c_long, pub si_fd: c_int,
373}
374
375#[repr(C)]
376#[derive(Clone, Copy)]
377pub struct __SigSys {
378 pub si_call_addr: *mut c_void,
379 pub si_syscall: c_int,
380 pub si_arch: c_uint, }
382
383#[repr(C)]
386#[derive(Clone, Copy)]
387pub union __FirstCommon {
388 pub __piduid: __PidUid,
389 pub __timer: __Timer,
390}
391
392#[repr(C)]
393#[derive(Clone, Copy)]
394pub struct __PidUid {
395 pub si_pid: PidT,
396 pub si_uid: UidT,
397}
398
399#[repr(C)]
400#[derive(Clone, Copy)]
401pub struct __Timer {
402 pub si_timerid: c_int,
403 pub si_overrun: c_int,
404}
405
406#[repr(C)]
407#[derive(Clone, Copy)]
408pub union __SecondCommon {
409 pub si_value: Sigval,
410 pub __sigchld: __SigChld,
411}
412
413#[repr(C)]
414#[derive(Clone, Copy)]
415pub struct __SigChld {
416 pub si_status: c_int,
417 pub si_utime: ClockT,
418 pub si_stime: ClockT,
419}
420
421#[repr(C)]
422#[derive(Clone, Copy)]
423pub union __FirstFault {
424 pub __addr_bnd: __AddrBnd,
425 pub si_pkey: c_uint, }
427
428#[repr(C)]
429#[derive(Clone, Copy)]
430pub struct __AddrBnd {
431 pub si_lower: *mut c_void,
432 pub si_upper: *mut c_void,
433}
434
435#[repr(C)]
438#[derive(Clone, Copy, Debug)]
439pub struct UContextT {
440 pub uc_flags: c_ulong,
442 pub uc_link: *mut UContextT,
444 pub uc_stack: SignalStack,
446 pub uc_sigmask: SigSetT,
448 pub uc_mcontext: MContextT,
450}
451
452impl UContextT {
453 pub fn default() -> Self {
455 Self {
456 uc_flags: 0,
457 uc_link: core::ptr::null_mut(),
458 uc_stack: SignalStack::default(),
459 uc_sigmask: 0,
460 uc_mcontext: MContextT::new(),
461 }
462 }
463
464 pub fn new(
473 flags: c_ulong,
474 link: *mut UContextT,
475 stack: SignalStack,
476 sigmask: SigSetT,
477 mcontext: MContextT,
478 ) -> Self {
479 Self {
480 uc_flags: flags,
481 uc_link: link,
482 uc_stack: stack,
483 uc_sigmask: sigmask,
484 uc_mcontext: mcontext,
485 }
486 }
487}
488
489#[repr(C)]
492#[derive(Clone, Copy, Debug, Default)]
493pub struct SignalStack {
494 pub ss_sp: usize,
496 pub ss_flags: c_int,
498 pub ss_size: SizeT,
500}
501
502pub const MINSIGSTKSZ: usize = 2048;
504pub const SIGSTKSZ: usize = 8192;
506pub const SS_ONSTACK: usize = 1;
509pub const SS_DISABLE: usize = 2;
511pub const SS_AUTODISARM: usize = 1 << 31;
513pub const SS_FLAG_BITS: usize = SS_AUTODISARM;
515
516#[repr(C)]
517#[derive(Clone, Copy, Debug)]
518pub struct MContextT {
520 pub gregs: [c_ulong; 32],
522 pub fpregs: [c_ulonglong; 66],
526}
527
528impl MContextT {
529 pub fn new() -> Self {
531 Self {
532 gregs: [0; 32],
533 fpregs: [0; 66],
534 }
535 }
536
537 pub fn from_trap_frame(tf: &TrapFrame) -> Self {
539 tf.to_mcontext()
540 }
541}
542
543pub const UC_GPRS_HIGH: usize = 1;
545pub const UC_VXRS: usize = 2;