cosca 0.8.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! `kill_tree` and `terminate_tree` of a group whose root was reaped behind its handle's back. Only
//! the handle can tell: the root's number names nothing, or (macOS) nothing the number shows to be
//! another process. The group recorders stand in for `killpg`.

use crate::containment::unix::fault::{record_kill_group, record_term_group};
use crate::log_capture::warns_since;
#[cfg(target_os = "linux")]
use crate::test_child::reaped_elsewhere::ROOT_KILLED_BY_PIDFD;
use crate::test_child::reaped_elsewhere::{
    assert_one_reaped_elsewhere_warn, assert_one_untrusted_number_warn, foreign_reaped_group_root, kill_own_child,
    session_cat,
};
use crate::test_groups::{stray_signal, Group};

/// Mutants: `killpg` sent; the decision taken on the number alone (it shows no recycle).
#[skuld::test]
fn kill_tree_after_a_foreign_reap_sends_no_killpg(#[fixture(stray_signal)] _stray: &Group) {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::log_capture::install();
    let recorder = record_kill_group();
    let (child, pgid) = foreign_reaped_group_root();

    let mark = crate::log_capture::mark();
    let err = child
        .kill_tree()
        .expect_err("a group whose root was reaped elsewhere is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        recorder.killed(),
        Vec::<i32>::new(),
        "no killpg by a reaped root's number"
    );
    assert_one_reaped_elsewhere_warn(mark, pgid);
}

/// The twin of [`kill_tree_after_a_foreign_reap_sends_no_killpg`]. Same mutants.
#[skuld::test]
fn terminate_tree_after_a_foreign_reap_sends_no_signal(#[fixture(stray_signal)] _stray: &Group) {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::log_capture::install();
    let recorder = record_term_group();
    let (child, pgid) = foreign_reaped_group_root();

    let mark = crate::log_capture::mark();
    let err = child
        .terminate_tree()
        .expect_err("a group whose root was reaped elsewhere is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        recorder.termed(),
        Vec::<i32>::new(),
        "no SIGTERM by a reaped root's number"
    );
    assert_one_reaped_elsewhere_warn(mark, pgid);
}

/// After this handle's own reap, a pid that cannot be read may name another process (a pid that
/// names none reads `Gone`): `kill_tree` sends no `killpg` and answers `Unassessable`.
///
/// Mutant: an unreadable pid after our own reap is taken as free, and `killpg` is sent.
#[skuld::test]
fn kill_tree_after_own_wait_whose_pid_reads_unknown_sends_no_killpg(#[fixture(stray_signal)] _stray: &Group) {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::log_capture::install();
    let recorder = record_kill_group();
    let child = session_cat().spawn().expect("spawn");
    let pid = child.pid();
    kill_own_child(pid);
    child.wait().expect("this handle reaps the root");
    let _unknown = crate::child::fault::force_next_root_read(crate::identity::Resolved::Unknown);

    let mark = crate::log_capture::mark();
    let err = child
        .kill_tree()
        .expect_err("a group whose root's pid cannot be read is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        recorder.killed(),
        Vec::<i32>::new(),
        "no killpg by a number that may name another process"
    );
    assert_one_untrusted_number_warn(mark, pid);
}

/// After this handle's own reap, a pid that names no process may be taken by another process once
/// its group has no member: `kill_tree` sends no `killpg`, and answers `Unassessable` with how to end
/// the descendants.
#[skuld::test]
fn kill_tree_after_own_wait_whose_pid_names_no_process_sends_no_killpg(#[fixture(stray_signal)] _stray: &Group) {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::log_capture::install();
    let recorder = record_kill_group();
    let child = session_cat().spawn().expect("spawn");
    let pid = child.pid();
    kill_own_child(pid);
    child.wait().expect("this handle reaps the root");
    let _gone = crate::child::fault::force_next_root_read(crate::identity::Resolved::Gone);

    let mark = crate::log_capture::mark();
    let err = child
        .kill_tree()
        .expect_err("a group the pid no longer names is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert!(
        err.to_string()
            .contains(crate::child::drop_report::END_THEM_BEFORE_WAIT),
        "{err}"
    );
    assert_eq!(
        recorder.killed(),
        Vec::<i32>::new(),
        "no killpg by a number that names no process"
    );
    assert_one_untrusted_number_warn(mark, pid);
}

