cosca 0.7.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! The tree paths' one look at the root, and what each sends by the root's number after it. Shared
//! by the sync and async `kill_tree`, `terminate_tree` and `graceful_shutdown_tree`.

use crate::containment::dispatch::{TreeKillOutcome, TreeSignal, Unkilled};
use crate::containment::{Attached, GroupRoot, TreeKilled};
use crate::error::Error;
use crate::identity::RawPid;
use crate::signal::{RootState, Sent};

/// What a tree path may send by the root's number, from one look at the root's state.
pub(crate) enum RootNumber {
    /// Everything: as far as the look shows, the number names the root.
    Trusted,
    /// macOS: the root is `Unpinned` or `Unknown`, so only what names the tree by identity is sent;
    /// see [`Attached::signal_tree_untrusted`](crate::containment::Attached::signal_tree_untrusted).
    #[cfg(target_os = "macos")]
    Untrusted(RootState),
    /// macOS: this handle reaped the root, its pid names no process, and an fd marker made a group.
    /// The group and the marker holders are signalled, but no walk runs from the pid: the root's
    /// children were reparented at its exit, so a walk would find only a later process's children.
    #[cfg(target_os = "macos")]
    NoWalk,
    /// This handle reaped the root, and its group is empty or it had none: another process holds
    /// the pid, and the kernel does not reissue a pid while it is some group's id, or the pid of a
    /// pid-less fd marker's root names nothing. Only the marker holders, if any, are signalled.
    Empty,
    /// The pid may name another process, for the reason `why`. `exact_root` (Linux): the root's
    /// handle still reaches the root exactly, if it reaches it at all.
    MayNameAnother { why: String, exact_root: bool },
    /// Linux 6.9+: the root is reaped, and its pidfd shows its group has no member left, so there is
    /// no tree to signal and nothing is sent.
    #[cfg(target_os = "linux")]
    GroupGone,
}

/// [`RootNumber`] for `attached`'s tree, from one call of `state`: the root's own handle's answer. On
/// macOS it is always called; elsewhere only for a mechanism that reaches its tree through the root's number
/// ([`Attached::needs_root_check`](crate::containment::Attached::needs_root_check)). After this
/// handle's own reap, a pid that names nothing (`Gone`) is still the id of the root's group while a
/// member survives, so a group is signalled by it, though once none survives a later process may
/// take the number, and lead a group by it, first. No walk runs from it.
pub(crate) fn root_number(attached: &Attached, pid: RawPid, state: impl FnOnce() -> RootState) -> RootNumber {
    use crate::identity::Resolved;
    #[cfg(target_os = "macos")]
    let state = {
        let looked = state();
        if matches!(looked, RootState::Unpinned | RootState::Unknown(_)) {
            return RootNumber::Untrusted(looked);
        }
        move || looked
    };
    if !attached.needs_root_check() {
        return RootNumber::Trusted;
    }
    let mut exact_root = false;
    let why = match state() {
        RootState::Unreaped => return RootNumber::Trusted,
        RootState::Reaped { own: true } => match crate::child::root_identity_now(pid) {
            Resolved::Gone => return own_reaped_free(attached),
            Resolved::Found(_) => return RootNumber::Empty,
            Resolved::Unknown => format!("this handle reaped the root, and its pid ({pid}) could not be read"),
        },
        RootState::Reaped { own: false } => format!("the root (pid {pid}) was reaped elsewhere"),
        RootState::Unknown(e) => {
            exact_root = cfg!(target_os = "linux");
            format!("the root's own handle could not say whether it (pid {pid}) was reaped ({e})")
        }
        RootState::Unpinned => {
            debug_assert!(false, "only macOS reports an unpinned root, and it is answered above");
            format!("the root (pid {pid}) is not pinned by this process")
        }
    };
    RootNumber::MayNameAnother { why, exact_root }
}

/// Whether the root's pid, `pid`, still names its process group, from `state`: the root's own
/// handle's answer, read for a send by that number
/// ([`GroupRoot::check`], as [`Child::kill_tree`](crate::Child::kill_tree) says). `Ok` while the
/// root is unreaped, and after this handle's own reap while the pid names no process, as
/// [`root_number`] decides. `Err` says why not.
pub(crate) fn group_number_holds(pid: RawPid, state: RootState) -> Result<(), String> {
    use crate::identity::Resolved;
    match state {
        RootState::Unreaped => Ok(()),
        RootState::Reaped { own: true } => match crate::child::root_identity_now(pid) {
            Resolved::Gone => Ok(()),
            Resolved::Found(_) => Err(format!(
                "this handle reaped the root, and another process holds its pid ({pid})"
            )),
            Resolved::Unknown => Err(format!(
                "this handle reaped the root, and its pid ({pid}) could not be read"
            )),
        },
        RootState::Reaped { own: false } => Err(format!("the root (pid {pid}) was reaped elsewhere")),
        RootState::Unknown(e) => Err(format!(
            "the root's own handle could not say whether it (pid {pid}) was reaped ({e})"
        )),
        RootState::Unpinned => Err(format!("the root (pid {pid}) is not pinned by this process")),
    }
}

/// Linux 6.9+: `number` for a process group whose root's pid may name another process, decided again
/// by the root's pidfd (`group`), which names the group exactly whoever reaped the root. While the
/// group has a member it is signalled as usual: the group's own signal goes through the pidfd, and
/// each send by its number is checked the same way ([`GroupRoot::check`]). A reaped root's group with
/// no member left is [`RootNumber::GroupGone`]. A root its handle cannot answer for, whose group has
/// no member, may have left the group alive, so it keeps `number`; so does a kernel that cannot say.
/// A drop and a failed elevated spawn's cleanup take the same decision
/// ([`Attached::hard_kill_for_drop`](crate::containment::Attached::hard_kill_for_drop)).
#[cfg(target_os = "linux")]
pub(crate) fn through_the_pidfd(attached: &Attached, number: RootNumber, group: &GroupRoot<'_>) -> RootNumber {
    let RootNumber::MayNameAnother { why, exact_root } = &number else {
        return number;
    };
    if !matches!(attached, Attached::ProcessGroup(_)) {
        return number;
    }
    match group.has_member() {
        Some(true) => {
            log::debug!(
                "{why}, but its pidfd names its process group exactly, which still has a member: it is signalled"
            );
            RootNumber::Trusted
        }
        Some(false) if !*exact_root => RootNumber::GroupGone,
        _ => number,
    }
}

/// [`RootNumber`] after this handle's own reap, with the root's pid naming no process.
fn own_reaped_free(attached: &Attached) -> RootNumber {
    #[cfg(target_os = "macos")]
    match attached.fd_marker_has_group() {
        Some(true) => return RootNumber::NoWalk,
        Some(false) => return RootNumber::Empty,
        None => {}
    }
    #[cfg(not(target_os = "macos"))]
    let _ = attached;
    RootNumber::Trusted
}

/// One tree call: the tree of `attached`, whose root is `pid`, sent `signal` by the operation
/// `op`, which already sent `earlier` (so an error never claims the operation sent nothing).
/// A `Kill` that reached the whole tree is settled into `killed` ([`TreeCall::answered`]).
pub(crate) struct TreeCall<'a> {
    pub(crate) attached: &'a Attached,
    pub(crate) pid: RawPid,
    pub(crate) signal: TreeSignal,
    pub(crate) op: &'static str,
    pub(crate) earlier: Option<&'a str>,
    pub(crate) killed: &'a TreeKilled,
}

/// What `graceful_shutdown_tree` (sync and async) does after its one look at the root
/// ([`TreeCall::graceful`]).
pub(crate) enum Graceful {
    /// Signal the tree as usual, then the grace and the sweep.
    Signal,
    /// The cooperative signal went out already, with `result`; then the grace and the sweep, which
    /// says `earlier` was sent before it.
    Sent {
        result: Result<(), Error>,
        earlier: Option<&'static str>,
    },
    /// The call ends now, with this error.
    End(Error),
    /// Linux: the root's number may name another process (`why`), but its pidfd names it exactly:
    /// the root alone is shut down through it ([`PidfdShutdown`]), and the call ends.
    #[cfg(target_os = "linux")]
    RootThroughPidfd(String),
    /// macOS: the root's number may name another process (`why`): an fd marker's holders alone are
    /// shut down, by the pipe they hold ([`TreeCall::holders_term`]), and the call ends.
    #[cfg(target_os = "macos")]
    HoldersOnly(String),
}

