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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//! What a mechanism that names its tree by the root's number did not send, as the mechanism says it
//! ([`Skip`], in the outcome of its kill and of its terminate), and how a tree call words it
//! ([`SkipCall`]).
use crate::containment::root::Refusal;
use crate::error::Error;
/// What a kill or a terminate skipped because the root's number may not name the root.
#[derive(Debug)]
#[cfg_attr(
not(unix),
allow(dead_code, reason = "only a Unix mechanism skips by the root's number")
)]
pub(crate) struct Skip {
/// What the skip left undone, as an action ("kill its process group (pgid 7)").
pub(crate) action: String,
pub(crate) why: Why,
/// Linux: the root's handle still reaches the root exactly, so the caller sends the call's signal
/// to the root alone, through its hold.
pub(crate) root_alone: bool,
/// macOS: the sweep by identity a fd marker still makes of its holders, which no reuse of a pid
/// redirects. `None`: the mechanism has none.
pub(crate) holders: Option<Result<(), Error>>,
}
#[cfg(unix)]
impl Skip {
/// Whether this handle's own reap is why the root's pid names no process or cannot be read, and
/// the handle already killed the tree: there is nothing left for the skip to leave undone.
pub(crate) fn is_moot(&self, tree: &crate::containment::Tree) -> bool {
tree.is_killed()
&& matches!(
&self.why,
Why::MayNameAnother(Refusal::ReapedPidGone(_) | Refusal::ReapedPidUnread(_))
)
}
/// The error of a sweep that skipped what the root's number names: the holders' sweep's own
/// error when it failed, else `Unassessable`, with the error behind [`why`](Skip::why) as its
/// `source`, or a peer of the holders' error's own.
#[cfg_attr(
not(target_os = "macos"),
allow(dead_code, reason = "only macOS's late sweep skips a root it cannot show ours")
)]
pub(crate) fn sweep_error(self) -> Error {
match self.holders {
Some(Err(e)) => e.with_peers(self.why.cause()),
_ => Error::Unassessable {
detail: format!(
"the root could not be shown to be ours, so its tree did not {}",
self.action
),
source: self.why.cause(),
},
}
}
}
/// Why the root's number was not used.
#[derive(Debug)]
#[cfg_attr(
not(unix),
allow(dead_code, reason = "only a Unix mechanism skips by the root's number")
)]
pub(crate) enum Why {
/// macOS: the root is `Unpinned` or `Unknown`; `why` says which, and `cause` is the error of an
/// `Unknown`, shared with the state it came from.
Untrusted {
why: String,
cause: Option<std::sync::Arc<std::io::Error>>,
},
/// The pid may name another process, for this reason.
MayNameAnother(Refusal),
}
#[cfg(unix)]
impl Why {
/// [`Why::Untrusted`] for `state`, the root's own handle's answer.
pub(crate) fn untrusted(state: &crate::signal::RootState) -> Why {
Why::Untrusted {
why: crate::signal::why_untrusted(state),
cause: match state {
crate::signal::RootState::Unknown(e) => Some(std::sync::Arc::clone(e)),
_ => None,
},
}
}
/// The error behind this reason, shared ([`crate::error::shared_io`]).
pub(crate) fn cause(&self) -> Option<std::sync::Arc<Error>> {
match self {
Why::Untrusted { cause, .. } => cause.as_ref().map(crate::error::shared_io_cause),
Why::MayNameAnother(refusal) => refusal.cause(),
}
}
/// The one `warn` of a skipped call: `detail`, then the chain of the error behind this reason.
pub(crate) fn warn(&self, detail: &str) {
match self.cause() {
Some(cause) => log::warn!("{detail}: {}", crate::error::Report(&*cause)),
None => log::warn!("{detail}"),
}
}
}
/// What a mechanism's cooperative signal to its tree did, from one look at the root. A tree call that
/// goes on to wait and sweep (`graceful_shutdown_tree`) acts on it.
#[derive(Debug)]
#[cfg_attr(
not(unix),
allow(dead_code, reason = "only a Unix mechanism skips by the root's number")
)]
pub(crate) enum TerminateOutcome {
/// The signal went out, as usual or to what names the tree by identity, and `result` is what the
/// check after it found. `earlier` names what a later sweep's error says was sent before it.
Sent {
result: Result<(), Error>,
earlier: Option<&'static str>,
},
/// Nothing was sent: the call ends with this error.
NotSent(Error),
/// Nothing is sent by the root's number, and nothing else names the tree: the call ends now.
End(Skip),
/// Linux: the root's number may name another process, but its handle names it exactly: the caller
/// shuts the root alone down through the handle, and the call ends.
#[cfg_attr(not(target_os = "linux"), allow(dead_code, reason = "only Linux has a pidfd"))]
RootThroughPidfd(Skip),
/// macOS: an fd marker's holders alone were signalled, by the pipe they hold (the result is in
/// [`Skip::holders`]): the caller waits the grace and kills any left, and the call ends.
#[cfg_attr(
not(target_os = "macos"),
allow(dead_code, reason = "only a macOS fd marker names its tree by identity")
)]
HoldersOnly(Skip),
}
impl TerminateOutcome {
/// The outcome of a send that answered as `sent`: the outer `Err` is a send that did not go out
/// ([`NotSent`](Self::NotSent)), the `Ok` what the check after the send found
/// ([`Sent`](Self::Sent)).
pub(crate) fn of(sent: Result<Result<(), Error>, Error>, earlier: Option<&'static str>) -> TerminateOutcome {
match sent {
Ok(result) => TerminateOutcome::Sent { result, earlier },
Err(not_sent) => TerminateOutcome::NotSent(not_sent),
}
}
}
#[cfg(all(test, unix))]
#[path = "skip_tests.rs"]
mod skip_tests;
#[cfg(unix)]
pub(crate) use call::SkipCall;
#[cfg(unix)]
mod call {
use super::{Skip, Why};
use crate::containment::dispatch::{Settled, TreeKillOutcome, TreeSignal, Unkilled};
use crate::containment::root::Refusal;
use crate::containment::Tree;
use crate::error::Error;
use crate::signal::Sent;
use std::sync::Arc;
/// A tree call that skipped what the root's number names: which call (`op`, with `earlier`, what
/// the same operation sent before it, so an error never claims it sent nothing), on which root
/// (`pid`), sending which signal.
pub(crate) struct SkipCall<'a> {
pub(crate) op: &'a str,
pub(crate) pid: u32,
pub(crate) signal: TreeSignal,
pub(crate) earlier: Option<&'a str>,
}
impl SkipCall<'_> {
/// The call's answer to `skip`: its kill, or its terminate that ended there. A `Kill` is
/// settled into the handle's state through [`Tree::settle`], the only writer of it, and a
/// `Term` never is.
///
/// Nothing was sent by the root's number, and what names the tree by identity (a macOS fd
/// marker's holders) was signalled by the mechanism. `root`: the signal to the root through
/// its handle, if the caller sent one ([`Skip::root_alone`]). One `warn` names what was
/// skipped and what was sent. The error is the holders' sweep's, the root signal's, or
/// `Unassessable`, with each other failure of the call, and the error behind the skip's
/// reason, below its variant ([`Error::with_peers`]); a root signal that failed with an
/// [`Error::Io`], a leaf with no slot, goes below the `Unassessable`, after the reason. `Ok`
/// only after this handle's own reap, when it already killed the tree.
pub(crate) fn answer(
&self,
tree: &Tree,
skip: Skip,
root: Option<Result<Sent, Error>>,
) -> Settled<Result<(), Error>> {
let mut skip = match skip {
Skip {
why: Why::Untrusted { why, cause },
action,
holders,
..
} => {
let error = self.untrusted(&action, &why, cause, holders);
return match self.signal {
TreeSignal::Kill => {
tree.settle_for_kill_tree(TreeKillOutcome::Ran(Err(Unkilled::from(error))), false)
}
TreeSignal::Term => Settled::of(Err(error)),
};
}
skip => skip,
};
let Why::MayNameAnother(refusal) = &skip.why else {
unreachable!("an untrusted skip is answered above")
};
if skip.is_moot(tree) {
log::debug!(
"{}({}): {refusal}, and this handle already killed the tree, so it does not {}",
self.op,
self.pid,
skip.action,
refusal = crate::error::Report(refusal)
);
return match self.signal {
TreeSignal::Kill => tree.settle_for_kill_tree(TreeKillOutcome::Skipped(skip), false),
TreeSignal::Term => Settled::of(skip.holders.take().unwrap_or(Ok(()))),
};
}
let (sent_root, failed_root) = match self.signal {
TreeSignal::Kill => (
"killed the root through its pidfd, which names it exactly, and sent nothing else",
"the kill of the root",
),
TreeSignal::Term => (
"sent the SIGTERM to the root through its pidfd, which names it exactly, and sent nothing else",
"the SIGTERM to the root",
),
};
// The warn says each failure in full; the returned `Unassessable` says only that it
// failed, for the failure is a peer or a cause of the answer.
let sent = |with_errors: bool| match (&skip.holders, &root) {
(Some(Ok(())), _) => "signalled only the marker holders".to_owned(),
(Some(Err(e)), _) if with_errors => format!(
"signalled only the marker holders, which failed ({})",
crate::error::Report(e)
),
(Some(Err(_)), _) => "signalled only the marker holders, which failed".to_owned(),
(None, None) => "sent nothing".to_owned(),
(None, Some(Ok(Sent::Delivered))) => sent_root.to_owned(),
(None, Some(Ok(Sent::Gone))) => "sent nothing: the root's pidfd shows it is gone".to_owned(),
(None, Some(Ok(_))) => "sent nothing: the root's handle no longer reaches it".to_owned(),
(None, Some(Err(e))) if with_errors => format!(
"sent nothing: {failed_root} through its pidfd failed ({})",
crate::error::Report(e)
),
(None, Some(Err(_))) => format!("sent nothing: {failed_root} through its pidfd failed"),
};
let detail = self.skipped_detail(&skip, &sent(false));
let cause = skip.why.cause();
skip.why.warn(&self.skipped_detail(&skip, &sent(true)));
let swept = skip.holders.is_some();
let mut killed_again = Vec::new();
let holders = match self.signal {
TreeSignal::Kill => {
let settled = tree.settle_for_kill_tree(TreeKillOutcome::Skipped(skip), false);
killed_again = settled.killed_again;
let settled = settled.answer;
debug_assert!(
swept || settled.is_ok(),
"a skipped kill that swept nothing leaves nothing to report: {settled:?}"
);
swept.then_some(settled)
}
TreeSignal::Term => skip.holders.take(),
};
// The other failures of the call join the one it returns, and the warn above still says
// each in one line.
let answer = match (holders, root.and_then(Result::err)) {
(Some(Err(held)), root) => held.with_peers(root.map(Arc::new).into_iter().chain(cause)),
// An `Io` is a leaf with no slot: it goes below the call's `Unassessable`.
(None, Some(root)) => root.with_peers_or_unassessable(|| detail, cause, []),
(_, root) => Error::Unassessable { detail, source: cause }.with_peers(root.map(Arc::new)),
};
Settled {
answer: Err(answer),
killed_again,
}
}
/// The error of a call that skipped what the root's number names, `skipped`, because the root
/// is `Unpinned` or `Unknown` (macOS), `why`, with the error of an `Unknown` as `cause`: no
/// `warn`, only a debug line when holders were signalled.
fn untrusted(
&self,
skipped: &str,
why: &str,
cause: Option<std::sync::Arc<std::io::Error>>,
holders: Option<Result<(), Error>>,
) -> Error {
let (root_pid, op) = (self.pid, self.op);
let earlier = self
.earlier
.map_or_else(String::new, |sent| format!(", though {sent} was sent before it"));
let said = cause.as_ref().map_or_else(
|| why.to_owned(),
|cause| format!("{why}: {}", crate::error::Report(&**cause)),
);
let unassessable = |sent: &str, because: &str| Error::Unassessable {
detail: format!("pid {root_pid}: {why}; {op} {sent}{earlier}: {because}it did not {skipped}"),
source: cause.as_ref().map(crate::error::shared_io_cause),
};
match holders {
None => unassessable("sent nothing", "its signals are all named by the root's number, so "),
Some(swept) => {
log::debug!("{op}: {said}, so it does not {skipped}; it signals the marker holders");
match swept {
// The holders are not the tree: a kill that skipped the rest is not `Ok`.
Ok(()) => unassessable("signalled only the marker holders", ""),
// The root's state is why nothing else was sent: its error is a peer.
Err(e) => e.with_peers(cause.as_ref().map(crate::error::shared_io_cause)),
}
}
}
}
/// The one line that says the call skipped what names the tree by the root's number. After
/// this handle's own reap, with a pid that names no process, it ends with how to end the
/// descendants the reaped root left.
pub(crate) fn skipped_detail(&self, skip: &Skip, sent: &str) -> String {
let Why::MayNameAnother(refusal) = &skip.why else {
unreachable!("only a skip that may name another process is worded here")
};
let remedy = match refusal {
Refusal::ReapedPidGone(_) => format!(
"; descendants that outlived the reaped root are not ended by it: {}",
crate::child::drop_report::END_THEM_BEFORE_WAIT
),
_ => String::new(),
};
let earlier = self
.earlier
.map_or_else(String::new, |sent| format!(", though {sent} was sent before it"));
format!(
"{}(): {refusal}, so its number may name another process; it did not {}, and {sent}{earlier}{remedy}",
self.op, skip.action
)
}
/// macOS: the end of a call that signalled an fd marker's holders alone ([`Skip::holders`] of
/// `skip` is their `SIGTERM`), after the grace: `kill` is the kill of any holder left, the
/// mechanism's own with the same root. One `warn` names what was skipped. The kill's error, or
/// `Unassessable`, with the `SIGTERM`'s failure and the refusal's reason below its variant.
pub(crate) fn holders_settled(&self, tree: &Tree, skip: Skip, kill: TreeKillOutcome) -> Settled<Error> {
let Settled {
answer: settled,
killed_again,
} = tree.settle_for_kill_tree(kill, false);
// The warn says each failure in full; the returned `Unassessable` says only that it
// failed, for the failure is a peer of the answer.
let sent = |with_errors: bool| {
let said = |failure: Option<&Error>| match failure {
None => String::new(),
Some(e) if with_errors => format!(", which failed ({})", crate::error::Report(e)),
Some(_) => ", which failed".to_owned(),
};
let term = said(skip.holders.as_ref().and_then(|h| h.as_ref().err()));
let kill_failed = said(settled.as_ref().err());
format!("sent the SIGTERM only to the marker holders{term}, then killed any left after the grace{kill_failed}")
};
let detail = self.skipped_detail(&skip, &sent(false));
skip.why.warn(&self.skipped_detail(&skip, &sent(true)));
// The `SIGTERM`'s failure and the refusal's reason are peers of the kill's failure, as they
// are of any answer of a skip.
let cause = skip.why.cause();
let term_failed = skip.holders.and_then(Result::err).map(Arc::new);
let error = match settled {
Err(e) => e.with_peers(term_failed.into_iter().chain(cause)),
Ok(()) => Error::Unassessable { detail, source: cause }.with_peers(term_failed),
};
Settled {
answer: error,
killed_again,
}
}
}
}