logd: Improve implementation of kernel log copying

I hadn't meant to upstream it yet. I accidentally added it in a commit
touching the netstack. In any case this commit integrates the kernel log
copying directly into LogScheme.

The kernel log copying is meant to show the kernel log on the fbbootlogd
rendered bootlog. Even once the kernel graphical debug has been disabled
or if it never got enabled due to the bootloader not presenting a
framebuffer.
This commit is contained in:
bjorn3
2025-07-12 16:58:35 +02:00
parent afa109c088
commit 8f319d4081
2 changed files with 46 additions and 41 deletions
-20
View File
@@ -1,5 +1,4 @@
use redox_scheme::{RequestKind, SignalBehavior, Socket};
use std::io::{Read, Write};
use std::process;
use crate::scheme::LogScheme;
@@ -9,11 +8,6 @@ mod scheme;
fn daemon(daemon: redox_daemon::Daemon) -> ! {
let socket = Socket::create("log").expect("logd: failed to create log scheme");
std::process::Command::new(std::env::current_exe().unwrap())
.arg("--internal-copy-kernel-log")
.spawn()
.unwrap();
eprintln!("logd: ready for logging on log:");
daemon.ready().expect("logd: failed to notify parent");
@@ -42,19 +36,5 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! {
}
fn main() {
if std::env::args().nth(1).as_deref() == Some("--internal-copy-kernel-log") {
let mut debug_file = std::fs::File::open("/scheme/sys/log").unwrap();
let mut log_file = std::fs::OpenOptions::new()
.write(true)
.open("/scheme/log/kernel")
.unwrap();
let mut buf = [0; 4096];
loop {
let n = debug_file.read(&mut buf).unwrap();
assert!(n != 0); // FIXME currently fails as /scheme/log/kernel presents a snapshot of the log queue
log_file.write_all(&buf[..n]).unwrap();
}
}
redox_daemon::Daemon::new(daemon).expect("logd: failed to daemonize");
}
+46 -21
View File
@@ -1,6 +1,6 @@
use std::collections::{BTreeMap, VecDeque};
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::io::{Read, Write};
use std::mem;
use std::path::PathBuf;
use std::sync::mpsc::{self, Sender};
@@ -75,12 +75,55 @@ impl LogScheme {
}
});
let output_tx2 = output_tx.clone();
std::thread::spawn(move || {
let mut debug_file = std::fs::File::open("/scheme/sys/log").unwrap();
let mut handle_buf = vec![];
let mut buf = [0; 4096];
buf[.."kernel: ".len()].copy_from_slice(b"kernel: ");
loop {
let n = debug_file.read(&mut buf["kernel: ".len()..]).unwrap();
if n == 0 {
// 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);
}
});
LogScheme {
next_id: 0,
output_tx,
handles: BTreeMap::new(),
}
}
fn write_logs(
output_tx: &Sender<OutputCmd>,
handle_buf: &mut Vec<u8>,
context: &str,
buf: &[u8],
) {
let mut i = 0;
while i < buf.len() {
let b = buf[i];
if handle_buf.is_empty() && !context.is_empty() {
handle_buf.extend_from_slice(context.as_bytes());
handle_buf.extend_from_slice(b": ");
}
handle_buf.push(b);
if b == b'\n' {
output_tx
.send(OutputCmd::Log(mem::take(handle_buf)))
.unwrap();
}
i += 1;
}
}
}
impl SchemeSync for LogScheme {
@@ -146,27 +189,9 @@ impl SchemeSync for LogScheme {
let handle_buf = bufs.entry(ctx.pid).or_insert_with(|| Vec::new());
let mut i = 0;
while i < buf.len() {
let b = buf[i];
Self::write_logs(&self.output_tx, handle_buf, context, buf);
if handle_buf.is_empty() && !context.is_empty() {
handle_buf.extend_from_slice(context.as_bytes());
handle_buf.extend_from_slice(b": ");
}
handle_buf.push(b);
if b == b'\n' {
self.output_tx
.send(OutputCmd::Log(mem::take(handle_buf)))
.unwrap();
}
i += 1;
}
Ok(i)
Ok(buf.len())
}
fn fcntl(&mut self, id: usize, _cmd: usize, _arg: usize, _ctx: &CallerCtx) -> Result<usize> {