Luna/kernel/src/sys/waitpid.cpp

29 lines
719 B
C++
Raw Normal View History

2023-03-23 21:42:24 +00:00
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/waitpid.h>
Result<u64> sys_waitpid(Registers*, SyscallArgs args)
{
pid_t pid = (pid_t)args[0];
int* status_ptr = (int*)args[1];
int options = (int)args[2];
Thread* thread = TRY(Result<Thread*>::from_option(Scheduler::find_by_pid(pid), ESRCH));
while (thread->state != ThreadState::Exited)
{
if (options & WNOHANG) return err(EAGAIN);
kernel_sleep(10);
}
int status = (int)thread->status;
thread->state = ThreadState::Dying;
if (status_ptr)
if (!MemoryManager::copy_to_user_typed(status_ptr, &status)) return err(EFAULT);
return (u64)pid;
}