Skip to content

Commit a393fff

Browse files
committed
Made Env Vars modifyable during runtime
1 parent c7a6d42 commit a393fff

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

src/arch/x86_64/kernel/mmio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn detect_device(phys_addr: usize) -> Option<VolatileRef<'static, DeviceRegister
132132
}
133133

134134
fn check_linux_args(
135-
linux_mmio: &'static [String],
135+
linux_mmio: &[String],
136136
) -> impl Iterator<Item = (VolatileRef<'static, DeviceRegisters>, u8)> {
137137
linux_mmio
138138
.iter()

src/env/mod.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ use core::str;
99

1010
use ahash::RandomState;
1111
use hashbrown::HashMap;
12-
use hashbrown::hash_map::Iter;
13-
use hermit_sync::OnceCell;
12+
use hermit_sync::InterruptSpinMutex;
1413
use shlex::Shlex;
1514

1615
pub use self::start_info::*;
1716

18-
static CLI: OnceCell<Cli> = OnceCell::new();
17+
static CLI: InterruptSpinMutex<Option<Cli>> = InterruptSpinMutex::new(None);
1918

2019
pub fn init() {
21-
CLI.set(Cli::default()).unwrap();
20+
assert!(
21+
CLI.lock().replace(Cli::default()).is_none(),
22+
"env::init must only be called once"
23+
);
2224
}
2325

2426
#[derive(Debug)]
@@ -118,25 +120,34 @@ impl Default for Cli {
118120
/// CPU Frequency in MHz if given through the -freq command-line parameter.
119121
#[cfg(not(target_arch = "riscv64"))]
120122
pub fn freq() -> Option<u16> {
121-
CLI.get().unwrap().freq
123+
CLI.lock().as_ref().unwrap().freq
122124
}
123125

124126
#[allow(dead_code)]
125-
pub fn var(key: &str) -> Option<&String> {
126-
CLI.get().unwrap().env_vars.get(key)
127+
pub fn var(key: &str) -> Option<String> {
128+
CLI.lock().as_ref().unwrap().env_vars.get(key).cloned()
127129
}
128130

129-
pub fn vars() -> Iter<'static, String, String> {
130-
CLI.get().unwrap().env_vars.iter()
131+
#[cfg(feature = "snapshot")]
132+
pub fn insert_var(key: &str, value: String) {
133+
CLI.lock()
134+
.as_mut()
135+
.unwrap()
136+
.env_vars
137+
.insert(key.to_owned(), value);
138+
}
139+
140+
pub fn vars() -> HashMap<String, String, RandomState> {
141+
CLI.lock().as_ref().unwrap().env_vars.clone()
131142
}
132143

133144
/// Returns the cmdline argument passed in after "--"
134-
pub fn args() -> &'static [String] {
135-
CLI.get().unwrap().args.as_slice()
145+
pub fn args() -> Vec<String> {
146+
CLI.lock().as_ref().unwrap().args.clone()
136147
}
137148

138149
/// Returns the configuration of all mmio devices
139150
#[allow(dead_code)]
140-
pub fn mmio() -> &'static [String] {
141-
CLI.get().unwrap().mmio.as_slice()
151+
pub fn mmio() -> Vec<String> {
152+
CLI.lock().as_ref().unwrap().mmio.clone()
142153
}

0 commit comments

Comments
 (0)