Print routes on SIGUSR1

Remove the handler which reads from stdin to trigger the printing of the
peers and routes as debug info. This caused unwanted side effects. The
current functionality will be moved to an API in the future, but for now
it is a sufficiently minor change which solves a couple of open issues.
Also trap SIGTERM for quitting. Functionally this does not change the
behavior of the application, but it might be useful in the future.

Fixes #16
Fixes #17

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet
2023-08-21 15:57:16 +02:00
parent 5f6569871f
commit c6f435ef2d
+27 -32
View File
@@ -11,7 +11,10 @@ use std::{
net::{IpAddr, Ipv6Addr, SocketAddr},
path::PathBuf,
};
use tokio::io::AsyncBufReadExt;
use tokio::{
io::AsyncBufReadExt,
signal::{self, unix::SignalKind},
};
mod babel;
mod codec;
@@ -215,44 +218,36 @@ async fn main() -> Result<(), Box<dyn Error>> {
});
}
let mut reader = tokio::io::BufReader::new(tokio::io::stdin());
let mut line = String::new();
// TODO: put in dedicated file so we can only rely on certain signals on unix platforms
let mut sigusr1 =
signal::unix::signal(SignalKind::user_defined1()).expect("Can install SIGUSR1 handler");
let mut sigint =
signal::unix::signal(SignalKind::interrupt()).expect("Can install SIGINT handler");
let mut sigterm =
signal::unix::signal(SignalKind::terminate()).expect("Can install SIGTERM handler");
let read_handle = tokio::spawn(async move {
loop {
line.clear();
match reader.read_line(&mut line).await {
Ok(0) => return, // EOF, quit
Ok(_) => {
// Remove trailing newline
line.pop();
println!("----------- Current selected routes -----------{}\n", line);
router.print_selected_routes();
// print info on SIGUSR1
tokio::spawn(async move {
while let Some(()) = sigusr1.recv().await {
println!("----------- Current selected routes -----------\n");
router.print_selected_routes();
println!("\n----------- Current peers: -----------");
for p in router.peer_interfaces() {
println!(
"Peer: {:?}, with link cost: {}",
p.overlay_ip(),
p.link_cost()
);
}
println!("\n\n");
}
Err(e) => {
eprintln!("Error reading line: {}", e);
return;
}
println!("\n----------- Current peers: -----------");
for p in router.peer_interfaces() {
println!(
"Peer: {:?}, with link cost: {}",
p.overlay_ip(),
p.link_cost()
);
}
println!("\n\n");
}
});
let quit_signal = tokio::signal::ctrl_c();
tokio::select! {
_ = read_handle => { /* The read task completed (this should never happen) */ }
_ = quit_signal => { /* The user pressed ctrl+c (the program should exit here) */ }
_ = sigint.recv() => { }
_ = sigterm.recv() => { }
}
Ok(())