/// A root whose own handle cannot say whether it was reaped: its pid may name another process, so
/// `kill_tree` sends no `killpg`, kills the root through its pidfd, which names it exactly, and
/// answers `Unassessable`. (macOS answers this earlier, through its untrusted-root path.)
///
/// Mutants: a `RootState::Unknown` root's group is signalled; the root is not killed through its
/// pidfd; the warn does not say it was.
#[cfg(target_os = "linux")]
#[skuld::test]
fn kill_tree_of_a_root_its_handle_cannot_read_kills_it_through_its_pidfd_alone() {
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    use std::os::unix::process::ExitStatusExt as _;
    crate::log_capture::install();
    let recorder = record_kill_group();
    let mut child = session_cat().spawn().expect("spawn");
    let stdin = child.stdin().expect("stdin pipe");
    let pid = child.pid();

    let mark = crate::log_capture::mark();
    let sends = crate::send_log::Capture::start();
    let fake = child
        .proc
        .script_over(|f| f.peeks([Err(std::io::Error::other("forced peek failure"))]));
    let err = child
        .kill_tree()
        .expect_err("a group whose root nothing can answer for is not assessable");
    fake.assert_peeks_exhausted();

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        recorder.killed(),
        Vec::<i32>::new(),
        "no killpg by a number that may name another process"
    );
    assert_eq!(
        sends.entries(),
        [(pid, crate::signal::Sig::Kill, crate::send_log::Via::Pidfd)],
        "the root is killed through its pidfd, and nothing else is sent"
    );
    assert_one_untrusted_number_warn(mark, pid);
    let warns = warns_since(mark, "");
    assert!(warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
    drop(stdin);
    let status = child.wait().expect("reap the root");
    assert_eq!(status.signal(), Some(libc::SIGKILL), "the pidfd kill ended the root");
}

/// A root whose own handle cannot say whether it was reaped: `terminate_tree` sends no `SIGTERM` by
/// its number, sends it one through its pidfd, which names it exactly, and answers `Unassessable`.
///
/// Mutants: nothing is sent to the root; a `SIGTERM` goes to its group; the warn does not say it was
/// sent.
#[cfg(target_os = "linux")]
#[skuld::test]
fn terminate_tree_of_a_root_its_handle_cannot_read_terms_it_through_its_pidfd_alone() {
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    use crate::test_child::reaped_elsewhere::ROOT_TERMED_BY_PIDFD;
    use std::os::unix::process::ExitStatusExt as _;
    crate::log_capture::install();
    let termed = record_term_group();
    let mut child = session_cat().spawn().expect("spawn");
    let stdin = child.stdin().expect("stdin pipe");
    let pid = child.pid();

    let mark = crate::log_capture::mark();
    let sends = crate::send_log::Capture::start();
    let fake = child
        .proc
        .script_over(|f| f.peeks([Err(std::io::Error::other("forced peek failure"))]));
    let err = child
        .terminate_tree()
        .expect_err("a group whose root nothing can answer for is not assessable");
    fake.assert_peeks_exhausted();

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        termed.termed(),
        Vec::<i32>::new(),
        "no SIGTERM by a number that may name another process"
    );
    assert_eq!(
        sends.entries(),
        [(pid, crate::signal::Sig::Term, crate::send_log::Via::Pidfd)],
        "the root is signalled through its pidfd, and nothing else is sent"
    );
    assert_one_untrusted_number_warn(mark, pid);
    let warns = warns_since(mark, "");
    assert!(warns[0].contains(ROOT_TERMED_BY_PIDFD), "{warns:?}");
    drop(stdin);
    let status = child.wait().expect("reap the root");
    assert_eq!(status.signal(), Some(libc::SIGTERM), "the pidfd SIGTERM ended the root");
}

