From 46fadf4bf14360be046b9b770ddf205fad96a0a7 Mon Sep 17 00:00:00 2001 From: Kacper Date: Mon, 22 Dec 2025 23:49:08 +0100 Subject: Add sys/resource.h implementation and nice(3) --- lib/libc/sys/resource/getpriority.c | 12 ++++++++++++ lib/libc/sys/resource/getrlimit.c | 7 +++++++ lib/libc/sys/resource/getrusage.c | 7 +++++++ lib/libc/sys/resource/setpriority.c | 7 +++++++ lib/libc/sys/resource/setrlimit.c | 7 +++++++ lib/libc/unistd/nice.c | 28 ++++++++++++++++++++++++---- 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 lib/libc/sys/resource/getpriority.c create mode 100644 lib/libc/sys/resource/getrlimit.c create mode 100644 lib/libc/sys/resource/getrusage.c create mode 100644 lib/libc/sys/resource/setpriority.c create mode 100644 lib/libc/sys/resource/setrlimit.c (limited to 'lib/libc') diff --git a/lib/libc/sys/resource/getpriority.c b/lib/libc/sys/resource/getpriority.c new file mode 100644 index 00000000..3df9eb30 --- /dev/null +++ b/lib/libc/sys/resource/getpriority.c @@ -0,0 +1,12 @@ +#include +#include + +int getpriority(int which, int who) +{ + int r = syscall(getpriority, which, who); + + if (r < 0) + return r; + + return 20 - r; +} diff --git a/lib/libc/sys/resource/getrlimit.c b/lib/libc/sys/resource/getrlimit.c new file mode 100644 index 00000000..7b7c8269 --- /dev/null +++ b/lib/libc/sys/resource/getrlimit.c @@ -0,0 +1,7 @@ +#include +#include + +int getrlimit(int resource, struct rlimit *rlp) +{ + return syscall(getrlimit, resource, rlp); +} diff --git a/lib/libc/sys/resource/getrusage.c b/lib/libc/sys/resource/getrusage.c new file mode 100644 index 00000000..a91ded16 --- /dev/null +++ b/lib/libc/sys/resource/getrusage.c @@ -0,0 +1,7 @@ +#include +#include + +int getrusage(int who, struct rusage *r_usage) +{ + return syscall(getrusage, who, r_usage); +} diff --git a/lib/libc/sys/resource/setpriority.c b/lib/libc/sys/resource/setpriority.c new file mode 100644 index 00000000..96bebbf2 --- /dev/null +++ b/lib/libc/sys/resource/setpriority.c @@ -0,0 +1,7 @@ +#include +#include + +int setpriority(int which, int who, int value) +{ + return syscall(setpriority, which, who, value); +} diff --git a/lib/libc/sys/resource/setrlimit.c b/lib/libc/sys/resource/setrlimit.c new file mode 100644 index 00000000..39148bc3 --- /dev/null +++ b/lib/libc/sys/resource/setrlimit.c @@ -0,0 +1,7 @@ +#include +#include + +int setrlimit(int resource, const struct rlimit *rlp) +{ + return syscall(setrlimit, resource, rlp); +} diff --git a/lib/libc/unistd/nice.c b/lib/libc/unistd/nice.c index 78dccbb0..29ac8487 100644 --- a/lib/libc/unistd/nice.c +++ b/lib/libc/unistd/nice.c @@ -1,8 +1,28 @@ -#include // for nice +#include +#include +#include +#include int nice(int incr) { - (void)incr; - // TODO: needs getpriority and setpriority from sys/resouce.h - return 0; + int priority = incr; + + if (incr > NZERO * -2 && incr < NZERO * 2) { + priority += getpriority(PRIO_PROCESS, 0); + } + + if (priority > NZERO - 1) + priority = NZERO - 1; + + if (priority < -NZERO) + priority = -NZERO; + + if (setpriority(PRIO_PROCESS, 0, priority)) { + if (errno == EACCES) + errno = EPERM; + + return -1; + } + + return priority; } -- cgit v1.2.3