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/handleset.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/kernel/handleset.c') 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