public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v2] Refactor pidns32 test using new LTP API
@ 2023-02-13  9:47 Andrea Cervesato via ltp
  2023-02-14 11:50 ` Richard Palethorpe
  0 siblings, 1 reply; 2+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-13  9:47 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
Using SAFE_CLONE
Using TST_EXP_* macros

 testcases/kernel/containers/pidns/pidns32.c | 41 +++++++++++----------
 1 file changed, 21 insertions(+), 20 deletions(-)

diff --git a/testcases/kernel/containers/pidns/pidns32.c b/testcases/kernel/containers/pidns/pidns32.c
index 3f7df788e..c9f8db3e5 100644
--- a/testcases/kernel/containers/pidns/pidns32.c
+++ b/testcases/kernel/containers/pidns/pidns32.c
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Huawei Technologies Co., Ltd., 2015
- * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
  */
 
 /*\
@@ -12,32 +12,32 @@
  */
 
 #define _GNU_SOURCE
-
 #include <sys/mman.h>
 #include "tst_test.h"
 #include "lapi/sched.h"
 
 #define MAXNEST 32
 
+static const struct tst_clone_args args = { CLONE_NEWPID, SIGCHLD };
 static int *level;
 
-static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
+static void child_func(void)
 {
 	pid_t cpid;
 	int status;
 
 	if (*level == MAXNEST)
-		return 0;
+		return;
 
 	(*level)++;
 
-	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
-	if (cpid < 0)
-		tst_brk(TBROK | TERRNO, "clone failed");
+	cpid = SAFE_CLONE(&args);
+	if (!cpid) {
+		child_func();
+		return;
+	}
 
 	SAFE_WAITPID(cpid, &status, 0);
-
-	return 0;
 }
 
 static void setup(void)
@@ -45,28 +45,29 @@ static void setup(void)
 	level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
 }
 
-static void run(void)
+static void cleanup(void)
 {
-	int ret, status;
+	SAFE_MUNMAP(level, sizeof(int));
+}
 
+static void run(void)
+{
 	*level = 1;
 
-	ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
-	if (ret < 0)
-		tst_brk(TBROK | TERRNO, "clone failed");
-
-	SAFE_WAITPID(ret, &status, 0);
-
-	if (*level < MAXNEST) {
-		tst_res(TFAIL, "Nested containers should be %d, but they are %d", MAXNEST, *level);
+	if (!SAFE_CLONE(&args)) {
+		child_func();
 		return;
 	}
 
-	tst_res(TPASS, "All %d containers have been nested", MAXNEST);
+	tst_reap_children();
+
+	TST_EXP_EQ_LI(*level, MAXNEST);
 }
 
 static struct tst_test test = {
 	.test_all = run,
 	.needs_root = 1,
 	.setup = setup,
+	.cleanup = cleanup,
+	.forks_child = 1,
 };
-- 
2.35.3


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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-02-14 11:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-13  9:47 [LTP] [PATCH v2] Refactor pidns32 test using new LTP API Andrea Cervesato via ltp
2023-02-14 11:50 ` Richard Palethorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox