Skip to content

Commit

Permalink
Rewrite logging. (#859)
Browse files Browse the repository at this point in the history
This PR implements all log handling with the exception of actual syslog in
Routinator itself. It also implements support for log rotation when logging
into files by re-opening the log file when receiving SIGUSR2.

Error handling for logging is now such that if trying to log to file or
syslog fails, Routinator will exit. It will also exit if it receives SIGUSR2
and can’t open the log file.

The motivation for this is that the log is used by many people to determine
issues with the RPKI repositories, so silently not having logs seems bad.
Also, not being able to log is a good indication for bigger problems to
come.

---------

Co-authored-by: Luuk Hendriks <mail@luukhendriks.eu>
  • Loading branch information
partim and DRiKE committed May 30, 2023
1 parent b688221 commit a5ea731
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 219 deletions.
29 changes: 1 addition & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ clap = { version = "4", features = [ "wrap_help", "cargo", "derive" ]
crossbeam-queue = "0.3.1"
crossbeam-utils = "0.8.1"
dirs = "4.0.0"
fern = "0.6.0"
form_urlencoded = "1.0"
futures = "0.3.4"
hyper = { version = "0.14", features = [ "server", "stream" ] }
listenfd = "1"
log = "0.4.8"
log-reroute = "0.1.5"
num_cpus = "1.12.0"
once_cell = "1"
pin-project-lite = "0.2.4"
rand = "0.8.1"
reqwest = { version = "0.11.0", default-features = false, features = ["blocking", "rustls-tls" ] }
Expand Down
4 changes: 4 additions & 0 deletions doc/manual/source/manual-page.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,10 @@ SIGUSR1: Reload TALs and restart validation
that succeeds, restart validation. If loading the TALs fails, Routinator
will exit.

SIGUSR2: Re-open log file
When receiving SIGUSR2 and logging to a file is enabled, Routinator will
re-open the log file. If this fails, Routinator will exit.

Exit Status
-----------

Expand Down
72 changes: 52 additions & 20 deletions src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::str::FromStr;
use std::sync::mpsc;
use std::sync::Arc;
use std::sync::mpsc::RecvTimeoutError;
use std::time::Duration;
use std::time::{Duration, Instant};
#[cfg(feature = "rta")] use bytes::Bytes;
use clap::{Arg, Args, ArgAction, ArgMatches, FromArgMatches, Parser};
use log::{error, info};
Expand Down Expand Up @@ -275,25 +275,46 @@ impl Server {
if let Some(log) = log.as_ref() {
log.flush();
}
match sig_rx.recv_timeout(timeout) {
Ok(UserSignal::ReloadTals) => {
match validation.reload_tals() {
Ok(_) => {
info!("Reloaded TALs at user request.");
},
Err(_) => {
error!(
"Fatal: Reloading TALs failed, \
shutting down."
);
break Err(Failed);

// Because we don’t want to restart validation upon
// log rotation, we need to loop here. But then we need
// to recalculate timeout.
let deadline = Instant::now() + timeout;
let end = loop {
let timeout = deadline.saturating_duration_since(
Instant::now()
);
match sig_rx.recv_timeout(timeout) {
Ok(UserSignal::ReloadTals) => {
match validation.reload_tals() {
Ok(_) => {
info!("Reloaded TALs at user request.");
break None;
},
Err(_) => {
error!(
"Fatal: Reloading TALs failed, \
shutting down."
);
break Some(Err(Failed));
}
}
}
Ok(UserSignal::RotateLog) => {
if process.rotate_log().is_err() {
break Some(Err(Failed));
}
}
Err(RecvTimeoutError::Timeout) => {
break None;
}
Err(RecvTimeoutError::Disconnected) => {
break Some(Ok(()));
}
}
Err(RecvTimeoutError::Timeout) => { }
Err(RecvTimeoutError::Disconnected) => {
break Ok(());
}
};
if let Some(end) = end {
break end;
}
};
// An error here means the receiver is gone which is fine.
Expand Down Expand Up @@ -1219,6 +1240,7 @@ impl Man {
#[allow(dead_code)]
enum UserSignal {
ReloadTals,
RotateLog,
}

/// Wait for the next validation run or a user telling us to quit or reload.
Expand All @@ -1227,6 +1249,7 @@ enum UserSignal {
#[cfg(unix)]
struct SignalListener {
usr1: Signal,
usr2: Signal,
}

#[cfg(unix)]
Expand All @@ -1239,16 +1262,25 @@ impl SignalListener {
error!("Attaching to signal USR1 failed: {}", err);
return Err(Failed)
}
}
},
usr2: match signal(SignalKind::user_defined2()) {
Ok(usr2) => usr2,
Err(err) => {
error!("Attaching to signal USR2 failed: {}", err);
return Err(Failed)
}
},
})
}

/// Waits for the next thing to do.
///
/// Returns what to do.
pub async fn next(&mut self) -> UserSignal {
self.usr1.recv().await;
UserSignal::ReloadTals
tokio::select! {
_ = self.usr1.recv() => UserSignal::ReloadTals,
_ = self.usr2.recv() => UserSignal::RotateLog,
}
}
}

Expand Down
Loading

0 comments on commit a5ea731

Please sign in to comment.