libc: setup args/env

This commit is contained in:
Mark Poliakov 2024-11-12 11:14:29 +02:00
parent 0a904a21fe
commit 4519e5385a
5 changed files with 48 additions and 15 deletions

View File

@ -14,3 +14,6 @@ bitflags = "2.6.0"
[build-dependencies] [build-dependencies]
cbindgen = { git = "https://git.alnyan.me/yggdrasil/cbindgen.git", branch = "master" } cbindgen = { git = "https://git.alnyan.me/yggdrasil/cbindgen.git", branch = "master" }
[lints.rust]
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(rust_analyzer)'] }

View File

@ -1,9 +1,23 @@
use alloc::{string::String, vec::Vec}; use alloc::{ffi::CString, vec::Vec};
use yggdrasil_rt::process::ProgramArgumentInner; use yggdrasil_rt::process::ProgramArgumentInner;
pub fn handle_kernel_argument(arg: usize) -> (Vec<String>, Vec<String>) { use crate::util::PointerExt;
let arg_ptr: *const ProgramArgumentInner = core::ptr::with_exposed_provenance(arg);
let arg = unsafe { arg_ptr.as_ref() }.expect("TODO");
todo!() pub fn handle_kernel_argument(arg: usize) -> (Vec<CString>, Vec<CString>) {
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)
} }

View File

@ -47,9 +47,9 @@
// <stdbool.h> - // <stdbool.h> -
// <stddef.h> - // <stddef.h> -
// <stdint.h> - // <stdint.h> -
// <stdio.h> ~ // <stdio.h> +
// <stdlib.h> - // <stdlib.h> -
// <string.h> - // <string.h> +
// <strings.h> - // <strings.h> -
// <stropts.h> - // <stropts.h> -
// <sys/ipc.h> - // <sys/ipc.h> -

View File

@ -17,6 +17,7 @@ unsafe extern "C" fn perror(message: *const c_char) {
out.write_unlocked(b": ").ok(); out.write_unlocked(b": ").ok();
} }
#[allow(static_mut_refs)]
out.write_unlocked(error::errno.to_c_str().to_bytes()).ok(); out.write_unlocked(error::errno.to_c_str().to_bytes()).ok();
out.write_unlocked(b"\n").ok(); out.write_unlocked(b"\n").ok();

View File

@ -14,25 +14,22 @@
extern crate alloc; extern crate alloc;
use core::{ use core::{
ffi::{c_char, c_int, c_void}, ffi::{c_char, c_int},
panic::PanicInfo, panic::PanicInfo,
ptr::null, ptr::null,
}; };
use alloc::boxed::Box; use alloc::vec::Vec;
use yggdrasil_rt::{ use yggdrasil_rt::{debug_trace, process::Signal};
debug_trace,
process::{ExitCode, ProcessId, Signal},
};
mod allocator; mod allocator;
mod args; mod args;
mod error; mod error;
mod io; mod io;
mod process; mod process;
mod util;
mod sync; mod sync;
mod types; mod types;
mod util;
pub mod headers; pub mod headers;
@ -44,7 +41,25 @@ unsafe extern "C" fn _start(arg: usize) {
io::init(); 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) process::c_exit(status)
} }