use crate::error::Error;
#[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,
pid: u32,
signal_root: impl FnOnce(crate::signal::Sig) -> Result<(), crate::error::Error>,
) -> Result<(), crate::error::Error> {
#[cfg(unix)]
let _ = pid;
#[cfg(windows)]
drop(signal_root);
match mechanism {
#[cfg(unix)]
GracefulMechanism::Process => signal_root(crate::signal::Sig::Term),
#[cfg(windows)]
GracefulMechanism::Process => Err(crate::error::Error::terminate_unsupported()),
#[cfg(windows)]
GracefulMechanism::ConsoleGroup | GracefulMechanism::OtherConsoleGroup => {
crate::containment::windows::terminate(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(),
}),
}
}
pub(crate) fn after_grace(watch: Result<bool, Error>, kill: impl FnOnce() -> Result<(), Error>) -> Result<(), Error> {
if matches!(watch, Ok(true)) {
return Ok(());
}
if let Err(ref e) = watch {
log::debug!("graceful_shutdown watch error before kill escalation (subsumed): {e}");
}
kill()?;
watch.map(|_| ())
}
pub(crate) fn after_tree_grace(
watch: Result<bool, Error>,
sweep: impl FnOnce() -> Result<(), Error>,
) -> Result<(), Error> {
if let Err(ref e) = watch {
log::debug!("graceful_shutdown_tree watch error before the sweep (may be subsumed): {e}");
}
sweep()?;
watch.map(|_| ())
}
#[cfg(test)]
#[path = "graceful_tests.rs"]
mod graceful_tests;