Skip to main content

c/
signal.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ============================================================
5// signal.h
6// ============================================================
7
8pub const SA_RESTORER: c_ulong = 0x04000000;
9
10pub const SIG_DFL: usize = 0;
11pub const SIG_IGN: usize = 1;
12pub const SIG_ERR: usize = !0usize;
13
14pub const SIGHUP: c_int = 1;
15pub const SIGINT: c_int = 2;
16pub const SIGQUIT: c_int = 3;
17pub const SIGILL: c_int = 4;
18pub const SIGTRAP: c_int = 5;
19pub const SIGABRT: c_int = 6;
20pub const SIGIOT: c_int = 6;
21pub const SIGBUS: c_int = 7;
22pub const SIGFPE: c_int = 8;
23pub const SIGKILL: c_int = 9;
24pub const SIGUSR1: c_int = 10;
25pub const SIGSEGV: c_int = 11;
26pub const SIGUSR2: c_int = 12;
27pub const SIGPIPE: c_int = 13;
28pub const SIGALRM: c_int = 14;
29pub const SIGTERM: c_int = 15;
30pub const SIGSTKFLT: c_int = 16;
31pub const SIGCHLD: c_int = 17;
32pub const SIGCONT: c_int = 18;
33pub const SIGSTOP: c_int = 19;
34pub const SIGTSTP: c_int = 20;
35pub const SIGTTIN: c_int = 21;
36pub const SIGTTOU: c_int = 22;
37pub const SIGURG: c_int = 23;
38pub const SIGXCPU: c_int = 24;
39pub const SIGXFSZ: c_int = 25;
40pub const SIGVTALRM: c_int = 26;
41pub const SIGPROF: c_int = 27;
42pub const SIGWINCH: c_int = 28;
43pub const SIGIO: c_int = 29;
44pub const SIGPOLL: c_int = 29;
45pub const SIGPWR: c_int = 30;
46pub const SIGSYS: c_int = 31;
47pub const SIGUNUSED: c_int = 31;
48pub const _NSIG: c_int = 65;
49
50pub const SIG_BLOCK: c_int = 0;
51pub const SIG_UNBLOCK: c_int = 1;
52pub const SIG_SETMASK: c_int = 2;
53
54pub const SA_NOCLDSTOP: c_ulong = 1;
55pub const SA_NOCLDWAIT: c_ulong = 2;
56pub const SA_SIGINFO: c_ulong = 4;
57pub const SA_ONSTACK: c_ulong = 0x08000000;
58pub const SA_RESTART: c_ulong = 0x10000000;
59pub const SA_NODEFER: c_ulong = 0x40000000;
60pub const SA_RESETHAND: c_ulong = 0x80000000;
61
62pub const SS_ONSTACK: c_int = 1;
63pub const SS_DISABLE: c_int = 2;
64#[cfg(target_arch = "aarch64")]
65pub const MINSIGSTKSZ: usize = 6144;
66#[cfg(target_arch = "aarch64")]
67pub const SIGSTKSZ: usize = 12288;
68#[cfg(target_arch = "powerpc64")]
69pub const MINSIGSTKSZ: usize = 4096;
70#[cfg(target_arch = "powerpc64")]
71pub const SIGSTKSZ: usize = 10240;
72#[cfg(target_arch = "loongarch64")]
73pub const MINSIGSTKSZ: usize = 4096;
74#[cfg(target_arch = "loongarch64")]
75pub const SIGSTKSZ: usize = 16384;
76#[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
77pub const MINSIGSTKSZ: usize = 2048;
78#[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
79pub const SIGSTKSZ: usize = 8192;
80
81pub const SI_USER: c_int = 0;
82pub const SI_TKILL: c_int = -6;
83
84#[repr(C)]
85pub struct timespec {
86    pub tv_sec: c_long,
87    pub tv_nsec: c_long,
88}
89
90#[repr(C)]
91pub struct siginfo_t {
92    pub si_signo: c_int,
93    pub si_errno: c_int,
94    pub si_code: c_int,
95    // The remaining bytes of the 128-byte siginfo_t are not accessed.
96    _pad: [u8; 128 - 3 * core::mem::size_of::<c_int>()],
97}
98
99#[repr(C)]
100pub struct stack_t {
101    pub ss_sp: *mut c_void,
102    pub ss_flags: c_int,
103    pub ss_size: usize,
104}
105
106// Kernel ABI for rt_sigaction.  All supported architectures use the same
107// handler/flags/restorer/8-byte-mask layout.
108#[repr(C)]
109pub struct KSigAction {
110    pub sa_handler: usize,
111    pub sa_flags: c_ulong,
112    pub sa_restorer: usize,
113    pub sa_mask: [c_ulong; 1],
114}
115
116// musl's public layout: handler, 128-byte sigset_t, flags, restorer.
117#[repr(C)]
118pub struct sigaction {
119    pub sa_handler: usize,
120    pub sa_mask: [u64; 16],
121    pub sa_flags: c_int,
122    pub sa_restorer: usize,
123}
124
125pub type SigSetT = c_ulong;
126
127#[cfg(target_arch = "x86_64")]
128core::arch::global_asm!(
129    ".global sig_restorer",
130    ".type sig_restorer, @function",
131    "sig_restorer:",
132    "mov eax, 15",
133    "syscall",
134);
135
136#[cfg(target_arch = "aarch64")]
137core::arch::global_asm!(
138    ".global sig_restorer",
139    ".type sig_restorer, @function",
140    "sig_restorer:",
141    "mov x8, #139",
142    "svc #0",
143);
144
145#[cfg(target_arch = "riscv64")]
146core::arch::global_asm!(
147    ".global sig_restorer",
148    ".type sig_restorer, @function",
149    "sig_restorer:",
150    "li a7, 139",
151    "ecall",
152);
153
154extern "C" {
155    fn sig_restorer();
156}
157
158#[inline]
159unsafe fn sys_rt_sigaction(sig: c_int,
160    act: *const KSigAction,
161    oldact: *mut KSigAction,
162    sigsetsize: usize,) -> i64 {
163    <Arch as Syscalls>::syscall4(SYS_RT_SIGACTION, sig as i64, act as i64, oldact as i64, sigsetsize as i64)
164}
165
166#[inline]
167unsafe fn sys_rt_sigprocmask(how: c_int,
168    set: *const SigSetT,
169    oldset: *mut SigSetT,
170    sigsetsize: usize,) -> i64 {
171    <Arch as Syscalls>::syscall4(SYS_RT_SIGPROCMASK, how as i64, set as i64, oldset as i64, sigsetsize as i64)
172}
173
174#[inline]
175unsafe fn sys_rt_sigpending(set: *mut SigSetT, sigsetsize: usize) -> i64 {
176    <Arch as Syscalls>::syscall2(SYS_RT_SIGPENDING, set as i64, sigsetsize as i64)
177}
178
179#[inline]
180unsafe fn sys_rt_sigsuspend(mask: *const SigSetT, sigsetsize: usize) -> i64 {
181    <Arch as Syscalls>::syscall2(SYS_RT_SIGSUSPEND, mask as i64, sigsetsize as i64)
182}
183
184#[inline]
185unsafe fn sys_rt_sigtimedwait(set: *const SigSetT,
186    info: *mut siginfo_t,
187    timeout: *const timespec,
188    sigsetsize: usize,) -> i64 {
189    <Arch as Syscalls>::syscall4(SYS_RT_SIGTIMEDWAIT, set as i64, info as i64, timeout as i64, sigsetsize as i64)
190}
191
192#[inline]
193unsafe fn sys_sigaltstack(ss: *const stack_t, old_ss: *mut stack_t) -> i64 {
194    <Arch as Syscalls>::syscall2(SYS_SIGALTSTACK, ss as i64, old_ss as i64)
195}
196
197#[inline]
198unsafe fn sys_tgkill(tgid: c_int, tid: c_int, sig: c_int) -> i64 {
199    <Arch as Syscalls>::syscall3(SYS_TGKILL, tgid as i64, tid as i64, sig as i64)
200}
201
202const CLOCK_REALTIME: c_int = 0;
203const CLOCK_MONOTONIC: c_int = 1;
204
205#[inline]
206unsafe fn sys_clock_gettime(clockid: c_int, ts: *mut timespec) -> i64 {
207    <Arch as Syscalls>::syscall2(SYS_CLOCK_GETTIME, clockid as i64, ts as i64)
208}
209
210// clock_gettime lives in vdso.rs: it prefers the kernel's vDSO entry point
211// and only falls back to sys_clock_gettime() there.
212
213#[no_mangle]
214pub unsafe extern "C" fn sigprocmask(
215    how: c_int,
216    set: *const SigSetT,
217    oldset: *mut SigSetT,
218) -> c_int {
219    // mask out internal rt signals (32, 33) before touching the kernel
220    let internal_mask: SigSetT = (1u64 << 31) | (1u64 << 32);
221    let filtered: SigSetT;
222    let set_ptr: *const SigSetT = if set.is_null() {
223        set
224    } else {
225        filtered = *set & !internal_mask;
226        &filtered
227    };
228    let r = sys_rt_sigprocmask(how, set_ptr, oldset, core::mem::size_of::<SigSetT>());
229    if r < 0 {
230        ERRNO!() = (-r) as c_int;
231        return -1;
232    }
233    if !oldset.is_null() {
234        *oldset &= !internal_mask;
235    }
236    0
237}
238
239#[inline]
240unsafe fn sys_kill(pid: c_int, sig: c_int) -> i64 {
241    <Arch as Syscalls>::syscall2(SYS_KILL, pid as i64, sig as i64)
242}
243
244#[inline]
245unsafe fn sys_getpid() -> i64 {
246    <Arch as Syscalls>::syscall0(SYS_GETPID)
247}
248
249#[no_mangle]
250pub unsafe extern "C" fn sigaction(
251    signum: c_int,
252    act: *const sigaction,
253    oldact: *mut sigaction,
254) -> c_int {
255    let mut kact: KSigAction = core::mem::zeroed();
256    let act_ptr = if act.is_null() {
257        core::ptr::null()
258    } else {
259        kact.sa_handler = (*act).sa_handler;
260        kact.sa_flags = ((*act).sa_flags as c_ulong) | SA_RESTORER;
261        kact.sa_restorer = if (*act).sa_flags as c_ulong & SA_RESTORER != 0
262            && (*act).sa_restorer != 0
263        {
264            (*act).sa_restorer
265        } else {
266            sig_restorer as *const () as usize
267        };
268        kact.sa_mask[0] = (*act).sa_mask[0];
269        &kact
270    };
271    let mut kold: KSigAction = core::mem::zeroed();
272    let old_ptr = if oldact.is_null() { core::ptr::null_mut() } else { &mut kold };
273    let r = sys_rt_sigaction(signum, act_ptr, old_ptr, 8);
274    if r < 0 {
275        ERRNO!() = (-r) as c_int;
276        return -1;
277    }
278    if !oldact.is_null() {
279        (*oldact).sa_handler = kold.sa_handler;
280        (*oldact).sa_flags = kold.sa_flags as c_int;
281        (*oldact).sa_restorer = kold.sa_restorer;
282        (*oldact).sa_mask = [0; 16];
283        (*oldact).sa_mask[0] = kold.sa_mask[0];
284    }
285    0
286}
287
288#[no_mangle]
289pub unsafe extern "C" fn signal(signum: c_int, handler: usize) -> usize {
290    // musl's signal() installs the handler with SA_RESTART, so interrupted
291    // syscalls (waitpid, read, ...) are restarted automatically.
292    let act = sigaction {
293        sa_handler: handler,
294        sa_mask: [0; 16],
295        sa_flags: SA_RESTART as c_int,
296        sa_restorer: 0,
297    };
298    let mut old: sigaction = core::mem::zeroed();
299    if sigaction(signum, &act, &mut old) == -1 {
300        SIG_ERR
301    } else {
302        old.sa_handler
303    }
304}
305
306#[no_mangle]
307pub unsafe extern "C" fn kill(pid: c_int, sig: c_int) -> c_int {
308    let r = sys_kill(pid, sig);
309    syscall_ret(r) as c_int
310}
311
312#[no_mangle]
313pub unsafe extern "C" fn tgkill(tgid: c_int, tid: c_int, sig: c_int) -> c_int {
314    let r = sys_tgkill(tgid, tid, sig);
315    syscall_ret(r) as c_int
316}
317
318#[no_mangle]
319pub unsafe extern "C" fn getpid() -> c_int {
320    sys_getpid() as c_int
321}
322
323#[no_mangle]
324pub unsafe extern "C" fn raise(sig: c_int) -> c_int {
325    let r = sys_tgkill(sys_getpid() as c_int, sys_gettid() as c_int, sig);
326    syscall_ret(r) as c_int
327}
328
329#[no_mangle]
330pub unsafe extern "C" fn sigemptyset(set: *mut SigSetT) -> c_int {
331    *set = 0;
332    0
333}
334
335#[no_mangle]
336pub unsafe extern "C" fn sigfillset(set: *mut SigSetT) -> c_int {
337    // musl: all signals except 32 (SIGCANCEL) and 33 (SIGSYNCCALL)
338    *set = 0xFFFF_FFFC_7FFF_FFFFu64;
339    0
340}
341
342#[no_mangle]
343pub unsafe extern "C" fn sigaddset(set: *mut SigSetT, signum: c_int) -> c_int {
344    let s = (signum as c_uint).wrapping_sub(1);
345    if s as usize >= 64 {
346        ERRNO!() = EINVAL;
347        return -1;
348    }
349    // internal rt signals (32, 33) cannot be manipulated
350    if signum >= 32 && signum < __libc_current_sigrtmin() {
351        return 0;
352    }
353    *set |= 1u64 << s;
354    0
355}
356
357#[no_mangle]
358pub unsafe extern "C" fn sigdelset(set: *mut SigSetT, signum: c_int) -> c_int {
359    let s = (signum as c_uint).wrapping_sub(1);
360    if s as usize >= 64 {
361        ERRNO!() = EINVAL;
362        return -1;
363    }
364    // internal rt signals (32, 33) are never members
365    if signum >= 32 && signum < __libc_current_sigrtmin() {
366        return 0;
367    }
368    *set &= !(1u64 << s);
369    0
370}
371
372#[no_mangle]
373pub unsafe extern "C" fn sigismember(set: *const SigSetT, signum: c_int) -> c_int {
374    let s = (signum as c_uint).wrapping_sub(1);
375    if s as usize >= 64 { return 0; }
376    // internal rt signals (32, 33) are never members
377    if signum >= 32 && signum < __libc_current_sigrtmin() {
378        return 0;
379    }
380    ((*set & (1u64 << s)) != 0) as c_int
381}
382
383#[no_mangle]
384pub unsafe extern "C" fn sigpending(set: *mut SigSetT) -> c_int {
385    let r = sys_rt_sigpending(set, core::mem::size_of::<SigSetT>());
386    syscall_ret(r) as c_int
387}
388
389#[no_mangle]
390pub unsafe extern "C" fn sigsuspend(mask: *const SigSetT) -> c_int {
391    let r = sys_rt_sigsuspend(mask, core::mem::size_of::<SigSetT>());
392    if r < 0 {
393        ERRNO!() = (-r) as c_int;
394    }
395    -1
396}
397
398#[no_mangle]
399pub unsafe extern "C" fn sigtimedwait(
400    mask: *const SigSetT,
401    info: *mut siginfo_t,
402    timeout: *const timespec,
403) -> c_int {
404    loop {
405        let r = sys_rt_sigtimedwait(mask, info, timeout, core::mem::size_of::<SigSetT>());
406        if r >= 0 {
407            return r as c_int;
408        }
409        let e = (-r) as c_int;
410        if e != EINTR {
411            ERRNO!() = e;
412            return -1;
413        }
414    }
415}
416
417#[no_mangle]
418pub unsafe extern "C" fn sigwaitinfo(
419    mask: *const SigSetT,
420    info: *mut siginfo_t,
421) -> c_int {
422    sigtimedwait(mask, info, core::ptr::null())
423}
424
425#[no_mangle]
426pub unsafe extern "C" fn sigwait(
427    mask: *const SigSetT,
428    sig: *mut c_int,
429) -> c_int {
430    let mut info: siginfo_t = core::mem::zeroed();
431    if sigtimedwait(mask, &mut info, core::ptr::null()) < 0 {
432        return ERRNO!();
433    }
434    *sig = info.si_signo;
435    0
436}
437
438#[no_mangle]
439pub unsafe extern "C" fn sigaltstack(ss: *const stack_t, old_ss: *mut stack_t) -> c_int {
440    if !ss.is_null() {
441        if (*ss).ss_flags & SS_ONSTACK != 0 {
442            ERRNO!() = EINVAL;
443            return -1;
444        }
445        if (*ss).ss_flags & SS_DISABLE == 0 && (*ss).ss_size < MINSIGSTKSZ {
446            ERRNO!() = ENOMEM;
447            return -1;
448        }
449    }
450    let r = sys_sigaltstack(ss, old_ss);
451    syscall_ret(r) as c_int
452}
453
454include!("setjmp.rs");