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
//! Async (tokio) raw `CreateProcessW` spawn backend.
//!
//! The async mirror of [`crate::child::spawn::windows_raw`]: it loads an `executable()`
//! independently of argv[0] (the case std/tokio cannot express on Windows), wires arbitrary
//! descriptors (fd >= 3, via the MSVCRT `lpReserved2` table), and applies containment (Job Object /
//! TreeWalk) — the full sync feature set. It reuses the sync backend's `CreateProcessW` FFI verbatim
//! (`spawn_step`/`create_process`, the fd-table encode + cap, the STARTUPINFOEXW/HANDLE_LIST build,
//! the containment decision, the lock/close discipline, the error-teardown) and differs only where
//! async demands it: piped ends come from the tokio overlapped-named-pipe machinery (std slots via
//! the stdin/stdout/stderr accessors, fd >= 3 via fd_read_end/fd_write_end), and the owned child is
//! a [`RawAsyncChild`] whose waits run on the blocking pool over a cancellable handle wait.

use std::collections::BTreeMap;
use std::ffi::OsString;
use std::os::windows::io::{AsRawHandle, OwnedHandle};
use std::process::ExitStatus;
use std::sync::Arc;

use windows::Win32::Foundation::{ERROR_ACCESS_DENIED, HANDLE, WAIT_OBJECT_0, WAIT_TIMEOUT};
use windows::Win32::System::Threading::{
    TerminateProcess, WaitForSingleObject, INFINITE, STARTF_USESTDHANDLES, STARTUPINFOEXW,
};

use crate::child::spawn::windows_raw as sync_raw;
use crate::child::spawn::{attach_or_fault, dup, resolve_identity, resolve_non_merge, spawn_lock};
use crate::command::{Command, EnvOp};
use crate::error::Error;
use crate::stdio::{Fd, ResolvedStdio};
use crate::tokio::child::{Child, ProcSource};
use crate::tokio::stdio::OwnedStd;
use crate::wait::backend::{new_cancel_event, signal_cancel};

use sync_raw::{exit_status, wait_handle_or_cancel, AttributeList, WaitOutcome};

/// A raw `CreateProcessW` child driven from tokio. The process handle lives behind an `Arc` so a
/// dropped `wait()` future — or a dropped child — never closes a handle a blocking watcher is still
/// parked on: the blocking task holds its own clone until it returns. Async waits run on the
/// blocking pool over the shared cancellable handle wait; a cancel event (signaled by the future's
/// drop guard) releases a parked wait promptly.
#[derive(Debug)]
pub(crate) struct RawAsyncChild {
    proc: Arc<OwnedHandle>,
    /// The child's OS pid — retained for diagnostics (the `Child`'s stable `ProcessId` is the
    /// source of truth for containment queries).
    pid: u32,
    /// Memoized once the child has exited, so a second `wait`/`try_wait` is immediate and cannot
    /// re-read a status off a (post-close) recycled handle.
    exited: Option<ExitStatus>,
    /// A runas (UAC-elevated) child a lower-integrity parent may be unable to terminate. Drives
    /// [`start_kill`](RawAsyncChild::start_kill)'s `can_terminate` probe, which is the sole owner
    /// of "this child is genuinely unkillable" — nothing waits on one.
    runas: bool,
    #[cfg(test)]
    observer: Option<WaitObserver>,
}

impl RawAsyncChild {
    pub(crate) fn new(proc: OwnedHandle, pid: u32) -> RawAsyncChild {
        RawAsyncChild {
            proc: Arc::new(proc),
            pid,
            exited: None,
            runas: false,
            #[cfg(test)]
            observer: None,
        }
    }

    /// A runas (UAC-elevated) child, whose kill may be refused (see the `runas` field).
    pub(crate) fn new_runas(proc: OwnedHandle, pid: u32) -> RawAsyncChild {
        RawAsyncChild {
            proc: Arc::new(proc),
            pid,
            exited: None,
            runas: true,
            #[cfg(test)]
            observer: None,
        }
    }

    fn handle(&self) -> HANDLE {
        HANDLE(self.proc.as_raw_handle())
    }

