Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sigsuspend #2279

Merged
merged 23 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions 2279.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a new API sigsuspend.
26 changes: 26 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,32 @@ impl SigSet {
})
}

/// Wait for a signal
///
/// # Return value
/// If `sigsuspend(2)` is interrupted (EINTR), this function returns `Ok`.
/// If `sigsuspend(2)` set other error, this function returns `Err`.
///
/// For more information see the
/// [`sigsuspend(2)`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigsuspend.html).
#[cfg(any(
linux_android,
target_os = "hurd",
target_os = "aix",
target_os = "fushsia"
))]
#[doc(alias("sigsuspend"))]
pub fn suspend(&self) -> Result<()> {
let res = unsafe {
libc::sigsuspend(&self.sigset as *const libc::sigset_t)
};
match Errno::result(res).map(drop) {
Err(Errno::EINTR) => Ok(()),
Err(e) => Err(e),
Ok(_) => unreachable!("because this syscall always returns -1 if returns"),
}
}

/// Converts a `libc::sigset_t` object to a [`SigSet`] without checking whether the
/// `libc::sigset_t` is already initialized.
///
Expand Down
48 changes: 48 additions & 0 deletions test/sys/test_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,54 @@ fn test_sigwait() {
.unwrap();
}

#[cfg(any(
linux_android,
target_os = "hurd",
target_os = "aix",
target_os = "fushsia"
))]
#[test]
fn test_sigsuspend() {
// This test change signal handler
let _m = crate::SIGNAL_MTX.lock();
static SIGNAL_RECIEVED: AtomicBool = AtomicBool::new(false);
extern "C" fn test_sigsuspend_handler(_: libc::c_int) {
assert!(!SIGNAL_RECIEVED.swap(true, Ordering::SeqCst));
}
thread::spawn(|| {
const SIGNAL: Signal = Signal::SIGUSR1;

// Add signal mask to this thread
let mut signal_set = SigSet::empty();
signal_set.add(SIGNAL);
signal_set.thread_block().unwrap();

// Set signal handler and save old one.
let act = SigAction::new(
SigHandler::Handler(test_sigsuspend_handler),
SaFlags::empty(),
SigSet::empty(),
);
let old_act = unsafe { sigaction(SIGNAL, &act) }
.expect("expect to be able to set new action and get old action");

raise(SIGNAL).expect("expect be able to send signal");
// Now `SIGNAL` was sended but it is blocked.
let mut not_wait_set = SigSet::all();
not_wait_set.remove(SIGNAL);
// signal handler must run in SigSet::suspend()
assert!(!SIGNAL_RECIEVED.load(Ordering::SeqCst));
not_wait_set.suspend().unwrap();
assert!(SIGNAL_RECIEVED.load(Ordering::SeqCst));

// Restore the signal handler.
unsafe { sigaction(SIGNAL, &old_act) }
.expect("expect to be able to restore old action ");
})
.join()
.unwrap();
}

#[test]
fn test_from_sigset_t_unchecked() {
let src_set = SigSet::empty();
Expand Down