From ff79f90a1b3641a8d3606a01fc2682d4d78c1677 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 28 Dec 2024 14:09:58 +0100 Subject: [PATCH 1/4] Add more context to canonicalize errors --- crates/container/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/container/src/lib.rs b/crates/container/src/lib.rs index b49373b7..36cd2d2b 100644 --- a/crates/container/src/lib.rs +++ b/crates/container/src/lib.rs @@ -9,7 +9,7 @@ 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}; @@ -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)?; From b1c4ef8c63c804b00085e4f6bdfc7fdc0560bc42 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 28 Dec 2024 14:10:14 +0100 Subject: [PATCH 2/4] Remove unused impl From> for ContainerError --- crates/container/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/container/src/lib.rs b/crates/container/src/lib.rs index 36cd2d2b..c9fcdb0e 100644 --- a/crates/container/src/lib.rs +++ b/crates/container/src/lib.rs @@ -450,7 +450,7 @@ pub enum Error { #[derive(Debug, Error)] enum ContainerError { #[error(transparent)] - Run(#[from] Box), + Run(Box), #[error(transparent)] Io(#[from] io::Error), From e627cf03bd1e2baffcc493ef5aed8fc16ec5e516 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 28 Dec 2024 14:10:42 +0100 Subject: [PATCH 3/4] Highlight io errors as such in ContainerError --- crates/container/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/container/src/lib.rs b/crates/container/src/lib.rs index c9fcdb0e..1d3a90d9 100644 --- a/crates/container/src/lib.rs +++ b/crates/container/src/lib.rs @@ -451,7 +451,7 @@ pub enum Error { enum ContainerError { #[error(transparent)] Run(Box), - #[error(transparent)] + #[error("io")] Io(#[from] io::Error), // Errors from linux system functions From fa5d60f3fd3773b730263f6451df2b44e4496e59 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 28 Dec 2024 14:24:19 +0100 Subject: [PATCH 4/4] Add more context to set_current_dir errors --- crates/container/src/lib.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/container/src/lib.rs b/crates/container/src/lib.rs index 1d3a90d9..87dcfe2c 100644 --- a/crates/container/src/lib.rs +++ b/crates/container/src/lib.rs @@ -1,7 +1,7 @@ // 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}; @@ -358,6 +358,26 @@ fn add_mount>( Ok(()) } +fn set_current_dir(path: impl AsRef) -> 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)? };