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
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
//! Async twins of `child/foreign_reap_tree_tests.rs`: `kill_tree` and `terminate_tree` of a group
//! whose root was reaped behind its handle's back. The recorders are thread-local and skuld's
//! default runtime is current-thread, so the sends are recorded on this thread.

use crate::containment::unix::fault::{record_kill_group, record_term_group};
use crate::log_capture::warns_since;
use crate::test_child::reaped_elsewhere::{
    assert_one_reaped_elsewhere_warn, assert_one_untrusted_number_warn, kill_own_child, reap_behind, MAY_STILL_RUN,
    REAPED_ELSEWHERE,
};
use crate::test_child::Console;
#[cfg(target_os = "linux")]
use crate::test_groups::{pidfd_group_signal, Group};

/// A `sleep` in its own session: `Attached::ProcessGroup`, or on macOS an fd marker with a group.
fn session_sleep() -> crate::tokio::Command {
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(["sleep", "300"]);
    cmd.contain_with(crate::ContainMode::Session);
    cmd
}

/// Async twin of the sync `session_cat`.
#[cfg(target_os = "linux")]
fn session_cat() -> crate::tokio::Command {
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(["cat"]);
    cmd.contain_with(crate::ContainMode::Session);
    cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
    cmd
}

/// A [`session_sleep`] child, killed and reaped behind its back, and its group's id.
fn foreign_reaped_group_root() -> (crate::tokio::Child, u32) {
    let child = session_sleep().spawn().expect("spawn");
    let pid = child.pid();
    kill_own_child(pid);
    reap_behind(pid);
    (child, pid)
}

/// Mutants: `killpg` sent; the decision taken on the number alone (it shows no recycle).
#[skuld::test]
async fn tokio_kill_tree_after_a_foreign_reap_sends_no_killpg() {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let recorder = record_kill_group();
    let (mut 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 [`tokio_kill_tree_after_a_foreign_reap_sends_no_killpg`]. Same mutants.
#[skuld::test]
async fn tokio_terminate_tree_after_a_foreign_reap_sends_no_signal() {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::tokio::test_runtime::assert_current_thread();
    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);
}

/// The sync `kill_tree_after_own_wait_whose_pid_reads_unknown_sends_no_killpg`'s twin. Same
/// mutant.
#[skuld::test]
async fn tokio_kill_tree_after_own_wait_whose_pid_reads_unknown_sends_no_killpg() {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let recorder = record_kill_group();
    let mut child = session_sleep().spawn().expect("spawn");
    let pid = child.pid();
    kill_own_child(pid);
    child.wait().await.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);
}

/// The sync `kill_tree_of_a_root_its_handle_cannot_read_kills_it_through_its_pidfd_alone`'s twin.
/// Same mutants.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_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 crate::test_child::reaped_elsewhere::ROOT_KILLED_BY_PIDFD;
    use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
    use std::os::unix::process::ExitStatusExt as _;
    crate::tokio::test_runtime::assert_current_thread();
    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 forced = force_peek_once(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");
    assert_peeks_exhausted();
    drop(forced);

    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().await.expect("reap the root");
    assert_eq!(status.signal(), Some(libc::SIGKILL), "the pidfd kill ended the root");
}