impl TreeCall<'_> {
    /// The call's one look at the root ([`root_number`]), and on Linux 6.9+ at its group through
    /// `group`'s pidfd (`through_the_pidfd`).
    #[cfg_attr(not(target_os = "linux"), allow(unused_variables, reason = "only Linux has a pidfd"))]
    pub(crate) fn root_number(&self, state: impl FnOnce() -> RootState, group: &GroupRoot<'_>) -> RootNumber {
        let number = root_number(self.attached, self.pid, state);
        #[cfg(target_os = "linux")]
        let number = through_the_pidfd(self.attached, number, group);
        #[cfg(test)]
        crate::graceful_hooks::run_hook(crate::graceful_hooks::HookPoint::AfterRootLook);
        number
    }

    /// What `kill_tree` and `terminate_tree` (sync and async) do instead of signalling a tree
    /// reached through the root's pid (a process group, or a macOS fd marker's group and walk) when
    /// that pid may name another process, decided on one look ([`root_number`]). `None`: signal it
    /// as usual.
    ///
    /// No walk from the root's pid runs here, and no `killpg` except a marker's group's after this
    /// handle's own reap ([`RootNumber::NoWalk`]). What names the tree by identity still runs: a
    /// macOS fd marker's holders, named by the pipe they hold.
    ///
    /// Each answer a `Kill` gives of a tree that was reached is [`answered`](Self::answered).
    ///
    /// - `Untrusted` (macOS): `Attached::signal_tree_untrusted`.
    /// - `NoWalk` (macOS): the group and the holders.
    /// - `Empty`: the holders are signalled, and the call is their sweep's answer (`Ok` with no
    ///   holders channel).
    /// - `GroupGone` (Linux): nothing is sent, and the call is `Ok`.
    /// - `MayNameAnother`: [`number_skipped`](Self::number_skipped), after `signal_root` when the
    ///   handle still names the root exactly.
    ///
    /// `signal_root` sends the call's own signal to the root through its handle. `group` is the root
    /// a send to its group needs.
    #[cfg_attr(
        not(target_os = "macos"),
        allow(unused_variables, reason = "only a macOS fd marker signals here")
    )]
    pub(crate) fn root_checked(
        &self,
        state: impl FnOnce() -> RootState,
        signal_root: impl FnOnce() -> Result<Sent, Error>,
        group: &GroupRoot<'_>,
    ) -> Option<Result<(), Error>> {
        match self.root_number(state, group) {
            RootNumber::Trusted => None,
            #[cfg(target_os = "macos")]
            RootNumber::Untrusted(root) => self.untrusted(&root),
            #[cfg(target_os = "macos")]
            RootNumber::NoWalk => Some(self.no_walk(group)),
            RootNumber::Empty => Some(self.answered(self.empty_group().unwrap_or(Ok(())))),
            #[cfg(target_os = "linux")]
            RootNumber::GroupGone => Some(self.group_gone()),
            RootNumber::MayNameAnother { why, exact_root } => {
                let root = exact_root.then(signal_root);
                Some(Err(self.number_skipped(&why, root)))
            }
        }
    }

    /// `graceful_shutdown_tree`'s decision on its one look at the root, for its `SIGTERM`: what
    /// [`root_checked`](Self::root_checked) decides, except that a root whose number may name
    /// another process is shut down through what still names it exactly, if anything does.
    #[cfg_attr(
        not(target_os = "macos"),
        allow(unused_variables, reason = "only a macOS fd marker signals here")
    )]
    pub(crate) fn graceful(&self, state: impl FnOnce() -> RootState, group: &GroupRoot<'_>) -> Graceful {
        match self.root_number(state, group) {
            RootNumber::Trusted => Graceful::Signal,
            // By identity alone. With no channel by identity there is nothing to signal, wait for or
            // sweep, so the call ends here with nothing sent.
            #[cfg(target_os = "macos")]
            RootNumber::Untrusted(root) => match self.untrusted(&root) {
                Some(Err(e)) if !self.attached.names_tree_by_identity() => Graceful::End(e),
                None => Graceful::Signal,
                Some(result) => Graceful::Sent {
                    result,
                    earlier: Some("the SIGTERM"),
                },
            },
            // This handle reaped the root: its marker's group and holders, and no walk from its pid.
            #[cfg(target_os = "macos")]
            RootNumber::NoWalk => Graceful::Sent {
                result: self.no_walk(group),
                earlier: Some("the SIGTERM"),
            },
            // The group has no member left: nothing to signal, wait for or sweep but the root.
            #[cfg(target_os = "linux")]
            RootNumber::GroupGone => Graceful::Sent {
                result: self.group_gone(),
                earlier: None,
            },
            // The group is empty: nothing goes out by its number, only to the marker holders, if any.
            RootNumber::Empty => {
                let holders = self.empty_group();
                Graceful::Sent {
                    earlier: holders.is_some().then_some("the SIGTERM to any marker holders"),
                    result: self.answered(holders.unwrap_or(Ok(()))),
                }
            }
            // The pid may name another process. A macOS fd marker shuts its holders down by the pipe
            // they hold, and a Linux root its handle cannot answer for is shut down through its
            // pidfd; any other mechanism has nothing to send. Either way the call ends with one
            // warn: a sweep would warn again.
            RootNumber::MayNameAnother { why, exact_root } => {
                #[cfg(target_os = "linux")]
                if exact_root {
                    return Graceful::RootThroughPidfd(why);
                }
                #[cfg(not(target_os = "linux"))]
                let _ = exact_root;
                #[cfg(target_os = "macos")]
                if self.attached.names_tree_by_identity() {
                    return Graceful::HoldersOnly(why);
                }
                Graceful::End(self.number_skipped(&why, None))
            }
        }
    }

    /// This call's answer: a `Kill`'s is settled into the handle's state (it reaches no root of its
    /// own to end, so `root_ended` is false), through [`Attached::settle`](crate::containment::Attached::settle),
    /// the only writer of it. A `SIGTERM`'s is the answer as it is.
    fn answered(&self, result: Result<(), Error>) -> Result<(), Error> {
        match self.signal {
            TreeSignal::Kill => self.attached.settle_for_kill_tree(
                self.op,
                self.killed,
                TreeKillOutcome::Ran(result.map_err(Unkilled::from)),
                false,
            ),
            TreeSignal::Term => result,
        }
    }

    /// The signal for [`RootNumber::Untrusted`] (macOS).
    #[cfg(target_os = "macos")]
    fn untrusted(&self, root: &RootState) -> Option<Result<(), Error>> {
        self.attached
            .signal_tree_untrusted(self.signal, root, self.pid, self.op, self.earlier)
            .map(|result| self.answered(result))
    }

    /// The signal for [`RootNumber::NoWalk`]: the group and a macOS fd marker's holders, and no
    /// walk from the root's pid.
    #[cfg(target_os = "macos")]
    fn no_walk(&self, group: &GroupRoot<'_>) -> Result<(), Error> {
        log::debug!(
            "{}({}): this handle reaped the root, and its pid names no process: its group and the marker holders \
             are signalled, and no walk runs from the pid, which would find none of the tree",
            self.op,
            self.pid
        );
        self.answered(
            self.attached
                .signal_group_and_holders(self.signal, group)
                .unwrap_or_else(|| {
                    debug_assert!(false, "only an fd marker with a group gets here");
                    Ok(())
                }),
        )
    }

    /// [`RootNumber::GroupGone`]: nothing is sent, and the answer is `Ok`.
    #[cfg(target_os = "linux")]
    fn group_gone(&self) -> Result<(), Error> {
        log::debug!(
            "{}({}): the root is reaped, and its pidfd shows its process group has no member left, so nothing is sent",
            self.op,
            self.pid
        );
        self.answered(Ok(()))
    }

    /// The signal for [`RootNumber::Empty`]: nothing by the root's number, and a macOS fd marker's
    /// holders by the pipe they hold. `None` for a mechanism with no such channel. The caller
    /// [`answered`](Self::answered)s it.
    fn empty_group(&self) -> Option<Result<(), Error>> {
        log::debug!(
            "{}({}): this handle reaped the root, so its group is empty or it had none, a walk from its pid \
             would find none of the tree, and nothing is sent by it",
            self.op,
            self.pid
        );
        self.attached.signal_holders_only(self.signal)
    }

    /// The answer for [`RootNumber::MayNameAnother`], `why` its reason: nothing is sent by the
    /// root's number, a macOS fd marker's holders are signalled, and one `warn` names what was
    /// skipped and what was sent, `root` included: the signal to the root through its handle, if
    /// one was sent. The holders' sweep's error, the root signal's error, or `Unassessable`.
    pub(crate) fn number_skipped(&self, why: &str, root: Option<Result<Sent, Error>>) -> Error {
        let holders = self.attached.signal_holders_only(self.signal);
        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",
            ),
        };
        let sent = match (&holders, &root) {
            (Some(Ok(())), _) => "signalled only the marker holders".to_owned(),
            (Some(Err(e)), _) => format!("signalled only the marker holders, which failed ({e})"),
            (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))) => format!("sent nothing: {failed_root} through its pidfd failed ({e})"),
        };
        let detail = self.skipped_detail(why, &sent);
        log::warn!("{detail}");
        match (holders, root) {
            (Some(Err(e)), _) | (None, Some(Err(e))) => e,
            _ => Error::Unassessable { detail, source: None },
        }
    }

    /// The one line that says the call skipped what names the tree by the root's number.
    fn skipped_detail(&self, why: &str, sent: &str) -> String {
        let verb = self.signal.verb();
        let skipped = self
            .attached
            .named_by_root_number(self.pid, verb)
            .unwrap_or_else(|| format!("{verb} its tree"));
        let earlier = self
            .earlier
            .map_or_else(String::new, |sent| format!(", though {sent} was sent before it"));
        format!(
            "{}(): {why}, so its number may name another process; it did not {skipped}, and {sent}{earlier}",
            self.op
        )
    }

    /// `signal` to the marker holders of a macOS fd marker, which names its tree by identity.
    #[cfg(target_os = "macos")]
    pub(crate) fn holders_only(&self, signal: TreeSignal) -> Result<(), Error> {
        self.attached.signal_holders_only(signal).unwrap_or_else(|| {
            debug_assert!(false, "only a mechanism that names its tree by identity gets here");
            Ok(())
        })
    }

    /// [`Graceful::HoldersOnly`]'s first step: the `SIGTERM` to the marker holders. The caller
    /// waits the grace, logs a failed wait with [`holders_grace_failed`](Self::holders_grace_failed),
    /// and ends with [`holders_shutdown_error`](Self::holders_shutdown_error).
    #[cfg(target_os = "macos")]
    pub(crate) fn holders_term(&self) -> Result<(), Error> {
        self.holders_only(TreeSignal::Term)
    }

    /// A failed grace wait of [`Graceful::HoldersOnly`]: the holders are killed now.
    #[cfg(target_os = "macos")]
    pub(crate) fn holders_grace_failed(&self, e: &Error) {
        log::debug!(
            "{}({}): the marker holders' grace wait failed, so they are killed now: {e}",
            self.op,
            self.pid
        );
    }

    /// [`Graceful::HoldersOnly`]'s end, `why` its reason: a kill of any holders left, by the pipe
    /// they hold (a marker's drain wait never shows every member exited), after `term`. One `warn`
    /// names what was skipped. The kill's error, or `Unassessable`.
    #[cfg(target_os = "macos")]
    pub(crate) fn holders_shutdown_error(&self, why: &str, term: Result<(), Error>) -> Error {
        let kill = self.holders_only(TreeSignal::Kill);
        let term = match &term {
            Ok(()) => String::new(),
            Err(e) => format!(", which failed ({e})"),
        };
        let kill_failed = match &kill {
            Ok(()) => String::new(),
            Err(e) => format!(", which failed ({e})"),
        };
        let sent = format!(
            "sent the SIGTERM only to the marker holders{term}, then killed any left after the grace{kill_failed}"
        );
        let detail = self.skipped_detail(why, &sent);
        log::warn!("{detail}");
        match kill {
            Err(e) => e,
            Ok(()) => Error::Unassessable { detail, source: None },
        }
    }
}

