diff --git a/src/errno.rs b/src/errno.rs index 148387cef1..dc22dc8ced 100644 --- a/src/errno.rs +++ b/src/errno.rs @@ -1,6 +1,3 @@ -#[thread_local] -pub static mut ERRNO: i32 = 0; - /// Operation not permitted pub const EPERM: i32 = 1; diff --git a/src/fd/file.rs b/src/fd/file.rs index 64d5587464..b66fff58ae 100644 --- a/src/fd/file.rs +++ b/src/fd/file.rs @@ -1,12 +1,11 @@ use alloc::boxed::Box; use core::{isize, slice}; -use crate::errno::*; use crate::fd::{ - uhyve_send, ObjectInterface, SysClose, SysLseek, SysRead, SysWrite, UHYVE_PORT_CLOSE, - UHYVE_PORT_LSEEK, UHYVE_PORT_READ, UHYVE_PORT_WRITE, + uhyve_send, DirectoryEntry, ObjectInterface, SysClose, SysLseek, SysRead, SysWrite, + UHYVE_PORT_CLOSE, UHYVE_PORT_LSEEK, UHYVE_PORT_READ, UHYVE_PORT_WRITE, }; -use crate::syscalls::fs::{self, Dirent, FileAttr, PosixFile, SeekWhence}; +use crate::syscalls::fs::{self, FileAttr, PosixFile, SeekWhence}; #[derive(Debug, Clone)] pub struct UhyveFile(i32); @@ -114,15 +113,15 @@ impl ObjectInterface for GenericFile { result } - fn readdir(&self) -> *const Dirent { + fn readdir(&self) -> DirectoryEntry { debug!("readdir ! {}", self.0); let mut fs = fs::FILESYSTEM.lock(); - let mut ret: *const Dirent = core::ptr::null(); + let mut ret = DirectoryEntry::Invalid(-crate::errno::EINVAL); fs.fd_op(self.0, |file: &mut Box| { match file.readdir() { - Ok(dir_ptr) => ret = dir_ptr, - Err(e) => unsafe { ERRNO = num::ToPrimitive::to_i32(&e).unwrap() }, + Ok(dir_ptr) => ret = DirectoryEntry::Valid(dir_ptr), + Err(e) => ret = DirectoryEntry::Invalid(-num::ToPrimitive::to_i32(&e).unwrap()), } }); diff --git a/src/fd/mod.rs b/src/fd/mod.rs index 2e4e9a7116..3632b1fab4 100644 --- a/src/fd/mod.rs +++ b/src/fd/mod.rs @@ -181,6 +181,13 @@ fn open_flags_to_perm(flags: i32, mode: u32) -> FilePerms { perms } +#[derive(Copy, Clone, Debug)] +#[repr(C)] +pub enum DirectoryEntry { + Invalid(i32), + Valid(*const Dirent), +} + pub trait ObjectInterface: Sync + Send + core::fmt::Debug + DynClone { /// `read` attempts to read `len` bytes from the object references /// by the descriptor @@ -217,11 +224,8 @@ pub trait ObjectInterface: Sync + Send + core::fmt::Debug + DynClone { /// 'readdir' returns a pointer to a dirent structure /// representing the next directory entry in the directory stream /// pointed to by the file descriptor - fn readdir(&self) -> *const Dirent { - unsafe { - ERRNO = ENOSYS; - } - core::ptr::null() + fn readdir(&self) -> DirectoryEntry { + DirectoryEntry::Invalid(-ENOSYS) } /// `mkdir` creates a directory entry diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index d295a364ad..5c090aab38 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -16,9 +16,8 @@ pub use self::system::*; pub use self::tasks::*; pub use self::timer::*; use crate::env; -use crate::errno::ERRNO; -use crate::fd::{dup_object, get_object, remove_object, FileDescriptor}; -use crate::syscalls::fs::{Dirent, FileAttr}; +use crate::fd::{dup_object, get_object, remove_object, DirectoryEntry, FileDescriptor}; +use crate::syscalls::fs::FileAttr; use crate::syscalls::interfaces::SyscallInterface; #[cfg(target_os = "none")] use crate::{__sys_free, __sys_malloc, __sys_realloc}; @@ -100,24 +99,6 @@ pub extern "C" fn sys_shutdown(arg: i32) -> ! { kernel_function!(__sys_shutdown(arg)) } -extern "C" fn __sys_get_errno() -> i32 { - unsafe { ERRNO } -} - -#[no_mangle] -pub extern "C" fn sys_get_errno() -> i32 { - kernel_function!(__sys_get_errno()) -} - -extern "C" fn __sys_set_errno(e: i32) { - unsafe { ERRNO = e }; -} - -#[no_mangle] -pub extern "C" fn sys_set_errno(e: i32) { - kernel_function!(__sys_set_errno(e)) -} - extern "C" fn __sys_unlink(name: *const u8) -> i32 { SYS.unlink(name) } @@ -256,13 +237,15 @@ pub extern "C" fn sys_lseek(fd: FileDescriptor, offset: isize, whence: i32) -> i kernel_function!(__sys_lseek(fd, offset, whence)) } -extern "C" fn __sys_readdir(fd: FileDescriptor) -> *const Dirent { +extern "C" fn __sys_readdir(fd: FileDescriptor) -> DirectoryEntry { let obj = get_object(fd); - obj.map_or(core::ptr::null(), |v| (*v).readdir()) + obj.map_or(DirectoryEntry::Invalid(-crate::errno::EINVAL), |v| { + (*v).readdir() + }) } #[no_mangle] -pub extern "C" fn sys_readdir(fd: FileDescriptor) -> *const Dirent { +pub extern "C" fn sys_readdir(fd: FileDescriptor) -> DirectoryEntry { kernel_function!(__sys_readdir(fd)) }