surrealpatch/src/net/signals.rs
2023-02-08 13:19:08 +00:00

61 lines
1.6 KiB
Rust

use crate::err::Error;
#[cfg(unix)]
pub async fn listen() -> Result<String, Error> {
// Import the OS signals
use tokio::signal::unix::{signal, SignalKind};
// Get the operating system signal types
let mut sighup = signal(SignalKind::hangup())?;
let mut sigint = signal(SignalKind::interrupt())?;
let mut sigquit = signal(SignalKind::quit())?;
let mut sigterm = signal(SignalKind::terminate())?;
// Listen and wait for the system signals
tokio::select! {
// Wait for a SIGQUIT signal
_ = sighup.recv() => {
Ok(String::from("SIGHUP"))
}
// Wait for a SIGINT signal
_ = sigint.recv() => {
Ok(String::from("SIGINT"))
}
// Wait for a SIGQUIT signal
_ = sigquit.recv() => {
Ok(String::from("SIGQUIT"))
}
// Wait for a SIGTERM signal
_ = sigterm.recv() => {
Ok(String::from("SIGTERM"))
}
}
}
#[cfg(windows)]
pub async fn listen() -> Result<String, Error> {
// Import the OS signals
use tokio::signal::windows;
// Get the operating system signal types
let mut exit = windows::ctrl_c()?;
let mut leave = windows::ctrl_break()?;
let mut close = windows::ctrl_close()?;
let mut shutdown = windows::ctrl_shutdown()?;
// Listen and wait for the system signals
tokio::select! {
// Wait for a CTRL-C signal
_ = exit.recv() => {
Ok(String::from("CTRL-C"))
}
// Wait for a CTRL-BREAK signal
_ = leave.recv() => {
Ok(String::from("CTRL-BREAK"))
}
// Wait for a CTRL-CLOSE signal
_ = close.recv() => {
Ok(String::from("CTRL-CLOSE"))
}
// Wait for a CTRL-SHUTDOWN signal
_ = shutdown.recv() => {
Ok(String::from("CTRL-SHUTDOWN"))
}
}
}