Skip to content

Commit 0393a2e

Browse files
committed
muvm-guest: pass config to split remote process
Signed-off-by: Val Packett <[email protected]>
1 parent 366cda3 commit 0393a2e

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

crates/muvm/src/bin/muvm.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,19 @@ fn main() -> Result<ExitCode> {
478478

479479
let krun_config_env = CString::new(format!("KRUN_CONFIG={}", config_file.path().display()))
480480
.context("Failed to process config_file var as it contains NUL character")?;
481+
#[allow(unused_assignments)] // wat?
482+
let mut muvm_config_env = None; // keep in this scope
481483
let mut env: Vec<*const c_char> = vec![krun_config_env.as_ptr()];
482484
if custom_init {
483485
env.push(c"KRUN_INIT_PID1=1".as_ptr());
486+
muvm_config_env = Some(
487+
CString::new(format!(
488+
"MUVM_REMOTE_CONFIG={}",
489+
muvm_config_file.path().display()
490+
))
491+
.context("Failed to process internal config path as it contains NUL character")?,
492+
);
493+
env.push(muvm_config_env.as_ref().unwrap().as_ptr());
484494
}
485495
env.push(std::ptr::null());
486496

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::fs::File;
22
use std::io::Read;
33
use std::os::fd::AsFd;
44
use std::panic::catch_unwind;
5-
use std::path::PathBuf;
65
use std::process::{Command, ExitCode};
76
use std::{cmp, env, fs, thread};
87

@@ -26,16 +25,32 @@ use rustix::process::{getrlimit, setrlimit, Resource};
2625

2726
const KRUN_CONFIG: &str = "KRUN_CONFIG";
2827

28+
fn parse_config(config_path: String) -> Result<GuestConfiguration> {
29+
let mut config_file = File::open(&config_path)?;
30+
let mut config_buf = Vec::new();
31+
config_file.read_to_end(&mut config_buf)?;
32+
fs::remove_file(config_path).context("Unable to delete temporary muvm configuration file")?;
33+
if let Ok(krun_config_path) = env::var(KRUN_CONFIG) {
34+
fs::remove_file(krun_config_path)
35+
.context("Unable to delete temporary krun configuration file")?;
36+
// SAFETY: We are single-threaded at this point
37+
env::remove_var(KRUN_CONFIG);
38+
}
39+
// SAFETY: We are single-threaded at this point
40+
env::remove_var("KRUN_WORKDIR");
41+
Ok(serde_json::from_slice::<GuestConfiguration>(&config_buf)?)
42+
}
43+
2944
fn main() -> Result<ExitCode> {
3045
env_logger::init();
3146

3247
let binary_path = env::args().next().context("arg0")?;
3348
let bb = binary_path.split('/').next_back().context("arg0 split")?;
3449
match bb {
35-
"muvm-configure-network" => return configure_network(),
50+
"muvm-configure-network" => return configure_network().map(|()| ExitCode::SUCCESS),
3651
"muvm-pwbridge" => {
3752
bridge_loop_with_listenfd::<PipeWireProtocolHandler>(pipewire_sock_path);
38-
return Ok(());
53+
return Ok(ExitCode::SUCCESS);
3954
},
4055
"muvm-x11bridge" => {
4156
bridge_loop_with_listenfd::<X11ProtocolHandler>(|| "/tmp/.X11-unix/X1".to_owned());
@@ -51,9 +66,13 @@ fn main() -> Result<ExitCode> {
5166
},
5267
"muvm-remote" => {
5368
let rt = tokio::runtime::Runtime::new().unwrap();
54-
let mut command_args = env::args().skip(1);
55-
let command = command_args.next().context("command name")?;
56-
return rt.block_on(server_main(PathBuf::from(command), command_args.collect()));
69+
let config_path =
70+
env::var("MUVM_REMOTE_CONFIG").context("expected MUVM_REMOTE_CONFIG to be set")?;
71+
let options = parse_config(config_path)?;
72+
return rt.block_on(server_main(
73+
options.command.command,
74+
options.command.command_args,
75+
));
5776
},
5877
_ => { /* continue with all-in-one mode */ },
5978
}
@@ -66,19 +85,7 @@ fn main() -> Result<ExitCode> {
6685
let config_path = env::args()
6786
.nth(1)
6887
.context("expected configuration file path")?;
69-
let mut config_file = File::open(&config_path)?;
70-
let mut config_buf = Vec::new();
71-
config_file.read_to_end(&mut config_buf)?;
72-
fs::remove_file(config_path).context("Unable to delete temporary muvm configuration file")?;
73-
if let Ok(krun_config_path) = env::var(KRUN_CONFIG) {
74-
fs::remove_file(krun_config_path)
75-
.context("Unable to delete temporary krun configuration file")?;
76-
// SAFETY: We are single-threaded at this point
77-
env::remove_var(KRUN_CONFIG);
78-
}
79-
// SAFETY: We are single-threaded at this point
80-
env::remove_var("KRUN_WORKDIR");
81-
let options = serde_json::from_slice::<GuestConfiguration>(&config_buf)?;
88+
let options = parse_config(config_path)?;
8289

8390
{
8491
const ESYNC_RLIMIT_NOFILE: u64 = 524288;

0 commit comments

Comments
 (0)