syscalls: chdir

This commit is contained in:
Matheus
2023-06-27 19:35:19 -03:00
parent 7e7504cde7
commit c5d87d3109
3 changed files with 56 additions and 5 deletions
+3 -1
View File
@@ -20,6 +20,7 @@ extern syscall_dup
extern syscall_dup2
extern syscall_dup3
extern syscall_fcntl
extern syscall_chdir
syscalltab:
dq syscall_print
dq syscall_mmap
@@ -42,7 +43,8 @@ dq syscall_dup
dq syscall_dup2
dq syscall_dup3
dq syscall_fcntl
syscallcount equ 21
dq syscall_chdir
syscallcount equ 22
section .text
global arch_syscall_entry
; on entry:
+43
View File
@@ -0,0 +1,43 @@
#include <kernel/syscalls.h>
#include <kernel/scheduler.h>
#include <kernel/vfs.h>
#include <kernel/alloc.h>
#include <errno.h>
syscallret_t syscall_chdir(context_t *, const char *upath) {
syscallret_t ret = {
.ret = -1
};
if (upath > (char *)USERSPACE_END) {
ret.errno = EFAULT;
return ret;
}
// TODO safe string ops
char *path = alloc(strlen(upath) + 1);
strcpy(path, upath);
vnode_t *ref = path[0] == '/' ? sched_getroot() : sched_getcwd();
vnode_t *new = NULL;
ret.errno = vfs_lookup(&new, ref, path, NULL, 0);
if (ret.errno)
goto cleanup;
if (new->type != V_TYPE_DIR) {
ret.errno = ENOTDIR;
goto cleanup;
}
sched_setcwd(new);
cleanup:
if (new)
VOP_RELEASE(new);
free(path);
VOP_RELEASE(ref);
return ret;
}