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
use std::cell::Cell;
use std::io;

#[cfg(unix)]
use super::relay_gate;
use super::{kill_gate, terminate_gate, Front, Gate, TrackedKind};
use crate::elevation::elevation_tests::{report, OS_CODE};
use crate::elevation::{Backend, ElevatedVia, ElevationReport};
use crate::error::{ElevationErrorKind, Error};
#[cfg(unix)]
use crate::Signal;

const PID: u32 = 4242;

/// The fronts a report names, and no other: pkexec execs the program, and UAC and an already-elevated
/// parent track it. Mutant: a backend's arm of `ElevationReport::tracked` answers another's.
#[skuld::test]
fn a_report_names_the_front_its_via_leaves_tracked() {
    for (via, name) in FRONTS {
        assert_eq!(
            report(via.clone()).tracked().front().map(Front::name),
            Some(name),
            "{via:?}"
        );
    }
    for via in NOT_FRONTS.iter().flatten() {
        assert_eq!(report(via.clone()).tracked().front(), None, "{via:?}");
    }
}

/// Sudo, doas, pkexec and UAC may run the tracked process as another user; osascript's front runs
/// as this user, and an already-elevated child is the caller's own. Mutant: an arm of
/// `ElevationReport::tracked` flips.
#[skuld::test]
fn a_report_says_whether_the_tracked_process_may_run_as_another_user() {
    let runs_as_another_user = |via: ElevatedVia| report(via).tracked().runs_as_another_user();
    for backend in [Backend::Sudo, Backend::Doas, Backend::Pkexec] {
        assert!(runs_as_another_user(ElevatedVia::Wrapped(backend)), "{backend:?}");
    }
    assert!(runs_as_another_user(ElevatedVia::WindowsUac));
    assert!(!runs_as_another_user(ElevatedVia::MacosOsascript));
    assert!(!runs_as_another_user(ElevatedVia::AlreadyElevated));
}

#[skuld::test]
fn an_unkillable_error_has_the_refusal_as_its_cause() {
    let refusal = io::Error::from_raw_os_error(OS_CODE);
    assert_eq!(
        refusal.kind(),
        io::ErrorKind::PermissionDenied,
        "the test's code is a refusal"
    );
    let text = refusal.to_string();
    let e = TrackedKind::AnotherUser.refusal(refusal);
    assert_eq!(crate::error::first_os_code(&e), Some(OS_CODE), "{e:?}");
    match e {
        Error::Elevation {
            kind: ElevationErrorKind::Unkillable,
            detail,
            ..
        } => assert!(!detail.contains(&text), "the cause is in the detail: {detail}"),
        other => panic!("expected Unkillable, got {other:?}"),
    }
}

#[skuld::test]
fn kill_error_on_an_elevated_wrapper_is_unkillable() {
    let eperm = io::Error::from(io::ErrorKind::PermissionDenied);
    let e = TrackedKind::Front(Front::Sudo).refusal(eperm);
    assert!(
        matches!(
            e,
            Error::Elevation {
                kind: ElevationErrorKind::Unkillable,
                ..
            }
        ),
        "{e:?}"
    );
}

#[skuld::test]
fn kill_error_on_a_plain_child_stays_io() {
    let eperm = io::Error::from(io::ErrorKind::PermissionDenied);
    assert!(matches!(TrackedKind::Own.refusal(eperm), Error::Io(_)));
}

#[skuld::test]
fn non_permission_kill_error_stays_io_even_when_elevated() {
    let other = io::Error::from(io::ErrorKind::NotFound);
    assert!(matches!(TrackedKind::AnotherUser.refusal(other), Error::Io(_)));
}

const FRONTS: [(ElevatedVia, &str); 3] = [
    (ElevatedVia::Wrapped(Backend::Sudo), "sudo"),
    (ElevatedVia::Wrapped(Backend::Doas), "doas"),
    (ElevatedVia::MacosOsascript, "osascript"),
];

const NOT_FRONTS: [Option<ElevatedVia>; 4] = [
    None,
    Some(ElevatedVia::Wrapped(Backend::Pkexec)),
    Some(ElevatedVia::WindowsUac),
    Some(ElevatedVia::AlreadyElevated),
];

