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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{FromInner, HandleTrait, Inner, IntoInner};
use std::convert::TryFrom;
use uv::{uv_signal_init, uv_signal_start, uv_signal_start_oneshot, uv_signal_stop, uv_signal_t};

callbacks! {
    pub SignalCB(handle: SignalHandle, signum: i32);
}

/// Additional data stored on the handle
#[derive(Default)]
pub(crate) struct SignalDataFields<'a> {
    signal_cb: SignalCB<'a>,
}

/// Callback for uv_signal_start
extern "C" fn uv_signal_cb(handle: *mut uv_signal_t, signum: std::os::raw::c_int) {
    let dataptr = crate::Handle::get_data(uv_handle!(handle));
    if !dataptr.is_null() {
        unsafe {
            if let super::SignalData(d) = &mut (*dataptr).addl {
                d.signal_cb.call(handle.into_inner(), signum as _);
            }
        }
    }
}

/// Signal handles implement Unix style signal handling on a per-event loop bases.
///
/// Windows notes: Reception of some signals is emulated:
///   * SIGINT is normally delivered when the user presses CTRL+C. However, like on Unix, it is not
///     generated when terminal raw mode is enabled.
///   * SIGBREAK is delivered when the user pressed CTRL + BREAK.
///   * SIGHUP is generated when the user closes the console window. On SIGHUP the program is given
///     approximately 10 seconds to perform cleanup. After that Windows will unconditionally
///     terminate it.
///   * SIGWINCH is raised whenever libuv detects that the console has been resized. When a libuv
///     app is running under a console emulator, or when a 32-bit libuv app is running on 64-bit
///     system, SIGWINCH will be emulated. In such cases SIGWINCH signals may not always be
///     delivered in a timely manner. For a writable TtyHandle libuv will only detect size changes
///     when the cursor is moved. When a readable TtyHandle is used, resizing of the console buffer
///     will be detected only if the handle is in raw mode and is being read.
///   * Watchers for other signals can be successfully created, but these signals are never
///     received. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV, SIGTERM and SIGKILL.
///   * Calls to raise() or abort() to programmatically raise a signal are not detected by libuv;
///     these will not trigger a signal watcher.
///
/// Unix notes
///   * SIGKILL and SIGSTOP are impossible to catch.
///   * Handling SIGBUS, SIGFPE, SIGILL or SIGSEGV via libuv results into undefined behavior.
///   * SIGABRT will not be caught by libuv if generated by abort(), e.g. through assert().
///   * On Linux SIGRT0 and SIGRT1 (signals 32 and 33) are used by the NPTL pthreads library to
///     manage threads. Installing watchers for those signals will lead to unpredictable behavior
///     and is strongly discouraged. Future versions of libuv may simply reject them.
#[derive(Clone, Copy)]
pub struct SignalHandle {
    handle: *mut uv_signal_t,
}

impl SignalHandle {
    /// Create and initialize a new signal handle
    pub fn new(r#loop: &crate::Loop) -> crate::Result<SignalHandle> {
        let layout = std::alloc::Layout::new::<uv_signal_t>();
        let handle = unsafe { std::alloc::alloc(layout) as *mut uv_signal_t };
        if handle.is_null() {
            return Err(crate::Error::ENOMEM);
        }

        let ret = unsafe { uv_signal_init(r#loop.into_inner(), handle) };
        if ret < 0 {
            unsafe { std::alloc::dealloc(handle as _, layout) };
            return Err(crate::Error::from_inner(ret as uv::uv_errno_t));
        }

        crate::Handle::initialize_data(uv_handle!(handle), super::SignalData(Default::default()));

        Ok(SignalHandle { handle })
    }

    /// Start the handle with the given callback, watching for the given signal.
    pub fn start<CB: Into<SignalCB<'static>>>(&mut self, cb: CB, signum: i32) -> crate::Result<()> {
        // uv_cb is either Some(uv_signal_cb) or None
        let cb = cb.into();
        let uv_cb = use_c_callback!(uv_signal_cb, cb);

        // cb is either Some(closure) or None - it is saved into data
        let dataptr = crate::Handle::get_data(uv_handle!(self.handle));
        if !dataptr.is_null() {
            if let super::SignalData(d) = unsafe { &mut (*dataptr).addl } {
                d.signal_cb = cb;
            }
        }

        crate::uvret(unsafe { uv_signal_start(self.handle, uv_cb, signum as _) })
    }

    /// Same functionality as start() but the signal handler is reset the moment the signal is
    /// received.
    pub fn start_oneshot<CB: Into<SignalCB<'static>>>(
        &mut self,
        cb: CB,
        signum: i32,
    ) -> crate::Result<()> {
        // uv_cb is either Some(uv_signal_cb) or None
        let cb = cb.into();
        let uv_cb = use_c_callback!(uv_signal_cb, cb);

        // cb is either Some(closure) or None - it is saved into data
        let dataptr = crate::Handle::get_data(uv_handle!(self.handle));
        if !dataptr.is_null() {
            if let super::SignalData(d) = unsafe { &mut (*dataptr).addl } {
                d.signal_cb = cb;
            }
        }

        crate::uvret(unsafe { uv_signal_start_oneshot(self.handle, uv_cb, signum as _) })
    }

    /// Stop the handle, the callback will no longer be called.
    pub fn stop(&mut self) -> crate::Result<()> {
        crate::uvret(unsafe { uv_signal_stop(self.handle) })
    }

    /// Signal being monitored by this handle.
    pub fn signum(&self) -> i32 {
        unsafe { (*self.handle).signum }
    }
}

impl FromInner<*mut uv_signal_t> for SignalHandle {
    fn from_inner(handle: *mut uv_signal_t) -> SignalHandle {
        SignalHandle { handle }
    }
}

impl Inner<*mut uv::uv_handle_t> for SignalHandle {
    fn inner(&self) -> *mut uv::uv_handle_t {
        uv_handle!(self.handle)
    }
}

impl From<SignalHandle> for crate::Handle {
    fn from(signal: SignalHandle) -> crate::Handle {
        crate::Handle::from_inner(Inner::<*mut uv::uv_handle_t>::inner(&signal))
    }
}

impl crate::ToHandle for SignalHandle {
    fn to_handle(&self) -> crate::Handle {
        crate::Handle::from_inner(Inner::<*mut uv::uv_handle_t>::inner(self))
    }
}

impl TryFrom<crate::Handle> for SignalHandle {
    type Error = crate::ConversionError;

    fn try_from(handle: crate::Handle) -> Result<Self, Self::Error> {
        let t = handle.get_type();
        if t != crate::HandleType::SIGNAL {
            Err(crate::ConversionError::new(t, crate::HandleType::SIGNAL))
        } else {
            Ok((handle.inner() as *mut uv_signal_t).into_inner())
        }
    }
}

impl HandleTrait for SignalHandle {}

impl crate::Loop {
    /// Create and initialize a new signal handle
    pub fn signal(&self) -> crate::Result<SignalHandle> {
        SignalHandle::new(self)
    }
}