Skip to content

Commit

Permalink
refactor the syscall error
Browse files Browse the repository at this point in the history
Signed-off-by: yihuaf <[email protected]>
  • Loading branch information
yihuaf committed May 4, 2023
1 parent 0eb35a1 commit 0a9234f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
20 changes: 13 additions & 7 deletions crates/libcontainer/src/error.rs
Original file line number Diff line number Diff line change
@@ -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<std::io::Error> for SyscallWrapperError {
impl From<std::io::Error> for SyscallError {
fn from(err: std::io::Error) -> Self {
SyscallWrapperError::Io(err)
SyscallError::Io(err)
}
}

impl From<nix::Error> for SyscallWrapperError {
impl From<nix::Error> for SyscallError {
fn from(err: nix::Error) -> Self {
SyscallWrapperError::Nix(err)
SyscallError::Nix(err)
}
}
}
impl From<crate::syscall::SyscallError> for SyscallError {
fn from(err: crate::syscall::SyscallError) -> Self {
SyscallError::LibcontainerSyscall(err)
}
}
2 changes: 1 addition & 1 deletion crates/libcontainer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,4 +16,3 @@ pub mod syscall;
pub mod tty;
pub mod utils;
pub mod workload;
mod error;
25 changes: 9 additions & 16 deletions crates/libcontainer/src/namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,12 +23,6 @@ pub enum NamespaceError {
#[source]
err: SyscallError,
},
#[error("failed to set namespace")]
ApplyNamespaceUnixSyscallFailed {
namespace: Box<LinuxNamespace>,
#[source]
err: nix::Error,
},
}

static ORDERED_NAMESPACES: &[CloneFlags] = &[
Expand Down Expand Up @@ -93,30 +88,28 @@ 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 => {
self.command
.unshare(get_clone_flag(namespace.typ()))
.map_err(|err| NamespaceError::ApplyNamespaceSyscallFailed {
namespace: Box::new(namespace.to_owned()),
err,
err: err.into(),
})?;
}
}
Expand Down

0 comments on commit 0a9234f

Please sign in to comment.