fix process cwd. fix open path

This commit is contained in:
WangRunji 2019-03-13 18:00:07 +08:00 committed by Tate Tian
parent 011b4bf8e7
commit 6e9f00b5db
7 changed files with 31 additions and 20 deletions

@ -27,10 +27,13 @@ pub fn do_open(path: &str, flags: u32, mode: u32) -> Result<FileDesc, Error> {
let flags = OpenFlags::from_bits_truncate(flags); let flags = OpenFlags::from_bits_truncate(flags);
info!("open: path: {:?}, flags: {:?}, mode: {:#o}", path, flags, mode); info!("open: path: {:?}, flags: {:?}, mode: {:#o}", path, flags, mode);
let current_ref = process::get_current();
let mut proc = current_ref.lock().unwrap();
let inode = let inode =
if flags.contains(OpenFlags::CREATE) { if flags.contains(OpenFlags::CREATE) {
let (dir_path, file_name) = split_path(&path); let (dir_path, file_name) = split_path(&path);
let dir_inode = ROOT_INODE.lookup(dir_path)?; let dir_inode = proc.lookup_inode(dir_path)?;
match dir_inode.find(file_name) { match dir_inode.find(file_name) {
Ok(file_inode) => { Ok(file_inode) => {
if flags.contains(OpenFlags::EXCLUSIVE) { if flags.contains(OpenFlags::EXCLUSIVE) {
@ -44,7 +47,7 @@ pub fn do_open(path: &str, flags: u32, mode: u32) -> Result<FileDesc, Error> {
Err(e) => return Err(Error::from(e)), Err(e) => return Err(Error::from(e)),
} }
} else { } else {
ROOT_INODE.lookup(&path)? proc.lookup_inode(&path)?
}; };
let file_ref: Arc<Box<File>> = Arc::new(Box::new( let file_ref: Arc<Box<File>> = Arc::new(Box::new(
@ -52,10 +55,8 @@ pub fn do_open(path: &str, flags: u32, mode: u32) -> Result<FileDesc, Error> {
)); ));
let fd = { let fd = {
let current_ref = process::get_current();
let mut current = current_ref.lock().unwrap();
let close_on_spawn = flags.contains(OpenFlags::CLOEXEC); let close_on_spawn = flags.contains(OpenFlags::CLOEXEC);
current.get_files_mut().put(file_ref, close_on_spawn) proc.get_files_mut().put(file_ref, close_on_spawn)
}; };
Ok(fd) Ok(fd)
} }
@ -245,11 +246,20 @@ extern "C" {
impl Process { impl Process {
pub fn lookup_inode(&self, path: &str) -> Result<Arc<INode>, Error> { pub fn lookup_inode(&self, path: &str) -> Result<Arc<INode>, Error> {
let cwd = self.get_exec_path().split_at(1).1; // skip start '/' debug!("lookup_inode: cwd: {:?}, path: {:?}", self.get_cwd(), path);
if path.len() > 0 && path.as_bytes()[0] == b'/' {
// absolute path
let abs_path = path.trim_start_matches('/');
let inode = ROOT_INODE.lookup(abs_path)?;
Ok(inode)
} else {
// relative path
let cwd = self.get_cwd().trim_start_matches('/');
let inode = ROOT_INODE.lookup(cwd)?.lookup(path)?; let inode = ROOT_INODE.lookup(cwd)?.lookup(path)?;
Ok(inode) Ok(inode)
} }
} }
}
/// Split a `path` str to `(base_path, file_name)` /// Split a `path` str to `(base_path, file_name)`
fn split_path(path: &str) -> (&str, &str) { fn split_path(path: &str) -> (&str, &str) {

@ -18,7 +18,7 @@ pub struct Process {
pgid: pid_t, pgid: pid_t,
tgid: pid_t, tgid: pid_t,
exit_status: i32, exit_status: i32,
exec_path: String, cwd: String,
parent: Option<ProcessRef>, parent: Option<ProcessRef>,
children: Vec<ProcessWeakRef>, children: Vec<ProcessWeakRef>,
waiting_children: Option<WaitQueue<ChildProcessFilter, pid_t>>, waiting_children: Option<WaitQueue<ChildProcessFilter, pid_t>>,

@ -13,7 +13,7 @@ lazy_static! {
pgid: 0, pgid: 0,
tgid: 0, tgid: 0,
exit_status: 0, exit_status: 0,
exec_path: "".to_owned(), cwd: "/".to_owned(),
parent: None, parent: None,
children: Vec::new(), children: Vec::new(),
waiting_children: Default::default(), waiting_children: Default::default(),
@ -25,7 +25,7 @@ lazy_static! {
impl Process { impl Process {
pub fn new( pub fn new(
exec_path: &str, cwd: &str,
task: Task, task: Task,
vm: ProcessVM, vm: ProcessVM,
file_table: FileTable, file_table: FileTable,
@ -37,7 +37,7 @@ impl Process {
pid: new_pid, pid: new_pid,
pgid: new_pid, pgid: new_pid,
tgid: new_pid, tgid: new_pid,
exec_path: exec_path.to_owned(), cwd: cwd.to_owned(),
exit_status: 0, exit_status: 0,
parent: None, parent: None,
children: Vec::new(), children: Vec::new(),
@ -69,8 +69,8 @@ impl Process {
pub fn get_exit_status(&self) -> i32 { pub fn get_exit_status(&self) -> i32 {
self.exit_status self.exit_status
} }
pub fn get_exec_path(&self) -> &str { pub fn get_cwd(&self) -> &str {
&self.exec_path &self.cwd
} }
pub fn get_vm(&self) -> &ProcessVM { pub fn get_vm(&self) -> &ProcessVM {
&self.vm &self.vm

@ -30,7 +30,7 @@ pub fn do_spawn<P: AsRef<Path>>(
parent_ref: &ProcessRef, parent_ref: &ProcessRef,
) -> Result<u32, Error> { ) -> Result<u32, Error> {
let mut elf_buf = { let mut elf_buf = {
let path = elf_path.as_ref().to_str().unwrap(); let path = elf_path.as_ref().to_str().unwrap().trim_start_matches('/');
let inode = ROOT_INODE.lookup(path)?; let inode = ROOT_INODE.lookup(path)?;
inode.read_as_vec()? inode.read_as_vec()?
}; };
@ -62,8 +62,8 @@ pub fn do_spawn<P: AsRef<Path>>(
init_task(program_entry, stack_top, argv, envp)? init_task(program_entry, stack_top, argv, envp)?
}; };
let files = init_files(parent_ref, file_actions)?; let files = init_files(parent_ref, file_actions)?;
let exec_path = elf_path.as_ref().to_str().unwrap(); let cwd = elf_path.as_ref().parent().unwrap().to_str().unwrap();
Process::new(exec_path, task, vm, files)? Process::new(cwd, task, vm, files)?
}; };
parent_adopts_new_child(&parent_ref, &new_process_ref); parent_adopts_new_child(&parent_ref, &new_process_ref);
process_table::put(new_pid, new_process_ref.clone()); process_table::put(new_pid, new_process_ref.clone());

@ -84,6 +84,7 @@ pub extern "C" fn dispatch_syscall(
_ => do_unknown(num), _ => do_unknown(num),
}; };
debug!("syscall return: {:?}", ret);
match ret { match ret {
Ok(code) => code as isize, Ok(code) => code as isize,
@ -475,7 +476,7 @@ fn do_getcwd(buf: *mut u8, size: usize) -> Result<isize, Error> {
}; };
let proc_ref = process::get_current(); let proc_ref = process::get_current();
let mut proc = proc_ref.lock().unwrap(); let mut proc = proc_ref.lock().unwrap();
let cwd = proc.get_exec_path(); let cwd = proc.get_cwd();
if cwd.len() + 1 > safe_buf.len() { if cwd.len() + 1 > safe_buf.len() {
return Err(Error::new(ERANGE, "buf is not long enough")); return Err(Error::new(ERANGE, "buf is not long enough"));
} }

@ -5,7 +5,7 @@
#include <stdio.h> #include <stdio.h>
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
DIR* dirp = opendir("."); DIR* dirp = opendir("/");
if (dirp == NULL) { if (dirp == NULL) {
printf("failed to open directory at /\n"); printf("failed to open directory at /\n");
return -1; return -1;

@ -9,7 +9,7 @@ S_FILES := $(C_SRCS:%.c=%.S)
C_OBJS := $(C_SRCS:%.c=%.o) C_OBJS := $(C_SRCS:%.c=%.o)
FS_PATH := ../fs FS_PATH := ../fs
BIN_NAME := $(shell basename $(CUR_DIR)) BIN_NAME := $(shell basename $(CUR_DIR))
BIN_FS_PATH := $(BIN_NAME) BIN_FS_PATH := /$(BIN_NAME)
BIN_PATH := $(FS_PATH)/$(BIN_FS_PATH) BIN_PATH := $(FS_PATH)/$(BIN_FS_PATH)
OBJDUMP_FILE := bin.objdump OBJDUMP_FILE := bin.objdump
READELF_FILE := bin.readelf READELF_FILE := bin.readelf