Skip to content

Commit d89ddbe

Browse files
sbrivio-rhteohhanhui
authored andcommitted
env: For debug builds, source muvm binaries from current executable path
...and, for non-debug/non-development builds, source them from the PATH variable only. Signed-off-by: Stefano Brivio <[email protected]> Co-authored-by: Teoh Han Hui <[email protected]>
1 parent 44d33a3 commit d89ddbe

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

crates/muvm/src/bin/muvm.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,17 @@ fn main() -> Result<()> {
370370
let muvm_server_path = find_muvm_exec("muvm-server")?;
371371

372372
let mut muvm_guest_args: Vec<String> = vec![
373-
muvm_guest_path,
373+
muvm_guest_path
374+
.to_str()
375+
.context("Failed to process `muvm-guest` path as it contains invalid UTF-8")?
376+
.to_owned(),
374377
username,
375378
format!("{uid}", uid = getuid().as_raw()),
376379
format!("{gid}", gid = getgid().as_raw()),
377-
muvm_server_path,
380+
muvm_server_path
381+
.to_str()
382+
.context("Failed to process `muvm-server` path as it contains invalid UTF-8")?
383+
.to_owned(),
378384
command
379385
.to_str()
380386
.context("Failed to process command as it contains invalid UTF-8")?

crates/muvm/src/env.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use std::collections::HashMap;
22
use std::env::{self, VarError};
33
use std::fs;
44
use std::io::ErrorKind;
5-
use std::path::Path;
5+
use std::path::{Path, PathBuf};
66

7-
use super::utils::env::find_in_path;
87
use anyhow::{Context, Result};
98
use log::debug;
109

10+
#[cfg(not(debug_assertions))]
11+
use crate::utils::env::find_in_path;
12+
1113
/// Automatically pass these environment variables to the microVM, if they are
1214
/// set.
1315
const WELL_KNOWN_ENV_VARS: [&str; 20] = [
@@ -101,23 +103,31 @@ pub fn prepare_env_vars(env: Vec<(String, Option<String>)>) -> Result<HashMap<St
101103
Ok(env_map)
102104
}
103105

104-
pub fn find_muvm_exec<P>(program: P) -> Result<String>
106+
#[cfg(not(debug_assertions))]
107+
pub fn find_muvm_exec<P>(program: P) -> Result<PathBuf>
105108
where
106109
P: AsRef<Path>,
107110
{
108111
let program = program.as_ref();
112+
109113
let path = find_in_path(program)
110114
.with_context(|| format!("Failed to check existence of {program:?}"))?;
111-
let path = if let Some(path) = path {
112-
path
113-
} else {
114-
let path = env::current_exe().and_then(|p| p.canonicalize());
115-
let path = path.context("Failed to get path of current running executable")?;
116-
path.with_file_name(program)
117-
};
118-
let path = path.to_str().with_context(|| {
119-
format!("Failed to process {program:?} path as it contains invalid UTF-8")
120-
})?;
121-
122-
Ok(path.to_string())
115+
let path = path.with_context(|| format!("Could not find {program:?}"))?;
116+
117+
Ok(path)
118+
}
119+
120+
#[cfg(debug_assertions)]
121+
pub fn find_muvm_exec<P>(program: P) -> Result<PathBuf>
122+
where
123+
P: AsRef<Path>,
124+
{
125+
let program = program.as_ref();
126+
127+
let path = env::current_exe()
128+
.and_then(|p| p.canonicalize())
129+
.context("Failed to get path of current running executable")?;
130+
let path = path.with_file_name(program);
131+
132+
Ok(path)
123133
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::process::Command;
55

66
use anyhow::{Context, Result};
77
use log::debug;
8+
use muvm::env::find_muvm_exec;
89
use muvm::guest::cli_options::options;
910
use muvm::guest::fex::setup_fex;
1011
use muvm::guest::mount::mount_filesystems;
@@ -63,7 +64,8 @@ fn main() -> Result<()> {
6364

6465
// Before switching to the user, start another instance of muvm-server to serve
6566
// launch requests as root.
66-
Command::new("muvm-server")
67+
let muvm_server_path = find_muvm_exec("muvm-server")?;
68+
Command::new(muvm_server_path)
6769
.spawn()
6870
.context("Failed to execute `muvm-server` as child process")?;
6971

0 commit comments

Comments
 (0)