Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more context to container errors #378

Merged
merged 4 commits into from
Dec 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading