From: Amelia Coutard Date: Sat, 2 Dec 2023 23:29:08 +0000 (+0100) Subject: Added (but didn't implement yet) the close syscall X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=53a5b24989cbb9333af91ef97c66eb3b8b9c9210;p=voyage-au-centre-des-fichiers.git Added (but didn't implement yet) the close syscall --- diff --git a/doc.txt b/doc.txt index 2eb0be8..cd179c8 100644 --- a/doc.txt +++ b/doc.txt @@ -67,6 +67,10 @@ ssize_t write(fd_t file, char const data[len], ssize_t len); Write up to len characters to file. Return the number of actually written characters, or -1 on error. +ssize_t close(fd_t file); + Termine the fd `file`. If there were no other copies of it, the corresponding file is closed. + Returns -1 if `file` isn't an opened file descriptor. + ssize_t fseek(fd_t file, ssize_t offset, int from); If from is 0, seek offset bytes from start. If from is 1, seek offset bytes from current file position. diff --git a/kernel/src/ring3.S b/kernel/src/ring3.S index 8e8fbcc..fb5f837 100644 --- a/kernel/src/ring3.S +++ b/kernel/src/ring3.S @@ -92,6 +92,7 @@ syscalls_call_table: .quad syscall_open .quad syscall_read .quad syscall_write + .quad syscall_close .quad syscall_fseek syscalls_call_table_end: .set syscall_n, (syscalls_call_table_end - syscalls_call_table) >> 3 # Because / 8 *doesn't work* ! diff --git a/kernel/src/ring3.cpp b/kernel/src/ring3.cpp index cc2d24f..11e6746 100644 --- a/kernel/src/ring3.cpp +++ b/kernel/src/ring3.cpp @@ -75,6 +75,12 @@ extern "C" std::int64_t syscall_read(std::int64_t file, char* data, std::int64_t return len; } +// extern "C" std::int64_t syscall_close(std::int64_t file) { +extern "C" std::int64_t syscall_close(std::int64_t) { + os::assert(false, "Close isn't implemented for now."); + __builtin_unreachable(); +} + extern "C" std::int64_t syscall_write(std::int64_t file, char const* data, std::int64_t len) { os::assert(file == 0, "Write isn't really implemented for now."); os::assert(len >= 0, "Write expects a positive size."); diff --git a/test_module/src/test.S b/test_module/src/test.S index 8635763..2d326b2 100644 --- a/test_module/src/test.S +++ b/test_module/src/test.S @@ -32,8 +32,14 @@ write: syscall ret +.globl close +close: + mov $3, %rax + syscall + ret + .globl fseek fseek: - mov $3, %rax + mov $4, %rax syscall ret diff --git a/test_module/src/test.cpp b/test_module/src/test.cpp index 0f6f836..14a0df2 100644 --- a/test_module/src/test.cpp +++ b/test_module/src/test.cpp @@ -26,30 +26,36 @@ namespace sys { extern "C" int64_t open(int64_t wd, char const* path, int64_t path_len, open_options options); extern "C" int64_t read(int64_t file, char* data, int64_t len); extern "C" int64_t write(int64_t file, char const* data, int64_t len); +extern "C" int64_t close(int64_t file); extern "C" int64_t fseek(int64_t file, int64_t offset, int from); } // namespace sys extern "C" void _start() { - const int64_t fd = 0; // sys::open(0, "/serial", 7, sys::MODE_RW | sys::OPTION_INPLACE); + const int64_t wd = sys::open(0, "/", 1, open_options(MODE_RW | OPTION_INPLACE | MODE_DIRECTORY)); + const int64_t serial = sys::open(wd, "serial", 1, MODE_RW); + const int64_t serial_ = sys::open(wd, "/serial", 1, MODE_WO); - sys::write(fd, "Entrez votre nom: ", 18); + sys::write(serial, "Entrez votre nom: ", 18); char data[32]; int64_t read = 0; int64_t read_this_time; do { - read_this_time = sys::read(fd, data + read, 1); + read_this_time = sys::read(serial, data + read, 1); if (read_this_time) { if (data[read] == '\r') { - sys::write(fd, "\n", 1); + sys::write(serial, "\n", 1); } else { - sys::write(fd, data + read, 1); + sys::write(serial, data + read, 1); } } read += read_this_time; } while (read_this_time != 0 && read < 32 && data[read - 1] != '\r'); - sys::write(fd, "Bonjour, ", 9); - sys::write(fd, data, read - (data[read - 1] == '\r' ? 1 : 0)); - sys::write(fd, ".\n", 2); + sys::write(serial_, "Bonjour, ", 9); + sys::write(serial_, data, read - (data[read - 1] == '\r' ? 1 : 0)); + sys::write(serial_, ".\n", 2); + + sys::close(serial); + sys::close(serial_); while (true) { sys::fseek(0, 0, 1);