/// [`Graceful::RootThroughPidfd`] (Linux), sync and async: the `SIGTERM` through the root's pidfd,
/// the grace, then a kill through the pidfd if the root outlived it. Nothing goes out by the
/// number. A front that may not be signalled is refused, as for the tree paths.
///
/// The caller [`start`](Self::start)s it, waits the grace when [`needs_grace`](Self::needs_grace),
/// and [`finish`](Self::finish)es it with the wait's outcome.
#[cfg(target_os = "linux")]
pub(crate) struct PidfdShutdown {
    term: Result<Sent, Error>,
}

#[cfg(target_os = "linux")]
impl PidfdShutdown {
    /// The `SIGTERM`, sent by `term` unless `gate` refuses it; a refusal is the call's error.
    pub(crate) fn start(
        gate: crate::elevation::front::Gate,
        term: impl FnOnce() -> Result<Sent, Error>,
    ) -> Result<PidfdShutdown, Error> {
        if let crate::elevation::front::Gate::Closed(unkillable) = gate {
            return Err(unkillable);
        }
        Ok(PidfdShutdown { term: term() })
    }

    /// Whether the `SIGTERM` reached the root, so the grace is waited.
    pub(crate) fn needs_grace(&self) -> bool {
        matches!(self.term, Ok(Sent::Delivered))
    }