    /// Block until the child exits, returning its status. Runs the blocking handle wait on the
    /// blocking pool; a `CancelGuard` in this future signals a cancel event on drop so an aborted
    /// or timed-out wait releases the parked watcher at once.
    pub(crate) async fn wait(&mut self) -> Result<ExitStatus, Error> {
        loop {
            if let Some(status) = self.exited {
                return Ok(status);
            }
            // Arc-owned handles: the blocking task holds clones for its whole lifetime, so dropping
            // this future (or the child) never closes a handle the task is parked on.
            let cancel = Arc::new(new_cancel_event()?);
            let proc = Arc::clone(&self.proc);
            let cancel_for_task = Arc::clone(&cancel);
            #[cfg(test)]
            let observer = self.observer.take();
            let task = ::tokio::task::spawn_blocking(move || {
                #[cfg(test)]
                let (started, outcome_tx) = match observer {
                    Some(obs) => (Some(obs.started), Some(obs.outcome)),
                    None => (None, None),
                };
                // The task is on the blocking pool and about to park on the handle wait.
                #[cfg(test)]
                if let Some(tx) = started {
                    let _ = tx.send(());
                }
                let result = wait_handle_or_cancel(
                    HANDLE(proc.as_raw_handle()),
                    Some(HANDLE(cancel_for_task.as_raw_handle())),
                );
                #[cfg(test)]
                if let (Some(tx), Ok(outcome)) = (outcome_tx, result.as_ref()) {
                    let _ = tx.send(*outcome);
                }
                result
            });
            // Signals the cancel event if THIS future is dropped before the blocking wait returns,
            // releasing the parked watcher promptly. Harmless after completion (manual-reset event,
            // nothing waits on it any more). The Arcs above keep both handles live until the task
            // returns, so the signal races nothing.
            let _guard = CancelGuard(cancel);
            let outcome = match task.await {
                Ok(o) => o,
                // A JoinError (task panic / runtime shutdown) is not an exit — surface it.
                Err(_e) => return Err(Error::Io(std::io::Error::other("raw async wait task failed"))),
            };
            match outcome {
                Ok(WaitOutcome::Exited) => {
                    let status = exit_status(self.handle()).map_err(Error::Io)?;
                    self.exited = Some(status);
                    return Ok(status);
                }
                // Only `CancelGuard::drop` signals the cancel event, and it runs solely when THIS
                // future is dropped — in which case this continuation never executes. A defensive
                // re-wait (fresh event) preserves the "resolved ⇒ exited" postcondition if that
                // ever broke; never a false exit.
                Ok(WaitOutcome::Cancelled) => {
                    debug_assert!(false, "raw async wait resolved Cancelled while its future was live");
                    continue;
                }
                Err(e) => return Err(Error::Io(e)),
            }
        }
    }

    /// Exit status if the child has already exited (non-blocking), memoizing it.
    pub(crate) fn try_wait(&mut self) -> Result<Option<ExitStatus>, Error> {
        if let Some(status) = self.exited {
            return Ok(Some(status));
        }
        // SAFETY: our live, owned process handle; a zero timeout polls without blocking.
        let r = unsafe { WaitForSingleObject(self.handle(), 0) };
        if r == WAIT_OBJECT_0 {
            let status = exit_status(self.handle()).map_err(Error::Io)?;
            self.exited = Some(status);
            Ok(Some(status))
        } else if r == WAIT_TIMEOUT {
            Ok(None)
        } else {
            Err(Error::Io(std::io::Error::last_os_error()))
        }
    }

    /// Signal a hard kill (does not reap). Signal-only, so it never blocks.
    pub(crate) fn start_kill(&mut self) -> Result<(), Error> {
        // SAFETY: our live, owned process handle; exit code 1 is the forced-kill code.
        match unsafe { TerminateProcess(self.handle(), 1) } {
            Ok(()) => Ok(()),
            // `ACCESS_DENIED` has two causes: (a) our own child is already exiting (the OS teardown
            // window signals the denial before the object is signaled — a spurious kill error), or
            // (b) a runas child is genuinely higher-integrity than us. A static `can_terminate`
            // probe (shared with the sync `RawChild`) separates them WITHOUT racing a `try_wait`.
            // Only (b) is a real denial → surface the `Io` error so `Child::kill` maps it to the
            // typed `Unkillable`; (a) is Ok (signal-only, so never block here).
            Err(e) if e.code() == windows::core::HRESULT::from_win32(ERROR_ACCESS_DENIED.0) => {
                if self.runas && !sync_raw::can_terminate(self.pid) {
                    Err(Error::Io(std::io::Error::from_raw_os_error(
                        ERROR_ACCESS_DENIED.0 as i32,
                    )))
                } else {
                    Ok(())
                }
            }
            Err(e) => Err(Error::Io(e.into())),
        }
    }

    /// `true` once this backend has collected the child's exit status, so no wait remains.
    pub(crate) fn is_reaped(&self) -> bool {
        self.exited.is_some()
    }

