libc_core/
internal.rs

1//! This module provides the `libc` types for libc internal use.
2//!
3//!
4
5use crate::types::SigSet;
6
7/// 信号处理函数的结构体(对应 C 的 `struct sigaction`)
8///
9/// MUSL: <https://github.com/bminor/musl/blob/c47ad25ea3b484e10326f933e927c0bc8cded3da/src/internal/ksigaction.h#L6>
10#[repr(C)]
11#[derive(Debug, Clone)]
12pub struct SigAction {
13    /// 信号处理函数指针,类似于 C 中的 void (*sa_handler)(int);
14    /// 当信号发生时将调用此函数。也可以是特殊值,如 SIG_IGN 或 SIG_DFL。
15    pub handler: usize,
16    /// 标志位,用于指定处理行为,如 SA_RESTART、SA_NOCLDSTOP 等。
17    /// 对应 C 中的 int sa_flags;
18    pub flags: usize,
19    /// 系统调用的恢复函数指针,一般在使用自定义恢复机制时使用。
20    /// 对应 C 中的 void (*sa_restorer)(void); 通常不使用,设为 0。
21    pub restorer: usize,
22    /// 一个信号集合,用于在处理该信号时阻塞的其他信号。
23    /// 对应 C 中的 sigset_t sa_mask;
24    pub mask: SigSet,
25}
26
27impl SigAction {
28    /// 表示使用该信号的默认处理方式(default action)
29    /// 用于注册信号处理函数时,表示恢复默认行为(如终止进程等)。
30    pub const SIG_DFL: usize = 0;
31    /// 表示忽略该信号(ignore)
32    /// 用于注册信号处理函数时,表示收到该信号时不做任何处理。
33    pub const SIG_IGN: usize = 1;
34    /// 创建一个新的信号处理函数结构体,所有字段初始化为默认值。
35    pub const fn empty() -> Self {
36        Self {
37            handler: 0,
38            mask: SigSet::empty(),
39            flags: 0,
40            restorer: 0,
41        }
42    }
43}