Skip to content

Commit

Permalink
allow copy verbatim from host proc
Browse files Browse the repository at this point in the history
Signed-off-by: Brian L. Troutwine <[email protected]>
  • Loading branch information
blt committed Dec 12, 2023
1 parent 8bd79b3 commit 78e48ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.20.3-rc1]
### Changed
- ProcFs generator now allows files to be copied from host verbatim.

## [0.20.3-rc0]
### Added
- ProcFs generator is now suitable for use.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lading/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lading"
version = "0.20.3-rc0"
version = "0.20.3-rc1"
authors = ["Brian L. Troutwine <[email protected]>", "George Hahn <[email protected]"]
edition = "2021"
license = "MIT"
Expand Down
26 changes: 26 additions & 0 deletions lading/src/generator/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
//! use Rust's C-compatible data types.

use std::fs;
use std::path::StripPrefixError;
use std::str::FromStr;
use std::{fs::File, io::Write, num::NonZeroU32, path::PathBuf};

use lading_payload::procfs;
Expand All @@ -23,6 +25,16 @@ pub enum Error {
/// Wrapper around [`std::io::Error`]
#[error("Io error: {0}")]
Io(#[from] ::std::io::Error),
/// Unable to strip prefix from passed copy file
#[error(transparent)]
Strip(#[from] StripPrefixError),
}

fn default_copy_from_host() -> Vec<PathBuf> {
vec![
PathBuf::from_str("/proc/uptime").unwrap(),
PathBuf::from_str("/proc/stat").unwrap(),
]
}

#[derive(Debug, Deserialize, PartialEq)]
Expand All @@ -34,6 +46,9 @@ pub struct Config {
pub root: PathBuf,
/// Total number of processes created
pub total_processes: NonZeroU32,
/// Files to copy from host /proc to `root`.
#[serde(default = "default_copy_from_host")]
pub copy_from_host: Vec<PathBuf>,
}

/// The procfs generator.
Expand Down Expand Up @@ -120,6 +135,17 @@ impl ProcFs {
}
}

// SAFETY: By construction this pathbuf cannot fail to be created.
let prefix = PathBuf::from_str("/proc").unwrap();

for path in &config.copy_from_host {
let base = path.strip_prefix(&prefix)?;
let mut new_path = config.root.clone();
new_path.push(base);

fs::copy(path, new_path)?;
}

Ok(Self { shutdown })
}

Expand Down

0 comments on commit 78e48ab

Please sign in to comment.