Skip to content

Commit d4d0691

Browse files
nrabulinskislp
authored andcommitted
Don't depend on the USER environment variable
Instead of reading $USER variable and finding the user based on that, use uid to lookup the user. Signed-off-by: Nikodem Rabuliński <[email protected]>
1 parent 1d29776 commit d4d0691

4 files changed

Lines changed: 9 additions & 14 deletions

File tree

crates/muvm/src/bin/muvm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,12 @@ fn main() -> Result<ExitCode> {
346346
}
347347
}
348348

349-
let username = env::var("USER").context("Failed to get username from environment")?;
350-
let user = User::from_name(&username)
349+
let uid = getuid().as_raw();
350+
let user = User::from_uid(uid.into())
351351
.map_err(Into::into)
352352
.and_then(|user| user.ok_or_else(|| anyhow!("requested entry not found")))
353-
.with_context(|| format!("Failed to get user `{username}` from user database"))?;
353+
.with_context(|| format!("Failed to get user `{uid}` from user database"))?;
354+
354355
let workdir_path = CString::new(
355356
user.dir
356357
.to_str()
@@ -389,8 +390,7 @@ fn main() -> Result<ExitCode> {
389390
tty: false,
390391
privileged: false,
391392
},
392-
username,
393-
uid: getuid().as_raw(),
393+
uid,
394394
gid: getgid().as_raw(),
395395
host_display: display,
396396
merged_rootfs: options.merged_rootfs,

crates/muvm/src/guest/bin/muvm-guest.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ fn main() -> Result<()> {
9696

9797
configure_network()?;
9898

99-
let run_path = match setup_user(
100-
options.username,
101-
Uid::from(options.uid),
102-
Gid::from(options.gid),
103-
) {
99+
let run_path = match setup_user(Uid::from(options.uid), Gid::from(options.gid)) {
104100
Ok(p) => p,
105101
Err(err) => return Err(err).context("Failed to set up user, bailing out"),
106102
};

crates/muvm/src/guest/user.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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-
pub fn setup_user(username: String, uid: Uid, gid: Gid) -> Result<PathBuf> {
11+
pub fn setup_user(uid: Uid, gid: Gid) -> Result<PathBuf> {
1212
setup_directories(uid, gid)?;
1313

1414
setresgid(gid, gid, Gid::from(0)).context("Failed to setgid")?;
@@ -24,10 +24,10 @@ pub fn setup_user(username: String, uid: Uid, gid: Gid) -> Result<PathBuf> {
2424
// See https://doc.rust-lang.org/std/env/fn.set_var.html#safety
2525
env::set_var("XDG_RUNTIME_DIR", &path);
2626

27-
let user = User::from_name(&username)
27+
let user = User::from_uid(uid)
2828
.map_err(Into::into)
2929
.and_then(|user| user.ok_or_else(|| anyhow!("requested entry not found")))
30-
.with_context(|| format!("Failed to get user `{username}` from user database"))?;
30+
.with_context(|| format!("Failed to get user `{uid}` from user database"))?;
3131

3232
{
3333
// SAFETY: Safe if and only if `muvm-guest` program is not multithreaded.

crates/muvm/src/utils/launch.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ impl FromStr for Emulator {
3838
#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
3939
pub struct GuestConfiguration {
4040
pub command: Launch,
41-
pub username: String,
4241
pub uid: u32,
4342
pub gid: u32,
4443
pub host_display: Option<String>,

0 commit comments

Comments
 (0)