From 82ff397489cc9fcc6bee1cdc98aa5eede18f8978 Mon Sep 17 00:00:00 2001 From: Fletcher Woodruff Date: Wed, 2 Oct 2019 13:11:34 -0600 Subject: sys_util: allow adding handlers for all signals Currently, sys_util's register_signal_handler only permits handlers for real-time signals. Rename that function to register_rt_signal_handler and add a new register_signal_handler that supports all signals, then update references to the old name. BUG=chromium:1008990 TEST=builds Change-Id: I455e14c562cd1f2ca4b308b4e38c503845321926 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1836185 Tested-by: Fletcher Woodruff Tested-by: kokoro Reviewed-by: Daniel Verkamp Reviewed-by: Dylan Reid Commit-Queue: Fletcher Woodruff --- sys_util/src/signal.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'sys_util') diff --git a/sys_util/src/signal.rs b/sys_util/src/signal.rs index 821f0f6..fee0a99 100644 --- a/sys_util/src/signal.rs +++ b/sys_util/src/signal.rs @@ -90,21 +90,15 @@ pub fn SIGRTMAX() -> c_int { unsafe { __libc_current_sigrtmax() } } -fn valid_signal_num(num: c_int) -> bool { +fn valid_rt_signal_num(num: c_int) -> bool { num >= SIGRTMIN() && num <= SIGRTMAX() } /// Registers `handler` as the signal handler of signum `num`. /// -/// The value of `num` must be within [`SIGRTMIN`, `SIGRTMAX`] range. -/// /// This is considered unsafe because the given handler will be called asynchronously, interrupting /// whatever the thread was doing and therefore must only do async-signal-safe operations. pub unsafe fn register_signal_handler(num: c_int, handler: extern "C" fn()) -> errno::Result<()> { - if !valid_signal_num(num) { - return Err(errno::Error::new(EINVAL)); - } - let mut sigact: sigaction = mem::zeroed(); sigact.sa_flags = SA_RESTART; sigact.sa_sigaction = handler as *const () as usize; @@ -117,6 +111,23 @@ pub unsafe fn register_signal_handler(num: c_int, handler: extern "C" fn()) -> e Ok(()) } +/// Registers `handler` as the signal handler for the real-time signal with signum `num`. +/// +/// The value of `num` must be within [`SIGRTMIN`, `SIGRTMAX`] range. +/// +/// This is considered unsafe because the given handler will be called asynchronously, interrupting +/// whatever the thread was doing and therefore must only do async-signal-safe operations. +pub unsafe fn register_rt_signal_handler( + num: c_int, + handler: extern "C" fn(), +) -> errno::Result<()> { + if !valid_rt_signal_num(num) { + return Err(errno::Error::new(EINVAL)); + } + + register_signal_handler(num, handler) +} + /// Creates `sigset` from an array of signal numbers. /// /// This is a helper function used when we want to manipulate signals. @@ -260,7 +271,7 @@ pub unsafe trait Killable { /// /// The value of `num` must be within [`SIGRTMIN`, `SIGRTMAX`] range. fn kill(&self, num: c_int) -> errno::Result<()> { - if !valid_signal_num(num) { + if !valid_rt_signal_num(num) { return Err(errno::Error::new(EINVAL)); } -- cgit 1.4.1