From addf62087c17b9ace90f1700b9805624ad02f8d8 Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Thu, 10 Sep 2020 11:19:01 -0500 Subject: [PATCH] agent/rustjail: implement functions to chroot Use conditional compilation (#[cfg]) to change chroot behaviour at compilation time. For example, such function will just return `Ok(())` when the unit tests are being compiled, otherwise real chroot operation is performed. Signed-off-by: Julio Montes --- src/agent/rustjail/src/mount.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/agent/rustjail/src/mount.rs b/src/agent/rustjail/src/mount.rs index 1ce746d72d..c8237ad8ed 100644 --- a/src/agent/rustjail/src/mount.rs +++ b/src/agent/rustjail/src/mount.rs @@ -483,6 +483,14 @@ fn parse_mount_table() -> Result> { Ok(infos) } +#[inline(always)] +fn chroot(path: &P) -> Result<(), nix::Error> { + #[cfg(not(test))] + return unistd::chroot(path); + #[cfg(test)] + return Ok(()); +} + pub fn ms_move_root(rootfs: &str) -> Result { unistd::chdir(rootfs)?; let mount_infos = parse_mount_table()?; @@ -544,7 +552,7 @@ pub fn ms_move_root(rootfs: &str) -> Result { MsFlags::MS_MOVE, None::<&str>, )?; - unistd::chroot(".")?; + chroot(".")?; unistd::chdir("/")?; Ok(true)