Skip to content

Commit de44d81

Browse files
committed
setrlimit06: cover the setting resource limit64 for process
The new test ensures that the process gets the correct signals in the correct order: 1. It should get SIGXCPU after reaching the soft CPU time limit64. 2. If the CPU time exceeds the hard limit, it should receive SIGKILL Signed-off-by: chunfuwen <chwen@redhat.com> Signed-off-by: Li Wang <liwang@redhat.com> Reviewed-by: Li Wang <liwang@redhat.com>
1 parent a3942c0 commit de44d81

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

include/lapi/resource.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2025 Red Hat Inc. All Rights Reserved.
4+
* Author: Chunfu Wen <chwen@redhat.com>
5+
*/
6+
7+
#ifndef LAPI_RESOURCE_H__
8+
#define LAPI_RESOURCE_H__
9+
10+
#define _GNU_SOURCE
11+
12+
#include <sys/resource.h>
13+
#include "config.h"
14+
#include "lapi/syscalls.h"
15+
16+
#ifndef HAVE_STRUCT_RLIMIT64
17+
struct rlimit64 {
18+
uint64_t rlim_cur;
19+
uint64_t rlim_max;
20+
};
21+
#endif
22+
23+
static int setrlimit_u64(int resource, struct rlimit64 *rlim)
24+
{
25+
return tst_syscall(__NR_prlimit64, 0, resource, rlim, NULL);
26+
}
27+
28+
#endif /* LAPI_RESOURCE_H__ */

testcases/kernel/syscalls/setrlimit/setrlimit06.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
/*\
99
* Set CPU time limit for a process and check its behavior
10-
* after reaching CPU time limit.
10+
* after reaching CPU time limit
1111
*
12-
* 1) Process got SIGXCPU after reaching soft limit of CPU time.
13-
* 2) Process got SIGKILL after reaching hard limit of CPU time.
12+
* - Process got SIGXCPU after reaching soft limit of CPU time
13+
* - Process got SIGKILL after reaching hard limit of CPU time
1414
*
1515
* Test is also a regression test for kernel bug:
1616
* c3bca5d450b62 ("posix-cpu-timers: Ensure set_process_cpu_timer is always evaluated")
@@ -27,6 +27,12 @@
2727
#include <sys/mman.h>
2828

2929
#include "tst_test.h"
30+
#include "lapi/resource.h"
31+
32+
#define TEST_VARIANTS 2
33+
34+
static struct rlimit *rlim;
35+
static struct rlimit64 *rlim_64;
3036

3137
static int *end;
3238

@@ -37,6 +43,11 @@ static void sighandler(int sig)
3743

3844
static void setup(void)
3945
{
46+
rlim->rlim_cur = 1;
47+
rlim->rlim_max = 2;
48+
rlim_64->rlim_cur = 1;
49+
rlim_64->rlim_max = 2;
50+
4051
SAFE_SIGNAL(SIGXCPU, sighandler);
4152

4253
end = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE,
@@ -58,12 +69,14 @@ static void verify_setrlimit(void)
5869

5970
pid = SAFE_FORK();
6071
if (!pid) {
61-
struct rlimit rlim = {
62-
.rlim_cur = 1,
63-
.rlim_max = 2,
64-
};
65-
66-
TEST(setrlimit(RLIMIT_CPU, &rlim));
72+
switch (tst_variant) {
73+
case 0:
74+
TEST(setrlimit(RLIMIT_CPU, rlim));
75+
break;
76+
case 1:
77+
TEST(setrlimit_u64(RLIMIT_CPU, rlim_64));
78+
break;
79+
}
6780
if (TST_RET == -1) {
6881
tst_res(TFAIL | TTERRNO,
6982
"setrlimit(RLIMIT_CPU) failed");
@@ -72,7 +85,8 @@ static void verify_setrlimit(void)
7285

7386
alarm(20);
7487

75-
while (1);
88+
while (1)
89+
;
7690
}
7791

7892
SAFE_WAITPID(pid, &status, 0);
@@ -112,6 +126,12 @@ static void verify_setrlimit(void)
112126
static struct tst_test test = {
113127
.test_all = verify_setrlimit,
114128
.setup = setup,
129+
.test_variants = TEST_VARIANTS,
130+
.bufs = (struct tst_buffers []) {
131+
{&rlim, .size = sizeof(*rlim)},
132+
{&rlim_64, .size = sizeof(*rlim_64)},
133+
{}
134+
},
115135
.cleanup = cleanup,
116136
.forks_child = 1,
117137
.tags = (const struct tst_tag[]) {

0 commit comments

Comments
 (0)