Skip to content

Commit 5fb8b6a

Browse files
sys_fstat: initial implementation
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 87bea3f commit 5fb8b6a

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

patches/mlibc/mlibc.patch

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
From d1583428ad4f44bb2ca458e06e3a0c60f2c45737 Mon Sep 17 00:00:00 2001
1+
From 4b435231bee99fa0da6f9498c5342488e02a7bbd Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
33
Date: Thu, 10 Feb 2022 19:12:25 +1100
44
Subject: [PATCH] yes
55

66
Signed-off-by: Andy-Python-Programmer <[email protected]>
77
---
8-
.gitignore | 7 +++++++
9-
sysdeps/aero/generic/filesystem.cpp | 28 +++++++++++++++++++++-------
10-
sysdeps/aero/include/aero/syscall.h | 5 +++++
11-
3 files changed, 33 insertions(+), 7 deletions(-)
8+
.gitignore | 7 ++++++
9+
sysdeps/aero/generic/filesystem.cpp | 34 +++++++++++++++++++++++------
10+
sysdeps/aero/include/aero/syscall.h | 6 +++++
11+
3 files changed, 40 insertions(+), 7 deletions(-)
1212

1313
diff --git a/.gitignore b/.gitignore
1414
index dbb35e8b..384b3395 100644
@@ -26,10 +26,15 @@ index dbb35e8b..384b3395 100644
2626
+# of the project directory.
2727
+compile_commands.json
2828
diff --git a/sysdeps/aero/generic/filesystem.cpp b/sysdeps/aero/generic/filesystem.cpp
29-
index 6a13f19c..ec81503c 100644
29+
index 6a13f19c..69833ae5 100644
3030
--- a/sysdeps/aero/generic/filesystem.cpp
3131
+++ b/sysdeps/aero/generic/filesystem.cpp
32-
@@ -84,9 +84,24 @@ int sys_access(const char *filename, int mode) {
32+
@@ -1,3 +1,4 @@
33+
+#include "mlibc/fsfd_target.hpp"
34+
#include <aero/syscall.h>
35+
36+
#include <sys/ioctl.h>
37+
@@ -84,9 +85,29 @@ int sys_access(const char *filename, int mode) {
3338

3439
int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags,
3540
struct stat *statbuf) {
@@ -42,6 +47,11 @@ index 6a13f19c..ec81503c 100644
4247
+ break;
4348
+ }
4449
+
50+
+ case fsfd_target::fd: {
51+
+ result = syscall(SYS_FSTAT, fd, statbuf);
52+
+ break;
53+
+ }
54+
+
4555
+ default: {
4656
+ mlibc::infoLogger() << "mlibc warning: sys_stat: unsupported fsfd target" << frg::endlog;
4757
+ return EINVAL;
@@ -56,7 +66,7 @@ index 6a13f19c..ec81503c 100644
5666
return 0;
5767
}
5868

59-
@@ -192,10 +207,10 @@ int sys_read_entries(int handle, void *buffer, size_t max_size,
69+
@@ -192,10 +213,10 @@ int sys_read_entries(int handle, void *buffer, size_t max_size,
6070
auto entry = (struct aero_dir_entry *)buffer;
6171

6272
struct dirent dirent = {
@@ -71,7 +81,7 @@ index 6a13f19c..ec81503c 100644
7181
};
7282

7383
// The reclen is the size of the dirent struct, plus the size of the name.
74-
@@ -252,7 +267,6 @@ int sys_pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
84+
@@ -252,7 +273,6 @@ int sys_pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
7585
const struct timespec *timeout, const sigset_t *sigmask,
7686
int *num_events) {
7787
mlibc::infoLogger() << "sys_pselect() is not implemented" << frg::endlog;
@@ -80,10 +90,10 @@ index 6a13f19c..ec81503c 100644
8090
}
8191

8292
diff --git a/sysdeps/aero/include/aero/syscall.h b/sysdeps/aero/include/aero/syscall.h
83-
index 07b1b51b..1e3178d2 100644
93+
index 07b1b51b..f6d7836d 100644
8494
--- a/sysdeps/aero/include/aero/syscall.h
8595
+++ b/sysdeps/aero/include/aero/syscall.h
86-
@@ -49,6 +49,11 @@
96+
@@ -49,6 +49,12 @@
8797
#define SYS_DUP 42
8898
#define SYS_FCNTL 43
8999
#define SYS_DUP2 44
@@ -92,6 +102,7 @@ index 07b1b51b..1e3178d2 100644
92102
+#define SYS_DISCOVER_ROOT 47
93103
+#define SYS_BECOME_ROOT 48
94104
+#define SYS_STAT 49
105+
+#define SYS_FSTAT 50
95106

96107
// Invalid syscall used to trigger a log error in the kernel (as a hint)
97108
// so, that we can implement the syscall in the kernel.

src/aero_kernel/src/syscall/fs.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,17 +364,36 @@ pub fn fcntl(fd: usize, command: usize, arg: usize) -> Result<usize, AeroSyscall
364364
}
365365
}
366366

