use crate::error::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GracefulMechanism {
Process,
ConsoleGroup,
OtherConsoleGroup,
Unknown,
None,
}
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(),
source: None,
})
}
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(),
source: None,
}),
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(),
source: None,
}),
}
}
pub(crate) fn joined(call: &str, first: Error, peers: impl IntoIterator<Item = Error>) -> Error {
first.with_peers_or_warn(call, peers)
}
pub(crate) fn after_grace(
call: impl FnOnce() -> String,
watch: Result<bool, Error>,
kill: impl FnOnce() -> Result<(), Error>,
) -> Result<Option<Error>, Error> {
if matches!(watch, Ok(true)) {
return Ok(None);
}
let watch = watch.err();
match kill() {
Ok(()) => Ok(watch),
Err(e) => Err(joined(&call(), e, watch)),
}
}
pub(crate) fn after_tree_grace(
call: &str,
watch: Result<bool, Error>,
sweep: impl FnOnce() -> Result<(), Error>,
) -> Result<(), Error> {
after_sweep(call, sweep(), watch)
}
pub(crate) fn after_sweep(call: &str, sweep: Result<(), Error>, watch: Result<bool, Error>) -> Result<(), Error> {
match sweep {
Ok(()) => watch.map(|_| ()),
Err(e) => Err(joined(call, e, watch.err())),
}
}
pub(crate) fn after_reap(
call: impl FnOnce() -> String,
waited: Result<std::process::ExitStatus, Error>,
refusal: Option<Error>,
watch: Option<Error>,
) -> Result<std::process::ExitStatus, Error> {
match waited {
Ok(status) => watch.map_or(Ok(status), Err),
Err(e) => Err(joined(&call(), e, refusal.into_iter().chain(watch))),
}
}
#[cfg(test)]
#[path = "graceful_tests.rs"]
mod graceful_tests;