1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Startup signal-state recording for the dispatch paths that do not run
//! `ported::init::zsh_main`.
//!
//! !!! WARNING: RUST-ONLY MODULE — NO C COUNTERPART FILE !!!
//! The BEHAVIOUR here is a faithful port of two lines of
//! `Src/init.c:1444-1445`, but the standalone home is a Rust-only
//! adaptation, so it lives under `src/extensions/` rather than
//! `src/ported/` (where `tests/port_purity.rs` and the build gate require
//! every fn to map to a real `Src/<x>.c` function).
//!
//! Why it exists: C's `main` has a SINGLE path —
//! `parseargs` -> `init_io` -> `setupvals` -> `init_signals` -> run — so
//! every invocation, `-c` included, reaches `init_signals`. zshrs
//! dispatches `-c` and a script FILE inside `bins/zshrs.rs` without going
//! through `ported::init::zsh_main`, so `init_signals` never ran for them.
//!
//! Calling the WHOLE of `init_signals` from those paths is not a safe
//! substitute: it also installs C's SIGCHLD handler, whose reaper then
//! races the pipeline's own `waitpid` and destroys `$pipestatus`
//! (measured with that call wired in: `parity-fuzz --mode pipeline` went
//! from 0 to 139 divergences, `jobs` 0 -> 16, `errexit` 0 -> 13). So
//! `init_dispatch_signals` below replays `init_signals` line by line with
//! that ONE line left out, until those dispatch paths are converged onto
//! `zsh_main`.
/// Whether the inherited-signal bookkeeping below applies at all.
///
/// This is ZSH behaviour, not shared POSIX behaviour, so it must not
/// leak into the drop-in emulation modes. Measured with SIGQUIT and
/// SIGHUP both inherited as SIG_IGN, `<shell> -c trap` prints:
/// zsh `trap -- '' QUIT` (QUIT only, bare name)
/// bash `trap -- '' SIGHUP` + `SIGQUIT` (both, SIG-prefixed)
/// dash nothing
/// ksh nothing
/// sh nothing
/// so recording it unconditionally made every POSIX-family drop-in
/// print a QUIT line the real shell never prints. bash's variant is a
/// different shape (both signals, SIG names) and is not implemented
/// here — it would need its own port rather than reusing zsh's.
/// Record an INHERITED `SIG_IGN` disposition for `SIGQUIT`.
///
/// Port of `Src/init.c:1444-1445`:
/// ```c
/// if (!sigaction(SIGQUIT, NULL, &act) && act.sa_handler == SIG_IGN)
/// sigtrapped[SIGQUIT] = ZSIG_IGNORED;
/// ```
///
/// A shell that inherits SIGQUIT ignored — `nohup`, a `trap '' QUIT`
/// parent, most supervisors, and `cargo test`'s own spawn — records it as
/// an ignored trap. `trap` then lists `trap -- '' QUIT` and `entersubsh`
/// keeps it ignored in children (`c:Src/exec.c:1231`). Without this,
/// `nohup zshrs -fc trap` printed nothing where zsh printed the QUIT line.
///
/// Note this only RECORDS the inherited state; it deliberately does not
/// call `signal_ignore(SIGQUIT)` (`c:1448`), which would be a no-op for an
/// already-ignored signal anyway.
/// Non-unix stub: there is no SIGQUIT to inherit.
/// Run `init_signals` (`Src/init.c:1427-1470`) on a dispatch path that
/// does not reach `ported::init::zsh_main`.
///
/// Every line of C's `init_signals` is replayed here in C's order bar
/// one, with the measurement at its site below: `install_handler(SIGCHLD)`
/// (`c:1455`). The `sigtrapped`/`siglists`
/// allocations (`c:1431-1432`) and the `sigchld_mask` cache (`c:1440`)
/// have no zshrs counterpart, same as in the ported `init_signals`
/// itself.
///
/// Without this, `zsh -f -i -c 'kill -ALRM $$'` printed `zsh:1: timeout`
/// and exited 14 while zshrs died from the raw signal with 142: SIGALRM,
/// SIGPIPE and the SIGTERM/SIGQUIT ignores were only ever armed for a
/// shell that read its input from stdin. SIGHUP was unarmed on BOTH the
/// interactive and non-interactive `-c` path (129 vs zsh's 1).
///
/// The ORDER matters beyond tidiness: C records an inherited `SIG_IGN` on
/// SIGQUIT (`c:1444-1445`) only AFTER the interactive branch has reset
/// every disposition to default (`c:1437-1438`), so an interactive shell
/// never records it. Doing the record first — which is what the two
/// dispatch sites used to do — made `nohup zshrs -fic trap` print
/// `trap -- '' QUIT` where zsh prints nothing.
/// Non-unix stub: none of these signals exist.