Support cmdline_args when sys_exec.
This commit is contained in:
parent
c43ec12175
commit
c8d851fc2b
16 changed files with 132 additions and 24 deletions
16
user/src/bin/cmdline_args.rs
Normal file
16
user/src/bin/cmdline_args.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
println!("argc = {}", argc);
|
||||
for i in 0..argc {
|
||||
println!("argc[{}] = {}", i, argv[i]);
|
||||
}
|
||||
0
|
||||
}
|
|
@ -14,7 +14,7 @@ use user_lib::{
|
|||
#[no_mangle]
|
||||
fn main() -> i32 {
|
||||
if fork() == 0 {
|
||||
exec("user_shell\0");
|
||||
exec("user_shell\0", &[0 as *const u8]);
|
||||
} else {
|
||||
loop {
|
||||
let mut exit_code: i32 = 0;
|
||||
|
|
|
@ -10,7 +10,7 @@ use user_lib::{fork, exec, wait};
|
|||
pub fn main() -> i32 {
|
||||
for i in 0..1000 {
|
||||
if fork() == 0 {
|
||||
exec("pipe_large_test\0");
|
||||
exec("pipe_large_test\0", &[0 as *const u8]);
|
||||
} else {
|
||||
let mut _unused: i32 = 0;
|
||||
wait(&mut _unused);
|
||||
|
|
|
@ -12,6 +12,7 @@ const DL: u8 = 0x7fu8;
|
|||
const BS: u8 = 0x08u8;
|
||||
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{fork, exec, waitpid};
|
||||
use user_lib::console::getchar;
|
||||
|
||||
|
@ -26,11 +27,29 @@ pub fn main() -> i32 {
|
|||
LF | CR => {
|
||||
println!("");
|
||||
if !line.is_empty() {
|
||||
line.push('\0');
|
||||
let args: Vec<_> = line.as_str().split(' ').collect();
|
||||
let mut args_copy: Vec<String> = args
|
||||
.iter()
|
||||
.map(|&arg| {
|
||||
let mut string = String::new();
|
||||
string.push_str(arg);
|
||||
string
|
||||
})
|
||||
.collect();
|
||||
args_copy
|
||||
.iter_mut()
|
||||
.for_each(|string| {
|
||||
string.push('\0');
|
||||
});
|
||||
let mut args_addr: Vec<*const u8> = args_copy
|
||||
.iter()
|
||||
.map(|arg| arg.as_ptr())
|
||||
.collect();
|
||||
args_addr.push(0 as *const u8);
|
||||
let pid = fork();
|
||||
if pid == 0 {
|
||||
// child process
|
||||
if exec(line.as_str()) == -1 {
|
||||
if exec(args_copy[0].as_str(), args_addr.as_slice()) == -1 {
|
||||
println!("Error when executing!");
|
||||
return -4;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ pub fn main() -> i32 {
|
|||
println!("Usertests: Running {}", test);
|
||||
let pid = fork();
|
||||
if pid == 0 {
|
||||
exec(*test);
|
||||
exec(*test, &[0 as *const u8]);
|
||||
panic!("unreachable!");
|
||||
} else {
|
||||
let mut exit_code: i32 = Default::default();
|
||||
|
|
|
@ -15,6 +15,7 @@ extern crate bitflags;
|
|||
|
||||
use syscall::*;
|
||||
use buddy_system_allocator::LockedHeap;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
const USER_HEAP_SIZE: usize = 16384;
|
||||
|
||||
|
@ -30,17 +31,31 @@ pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
|
|||
|
||||
#[no_mangle]
|
||||
#[link_section = ".text.entry"]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
pub extern "C" fn _start(argc: usize, argv: usize) -> ! {
|
||||
unsafe {
|
||||
HEAP.lock()
|
||||
.init(HEAP_SPACE.as_ptr() as usize, USER_HEAP_SIZE);
|
||||
}
|
||||
exit(main());
|
||||
let mut v: Vec<&'static str> = Vec::new();
|
||||
for i in 0..argc {
|
||||
let str_start = unsafe {
|
||||
((argv + i * core::mem::size_of::<usize>()) as *const usize).read_volatile()
|
||||
};
|
||||
let len = (0usize..).find(|i| unsafe {
|
||||
((str_start + *i) as *const u8).read_volatile() == 0
|
||||
}).unwrap();
|
||||
v.push(
|
||||
core::str::from_utf8(unsafe {
|
||||
core::slice::from_raw_parts(str_start as *const u8, len)
|
||||
}).unwrap()
|
||||
);
|
||||
}
|
||||
exit(main(argc, v.as_slice()));
|
||||
}
|
||||
|
||||
#[linkage = "weak"]
|
||||
#[no_mangle]
|
||||
fn main() -> i32 {
|
||||
fn main(_argc: usize, _argv: &[&str]) -> i32 {
|
||||
panic!("Cannot find main!");
|
||||
}
|
||||
|
||||
|
@ -64,7 +79,7 @@ pub fn yield_() -> isize { sys_yield() }
|
|||
pub fn get_time() -> isize { sys_get_time() }
|
||||
pub fn getpid() -> isize { sys_getpid() }
|
||||
pub fn fork() -> isize { sys_fork() }
|
||||
pub fn exec(path: &str) -> isize { sys_exec(path) }
|
||||
pub fn exec(path: &str, args: &[*const u8]) -> isize { sys_exec(path, args) }
|
||||
pub fn wait(exit_code: &mut i32) -> isize {
|
||||
loop {
|
||||
match sys_waitpid(-1, exit_code as *mut _) {
|
||||
|
|
|
@ -65,8 +65,8 @@ pub fn sys_fork() -> isize {
|
|||
syscall(SYSCALL_FORK, [0, 0, 0])
|
||||
}
|
||||
|
||||
pub fn sys_exec(path: &str) -> isize {
|
||||
syscall(SYSCALL_EXEC, [path.as_ptr() as usize, 0, 0])
|
||||
pub fn sys_exec(path: &str, args: &[*const u8]) -> isize {
|
||||
syscall(SYSCALL_EXEC, [path.as_ptr() as usize, args.as_ptr() as usize, 0])
|
||||
}
|
||||
|
||||
pub fn sys_waitpid(pid: isize, exit_code: *mut i32) -> isize {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue