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
//! Async twins of `child/front_kill_tests.rs`: no forced kill of a `cosca::tokio::Child` may
//! signal a live elevation front, and no `SIGTERM` a live osascript. The front is an ordinary `cat`,
//! reported as launched by `sudo` or `osascript`, that exits 0 once its stdin closes and dies of a
//! signal if it was sent one first. The cgroup lane's cases are in `front_cgroup_tests.rs`.

use std::time::Duration;

use crate::test_child::Console;

use crate::child::front_kill_tests::{assert_refused_by, assert_unkillable_front, report};
use crate::containment::unix::fault::record_kill_group;
use crate::elevation::{Backend, ElevatedVia};
use crate::error::ChildFate;
use crate::tokio::child::{drop_fault, Child};
use crate::tokio::{ChildStdin, Command};
use crate::{ContainMode, Containment, Stdio};

pub(super) fn spawn_as(mut cmd: Command, via: ElevatedVia) -> (Child, ChildStdin) {
    cmd.stdin(Stdio::pipe()).expect("stdin pipe");
    cmd.set_elevation_front(crate::elevation::front::front(Some(&via)));
    let mut child = cmd.spawn().expect("spawn");
    child.set_elevation(report(via));
    let stdin = child.stdin().expect("stdin pipe");
    (child, stdin)
}

pub(super) fn cat() -> Command {
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    cmd.args(["cat"]);
    cmd
}

/// Closes the `cat`'s stdin and reaps it: it must exit 0, so nothing signalled it.
pub(super) async fn assert_ends_unsignalled(child: &mut Child, stdin: ChildStdin) {
    drop(stdin);
    let status = child.wait().await.expect("wait");
    assert!(status.success(), "the front was signalled: {status:?}");
}

#[skuld::test]
async fn kill_of_a_live_front_is_unkillable_and_sends_nothing() {
    let (mut child, stdin) = spawn_as(cat(), ElevatedVia::Wrapped(Backend::Sudo));
    let pid = child.pid();
    assert_unkillable_front(child.kill(), pid);
    assert_ends_unsignalled(&mut child, stdin).await;
}

/// Async twin of the sync `kill_of_an_exited_front_is_ok`.
#[skuld::test]
async fn kill_of_an_exited_front_is_ok() {
    let (mut child, stdin) = spawn_as(cat(), ElevatedVia::Wrapped(Backend::Sudo));
    drop(stdin);
    crate::test_child::wait_until_zombie(child.pid());
    let refused = crate::signal::seams::refuse_kills();
    child.kill().expect("an exited front is killed like any child");
    drop(refused);
    assert!(child.wait().await.expect("wait").success());
}

/// Async twin of the sync `kill_of_an_exited_front_returns_any_other_failure`.
#[skuld::test]
async fn kill_of_an_exited_front_returns_any_other_failure() {
    let (mut child, stdin) = spawn_as(cat(), ElevatedVia::Wrapped(Backend::Sudo));
    drop(stdin);
    crate::test_child::wait_until_zombie(child.pid());
    let failing = crate::signal::seams::fail_kills_with(libc::EINVAL);
    match child.kill() {
        Err(crate::error::Error::Io(io)) => assert_eq!(io.kind(), std::io::ErrorKind::InvalidInput, "{io}"),
        other => panic!("expected the kill's own failure, got {other:?}"),
    }
    drop(failing);
    assert!(child.wait().await.expect("wait").success());
}

/// Async twin of the sync `kill_tree_of_an_exited_front_is_ok`.
#[skuld::test]
async fn kill_tree_of_an_exited_front_is_ok() {
    let mut cmd = cat();
    cmd.contain_with(ContainMode::Session);
    let (mut child, stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
    drop(stdin);
    crate::test_child::wait_until_zombie(child.pid());
    let refused = crate::signal::seams::refuse_kills();
    child
        .kill_tree()
        .expect("an exited front's tree is killed like any child's");
    drop(refused);
    assert!(child.wait().await.expect("wait").success());
}

/// Async twin of the sync `drop_of_an_exited_front_reaps_it_and_warns_of_nothing`.
#[skuld::test]
async fn drop_of_an_exited_front_reaps_it_and_warns_of_nothing() {
    use crate::child::front_kill_tests::reap;
    crate::log_capture::install();
    let (child, stdin) = spawn_as(cat(), ElevatedVia::Wrapped(Backend::Sudo));
    let pid = child.pid();
    drop(stdin);
    crate::test_child::wait_until_zombie(pid);
    let mark = crate::log_capture::mark();
    drop(child);
    let warns = crate::log_capture::records_since_on_current_thread(mark, "Child::drop");
    assert!(warns.is_empty(), "{warns:?}");
    assert_eq!(reap(pid), None, "the drop reaps an exited front");
}

/// Async twin of the sync `kill_reads_the_front_its_spawn_found_not_its_report`.
#[skuld::test]
async fn kill_reads_the_front_its_spawn_found_not_its_report() {
    use std::os::unix::process::ExitStatusExt as _;
    let mut cmd = cat();
    cmd.stdin(Stdio::pipe()).expect("stdin pipe");
    let mut child = cmd.spawn().expect("spawn");
    child.set_elevation(report(ElevatedVia::Wrapped(Backend::Sudo)));
    let stdin = child.stdin().expect("stdin pipe");
    child.kill().expect("a child with no front is killed as any child");
    // Closed first: a child nothing killed then exits 0, and the assertion fails.
    drop(stdin);
    assert_eq!(child.wait().await.expect("wait").signal(), Some(libc::SIGKILL));
}

/// Async twin of the sync `kill_of_a_live_doas_front_is_unkillable_and_sends_nothing`.
#[skuld::test]
async fn kill_of_a_live_doas_front_is_unkillable_and_sends_nothing() {
    let (mut child, stdin) = spawn_as(cat(), ElevatedVia::Wrapped(Backend::Doas));
    let pid = child.pid();
    assert_refused_by(child.kill(), &format!("pid {pid} is what doas left"));
    assert_ends_unsignalled(&mut child, stdin).await;
}

/// Async twin of the sync `a_failed_password_write_leaves_a_live_front_running_and_names_it`.
///
/// Mutant: the refused kill's fate is `Unknown`, `Gone`, or carries no identity.
#[skuld::test]
async fn a_failed_password_write_leaves_a_live_front_running_and_names_it() {
    let (child, stdin) = spawn_as(cat(), ElevatedVia::Wrapped(Backend::Sudo));
    let id = child.id().expect(crate::test_child::IDENTITY_READ);
    let (_err, fate) = crate::tokio::spawn::finish_elevated(
        child,
        Err(crate::error::Error::Elevation {
            kind: crate::error::ElevationErrorKind::AuthFailed,
            detail: "forced password-write failure".into(),
        }),
    )
    .expect_err("a failed write fails the spawn")
    .expect_may_have_started_with();
    assert_eq!(fate, crate::error::ChildFate::Running { id: Some(id) });
    drop(stdin);
    crate::child::front_kill_tests::assert_reaped_unsignalled(id.pid());
}

/// The tokio twins of the sync `uncontained_cats`.
fn uncontained_cats() -> [Command; 2] {
    let mut walked = cat();
    walked.contain_with(ContainMode::TreeWalk);
    [cat(), walked]
}

/// Async twin of the sync `kill_of_a_live_front_without_a_group_sends_nothing`.
#[skuld::test]
async fn kill_of_a_live_front_without_a_group_sends_nothing() {
    for cmd in uncontained_cats() {
        let (mut child, stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
        let pid = child.pid();
        assert_unkillable_front(child.kill(), pid);
        assert_ends_unsignalled(&mut child, stdin).await;
    }
}

/// Async twin of the sync `drop_of_a_live_front_without_a_group_sends_nothing`.
#[skuld::test]
async fn drop_of_a_live_front_without_a_group_sends_nothing() {
    crate::tokio::test_runtime::assert_current_thread();
    for cmd in uncontained_cats() {
        let roots = drop_fault::record();
        let (child, _stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
        drop(child);
        assert_eq!(roots.kills(), 0, "the drop must not kill a live front");
    }
}

/// Async twin of the sync `a_failed_password_write_sends_a_live_front_without_a_group_nothing`.
#[skuld::test]
async fn a_failed_password_write_sends_a_live_front_without_a_group_nothing() {
    crate::tokio::test_runtime::assert_current_thread();
    for cmd in uncontained_cats() {
        let roots = drop_fault::record();
        let (child, _stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
        let id = child.id().expect(crate::test_child::IDENTITY_READ);
        let pid = id.pid();
        let (detail, fate) = failed_password_write_detail(child);
        assert_eq!(fate, ChildFate::Running { id: Some(id) }, "a live front is left");
        assert!(detail.contains("could not be terminated"), "{detail}");
        assert!(detail.contains(&format!("pid {pid} is what sudo left")), "{detail}");
        assert_eq!(roots.kills(), 0, "no kill of a live front, by its drop either");
    }
}

/// The `detail` of the error the tokio `finish_elevated` returns for `child` after a failed
/// password write, and its fate.
fn failed_password_write_detail(child: Child) -> (String, ChildFate) {
    let (err, fate) = crate::tokio::spawn::finish_elevated(
        child,
        Err(crate::error::Error::Elevation {
            kind: crate::error::ElevationErrorKind::AuthFailed,
            detail: "forced password-write failure".into(),
        }),
    )
    .expect_err("a failed write fails the spawn")
    .expect_may_have_started_with();
    let crate::error::Error::Elevation { detail, .. } = err else {
        panic!("expected an Elevation error, got {err:?}");
    };
    (detail, fate)
}

/// Async twin of the sync `graceful_shutdown_of_a_front_that_exits_within_the_grace_returns_its_status`.
#[skuld::test]
async fn graceful_shutdown_of_a_front_that_exits_within_the_grace_returns_its_status() {
    use std::os::unix::process::ExitStatusExt as _;
    let (mut child, stdin) = spawn_as(cat(), ElevatedVia::Wrapped(Backend::Sudo));
    // Released once the `SIGTERM` has been sent, as in the sync twin.
    let _release = crate::graceful_hooks::release_at(crate::graceful_hooks::HookPoint::AfterTerminate, stdin);
    let status = child
        .graceful_shutdown(Duration::from_secs(3600))
        .await
        .expect("the relayed SIGTERM ends the front");
    assert_eq!(status.signal(), Some(libc::SIGTERM));
}

/// Async twin of the sync `a_failed_password_write_to_a_front_someone_else_reaped_says_it_had_exited`.
#[skuld::test]
async fn a_failed_password_write_to_a_front_someone_else_reaped_says_it_had_exited() {
    use crate::child::front_kill_tests::reap;
    let mut cmd = cat();
    cmd.kill_on_drop(false);
    let (child, stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
    let pid = child.pid();
    drop(stdin);
    crate::test_child::wait_until_zombie(pid);
    assert!(reap(pid).is_some(), "the test reaps the front itself");
    let (detail, fate) = failed_password_write_detail(child);
    assert_eq!(fate, ChildFate::Gone);
    assert!(
        detail.contains("the elevated child had already exited, and was reaped by someone else"),
        "{detail}"
    );
}

#[skuld::test]
async fn kill_tree_of_a_live_front_outside_a_cgroup_is_unkillable_and_sends_nothing() {
    let mut cmd = cat();
    cmd.contain_with(ContainMode::Session);
    let (mut child, stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
    assert_ne!(child.containment(), Containment::CgroupV2);
    let pid = child.pid();
    assert_unkillable_front(child.kill_tree(), pid);
    assert_ends_unsignalled(&mut child, stdin).await;
}

#[skuld::test]
async fn graceful_shutdown_of_a_front_that_outlives_the_grace_is_unkillable() {
    use tokio::io::AsyncReadExt as _;
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    // An ignored disposition survives `exec`; `ready` says it is set.
    cmd.args(["sh", "-c", "trap '' TERM; echo ready; exec cat"]);
    cmd.stdout(Stdio::pipe()).expect("stdout pipe");
    let (mut child, stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
    let mut ready = [0u8; 6];
    child
        .stdout()
        .expect("stdout pipe")
        .read_exact(&mut ready)
        .await
        .expect("read `ready`");
    assert_eq!(&ready, b"ready\n");
    let pid = child.pid();
    assert_unkillable_front(child.graceful_shutdown(Duration::ZERO).await, pid);
    assert_ends_unsignalled(&mut child, stdin).await;
}

/// The drop signals neither the front nor its group, and says so. The `killpg` recorder stands in
/// for the group kill.
#[skuld::test]
async fn drop_of_a_live_front_signals_nothing_and_warns() {
    crate::tokio::test_runtime::assert_current_thread();
    crate::log_capture::install();
    let groups = record_kill_group();
    let roots = drop_fault::record();
    let mut cmd = cat();
    cmd.contain_with(ContainMode::Session);
    let (child, _stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
    let pid = child.pid();

    let mark = crate::log_capture::mark();
    drop(child);

    assert_eq!(roots.kills(), 0, "the drop must not kill a live front");
    assert_eq!(
        groups.killed(),
        Vec::<i32>::new(),
        "the drop must not kill a live front's group"
    );
    let warns = crate::log_capture::records_since_on_current_thread(mark, "Child::drop");
    assert_eq!(warns.len(), 1, "{warns:?}");
    assert_eq!(warns[0].0, log::Level::Warn);
    assert!(
        warns[0].1.contains(&format!("pid {pid} is what sudo left")),
        "{warns:?}"
    );
}

/// Async twin of the sync `a_failed_password_write_signals_neither_a_live_front_nor_its_group`.
#[skuld::test]
async fn a_failed_password_write_signals_neither_a_live_front_nor_its_group() {
    crate::tokio::test_runtime::assert_current_thread();
    let groups = record_kill_group();
    let roots = drop_fault::record();
    let mut cmd = cat();
    cmd.contain_with(ContainMode::Session);
    let (child, _stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
    let id = child.id().expect(crate::test_child::IDENTITY_READ);
    let pid = id.pid();
    let (err, fate) = crate::tokio::spawn::finish_elevated(
        child,
        Err(crate::error::Error::Elevation {
            kind: crate::error::ElevationErrorKind::AuthFailed,
            detail: "forced password-write failure".into(),
        }),
    )
    .expect_err("a failed write fails the spawn")
    .expect_may_have_started_with();
    assert_eq!(fate, ChildFate::Running { id: Some(id) }, "a live front is left");
    assert_eq!(
        groups.killed(),
        Vec::<i32>::new(),
        "no group kill of a live front: {err}"
    );
    assert_eq!(roots.kills(), 0, "no kill of a live front, by its drop either: {err}");
    let crate::error::Error::Elevation { detail, .. } = &err else {
        panic!("expected an Elevation error, got {err:?}");
    };
    assert!(detail.contains("could not be terminated"), "{detail}");
    assert!(detail.contains(&format!("pid {pid} is what sudo left")), "{detail}");
}

/// Async twin of the sync `a_failed_password_write_to_a_front_that_had_exited_says_so`.
#[skuld::test]
async fn a_failed_password_write_to_a_front_that_had_exited_says_so() {
    let mut cmd = cat();
    // The drop, whose own teardown would ask the gate again, is opted out of.
    cmd.kill_on_drop(false);
    let (child, stdin) = spawn_as(cmd, ElevatedVia::Wrapped(Backend::Sudo));
    drop(stdin);
    crate::test_child::wait_until_zombie(child.pid());
    let _refused = crate::signal::seams::refuse_kills();
    let gates = crate::elevation::front::seams::count_kill_gates();
    let (err, fate) = crate::tokio::spawn::finish_elevated(
        child,
        Err(crate::error::Error::Elevation {
            kind: crate::error::ElevationErrorKind::AuthFailed,
            detail: "forced password-write failure".into(),
        }),
    )
    .expect_err("a failed write fails the spawn")
    .expect_may_have_started_with();
    assert_eq!(fate, ChildFate::Reaped, "the non-blocking look reaps the exited front");
    assert_eq!(gates.count(), 1, "{err}");
    let crate::error::Error::Elevation { detail, .. } = &err else {
        panic!("expected an Elevation error, got {err:?}");
    };
    assert!(detail.contains("the elevated child had already exited"), "{detail}");
    assert!(!detail.contains("terminated"), "{detail}");
}

#[skuld::test]
async fn terminate_of_a_live_osascript_is_unkillable_and_sends_nothing() {
    let (mut child, stdin) = spawn_as(cat(), ElevatedVia::MacosOsascript);
    let pid = child.pid();
    assert_refused_by(child.terminate(), &format!("pid {pid} is osascript"));
    assert_ends_unsignalled(&mut child, stdin).await;
}

#[skuld::test]
async fn graceful_shutdown_of_a_live_osascript_is_unkillable_and_sends_nothing() {
    let (mut child, stdin) = spawn_as(cat(), ElevatedVia::MacosOsascript);
    let pid = child.pid();
    assert_refused_by(
        child.graceful_shutdown(Duration::ZERO).await,
        &format!("pid {pid} is osascript"),
    );
    assert_refused_by(child.kill(), &format!("pid {pid} is osascript"));
    assert_ends_unsignalled(&mut child, stdin).await;
}

#[skuld::test]
async fn terminate_of_a_live_sudo_front_is_sent() {
    use std::os::unix::process::ExitStatusExt as _;
    let (mut child, stdin) = spawn_as(cat(), ElevatedVia::Wrapped(Backend::Sudo));
    child.terminate().expect("sudo relays SIGTERM");
    // Closed first: a front nothing killed then exits 0, and the assertion fails.
    drop(stdin);
    assert_eq!(child.wait().await.expect("wait").signal(), Some(libc::SIGTERM));
}

/// Async twin of the sync `a_failed_spawn_hands_back_an_elevation_front_and_says_so`, which leaves
/// the front where the sync one hands it back.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_failed_spawn_leaves_an_elevation_front_running_and_says_so() {
    use crate::child::front_kill_tests::{assert_noted, failed_front_spawns, reap};
    crate::log_capture::install();
    let mark = crate::log_capture::mark();
    let failures = failed_front_spawns(None, |cmd| {
        crate::tokio::spawn::spawn(cmd).map(drop).map_err(Into::into)
    });
    assert_noted(&failures, "the elevated program may be running; left unreaped");
    // Each failed spawn is one event: the forget of tokio's `Child` and the front left running share
    // one warn, which carries the leak. (Mutant: the forget warns on its own.) The front is only
    // looked at, and is left running: the warn does not say it was torn down. (Mutant:
    // `leave_front_noting`'s forget note claims a teardown.)
    let warns = crate::log_capture::warns_since(mark, "");
    assert_eq!(warns.len(), failures.len(), "{warns:?}");
    assert!(
        warns
            .iter()
            .all(|text| text.contains("leaks") && text.contains("is left running and unreaped")),
        "{warns:?}"
    );
    assert!(warns.iter().all(|text| !text.contains("torn down")), "{warns:?}");
    for (_, pid, handed_back) in &failures {
        assert!(
            handed_back.is_none(),
            "an async spawn leaves the front, it hands nothing back"
        );
        let status = reap(*pid).expect("the front must be left unreaped");
        assert!(status.success(), "the teardown signalled the front: {status:?}");
    }
}

/// A tokio spawn that fails after its fork, as when its reaper registration is refused
/// (`epoll_ctl`'s `ENOSPC`), sends an elevation front nothing, leaves it unreaped, and says so on
/// the error, its variant kept. The `cat`'s stdin is a pipe this test owns: tokio's `Child`, whose
/// streams the failure leaks, holds none of it.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_spawn_failing_after_its_fork_leaves_an_elevation_front_running_and_says_so() {
    use std::os::fd::OwnedFd;

    use crate::child::front_kill_tests::reap;
    use crate::child::spawn::fault;
    let (reader, writer) = std::io::pipe().expect("pipe");
    let mut cmd = crate::test_child::command(Console::Hidden);
    cmd.args(["cat"]);
    cmd.stdin(Stdio::from_file(std::fs::File::from(OwnedFd::from(reader))))
        .expect("stdin");
    cmd.set_elevation_front(crate::elevation::front::front(Some(&ElevatedVia::Wrapped(
        Backend::Sudo,
    ))));
    let (err, fate) = {
        let _failing = fault::fail_tokio_spawns_after_fork();
        crate::tokio::spawn::spawn(&mut cmd)
            .map(drop)
            .expect_err("the forced failure fails the spawn")
            .expect_may_have_started_with()
    };
    assert_eq!(
        fate,
        ChildFate::Running { id: None },
        "the front is left unreaped, its identity unread"
    );
    let pid = fault::take_forgotten_pid().expect("the seam forked a child");
    let crate::error::Error::Io(io) = &err else {
        panic!("the spawn's error keeps its variant: {err:?}");
    };
    assert_eq!(io.raw_os_error(), None, "noted, with the original as its source: {io}");
    let text = err.to_string();
    assert!(text.contains("the spawned child is what sudo left"), "{text}");
    assert!(
        text.contains("the elevated program may be running; left unreaped"),
        "{text}"
    );
    drop(writer);
    let status = reap(pid).expect("the front must be left unreaped");
    assert!(status.success(), "the teardown signalled the front: {status:?}");
}

/// A front whose exec fails never ran the program: std collected the child, so its error carries no
/// note that the program may be running. The exec fails on an argument longer than `MAX_ARG_STRLEN`
/// (`E2BIG`). The spawn still says the program may have started: tokio's error does not say whether
/// std's spawn failed.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_front_whose_exec_fails_is_not_noted() {
    let mut cmd = crate::test_child::command(Console::Hidden);
    cmd.args(["true".to_owned(), "x".repeat(256 * 1024)]);
    cmd.set_elevation_front(crate::elevation::front::front(Some(&ElevatedVia::Wrapped(
        Backend::Sudo,
    ))));
    let (err, fate) = crate::tokio::spawn::spawn(&mut cmd)
        .map(drop)
        .expect_err("an argument over MAX_ARG_STRLEN fails the exec")
        .expect_may_have_started_with();
    assert_eq!(fate, ChildFate::Gone, "the handshake found the child already collected");
    let crate::error::Error::Io(io) = &err else {
        panic!("an exec failure is an Io error: {err:?}");
    };
    assert_eq!(io.raw_os_error(), Some(libc::E2BIG), "{io}");
    assert!(!err.to_string().contains("what sudo left"), "no note: {err}");
}

/// A front reaped behind its handle before its identity is read, as another reaper in the process
/// may (`SIGCHLD` set to `SIG_IGN`), is `Gone`, in one warn that carries the forget of tokio's
/// `Child`. The sync spawn answers it the same way.
///
/// Mutant: `leave_front_noting` keeps its `debug_assert!(false)` on a backend forgotten as foreign
/// (a debug panic), or answers `Unknown`.
#[cfg(target_os = "linux")]
#[skuld::test]
async fn a_front_reaped_elsewhere_before_its_identity_read_is_gone() {
    use crate::child::spawn::fault;
    crate::log_capture::install();
    let (stdin, writer) = crate::test_child::held_writer_stdin();
    let mut cmd = crate::test_child::command(Console::Hidden);
    cmd.args(["cat"]);
    cmd.stdin(stdin).expect("stdin");
    cmd.set_elevation_front(crate::elevation::front::front(Some(&ElevatedVia::Wrapped(
        Backend::Sudo,
    ))));
    let _reaped_elsewhere = fault::set_at(fault::SpawnPoint::BeforeIdentity, move || {
        drop(writer);
        crate::test_child::reaped_elsewhere::reap_behind(fault::spawn_pid());
    });
    let mark = crate::log_capture::mark();
    let (err, fate) = crate::tokio::spawn::spawn(&mut cmd)
        .map(drop)
        .expect_err("a front reaped before its identity read fails the spawn")
        .expect_may_have_started_with();
    assert_eq!(fate, ChildFate::Gone, "{err}");
    let warns = crate::log_capture::warns_since(mark, "");
    assert_eq!(warns.len(), 1, "{warns:?}");
    assert!(warns[0].contains("leaks"), "the warn carries the forget: {warns:?}");
}

/// macOS: a child that is not a front, whose report cannot be read, cannot be shown to be ours. The
/// teardown sends it nothing, so it may be running: not `Gone`, which would say someone else reaped it.
///
/// Mutant: the teardown reads a kill that sent nothing (`Sent::Gone`) as a foreign reap.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn macos_a_child_whose_report_read_fails_may_be_running() {
    use crate::child::front_kill_tests::{fail_a_front_spawn, fail_the_report_read};
    let (_err, _id, fate) = fail_a_front_spawn(
        |cmd| cmd.set_elevation_front(None),
        fail_the_report_read,
        |cmd| crate::tokio::spawn::spawn(cmd).map(drop).map_err(Into::into),
    );
    assert_eq!(fate, crate::error::ChildFate::Running { id: None });
}

/// Async twin of the sync `macos_a_front_whose_report_read_fails_is_left_and_noted`.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn macos_a_front_whose_report_read_fails_is_left_and_noted() {
    use crate::child::front_kill_tests::{assert_unadopted_front_noted, fail_a_front_spawn, fail_the_report_read};
    let (err, id, fate) = fail_a_front_spawn(
        |_| {},
        fail_the_report_read,
        |cmd| crate::tokio::spawn::spawn(cmd).map(drop).map_err(Into::into),
    );
    assert_eq!(
        fate,
        crate::error::ChildFate::Running { id: None },
        "the front is left running"
    );
    let pid = id.pid();
    assert_unadopted_front_noted(&err, pid);
}

/// Async twin of the sync `macos_a_contained_front_whose_identity_is_refused_is_left_and_noted`.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn macos_a_contained_front_whose_identity_is_refused_is_left_and_noted() {
    use crate::child::front_kill_tests::{assert_unadopted_front_noted, fail_a_front_spawn, refuse_the_identity};
    let (err, id, fate) = fail_a_front_spawn(
        |cmd| _ = cmd.contain(),
        refuse_the_identity,
        |cmd| crate::tokio::spawn::spawn(cmd).map(drop).map_err(Into::into),
    );
    assert_eq!(
        fate,
        crate::error::ChildFate::Running { id: None },
        "the front is left running"
    );
    let pid = id.pid();
    assert_unadopted_front_noted(&err, pid);
}

/// Async twin of the sync `macos_a_front_whose_attach_fails_is_left_unreaped_and_noted`.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn macos_a_front_whose_attach_fails_is_left_unreaped_and_noted() {
    use crate::child::front_kill_tests::{assert_attach_failure_left_the_front, fail_a_front_spawn, fail_the_attach};
    crate::log_capture::install();
    let mark = crate::log_capture::mark();
    let (err, id, fate) = fail_a_front_spawn(
        |_| {},
        fail_the_attach,
        |cmd| crate::tokio::spawn::spawn(cmd).map(drop).map_err(Into::into),
    );
    assert_eq!(
        fate,
        crate::error::ChildFate::Running { id: Some(id) },
        "the front is left running"
    );
    let pid = id.pid();
    assert_attach_failure_left_the_front(&err, pid, mark);
}

/// Async twin of the sync `macos_an_identity_check_that_found_the_front_gone_does_not_claim_it_unreaped`.
#[cfg(target_os = "macos")]
#[skuld::test]
async fn macos_an_identity_check_that_found_the_front_gone_does_not_claim_it_unreaped() {
    use crate::child::front_kill_tests::{assert_noted_unaccounted, fail_a_front_spawn, identity_finds_the_front_gone};
    let (err, _id, fate) = fail_a_front_spawn(
        |_| {},
        identity_finds_the_front_gone,
        |cmd| crate::tokio::spawn::spawn(cmd).map(drop).map_err(Into::into),
    );
    assert_eq!(
        fate,
        crate::error::ChildFate::Gone,
        "the check found it reaped by someone else"
    );
    assert_noted_unaccounted(&err);
}