return error code from syscall, not always -1

master
Ben Pig Chu 6 years ago
parent fa03f7b112
commit f8533442f2

@ -53,7 +53,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> i32 {
}; };
match ret { match ret {
Ok(code) => code, Ok(code) => code,
Err(_) => -1, Err(err) => -(err as i32),
} }
} }
@ -96,7 +96,7 @@ fn sys_close(fd: usize) -> SysResult {
info!("close: fd: {:?}", fd); info!("close: fd: {:?}", fd);
match process().files.remove(&fd) { match process().files.remove(&fd) {
Some(_) => Ok(0), Some(_) => Ok(0),
None => Err(SysError::InvalidFile), None => Err(SysError::Badf),
} }
} }
@ -118,11 +118,11 @@ fn sys_getdirentry(fd: usize, dentry_ptr: *mut DirEntry) -> SysResult {
let file = get_file(fd)?; let file = get_file(fd)?;
let dentry = unsafe { &mut *dentry_ptr }; let dentry = unsafe { &mut *dentry_ptr };
if !dentry.check() { if !dentry.check() {
return Err(SysError::InvalidArgument); return Err(SysError::Inval);
} }
let info = file.lock().info()?; let info = file.lock().info()?;
if info.type_ != FileType::Dir || info.size <= dentry.entry_id() { if info.type_ != FileType::Dir || info.size <= dentry.entry_id() {
return Err(SysError::InvalidArgument); return Err(SysError::Inval);
} }
let name = file.lock().get_entry(dentry.entry_id())?; let name = file.lock().get_entry(dentry.entry_id())?;
dentry.set_name(name.as_str()); dentry.set_name(name.as_str());
@ -133,7 +133,7 @@ fn sys_dup(fd1: usize, fd2: usize) -> SysResult {
info!("dup: {} {}", fd1, fd2); info!("dup: {} {}", fd1, fd2);
let file = get_file(fd1)?; let file = get_file(fd1)?;
if process().files.contains_key(&fd2) { if process().files.contains_key(&fd2) {
return Err(SysError::InvalidFile); return Err(SysError::Badf);
} }
process().files.insert(fd2, file.clone()); process().files.insert(fd2, file.clone());
Ok(0) Ok(0)
@ -278,7 +278,7 @@ fn sys_putc(c: char) -> SysResult {
} }
fn get_file(fd: usize) -> Result<&'static Arc<Mutex<File>>, SysError> { fn get_file(fd: usize) -> Result<&'static Arc<Mutex<File>>, SysError> {
process().files.get(&fd).ok_or(SysError::InvalidFile) process().files.get(&fd).ok_or(SysError::Badf)
} }
pub type SysResult = Result<i32, SysError>; pub type SysResult = Result<i32, SysError>;
@ -286,14 +286,21 @@ pub type SysResult = Result<i32, SysError>;
#[repr(i32)] #[repr(i32)]
#[derive(Debug)] #[derive(Debug)]
pub enum SysError { pub enum SysError {
VfsError, // ucore compatible error code, which is a modified version of the ones used in linux
InvalidFile, // name conversion E_XXXXX -> SysError::Xxxxx
InvalidArgument, // see https://github.com/oscourse-tsinghua/ucore_plus/blob/master/ucore/src/libs-user-ucore/common/error.h
// we only add current used errors here
Badf = 9,// Invaild fd number
Inval = 22,// Invalid argument.
Unknown = 65535,// A really really unknown error.
} }
impl From<FsError> for SysError { impl From<FsError> for SysError {
fn from(_: FsError) -> Self { fn from(error: FsError) -> Self {
SysError::VfsError match error {
_ => SysError::Unknown
}
} }
} }

Loading…
Cancel
Save