use std::sync::Arc;
use std::sync::Mutex;
use reverie::syscalls::Errno;
use serde::Deserialize;
use serde::Serialize;
pub(crate) type Sigset = u64;
pub(crate) const MAX_SIGNAL: i32 = 64;
const SIG_DFL: u64 = 0;
const SIG_IGN: u64 = 1;
const UAPI_SA_FLAGS: u64 = sa_flag(libc::SA_NOCLDSTOP)
| sa_flag(libc::SA_NOCLDWAIT)
| sa_flag(libc::SA_SIGINFO)
| sa_flag(libc::SA_ONSTACK)
| sa_flag(libc::SA_RESTART)
| sa_flag(libc::SA_NODEFER)
| sa_flag(libc::SA_RESETHAND)
| sa_flag(SA_EXPOSE_TAGBITS)
| sa_flag(SA_RESTORER);
const fn sa_flag(flag: libc::c_int) -> u64 {
flag as u32 as u64
}
const SA_EXPOSE_TAGBITS: libc::c_int = 0x0000_0800;
const SA_RESTORER: libc::c_int = 0x0400_0000;
pub(crate) fn sigbit(signal: i32) -> Sigset {
if (1..=MAX_SIGNAL).contains(&signal) {
1 << (signal - 1)
} else {
0
}
}
const CLONE_CLEAR_SIGHAND: u64 = 0x1_0000_0000;
pub(crate) const MAX_OPEN_FRAMES: usize = 32;
const UNBLOCKABLE: Sigset = (1 << (libc::SIGKILL - 1)) | (1 << (libc::SIGSTOP - 1));
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub(crate) struct Action {
pub(crate) handler: u64,
pub(crate) flags: u64,
pub(crate) restorer: u64,
pub(crate) mask: Sigset,
}
impl Action {
pub(crate) fn normalized(self) -> Self {
Action {
flags: self.flags & UAPI_SA_FLAGS,
mask: self.mask & !UNBLOCKABLE,
..self
}
}
pub(crate) fn is_caught(&self) -> bool {
self.handler != SIG_DFL && self.handler != SIG_IGN
}
pub(crate) fn is_ignored(&self) -> bool {
self.handler == SIG_IGN
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "Vec<Action>", into = "Vec<Action>")]
pub(crate) struct Dispositions {
actions: Vec<Action>,
}
impl From<Dispositions> for Vec<Action> {
fn from(dispositions: Dispositions) -> Self {
dispositions.actions
}
}
impl TryFrom<Vec<Action>> for Dispositions {
type Error = String;
fn try_from(actions: Vec<Action>) -> Result<Self, Self::Error> {
if actions.len() == MAX_SIGNAL as usize {
Ok(Dispositions { actions })
} else {
Err(format!(
"a signal action table has {} entries, not {MAX_SIGNAL}",
actions.len()
))
}
}
}
impl Dispositions {
pub(crate) fn after_exec(ignored: Sigset) -> Self {
let mut actions = vec![Action::default(); MAX_SIGNAL as usize];
for (index, action) in actions.iter_mut().enumerate() {
if ignored & (1 << index) & !UNBLOCKABLE != 0 {
action.handler = SIG_IGN;
}
}
Dispositions { actions }
}
fn index(signal: i32) -> Result<usize, Errno> {
if (1..=MAX_SIGNAL).contains(&signal) {
Ok((signal - 1) as usize)
} else {
Err(Errno::EINVAL)
}
}
pub(crate) fn action(&self, signal: i32) -> Result<Action, Errno> {
Ok(self.actions[Self::index(signal)?])
}
pub(crate) fn sigaction(&mut self, signal: i32, new: Option<Action>) -> Result<Action, Errno> {
let index = Self::index(signal)?;
if new.is_some() && sigbit(signal) & UNBLOCKABLE != 0 {
return Err(Errno::EINVAL);
}
let old = self.actions[index];
if let Some(new) = new {
self.actions[index] = new.normalized();
}
Ok(old)
}
pub(crate) fn select_for_delivery(&mut self, signal: i32) -> Result<Action, Errno> {
let index = Self::index(signal)?;
let action = self.actions[index];
if action.is_caught() && action.flags & sa_flag(libc::SA_RESETHAND) != 0 {
self.actions[index].handler = SIG_DFL;
}
Ok(action)
}
pub(crate) fn reset_for_exec(&mut self) {
for action in &mut self.actions {
let handler = if action.handler == SIG_IGN {
SIG_IGN
} else {
SIG_DFL
};
*action = Action {
handler,
..Action::default()
};
}
}
pub(crate) fn ignored(&self) -> Sigset {
self.select(|action| action.handler == SIG_IGN)
}
pub(crate) fn caught(&self) -> Sigset {
self.select(Action::is_caught)
}
fn select(&self, keep: impl Fn(&Action) -> bool) -> Sigset {
self.actions
.iter()
.enumerate()
.filter(|(_, action)| keep(action))
.fold(0, |set, (index, _)| set | (1 << index))
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub(crate) struct ThreadSignals {
pub(crate) blocked: Sigset,
#[serde(default)]
pub(crate) saved: Option<Sigset>,
#[serde(default)]
pub(crate) wait_mask: Option<Sigset>,
#[serde(default)]
pub(crate) activations: Vec<Sigset>,
#[serde(default)]
pub(crate) observed: Vec<bool>,
#[serde(default)]
pub(crate) verify_at_entry: bool,
}
impl ThreadSignals {
pub(crate) fn new(blocked: Sigset) -> Self {
ThreadSignals {
blocked: blocked & !UNBLOCKABLE,
saved: None,
wait_mask: None,
activations: Vec::new(),
observed: Vec::new(),
verify_at_entry: false,
}
}
pub(crate) fn sigprocmask(&mut self, how: i32, set: Option<Sigset>) -> Result<Sigset, Errno> {
let old = self.blocked;
if let Some(set) = set {
let set = set & !UNBLOCKABLE;
self.blocked = match how {
libc::SIG_BLOCK => old | set,
libc::SIG_UNBLOCK => old & !set,
libc::SIG_SETMASK => set,
_ => return Err(Errno::EINVAL),
};
}
Ok(old)
}
pub(crate) fn interrupted_under_temporary_mask(&mut self, mask: Sigset) {
self.settle();
self.saved = Some(self.blocked);
self.blocked = mask & !UNBLOCKABLE;
}
pub(crate) fn settle(&mut self) {
if let Some(saved) = self.saved.take() {
self.blocked = saved;
}
}
#[must_use]
pub(crate) fn enter_handler(&mut self, signal: i32, action: &Action) -> bool {
if self.activations.len() == MAX_OPEN_FRAMES {
return false;
}
let frame = self.saved.take().unwrap_or(self.blocked);
let mut blocked = self.blocked | action.mask;
if action.flags & sa_flag(libc::SA_NODEFER) == 0 {
blocked |= sigbit(signal);
}
self.blocked = blocked & !UNBLOCKABLE;
self.activations.push(frame);
self.observed.push(false);
true
}
pub(crate) fn observe_handler_mask(&mut self) {
if self.activations.last() != Some(&self.blocked)
&& let Some(observed) = self.observed.last_mut()
{
*observed = true;
}
}
pub(crate) fn innermost_observed(&self) -> bool {
self.observed.last().copied().unwrap_or(true)
}
pub(crate) fn sigreturn(&mut self, mask: Sigset) {
self.activations.pop();
self.observed.pop();
self.saved = None;
self.blocked = mask & !UNBLOCKABLE;
}
pub(crate) fn reconcile_returns(&mut self, observed: Sigset, ignore: Sigset) -> bool {
let same = |mask: Sigset| mask & !ignore == observed & !ignore;
if same(self.blocked) {
return true;
}
match self.activations.iter().rposition(|&mask| same(mask)) {
Some(frame) => {
self.blocked = self.activations[frame];
self.activations.truncate(frame);
self.observed.truncate(frame);
true
}
None => false,
}
}
}
pub(crate) fn deliver(
dispositions: &mut Dispositions,
thread: &mut ThreadSignals,
signal: i32,
) -> Result<Option<Action>, Errno> {
let action = dispositions.select_for_delivery(signal)?;
if action.is_caught() {
thread.wait_mask = None;
if !thread.enter_handler(signal, &action) {
return Ok(None);
}
}
Ok(Some(action))
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct SignalShadow {
pub(crate) dispositions: Arc<Mutex<Option<Dispositions>>>,
pub(crate) thread: ThreadSignals,
}
impl SignalShadow {
pub(crate) fn seeded(ignored: Sigset, blocked: Sigset) -> Self {
SignalShadow {
dispositions: Arc::new(Mutex::new(Some(Dispositions::after_exec(ignored)))),
thread: ThreadSignals::new(blocked),
}
}
pub(crate) fn for_clone(&self, flags: u64) -> Option<Self> {
let dispositions = if flags & libc::CLONE_SIGHAND as u64 != 0 {
self.dispositions.lock().unwrap().as_ref()?;
Arc::clone(&self.dispositions)
} else {
let mut copy = self.dispositions.lock().unwrap().clone()?;
if flags & CLONE_CLEAR_SIGHAND != 0 {
copy.reset_for_exec();
}
Arc::new(Mutex::new(Some(copy)))
};
let mut thread = self.thread.clone();
thread.settle();
if !thread.activations.is_empty() {
if flags & libc::CLONE_SIGHAND as u64 != 0 {
self.forget_dispositions();
}
return None;
}
thread.activations.clear();
thread.observed.clear();
Some(SignalShadow {
dispositions,
thread,
})
}
pub(crate) fn exec(&mut self) -> bool {
let Some(mut own) = self.dispositions.lock().unwrap().clone() else {
return false;
};
own.reset_for_exec();
self.dispositions = Arc::new(Mutex::new(Some(own)));
self.thread.settle();
self.thread.activations.clear();
self.thread.observed.clear();
true
}
pub(crate) fn forget_dispositions(&self) {
*self.dispositions.lock().unwrap() = None;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn handler(address: u64, flags: libc::c_int, mask: Sigset) -> Action {
Action {
handler: address,
flags: sa_flag(flags | SA_RESTORER),
restorer: 0x7000,
mask,
}
}
#[test]
fn exec_image_starts_ignored_or_default_and_never_ignores_sigkill() {
let ignored = sigbit(libc::SIGPIPE) | sigbit(libc::SIGKILL);
let table = Dispositions::after_exec(ignored);
assert_eq!(table.ignored(), sigbit(libc::SIGPIPE));
assert_eq!(table.caught(), 0);
}
#[test]
fn sigaction_returns_old_and_stores_normalized() {
let mut table = Dispositions::after_exec(0);
let new = Action {
handler: 0x4000,
flags: sa_flag(libc::SA_SIGINFO | SA_RESTORER) | 0x400 | (1 << 40),
restorer: 0x7000,
mask: sigbit(libc::SIGUSR2) | UNBLOCKABLE,
};
assert_eq!(
table.sigaction(libc::SIGUSR1, Some(new)),
Ok(Action::default())
);
let stored = table.action(libc::SIGUSR1).unwrap();
assert_eq!(
stored.flags,
sa_flag(libc::SA_SIGINFO | SA_RESTORER),
"SA_UNSUPPORTED (0x400) and bits above 31 are not kept"
);
assert_eq!(stored.mask, sigbit(libc::SIGUSR2));
assert_eq!(table.sigaction(libc::SIGUSR1, None), Ok(stored));
assert_eq!(table.caught(), sigbit(libc::SIGUSR1));
}
#[test]
fn sigaction_refuses_sigkill_changes_and_out_of_range_signals() {
let mut table = Dispositions::after_exec(0);
let new = handler(0x4000, 0, 0);
assert_eq!(
table.sigaction(libc::SIGKILL, Some(new)),
Err(Errno::EINVAL)
);
assert_eq!(
table.sigaction(libc::SIGSTOP, Some(new)),
Err(Errno::EINVAL)
);
assert_eq!(table.sigaction(libc::SIGKILL, None), Ok(Action::default()));
assert_eq!(table.sigaction(0, None), Err(Errno::EINVAL));
assert_eq!(table.sigaction(65, None), Err(Errno::EINVAL));
assert_eq!(table.sigaction(64, None), Ok(Action::default()));
}
#[test]
fn exec_reset_keeps_ignored_and_clears_everything_else() {
let mut table = Dispositions::after_exec(0);
table
.sigaction(libc::SIGUSR1, Some(handler(0x4000, libc::SA_RESTART, 3)))
.unwrap();
table
.sigaction(
libc::SIGPIPE,
Some(Action {
handler: SIG_IGN,
flags: sa_flag(libc::SA_RESTART),
restorer: 0x7000,
mask: 3,
}),
)
.unwrap();
table.reset_for_exec();
assert_eq!(table.action(libc::SIGUSR1), Ok(Action::default()));
assert_eq!(
table.action(libc::SIGPIPE),
Ok(Action {
handler: SIG_IGN,
..Action::default()
})
);
}
#[test]
fn resethand_resets_at_selection_and_keeps_flags() {
let mut table = Dispositions::after_exec(0);
let action = handler(0x4000, libc::SA_RESETHAND, 0);
table.sigaction(libc::SIGUSR1, Some(action)).unwrap();
assert_eq!(
table.select_for_delivery(libc::SIGUSR1),
Ok(action.normalized())
);
let after = table.action(libc::SIGUSR1).unwrap();
assert_eq!(after.handler, SIG_DFL);
assert_eq!(after.flags, action.normalized().flags);
}
#[test]
fn sign_extended_flags_from_glibc_keep_only_the_kernel_bits() {
let widened = libc::SA_RESETHAND as i64 as u64;
let mut table = Dispositions::after_exec(0);
let new = Action {
flags: widened | sa_flag(SA_RESTORER),
..handler(0x4000, 0, 0)
};
table.sigaction(libc::SIGUSR1, Some(new)).unwrap();
assert_eq!(
table.action(libc::SIGUSR1).unwrap().flags,
0x8000_0000 | sa_flag(SA_RESTORER)
);
}
#[test]
fn sigprocmask_returns_the_old_mask_and_never_blocks_sigkill() {
let mut thread = ThreadSignals::new(sigbit(libc::SIGUSR1));
assert_eq!(
thread.sigprocmask(libc::SIG_BLOCK, Some(sigbit(libc::SIGKILL) | 4)),
Ok(sigbit(libc::SIGUSR1))
);
assert_eq!(thread.blocked, sigbit(libc::SIGUSR1) | 4);
assert_eq!(
thread.sigprocmask(libc::SIG_UNBLOCK, Some(4)),
Ok(sigbit(libc::SIGUSR1) | 4)
);
assert_eq!(
thread.sigprocmask(libc::SIG_SETMASK, Some(8)),
Ok(sigbit(libc::SIGUSR1))
);
assert_eq!(thread.blocked, 8);
assert_eq!(thread.sigprocmask(99, Some(1)), Err(Errno::EINVAL));
assert_eq!(thread.blocked, 8, "a refused call changes nothing");
assert_eq!(
thread.sigprocmask(99, None),
Ok(8),
"how is ignored without a set"
);
}
#[test]
fn handler_entry_adds_sa_mask_and_the_signal_unless_nodefer() {
let mut thread = ThreadSignals::new(1);
assert!(thread.enter_handler(libc::SIGUSR1, &handler(0x4000, 0, sigbit(libc::SIGUSR2))));
assert_eq!(
thread.blocked,
1 | sigbit(libc::SIGUSR1) | sigbit(libc::SIGUSR2)
);
let mut thread = ThreadSignals::new(1);
assert!(thread.enter_handler(libc::SIGUSR1, &handler(0x4000, libc::SA_NODEFER, 0)));
assert_eq!(thread.blocked, 1);
}
#[test]
fn sigreturn_cannot_block_sigkill() {
let mut thread = ThreadSignals::new(0);
thread.sigreturn(u64::MAX);
assert_eq!(thread.blocked, !UNBLOCKABLE);
}
#[test]
fn delivery_to_a_handler_applies_resethand_and_the_handler_mask() {
let mut dispositions = Dispositions::after_exec(sigbit(libc::SIGPIPE));
let mut thread = ThreadSignals::new(0);
dispositions
.sigaction(
libc::SIGUSR1,
Some(handler(0x4000, libc::SA_RESETHAND, sigbit(libc::SIGUSR2))),
)
.unwrap();
let used = deliver(&mut dispositions, &mut thread, libc::SIGUSR1)
.unwrap()
.unwrap();
assert_eq!(used.handler, 0x4000);
assert_eq!(
thread.blocked,
sigbit(libc::SIGUSR1) | sigbit(libc::SIGUSR2)
);
assert_eq!(dispositions.caught(), 0);
let ignored = deliver(&mut dispositions, &mut thread, libc::SIGPIPE)
.unwrap()
.unwrap();
assert_eq!(ignored.handler, SIG_IGN);
assert_eq!(
thread.blocked,
sigbit(libc::SIGUSR1) | sigbit(libc::SIGUSR2),
"an ignored signal changes no mask"
);
}
#[test]
fn clone_sighand_shares_actions_and_fork_copies_them() {
let parent = SignalShadow::seeded(0, 4);
let thread = parent
.for_clone(libc::CLONE_SIGHAND as u64 | libc::CLONE_VM as u64)
.unwrap();
let mut child = parent.for_clone(0).unwrap();
thread
.dispositions
.lock()
.unwrap()
.as_mut()
.unwrap()
.sigaction(libc::SIGUSR1, Some(handler(0x4000, 0, 0)))
.unwrap();
assert_eq!(
parent
.dispositions
.lock()
.unwrap()
.as_ref()
.unwrap()
.caught(),
sigbit(libc::SIGUSR1)
);
assert_eq!(
child
.dispositions
.lock()
.unwrap()
.as_ref()
.unwrap()
.caught(),
0
);
assert_eq!(child.thread.blocked, 4, "the mask is inherited");
child
.thread
.sigprocmask(libc::SIG_SETMASK, Some(0))
.unwrap();
assert_eq!(parent.thread.blocked, 4, "masks are per thread");
}
#[test]
fn exec_unshares_shared_actions_before_resetting_them() {
let parent = SignalShadow::seeded(0, 0);
let mut sibling = parent
.for_clone(libc::CLONE_SIGHAND as u64 | libc::CLONE_VM as u64)
.unwrap();
parent
.dispositions
.lock()
.unwrap()
.as_mut()
.unwrap()
.sigaction(libc::SIGUSR1, Some(handler(0x4000, 0, 0)))
.unwrap();
assert!(
sibling
.thread
.enter_handler(libc::SIGUSR2, &handler(0x4000, 0, 0))
);
assert!(sibling.exec());
assert_eq!(
sibling
.dispositions
.lock()
.unwrap()
.as_ref()
.unwrap()
.caught(),
0
);
assert!(
sibling.thread.activations.is_empty(),
"exec leaves no frame open"
);
assert_eq!(
parent
.dispositions
.lock()
.unwrap()
.as_ref()
.unwrap()
.caught(),
sigbit(libc::SIGUSR1),
"the peer keeps its handler"
);
}
#[test]
fn shadow_round_trips_through_serde_with_all_64_actions() {
let shadow = SignalShadow::seeded(sigbit(64), 2);
let bytes = serde_json::to_vec(&shadow).unwrap();
let back: SignalShadow = serde_json::from_slice(&bytes).unwrap();
assert_eq!(
*back.dispositions.lock().unwrap(),
*shadow.dispositions.lock().unwrap()
);
assert_eq!(back.thread, shadow.thread);
assert_eq!(
back.dispositions
.lock()
.unwrap()
.as_ref()
.unwrap()
.ignored(),
sigbit(64)
);
let short = serde_json::to_vec(&vec![Action::default(); 63]).unwrap();
assert!(serde_json::from_slice::<Dispositions>(&short).is_err());
}
#[test]
fn unreported_returns_are_recognised_by_their_saved_masks() {
let mut thread = ThreadSignals::new(1);
assert!(thread.enter_handler(libc::SIGUSR1, &handler(0x4000, 0, 0)));
let outer = thread.blocked;
assert!(thread.enter_handler(libc::SIGUSR2, &handler(0x4000, 0, 0)));
assert_eq!(thread.activations, vec![1, outer]);
assert!(thread.reconcile_returns(thread.blocked, 0), "still inside");
assert!(
thread.reconcile_returns(outer, 0),
"the inner handler returned"
);
assert_eq!(thread.activations, vec![1]);
assert!(thread.reconcile_returns(1, 0), "the outer one too");
assert!(thread.activations.is_empty());
assert_eq!(thread.blocked, 1);
assert!(
!thread.reconcile_returns(4, 0),
"an unexplained mask is a mismatch"
);
assert!(
thread.reconcile_returns(1 | 4, 4),
"an ignored bit is left out"
);
assert_eq!(
thread.blocked, 1,
"the copy keeps its own value of an ignored bit"
);
}
#[test]
fn two_returns_at_once_pop_both_frames() {
let mut thread = ThreadSignals::new(0);
assert!(thread.enter_handler(libc::SIGUSR1, &handler(0x4000, 0, 0)));
assert!(thread.enter_handler(libc::SIGUSR2, &handler(0x4000, 0, 0)));
assert!(thread.reconcile_returns(0, 0));
assert!(thread.activations.is_empty());
}
#[test]
fn handler_after_an_interrupted_temporary_mask_wait_saves_the_original_mask() {
let original = sigbit(libc::SIGUSR1) | sigbit(libc::SIGUSR2);
let temporary = sigbit(libc::SIGUSR1);
let mut thread = ThreadSignals::new(original);
thread.interrupted_under_temporary_mask(temporary);
assert_eq!(thread.blocked, temporary);
assert!(thread.enter_handler(libc::SIGINT, &handler(0x4000, 0, 0)));
assert_eq!(thread.blocked, temporary | sigbit(libc::SIGINT));
assert_eq!(thread.saved, None, "the frame now carries the restore");
assert_eq!(thread.activations, vec![original]);
assert!(
thread.reconcile_returns(original, 0),
"its return restores it"
);
assert_eq!(thread.blocked, original);
assert!(thread.activations.is_empty());
}
#[test]
fn temporary_mask_without_a_handler_comes_back_when_the_thread_goes_on() {
let mut thread = ThreadSignals::new(4);
thread.interrupted_under_temporary_mask(8);
thread.settle();
assert_eq!(thread, ThreadSignals::new(4));
thread.interrupted_under_temporary_mask(8);
assert_eq!(
thread.sigprocmask(libc::SIG_BLOCK, None),
Ok(8),
"not settled yet"
);
}
#[test]
fn a_handler_that_changes_its_own_mask_is_still_followed_back() {
let mut thread = ThreadSignals::new(1);
assert!(thread.enter_handler(libc::SIGUSR1, &handler(0x4000, libc::SA_NODEFER, 0)));
assert_eq!(thread.activations, vec![1]);
thread
.sigprocmask(libc::SIG_BLOCK, Some(sigbit(libc::SIGUSR2)))
.unwrap();
assert!(thread.reconcile_returns(1, 0), "its return restores 1");
assert_eq!(thread.blocked, 1);
assert!(thread.activations.is_empty());
}
#[test]
fn frames_beyond_the_limit_give_the_copy_up() {
let mut thread = ThreadSignals::new(0);
for _ in 0..MAX_OPEN_FRAMES {
assert!(thread.enter_handler(libc::SIGUSR1, &handler(0x4000, libc::SA_NODEFER, 0)));
}
assert!(!thread.enter_handler(libc::SIGUSR1, &handler(0x4000, libc::SA_NODEFER, 0)));
assert_eq!(
thread.activations.len(),
MAX_OPEN_FRAMES,
"nothing is dropped silently"
);
}
#[test]
fn a_child_made_inside_a_handler_gets_no_copy() {
let mut parent = SignalShadow::seeded(0, 0);
assert!(
parent
.thread
.enter_handler(libc::SIGUSR1, &handler(0x4000, 0, 0))
);
assert!(parent.for_clone(0).is_none(), "fork");
let vfork = libc::CLONE_VM as u64 | libc::CLONE_VFORK as u64;
assert!(parent.for_clone(vfork).is_none(), "vfork");
assert!(
parent.dispositions.lock().unwrap().is_some(),
"children with copied actions leave the parent's known"
);
let thread = libc::CLONE_VM as u64 | libc::CLONE_SIGHAND as u64 | libc::CLONE_THREAD as u64;
assert!(parent.for_clone(thread).is_none());
assert!(parent.dispositions.lock().unwrap().is_none());
}
#[test]
fn an_ignored_signal_does_not_end_the_wait() {
let mut dispositions = Dispositions::after_exec(sigbit(libc::SIGURG));
let mut thread = ThreadSignals::new(0);
thread.wait_mask = Some(sigbit(libc::SIGUSR1));
deliver(&mut dispositions, &mut thread, libc::SIGURG).unwrap();
assert_eq!(
thread.wait_mask,
Some(sigbit(libc::SIGUSR1)),
"still waiting"
);
dispositions
.sigaction(libc::SIGUSR2, Some(handler(0x4000, 0, 0)))
.unwrap();
deliver(&mut dispositions, &mut thread, libc::SIGUSR2).unwrap();
assert_eq!(thread.wait_mask, None, "a handler ends it");
}
#[test]
fn a_frame_is_observed_only_once_a_check_saw_its_handler_mask() {
let mut thread = ThreadSignals::new(0);
assert!(thread.innermost_observed(), "no open frame needs observing");
assert!(thread.enter_handler(libc::SIGUSR1, &handler(0x4000, 0, 0)));
assert!(!thread.innermost_observed());
thread.observe_handler_mask();
assert!(thread.innermost_observed());
let mut nodefer = ThreadSignals::new(sigbit(libc::SIGUSR1));
assert!(nodefer.enter_handler(libc::SIGUSR1, &handler(0x4000, libc::SA_NODEFER, 0)));
nodefer.observe_handler_mask();
assert!(!nodefer.innermost_observed());
assert!(thread.enter_handler(libc::SIGUSR2, &handler(0x4000, 0, 0)));
assert!(
!thread.innermost_observed(),
"a nested frame starts unobserved"
);
thread.sigreturn(sigbit(libc::SIGUSR1));
assert!(thread.innermost_observed(), "the outer frame was observed");
}
#[test]
fn clone_clear_sighand_resets_caught_signals_and_keeps_ignored_ones() {
let parent = SignalShadow::seeded(sigbit(libc::SIGPIPE), 0);
parent
.dispositions
.lock()
.unwrap()
.as_mut()
.unwrap()
.sigaction(libc::SIGUSR1, Some(handler(0x4000, 0, 0)))
.unwrap();
let child = parent.for_clone(CLONE_CLEAR_SIGHAND).unwrap();
let child = child.dispositions.lock().unwrap();
assert_eq!(child.as_ref().unwrap().caught(), 0);
assert_eq!(child.as_ref().unwrap().ignored(), sigbit(libc::SIGPIPE));
}
#[test]
fn unknown_actions_give_no_clone_and_no_exec() {
let mut shadow = SignalShadow::seeded(0, 0);
shadow.forget_dispositions();
assert!(shadow.for_clone(0).is_none());
assert!(
shadow
.for_clone(libc::CLONE_SIGHAND as u64 | libc::CLONE_VM as u64)
.is_none()
);
assert!(!shadow.exec());
}
}