| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::io::{self, Read, Write}; |
| 6 | use std::path::{Path, PathBuf}; |
| 7 | use std::process::{Command, Stdio}; |
| 8 | |
| 9 | use serde::{Deserialize, Serialize}; |
| 10 | |
| 11 | use super::unpack::{Ownership, UnpackError, UnpackStats, unpack_layers}; |
| 12 | use crate::sandbox::idmap::{IdMap, enter_mapped_userns}; |
| 13 | |
| 14 | |
| 15 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 16 | pub(super) struct UnpackRequest { |
| 17 | pub map: IdMap, |
| 18 | pub rootfs: PathBuf, |
| 19 | pub layers: Vec<PathBuf>, |
| 20 | pub max_bytes: u64, |
| 21 | } |
| 22 | |
| 23 | |
| 24 | pub(super) fn run_unpack_helper( |
| 25 | exe: &Path, |
| 26 | req: &UnpackRequest, |
| 27 | ) -> Result<UnpackStats, UnpackError> { |
| 28 | let body = serde_json::to_vec(req).map_err(|err| UnpackError::Helper(err.to_string()))?; |
| 29 | let mut child = Command::new(exe) |
| 30 | .arg("__sandbox-unpack") |
| 31 | .stdin(Stdio::piped()) |
| 32 | .stdout(Stdio::piped()) |
| 33 | .stderr(Stdio::piped()) |
| 34 | .spawn()?; |
| 35 | if let Some(mut stdin) = child.stdin.take() { |
| 36 | |
| 37 | |
| 38 | let _ = stdin.write_all(&body); |
| 39 | } |
| 40 | let out = child.wait_with_output()?; |
| 41 | if !out.status.success() { |
| 42 | let stderr = String::from_utf8_lossy(&out.stderr); |
| 43 | return Err(UnpackError::Helper(format!( |
| 44 | "{} ({})", |
| 45 | stderr.trim(), |
| 46 | out.status |
| 47 | ))); |
| 48 | } |
| 49 | serde_json::from_slice(&out.stdout) |
| 50 | .map_err(|err| UnpackError::Helper(format!("bad stats from the helper: {err}"))) |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | pub fn unpack_helper_main() -> i32 { |
| 58 | match run_helper() { |
| 59 | Ok(()) => 0, |
| 60 | Err(message) => { |
| 61 | eprintln!("forge-sandbox: {message}"); |
| 62 | 125 |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | fn run_helper() -> Result<(), String> { |
| 68 | let mut input = Vec::new(); |
| 69 | io::stdin() |
| 70 | .read_to_end(&mut input) |
| 71 | .map_err(|err| format!("cannot read the request: {err}"))?; |
| 72 | let req: UnpackRequest = |
| 73 | serde_json::from_slice(&input).map_err(|err| format!("bad request: {err}"))?; |
| 74 | enter_mapped_userns(&req.map).map_err(|err| err.to_string())?; |
| 75 | let ownership = Ownership::Preserve { |
| 76 | size: req.map.size(), |
| 77 | }; |
| 78 | let stats = unpack_layers(&req.rootfs, &req.layers, req.max_bytes, ownership) |
| 79 | .map_err(|err| err.to_string())?; |
| 80 | let json = serde_json::to_vec(&stats).map_err(|err| err.to_string())?; |
| 81 | let mut stdout = io::stdout().lock(); |
| 82 | stdout |
| 83 | .write_all(&json) |
| 84 | .and_then(|()| stdout.flush()) |
| 85 | .map_err(|err| format!("cannot write the stats: {err}")) |
| 86 | } |