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
//! macOS has no direct equivalent to Linux's `prctl(PR_SET_PDEATHSIG, ...)`.
//! Darwin/BSD's own idiomatic "watch another process die" mechanism is
//! `kqueue`'s `EVFILT_PROC`/`NOTE_EXIT`, which (unlike `prctl`) delivers
//! nothing automatically - something has to actually block on the kqueue
//! waiting for the event. `arm_pdeathsig` below spawns a dedicated OS
//! thread for exactly that (not a tokio task: this runs very early, before
//! anything async-runtime-shaped is assumed ready, and the wait itself is
//! a blocking `kevent` call that would starve an executor thread) and,
//! once it sees the real parent exit, sends itself `SIGTERM` - not
//! `SIGKILL`, the same choice `linux.rs` makes and for the same reason:
//! gives an orphaned replacement a chance to run its own graceful-shutdown
//! path first.
//!
//! Mirrors both real bugs `linux.rs`'s own doc comment documents in full:
//! - **The spawn-to-arm race.** If the real parent already exited by the
//! time this runs, this process has already been reparented to
//! `launchd` (Darwin's init-equivalent, pid 1) - `getppid() == 1` is
//! checked directly and exits immediately, rather than registering a
//! kqueue watch against a parent PID that may already be gone (and,
//! worse, may since have been *reused* by an unrelated process).
//! - **The disarm requirement.** This process's own predecessor is
//! *expected* to exit, deliberately, the moment its own drain completes,
//! the successful conclusion of every ordinary handoff. Left watching,
//! that completely normal exit would be indistinguishable from a real
//! crash. `disarm_pdeathsig` must stop the watcher reacting to it before
//! this process starts actually serving.
//!
//! **Why the watcher thread closes its own kqueue fd, never `disarm_pdeathsig`
//! from another thread:** closing a file descriptor while a *different*
//! thread is blocked on it in a syscall is a well-known hazard - the
//! blocked call's behavior is unreliable, and if the fd number happens to
//! get reused by something else in the process before the blocked thread
//! notices, operations meant for the old resource can land on the new one
//! instead. `disarm_pdeathsig` never touches the fd directly: it submits a
//! second, independent `EVFILT_USER` event on the *same* kqueue, which
//! wakes the watcher thread through the completely ordinary "a new event
//! is ready" path `kevent` already handles - the watcher then closes its
//! own fd itself, from the one thread that was ever blocked on it, once
//! it's done.
use ;
/// Identifies `disarm_pdeathsig`'s own wake-up event among a kqueue that
/// also watches a real process exit - an arbitrary, fixed value (`kqueue`
/// never generates idents of its own for `EVFILT_USER`; the caller picks
/// one), distinct in *filter* from the `EVFILT_PROC` registration, so a
/// wake from this event is never mistaken for the parent actually exiting.
const DISARM_EVENT_IDENT: uintptr_t = 1;
/// The armed watcher's own kqueue fd - `-1` when disarmed or never armed.
/// A plain `AtomicI32`, not a `Mutex`: the only two operations against it
/// are "set once, from `arm_pdeathsig`" and "read-and-clear, from
/// `disarm_pdeathsig`" - independent, single accesses with nothing larger
/// to coordinate.
static KQUEUE_FD: AtomicI32 = new;
/// See this module's own doc comment for why this is a background thread
/// blocking on `kqueue`, not a one-shot call the way Linux's `prctl`-based
/// version is.
pub
/// Blocks on exactly one of the two events `arm_pdeathsig` registered:
/// either the watched parent actually exiting (`EVFILT_PROC`) or
/// `disarm_pdeathsig`'s own wake-up (`EVFILT_USER`) - see this module's
/// own doc comment for why the fd is closed from here, not from
/// `disarm_pdeathsig` on a different thread.
/// Wakes the watcher thread `arm_pdeathsig` spawned, so it stops watching
/// before this process starts actually serving - see this module's own
/// doc comment for why that matters (this process's own predecessor is
/// *expected* to exit at the end of a successful handoff).
pub