summaryrefslogtreecommitdiff
path: root/src/kernel/proc.c
diff options
context:
space:
mode:
authordzwdz2024-05-11 20:24:17 +0200
committerdzwdz2024-05-11 20:24:17 +0200
commit9047f1e3f502658de12808015179ab3881a4b03f (patch)
tree4b7f19f3152ab1ed19bc83e2213c679f41110e86 /src/kernel/proc.c
parent1e9887d904280c43c5a92570a07627689c89b48f (diff)
kernel: refactor handle management out of proc.c
Diffstat (limited to 'src/kernel/proc.c')
-rw-r--r--src/kernel/proc.c101
1 files changed, 6 insertions, 95 deletions
diff --git a/src/kernel/proc.c b/src/kernel/proc.c
index 8182a54..194353a 100644
--- a/src/kernel/proc.c
+++ b/src/kernel/proc.c
@@ -24,7 +24,7 @@ Proc *proc_seed(void *data, size_t datalen) {
proc_first->pages = pagedir_new();
proc_first->mount = vfs_mount_seed();
proc_first->globalid = next_pid++;
- proc_first->_handles = kzalloc(sizeof(Handle) * HANDLE_MAX, "init fds");
+ proc_first->hs = hs_init();
proc_first->pns = proc_first;
proc_first->localid = 1;
@@ -95,20 +95,10 @@ Proc *proc_fork(Proc *parent, int flags) {
child->mount->refs++;
if (flags & FORK_SHAREHANDLE) {
- if (!parent->handles_refcount) {
- parent->handles_refcount = kmalloc(sizeof *parent->handles_refcount, "fd refs");
- *parent->handles_refcount = 1;
- }
- *parent->handles_refcount += 1;
- child->handles_refcount = parent->handles_refcount;
- child->_handles = parent->_handles;
+ child->hs = parent->hs;
+ parent->hs->refcount++;
} else {
- child->_handles = kzalloc(sizeof(Handle) * HANDLE_MAX, "handles");
- for (hid_t h = 0; h < HANDLE_MAX; h++) {
- child->_handles[h] = parent->_handles[h];
- if (child->_handles[h])
- child->_handles[h]->refcount++;
- }
+ child->hs = hs_copy(parent->hs);
}
return child;
@@ -253,12 +243,8 @@ void proc_kill(Proc *p, int ret) {
timer_deschedule(p);
}
- if (unref(p->handles_refcount)) {
- for (hid_t hid = 0; hid < HANDLE_MAX; hid++)
- proc_handle_close(p, hid);
- kfree(p->_handles);
- }
- p->_handles = NULL;
+ hs_unref(p->hs);
+ p->hs = NULL;
vfs_mount_remref(p->mount);
p->mount = NULL;
@@ -484,81 +470,6 @@ Proc *proc_next(Proc *root, Proc *p) {
return p->sibling;
}
-hid_t proc_find_free_handle(Proc *proc, hid_t start_at) {
- if (start_at < 0) {
- start_at = 0;
- }
- for (hid_t hid = start_at; hid < HANDLE_MAX; hid++) {
- if (proc->_handles[hid] == NULL)
- return hid;
- }
- return -1;
-}
-
-Handle *proc_handle_get(Proc *p, hid_t id) {
- if (id == HANDLE_NULLFS) {
- static Handle h = (Handle){
- .type = HANDLE_FS_FRONT,
- .backend = NULL,
- .refcount = 2, /* never free */
- };
- return &h;
- } else if (0 <= id && id < HANDLE_MAX) {
- return p->_handles[id];
- } else {
- return NULL;
- }
-}
-
-hid_t proc_handle_init(Proc *p, enum handle_type type, Handle **hs) {
- hid_t hid = proc_find_free_handle(p, 1);
- if (hid < 0) return -1;
- p->_handles[hid] = handle_init(type);
- if (hs) *hs = p->_handles[hid];
- return hid;
-}
-
-hid_t proc_handle_dup(Proc *p, hid_t from, hid_t to, int flags) {
- Handle *fromh, **toh;
-
- if (to < 0 || (flags & DUP_SEARCH)) {
- to = proc_find_free_handle(p, to);
- if (to < 0) return -EMFILE;
- } else if (to >= HANDLE_MAX) {
- return -EBADF;
- }
-
- if (to == from) return to;
- toh = &p->_handles[to];
- fromh = proc_handle_get(p, from);
-
- if (*toh) handle_close(*toh);
- *toh = fromh;
- if (fromh) fromh->refcount++;
-
- return to;
-}
-
-Handle *proc_hid_take(Proc *p, hid_t hid) {
- if (hid < 0 || hid >= HANDLE_MAX) {
- return proc_handle_get(p, hid);
- }
- Handle *h = p->_handles[hid];
- p->_handles[hid] = NULL;
- return h;
-}
-
-hid_t proc_handle_put(Proc *p, Handle *h) {
- assert(h);
- hid_t hid = proc_find_free_handle(p, 1);
- if (hid < 0) {
- handle_close(h);
- return hid;
- }
- p->_handles[hid] = h;
- return hid;
-}
-
void proc_setstate(Proc *p, enum proc_state state) {
assert(p->state != PS_FREED);
if (state == PS_FREED) {