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.
This commit is contained in:
bjorn3
2026-02-19 21:06:53 +01:00
parent b7524c3a65
commit 87abd3fb70
5 changed files with 26 additions and 19 deletions
-2
View File
@@ -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
-1
View File
@@ -1,4 +1,3 @@
notify ps2d
notify RSDP_ADDR=$ RSDP_SIZE=$ hwd
unset RSDP_ADDR RSDP_SIZE
pcid-spawner --initfs
-1
View File
@@ -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
+21 -13
View File
@@ -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<String>,
envs: BTreeMap<String, OsString>,
}
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");
+5 -2
View File
@@ -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<String, OsString>) -> 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
}
}