diff options
author | Anton Kling <anton@kling.gg> | 2023-11-15 02:44:45 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2023-11-15 21:40:13 +0100 |
commit | e22e493940266e0aa9be2bf58df1821a3539e19b (patch) | |
tree | 64d3516642fae984b5f801db3348d884f661731d /kernel | |
parent | 6747f9407a061684c2fba837541c254f48bfcff0 (diff) |
Kernel/LibC: Add chdir function and syscall
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/Makefile | 4 | ||||
-rw-r--r-- | kernel/cpu/syscall.c | 9 | ||||
-rw-r--r-- | kernel/fs/vfs.c | 21 | ||||
-rw-r--r-- | kernel/fs/vfs.h | 2 | ||||
-rw-r--r-- | kernel/scalls/chdir.c | 6 | ||||
-rw-r--r-- | kernel/scalls/chdir.h | 1 | ||||
-rw-r--r-- | kernel/sched/scheduler.c | 6 |
7 files changed, 41 insertions, 8 deletions
diff --git a/kernel/Makefile b/kernel/Makefile index e7d67dd..eb8a90f 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,7 +1,7 @@ CC="i686-sb-gcc" AS="i686-sb-as" -OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o scalls/ppoll.o scalls/ftruncate.o kubsan.o scalls/mmap.o drivers/serial.o scalls/accept.o scalls/bind.o scalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o scalls/shm.o elf.o ksbrk.o sched/scheduler.o scalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o scalls/msleep.o scalls/uptime.o scalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o scalls/recvfrom.o math.o scalls/sendto.o signal.o scalls/kill.o scalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o -CFLAGS = -O3 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign +OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o scalls/ppoll.o scalls/ftruncate.o kubsan.o scalls/mmap.o drivers/serial.o scalls/accept.o scalls/bind.o scalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o scalls/shm.o elf.o ksbrk.o sched/scheduler.o scalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o scalls/msleep.o scalls/uptime.o scalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o scalls/recvfrom.o math.o scalls/sendto.o signal.o scalls/kill.o scalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o scalls/chdir.o +CFLAGS = -O0 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign INCLUDE=-I./includes/ -I./libc/include/ all: myos.iso diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c index 024d06f..31fa055 100644 --- a/kernel/cpu/syscall.c +++ b/kernel/cpu/syscall.c @@ -9,6 +9,7 @@ #include <kmalloc.h> #include <scalls/accept.h> #include <scalls/bind.h> +#include <scalls/chdir.h> #include <scalls/ftruncate.h> #include <scalls/kill.h> #include <scalls/mkdir.h> @@ -22,8 +23,8 @@ #include <scalls/socket.h> #include <scalls/stat.h> #include <scalls/uptime.h> -#include <typedefs.h> #include <string.h> +#include <typedefs.h> #pragma GCC diagnostic ignored "-Wpedantic" @@ -167,11 +168,11 @@ void (*syscall_functions[])() = { (void(*))syscall_msleep, (void(*))syscall_uptime, (void(*))syscall_mkdir, (void(*))syscall_recvfrom, (void(*))syscall_sendto, (void(*))syscall_kill, - (void(*))syscall_sigaction, + (void(*))syscall_sigaction, (void(*))syscall_chdir, }; -void syscall_function_handler(u32 eax, u32 arg1, u32 arg2, - u32 arg3, u32 arg4, u32 arg5) { +void syscall_function_handler(u32 eax, u32 arg1, u32 arg2, u32 arg3, u32 arg4, + u32 arg5) { assert(eax < sizeof(syscall_functions) / sizeof(syscall_functions[0])); syscall_functions[eax](arg1, arg2, arg3, arg4, arg5); } diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c index 0911c95..8c9e08d 100644 --- a/kernel/fs/vfs.c +++ b/kernel/fs/vfs.c @@ -168,6 +168,27 @@ int vfs_fstat(int fd, struct stat *buf) { return fd_ptr->inode->stat(fd_ptr, buf); } +int vfs_chdir(const char *path) { + char tmp_path[256]; + char *resolved_path = vfs_resolve_path(path, tmp_path); + { + int tmp_fd = vfs_open(resolved_path, O_READ, 0); + if (0 > tmp_fd) + return tmp_fd; + struct stat stat_result; + vfs_fstat(tmp_fd, &stat_result); + if (STAT_DIR != stat_result.st_mode) { + kprintf("vfs_chdir: -ENOTDIR\n"); + return -ENOTDIR; + } + vfs_close(tmp_fd); + } + strcpy(get_current_task()->current_working_directory, resolved_path); + if ('/' != resolved_path[strlen(resolved_path)]) + strcat(get_current_task()->current_working_directory, "/"); + return 0; +} + int vfs_mkdir(const char *path, int mode) { vfs_mounts_t *file_mount = 0; int length = 0; diff --git a/kernel/fs/vfs.h b/kernel/fs/vfs.h index 3e3e1ff..b6a3401 100644 --- a/kernel/fs/vfs.h +++ b/kernel/fs/vfs.h @@ -79,7 +79,7 @@ void vfs_mount(char *path, vfs_inode_t *local_root); int vfs_pwrite(int fd, void *buf, u64 count, u64 offset); int raw_vfs_pwrite(vfs_fd_t *vfs_fd, void *buf, u64 count, u64 offset); int raw_vfs_pread(vfs_fd_t *vfs_fd, void *buf, u64 count, u64 offset); -int vfs_pread(int fd, void *buf, u32 count, u32 offset); +int vfs_pread(int fd, void *buf, u64 count, u64 offset); vfs_vm_object_t *vfs_get_vm_object(int fd, u64 length, u64 offset); int vfs_dup2(int org_fd, int new_fd); vfs_inode_t *vfs_internal_open(const char *file); diff --git a/kernel/scalls/chdir.c b/kernel/scalls/chdir.c new file mode 100644 index 0000000..ec6ba92 --- /dev/null +++ b/kernel/scalls/chdir.c @@ -0,0 +1,6 @@ +#include <fs/vfs.h> +#include <scalls/chdir.h> + +int syscall_chdir(const char *path) { + return vfs_chdir(path); +} diff --git a/kernel/scalls/chdir.h b/kernel/scalls/chdir.h new file mode 100644 index 0000000..574ca0d --- /dev/null +++ b/kernel/scalls/chdir.h @@ -0,0 +1 @@ +int syscall_chdir(const char *path); diff --git a/kernel/sched/scheduler.c b/kernel/sched/scheduler.c index e81320b..7d13277 100644 --- a/kernel/sched/scheduler.c +++ b/kernel/sched/scheduler.c @@ -54,7 +54,11 @@ process_t *create_process(process_t *p) { r->cr3); r->signal_handler_stack = 0x80000000; - strcpy(r->current_working_directory, "/"); + if (p) { + strcpy(r->current_working_directory, p->current_working_directory); + } else { + strcpy(r->current_working_directory, "/"); + } r->data_segment_end = (p) ? p->data_segment_end : NULL; memset((void *)r->halts, 0, 2 * sizeof(u32)); for (int i = 0; i < 100; i++) { |