    /// Wait for the child's exit; the caller has already signalled it. **Never kills:** a second
    /// `TerminateProcess` on an already-exiting child returns `ACCESS_DENIED`, and the classified
    /// kill decision (including a genuinely-undeniable runas child) was already made by
    /// [`start_kill`](RawAsyncChild::start_kill)'s `can_terminate` probe.
    pub(crate) fn wait_and_reap(&mut self) {
        if self.exited.is_some() {
            return;
        }
        // SAFETY: our live, owned process handle; INFINITE is bounded by the caller's kill.
        let waited = unsafe { WaitForSingleObject(self.handle(), INFINITE) };
        debug_assert!(
            waited == WAIT_OBJECT_0,
            "raw async teardown did not observe child {} exit: {waited:?}",
            self.pid
        );
        let _ = waited;
    }

    /// Install the per-instance test wait observer on THIS child (see `WaitObserver`).
    #[cfg(test)]
    pub(crate) fn set_observer(
        &mut self,
        started: ::tokio::sync::oneshot::Sender<()>,
        outcome: ::tokio::sync::oneshot::Sender<WaitOutcome>,
    ) {
        self.observer = Some(WaitObserver { started, outcome });
    }
}

/// Signals the cancel event when the `wait()` future is dropped (aborted / timed-out), releasing
/// the parked blocking watcher. Harmless once the wait has already returned.
struct CancelGuard(Arc<OwnedHandle>);

impl Drop for CancelGuard {
    fn drop(&mut self) {
        // `signal_cancel` asserts loudly on failure (see wait/windows.rs); Drop cannot propagate.
        signal_cancel(&self.0);
    }
}

/// Per-instance test observable seam: the blocking wait closure signals when it has parked
/// (`started`) and reports its `WaitOutcome` — on THIS child's channels only, so a parallel test's
/// wait never fires into another test's receivers (a process-global seam would). Injected via
/// `RawAsyncChild::set_observer`; consumed on the next `wait()`.
#[cfg(test)]
#[derive(Debug)]
struct WaitObserver {
    started: ::tokio::sync::oneshot::Sender<()>,
    outcome: ::tokio::sync::oneshot::Sender<WaitOutcome>,
}

/// Spawn `cmd` via the async raw backend: an `executable()` loaded independently of argv[0], with
/// arbitrary descriptors (fd >= 3) and containment — the full sync feature set. Reuses the sync
/// backend's FFI (fd-table + HANDLE_LIST + containment decision + lock/close + error-teardown).
pub(crate) fn spawn_raw(cmd: &Command, fds: BTreeMap<Fd, ResolvedStdio>, kill_on_drop: bool) -> Result<Child, Error> {
    // Batch reject on the program token, resolve the executable, NUL-check, build the command line
    // — all shared verbatim with the sync raw backend.
    sync_raw::reject_batch_program(cmd)?;
    let image = cmd
        .executable_path()
        .map(sync_raw::resolve::resolve_executable)
        .transpose()?;
    if let Some(p) = &image {
        sync_raw::resolve::ensure_no_nul_wide(p.as_os_str())?;
    }
    if let Some(c) = cmd.cwd() {
        sync_raw::resolve::ensure_no_nul_wide(c.as_os_str())?;
    }
    let app_name: Option<Vec<u16>> = image.as_ref().map(|p| sync_raw::to_wide_nul(p.as_os_str()));
    let mut cmdline = sync_raw::raw_program_and_line(cmd)?; // each token NUL-checked
    cmdline.push(0);

    // Containment: mirror the sync raw backend's pre-spawn decision. Uncontained keeps the defaults
    // (flags 0, `mode: None`/`is_root: false`); a Strongest root spawns CREATE_SUSPENDED and is
    // job-assigned + resumed in `attach_or_fault`.
    let req = cmd.contain_request();
    let marker_present = std::env::var_os(crate::containment::NESTED_ENV).is_some();
    let is_root = !crate::containment::dispatch::is_nested(marker_present);
    // Composed and validated BEFORE `clear_std_handle_inheritance`, which is a process-global
    // `SetHandleInformation` on THIS process's std handles that nothing undoes: a refused spawn
    // must not have mutated the parent.
    let plan = crate::command::flags::windows_spawn(
        &req,
        *cmd.flags_request(),
        is_root,
        crate::command::flags::SpawnBackend::Raw,
    )?;
    let marker_env = plan.marker_env;
    if req.mode.is_some() {
        crate::containment::windows::clear_std_handle_inheritance();
    }

    // Append the inherited root marker AFTER the user's env ops so it survives a user `env_clear()`
    // (mirrors the sync path).
    let env_block = if marker_env {
        let mut ops = cmd.env_ops().to_vec();
        ops.push(EnvOp::Set(
            OsString::from(crate::containment::NESTED_ENV),
            OsString::from("1"),
        ));
        sync_raw::resolve::build_env_block(&ops)?
    } else {
        sync_raw::resolve::build_env_block(cmd.env_ops())?
    };
    let cwd_w = cmd.cwd().map(|c| sync_raw::to_wide_nul(c.as_os_str()));

    // Cap the MSVCRT fd-table to the WORD-sized `cbReserved2` field BEFORE allocating any pipes.
    sync_raw::ensure_fd_table_fits(&fds)?;

    // Resolve 0/1/2 (always) plus any configured fd >= 3 to child handles: piped slots via the
    // tokio overlapped-pipe machinery (we own the async parent ends — std slots via the
    // stdin/stdout/stderr accessors, fd >= 3 via fd_read_end/fd_write_end), merges as dups, the rest
    // (inherit/file/null) via the shared core (which rejects inherit on fd >= 3).
    let (child_ends, owned_std, fd_pipes) = resolve_raw_ends(&fds)?;

    // Classify + encode the dense MSVCRT fd-table the child CRT reads back from `lpReserved2` — the
    // SAME layout + single HANDLE_LIST source as the sync backend.
    let table = sync_raw::build_fd_table(&child_ends)?;

    // STARTUPINFOEXW: STARTF_USESTDHANDLES + hStd* for 0/1/2; `lpReserved2` carries the fd-table so
    // the child CRT recovers fd >= 3; the HANDLE_LIST (`table.handles`) scopes inheritance AND backs
    // EXTENDED_STARTUPINFO_PRESENT. `table` (`bytes`) is kept alive until after CreateProcessW.
    let mut si = STARTUPINFOEXW::default();
    si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
    si.StartupInfo.hStdInput = HANDLE(child_ends[&Fd::STDIN].as_raw_handle());
    si.StartupInfo.hStdOutput = HANDLE(child_ends[&Fd::STDOUT].as_raw_handle());
    si.StartupInfo.hStdError = HANDLE(child_ends[&Fd::STDERR].as_raw_handle());
    si.StartupInfo.cbReserved2 = table.bytes.len() as u16; // fits: capped above
    si.StartupInfo.lpReserved2 = table.bytes.as_ptr() as *mut u8;
    // SINGLE handle source: `table.handles` is 0/1/2 + fd >= 3, each a distinct fresh dup (its
    // 0/1/2 entries ARE the hStd* handles), so no duplicate reaches the list.
    let all_handles: &[HANDLE] = &table.handles;
    let attr = AttributeList::build(all_handles)?;
    si.lpAttributeList = attr.as_ptr();
    // The complete word, composed above by the ONE function all three backends share — the two
    // structural bits this backend cannot spawn without included. Nothing ORs into it here.
    let flags = plan.creation_flags;

    // UNDER THE LOCK: mark the listed child ends inheritable, spawn, then CLOSE the child ends and
    // the attribute list BEFORE the guard releases on EVERY path (mirrors the sync raw backend —
    // an early `?` would drop the inner guard before `child_ends`/`attr`, exposing inheritable
    // handles to a concurrent spawn).
    let spawned = {
        let _guard = spawn_lock();
        let r = sync_raw::spawn_step(
            all_handles,
            app_name.as_deref(),
            &mut cmdline,
            &mut si,
            &env_block,
            &cwd_w,
            flags,
            *cmd.flags_request(),
        );
        drop(child_ends); // close the child ends inside the lock, on success AND error
        drop(attr); // DeleteProcThreadAttributeList before the guard releases
        r
    };
    let (proc, pid) = spawned?;

    // Identity read + attach BEFORE building `Child`, with the REAL mode + is_root (Job Object for a
    // Strongest root, else TreeWalk/Delegated), and the SAME kill+reap error-teardown as the
    // sync-raw/std path (dropping the OwnedHandle alone neither kills nor reaps on Windows). Spawn +
    // attach + identity are synchronous — no await before the identity read, so the runtime cannot
    // park and reap in between.
    let prepared = crate::containment::Prepared {
        mode: req.mode,
        is_root,
        // From the COMPOSED word, which carries the caller's flags as well as the containment
        // decision: a derivation reading only the containment half would report an in-process
        // route for a child this spawn deliberately put in another console.
        graceful: crate::containment::windows::mechanism_from_flags(flags),
    };
    let raw_handle = proc.as_raw_handle();
    let attachment = match attach_or_fault(pid, raw_handle, prepared) {
        Ok(v) => v,
        Err(e) => {
            sync_raw::raw_spawn_teardown(proc, pid);
            return Err(e);
        }
    };
    let id = match resolve_identity(pid) {
        crate::identity::Resolved::Found(id) => id,
        other => {
            sync_raw::raw_spawn_teardown(proc, pid);
            return Err(crate::child::spawn::spawn_identity_error(other));
        }
    };

    Ok(Child::from_parts(
        ProcSource::Raw(RawAsyncChild::new(proc, pid)),
        id,
        kill_on_drop,
        attachment,
        fd_pipes,
        owned_std,
    ))
}

/// The raw backend's resolved stdio ends: the child's handle ends (0/1/2 + fd >= 3) keyed by slot,
/// plus the async parent ends we own — std-slot pipes (for the stdin/stdout/stderr accessors) and
/// fd >= 3 pipes (for fd_read_end/fd_write_end), each keyed by slot.
type RawEnds = (
    BTreeMap<Fd, OwnedHandle>,
    BTreeMap<Fd, OwnedStd>,
    BTreeMap<Fd, OwnedStd>,
);

/// Resolve the child's handle ends (0/1/2 + fd >= 3) for the raw backend, plus the async parent ends
/// we own. Piped slots use OUR overlapped pipe (tokio owns none here — there is no
/// `tokio::process::Child`); merges dup their target's child end; the rest resolve via the shared
/// core (which rejects inherit on fd >= 3).
fn resolve_raw_ends(fds: &BTreeMap<Fd, ResolvedStdio>) -> Result<RawEnds, Error> {
    // Slots to resolve: 0/1/2 always, plus every configured fd >= 3.
    let slots: Vec<Fd> = {
        let mut v = vec![Fd::STDIN, Fd::STDOUT, Fd::STDERR];
        v.extend(fds.keys().copied().filter(|f| f.raw() >= 3));
        v
    };
    // Reject chained merges (single-level indirection only), mirroring the shared resolver.
    for &slot in &slots {
        if let Some(ResolvedStdio::Merge(target)) = fds.get(&slot) {
            if matches!(fds.get(target), Some(ResolvedStdio::Merge(_))) {
                return Err(Error::Unsupported {
                    op: format!("merge {slot} -> {target} -> <another merge>"),
                    platform: "windows",
                    detail: "chained merges (merge-to-merge) are not supported; redirect to a \
                             concrete slot (pipe/file/null/inherit)"
                        .into(),
                });
            }
        }
    }
    let mut child_ends: BTreeMap<Fd, OwnedHandle> = BTreeMap::new();
    let mut owned_std: BTreeMap<Fd, OwnedStd> = BTreeMap::new();
    let mut fd_pipes: BTreeMap<Fd, OwnedStd> = BTreeMap::new();
    // First pass: non-merge slots. Piped slots use our overlapped pipe (async parent end stashed —
    // std slots for the stdin/stdout/stderr accessors, fd >= 3 for fd_read_end/fd_write_end); the
    // rest (inherit/file/null/unconfigured std) resolve via the shared core.
    for &slot in &slots {
        match fds.get(&slot) {
            Some(ResolvedStdio::Merge(_)) => continue, // second pass
            Some(ResolvedStdio::Pipe(dir)) => {
                let (child_end, parent) = crate::tokio::stdio::owned_overlapped_pipe(*dir)?;
                child_ends.insert(slot, child_end);
                if slot.raw() < 3 {
                    owned_std.insert(slot, parent);
                } else {
                    fd_pipes.insert(slot, parent);
                }
            }
            other => {
                let (child_end, parent) = resolve_non_merge(slot, other)?;
                debug_assert!(parent.is_none(), "a raw non-pipe slot yields no parent end");
                child_ends.insert(slot, child_end);
            }
        }
    }
    // Second pass: each merge dups its target's already-resolved child end.
    for &slot in &slots {
        if let Some(ResolvedStdio::Merge(target)) = fds.get(&slot) {
            let src = child_ends.get(target).ok_or_else(|| Error::Unsupported {
                op: format!("merge {slot} -> {target}"),
                platform: "windows",
                detail: "merge target descriptor is not configured".into(),
            })?;
            child_ends.insert(slot, dup(src)?);
        }
    }
    Ok((child_ends, owned_std, fd_pipes))
}

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