    /// The call's error, after `exited`, the grace wait's outcome (`None`: not waited): a root
    /// that did not exit within it, or whose wait failed, is killed by `kill` unless `kill_gate`
    /// refuses it. One `warn` names what was skipped and what was sent. The failed signal's error,
    /// or `Unassessable`.
    pub(crate) fn finish(
        self,
        call: &TreeCall<'_>,
        why: &str,
        exited: Option<Result<bool, impl std::fmt::Display>>,
        kill_gate: impl FnOnce() -> crate::elevation::front::Gate,
        kill: impl FnOnce() -> Result<Sent, Error>,
    ) -> Error {
        use crate::elevation::front::Gate;
        let kill = match exited {
            None | Some(Ok(true)) => None,
            Some(outcome) => {
                if let Err(e) = outcome {
                    log::debug!(
                        "{}({}): the root's grace wait failed, so it is killed now: {e}",
                        call.op,
                        call.pid
                    );
                }
                Some(match kill_gate() {
                    Gate::Closed(unkillable) => Err(unkillable),
                    _ => kill(),
                })
            }
        };
        let termed = "sent the SIGTERM to the root through its pidfd, which names it exactly";
        let sent = match (&self.term, &kill) {
            (Err(e), _) => format!("sent nothing: the SIGTERM to the root through its pidfd failed ({e})"),
            (Ok(Sent::Delivered), None) => format!("{termed}, and it exited within the grace; sent nothing else"),
            (Ok(Sent::Delivered), Some(Ok(Sent::Delivered))) => {
                format!("{termed}, then killed the root through its pidfd after the grace, and sent nothing else")
            }
            (Ok(Sent::Delivered), Some(Err(e))) => {
                format!("{termed}, then the kill of the root through its pidfd failed ({e})")
            }
            (Ok(Sent::Delivered), Some(Ok(_))) => {
                format!("{termed}, and its pidfd showed it gone after the grace; sent nothing else")
            }
            (Ok(Sent::Gone), _) => "sent nothing: the root's pidfd shows it is gone".to_owned(),
            (Ok(_), _) => "sent nothing: the root's handle no longer reaches it".to_owned(),
        };
        let detail = call.skipped_detail(why, &sent);
        log::warn!("{detail}");
        match (self.term, kill) {
            (Err(e), _) | (_, Some(Err(e))) => e,
            _ => Error::Unassessable { detail, source: None },
        }
    }
}

