Linux Test Project
 help / color / mirror / Atom feed
From: chunfuwen <chwen@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2] Add test case to cover the setting resource limit64 for process
Date: Wed, 19 Feb 2025 01:47:43 -0500	[thread overview]
Message-ID: <20250219064804.1313358-1-chwen@redhat.com> (raw)
In-Reply-To: <bc3276b8-8383-4ef8-a4d7-a2a786662cbd@suse.com>

The test ensures that the process gets the correct signals in the correct order:

First, it should get SIGXCPU after reaching the soft CPU time limit64.
Then, if the CPU time exceeds the hard limit, it should receive SIGKILL

Signed-off-by: chunfuwen <chwen@redhat.com>
---
Changes in v2:
- Romove test descriptions and trailing line as suggested by Ricardo B. Marlière
- Added 2025 copyright as suggested by Ricardo B. Marlière
- Trim down include files as suggested by Ricardo B. Marlière
- Create new lapi/resource.h residing struct rlimit64 as suggested by Andrea
- Move setrlimit_u64() syscalls definitions into lapi/resource.h as suggested by Andrea
- Skip  SAFE_* variants as suggested by Andrea
- use tst_buffers when passing the pointeras suggested by Andrea
- Link to v1:https://lore.kernel.org/all/20250218023107.1208990-1-chwen@redhat.com/
---
 include/lapi/resource.h                       |  26 ++++
 .../kernel/syscalls/setrlimit/setrlimit07.c   | 127 ++++++++++++++++++
 2 files changed, 153 insertions(+)
 create mode 100644 include/lapi/resource.h
 create mode 100644 testcases/kernel/syscalls/setrlimit/setrlimit07.c

diff --git a/include/lapi/resource.h b/include/lapi/resource.h
new file mode 100644
index 000000000..3310bc934
--- /dev/null
+++ b/include/lapi/resource.h
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 Red Hat Inc. All Rights Reserved.
+ * Author: Chunfu Wen <chwen@redhat.com>
+ */
+
+#ifndef LAPI_RESOURCE_H__
+#define LAPI_RESOURCE_H__
+
+#define _GNU_SOURCE
+
+#include "lapi/syscalls.h"
+
+#ifndef HAVE_STRUCT_RLIMIT64
+struct rlimit64 {
+        uint64_t rlim_cur;
+        uint64_t rlim_max;
+};
+#endif
+
+static int setrlimit_u64(int resource, const struct rlimit64 *rlim)
+{
+        return tst_syscall(__NR_prlimit64, 0, resource, rlim, NULL);
+}
+
+#endif /* LAPI_RESOURCE_H__ */
diff --git a/testcases/kernel/syscalls/setrlimit/setrlimit07.c b/testcases/kernel/syscalls/setrlimit/setrlimit07.c
new file mode 100644
index 000000000..60a4580da
--- /dev/null
+++ b/testcases/kernel/syscalls/setrlimit/setrlimit07.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2025 Red Hat Inc. All Rights Reserved.
+ * Author: Chunfu Wen <chwen@redhat.com>
+ */
+
+/*
+ * Set CPU time limit64 for a process and check its behavior
+ * after reaching CPU time limit64.
+ * 1) Process got SIGXCPU after reaching soft limit of CPU time limit64.
+ * 2) Process got SIGKILL after reaching hard limit of CPU time limit64.
+ */
+
+#define _GNU_SOURCE
+
+#include <sys/wait.h>
+
+#include "tst_test.h"
+
+#include "lapi/syscalls.h"
+
+#include "lapi/resource.h"
+
+#ifndef HAVE_STRUCT_RLIMIT64
+struct rlimit64 {
+	uint64_t rlim_cur;
+	uint64_t rlim_max;
+};
+#endif
+
+static struct  rlimit64*rlim;
+
+static int *end;
+
+static void sighandler(int sig)
+{
+	*end = sig;
+}
+
+static void setup(void)
+{
+	signal(SIGXCPU, sighandler);
+
+	end = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
+		   MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+}
+
+static void cleanup(void)
+{
+	if (end)
+		munmap(end, sizeof(int));
+}
+/*
+static int setrlimit_u64(int resource, const struct rlimit64 *rlim)
+{
+	return tst_syscall(__NR_prlimit64, 0, resource, rlim, NULL);
+}*/
+
+static void verify_setrlimit64(void)
+{
+	int status;
+	pid_t pid;
+
+	rlim->rlim_cur = 1;
+	rlim->rlim_max = 2;
+
+	*end = 0;
+
+	pid = fork();
+	if (!pid) {
+		TEST(setrlimit_u64(RLIMIT_CPU, rlim));
+		if (TST_RET == -1) {
+			tst_res(TFAIL | TTERRNO,
+				"setrlimit_u64(RLIMIT_CPU) failed");
+			exit(1);
+		}
+
+		alarm(20);
+
+		while (1)
+			;
+	}
+
+	waitpid(pid, &status, 0);
+
+	if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
+		return;
+
+	if (WIFSIGNALED(status)) {
+		if (WTERMSIG(status) == SIGKILL && *end == SIGXCPU) {
+			tst_res(TPASS,
+				"Got SIGXCPU then SIGKILL after reaching both limit");
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGKILL && !*end) {
+			tst_res(TFAIL,
+				"Got only SIGKILL after reaching both limit");
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGALRM && *end == SIGXCPU) {
+			tst_res(TFAIL,
+				"Got only SIGXCPU after reaching both limit");
+			return;
+		}
+
+		if (WTERMSIG(status) == SIGALRM && !*end) {
+			tst_res(TFAIL,
+				"Got no signal after reaching both limit");
+			return;
+		}
+	}
+
+	tst_res(TFAIL, "Child %s", tst_strstatus(status));
+}
+
+static struct tst_test test = {
+	.test_all = verify_setrlimit64,
+        .bufs = (struct tst_buffers []) {
+                {&rlim, .size = sizeof(*rlim)},
+                {},
+	},
+	.setup = setup,
+	.cleanup = cleanup,
+	.forks_child = 1,
+};
-- 
2.43.5


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2025-02-19  6:48 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-18  2:31 [LTP] [PATCH] Add test case to cover the setting resource limit64 for process chunfuwen
2025-02-18 11:44 ` Ricardo B. Marlière
2025-02-18 13:29   ` Andrea Cervesato via ltp
2025-02-19  2:35     ` Chunfu Wen
2025-02-19  9:29       ` Andrea Cervesato via ltp
2025-02-19  6:47     ` chunfuwen [this message]
2025-02-19 16:44       ` [LTP] [PATCH v2] " Petr Vorel
2025-02-20  2:08         ` Li Wang
2025-02-20  9:35           ` Petr Vorel
2025-02-20  8:01         ` Chunfu Wen
2025-02-20  8:35         ` [LTP] [PATCH v3] " Chunfu Wen
2025-02-21 10:34           ` Andrea Cervesato via ltp
2025-02-24  3:06             ` Chunfu Wen
2025-02-24  3:07             ` [LTP] [PATCH v4] " Chunfu Wen
2025-02-26  1:15               ` Chunfu Wen
2025-02-28  3:41                 ` Li Wang
2025-02-28  7:45                   ` Andrea Cervesato via ltp
2025-02-28  8:39                     ` Li Wang
2025-02-28  9:22                       ` Li Wang
2025-02-28 11:37                         ` Petr Vorel
2025-02-26  9:21               ` Li Wang
2025-02-19 16:50       ` [LTP] [PATCH v2] " Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250219064804.1313358-1-chwen@redhat.com \
    --to=chwen@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox