From 44192051dcc60a9ebd9803ed085bbdd9d0b53d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Ker=C3=A4nen?= Date: Fri, 2 Mar 2018 20:09:37 +0200 Subject: [PATCH] Change SigAction::flags to use from_bits_truncated On Linux, if the signal trampoline code is in the C library, sigaction sets the SA_RESTORER flag (0x04000000) in the sa_flags field of old sigaction (see sigreturn(2)). This is not intended for application use and is missing from SaFlags, therefore from_bits fails and unwrapping panics the user program. This fix just drops the bits that are not defined in SaFlags. --- src/sys/signal.rs | 2 +- test/sys/test_signal.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/sys/signal.rs b/src/sys/signal.rs index 10798ba1c9..875f5de6e6 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -389,7 +389,7 @@ impl SigAction { } pub fn flags(&self) -> SaFlags { - SaFlags::from_bits(self.sigaction.sa_flags).unwrap() + SaFlags::from_bits_truncate(self.sigaction.sa_flags) } pub fn mask(&self) -> SigSet { diff --git a/test/sys/test_signal.rs b/test/sys/test_signal.rs index ae959ef4e2..7d3a9bf2f4 100644 --- a/test/sys/test_signal.rs +++ b/test/sys/test_signal.rs @@ -6,6 +6,20 @@ fn test_kill_none() { kill(getpid(), None).expect("Should be able to send signal to myself."); } +#[test] +fn test_old_sigaction_flags() { + extern "C" fn handler(_: ::libc::c_int) {} + let act = SigAction::new( + SigHandler::Handler(handler), + SaFlags::empty(), + SigSet::empty(), + ); + let oact = unsafe { sigaction(SIGINT, &act) }.unwrap(); + let _flags = oact.flags(); + let oact = unsafe { sigaction(SIGINT, &act) }.unwrap(); + let _flags = oact.flags(); +} + #[test] fn test_sigprocmask_noop() { sigprocmask(SigmaskHow::SIG_BLOCK, None, None)