From b75dd3a112708584a930f85d345835f36a722f5a Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 18 Apr 2024 18:30:42 -0400 Subject: kernel: filedesc: Handle multiple oflag bits Signed-off-by: Ian Moffett --- sys/include/sys/filedesc.h | 1 + sys/kern/kern_filedesc.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'sys') diff --git a/sys/include/sys/filedesc.h b/sys/include/sys/filedesc.h index 83c32ee..f8f9ba6 100644 --- a/sys/include/sys/filedesc.h +++ b/sys/include/sys/filedesc.h @@ -47,6 +47,7 @@ struct proc; struct filedesc { int fdno; + int oflag; off_t offset; bool is_dir; struct vnode *vnode; diff --git a/sys/kern/kern_filedesc.c b/sys/kern/kern_filedesc.c index 5488c35..b95d123 100644 --- a/sys/kern/kern_filedesc.c +++ b/sys/kern/kern_filedesc.c @@ -242,6 +242,10 @@ write(int fd, const void *buf, size_t count) } desc = fd_from_fdnum(td, fd); + if (desc->oflag != O_WRONLY && desc->oflag != O_WRONLY) { + ret = -EACCES; + goto cleanup; + } /* Does this file descriptor exist? */ if (desc == NULL) { @@ -291,15 +295,7 @@ open(const char *pathname, int oflag) return status; } - /* - * TODO: Handle more flags... For now we only support - * O_RDONLY, so deny other flags. - */ - if ((oflag & ~O_RDONLY) != 0){ - fd_close_fdnum(this_td(), fd->fdno); - return -EACCES; - } - + fd->oflag = oflag; fd->vnode = vp; fd->is_dir = (vp->type == VDIR); return fd->fdno; @@ -329,6 +325,10 @@ read(int fd, void *buf, size_t count) return -EBADF; } + if (fd_desc->oflag == O_WRONLY) { + return -EACCES; + } + sio.offset = fd_desc->offset; vnode = fd_desc->vnode; -- cgit v1.2.3