Skip to content

Commit 51c9079

Browse files
feat(syscall/fs): handle non-cwd fd {open,chdir}
* Handle non-AT_FDCWD file descriptors for {open,chdir} system calls. The updated signature and behaviour has also been updated in the mlibc patches. Signed-off-by: Anhad Singh <andypythonappdeveloper@gmail.com>
1 parent 126af76 commit 51c9079

File tree

3 files changed

+87
-23
lines changed

3 files changed

+87
-23
lines changed

patches/mlibc/jinx-working-patch.patch

+55
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,58 @@ index 3474615..d06f130 100644
1515
}
1616

1717
char **backtrace_symbols(void *const *, int) {
18+
diff --git mlibc-clean/sysdeps/aero/generic/aero.cpp mlibc-workdir/sysdeps/aero/generic/aero.cpp
19+
index 80f9c6f..897986d 100644
20+
--- mlibc-clean/sysdeps/aero/generic/aero.cpp
21+
+++ mlibc-workdir/sysdeps/aero/generic/aero.cpp
22+
@@ -200,14 +200,19 @@ int sys_getcwd(char *buffer, size_t size) {
23+
return 0;
24+
}
25+
26+
-int sys_chdir(const char *path) {
27+
- auto result = syscall(SYS_CHDIR, path, strlen(path));
28+
+static int sys_chdir_impl(int fd, const char *path) {
29+
+ auto ret = syscall(SYS_CHDIR, fd, path, strlen(path));
30+
+ if(int e = sc_error(ret); e)
31+
+ return e;
32+
+ return 0;
33+
+}
34+
35+
- if (result < 0) {
36+
- return -result;
37+
- }
38+
+int sys_chdir(const char *path) {
39+
+ return sys_chdir_impl(AT_FDCWD, path);
40+
+}
41+
42+
- return 0;
43+
+int sys_fchdir(int fd) {
44+
+ return sys_chdir_impl(fd, "");
45+
}
46+
47+
int sys_gethostname(char *buffer, size_t bufsize) {
48+
diff --git mlibc-clean/sysdeps/aero/generic/filesystem.cpp mlibc-workdir/sysdeps/aero/generic/filesystem.cpp
49+
index 95c49b9..9416be7 100644
50+
--- mlibc-clean/sysdeps/aero/generic/filesystem.cpp
51+
+++ mlibc-workdir/sysdeps/aero/generic/filesystem.cpp
52+
@@ -69,13 +69,14 @@ int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
53+
}
54+
55+
int sys_open(const char *filename, int flags, mode_t mode, int *fd) {
56+
- auto result = syscall(SYS_OPEN, 0, filename, strlen(filename), flags);
57+
-
58+
- if (result < 0) {
59+
- return -result;
60+
- }
61+
+ return sys_openat(AT_FDCWD, filename, flags, mode, fd);
62+
+}
63+
64+
- *fd = result;
65+
+int sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd) {
66+
+ auto ret = syscall(SYS_OPEN, dirfd, path, strlen(path), flags, mode);
67+
+ if (int e = sc_error(ret); e)
68+
+ return e;
69+
+ *fd = ret;
70+
return 0;
71+
}
72+

src/aero_kernel/src/syscall/fs.rs

+30-21
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,16 @@ pub fn read(fd: FileDescriptor, buffer: &mut [u8]) -> Result<usize, SyscallError
102102
}
103103

