cosca 0.10.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
// Unit tests for the Unix process-group signal wrappers.
// Substantive end-to-end coverage is in the integration test
// (`unix_kill_tree_reaps_the_grandchild` in `tests/spawn_io.rs`).

use super::{kill_group, term_group};
use crate::test_child::blocker_with;
use crate::test_groups::{stray_signal, Group};
use crate::test_support::stated_root::StatedRoot;

// This file deliberately has NO "absent group" test that calls kill_group/term_group on a
// scanned-forward or freshly-reaped pgid. Once these functions unconditionally verify (this
// task's note above), they always list whatever pgid they are given and SIGNAL whatever
// `converge` finds there — so aiming one at a guessed-empty pgid is a check-then-act race
// against the kernel's own pid allocator (which favors exactly the small, recently-used
// numbers such a guess would produce) and against this crate's own sibling tests, several of
// which spawn `process_group(0)` children (pgid == pid). A collision would deliver a REAL
// SIGKILL to an unrelated process. That coverage is not missing: `converge`'s "empty result
// list ⇒ Cleared" path is identical whether the listing found zero members or one member
// `classify_member` immediately drops as `NotASurvivor` (the pure
// `classify_member_is_total_over_liveness` test proves the classification directly, with no
// listing involved at all), and `kill_group_on_an_all_zombie_group_is_ok`/
// `term_group_on_an_all_zombie_group_is_ok` below exercise that exact shape through the real
// public API, end to end, without ever risking a signal to a pgid this test does not own.

// Block until `pid` has exited WITHOUT reaping it, so it stays a zombie the
// kernel still lists in its process group. Nothing here is timed. Retries on
// EINTR, matching the `waitid(WEXITED | WNOWAIT)` idiom `reap_now` uses in
// `src/tokio/child.rs`, so a stray signal to the test process fails the wait,
// not the test.
fn await_zombie(pid: u32) {
    let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
    loop {
        // SAFETY: a well-formed `waitid` call; `info` is a valid, owned, zeroed
        // `siginfo_t` the kernel fills in. WNOWAIT leaves the child reapable.
        let rc = unsafe { libc::waitid(libc::P_PID, pid as libc::id_t, &mut info, libc::WEXITED | libc::WNOWAIT) };
        if rc == 0 {
            return;
        }
        let err = std::io::Error::last_os_error();
        if err.raw_os_error() == Some(libc::EINTR) {
            continue;
        }
        panic!("waitid failed: {err}");
    }
}

/// `pgid <= 0` must be rejected before any signal is sent — 0 addresses the CALLER's own
/// process group (POSIX), and a negative value is the broadcast/double-negation hazard this
/// plan's Background measures. Safe to test directly: no real process or process group is
/// involved, and a correct implementation never reaches the `killpg`/`signal_direct` calls
/// this guards, so there is nothing to signal even if the test is wrong.
#[skuld::test]
fn kill_group_and_term_group_reject_non_positive_pgid(#[fixture(stray_signal)] _stray: &Group) {
    for pgid in [0, -1, i32::MIN] {
        let kill_err = kill_group(pgid, Some(&StatedRoot::unreaped()), &mut Vec::new())
            .expect_err("kill_group(non-positive) must be Err");
        assert!(
            matches!(kill_err, crate::error::Error::Unassessable { .. }),
            "got {kill_err:?}"
        );
        let term_err =
            term_group(pgid, Some(&StatedRoot::unreaped())).expect_err("term_group(non-positive) must be Err");
        assert!(
            matches!(term_err, crate::error::Error::Unassessable { .. }),
            "got {term_err:?}"
        );
    }
}

/// A `pgid <= 0` refusal is a terminate that sent nothing: the tree call ends with it, with no grace.
///
/// Mutant: the refusal is a `Sent { result: Err(..) }`, which `graceful_shutdown_tree` holds for.
#[skuld::test]
fn term_group_tree_of_a_non_positive_pgid_says_nothing_was_sent(#[fixture(stray_signal)] _stray: &Group) {
    for pgid in [0, -1, i32::MIN] {
        match super::term_group_tree(
            pgid,
            Some(&StatedRoot::unreaped()),
            crate::containment::TreeStop::default(),
        ) {
            crate::containment::TerminateOutcome::NotSent(crate::error::Error::Unassessable {
                source: None, ..
            }) => {}
            other => panic!("pgid {pgid}: expected NotSent(Unassessable), got {other:?}"),
        }
    }
}

/// kill_group on a real owned process group succeeds, and really did kill it.
#[skuld::test]
fn kill_group_on_owned_group_succeeds() {
    use std::os::unix::process::CommandExt;
    use std::os::unix::process::ExitStatusExt as _;
    // Spawn a child in its own private group (pgid == child pid) so we can
    // SIGKILL it without disturbing the test runner's own group.
    let mut child = blocker_with(|cat| {
        cat.process_group(0);
    });
    // Closed right after the kill: a `kill_group` that signals nothing exits 0.
    let stdin = child.stdin.take().expect("piped stdin");
    let pgid = child.id() as i32;
    assert!(
        kill_group(pgid, Some(&StatedRoot::unreaped()), &mut Vec::new()).is_ok(),
        "kill_group on owned group must succeed"
    );
    drop(stdin);
    let status = child.wait().expect("wait after kill_group");
    assert_eq!(
        status.signal(),
        Some(libc::SIGKILL),
        "kill_group must have actually killed the leader, got {status:?}"
    );
}

/// `kill_group`'s `killpg` dooms the group BEFORE `converge` lists it: a member cannot fork once
/// its `SIGKILL` is pending, so the process the listing never saw is never created. Without that
/// first `killpg`, `converge`'s per-member resend is the only delivery, and a member that forks
/// in the window between the listing and its own signal leaves a survivor no one signals.
///
/// The leader is a shell that forks a process, writing `spawned`, for each line it reads. The
/// hook, fired in `converge` right after the listing, feeds it one line and reads one back from a
/// stdout only the group holds. With the first `killpg` the shell is already dead, so the write
/// finds no reader (`EPIPE`, ignored) and the read is EOF: empty. Without it the shell forks, and
/// the read returns `spawned`.
///
/// Linux only: `fork` refuses once a fatal signal is pending; no such guarantee is asserted elsewhere.
#[cfg(target_os = "linux")]
#[skuld::test]
fn kill_group_dooms_the_group_before_it_is_listed() {
    use std::io::BufRead as _;
    use std::os::unix::process::CommandExt;
    use std::os::unix::process::ExitStatusExt as _;

    let mut child = crate::test_child::StdChild::spawn(
        std::process::Command::new("sh")
            .arg("-c")
            .arg("while read x; do (echo spawned; exec cat) & done")
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .process_group(0),
    );
    let mut stdin = child.stdin.take().expect("piped stdin");
    let mut stdout = std::io::BufReader::new(child.stdout.take().expect("piped stdout"));
    let pgid = child.id() as i32;

    let seen = std::rc::Rc::new(std::cell::RefCell::new(None::<String>));
    let record = seen.clone();
    let _armed = super::group::fault::set_after_listing(move || {
        crate::test_child::write_to_possibly_dead_stdin(&mut stdin, b"x\n");
        let mut line = String::new();
        stdout.read_line(&mut line).expect("read to a line or EOF");
        *record.borrow_mut() = Some(line);
    });
    kill_group(pgid, Some(&StatedRoot::unreaped()), &mut Vec::new()).expect("kill_group on an owned group");

    let status = child.wait().expect("reap the leader");
    let line = seen.borrow_mut().take().expect("the hook ran in converge");
    assert_eq!(
        line, "",
        "a member forked after the listing: the first killpg did not doom the group"
    );
    assert_eq!(status.signal(), Some(libc::SIGKILL), "got {status:?}");
}

