Skip to content

Commit

Permalink
wrappers: Add sigaction
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Oct 18, 2024
1 parent f0b0f75 commit e4469bc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
15 changes: 1 addition & 14 deletions examples/mprotect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,7 @@ fn handle_segfault(sig: i32) {
}

fn main() {
#[cfg(nc_has_sa_restorer)]
let sa = nc::sigaction_t {
sa_handler: handle_segfault as nc::sighandler_t,
sa_flags: nc::SA_RESTART | nc::SA_RESTORER,
sa_restorer: nc::restore::get_sa_restorer(),
..nc::sigaction_t::default()
};
#[cfg(target_arch = "riscv64")]
#[cfg(not(nc_has_sa_restorer))]
let sa = nc::sigaction_t {
sa_handler: handle_segfault as nc::sighandler_t,
sa_flags: nc::SA_RESTART,
..nc::sigaction_t::default()
};
let sa = nc::new_sigaction(handle_segfault);
let ret = unsafe { nc::rt_sigaction(nc::SIGSEGV, Some(&sa), None) };
assert!(ret.is_ok());

Expand Down
3 changes: 3 additions & 0 deletions src/wrappers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ mod alarm;
)
))]
pub use alarm::alarm;

mod sigaction;
pub use sigaction::new_sigaction;
26 changes: 26 additions & 0 deletions src/wrappers/sigaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2024 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Apache-2.0 License that can be found
// in the LICENSE file.

#![allow(clippy::module_name_repetitions)]

use crate::{sigaction_t, sighandler_t, SA_RESTART};

#[cfg(nc_has_sa_restorer)]
pub fn new_sigaction(handler: fn(i32)) -> sigaction_t {
sigaction_t {
sa_handler: handler as sighandler_t,
sa_flags: SA_RESTART | crate::SA_RESTORER,
sa_restorer: crate::restore::get_sa_restorer(),
..sigaction_t::default()
}
}

#[cfg(not(nc_has_sa_restorer))]
pub fn new_sigaction(handler: fn(i32)) -> sigaction_t {
sigaction_t {
sa_handler: handler as sighandler_t,
sa_flags: SA_RESTART,
..sigaction_t::default()
}
}

0 comments on commit e4469bc

Please sign in to comment.