Skip to content

Commit

Permalink
fix: namespace setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Yato202010 committed Jan 12, 2025
1 parent fff746a commit c131bef
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions tests/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,27 @@ const EXPECTED_STATUS: i32 = 23;
static ONCE: Once = std::sync::Once::new();

fn setup_namespaces() -> Result<(), Errno> {
let mut err = None;
ONCE.call_once(|| {
// Hold on to the uid in the parent namespace.
let uid = getuid();
static mut ST_ERR: Option<Errno> = None;
let mut err = unsafe { ST_ERR };
if err.is_none() {
ONCE.call_once(|| {
// Hold on to the uid in the parent namespace.
let uid = getuid();

if let Err(e) = unshare(CloneFlags::CLONE_NEWUSER.union(CloneFlags::CLONE_NEWNS)) {
return err = Some(e);
};
// Map user as uid 1000.
if let Err(e) = fs::OpenOptions::new()
.write(true)
.open("/proc/self/uid_map")
.and_then(|mut f| f.write(format!("1000 {} 1\n", uid).as_bytes()))
{
err = Some(Errno::from_raw(e.raw_os_error().unwrap_or(-1)))
}
});
if let Err(e) = unshare(CloneFlags::CLONE_NEWUSER.union(CloneFlags::CLONE_NEWNS)) {
return err = Some(e);
};
// Map user as uid 1000.
if let Err(e) = fs::OpenOptions::new()
.write(true)
.open("/proc/self/uid_map")
.and_then(|mut f| f.write(format!("1000 {} 1\n", uid).as_bytes()))
{
err = Some(Errno::from_raw(e.raw_os_error().unwrap_or(-1)))
}
});
unsafe { ST_ERR = err };
}
if let Some(e) = err {
Err(e)
} else {
Expand Down

0 comments on commit c131bef

Please sign in to comment.