367-
pub fn stat(path: usize, path_size: usize, stat_struct: usize) -> Result<usize, AeroSyscallError> {
367+
/// Validates the [`aero_syscall::Stat`] struct provided by the user and returns a mutable
368+
/// reference to it. [`AeroSyscallError::EFAULT`] is returned if the provided pointer is outside
369+
/// of the user's address space.
370+
fn validate_stat_struct<'struc>(
371+
stat_struct: usize,
372+
) -> Result<&'struc mut aero_syscall::Stat, AeroSyscallError> {
368373
let stat_struct = VirtAddr::new(stat_struct as _)
369374
.validate_user()
370375
.ok_or(AeroSyscallError::EFAULT)?;
371376

372377
// SAFETY: The user provided address is validated above.
373-
let stat_struct = unsafe { stat_struct.read_mut::<aero_syscall::Stat>() };
378+
Ok(unsafe { stat_struct.read_mut::<aero_syscall::Stat>() })
379+
}
374380

375-
let path = validate_str(path as *mut u8, path_size).ok_or(AeroSyscallError::EINVAL)?;
376-
log::debug!("sys_stat: {path}");
381+
pub fn fstat(fd: usize, stat_struct: usize) -> Result<usize, AeroSyscallError> {
382+
let stat_struct = validate_stat_struct(stat_struct)?;
383+
let handle = scheduler::get_scheduler()
384+
.current_task()
385+
.file_table
386+
.get_handle(fd)
387+
.ok_or(AeroSyscallError::EBADFD)?;
388+
389+
*stat_struct = handle.inode().stat()?;
390+
Ok(0)
391+
}
377392

393+
pub fn stat(path: usize, path_size: usize, stat_struct: usize) -> Result<usize, AeroSyscallError> {
394+
let stat_struct = validate_stat_struct(stat_struct)?;
395+
396+
let path = validate_str(path as *mut u8, path_size).ok_or(AeroSyscallError::EINVAL)?;
378397
let path = Path::new(path);
379398

380399
let file = fs::lookup_path(path)?;

src/aero_kernel/src/syscall/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
//! | 46 | ipc_recv |
7171
//! | 47 | ipc_discover_root |
7272
//! | 48 | ipc_become_root |
73+
//! | 49 | stat |
74+
//! | 50 | fstat |
7375
7476
use core::mem::MaybeUninit;
7577

@@ -185,6 +187,7 @@ pub fn generic_do_syscall(
185187
SYS_DUP2 => fs::dup2(b, c, d),
186188
SYS_FCNTL => fs::fcntl(b, c, d),
187189
SYS_STAT => fs::stat(b, c, d),
190+
SYS_FSTAT => fs::fstat(b, c),
188191

189192
SYS_SOCKET => net::socket(b, c, d),
190193
SYS_BIND => net::bind(b, c, d),

src/aero_syscall/src/consts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub const SYS_IPC_RECV: usize = 46;
6868
pub const SYS_IPC_DISCOVER_ROOT: usize = 47;
6969
pub const SYS_IPC_BECOME_ROOT: usize = 48;
7070
pub const SYS_STAT: usize = 49;
71+
pub const SYS_FSTAT: usize = 50;
7172

7273
// fcntl constants:
7374
pub const F_GETFD: usize = 3;

src/aero_syscall/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,3 +939,8 @@ pub fn sys_stat(path: &str, stat: &mut Stat) -> Result<usize, AeroSyscallError>
939939

940940
isize_as_syscall_result(value as _)
941941
}
942+
943+
pub fn sys_fstat(fd: usize, stat: &mut Stat) -> Result<usize, AeroSyscallError> {
944+
let value = syscall2(prelude::SYS_FSTAT, fd, stat as *mut Stat as usize);
945+
isize_as_syscall_result(value as _)
946+
}

0 commit comments

Comments
 (0)