/// `graceful_shutdown_tree`'s answer (sync and async) from the root's reap, `waited`, once the tree is
/// down. A root reaped elsewhere (`ECHILD`) leaves no exit status for this handle: that is
/// `Unassessable`, saying the tree is down, as for a root launchd holds. Any other answer stands.
pub(crate) fn status_unless_reaped_elsewhere(
    waited: Result<std::process::ExitStatus, Error>,
    pid: RawPid,
) -> Result<std::process::ExitStatus, Error> {
    match waited {
        Err(Error::Io(e)) if e.raw_os_error() == Some(libc::ECHILD) => {
            log::debug!("graceful_shutdown_tree({pid}): the tree is down, and the root was reaped elsewhere");
            Err(Error::Unassessable {
                detail: format!(
                    "pid {pid}: graceful_shutdown_tree brought the tree down, but its exit status cannot be \
                     collected: the root was reaped elsewhere"
                ),
                source: None,
            })
        }
        other => other,
    }
}

/// The async `graceful_shutdown_tree`'s answer once the tree is down, for a root whose handle was
/// forgotten because it could not be shown to be ours: no exit status is collected from it.
#[cfg(feature = "tokio")]
pub(crate) fn status_of_an_unproven_root(pid: RawPid) -> Error {
    Error::Unassessable {
        detail: format!(
            "pid {pid}: graceful_shutdown_tree brought the tree down, but its exit status cannot be \
             collected: the root's handle could not say whether it was reaped"
        ),
        source: None,
    }
}

#[cfg(test)]
#[path = "tree_call_tests.rs"]
mod tree_call_tests;