Skip to content

Commit b682370

Browse files
authored
uh_init: Handle safety around set_var (#514)
Part of #288
1 parent 65843de commit b682370

File tree

1 file changed

+13
-6
lines changed
  • openhcl/underhill_init/src

1 file changed

+13
-6
lines changed

openhcl/underhill_init/src/lib.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ fn setup(
204204

205205
use_host_entropy().context("use host entropy")?;
206206

207-
for setup in &options.setup_script {
207+
Ok(())
208+
}
209+
210+
fn run_setup_scripts(scripts: &[String]) -> anyhow::Result<Vec<(String, String)>> {
211+
let mut new_env = Vec::new();
212+
for setup in scripts {
208213
log::info!("Running provided setup script {}", setup);
209214

210215
let result = Command::new("/bin/sh")
@@ -226,17 +231,18 @@ fn setup(
226231
.and_then(|line| line.split_once('='))
227232
{
228233
log::info!("setting env var {}={}", key, value);
229-
std::env::set_var(key, value);
234+
new_env.push((key.into(), value.into()));
230235
}
231236
}
232237
}
233-
Ok(())
238+
Ok(new_env)
234239
}
235240

236-
fn run(options: &Options) -> anyhow::Result<()> {
241+
fn run(options: &Options, env: impl IntoIterator<Item = (String, String)>) -> anyhow::Result<()> {
237242
let mut command = Command::new(UNDERHILL_PATH);
238243
command.arg("--pid").arg("/run/underhill.pid");
239244
command.args(&options.underhill_args);
245+
command.envs(env);
240246

241247
// Update the file descriptor limit for the main process, since large VMs
242248
// require lots of fds. There is no downside to a larger value except that
@@ -423,7 +429,6 @@ fn timestamp() -> u64 {
423429

424430
fn do_main() -> anyhow::Result<()> {
425431
let boot_time = timestamp();
426-
std::env::set_var("KERNEL_BOOT_TIME", boot_time.to_string());
427432

428433
init_logging();
429434

@@ -550,6 +555,8 @@ fn do_main() -> anyhow::Result<()> {
550555
];
551556

552557
setup(&stat_files, &options, writes, &filesystems)?;
558+
let mut new_env = run_setup_scripts(&options.setup_script)?;
559+
new_env.push(("KERNEL_BOOT_TIME".into(), boot_time.to_string()));
553560

554561
if matches!(
555562
std::env::var("OPENHCL_NVME_VFIO").as_deref(),
@@ -574,7 +581,7 @@ fn do_main() -> anyhow::Result<()> {
574581
}
575582
});
576583

577-
run(&options)
584+
run(&options, new_env)
578585
}
579586

580587
pub fn main() -> ! {

0 commit comments

Comments
 (0)