Skip to content

Commit

Permalink
Replace custom sa_restorer with restore module
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Aug 5, 2024
1 parent d7a7659 commit 6384ea6
Show file tree
Hide file tree
Showing 23 changed files with 282 additions and 230 deletions.
20 changes: 6 additions & 14 deletions examples/alarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ fn handle_alarm(signum: i32) {
assert_eq!(signum, nc::SIGALRM);
}

#[must_use]
#[inline]
fn get_sa_restorer() -> Option<nc::restorefn_t> {
let mut old_sa = nc::sigaction_t::default();
let ret = unsafe { nc::rt_sigaction(nc::SIGSEGV, None, Some(&mut old_sa)) };
if ret.is_ok() {
old_sa.sa_restorer
} else {
None
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_possible_wrap)]
Expand Down Expand Up @@ -51,8 +39,8 @@ pub fn alarm(seconds: u32) -> Result<u32, nc::Errno> {
fn main() {
let sa = nc::sigaction_t {
sa_handler: handle_alarm as nc::sighandler_t,
sa_flags: nc::SA_RESTART | nc::SA_RESTORER,
sa_restorer: get_sa_restorer(),
sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
sa_restorer: nc::restore::get_sa_restorer(),
..nc::sigaction_t::default()
};
let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
Expand All @@ -67,4 +55,8 @@ fn main() {
assert_eq!(ret, Err(nc::EINTR));

assert_eq!(remaining.unwrap(), 0);

unsafe {
nc::exit(1);
}
}
14 changes: 1 addition & 13 deletions examples/mprotect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,12 @@ fn handle_segfault(sig: i32) {
}
}

#[must_use]
#[inline]
fn get_sa_restorer() -> Option<nc::restorefn_t> {
let mut old_sa = nc::sigaction_t::default();
let ret = unsafe { nc::rt_sigaction(nc::SIGSEGV, None, Some(&mut old_sa)) };
if ret.is_ok() {
old_sa.sa_restorer
} else {
None
}
}

fn main() {
// Register SIGSEGV handler.
let sa = nc::sigaction_t {
sa_handler: handle_segfault as nc::sighandler_t,
sa_flags: nc::SA_RESTART | nc::SA_RESTORER,
sa_restorer: get_sa_restorer(),
sa_restorer: nc::restore::get_sa_restorer(),
..nc::sigaction_t::default()
};
let ret = unsafe { nc::rt_sigaction(nc::SIGSEGV, Some(&sa), None) };
Expand Down
20 changes: 1 addition & 19 deletions examples/sigaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ fn signal_handler(sig_num: i32) {
println!("signal handler {sig_num}");
}

fn __restore() {
unsafe {
nc::rt_sigreturn();
}
}

const SIGNALS: [i32; 14] = [
nc::SIGSEGV,
nc::SIGHUP,
Expand All @@ -29,23 +23,11 @@ const SIGNALS: [i32; 14] = [
nc::SIGCHLD,
];

#[must_use]
#[inline]
fn get_sa_restorer() -> Option<nc::restorefn_t> {
let mut old_sa = nc::sigaction_t::default();
let ret = unsafe { nc::rt_sigaction(nc::SIGSEGV, None, Some(&mut old_sa)) };
if ret.is_ok() {
old_sa.sa_restorer
} else {
None
}
}

fn main() {
let sa = nc::sigaction_t {
sa_handler: signal_handler as nc::sighandler_t,
sa_flags: nc::SA_RESTART | nc::SA_RESTORER,
sa_restorer: get_sa_restorer(),
sa_restorer: nc::restore::get_sa_restorer(),
..Default::default()
};
for sig_num in SIGNALS {
Expand Down
14 changes: 1 addition & 13 deletions examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@
// Use of this source is governed by Apache-2.0 License that can be found
// in the LICENSE file.

#[must_use]
#[inline]
fn get_sa_restorer() -> Option<nc::restorefn_t> {
let mut old_sa = nc::sigaction_t::default();
let ret = unsafe { nc::rt_sigaction(nc::SIGSEGV, None, Some(&mut old_sa)) };
if ret.is_ok() {
old_sa.sa_restorer
} else {
None
}
}

fn main() {
fn handle_alarm(signum: i32) {
assert_eq!(signum, nc::SIGALRM);
Expand All @@ -24,7 +12,7 @@ fn main() {
let sa = nc::sigaction_t {
sa_handler: handle_alarm as nc::sighandler_t,
sa_flags: nc::SA_RESTART | nc::SA_RESTORER,
sa_restorer: get_sa_restorer(),
sa_restorer: nc::restore::get_sa_restorer(),
..nc::sigaction_t::default()
};
let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
Expand Down
2 changes: 2 additions & 0 deletions src/calls/alarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
Expand Down
3 changes: 2 additions & 1 deletion src/calls/getitimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: 0,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
Expand Down
2 changes: 2 additions & 0 deletions src/calls/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
Expand Down
3 changes: 2 additions & 1 deletion src/calls/rt_sigaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_sigterm as nc::sighandler_t,
/// sa_mask: (nc::SA_RESTART | nc::SA_SIGINFO | nc::SA_ONSTACK).into(),
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(nc::SIGTERM, Some(&sa), None) };
Expand Down
3 changes: 2 additions & 1 deletion src/calls/setitimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: 0,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
Expand Down
3 changes: 2 additions & 1 deletion src/calls/sigaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_sigterm as nc::sighandler_t,
/// sa_mask: (nc::SA_RESTART | nc::SA_SIGINFO | nc::SA_ONSTACK).into(),
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::sigaction(nc::SIGTERM, Some(&sa), None) };
Expand Down
9 changes: 5 additions & 4 deletions src/calls/timer_getoverrun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
/// # Examples
///
/// ```
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// fn handle_alarm(signum: i32) {
/// assert_eq!(signum, nc::SIGALRM);
/// assert_eq!(signum, TIMER_SIG);
/// }
///
/// fn main() {
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// let sa = nc::sigaction_t {
/// sa_flags: nc::SA_SIGINFO,
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(TIMER_SIG, Some(&sa), None) };
Expand Down
9 changes: 5 additions & 4 deletions src/calls/timer_gettime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
/// # Examples
///
/// ```
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// fn handle_alarm(signum: i32) {
/// assert_eq!(signum, nc::SIGALRM);
/// assert_eq!(signum, TIMER_SIG);
/// }
///
/// fn main() {
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// let sa = nc::sigaction_t {
/// sa_flags: nc::SA_SIGINFO,
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(TIMER_SIG, Some(&sa), None) };
Expand Down
9 changes: 5 additions & 4 deletions src/calls/timer_settime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
/// # Examples
///
/// ```
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// fn handle_alarm(signum: i32) {
/// assert_eq!(signum, nc::SIGALRM);
/// assert_eq!(signum, TIMER_SIG);
/// }
///
/// fn main() {
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// let sa = nc::sigaction_t {
/// sa_flags: nc::SA_SIGINFO,
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(TIMER_SIG, Some(&sa), None) };
Expand Down
36 changes: 21 additions & 15 deletions src/platform/linux-aarch64/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,8 @@ pub unsafe fn getgroups(group_list: &mut [gid_t]) -> Result<i32, Errno> {
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: 0,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
Expand Down Expand Up @@ -6111,7 +6112,8 @@ pub unsafe fn rseq(rseq: &mut [rseq_t], flags: i32, sig: u32) -> Result<i32, Err
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_sigterm as nc::sighandler_t,
/// sa_mask: (nc::SA_RESTART | nc::SA_SIGINFO | nc::SA_ONSTACK).into(),
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(nc::SIGTERM, Some(&sa), None) };
Expand Down Expand Up @@ -7292,7 +7294,8 @@ pub unsafe fn sethostname<P: AsRef<Path>>(name: P) -> Result<(), Errno> {
///
/// let sa = nc::sigaction_t {
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: 0,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(nc::SIGALRM, Some(&sa), None) };
Expand Down Expand Up @@ -8429,16 +8432,17 @@ pub unsafe fn timer_delete(timer_id: timer_t) -> Result<(), Errno> {
/// # Examples
///
/// ```
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// fn handle_alarm(signum: i32) {
/// assert_eq!(signum, nc::SIGALRM);
/// assert_eq!(signum, TIMER_SIG);
/// }
///
/// fn main() {
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// let sa = nc::sigaction_t {
/// sa_flags: nc::SA_SIGINFO,
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(TIMER_SIG, Some(&sa), None) };
Expand Down Expand Up @@ -8501,16 +8505,17 @@ pub unsafe fn timer_getoverrun(timer_id: timer_t) -> Result<i32, Errno> {
/// # Examples
///
/// ```
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// fn handle_alarm(signum: i32) {
/// assert_eq!(signum, nc::SIGALRM);
/// assert_eq!(signum, TIMER_SIG);
/// }
///
/// fn main() {
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// let sa = nc::sigaction_t {
/// sa_flags: nc::SA_SIGINFO,
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(TIMER_SIG, Some(&sa), None) };
Expand Down Expand Up @@ -8570,16 +8575,17 @@ pub unsafe fn timer_gettime(timer_id: timer_t, curr: &mut itimerspec_t) -> Resul
/// # Examples
///
/// ```
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// fn handle_alarm(signum: i32) {
/// assert_eq!(signum, nc::SIGALRM);
/// assert_eq!(signum, TIMER_SIG);
/// }
///
/// fn main() {
/// const TIMER_SIG: i32 = nc::SIGRTMAX;
///
/// let sa = nc::sigaction_t {
/// sa_flags: nc::SA_SIGINFO,
/// sa_handler: handle_alarm as nc::sighandler_t,
/// sa_flags: nc::SA_RESTORER | nc::SA_RESTART,
/// sa_restorer: nc::restore::get_sa_restorer(),
/// ..nc::sigaction_t::default()
/// };
/// let ret = unsafe { nc::rt_sigaction(TIMER_SIG, Some(&sa), None) };
Expand Down
Loading

0 comments on commit 6384ea6

Please sign in to comment.