44 lines
1.1 KiB
Rust
44 lines
1.1 KiB
Rust
use crate::platform::{self, Platform};
|
|
use crate::result::{Context, Result};
|
|
|
|
pub enum Mode {
|
|
Spawn,
|
|
Exec,
|
|
}
|
|
|
|
pub struct Config {
|
|
pub platform: Option<Box<dyn Platform>>,
|
|
pub mode: Mode,
|
|
pub target: String,
|
|
}
|
|
|
|
pub fn get_config() -> Result<Config> {
|
|
let mut values = std::collections::HashMap::<&str, String>::new();
|
|
let cmdline = std::fs::read_to_string("/proc/cmdline")
|
|
.context(format_args!("could not read kernel cmdline"))?;
|
|
|
|
for word in cmdline.split_whitespace() {
|
|
if let Some((lhs, rhs)) = word.split_once('=') {
|
|
if let Some(("nit", name)) = lhs.split_once('.') {
|
|
values.insert(name, rhs.to_string());
|
|
}
|
|
}
|
|
}
|
|
|
|
let mode = if let Some(mode) = values.remove("mode") {
|
|
match &*mode {
|
|
"spawn" => Mode::Spawn,
|
|
"exec" => Mode::Exec,
|
|
m => panic!("Bad mode: {m}"),
|
|
}
|
|
} else {
|
|
Mode::Spawn
|
|
};
|
|
|
|
let platform = platform::get_current_platform(values.remove("platform").as_deref())?;
|
|
|
|
let target = values.remove("target").unwrap();
|
|
|
|
Ok(Config { platform, mode, target })
|
|
}
|