Signals

Struct Signals 

Source
pub struct Signals { /* private fields */ }
Expand description

Notification of process signals.

§Multithreaded process

For Signals to function correctly in multithreaded processes it must be created on the main thread before spawning any threads. This is due to an implementation detail where the spawned threads must inherit various signal related thread options from the parent thread (mainly the blocked signals on Linux).

Any threads spawned before calling Signals::new will experience the default process signals behaviour, i.e. sending it a signal will stop it.

§Notes

On Android and Linux this will block all signals in the signal set given when creating Signals, using pthread_sigmask(3). This means that the thread in which Signals was created is not interrupted, or in any way notified of signal until the assiocated Poll is polled.

On platforms that support kqueue(2) the signal handler action is set to SIG_IGN using sigaction(2), meaning that all signals will be ignored. Same as on Linux based systems; the program is not interrupted, or in any way notified of signal until the assiocated Poll is polled.

§Implementation notes

On platforms that support kqueue(2) this will use the EVFILT_SIGNAL event filter. On Android and Linux it uses signalfd(2).

§Examples

use std::io;

use mio::{Poll, Events, Interest, Token};
use mio_signals::{Signals, Signal, SignalSet};

const SIGNAL: Token = Token(10);

fn main() -> io::Result<()> {
    let mut poll = Poll::new()?;
    let mut events = Events::with_capacity(128);

    // Create a `Signals` instance that will catch signals for us.
    let mut signals = Signals::new(SignalSet::all())?;
    // And register it with our `Poll` instance.
    poll.registry().register(&mut signals, SIGNAL, Interest::READABLE)?;

    loop {
        poll.poll(&mut events, None)?;

        for event in events.iter() {
            match event.token() {
                // Because we're using edge triggers (default in Mio) we need
                // to keep calling `receive` until it returns `Ok(None)`.
                SIGNAL => loop {
                    match signals.receive()? {
                        Some(Signal::Interrupt) => println!("Got interrupt signal"),
                        Some(Signal::Terminate) => println!("Got terminate signal"),
                        Some(Signal::Quit) => println!("Got quit signal"),
                        Some(Signal::User1) => println!("Got user signal 1"),
                        Some(Signal::User2) => println!("Got user signal 2"),
                        None => break,
                    }
                },
                _ => println!("Got unexpected event: {:?}", event),
            }
        }
    }
}

Implementations§

Source§

impl Signals

Source

pub fn new(signals: SignalSet) -> Result<Signals>

Create a new signal notifier.

Source

pub fn receive(&mut self) -> Result<Option<Signal>>

Receive a signal, if any.

If no signal is available this returns Ok(None).

Trait Implementations§

Source§

impl Debug for Signals

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Source for Signals

Source§

fn register( &mut self, registry: &Registry, token: Token, interests: Interest, ) -> Result<()>

Register self with the given Registry instance. Read more
Source§

fn reregister( &mut self, registry: &Registry, token: Token, interests: Interest, ) -> Result<()>

Re-register self with the given Registry instance. Read more
Source§

fn deregister(&mut self, registry: &Registry) -> Result<()>

Deregister self from the given Registry instance. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.