From 4616997f5b7b997f68b9011ae0cfe67c65636c8e Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 4 May 2023 22:58:04 +0200 Subject: [PATCH] kernel: Store a thread's parent directly instead of its parent's PID --- kernel/src/sys/exec.cpp | 2 +- kernel/src/sys/exit.cpp | 2 +- kernel/src/sys/id.cpp | 4 +++- kernel/src/sys/waitpid.cpp | 2 +- kernel/src/thread/Scheduler.cpp | 12 ++++++++---- kernel/src/thread/Scheduler.h | 3 ++- kernel/src/thread/Thread.h | 3 ++- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/kernel/src/sys/exec.cpp b/kernel/src/sys/exec.cpp index 5aa01261..231c4ad2 100644 --- a/kernel/src/sys/exec.cpp +++ b/kernel/src/sys/exec.cpp @@ -125,12 +125,12 @@ Result sys_fork(Registers* regs, SyscallArgs) thread->state = ThreadState::Runnable; thread->is_kernel = false; - thread->parent_id = current->id; thread->fp_data.save(); thread->name = current->name; thread->auth = current->auth; thread->current_directory = current->current_directory; thread->current_directory_path = move(current_directory_path); + thread->parent = current; for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; } diff --git a/kernel/src/sys/exit.cpp b/kernel/src/sys/exit.cpp index 1b005e5b..4fe3e81e 100644 --- a/kernel/src/sys/exit.cpp +++ b/kernel/src/sys/exit.cpp @@ -8,7 +8,7 @@ Result sys_exit(Registers*, SyscallArgs args) Thread* current = Scheduler::current(); Scheduler::for_each_child(current, [](Thread* child) { - child->parent_id = 1; + child->parent = Scheduler::init_thread(); return true; }); diff --git a/kernel/src/sys/id.cpp b/kernel/src/sys/id.cpp index 899e1fb2..608a7a3f 100644 --- a/kernel/src/sys/id.cpp +++ b/kernel/src/sys/id.cpp @@ -9,7 +9,9 @@ Result sys_getpid(Registers*, SyscallArgs) Result sys_getppid(Registers*, SyscallArgs) { - return Scheduler::current()->parent_id; + auto* parent = Scheduler::current()->parent; + + return parent ? parent->id : 0; } Result sys_getuid(Registers*, SyscallArgs) diff --git a/kernel/src/sys/waitpid.cpp b/kernel/src/sys/waitpid.cpp index ce069d6d..f24f83d5 100644 --- a/kernel/src/sys/waitpid.cpp +++ b/kernel/src/sys/waitpid.cpp @@ -17,7 +17,7 @@ Result sys_waitpid(Registers*, SyscallArgs args) { thread = TRY(Result::from_option(Scheduler::find_by_pid(pid), ESRCH)); - if (thread->parent_id != current->id) return err(ECHILD); + if (thread->parent && thread->parent != current) return err(ECHILD); while (thread->state != ThreadState::Exited) { diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 96c41356..142022fc 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -11,6 +11,7 @@ static Thread g_idle; static Thread* g_current = nullptr; +static Thread* g_init = nullptr; static const usize TICKS_PER_TIMESLICE = 20; @@ -23,7 +24,7 @@ namespace Scheduler g_idle.set_ip((u64)CPU::idle_loop); g_idle.state = ThreadState::Idle; g_idle.is_kernel = true; - g_idle.parent_id = 0; + g_idle.parent = nullptr; g_idle.name = "[idle]"; g_idle.ticks_left = 1; @@ -52,6 +53,11 @@ namespace Scheduler return &g_idle; } + Thread* init_thread() + { + return g_init; + } + Result new_kernel_thread_impl(Thread* thread, const char* name) { // If anything fails, make sure to clean up. @@ -66,8 +72,6 @@ namespace Scheduler thread->stack = thread_stack; - thread->parent_id = 0; - thread->name = name; thread->is_kernel = true; @@ -119,7 +123,6 @@ namespace Scheduler thread->is_kernel = false; thread->id = 1; thread->name = name; - thread->parent_id = 0; thread->auth = Credentials { .uid = 0, .euid = 0, .suid = 0, .gid = 0, .egid = 0, .sgid = 0 }; Vector args; @@ -143,6 +146,7 @@ namespace Scheduler thread->sp(), thread->kernel_stack.top()); g_threads.append(thread); + g_init = thread; return thread; } diff --git a/kernel/src/thread/Scheduler.h b/kernel/src/thread/Scheduler.h index 2fc1d7e0..0fba7e82 100644 --- a/kernel/src/thread/Scheduler.h +++ b/kernel/src/thread/Scheduler.h @@ -8,6 +8,7 @@ namespace Scheduler Thread* current(); Thread* idle(); + Thread* init_thread(); Result new_kernel_thread(u64 address, const char* name); Result new_kernel_thread(void (*func)(void), const char* name); @@ -33,7 +34,7 @@ namespace Scheduler { for (Thread* current = thread; current; current = g_threads.next(current).value_or(nullptr)) { - if (current->parent_id == thread->id) + if (current->parent == thread) { bool should_continue = callback(current); if (!should_continue) return; diff --git a/kernel/src/thread/Thread.h b/kernel/src/thread/Thread.h index 5257d5f0..41e4593e 100644 --- a/kernel/src/thread/Thread.h +++ b/kernel/src/thread/Thread.h @@ -55,7 +55,6 @@ struct Thread : public LinkedListNode Registers regs; u64 id; - u64 parent_id; Credentials auth; @@ -90,6 +89,8 @@ struct Thread : public LinkedListNode String current_directory_path = {}; SharedPtr current_directory = {}; + Thread* parent { nullptr }; + PageDirectory* directory; bool is_idle()