104104
#[syscall]
105-
pub fn open(fd: usize, path: &Path, mode: usize) -> Result<usize, SyscallError> {
106-
let dir = match fd as isize {
107-
0 => {
108-
if !path.is_absolute() {
109-
scheduler::get_scheduler().current_task().cwd_dirent()
110-
} else {
111-
crate::fs::root_dir().clone()
112-
}
113-
}
114-
115-
_ => {
116-
todo!()
105+
pub fn open(fd: usize, path: &Path, _flags: usize, mode: usize) -> Result<usize, SyscallError> {
106+
let current_thread = scheduler::current_thread();
107+
let at = match fd as isize {
108+
AT_FDCWD if !path.is_absolute() => current_thread.cwd_dirent(),
109+
_ if !path.is_absolute() => {
110+
let ent = FileDescriptor::from_usize(fd).handle()?.inode.clone();
111+
assert!(ent.inode().metadata()?.is_directory());
112+
ent
117113
}
114+
_ => fs::root_dir().clone(),
118115
};
119116

120117
let mut flags = OpenFlags::from_bits(mode).ok_or(SyscallError::EINVAL)?;
@@ -129,7 +126,7 @@ pub fn open(fd: usize, path: &Path, mode: usize) -> Result<usize, SyscallError>
129126
lookup_mode = LookupMode::Create;
130127
}
131128

132-
let inode = fs::lookup_path_with(dir, path, lookup_mode, true)?;
129+
let inode = fs::lookup_path_with(at, path, lookup_mode, true)?;
133130

134131
if flags.contains(OpenFlags::O_DIRECTORY) && !inode.inode().metadata()?.is_directory() {
135132
return Err(SyscallError::ENOTDIR);
@@ -139,9 +136,7 @@ pub fn open(fd: usize, path: &Path, mode: usize) -> Result<usize, SyscallError>
139136
inode.inode().truncate(0)?;
140137
}
141138

142-
Ok(scheduler::current_thread()
143-
.file_table
144-
.open_file(inode.clone(), flags)?)
139+
Ok(current_thread.file_table.open_file(inode.clone(), flags)?)
145140
}
146141

147142
#[syscall]
@@ -183,15 +178,29 @@ pub fn close(fd: FileDescriptor) -> Result<usize, SyscallError> {
183178
}
184179

185180
#[syscall]
186-
pub fn chdir(path: &str) -> Result<usize, SyscallError> {
187-
let inode = fs::lookup_path(Path::new(path))?;
181+
pub fn chdir(fd: usize, path: &Path) -> Result<usize, SyscallError> {
182+
let current_thread = scheduler::current_thread();
183+
let at = match fd as isize {
184+
AT_FDCWD if !path.is_absolute() => current_thread.cwd_dirent(),
185+
_ if !path.is_absolute() => {
186+
let ent = FileDescriptor::from_usize(fd).handle()?.inode.clone();
187+
assert!(ent.inode().metadata()?.is_directory());
188+
ent
189+
}
190+
_ => fs::root_dir().clone(),
191+
};
188192

189-
if !inode.inode().metadata()?.is_directory() {
190-
// A component of path is not a directory.
193+
if path.is_empty() {
194+
current_thread.set_cwd(at);
195+
return Ok(0);
196+
}
197+
198+
let ent = fs::lookup_path_with(at, path, LookupMode::None, true)?;
199+
if !ent.inode().metadata()?.is_directory() {
191200
return Err(SyscallError::ENOTDIR);
192201
}
193202

194-
scheduler::get_scheduler().current_task().set_cwd(inode);
203+
current_thread.set_cwd(ent);
195204
Ok(0)
196205
}
197206

src/aero_kernel/src/syscall/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,12 @@ pub fn generic_do_syscall(
207207
SYS_GETPGID => process::getpgid(b),
208208

209209
SYS_READ => fs::read(b, c, d),
210-
SYS_OPEN => fs::open(b, c, d, e),
210+
SYS_OPEN => fs::open(b, c, d, e, f),
211211
SYS_CLOSE => fs::close(b),
212212
SYS_WRITE => fs::write(b, c, d),
213213
SYS_GETDENTS => fs::getdents(b, c, d),
214214
SYS_GETCWD => fs::getcwd(b, c),
215-
SYS_CHDIR => fs::chdir(b, c),
215+
SYS_CHDIR => fs::chdir(b, c, d),
216216
SYS_MKDIR_AT => fs::mkdirat(b, c, d),
217217
SYS_RMDIR => fs::rmdir(b, c),
218218
SYS_IOCTL => fs::ioctl(b, c, d),

0 commit comments

Comments
 (0)