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 <bin@hyper.sh>
This commit is contained in:
bin 2022-02-15 13:27:14 +08:00 committed by Samuel Ortiz
parent 80e8dbf1f5
commit 72bf5496fd

View File

@ -1489,7 +1489,7 @@ async fn execute_hook(logger: &Logger, h: &Hook, st: &OCIState) -> Result<()> {
args.remove(0); 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(); 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. // Avoid the exit signal to be reaped by the global reaper.
@ -1517,37 +1517,39 @@ async fn execute_hook(logger: &Logger, h: &Hook, st: &OCIState) -> Result<()> {
let path = h.path.clone(); let path = h.path.clone();
let join_handle = tokio::spawn(async move { let join_handle = tokio::spawn(async move {
child if let Some(mut stdin) = child.stdin.take() {
.stdin match stdin.write_all(state.as_bytes()).await {
.as_mut() Ok(_) => {}
.unwrap() Err(e) => {
.write_all(state.as_bytes()) info!(logger, "write to child stdin failed: {:?}", e);
.await }
.unwrap(); }
}
// Close stdin so that hook program could receive EOF
child.stdin.take();
// read something from stdout and stderr for debug // read something from stdout and stderr for debug
let mut out = String::new(); if let Some(stdout) = child.stdout.as_mut() {
child let mut out = String::new();
.stdout match stdout.read_to_string(&mut out).await {
.as_mut() Ok(_) => {
.unwrap() info!(logger, "child stdout: {}", out.as_str());
.read_to_string(&mut out) }
.await Err(e) => {
.unwrap(); info!(logger, "read from child stdout failed: {:?}", e);
info!(logger, "child stdout: {}", out.as_str()); }
}
}
let mut err = String::new(); let mut err = String::new();
child if let Some(stderr) = child.stderr.as_mut() {
.stderr match stderr.read_to_string(&mut err).await {
.as_mut() Ok(_) => {
.unwrap() info!(logger, "child stderr: {}", err.as_str());
.read_to_string(&mut err) }
.await Err(e) => {
.unwrap(); info!(logger, "read from child stderr failed: {:?}", e);
info!(logger, "child stderr: {}", err.as_str()); }
}
}
match child.wait().await { match child.wait().await {
Ok(exit) => { Ok(exit) => {