Skip to content

Commit a0bb5f8

Browse files
committed
tools/nolibc: add support for getrlimit/setrlimit
The implementation uses the prlimit64 systemcall as that is available on all architectures. Signed-off-by: Thomas Weißschuh <linux@weissschuh.net> Link: https://lore.kernel.org/lkml/20231123-nolibc-rlimit-v1-2-a428b131de2a@weissschuh.net/ Acked-by: Willy Tarreau <w@1wt.eu>
1 parent 7b20478 commit a0bb5f8

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

tools/include/nolibc/sys.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/fcntl.h> /* for O_* and AT_* */
2222
#include <linux/stat.h> /* for statx() */
2323
#include <linux/prctl.h>
24+
#include <linux/resource.h>
2425

2526
#include "arch.h"
2627
#include "errno.h"
@@ -898,6 +899,43 @@ int reboot(int cmd)
898899
}
899900

900901

902+
/*
903+
* int getrlimit(int resource, struct rlimit *rlim);
904+
* int setrlimit(int resource, const struct rlimit *rlim);
905+
*/
906+
907+
static __attribute__((unused))
908+
int sys_prlimit64(pid_t pid, int resource,
909+
const struct rlimit64 *new_limit, struct rlimit64 *old_limit)
910+
{
911+
return my_syscall4(__NR_prlimit64, pid, resource, new_limit, old_limit);
912+
}
913+
914+
static __attribute__((unused))
915+
int getrlimit(int resource, struct rlimit *rlim)
916+
{
917+
struct rlimit64 rlim64;
918+
int ret;
919+
920+
ret = __sysret(sys_prlimit64(0, resource, NULL, &rlim64));
921+
rlim->rlim_cur = rlim64.rlim_cur;
922+
rlim->rlim_max = rlim64.rlim_max;
923+
924+
return ret;
925+
}
926+
927+
static __attribute__((unused))
928+
int setrlimit(int resource, const struct rlimit *rlim)
929+
{
930+
struct rlimit64 rlim64 = {
931+
.rlim_cur = rlim->rlim_cur,
932+
.rlim_max = rlim->rlim_max,
933+
};
934+
935+
return __sysret(sys_prlimit64(0, resource, &rlim64, NULL));
936+
}
937+
938+
901939
/*
902940
* int sched_yield(void);
903941
*/

tools/testing/selftests/nolibc/nolibc-test.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sys/mount.h>
2323
#include <sys/prctl.h>
2424
#include <sys/reboot.h>
25+
#include <sys/resource.h>
2526
#include <sys/stat.h>
2627
#include <sys/syscall.h>
2728
#include <sys/sysmacros.h>
@@ -839,6 +840,33 @@ int test_pipe(void)
839840
return !!memcmp(buf, msg, len);
840841
}
841842

843+
int test_rlimit(void)
844+
{
845+
struct rlimit rlim = {
846+
.rlim_cur = 1 << 20,
847+
.rlim_max = 1 << 21,
848+
};
849+
int ret;
850+
851+
ret = setrlimit(RLIMIT_CORE, &rlim);
852+
if (ret)
853+
return -1;
854+
855+
rlim.rlim_cur = 0;
856+
rlim.rlim_max = 0;
857+
858+
ret = getrlimit(RLIMIT_CORE, &rlim);
859+
if (ret)
860+
return -1;
861+
862+
if (rlim.rlim_cur != 1 << 20)
863+
return -1;
864+
if (rlim.rlim_max != 1 << 21)
865+
return -1;
866+
867+
return 0;
868+
}
869+
842870

843871
/* Run syscall tests between IDs <min> and <max>.
844872
* Return 0 on success, non-zero on failure.
@@ -928,6 +956,7 @@ int run_syscall(int min, int max)
928956
CASE_TEST(poll_fault); EXPECT_SYSER(1, poll(NULL, 1, 0), -1, EFAULT); break;
929957
CASE_TEST(prctl); EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break;
930958
CASE_TEST(read_badf); EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break;
959+
CASE_TEST(rlimit); EXPECT_SYSZR(1, test_rlimit()); break;
931960
CASE_TEST(rmdir_blah); EXPECT_SYSER(1, rmdir("/blah"), -1, ENOENT); break;
932961
CASE_TEST(sched_yield); EXPECT_SYSZR(1, sched_yield()); break;
933962
CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;

0 commit comments

Comments
 (0)