Luna/kernel/src/sys/resource.cpp

33 lines
955 B
C++

#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/rusage.h>
static void ticks_to_rusage(struct rusage* ru, u64 sticks, u64 uticks)
{
ru->ru_stime.tv_sec = sticks / 1000;
ru->ru_stime.tv_usec = (sticks % 1000) * 1000;
ru->ru_utime.tv_sec = uticks / 1000;
ru->ru_utime.tv_usec = (uticks % 1000) * 1000;
}
Result<u64> sys_getrusage(Registers*, SyscallArgs args)
{
int who = (int)args[0];
struct rusage* ru = (struct rusage*)args[1];
auto* current = Scheduler::current();
struct rusage kru;
switch (who)
{
case RUSAGE_SELF: ticks_to_rusage(&kru, current->kernel_ticks_self, current->user_ticks_self); break;
case RUSAGE_CHILDREN: ticks_to_rusage(&kru, current->kernel_ticks_children, current->user_ticks_children); break;
default: return err(EINVAL);
}
if (!MemoryManager::copy_to_user_typed(ru, &kru)) return err(EFAULT);
return 0;
}