From 7930a31af667e2658a820156502311316ed38d53 Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Sun, 29 Nov 2026 16:40:44 +0900 Subject: [PATCH] fix(youki): print the error of a failed exec or run to stderr with --log Signed-off-by: Yusuke Sakurai --- crates/youki/src/main.rs | 14 ++++++- crates/youki/tests/error_output.rs | 65 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 crates/youki/tests/error_output.rs diff --git a/crates/youki/src/main.rs b/crates/youki/src/main.rs index 980a01e71f..efb539759e 100644 --- a/crates/youki/src/main.rs +++ b/crates/youki/src/main.rs @@ -95,6 +95,7 @@ fn main() -> Result<()> { std::env::args_os() ); + let log_to_file = opts.global.log.is_some(); let root_path = rootpath::determine(opts.global.root, &*syscall)?; let systemd_cgroup = opts.global.systemd_cgroup; @@ -116,7 +117,7 @@ fn main() -> Result<()> { CommonCmd::Exec(exec) => match commands::exec::exec(exec, root_path) { Ok(exit_code) => std::process::exit(exit_code), Err(e) => { - tracing::error!("error in executing command: {:?}", e); + report_error(&e, log_to_file); std::process::exit(-1); } }, @@ -128,7 +129,7 @@ fn main() -> Result<()> { CommonCmd::Run(run) => match commands::run::run(run, root_path, systemd_cgroup) { Ok(exit_code) => std::process::exit(exit_code), Err(e) => { - tracing::error!("error in executing command: {:?}", e); + report_error(&e, log_to_file); std::process::exit(-1); } }, @@ -150,3 +151,12 @@ fn main() -> Result<()> { } cmd_result } + +/// Logs the error, and also prints it to stderr when the log goes to a file +/// (`--log`), as runc and crun do. Without `--log` the log is already stderr. +fn report_error(e: &anyhow::Error, log_to_file: bool) { + tracing::error!("error in executing command: {:?}", e); + if log_to_file { + eprintln!("Error: {e:?}"); + } +} diff --git a/crates/youki/tests/error_output.rs b/crates/youki/tests/error_output.rs new file mode 100644 index 0000000000..331c5acef3 --- /dev/null +++ b/crates/youki/tests/error_output.rs @@ -0,0 +1,65 @@ +//! A failed `exec` or `run` must report the error on stderr when the log goes to a +//! file, as runc and crun do: callers such as conmon read the reason from stderr. +//! Without `--log` the log is stderr itself, so the error must appear only once. +//! +//! An invalid container id makes both commands fail before they touch any +//! container, so these tests need no privileges. + +use std::fs; +use std::path::Path; +use std::process::{Command, Output}; + +const INVALID_ID: &str = "invalid$id"; +const EXPECTED: &str = "container id contains invalid characters"; + +fn youki(root: &Path, log: Option<&Path>, args: &[&str]) -> Output { + let mut command = Command::new(env!("CARGO_BIN_EXE_youki")); + command.arg("--root").arg(root); + if let Some(log) = log { + command.arg("--log").arg(log).args(["--log-format", "json"]); + } + command.args(args).output().unwrap() +} + +fn check_with_log_file(args: &[&str]) { + let dir = tempfile::tempdir().unwrap(); + let log = dir.path().join("youki.log"); + + let output = youki(&dir.path().join("root"), Some(&log), args); + + assert_eq!(output.status.code(), Some(255)); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.contains(EXPECTED), "stderr: {stderr:?}"); + let log_content = fs::read_to_string(&log).unwrap(); + assert!(log_content.contains(EXPECTED), "log: {log_content:?}"); +} + +fn check_without_log_file(args: &[&str]) { + let dir = tempfile::tempdir().unwrap(); + + let output = youki(&dir.path().join("root"), None, args); + + assert_eq!(output.status.code(), Some(255)); + let stderr = String::from_utf8_lossy(&output.stderr); + assert_eq!(stderr.matches(EXPECTED).count(), 1, "stderr: {stderr:?}"); +} + +#[test] +fn exec_error_with_log_file() { + check_with_log_file(&["exec", INVALID_ID, "true"]); +} + +#[test] +fn exec_error_without_log_file() { + check_without_log_file(&["exec", INVALID_ID, "true"]); +} + +#[test] +fn run_error_with_log_file() { + check_with_log_file(&["run", INVALID_ID]); +} + +#[test] +fn run_error_without_log_file() { + check_without_log_file(&["run", INVALID_ID]); +}