/// [`session_cat`] that ignores `SIGTERM` (an ignored signal stays ignored across `exec`), so only
/// a kill ends it before its stdin closes.
#[cfg(target_os = "linux")]
fn term_ignoring_session_cat() -> crate::tokio::Command {
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(["sh", "-c", r#"trap "" TERM; echo ready; exec cat"#]);
    cmd.contain_with(crate::ContainMode::Session);
    cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
    cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
    cmd
}

/// The sync `terminate_tree_of_a_root_its_handle_cannot_read_terms_it_through_its_pidfd_alone`'s
/// twin. Same mutants.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_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 crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
    use std::os::unix::process::ExitStatusExt as _;
    crate::tokio::test_runtime::assert_current_thread();
    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 forced = force_peek_once(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");
    assert_peeks_exhausted();
    drop(forced);

    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().await.expect("reap the root");
    assert_eq!(status.signal(), Some(libc::SIGTERM), "the pidfd SIGTERM ended the root");
}

/// The sync `graceful_shutdown_tree_of_a_root_its_handle_cannot_read_escalates_through_its_pidfd_alone`'s
/// twin. Same mutants.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_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::{ROOT_KILLED_BY_PIDFD, ROOT_TERMED_BY_PIDFD};
    use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
    use std::os::unix::process::ExitStatusExt as _;
    crate::tokio::test_runtime::assert_current_thread();
    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();
    ::tokio::io::AsyncBufReadExt::read_line(
        &mut ::tokio::io::BufReader::new(child.stdout().expect("stdout pipe")),
        &mut ready,
    )
    .await
    .expect("the root ignores SIGTERM");

    let mark = crate::log_capture::mark();
    let sends = crate::send_log::Capture::start();
    let forced = force_peek_once(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)
        .await
        .expect_err("a group whose root nothing can answer for is not assessable");
    assert_peeks_exhausted();
    drop(forced);

    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().await.expect("reap the root");
    assert_eq!(status.signal(), Some(libc::SIGKILL), "the kill ended the root");
}

/// The sync `graceful_shutdown_tree_of_a_root_its_handle_cannot_read_sends_no_kill_to_one_that_exited`'s
/// twin. The grace's deadline has passed when its wait arms (the clock it is computed on lags an
/// hour), so the timer wins the first poll, before the reactor reports the exit: only the one look at
/// the pidfd at expiry can see it. The hook releases the root's stdin before it waits for the exit,
/// so a `SIGTERM` that was not delivered shows in the root's status.
///
/// Mutants: the sync twin's; no look at the pidfd at expiry.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_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::signal::Sig::Term;
    use crate::test_child::reaped_elsewhere::{ROOT_KILLED_BY_PIDFD, ROOT_TERMED_BY_PIDFD};
    use crate::wait::exit_only::seams::{assert_peeks_exhausted, force_peek_once};
    use std::os::unix::process::ExitStatusExt as _;
    crate::tokio::test_runtime::assert_current_thread();
    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 forced = force_peek_once(Err(std::io::Error::other("forced peek failure")));
    let exited_first = crate::graceful_hooks::at(crate::graceful_hooks::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 past = crate::tokio::wait::tokio_now()
        .checked_sub(std::time::Duration::from_secs(3600))
        .expect("an hour before now");
    let lagging = crate::tokio::wait::now_override::install(past);
    let err = child
        .graceful_shutdown_tree(std::time::Duration::from_secs(60))
        .await
        .expect_err("a group whose root nothing can answer for is not assessable");
    drop(lagging);
    drop(exited_first);
    assert_peeks_exhausted();
    drop(forced);

    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().await.expect("reap the root");
    assert_eq!(status.signal(), Some(libc::SIGTERM), "the SIGTERM ended the root");
}

/// The sync `graceful_shutdown_tree_after_a_foreign_reap_warns_once_and_ends`'s twin. Same mutant.
#[skuld::test]
async fn tokio_graceful_shutdown_tree_after_a_foreign_reap_warns_once_and_ends() {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let killed = record_kill_group();
    let termed = record_term_group();
    let (mut child, pgid) = foreign_reaped_group_root();

    let mark = crate::log_capture::mark();
    let err = child
        .graceful_shutdown_tree(std::time::Duration::from_secs(1))
        .await
        .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:?}"
    );
}

/// The sync `graceful_shutdown_tree_after_own_wait_claims_no_sigterm_it_did_not_send`'s twin. Same
/// mutant.
#[skuld::test]
async fn tokio_graceful_shutdown_tree_after_own_wait_claims_no_sigterm_it_did_not_send() {
    #[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::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let killed = record_kill_group();
    let termed = record_term_group();
    let mut child = session_sleep().spawn().expect("spawn");
    let pid = child.pid();
    kill_own_child(pid);
    child.wait().await.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)
        .await
        .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:?}"
    );
}

/// A backend forgotten because it could not be shown to be ours may still be running: `kill_tree`
/// treats its root as `Unknown`, never as reaped elsewhere. On Linux below 6.9 the root is killed
/// through the pidfd the handle kept, which names it exactly, and nothing goes by its number. On
/// macOS that is the identity path: the marker holders, the root among them, are killed by the pipe
/// they hold.
///
/// Mutants: a backend forgotten unproven reads as `Reaped`; it drops its pidfd; it answers a signal
/// without sending it through its pidfd.
#[skuld::test]
async fn tokio_kill_tree_of_a_backend_forgotten_unproven_treats_the_root_as_unknown() {
    #[cfg(target_os = "linux")]
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let groups = record_kill_group();
    #[cfg(target_os = "macos")]
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (stdin, writer) = crate::test_child::held_writer_stdin();
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.stdin(stdin).expect("set stdin");
    cmd.contain_with(crate::ContainMode::Session);
    let mut child = cmd.spawn().expect("spawn");
    let pid = child.pid();
    child
        .proc_mut()
        .forget_as("was forgotten by a test", super::proc_source::Forgotten::Unproven);

    let mark = crate::log_capture::mark();
    #[cfg(target_os = "linux")]
    let sends = crate::send_log::Capture::start();
    let result = child.kill_tree();

    assert_eq!(
        crate::log_capture::records_since_on_current_thread(mark, REAPED_ELSEWHERE),
        [],
        "a root not shown reaped is not called reaped"
    );
    assert_eq!(
        groups.killed(),
        Vec::<i32>::new(),
        "no killpg by a number that may name another process"
    );
    #[cfg(target_os = "linux")]
    {
        use crate::test_child::reaped_elsewhere::ROOT_KILLED_BY_PIDFD;
        let err = result.expect_err("a group whose root nothing can answer for is not assessable");
        assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
        assert_eq!(
            sends.entries(),
            [(pid, crate::signal::Sig::Kill, crate::send_log::Via::Pidfd)],
            "the root alone is killed, through its pidfd"
        );
        let warns = warns_since(mark, "");
        assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
        assert!(
            warns[0].contains(MAY_STILL_RUN),
            "it says the root may still be running: {warns:?}"
        );
        assert!(
            warns[0].contains(&format!("pgid {pid}")),
            "it names the group: {warns:?}"
        );
        assert!(warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
    }
    #[cfg(target_os = "macos")]
    {
        let _ = MAY_STILL_RUN;
        // The holders are not the tree: a kill that skipped the rest is `Unassessable`, though every
        // holder was reached.
        crate::child::unpinned_public_tests::assert_holders_only(
            &result.expect_err("the sweep skipped the rest of the tree"),
            "kill_tree",
            crate::child::unpinned_public_tests::Untrusted::Unknown,
        );
        assert!(
            holders.killed().contains(&pid),
            "the root holds the marker: {:?}",
            holders.killed()
        );
    }
    drop(writer);
    reap_behind(pid);
}

/// `graceful_shutdown_tree` of a backend forgotten because it could not be shown to be ours, below
/// 6.9: the root is `Unknown`, so the `SIGTERM` goes through the pidfd the handle kept, and the grace
/// watches the root's exit through it. A hook after the `SIGTERM` checks it went out, then waits for
/// the root to exit, so the grace's one look at expiry finds it gone and no kill follows.
///
/// Mutants: a backend forgotten unproven answers a signal without sending it through its pidfd; its
/// grace does not watch the pidfd.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_of_a_backend_forgotten_unproven_shuts_the_root_down_through_its_pidfd() {
    use crate::graceful_hooks::{at, HookPoint};
    use crate::signal::Sig::Term;
    use crate::test_child::reaped_elsewhere::{reap_behind_status, ROOT_KILLED_BY_PIDFD, ROOT_TERMED_BY_PIDFD};
    use std::os::unix::process::ExitStatusExt as _;
    let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let killed = record_kill_group();
    let termed = record_term_group();
    let (stdin, writer) = crate::test_child::held_writer_stdin();
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.stdin(stdin).expect("set stdin");
    cmd.contain_with(crate::ContainMode::Session);
    let mut child = cmd.spawn().expect("spawn");
    let pid = child.pid();
    child
        .proc_mut()
        .forget_as("was forgotten by a test", super::proc_source::Forgotten::Unproven);

    let mark = crate::log_capture::mark();
    let sends = std::rc::Rc::new(crate::send_log::Capture::start());
    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"
            );
            // Released after the SIGTERM, so a SIGTERM that never took effect fails the assertion
            // on how the root ended below, not the wait here.
            drop(writer);
            crate::test_child::wait_until_zombie(pid);
        }
    });
    let err = child
        .graceful_shutdown_tree(std::time::Duration::ZERO)
        .await
        .expect_err("a group whose root nothing can answer for is not assessable");
    drop(exited_first);

    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_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 warns = warns_since(mark, "");
    assert_eq!(warns.len(), 1, "exactly one warn: {warns:?}");
    assert!(warns[0].contains(MAY_STILL_RUN), "{warns:?}");
    assert!(warns[0].contains(ROOT_TERMED_BY_PIDFD), "{warns:?}");
    assert!(!warns[0].contains(ROOT_KILLED_BY_PIDFD), "{warns:?}");
    assert_eq!(
        reap_behind_status(pid).signal(),
        Some(libc::SIGTERM),
        "the SIGTERM ended the root"
    );
}

