dkl logger: avoid a full thread just to forward signals
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use async_compression::tokio::write::{ZstdDecoder, ZstdEncoder};
|
use async_compression::tokio::write::{ZstdDecoder, ZstdEncoder};
|
||||||
use chrono::{DurationRound, TimeDelta, Utc};
|
use chrono::{DurationRound, TimeDelta, Utc};
|
||||||
use eyre::{Result, format_err};
|
use eyre::{format_err, Result};
|
||||||
use log::{debug, error, warn};
|
use log::{debug, error, warn};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
@@ -9,7 +9,8 @@ use tokio::{
|
|||||||
io::{self, AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, BufWriter},
|
io::{self, AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, BufWriter},
|
||||||
process,
|
process,
|
||||||
sync::mpsc,
|
sync::mpsc,
|
||||||
time::{Duration, sleep},
|
task::spawn_blocking,
|
||||||
|
time::{sleep, Duration},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{cgroup, fs};
|
use crate::{cgroup, fs};
|
||||||
@@ -92,7 +93,7 @@ impl<'t> Logger<'t> {
|
|||||||
|
|
||||||
// forward signals
|
// forward signals
|
||||||
if let Some(child_pid) = child.id() {
|
if let Some(child_pid) = child.id() {
|
||||||
std::thread::spawn(move || forward_signals_to(child_pid as i32));
|
spawn_blocking(move || forward_signals_to(child_pid as i32)).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle output
|
// handle output
|
||||||
@@ -200,24 +201,29 @@ impl<'t> Logger<'t> {
|
|||||||
|
|
||||||
fn forward_signals_to(pid: i32) {
|
fn forward_signals_to(pid: i32) {
|
||||||
use nix::{
|
use nix::{
|
||||||
sys::signal::{Signal, kill},
|
sys::signal::{kill, Signal},
|
||||||
unistd::Pid,
|
unistd::Pid,
|
||||||
};
|
};
|
||||||
use signal_hook::{consts::*, iterator::Signals};
|
use signal_hook::{consts::*, low_level::register};
|
||||||
|
|
||||||
log::debug!("forwarding signals to pid {pid}");
|
log::debug!("forwarding signals to pid {pid}");
|
||||||
|
|
||||||
let mut signals = Signals::new([
|
|
||||||
SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGUSR2, SIGPIPE, SIGALRM,
|
|
||||||
])
|
|
||||||
.expect("signal setup failed");
|
|
||||||
|
|
||||||
let pid = Pid::from_raw(pid);
|
let pid = Pid::from_raw(pid);
|
||||||
for signal in &mut signals {
|
let signals = [
|
||||||
let Ok(signal) = Signal::try_from(signal) else {
|
SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGUSR2, SIGPIPE, SIGALRM,
|
||||||
|
];
|
||||||
|
|
||||||
|
for sig in signals {
|
||||||
|
let Ok(signal) = Signal::try_from(sig) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
unsafe {
|
||||||
|
register(sig, move || {
|
||||||
|
log::debug!("forwarding {signal} to {pid}");
|
||||||
let _ = kill(pid, signal);
|
let _ = kill(pid, signal);
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user