Files
tokio/tokio-signal/src/lib.rs
T

142 lines
4.8 KiB
Rust
Raw Normal View History

2016-09-06 23:00:17 -07:00
//! Asynchronous signal handling for Tokio
//!
2016-11-07 22:04:08 +00:00
//! This crate implements asynchronous signal handling for Tokio, an
2016-09-06 23:00:17 -07:00
//! asynchronous I/O framework in Rust. The primary type exported from this
//! crate, `unix::Signal`, allows listening for arbitrary signals on Unix
//! platforms, receiving them in an asynchronous fashion.
//!
//! Note that signal handling is in general a very tricky topic and should be
//! used with great care. This crate attempts to implement 'best practice' for
//! signal handling, but it should be evaluated for your own applications' needs
//! to see if it's suitable.
//!
//! The are some fundamental limitations of this crate documented on the
//! `Signal` structure as well.
//!
2017-06-07 12:29:31 -07:00
//! # Examples
//!
//! Print out all ctrl-C notifications received
//!
//! ```rust,no_run
//! extern crate futures;
2018-09-02 11:32:50 +02:00
//! extern crate tokio;
2017-06-07 12:29:31 -07:00
//! extern crate tokio_signal;
//!
//! use futures::{Future, Stream};
//!
//! fn main() {
//! // Create an infinite stream of "Ctrl+C" notifications. Each item received
//! // on this stream may represent multiple ctrl-c signals.
//! let ctrl_c = tokio_signal::ctrl_c().flatten_stream();
2017-06-07 12:29:31 -07:00
//!
//! // Process each ctrl-c as it comes in
//! let prog = ctrl_c.for_each(|()| {
//! println!("ctrl-c received!");
//! Ok(())
//! });
//!
2018-09-02 11:32:50 +02:00
//! tokio::runtime::current_thread::block_on_all(prog).unwrap();
2017-06-07 12:29:31 -07:00
//! }
//! ```
//!
//! Wait for SIGHUP on Unix
//!
//! ```rust,no_run
//! # extern crate futures;
2018-09-02 11:32:50 +02:00
//! # extern crate tokio;
2017-06-07 12:29:31 -07:00
//! # extern crate tokio_signal;
//! # #[cfg(unix)]
//! # mod foo {
//! #
//! extern crate futures;
2018-09-02 11:32:50 +02:00
//! extern crate tokio;
2017-06-07 12:29:31 -07:00
//! extern crate tokio_signal;
//!
//! use futures::{Future, Stream};
//! use tokio_signal::unix::{Signal, SIGHUP};
//!
//! fn main() {
//! // Like the previous example, this is an infinite stream of signals
//! // being received, and signals may be coalesced while pending.
//! let stream = Signal::new(SIGHUP).flatten_stream();
2017-06-07 12:29:31 -07:00
//!
//! // Convert out stream into a future and block the program
2018-09-02 11:32:50 +02:00
//! tokio::runtime::current_thread::block_on_all(stream.into_future()).ok().unwrap();
2017-06-07 12:29:31 -07:00
//! }
//! # }
//! # fn main() {}
//! ```
2016-09-06 23:00:17 -07:00
2018-11-21 17:11:31 -08:00
#![doc(html_root_url = "https://docs.rs/tokio-signal/0.2.7")]
2016-09-06 23:00:17 -07:00
#![deny(missing_docs)]
extern crate futures;
extern crate mio;
extern crate tokio_executor;
2017-03-09 09:06:37 -08:00
extern crate tokio_io;
extern crate tokio_reactor;
2016-09-06 23:00:17 -07:00
2018-01-03 08:41:41 -08:00
use std::io;
2016-09-08 17:13:18 -07:00
use futures::stream::Stream;
use futures::{future, Future};
use tokio_reactor::Handle;
2016-09-08 17:13:18 -07:00
2016-09-06 23:00:17 -07:00
pub mod unix;
2016-09-08 17:13:18 -07:00
pub mod windows;
2018-01-03 08:41:41 -08:00
/// A future whose error is `io::Error`
pub type IoFuture<T> = Box<Future<Item = T, Error = io::Error> + Send>;
/// A stream whose error is `io::Error`
pub type IoStream<T> = Box<Stream<Item = T, Error = io::Error> + Send>;
/// Creates a stream which receives "ctrl-c" notifications sent to a process.
///
/// In general signals are handled very differently across Unix and Windows, but
/// this is somewhat cross platform in terms of how it can be handled. A ctrl-c
/// event to a console process can be represented as a stream for both Windows
/// and Unix.
///
/// This function binds to the default event loop. Note that
/// there are a number of caveats listening for signals, and you may wish to
/// read up on the documentation in the `unix` or `windows` module to take a
/// peek.
pub fn ctrl_c() -> IoFuture<IoStream<()>> {
ctrl_c_handle(&Handle::current())
}
2016-09-08 17:13:18 -07:00
/// Creates a stream which receives "ctrl-c" notifications sent to a process.
///
/// In general signals are handled very differently across Unix and Windows, but
/// this is somewhat cross platform in terms of how it can be handled. A ctrl-c
/// event to a console process can be represented as a stream for both Windows
/// and Unix.
///
/// This function receives a `Handle` to an event loop and returns a future
/// which when resolves yields a stream receiving all signal events. Note that
/// there are a number of caveats listening for signals, and you may wish to
/// read up on the documentation in the `unix` or `windows` module to take a
/// peek.
pub fn ctrl_c_handle(handle: &Handle) -> IoFuture<IoStream<()>> {
2016-09-08 17:13:18 -07:00
return ctrl_c_imp(handle);
#[cfg(unix)]
fn ctrl_c_imp(handle: &Handle) -> IoFuture<IoStream<()>> {
2018-05-05 00:33:02 +02:00
let handle = handle.clone();
Box::new(future::lazy(move || {
unix::Signal::with_handle(unix::libc::SIGINT, &handle)
2018-05-05 00:33:02 +02:00
.map(|x| Box::new(x.map(|_| ())) as Box<Stream<Item = _, Error = _> + Send>)
}))
2016-09-08 17:13:18 -07:00
}
#[cfg(windows)]
fn ctrl_c_imp(handle: &Handle) -> IoFuture<IoStream<()>> {
let handle = handle.clone();
// Use lazy to ensure that `ctrl_c` gets called while on an event loop
Box::new(future::lazy(move || {
windows::Event::ctrl_c_handle(&handle)
.map(|x| Box::new(x) as Box<Stream<Item = _, Error = _> + Send>)
}))
2016-09-08 17:13:18 -07:00
}
}