diff options
author | dzwdz | 2022-05-06 14:55:58 +0200 |
---|---|---|
committer | dzwdz | 2022-05-06 14:55:58 +0200 |
commit | c868eb79353cf1da06c00bbd426fbf8aed7b81c9 (patch) | |
tree | 982af9cfa96bb62a8854b1fbc1ceb05587d1ccf2 /src/kernel/proc.c | |
parent | 8ee57c885a72854d1884a886de4db538a8468e07 (diff) |
kernel/proc: fork: be explicit about copying fields
Diffstat (limited to 'src/kernel/proc.c')
-rw-r--r-- | src/kernel/proc.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/kernel/proc.c b/src/kernel/proc.c index 7ef4579..2b96c8f 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -44,36 +44,42 @@ struct process *process_seed(struct kmain_info *info) { struct process *process_fork(struct process *parent, int flags) { struct process *child = kmalloc(sizeof *child); - memcpy(child, parent, sizeof *child); // TODO manually set fields + memset(child, 0, sizeof *child); child->pages = pagedir_copy(parent->pages); + child->regs = parent->regs; + child->state = parent->state; + assert(child->state == PS_RUNNING); // not copying the state union + + child->noreap = (flags & FORK_NOREAP) > 0; + child->sibling = parent->child; child->child = NULL; child->parent = parent; parent->child = child; - child->noreap = (flags & FORK_NOREAP) > 0; - parent->handled_req = NULL; // TODO control this with a flag + child->id = next_pid++; - if ((flags & FORK_NEWFS) == 0) { - if (child->controlled) { - child->controlled->potential_handlers++; - child->controlled->refcount++; - } - } else { - child->controlled = NULL; - } + // TODO control this with a flag + child->handled_req = parent->handled_req; + parent->handled_req = NULL; - for (handle_t h = 0; h < HANDLE_MAX; h++) { - if (child->handles[h]) - child->handles[h]->refcount++; - // no overflow check - if you manage to get 2^32 references to a handle you have bigger problems + if ((flags & FORK_NEWFS) == 0 && parent->controlled) { + // TODO would it be better to change the default to not sharing the controlled fs? + child->controlled = parent->controlled; + child->controlled->potential_handlers++; + child->controlled->refcount++; } + child->mount = parent->mount; assert(child->mount); child->mount->refs++; - child->id = next_pid++; + for (handle_t h = 0; h < HANDLE_MAX; h++) { + child->handles[h] = parent->handles[h]; + if (child->handles[h]) + child->handles[h]->refcount++; + } return child; } |