aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-11-15 18:57:04 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-11-15 18:57:04 +0100
commitfedf1f952914651862395e2efca626e75b0d0d44 (patch)
tree0e967773a8e8f28af4c1c420a3a08ce546c44efc
parent3d7f92e5f182d798157be72773cd2775b54f172e (diff)
shut down properly on SIGTERM
-rw-r--r--Cargo.lock10
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs6
-rw-r--r--src/supervisor.rs12
4 files changed, 26 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 41289c5..ff01859 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -556,6 +556,15 @@ dependencies = [
]
[[package]]
+name = "signal-hook-registry"
+version = "1.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
+dependencies = [
+ "libc",
+]
+
+[[package]]
name = "slab"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -637,6 +646,7 @@ dependencies = [
"mio",
"num_cpus",
"pin-project-lite",
+ "signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys",
diff --git a/Cargo.toml b/Cargo.toml
index c7b2cf4..e9e5852 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,4 +19,4 @@ serde_json = "1.0"
socket2 = "0.5.5"
sysinfo = { version = "0.29.10", default-features = false }
thiserror = "1.0"
-tokio = { version = "1.0", features = ["rt-multi-thread", "macros", "sync", "time", "fs", "io-util", "net"] }
+tokio = { version = "1.0", features = ["rt-multi-thread", "macros", "sync", "time", "fs", "io-util", "net", "signal"] }
diff --git a/src/main.rs b/src/main.rs
index 14a9e46..ca39ce8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -85,8 +85,10 @@ async fn main() -> Result<()> {
}
}
result = &mut join_handle => {
- result??; // This always fails, the task never exits with an Ok(_).
- unreachable!("Client::run exited successfully")
+ result??;
+
+ println!("[info] <> exiting");
+ return Ok(());
}
}
}
diff --git a/src/supervisor.rs b/src/supervisor.rs
index d35bbcf..805025b 100644
--- a/src/supervisor.rs
+++ b/src/supervisor.rs
@@ -9,6 +9,7 @@ use std::time::Duration;
use std::{io, mem};
use tokio::io::unix::AsyncFd;
+use tokio::signal::unix::{signal, SignalKind};
use tokio::sync::mpsc;
use tokio::time::Interval;
@@ -255,7 +256,7 @@ impl Client {
}
/// Tries to keep a session open at all costs.
- /// Blocks the caller forever unless a panic occurs.
+ /// Blocks the caller forever unless a panic occurs or SIGTERM is received.
///
/// # Arguments
///
@@ -273,6 +274,8 @@ impl Client {
let mut link_buf = [0; 1494];
let mut net_buf = [0; 1494];
+ let mut sigterm = signal(SignalKind::terminate())?;
+
let mut echo_timeout = tokio::time::interval(Duration::from_secs(12));
let mut ncp_check = tokio::time::interval(Duration::from_secs(20));
@@ -299,6 +302,13 @@ impl Client {
tokio::select! {
biased;
+ _ = sigterm.recv() => {
+ self.lcp.close();
+
+ println!("[info] <> disconnect: sigterm");
+ return Ok(());
+ }
+
packet = self.pppoe.to_send() => self.send_pppoe(&sock_disc, packet).await?,
packet = self.lcp.to_send() => self.send_lcp(
session_fds.as_mut().map(|fds| fds.link()),