use thiserror::Error;
use tokio_util::sync::CancellationToken;
#[cfg(unix)]
pub mod unix;
#[cfg(windows)]
pub mod windows;
#[cfg(test)]
mod tests;
pub fn send_sigterm(pid: u32) -> std::io::Result<()> {
#[cfg(unix)]
{
unix::send_sigterm(pid)
}
#[cfg(windows)]
{
windows::send_sigterm(pid)
}
}
pub fn pid_alive(pid: u32) -> bool {
#[cfg(unix)]
{
unix::pid_alive(pid)
}
#[cfg(windows)]
{
windows::pid_alive(pid)
}
}
#[derive(Debug, Error)]
pub enum SignalError {
#[error("failed to install OS signal handler: {0}")]
Install(#[from] std::io::Error),
}
#[derive(Debug, Clone)]
pub struct ShutdownSignals {
token: CancellationToken,
}
impl ShutdownSignals {
pub fn token(&self) -> CancellationToken {
self.token.clone()
}
}
pub fn install_shutdown_handler() -> Result<ShutdownSignals, SignalError> {
let token = CancellationToken::new();
#[cfg(unix)]
{
unix::install(token.clone())?;
}
#[cfg(windows)]
{
windows::install(token.clone())?;
}
Ok(ShutdownSignals { token })
}