From 87abd3fb703b6b23cb95d3eb264ff627150235a1 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 19 Feb 2026 21:06:53 +0100 Subject: [PATCH] init: Maintain default service env vars separate from init env vars With this only the env vars set using export or explicitly specify for a service are passed to services (in addition to RUST_BACKTRACE, PATH and LD_LIBRARY_PATH). As such we no longer need to manually unset bootloader env vars to keep the environment clean. And finally this means that env var substitutions in shell scripts always use the bootloader provided env vars rather than locally specified ones. This does regress multi monitor support when there is no proper graphics driver. --- init.d/20_graphics | 2 -- init.d/40_drivers | 1 - init.d/50_rootfs | 1 - init/src/main.rs | 34 +++++++++++++++++++++------------- init/src/script.rs | 7 +++++-- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/init.d/20_graphics b/init.d/20_graphics index bf0719ca9e..ecfd423b4c 100644 --- a/init.d/20_graphics +++ b/init.d/20_graphics @@ -1,7 +1,5 @@ scheme input inputd notify FRAMEBUFFER_ADDR=$ FRAMEBUFFER_VIRT=$ FRAMEBUFFER_WIDTH=$ FRAMEBUFFER_HEIGHT=$ FRAMEBUFFER_STRIDE=$ vesad -unset FRAMEBUFFER_ADDR FRAMEBUFFER_VIRT FRAMEBUFFER_WIDTH FRAMEBUFFER_HEIGHT FRAMEBUFFER_STRIDE -#TODO: unset FRAMEBUFFER1 and beyond? scheme fbbootlog fbbootlogd # Activate framebuffer log VT, which disables kernel graphical debug inputd -A 1 diff --git a/init.d/40_drivers b/init.d/40_drivers index 4427519a94..82daf676c1 100644 --- a/init.d/40_drivers +++ b/init.d/40_drivers @@ -1,4 +1,3 @@ notify ps2d notify RSDP_ADDR=$ RSDP_SIZE=$ hwd -unset RSDP_ADDR RSDP_SIZE pcid-spawner --initfs diff --git a/init.d/50_rootfs b/init.d/50_rootfs index 07baec4e43..ece2c67124 100644 --- a/init.d/50_rootfs +++ b/init.d/50_rootfs @@ -1,2 +1 @@ REDOXFS_PASSWORD_ADDR=$ REDOXFS_PASSWORD_SIZE=$ redoxfs --uuid $REDOXFS_UUID file $REDOXFS_BLOCK -unset REDOXFS_UUID REDOXFS_BLOCK REDOXFS_PASSWORD_ADDR REDOXFS_PASSWORD_SIZE diff --git a/init/src/main.rs b/init/src/main.rs index de8a81bc67..27afab8cad 100644 --- a/init/src/main.rs +++ b/init/src/main.rs @@ -1,4 +1,6 @@ +use std::collections::BTreeMap; use std::env; +use std::ffi::OsString; use std::io::Result; use std::path::Path; @@ -23,6 +25,7 @@ fn switch_stdio(stdio: &str) -> Result<()> { struct InitConfig { log_debug: bool, skip_cmd: Vec, + envs: BTreeMap, } impl InitConfig { @@ -37,15 +40,20 @@ impl InitConfig { Self { log_debug, skip_cmd, + envs: BTreeMap::from([("RUST_BACKTRACE".to_owned(), "1".into())]), } } } -fn switch_root(prefix: &Path, etcdir: &Path, config: &InitConfig) { - unsafe { - env::set_var("PATH", prefix.join("bin")); - env::set_var("LD_LIBRARY_PATH", prefix.join("lib")); - } +fn switch_root(prefix: &Path, etcdir: &Path, config: &mut InitConfig) { + config + .envs + .insert("PATH".to_owned(), prefix.join("bin").into_os_string()); + config.envs.insert( + "LD_LIBRARY_PATH".to_owned(), + prefix.join("lib").into_os_string(), + ); + let entries = match config::config_for_dirs(&[ prefix.join("lib").join("init.d"), etcdir.join("init.d"), @@ -64,7 +72,7 @@ fn switch_root(prefix: &Path, etcdir: &Path, config: &InitConfig) { } } -fn run(file: &Path, config: &InitConfig) -> Result<()> { +fn run(file: &Path, config: &mut InitConfig) -> Result<()> { let (script, errors) = script::Script::from_file(file)?; for error in errors { @@ -81,7 +89,7 @@ fn run(file: &Path, config: &InitConfig) -> Result<()> { Ok(()) } -fn run_command(cmd: Command, config: &InitConfig) { +fn run_command(cmd: Command, config: &mut InitConfig) { match cmd { Command::Nothing => {} Command::Echo(text) => println!("{text}"), @@ -105,7 +113,7 @@ fn run_command(cmd: Command, config: &InitConfig) { return; } - let mut command = cmd.into_command(); + let mut command = cmd.into_command(&config.envs); match command.spawn() { Ok(_child) => {} @@ -118,7 +126,7 @@ fn run_command(cmd: Command, config: &InitConfig) { return; } - let command = cmd.into_command(); + let command = cmd.into_command(&config.envs); daemon::Daemon::spawn(command); } @@ -128,7 +136,7 @@ fn run_command(cmd: Command, config: &InitConfig) { return; } - let command = cmd.into_command(); + let command = cmd.into_command(&config.envs); daemon::SchemeDaemon::spawn(command, &scheme); } @@ -138,7 +146,7 @@ fn run_command(cmd: Command, config: &InitConfig) { return; } - let mut command = cmd.into_command(); + let mut command = cmd.into_command(&config.envs); let mut child = match command.spawn() { Ok(child) => child, @@ -162,11 +170,11 @@ fn run_command(cmd: Command, config: &InitConfig) { } fn main() { - let init_config = InitConfig::new(); + let mut init_config = InitConfig::new(); switch_root( Path::new("/scheme/initfs"), Path::new("/scheme/initfs/etc"), - &init_config, + &mut init_config, ); libredox::call::setrens(0, 0).expect("init: failed to enter null namespace"); diff --git a/init/src/script.rs b/init/src/script.rs index ad03a289bc..c22ee99b19 100644 --- a/init/src/script.rs +++ b/init/src/script.rs @@ -1,3 +1,5 @@ +use std::collections::BTreeMap; +use std::ffi::OsString; use std::path::{Path, PathBuf}; use std::{env, fs, io, iter, process}; @@ -149,9 +151,10 @@ impl Process { } } - pub fn into_command(self) -> process::Command { + pub fn into_command(self, base_envs: &BTreeMap) -> process::Command { let mut command = process::Command::new(self.cmd); - command.args(self.args).envs(self.envs); + command.args(self.args); + command.env_clear().envs(base_envs).envs(self.envs); command } }