Skip to content

Commit

Permalink
removed unified syscall error
Browse files Browse the repository at this point in the history
Signed-off-by: yihuaf <yihuaf@unkies.org>
  • Loading branch information
yihuaf committed May 20, 2023
1 parent 8a6a8c6 commit c45e5f2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 43 deletions.
13 changes: 0 additions & 13 deletions crates/libcontainer/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
/// UnifiedSyscallError aims to simplify error handling of syscalls in
/// libcontainer. In many occasions, we mix nix::Error, std::io::Error and our
/// own syscall wrappers, which makes error handling complicated.
#[derive(Debug, thiserror::Error)]
pub enum UnifiedSyscallError {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Nix(#[from] nix::Error),
#[error(transparent)]
Syscall(#[from] crate::syscall::SyscallError),
}

#[derive(Debug, thiserror::Error)]
pub enum MissingSpecError {
#[error("missing process in spec")]
Expand Down
37 changes: 18 additions & 19 deletions crates/libcontainer/src/namespaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! UTS (hostname and domain information, processes will think they're running on servers with different names),
//! Cgroup (Resource limits, execution priority etc.)

use crate::error::UnifiedSyscallError;
use crate::syscall::{syscall::create_syscall, Syscall};
use nix::{fcntl, sched::CloneFlags, sys::stat, unistd};
use oci_spec::runtime::{LinuxNamespace, LinuxNamespaceType};
Expand All @@ -17,12 +16,12 @@ type Result<T> = std::result::Result<T, NamespaceError>;

#[derive(Debug, thiserror::Error)]
pub enum NamespaceError {
#[error("failed to set namespace")]
ApplyNamespaceSyscallFailed {
namespace: Box<LinuxNamespace>,
#[source]
err: UnifiedSyscallError,
},
#[error(transparent)]
Nix(#[from] nix::Error),
#[error(transparent)]
IO(#[from] std::io::Error),
#[error(transparent)]
Syscall(#[from] crate::syscall::SyscallError),
}

static ORDERED_NAMESPACES: &[CloneFlags] = &[
Expand Down Expand Up @@ -88,28 +87,28 @@ impl Namespaces {
match namespace.path() {
Some(path) => {
let fd = fcntl::open(path, fcntl::OFlag::empty(), stat::Mode::empty()).map_err(
|err| NamespaceError::ApplyNamespaceSyscallFailed {
namespace: Box::new(namespace.to_owned()),
err: err.into(),
|err| {
tracing::error!(?err, ?namespace, "failed to open namespace file");
err
},
)?;
self.command
.set_ns(fd, get_clone_flag(namespace.typ()))
.map_err(|err| NamespaceError::ApplyNamespaceSyscallFailed {
namespace: Box::new(namespace.to_owned()),
err: err.into(),
.map_err(|err| {
tracing::error!(?err, ?namespace, "failed to set namespace");
err
})?;
unistd::close(fd).map_err(|err| NamespaceError::ApplyNamespaceSyscallFailed {
namespace: Box::new(namespace.to_owned()),
err: err.into(),
unistd::close(fd).map_err(|err| {
tracing::error!(?err, ?namespace, "failed to close namespace file");
err
})?;
}
None => {
self.command
.unshare(get_clone_flag(namespace.typ()))
.map_err(|err| NamespaceError::ApplyNamespaceSyscallFailed {
namespace: Box::new(namespace.to_owned()),
err: err.into(),
.map_err(|err| {
tracing::error!(?err, ?namespace, "failed to unshare namespace");
err
})?;
}
}
Expand Down
12 changes: 5 additions & 7 deletions crates/libcontainer/src/rootfs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ use std::path::{Path, PathBuf};
pub enum DeviceError {
#[error("{0:?} is not a valid device path")]
InvalidDevicePath(std::path::PathBuf),
#[error("failed to bind dev")]
BindDev {
source: crate::error::UnifiedSyscallError,
},
#[error("failed syscall to create device")]
Syscall(#[from] crate::syscall::SyscallError),
#[error(transparent)]
Nix(#[from] nix::Error),
#[error(transparent)]
Other(Box<dyn std::error::Error + Send + Sync>),
#[error("{0}")]
Custom(String),
Expand Down Expand Up @@ -91,9 +89,9 @@ impl Device {
)
.map_err(|err| {
tracing::error!("failed to open bind dev {:?}: {}", full_container_path, err);
DeviceError::BindDev { source: err.into() }
err
})?;
close(fd).map_err(|err| DeviceError::BindDev { source: err.into() })?;
close(fd)?;
self.syscall
.mount(
Some(dev.path()),
Expand All @@ -108,7 +106,7 @@ impl Device {
full_container_path,
err
);
DeviceError::BindDev { source: err.into() }
err
})?;

Ok(())
Expand Down
6 changes: 2 additions & 4 deletions crates/libcontainer/src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use std::os::unix::io::AsRawFd;
use std::os::unix::prelude::RawFd;
use std::path::{Path, PathBuf};

use crate::error::UnifiedSyscallError;

#[derive(Debug)]
pub enum StdIO {
Stdin = 0,
Expand Down Expand Up @@ -60,7 +58,7 @@ pub enum TTYError {
source: nix::Error,
},
#[error("failed to create console socket fd")]
CreateConsoleSocketFd { source: UnifiedSyscallError },
CreateConsoleSocketFd { source: nix::Error },
#[error("could not create pseudo terminal")]
CreatePseudoTerminal { source: nix::Error },
#[error("failed to send pty master")]
Expand Down Expand Up @@ -90,7 +88,7 @@ pub fn setup_console_socket(
socket::SockFlag::empty(),
None,
)
.map_err(|err| TTYError::CreateConsoleSocketFd { source: err.into() })?;
.map_err(|err| TTYError::CreateConsoleSocketFd { source: err })?;
csocketfd = match socket::connect(
csocketfd,
&socket::UnixAddr::new(socket_name).map_err(|err| TTYError::InvalidSocketName {
Expand Down

0 comments on commit c45e5f2

Please sign in to comment.