Skip to content

Commit

Permalink
Merge pull request #378 from jplatte/jplatte/container-error
Browse files Browse the repository at this point in the history
Add more context to container errors
  • Loading branch information
ikeycode authored Dec 28, 2024
2 parents 2b66d7b + fa5d60f commit 84498b7
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions crates/container/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-FileCopyrightText: Copyright © 2020-2024 Serpent OS Developers
//
// SPDX-License-Identifier: MPL-2.0
use std::env::set_current_dir;

use std::io;
use std::os::fd::AsRawFd;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::ptr::addr_of_mut;
use std::sync::atomic::{AtomicI32, Ordering};

use fs_err::{self as fs, copy, create_dir_all, remove_dir};
use fs_err::{self as fs, copy, create_dir_all, remove_dir, PathExt as _};
use nix::libc::SIGCHLD;
use nix::mount::{mount, umount2, MntFlags, MsFlags};
use nix::sched::{clone, CloneFlags};
Expand Down Expand Up @@ -265,7 +265,7 @@ fn pivot(root: &Path, binds: &[Bind]) -> Result<(), ContainerError> {
add_mount(Some(root), root, None, MsFlags::MS_BIND)?;

for bind in binds {
let source = bind.source.canonicalize()?;
let source = bind.source.fs_err_canonicalize()?;
let target = root.join(bind.target.strip_prefix("/").unwrap_or(&bind.target));

add_mount(Some(&source), &target, None, MsFlags::MS_BIND)?;
Expand Down Expand Up @@ -358,6 +358,26 @@ fn add_mount<T: AsRef<Path>>(
Ok(())
}

fn set_current_dir(path: impl AsRef<Path>) -> io::Result<()> {
#[derive(Debug, Error)]
#[error("failed to set current directory to `{}`", path.display())]
struct SetCurrentDirError {
source: io::Error,
path: PathBuf,
}

let path = path.as_ref();
std::env::set_current_dir(path).map_err(|source| {
io::Error::new(
source.kind(),
SetCurrentDirError {
source,
path: path.to_owned(),
},
)
})
}

fn ignore_sigint() -> Result<(), nix::Error> {
let action = SigAction::new(SigHandler::SigIgn, SaFlags::empty(), SigSet::empty());
unsafe { sigaction(Signal::SIGINT, &action)? };
Expand Down Expand Up @@ -450,8 +470,8 @@ pub enum Error {
#[derive(Debug, Error)]
enum ContainerError {
#[error(transparent)]
Run(#[from] Box<dyn std::error::Error>),
#[error(transparent)]
Run(Box<dyn std::error::Error>),
#[error("io")]
Io(#[from] io::Error),

// Errors from linux system functions
Expand Down

0 comments on commit 84498b7

Please sign in to comment.