From fc7986e5be2148d68d627f633e440d19e310bce3 Mon Sep 17 00:00:00 2001 From: Matheus Date: Fri, 19 Aug 2022 22:54:07 -0300 Subject: [PATCH] close syscall --- src/arch/x86_64/syscall.asm | 4 +++- src/fs/tmpfs.c | 2 +- src/sys/syscalls/close.c | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/sys/syscalls/close.c diff --git a/src/arch/x86_64/syscall.asm b/src/arch/x86_64/syscall.asm index c75e911a..db797d31 100644 --- a/src/arch/x86_64/syscall.asm +++ b/src/arch/x86_64/syscall.asm @@ -8,7 +8,8 @@ extern syscall_gettid extern syscall_open extern syscall_read extern syscall_lseek -func_count equ 6 +extern syscall_close +func_count equ 8 func_table: @@ -19,6 +20,7 @@ func_table: dq syscall_open dq syscall_read dq syscall_lseek + dq syscall_close section .text global asm_syscall_entry diff --git a/src/fs/tmpfs.c b/src/fs/tmpfs.c index aa360977..9ae20f49 100644 --- a/src/fs/tmpfs.c +++ b/src/fs/tmpfs.c @@ -69,7 +69,7 @@ static int tmpfs_read(int *error, vnode_t* node, void* buff, size_t count, size_ static int tmpfs_unmount(fs_t* fs) UNIMPLEMENTED static int tmpfs_open(dirnode_t* parent, char* name){ return ENOENT;} // tmpfs open should never be called if a file exists -static int tmpfs_close(vnode_t* node) UNIMPLEMENTED +static int tmpfs_close(vnode_t* node){return 0;} // nothing to do really static int tmpfs_mkdir(dirnode_t* parent, char* name, mode_t mode){ diff --git a/src/sys/syscalls/close.c b/src/sys/syscalls/close.c new file mode 100644 index 00000000..330d5149 --- /dev/null +++ b/src/sys/syscalls/close.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +syscallret syscall_close(int ifd){ + + syscallret retv; + retv.ret = -1; + + proc_t* proc = arch_getcls()->thread->proc; + + fd_t* fd = proc->fds + ifd; + + spinlock_acquire(&proc->fdlock); + + if(proc->fdcount <= ifd || fd->node == NULL || (fd->flags & FD_FLAGS_READ) == 0){ + spinlock_release(&proc->fdlock); + retv.errno = EBADF; + return retv; + } + + spinlock_acquire(&fd->lock); + spinlock_release(&proc->fdlock); + + int ret = vfs_close(fd->node); + + if(ret){ + retv.errno = ret; + goto _ret; + } + + // the fd table isn't resized on a close + // although that can be added if it becomes an issue + + fd->node = NULL; + + retv.ret = 0; + retv.errno = 0; + + _ret: + + spinlock_release(&fd->lock); + return retv; +}