Skip to content

Commit

Permalink
remove definition of a classical error number
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Sep 18, 2023
1 parent 89c6fa5 commit e7702d4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 40 deletions.
3 changes: 0 additions & 3 deletions src/errno.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[thread_local]
pub static mut ERRNO: i32 = 0;

/// Operation not permitted
pub const EPERM: i32 = 1;

Expand Down
15 changes: 7 additions & 8 deletions src/fd/file.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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<dyn PosixFile + Send>| {
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()),
}
});

Expand Down
14 changes: 9 additions & 5 deletions src/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 7 additions & 24 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit e7702d4

Please sign in to comment.