From 56e472c8f893c55b62799da2e62808d3b6598080 Mon Sep 17 00:00:00 2001 From: chyyuu Date: Wed, 17 Apr 2019 14:59:06 +0800 Subject: [PATCH] fix bug: sys_exec should name as exec_path, then loader can find the exec file. items of VEC args from argv is the args lists, and args[0] isn't the complete exec_path. the new_user function should add exec_path as a new args. BUGS: ls app doesn't show any thing. Why? --- kernel/src/process/structs.rs | 7 +++++-- kernel/src/shell.rs | 4 ++-- kernel/src/syscall/proc.rs | 24 +++++++++++++++++++----- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/kernel/src/process/structs.rs b/kernel/src/process/structs.rs index 700d4c2..0bf6781 100644 --- a/kernel/src/process/structs.rs +++ b/kernel/src/process/structs.rs @@ -158,7 +158,7 @@ impl Thread { } /// Make a new user process from ELF `data` - pub fn new_user<'a, Iter>(data: &[u8], args: Iter) -> Box + pub fn new_user<'a, Iter>(data: &[u8], exec_path: &str, args: Iter) -> Box where Iter: Iterator, { @@ -182,7 +182,10 @@ impl Thread { // No infinite loop let mut new_args: Vec<&str> = args.collect(); new_args.insert(0, loader_path); - return Thread::new_user(buf.as_slice(), new_args.into_iter()); + new_args.insert(1, exec_path); + new_args.remove(2); + warn!("loader args: {:?}", new_args); + return Thread::new_user(buf.as_slice(), exec_path,new_args.into_iter()); } else { warn!("loader specified as {} but failed to read", &loader_path); } diff --git a/kernel/src/shell.rs b/kernel/src/shell.rs index ce0f156..9b2690e 100644 --- a/kernel/src/shell.rs +++ b/kernel/src/shell.rs @@ -12,7 +12,7 @@ pub fn run_user_shell() { let data = inode.read_as_vec().unwrap(); processor() .manager() - .add(Thread::new_user(data.as_slice(), "sh".split(' '))); + .add(Thread::new_user(data.as_slice(), "rust/sh", "sh".split(' '))); } else { processor().manager().add(Thread::new_kernel(shell, 0)); } @@ -44,7 +44,7 @@ pub extern "C" fn shell(_arg: usize) -> ! { let data = file.read_as_vec().unwrap(); let _pid = processor() .manager() - .add(Thread::new_user(data.as_slice(), cmd.split(' '))); + .add(Thread::new_user(data.as_slice(), &cmd, cmd.split(' '))); // TODO: wait until process exits, or use user land shell completely //unsafe { thread::JoinHandle::<()>::_of(pid) }.join().unwrap(); } else { diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index 8187628..1159ff3 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -126,7 +126,7 @@ pub fn sys_exec( ) -> SysResult { info!("exec: name: {:?}, argv: {:?} envp: {:?}", name, argv, envp); let proc = process(); - let _name = if name.is_null() { + let exec_name = if name.is_null() { String::from("") } else { unsafe { proc.vm.check_and_clone_cstr(name)? } @@ -146,19 +146,33 @@ pub fn sys_exec( current_argv = current_argv.add(1); } } - info!("exec: args {:?}", args); +// // Check and copy envs to kernel +// let mut envs = Vec::new(); +// unsafe { +// let mut current_env = envp as *const *const u8; +// proc.vm.check_read_ptr(current_env)?; +// while !(*current_env).is_null() { +// let env = proc.vm.check_and_clone_cstr(*current_env)?; +// envs.push(env); +// current_env = current_env.add(1); +// } +// } +// if args.is_empty() { return Err(SysError::EINVAL); } + info!("EXEC: name:{:?} , args {:?}", exec_name, args); + // Read program file - let path = args[0].as_str(); - let inode = proc.lookup_inode(path)?; + //let path = args[0].as_str(); + let exec_path = exec_name.as_str(); + let inode = proc.lookup_inode(exec_path)?; let buf = inode.read_as_vec()?; // Make new Thread let iter = args.iter().map(|s| s.as_str()); - let mut thread = Thread::new_user(buf.as_slice(), iter); + let mut thread = Thread::new_user(buf.as_slice(), exec_path, iter); thread.proc.lock().clone_for_exec(&proc); // Activate new page table