/// `term_group` on a real owned, LIVE (non-zombie) group succeeds, and the leader really did
/// exit because of the delivered `SIGTERM` — not merely because it stayed reachable.
/// Previously undertested (review): the only prior `term_group` coverage besides the
/// pgid-guard test was the all-zombie case; the actual real-delivery half of `term_group` —
/// the initial `killpg(pgid, SIGTERM)` inside `signal_group`, which is the ONLY place a real
/// SIGTERM is ever sent (`converge`'s own SIGTERM path deliberately only probes,
/// never resends) — had no test proving delivery landed. Without this, a broken initial
/// `killpg` call (wrong signal constant, call removed, argument order swapped) would still
/// pass every other `term_group` test: the probe would confirm the leader is reachable,
/// `converge` would report `Cleared`, and `term_group` would return `Ok(())` while the leader
/// stayed alive — exactly the false-`Ok` class #61 exists to close, on the one path with no
/// closer until now.
#[skuld::test]
fn term_group_on_owned_group_succeeds() {
    use std::os::unix::process::{CommandExt, ExitStatusExt as _};
    // `cat` installs no SIGTERM handler of its own, so a delivered SIGTERM actually ends it —
    // distinguishing "exited because of the signal" from "merely stayed reachable". Its stdin is
    // closed right after the call: a `term_group` that delivers nothing exits 0.
    let mut child = blocker_with(|cat| {
        cat.process_group(0);
    });
    let stdin = child.stdin.take().expect("piped stdin");
    let pgid = child.id() as i32;
    assert!(
        term_group(pgid, Some(&StatedRoot::unreaped())).is_ok(),
        "term_group on owned group must succeed"
    );
    drop(stdin);
    let status = child.wait().expect("wait after term_group");
    assert_eq!(
        status.signal(),
        Some(libc::SIGTERM),
        "term_group must have actually delivered SIGTERM and ended the leader, got {status:?}"
    );
}

/// The benign half of the bug. A group whose only member is an unreaped zombie
/// has nothing left to kill, so teardown of it is honestly `Ok`.
///
/// On macOS this is the arm that made the bug hard: xnu excludes zombies from
/// the pgrp iteration before counting, so `killpg` reports EPERM for what is
/// really an empty group. The assertion below pins that, so the test cannot
/// quietly stop exercising the interesting path.
#[skuld::test]
fn kill_group_on_an_all_zombie_group_is_ok() {
    use std::os::unix::process::CommandExt;
    let child = crate::test_spawn::spawn(std::process::Command::new("true").process_group(0)).expect("spawn true");
    let pid = child.id();
    await_zombie(pid);

    #[cfg(target_os = "macos")]
    assert_eq!(
        nix::sys::signal::killpg(nix::unistd::Pid::from_raw(pid as i32), None),
        Err(nix::errno::Errno::EPERM),
        "macOS reports EPERM for an all-zombie group; if this changed, the arm under test moved"
    );

    assert!(
        kill_group(pid as i32, Some(&StatedRoot::unreaped()), &mut Vec::new()).is_ok(),
        "a group holding only a zombie is already down"
    );
    let mut child = child;
    child.wait().expect("reap");
}

/// Same, for the graceful signal. Same arm-pinning assertion as
/// `kill_group_on_an_all_zombie_group_is_ok` above, for the same reason: without it, a
/// future change to macOS's zombie-exclusion behavior (or a Linux-vs-macOS divergence
/// change) could leave this test passing on `term_group`'s own answer alone while silently
/// no longer exercising the branch it documents.
#[skuld::test]
fn term_group_on_an_all_zombie_group_is_ok() {
    use std::os::unix::process::CommandExt;
    let child = crate::test_spawn::spawn(std::process::Command::new("true").process_group(0)).expect("spawn true");
    let pid = child.id();
    await_zombie(pid);

    #[cfg(target_os = "macos")]
    assert_eq!(
        nix::sys::signal::killpg(
            nix::unistd::Pid::from_raw(pid as i32),
            nix::sys::signal::Signal::SIGTERM
        ),
        Err(nix::errno::Errno::EPERM),
        "macOS reports EPERM for an all-zombie group; if this changed, the arm under test moved"
    );
    #[cfg(target_os = "linux")]
    assert_eq!(
        nix::sys::signal::killpg(
            nix::unistd::Pid::from_raw(pid as i32),
            nix::sys::signal::Signal::SIGTERM
        ),
        Ok(()),
        "Linux reports Ok for an all-zombie group (no live member to refuse); if this changed, \
         the arm under test moved"
    );

    assert!(
        term_group(pid as i32, Some(&StatedRoot::unreaped())).is_ok(),
        "a group holding only a zombie is already down"
    );
    let mut child = child;
    child.wait().expect("reap");
}

