From 7af719e47c90bbb0531ffb0016c38f67af54e07c Mon Sep 17 00:00:00 2001 From: bin Date: Tue, 15 Feb 2022 13:27:14 +0800 Subject: [PATCH] agent: handle hook process result Current hook process is handled by just calling unwrap() on it, sometime it will cause panic. By handling all Result type and check the error can avoid panic. Fixes: #3649 Signed-off-by: bin --- src/agent/rustjail/src/container.rs | 58 +++++++++++++++-------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/agent/rustjail/src/container.rs b/src/agent/rustjail/src/container.rs index 10b31dfef1..c3df3c1c2c 100644 --- a/src/agent/rustjail/src/container.rs +++ b/src/agent/rustjail/src/container.rs @@ -1485,7 +1485,7 @@ async fn execute_hook(logger: &Logger, h: &Hook, st: &OCIState) -> Result<()> { args.remove(0); } - // all invalid envs will be ommit, only valid envs will be passed to hook. + // all invalid envs will be omitted, only valid envs will be passed to hook. let env: HashMap<&str, &str> = h.env.iter().filter_map(|e| valid_env(e)).collect(); // Avoid the exit signal to be reaped by the global reaper. @@ -1513,37 +1513,39 @@ async fn execute_hook(logger: &Logger, h: &Hook, st: &OCIState) -> Result<()> { let path = h.path.clone(); let join_handle = tokio::spawn(async move { - child - .stdin - .as_mut() - .unwrap() - .write_all(state.as_bytes()) - .await - .unwrap(); - - // Close stdin so that hook program could receive EOF - child.stdin.take(); + if let Some(mut stdin) = child.stdin.take() { + match stdin.write_all(state.as_bytes()).await { + Ok(_) => {} + Err(e) => { + info!(logger, "write to child stdin failed: {:?}", e); + } + } + } // read something from stdout and stderr for debug - let mut out = String::new(); - child - .stdout - .as_mut() - .unwrap() - .read_to_string(&mut out) - .await - .unwrap(); - info!(logger, "child stdout: {}", out.as_str()); + if let Some(stdout) = child.stdout.as_mut() { + let mut out = String::new(); + match stdout.read_to_string(&mut out).await { + Ok(_) => { + info!(logger, "child stdout: {}", out.as_str()); + } + Err(e) => { + info!(logger, "read from child stdout failed: {:?}", e); + } + } + } let mut err = String::new(); - child - .stderr - .as_mut() - .unwrap() - .read_to_string(&mut err) - .await - .unwrap(); - info!(logger, "child stderr: {}", err.as_str()); + if let Some(stderr) = child.stderr.as_mut() { + match stderr.read_to_string(&mut err).await { + Ok(_) => { + info!(logger, "child stderr: {}", err.as_str()); + } + Err(e) => { + info!(logger, "read from child stderr failed: {:?}", e); + } + } + } match child.wait().await { Ok(exit) => {