/// 6.9+: `graceful_shutdown_tree` of a live root whose backend was forgotten unproven shuts its
/// group down through the pidfd the handle kept, and does not say the root was reaped elsewhere: the
/// handle cannot say whether it was reaped, so its status is not collected.
///
/// Mutants: an unproven forget drops the pidfd; the answer says the root was reaped elsewhere.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_of_a_backend_forgotten_unproven_shuts_its_group_down(
    #[fixture(pidfd_group_signal)] _group: &Group,
) {
    crate::tokio::test_runtime::assert_current_thread();
    let (stdin, writer) = crate::test_child::held_writer_stdin();
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.stdin(stdin).expect("set stdin");
    cmd.contain_with(crate::ContainMode::Session);
    let mut child = cmd.spawn().expect("spawn");
    let pid = child.pid();
    child
        .proc_mut()
        .forget_as("was forgotten by a test", super::proc_source::Forgotten::Unproven);

    let capture = crate::send_log::Capture::start();
    let result = child.graceful_shutdown_tree(std::time::Duration::ZERO).await;
    let steps = capture.group_steps();

    crate::send_log::assert_group_signalled_first_through_the_pidfd(&steps, libc::SIGTERM);
    match result {
        Err(crate::error::Error::Unassessable { detail, source: None }) => {
            assert!(detail.contains("could not say whether it was reaped"), "{detail}");
            assert!(!detail.contains(REAPED_ELSEWHERE), "{detail}");
        }
        other => panic!("expected the tree down without the root's status, got {other:?}"),
    }
    drop(writer);
    reap_behind(pid);
}

/// A root that leaves a marker holder outside its process group: see
/// [`escaped_holder_script`](crate::test_child::reaped_elsewhere::escaped_holder_script). Returns the
/// child, the holder's pid, the held stdin and the stderr the holder's death closes.
#[cfg(target_os = "macos")]
async fn escaped_holder_tree(
    mode: crate::ContainMode,
    ignore_term: bool,
) -> (
    crate::tokio::Child,
    u32,
    crate::tokio::ChildStdin,
    crate::tokio::ChildStderr,
) {
    use ::tokio::io::AsyncBufReadExt as _;
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args([
        "sh",
        "-c",
        &crate::test_child::reaped_elsewhere::escaped_holder_script(ignore_term),
    ]);
    cmd.contain_with(mode);
    cmd.stdin(crate::Stdio::pipe_in()).expect("stdin pipe");
    cmd.stdout(crate::Stdio::pipe_out()).expect("stdout pipe");
    cmd.stderr(crate::Stdio::pipe_out()).expect("stderr pipe");
    let mut child = cmd.spawn().expect("spawn");
    assert_eq!(child.containment(), crate::Containment::FdMarker);
    let mut line = String::new();
    ::tokio::io::BufReader::new(child.stdout().expect("stdout pipe"))
        .read_line(&mut line)
        .await
        .expect("read the holder's pid");
    let holder: u32 = line.trim().parse().expect("the holder's pid");
    crate::test_child::reaped_elsewhere::assert_left_the_group(holder, child.pid());
    let stdin = child.stdin().expect("stdin pipe");
    let stderr = child.stderr().expect("stderr pipe");
    (child, holder, stdin, stderr)
}

/// Blocks until the holder is gone: its stderr reaches EOF. Call only once the test has shown the
/// holder was signalled, so a missed kill fails an assertion rather than hanging here.
#[cfg(target_os = "macos")]
async fn assert_holder_dies(mut stderr: crate::tokio::ChildStderr) {
    use ::tokio::io::AsyncReadExt as _;
    let mut rest = Vec::new();
    stderr
        .read_to_end(&mut rest)
        .await
        .expect("the holder's stderr reaches EOF");
    assert!(rest.is_empty(), "unexpected stderr from the holder: {rest:?}");
}

/// The sync `kill_tree_after_own_wait_and_reuse_kills_a_holder_that_left_the_group`'s twin. Same
/// mutants.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_after_own_wait_and_reuse_kills_a_holder_that_left_the_group() {
    use crate::identity::Resolved;
    crate::tokio::test_runtime::assert_current_thread();
    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 (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, false).await;
    child.kill().expect("kill the root");
    child.wait().await.expect("this handle reaps the root");
    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),
        "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"
    );
    assert_holder_dies(stderr).await;
}

/// The sync `kill_tree_after_own_wait_of_a_marker_with_a_group_is_ok_and_marks_the_tree_killed`'s
/// twin. Same mutants.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_after_own_wait_of_a_marker_with_a_group_is_ok_and_marks_the_tree_killed() {
    use crate::identity::Resolved;
    crate::tokio::test_runtime::assert_current_thread();
    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 (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, false).await;
    let pid = child.pid();
    child.kill().expect("kill the root");
    child.wait().await.expect("this handle reaps the root");
    let _gone = crate::child::fault::force_root_reads(Resolved::Gone);

    child.kill_tree().expect("the group and every holder were reached");

    // A pid that names nothing is still killed as the group's id.
    let by_id = groups.killed();
    assert!(
        !by_id.is_empty() && by_id.iter().all(|g| *g == pid as i32),
        "the group is killed by its id: {by_id:?}"
    );
    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    assert_eq!(
        holders.signals_to(holder),
        [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, ""),
        Vec::<String>::new(),
        "the tree was killed: the drop does not warn"
    );
    assert_holder_dies(stderr).await;
}