// What a group's kill and terminate decide on one look at the root -----

mod group_kill_and_terminate {
    use super::super::{fault, kill_group_tree, term_group_tree};
    use crate::containment::dispatch::TreeKillOutcome;
    use crate::containment::{Refusal, TerminateOutcome, Why};
    use crate::signal::RootState;
    use crate::test_support::stated_root::StatedRoot;

    /// A pgid no test process holds: the recorders stand in for the sends.
    const PGID: i32 = 999_999;

    /// What the root's own handle says, for a row.
    #[derive(Clone, Copy, Debug)]
    enum Root {
        Unreaped,
        ReapedElsewhere,
        Unknown,
    }

    fn stated(root: Root) -> StatedRoot<'static> {
        StatedRoot::with_state(PGID as u32, move || match root {
            Root::Unreaped => RootState::Unreaped,
            Root::ReapedElsewhere => RootState::Reaped,
            Root::Unknown => RootState::Unknown(std::sync::Arc::new(std::io::Error::other("a failed peek"))),
        })
    }

    /// What a skipped kill or terminate says it did not do, and why.
    fn assert_skip(skip: &crate::containment::Skip, verb: &str, root: Root) {
        assert_eq!(
            skip.action,
            format!("{verb} its process group (pgid {PGID})"),
            "{root:?}"
        );
        assert!(skip.holders.is_none(), "a group has no holders: {root:?}");
        match (&skip.why, root) {
            (Why::MayNameAnother(Refusal::ReapedElsewhere(_), _), Root::ReapedElsewhere) => {
                assert!(!skip.root_alone, "{root:?}");
            }
            (Why::MayNameAnother(Refusal::Unsure(..), _), Root::Unknown) => {
                assert_eq!(skip.root_alone, cfg!(target_os = "linux"), "{root:?}");
            }
            // macOS trusts no number its root's handle cannot answer for.
            (Why::Untrusted { .. }, Root::Unknown) if cfg!(target_os = "macos") => {}
            (why, _) => panic!("{root:?} was skipped for {why:?}"),
        }
    }

    /// The kill: sent as usual while the root's number names it, and skipped, nothing sent by the
    /// number, when the number may name another process.
    #[skuld::test]
    fn a_group_kill_sends_or_skips_by_the_roots_state() {
        for root in [Root::Unreaped, Root::ReapedElsewhere, Root::Unknown] {
            let recorder = fault::record_kill_group();
            let outcome = kill_group_tree(PGID, Some(&stated(root))).outcome();
            let sent = recorder.killed();
            match (root, outcome) {
                (Root::Unreaped, TreeKillOutcome::Ran(Ok(()))) => assert_eq!(sent, [PGID], "{root:?}"),
                (_, TreeKillOutcome::Skipped(skip)) => {
                    assert_eq!(sent, Vec::<i32>::new(), "{root:?}");
                    assert_skip(&skip, "kill", root);
                }
                (_, outcome) => panic!("{root:?} was answered {outcome:?}"),
            }
        }
    }

    /// The terminate: the same decisions, with a skip ending the call, and on Linux, for a root its
    /// handle cannot answer for, leaving the root alone to the caller through its handle.
    #[skuld::test]
    fn a_group_terminate_sends_or_skips_by_the_roots_state() {
        for root in [Root::Unreaped, Root::ReapedElsewhere, Root::Unknown] {
            let recorder = fault::record_term_group();
            let outcome = term_group_tree(PGID, Some(&stated(root)), crate::containment::TreeStop::default());
            let sent = recorder.termed();
            match (root, outcome) {
                (
                    Root::Unreaped,
                    TerminateOutcome::Sent {
                        result: Ok(()),
                        earlier: Some(ref earlier),
                        ..
                    },
                ) if earlier == "the SIGTERM" => assert_eq!(sent, [PGID], "{root:?}"),
                // Only Linux has a pidfd to name the root with.
                (Root::Unknown, TerminateOutcome::RootThroughPidfd(skip)) if cfg!(target_os = "linux") => {
                    assert_eq!(sent, Vec::<i32>::new(), "{root:?}");
                    assert_skip(&skip, "signal", root);
                }
                (_, TerminateOutcome::End(skip)) => {
                    assert_eq!(sent, Vec::<i32>::new(), "{root:?}");
                    assert_skip(&skip, "signal", root);
                }
                (_, outcome) => panic!("{root:?} was answered {outcome:?}"),
            }
        }
    }
}

/// A group's send refused because its root's handle could not say whether the root was reaped is
/// `Unassessable` with the handle's error as its `source()`, so the OS code is reachable, and not in
/// the `detail`. Nothing is sent: the check fails first.
///
/// Mutant: the error is folded into the text and no source is kept.
#[skuld::test]
fn a_send_refused_for_an_unsure_root_has_the_handles_error_as_its_source() {
    use super::send_to_group;
    use crate::error::Error;
    use crate::signal::RootState;

    let os_error = std::io::Error::from_raw_os_error(libc::EPERM).to_string();
    let root = StatedRoot::with_state(999_999, || {
        RootState::Unknown(std::sync::Arc::new(std::io::Error::from_raw_os_error(libc::EPERM)))
    });

    let refused = send_to_group(999_999, nix::sys::signal::Signal::SIGTERM, Some(&root))
        .expect_err("a root that cannot say is no check that passes");

    let Error::Unassessable { detail, .. } = &refused else {
        panic!("got {refused:?}");
    };
    assert_eq!(crate::error::first_os_code(&refused), Some(libc::EPERM), "{refused:?}");
    assert!(!detail.contains(&os_error), "{detail}");
}

/// A skip over an untrusted root (macOS reads it; the answer is built on every Unix) answers
/// `Unassessable` with the handle's error as its `source()`, and not in the `detail`.
#[skuld::test]
fn a_skip_over_an_untrusted_root_has_the_handles_error_as_its_source() {
    use super::skipped_group;
    use crate::containment::root::NumberLook;
    use crate::containment::{SkipCall, Tree};
    use crate::error::Error;
    use crate::signal::RootState;
    use crate::signal::Sig;

    let os_error = std::io::Error::from_raw_os_error(libc::EPERM).to_string();
    let call = SkipCall {
        op: "skip_untrusted_probe_4c2",
        pid: 999_999,
        signal: Sig::Graceful(crate::signal::Signal::Term),
        earlier: None,
    };
    let skip = skipped_group(
        999_999,
        "kill",
        NumberLook::Untrusted(RootState::Unknown(std::sync::Arc::new(
            std::io::Error::from_raw_os_error(libc::EPERM),
        ))),
    );

    let answer = call
        .answer(&Tree::none(), skip, None)
        .answer
        .expect_err("a skipped call is no success");

    let Error::Unassessable { detail, .. } = &answer else {
        panic!("got {answer:?}");
    };
    assert_eq!(crate::error::first_os_code(&answer), Some(libc::EPERM), "{answer:?}");
    assert!(!detail.contains(&os_error), "{detail}");
}

/// The handle's error of an untrusted root (macOS: `RootState::Unknown`, whose error is an
/// `io_context` over the OS error) keeps its OS code in the copy the skip holds, so the answer's
/// `source()` reaches it.
///
/// Mutant: the copy reads the code of the outer error only.
#[skuld::test]
fn a_skip_over_an_untrusted_root_with_a_context_error_keeps_its_os_code() {
    use super::skipped_group;
    use crate::containment::root::NumberLook;
    use crate::containment::{SkipCall, Tree};
    use crate::signal::RootState;
    use crate::signal::Sig;

    let call = SkipCall {
        op: "skip_untrusted_context_probe",
        pid: 999_999,
        signal: Sig::Graceful(crate::signal::Signal::Term),
        earlier: None,
    };
    let handle_error = crate::error::io_context(
        "the root could not be peeked",
        std::io::Error::from_raw_os_error(libc::EPERM),
    );
    let skip = skipped_group(
        999_999,
        "kill",
        NumberLook::Untrusted(RootState::Unknown(std::sync::Arc::new(handle_error))),
    );

    let answer = call
        .answer(&Tree::none(), skip, None)
        .answer
        .expect_err("a skipped call is no success");

    assert_eq!(crate::error::first_os_code(&answer), Some(libc::EPERM), "{answer:?}");
}

// What a group that was not cleared answers =====

/// A member's cause: the OS error `code`.
fn member_cause(code: i32) -> crate::error::Error {
    crate::error::Error::Io(std::io::Error::from_raw_os_error(code))
}

/// The members of the `Several` an error holds as its `source`, as `(pid named, OS code)`.
fn members_below(error: &crate::error::Error) -> Vec<(String, Option<i32>)> {
    use std::error::Error as _;
    let source = error.source().and_then(|s| s.downcast_ref::<crate::error::Error>());
    let Some(crate::error::Error::Several(list)) = source else {
        panic!("expected a Several below the variant: {error:?}");
    };
    list.iter()
        .map(|member| (member.to_string(), crate::error::first_os_code(&**member)))
        .collect()
}

/// A group with refusers and unassessable members answers `Containment`, with each member's own
/// cause as a member of the `Several` below it, the refusers first, and none of their words in the
/// detail.
///
/// Mutants: the members are reduced to a list of pids; the unassessable members are dropped when a
/// refuser settles the verdict; a cause is put in the detail.
#[skuld::test]
fn a_group_with_refusers_answers_containment_with_every_members_cause() {
    use super::group::GroupState;
    use crate::error::Error;

    let answer = super::answer_of(
        GroupState::Refused {
            refused: vec![(7, member_cause(libc::EPERM)), (8, member_cause(libc::EACCES))],
            unassessable: vec![(9, member_cause(libc::EIO))],
        },
        42,
        nix::sys::signal::Signal::SIGKILL,
    )
    .expect_err("the group was not cleared");

    let Error::Containment { detail, .. } = &answer else {
        panic!("{answer:?}");
    };
    assert!(detail.contains("SIGKILL") && detail.contains("42"), "{detail}");
    assert!(
        !detail.contains(&std::io::Error::from_raw_os_error(libc::EPERM).to_string()),
        "{detail}"
    );
    let members = members_below(&answer);
    let codes: Vec<_> = members.iter().map(|(_, code)| *code).collect();
    assert_eq!(codes, [Some(libc::EPERM), Some(libc::EACCES), Some(libc::EIO)]);
    for ((said, _), pid) in members.iter().zip([7, 8, 9]) {
        assert!(said.contains(&format!("pid {pid}")), "{said}");
    }
}

/// One member that refused is the whole cause: no list of one.
#[skuld::test]
fn a_group_with_one_refuser_has_that_member_as_its_cause() {
    use super::group::GroupState;
    use crate::error::Error;
    use std::error::Error as _;

    let answer = super::answer_of(
        GroupState::Refused {
            refused: vec![(7, member_cause(libc::EPERM))],
            unassessable: vec![],
        },
        42,
        nix::sys::signal::Signal::SIGTERM,
    )
    .expect_err("the group was not cleared");

    let source = answer.source().and_then(|s| s.downcast_ref::<Error>());
    assert!(
        matches!(source, Some(Error::Containment { detail, .. }) if detail.contains("pid 7")),
        "{answer:?}"
    );
    assert_eq!(crate::error::first_os_code(&answer), Some(libc::EPERM));
}

