Skip to content

Commit 51c3960

Browse files
dramforeverslp
authored andcommitted
Create $XDG_RUNTIME_DIR in /run/user/{uid}
Using tempfile::Builder::tempdir() defaults to creating this directory in /tmp, which is shared with the host and often somewhat persistent. This causes one extra directory to be created in the host /tmp every time muvm runs. Since we mount /run as a tmpfs now in the guest now, just create $XDG_RUNTIME_DIR in /run/user/{uid}, a common default. Specifically: - Create /run/user as 0o755 (rwxr-xr-x) owned by root:root - Create /run/user/{uid} as 0o700 (rwx------) owned by uid:gid Signed-off-by: Vivian Wang <[email protected]>
1 parent fe3fe12 commit 51c3960

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

crates/muvm/src/guest/user.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
use std::env;
2-
use std::fs::{self, Permissions};
3-
use std::os::unix::fs::{chown, PermissionsExt as _};
2+
use std::fs;
3+
use std::os::unix::fs::chown;
44
use std::path::{Path, PathBuf};
55

66
use crate::guest::hidpipe::UINPUT_PATH;
7+
use crate::utils::fs::mkdir_mode;
78
use anyhow::{anyhow, Context, Result};
89
use nix::sys::wait::{waitpid, WaitStatus};
910
use nix::unistd::{fork, setresgid, setresuid, ForkResult, Gid, Uid, User};
1011

1112
pub fn setup_user(uid: Uid, gid: Gid) -> Result<PathBuf> {
1213
setup_directories(uid, gid)?;
1314

15+
let path = PathBuf::from(format!("/run/user/{uid}"));
16+
17+
mkdir_mode(path.parent().unwrap(), 0o755)?;
18+
mkdir_mode(&path, 0o700)?;
19+
20+
chown(&path, Some(uid.into()), Some(gid.into()))
21+
.with_context(|| format!("Failed to chown {path:?}"))?;
22+
1423
setresgid(gid, gid, Gid::from(0)).context("Failed to setgid")?;
1524
setresuid(uid, uid, Uid::from(0)).context("Failed to setuid")?;
1625

17-
let path = tempfile::Builder::new()
18-
.prefix(&format!("muvm-run-{uid}-"))
19-
.permissions(Permissions::from_mode(0o700))
20-
.tempdir()
21-
.context("Failed to create temp dir for `XDG_RUNTIME_DIR`")?
22-
.into_path();
2326
// SAFETY: Safe if and only if `muvm-guest` program is not multithreaded.
2427
// See https://doc.rust-lang.org/std/env/fn.set_var.html#safety
2528
env::set_var("XDG_RUNTIME_DIR", &path);

crates/muvm/src/utils/fs.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fs;
2-
use std::os::unix::fs::PermissionsExt as _;
2+
use std::os::unix::fs::{DirBuilderExt as _, PermissionsExt as _};
33
use std::path::{Path, PathBuf};
44

55
use anyhow::{Context, Result};
@@ -25,3 +25,10 @@ where
2525

2626
Ok(None)
2727
}
28+
29+
pub fn mkdir_mode<P: AsRef<Path>>(path: P, mode: u32) -> Result<()> {
30+
fs::DirBuilder::new()
31+
.mode(mode)
32+
.create(&path)
33+
.with_context(|| format!("Failed to create {:?}", path.as_ref()))
34+
}

0 commit comments

Comments
 (0)