Skip to content

Commit c751e4d

Browse files
author
Alexandra Iordache
committed
sys_util: generic register_signal_handler
Renamed register_sigsys_handler to register_signal_handler, enabling the installation of custom signal handlers for any signal. Signed-off-by: Alexandra Iordache <[email protected]>
1 parent 9888c00 commit c751e4d

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

sys_util/src/signal.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn validate_vcpu_signal_num(num: c_int) -> io::Result<c_int> {
5454
///
5555
/// This is considered unsafe because the given handler will be called asynchronously, interrupting
5656
/// whatever the thread was doing and therefore must only do async-signal-safe operations.
57-
pub unsafe fn register_vcpu_signal_handler(num: i32, handler: SignalHandler) -> io::Result<()> {
57+
pub unsafe fn register_vcpu_signal_handler(num: c_int, handler: SignalHandler) -> io::Result<()> {
5858
let num = validate_vcpu_signal_num(num)?;
5959
// Safe, because this is a POD struct.
6060
let mut sigact: sigaction = mem::zeroed();
@@ -64,11 +64,11 @@ pub unsafe fn register_vcpu_signal_handler(num: i32, handler: SignalHandler) ->
6464
}
6565

6666
/// Registers the specified signal handler for `SIGSYS`.
67-
pub fn register_sigsys_handler(sigsys_handler: SignalHandler) -> Result<(), io::Error> {
67+
pub fn register_signal_handler(signum: c_int, handler: SignalHandler) -> Result<(), io::Error> {
6868
// Safe, because this is a POD struct.
6969
let mut sigact: sigaction = unsafe { mem::zeroed() };
7070
sigact.sa_flags = libc::SA_SIGINFO;
71-
sigact.sa_sigaction = sigsys_handler as usize;
71+
sigact.sa_sigaction = handler as usize;
7272

7373
// We set all the bits of sa_mask, so all signals are blocked on the current thread while the
7474
// SIGSYS handler is executing. Safe because the parameter is valid and we check the return
@@ -78,7 +78,7 @@ pub fn register_sigsys_handler(sigsys_handler: SignalHandler) -> Result<(), io::
7878
}
7979

8080
// Safe because the parameters are valid and we check the return value.
81-
unsafe { SyscallReturnCode(sigaction(libc::SIGSYS, &sigact, null_mut())).into_empty_result() }
81+
unsafe { SyscallReturnCode(sigaction(signum, &sigact, null_mut())).into_empty_result() }
8282
}
8383

8484
/// Trait for threads that can be signalled via `pthread_kill`.
@@ -116,6 +116,8 @@ mod tests {
116116
use std::thread;
117117
use std::time::Duration;
118118

119+
use libc::SIGSYS;
120+
119121
static mut SIGNAL_HANDLER_CALLED: bool = false;
120122

121123
extern "C" fn handle_signal(_: c_int, _: *mut siginfo_t, _: *mut c_void) {
@@ -130,7 +132,7 @@ mod tests {
130132
// testing bad value
131133
assert!(register_vcpu_signal_handler(SIGRTMAX(), handle_signal).is_err());
132134
assert!(register_vcpu_signal_handler(0, handle_signal).is_ok());
133-
assert!(register_sigsys_handler(handle_signal).is_ok());
135+
assert!(register_signal_handler(SIGSYS, handle_signal).is_ok());
134136
}
135137
}
136138

vmm/src/signal_handler.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::result::Result;
1010
use libc::{_exit, c_int, c_void, siginfo_t, SIGSYS};
1111

1212
use logger::{Metric, LOGGER, METRICS};
13-
use sys_util::register_sigsys_handler;
13+
use sys_util::register_signal_handler;
1414

1515
// The offset of `si_syscall` (offending syscall identifier) within the siginfo structure
1616
// expressed as an `(u)int*`.
@@ -58,7 +58,7 @@ extern "C" fn sigsys_handler(num: c_int, info: *mut siginfo_t, _unused: *mut c_v
5858
/// Custom handlers are installed for: `SIGSYS`.
5959
///
6060
pub fn register_signal_handlers() -> Result<(), io::Error> {
61-
register_sigsys_handler(sigsys_handler)
61+
register_signal_handler(SIGSYS, sigsys_handler)
6262
}
6363

6464
#[cfg(test)]

0 commit comments

Comments
 (0)