From 4519e5385afc79defd255d9715ed46c17546e303 Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Tue, 12 Nov 2024 11:14:29 +0200 Subject: [PATCH] libc: setup args/env --- userspace/lib/ygglibc/Cargo.toml | 3 ++ userspace/lib/ygglibc/src/args.rs | 24 +++++++++++--- userspace/lib/ygglibc/src/headers/mod.rs | 4 +-- .../lib/ygglibc/src/headers/stdio/util.rs | 1 + userspace/lib/ygglibc/src/lib.rs | 31 ++++++++++++++----- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/userspace/lib/ygglibc/Cargo.toml b/userspace/lib/ygglibc/Cargo.toml index 44cdfb74..bce22679 100644 --- a/userspace/lib/ygglibc/Cargo.toml +++ b/userspace/lib/ygglibc/Cargo.toml @@ -14,3 +14,6 @@ bitflags = "2.6.0" [build-dependencies] cbindgen = { git = "https://git.alnyan.me/yggdrasil/cbindgen.git", branch = "master" } + +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(rust_analyzer)'] } diff --git a/userspace/lib/ygglibc/src/args.rs b/userspace/lib/ygglibc/src/args.rs index de613499..db5a2a89 100644 --- a/userspace/lib/ygglibc/src/args.rs +++ b/userspace/lib/ygglibc/src/args.rs @@ -1,9 +1,23 @@ -use alloc::{string::String, vec::Vec}; +use alloc::{ffi::CString, vec::Vec}; use yggdrasil_rt::process::ProgramArgumentInner; -pub fn handle_kernel_argument(arg: usize) -> (Vec, Vec) { - let arg_ptr: *const ProgramArgumentInner = core::ptr::with_exposed_provenance(arg); - let arg = unsafe { arg_ptr.as_ref() }.expect("TODO"); +use crate::util::PointerExt; - todo!() +pub fn handle_kernel_argument(arg: usize) -> (Vec, Vec) { + let arg_ptr: *const ProgramArgumentInner = core::ptr::with_exposed_provenance(arg); + let arg = unsafe { arg_ptr.ensure() }; + + let mut args = Vec::new(); + let mut envs = Vec::new(); + + for &arg in arg.args { + let c_arg = CString::new(arg).unwrap(); + args.push(c_arg); + } + for &env in arg.env { + let c_env = CString::new(env).unwrap(); + envs.push(c_env); + } + + (args, envs) } diff --git a/userspace/lib/ygglibc/src/headers/mod.rs b/userspace/lib/ygglibc/src/headers/mod.rs index 8b5efa7a..d2343b20 100644 --- a/userspace/lib/ygglibc/src/headers/mod.rs +++ b/userspace/lib/ygglibc/src/headers/mod.rs @@ -47,9 +47,9 @@ // - // - // - -// ~ +// + // - -// - +// + // - // - // - diff --git a/userspace/lib/ygglibc/src/headers/stdio/util.rs b/userspace/lib/ygglibc/src/headers/stdio/util.rs index 5d15c6ec..9dfd092a 100644 --- a/userspace/lib/ygglibc/src/headers/stdio/util.rs +++ b/userspace/lib/ygglibc/src/headers/stdio/util.rs @@ -17,6 +17,7 @@ unsafe extern "C" fn perror(message: *const c_char) { out.write_unlocked(b": ").ok(); } + #[allow(static_mut_refs)] out.write_unlocked(error::errno.to_c_str().to_bytes()).ok(); out.write_unlocked(b"\n").ok(); diff --git a/userspace/lib/ygglibc/src/lib.rs b/userspace/lib/ygglibc/src/lib.rs index 8aeef3d4..ba697924 100644 --- a/userspace/lib/ygglibc/src/lib.rs +++ b/userspace/lib/ygglibc/src/lib.rs @@ -14,25 +14,22 @@ extern crate alloc; use core::{ - ffi::{c_char, c_int, c_void}, + ffi::{c_char, c_int}, panic::PanicInfo, ptr::null, }; -use alloc::boxed::Box; -use yggdrasil_rt::{ - debug_trace, - process::{ExitCode, ProcessId, Signal}, -}; +use alloc::vec::Vec; +use yggdrasil_rt::{debug_trace, process::Signal}; mod allocator; mod args; mod error; mod io; mod process; -mod util; mod sync; mod types; +mod util; pub mod headers; @@ -44,7 +41,25 @@ unsafe extern "C" fn _start(arg: usize) { io::init(); - let status = main(0, null(), null()); + // Setup args + let (args, envs) = args::handle_kernel_argument(arg); + let mut c_args = Vec::new(); + let mut c_envs = Vec::new(); + + for arg in args.iter() { + c_args.push(arg.as_ptr()); + } + for env in envs.iter() { + c_envs.push(env.as_ptr()); + } + c_args.push(null()); + c_envs.push(null()); + + let status = main( + args.len().try_into().unwrap(), + c_args.as_ptr(), + c_envs.as_ptr(), + ); process::c_exit(status) }