mirror of
https://github.com/kata-containers/kata-containers.git
synced 2026-07-01 22:50:54 +00:00
runtime-rs: Introduce a helper to check if process/container exists
Returns `true` if the error indicates that the target process/container no longer exists. This is used to determine if an operation, like signaling a process, failed because the target is no longer available. The function checks for standard OS error codes (`ESRCH`, `ENOENT`) and common error message patterns. Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
use nix::libc;
|
||||
|
||||
use crate::types::{ContainerProcess, SandboxResponse, TaskResponse};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
@@ -51,3 +53,33 @@ pub fn is_normal_oom_shutdown_error(err: &anyhow::Error) -> bool {
|
||||
.iter()
|
||||
.any(|pattern| error_string.contains(&pattern.to_lowercase()))
|
||||
}
|
||||
|
||||
/// List of common error message patterns indicating that a process or container is missing.
|
||||
const NO_SUCH_PROCESS_MESSAGES: &[&str] = &[
|
||||
"no such process",
|
||||
"process not found",
|
||||
"init process not found",
|
||||
"cannot find init process",
|
||||
];
|
||||
|
||||
/// Returns `true` if the error indicates that the target process/container no longer exists.
|
||||
/// This is used to determine if an operation, like signaling a process, failed because the
|
||||
/// target is no longer available.
|
||||
/// The function checks for standard OS error codes (`ESRCH`, `ENOENT`) and common error message patterns.
|
||||
pub fn is_no_such_process_error(err: &anyhow::Error) -> bool {
|
||||
// Check for standard OS error codes.
|
||||
if let Some(io_err) = err.downcast_ref::<std::io::Error>() {
|
||||
if let Some(raw_os_error) = io_err.raw_os_error() {
|
||||
// standard "no such process" error.
|
||||
if raw_os_error == libc::ESRCH || raw_os_error == libc::ENOENT {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to checking the error message for known patterns.
|
||||
let error_string = err.to_string().to_lowercase();
|
||||
NO_SUCH_PROCESS_MESSAGES
|
||||
.iter()
|
||||
.any(|pattern| error_string.contains(&pattern.to_lowercase()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user