From 2a4fbd6d8c8abe8fa0b315dd86115850d1b1c774 Mon Sep 17 00:00:00 2001 From: quanweiZhou Date: Fri, 17 Jun 2022 16:51:10 +0800 Subject: [PATCH] agent: enhance get handled signal For runC, send the signal to the init process directly. For kata, we try to send `SIGKILL` instead of `SIGTERM` when the process has not installed the handler for `SIGTERM`. The `is_signal_handled` function determine which signal the container process has been handled. But currently `is_signal_handled` is only catching (SigCgt). While the container process is ignoring (SigIgn) or blocking (SigBlk) also should not be converted from the `SIGTERM` to `SIGKILL`. For example, when using terminationGracePeriodSeconds the k8s will send SIGTERM first and then send `SIGKILL`, in this case, the container ignores the `SIGTERM`, so we should send the `SIGTERM` not the `SIGKILL` to the container. Fixes: #4478 Signed-off-by: quanweiZhou --- src/agent/src/rpc.rs | 50 ++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index bcf2096d2b..10f1a0021b 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -1793,34 +1793,25 @@ fn is_signal_handled(proc_status_file: &str, signum: u32) -> bool { let sig_mask: u64 = 1 << shift_count; let reader = BufReader::new(file); - // Read the file line by line using the lines() iterator from std::io::BufRead. - for (_index, line) in reader.lines().enumerate() { - let line = match line { - Ok(l) => l, - Err(_) => { - warn!(sl!(), "failed to read file {}", proc_status_file); - return false; - } - }; - if line.starts_with("SigCgt:") { + // read lines start with SigBlk/SigIgn/SigCgt and check any match the signal mask + reader + .lines() + .flatten() + .filter(|line| { + line.starts_with("SigBlk:") + || line.starts_with("SigIgn:") + || line.starts_with("SigCgt:") + }) + .any(|line| { let mask_vec: Vec<&str> = line.split(':').collect(); - if mask_vec.len() != 2 { - warn!(sl!(), "parse the SigCgt field failed"); - return false; - } - let sig_cgt_str = mask_vec[1].trim(); - let sig_cgt_mask = match u64::from_str_radix(sig_cgt_str, 16) { - Ok(h) => h, - Err(_) => { - warn!(sl!(), "failed to parse the str {} to hex", sig_cgt_str); - return false; + if mask_vec.len() == 2 { + let sig_str = mask_vec[1].trim(); + if let Ok(sig) = u64::from_str_radix(sig_str, 16) { + return sig & sig_mask == sig_mask; } - }; - - return (sig_cgt_mask & sig_mask) == sig_mask; - } - } - false + } + false + }) } fn do_mem_hotplug_by_probe(addrs: &[u64]) -> Result<()> { @@ -2642,7 +2633,12 @@ OtherField:other TestData { status_file_data: Some("SigBlk:0000000000000001"), signum: 1, - result: false, + result: true, + }, + TestData { + status_file_data: Some("SigIgn:0000000000000001"), + signum: 1, + result: true, }, TestData { status_file_data: None,