#[cfg(feature = "unsafe_syscall")]
use crate::LINUX_SIGACTION;
use crate::{Debug, FmtResult, Formatter, c_size_t, c_void};
use crate::{LinuxSigactionFlags, LinuxSigactionHandler, LinuxSiginfo, LinuxSigset};
#[doc = crate::_tags!(linux signal abi)]
#[doc = crate::_doc_meta!{
location("sys/os/linux/process"),
#[cfg(target_pointer_width = "32")]
test_size_of(LinuxSigaction = 16|128),
#[cfg(target_pointer_width = "64")]
test_size_of(LinuxSigaction = 32|256),
}]
#[repr(C)]
#[must_use]
pub struct LinuxSigaction {
sa_handler: LinuxSigactionHandler,
sa_flags: c_size_t,
sa_restorer: Option<extern "C" fn()>,
sa_mask: LinuxSigset,
}
#[rustfmt::skip]
impl Debug for LinuxSigaction {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
write!(f, "LinuxSigaction {{ ")?;
#[cfg(feature = "unsafe_syscall")]
if self.sa_flags & LINUX_SIGACTION::SA_SIGINFO == 0 {
write!(f, "sa_handler: {:p}, ", unsafe { self.sa_handler.sa_handler })?;
} else {
write!(f, "sa_sigaction: {:p}, ", unsafe { self.sa_handler.sa_sigaction })?;
}
write!(f, "sa_flags: {:#x}, ", self.sa_flags)?;
match self.sa_restorer {
Some(ptr) => write!(f, "sa_restorer: {ptr:p}, ")?,
None => write!(f, "sa_restorer: None, ")?,
}
write!(f, "sa_mask: {:?}", self.sa_mask)?;
write!(f, " }}")
}
}
impl LinuxSigaction {
pub const fn new(
handler: extern "C" fn(i32),
flags: LinuxSigactionFlags,
mask: LinuxSigset,
restorer: Option<extern "C" fn()>,
) -> Self {
Self {
sa_handler: LinuxSigactionHandler { sa_handler: handler },
sa_flags: flags.as_c_size_t(),
sa_restorer: restorer,
sa_mask: mask,
}
}
pub const fn new_siginfo(
handler: extern "C" fn(i32, *mut LinuxSiginfo, *mut c_void),
flags: LinuxSigactionFlags,
mask: LinuxSigset,
restorer: Option<extern "C" fn()>,
) -> Self {
Self {
sa_handler: LinuxSigactionHandler { sa_sigaction: handler },
sa_flags: flags.as_c_size_t(),
sa_restorer: restorer,
sa_mask: mask,
}
}
#[cfg(feature = "unsafe_syscall")]
pub const fn sa_handler(&self) -> Option<extern "C" fn(i32)> {
if self.sa_flags & LINUX_SIGACTION::SA_SIGINFO == 0 {
Some(unsafe { self.sa_handler.sa_handler })
} else {
None
}
}
#[must_use]
#[cfg(feature = "unsafe_syscall")]
pub const fn sa_sigaction(&self) -> Option<extern "C" fn(i32, *mut LinuxSiginfo, *mut c_void)> {
if self.sa_flags & LINUX_SIGACTION::SA_SIGINFO != 0 {
Some(unsafe { self.sa_handler.sa_sigaction })
} else {
None
}
}
#[must_use]
pub const fn flags(&self) -> LinuxSigactionFlags {
LinuxSigactionFlags::from_c_size_t(self.sa_flags as c_size_t)
}
#[must_use]
pub const fn raw_flags(&self) -> c_size_t {
self.sa_flags
}
}
impl LinuxSigaction {
pub const SIG_DFL: isize = 0;
pub const SIG_IGN: isize = 1;
}