#[cfg(windows)]
use crate::containment::ContainRequest;
#[cfg(windows)]
use crate::error::Error;
#[cfg(windows)]
use windows::Win32::System::Threading::{
CREATE_BREAKAWAY_FROM_JOB, CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW, CREATE_SUSPENDED,
CREATE_UNICODE_ENVIRONMENT, DEBUG_ONLY_THIS_PROCESS, DEBUG_PROCESS, DETACHED_PROCESS, EXTENDED_STARTUPINFO_PRESENT,
};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct FlagsRequest {
#[cfg_attr(not(windows), allow(dead_code))]
pub no_window: bool,
#[cfg(windows)]
pub detached: bool,
#[cfg(windows)]
pub breakaway_from_job: bool,
#[cfg(windows)]
pub raw: u32,
}
#[cfg(windows)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SpawnBackend {
Std,
Raw,
}
#[cfg(windows)]
#[derive(Debug)]
pub(crate) struct WindowsSpawn {
pub creation_flags: u32,
pub marker_env: bool,
}
#[cfg(windows)]
struct Reserved {
bit: u32,
name: &'static str,
remedy: &'static str,
}
#[cfg(windows)]
fn reserved_table() -> [Reserved; 10] {
[
Reserved {
bit: DEBUG_PROCESS.0,
name: "DEBUG_PROCESS",
remedy: "no replacement: it makes the spawner a debugger, and the child stops on debug \
events nothing here services, so wait()/wait_tree() would hang",
},
Reserved {
bit: DEBUG_ONLY_THIS_PROCESS.0,
name: "DEBUG_ONLY_THIS_PROCESS",
remedy: "no replacement: same reason as DEBUG_PROCESS",
},
Reserved {
bit: CREATE_SUSPENDED.0,
name: "CREATE_SUSPENDED",
remedy: "no replacement today, a suspended-spawn window is being designed in cosca#49: \
cosca suspends and resumes a contained root itself, so a caller's suspend \
would be resumed silently on a contained spawn and never resumed at all on \
an uncontained one",
},
Reserved {
bit: DETACHED_PROCESS.0,
name: "DETACHED_PROCESS",
remedy: "use detached()",
},
Reserved {
bit: CREATE_NEW_CONSOLE.0,
name: "CREATE_NEW_CONSOLE",
remedy: "no replacement: measured to give the child its own VISIBLE console window, \
overriding a requested window suppression",
},
Reserved {
bit: CREATE_NEW_PROCESS_GROUP.0,
name: "CREATE_NEW_PROCESS_GROUP",
remedy: "use contain(), which is what makes the child's group addressable",
},
Reserved {
bit: CREATE_UNICODE_ENVIRONMENT.0,
name: "CREATE_UNICODE_ENVIRONMENT",
remedy: "no replacement: both backends supply it structurally — the raw one always \
builds a UTF-16 environment block, and std ORs the bit in regardless of what \
it was given",
},
Reserved {
bit: EXTENDED_STARTUPINFO_PRESENT.0,
name: "EXTENDED_STARTUPINFO_PRESENT",
remedy: "no replacement: it announces a structure only the spawn backend can supply",
},
Reserved {
bit: CREATE_BREAKAWAY_FROM_JOB.0,
name: "CREATE_BREAKAWAY_FROM_JOB",
remedy: "use breakaway_from_job(), which also classifies the failure a forbidding \
ambient job produces",
},
Reserved {
bit: CREATE_NO_WINDOW.0,
name: "CREATE_NO_WINDOW",
remedy: "use no_window()",
},
]
}
#[cfg(windows)]
fn reserved_bits_in(raw: u32) -> Vec<Reserved> {
let mut hits: Vec<Reserved> = reserved_table().into_iter().filter(|r| raw & r.bit != 0).collect();
hits.sort_by_key(|r| r.bit);
hits
}
#[cfg(windows)]
pub(crate) fn windows_spawn(
contain: &ContainRequest,
flags: FlagsRequest,
is_root: bool,
backend: SpawnBackend,
) -> Result<WindowsSpawn, Error> {
let offending = reserved_bits_in(flags.raw);
if !offending.is_empty() {
let named = offending
.iter()
.map(|r| format!("{} ({})", r.name, r.remedy))
.collect::<Vec<_>>()
.join("; ");
return Err(Error::Unsupported {
op: "creation_flags".into(),
platform: "windows",
detail: format!("creation flags cosca manages itself cannot be set through the raw hatch: {named}"),
});
}
if flags.breakaway_from_job && contain.mode.is_some() {
return Err(Error::Unsupported {
op: "breakaway_from_job() with contain()".into(),
platform: "windows",
detail: "cosca's containment is a job object the child must be inside for the tree \
teardown it reports to be true, and a nested contained spawn's containment IS \
inheriting the ancestor's job. Drop one of the two."
.into(),
});
}
let setup = crate::containment::dispatch::windows_contain_setup(contain, is_root);
let mut creation_flags = setup.creation_flags | flags.raw;
if flags.no_window {
creation_flags |= CREATE_NO_WINDOW.0;
}
if flags.detached {
creation_flags |= DETACHED_PROCESS.0;
}
if flags.breakaway_from_job {
creation_flags |= CREATE_BREAKAWAY_FROM_JOB.0;
}
if backend == SpawnBackend::Raw {
creation_flags |= CREATE_UNICODE_ENVIRONMENT.0 | EXTENDED_STARTUPINFO_PRESENT.0;
}
Ok(WindowsSpawn {
creation_flags,
marker_env: setup.marker_env,
})
}
#[cfg(windows)]
pub(crate) fn classify_spawn_denial(
err: Error,
flags: FlagsRequest,
job: crate::containment::windows::JobBreakaway,
) -> Error {
use crate::containment::windows::JobBreakaway;
use windows::Win32::Foundation::ERROR_ACCESS_DENIED;
if !flags.breakaway_from_job {
return err;
}
let Error::Io(io) = &err else {
return err;
};
let win32 = ERROR_ACCESS_DENIED.0 as i32;
let hresult = windows::core::HRESULT::from_win32(ERROR_ACCESS_DENIED.0).0;
if !matches!(io.raw_os_error(), Some(c) if c == win32 || c == hresult) {
return err;
}
if job != JobBreakaway::Forbidden {
return err;
}
Error::Containment {
detail: "the spawn was refused with ACCESS_DENIED, and this process's job object sets \
neither JOB_OBJECT_LIMIT_BREAKAWAY_OK nor JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK, \
so it forbids the breakaway_from_job() this spawn requested. Windows evaluates \
that check before it resolves the image, so this is the FIRST thing refused and \
not necessarily the only problem: dropping the request may reveal a different \
failure rather than making the spawn work. Only whoever created that job can \
permit breakaway — and if this process was itself spawned by cosca with \
contain(), the job is cosca's own, which sets neither limit and whose limits a \
member process cannot relax."
.into(),
}
}
#[cfg(windows)]
pub(crate) fn classify_spawn_syscall_error(err: Error, flags: FlagsRequest) -> Error {
classify_spawn_denial(err, flags, crate::containment::windows::probe_job_breakaway())
}
#[cfg(all(test, windows))]
#[path = "flags_tests.rs"]
mod flags_tests;