diff options
| author | Kacper <kacper@mail.openlinux.dev> | 2025-12-22 23:49:08 +0100 |
|---|---|---|
| committer | Kacper <kacper@mail.openlinux.dev> | 2025-12-22 23:49:08 +0100 |
| commit | 46fadf4bf14360be046b9b770ddf205fad96a0a7 (patch) | |
| tree | 6e88bf6050e4f0cab877760e80f64bd708dd7400 /lib/libc | |
| parent | 0f30d227497418c6d3bef7d52244407e30454504 (diff) | |
Add sys/resource.h implementation and nice(3)
Diffstat (limited to 'lib/libc')
| -rw-r--r-- | lib/libc/sys/resource/getpriority.c | 12 | ||||
| -rw-r--r-- | lib/libc/sys/resource/getrlimit.c | 7 | ||||
| -rw-r--r-- | lib/libc/sys/resource/getrusage.c | 7 | ||||
| -rw-r--r-- | lib/libc/sys/resource/setpriority.c | 7 | ||||
| -rw-r--r-- | lib/libc/sys/resource/setrlimit.c | 7 | ||||
| -rw-r--r-- | lib/libc/unistd/nice.c | 28 |
6 files changed, 64 insertions, 4 deletions
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 <sys/resource.h> +#include <syscall.h> + +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 <sys/resource.h> +#include <syscall.h> + +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 <sys/resource.h> +#include <syscall.h> + +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 <sys/resource.h> +#include <syscall.h> + +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 <sys/resource.h> +#include <syscall.h> + +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 <unistd.h> // for nice +#include <errno.h> +#include <limits.h> +#include <sys/resource.h> +#include <unistd.h> 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; } |
