From 0a9234fc2b9755b2c4b12acce32cdaa145e68426 Mon Sep 17 00:00:00 2001 From: yihuaf Date: Thu, 4 May 2023 19:28:09 +0000 Subject: [PATCH] refactor the syscall error Signed-off-by: yihuaf --- crates/libcontainer/src/error.rs | 20 +++++++++++++------- crates/libcontainer/src/lib.rs | 2 +- crates/libcontainer/src/namespaces.rs | 25 +++++++++---------------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/crates/libcontainer/src/error.rs b/crates/libcontainer/src/error.rs index e3452c0ad4..4b71bc2165 100644 --- a/crates/libcontainer/src/error.rs +++ b/crates/libcontainer/src/error.rs @@ -1,23 +1,29 @@ - /// SyscallWrapperError aims to simplify error handling of syscalls in /// libcontainer. In many occasions, we mix nix::Error and std::io::Error, which /// makes error handling complicated. #[derive(Debug, thiserror::Error)] -pub enum SyscallWrapperError { +pub enum SyscallError { #[error(transparent)] Io(std::io::Error), #[error(transparent)] Nix(nix::Error), + #[error(transparent)] + LibcontainerSyscall(crate::syscall::SyscallError), } -impl From for SyscallWrapperError { +impl From for SyscallError { fn from(err: std::io::Error) -> Self { - SyscallWrapperError::Io(err) + SyscallError::Io(err) } } -impl From for SyscallWrapperError { +impl From for SyscallError { fn from(err: nix::Error) -> Self { - SyscallWrapperError::Nix(err) + SyscallError::Nix(err) } -} \ No newline at end of file +} +impl From for SyscallError { + fn from(err: crate::syscall::SyscallError) -> Self { + SyscallError::LibcontainerSyscall(err) + } +} diff --git a/crates/libcontainer/src/lib.rs b/crates/libcontainer/src/lib.rs index 1fd286234e..c335e68064 100644 --- a/crates/libcontainer/src/lib.rs +++ b/crates/libcontainer/src/lib.rs @@ -2,6 +2,7 @@ pub mod apparmor; pub mod capabilities; pub mod config; pub mod container; +pub mod error; pub mod hooks; pub mod namespaces; pub mod notify_socket; @@ -15,4 +16,3 @@ pub mod syscall; pub mod tty; pub mod utils; pub mod workload; -mod error; diff --git a/crates/libcontainer/src/namespaces.rs b/crates/libcontainer/src/namespaces.rs index 3d5495de01..09e17d01ee 100644 --- a/crates/libcontainer/src/namespaces.rs +++ b/crates/libcontainer/src/namespaces.rs @@ -7,7 +7,8 @@ //! UTS (hostname and domain information, processes will think they're running on servers with different names), //! Cgroup (Resource limits, execution priority etc.) -use crate::syscall::{syscall::create_syscall, Syscall, SyscallError}; +use crate::error::SyscallError; +use crate::syscall::{syscall::create_syscall, Syscall}; use nix::{fcntl, sched::CloneFlags, sys::stat, unistd}; use oci_spec::runtime::{LinuxNamespace, LinuxNamespaceType}; use std::collections; @@ -22,12 +23,6 @@ pub enum NamespaceError { #[source] err: SyscallError, }, - #[error("failed to set namespace")] - ApplyNamespaceUnixSyscallFailed { - namespace: Box, - #[source] - err: nix::Error, - }, } static ORDERED_NAMESPACES: &[CloneFlags] = &[ @@ -93,22 +88,20 @@ impl Namespaces { match namespace.path() { Some(path) => { let fd = fcntl::open(path, fcntl::OFlag::empty(), stat::Mode::empty()).map_err( - |err| NamespaceError::ApplyNamespaceUnixSyscallFailed { + |err| NamespaceError::ApplyNamespaceSyscallFailed { namespace: Box::new(namespace.to_owned()), - err, + err: err.into(), }, )?; self.command .set_ns(fd, get_clone_flag(namespace.typ())) .map_err(|err| NamespaceError::ApplyNamespaceSyscallFailed { namespace: Box::new(namespace.to_owned()), - err, + err: err.into(), })?; - unistd::close(fd).map_err(|err| { - NamespaceError::ApplyNamespaceUnixSyscallFailed { - namespace: Box::new(namespace.to_owned()), - err, - } + unistd::close(fd).map_err(|err| NamespaceError::ApplyNamespaceSyscallFailed { + namespace: Box::new(namespace.to_owned()), + err: err.into(), })?; } None => { @@ -116,7 +109,7 @@ impl Namespaces { .unshare(get_clone_flag(namespace.typ())) .map_err(|err| NamespaceError::ApplyNamespaceSyscallFailed { namespace: Box::new(namespace.to_owned()), - err, + err: err.into(), })?; } }