libc_core/internal.rs
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
//! This module provides the `libc` types for libc internal use.
//!
//!
use crate::types::SigSet;
/// 信号处理函数的结构体(对应 C 的 `struct sigaction`)
///
/// MUSL: <https://github.com/bminor/musl/blob/c47ad25ea3b484e10326f933e927c0bc8cded3da/src/internal/ksigaction.h#L6>
#[repr(C)]
#[cfg_attr(
feature = "zerocopy",
derive(
zerocopy::FromBytes,
zerocopy::Immutable,
zerocopy::IntoBytes,
zerocopy::KnownLayout
)
)]
#[derive(Debug, Clone)]
pub struct SigAction {
/// 信号处理函数指针,类似于 C 中的 void (*sa_handler)(int);
/// 当信号发生时将调用此函数。也可以是特殊值,如 SIG_IGN 或 SIG_DFL。
pub handler: usize,
/// 标志位,用于指定处理行为,如 SA_RESTART、SA_NOCLDSTOP 等。
/// 对应 C 中的 int sa_flags;
pub flags: usize,
/// 系统调用的恢复函数指针,一般在使用自定义恢复机制时使用。
/// 对应 C 中的 void (*sa_restorer)(void); 通常不使用,设为 0。
pub restorer: usize,
/// 一个信号集合,用于在处理该信号时阻塞的其他信号。
/// 对应 C 中的 sigset_t sa_mask;
pub mask: SigSet,
}
impl SigAction {
/// 表示使用该信号的默认处理方式(default action)
/// 用于注册信号处理函数时,表示恢复默认行为(如终止进程等)。
pub const SIG_DFL: usize = 0;
/// 表示忽略该信号(ignore)
/// 用于注册信号处理函数时,表示收到该信号时不做任何处理。
pub const SIG_IGN: usize = 1;
/// 创建一个新的信号处理函数结构体,所有字段初始化为默认值。
pub const fn empty() -> Self {
Self {
handler: 0,
mask: SigSet::empty(),
flags: 0,
restorer: 0,
}
}
}