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#[cfg_attr(
12    feature = "zerocopy",
13    derive(
14        zerocopy::FromBytes,
15        zerocopy::Immutable,
16        zerocopy::IntoBytes,
17        zerocopy::KnownLayout
18    )
19)]
20#[derive(Debug, Clone)]
21pub struct SigAction {
22    /// 信号处理函数指针,类似于 C 中的 void (*sa_handler)(int);
23    /// 当信号发生时将调用此函数。也可以是特殊值,如 SIG_IGN 或 SIG_DFL。
24    pub handler: usize,
25    /// 标志位,用于指定处理行为,如 SA_RESTART、SA_NOCLDSTOP 等。
26    /// 对应 C 中的 int sa_flags;
27    pub flags: usize,
28    /// 系统调用的恢复函数指针,一般在使用自定义恢复机制时使用。
29    /// 对应 C 中的 void (*sa_restorer)(void); 通常不使用,设为 0。
30    pub restorer: usize,
31    /// 一个信号集合,用于在处理该信号时阻塞的其他信号。
32    /// 对应 C 中的 sigset_t sa_mask;
33    pub mask: SigSet,
34}
35
36impl SigAction {
37    /// 表示使用该信号的默认处理方式(default action)
38    /// 用于注册信号处理函数时,表示恢复默认行为(如终止进程等)。
39    pub const SIG_DFL: usize = 0;
40    /// 表示忽略该信号(ignore)
41    /// 用于注册信号处理函数时,表示收到该信号时不做任何处理。
42    pub const SIG_IGN: usize = 1;
43    /// 创建一个新的信号处理函数结构体,所有字段初始化为默认值。
44    pub const fn empty() -> Self {
45        Self {
46            handler: 0,
47            mask: SigSet::empty(),
48            flags: 0,
49            restorer: 0,
50        }
51    }
52}