use std::{sync::Arc, time::Duration};
use wasmer_wasix_types::types::Signal;
#[derive(thiserror::Error, Debug)]
#[error("Signal could not be delivered")]
pub struct SignalDeliveryError;
pub trait SignalHandlerAbi
where
Self: std::fmt::Debug,
{
fn signal(&self, signal: u8) -> Result<(), SignalDeliveryError>;
}
pub type DynSignalHandlerAbi = dyn SignalHandlerAbi + Send + Sync + 'static;
#[derive(Debug)]
pub struct WasiSignalInterval {
pub signal: Signal,
pub interval: Duration,
pub repeat: bool,
pub last_signal: u128,
}
pub fn default_signal_handler() -> Arc<DynSignalHandlerAbi> {
#[derive(Debug)]
struct DefaultHandler {}
impl SignalHandlerAbi for DefaultHandler {
fn signal(&self, signal: u8) -> Result<(), SignalDeliveryError> {
if let Ok(signal) = TryInto::<Signal>::try_into(signal) {
match signal {
Signal::Sigkill
| Signal::Sigterm
| Signal::Sigabrt
| Signal::Sigquit
| Signal::Sigint
| Signal::Sigstop => {
tracing::debug!("handling terminate signal");
std::process::exit(1);
}
signal => tracing::info!("unhandled signal - {:?}", signal),
}
} else {
tracing::info!("unknown signal - {}", signal)
}
Ok(())
}
}
Arc::new(DefaultHandler {})
}