cosca 0.4.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
//! Per-OS containment dispatch (two-phase: prepare before spawn, attach after).

use crate::containment::{ContainMode, ContainRequest, Containment, Nesting};
use crate::error::Error;

/// The pre-spawn containment decision produced by `prepare` (env-marker root
/// detection plus per-OS pre-spawn setup).
pub(crate) struct Prepared {
    #[allow(dead_code)] // read in #[cfg(unix)] branch of attach()
    pub mode: Option<ContainMode>,
    #[allow(dead_code)] // read in #[cfg(unix)] branch of attach()
    pub is_root: bool,
    /// Pre-created cgroup leaf (Linux only). `Some` means the child must be
    /// placed in the cgroup via the `pre_exec` closure; `None` means fall back
    /// to the process-group mechanism.
    #[cfg(target_os = "linux")]
    pub cgroup_leaf: Option<crate::containment::cgroup::CgroupLeaf>,
    /// The marker pipe for a contained macOS root. `None` for a nested member, an
    /// uncontained or elevation-derived spawn, or a failed install (which degrades to the
    /// process group).
    #[cfg(target_os = "macos")]
    pub marker: Option<crate::containment::fdmarker::PreparedMarker>,
    /// The cooperative-signal mechanism implied by the creation-flag word this spawn passed to
    /// `CreateProcessW`. Set by whichever backend chose that word, so the derivation reads what
    /// the OS was actually given.
    #[cfg(windows)]
    pub graceful: crate::graceful::GracefulMechanism,
}

impl Prepared {
    /// The cooperative-signal mechanism for a child spawned from this decision. The `cfg` lives
    /// here, once, so no spawn path carries one for it: every non-Windows child cosca owns can
    /// be sent an identity-bound `SIGTERM`, whatever its containment.
    pub(crate) fn graceful_mechanism(&self) -> crate::graceful::GracefulMechanism {
        #[cfg(windows)]
        return self.graceful;
        #[cfg(not(windows))]
        return crate::graceful::GracefulMechanism::Process;
    }
}

/// What a spawn achieved, beyond the child handle itself: the tree-teardown mechanism and the
/// cooperative-signal mechanism, which are independent axes (see [`crate::graceful`]).
///
/// Bundled rather than threaded as separate positional arguments so the spawn constructors do
/// not grow an argument per spawn fact, and so a new fact is a compile error at every
/// construction site rather than a silently diverging path.
pub(crate) struct Attachment {
    pub containment: Containment,
    pub attached: Attached,
    pub graceful: crate::graceful::GracefulMechanism,
}

#[cfg(windows)]
impl Attachment {
    /// The attachment for a UAC-elevated spawn, which `ShellExecuteEx("runas")` creates through
    /// a system service with creation flags this process never sees and never chose.
    ///
    /// The mechanism is `Unknown`, so [`crate::graceful::signal`] refuses this child rather than
    /// addressing a console control event to its pid. `OtherConsoleGroup` would claim a route to
    /// a group known to exist, and `None` that no cooperative signal exists from any process
    /// ever; cosca can make neither claim about a child it did not create.
    pub(crate) fn uac_elevated() -> Attachment {
        Attachment {
            containment: Containment::None,
            attached: Attached::None,
            graceful: crate::graceful::GracefulMechanism::Unknown,
        }
    }
}

