mirror of
https://github.com/zaphar/clio.git
synced 2025-07-23 20:49:51 -04:00
fix: Some signal handling woes
tokio won't allow me to trap sigkill
This commit is contained in:
parent
a1ef30dec9
commit
b3680173a7
20
Cargo.lock
generated
20
Cargo.lock
generated
@ -157,6 +157,16 @@ version = "0.7.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
|
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clio"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"clap",
|
||||||
|
"nix",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "colorchoice"
|
name = "colorchoice"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
@ -193,16 +203,6 @@ version = "0.2.155"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "logme"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"anyhow",
|
|
||||||
"clap",
|
|
||||||
"nix",
|
|
||||||
"tokio",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.7.2"
|
version = "2.7.2"
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "logme"
|
name = "clio"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
description = "A small log redirection utility"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
22
src/main.rs
22
src/main.rs
@ -10,7 +10,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
use tokio::signal::unix::{signal, SignalKind};
|
use tokio::signal::unix::{signal, SignalKind};
|
||||||
|
|
||||||
#[derive(Parser, Clone, ValueEnum)]
|
#[derive(Parser, Clone, ValueEnum, Debug)]
|
||||||
pub enum HandledSignals {
|
pub enum HandledSignals {
|
||||||
SIGHUP,
|
SIGHUP,
|
||||||
SIGUSR1,
|
SIGUSR1,
|
||||||
@ -27,7 +27,7 @@ impl From<&HandledSignals> for SignalKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
#[arg(short = 'e', long = "err-path", help = "Path to write stderr to")]
|
#[arg(short = 'e', long = "err-path", help = "Path to write stderr to")]
|
||||||
@ -49,7 +49,6 @@ async fn main() -> anyhow::Result<ExitCode> {
|
|||||||
let handled_sig: SignalKind = (&args.rotated_signal).into();
|
let handled_sig: SignalKind = (&args.rotated_signal).into();
|
||||||
let mut rotation_signal_stream = signal(handled_sig)?;
|
let mut rotation_signal_stream = signal(handled_sig)?;
|
||||||
let mut sigterm_stream = signal(SignalKind::terminate())?;
|
let mut sigterm_stream = signal(SignalKind::terminate())?;
|
||||||
let mut sigkill_stream = signal(SignalKind::from_raw(9))?;
|
|
||||||
let mut sigquit_stream = signal(SignalKind::quit())?;
|
let mut sigquit_stream = signal(SignalKind::quit())?;
|
||||||
// Setup our output wiring.
|
// Setup our output wiring.
|
||||||
let app_name = match args.cmd.first() {
|
let app_name = match args.cmd.first() {
|
||||||
@ -70,8 +69,8 @@ async fn main() -> anyhow::Result<ExitCode> {
|
|||||||
.expect("no valid stderr from command available");
|
.expect("no valid stderr from command available");
|
||||||
let mut stderr_buffer = [0; 8 * 1024];
|
let mut stderr_buffer = [0; 8 * 1024];
|
||||||
|
|
||||||
let mut stderr_writer = File::options().append(true).open(stderr_path).await?;
|
let mut stderr_writer = File::options().append(true).create(true).open(stderr_path).await?;
|
||||||
let mut stdout_writer = File::options().append(true).open(stdout_path).await?;
|
let mut stdout_writer = File::options().append(true).create(true).open(stdout_path).await?;
|
||||||
// TODO(jwall): Forward all other signals to the running process.
|
// TODO(jwall): Forward all other signals to the running process.
|
||||||
loop {
|
loop {
|
||||||
// NOTE(zaphar): Each select block will run exclusively of the other blocks using a
|
// NOTE(zaphar): Each select block will run exclusively of the other blocks using a
|
||||||
@ -84,7 +83,7 @@ async fn main() -> anyhow::Result<ExitCode> {
|
|||||||
// TODO(zaphar): It is possible we should try to reopen the file if this
|
// TODO(zaphar): It is possible we should try to reopen the file if this
|
||||||
// write fails in some cases.
|
// write fails in some cases.
|
||||||
if let Err(_) = stdout_writer.write(&stdout_buffer[0..n]).await {
|
if let Err(_) = stdout_writer.write(&stdout_buffer[0..n]).await {
|
||||||
stdout_writer = File::options().append(true).open(stdout_path).await?;
|
stdout_writer = File::options().append(true).create(true).open(stdout_path).await?;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@ -102,7 +101,7 @@ async fn main() -> anyhow::Result<ExitCode> {
|
|||||||
// TODO(zaphar): It is possible we should try to reopen the file if this
|
// TODO(zaphar): It is possible we should try to reopen the file if this
|
||||||
// write fails in some cases.
|
// write fails in some cases.
|
||||||
if let Err(_) = stderr_writer.write(&stderr_buffer[0..n]).await {
|
if let Err(_) = stderr_writer.write(&stderr_buffer[0..n]).await {
|
||||||
stderr_writer = File::options().append(true).open(stderr_path).await?;
|
stderr_writer = File::options().append(true).create(true).open(stderr_path).await?;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@ -121,8 +120,8 @@ async fn main() -> anyhow::Result<ExitCode> {
|
|||||||
// TODO(zaphar): These should do something in the event of an error
|
// TODO(zaphar): These should do something in the event of an error
|
||||||
_ = stderr_writer.sync_all().await;
|
_ = stderr_writer.sync_all().await;
|
||||||
_ = stdout_writer.sync_all().await;
|
_ = stdout_writer.sync_all().await;
|
||||||
stderr_writer = File::options().append(true).open(stderr_path).await?;
|
stderr_writer = File::options().append(true).create(true).open(stderr_path).await?;
|
||||||
stdout_writer = File::options().append(true).open(stdout_path).await?;
|
stdout_writer = File::options().append(true).create(true).open(stdout_path).await?;
|
||||||
}
|
}
|
||||||
_ = sigterm_stream.recv() => {
|
_ = sigterm_stream.recv() => {
|
||||||
// NOTE(zaphar): This is a giant hack.
|
// NOTE(zaphar): This is a giant hack.
|
||||||
@ -150,13 +149,10 @@ async fn main() -> anyhow::Result<ExitCode> {
|
|||||||
if let Some(pid) = child.id() {
|
if let Some(pid) = child.id() {
|
||||||
// If the child hasn't already completed, send a SIGTERM.
|
// If the child hasn't already completed, send a SIGTERM.
|
||||||
if let Err(e) = kill(Pid::from_raw(pid.try_into().expect("Invalid PID")), SIGQUIT) {
|
if let Err(e) = kill(Pid::from_raw(pid.try_into().expect("Invalid PID")), SIGQUIT) {
|
||||||
eprintln!("Failed to forward SIGTERM to child process: {}", e);
|
eprintln!("Failed to forward SIGQUIT to child process: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ = sigkill_stream.recv() => {
|
|
||||||
child.start_kill()?;
|
|
||||||
}
|
|
||||||
result = child.wait() => {
|
result = child.wait() => {
|
||||||
// The child has finished
|
// The child has finished
|
||||||
return Ok(ExitCode::from(result?.code().expect("No exit code for process") as u8));
|
return Ok(ExitCode::from(result?.code().expect("No exit code for process") as u8));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user