Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

muvm-guest: Find actual path for muvm-server before trying to execute it #112

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/muvm/src/bin/muvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,17 @@ fn main() -> Result<()> {
let muvm_server_path = find_muvm_exec("muvm-server")?;

let mut muvm_guest_args: Vec<String> = vec![
muvm_guest_path,
muvm_guest_path
.to_str()
.context("Failed to process `muvm-guest` path as it contains invalid UTF-8")?
.to_owned(),
username,
format!("{uid}", uid = getuid().as_raw()),
format!("{gid}", gid = getgid().as_raw()),
muvm_server_path,
muvm_server_path
.to_str()
.context("Failed to process `muvm-server` path as it contains invalid UTF-8")?
.to_owned(),
command
.to_str()
.context("Failed to process command as it contains invalid UTF-8")?
Expand Down
40 changes: 25 additions & 15 deletions crates/muvm/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use std::collections::HashMap;
use std::env::{self, VarError};
use std::fs;
use std::io::ErrorKind;
use std::path::Path;
use std::path::{Path, PathBuf};

use super::utils::env::find_in_path;
use anyhow::{Context, Result};
use log::debug;

#[cfg(not(debug_assertions))]
use crate::utils::env::find_in_path;

/// Automatically pass these environment variables to the microVM, if they are
/// set.
const WELL_KNOWN_ENV_VARS: [&str; 20] = [
Expand Down Expand Up @@ -101,23 +103,31 @@ pub fn prepare_env_vars(env: Vec<(String, Option<String>)>) -> Result<HashMap<St
Ok(env_map)
}

pub fn find_muvm_exec<P>(program: P) -> Result<String>
#[cfg(not(debug_assertions))]
pub fn find_muvm_exec<P>(program: P) -> Result<PathBuf>
where
P: AsRef<Path>,
{
let program = program.as_ref();

let path = find_in_path(program)
.with_context(|| format!("Failed to check existence of {program:?}"))?;
let path = if let Some(path) = path {
path
} else {
let path = env::current_exe().and_then(|p| p.canonicalize());
let path = path.context("Failed to get path of current running executable")?;
path.with_file_name(program)
};
let path = path.to_str().with_context(|| {
format!("Failed to process {program:?} path as it contains invalid UTF-8")
})?;

Ok(path.to_string())
let path = path.with_context(|| format!("Could not find {program:?}"))?;

Ok(path)
}

#[cfg(debug_assertions)]
pub fn find_muvm_exec<P>(program: P) -> Result<PathBuf>
where
P: AsRef<Path>,
{
let program = program.as_ref();

let path = env::current_exe()
.and_then(|p| p.canonicalize())
.context("Failed to get path of current running executable")?;
let path = path.with_file_name(program);

Ok(path)
}
4 changes: 3 additions & 1 deletion crates/muvm/src/guest/bin/muvm-guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::process::Command;

use anyhow::{Context, Result};
use log::debug;
use muvm::env::find_muvm_exec;
use muvm::guest::cli_options::options;
use muvm::guest::fex::setup_fex;
use muvm::guest::mount::mount_filesystems;
Expand Down Expand Up @@ -65,7 +66,8 @@ fn main() -> Result<()> {

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

Expand Down
Loading