user/proc: fix bug in env passing, more shell env

This commit is contained in:
2025-03-05 15:14:21 +02:00
parent be3e72b80e
commit fb25e70714
8 changed files with 54 additions and 31 deletions
+1
View File
@@ -30,6 +30,7 @@ pub mod elf;
pub type LoadedProcess = (Arc<Process>, Arc<Thread>); pub type LoadedProcess = (Arc<Process>, Arc<Thread>);
#[derive(Debug)]
pub struct LoadOptions<'e, P: AsRef<Path>> { pub struct LoadOptions<'e, P: AsRef<Path>> {
pub parent: Option<Arc<Process>>, pub parent: Option<Arc<Process>>,
pub group_id: ProcessGroupId, pub group_id: ProcessGroupId,
+7
View File
@@ -1,4 +1,5 @@
use core::{ use core::{
fmt,
sync::atomic::{AtomicU32, AtomicUsize, Ordering}, sync::atomic::{AtomicU32, AtomicUsize, Ordering},
task::{Context, Poll}, task::{Context, Poll},
}; };
@@ -646,6 +647,12 @@ impl Process {
} }
} }
impl fmt::Debug for Process {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.id, f)
}
}
impl Drop for Process { impl Drop for Process {
fn drop(&mut self) { fn drop(&mut self) {
log::debug!("Drop Process {}", self.id); log::debug!("Drop Process {}", self.id);
+1 -19
View File
@@ -95,19 +95,6 @@ pub(crate) fn spawn_process(options: &SpawnOptions<'_>) -> Result<ProcessId, Err
.optional .optional
.iter() .iter()
.any(|opt| matches!(opt, SpawnOption::AttachTrace)); .any(|opt| matches!(opt, SpawnOption::AttachTrace));
// let attach_debugger = options
// .optional
// .iter()
// .try_find_map::<_, Error, _>(|entry| {
// if let &SpawnOption::AttachDebug(fd) = entry {
// let channel = io.files.file(fd)?;
// let channel = channel.as_message_channel()?.clone();
// Ok(Some(channel))
// } else {
// Ok(None)
// }
// })?;
// Setup a new process from the file // Setup a new process from the file
let load_options = LoadOptions { let load_options = LoadOptions {
@@ -115,7 +102,7 @@ pub(crate) fn spawn_process(options: &SpawnOptions<'_>) -> Result<ProcessId, Err
parent: Some(process.clone()), parent: Some(process.clone()),
path: options.program, path: options.program,
args: options.arguments, args: options.arguments,
envs: options.arguments, envs: options.environment,
single_step: attach_trace, single_step: attach_trace,
disable_aslr: options.flags.contains(SpawnFlags::DISABLE_ASLR), disable_aslr: options.flags.contains(SpawnFlags::DISABLE_ASLR),
}; };
@@ -179,11 +166,6 @@ pub(crate) fn spawn_process(options: &SpawnOptions<'_>) -> Result<ProcessId, Err
.attach_trace(process.id) .attach_trace(process.id)
.expect("BUG: Could not attach to thread trace"); .expect("BUG: Could not attach to thread trace");
} }
// if let Some(debugger) = attach_debugger {
// child_main.attach_debugger(ThreadDebugger::new(debugger));
// } else {
// child_main.enqueue();
// }
Ok(pid as _) Ok(pid as _)
}); });
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
md2txt $1 | view -
+12 -3
View File
@@ -38,9 +38,18 @@ impl Env {
} }
} }
pub fn setup_builtin(&mut self) { pub fn setup_builtin(&mut self, args: &[String]) {
let bin_name = std::env::args().next().unwrap_or_default(); let shell_name = std::env::args().next().unwrap_or_default();
self.vars.insert("SHELL".into(), Variable::String(bin_name)); self.put_var("SHELL", shell_name.into());
let mut arg_list = String::new();
for (i, arg) in args.iter().enumerate() {
if !arg_list.is_empty() {
arg_list.push(' ');
}
arg_list.push_str(&arg);
self.put_var(&format!("{}", i + 1), arg.as_str().into());
}
self.put_var("*", arg_list.into());
} }
pub fn lookup(&self, name: &str) -> Option<Variable> { pub fn lookup(&self, name: &str) -> Option<Variable> {
+15 -8
View File
@@ -127,25 +127,32 @@ fn run(mut input: ShellInput, env: &Env) -> Result<(), Error> {
} }
} }
fn open_script<P: AsRef<Path>>(arg: P) -> io::Result<File> { fn find_script<P: AsRef<Path>>(arg: &P) -> &Path {
#[cfg(any(target_os = "yggdrasil", rust_analyzer))] #[cfg(any(target_os = "yggdrasil", rust_analyzer))]
{ {
let from_auxv = std::os::yggdrasil::real_binary_path(); let from_auxv = std::os::yggdrasil::real_binary_path();
if from_auxv.exists() { if from_auxv.exists() {
return File::open(from_auxv); return from_auxv;
} }
} }
File::open(arg) arg.as_ref()
} }
fn run_wrapper(args: ShellArgs, env: &Env) -> Result<(), Error> { fn run_wrapper(args: ShellArgs, env: &mut Env) -> Result<(), Error> {
match args.script { match args.script {
Some(script) => { Some(script) => {
let script = BufReader::new(open_script(&script)?); let script_path = find_script(&script);
let script_path_str = script_path.to_str().unwrap();
env.put_var("0", script_path_str.into());
let script = BufReader::new(File::open(script_path)?);
run(ShellInput::File(script), env) run(ShellInput::File(script), env)
} }
None => run(ShellInput::Interactive, env), None => {
let shell_name = std::env::args().next().unwrap();
env.put_var("0", shell_name.into());
run(ShellInput::Interactive, env)
}
} }
} }
@@ -156,7 +163,7 @@ fn main() -> ExitCode {
let args = ShellArgs::parse(); let args = ShellArgs::parse();
let mut env = Env::new(); let mut env = Env::new();
env.setup_builtin(); env.setup_builtin(&args.args);
builtin::register_default(); builtin::register_default();
if args.login { if args.login {
@@ -171,7 +178,7 @@ fn main() -> ExitCode {
} }
} }
match run_wrapper(args, &env) { match run_wrapper(args, &mut env) {
Ok(()) => ExitCode::SUCCESS, Ok(()) => ExitCode::SUCCESS,
Err(error) => { Err(error) => {
eprintln!("{error}"); eprintln!("{error}");
+1 -1
View File
@@ -138,7 +138,7 @@ impl FromStr for Keyword {
} }
fn lex_identifier(i: &str) -> IResult<&str, &str> { fn lex_identifier(i: &str) -> IResult<&str, &str> {
recognize(many1_count(alt((alphanumeric1, is_a("-_")))))(i) recognize(many1_count(alt((alphanumeric1, is_a("-_@?*")))))(i)
} }
fn lex_filename(i: &str) -> IResult<&str, &str> { fn lex_filename(i: &str) -> IResult<&str, &str> {
recognize(many1_count(alt((alphanumeric1, is_a("./-_:+,")))))(i) recognize(many1_count(alt((alphanumeric1, is_a("./-_:+,")))))(i)
+14
View File
@@ -128,6 +128,20 @@ fn build_rootfs<S: AsRef<Path>, D: AsRef<Path>>(
util::copy_file(src_path, dst_path)?; util::copy_file(src_path, dst_path)?;
} }
if let Ok(read_dir) = fs::read_dir("userspace/scripts") {
for entry in read_dir {
let Ok(entry) = entry else {
continue;
};
let path = entry.path();
let Some(filename) = path.file_name() else {
continue;
};
fs::copy(&path, rootfs_dir.join("bin").join(filename))?;
}
}
// TODO this is a temporary hack // TODO this is a temporary hack
fs::create_dir_all(rootfs_dir.join("lib"))?; fs::create_dir_all(rootfs_dir.join("lib"))?;
// TODO other architectures // TODO other architectures