/// Owns the OS containment resource for a spawned child; `hard_kill`/`terminate`
/// act on the tree, `disarm` neutralizes teardown for `detach()`. `None` =
/// uncontained (lone-process semantics).
#[derive(Debug, Default)]
pub(crate) enum Attached {
    #[default]
    None,
    /// A nested containment member: it joined an ancestor's group/job and owns no
    /// teardown mechanism of its own (the outermost root tears the tree down). Distinct
    /// from `None` (genuinely uncontained) so `_tree` ops can error honestly.
    Delegated,
    #[cfg(unix)]
    ProcessGroup(i32), // pgid (== root pid)
    #[cfg(target_os = "linux")]
    Cgroup(crate::containment::cgroup::CgroupLeaf),
    #[cfg(windows)]
    JobObject(crate::containment::windows::JobHandle),
    /// macOS inherited-fd marker: membership survives `setsid`, `setpgid`, reparenting and
    /// `exec`. Owns the supervisor's read end of the marker pipe — which is what keeps the
    /// pipe's kernel identity from being re-issued — plus, when the mode created one, the pgid.
    #[cfg(target_os = "macos")]
    FdMarker(crate::containment::fdmarker::Marker),
    /// Identity-aware tree-walk teardown (cross-platform; the root identity is
    /// re-enumerated and killed by identity at teardown). No cfg gate: this is
    /// the universal fallback and a directly-selectable mode on every OS.
    TreeWalk(crate::identity::ProcessId),
}

// CgroupLeaf is not Debug; provide a minimal impl.
#[cfg(target_os = "linux")]
impl std::fmt::Debug for crate::containment::cgroup::CgroupLeaf {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CgroupLeaf").finish_non_exhaustive()
    }
}

impl Attached {
    /// Hard-kill the contained tree (best-effort; already-gone is success).
    pub(crate) fn hard_kill(&self) -> Result<(), crate::error::Error> {
        match self {
            Attached::None | Attached::Delegated => Ok(()),
            #[cfg(unix)]
            Attached::ProcessGroup(pgid) => crate::containment::unix::kill_group(*pgid),
            #[cfg(target_os = "linux")]
            Attached::Cgroup(leaf) => {
                leaf.hard_kill();
                Ok(())
            }
            #[cfg(windows)]
            Attached::JobObject(job) => job.hard_kill().map_err(Error::Io),
            #[cfg(target_os = "macos")]
            Attached::FdMarker(m) => m.hard_kill(),
            Attached::TreeWalk(root) => {
                crate::containment::treewalk::hard_kill(*root);
                Ok(())
            }
        }
    }

    /// Send the graceful termination signal to the group (signal-only).
    pub(crate) fn terminate(&self, _child_pid: u32) -> Result<(), Error> {
        match self {
            Attached::None | Attached::Delegated => {
                debug_assert!(
                    self.is_actionable(),
                    "Attached::terminate on a non-actionable mechanism"
                );
                Err(crate::error::Error::Unsupported {
                    op: "terminate on a non-actionable mechanism".into(),
                    platform: std::env::consts::OS,
                    detail: "internal invariant: a non-actionable mechanism reached terminate".into(),
                })
            }
            #[cfg(unix)]
            Attached::ProcessGroup(pgid) => crate::containment::unix::term_group(*pgid),
            #[cfg(target_os = "linux")]
            Attached::Cgroup(leaf) => leaf.terminate().map_err(Error::Io),
            #[cfg(windows)]
            Attached::JobObject(_) => crate::containment::windows::terminate(_child_pid),
            #[cfg(target_os = "macos")]
            Attached::FdMarker(m) => m.terminate(),
            Attached::TreeWalk(root) => crate::containment::treewalk::terminate(*root),
        }
    }

    /// Neutralize teardown so `detach()` leaves the tree running. For Job Objects,
    /// clears `KILL_ON_JOB_CLOSE` so the handle close does not kill the tree.
    /// No-op for mechanisms whose resource-drop does not kill (pgroup/cgroup/none).
    pub(crate) fn disarm(&self) {
        match self {
            Attached::None | Attached::Delegated => {}
            #[cfg(unix)]
            Attached::ProcessGroup(_) => {} // pgroup drop doesn't kill — no-op
            #[cfg(target_os = "linux")]
            Attached::Cgroup(_) => {} // cgroup.kill is explicit — drop doesn't kill
            #[cfg(windows)]
            Attached::JobObject(job) => job.disarm(), // clear KILL_ON_JOB_CLOSE before handle drops
            // dropping the read end does not kill; detach opts out via kill_on_drop
            #[cfg(target_os = "macos")]
            Attached::FdMarker(_) => {}
            Attached::TreeWalk(_) => {} // no kernel resource whose drop kills; detach opts out via kill_on_drop
        }
    }

    /// Whether this mechanism fires `killpg` against a pgid subject to the reap-then-recycle
    /// hazard `Child::kill_tree`/`terminate_tree`'s precondition assert guards against: the OS
    /// reaping the leader and recycling its pid/pgid onto a different, live process group
    /// before the signal is sent. `ProcessGroup` always carries one; a macOS `FdMarker` does
    /// too, when its mode created one (`Marker::has_pgid` — `None` for `TreeWalk`, which needs
    /// no pgid and stays exempt, matching `Attached::TreeWalk` below). `FdMarker` fires
    /// `killpg` on `self.pgid` unconditionally on pass 1 of every sweep, so the hazard is
    /// identical, not merely analogous — routing every contained macOS root through
    /// `FdMarker` instead of `ProcessGroup` must not silently drop this precondition's coverage.
    #[cfg(unix)]
    pub(crate) fn carries_recyclable_pgid(&self) -> bool {
        match self {
            Attached::ProcessGroup(_) => true,
            #[cfg(target_os = "macos")]
            Attached::FdMarker(m) => m.has_pgid(),
            _ => false,
        }
    }

    /// Whether this child holds an actionable tree-teardown mechanism.
    pub(crate) fn is_actionable(&self) -> bool {
        match self {
            Attached::None | Attached::Delegated => false,
            #[cfg(unix)]
            Attached::ProcessGroup(_) => true,
            #[cfg(target_os = "linux")]
            Attached::Cgroup(_) => true,
            #[cfg(windows)]
            Attached::JobObject(_) => true,
            #[cfg(target_os = "macos")]
            Attached::FdMarker(_) => true,
            Attached::TreeWalk(_) => true,
        }
    }

    /// Whether this handle owns a mechanism with a kernel drain edge — mirrors
    /// [`Containment::can_observe_drain`](crate::containment::Containment::can_observe_drain),
    /// checked against the concrete resource rather than the reported enum so the two can
    /// never drift silently (`require_drainable` asserts them equal in debug).
    pub(crate) fn can_observe_drain(&self) -> bool {
        match self {
            #[cfg(target_os = "linux")]
            Attached::Cgroup(_) => true,
            #[cfg(windows)]
            Attached::JobObject(_) => true,
            #[cfg(target_os = "macos")]
            Attached::FdMarker(_) => true,
            _ => false,
        }
    }

    /// Block until every member of the contained tree has EXITED (not reaped), or until
    /// `deadline`. `Unsupported` on every mechanism without a kernel drain edge (checked by the
    /// caller via `require_drainable`/`can_observe_drain` before this is reached in the public
    /// API, but this still refuses honestly if ever called directly against a non-drainable
    /// mechanism).
    pub(crate) fn wait_drained(
        &self,
        deadline: Option<Option<std::time::Instant>>,
    ) -> Result<crate::containment::TreeDrain, Error> {
        match self {
            #[cfg(target_os = "linux")]
            Attached::Cgroup(leaf) => leaf.wait_drained(deadline),
            #[cfg(windows)]
            Attached::JobObject(job) => job.wait_drained(deadline, None),
            #[cfg(target_os = "macos")]
            Attached::FdMarker(m) => m.wait_drained(deadline),
            _ => Err(Error::Unsupported {
                op: "wait for the contained tree to drain".into(),
                platform: std::env::consts::OS,
                detail: "this child's containment mechanism exposes no kernel edge for tree drain".into(),
            }),
        }
    }
}

/// Returns `true` when the current process is the outermost contained root
/// (i.e. the env marker is absent). Pure function: takes the marker-presence
/// flag so it can be unit-tested without touching the process environment.
pub(crate) fn is_nested(marker_present: bool) -> bool {
    marker_present
}

