Skip to content

Commit d906002

Browse files
authored
Implement more runtime signal-handling functions. (#949)
Implement `sigpending`, `sigsuspend`, `sigrtmin`, and `sigrtmax`. These are not currently evaluated for use outside of libc implementations, so they're in the `runtime` module.
1 parent dbd3521 commit d906002

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

src/backend/linux_raw/runtime/syscalls.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::backend::c;
99
#[cfg(target_arch = "x86")]
1010
use crate::backend::conv::by_mut;
1111
use crate::backend::conv::{
12-
by_ref, c_int, c_uint, ret, ret_c_int, ret_c_int_infallible, ret_error, ret_void_star, size_of,
13-
zero,
12+
by_ref, c_int, c_uint, ret, ret_c_int, ret_c_int_infallible, ret_error, ret_infallible,
13+
ret_void_star, size_of, zero,
1414
};
1515
#[cfg(feature = "fs")]
1616
use crate::fd::BorrowedFd;
@@ -28,9 +28,9 @@ use core::mem::MaybeUninit;
2828
#[cfg(target_pointer_width = "32")]
2929
use linux_raw_sys::general::__kernel_old_timespec;
3030
use linux_raw_sys::general::kernel_sigset_t;
31-
use linux_raw_sys::prctl::PR_SET_NAME;
3231
#[cfg(target_arch = "x86_64")]
33-
use {crate::backend::conv::ret_infallible, linux_raw_sys::general::ARCH_SET_FS};
32+
use linux_raw_sys::general::ARCH_SET_FS;
33+
use linux_raw_sys::prctl::PR_SET_NAME;
3434

3535
#[inline]
3636
pub(crate) unsafe fn fork() -> io::Result<Fork> {
@@ -198,6 +198,30 @@ pub(crate) unsafe fn sigprocmask(how: How, new: Option<&Sigset>) -> io::Result<S
198198
Ok(old.assume_init())
199199
}
200200

201+
#[inline]
202+
pub(crate) fn sigpending() -> Sigset {
203+
let mut pending = MaybeUninit::<Sigset>::uninit();
204+
unsafe {
205+
ret_infallible(syscall!(
206+
__NR_rt_sigpending,
207+
&mut pending,
208+
size_of::<kernel_sigset_t, _>()
209+
));
210+
pending.assume_init()
211+
}
212+
}
213+
214+
#[inline]
215+
pub(crate) fn sigsuspend(set: &Sigset) -> io::Result<()> {
216+
unsafe {
217+
ret(syscall_readonly!(
218+
__NR_rt_sigsuspend,
219+
by_ref(set),
220+
size_of::<kernel_sigset_t, _>()
221+
))
222+
}
223+
}
224+
201225
#[inline]
202226
pub(crate) fn sigwait(set: &Sigset) -> io::Result<Signal> {
203227
unsafe {

src/runtime.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,28 @@ pub unsafe fn sigprocmask(how: How, set: Option<&Sigset>) -> io::Result<Sigset>
443443
backend::runtime::syscalls::sigprocmask(how, set)
444444
}
445445

446+
/// `sigpending()`—Query the pending signals.
447+
///
448+
/// # References
449+
/// - [Linux `sigpending`]
450+
///
451+
/// [Linux `sigpending`]: https://man7.org/linux/man-pages/man2/sigpending.2.html
452+
#[inline]
453+
pub fn sigpending() -> Sigset {
454+
backend::runtime::syscalls::sigpending()
455+
}
456+
457+
/// `sigsuspend(set)`—Suspend the calling thread and wait for signals.
458+
///
459+
/// # References
460+
/// - [Linux `sigsuspend`]
461+
///
462+
/// [Linux `sigsuspend`]: https://man7.org/linux/man-pages/man2/sigsuspend.2.html
463+
#[inline]
464+
pub fn sigsuspend(set: &Sigset) -> io::Result<()> {
465+
backend::runtime::syscalls::sigsuspend(set)
466+
}
467+
446468
/// `sigwait(set)`—Wait for signals.
447469
///
448470
/// # Safety
@@ -531,3 +553,31 @@ pub fn linux_secure() -> bool {
531553
pub unsafe fn brk(addr: *mut c_void) -> io::Result<*mut c_void> {
532554
backend::runtime::syscalls::brk(addr)
533555
}
556+
557+
/// `__SIGRTMIN`—The start of the realtime signal range.
558+
///
559+
/// This is the raw `SIGRTMIN` value from the OS, which is not the same as
560+
/// the `SIGRTMIN` macro provided by libc. Don't use this unless you are
561+
/// implementing libc.
562+
#[cfg(linux_raw)]
563+
pub const SIGRTMIN: u32 = linux_raw_sys::general::SIGRTMIN;
564+
565+
/// `__SIGRTMAX`—The last of the realtime signal range.
566+
///
567+
/// This is the raw `SIGRTMAX` value from the OS, which is not the same as
568+
/// the `SIGRTMAX` macro provided by libc. Don't use this unless you are
569+
/// implementing libc.
570+
#[cfg(linux_raw)]
571+
pub const SIGRTMAX: u32 = {
572+
// Use the actual `SIGRTMAX` value on platforms which define it.
573+
#[cfg(not(any(target_arch = "arm", target_arch = "x86", target_arch = "x86_64")))]
574+
{
575+
linux_raw_sys::general::SIGRTMAX
576+
}
577+
578+
// On platfoms that don't, derive it from `_NSIG`.
579+
#[cfg(any(target_arch = "arm", target_arch = "x86", target_arch = "x86_64"))]
580+
{
581+
linux_raw_sys::general::_NSIG - 1
582+
}
583+
};

0 commit comments

Comments
 (0)