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-12-05 11:40:24 +01:00
|
|
|
use futures::future::lazy;
|
2018-12-11 06:06:54 +01:00
|
|
|
|
|
|
|
use crate::server::Server;
|
|
|
|
|
|
|
|
/// Different types of process signals
|
2019-12-06 09:06:14 +01:00
|
|
|
#[allow(dead_code)]
|
2018-12-11 06:06:54 +01:00
|
|
|
#[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-12-06 09:06:14 +01:00
|
|
|
stream: Pin<Box<dyn Future<Output = io::Result<()>>>>,
|
2018-12-11 06:06:54 +01:00
|
|
|
#[cfg(unix)]
|
2019-12-02 06:30:27 +01:00
|
|
|
streams: Vec<(Signal, actix_rt::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<()> {
|
2019-12-05 11:40:24 +01:00
|
|
|
actix_rt::spawn(lazy(|_| {
|
2018-12-11 06:06:54 +01:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2019-12-06 09:06:14 +01:00
|
|
|
actix_rt::spawn(Signals {
|
|
|
|
srv,
|
|
|
|
stream: Box::pin(actix_rt::signal::ctrl_c()),
|
|
|
|
});
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
2019-12-02 06:30:27 +01:00
|
|
|
use actix_rt::signal::unix;
|
2019-11-14 13:38:24 +01:00
|
|
|
|
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-12-02 17:30:09 +01:00
|
|
|
for (kind, sig) in sig_map.iter() {
|
2019-12-05 11:40:24 +01:00
|
|
|
match unix::signal(*kind) {
|
|
|
|
Ok(stream) => streams.push((*sig, stream)),
|
|
|
|
Err(e) => log::error!(
|
|
|
|
"Can not initialize stream handler for {:?} err: {}",
|
|
|
|
sig,
|
|
|
|
e
|
|
|
|
),
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:40:24 +01:00
|
|
|
actix_rt::spawn(Signals { srv, streams })
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
2019-12-05 11:40:24 +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))]
|
2019-12-06 09:06:14 +01:00
|
|
|
match Pin::new(&mut self.stream).poll(cx) {
|
|
|
|
Poll::Ready(_) => {
|
|
|
|
self.srv.signal(Signal::Int);
|
|
|
|
Poll::Ready(())
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
2019-12-06 09:06:14 +01:00
|
|
|
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-12-05 11:40:24 +01:00
|
|
|
match self.streams[idx].1.poll_recv(cx) {
|
2019-11-26 12:03:52 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|