2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
2018-12-11 06:06:54 +01:00
|
|
|
use std::io;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2018-12-11 06:06:54 +01:00
|
|
|
|
2019-11-26 12:03:52 +01:00
|
|
|
use futures_core::stream::Stream;
|
2018-12-11 06:06:54 +01:00
|
|
|
|
|
|
|
use crate::server::Server;
|
|
|
|
|
|
|
|
/// Different types of process signals
|
|
|
|
#[derive(PartialEq, Clone, Copy, Debug)]
|
|
|
|
pub(crate) enum Signal {
|
|
|
|
/// SIGHUP
|
|
|
|
Hup,
|
|
|
|
/// SIGINT
|
|
|
|
Int,
|
|
|
|
/// SIGTERM
|
|
|
|
Term,
|
|
|
|
/// SIGQUIT
|
|
|
|
Quit,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Signals {
|
|
|
|
srv: Server,
|
|
|
|
#[cfg(not(unix))]
|
2019-11-26 12:03:52 +01:00
|
|
|
stream: tokio_net::signal::CtrlC,
|
2018-12-11 06:06:54 +01:00
|
|
|
#[cfg(unix)]
|
2019-11-26 12:03:52 +01:00
|
|
|
streams: Vec<(Signal, tokio_net::signal::unix::Signal)>,
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Signals {
|
2019-11-26 12:03:52 +01:00
|
|
|
pub(crate) fn start(srv: Server) -> io::Result<()> {
|
|
|
|
actix_rt::spawn({
|
2018-12-11 06:06:54 +01:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2019-11-26 12:03:52 +01:00
|
|
|
let stream = tokio_net::signal::ctrl_c()?;
|
|
|
|
Signals { srv, stream }
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
use tokio_net::signal::unix;
|
|
|
|
|
2019-11-26 12:03:52 +01:00
|
|
|
let mut streams = Vec::new();
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-11-26 12:03:52 +01:00
|
|
|
let sig_map = [
|
|
|
|
(unix::SignalKind::interrupt(), Signal::Int),
|
|
|
|
(unix::SignalKind::hangup(), Signal::Hup),
|
|
|
|
(unix::SignalKind::terminate(), Signal::Term),
|
|
|
|
(unix::SignalKind::quit(), Signal::Quit),
|
2019-11-14 13:38:24 +01:00
|
|
|
];
|
|
|
|
|
2019-11-26 12:03:52 +01:00
|
|
|
for (kind, sig) in sig_map.into_iter() {
|
2019-11-14 13:38:24 +01:00
|
|
|
let sig = sig.clone();
|
2019-11-26 12:03:52 +01:00
|
|
|
let fut = unix::signal(*kind)?;
|
|
|
|
streams.push((sig, fut));
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 12:03:52 +01:00
|
|
|
Signals { srv, streams }
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
2019-11-26 12:03:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for Signals {
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = ();
|
|
|
|
|
2019-11-26 12:03:52 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2018-12-11 06:06:54 +01:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
loop {
|
2019-11-26 12:03:52 +01:00
|
|
|
match Pin::new(&mut self.stream).poll_next(cx) {
|
|
|
|
Poll::Ready(Ok(Some(_))) => self.srv.signal(Signal::Int),
|
|
|
|
Poll::Ready(None) => return Poll::Ready(()),
|
|
|
|
Poll::Pending => return Poll::Pending,
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
2019-11-26 12:03:52 +01:00
|
|
|
for idx in 0..self.streams.len() {
|
2018-12-11 06:06:54 +01:00
|
|
|
loop {
|
2019-11-26 12:03:52 +01:00
|
|
|
match Pin::new(&mut self.streams[idx].1).poll_next(cx) {
|
|
|
|
Poll::Ready(None) => return Poll::Ready(()),
|
|
|
|
Poll::Pending => break,
|
|
|
|
Poll::Ready(Some(_)) => {
|
|
|
|
let sig = self.streams[idx].0;
|
|
|
|
self.srv.signal(sig);
|
|
|
|
}
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 12:03:52 +01:00
|
|
|
Poll::Pending
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|