Skip to content

Commit

Permalink
Kernel: Add getrusage() syscall
Browse files Browse the repository at this point in the history
Only the two timeval fields are maintained, as required by the POSIX
standard.
  • Loading branch information
LucasChollet authored and awesomekling committed Feb 28, 2022
1 parent 39bfc48 commit 839d3d9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
40 changes: 40 additions & 0 deletions Kernel/API/POSIX/sys/resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2022, Lucas Chollet <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <Kernel/API/POSIX/sys/time.h>
#include <Kernel/API/POSIX/sys/types.h>

#ifdef __cplusplus
extern "C" {
#endif

struct rusage {
struct timeval ru_utime;
struct timeval ru_stime;
long ru_maxrss;
long ru_ixrss;
long ru_idrss;
long ru_isrss;
long ru_minflt;
long ru_majflt;
long ru_nswap;
long ru_inblock;
long ru_oublock;
long ru_msgsnd;
long ru_msgrcv;
long ru_nsignals;
long ru_nvcsw;
long ru_nivcsw;
};

#define RUSAGE_SELF 1
#define RUSAGE_CHILDREN 2

#ifdef __cplusplus
}
#endif
1 change: 1 addition & 0 deletions Kernel/API/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ enum class NeedsBigProcessLock {
S(getrandom, NeedsBigProcessLock::No) \
S(getresgid, NeedsBigProcessLock::Yes) \
S(getresuid, NeedsBigProcessLock::Yes) \
S(getrusage, NeedsBigProcessLock::Yes) \
S(getsid, NeedsBigProcessLock::Yes) \
S(getsockname, NeedsBigProcessLock::Yes) \
S(getsockopt, NeedsBigProcessLock::Yes) \
Expand Down
5 changes: 3 additions & 2 deletions Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ set(KERNEL_SOURCES
Syscalls/readlink.cpp
Syscalls/realpath.cpp
Syscalls/rename.cpp
Syscalls/resource.cpp
Syscalls/rmdir.cpp
Syscalls/sched.cpp
Syscalls/sendfd.cpp
Expand Down Expand Up @@ -383,7 +384,7 @@ else()
set(SOURCES
${SOURCES}
${AK_SOURCES}

Arch/aarch64/dummy.cpp
Arch/aarch64/SmapDisabler.cpp
Arch/aarch64/ScopedCritical.cpp
Expand Down Expand Up @@ -426,7 +427,7 @@ else() # Assume Clang

# We need this in order to pick up the #define __serenity__, otherwise we end up including unistd.h into the linker script
set(TARGET_STRING "--target=${CMAKE_CXX_COMPILER_TARGET}")

add_link_options(LINKER:--build-id=none LINKER:--pack-dyn-relocs=relr)
endif()

Expand Down
2 changes: 2 additions & 0 deletions Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <AK/Variant.h>
#include <AK/WeakPtr.h>
#include <AK/Weakable.h>
#include <Kernel/API/POSIX/sys/resource.h>
#include <Kernel/API/Syscall.h>
#include <Kernel/Assertions.h>
#include <Kernel/AtomicEdgeAction.h>
Expand Down Expand Up @@ -286,6 +287,7 @@ class Process final
ErrorOr<FlatPtr> sys$getppid();
ErrorOr<FlatPtr> sys$getresuid(Userspace<UserID*>, Userspace<UserID*>, Userspace<UserID*>);
ErrorOr<FlatPtr> sys$getresgid(Userspace<GroupID*>, Userspace<GroupID*>, Userspace<GroupID*>);
ErrorOr<FlatPtr> sys$getrusage(int, Userspace<rusage*>);
ErrorOr<FlatPtr> sys$umask(mode_t);
ErrorOr<FlatPtr> sys$open(Userspace<const Syscall::SC_open_params*>);
ErrorOr<FlatPtr> sys$close(int fd);
Expand Down
37 changes: 37 additions & 0 deletions Kernel/Syscalls/resource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022, Lucas Chollet <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <Kernel/Process.h>

namespace Kernel {

ErrorOr<FlatPtr> Process::sys$getrusage(int who, Userspace<rusage*> user_usage)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)

rusage usage {};

auto const ticks_per_second = TimeManagement::the().ticks_per_second();

switch (who) {
case RUSAGE_SELF:
usage.ru_utime = Time::from_ticks(m_ticks_in_user, ticks_per_second).to_timeval();
usage.ru_stime = Time::from_ticks(m_ticks_in_kernel, ticks_per_second).to_timeval();
break;
case RUSAGE_CHILDREN:
usage.ru_utime = Time::from_ticks(m_ticks_in_user_for_dead_children, ticks_per_second).to_timeval();
usage.ru_stime = Time::from_ticks(m_ticks_in_kernel_for_dead_children, ticks_per_second).to_timeval();
break;
default:
return EINVAL;
}

TRY(copy_to_user(user_usage, &usage));

return 0;
}

}

0 comments on commit 839d3d9

Please sign in to comment.