wasmer_wasix/os/task/
signal.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;

/// Signal handles...well...they process signals
pub trait SignalHandlerAbi
where
    Self: std::fmt::Debug,
{
    /// Processes a signal
    fn signal(&self, signal: u8) -> Result<(), SignalDeliveryError>;
}

pub type DynSignalHandlerAbi = dyn SignalHandlerAbi + Send + Sync + 'static;

#[derive(Debug)]
pub struct WasiSignalInterval {
    /// Signal that will be raised
    pub signal: Signal,
    /// Time between the signals
    pub interval: Duration,
    /// Flag that indicates if the signal should repeat
    pub repeat: bool,
    /// Last time that a signal was triggered
    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 {})
}