Merge branch 'doc-signal' into 'master'

Doc signal

See merge request redox-os/relibc!821
This commit is contained in:
Jeremy Soller
2025-12-19 05:45:02 -07:00
+69 -2
View File
@@ -1,4 +1,6 @@
//! signal implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/signal.h.html
//! `signal.h` implementation.
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/signal.h.html>.
use core::{arch::global_asm, mem, ptr};
@@ -8,7 +10,13 @@ use crate::{
c_str::CStr,
error::{Errno, ResultExt},
header::{errno, setjmp, time::timespec},
platform::{self, Pal, PalSignal, Sys, types::*},
platform::{
self, Pal, PalSignal, Sys,
types::{
c_char, c_int, c_ulong, c_ulonglong, c_void, pid_t, pthread_attr_t, pthread_t, size_t,
uid_t,
},
},
};
pub use self::sys::*;
@@ -41,6 +49,7 @@ pub const SIGEV_SIGNAL: c_int = 0;
pub const SIGEV_NONE: c_int = 1;
pub const SIGEV_THREAD: c_int = 2;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/signal.h.html>.
#[repr(C)]
#[derive(Clone, Debug)]
/// cbindgen:ignore
@@ -59,6 +68,7 @@ pub struct sigaltstack {
pub ss_size: size_t,
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/signal.h.html>.
#[repr(C)]
#[derive(Clone)]
pub struct sigevent {
@@ -86,6 +96,7 @@ pub struct siginfo {
#[unsafe(no_mangle)]
pub extern "C" fn _cbindgen_export_siginfo(a: siginfo) {}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/signal.h.html>.
#[derive(Clone, Copy)]
#[repr(C)]
pub union sigval {
@@ -95,9 +106,11 @@ pub union sigval {
/// cbindgen:ignore
pub type sigset_t = c_ulonglong;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/signal.h.html>.
/// cbindgen:ignore
pub type siginfo_t = siginfo;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/signal.h.html>
pub type stack_t = sigaltstack;
#[cfg(target_arch = "aarch64")]
@@ -140,10 +153,13 @@ pub unsafe extern "C" fn siglongjmp(jb: *mut u64, ret: i32) {
setjmp::longjmp(jb, ret);
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/kill.html>.
#[unsafe(no_mangle)]
pub extern "C" fn kill(pid: pid_t, sig: c_int) -> c_int {
Sys::kill(pid, sig).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigqueue.html>.
#[unsafe(no_mangle)]
pub extern "C" fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> c_int {
Sys::sigqueue(pid, sig, val)
@@ -151,11 +167,13 @@ pub extern "C" fn sigqueue(pid: pid_t, sig: c_int, val: sigval) -> c_int {
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/killpg.html>.
#[unsafe(no_mangle)]
pub extern "C" fn killpg(pgrp: pid_t, sig: c_int) -> c_int {
Sys::killpg(pgrp, sig).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_kill.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_kill(thread: pthread_t, sig: c_int) -> c_int {
let os_tid = {
@@ -165,6 +183,7 @@ pub unsafe extern "C" fn pthread_kill(thread: pthread_t, sig: c_int) -> c_int {
crate::header::pthread::e(Sys::rlct_kill(os_tid, sig as usize))
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_sigmask.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_sigmask(
how: c_int,
@@ -180,11 +199,13 @@ pub unsafe extern "C" fn pthread_sigmask(
}
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/raise.html>.
#[unsafe(no_mangle)]
pub extern "C" fn raise(sig: c_int) -> c_int {
Sys::raise(sig).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigaction.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigaction(
sig: c_int,
@@ -196,6 +217,7 @@ pub unsafe extern "C" fn sigaction(
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigaddset.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigaddset(set: *mut sigset_t, signo: c_int) -> c_int {
if signo <= 0 || signo as usize > NSIG.max(SIGRTMAX)
@@ -211,6 +233,7 @@ pub unsafe extern "C" fn sigaddset(set: *mut sigset_t, signo: c_int) -> c_int {
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigaltstack.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigaltstack(ss: *const stack_t, old_ss: *mut stack_t) -> c_int {
Sys::sigaltstack(ss.as_ref(), old_ss.as_mut())
@@ -218,6 +241,7 @@ pub unsafe extern "C" fn sigaltstack(ss: *const stack_t, old_ss: *mut stack_t) -
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigdelset.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigdelset(set: *mut sigset_t, signo: c_int) -> c_int {
if signo <= 0 || signo as usize > NSIG.max(SIGRTMAX)
@@ -233,6 +257,7 @@ pub unsafe extern "C" fn sigdelset(set: *mut sigset_t, signo: c_int) -> c_int {
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigemptyset.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigemptyset(set: *mut sigset_t) -> c_int {
if let Some(set) = (set as *mut SigSet).as_mut() {
@@ -241,6 +266,7 @@ pub unsafe extern "C" fn sigemptyset(set: *mut sigset_t) -> c_int {
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigfillset.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigfillset(set: *mut sigset_t) -> c_int {
if let Some(set) = (set as *mut SigSet).as_mut() {
@@ -249,6 +275,11 @@ pub unsafe extern "C" fn sigfillset(set: *mut sigset_t) -> c_int {
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html>.
///
/// Present in issue 7. Removed in issue 8.
///
/// Use of this function is unspecified in a multi-threaded process.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sighold(sig: c_int) -> c_int {
let mut pset = mem::MaybeUninit::<sigset_t>::uninit();
@@ -260,6 +291,11 @@ pub unsafe extern "C" fn sighold(sig: c_int) -> c_int {
sigprocmask(SIG_BLOCK, &set, ptr::null_mut())
}
/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html>.
///
/// Present in issue 7. Removed in issue 8.
///
/// Use of this function is unspecified in a multi-threaded process.
#[unsafe(no_mangle)]
pub extern "C" fn sigignore(sig: c_int) -> c_int {
let mut psa = mem::MaybeUninit::<sigaction>::uninit();
@@ -270,6 +306,9 @@ pub extern "C" fn sigignore(sig: c_int) -> c_int {
unsafe { sigaction(sig, &mut sa, ptr::null_mut()) }
}
/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/siginterrupt.html>.
///
/// Marked obsolescent in issue 7. Removed in issue 8.
#[unsafe(no_mangle)]
pub extern "C" fn siginterrupt(sig: c_int, flag: c_int) -> c_int {
let mut psa = mem::MaybeUninit::<sigaction>::uninit();
@@ -284,6 +323,7 @@ pub extern "C" fn siginterrupt(sig: c_int, flag: c_int) -> c_int {
unsafe { sigaction(sig, &mut sa, ptr::null_mut()) }
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigismember.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigismember(set: *const sigset_t, signo: c_int) -> c_int {
if signo <= 0 || signo as usize > NSIG {
@@ -299,6 +339,7 @@ pub unsafe extern "C" fn sigismember(set: *const sigset_t, signo: c_int) -> c_in
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/signal.html>.
#[unsafe(no_mangle)]
pub extern "C" fn signal(
sig: c_int,
@@ -318,6 +359,11 @@ pub extern "C" fn signal(
unsafe { old_sa.assume_init() }.sa_handler
}
/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html>.
///
/// Present in issue 7. Removed in issue 8.
///
/// Use of this function is unspecified in a multi-threaded process.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigpause(sig: c_int) -> c_int {
let mut pset = mem::MaybeUninit::<sigset_t>::uninit();
@@ -329,6 +375,7 @@ pub unsafe extern "C" fn sigpause(sig: c_int) -> c_int {
sigsuspend(&set)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigpending.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigpending(set: *mut sigset_t) -> c_int {
(|| Sys::sigpending(set.as_mut().ok_or(Errno(EFAULT))?))()
@@ -340,6 +387,7 @@ const BELOW_SIGRTMIN_MASK: sigset_t = (1 << SIGRTMIN) - 1;
const STANDARD_SIG_MASK: sigset_t = (1 << 32) - 1;
const RLCT_SIGNAL_MASK: sigset_t = BELOW_SIGRTMIN_MASK & !STANDARD_SIG_MASK;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigprocmask.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigprocmask(
how: c_int,
@@ -365,6 +413,11 @@ pub unsafe extern "C" fn sigprocmask(
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html>.
///
/// Present in issue 7. Removed in issue 8.
///
/// Use of this function is unspecified in a multi-threaded process.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigrelse(sig: c_int) -> c_int {
let mut pset = mem::MaybeUninit::<sigset_t>::uninit();
@@ -376,6 +429,11 @@ pub unsafe extern "C" fn sigrelse(sig: c_int) -> c_int {
sigprocmask(SIG_UNBLOCK, &mut set, ptr::null_mut())
}
/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sighold.html>.
///
/// Present in issue 7. Removed in issue 8.
///
/// Use of this function is unspecified in a multi-threaded process.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigset(
sig: c_int,
@@ -419,11 +477,13 @@ pub unsafe extern "C" fn sigset(
old_sa.assume_init().sa_handler
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigsuspend.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigsuspend(sigmask: *const sigset_t) -> c_int {
Err(Sys::sigsuspend(&*sigmask)).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigwait.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigwait(set: *const sigset_t, sig: *mut c_int) -> c_int {
let mut pinfo = mem::MaybeUninit::<siginfo_t>::uninit();
@@ -435,6 +495,7 @@ pub unsafe extern "C" fn sigwait(set: *const sigset_t, sig: *mut c_int) -> c_int
0
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigtimedwait.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigtimedwait(
set: *const sigset_t,
@@ -448,6 +509,8 @@ pub unsafe extern "C" fn sigtimedwait(
.map(|()| 0)
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigwaitinfo.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sigwaitinfo(set: *const sigset_t, sig: *mut siginfo_t) -> c_int {
sigtimedwait(set, sig, core::ptr::null())
@@ -487,6 +550,8 @@ pub(crate) const SIGNAL_STRINGS: [&str; 32] = [
"Power failure\0",
"Bad system call\0",
];
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/psignal.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn psignal(sig: c_int, prefix: *const c_char) {
let c_description = usize::try_from(sig)
@@ -504,6 +569,8 @@ pub unsafe extern "C" fn psignal(sig: c_int, prefix: *const c_char) {
string.as_bytes().len(),
);
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/psiginfo.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn psiginfo(info: *const siginfo_t, prefix: *const c_char) {
let siginfo_t {