2021-11-04 20:30:43 +00:00
|
|
|
use std::{
|
|
|
|
fmt,
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2018-12-10 21:06:54 -08:00
|
|
|
|
2021-11-04 20:30:43 +00:00
|
|
|
use log::trace;
|
2018-12-10 21:06:54 -08:00
|
|
|
|
2021-10-11 05:14:34 +01:00
|
|
|
/// Types of process signals.
|
2021-11-04 20:30:43 +00:00
|
|
|
// #[allow(dead_code)]
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
#[allow(dead_code)] // variants are never constructed on non-unix
|
2018-12-10 21:06:54 -08:00
|
|
|
pub(crate) enum Signal {
|
2021-10-11 05:14:34 +01:00
|
|
|
/// `SIGINT`
|
2018-12-10 21:06:54 -08:00
|
|
|
Int,
|
2021-10-11 05:14:34 +01:00
|
|
|
|
|
|
|
/// `SIGTERM`
|
2018-12-10 21:06:54 -08:00
|
|
|
Term,
|
2021-10-11 05:14:34 +01:00
|
|
|
|
|
|
|
/// `SIGQUIT`
|
2018-12-10 21:06:54 -08:00
|
|
|
Quit,
|
|
|
|
}
|
|
|
|
|
2021-11-04 20:30:43 +00:00
|
|
|
impl fmt::Display for Signal {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.write_str(match self {
|
|
|
|
Signal::Int => "SIGINT",
|
|
|
|
Signal::Term => "SIGTERM",
|
|
|
|
Signal::Quit => "SIGQUIT",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 05:14:34 +01:00
|
|
|
/// Process signal listener.
|
2018-12-10 21:06:54 -08:00
|
|
|
pub(crate) struct Signals {
|
|
|
|
#[cfg(not(unix))]
|
2021-02-05 19:38:11 -08:00
|
|
|
signals: futures_core::future::LocalBoxFuture<'static, std::io::Result<()>>,
|
2021-10-11 05:14:34 +01:00
|
|
|
|
2018-12-10 21:06:54 -08:00
|
|
|
#[cfg(unix)]
|
2021-02-05 19:38:11 -08:00
|
|
|
signals: Vec<(Signal, actix_rt::signal::unix::Signal)>,
|
2018-12-10 21:06:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Signals {
|
2021-11-04 20:30:43 +00:00
|
|
|
/// Constructs an OS signal listening future.
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
trace!("setting up OS signal listener");
|
|
|
|
|
2020-12-29 07:44:53 +08:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2021-11-04 20:30:43 +00:00
|
|
|
Signals {
|
2020-12-29 07:44:53 +08:00
|
|
|
signals: Box::pin(actix_rt::signal::ctrl_c()),
|
2021-11-04 20:30:43 +00:00
|
|
|
}
|
2020-12-29 07:44:53 +08:00
|
|
|
}
|
2021-10-11 05:14:34 +01:00
|
|
|
|
2020-12-29 07:44:53 +08:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use actix_rt::signal::unix;
|
2019-11-14 18:38:24 +06:00
|
|
|
|
2020-12-29 07:44:53 +08:00
|
|
|
let sig_map = [
|
|
|
|
(unix::SignalKind::interrupt(), Signal::Int),
|
|
|
|
(unix::SignalKind::terminate(), Signal::Term),
|
|
|
|
(unix::SignalKind::quit(), Signal::Quit),
|
|
|
|
];
|
2019-11-14 18:38:24 +06:00
|
|
|
|
2021-02-05 19:38:11 -08:00
|
|
|
let signals = sig_map
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(kind, sig)| {
|
|
|
|
unix::signal(*kind)
|
|
|
|
.map(|tokio_sig| (*sig, tokio_sig))
|
|
|
|
.map_err(|e| {
|
|
|
|
log::error!(
|
|
|
|
"Can not initialize stream handler for {:?} err: {}",
|
|
|
|
sig,
|
|
|
|
e
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2020-12-29 07:44:53 +08:00
|
|
|
|
2021-11-04 20:30:43 +00:00
|
|
|
Signals { signals }
|
2020-12-29 07:44:53 +08:00
|
|
|
}
|
2018-12-10 21:06:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for Signals {
|
2021-11-04 20:30:43 +00:00
|
|
|
type Output = Signal;
|
2019-11-14 18:38:24 +06:00
|
|
|
|
2019-11-26 17:03:52 +06:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2018-12-10 21:06:54 -08:00
|
|
|
#[cfg(not(unix))]
|
2021-11-04 20:30:43 +00:00
|
|
|
{
|
|
|
|
self.signals.as_mut().poll(cx).map(|_| Signal::Int)
|
2018-12-10 21:06:54 -08:00
|
|
|
}
|
2021-10-11 05:14:34 +01:00
|
|
|
|
2018-12-10 21:06:54 -08:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
2020-12-29 07:44:53 +08:00
|
|
|
for (sig, fut) in self.signals.iter_mut() {
|
2021-11-04 20:30:43 +00:00
|
|
|
// TODO: match on if let Some ?
|
2021-02-05 19:38:11 -08:00
|
|
|
if Pin::new(fut).poll_recv(cx).is_ready() {
|
2021-11-04 20:30:43 +00:00
|
|
|
trace!("{} received", sig);
|
|
|
|
return Poll::Ready(*sig);
|
2018-12-10 21:06:54 -08:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 20:30:43 +00:00
|
|
|
|
2019-11-26 17:03:52 +06:00
|
|
|
Poll::Pending
|
2018-12-10 21:06:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|