Skip to content

Commit

Permalink
Add more types and constants to the signals API. (#14)
Browse files Browse the repository at this point in the history
Expose `Sigaction`, `Signal`, `Sigflags`, and `sig_ign` in the signals
APIs.
  • Loading branch information
sunfishcode committed Aug 27, 2023
1 parent 77e77f6 commit 43a57e1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern crate unwinding;
mod arch;

pub use program::{at_exit, exit, exit_immediately};
pub use signals::{sigaction, Sigaction};
pub use signals::{sig_ign, sigaction, Sigaction, Sigflags, Sighandler, Signal, SA_RESTART};
#[cfg(feature = "set_thread_id")]
pub use threads::set_current_thread_id_after_a_fork;
#[cfg(feature = "origin-threads")]
Expand Down
21 changes: 19 additions & 2 deletions src/signals.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rustix::io;
use rustix::process::Signal;
#[cfg(not(target_arch = "riscv64"))]
use {
crate::arch,
Expand All @@ -8,7 +7,16 @@ use {
};

/// A signal action record for use with [`sigaction`].
pub type Sigaction = linux_raw_sys::general::kernel_sigaction;
pub use rustix::runtime::Sigaction;

/// A signal identifiier for use with [`sigaction`].
pub use rustix::process::Signal;

/// A signal handler function for use with [`Sigaction`].
pub use linux_raw_sys::general::__kernel_sighandler_t as Sighandler;

/// A flags type for use with [`Sigaction`].
pub use linux_raw_sys::ctypes::c_ulong as Sigflags;

/// Register a signal handler.
///
Expand All @@ -32,3 +40,12 @@ pub unsafe fn sigaction(sig: Signal, action: Option<Sigaction>) -> io::Result<Si

rustix::runtime::sigaction(sig, action)
}

/// Return a special "ignore" signal handler for ignoring signals.
#[doc(alias = "SIG_IGN")]
pub fn sig_ign() -> Sighandler {
linux_raw_sys::signal_macros::sig_ign()
}

/// `SA_RESTART`
pub const SA_RESTART: Sigflags = linux_raw_sys::general::SA_RESTART as _;
19 changes: 18 additions & 1 deletion src/signals_via_libc.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use core::mem::MaybeUninit;
use core::ptr::null;
use rustix::io;
use rustix::process::Signal;

/// A signal action record for use with [`sigaction`].
pub type Sigaction = libc::sigaction;

/// A signal identifiier for use with [`sigaction`].
pub use rustix::process::Signal;

/// A signal handler function for use with [`Sigaction`].
pub use libc::sighandler_t as Sighandler;

/// A flags type for use with [`Sigaction`].
pub use linux_raw_sys::ctypes::c_int as Sigflags;

/// Register a signal handler.
///
/// # Safety
Expand All @@ -26,3 +34,12 @@ pub unsafe fn sigaction(sig: Signal, action: Option<Sigaction>) -> io::Result<Si
Err(rustix::io::Errno::from_raw_os_error(libc::EINVAL))
}
}

/// Return a special "ignore" signal handler for ignoring signals.
#[doc(alias = "SIG_IGN")]
pub fn sig_ign() -> Sighandler {
libc::SIG_IGN
}

/// `SA_RESTART`
pub const SA_RESTART: Sigflags = libc::SA_RESTART;

0 comments on commit 43a57e1

Please sign in to comment.