/// The sync `graceful_shutdown_tree_after_own_wait_of_a_marker_with_a_group_answers_the_collected_status`'s
/// twin. Same mutants.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_after_own_wait_of_a_marker_with_a_group_answers_the_collected_status() {
    use crate::identity::Resolved;
    crate::tokio::test_runtime::assert_current_thread();
    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 (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, true).await;
    let pid = child.pid();
    child.kill().expect("kill the root");
    let collected = child.wait().await.expect("this handle reaps the root");
    let _gone = crate::child::fault::force_root_reads(Resolved::Gone);

    let status = child
        .graceful_shutdown_tree(std::time::Duration::ZERO)
        .await
        .expect("the group and every holder were reached");

    assert_eq!(status, collected, "the status the wait collected");
    // A pid that names nothing is still signalled as the group's id.
    for (what, by_id) in [("SIGTERM", termed.termed()), ("kill", killed.killed())] {
        assert!(
            !by_id.is_empty() && by_id.iter().all(|g| *g == pid as i32),
            "the group's {what} by its id: {by_id:?}"
        );
    }
    assert_eq!(walks.walked(), Vec::<u32>::new(), "no walk from a reaped root's number");
    assert_eq!(
        holders.signals_to(holder),
        [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, ""),
        Vec::<String>::new(),
        "the tree was killed: the drop does not warn"
    );
    assert_holder_dies(stderr).await;
}

/// The sync `kill_tree_after_a_foreign_reap_kills_a_holder_that_left_the_group`'s twin. Same mutant.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_after_a_foreign_reap_kills_a_holder_that_left_the_group() {
    crate::tokio::test_runtime::assert_current_thread();
    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 (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, false).await;
    let pid = child.pid();
    child.kill().expect("kill the root");
    reap_behind(pid);

    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),
        "the holder that left the group is killed: {:?}",
        holders.killed()
    );
    assert_one_reaped_elsewhere_warn(mark, pid);
    assert_holder_dies(stderr).await;
}

/// The sync `graceful_shutdown_tree_after_a_foreign_reap_kills_a_holder_that_ignores_sigterm`'s
/// twin. Same mutants.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_graceful_shutdown_tree_after_a_foreign_reap_kills_a_holder_that_ignores_sigterm() {
    crate::tokio::test_runtime::assert_current_thread();
    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 (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::Session, true).await;
    let pid = child.pid();
    child.kill().expect("kill the root");
    reap_behind(pid);

    let mark = crate::log_capture::mark();
    let err = child
        .graceful_shutdown_tree(std::time::Duration::ZERO)
        .await
        .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),
        [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:?}");
    assert_holder_dies(stderr).await;
}

/// The sync `kill_tree_of_a_walk_marker_after_a_foreign_reap_walks_nothing_and_kills_the_holder`'s
/// twin. Same mutant.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_of_a_walk_marker_after_a_foreign_reap_walks_nothing_and_kills_the_holder() {
    use crate::test_child::reaped_elsewhere::assert_one_reaped_elsewhere_walk_warn;
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::TreeWalk, false).await;
    let pid = child.pid();
    child.kill().expect("kill the root");
    reap_behind(pid);

    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),
        "the holder is killed: {:?}",
        holders.killed()
    );
    assert_one_reaped_elsewhere_walk_warn(mark, pid);
    assert_holder_dies(stderr).await;
}

/// The sync `kill_tree_of_a_walk_marker_after_own_wait_and_reuse_walks_nothing_and_kills_the_holder`'s
/// twin. Same mutant.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn tokio_kill_tree_of_a_walk_marker_after_own_wait_and_reuse_walks_nothing_and_kills_the_holder() {
    use crate::identity::Resolved;
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let walks = crate::containment::treewalk::fault::record_walks();
    let holders = crate::containment::fdmarker::fault::record_holder_kills();
    let (mut child, holder, _stdin, stderr) = escaped_holder_tree(crate::ContainMode::TreeWalk, false).await;
    child.kill().expect("kill the root");
    child.wait().await.expect("this handle reaps the root");
    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),
        "the holder is killed: {:?}",
        holders.killed()
    );
    assert_holder_dies(stderr).await;
}