/// A root whose own handle cannot say whether it was reaped and ignores `SIGTERM`:
/// `graceful_shutdown_tree` sends it the `SIGTERM` through its pidfd, and after the grace the kill
/// through its pidfd, and nothing by its number. It ends `Unassessable` with one warn that says so.
///
/// Mutants: nothing is sent to the root; the kill after the grace is not sent; a second `SIGTERM`
/// in its place; a `killpg` or a `SIGTERM` to the group.
#[cfg(target_os = "linux")]
#[skuld::test]
fn graceful_shutdown_tree_of_a_root_its_handle_cannot_read_escalates_through_its_pidfd_alone() {
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    use crate::signal::Sig::{Kill, Term};
    use crate::test_child::reaped_elsewhere::{term_ignoring_session_cat, ROOT_TERMED_BY_PIDFD};
    use std::os::unix::process::ExitStatusExt as _;
    crate::log_capture::install();
    let killed = record_kill_group();
    let termed = record_term_group();
    let mut child = term_ignoring_session_cat().spawn().expect("spawn");
    let stdin = child.stdin().expect("stdin pipe");
    let pid = child.pid();
    let mut ready = String::new();
    std::io::BufRead::read_line(
        &mut std::io::BufReader::new(child.stdout().expect("stdout pipe")),
        &mut ready,
    )
    .expect("the root ignores SIGTERM");

    let mark = crate::log_capture::mark();
    let sends = crate::send_log::Capture::start();
    let fake = child
        .proc
        .script_over(|f| f.peeks([Err(std::io::Error::other("forced peek failure"))]));
    // A root that ignores the SIGTERM is alive after any grace, so the kill follows.
    let err = child
        .graceful_shutdown_tree(std::time::Duration::ZERO)
        .expect_err("a group whose root nothing can answer for is not assessable");
    fake.assert_peeks_exhausted();

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(killed.killed(), Vec::<i32>::new(), "no killpg by the root's number");
    assert_eq!(termed.termed(), Vec::<i32>::new(), "no SIGTERM by the root's number");
    let via_pidfd = crate::send_log::Via::Pidfd;
    assert_eq!(
        sends.entries(),
        [(pid, Term, via_pidfd), (pid, Kill, via_pidfd)],
        "the SIGTERM, then the kill, both through the pidfd"
    );
    assert_one_untrusted_number_warn(mark, pid);
    let warns = warns_since(mark, "");
    assert!(warns[0].contains(ROOT_TERMED_BY_PIDFD), "{warns:?}");
    assert!(warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
    drop(stdin);
    let status = child.wait().expect("reap the root");
    assert_eq!(status.signal(), Some(libc::SIGKILL), "the kill ended the root");
}

/// The same root, but it exits on the `SIGTERM`: `graceful_shutdown_tree` sends no kill, and the one
/// warn does not say it did. A hook after the `SIGTERM` checks it went out, releases the root's
/// stdin, then waits for the root to exit, so the grace's one look at the pidfd at expiry finds it
/// readable. A `SIGTERM` that was not delivered lets the release end the root by itself, which shows
/// in its status.
///
/// Mutants: the kill is sent whatever the root did; the warn says it was; the grace wait misses the
/// pidfd's exit.
#[cfg(target_os = "linux")]
#[skuld::test]
fn graceful_shutdown_tree_of_a_root_its_handle_cannot_read_sends_no_kill_to_one_that_exited() {
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    use crate::graceful_hooks::{at, HookPoint};
    use crate::signal::Sig::Term;
    use crate::test_child::reaped_elsewhere::ROOT_TERMED_BY_PIDFD;
    use std::os::unix::process::ExitStatusExt as _;
    crate::log_capture::install();
    let mut child = session_cat().spawn().expect("spawn");
    let stdin = child.stdin().expect("stdin pipe");
    let pid = child.pid();

    let mark = crate::log_capture::mark();
    let sends = std::rc::Rc::new(crate::send_log::Capture::start());
    let fake = child
        .proc
        .script_over(|f| f.peeks([Err(std::io::Error::other("forced peek failure"))]));
    let exited_first = at(HookPoint::AfterTerminate, {
        let sends = std::rc::Rc::clone(&sends);
        move || {
            assert_eq!(
                sends.entries(),
                [(pid, Term, crate::send_log::Via::Pidfd)],
                "the SIGTERM went out before the grace"
            );
            drop(stdin);
            crate::test_child::wait_until_zombie(pid);
        }
    });
    let err = child
        .graceful_shutdown_tree(std::time::Duration::ZERO)
        .expect_err("a group whose root nothing can answer for is not assessable");
    drop(exited_first);
    fake.assert_peeks_exhausted();

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        sends.entries(),
        [(pid, Term, crate::send_log::Via::Pidfd)],
        "only the SIGTERM, through the pidfd"
    );
    assert_one_untrusted_number_warn(mark, pid);
    let warns = warns_since(mark, "");
    assert!(warns[0].contains(ROOT_TERMED_BY_PIDFD), "{warns:?}");
    assert!(!warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
    let status = child.wait().expect("reap the root");
    assert_eq!(status.signal(), Some(libc::SIGTERM), "the SIGTERM ended the root");
}

/// After a foreign reap, `graceful_shutdown_tree` ends `Unassessable` with one warn and no sweep by
/// the root's number, which would warn again and claim a `SIGTERM` was sent. (A macOS fd marker's
/// holders still get their shutdown; here there are none left.)
///
/// Mutant: the `Unassessable` falls through to the grace and the sweep.
#[skuld::test]
fn graceful_shutdown_tree_after_a_foreign_reap_warns_once_and_ends(#[fixture(stray_signal)] _stray: &Group) {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::log_capture::install();
    let killed = record_kill_group();
    let termed = record_term_group();
    let (child, pgid) = foreign_reaped_group_root();

    let mark = crate::log_capture::mark();
    let err = child
        .graceful_shutdown_tree(std::time::Duration::from_secs(1))
        .expect_err("a group whose root was reaped elsewhere is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        killed.killed(),
        Vec::<i32>::new(),
        "no killpg by a reaped root's number"
    );
    assert_eq!(
        termed.termed(),
        Vec::<i32>::new(),
        "no SIGTERM by a reaped root's number"
    );
    assert_one_reaped_elsewhere_warn(mark, pgid);
    let warns = warns_since(mark, "");
    assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
    assert!(
        !warns[0].contains("was sent before it"),
        "no SIGTERM was sent: {warns:?}"
    );
}

/// After this handle's own reap, with the pid held by another process, `graceful_shutdown_tree`
/// sends no `SIGTERM` by the number: the group is empty. When its sweep then finds the pid
/// unreadable, the warn says what the shutdown really sent before it: nothing on Linux, the
/// `SIGTERM` to any marker holders on macOS.
///
/// Mutant: the sweep is told that "the SIGTERM" went out whatever the shutdown sent.
#[skuld::test]
fn graceful_shutdown_tree_after_own_wait_claims_no_sigterm_it_did_not_send(#[fixture(stray_signal)] _stray: &Group) {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    use crate::child::fault::force_next_root_read;
    use crate::identity::Resolved;
    use crate::test_child::reaped_elsewhere::UNTRUSTED_NUMBER;
    crate::log_capture::install();
    let killed = record_kill_group();
    let termed = record_term_group();
    let child = session_cat().spawn().expect("spawn");
    let pid = child.pid();
    kill_own_child(pid);
    child.wait().expect("this handle reaps the root");
    let other = child.id().expect(crate::test_child::IDENTITY_READ).with_other_value();
    // The shutdown's look finds another process; the sweep's, after the terminate, cannot read the
    // pid. This guard clears the second force too, should it go unconsumed.
    let _reads = force_next_root_read(Resolved::Found(other));
    let _unreadable = crate::graceful_hooks::at(crate::graceful_hooks::HookPoint::AfterTerminate, || {
        std::mem::forget(force_next_root_read(Resolved::Unknown))
    });

    let mark = crate::log_capture::mark();
    let err = child
        .graceful_shutdown_tree(std::time::Duration::ZERO)
        .expect_err("a sweep whose root's pid cannot be read is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        termed.termed(),
        Vec::<i32>::new(),
        "no SIGTERM by a reaped root's number"
    );
    assert_eq!(
        killed.killed(),
        Vec::<i32>::new(),
        "no killpg by a reaped root's number"
    );
    let warns = warns_since(mark, "");
    assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
    assert!(warns[0].contains(UNTRUSTED_NUMBER), "{warns:?}");
    #[cfg(target_os = "linux")]
    assert!(
        !warns[0].contains("was sent before it"),
        "no SIGTERM was sent: {warns:?}"
    );
    #[cfg(target_os = "macos")]
    assert!(
        warns[0].contains("though the SIGTERM to any marker holders was sent before it"),
        "the SIGTERM went to the marker holders alone: {warns:?}"
    );
}

/// After this handle's own reap, with the pid reused, an fd marker still kills its holders by the
/// pipe they hold, which no pid reuse redirects, including one that left the group. `Ok` follows
/// the sweep, and the tree counts as killed, so the drop has nothing to warn about.
///
/// Mutants: `Ok` without the holders sweep; the tree not marked killed.
#[cfg(target_os = "macos")]
#[skuld::test]
fn kill_tree_after_own_wait_and_reuse_kills_a_holder_that_left_the_group(#[fixture(stray_signal)] _stray: &Group) {
    use crate::identity::Resolved;
    use crate::test_child::reaped_elsewhere::escaped_holder_tree;
    crate::log_capture::install();
    let groups = record_kill_group();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (child, mut holder) = escaped_holder_tree(crate::ContainMode::Session, false);
    holder.kill_and_wait(&child);
    let other = child.id().expect(crate::test_child::IDENTITY_READ).with_other_value();
    let _reused = crate::child::fault::force_next_root_read(Resolved::Found(other));

    child.kill_tree().expect("every holder was reached");

    assert_eq!(
        groups.killed(),
        Vec::<i32>::new(),
        "no killpg by a reaped root's number"
    );
    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    assert!(
        holders.killed().contains(&holder.pid),
        "the holder that left the group is killed: {:?}",
        holders.killed()
    );
    let mark = crate::log_capture::mark();
    drop(child);
    assert_eq!(
        warns_since(mark, ""),
        Vec::<String>::new(),
        "the tree was killed: the drop does not warn"
    );
    holder.assert_dies();
}

/// After this handle's own reap, with the pid naming nothing, an fd marker with a group (the default
/// containment) still kills its holders.
#[cfg(target_os = "macos")]
#[skuld::test]
fn kill_tree_after_own_wait_of_a_marker_with_a_group_kills_only_the_holders(#[fixture(stray_signal)] _stray: &Group) {
    use crate::identity::Resolved;
    use crate::test_child::reaped_elsewhere::escaped_holder_tree;
    crate::log_capture::install();
    let groups = record_kill_group();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (child, mut holder) = escaped_holder_tree(crate::ContainMode::Session, false);
    holder.kill_and_wait(&child);
    let _gone = crate::child::fault::force_root_reads(Resolved::Gone);

    let err = child
        .kill_tree()
        .expect_err("a group the pid no longer names is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert!(
        err.to_string()
            .contains(crate::child::drop_report::END_THEM_BEFORE_WAIT),
        "{err}"
    );
    assert_eq!(
        groups.killed(),
        Vec::<i32>::new(),
        "no killpg by a reaped root's number"
    );
    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    assert_eq!(
        holders.signals_to(holder.pid),
        [nix::sys::signal::Signal::SIGKILL],
        "the holder that left the group is killed"
    );
    let mark = crate::log_capture::mark();
    drop(child);
    assert_eq!(
        warns_since(mark, "").len(),
        1,
        "the group was not killed: the drop warns once"
    );
    holder.assert_dies();
}

/// `graceful_shutdown_tree` after this handle's own reap, with the pid naming nothing: the holders
/// get the `SIGTERM`, then the kill, and nothing goes out by the pid. The holder ignores the
/// `SIGTERM`, so only the kill ends it. The call is `Unassessable`, its text says how to end the
/// descendants, and the drop warns that the group was not killed.
#[cfg(target_os = "macos")]
#[skuld::test]
fn graceful_shutdown_tree_after_own_wait_of_a_marker_with_a_group_signals_only_the_holders(
    #[fixture(stray_signal)] _stray: &Group,
) {
    use crate::identity::Resolved;
    use crate::test_child::reaped_elsewhere::escaped_holder_tree;
    crate::log_capture::install();
    let killed = record_kill_group();
    let termed = record_term_group();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (child, mut holder) = escaped_holder_tree(crate::ContainMode::Session, true);
    holder.kill_and_wait(&child);
    let _gone = crate::child::fault::force_root_reads(Resolved::Gone);

    let err = child
        .graceful_shutdown_tree(std::time::Duration::ZERO)
        .expect_err("a group the pid no longer names is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert!(
        err.to_string()
            .contains(crate::child::drop_report::END_THEM_BEFORE_WAIT),
        "{err}"
    );
    assert_eq!(
        termed.termed(),
        Vec::<i32>::new(),
        "no SIGTERM by a reaped root's number"
    );
    assert_eq!(
        killed.killed(),
        Vec::<i32>::new(),
        "no killpg by a reaped root's number"
    );
    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    assert_eq!(
        holders.signals_to(holder.pid),
        [nix::sys::signal::Signal::SIGTERM, nix::sys::signal::Signal::SIGKILL],
        "the SIGTERM, then the kill"
    );
    let mark = crate::log_capture::mark();
    drop(child);
    assert_eq!(
        warns_since(mark, "").len(),
        1,
        "the group was not killed: the drop warns once"
    );
    holder.assert_dies();
}

/// After a foreign reap, an fd marker still kills its holders, by the pipe they hold, including one
/// that left the group. Only what names the tree by the root's number is skipped, so the call is
/// `Unassessable`, with one warn naming the group.
///
/// Mutant: no holders sweep after a foreign reap.
#[cfg(target_os = "macos")]
#[skuld::test]
fn kill_tree_after_a_foreign_reap_kills_a_holder_that_left_the_group(#[fixture(stray_signal)] _stray: &Group) {
    use crate::test_child::reaped_elsewhere::escaped_holder_tree;
    crate::log_capture::install();
    let groups = record_kill_group();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (child, mut holder) = escaped_holder_tree(crate::ContainMode::Session, false);
    let pid = child.pid();
    holder.kill_and_reap_behind(&child);

    let mark = crate::log_capture::mark();
    let err = child
        .kill_tree()
        .expect_err("a group whose root was reaped elsewhere is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        groups.killed(),
        Vec::<i32>::new(),
        "no killpg by a reaped root's number"
    );
    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    assert!(
        holders.killed().contains(&holder.pid),
        "the holder that left the group is killed: {:?}",
        holders.killed()
    );
    assert_one_reaped_elsewhere_warn(mark, pid);
    holder.assert_dies();
}

/// After a foreign reap, `graceful_shutdown_tree` still shuts an fd marker's holders down by the
/// pipe they hold: the `SIGTERM`, then, after the grace, a kill of any left. This holder ignores the
/// `SIGTERM`, so only that kill ends it. Nothing goes out by the root's number, and the call is
/// `Unassessable`, with one warn.
///
/// Mutants: no kill after the grace; a second `SIGTERM` in place of the kill. Either fails by
/// assertion before the wait for the holder's death.
#[cfg(target_os = "macos")]
#[skuld::test]
fn graceful_shutdown_tree_after_a_foreign_reap_kills_a_holder_that_ignores_sigterm(
    #[fixture(stray_signal)] _stray: &Group,
) {
    use crate::test_child::reaped_elsewhere::escaped_holder_tree;
    crate::log_capture::install();
    let killed = record_kill_group();
    let termed = record_term_group();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (child, mut holder) = escaped_holder_tree(crate::ContainMode::Session, true);
    let pid = child.pid();
    holder.kill_and_reap_behind(&child);

    let mark = crate::log_capture::mark();
    let err = child
        .graceful_shutdown_tree(std::time::Duration::ZERO)
        .expect_err("a group whose root was reaped elsewhere is not assessable");

    assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
    assert_eq!(
        killed.killed(),
        Vec::<i32>::new(),
        "no killpg by a reaped root's number"
    );
    assert_eq!(
        termed.termed(),
        Vec::<i32>::new(),
        "no SIGTERM by a reaped root's number"
    );
    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    assert_eq!(
        holders.signals_to(holder.pid),
        [nix::sys::signal::Signal::SIGTERM, nix::sys::signal::Signal::SIGKILL],
        "the SIGTERM, then the kill"
    );
    assert_one_reaped_elsewhere_warn(mark, pid);
    let warns = warns_since(mark, "");
    assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
    holder.assert_dies();
}

/// A `TreeWalk` fd marker has no group, but its sweep walks from the root's pid. After a foreign
/// reap that pid may name another process, so the walk is skipped; the holders are still killed by
/// the pipe they hold. The call is `Unassessable`, with one warn naming the walk.
///
/// Mutant: the root is checked only for a mechanism with a pgid.
#[cfg(target_os = "macos")]
#[skuld::test]
fn kill_tree_of_a_walk_marker_after_a_foreign_reap_walks_nothing_and_kills_the_holder(
    #[fixture(stray_signal)] _stray: &Group,
) {
    use crate::test_child::reaped_elsewhere::{assert_one_reaped_elsewhere_walk_warn, escaped_holder_tree};
    crate::log_capture::install();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (child, mut holder) = escaped_holder_tree(crate::ContainMode::TreeWalk, false);
    let pid = child.pid();
    holder.kill_and_reap_behind(&child);

    let mark = crate::log_capture::mark();
    let result = child.kill_tree();

    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    assert!(
        matches!(result, Err(crate::error::Error::Unassessable { .. })),
        "a walk whose root was reaped elsewhere is not assessable: {result:?}"
    );
    assert!(
        holders.killed().contains(&holder.pid),
        "the holder is killed: {:?}",
        holders.killed()
    );
    assert_one_reaped_elsewhere_walk_warn(mark, pid);
    holder.assert_dies();
}

/// After this handle's own reap, with the pid reused, a `TreeWalk` fd marker's walk from that pid
/// would name only the reuser's children: it is skipped, and the holders are killed by the pipe
/// they hold. `Ok` follows their sweep.
///
/// Mutant: the root is checked only for a mechanism with a pgid.
#[cfg(target_os = "macos")]
#[skuld::test]
fn kill_tree_of_a_walk_marker_after_own_wait_and_reuse_walks_nothing_and_kills_the_holder(
    #[fixture(stray_signal)] _stray: &Group,
) {
    use crate::identity::Resolved;
    use crate::test_child::reaped_elsewhere::escaped_holder_tree;
    crate::log_capture::install();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (child, mut holder) = escaped_holder_tree(crate::ContainMode::TreeWalk, false);
    holder.kill_and_wait(&child);
    let other = child.id().expect(crate::test_child::IDENTITY_READ).with_other_value();
    let _reused = crate::child::fault::force_next_root_read(Resolved::Found(other));

    let result = child.kill_tree();

    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    result.expect("every holder was reached");
    assert!(
        holders.killed().contains(&holder.pid),
        "the holder is killed: {:?}",
        holders.killed()
    );
    holder.assert_dies();
}