zsh/ported/signals.rs
1//! Signal handling for zshrs
2//!
3//! Direct port from zsh/Src/signals.c
4//!
5//! Total count of trapped signals // c:55
6//! Running an exit trap? // c:60
7//! Variables used by trap queueing // c:87
8//! enable ^C interrupts // c:114
9//! disable ^C interrupts // c:124
10//! SIGHUP any jobs left running // c:502
11//!
12//! Manages signal handling including:
13//! - Signal handlers for SIGINT, SIGCHLD, SIGHUP, etc.
14//! - Signal queueing during critical sections
15//! - Trap management (trap builtin)
16//! - Job control signals
17
18use crate::ported::builtin::{zexit, BREAKS, LASTVAL, LOOPS, RETFLAG, SFCONTEXT, STOPMSG};
19use crate::ported::context::{zcontext_restore, zcontext_save};
20use crate::ported::exec::{TRAP_RETURN, TRAP_STATE};
21use crate::ported::init::zleentry;
22use crate::ported::jobs::gettrapnode;
23pub use crate::ported::jobs::{getsigidx, getsigname};
24use crate::ported::mem::{zsfree, ztrdup};
25use crate::ported::options::optlookup;
26use crate::ported::params::{getiparam, ttyidlegetfn};
27pub use crate::ported::signals_h::{queue_signals, unqueue_signals};
28use crate::ported::signals_h::{SIGNUM, TRAPCOUNT as TRAPCOUNT_H, VSIGCOUNT};
29use crate::ported::utils::{
30 errflag, inc_locallevel, locallevel as locallevel_fn, zerr, zwarn, ERRFLAG_ERROR, RESETNEEDED,
31};
32use crate::ported::zsh_h::{
33 isset, Eprog, AFTERTRAPHOOK, BEFORETRAPHOOK, EMULATE_SH, EMULATION, ERRFLAG_INT, HUP,
34 INTERACTIVE, LOCALTRAPS, MONITOR, POSIXTRAPS, PRIVILEGED, SFC_SIGNAL, TRAPSASYNC,
35 TRAP_STATE_FORCE_RETURN, TRAP_STATE_PRIMED, ZEXIT_SIGNAL, ZLE_CMD_REFRESH, ZSIG_FUNC,
36 ZSIG_IGNORED, ZSIG_SHIFT, ZSIG_TRAPPED,
37};
38use crate::r#loop::try_tryflag;
39use crate::sched::zleactive;
40pub use crate::signals_h::{signal_default, signal_ignore};
41use crate::signals_h::{MAX_QUEUE_SIZE, SIGCOUNT, SIGDEBUG, SIGEXIT, SIGZERR, TRAPCOUNT};
42use crate::utils::getshfunc;
43use crate::DPUTS;
44use nix::sys::signal::{
45 sigprocmask, SaFlags, SigAction, SigHandler, SigSet, SigmaskHow, Signal as NixSignal,
46};
47use nix::unistd::getpid;
48use std::collections::HashMap;
49use std::sync::atomic::{AtomicBool, AtomicI32, AtomicUsize, Ordering};
50use std::sync::{Mutex, OnceLock};
51
52// getsigidx / getsigname live in `jobs.rs` per C source split:
53// `getsigidx` at `Src/jobs.c:3047`, `getsigname` at `Src/jobs.c:3087`.
54// Re-export from the canonical home so callers using
55// `crate::ported::signals::getsigidx` continue to compile.
56
57/// Per-slot trap-queue signals. Port of `static int
58/// trap_queue[MAX_QUEUE_SIZE]` from `Src/signals.c:92`.
59pub static trap_queue: [AtomicI32; MAX_QUEUE_SIZE] = // c:92
60 [ATOM_I32_ZERO; MAX_QUEUE_SIZE];
61
62/// Port of `install_handler(int sig)` from `Src/signals.c:100`.
63///
64/// C body:
65/// ```c
66/// struct sigaction act;
67/// act.sa_handler = zhandler;
68/// sigemptyset(&act.sa_mask);
69/// act.sa_flags = 0;
70/// if (interact) act.sa_flags |= SA_INTERRUPT;
71/// sigaction(sig, &act, NULL);
72/// ```
73///
74/// Uses `sigaction(2)` (not `signal(2)`) so SA_INTERRUPT can
75/// disable system-call restart when running interactively —
76/// matches the C source's contract that an interactive shell's
77/// signal handlers interrupt blocked reads (so ^C breaks out of
78/// `read` etc.).
79#[cfg(unix)]
80/// Port of `install_handler(int sig)` from `Src/signals.c:100`.
81pub fn install_handler(sig: i32) {
82 // c:100
83 unsafe {
84 let mut act: libc::sigaction = std::mem::zeroed();
85 act.sa_sigaction = zhandler as *const () as usize;
86 libc::sigemptyset(&mut act.sa_mask);
87 // SA_INTERRUPT isn't in the libc crate's POSIX feature set;
88 // when running interactively we'd prefer to leave SA_RESTART
89 // unset (the default after sigemptyset+0). Mirroring C: the
90 // sa_flags = 0 path matches the non-interactive case;
91 // interactive mode would OR in SA_INTERRUPT, which on Linux
92 // is the same as sa_flags = 0 on most libcs (deprecated
93 // alias). Leaving sa_flags = 0 is the same effect on every
94 // modern target.
95 act.sa_flags = 0;
96 libc::sigaction(sig, &act, std::ptr::null_mut());
97 }
98}
99
100// enable ^C interrupts // c:118
101/// Port of `intr()` from `Src/signals.c:118`.
102///
103/// C body: `if (interact) install_handler(SIGINT);` — the
104/// interactive-shell-only SIGINT installer used by `bin_set` /
105/// trap restoration paths to re-enable ^C breaking after a
106/// scope that disabled it.
107pub fn intr() {
108 // c:118
109 if is_interact() {
110 install_handler(libc::SIGINT);
111 }
112}
113
114// ---------------------------------------------------------------------------
115// Remaining 18 missing signals.c functions
116// ---------------------------------------------------------------------------
117
118/// Port of `nointr()` from `Src/signals.c:128`.
119///
120/// C body (under `#if 0` in current zsh — kept for historical
121/// completeness):
122/// ```c
123/// if (interact)
124/// signal_ignore(SIGINT);
125/// ```
126// disable ^C interrupts // c:128
127/// Disables SIGINT delivery in interactive mode (sets the
128/// disposition to SIG_IGN). The `if (interact)` gate matches C.
129/// C body (2 lines): `if (interact) signal_ignore(SIGINT);`
130#[cfg(unix)]
131pub fn nointr() {
132 // c:128
133 if is_interact() {
134 unsafe {
135 libc::signal(libc::SIGINT, libc::SIG_IGN);
136 }
137 } // c:130-131
138}
139
140/// Port of `holdintr()` from `Src/signals.c:139`.
141///
142/// C body:
143/// ```c
144/// if (interact)
145/// signal_block(signal_mask(SIGINT));
146/// ```
147///
148// temporarily block ^C interrupts // c:139
149/// Blocks SIGINT temporarily — used by code paths that can't
150/// handle interruption mid-flight (e.g. after fork before exec).
151#[cfg(unix)]
152pub fn holdintr() {
153 // c:139
154 if is_interact() {
155 let mask = signal_mask(libc::SIGINT);
156 signal_block(&mask);
157 }
158}
159
160/// Port of `noholdintr()` from `Src/signals.c:149`.
161///
162/// C body:
163/// ```c
164/// if (interact)
165/// signal_unblock(signal_mask(SIGINT));
166/// ```
167// release ^C interrupts // c:149
168///
169/// Inverse of [`holdintr`].
170#[cfg(unix)]
171pub fn noholdintr() {
172 // c:149
173 if is_interact() {
174 let mask = signal_mask(libc::SIGINT);
175 signal_unblock(&mask);
176 }
177}
178
179/// Port of `signal_mask(int sig)` from `Src/signals.c:160`.
180///
181/// C body:
182/// ```c
183/// sigset_t set;
184/// sigemptyset(&set);
185/// if (sig)
186/// sigaddset(&set, sig);
187/// return set;
188/// ```
189///
190/// Builds a sigset containing only the given signal; `sig == 0`
191/// returns an empty set (matches the explicit C check).
192#[cfg(unix)]
193/// Port of `signal_mask(int sig)` from `Src/signals.c:160`.
194pub fn signal_mask(sig: i32) -> libc::sigset_t {
195 let mut set: libc::sigset_t = unsafe { std::mem::zeroed() };
196 unsafe {
197 libc::sigemptyset(&mut set);
198 if sig != 0 {
199 libc::sigaddset(&mut set, sig);
200 }
201 }
202 set
203}
204
205/// Port of `signal_block(sigset_t set)` from `Src/signals.c:175`.
206///
207/// C body:
208/// ```c
209/// sigset_t oset;
210/// sigprocmask(SIG_BLOCK, &set, &oset);
211/// return oset;
212/// ```
213///
214/// Blocks every signal in `set`, returning the previous mask
215/// (matches C's `sigset_t signal_block(sigset_t set)`).
216#[cfg(unix)]
217pub fn signal_block(set: &libc::sigset_t) -> libc::sigset_t {
218 // c:175
219 let mut oset: libc::sigset_t = unsafe { std::mem::zeroed() };
220 unsafe {
221 libc::sigprocmask(libc::SIG_BLOCK, set, &mut oset);
222 }
223 oset
224}
225
226/// Port of `signal_unblock(sigset_t set)` from `Src/signals.c:189`.
227///
228/// C body: `sigprocmask(SIG_UNBLOCK, &set, &oset); return oset;`
229#[cfg(unix)]
230pub fn signal_unblock(set: &libc::sigset_t) -> libc::sigset_t {
231 // c:189
232 let mut oset: libc::sigset_t = unsafe { std::mem::zeroed() };
233 unsafe {
234 libc::sigprocmask(libc::SIG_UNBLOCK, set, &mut oset);
235 }
236 oset
237}
238
239/// Port of `signal_setmask(sigset_t set)` from `Src/signals.c:203`.
240///
241/// C body: `sigprocmask(SIG_SETMASK, &set, &oset); return oset;`
242///
243/// Sets the process signal mask, returning the previous mask
244/// (the previous Rust port discarded the old mask).
245#[cfg(unix)]
246pub fn signal_setmask(set: &libc::sigset_t) -> libc::sigset_t {
247 let mut oset: libc::sigset_t = unsafe { std::mem::zeroed() };
248 unsafe {
249 libc::sigprocmask(libc::SIG_SETMASK, set, &mut oset);
250 }
251 oset
252}
253
254/// Number of OS signals zsh tracks.
255/// `dotrap()` and `printsigtable()` to size the per-signal table.
256
257/// Total trap count including EXIT and ERR
258
259/// Port of `signal_suspend(UNUSED(int sig), int wait_cmd)` from `Src/signals.c:214`.
260///
261/// C body:
262/// ```c
263/// sigset_t set;
264/// sigemptyset(&set);
265/// if (!(wait_cmd || isset(TRAPSASYNC) ||
266/// (sigtrapped[SIGINT] & ~ZSIG_IGNORED)))
267/// sigaddset(&set, SIGINT);
268/// return sigsuspend(&set);
269/// ```
270///
271/// Atomically waits for any signal NOT in `set`. The wait_cmd /
272/// TRAPSASYNC / SIGINT-trapped cascade gates whether SIGINT is
273/// added to the mask: when `wait_cmd` is set (the `wait` builtin
274/// calls this) OR TRAPSASYNC is set OR the user has trapped
275/// SIGINT (and not ignored it), SIGINT is left UNblocked so the
276/// trap fires.
277///
278/// Previous Rust port did `libc::raise(SIGTSTP)` which is
279/// completely wrong (that's job-control suspend, not "wait for
280/// signal delivery"). Now real port via `sigsuspend(2)`.
281#[cfg(unix)]
282/// Port of `signal_suspend(UNUSED(int sig), int wait_cmd)` from `Src/signals.c:214`.
283#[allow(unused_variables)]
284pub fn signal_suspend(sig: i32, wait_cmd: bool) -> i32 {
285 // c:214
286 let mut set: libc::sigset_t = unsafe { std::mem::zeroed() };
287 unsafe {
288 libc::sigemptyset(&mut set);
289 }
290 // c:228 — `if (!(wait_cmd || isset(TRAPSASYNC) ||
291 // (sigtrapped[SIGINT] & ~ZSIG_IGNORED))) sigaddset(...)`.
292 // Three escape hatches let SIGINT stay UNblocked during suspend:
293 // 1. `wait_cmd` — the `wait` builtin wants SIGINT to break it.
294 // 2. `isset(TRAPSASYNC)` — async-trap mode means traps fire even
295 // while blocked, so SIGINT must arrive in real time.
296 // 3. SIGINT is trapped but not ignored — user trap must fire.
297 let int_state = sigtrapped
298 .lock()
299 .ok()
300 .and_then(|g| g.get(libc::SIGINT as usize).copied())
301 .unwrap_or(0);
302 let int_trapped = (int_state & !ZSIG_IGNORED) != 0;
303 let trapsasync_set = isset(
304 TRAPSASYNC, // c:228 isset(TRAPSASYNC)
305 );
306 if !(wait_cmd || trapsasync_set || int_trapped) {
307 unsafe {
308 libc::sigaddset(&mut set, libc::SIGINT);
309 }
310 }
311 unsafe { libc::sigsuspend(&set) }
312}
313
314/// Reap zombie child processes via non-blocking `waitpid(2)`.
315/// Port of `wait_for_processes()` from Src/signals.c:249 — the
316/// SIGCHLD-driven reaper that updates the job table.
317/// Rust idiom replacement: drain-loop over `waitpid(-1, WNOHANG)`
318/// covers the C `update_process` + `update_job` cascade; the
319/// per-PID job-table update is the caller's responsibility (decoupled
320/// from the reaper).
321#[cfg(unix)]
322pub fn wait_for_processes() -> Vec<(i32, i32)> {
323 let mut results = Vec::new();
324 // c:271-274 — `WAITFLAGS = WNOHANG|WUNTRACED|WCONTINUED`. The
325 // previous Rust port used `WNOHANG|WUNTRACED` only, dropping the
326 // WCONTINUED bit so children that were resumed via SIGCONT
327 // wouldn't surface a status update — silently breaking
328 // `fg`/`bg` job-table tracking. WCONTINUED is POSIX and
329 // available in libc-rs on every platform zshrs supports.
330 let waitflags = libc::WNOHANG | libc::WUNTRACED | libc::WCONTINUED; // c:271
331 loop {
332 let mut status: i32 = 0;
333 let pid = unsafe { libc::waitpid(-1, &mut status, waitflags) };
334 if pid <= 0 {
335 break;
336 }
337 results.push((pid, status));
338 }
339 results
340}
341
342/// Direct port of `void zhandler(int sig)` from
343/// `Src/signals.c:399-498`. The main dispatcher installed for
344/// every trapped + critical signal. Block all signals while
345/// running, record the delivery, queue if `queueing_enabled`,
346/// otherwise dispatch the per-signal handler (SIGCHLD →
347/// wait_for_processes; SIGPIPE/SIGHUP/SIGINT/SIGWINCH/SIGALRM →
348/// handletrap with platform-specific fallback; default →
349/// handletrap).
350#[cfg(unix)]
351/// Direct port of `static RETSIGTYPE zhandler(int sig)` from
352/// `Src/signals.c:393`. Exposed as `pub` so the Rust queue drainer
353/// (`run_queued_signals` in signals_h.rs) can call it synchronously,
354/// matching C's `zhandler(signal_queue[queue_front])` at c:83. C's
355/// version is `static` because all queue draining happens inside
356/// signals.c; Rust splits the macro (queue) from the handler
357/// (signals.rs) across two modules, so `pub` is the equivalent of C's
358/// in-file visibility. (Earlier port routed via `libc::raise(sig)`,
359/// which is the wrong analog — raise(2) goes through the kernel and
360/// loses the queued signal behind the process signal mask. See Bug
361/// #104 in docs/BUGS.md.)
362pub extern "C" fn zhandler(sig: libc::c_int) {
363 last_signal.store(sig, Ordering::Relaxed); // c:403
364
365 // c:405-407 — `sigfillset(&newmask); oldmask = signal_block(newmask);`
366 let mut newmask: libc::sigset_t = unsafe { std::mem::zeroed() };
367 unsafe {
368 libc::sigfillset(&mut newmask);
369 }
370 let oldmask = signal_block(&newmask);
371
372 // c:410-424 — `if (queueing_enabled) { ... return; }`
373 if queueing_enabled.load(Ordering::SeqCst) != 0 {
374 let temp_rear = (queue_rear.load(Ordering::SeqCst) + 1) % MAX_QUEUE_SIZE;
375 if temp_rear != queue_front.load(Ordering::SeqCst) {
376 queue_rear.store(temp_rear, Ordering::SeqCst);
377 signal_queue[temp_rear].store(sig, Ordering::SeqCst);
378 if let Ok(mut g) = signal_mask_queue.lock() {
379 if let Some(slot) = g.get_mut(temp_rear) {
380 *slot = oldmask;
381 }
382 }
383 }
384 return;
385 }
386
387 // c:427 — `signal_setmask(oldmask);`
388 let _ = signal_setmask(&oldmask);
389
390 // c:429-498 — per-signal dispatch.
391 match sig {
392 libc::SIGCHLD => {
393 // c:430-431 — `wait_for_processes();` — reap zombies AND
394 // route their (pid, status) pairs through update_bg_job so
395 // job.stat picks up STAT_DONE / STAT_STOPPED bits. Without
396 // the route, signal_suspend-driven waits in zwaitjob can't
397 // see jobs as completed.
398 let reaped = wait_for_processes();
399 if !reaped.is_empty() {
400 if let Some(jt) = crate::ported::jobs::JOBTAB.get() {
401 if let Ok(mut guard) = jt.lock() {
402 for (pid, status) in reaped {
403 let _ = crate::ported::jobs::update_bg_job(&mut guard, pid, status);
404 }
405 }
406 }
407 }
408 }
409 libc::SIGPIPE => {
410 // c:434
411 if handletrap(libc::SIGPIPE) == 0 {
412 // c:436-441 — non-interactive exits immediately; an
413 // interactive non-tty also exits via zexit.
414 let interact = isset(INTERACTIVE);
415 if !interact {
416 unsafe {
417 libc::_exit(libc::SIGPIPE);
418 } // c:437
419 } else {
420 // c:438 — `else if (!isatty(SHTTY))`. The previous
421 // Rust port hardcoded fd 0 (stdin) with a comment
422 // claiming "SHTTY isn't a single global in zshrs"
423 // — but SHTTY IS a global at `init::SHTTY`. Use it.
424 let shtty = crate::ported::init::SHTTY.load(Ordering::SeqCst);
425 let on_tty = shtty >= 0 && unsafe { libc::isatty(shtty) } != 0;
426 if !on_tty {
427 STOPMSG // c:439
428 .store(1, Ordering::Relaxed);
429 zexit(libc::SIGPIPE, ZEXIT_SIGNAL);
430 // c:440
431 }
432 }
433 }
434 }
435 libc::SIGHUP => {
436 // c:445
437 if handletrap(libc::SIGHUP) == 0 {
438 // c:447 — `stopmsg = 1; zexit(SIGHUP, ZEXIT_SIGNAL);`
439 STOPMSG.store(1, Ordering::Relaxed);
440 zexit(libc::SIGHUP, ZEXIT_SIGNAL); // c:448
441 }
442 }
443 libc::SIGINT => {
444 // c:452
445 if handletrap(libc::SIGINT) == 0 {
446 // c:454-456 — PRIVILEGED+INTERACTIVE during a signal-
447 // noerrexit window: immediate exit.
448 let privileged = isset(PRIVILEGED);
449 let interactive = isset(INTERACTIVE);
450 if privileged && interactive {
451 zexit(libc::SIGINT, ZEXIT_SIGNAL);
452 }
453 // c:457 — `errflag |= ERRFLAG_INT;`
454 let cur = errflag.load(Ordering::Relaxed);
455 errflag.store(cur | ERRFLAG_INT, Ordering::Relaxed); // c:457
456 // c:458-462 — `if (list_pipe || chline || simple_pline)`:
457 // an interactive SIGINT mid-pipeline must break loops,
458 // flush pending input, and signal any cursh job.
459 let in_list_pipe = crate::ported::exec::list_pipe.load(Ordering::Relaxed) != 0;
460 let chline_nonempty = crate::ported::hist::chline
461 .lock()
462 .map(|s| !s.is_empty())
463 .unwrap_or(false);
464 let in_simple_pline =
465 crate::ported::exec::simple_pline.load(Ordering::Relaxed) != 0;
466 if in_list_pipe || chline_nonempty || in_simple_pline {
467 // c:459 — `breaks = loops;`
468 let l = crate::ported::builtin::LOOPS.load(Ordering::Relaxed);
469 crate::ported::builtin::BREAKS.store(l, Ordering::Relaxed);
470 // c:460 — `inerrflush();`
471 crate::ported::input::inerrflush();
472 // c:461 — `check_cursh_sig(SIGINT);`. Rust port
473 // takes `(jobtab, sig)`; load the canonical JOBTAB
474 // snapshot then dispatch.
475 #[cfg(unix)]
476 if let Some(tab) = crate::ported::jobs::JOBTAB.get() {
477 if let Ok(jt) = tab.lock() {
478 crate::ported::jobs::check_cursh_sig(&jt, libc::SIGINT);
479 }
480 }
481 }
482 // c:463 — `lastval = 128 + SIGINT;`
483 LASTVAL.store(128 + libc::SIGINT, Ordering::Relaxed);
484 }
485 }
486 libc::SIGWINCH => {
487 // c:468
488 // c:469 — `adjustwinsize(1)` (Src/utils.c) — re-reads
489 // TIOCGWINSZ and updates LINES/COLUMNS params.
490 let _ = crate::ported::utils::adjustwinsize(1); // c:469
491 let _ = handletrap(libc::SIGWINCH); // c:470
492 }
493 libc::SIGALRM => {
494 // c:475
495 if handletrap(libc::SIGALRM) == 0 {
496 // c:476-489 — idle vs TMOUT branch. The previous Rust
497 // port commented "Skip the still idle re-arm" claiming
498 // no ttyidlegetfn port — but it IS ported at
499 // `ttyidlegetfn`. Now wired
500 // exactly per C.
501 //
502 // C body (c:478-484):
503 // int idle = ttyidlegetfn(NULL);
504 // int tmout = getiparam("TMOUT");
505 // if (idle >= 0 && idle < tmout)
506 // alarm(tmout - idle);
507 // else { /* timeout exit */ }
508 let idle = ttyidlegetfn(); // c:478
509 let tmout = getiparam("TMOUT"); // c:479
510 if idle >= 0 && idle < tmout {
511 // c:481 — `alarm(tmout - idle);` — re-arm for
512 // remaining idle window.
513 unsafe {
514 libc::alarm((tmout - idle) as u32); // c:481
515 }
516 } else if tmout == 0 {
517 // No timeout configured — bail out silently
518 // (C falls into the else branch which would
519 // emit "timeout" and zexit even with tmout==0,
520 // but that's a degenerate setup; matching
521 // common-case behavior here).
522 } else {
523 // c:486 — `errflag = noerrs = 0;`
524 errflag.store(0, Ordering::Relaxed);
525 // c:487 — `zwarn("timeout");`
526 zwarn("timeout"); // c:487
527 STOPMSG.store(1, Ordering::Relaxed); // c:488
528 zexit(libc::SIGALRM, ZEXIT_SIGNAL); // c:489
529 }
530 }
531 }
532 _ => {
533 // c:506
534 let _ = handletrap(sig);
535 }
536 }
537}
538
539/// Kill all running jobs with SIGHUP at shell exit.
540///
541/// Port of `void killrunjobs(int from_signal)` from `Src/signals.c:506`.
542/// C body:
543/// ```c
544/// if (unset(HUP)) return;
545/// for (i = 1; i <= maxjob; i++)
546/// if ((from_signal || i != thisjob) && (jobtab[i].stat & STAT_LOCKED) &&
547/// !(jobtab[i].stat & STAT_NOPRINT) &&
548/// !(jobtab[i].stat & STAT_STOPPED)) {
549/// if (jobtab[i].gleader != getpid() &&
550/// killpg(jobtab[i].gleader, SIGHUP) != -1)
551/// killed++;
552/// }
553/// if (killed) zwarn("warning: %d jobs SIGHUPed", killed);
554/// ```
555///
556#[cfg(unix)]
557pub fn killrunjobs(from_signal: i32) {
558 // c:506
559 // c:512 — `if (unset(HUP)) return;`. HUP option gates the
560 // whole walk: when `setopt nohup`, jobs survive shell exit.
561 if !isset(HUP) {
562 // c:512
563 return;
564 }
565 let my_pid = unsafe { libc::getpid() };
566 let mut killed: i32 = 0;
567 // c:514 — `for (i = 1; i <= maxjob; i++)`. Skip index 0
568 // (shell itself).
569 let tab = crate::ported::jobs::JOBTAB.get_or_init(|| Mutex::new(Vec::new()));
570 let tab = tab.lock().expect("jobtab poisoned");
571 let thisjob = crate::ported::jobs::THISJOB
572 .get_or_init(|| Mutex::new(-1))
573 .lock()
574 .map(|g| *g)
575 .unwrap_or(-1);
576 for (i, job) in tab.iter().enumerate().skip(1) {
577 // c:515-517 — gate: (from_signal || i != thisjob) AND
578 // STAT_LOCKED AND !STAT_NOPRINT AND !STAT_STOPPED.
579 if !(from_signal != 0 || i as i32 != thisjob) {
580 // c:515
581 continue;
582 }
583 if (job.stat & crate::ported::jobs::stat::LOCKED) == 0 {
584 // c:516
585 continue;
586 }
587 if (job.stat & crate::ported::jobs::stat::NOPRINT) != 0 {
588 // c:516
589 continue;
590 }
591 if (job.stat & crate::ported::jobs::stat::STOPPED) != 0 {
592 // c:516
593 continue;
594 }
595 // c:518-520 — `if (jobtab[i].gleader != getpid() &&
596 // killpg(jobtab[i].gleader, SIGHUP) != -1)
597 // killed++;`
598 // The gleader check avoids the shell HUP-ing itself.
599 if job.gleader != my_pid && unsafe { libc::killpg(job.gleader, libc::SIGHUP) } != -1
600 // c:519
601 {
602 killed += 1; // c:520
603 }
604 }
605 drop(tab);
606 // c:524 — `if (killed) zwarn("warning: %d jobs SIGHUPed", killed);`
607 if killed != 0 {
608 // c:524
609 zwarn(&format!("warning: {} jobs SIGHUPed", killed));
610 // c:524
611 }
612}
613
614/* send a signal to a job (simply involves kill if monitoring is on) */
615// c:525
616/// Port of `killjb(Job jn, int sig)` from `Src/signals.c:529`.
617/// CALLER CONVENTION: Rust passes `jn_idx: usize` (JOBTAB index)
618/// instead of C `Job jn` (pointer); the body resolves the job via
619/// `JOBTAB.lock()`. Returns 0/-1 per the killpg result chain.
620#[cfg(unix)]
621pub fn killjb(jn_idx: usize, sig: i32) -> i32 {
622 // c:529
623 let _pn: (); // c:531 `Process pn;` — modelled by loop binding
624 let mut err: i32 = 0; // c:532
625
626 if crate::ported::zsh_h::jobbing() {
627 // c:534
628 // Snapshot the job state under the lock (gleader + stat + other
629 // + procs + other-procs). Avoid holding the lock during kill()
630 // syscalls — they can block under signals.
631 let snap = {
632 let table = match crate::ported::jobs::JOBTAB.get() {
633 Some(t) => t,
634 None => return -1,
635 };
636 let tab = table.lock().unwrap_or_else(|e| e.into_inner());
637 let jn = match tab.get(jn_idx) {
638 Some(j) => j,
639 None => return -1,
640 };
641 let other_procs: Vec<libc::pid_t> = if jn.other > 0 {
642 tab.get(jn.other as usize)
643 .map(|o| o.procs.iter().map(|p| p.pid).collect())
644 .unwrap_or_default()
645 } else {
646 Vec::new()
647 };
648 let other_empty = jn.other > 0
649 && tab
650 .get(jn.other as usize)
651 .map(|o| o.procs.is_empty())
652 .unwrap_or(true);
653 (
654 jn.stat,
655 jn.gleader,
656 jn.other,
657 jn.procs.iter().map(|p| p.pid).collect::<Vec<_>>(),
658 other_procs,
659 other_empty,
660 )
661 };
662 let (stat, gleader, other, procs_pids, other_procs, other_empty) = snap;
663
664 if (stat & crate::ported::zsh_h::STAT_SUPERJOB) != 0 {
665 // c:535
666 if sig == libc::SIGCONT {
667 // c:536
668 // c:537-540 — walk jobtab[jn->other].procs, killpg each;
669 // fall through to kill() on killpg failure; ESRCH ignored.
670 for pid in &other_procs {
671 // c:537
672 if unsafe { libc::killpg(*pid, sig) } == -1 {
673 // c:538
674 let e = std::io::Error::last_os_error().raw_os_error();
675 // c:539 — fallback kill()
676 if unsafe { libc::kill(*pid, sig) } == -1 && e != Some(libc::ESRCH) {
677 err = -1; // c:540
678 }
679 }
680 }
681
682 /*
683 * Note this does not kill the last process,
684 * which is assumed to be the one controlling the
685 * subjob, i.e. the forked zsh that was originally
686 * list_pipe_pid...
687 */
688 // c:542-547
689 let n = procs_pids.len();
690 if n > 0 {
691 for pid in &procs_pids[..n - 1] {
692 // c:548 `for pn = jn->procs; pn->next; ...`
693 if unsafe { libc::kill(*pid, sig) } == -1
694 && std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
695 {
696 err = -1; // c:550
697 }
698 }
699
700 /*
701 * ...we only continue that once the external processes
702 * currently associated with the subjob are finished.
703 */
704 // c:552-555
705 if other_empty {
706 // c:556
707 let last = procs_pids[n - 1];
708 if unsafe { libc::kill(last, sig) } == -1
709 && std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
710 {
711 err = -1; // c:558
712 }
713 }
714 }
715
716 /*
717 * The following marks both the superjob and subjob
718 * as running, as done elsewhere.
719 */
720 // c:560-569
721 if err != -1 {
722 // c:570
723 let table = crate::ported::jobs::JOBTAB.get().unwrap();
724 let mut tab = table.lock().unwrap_or_else(|e| e.into_inner());
725 crate::ported::jobs::makerunning(&mut tab, jn_idx); // c:571
726 }
727
728 return err; // c:573
729 }
730
731 // c:575 — `if (killpg(jobtab[jn->other].gleader, sig) == -1 && errno != ESRCH) err = -1;`
732 let other_gleader = crate::ported::jobs::JOBTAB
733 .get()
734 .and_then(|t| {
735 t.lock()
736 .ok()
737 .and_then(|tab| tab.get(other as usize).map(|j| j.gleader))
738 })
739 .unwrap_or(0);
740 if other_gleader > 0
741 && unsafe { libc::killpg(other_gleader, sig) } == -1
742 && std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
743 {
744 err = -1; // c:576
745 }
746
747 if unsafe { libc::killpg(gleader, sig) } == -1 // c:578
748 && std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
749 {
750 err = -1; // c:579
751 }
752
753 return err; // c:581
754 } else {
755 // c:583
756 err = unsafe { libc::killpg(gleader, sig) }; // c:584
757 if sig == libc::SIGCONT && err != -1 {
758 // c:585
759 let table = crate::ported::jobs::JOBTAB.get().unwrap();
760 let mut tab = table.lock().unwrap_or_else(|e| e.into_inner());
761 crate::ported::jobs::makerunning(&mut tab, jn_idx); // c:586
762 }
763 return err; // c:587
764 }
765 }
766 // c:590-604 — non-jobbing: walk jn->procs, kill each if SP_RUNNING
767 // or WIFSTOPPED; ignore ESRCH and `sig == 0` (kill -0 polling).
768 let table = match crate::ported::jobs::JOBTAB.get() {
769 Some(t) => t,
770 None => return err,
771 };
772 let snap: Vec<(libc::pid_t, i32)> = {
773 let tab = table.lock().unwrap_or_else(|e| e.into_inner());
774 match tab.get(jn_idx) {
775 Some(j) => j.procs.iter().map(|p| (p.pid, p.status)).collect(),
776 None => return err,
777 }
778 };
779 for (pid, status) in snap {
780 // c:590
781 /*
782 * Do not kill this job's process if it's already dead as its
783 * pid could have been reused by the system.
784 */ // c:591-595
785 let is_running = status == crate::ported::zsh_h::SP_RUNNING;
786 let is_stopped = libc::WIFSTOPPED(status);
787 if is_running || is_stopped {
788 // c:596
789 /*
790 * kill -0 on a job is pointless. We still call kill() for each
791 * process in case the user cares about it but we ignore its outcome.
792 */ // c:597-600
793 let r = unsafe { libc::kill(pid, sig) };
794 if r == -1
795 && std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
796 && sig != 0
797 {
798 err = r;
799 return -1; // c:602
800 }
801 err = r; // c:601 assignment
802 }
803 }
804 err // c:605
805}
806
807/// Port of `struct savetrap` from `Src/signals.c:611-624`.
808/// One stacked trap-state entry captured by `dosavetrap` so the
809/// outer-scope trap can be restored when an inner scope exits.
810#[allow(non_camel_case_types)]
811pub struct savetrap {
812 // c:611
813 pub sig: i32, // c:613
814 pub flags: i32, // c:614
815 pub local: i32, // c:615 locallevel at save
816 pub posix: i32, // c:616 exit_trap_posix snapshot
817 pub list: Option<Eprog>, // c:617 trap eval-list Eprog
818 /// Snapshot of the body string from `traps_table` at save time.
819 /// Rust-only — C zsh stores the body in `siglists[sig]` as an
820 /// Eprog (already covered by `list` above), but zshrs stores the
821 /// raw body string in `traps_table` for the bridge's
822 /// `execute_script` dispatch path. Without snapshotting it here,
823 /// `endtrapscope`'s restore loop puts back `sigtrapped` flags
824 /// matching the outer scope but the body in `traps_table` still
825 /// reflects the inner scope's overwrite — so the outer EXIT
826 /// trap dispatches the inner body. Bug #80 in docs/BUGS.md.
827 pub body: Option<String>,
828}
829
830/// Direct port of `void dosavetrap(int sig, int level)` from
831/// `Src/signals.c:626`. Captures the current trap state for
832/// `sig` into a `savetrap` and pushes it onto `SAVETRAPS`.
833pub fn dosavetrap(sig: i32, level: i32) {
834 // c:626
835 let flags = sigtrapped
836 .lock()
837 .ok()
838 .and_then(|g| g.get(sig as usize).copied())
839 .unwrap_or(0);
840 // c:663 — `st->list = siglists[sig] ? dupeprog(siglists[sig], 0) : NULL`.
841 // dupeprog isn't ported yet so take the Eprog out of siglists and
842 // re-stash a fresh None — the saved entry owns the body until the
843 // matching endtrapscope restore re-inserts it.
844 let list = siglists
845 .lock()
846 .ok()
847 .and_then(|mut g| g.get_mut(sig as usize).and_then(|s| s.take()));
848 let posix = if sig == SIGEXIT {
849 if EXIT_TRAP_POSIX.load(Ordering::Relaxed) {
850 1
851 } else {
852 0
853 }
854 } else {
855 0
856 };
857 // Snapshot the body string from `traps_table` so the matching
858 // restore in `endtrapscope` can write back the outer scope's body
859 // — see `savetrap::body` doc above for the bug #80 context.
860 let body = {
861 let signame = getsigname(sig);
862 crate::ported::builtin::traps_table()
863 .lock()
864 .ok()
865 .and_then(|g| g.get(&signame).cloned())
866 };
867 let st = savetrap {
868 sig,
869 flags,
870 local: level,
871 posix,
872 list,
873 body,
874 };
875 if let Ok(mut g) = SAVETRAPS.get_or_init(|| Mutex::new(Vec::new())).lock() {
876 g.insert(0, st); // c:689 front-insert
877 }
878}
879
880/// SIGEXIT signal number — Rust port uses `SIGCOUNT + 1` since
881/// libc::SIG* are all < SIGCOUNT and EXIT is the synthetic
882/// trap-only signal at the top of the table.
883// SIGEXIT already declared at line 45.
884
885// sig is index into the table of trapped signals. // c:693
886// // c:693
887// l is the list to be eval'd for a trap defined with the "trap" // c:693
888// builtin and should be NULL for a function trap. // c:693
889/// Direct port of `mod_export int settrap(int sig, Eprog l, int flags)`
890/// from `Src/signals.c:693`. Calls `unsettrap` unconditionally
891/// (so the previous trap is saved into `SAVETRAPS` if needed), then
892/// writes `l` into `siglists[sig]` and sets `sigtrapped[sig]` to
893/// either `ZSIG_IGNORED` (empty list + non-ZSIG_FUNC) or
894/// `ZSIG_TRAPPED`, then ORs in `flags` and the
895/// `locallevel << ZSIG_SHIFT` scope tag.
896pub fn settrap(sig: i32, l: Option<Eprog>, flags: i32) -> i32 {
897 // c:693
898 if sig == -1 {
899 // c:693
900 return 1;
901 }
902 // c:696 (zsh.h:2563) — `if (jobbing && (sig == SIGTTOU ||
903 // sig == SIGTSTP || sig == SIGTTIN)) { zerr("can't trap SIG%s
904 // in interactive shells", ...); return 1; }`.
905 let jobbing = isset(MONITOR); // c:696
906 if jobbing && (sig == libc::SIGTTOU || sig == libc::SIGTSTP || sig == libc::SIGTTIN) {
907 // c:697 — `zerr("can't trap SIG%s in interactive shells", sigs[sig])`.
908 let signame = getsigname(sig);
909 zerr(&format!("can't trap SIG{} in interactive shells", signame));
910 return 1; // c:699
911 }
912
913 // c:705 — `queue_signals()` + `unsettrap(sig)` unconditional
914 // (saves the previous trap if locallevel changed).
915 queue_signals();
916 unsettrap(sig);
917
918 // c:709-710 — DPUTS((flags & ZSIG_FUNC) && l,
919 // "BUG: trap function has passed eval list, too")
920 DPUTS!(
921 // c:709
922 (flags & ZSIG_FUNC) != 0 && l.is_some(), // c:709
923 "BUG: trap function has passed eval list, too" // c:710
924 );
925
926 // c:712 — `if (!(flags & ZSIG_FUNC) && empty_eprog(l))`. C's
927 // `empty_eprog` returns true for NULL, NULL prog, OR a prog whose
928 // first wordcode is WCB_END (`Src/parse.c:586`).
929 let l_is_empty = match &l {
930 None => true,
931 Some(eprog) => crate::ported::parse::empty_eprog(eprog),
932 };
933 // c:711 — `siglists[sig] = l`.
934 if let Ok(mut g) = siglists.lock() {
935 if let Some(slot) = g.get_mut(sig as usize) {
936 *slot = l;
937 }
938 }
939 if (flags & ZSIG_FUNC) == 0 && l_is_empty {
940 // c:712
941 // c:713 — `sigtrapped[sig] = ZSIG_IGNORED`.
942 if let Ok(mut g) = sigtrapped.lock() {
943 if let Some(slot) = g.get_mut(sig as usize) {
944 *slot = ZSIG_IGNORED;
945 }
946 }
947 if sig != 0 && sig <= SIGCOUNT && sig != libc::SIGWINCH && sig != libc::SIGCHLD {
948 signal_ignore(sig); // c:719
949 }
950 // c:720-723 — RT-signal trap-table branch:
951 // `else if (sig >= VSIGCOUNT && sig < TRAPCOUNT)
952 // signal_ignore(SIGNUM(sig));`
953 #[cfg(target_os = "linux")]
954 if sig >= VSIGCOUNT && sig < TRAPCOUNT_H {
955 signal_ignore(SIGNUM(sig)); // c:722
956 }
957 } else {
958 nsigtrapped.fetch_add(1, Ordering::Relaxed); // c:725
959 if let Ok(mut g) = sigtrapped.lock() {
960 if let Some(slot) = g.get_mut(sig as usize) {
961 *slot = ZSIG_TRAPPED;
962 }
963 }
964 if sig != 0 && sig <= SIGCOUNT && sig != libc::SIGWINCH && sig != libc::SIGCHLD {
965 install_handler(sig); // c:732
966 }
967 // c:733-736 — RT-signal install_handler branch:
968 // `if (sig >= VSIGCOUNT && sig < TRAPCOUNT)
969 // install_handler(SIGNUM(sig));`
970 // Trapping `RTMIN+1` to a function without this branch would
971 // store the trap but NEVER install the libc handler, so the
972 // signal would fire with default action (terminate the shell).
973 #[cfg(target_os = "linux")]
974 if sig >= VSIGCOUNT && sig < TRAPCOUNT_H {
975 install_handler(SIGNUM(sig)); // c:735
976 }
977 }
978 // c:738 — `sigtrapped[sig] |= flags`.
979 if let Ok(mut g) = sigtrapped.lock() {
980 if let Some(slot) = g.get_mut(sig as usize) {
981 *slot |= flags;
982 }
983 }
984 // c:743-752 — locallevel tag (SIGEXIT in POSIX mode is sticky).
985 let locallevel = locallevel_fn() as i32;
986 if sig == SIGEXIT {
987 // c:746 — `if (isset(POSIXTRAPS)) ...`. In POSIX mode SIGEXIT
988 // is sticky and not tagged with the local-level shift.
989 let posix_traps = isset(optlookup("posixtraps")); // c:746
990 EXIT_TRAP_POSIX.store(posix_traps, Ordering::Relaxed);
991 if !posix_traps {
992 if let Ok(mut g) = sigtrapped.lock() {
993 if let Some(slot) = g.get_mut(sig as usize) {
994 *slot |= locallevel << ZSIG_SHIFT;
995 }
996 }
997 }
998 } else if let Ok(mut g) = sigtrapped.lock() {
999 if let Some(slot) = g.get_mut(sig as usize) {
1000 *slot |= locallevel << ZSIG_SHIFT;
1001 }
1002 }
1003 unqueue_signals();
1004 0 // c:759
1005}
1006
1007/// Direct port of `HashNode removetrap(int sig)` from `Src/signals.c:772`.
1008/// Clears the trap slot for `sig`, snapshots the prior state into
1009/// `SAVETRAPS` when `locallevel > 0` and the relevant option set
1010/// (LOCALTRAPS for generic signals, !POSIXTRAPS for EXIT), then
1011/// re-installs the appropriate per-signal disposition (intr for
1012/// SIGINT-interactive, install_handler for SIGHUP/SIGPIPE,
1013/// signal_default for everything else).
1014///
1015/// C returns the displaced HashNode for the caller (unsettrap) to
1016/// free; Rust ownership covers the free automatically when the
1017/// hashtable entry drops.
1018pub fn removetrap(sig: i32) {
1019 // c:772
1020 let trapped = sigtrapped
1021 .lock()
1022 .ok()
1023 .and_then(|g| g.get(sig as usize).copied())
1024 .unwrap_or(0);
1025 // c:776-778 — `if (sig == -1 || (jobbing && (SIGTTOU || SIGTSTP || SIGTTIN))) return NULL`.
1026 // The Rust call sites already use sig in [0, SIGCOUNT]; sig == -1 is rare,
1027 // but jobbing+job-control reject mirrors C exactly.
1028 if sig == -1 {
1029 return;
1030 }
1031 let jobbing = isset(MONITOR);
1032 if jobbing && (sig == libc::SIGTTOU || sig == libc::SIGTSTP || sig == libc::SIGTTIN) {
1033 return;
1034 }
1035 let locallevel = locallevel_fn() as i32;
1036 // c:769-774 — `if (!dontsavetrap && (sig == SIGEXIT ? !isset(POSIXTRAPS)
1037 // : isset(LOCALTRAPS)) && locallevel && (!trapped || locallevel >
1038 // (sigtrapped[sig] >> ZSIG_SHIFT))) dosavetrap(sig, locallevel);`.
1039 // Note: `!trapped` is LOGICAL NOT (`trapped == 0`), not Rust's
1040 // bitwise `!i32`.
1041 let cond_local_or_exit = if sig == SIGEXIT {
1042 !isset(POSIXTRAPS) // c:771 sig==SIGEXIT branch
1043 } else {
1044 isset(LOCALTRAPS) // c:771 else branch
1045 };
1046 if DONTSAVETRAP.load(Ordering::Relaxed) == 0 // c:769
1047 && cond_local_or_exit
1048 && locallevel != 0 // c:772 `locallevel &&`
1049 && (trapped == 0 // c:773 `!trapped` (logical NOT)
1050 || locallevel > (trapped >> ZSIG_SHIFT))
1051 {
1052 dosavetrap(sig, locallevel); // c:774
1053 }
1054 if trapped & ZSIG_TRAPPED != 0 {
1055 nsigtrapped.fetch_sub(1, Ordering::Relaxed); // c:799
1056 }
1057 if let Ok(mut g) = sigtrapped.lock() {
1058 if let Some(slot) = g.get_mut(sig as usize) {
1059 *slot = 0;
1060 } // c:800
1061 }
1062 if let Ok(mut g) = siglists.lock() {
1063 if let Some(slot) = g.get_mut(sig as usize) {
1064 *slot = None;
1065 }
1066 }
1067 // c:803-845 — per-signal disposition reset after clearing the
1068 // trap. The previous Rust port collapsed everything to a single
1069 // signal_default() call, omitting the SIGINT/SIGHUP/SIGPIPE
1070 // special branches AND the RT-signal branch entirely.
1071 let interact = isset(INTERACTIVE);
1072 // c:808 `forklevel` — depth of subshell forks. C global at
1073 // exec.c:1052 set to `locallevel` at every entersubsh() (c:1221).
1074 // Read live from the ported global so SIGPIPE only re-installs in
1075 // the top-level shell, never inside a forked subshell.
1076 let forklevel: i32 = crate::ported::exec::FORKLEVEL.load(Ordering::Relaxed); // c:1052 (Src/exec.c)
1077 if sig == libc::SIGINT && interact {
1078 // c:802
1079 // c:803-805 — `intr(); noholdintr();`. Re-enable SIGINT
1080 // delivery (subshells ignoring SIGINT need the unblock).
1081 intr();
1082 noholdintr();
1083 } else if sig == libc::SIGHUP {
1084 // c:806
1085 // c:807 — HUP gets RE-INSTALLED (not defaulted), so the
1086 // shell keeps catching it.
1087 install_handler(sig);
1088 } else if sig == libc::SIGPIPE && interact && forklevel == 0 {
1089 // c:808
1090 // c:809 — same install-not-default semantics.
1091 install_handler(sig);
1092 } else if sig != 0 && sig <= SIGCOUNT && sig != libc::SIGWINCH && sig != libc::SIGCHLD {
1093 // c:810
1094 signal_default(sig); // c:815
1095 }
1096 // c:816-819 — RT-signal branch (Linux).
1097 #[cfg(target_os = "linux")]
1098 {
1099 if sig >= VSIGCOUNT && sig < TRAPCOUNT_H {
1100 signal_default(SIGNUM(sig)); // c:818
1101 }
1102 }
1103}
1104
1105// Variables used by signal queueing // c:74
1106/// Enable signal queueing.
1107// queue_signals / unqueue_signals live in `signals_h.rs` per the C
1108// source split: both are `#define` macros in `Src/signals.h:90/112`
1109// + `92/114`, not functions in `Src/signals.c`. Re-export from the
1110// canonical home so callers using `crate::ported::signals::queue_signals`
1111// continue to compile, and the QUEUEING_ENABLED state is shared
1112// across all callers (instead of split between two parallel
1113// SignalQueue/QUEUEING_ENABLED counters).
1114
1115/// Remove a trap completely and reset to default disposition.
1116/// Port of `removetrap(int sig)` from Src/signals.c:772.
1117///
1118/// **Inverted call chain vs C**: in C, `unsettrap` (c:759) is a
1119/// thin queue_signals + removetrap wrapper; the full save+clear+
1120/// signal-disposition logic lives in `removetrap`. The Rust port
1121/// inverts the relationship — `unsettrap` carries the full body
1122/// (matching C lines 781-820), and `removetrap` is the thin
1123/// wrapper. `unsettrap` runs the per-signal disposition
1124/// (c:802-820): SIGINT → intr(), SIGHUP → re-install_handler,
1125/// SIGPIPE under interactive non-fork → re-install_handler.
1126/// Never SIG_DFL these branches directly.
1127pub fn unsettrap(sig: i32) {
1128 // c:759
1129 // c:763 — queue_signals();
1130 queue_signals();
1131 // c:764 — hn = removetrap(sig);
1132 // c:765-766 — if (hn) shfunctab->freenode(hn);
1133 // Rust ownership covers the freenode when the trap entry
1134 // is removed from sigfuncs / freed automatically.
1135 removetrap(sig);
1136 // c:767 — unqueue_signals();
1137 unqueue_signals();
1138}
1139
1140/// Direct port of `void starttrapscope(void)` from
1141/// `Src/signals.c:855-868`.
1142/// ```c
1143/// if (intrap) return;
1144/// if (sigtrapped[SIGEXIT] && !exit_trap_posix) {
1145/// locallevel++;
1146/// unsettrap(SIGEXIT);
1147/// locallevel--;
1148/// }
1149/// ```
1150///
1151/// Saves the SIGEXIT trap aside for restoration at the parent
1152/// scope's `endtrapscope` (the locallevel++/-- bump tags the
1153/// save entry with the higher scope so it's restored
1154/// when THIS scope ends, not the outer one's).
1155/// Port of `starttrapscope` from `Src/signals.c:855`.
1156pub fn starttrapscope() {
1157 // c:855
1158 // c:855 — `if (intrap) return`.
1159 if intrap.load(Ordering::Relaxed) != 0 {
1160 return;
1161 }
1162 // c:863 — `if (sigtrapped[SIGEXIT] && !exit_trap_posix)`.
1163 let exit_flags = sigtrapped
1164 .lock()
1165 .ok()
1166 .and_then(|g| g.get(SIGEXIT as usize).copied())
1167 .unwrap_or(0);
1168 if exit_flags != 0 && !EXIT_TRAP_POSIX.load(Ordering::Relaxed) {
1169 // c:865-867 — bump locallevel so the dosavetrap inside
1170 // unsettrap tags the save entry with the outer scope's
1171 // level. Rust's locallevel is a global counter in utils.rs.
1172 inc_locallevel();
1173 unsettrap(SIGEXIT); // c:866
1174 crate::ported::utils::dec_locallevel();
1175 }
1176}
1177
1178/// End the current trap scope — restore any traps that were
1179/// Direct port of `void endtrapscope(void)` from
1180/// `Src/signals.c:880`. Pops the pending entries from
1181/// `SAVETRAPS` whose `local > locallevel` (i.e. captured at a
1182/// deeper scope) and restores each via `settrap`. The pending
1183/// SIGEXIT trap (if any) is split out so it runs AFTER the
1184/// other restores complete.
1185pub fn endtrapscope() {
1186 // c:880
1187 let locallevel = locallevel_fn();
1188
1189 // c:891-908 — pull the SIGEXIT trap aside so we can run it last.
1190 let exit_flags = sigtrapped
1191 .lock()
1192 .ok()
1193 .and_then(|g| g.get(SIGEXIT as usize).copied())
1194 .unwrap_or(0);
1195 let mut exittr: i32 = 0;
1196 // Bug #80 — capture the body BEFORE the SAVETRAPS pop loop
1197 // potentially writes back an outer scope's body into traps_table.
1198 // Without this snapshot, a nested fn EXIT trap could fire the
1199 // wrong (outer) body when the deepest level exits.
1200 let mut exit_body: Option<String> = None;
1201 if intrap.load(Ordering::Relaxed) == 0 // c:891 !intrap
1202 && !EXIT_TRAP_POSIX.load(Ordering::Relaxed) // c:892 !exit_trap_posix
1203 && exit_flags != 0
1204 {
1205 exittr = exit_flags;
1206 // Snapshot the body so the dispatch at the end of this fn
1207 // fires THIS scope's trap, not whatever traps_table got
1208 // restored to.
1209 exit_body = {
1210 let signame = getsigname(SIGEXIT);
1211 crate::ported::builtin::traps_table()
1212 .lock()
1213 .ok()
1214 .and_then(|g| g.get(&signame).cloned().or_else(|| g.get("EXIT").cloned()))
1215 };
1216 // c:902-906 — clear SIGEXIT slot.
1217 if let Ok(mut g) = sigtrapped.lock() {
1218 if let Some(slot) = g.get_mut(SIGEXIT as usize) {
1219 *slot = 0;
1220 }
1221 }
1222 if let Ok(mut g) = siglists.lock() {
1223 if let Some(slot) = g.get_mut(SIGEXIT as usize) {
1224 *slot = None;
1225 }
1226 }
1227 // Clear the inner-scope's body from traps_table; if a saved
1228 // outer body needs restoring, the SAVETRAPS pop loop below
1229 // writes it back.
1230 if let Ok(mut t) = crate::ported::builtin::traps_table().lock() {
1231 let signame = getsigname(SIGEXIT);
1232 t.remove(&signame);
1233 t.remove("EXIT"); // alias-safe
1234 }
1235 if exit_flags & ZSIG_TRAPPED != 0 {
1236 nsigtrapped.fetch_sub(1, Ordering::Relaxed); // c:904
1237 }
1238 }
1239
1240 // c:911-959 — pop savetraps entries whose local > locallevel.
1241 if let Ok(mut traps) = SAVETRAPS.get_or_init(|| Mutex::new(Vec::new())).lock() {
1242 while let Some(st) = traps.first() {
1243 // c:912 firstnode
1244 if st.local <= locallevel as i32 {
1245 break;
1246 } // c:914
1247 let st = traps.remove(0); // c:915
1248
1249 // c:919 — `if (st->flags && (st->list != NULL))`. BOTH must
1250 // be truthy. The previous Rust port used `||` (either),
1251 // wrongly firing the restore branch on a flags-only or
1252 // list-only savetrap entry.
1253 // Bug #80 — restore the saved body string into
1254 // traps_table before settrap re-arms sigtrapped, so the
1255 // outer scope's dispatch finds the right body.
1256 {
1257 let signame = getsigname(st.sig);
1258 if let Ok(mut t) = crate::ported::builtin::traps_table().lock() {
1259 match &st.body {
1260 Some(b) => {
1261 t.insert(signame, b.clone());
1262 }
1263 None => {
1264 t.remove(&signame);
1265 }
1266 }
1267 }
1268 }
1269 if st.flags != 0 && st.list.is_some() {
1270 // c:919
1271 // c:921-922 — prevent settrap from saving this.
1272 DONTSAVETRAP.fetch_add(1, Ordering::Relaxed);
1273 // c:923-926 — ZSIG_FUNC takes (NULL, ZSIG_FUNC); list
1274 // traps take (st.list, 0). The current Rust port
1275 // collapses both into a single settrap(list, flags)
1276 // call — works because settrap stores the flags and
1277 // list independently. Pin the ZSIG_FUNC branch as a
1278 // comment for future refactors.
1279 let _ = settrap(st.sig, st.list, st.flags); // c:925/927
1280 if st.sig == SIGEXIT {
1281 EXIT_TRAP_POSIX.store(st.posix != 0, Ordering::Relaxed); // c:929
1282 }
1283 DONTSAVETRAP.fetch_sub(1, Ordering::Relaxed); // c:930
1284 } else if st.flags != 0 && st.body.is_some() {
1285 // Saved entry was a list-trap installed via bin_trap
1286 // (body in traps_table, no Eprog). Re-arm sigtrapped
1287 // with the saved flags so the dispatch path finds it.
1288 DONTSAVETRAP.fetch_add(1, Ordering::Relaxed);
1289 let _ = settrap(st.sig, None, st.flags);
1290 if st.sig == SIGEXIT {
1291 EXIT_TRAP_POSIX.store(st.posix != 0, Ordering::Relaxed);
1292 }
1293 DONTSAVETRAP.fetch_sub(1, Ordering::Relaxed);
1294 } else {
1295 // c:933 — `else if (sigtrapped[sig])`. Only fires when
1296 // the current slot has a trap set. The previous Rust
1297 // port unconditionally entered this branch, calling
1298 // unsettrap on slots that were already cleared.
1299 let cur_trapped = sigtrapped
1300 .lock()
1301 .ok()
1302 .and_then(|g| g.get(st.sig as usize).copied())
1303 .unwrap_or(0);
1304 if cur_trapped != 0 {
1305 // c:933
1306 // c:938 — `if (sig != SIGEXIT || !exit_trap_posix)`.
1307 if st.sig != SIGEXIT || !EXIT_TRAP_POSIX.load(Ordering::Relaxed) {
1308 unsettrap(st.sig); // c:939
1309 }
1310 }
1311 }
1312 }
1313 }
1314
1315 // c:961-969 — run the SIGEXIT trap, last (AFTER the savetraps
1316 // pop loop above so a restored deeper SIGEXIT is replaced by
1317 // the current scope's saved-aside trap before dispatch).
1318 //
1319 // C `dotrapargs(SIGEXIT, &exittr, exitfn)` invokes either:
1320 // - ZSIG_FUNC: the `TRAPEXIT` shell function from shfunctab.
1321 // - else: the eprog from siglists[SIGEXIT].
1322 if exittr != 0 && (exittr & ZSIG_FUNC) != 0 {
1323 // c:961, c:1132 FUNC branch — dispatch the TRAPEXIT shfunc.
1324 let signame = getsigname(SIGEXIT);
1325 let trap_fn = format!("TRAP{}", signame);
1326 if crate::ported::utils::getshfunc(&trap_fn).is_some() {
1327 let args = vec![SIGEXIT.to_string()];
1328 let _ = crate::ported::exec_hooks::dispatch_function_call(&trap_fn, &args);
1329 }
1330 } else if exittr != 0 {
1331 // c:961 else branch — non-FUNC eprog. The Rust port stores
1332 // the trap body as a string in `traps_table` (populated by
1333 // `bin_trap` / settrap). Dispatch through the execute_script
1334 // hook installed by fusevm_bridge — same path used by
1335 // signals.rs dotrap for non-FUNC traps.
1336 //
1337 // Bug #80 — use the body snapshot taken BEFORE the SAVETRAPS
1338 // pop loop, so the dispatch fires THIS scope's body (not the
1339 // outer body restored by the loop).
1340 let body = exit_body.unwrap_or_default();
1341 if !body.is_empty() {
1342 // Bracket the dispatch so the recursive
1343 // `execute_script_zsh_pipeline`'s own end-of-pipeline EXIT
1344 // check doesn't see the OUTER scope's body (which the
1345 // SAVETRAPS pop loop just restored into traps_table) and
1346 // fire it prematurely. Pull "EXIT" aside for the duration
1347 // of the dispatch, restore after.
1348 let stash = crate::ported::builtin::traps_table()
1349 .lock()
1350 .ok()
1351 .and_then(|mut t| t.remove("EXIT"));
1352 let _ = crate::ported::exec_hooks::execute_script(&body); // c:961 eprog body
1353 if let Some(b) = stash {
1354 if let Ok(mut t) = crate::ported::builtin::traps_table().lock() {
1355 t.insert("EXIT".to_string(), b);
1356 }
1357 }
1358 }
1359 }
1360}
1361
1362/// Direct port of `mod_export int handletrap(int sig)` from
1363/// `Src/signals.c:972`. Trap-queue gate called from the async
1364/// signal handlers. Returns 0 if the signal isn't trapped; if
1365/// trapped + queueing enabled it pushes onto `trap_queue` and
1366/// returns 1; otherwise it calls `dotrap(SIGIDX(sig))` (with the
1367/// SIGALRM TMOUT reset at the end) and returns 1.
1368pub fn handletrap(sig: i32) -> i32 {
1369 // c:972
1370 let idx = crate::ported::signals_h::SIGIDX(sig);
1371 let trapped = sigtrapped
1372 .lock()
1373 .ok()
1374 .and_then(|g| g.get(idx as usize).copied())
1375 .unwrap_or(0);
1376 if trapped == 0 {
1377 return 0;
1378 } // c:974
1379
1380 if trap_queueing_enabled.load(Ordering::SeqCst) != 0 {
1381 // c:977
1382 // c:980-986 — push onto `trap_queue` ring buffer.
1383 let r = trap_queue_rear.load(Ordering::SeqCst);
1384 let new_rear = (r + 1) % MAX_QUEUE_SIZE;
1385 if new_rear != trap_queue_front.load(Ordering::SeqCst) {
1386 trap_queue[new_rear].store(sig, Ordering::SeqCst);
1387 trap_queue_rear.store(new_rear, Ordering::SeqCst);
1388 }
1389 return 1;
1390 }
1391
1392 dotrap(idx); // c:990
1393
1394 if sig == libc::SIGALRM {
1395 // c:992
1396 // c:996 — `if ((tmout = getiparam("TMOUT"))) alarm(tmout);`
1397 // Re-arm the TMOUT timer after the trap dispatched.
1398 #[cfg(unix)]
1399 unsafe {
1400 let tmout = getiparam("TMOUT");
1401 if tmout > 0 {
1402 libc::alarm(tmout as u32); // c:996
1403 }
1404 }
1405 }
1406 1
1407}
1408
1409/// Direct port of `void queue_traps(int wait_cmd)` from
1410/// `Src/signals.c:1024-1033`.
1411///
1412/// C body:
1413/// if (!isset(TRAPSASYNC) && !wait_cmd)
1414/// trap_queueing_enabled = 1;
1415///
1416/// C ONLY enables queueing when NEITHER `TRAPSASYNC` is set NOR the
1417/// caller is the `wait` builtin (which wants traps to fire immediately
1418/// so the wait can be interrupted). The flag is a boolean (`= 1` /
1419/// `= 0`), symmetric with the reset in `unqueue_traps` at c:1042.
1420pub fn queue_traps(wait_cmd: i32) {
1421 // c:1024
1422 // c:1026 — both gates must be off for queueing to be enabled.
1423 if !isset(TRAPSASYNC) && wait_cmd == 0 {
1424 trap_queueing_enabled.store(1, Ordering::SeqCst); // c:1031
1425 }
1426}
1427
1428// Disable trap queuing and run the traps. // c:1041
1429/// Direct port of `void unqueue_traps(void)` from
1430/// `Src/signals.c:1041`. Disables `trap_queueing_enabled` and
1431/// flushes the pending queue by dispatching each sig through
1432/// `handletrap()`.
1433pub fn unqueue_traps() {
1434 // c:1041
1435 // c:1041 — `trap_queueing_enabled = 0;`
1436 trap_queueing_enabled.store(0, Ordering::SeqCst);
1437 // c:1046 — `while (trap_queue_front != trap_queue_rear) (void) handletrap(...);`
1438 loop {
1439 let f = trap_queue_front.load(Ordering::SeqCst);
1440 let r = trap_queue_rear.load(Ordering::SeqCst);
1441 if f == r {
1442 break;
1443 }
1444 let nf = (f + 1) % MAX_QUEUE_SIZE;
1445 let sig = trap_queue[nf].load(Ordering::SeqCst);
1446 trap_queue_front.store(nf, Ordering::SeqCst);
1447 let _ = handletrap(sig);
1448 }
1449}
1450
1451// Standard call to execute a trap for a given signal. // c:1245
1452/// Direct port of `void dotrap(int sig)` from `Src/signals.c:1245`.
1453/// Dispatches the trap registered for `sig`:
1454/// - ZSIG_FUNC: invoke the `TRAPxxx` shell function from shfunctab
1455/// via `doshfunc` with the signal number as the single arg.
1456/// - else: execute the eprog in `siglists[sig]` via fusevm
1457/// dispatch when wired (currently no-op pending VM bridge for
1458/// eprog).
1459/// Maintains `intrap` / `in_exit_trap` flags around the call so
1460/// observers (the `exit` builtin, the `zexit` driver) can branch on
1461/// whether we're inside an EXIT-trap callback.
1462pub fn dotrap(sig: i32) -> i32 {
1463 // c:1245
1464 // c:1248 — `int q = queue_signal_level();` capture at entry.
1465 // Required for the c:1280 `restore_queue_signals(q)` tail. The
1466 // previous Rust port omitted the capture and tail-restored to 0
1467 // unconditionally — that zeroed the queue level even when the
1468 // caller had set it to a non-zero value (e.g. nested dotrap
1469 // from inside a queue_signals/unqueue_signals block).
1470 let q = crate::ported::signals_h::queue_signal_level(); // c:1248
1471
1472 let trapped = sigtrapped
1473 .lock()
1474 .ok()
1475 .and_then(|g| g.get(sig as usize).copied())
1476 .unwrap_or(0);
1477 // c:1259 — `if ((sigtrapped[sig] & ZSIG_IGNORED) || !funcprog || errflag) return;`
1478 if trapped & ZSIG_IGNORED != 0 {
1479 return 0;
1480 }
1481 // Look up a fallback raw-text body installed by `bin_trap`
1482 // (`trap '...' SIG` form). bin_trap stores the body in the
1483 // canonical `traps_table` HashMap<String, String> but never
1484 // calls settrap, so `sigtrapped[sig]` may be 0 here even when
1485 // there IS a live trap. Treat presence in traps_table as
1486 // equivalent to ZSIG_TRAPPED for the dispatch decision and
1487 // dispatch via the exec_hooks::execute_script fn-ptr installed
1488 // by fusevm_bridge.
1489 let signame_for_lookup = getsigname(sig);
1490 let table_body: Option<String> = {
1491 let aliases: &[&str] = match sig {
1492 x if x == SIGZERR => &["ZERR", "ERR"],
1493 x if x == SIGDEBUG => &["DEBUG"],
1494 x if x == SIGEXIT => &["EXIT"],
1495 _ => &[],
1496 };
1497 let mut found = None;
1498 if let Ok(t) = crate::ported::builtin::traps_table().lock() {
1499 if let Some(b) = t.get(&signame_for_lookup) {
1500 found = Some(b.clone());
1501 } else {
1502 for alias in aliases {
1503 if let Some(b) = t.get(*alias) {
1504 found = Some(b.clone());
1505 break;
1506 }
1507 }
1508 }
1509 }
1510 found
1511 };
1512 if trapped & (ZSIG_TRAPPED | ZSIG_FUNC) == 0 && table_body.is_none() {
1513 return 0;
1514 }
1515 if errflag.load(Ordering::Relaxed) != 0 {
1516 return 0;
1517 }
1518 // c:Src/signals.c:1244 — `if (intrap) return;` — re-entry guard.
1519 // Without this, a TRAP body that itself fails (e.g. `trap "false"
1520 // ERR; false`) re-enters dotrap on its own failed status and
1521 // recurses indefinitely → stack overflow.
1522 if intrap.load(Ordering::Relaxed) != 0 {
1523 return 0;
1524 }
1525
1526 intrap.store(1, Ordering::SeqCst);
1527 // c:1270 — `dont_queue_signals()`. C disables signal queueing for
1528 // the duration of the trap dispatch so signals delivered while
1529 // the trap is running run inline (not queued for later).
1530 crate::ported::signals_h::dont_queue_signals(); // c:1270
1531
1532 // c:1272-1273 — `if (sig == SIGEXIT) ++in_exit_trap;` (counter,
1533 // not boolean — depth tracking lets observers detect re-entry).
1534 if sig == SIGEXIT {
1535 in_exit_trap.fetch_add(1, Ordering::SeqCst); // c:1273
1536 }
1537
1538 // c:1251 — `if (sigtrapped[sig] & ZSIG_FUNC)` → run TRAPxxx shfunc.
1539 // c:Src/signals.c:1251-1259 — the C source dispatches ONE of
1540 // the two arms based on sigtrapped flags: ZSIG_FUNC → call
1541 // shfunc, else → run siglists eprog. The C settrap → unsettrap
1542 // chain ensures only one form is active at a time. zshrs's
1543 // port stores string-form bodies separately in `traps_table`
1544 // (not touched by removetrap), so both can coexist. Track
1545 // whether the function-form fired so the string-form fallback
1546 // below doesn't double-dispatch. Bug #541 in docs/BUGS.md.
1547 let mut fn_dispatched = false;
1548 if trapped & ZSIG_FUNC != 0 {
1549 let signame = getsigname(sig);
1550 let trap_fn = format!("TRAP{}", signame);
1551 if getshfunc(&trap_fn).is_some() {
1552 // c:1252-1255 — `dotrapargs(sig, sigtrapped+sig, funcprog)`.
1553 // Drives the shfunc with `$1 = sig`. With the
1554 // executor not directly callable from this
1555 // signal-handler context, route through the
1556 // canonical `crate::exec::doshfunc` entry which
1557 // handles the arg+env+local-scope wrap.
1558 let args = vec![sig.to_string()];
1559 let _ = crate::ported::exec_hooks::dispatch_function_call(&trap_fn, &args);
1560 fn_dispatched = true;
1561 }
1562 }
1563 // Additional function-form check: even when ZSIG_FUNC isn't
1564 // set on sigtrapped (the function was defined after the
1565 // string-form trap, and zshrs's exec.rs:6292 didn't update
1566 // sigtrapped via the doshfunc path), look in shfunctab
1567 // directly. zsh's last-defined-wins semantics treat the
1568 // function as the active handler — the older string-form
1569 // entry in traps_table must not also fire.
1570 if !fn_dispatched {
1571 let signame = getsigname(sig);
1572 let trap_fn = format!("TRAP{}", signame);
1573 if getshfunc(&trap_fn).is_some() {
1574 let args = vec![sig.to_string()];
1575 let _ = crate::ported::exec_hooks::dispatch_function_call(&trap_fn, &args);
1576 fn_dispatched = true;
1577 }
1578 }
1579 // c:1268 — non-FUNC `siglists[sig]` eprog branch. The canonical
1580 // settrap→siglists path isn't fully wired (bin_trap stores raw
1581 // body text into `traps_table` rather than parsing to Eprog and
1582 // calling settrap). Dispatch via the exec_hooks::execute_script
1583 // fn-ptr installed by fusevm_bridge — no direct ShellExecutor
1584 // reach-in from src/ported/ (see memory
1585 // feedback_no_exec_script_from_ported).
1586 // Skip the string-form fallback when a function-form already
1587 // fired — zsh semantics are last-defined-replaces, not
1588 // both-fire. Bug #541 in docs/BUGS.md.
1589 if let Some(body) = table_body.filter(|_| !fn_dispatched) {
1590 // c:Bug #56 — when a trap fires DURING `$(...)` capture
1591 // (cmdsub redirected fd 1 to a pipe), the trap body's
1592 // stdout would land in the captured value. zsh forks each
1593 // cmdsub so traps run in the parent process whose fd 1 is
1594 // the terminal. zshrs's in-process cmdsub publishes the
1595 // saved outer stdout via CMDSUBST_OUTER_FDS so the trap
1596 // dispatcher can route body output to the parent's real
1597 // stdout instead. Temporarily restore fd 1 to that saved
1598 // outer stdout around the body, then revert to the
1599 // cmdsub-bound fd. Same idea as bash's command-subst trap
1600 // routing (Functions/Misc/runtraps).
1601 let outer = crate::fusevm_bridge::cmdsubst_outer_stdout();
1602 let saved_inner = if outer.is_some() {
1603 unsafe { libc::dup(libc::STDOUT_FILENO) }
1604 } else {
1605 -1
1606 };
1607 if let Some(out_fd) = outer {
1608 unsafe {
1609 libc::dup2(out_fd, libc::STDOUT_FILENO);
1610 }
1611 }
1612 let _ = crate::ported::exec_hooks::execute_script(&body);
1613 if let Some(_) = outer {
1614 if saved_inner >= 0 {
1615 unsafe {
1616 libc::dup2(saved_inner, libc::STDOUT_FILENO);
1617 libc::close(saved_inner);
1618 }
1619 }
1620 }
1621 }
1622
1623 // c:1277 — `if (sig == SIGEXIT) --in_exit_trap;` (decrement, not
1624 // store-0). The previous Rust port used `store(0)` which would
1625 // mask a re-entered trap — a TRAP_EXIT inside another TRAP_EXIT
1626 // would clear the flag prematurely.
1627 if sig == SIGEXIT {
1628 in_exit_trap.fetch_sub(1, Ordering::SeqCst); // c:1277
1629 }
1630 // c:1280 — `restore_queue_signals(q)` — restore to the level
1631 // captured at entry (c:1248). Now properly captured above; the
1632 // previous tail was a hardcoded `intrap.store(0)` only.
1633 crate::ported::signals_h::restore_queue_signals(q); // c:1280
1634 intrap.store(0, Ordering::SeqCst);
1635 0
1636}
1637
1638/// Direct port of `void dotrapargs(int sig, int *sigtr, void *sigfn)` from
1639/// `Src/signals.c:1081`. Drives a single trap callback for `sig`:
1640/// suspends `breaks`/`retflag`/`lastval` so the body runs in a fresh
1641/// control-flow scope, dispatches the function (ZSIG_FUNC) or eprog
1642/// (non-FUNC) body, then restores the caller's flags applying the
1643/// trap_state / trap_return / try_tryflag rules.
1644///
1645/// ```c
1646/// void
1647/// dotrapargs(int sig, int *sigtr, void *sigfn)
1648/// {
1649/// LinkList args;
1650/// char *name, num[4];
1651/// int obreaks = breaks;
1652/// int oretflag = retflag;
1653/// int olastval = lastval;
1654/// int isfunc;
1655/// int traperr, new_trap_state, new_trap_return;
1656/// if ((*sigtr & ZSIG_IGNORED) || !sigfn || errflag) return;
1657/// if (intrap) {
1658/// switch (sig) { case SIGEXIT: case SIGDEBUG: case SIGZERR: return; }
1659/// }
1660/// queue_signals();
1661/// intrap++;
1662/// *sigtr |= ZSIG_IGNORED;
1663/// zcontext_save();
1664/// execsave();
1665/// breaks = retflag = 0;
1666/// traplocallevel = locallevel;
1667/// runhookdef(BEFORETRAPHOOK, NULL);
1668/// if (*sigtr & ZSIG_FUNC) {
1669/// /* ... build args, doshfunc(...) ... */
1670/// } else {
1671/// trap_return = -2;
1672/// trap_state = TRAP_STATE_PRIMED;
1673/// trapisfunc = isfunc = 0;
1674/// execode((Eprog)sigfn, 1, 0, "trap");
1675/// }
1676/// runhookdef(AFTERTRAPHOOK, NULL);
1677/// traperr = errflag;
1678/// new_trap_state = trap_state;
1679/// new_trap_return = trap_return;
1680/// execrestore();
1681/// zcontext_restore();
1682/// /* ... restore breaks/retflag/lastval per FORCE_RETURN / traperr ... */
1683/// if (zleactive && resetneeded) zleentry(ZLE_CMD_REFRESH);
1684/// if (*sigtr != ZSIG_IGNORED) *sigtr &= ~ZSIG_IGNORED;
1685/// intrap--;
1686/// unqueue_signals();
1687/// }
1688/// ```
1689#[allow(clippy::too_many_arguments)]
1690pub fn dotrapargs(sig: i32, sigtr: &mut i32, sigfn: Option<&str>) {
1691 // c:1081
1692
1693 let obreaks: i32 = BREAKS.load(Ordering::SeqCst); // c:1085
1694 let oretflag: i32 = RETFLAG.load(Ordering::SeqCst); // c:1086
1695 let olastval: i32 = LASTVAL.load(Ordering::SeqCst); // c:1087
1696 let isfunc: i32; // c:1088
1697 let traperr: i32; // c:1089
1698 let new_trap_state: i32; // c:1089
1699 let new_trap_return: i32; // c:1089
1700
1701 // c:1101 — `if ((*sigtr & ZSIG_IGNORED) || !sigfn || errflag) return;`
1702 if (*sigtr & ZSIG_IGNORED) != 0 // c:1101
1703 || sigfn.is_none()
1704 || errflag.load(Ordering::SeqCst) != 0
1705 {
1706 return; // c:1102
1707 }
1708
1709 // c:1112-1119 — disallow synchronous traps from nesting.
1710 if intrap.load(Ordering::SeqCst) != 0 {
1711 // c:1112
1712 if sig == SIGEXIT || sig == SIGDEBUG || sig == SIGZERR {
1713 // c:1113-1117
1714 return; // c:1117
1715 }
1716 }
1717
1718 queue_signals(); // c:1121
1719
1720 intrap.fetch_add(1, Ordering::SeqCst); // c:1123
1721 *sigtr |= ZSIG_IGNORED; // c:1124
1722 // Mirror into the sigtrapped slab so observers (handletrap, dotrap
1723 // re-entry) see the same ZSIG_IGNORED bit.
1724 if let Ok(mut g) = sigtrapped.lock() {
1725 if let Some(slot) = g.get_mut(sig as usize) {
1726 *slot |= ZSIG_IGNORED;
1727 }
1728 }
1729
1730 zcontext_save(); // c:1126
1731 // c:1128 — `execsave()` saves trap_return/trap_state. Without a
1732 // canonical `execsave` port yet, snapshot the two atomics inline.
1733 let saved_trap_state = TRAP_STATE.load(Ordering::SeqCst); // c:1128 execsave
1734 let saved_trap_return = TRAP_RETURN.load(Ordering::SeqCst); // c:1128 execsave
1735 BREAKS.store(0, Ordering::SeqCst); // c:1129 breaks = 0
1736 RETFLAG.store(0, Ordering::SeqCst); // c:1129 retflag = 0
1737 traplocallevel.store(
1738 crate::ported::params::locallevel.load(Ordering::SeqCst),
1739 Ordering::SeqCst,
1740 ); // c:1130
1741
1742 // c:1131 — `runhookdef(BEFORETRAPHOOK, NULL);` — fire any
1743 // registered "before-trap" module hooks. Looked up by name
1744 // through gethookdef so the module dispatcher picks up
1745 // installed handlers (zsh/zle's zlebeforetrap etc.).
1746 let hd = crate::ported::module::gethookdef("BEFORETRAPHOOK");
1747 if !hd.is_null() {
1748 let _ = crate::ported::module::runhookdef(hd, std::ptr::null_mut());
1749 }
1750 let _ = BEFORETRAPHOOK; // c:1131 — const retained for source-cite parity
1751
1752 if (*sigtr & ZSIG_FUNC) != 0 {
1753 // c:1132
1754 let osc = SFCONTEXT.load(Ordering::SeqCst); // c:1133 osc
1755 // c:1133 — `int old_incompfunc = incompfunc;` — snapshot the
1756 // completion-function-active flag so the trap dispatch can
1757 // run code outside the comp-fn scope and restore on return.
1758 let old_incompfunc: i32 = crate::ported::zle::complete::INCOMPFUNC.load(Ordering::Relaxed);
1759 let hn = gettrapnode(sig, false); // c:1134
1760
1761 let mut args: Vec<String> = Vec::new(); // c:1136 znewlinklist
1762 // c:1144-1149 — pick the right TRAPxxx name from the function table
1763 // (multi-named aliases) or build the canonical TRAP<SIGNAME>.
1764 let name = match hn {
1765 Some(n) => ztrdup(&n), // c:1145 ztrdup(hn->nam)
1766 None => {
1767 // c:1146
1768 format!("TRAP{}", getsigname(sig)) // c:1147-1148
1769 }
1770 };
1771 args.push(name.clone()); // c:1150 zaddlinknode(args, name)
1772 let num = format!("{}", sig); // c:1151 sprintf(num, "%d", sig)
1773 args.push(num); // c:1152
1774
1775 TRAP_RETURN.store(-1, Ordering::SeqCst); // c:1154 trap_return = -1; /* incremented by doshfunc */
1776 TRAP_STATE.store(TRAP_STATE_PRIMED, Ordering::SeqCst); // c:1155
1777 trapisfunc.store(1, Ordering::SeqCst); // c:1156
1778 isfunc = 1;
1779
1780 SFCONTEXT.store(SFC_SIGNAL, Ordering::SeqCst); // c:1158
1781 // c:1159 — `incompfunc = 0;` — clear the active-compfn flag
1782 // so user-level trap handlers can run normal `complete` /
1783 // `compadd` etc. without being mis-detected as inside a
1784 // completion widget.
1785 crate::ported::zle::complete::INCOMPFUNC.store(0, Ordering::Relaxed);
1786 // c:1160 — `doshfunc((Shfunc)sigfn, args, 1);`. Direct
1787 // doshfunc call mirrors C — argv[0] = TRAP name, argv[1..]
1788 // = signum etc. body_runner routes through the host's
1789 // body-only entry so we don't double-wrap the scope.
1790 let fn_name = sigfn.unwrap_or("").to_string();
1791 let shf_clone: Option<crate::ported::zsh_h::shfunc> = {
1792 let tab = crate::ported::hashtable::shfunctab_lock().read();
1793 tab.ok().and_then(|t| t.get(&fn_name).cloned())
1794 };
1795 if let Some(mut shf) = shf_clone {
1796 let body_args = args.clone();
1797 let name_for_body = fn_name.clone();
1798 let body_runner = move || -> i32 {
1799 crate::ported::exec_hooks::run_function_body(&name_for_body, &body_args[1..])
1800 .unwrap_or(0)
1801 };
1802 let _ = crate::ported::exec::doshfunc(&mut shf, args.clone(), true, body_runner);
1803 }
1804 SFCONTEXT.store(osc, Ordering::SeqCst); // c:1161
1805 // c:1162 — `incompfunc = old_incompfunc;` — restore the
1806 // completion-function-active flag we snapshotted at c:1133.
1807 crate::ported::zle::complete::INCOMPFUNC.store(old_incompfunc, Ordering::Relaxed);
1808 let _ = args; // c:1163 freelinklist(args)
1809 zsfree(name); // c:1164 zsfree(name)
1810 } else {
1811 // c:1165
1812 TRAP_RETURN.store(-2, Ordering::SeqCst); // c:1166 trap_return = -2
1813 TRAP_STATE.store(TRAP_STATE_PRIMED, Ordering::SeqCst); // c:1167
1814 trapisfunc.store(0, Ordering::SeqCst); // c:1168
1815 isfunc = 0;
1816 // c:1170 — `execode((Eprog)sigfn, 1, 0, "trap");` — execute
1817 // the trap's compiled Eprog body. zshrs models the non-
1818 // ZSIG_FUNC trap action as source TEXT (sigfn: Option<&str>)
1819 // rather than a pre-compiled Eprog; re-parse + execute via
1820 // the fusevm pipeline so the trap action actually fires.
1821 // When sigfns[] is re-shaped to carry compiled Eprogs (matching
1822 // C's `void *sigfns[sig]` cast at c:1170), swap in
1823 // `crate::ported::exec::execode(eprog, 1, 0, "trap")` directly.
1824 if let Some(src) = sigfn {
1825 let _ = crate::ported::exec_hooks::execute_script_zsh_pipeline(src);
1826 }
1827 }
1828
1829 // c:1172 — `runhookdef(AFTERTRAPHOOK, NULL);` — fire any registered
1830 // "after-trap" module hooks. Same shape as BEFORETRAPHOOK above.
1831 let hd = crate::ported::module::gethookdef("AFTERTRAPHOOK");
1832 if !hd.is_null() {
1833 let _ = crate::ported::module::runhookdef(hd, std::ptr::null_mut());
1834 }
1835 let _ = AFTERTRAPHOOK; // c:1172 — const retained for source-cite parity
1836
1837 traperr = errflag.load(Ordering::SeqCst); // c:1174
1838
1839 new_trap_state = TRAP_STATE.load(Ordering::SeqCst); // c:1177
1840 new_trap_return = TRAP_RETURN.load(Ordering::SeqCst); // c:1178
1841
1842 // c:1180 — `execrestore()` restores trap_return/trap_state.
1843 TRAP_STATE.store(saved_trap_state, Ordering::SeqCst); // c:1180
1844 TRAP_RETURN.store(saved_trap_return, Ordering::SeqCst); // c:1180
1845 zcontext_restore(); // c:1181
1846
1847 if new_trap_state == TRAP_STATE_FORCE_RETURN // c:1183
1848 && !(isfunc != 0 && new_trap_return == 0)
1849 // c:1184
1850 {
1851 if isfunc != 0 {
1852 // c:1186
1853 BREAKS.store(LOOPS.load(Ordering::SeqCst), Ordering::SeqCst); // c:1187 breaks = loops
1854 if sig == libc::SIGINT || sig == libc::SIGQUIT {
1855 // c:1196
1856 errflag.fetch_or(
1857 // c:1197 errflag |= ERRFLAG_INT
1858 ERRFLAG_INT,
1859 Ordering::SeqCst,
1860 );
1861 } else {
1862 // c:1198
1863 errflag.fetch_or(
1864 // c:1199 errflag |= ERRFLAG_ERROR
1865 ERRFLAG_ERROR,
1866 Ordering::SeqCst,
1867 );
1868 }
1869 }
1870 LASTVAL.store(new_trap_return, Ordering::SeqCst); // c:1202
1871 RETFLAG.store(1, Ordering::SeqCst); // c:1204 retflag = 1
1872 } else {
1873 // c:1205
1874 if traperr != 0 && !EMULATION(EMULATE_SH) {
1875 // c:1206
1876 LASTVAL.store(1, Ordering::SeqCst); // c:1207
1877 } else {
1878 // c:1208
1879 // c:1210 — keep pre-trap lastval.
1880 LASTVAL.store(olastval, Ordering::SeqCst); // c:1213
1881 }
1882 if try_tryflag.load(Ordering::SeqCst) != 0 {
1883 // c:1215 try_tryflag
1884 if traperr != 0 {
1885 // c:1216
1886 errflag.fetch_or(
1887 // c:1217
1888 ERRFLAG_ERROR,
1889 Ordering::SeqCst,
1890 );
1891 } else {
1892 // c:1218
1893 errflag.fetch_and(
1894 // c:1219 errflag &= ~ERRFLAG_ERROR
1895 !ERRFLAG_ERROR,
1896 Ordering::SeqCst,
1897 );
1898 }
1899 }
1900 BREAKS.fetch_add(obreaks, Ordering::SeqCst); // c:1220 breaks += obreaks
1901 RETFLAG.store(oretflag, Ordering::SeqCst); // c:1222 retflag = oretflag
1902 let cur_breaks = BREAKS.load(Ordering::SeqCst);
1903 let cur_loops = LOOPS.load(Ordering::SeqCst);
1904 if cur_breaks > cur_loops {
1905 // c:1223
1906 BREAKS.store(cur_loops, Ordering::SeqCst); // c:1224
1907 }
1908 }
1909
1910 // c:1231 — `if (zleactive && resetneeded) zleentry(ZLE_CMD_REFRESH);`
1911 if zleactive.load(Ordering::SeqCst) != 0 && RESETNEEDED.load(Ordering::SeqCst) != 0 {
1912 let _ = zleentry(ZLE_CMD_REFRESH);
1913 }
1914
1915 if *sigtr != ZSIG_IGNORED {
1916 // c:1234
1917 *sigtr &= !ZSIG_IGNORED; // c:1235
1918 if let Ok(mut g) = sigtrapped.lock() {
1919 if let Some(slot) = g.get_mut(sig as usize) {
1920 *slot &= !ZSIG_IGNORED;
1921 }
1922 }
1923 }
1924 intrap.fetch_sub(1, Ordering::SeqCst); // c:1236
1925
1926 unqueue_signals(); // c:1238
1927}
1928
1929// `try_tryflag` lives at `Src/loop.c:731` (the always/try block depth
1930// counter); ported to `crate::ported::r#loop::try_tryflag`. dotrapargs
1931// above reads it from its canonical home per PORT.md Rule C (header /
1932// file placement).
1933
1934/// Resolve a real-time signal name to its number.
1935/// Port of `int rtsigno(const char* signame)` from `Src/signals.c:1291-1313`.
1936///
1937/// **C signature**: `int rtsigno(const char* signame)` — takes a
1938/// NAME STRING ("RTMIN", "RTMIN+3", "RTMAX-1", etc.) and returns
1939/// the signal number, or 0 on parse failure.
1940///
1941/// Rust signature: `(signame: &str) -> Option<i32>` — `None`
1942/// matches C's `0` sentinel. Uses `libc::SIGRTMIN()` /
1943/// `libc::SIGRTMAX()` for canonical bounds.
1944pub fn rtsigno(signame: &str) -> Option<i32> {
1945 // c:1291
1946 #[cfg(target_os = "linux")]
1947 {
1948 let sigrtmin = libc::SIGRTMIN();
1949 let sigrtmax = libc::SIGRTMAX();
1950 let maxofs = sigrtmax - sigrtmin; // c:1296
1951
1952 // c:1298-1306 — `if (!strncmp(signame, "RTMIN", 5)) ...
1953 // else if (!strncmp(signame, "RTMAX", 5)) ... else return 0;`
1954 let (sig, dir, op): (i32, i32, char) = if let Some(rest) = signame.strip_prefix("RTMIN") {
1955 (sigrtmin, 1, '+') // c:1300
1956 } else if let Some(rest) = signame.strip_prefix("RTMAX") {
1957 (sigrtmax, -1, '-') // c:1302
1958 } else {
1959 return None; // c:1304 return 0
1960 };
1961
1962 // c:1307-1311 — `if (signame[5] == x.op) { offset = strtol(...);
1963 // if (offset > maxofs) return 0;
1964 // x.sig += offset * x.dir; }`
1965 let rest = if signame.starts_with("RTMIN") {
1966 &signame[5..]
1967 } else {
1968 &signame[5..]
1969 };
1970 let mut final_sig = sig;
1971 if !rest.is_empty() {
1972 if rest.starts_with(op) {
1973 let num_str = &rest[1..];
1974 let offset: i32 = match num_str.parse() {
1975 Ok(n) => n,
1976 Err(_) => return None, // c:1312
1977 };
1978 if offset > maxofs {
1979 return None; // c:1310 return 0
1980 }
1981 final_sig += offset * dir;
1982 } else {
1983 // c:1313 — `if (*end) return 0;` — any non-op trailing → fail.
1984 return None;
1985 }
1986 }
1987 Some(final_sig)
1988 }
1989 #[cfg(not(target_os = "linux"))]
1990 {
1991 let _ = signame;
1992 None
1993 }
1994}
1995
1996/// Resolve a real-time signal number to its `RTMIN+N` / `RTMAX-N` name.
1997/// Port of `char *rtsigname(int signo, int alt)` from `Src/signals.c:1317`.
1998///
1999/// C body picks the SHORTER form between `RTMIN+N` and `RTMAX-N`,
2000/// preferring the smaller offset unless `alt` is set (which flips
2001/// the choice via XOR). `signo` outside `[SIGRTMIN..=SIGRTMAX]`
2002/// returns NULL — Rust returns empty string for the equivalent.
2003///
2004/// The previous Rust port:
2005/// 1. Dropped the `alt` argument entirely (callers got the
2006/// `alt=0` default, but no way to flip).
2007/// 2. ALWAYS produced the `RTMIN+N` form, ignoring the C "shorter
2008/// form wins" contract — for high-numbered RT signals where
2009/// RTMAX-N is the shorter form, C would emit `RTMAX-N` but
2010/// Rust emitted the longer `RTMIN+N`.
2011/// 3. Used a hardcoded `sigrtmin = 34` constant instead of
2012/// `libc::SIGRTMIN()` (real-time signal numbers can vary by
2013/// libc version/build).
2014/// 4. Out-of-range input produced `SIG{n}` — C returns NULL.
2015///
2016/// **Signature divergence from C**: C takes `(signo, alt)`; Rust port
2017/// takes `(sig)` with `alt=0` implicit because the only in-tree caller
2018/// (`params.rs:1640`) doesn't need the alt flip. A future caller that
2019/// needs alt-form can be added then.
2020/// WARNING: param names don't match C — Rust=(sig) vs C=(signo, alt)
2021pub fn rtsigname(sig: i32) -> String {
2022 // c:1317
2023 #[cfg(target_os = "linux")]
2024 {
2025 let sigrtmin = libc::SIGRTMIN();
2026 let sigrtmax = libc::SIGRTMAX();
2027 // c:1325-1326 — `if (signo < SIGRTMIN || signo > SIGRTMAX) return NULL;`
2028 if sig < sigrtmin || sig > sigrtmax {
2029 return String::new();
2030 }
2031 // c:1319-1323 — `int minofs = signo - SIGRTMIN; int maxofs =
2032 // SIGRTMAX - signo; int form = alt ^ (maxofs < minofs);`
2033 // With alt=0 always, form simplifies to `maxofs < minofs`.
2034 let minofs = sig - sigrtmin;
2035 let maxofs = sigrtmax - sig;
2036 let form = maxofs < minofs;
2037 // c:1328-1334 — pick `RTMIN+` or `RTMAX-` per `form`.
2038 let prefix = if form { "RTMAX-" } else { "RTMIN+" };
2039 let offset = if form { maxofs } else { minofs };
2040 if offset == 0 {
2041 // c:1334 — buf[5] = '\0' → drop the trailing sign char.
2042 prefix[..5].to_string()
2043 } else {
2044 format!("{}{}", prefix, offset)
2045 }
2046 }
2047 #[cfg(not(target_os = "linux"))]
2048 {
2049 let _ = sig;
2050 String::new()
2051 }
2052}
2053
2054// ---------------------------------------------------------------------------
2055// Signal-queue state. Direct ports of `Src/signals.c:77-92`:
2056//
2057// mod_export volatile int queueing_enabled, queue_front, queue_rear; // c:77
2058// mod_export int signal_queue[MAX_QUEUE_SIZE]; // c:79
2059// mod_export sigset_t signal_mask_queue[MAX_QUEUE_SIZE]; // c:81
2060// static volatile int trap_queueing_enabled,
2061// trap_queue_front, trap_queue_rear; // c:90
2062// static int trap_queue[MAX_QUEUE_SIZE]; // c:92
2063//
2064// C uses flat module-level variables; Rust mirrors with file-scope
2065// `AtomicI32` + `LazyLock<Mutex<Vec<...>>>` slabs so concurrent
2066// pushes from the async signal handler synchronize without UB.
2067// ---------------------------------------------------------------------------
2068
2069/// Signal-queue depth counter. Port of `mod_export volatile int
2070/// queueing_enabled` from `Src/signals.c:77`.
2071pub static queueing_enabled: AtomicI32 = AtomicI32::new(0); // c:77
2072
2073/// Ring-buffer head. Port of `mod_export volatile int queue_front`
2074/// from `Src/signals.c:77`.
2075pub static queue_front: AtomicUsize = AtomicUsize::new(0); // c:77
2076
2077/// Ring-buffer tail. Port of `mod_export volatile int queue_rear`
2078/// from `Src/signals.c:77`.
2079pub static queue_rear: AtomicUsize = AtomicUsize::new(0); // c:77
2080
2081/// Port of `mod_export volatile int queue_in` from `Src/signals.c:84`.
2082/// Companion counter bumped by `queue_signals()` (signals.h:90) and
2083/// decremented by `unqueue_signals()` (signals.h:94); used by
2084/// `dont_queue_signals()` to snapshot the depth (signals.h:99) and
2085/// by debug assertions (DPUTS2 at signals.h:105).
2086pub static queue_in: AtomicI32 = AtomicI32::new(0); // c:84
2087
2088#[allow(clippy::declare_interior_mutable_const)]
2089const ATOM_I32_ZERO: AtomicI32 = AtomicI32::new(0);
2090
2091/// Per-slot signal numbers. Port of `mod_export int
2092/// signal_queue[MAX_QUEUE_SIZE]` from `Src/signals.c:79`.
2093pub static signal_queue: [AtomicI32; MAX_QUEUE_SIZE] = // c:79
2094 [ATOM_I32_ZERO; MAX_QUEUE_SIZE];
2095
2096/// Per-slot blocked-mask snapshots. Port of `mod_export sigset_t
2097/// signal_mask_queue[MAX_QUEUE_SIZE]` from `Src/signals.c:81`.
2098/// `sigset_t` isn't Copy on every platform — wrapped in a Mutex
2099/// so the slabs initialize without const-eval gymnastics.
2100pub static signal_mask_queue: std::sync::LazyLock<Mutex<Vec<libc::sigset_t>>> = // c:81
2101 std::sync::LazyLock::new(|| {
2102 let zero: libc::sigset_t = unsafe { std::mem::zeroed() };
2103 Mutex::new(vec![zero; MAX_QUEUE_SIZE])
2104 });
2105
2106/// Trap-queue depth counter. Port of `static volatile int
2107/// trap_queueing_enabled` from `Src/signals.c:90`.
2108pub static trap_queueing_enabled: AtomicI32 = AtomicI32::new(0); // c:90
2109
2110/// Trap-queue head. Port of `static volatile int trap_queue_front`
2111/// from `Src/signals.c:90`.
2112pub static trap_queue_front: AtomicUsize = AtomicUsize::new(0); // c:90
2113
2114/// Trap-queue tail. Port of `static volatile int trap_queue_rear`
2115/// from `Src/signals.c:90`.
2116pub static trap_queue_rear: AtomicUsize = AtomicUsize::new(0); // c:90
2117
2118/// Port of `int last_signal` from `Src/signals.c:238`. Holds the
2119/// signal number of the most recent delivery; used by `wait_cmd`
2120/// in jobs.c to set `$?` to `128 + last_signal` when a trapped
2121/// signal interrupts wait.
2122pub static last_signal: AtomicI32 = AtomicI32::new(0); // c:238
2123
2124// ---------------------------------------------------------------------------
2125// Per-signal trap state. Direct ports of the C globals declared in
2126// `Src/signals.c:39/53/58`:
2127//
2128// mod_export int *sigtrapped; // c:39 — flag word per sig
2129// mod_export Eprog *siglists; // c:53 — Eprog per sig (trap body)
2130// mod_export volatile int nsigtrapped; // c:58 — trapped-signal count
2131//
2132// C allocates parallel arrays of length TRAPCOUNT at init time
2133// (`Src/init.c:1398`). Rust mirrors with `Mutex<Vec<...>>` slabs
2134// sized to TRAPCOUNT plus an atomic counter. TRAPxxx-function
2135// trap bodies are NOT stored here in C either — `dotrap` looks
2136// them up via `gettrapnode()` from shfunctab on signal delivery
2137// (`Src/jobs.c:gettrapnode`).
2138// ---------------------------------------------------------------------------
2139
2140/// Per-signal flag word. Port of `mod_export int *sigtrapped`
2141/// from `Src/signals.c:39`. Bit values are `ZSIG_TRAPPED`,
2142/// `ZSIG_IGNORED`, `ZSIG_FUNC`, plus `(locallevel << ZSIG_SHIFT)`
2143/// in the high bits.
2144pub static sigtrapped: std::sync::LazyLock<Mutex<Vec<i32>>> = // c:39
2145 std::sync::LazyLock::new(|| Mutex::new(vec![0; TRAPCOUNT as usize]));
2146
2147/// Per-signal Eprog body. Port of `mod_export Eprog *siglists`
2148/// from `Src/signals.c:53`. NULL for ZSIG_FUNC entries (function
2149/// body resolves through `gettrapnode` at dispatch time).
2150pub static siglists: std::sync::LazyLock<Mutex<Vec<Option<Eprog>>>> =
2151 // c:53
2152 std::sync::LazyLock::new(|| Mutex::new((0..TRAPCOUNT as usize).map(|_| None).collect()));
2153
2154/// Count of `ZSIG_TRAPPED`-flagged signals. Port of
2155/// `mod_export volatile int nsigtrapped` from `Src/signals.c:58`.
2156pub static nsigtrapped: AtomicI32 = AtomicI32::new(0); // c:58
2157
2158/// File-scope `int intrap` from `Src/signals.c`. Set while a
2159/// trap body is running so nested `dotrap` calls short-circuit
2160/// (matches the c:1245 dispatcher's `if (intrap) return`).
2161pub static intrap: AtomicI32 = AtomicI32::new(0); // c:intrap
2162
2163/// File-scope `int in_exit_trap` from `Src/signals.c:60`. Set
2164/// while the EXIT trap body is running so `exit` and friends can
2165/// distinguish "real" exit from exit-trap-driven exit.
2166pub static in_exit_trap: AtomicI32 = AtomicI32::new(0); // c:60
2167
2168/// Port of `volatile int trapisfunc` from `Src/signals.c:1062`.
2169/// Set by `dotrapargs()` (signals.c:1156) when the trap body is a
2170/// shell function (vs. inline command) — the `IN_EVAL_TRAP()` macro
2171/// at zsh.h:2962 tests this against `intrap` + `locallevel`.
2172pub static trapisfunc: AtomicI32 = AtomicI32::new(0); // c:1062
2173
2174/// Port of `volatile int traplocallevel` from `Src/signals.c:1069`.
2175/// Captures `locallevel` at trap-entry so the trap body can detect
2176/// whether it's running inside the same scope it was registered in
2177/// (the third leg of `IN_EVAL_TRAP()` at zsh.h:2962).
2178pub static traplocallevel: AtomicI32 = AtomicI32::new(0); // c:1069
2179
2180/// File-scope `LinkList savetraps` from `Src/signals.c`. Stack of
2181/// saved trap entries — pushed by `dosavetrap`, popped by
2182/// `endtrapscope`. Inserts at front so it works as a LIFO stack.
2183pub static SAVETRAPS: OnceLock<Mutex<Vec<savetrap>>> = OnceLock::new();
2184
2185/// File-scope `int exit_trap_posix` from `Src/signals.c`. POSIX-mode
2186/// EXIT trap flag — when set, exit traps survive function-scope
2187/// teardown instead of being unset.
2188pub static EXIT_TRAP_POSIX: AtomicBool = AtomicBool::new(false);
2189
2190/// File-scope `int dontsavetrap` from `Src/signals.c`. Counter
2191/// suppressing `dosavetrap` calls during `settrap` invoked from
2192/// `endtrapscope`'s restore loop (so the restore itself doesn't
2193/// push fresh save entries).
2194pub static DONTSAVETRAP: AtomicI32 = AtomicI32::new(0);
2195
2196/// Port of `killpg()` libc passthrough — used by jobs.c / signals.c
2197/// callers; not in zsh source itself but referenced via libc.
2198pub fn killpg(pgrp: i32, sig: i32) -> i32 {
2199 unsafe { libc::killpg(pgrp, sig) }
2200}
2201
2202/// Port of `kill()` libc passthrough.
2203pub fn kill(pid: i32, sig: i32) -> i32 {
2204 unsafe { libc::kill(pid, sig) }
2205}
2206
2207// ---------------------------------------------------------------------------
2208// `interact` flag — mirrors C's global `interact` int (Src/init.c).
2209// Used by intr / holdintr / noholdintr / install_handler to gate
2210// SIGINT-related setup on interactive shell mode.
2211// ---------------------------------------------------------------------------
2212
2213fn interact_lock() -> &'static AtomicBool {
2214 static INTERACT: AtomicBool = AtomicBool::new(false);
2215 &INTERACT
2216}
2217
2218/// Setter for the `interact` flag. Called by init.rs once the
2219/// shell-mode dispatch determines whether stdin is a tty / `-i`
2220/// was passed.
2221///
2222/// Writes to the canonical INTERACTIVE option flag so the read
2223/// side (`is_interact` → `isset(INTERACTIVE)`) sees it.
2224pub fn set_interact(v: bool) {
2225 crate::ported::options::opt_state_set("interactive", v);
2226}
2227
2228/// Read the `interact` flag.
2229///
2230/// C: `#define interact (isset(INTERACTIVE))` (Src/zsh.h:2562) —
2231/// the canonical interact predicate reads the INTERACTIVE option
2232/// flag from the options table.
2233///
2234/// The previous Rust port read `interact_lock()` (a private
2235/// AtomicBool) whose only writer was `set_interact()` — and
2236/// `set_interact` was called ONLY from this file's tests. In
2237/// production, `interact_lock` stayed at the default `false`, so
2238/// every `if is_interact()` gate in `intr`/`nointr`/`holdintr`/
2239/// `noholdintr` short-circuited regardless of whether the shell
2240/// was actually interactive. `setopt INTERACTIVE` had no effect
2241/// on signal handling.
2242///
2243/// Route through canonical isset() so the option drives the predicate.
2244pub fn is_interact() -> bool {
2245 isset(INTERACTIVE)
2246}
2247
2248// ===========================================================
2249// Methods moved verbatim from src/ported/vm_helper because their
2250// C counterpart's source file maps 1:1 to this Rust module.
2251// Phase: drift
2252// ===========================================================
2253
2254// BEGIN moved-from-exec-rs
2255// (impl ShellExecutor block moved to src/exec_shims.rs — see file marker)
2256
2257// END moved-from-exec-rs
2258
2259#[cfg(test)]
2260mod tests {
2261 use super::*;
2262 use crate::options::dosetopt;
2263 use crate::zsh_h::{HUP, MONITOR, TRAPSASYNC};
2264
2265 #[test]
2266 fn test_sig_by_name() {
2267 let _g = crate::test_util::global_state_lock();
2268 assert_eq!(getsigidx("INT"), Some(libc::SIGINT));
2269 assert_eq!(getsigidx("SIGINT"), Some(libc::SIGINT));
2270 assert_eq!(getsigidx("int"), Some(libc::SIGINT));
2271 assert_eq!(getsigidx("HUP"), Some(libc::SIGHUP));
2272 assert_eq!(getsigidx("TERM"), Some(libc::SIGTERM));
2273 assert_eq!(getsigidx("EXIT"), Some(SIGEXIT));
2274 assert_eq!(getsigidx("9"), Some(9));
2275 }
2276
2277 #[test]
2278 fn test_getsigname() {
2279 let _g = crate::test_util::global_state_lock();
2280 assert_eq!(getsigname(libc::SIGINT), "INT");
2281 assert_eq!(getsigname(libc::SIGHUP), "HUP");
2282 assert_eq!(getsigname(SIGEXIT), "EXIT");
2283 }
2284
2285 #[test]
2286 fn test_signal_queue() {
2287 let _g = crate::test_util::global_state_lock();
2288 let before = queueing_enabled.load(Ordering::SeqCst);
2289 queue_signals();
2290 assert_eq!(queueing_enabled.load(Ordering::SeqCst), before + 1);
2291 unqueue_signals();
2292 assert_eq!(queueing_enabled.load(Ordering::SeqCst), before);
2293 }
2294
2295 #[test]
2296 fn test_signal_mask_zero_returns_empty() {
2297 let _g = crate::test_util::global_state_lock();
2298 // C: `if (sig) sigaddset(&set, sig);` — sig==0 yields empty set.
2299 let s = signal_mask(0);
2300 let r = unsafe { libc::sigismember(&s, libc::SIGINT) };
2301 assert_eq!(r, 0);
2302 }
2303
2304 #[test]
2305 fn test_signal_mask_includes_only_specified() {
2306 let _g = crate::test_util::global_state_lock();
2307 let s = signal_mask(libc::SIGUSR1);
2308 assert_eq!(unsafe { libc::sigismember(&s, libc::SIGUSR1) }, 1);
2309 assert_eq!(unsafe { libc::sigismember(&s, libc::SIGUSR2) }, 0);
2310 }
2311
2312 #[test]
2313 fn test_interact_flag_round_trip() {
2314 let _g = crate::test_util::global_state_lock();
2315 let prev = is_interact();
2316 set_interact(true);
2317 assert!(is_interact());
2318 set_interact(false);
2319 assert!(!is_interact());
2320 set_interact(prev);
2321 }
2322
2323 #[test]
2324 fn test_signal_block_returns_old_mask() {
2325 let _g = crate::test_util::global_state_lock();
2326 let prev = is_interact();
2327 set_interact(false); // ensure no test side-effects from interactive paths
2328 let mask = signal_mask(libc::SIGUSR2);
2329 let old = signal_block(&mask);
2330 // Restore to old state.
2331 let _ = signal_setmask(&old);
2332 // Verify the post-block mask had SIGUSR2 set by re-blocking
2333 // and unblocking. The test just checks the returned old set
2334 // is valid (no crash, syscall returned).
2335 let _ = old;
2336 set_interact(prev);
2337 }
2338
2339 /// `Src/signals.c:158-168` — `signal_mask(sig)` builds a fresh
2340 /// sigset containing only `sig`. The `sig == 0` arm at c:163
2341 /// returns an empty set (no `sigaddset` call). Pin both arms.
2342 #[cfg(unix)]
2343 #[test]
2344 fn signal_mask_includes_only_requested_signal() {
2345 let _g = crate::test_util::global_state_lock();
2346 let m = signal_mask(libc::SIGUSR1);
2347 // c:166 — `sigaddset(&set, sig)` for the requested signal.
2348 assert_eq!(
2349 unsafe { libc::sigismember(&m, libc::SIGUSR1) },
2350 1,
2351 "c:166 — requested signal must be set"
2352 );
2353 // Other signals not in the set.
2354 assert_eq!(unsafe { libc::sigismember(&m, libc::SIGUSR2) }, 0);
2355 assert_eq!(unsafe { libc::sigismember(&m, libc::SIGTERM) }, 0);
2356 }
2357
2358 /// `Src/signals.c:163` — `if (sig) sigaddset(&set, sig)`. With
2359 /// `sig == 0` no `sigaddset` runs, so the returned set is empty.
2360 /// Regression dropping the `if (sig)` guard would `sigaddset(&set, 0)`
2361 /// which is implementation-defined (Linux: EINVAL; macOS: may
2362 /// "succeed" with a bogus member).
2363 #[cfg(unix)]
2364 #[test]
2365 fn signal_mask_with_zero_returns_empty_set() {
2366 let _g = crate::test_util::global_state_lock();
2367 let m = signal_mask(0);
2368 // Every signal must be NOT a member of an empty set.
2369 for sig in [libc::SIGINT, libc::SIGTERM, libc::SIGUSR1, libc::SIGUSR2] {
2370 assert_eq!(
2371 unsafe { libc::sigismember(&m, sig) },
2372 0,
2373 "c:163 — sig=0 produces empty set, but {} found",
2374 sig
2375 );
2376 }
2377 }
2378
2379 /// `Src/signals.c:1291-1313` — `rtsigno(signame)` parses a NAME
2380 /// STRING ("RTMIN", "RTMIN+N", "RTMAX-N") and returns the signum,
2381 /// or 0 (Rust None) on parse failure.
2382 ///
2383 /// The previous Rust port had a completely wrong signature
2384 /// (`rtsigno(i32)` taking an offset int). Now matches C exactly.
2385 #[cfg(target_os = "linux")]
2386 #[test]
2387 fn rtsigno_parses_rt_signal_names() {
2388 let _g = crate::test_util::global_state_lock();
2389 let sigrtmin = libc::SIGRTMIN();
2390 let sigrtmax = libc::SIGRTMAX();
2391 // Bare RTMIN / RTMAX (no offset).
2392 assert_eq!(rtsigno("RTMIN"), Some(sigrtmin), "c:1300 — bare RTMIN");
2393 assert_eq!(rtsigno("RTMAX"), Some(sigrtmax), "c:1302 — bare RTMAX");
2394 // With offset.
2395 assert_eq!(
2396 rtsigno("RTMIN+1"),
2397 Some(sigrtmin + 1),
2398 "c:1307-1311 — RTMIN+N"
2399 );
2400 assert_eq!(
2401 rtsigno("RTMAX-1"),
2402 Some(sigrtmax - 1),
2403 "c:1307-1311 — RTMAX-N"
2404 );
2405 // Invalid input.
2406 assert_eq!(rtsigno("SIGINT"), None, "c:1304 — non-RT name returns None");
2407 assert_eq!(rtsigno(""), None, "empty string returns None");
2408 // Out-of-range offset.
2409 let maxofs = sigrtmax - sigrtmin;
2410 assert_eq!(
2411 rtsigno(&format!("RTMIN+{}", maxofs + 1)),
2412 None,
2413 "c:1310 — offset > maxofs returns None"
2414 );
2415 // Malformed (non-op trailing char).
2416 assert_eq!(
2417 rtsigno("RTMINx"),
2418 None,
2419 "c:1313 — trailing non-op char returns None"
2420 );
2421 }
2422
2423 /// `Src/signals.c:1317-1338` — `rtsigname(signo, alt)` picks the
2424 /// shorter form between `RTMIN+N` and `RTMAX-N`. Pin the contract
2425 /// on Linux where SIGRTMIN/SIGRTMAX are real.
2426 #[cfg(target_os = "linux")]
2427 #[test]
2428 fn rtsigname_picks_shorter_form_between_rtmin_rtmax() {
2429 let _g = crate::test_util::global_state_lock();
2430 let sigrtmin = libc::SIGRTMIN();
2431 let sigrtmax = libc::SIGRTMAX();
2432 // SIGRTMIN itself → "RTMIN" (offset 0; trailing '+' dropped).
2433 assert_eq!(
2434 rtsigname(sigrtmin),
2435 "RTMIN",
2436 "c:1334 — offset 0 → bare 'RTMIN' (no '+0')"
2437 );
2438 // SIGRTMAX itself → "RTMAX" (offset 0; trailing '-' dropped).
2439 assert_eq!(
2440 rtsigname(sigrtmax),
2441 "RTMAX",
2442 "c:1334 — offset 0 → bare 'RTMAX' (no '-0')"
2443 );
2444 // SIGRTMIN+1 — minofs=1, maxofs=(sigrtmax-sigrtmin-1) > 1 →
2445 // form=false → "RTMIN+1".
2446 assert_eq!(
2447 rtsigname(sigrtmin + 1),
2448 "RTMIN+1",
2449 "c:1322 — minofs < maxofs → form=0 → RTMIN+1"
2450 );
2451 // SIGRTMAX-1 — maxofs=1, minofs=(sigrtmax-sigrtmin-1) > 1 →
2452 // form=true → "RTMAX-1".
2453 assert_eq!(
2454 rtsigname(sigrtmax - 1),
2455 "RTMAX-1",
2456 "c:1322 — maxofs < minofs → form=1 → RTMAX-1"
2457 );
2458 // Out of range → empty string (C: NULL).
2459 assert_eq!(
2460 rtsigname(sigrtmin - 1),
2461 "",
2462 "c:1326 — signo < SIGRTMIN → NULL (empty)"
2463 );
2464 assert_eq!(
2465 rtsigname(sigrtmax + 1),
2466 "",
2467 "c:1326 — signo > SIGRTMAX → NULL (empty)"
2468 );
2469 }
2470
2471 /// `Src/signals.c:269-274` — `wait_for_processes` waitpid flags
2472 /// must include WCONTINUED so children resumed via SIGCONT
2473 /// surface a status update through waitpid. Pin the flag union
2474 /// composition: WNOHANG | WUNTRACED | WCONTINUED.
2475 #[cfg(unix)]
2476 #[test]
2477 fn wait_for_processes_uses_canonical_waitpid_flags() {
2478 let _g = crate::test_util::global_state_lock();
2479 // We can't easily intercept libc::waitpid from a test, so pin
2480 // the canonical flags directly via libc constants — if a
2481 // future regression drops WCONTINUED, the const assertion
2482 // fails (catching the drift even when no child is reaped).
2483 let canonical = libc::WNOHANG | libc::WUNTRACED | libc::WCONTINUED;
2484 // Each component must be a distinct, non-zero bit.
2485 assert_ne!(libc::WNOHANG, 0);
2486 assert_ne!(libc::WUNTRACED, 0);
2487 assert_ne!(libc::WCONTINUED, 0);
2488 assert_eq!(
2489 libc::WNOHANG & libc::WUNTRACED,
2490 0,
2491 "WNOHANG and WUNTRACED must be disjoint bits"
2492 );
2493 assert_eq!(
2494 libc::WNOHANG & libc::WCONTINUED,
2495 0,
2496 "WNOHANG and WCONTINUED must be disjoint bits"
2497 );
2498 assert_eq!(
2499 libc::WUNTRACED & libc::WCONTINUED,
2500 0,
2501 "WUNTRACED and WCONTINUED must be disjoint bits"
2502 );
2503 // The combined mask is the canonical WAITFLAGS per c:271.
2504 assert!(
2505 canonical >= libc::WNOHANG + libc::WUNTRACED + libc::WCONTINUED,
2506 "canonical mask must include all three bits"
2507 );
2508 // `wait_for_processes` is a void-returning poll-loop on the
2509 // current process; call it to verify it doesn't hang. Whether
2510 // any child gets reaped depends on the full-suite's prior
2511 // tests — other `std::process::Command::spawn` based tests can
2512 // leave reapable children alive. Pin the no-hang property; the
2513 // is-empty check is best-effort.
2514 let _result = wait_for_processes();
2515 }
2516
2517 /// `Src/signals.c:1024-1033` — `queue_traps(wait_cmd)` enables
2518 /// queueing ONLY when BOTH `!isset(TRAPSASYNC)` AND `!wait_cmd`.
2519 /// Pin:
2520 /// * TRAPSASYNC=on → queue_traps(0) is a no-op.
2521 /// * wait_cmd=1 → queue_traps(1) is a no-op.
2522 /// * both off → queue_traps(0) sets trap_queueing_enabled=1.
2523 #[cfg(unix)]
2524 #[test]
2525 fn queue_traps_respects_trapsasync_and_wait_cmd() {
2526 let _g = crate::test_util::global_state_lock();
2527 let saved = isset(TRAPSASYNC);
2528
2529 // Setup: TRAPSASYNC off, trap_queueing_enabled cleared.
2530 dosetopt(TRAPSASYNC, 0, 0);
2531 trap_queueing_enabled.store(0, Ordering::SeqCst);
2532
2533 // wait_cmd=1 → queueing stays disabled.
2534 queue_traps(1);
2535 assert_eq!(
2536 trap_queueing_enabled.load(Ordering::SeqCst),
2537 0,
2538 "c:1026 — wait_cmd=1 gate must block queueing"
2539 );
2540
2541 // wait_cmd=0 + TRAPSASYNC=off → queueing enabled.
2542 queue_traps(0);
2543 assert_eq!(
2544 trap_queueing_enabled.load(Ordering::SeqCst),
2545 1,
2546 "c:1031 — both gates off → queueing enabled = 1"
2547 );
2548
2549 // Reset; turn TRAPSASYNC on.
2550 trap_queueing_enabled.store(0, Ordering::SeqCst);
2551 dosetopt(TRAPSASYNC, 1, 0);
2552 queue_traps(0);
2553 assert_eq!(
2554 trap_queueing_enabled.load(Ordering::SeqCst),
2555 0,
2556 "c:1026 — TRAPSASYNC=on must block queueing even with wait_cmd=0"
2557 );
2558
2559 // Restore.
2560 dosetopt(TRAPSASYNC, if saved { 1 } else { 0 }, 0);
2561 trap_queueing_enabled.store(0, Ordering::SeqCst);
2562 }
2563
2564 /// `Src/signals.c:696-699` — `settrap` rejects trapping
2565 /// SIGTTOU/SIGTSTP/SIGTTIN when `jobbing` (= `isset(MONITOR)`).
2566 /// Pin:
2567 /// * MONITOR unset → settrap on SIGTSTP succeeds (returns 0).
2568 /// * MONITOR set → settrap on SIGTSTP rejected (returns 1).
2569 #[cfg(unix)]
2570 #[test]
2571 fn settrap_rejects_job_control_signals_when_monitor_set() {
2572 let _g = crate::test_util::global_state_lock();
2573 // Save current MONITOR state; restore at end.
2574 let saved = isset(MONITOR);
2575 // MONITOR off → trapping SIGTSTP is allowed.
2576 dosetopt(MONITOR, 0, 0);
2577 assert_eq!(
2578 settrap(libc::SIGTSTP, None, 0),
2579 0,
2580 "c:696 — MONITOR off → settrap on SIGTSTP succeeds"
2581 );
2582 // Cleanup our successful set.
2583 unsettrap(libc::SIGTSTP);
2584
2585 // MONITOR on → trapping SIGTSTP is rejected.
2586 // Use force=1 to bypass the c:854 SHTTY check (tests have
2587 // no real tty); we only care that the option flag flips,
2588 // not the pgrp acquisition side effect.
2589 dosetopt(MONITOR, 1, 1);
2590 assert_eq!(
2591 settrap(libc::SIGTSTP, None, 0),
2592 1,
2593 "c:696-699 — MONITOR on → settrap on SIGTSTP rejected"
2594 );
2595 assert_eq!(
2596 settrap(libc::SIGTTOU, None, 0),
2597 1,
2598 "c:696-699 — SIGTTOU also rejected under MONITOR"
2599 );
2600 assert_eq!(
2601 settrap(libc::SIGTTIN, None, 0),
2602 1,
2603 "c:696-699 — SIGTTIN also rejected under MONITOR"
2604 );
2605
2606 // Restore prior MONITOR state (also force=1 to bypass tty check).
2607 dosetopt(MONITOR, if saved { 1 } else { 0 }, 1);
2608 }
2609
2610 /// Pin: `killrunjobs` short-circuits when `HUP` option is
2611 /// unset per `Src/signals.c:512` (`if (unset(HUP)) return;`).
2612 /// Pin the side-effect-free path; testing the killpg side is
2613 /// inherently hostile (would kill real process groups) and
2614 /// requires fork+exec scaffolding outside unit-test scope.
2615 #[cfg(unix)]
2616 #[test]
2617 fn killrunjobs_short_circuits_when_hup_unset() {
2618 let _g = crate::test_util::global_state_lock();
2619 let saved = isset(HUP);
2620 // Force HUP off; killrunjobs must return immediately.
2621 dosetopt(HUP, 0, 0);
2622 // If the body iterated, it would try to read JOBTAB. With
2623 // no jobs added it would return without doing anything. The
2624 // contract pinned here: this returns without panicking
2625 // regardless of jobtab state.
2626 killrunjobs(0);
2627 killrunjobs(1);
2628 // Restore.
2629 dosetopt(HUP, if saved { 1 } else { 0 }, 0);
2630 }
2631
2632 // ═══════════════════════════════════════════════════════════════════
2633 // C-parity tests pinning Src/signals.c.
2634 // ═══════════════════════════════════════════════════════════════════
2635
2636 /// `intr()` is a no-op when not interactive. C:
2637 /// `if (interact) install_handler(SIGINT);`
2638 #[test]
2639 fn intr_returns_without_panic() {
2640 let _g = crate::test_util::global_state_lock();
2641 intr();
2642 intr();
2643 intr();
2644 }
2645
2646 /// `nointr()` symmetric no-op when not interactive.
2647 #[test]
2648 fn nointr_returns_without_panic() {
2649 let _g = crate::test_util::global_state_lock();
2650 nointr();
2651 nointr();
2652 }
2653
2654 /// `holdintr()`/`noholdintr()` block-then-unblock SIGINT.
2655 #[test]
2656 fn holdintr_noholdintr_round_trip_no_panic() {
2657 let _g = crate::test_util::global_state_lock();
2658 holdintr();
2659 holdintr();
2660 noholdintr();
2661 noholdintr();
2662 }
2663
2664 /// `signal_mask(SIGTERM)` returns sigset with ONLY SIGTERM set.
2665 /// C `Src/signals.c:194` — sigemptyset + sigaddset(SIGTERM).
2666 #[cfg(unix)]
2667 #[test]
2668 fn signal_mask_sigterm_isolates_signal() {
2669 let _g = crate::test_util::global_state_lock();
2670 let mask = signal_mask(libc::SIGTERM);
2671 let has_term = unsafe { libc::sigismember(&mask, libc::SIGTERM) };
2672 let has_int = unsafe { libc::sigismember(&mask, libc::SIGINT) };
2673 assert_eq!(has_term, 1, "SIGTERM bit must be set");
2674 assert_eq!(has_int, 0, "SIGINT bit must NOT be set");
2675 }
2676
2677 /// `signal_block` + `signal_unblock` round-trip safely.
2678 #[cfg(unix)]
2679 #[test]
2680 fn signal_block_unblock_round_trip_no_panic() {
2681 let _g = crate::test_util::global_state_lock();
2682 let mask = signal_mask(libc::SIGUSR1);
2683 let prev = signal_block(&mask);
2684 let _ = signal_unblock(&mask);
2685 let _ = signal_setmask(&prev);
2686 }
2687
2688 // ═══════════════════════════════════════════════════════════════════
2689 // C-parity tests pinning Src/signals.c contracts.
2690 // ═══════════════════════════════════════════════════════════════════
2691
2692 /// c:1304 — `rtsigno` returns None for non-RT names. Pin so any
2693 /// accidental fall-through that returns Some(0) is caught (would
2694 /// alias as a valid signal number → null-signal kill).
2695 #[cfg(target_os = "linux")]
2696 #[test]
2697 fn rtsigno_returns_none_for_non_rt_names() {
2698 let _g = crate::test_util::global_state_lock();
2699 assert_eq!(rtsigno("TERM"), None);
2700 assert_eq!(rtsigno("KILL"), None);
2701 assert_eq!(rtsigno(""), None);
2702 assert_eq!(rtsigno("RT"), None); // RTMIN/RTMAX prefix incomplete
2703 }
2704
2705 /// c:1310 — offset exceeding maxofs returns None.
2706 #[cfg(target_os = "linux")]
2707 #[test]
2708 fn rtsigno_returns_none_for_offset_overflow() {
2709 let _g = crate::test_util::global_state_lock();
2710 assert_eq!(rtsigno("RTMIN+999"), None);
2711 assert_eq!(rtsigno("RTMAX-999"), None);
2712 }
2713
2714 /// c:1313 — trailing non-op chars after RTMIN/RTMAX → None.
2715 #[cfg(target_os = "linux")]
2716 #[test]
2717 fn rtsigno_returns_none_for_bad_trailing_chars() {
2718 let _g = crate::test_util::global_state_lock();
2719 assert_eq!(rtsigno("RTMINx"), None);
2720 assert_eq!(rtsigno("RTMAX-abc"), None);
2721 }
2722
2723 /// c:1300 — `RTMIN` with no offset returns SIGRTMIN.
2724 #[cfg(target_os = "linux")]
2725 #[test]
2726 fn rtsigno_bare_rtmin_returns_sigrtmin() {
2727 let _g = crate::test_util::global_state_lock();
2728 assert_eq!(rtsigno("RTMIN"), Some(libc::SIGRTMIN()));
2729 }
2730
2731 /// c:1302 — `RTMAX` with no offset returns SIGRTMAX.
2732 #[cfg(target_os = "linux")]
2733 #[test]
2734 fn rtsigno_bare_rtmax_returns_sigrtmax() {
2735 let _g = crate::test_util::global_state_lock();
2736 assert_eq!(rtsigno("RTMAX"), Some(libc::SIGRTMAX()));
2737 }
2738
2739 /// c:1307 — RTMIN+0 == RTMIN; RTMAX-0 == RTMAX (offset of 0 valid).
2740 #[cfg(target_os = "linux")]
2741 #[test]
2742 fn rtsigno_zero_offset_equals_bare() {
2743 let _g = crate::test_util::global_state_lock();
2744 assert_eq!(rtsigno("RTMIN+0"), Some(libc::SIGRTMIN()));
2745 assert_eq!(rtsigno("RTMAX-0"), Some(libc::SIGRTMAX()));
2746 }
2747
2748 /// c:1307 — wrong-direction offset rejected: RTMIN-N and RTMAX+N
2749 /// fall through (RTMIN only accepts `+`, RTMAX only accepts `-`).
2750 #[cfg(target_os = "linux")]
2751 #[test]
2752 fn rtsigno_wrong_direction_offset_returns_none() {
2753 let _g = crate::test_util::global_state_lock();
2754 assert_eq!(rtsigno("RTMIN-1"), None); // RTMIN doesn't take minus
2755 assert_eq!(rtsigno("RTMAX+1"), None); // RTMAX doesn't take plus
2756 }
2757
2758 /// `signal_mask` with negative signal is harmless (just empty set).
2759 #[cfg(unix)]
2760 #[test]
2761 fn signal_mask_with_negative_sig_does_not_panic() {
2762 let _g = crate::test_util::global_state_lock();
2763 // sigaddset rejects invalid signals but doesn't crash.
2764 let _mask = signal_mask(-1);
2765 }
2766
2767 /// `signal_setmask` round-trip: saving current mask, replacing,
2768 /// then restoring should leave process state unchanged.
2769 #[cfg(unix)]
2770 #[test]
2771 fn signal_setmask_round_trip_preserves_mask() {
2772 let _g = crate::test_util::global_state_lock();
2773 unsafe {
2774 let mut current: libc::sigset_t = std::mem::zeroed();
2775 libc::sigprocmask(libc::SIG_BLOCK, std::ptr::null(), &mut current);
2776 let new_mask = signal_mask(libc::SIGUSR2);
2777 let old = signal_setmask(&new_mask);
2778 let _ = signal_setmask(&old);
2779 // No assertion on final state — pin: no panic.
2780 }
2781 }
2782
2783 /// `kill(0, 0)` is a no-op null-check — should not error.
2784 #[cfg(unix)]
2785 #[test]
2786 fn kill_with_sig_zero_is_a_null_check() {
2787 let _g = crate::test_util::global_state_lock();
2788 // sig=0 → no signal sent; rc encodes whether the process exists.
2789 let _ = kill(std::process::id() as i32, 0);
2790 }
2791
2792 /// `killpg` accepts negative pgrp values (libc passthrough).
2793 /// Pin no-panic — the syscall handles validation.
2794 #[cfg(unix)]
2795 #[test]
2796 fn killpg_with_invalid_pgrp_does_not_panic() {
2797 let _g = crate::test_util::global_state_lock();
2798 let _ = killpg(-1, 0);
2799 }
2800
2801 /// `intr` / `nointr` are idempotent — calling either twice in a
2802 /// row should not leave residual state changes.
2803 #[test]
2804 fn intr_nointr_pair_idempotent() {
2805 let _g = crate::test_util::global_state_lock();
2806 intr();
2807 intr();
2808 nointr();
2809 nointr();
2810 }
2811
2812 /// `set_interact` / `is_interact` round-trip via the option flag.
2813 /// Pin that the setter actually flips the predicate (this caught
2814 /// the historical bug where set_interact wrote to a dead AtomicBool).
2815 #[test]
2816 fn set_interact_round_trip_flips_predicate() {
2817 let _g = crate::test_util::global_state_lock();
2818 let saved = is_interact();
2819 set_interact(true);
2820 assert!(is_interact(), "set_interact(true) must set predicate");
2821 set_interact(false);
2822 assert!(!is_interact(), "set_interact(false) must clear predicate");
2823 set_interact(saved);
2824 }
2825
2826 /// `signal_mask(0)` matches `signal_mask(SIGTERM)` minus SIGTERM:
2827 /// signal 0 alone produces an empty set.
2828 #[cfg(unix)]
2829 #[test]
2830 fn signal_mask_zero_has_no_sigterm() {
2831 let _g = crate::test_util::global_state_lock();
2832 let m = signal_mask(0);
2833 let has_term = unsafe { libc::sigismember(&m, libc::SIGTERM) };
2834 assert_eq!(has_term, 0, "signal 0 must not add SIGTERM");
2835 }
2836
2837 // ═══════════════════════════════════════════════════════════════════
2838 // Additional C-parity tests for Src/signals.c
2839 // c:160 signal_mask / c:1291 rtsigno / c:1317 rtsigname
2840 // ═══════════════════════════════════════════════════════════════════
2841
2842 /// c:160 — `signal_mask(SIG)` only contains SIG, no other signal.
2843 #[cfg(unix)]
2844 #[test]
2845 fn signal_mask_isolates_only_requested_signal() {
2846 let _g = crate::test_util::global_state_lock();
2847 let m = signal_mask(libc::SIGUSR1);
2848 for s in [libc::SIGTERM, libc::SIGINT, libc::SIGUSR2, libc::SIGHUP] {
2849 let has = unsafe { libc::sigismember(&m, s) };
2850 assert_eq!(has, 0, "signal_mask(SIGUSR1) must not include {}", s);
2851 }
2852 let has_usr1 = unsafe { libc::sigismember(&m, libc::SIGUSR1) };
2853 assert_eq!(has_usr1, 1, "signal_mask(SIGUSR1) MUST include SIGUSR1");
2854 }
2855
2856 /// c:160 — `signal_mask` is a pure function (same input → same output,
2857 /// no side effects).
2858 #[cfg(unix)]
2859 #[test]
2860 fn signal_mask_is_pure() {
2861 let _g = crate::test_util::global_state_lock();
2862 for sig in [libc::SIGINT, libc::SIGTERM, libc::SIGUSR1] {
2863 let m1 = signal_mask(sig);
2864 let m2 = signal_mask(sig);
2865 for s in [libc::SIGINT, libc::SIGTERM, libc::SIGUSR1, libc::SIGUSR2] {
2866 let h1 = unsafe { libc::sigismember(&m1, s) };
2867 let h2 = unsafe { libc::sigismember(&m2, s) };
2868 assert_eq!(h1, h2, "signal_mask must be pure for sig {}", sig);
2869 }
2870 }
2871 }
2872
2873 /// c:1291 — `rtsigno("RTMIN")` returns Some(SIGRTMIN) on Linux,
2874 /// None elsewhere.
2875 #[test]
2876 fn rtsigno_bare_rtmin_smoke() {
2877 #[cfg(target_os = "linux")]
2878 {
2879 assert_eq!(rtsigno("RTMIN"), Some(libc::SIGRTMIN()));
2880 }
2881 #[cfg(not(target_os = "linux"))]
2882 {
2883 assert_eq!(rtsigno("RTMIN"), None, "non-linux: no rt signals");
2884 }
2885 }
2886
2887 /// c:1291 — empty string is not a valid rt signal name.
2888 #[test]
2889 fn rtsigno_empty_string_returns_none() {
2890 assert_eq!(rtsigno(""), None);
2891 }
2892
2893 /// c:1291 — random non-RT names return None.
2894 #[test]
2895 fn rtsigno_arbitrary_names_return_none() {
2896 for s in ["INT", "TERM", "rtmin", "RTMINX", "RT", "MIN", "SIGRTMIN"] {
2897 assert_eq!(rtsigno(s), None, "{:?} must not parse as RT name", s);
2898 }
2899 }
2900
2901 /// c:1291 — `rtsigno("RTMIN+0")` equals `rtsigno("RTMIN")` (zero offset
2902 /// is a no-op).
2903 #[test]
2904 #[cfg(target_os = "linux")]
2905 fn rtsigno_rtmin_plus_zero_equals_bare_rtmin() {
2906 assert_eq!(rtsigno("RTMIN+0"), rtsigno("RTMIN"));
2907 }
2908
2909 /// c:1317 — `rtsigname(SIG)` returns empty string for out-of-range.
2910 #[test]
2911 #[cfg(target_os = "linux")]
2912 fn rtsigname_out_of_range_returns_empty() {
2913 let too_low = libc::SIGRTMIN() - 1;
2914 let too_high = libc::SIGRTMAX() + 1;
2915 assert_eq!(rtsigname(too_low), "");
2916 assert_eq!(rtsigname(too_high), "");
2917 assert_eq!(rtsigname(0), "");
2918 assert_eq!(rtsigname(-1), "");
2919 }
2920
2921 /// c:1317 — `rtsigname(SIGRTMIN)` returns "RTMIN" (zero offset).
2922 #[test]
2923 #[cfg(target_os = "linux")]
2924 fn rtsigname_sigrtmin_returns_rtmin() {
2925 assert_eq!(rtsigname(libc::SIGRTMIN()), "RTMIN");
2926 }
2927
2928 /// c:1317 — `rtsigname(SIGRTMAX)` returns "RTMAX" (zero offset).
2929 #[test]
2930 #[cfg(target_os = "linux")]
2931 fn rtsigname_sigrtmax_returns_rtmax() {
2932 assert_eq!(rtsigname(libc::SIGRTMAX()), "RTMAX");
2933 }
2934
2935 /// c:1317 — `rtsigname` and `rtsigno` round-trip for SIGRTMIN.
2936 #[test]
2937 #[cfg(target_os = "linux")]
2938 fn rtsigname_rtsigno_round_trip_sigrtmin() {
2939 let s = rtsigname(libc::SIGRTMIN());
2940 let n = rtsigno(&s);
2941 assert_eq!(n, Some(libc::SIGRTMIN()));
2942 }
2943
2944 /// c:175 — `signal_block(empty)` is a no-op (returns current mask,
2945 /// blocks nothing new).
2946 #[cfg(unix)]
2947 #[test]
2948 fn signal_block_empty_set_no_panic() {
2949 let _g = crate::test_util::global_state_lock();
2950 let empty: libc::sigset_t = unsafe {
2951 let mut s: libc::sigset_t = std::mem::zeroed();
2952 libc::sigemptyset(&mut s);
2953 s
2954 };
2955 let _prev = signal_block(&empty);
2956 // Restore (block-empty + unblock-empty is round-trip-safe).
2957 let _ = signal_unblock(&empty);
2958 }
2959
2960 // ═══════════════════════════════════════════════════════════════════
2961 // Additional C-parity tests for Src/signals.c
2962 // c:107 intr / c:131 nointr / c:152 holdintr / c:171 noholdintr /
2963 // c:2050 kill / c:2045 killpg / c:2071 set_interact / c:2091 is_interact
2964 // ═══════════════════════════════════════════════════════════════════
2965
2966 /// c:107 — `intr` is idempotent (callable repeatedly).
2967 #[test]
2968 fn intr_idempotent_full_sweep() {
2969 let _g = crate::test_util::global_state_lock();
2970 for _ in 0..10 {
2971 intr();
2972 }
2973 }
2974
2975 /// c:131 — `nointr` is idempotent.
2976 #[test]
2977 fn nointr_idempotent_full_sweep() {
2978 let _g = crate::test_util::global_state_lock();
2979 for _ in 0..10 {
2980 nointr();
2981 }
2982 }
2983
2984 /// c:152 — `holdintr` is idempotent.
2985 #[test]
2986 fn holdintr_idempotent() {
2987 let _g = crate::test_util::global_state_lock();
2988 for _ in 0..10 {
2989 holdintr();
2990 }
2991 // Restore via noholdintr to match push/pop semantic.
2992 for _ in 0..10 {
2993 noholdintr();
2994 }
2995 }
2996
2997 /// c:2050 — `kill(0, 0)` self-check returns i32 (type pin).
2998 /// kill(pid=0, sig=0) is a permission check: returns 0 if alive +
2999 /// permitted, -1 otherwise. Safe in test context.
3000 #[test]
3001 fn kill_self_check_returns_i32_type() {
3002 let _g = crate::test_util::global_state_lock();
3003 let _: i32 = kill(0, 0);
3004 }
3005
3006 /// c:2045 — `killpg(0, 0)` returns i32 (compile-time type pin).
3007 #[test]
3008 fn killpg_returns_i32_type() {
3009 let _g = crate::test_util::global_state_lock();
3010 let _: i32 = killpg(0, 0);
3011 }
3012
3013 /// c:2071 + c:2091 — `set_interact(false)` then `is_interact()` round-trips.
3014 #[test]
3015 fn set_interact_round_trip_preserves_value() {
3016 let _g = crate::test_util::global_state_lock();
3017 let saved = is_interact();
3018 set_interact(true);
3019 assert!(is_interact(), "set(true) → true");
3020 set_interact(false);
3021 assert!(!is_interact(), "set(false) → false");
3022 set_interact(saved);
3023 }
3024
3025 /// c:2091 — `is_interact()` returns bool (compile-time type pin).
3026 #[test]
3027 fn is_interact_returns_bool_type() {
3028 let _g = crate::test_util::global_state_lock();
3029 let _: bool = is_interact();
3030 }
3031
3032 /// c:194 — `signal_mask(SIGINT|SIGTERM)` masks differ from
3033 /// `signal_mask(SIGINT)` alone.
3034 #[cfg(unix)]
3035 #[test]
3036 fn signal_mask_two_signals_differ_from_one() {
3037 let _g = crate::test_util::global_state_lock();
3038 let one = signal_mask(libc::SIGINT);
3039 let single_has_term = unsafe { libc::sigismember(&one, libc::SIGTERM) };
3040 assert_eq!(
3041 single_has_term, 0,
3042 "single SIGINT mask must NOT contain SIGTERM"
3043 );
3044 }
3045
3046 /// c:217 — `signal_block` returns sigset_t (compile-time type pin).
3047 #[cfg(unix)]
3048 #[test]
3049 fn signal_block_returns_sigset_t_type() {
3050 let _g = crate::test_util::global_state_lock();
3051 let empty: libc::sigset_t = unsafe {
3052 let mut s: libc::sigset_t = std::mem::zeroed();
3053 libc::sigemptyset(&mut s);
3054 s
3055 };
3056 let _: libc::sigset_t = signal_block(&empty);
3057 // Cleanup
3058 let _ = signal_unblock(&empty);
3059 }
3060
3061 /// c:246 — `signal_setmask` returns sigset_t (compile-time type pin).
3062 #[cfg(unix)]
3063 #[test]
3064 fn signal_setmask_returns_sigset_t_type() {
3065 let _g = crate::test_util::global_state_lock();
3066 let cur = signal_block(&unsafe {
3067 let mut s: libc::sigset_t = std::mem::zeroed();
3068 libc::sigemptyset(&mut s);
3069 s
3070 });
3071 let _: libc::sigset_t = signal_setmask(&cur);
3072 }
3073
3074 // ═══════════════════════════════════════════════════════════════════
3075 // Additional C-parity tests for Src/signals.c
3076 // c:107 intr/nointr / c:152 holdintr/noholdintr / c:194 signal_mask /
3077 // c:1791 rtsigno / c:1868 rtsigname / c:2050 kill / c:2071 set_interact
3078 // ═══════════════════════════════════════════════════════════════════
3079
3080 /// c:107 — `intr` is idempotent.
3081 #[test]
3082 fn intr_idempotent() {
3083 let _g = crate::test_util::global_state_lock();
3084 for _ in 0..10 {
3085 intr();
3086 }
3087 }
3088
3089 /// c:131 — `nointr` is idempotent.
3090 #[test]
3091 fn nointr_idempotent() {
3092 let _g = crate::test_util::global_state_lock();
3093 for _ in 0..10 {
3094 nointr();
3095 }
3096 }
3097
3098 /// c:152 — `holdintr` is idempotent (alt with 10-call loop).
3099 #[test]
3100 fn holdintr_idempotent_alt() {
3101 let _g = crate::test_util::global_state_lock();
3102 for _ in 0..10 {
3103 holdintr();
3104 }
3105 }
3106
3107 /// c:171 — `noholdintr` is idempotent.
3108 #[test]
3109 fn noholdintr_idempotent() {
3110 let _g = crate::test_util::global_state_lock();
3111 for _ in 0..10 {
3112 noholdintr();
3113 }
3114 }
3115
3116 /// c:194 — `signal_mask(SIGINT)` returns sigset containing SIGINT.
3117 #[cfg(unix)]
3118 #[test]
3119 fn signal_mask_sigint_contains_sigint() {
3120 let _g = crate::test_util::global_state_lock();
3121 let m = signal_mask(libc::SIGINT);
3122 let contains = unsafe { libc::sigismember(&m, libc::SIGINT) };
3123 assert_eq!(contains, 1, "signal_mask(SIGINT) must contain SIGINT");
3124 }
3125
3126 /// c:1791 — `rtsigno` returns Option<i32> (compile-time pin).
3127 #[test]
3128 fn rtsigno_returns_option_i32_type() {
3129 let _g = crate::test_util::global_state_lock();
3130 let _: Option<i32> = rtsigno("RTMIN");
3131 }
3132
3133 /// c:1791 — `rtsigno("")` empty name returns None.
3134 #[test]
3135 fn rtsigno_empty_returns_none() {
3136 let _g = crate::test_util::global_state_lock();
3137 assert!(rtsigno("").is_none(), "empty name → None");
3138 }
3139
3140 /// c:1791 — `rtsigno` for nonsense name returns None.
3141 #[test]
3142 fn rtsigno_nonsense_returns_none() {
3143 let _g = crate::test_util::global_state_lock();
3144 assert!(rtsigno("__bogus_rtsig_xyz__").is_none());
3145 }
3146
3147 /// c:1868 — `rtsigname` returns String (compile-time pin).
3148 #[test]
3149 fn rtsigname_returns_string_type() {
3150 let _g = crate::test_util::global_state_lock();
3151 let _: String = rtsigname(0);
3152 }
3153
3154 /// c:2050 — `kill(self, 0)` (existence check) returns i32.
3155 #[cfg(unix)]
3156 #[test]
3157 fn kill_self_pid_zero_sig_returns_i32_alt() {
3158 let _g = crate::test_util::global_state_lock();
3159 let pid = unsafe { libc::getpid() };
3160 let _: i32 = kill(pid, 0);
3161 }
3162
3163 /// c:2071/2091 — `set_interact(true)` twice still leaves true.
3164 #[test]
3165 fn set_interact_twice_true_still_true() {
3166 let _g = crate::test_util::global_state_lock();
3167 let saved = is_interact();
3168 set_interact(true);
3169 assert!(is_interact());
3170 set_interact(true);
3171 assert!(is_interact(), "set(true) twice still true");
3172 set_interact(saved);
3173 }
3174
3175 // ═══════════════════════════════════════════════════════════════════
3176 // Additional C-parity pins for Src/signals.c
3177 // c:194 signal_mask / c:1791 rtsigno / c:1868 rtsigname /
3178 // c:2045 killpg / c:2050 kill / c:2071/2091 set/is_interact /
3179 // c:107-194 intr/holdintr family / c:1326 queue_traps / c:1339 unqueue_traps
3180 // ═══════════════════════════════════════════════════════════════════
3181
3182 /// c:1791 — `rtsigno` is for REAL-TIME signal names only (e.g.
3183 /// `RTMIN+1`); regular signal names like `TERM`/`INT` are NOT
3184 /// matched here. Pin the documented contract.
3185 #[test]
3186 fn rtsigno_rejects_regular_signal_names() {
3187 assert_eq!(
3188 rtsigno("TERM"),
3189 None,
3190 "rtsigno is rt-only; TERM (regular) must return None"
3191 );
3192 assert_eq!(
3193 rtsigno("INT"),
3194 None,
3195 "rtsigno is rt-only; INT (regular) must return None"
3196 );
3197 }
3198
3199 /// c:1791 — `rtsigno("0")` empty signal name path doesn't panic.
3200 #[test]
3201 fn rtsigno_numeric_zero_no_panic() {
3202 let _ = rtsigno("0");
3203 }
3204
3205 /// c:1868 — `rtsigname(invalid)` (negative or huge) doesn't panic.
3206 #[test]
3207 fn rtsigname_extreme_values_no_panic() {
3208 let _ = rtsigname(-1);
3209 let _ = rtsigname(99999);
3210 let _ = rtsigname(0);
3211 }
3212
3213 /// c:1868 — `rtsigname` is deterministic for same input.
3214 #[test]
3215 fn rtsigname_deterministic() {
3216 for sig in [1, 2, 9, 15, 0] {
3217 let a = rtsigname(sig);
3218 let b = rtsigname(sig);
3219 assert_eq!(a, b, "rtsigname({}) must be deterministic", sig);
3220 }
3221 }
3222
3223 /// c:194 — `signal_mask(0)` doesn't panic.
3224 #[test]
3225 fn signal_mask_signal_zero_no_panic() {
3226 let _g = crate::test_util::global_state_lock();
3227 let _: libc::sigset_t = signal_mask(0);
3228 }
3229
3230 /// c:194 — `signal_mask` returns sigset_t (type pin).
3231 #[test]
3232 fn signal_mask_returns_sigset_t_type() {
3233 let _g = crate::test_util::global_state_lock();
3234 let _: libc::sigset_t = signal_mask(libc::SIGUSR1);
3235 }
3236
3237 /// c:2071/2091 — set_interact(false) then is_interact() returns false.
3238 #[test]
3239 fn set_interact_false_round_trip() {
3240 let _g = crate::test_util::global_state_lock();
3241 let saved = is_interact();
3242 set_interact(false);
3243 assert!(
3244 !is_interact(),
3245 "after set(false), is_interact must be false"
3246 );
3247 set_interact(saved);
3248 }
3249
3250 /// c:2071/2091 — set_interact() alternation is monotonic w.r.t. observed value.
3251 #[test]
3252 fn set_interact_alternation_round_trip() {
3253 let _g = crate::test_util::global_state_lock();
3254 let saved = is_interact();
3255 set_interact(true);
3256 assert!(is_interact());
3257 set_interact(false);
3258 assert!(!is_interact());
3259 set_interact(true);
3260 assert!(is_interact());
3261 set_interact(false);
3262 assert!(!is_interact());
3263 set_interact(saved);
3264 }
3265
3266 /// c:1326/1339 — queue_traps + unqueue_traps round-trip is safe.
3267 #[test]
3268 fn queue_unqueue_traps_round_trip_safe() {
3269 let _g = crate::test_util::global_state_lock();
3270 queue_traps(0);
3271 unqueue_traps();
3272 queue_traps(1);
3273 unqueue_traps();
3274 }
3275
3276 /// c:1339 — `unqueue_traps` on empty queue safe.
3277 #[test]
3278 fn unqueue_traps_on_empty_queue_no_panic() {
3279 let _g = crate::test_util::global_state_lock();
3280 for _ in 0..10 {
3281 unqueue_traps();
3282 }
3283 }
3284
3285 /// c:107/131 — intr/nointr alternation safe.
3286 #[test]
3287 fn intr_nointr_alternation_safe() {
3288 let _g = crate::test_util::global_state_lock();
3289 for _ in 0..5 {
3290 intr();
3291 nointr();
3292 }
3293 }
3294
3295 /// c:152/171 — holdintr/noholdintr alternation safe.
3296 #[test]
3297 fn holdintr_noholdintr_alternation_safe() {
3298 let _g = crate::test_util::global_state_lock();
3299 for _ in 0..5 {
3300 holdintr();
3301 noholdintr();
3302 }
3303 }
3304}