Skip to contentSkip to conversation

Refactor pidns32 test using new LTP API

PATCH Superseded Andrea Cervesato via ltp 2 participants 2 messages Feb 13, 2023 – Feb 14, 2023

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Refactor pidns32 test using new LTP API

---
Using SAFE_CLONE
Using TST_EXP_* macros

 testcases/kernel/containers/pidns/pidns32.c | 41 +++++++++++----------
 1 file changed, 21 insertions(+), 20 deletions(-)
testcases/kernel/containers/pidns/pidns32.c +21 -20
index 3f7df788e..c9f8db3e5 100644
@@ -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 <[email protected]>
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
  */
 
 /*\
@@ -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,
 };
Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hello,

Merged with a few changes!

Again we are using waitpid on its own. It will wait the children and we'll only check the exit status of one of them. So I replaced it with tst_reap_children.

Also I used tst_atomic to read and write from level. Not sure if that is necessary, but it looks cleaner.

Quoted 105 lines · Andrea Cervesato via ltp

---
Using SAFE_CLONE
Using TST_EXP_* macros

 testcases/kernel/containers/pidns/pidns32.c | 41 +++++++++++----------
 1 file changed, 21 insertions(+), 20 deletions(-)
testcases/kernel/containers/pidns/pidns32.c +21 -20
index 3f7df788e..c9f8db3e5 100644
@@ -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 <[email protected]>
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
  */
 
 /*\
@@ -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,
 };
Signature · 1 line
2.35.3
Signature · 5 lines
Thank you,
Richard.

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