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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! `stop_pid` and `restart_pid`: the lifecycle for a daemon the caller
//! identified by pid (#11085, AC10).
//!
//! Why: trusty-console keeps running when its socket bind fails, reachable
//! only through its `http_addr`. [`stop`] finds a daemon only through its
//! socket's peer, and the `/proc` scan refuses on such a daemon without
//! signalling it, so the console could not be stopped at all. The consumer
//! finds the pid itself — an `http_addr` lookup, a pid file — and this file
//! turns that pid into a stop with `stop`'s safety rules: a pidfd pin, an
//! identity and instance check before any signal, survivor detection.
//! What: [`stop_pid_with`] pins the pid, requires it to be this instance and
//! the socket to be unserved or served by it, then signals and proves the stop
//! as [`stop_with`] does. [`restart_pid_with`] does the same checks, refuses on
//! any other process of this instance before the stop, and then starts. A pid
//! that no longer exists proves nothing, so both hand over to the socket path
//! ([`stop_with`], [`restart_with`]).
//! Test: the `stop_pid_*` and `restart_pid_*` tests in `tests/linux_lifecycle.rs`.
//!
//! [`stop`]: crate::linux_lifecycle::stop()
//! [`stop_with`]: crate::linux_lifecycle::stop_with()
//! [`restart_with`]: crate::linux_lifecycle::restart_with()
//! [`stop_pid_with`]: crate::linux_lifecycle::stop_pid_with()
//! [`restart_pid_with`]: crate::linux_lifecycle::restart_pid_with()
use ;
use ;
use ;
use ;
use ;
/// Stop the daemon running as `pid`, delivering signals through the kernel.
///
/// Why: AC10 — a daemon whose socket bind failed is found by pid, not socket.
/// What: [`stop_pid_with`] with [`KernelDelivery`].
/// Test: `stop_pid_stops_a_daemon_whose_socket_bind_failed`.
///
/// [`stop_pid_with`]: crate::linux_lifecycle::stop_pid_with()
/// [`KernelDelivery`]: crate::linux_lifecycle::KernelDelivery
pub async
/// Stop the daemon running as `pid`, delivering signals through `delivery`.
///
/// Why: the caller's pid comes from a file or an address lookup, either of
/// which can be stale or name a reused pid, so the pid is a hint that must be
/// proven to be this daemon instance before it is signalled.
/// What, in order:
///
/// 1. Empty `liveness_args` is `InvalidDescriptor`: every process of the
/// binary, a stdio bridge included, would pass the instance check.
/// 2. Pin `pid` with a pidfd. No such process: the answer is [`stop_with`]'s,
/// the socket and then the `/proc` scan, so a stale pid never reads as
/// `NotRunning` while the daemon runs under another pid.
/// 3. Require `/proc/<pid>/exe` to name `desc.binary` (`IdentityMismatch` or
/// `IdentityUnreadable` otherwise). Then require the pid to be this
/// instance: this uid, every `liveness_args` element, no
/// `instance_path_args` flag naming another path, every `instance_env`
/// value — `NotThisInstance` otherwise. A path flag with no value, or a
/// relative or unresolvable one, is `InstanceUndecided`, and an unreadable
/// `cmdline` or `environ` is an error. The pidfd is checked after the
/// reads: a pid that exited mid-check goes to step 2's answer, and a reused
/// pid is never read.
/// 4. Dial the socket. Served by another pid is `ServedByAnother`, since the
/// stopped signal could not hold after the stop; an undecided dial is
/// `ProbeInconclusive` or `NoPeerPid`.
/// 5. SIGTERM, SIGKILL after `stop_grace`, `Survivor` after `kill_wait`, then
/// the stopped signal, as in [`stop_with`].
///
/// Test: `stop_pid_stops_a_daemon_whose_socket_bind_failed`,
/// `stop_pid_refuses_a_pid_that_is_not_this_instance`,
/// `stop_pid_and_restart_pid_refuse_an_undecided_instance_before_any_signal`,
/// `stop_pid_and_restart_pid_refuse_before_any_signal_when_another_pid_serves_the_socket`,
/// `stop_pid_and_restart_pid_stop_short_when_the_socket_cannot_be_probed`,
/// `stop_pid_of_a_pid_that_is_gone_answers_as_stop_does`,
/// `stop_pid_reports_a_survivor_and_restart_pid_starts_nothing`.
///
/// [`stop_with`]: crate::linux_lifecycle::stop_with()
pub async
/// [`stop_pid_with`], with the scan reading `table` instead of `/proc`
/// (#11085).
///
/// What: [`stop_pid_with`]'s steps. `pid` itself is still pinned and checked
/// in the real `/proc`; `table` is read only by the scan [`stop_with_table`]
/// runs when `pid` is gone.
/// Test: `stop_pid_of_a_pid_that_is_gone_answers_as_stop_does`,
/// `stop_pid_stops_a_daemon_whose_socket_bind_failed` (through [`stop_pid_with`]).
///
/// [`stop_pid_with`]: crate::linux_lifecycle::stop_pid_with()
/// [`stop_with_table`]: crate::linux_lifecycle::stop_with_table()
pub async
/// Restart the daemon running as `pid`, delivering signals through the kernel.
///
/// What: [`restart_pid_with`] with [`KernelDelivery`].
/// Test: `restart_pid_restarts_a_daemon_whose_socket_bind_failed`.
///
/// [`restart_pid_with`]: crate::linux_lifecycle::restart_pid_with()
/// [`KernelDelivery`]: crate::linux_lifecycle::KernelDelivery
pub async
/// [`restart_pid`] with an explicit signal delivery (the test seam).
///
/// Why: D1 — stop, prove it stopped, then start — for a daemon found by pid.
/// What: steps 1-3 of [`stop_pid_with`], with `BinaryMissing` checked first.
/// When `pid` no longer exists, this is [`restart_with`]. Otherwise, before
/// any signal: a socket served by another pid is
/// [`LifecycleError::RestartBlocked`] naming that peer, an undecided dial is
/// `ProbeInconclusive` or `NoPeerPid`, and any other process of this instance
/// is `RestartBlocked`. Then the stop, then [`start`]; every stop error
/// returns before a spawn.
/// Test: `restart_pid_restarts_a_daemon_whose_socket_bind_failed`,
/// `restart_pid_refuses_before_any_signal_when_another_process_is_this_instance`,
/// `stop_pid_and_restart_pid_refuse_before_any_signal_when_another_pid_serves_the_socket`,
/// `restart_and_restart_pid_stop_nothing_when_the_install_binary_is_missing`,
/// `stop_pid_reports_a_survivor_and_restart_pid_starts_nothing`.
///
/// [`restart_pid`]: crate::linux_lifecycle::restart_pid()
/// [`stop_pid_with`]: crate::linux_lifecycle::stop_pid_with()
/// [`restart_with`]: crate::linux_lifecycle::restart_with()
/// [`start`]: crate::linux_lifecycle::start()
/// [`LifecycleError::RestartBlocked`]: crate::linux_lifecycle::LifecycleError::RestartBlocked
pub async
/// [`restart_pid_with`], with every scan reading `table` instead of `/proc`
/// (#11085).
///
/// What: [`restart_pid_with`]'s steps. `pid` itself is still pinned and
/// checked in the real `/proc`; the scan for another instance and the
/// start's scan read `table`.
/// Test: `restart_pid_refuses_before_any_signal_when_another_process_is_this_instance`,
/// `restart_pid_restarts_a_daemon_whose_socket_bind_failed` (through
/// [`restart_pid_with`]).
///
/// [`restart_pid_with`]: crate::linux_lifecycle::restart_pid_with()
pub async
/// Step 1 of [`stop_pid_with`]: the descriptor, and `liveness_args` set.
///
/// [`stop_pid_with`]: crate::linux_lifecycle::stop_pid_with()
/// `pid`, pinned, once it is proven this instance; `None` when it is gone.
///
/// What: step 3 of [`stop_pid_with`]. `Ok(None)` only when the pidfd shows
/// the process exited (or it never existed); a live process that is not
/// proven this instance is an error, never `None`.
///
/// [`stop_pid_with`]: crate::linux_lifecycle::stop_pid_with()
/// The socket's peer when it is a pid other than `pid`; `None` when the
/// socket is not served or `pid` serves it.
///
/// What: an undecided dial is [`LifecycleError::ProbeInconclusive`] or
/// [`LifecycleError::NoPeerPid`].
///
/// [`LifecycleError::ProbeInconclusive`]: crate::linux_lifecycle::LifecycleError::ProbeInconclusive
/// [`LifecycleError::NoPeerPid`]: crate::linux_lifecycle::LifecycleError::NoPeerPid
async