Skip to content

Commit

Permalink
SignalValueIterator now handles missing standard signals gracefully i…
Browse files Browse the repository at this point in the history
…nstead of leaping to SIGRTMIN
  • Loading branch information
ChrysoliteAzalea committed Jul 28, 2024
1 parent e6009f8 commit faef524
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
20 changes: 16 additions & 4 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,21 @@ impl Iterator for SignalValueIterator {
s
}
Err(_) => {
if self.next < libc::SIGRTMIN() {
self.next = libc::SIGRTMIN() + 1;
SignalValue::Realtime(0)
} else { return None; }
// Some standard signals seem to be missing on some architectures, to fix that, iterator must skip unrecognized standard signals instead of leaping to SIGRTMIN
while self.next < libc::SIGRTMIN()
{
self.next += 1;
match SignalValue::try_from(self.next)
{
Ok(s) => {
self.next += 1;
return if s.is_valid() { Some(s) } else { None };
},
Err(_) => { continue; },
};
}
self.next = libc::SIGRTMIN() + 1;
SignalValue::Realtime(0)
},
};
if next_signal.is_valid() { Some(next_signal) } else { None }
Expand Down Expand Up @@ -1002,6 +1013,7 @@ impl Hash for SigSet {
/// Iterator for a [`SigSet`].
///
/// Call [`SigSet::iter`] to create an iterator.
#[allow(dead_code)]
#[derive(Clone, Debug)]
#[cfg(not(any(
target_os = "haiku",
Expand Down
20 changes: 15 additions & 5 deletions test/sys/test_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,12 @@ fn test_extend() {
two_signals.add(SIGUSR2);
two_signals.extend(&one_signal);

assert!(two_signals.contains(SIGUSR1));
assert!(two_signals.contains(SIGUSR2));
assert!(one_signal
.rt_contains(nix::sys::signal::SignalValue::Standard(SIGUSR1)));
assert!(two_signals
.rt_contains(nix::sys::signal::SignalValue::Standard(SIGUSR2)));
assert!(two_signals
.rt_contains(nix::sys::signal::SignalValue::Standard(SIGUSR1)));
}

#[test]
Expand All @@ -213,11 +217,15 @@ fn test_extend_rt() {
one_signal.rt_add(SignalValue::Standard(SIGUSR1)).unwrap();

let mut two_signals = SigSet::empty();
one_signal.rt_add(SignalValue::Standard(SIGUSR2)).unwrap();
two_signals.rt_add(SignalValue::Standard(SIGUSR2)).unwrap();
two_signals.extend(&one_signal);

assert!(two_signals.contains(SIGUSR1));
assert!(two_signals.contains(SIGUSR2));
assert!(one_signal
.rt_contains(nix::sys::signal::SignalValue::Standard(SIGUSR1)));
assert!(two_signals
.rt_contains(nix::sys::signal::SignalValue::Standard(SIGUSR2)));
assert!(two_signals
.rt_contains(nix::sys::signal::SignalValue::Standard(SIGUSR1)));
}

#[test]
Expand Down Expand Up @@ -305,6 +313,8 @@ fn test_from_and_into_iterator() {
SignalValue::Standard(Signal::SIGUSR1),
SignalValue::Standard(Signal::SIGUSR2),
]);
assert!(sigset.contains(SIGUSR1));
assert!(sigset.rt_contains(SignalValue::Standard(Signal::SIGUSR2)));
let signals = sigset.into_iter().collect::<Vec<SignalValue>>();
assert_eq!(
signals,
Expand Down

0 comments on commit faef524

Please sign in to comment.