Skip to content

Commit 183f414

Browse files
committed
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 <dramforever@live.com>
1 parent 5b35e56 commit 183f414

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

crates/muvm/src/guest/user.rs

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

66
use crate::guest::hidpipe::UINPUT_PATH;
77
use anyhow::{anyhow, Context, Result};
88
use nix::sys::wait::{waitpid, WaitStatus};
99
use nix::unistd::{fork, setresgid, setresuid, ForkResult, Gid, Uid, User};
1010

11+
fn mkdir_mode(path: &Path, mode: u32) -> Result<()> {
12+
DirBuilder::new()
13+
.mode(mode)
14+
.create(path)
15+
.with_context(|| format!("Failed to create {path:?}"))
16+
}
17+
1118
pub fn setup_user(uid: Uid, gid: Gid) -> Result<PathBuf> {
1219
setup_directories(uid, gid)?;
1320

21+
let path = PathBuf::from("/run/user").join(format!("{uid}"));
22+
23+
mkdir_mode(&path.parent().unwrap(), 0o755)?;
24+
mkdir_mode(&path, 0o700)?;
25+
26+
chown(&path, Some(uid.into()), Some(gid.into()))
27+
.with_context(|| format!("Failed to chown {path:?}"))?;
28+
1429
setresgid(gid, gid, Gid::from(0)).context("Failed to setgid")?;
1530
setresuid(uid, uid, Uid::from(0)).context("Failed to setuid")?;
1631

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();
2332
// SAFETY: Safe if and only if `muvm-guest` program is not multithreaded.
2433
// See https://doc.rust-lang.org/std/env/fn.set_var.html#safety
2534
env::set_var("XDG_RUNTIME_DIR", &path);

0 commit comments

Comments
 (0)