/// `answers` in turn, one per call; the last is repeated once they run out.
fn in_turn(answers: Vec<io::Result<bool>>) -> impl Fn() -> io::Result<bool> {
    let answers: Vec<_> = answers
        .into_iter()
        .map(|a| a.map_err(|e| (e.kind(), e.to_string())))
        .collect();
    let next = Cell::new(0);
    move || {
        let i = next.get().min(answers.len() - 1);
        next.set(next.get() + 1);
        answers[i].clone().map_err(|(kind, text)| io::Error::new(kind, text))
    }
}

/// What a gate asked, in order: `r` for `running`, `c` for `in_cgroup`.
fn kill_gate_asking(
    report: Option<ElevationReport>,
    running: io::Result<bool>,
    in_cgroup: Result<bool, Error>,
) -> (Gate, String) {
    kill_gate_asking_in_turn(report, vec![running], in_cgroup)
}

/// [`kill_gate_asking`], with `running` answering in turn.
fn kill_gate_asking_in_turn(
    report: Option<ElevationReport>,
    running: Vec<io::Result<bool>>,
    in_cgroup: Result<bool, Error>,
) -> (Gate, String) {
    let asked = Cell::new(String::new());
    let note = |c: char| {
        let mut s = asked.take();
        s.push(c);
        asked.set(s);
    };
    let running = in_turn(running);
    let g = kill_gate(
        report.and_then(|report| report.tracked().front()),
        PID,
        || {
            note('r');
            running()
        },
        || {
            note('c');
            in_cgroup
        },
    );
    (g, asked.take())
}

fn refusal_detail(g: Gate) -> String {
    match g {
        Gate::Closed(Error::Elevation {
            kind: ElevationErrorKind::Unkillable,
            detail,
            ..
        }) => detail,
        other => panic!("expected a closed gate with Unkillable, got {other:?}"),
    }
}

#[skuld::test]
fn a_live_front_outside_a_cgroup_is_closed_with_unkillable_naming_it() {
    for (via, name) in FRONTS {
        let (g, asked) = kill_gate_asking(Some(report(via.clone())), Ok(true), Ok(false));
        assert_eq!(asked, "rcr", "{via:?}");
        let detail = refusal_detail(g);
        assert!(detail.contains(&format!("pid {PID} is")), "{detail}");
        assert!(detail.contains(name), "{detail}");
        assert!(detail.contains("no kill was sent"), "{detail}");
        assert_eq!(
            detail.contains("direct exec"),
            name != "osascript",
            "only a wrapper may track the program itself: {detail}"
        );
    }
}

/// A front that has left its cgroup (pam_systemd moving sudo into a session scope) is outside it.
#[skuld::test]
fn a_live_front_that_left_its_cgroup_is_closed() {
    let (g, _) = kill_gate_asking(Some(report(ElevatedVia::Wrapped(Backend::Sudo))), Ok(true), Ok(false));
    refusal_detail(g);
}

/// An exited front has nothing left to orphan, and its cgroup is not asked about.
#[skuld::test]
fn an_exited_front_is_exited() {
    for (via, _) in FRONTS {
        let (g, asked) = kill_gate_asking(Some(report(via.clone())), Ok(false), Ok(true));
        assert_eq!(asked, "r", "{via:?}");
        assert!(matches!(g, Gate::Exited), "{via:?}: {g:?}");
    }
}

/// A cgroup kill reaches a member whatever its credentials, so a live front in one, or one whose
/// state cannot be read, is killed through it.
#[skuld::test]
fn a_live_front_in_its_cgroup_is_killed_through_the_cgroup_only() {
    for (via, _) in FRONTS {
        for running in [Ok(true), Err(io::Error::other("peek refused"))] {
            let (g, asked) = kill_gate_asking(Some(report(via.clone())), running, Ok(true));
            assert_eq!(asked, "rc", "{via:?}");
            assert!(
                matches!(&g, Gate::CgroupOnly(f) if Some(*f) == report(via.clone()).tracked().front()),
                "{via:?}: {g:?}"
            );
        }
    }
}

const CODE: i32 = 5;

fn os_error() -> io::Error {
    io::Error::from_raw_os_error(CODE)
}

fn refusal(g: Gate) -> Error {
    match g {
        Gate::Closed(error) => error,
        other => panic!("expected a closed gate, got {other:?}"),
    }
}

#[track_caller]
fn assert_refused_by_one_read(error: &Error, words: &str) {
    let Error::Elevation {
        kind: ElevationErrorKind::Unkillable,
        detail,
        ..
    } = error
    else {
        panic!("expected Unkillable, got {error:?}");
    };
    assert!(detail.contains(words), "{detail}");
    assert!(
        !detail.contains(&os_error().to_string()),
        "the cause is in the detail: {detail}"
    );
    assert_eq!(crate::error::first_os_code(error), Some(CODE), "{error:?}");
}

#[skuld::test]
fn a_front_whose_state_cannot_be_read_is_closed_and_says_why() {
    let g = kill_gate(Some(Front::Sudo), PID, || Err(os_error()), || Ok(false));
    assert_refused_by_one_read(&refusal(g), "whether it had exited could not be read");
    let g = kill_gate(Some(Front::Sudo), PID, || Ok(true), || Err(Error::Io(os_error())));
    assert_refused_by_one_read(&refusal(g), "whether its cgroup kill reaches it could not be read");
}

#[skuld::test]
fn a_front_whose_place_cannot_be_read_after_the_kill_has_the_read_as_its_cause() {
    let (r, _) = reached_asking(Ok(false), Err(Error::Io(os_error())));
    assert_refused_by_one_read(&r.expect_err("not shown reached"), "its cgroup could not be read");
}

/// A cgroup read refused as the platform lacks what it needs, over the errno it was refused with.
fn refused_read() -> Error {
    Error::Unsupported {
        op: "reading the cgroup of the front".to_owned(),
        platform: "linux",
        detail: "cosca requires openat2".to_owned(),
        source: Some(Error::io_cause(os_error())),
    }
}

/// The refusal a read left as a refusal's cause, still the `Unsupported` it was, with its errno.
#[track_caller]
fn assert_refused_by_a_refusal(error: &Error) {
    let Error::Elevation {
        kind: ElevationErrorKind::Unkillable,
        source: Some(source),
        ..
    } = error
    else {
        panic!("expected Unkillable with a source, got {error:?}");
    };
    assert!(matches!(&**source, Error::Unsupported { .. }), "{error:?}");
    assert_eq!(crate::error::first_os_code(error), Some(CODE), "{error:?}");
}

/// A refused read of the front's cgroup, in either gate, is the refusal's cause as it is: neither
/// rebuilt as an I/O failure nor flattened.
///
/// Mutant: `unread_reason` wraps each read in `Error::io_cause`.
#[skuld::test]
fn a_front_whose_cgroup_read_is_refused_has_the_refusal_as_its_cause() {
    let g = kill_gate(Some(Front::Sudo), PID, || Ok(true), || Err(refused_read()));
    assert_refused_by_a_refusal(&refusal(g));
    let (r, _) = reached_asking(Ok(false), Err(refused_read()));
    assert_refused_by_a_refusal(&r.expect_err("not shown reached"));
}

#[skuld::test]
fn a_front_whose_cgroup_kill_failed_has_the_failure_as_its_cause() {
    let e = super::cgroup_kill_failed(Front::Sudo, PID, Error::Io(os_error()));
    assert_refused_by_one_read(&e, "its cgroup kill failed");
}

/// A refusal of a front whose cgroup was not killed says so and carries no failure: the tree's
/// failure stands before it in a failed spawn's `cleanup`, so a `Report` of the spawn prints each
/// failure once, the tree's first.
///
/// Mutant: the refusal repeats the tree's failure in its words.
#[skuld::test]
fn a_front_whose_cgroup_was_not_killed_says_so_and_the_report_prints_each_failure_once() {
    use std::sync::Arc;

    use crate::error::{ChildFate, Failures};

    let refusal = super::cgroup_not_killed(Front::Sudo, PID);
    assert!(std::error::Error::source(&refusal).is_none(), "{refusal:?}");
    let refusal_words = refusal.to_string();
    assert!(
        refusal_words.contains(&format!("pid {PID} is what sudo left"))
            && refusal_words.ends_with(" (its cgroup was not killed)"),
        "{refusal_words}"
    );

    let tree = Error::Containment {
        detail: "the kernel refused the write of leaf/cgroup.kill".into(),
        source: Some(Arc::new(Error::Io(io::Error::from_raw_os_error(libc::EISDIR)))),
    };
    let started = Error::MayHaveStarted {
        fate: ChildFate::Running { id: None },
        wrapper_elevated: true,
        detail: None,
        source: Arc::new(Error::Io(io::Error::from_raw_os_error(libc::EPIPE))),
        unreaped: None,
        cleanup: Failures::collect([Arc::new(tree), Arc::new(refusal)]),
    };
    let epipe = io::Error::from_raw_os_error(libc::EPIPE).to_string();
    let eisdir = io::Error::from_raw_os_error(libc::EISDIR).to_string();
    assert_eq!(
        crate::error::said(&started),
        format!(
            "the program may have started, and its child may still be running; its teardown failed too (2 failures, \
             in `cleanup`): {}; cleanup: [(1) process containment failed: the kernel \
             refused the write of leaf/cgroup.kill: {}; (2) {refusal_words}]",
            crate::error::io_said(&epipe),
            crate::error::io_said(&eisdir)
        )
    );
}

/// The tracked process is the program itself, or there is no elevation: signalled like any child,
/// with nothing read.
#[skuld::test]
fn a_child_that_is_not_a_front_is_open_whatever_its_containment() {
    for via in &NOT_FRONTS {
        for in_cgroup in [false, true] {
            let reported = via.clone().map(report);
            let (g, asked) = kill_gate_asking(reported.clone(), Ok(true), Ok(in_cgroup));
            assert_eq!(asked, "", "{via:?}");
            assert!(matches!(g, Gate::Open), "{via:?}: {g:?}");
            let g = terminate_gate(reported.and_then(|reported| reported.tracked().front()), PID, || {
                panic!("not asked")
            });
            assert!(matches!(g, Gate::Open), "{via:?}: {g:?}");
        }
    }
}

/// sudo and doas relay `SIGTERM` to the program: never gated.
#[skuld::test]
fn a_sigterm_to_a_relaying_front_is_open() {
    for via in [ElevatedVia::Wrapped(Backend::Sudo), ElevatedVia::Wrapped(Backend::Doas)] {
        let g = terminate_gate(report(via.clone()).tracked().front(), PID, || panic!("not asked"));
        assert!(matches!(g, Gate::Open), "{via:?}: {g:?}");
    }
}

/// A `SIGTERM` would end osascript and orphan the program: refused while osascript runs, sent once
/// it has exited.
#[skuld::test]
fn a_sigterm_to_a_live_osascript_is_closed() {
    let osascript = report(ElevatedVia::MacosOsascript).tracked().front();
    let detail = refusal_detail(terminate_gate(osascript, PID, || Ok(true)));
    assert!(detail.contains(&format!("pid {PID} is osascript")), "{detail}");
    assert!(detail.contains("no signal was sent"), "{detail}");
    let error = refusal(terminate_gate(osascript, PID, || Err(os_error())));
    assert_refused_by_one_read(&error, "whether it had exited could not be read");
    assert!(matches!(terminate_gate(osascript, PID, || Ok(false)), Gate::Open));
}

/// A signal a front relays is open without reading the front.
///
/// Mutant: the gate asks the front's state for a relayed signal.
#[cfg(unix)]
#[skuld::test]
fn a_signal_a_front_relays_is_open_without_reading_it() {
    for front in [Front::Sudo, Front::Doas] {
        for &signal in front.relayed() {
            let g = relay_gate(Some(front), PID, signal, || panic!("not asked"));
            assert!(matches!(g, Gate::Open), "{front:?} {signal:?}: {g:?}");
        }
    }
}

/// A signal a live sudo or doas front does not relay would end the front and leave the program running
/// (or reach nothing): refused as `Unsupported`, naming the front, the signal and what the front relays.
///
/// Mutant: the table lets a signal the front does not relay through.
#[cfg(unix)]
#[skuld::test]
fn a_signal_a_live_front_does_not_relay_is_refused_as_unsupported() {
    for (front, signal) in [
        (Front::Sudo, Signal::Alrm),
        (Front::Sudo, Signal::Cont),
        (Front::Sudo, Signal::Chld),
        (Front::Doas, Signal::Hup),
        (Front::Doas, Signal::Usr1),
        (Front::Doas, Signal::Int),
    ] {
        let Gate::Closed(Error::Unsupported { detail, .. }) = relay_gate(Some(front), PID, signal, || Ok(true)) else {
            panic!("{front:?} {signal:?} was not refused as Unsupported");
        };
        assert!(detail.contains(&format!("pid {PID}")), "{detail}");
        assert!(detail.contains(front.name()), "{detail}");
        assert!(detail.contains(signal.name()), "{detail}");
        for relayed in front.relayed() {
            assert!(detail.contains(relayed.name()), "{detail}");
        }
        assert!(detail.contains("no signal was sent"), "{detail}");
    }
}

/// An exited front is signalled whatever the signal, as one is for a `SIGTERM`; one whose state cannot be
/// read is refused, with the read's error as the cause.
#[cfg(unix)]
#[skuld::test]
fn a_front_that_has_exited_is_open_to_any_signal_and_an_unread_one_is_refused() {
    let g = relay_gate(Some(Front::Sudo), PID, Signal::Alrm, || Ok(false));
    assert!(matches!(g, Gate::Open), "{g:?}");
    let error = refusal(relay_gate(Some(Front::Doas), PID, Signal::Hup, || Err(os_error())));
    assert!(matches!(error, Error::Unsupported { .. }), "{error:?}");
    assert_eq!(crate::error::first_os_code(&error), Some(CODE), "{error:?}");
    assert!(
        crate::error::said(&error).contains("whether it had exited could not be read"),
        "{error:?}"
    );
}

/// Not a front, or osascript (whose refusal is `terminate_gate`'s): the relay gate adds nothing.
#[cfg(unix)]
#[skuld::test]
fn the_relay_gate_leaves_osascript_and_every_non_front_to_the_other_gate() {
    let g = relay_gate(None, PID, Signal::Alrm, || panic!("not asked"));
    assert!(matches!(g, Gate::Open), "{g:?}");
    let g = relay_gate(Some(Front::Osascript), PID, Signal::Alrm, || panic!("not asked"));
    assert!(matches!(g, Gate::Open), "{g:?}");
}

/// What `cgroup_kill_reached` asked, in order (`e`xited, `u`nder the leaf), and its answer.
fn reached_asking(exited: io::Result<bool>, under_leaf: Result<bool, Error>) -> (Result<(), Error>, String) {
    reached_asking_in_turn(vec![exited], under_leaf)
}

/// [`reached_asking`], with `exited` answering in turn.
fn reached_asking_in_turn(
    exited: Vec<io::Result<bool>>,
    under_leaf: Result<bool, Error>,
) -> (Result<(), Error>, String) {
    let asked = Cell::new(String::new());
    let note = |c: char| {
        let mut s = asked.take();
        s.push(c);
        asked.set(s);
    };
    let exited = in_turn(exited);
    let r = super::cgroup_kill_reached(
        Front::Sudo,
        PID,
        || {
            note('e');
            exited()
        },
        || {
            note('u');
            under_leaf
        },
    );
    (r, asked.take())
}

/// A front exited, or in its leaf after the write, was there for the kill.
#[skuld::test]
fn a_front_in_its_leaf_after_the_kill_was_reached() {
    let (r, asked) = reached_asking(Ok(true), Ok(false));
    assert!(r.is_ok() && asked == "e", "{r:?} {asked}");
    let (r, asked) = reached_asking(Ok(false), Ok(true));
    assert!(r.is_ok() && asked == "eu", "{r:?} {asked}");
}

/// A front neither exited nor in its leaf left it before the kill: refused, and named.
#[skuld::test]
fn a_front_outside_its_leaf_after_the_kill_was_not_reached() {
    let (r, _) = reached_asking(Ok(false), Ok(false));
    let detail = refusal_detail(Gate::Closed(r.expect_err("not reached")));
    assert!(detail.contains("left the cgroup before its kill"), "{detail}");
}

/// The two failed reads of a refusal, as its cause: both are the `source` (a `Several`, the first
/// read first), and neither is in the detail, which says only what was done.
#[track_caller]
fn assert_refused_by_two_reads(error: &Error, words: &[&str], reads: [&str; 2]) {
    let Error::Elevation {
        kind: ElevationErrorKind::Unkillable,
        detail,
        source: Some(source),
    } = error
    else {
        panic!("expected Unkillable with a source, got {error:?}");
    };
    for word in words {
        assert!(detail.contains(word), "{detail}");
    }
    for read in reads {
        assert!(!detail.contains(read), "a cause is in the detail: {detail}");
    }
    let Error::Several(failures) = &**source else {
        panic!("expected the two reads as a Several, got {source:?}");
    };
    let texts: Vec<_> = failures.iter().map(|failure| crate::error::said(&**failure)).collect();
    assert_eq!(texts, reads.map(crate::error::io_said), "{error:?}");
}

/// A front whose place cannot be read is not shown reached: refused, with every read that failed
/// as its cause.
///
/// Mutants: only the first read is the cause; the reads are in the detail's text.
#[skuld::test]
fn a_front_whose_place_after_the_kill_cannot_be_read_is_refused() {
    let (r, _) = reached_asking(
        Err(io::Error::other("peek refused")),
        Err(Error::Io(io::Error::other("hidepid"))),
    );
    let error = r.expect_err("not shown reached");
    assert_refused_by_two_reads(
        &error,
        &[
            "nothing shows the cgroup kill reached it",
            "whether it had exited could not be read",
            "its cgroup could not be read",
        ],
        ["peek refused", "hidepid"],
    );
}

/// The gate before a kill, when both of its reads fail: closed, with both as the cause.
///
/// Mutant: the second failed read replaces the first, or is only in the detail.
#[skuld::test]
fn a_front_whose_two_gate_reads_fail_is_closed_with_both_as_its_cause() {
    let g = kill_gate(
        Some(Front::Sudo),
        PID,
        || Err(io::Error::other("peek refused")),
        || Err(Error::Io(io::Error::other("hidepid"))),
    );
    assert_refused_by_two_reads(
        &refusal(g),
        &[
            "whether it had exited could not be read",
            "whether its cgroup kill reaches it could not be read",
        ],
        ["peek refused", "hidepid"],
    );
}

/// A front that exits, and is reaped by another thread's `wait`, between the gate's two reads has
/// no cgroup left to read: the gate reads it again, finds it exited, and kills nothing.
#[skuld::test]
fn a_front_reaped_between_the_gates_reads_has_exited() {
    let sudo = ElevatedVia::Wrapped(Backend::Sudo);
    let reaped = || Err(Error::Io(io::Error::other("ESRCH: reaped")));
    let (g, asked) = kill_gate_asking_in_turn(Some(report(sudo.clone())), vec![Ok(true), Ok(false)], reaped());
    assert!(matches!(g, Gate::Exited), "{g:?}");
    assert_eq!(asked, "rcr");
    let (g, _) = kill_gate_asking_in_turn(Some(report(sudo)), vec![Ok(true), Ok(true)], reaped());
    refusal_detail(g);
}

/// A front its cgroup kill ended, reaped by another thread's `wait` before its cgroup is read,
/// was reached: the check reads its exit again.
#[skuld::test]
fn a_front_reaped_before_its_cgroup_is_read_was_reached() {
    let reaped = || Err(Error::Io(io::Error::other("ESRCH: reaped")));
    let (r, asked) = reached_asking_in_turn(vec![Ok(false), Ok(true)], reaped());
    assert!(r.is_ok(), "{r:?}");
    assert_eq!(asked, "eue");
    let (r, _) = reached_asking_in_turn(vec![Ok(false), Ok(false)], reaped());
    r.expect_err("still running, unplaced");
}