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);
}
// 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.
@ -1517,37 +1517,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) => {