/// A group whose members could only not be assessed answers `Unassessable`, with each member's cause.
#[skuld::test]
fn a_group_with_only_unassessable_members_answers_unassessable_with_every_cause() {
    use super::group::GroupState;
    use crate::error::Error;

    let answer = super::answer_of(
        GroupState::Unassessed {
            members: vec![(5, member_cause(libc::EIO)), (6, member_cause(libc::EACCES))],
        },
        42,
        nix::sys::signal::Signal::SIGKILL,
    )
    .expect_err("the group was not cleared");

    let Error::Unassessable { detail, .. } = &answer else {
        panic!("{answer:?}");
    };
    assert!(
        !detail.contains(&std::io::Error::from_raw_os_error(libc::EIO).to_string()),
        "{detail}"
    );
    let codes: Vec<_> = members_below(&answer).iter().map(|(_, code)| *code).collect();
    assert_eq!(codes, [Some(libc::EIO), Some(libc::EACCES)]);
}

/// A group whose listing is refused answers `Unsupported` naming the listing and the requirement, as a
/// wait, a spawn or the process snapshot does. Mutant: "a refused listing is an unassessable group".
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_group_whose_listing_is_refused_answers_unsupported() {
    use rustix::io::Errno;

    use super::group::{GroupState, ListingFailure};
    use crate::error::Error;

    let refusal = crate::identity::FailedCall::openat2(Errno::PERM)
        .refusal()
        .expect("an openat2 answering EPERM is a refusal");
    let answer = super::answer_of(
        GroupState::Unlistable {
            detail: "process group 42 could not be listed".to_owned(),
            source: ListingFailure::Unsupported(refusal.unsupported("listing process group 42")),
        },
        42,
        nix::sys::signal::Signal::SIGKILL,
    )
    .expect_err("the group was not cleared");
    let Error::Unsupported { op, detail, .. } = &answer else {
        panic!("{answer:?}");
    };
    assert_eq!(op, "listing process group 42");
    assert!(detail.contains("requires openat2"), "{detail}");
    assert_eq!(crate::error::first_os_code(&answer), Some(libc::EPERM));
}

/// A listing that failed is no refusal, whatever its error's kind: std maps `ENOSYS` to `Unsupported`,
/// and a listing that answers `ENOSYS` of its own is an unassessable group. Mutant: "an `io::Error` of
/// kind `Unsupported` is a refusal".
#[skuld::test]
fn a_group_whose_listing_failed_with_an_enosys_of_its_own_answers_unassessable() {
    use super::group::{GroupState, ListingFailure};
    use crate::error::Error;

    let source = std::io::Error::from_raw_os_error(libc::ENOSYS);
    assert_eq!(
        source.kind(),
        std::io::ErrorKind::Unsupported,
        "std maps ENOSYS to Unsupported"
    );
    let answer = super::answer_of(
        GroupState::Unlistable {
            detail: "process group 42 could not be listed".to_owned(),
            source: ListingFailure::Failed(source),
        },
        42,
        nix::sys::signal::Signal::SIGKILL,
    )
    .expect_err("the group was not cleared");
    assert!(matches!(answer, Error::Unassessable { .. }), "{answer:?}");
    assert_eq!(crate::error::first_os_code(&answer), Some(libc::ENOSYS));
}

/// End to end, from a `/proc` listing: `members`, `state` and `answer_of` answer `Unassessable` carrying
/// the errno. Mutant: "a failure after the open that std maps to `Unsupported` is a refusal".
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_listing_that_fails_after_its_open_with_enosys_answers_unassessable() {
    use crate::error::Error;
    use crate::identity::{FakeKernel, HostKernel};

    let kernel = FakeKernel::over(&HostKernel)
        .same_view()
        .on_pids_failing_after_open(libc::ENOSYS);
    let signal = nix::sys::signal::Signal::SIGKILL;
    let answer = super::answer_of(
        super::group::state(&kernel, 42, signal, None, &mut Vec::new()),
        42,
        signal,
    )
    .expect_err("a group that cannot be listed is not cleared");
    assert!(matches!(answer, Error::Unassessable { .. }), "{answer:?}");
    assert_eq!(crate::error::first_os_code(&answer), Some(libc::ENOSYS), "{answer:?}");
}