/// Resolve the spawned root's identity by pid. **Precondition:** the caller holds the owning
/// `Child` (sync `std::process::Child` / async `::tokio::process::Child`) across this call — it
/// pins the pid against reuse, so the by-pid resolve is race-free (the freshly spawned root is
/// un-reaped, and on Windows still suspended, hence resolvable).
#[cfg(any(unix, windows))]
fn resolve_root_id(pid: u32) -> Result<crate::identity::ProcessId, Error> {
    match crate::identity::ProcessId::of(pid) {
        crate::identity::Resolved::Found(id) => Ok(id),
        crate::identity::Resolved::Gone => Err(Error::Containment {
            detail: "tree-walk root vanished before its identity could be read".into(),
        }),
        crate::identity::Resolved::Unknown => Err(Error::Unassessable {
            detail: format!("tree-walk root pid {pid} identity could not be read"),
            source: None,
        }),
    }
}

/// Which Unix setup action to apply to a root `Command` for a given mode.
/// Pure function: used by `prepare` and unit-tested separately to verify
/// mutual exclusivity: Session → setsid only; Strongest/default → pgroup
/// only; TreeWalk → neither (it must catch process-group escapees).
#[cfg(unix)]
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum UnixSetup {
    /// Apply `setsid` via `pre_exec` (ContainMode::Session). Must NOT be
    /// combined with ProcessGroup on the same Command (EPERM on a session leader).
    Session,
    /// Apply `process_group(0)` (ContainMode::Strongest or default).
    ProcessGroup,
    /// Apply NO pre-spawn grouping (ContainMode::TreeWalk). TreeWalk's whole
    /// point is to catch children that `setsid`/`setpgid` out of a process group,
    /// so it must not put the root in a group itself; teardown is by identity.
    None,
}

/// Decide which Unix mechanism to apply for `mode` (root spawns only).
/// Keeping this as a pure function makes the mutual-exclusivity invariant
/// directly unit-testable without inspecting `std::process::Command` internals.
#[cfg(unix)]
pub(crate) fn unix_setup_for(mode: Option<ContainMode>) -> UnixSetup {
    match mode {
        Some(ContainMode::Session) => UnixSetup::Session,
        Some(ContainMode::TreeWalk) => UnixSetup::None,
        _ => UnixSetup::ProcessGroup,
    }
}

/// The Windows pre-spawn containment decision, shared by the std `prepare` path and the raw
/// `CreateProcessW` backend. Encodes ONLY the decision; the caller applies it (`creation_flags`
/// onto the command, the root env marker, `clear_std_handle_inheritance`).
#[cfg(windows)]
pub(crate) struct WindowsContain {
    /// Creation flags to OR into the spawn: `root_flags` (suspend + new group) for a Strongest
    /// root, `group_flags` (new group only) otherwise; `0` when uncontained (`mode` is `None`).
    pub creation_flags: u32,
    /// Whether this spawn must set the inherited root marker so descendants join THIS group.
    pub marker_env: bool,
}

/// Decide the Windows pre-spawn containment flags + marker for `req`/`is_root`. Pure — directly
/// unit-testable. Returns `{ creation_flags: 0, marker_env: false }` when `req.mode` is `None`.
#[cfg(windows)]
pub(crate) fn windows_contain_setup(req: &ContainRequest, is_root: bool) -> WindowsContain {
    let Some(mode) = req.mode else {
        return WindowsContain {
            creation_flags: 0,
            marker_env: false,
        };
    };
    let creation_flags = if is_root && !matches!(mode, ContainMode::TreeWalk) {
        // Strongest root: suspend + new process group (job assigned in attach).
        crate::containment::windows::root_flags()
    } else {
        // TreeWalk root (no suspend, no job — identity teardown) and all nested spawns:
        // CREATE_NEW_PROCESS_GROUP only, so `terminate` can CTRL_BREAK the root's group.
        crate::containment::windows::group_flags()
    };
    WindowsContain {
        creation_flags,
        marker_env: is_root && req.nesting == Nesting::Mark,
    }
}

/// Phase 1 (before spawn): env-marker root detection + pre-spawn OS setup.
///
/// `reserved_fds` and `marker_suppressed` drive the macOS fd-marker install only; every
/// other platform ignores them. `flags` is the caller's creation-flag request, which only
/// Windows can honour.
///
/// Fallible only on Windows, where the composed creation-flag word may name a reserved bit. The
/// composition runs at the very top — before anything is mutated, so a refusal leaves nothing to
/// unwind, and above the uncontained early return, so an uncontained spawn carries the caller's
/// flags too.
pub(crate) fn prepare(
    std_cmd: &mut std::process::Command,
    req: &ContainRequest,
    #[cfg_attr(not(windows), allow(unused_variables))] flags: &crate::command::flags::FlagsRequest,
    #[cfg_attr(not(target_os = "macos"), allow(unused_variables))] reserved_fds: &[i32],
    #[cfg_attr(not(target_os = "macos"), allow(unused_variables))] marker_suppressed: bool,
) -> Result<Prepared, Error> {
    let mode = req.mode;
    // Read once, above every branch, so the word is composed exactly once per spawn and no two
    // compositions can disagree. This is this process's own environment variable — read-only and
    // constant for the run — so reading it for an uncontained spawn too is unobservable.
    let marker_present = std::env::var_os(crate::containment::NESTED_ENV).is_some();
    let is_root = !is_nested(marker_present);

    #[cfg(windows)]
    let graceful = {
        use std::os::windows::process::CommandExt;
        let spawn =
            crate::command::flags::windows_spawn(req, *flags, is_root, crate::command::flags::SpawnBackend::Std)?;
        std_cmd.creation_flags(spawn.creation_flags);
        // Sound for THIS derivation and no other: `mechanism_from_flags` reads only
        // CREATE_NEW_PROCESS_GROUP / DETACHED_PROCESS / CREATE_NEW_CONSOLE / CREATE_NO_WINDOW,
        // and std adds and removes none of them. The word is what cosca supplies, never what
        // `CreateProcessW` receives — see `crate::command::flags`.
        crate::containment::windows::mechanism_from_flags(spawn.creation_flags)
    };

    if mode.is_none() {
        return Ok(Prepared {
            mode: None,
            is_root: false,
            #[cfg(target_os = "linux")]
            cgroup_leaf: None,
            #[cfg(target_os = "macos")]
            marker: None,
            #[cfg(windows)]
            graceful,
        });
    }
    if is_root && req.nesting == Nesting::Mark {
        // Set AFTER any user env ops (env_clear) have been applied to std_cmd by
        // the spawn engine, so the marker survives env_clear. `env` appends.
        std_cmd.env(crate::containment::NESTED_ENV, "1");
    }

    // Linux: session mode or (cgroup v2 + process group).
    // Mechanism selection via `unix_setup_for` (setsid/pgroup mutual exclusivity):
    // Session → setsid only; Strongest/TreeWalk → process_group(0) + try cgroup.
    #[cfg(target_os = "linux")]
    {
        if is_root && mode.is_some() {
            match unix_setup_for(mode) {
                UnixSetup::Session => {
                    // Session: setsid only — no process_group(0) (would EPERM).
                    crate::containment::unix::set_session(std_cmd);
                    return Ok(Prepared {
                        mode,
                        is_root,
                        cgroup_leaf: None, // cgroup not used for Session
                    });
                }
                UnixSetup::None => {
                    // TreeWalk: NO process group / setsid / cgroup — teardown is
                    // by identity so we can catch process-group escapees.
                    return Ok(Prepared {
                        mode,
                        is_root,
                        cgroup_leaf: None,
                    });
                }
                UnixSetup::ProcessGroup => {
                    // Strongest: set a new process group + try cgroup.
                    crate::containment::unix::set_process_group(std_cmd);
                }
            }

            let leaf = crate::containment::cgroup::try_create_leaf();
            if let Some(ref l) = leaf {
                // Wire the pre_exec self-placement. The closure captures the raw
                // fd integer (Copy) — not the leaf itself (which stays in Prepared).
                // On error (e.g. EBUSY — "no internal processes" rule), the closure
                // returns Ok so the spawn proceeds and `attach` falls back to the
                // already-configured process group rather than aborting the spawn.
                // Safety: pre_exec runs post-fork, pre-exec; the function is
                // async-signal-safe (libc::write + libc::close, no alloc).
                let procs_fd = l.procs_fd();
                unsafe {
                    use std::os::unix::process::CommandExt;
                    std_cmd.pre_exec(move || {
                        let _ = crate::containment::cgroup::place_self_in_cgroup_pre_exec(procs_fd);
                        Ok(())
                    });
                }
            }
            return Ok(Prepared {
                mode,
                is_root,
                cgroup_leaf: leaf,
            });
        }
        return Ok(Prepared {
            mode,
            is_root,
            cgroup_leaf: None,
        });
    }

    // Non-Linux Unix: process group or session (mutually exclusive).
    // Mechanism selection via `unix_setup_for`: Session → setsid only;
    // Strongest (= ProcessGroup on macOS) → process_group(0).
    #[cfg(all(unix, not(target_os = "linux")))]
    if is_root && mode.is_some() {
        match unix_setup_for(mode) {
            UnixSetup::Session => crate::containment::unix::set_session(std_cmd),
            UnixSetup::ProcessGroup => crate::containment::unix::set_process_group(std_cmd),
            UnixSetup::None => {} // TreeWalk: no pre-spawn grouping (identity teardown)
        }
    }

    // macOS: install the inherited-fd marker for every contained root, regardless of the
    // requested mode (decision: the marker survives what the mode-specific mechanism above
    // does not). `marker_wanted` gates on root/mode/suppression; `install` degrades to `None`
    // on any failure rather than failing the spawn.
    #[cfg(target_os = "macos")]
    let marker = if crate::containment::fdmarker::marker_wanted(mode, is_root, marker_suppressed) {
        crate::containment::fdmarker::install(std_cmd, reserved_fds)
    } else {
        None
    };

    // Windows: stop the child inheriting this process's std handles. Contained spawns only, and
    // below the composition above so a refused spawn has mutated nothing. The creation-flag word
    // itself is applied at the top, since an uncontained spawn never reaches this point.
    #[cfg(windows)]
    crate::containment::windows::clear_std_handle_inheritance();

    #[allow(unreachable_code)]
    Ok(Prepared {
        mode,
        is_root,
        #[cfg(target_os = "linux")]
        cgroup_leaf: None,
        #[cfg(target_os = "macos")]
        marker,
        #[cfg(windows)]
        graceful,
    })
}

/// Phase 2 (after spawn, before SharedChild::new): attach the mechanism and bundle it with the
/// spawn's other achieved facts. Consumes `prepared` so Linux cgroup leaf ownership transfers
/// cleanly to `Attached::Cgroup` without requiring interior mutability — hence the
/// cooperative-signal mechanism is read off it first.
pub(crate) fn attach(
    pid: u32,
    #[cfg(windows)] proc_handle: std::os::windows::io::RawHandle,
    prepared: Prepared,
) -> Result<Attachment, Error> {
    let graceful = prepared.graceful_mechanism();
    let (containment, attached) = attach_tree(
        pid,
        #[cfg(windows)]
        proc_handle,
        prepared,
    )?;
    Ok(Attachment {
        containment,
        attached,
        graceful,
    })
}

/// The tree-teardown half of [`attach`]: which mechanism owns this child's tree.
fn attach_tree(
    pid: u32,
    #[cfg(windows)] proc_handle: std::os::windows::io::RawHandle,
    prepared: Prepared,
) -> Result<(Containment, Attached), Error> {
    // Linux: session, or cgroup v2 / process group.
    #[cfg(target_os = "linux")]
    {
        if prepared.mode.is_some() {
            if prepared.is_root {
                // TreeWalk root: no kernel container / process group; teardown is
                // by identity. Resolve the root identity (consistent with the
                // post-attach identity read in spawn.rs).
                if matches!(prepared.mode, Some(ContainMode::TreeWalk)) {
                    return Ok((Containment::TreeWalk, Attached::TreeWalk(resolve_root_id(pid)?)));
                }

                let raw_pid = pid;
                debug_assert!(
                    raw_pid <= i32::MAX as u32,
                    "pid {raw_pid} exceeds i32::MAX; pgid cast would truncate"
                );
                let pgid = raw_pid as i32;

                // Session mode: setsid was applied pre-spawn; no cgroup.
                // pgid == sid == pid for the session leader; killpg works.
                if matches!(prepared.mode, Some(ContainMode::Session)) {
                    return Ok((Containment::Session, Attached::ProcessGroup(pgid)));
                }

                // Strongest: cgroup v2 if available, else process group.
                if let Some(leaf) = prepared.cgroup_leaf {
                    // Verify placement: the pre_exec write can silently fail
                    // (EBUSY — "no internal processes" rule when the supervisor
                    // is itself an undelegated leaf). Read cgroup.procs to
                    // confirm the child's pid is actually present.
                    if leaf.contains_pid(raw_pid) {
                        return Ok((Containment::CgroupV2, Attached::Cgroup(leaf)));
                    }
                    // Placement failed — the leaf is empty; drop it (triggers
                    // rmdir). The process group set pre-spawn is the real container.
                    drop(leaf);
                    return Ok((Containment::ProcessGroup, Attached::ProcessGroup(pgid)));
                }
                // No cgroup leaf: fall back to process group (set pre-spawn).
                return Ok((Containment::ProcessGroup, Attached::ProcessGroup(pgid)));
            } else {
                // Nested member: it joined the ancestor's cgroup/process group (or the
                // root's tree-walk) rather than creating its own, so it owns no teardown —
                // the outermost root tears the whole tree down.
                return Ok((Containment::Delegated, Attached::Delegated));
            }
        }
    }

    // Non-Linux Unix: process group or session.
    // For Session: setsid was called pre-spawn (not process_group(0)); the child
    // is a session leader with pgid == pid. Teardown via killpg is identical —
    // Attached::ProcessGroup(pgid) is reused since the pgroup == session leader's pid.
    // For Strongest (= ProcessGroup on macOS): process_group(0) was called pre-spawn.
    #[cfg(all(unix, not(target_os = "linux")))]
    {
        if prepared.mode.is_some() {
            if prepared.is_root {
                // macOS fd marker: installed for every contained root regardless of mode, so
                // it takes priority over the mode-specific mechanism below — it is what
                // survives setsid/reparenting/exec that the mode-specific mechanism does not.
                #[cfg(target_os = "macos")]
                if let Some(marker) = prepared.marker {
                    // `Gone` is routine, not an anomaly: `SharedChild::new`'s internal
                    // `try_wait` can reap a fast-exiting child inside `spawn()` itself (#61) —
                    // `debug!`, no `incomplete`. The marker and group channels below don't need
                    // `root`, so a reparented-away live descendant stays reachable through them.
                    // `Unknown` means the OS refused to answer — a real gap — so it is `warn!`
                    // and carries `root_denied` into `Marker`, the only way `sweep_pass` can
                    // tell "denied" apart from an ordinary `Gone` on every later pass.
                    let (root, root_denied) = match crate::identity::ProcessId::of(pid) {
                        crate::identity::Resolved::Found(id) => (Some(id), false),
                        crate::identity::Resolved::Gone => {
                            log::debug!(
                                "fd marker: the root exited before its identity could be read; \
                                 the sweep runs without its ppid-walk channel (the marker and \
                                 group channels do not need it)"
                            );
                            (None, false)
                        }
                        crate::identity::Resolved::Unknown => {
                            log::warn!(
                                "fd marker: the root's identity could not be read (access \
                                 denied); the sweep runs without its ppid-walk channel, and \
                                 every pass reports this marker as incomplete"
                            );
                            (None, true)
                        }
                    };
                    let pgid = match prepared.mode {
                        Some(ContainMode::TreeWalk) => None,
                        _ => {
                            debug_assert!(
                                pid <= i32::MAX as u32,
                                "pid {pid} exceeds i32::MAX; pgid cast would truncate"
                            );
                            Some(pid as i32)
                        }
                    };
                    return Ok((
                        Containment::FdMarker,
                        Attached::FdMarker(crate::containment::fdmarker::Marker::new(
                            marker,
                            root,
                            pgid,
                            root_denied,
                        )),
                    ));
                }
                // TreeWalk root: no process group; identity teardown.
                if matches!(prepared.mode, Some(ContainMode::TreeWalk)) {
                    return Ok((Containment::TreeWalk, Attached::TreeWalk(resolve_root_id(pid)?)));
                }
                let raw_pid = pid;
                debug_assert!(
                    raw_pid <= i32::MAX as u32,
                    "pid {raw_pid} exceeds i32::MAX; pgid cast would truncate"
                );
                let pgid = raw_pid as i32;
                let containment = match prepared.mode {
                    Some(ContainMode::Session) => Containment::Session,
                    _ => Containment::ProcessGroup,
                };
                return Ok((containment, Attached::ProcessGroup(pgid)));
            } else {
                // Nested member: it joined the ancestor's process group (or the root's
                // tree-walk) rather than creating its own, so it owns no teardown — the
                // outermost root tears the whole tree down.
                #[cfg(target_os = "macos")]
                if prepared.marker.is_some() {
                    log::warn!(
                        "fd marker: a nested spawn's prepare() unexpectedly produced a marker; \
                         a nested member must inherit the root's marker, never create its own \
                         — discarding it"
                    );
                    debug_assert!(
                        false,
                        "a nested member inherits the root's marker and must never create one"
                    );
                }
                return Ok((Containment::Delegated, Attached::Delegated));
            }
        }
    }

    // Windows: Job Object (strongest available on this OS), or TreeWalk.
    #[cfg(windows)]
    {
        if prepared.mode.is_some() && prepared.is_root {
            // TreeWalk root: no job (spawned with CREATE_NEW_PROCESS_GROUP only);
            // identity teardown, with CTRL_BREAK to the group as cooperative term.
            if matches!(prepared.mode, Some(ContainMode::TreeWalk)) {
                return Ok((Containment::TreeWalk, Attached::TreeWalk(resolve_root_id(pid)?)));
            }
            match crate::containment::windows::attach_job(proc_handle) {
                Ok(Some(job)) => return Ok((Containment::JobObject, Attached::JobObject(job))),
                Ok(None) => {
                    // Job assignment failed: fall back to the universal TreeWalk
                    // mechanism rather than silently yielding no containment. The
                    // root was spawned with CREATE_NEW_PROCESS_GROUP (root_flags),
                    // so `terminate`'s CTRL_BREAK still reaches the group.
                    return Ok((Containment::TreeWalk, Attached::TreeWalk(resolve_root_id(pid)?)));
                }
                Err(e) => return Err(Error::Containment { detail: e.to_string() }),
            }
        } else if prepared.mode.is_some() {
            // Nested member: it inherits the ancestor's job (or the root's tree-walk; no
            // new job is created), so it owns no teardown — the outermost root's job tears
            // the whole tree down.
            return Ok((Containment::Delegated, Attached::Delegated));
        }
    }

    // Uncontained (or unsupported platform).
    let _ = prepared;
    Ok((Containment::None, Attached::None))
}

#[cfg(test)]
#[path = "dispatch_tests.rs"]
mod dispatch_tests;