#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GracefulMechanism {
Process,
ConsoleGroup,
OtherConsoleGroup,
Unknown,
None,
}
#[cfg(all(windows, feature = "tokio"))]
impl GracefulMechanism {
pub(crate) fn addresses_a_bare_pid(self) -> bool {
match self {
GracefulMechanism::ConsoleGroup | GracefulMechanism::OtherConsoleGroup => true,
GracefulMechanism::Process => false,
GracefulMechanism::Unknown | GracefulMechanism::None => false,
}
}
}
impl std::fmt::Display for GracefulMechanism {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
GracefulMechanism::Process => "process",
GracefulMechanism::ConsoleGroup => "own console group",
GracefulMechanism::OtherConsoleGroup => "own console group in another console",
GracefulMechanism::Unknown => "unknown",
GracefulMechanism::None => "none",
};
f.write_str(s)
}
}
pub(crate) fn signal(mechanism: GracefulMechanism, id: crate::identity::ProcessId) -> Result<(), crate::error::Error> {
match mechanism {
GracefulMechanism::Process => crate::wait::terminate(id),
#[cfg(windows)]
GracefulMechanism::ConsoleGroup | GracefulMechanism::OtherConsoleGroup => {
crate::containment::windows::terminate(id.pid())
}
#[cfg(not(windows))]
GracefulMechanism::ConsoleGroup | GracefulMechanism::OtherConsoleGroup => {
debug_assert!(false, "a console-group mechanism is unconstructible off Windows");
Err(crate::error::Error::Unsupported {
op: "graceful terminate".into(),
platform: std::env::consts::OS,
detail: "internal invariant: a console-group mechanism reached signal off Windows".into(),
})
}
GracefulMechanism::Unknown => Err(crate::error::Error::Unsupported {
op: "graceful terminate".into(),
platform: std::env::consts::OS,
detail: "cosca did not create this child — a system elevation service did — so \
whether it leads a console process group of its own is unknown, and an \
event addressed to its pid would reach its leader's whole group instead. \
Use kill() for a hard teardown."
.into(),
}),
GracefulMechanism::None => Err(crate::error::Error::Unsupported {
op: "graceful terminate".into(),
platform: std::env::consts::OS,
detail: "this child leads no console process group of its own, so no console control \
event can be addressed to it by any process; group leadership is fixed at \
creation. Spawn it with contain() to give it one, or use kill() for a hard \
teardown."
.into(),
}),
}
}
#[cfg(test)]
#[path = "graceful_tests.rs"]
mod graceful_tests;