From 15e02a470652a5d4ef87485b5ae12afc06dc53f8 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sat, 11 May 2024 22:05:04 +0200 Subject: kernel: DUP_RDONLY and DUP_WRONLY I probably should've tested DUP_WRONLY too, now that I think about it. TODO? --- src/kernel/handle.c | 6 +++++- src/kernel/handle.h | 7 +++++-- src/kernel/handleset.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) (limited to 'src/kernel') diff --git a/src/kernel/handle.c b/src/kernel/handle.c index ca4c4c4..882a4fa 100644 --- a/src/kernel/handle.c +++ b/src/kernel/handle.c @@ -19,12 +19,16 @@ void handle_close(Handle *h) { if (--(h->refcount) > 0) return; if (h->type == HANDLE_FILE) { - vfsreq_create((VfsReq) { + if (h->base) { + handle_close(h->base); + } else { + vfsreq_create((VfsReq) { .type = VFSOP_CLOSE, .id = h->file_id, .caller = NULL, .backend = h->backend, }); + } } else if (h->type == HANDLE_PIPE) { assert(!h->pipe.queued); if (h->pipe.sister) { diff --git a/src/kernel/handle.h b/src/kernel/handle.h index d502d5d..af9d45a 100644 --- a/src/kernel/handle.h +++ b/src/kernel/handle.h @@ -13,16 +13,19 @@ enum handle_type { HANDLE_NULL, }; +// TODO use unions +// when making changes, keep in mind that dup creates copies of HANDLE_FILEs struct Handle { enum handle_type type; - VfsBackend *backend; // HANDLE_FILE | HANDLE_FS_FRONT - void __user *file_id; // only applicable to HANDLE_FILE + VfsBackend *backend; /* HANDLE_FILE | HANDLE_FS_FRONT */ + void __user *file_id; /* only applicable to HANDLE_FILE */ bool readable, writeable; /* HANDLE_FILE | HANDLE_PIPE */ VfsReq *req; /* HANDLE_FS_REQ */ struct { Proc *queued; Handle *sister; // the other end, not included in refcount } pipe; + Handle *base; /* used by dup'd HANDLE_FILEs. */ size_t refcount; }; diff --git a/src/kernel/handleset.c b/src/kernel/handleset.c index a25f5c7..fc2d426 100644 --- a/src/kernel/handleset.c +++ b/src/kernel/handleset.c @@ -89,6 +89,34 @@ hs_dup(HandleSet *hs, hid_t from, hid_t to, int flags) fromh = hs_get(hs, from); if (*toh) handle_close(*toh); + + if (flags & (DUP_RDONLY | DUP_WRONLY)) { + if (!fromh || fromh->type != HANDLE_FILE) { + /* only errors out after closing the old handle */ + *toh = NULL; + return -ENOSYS; + } + + /* figure out if we need a need handle */ + bool different = false; + different = different || ((flags & DUP_RDONLY) && fromh->writeable); + different = different || ((flags & DUP_WRONLY) && fromh->readable); + + if (different) { + Handle *new = handle_init(HANDLE_FILE); + new->backend = fromh->backend; + if (new->backend) new->backend->usehcnt++; + new->file_id = fromh->file_id; + new->readable = fromh->readable && !(flags & DUP_WRONLY); + new->writeable = fromh->writeable && !(flags & DUP_RDONLY); + new->base = fromh; + fromh->refcount++; + + *toh = new; + return to; + } + } + *toh = fromh; if (fromh) fromh->refcount++; -- cgit v1.2.3