summaryrefslogtreecommitdiff
path: root/src/kernel
diff options
context:
space:
mode:
authordzwdz2024-05-11 22:05:04 +0200
committerdzwdz2024-05-11 22:05:04 +0200
commit15e02a470652a5d4ef87485b5ae12afc06dc53f8 (patch)
treee27e410885abe69d4fd20211a27e0196392993a4 /src/kernel
parentec3d2400db15e6911138d88f95cae141a9da2130 (diff)
kernel: DUP_RDONLY and DUP_WRONLY
I probably should've tested DUP_WRONLY too, now that I think about it. TODO?
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/handle.c6
-rw-r--r--src/kernel/handle.h7
-rw-r--r--src/kernel/handleset.c28
3 files changed, 38 insertions, 3 deletions
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++;