diff options
author | dzwdz | 2021-08-18 17:14:22 +0200 |
---|---|---|
committer | dzwdz | 2021-08-18 17:14:22 +0200 |
commit | 5ec560b7a0b5cea60bbda61a48fcc4e9866ce6b1 (patch) | |
tree | 41df495c0b5aa7a4e3cdb70857fa2604952c4213 /src/kernel | |
parent | c9d4cf8572f8cbb0024bfa9802c25473f90e9d60 (diff) |
store the processes as a tree instead of a list
I'm about to need that for waits(). There's no single list of processes
for simplicity's sake, but the search will now be even slower and it
might even introduce a potential vuln! How fun! Someone could override
stuff in .bss with random values. I'll either make gcc check if it
hasn't gone past the end of the stack, or turn this into a non recursive
function.
Diffstat (limited to 'src/kernel')
-rw-r--r-- | src/kernel/proc.c | 41 | ||||
-rw-r--r-- | src/kernel/proc.h | 3 |
2 files changed, 32 insertions, 12 deletions
diff --git a/src/kernel/proc.c b/src/kernel/proc.c index b705324..991ab78 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -11,7 +11,8 @@ struct process *process_seed() { struct process *proc = kmalloc(sizeof(struct process)); proc->pages = pagedir_new(); proc->state = PS_RUNNING; - proc->next = NULL; + proc->sibling = NULL; + proc->child = NULL; process_first = proc; @@ -29,13 +30,16 @@ struct process *process_seed() { return proc; } -struct process *process_clone(struct process *orig) { - struct process *clone = kmalloc(sizeof(struct process)); - memcpy(clone, orig, sizeof(struct process)); - clone->pages = pagedir_copy(orig->pages); - orig->next = clone; +struct process *process_clone(struct process *parent) { + struct process *child = kmalloc(sizeof(struct process)); + memcpy(child, parent, sizeof(struct process)); - return clone; + child->pages = pagedir_copy(parent->pages); + child->sibling = parent->child; + child->child = NULL; + parent->child = child; + + return child; } void process_switch(struct process *proc) { @@ -44,9 +48,24 @@ void process_switch(struct process *proc) { sysexit(proc->regs); } +// TODO there's no check for going past the stack - VULN +struct process *_process_find_recursive( + enum process_state target, struct process *iter) { + struct process *in; + while (iter) { + if (iter->state == target) + return iter; + + // DFS + in = _process_find_recursive(target, iter->child); + if (in) + return in; + + iter = iter->sibling; + } + return NULL; +} + struct process *process_find(enum process_state target) { - struct process *iter = process_first; - while (iter && (iter->state != target)) - iter = iter->next; - return iter; + return _process_find_recursive(target, process_first); } diff --git a/src/kernel/proc.h b/src/kernel/proc.h index f5b943a..b2a05c0 100644 --- a/src/kernel/proc.h +++ b/src/kernel/proc.h @@ -12,7 +12,8 @@ struct process { struct registers regs; enum process_state state; - struct process *next; + struct process *sibling; + struct process *child; }; extern struct process *process_first; |