From 8c8aa294685f3ee92ea4cb4d1e7655636182ca4f Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 12 Jul 2025 17:01:56 +0200 Subject: [PATCH] logd: Avoid echoing back kernel logs to the kernel debug scheme --- logd/src/scheme.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/logd/src/scheme.rs b/logd/src/scheme.rs index d772f21212..0d3ada8c7a 100644 --- a/logd/src/scheme.rs +++ b/logd/src/scheme.rs @@ -26,6 +26,8 @@ pub struct LogScheme { enum OutputCmd { Log(Vec), + /// Log a message from the kernel. This skips writing it back to the kernel debug output. + LogKernel(Vec), AddSink(PathBuf), } @@ -56,6 +58,17 @@ impl LogScheme { logs.pop_front(); } } + OutputCmd::LogKernel(line) => { + for file in &mut files { + let _ = file.write(&line); + let _ = file.flush(); + } + logs.push_back(line); + // Keep a limited amount of logs for backfilling to bound memory usage + while logs.len() > 1000 { + logs.pop_front(); + } + } OutputCmd::AddSink(sink_path) => { match OpenOptions::new().write(true).open(&sink_path) { Ok(mut file) => { @@ -87,7 +100,7 @@ impl LogScheme { // FIXME currently possible as /scheme/log/kernel presents a snapshot of the log queue break; } - Self::write_logs(&output_tx2, &mut handle_buf, "kernel", &buf); + Self::write_logs(&output_tx2, &mut handle_buf, "kernel", &buf, true); } }); @@ -103,6 +116,7 @@ impl LogScheme { handle_buf: &mut Vec, context: &str, buf: &[u8], + kernel: bool, ) { let mut i = 0; while i < buf.len() { @@ -117,7 +131,11 @@ impl LogScheme { if b == b'\n' { output_tx - .send(OutputCmd::Log(mem::take(handle_buf))) + .send(if kernel { + OutputCmd::LogKernel(mem::take(handle_buf)) + } else { + OutputCmd::Log(mem::take(handle_buf)) + }) .unwrap(); } @@ -189,7 +207,7 @@ impl SchemeSync for LogScheme { let handle_buf = bufs.entry(ctx.pid).or_insert_with(|| Vec::new()); - Self::write_logs(&self.output_tx, handle_buf, context, buf); + Self::write_logs(&self.output_tx, handle_buf, context, buf, false); Ok(buf.len()) }