[LTP] [PATCH v1] Refactor pidns32 test using new LTP API

Andrea Cervesato andrea.cervesato@suse.com
Wed Aug 10 11:48:24 CEST 2022


Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/pidns/pidns32.c | 36 ++++++++++++---------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/testcases/kernel/containers/pidns/pidns32.c b/testcases/kernel/containers/pidns/pidns32.c
index 215ca44a7..cbc91df68 100644
--- a/testcases/kernel/containers/pidns/pidns32.c
+++ b/testcases/kernel/containers/pidns/pidns32.c
@@ -7,29 +7,31 @@
 /*\
  * [Description]
  *
- * Clone a process with CLONE_NEWPID flag and reach the maximum amount of
- * nested containers checking for errors.
+ * Clone a process with CLONE_NEWPID flag and check for the maxium amount of
+ * nested containers.
  */
 
+#define _GNU_SOURCE
+
+#include <sys/mman.h>
 #include "tst_test.h"
 #include "lapi/namespaces_constants.h"
 
 #define MAXNEST 32
 
-static int child_func(void *arg)
+static int *level;
+
+static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
 {
 	pid_t cpid;
-	int *level = (int *)arg;
 	int status;
 
-	tst_res(TINFO, "%p=%d", level, *level);
-
 	if (*level == MAXNEST)
 		return 0;
 
 	(*level)++;
 
-	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, level);
+	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
 	if (cpid < 0)
 		tst_brk(TBROK | TERRNO, "clone failed");
 
@@ -38,30 +40,32 @@ static int child_func(void *arg)
 	return 0;
 }
 
+static void setup(void)
+{
+	level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	*level = 1;
+}
+
 static void run(void)
 {
 	int ret, status;
-	int level = 1;
 
-	ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, &level);
+	ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
 	if (ret < 0)
 		tst_brk(TBROK | TERRNO, "clone failed");
 
-	tst_res(TINFO, "%p=%d", &level, level);
-
 	SAFE_WAITPID(ret, &status, 0);
 
-	tst_res(TINFO, "%p=%d", &level, level);
-
-	if (level < MAXNEST) {
-		tst_res(TFAIL, "Not enough nested containers: %d", level);
+	if (*level < MAXNEST) {
+		tst_res(TFAIL, "Nested containers should be %d, but they are %d", MAXNEST, *level);
 		return;
 	}
 
-	tst_res(TPASS, "All containers have been nested");
+	tst_res(TPASS, "All %d containers have been nested", MAXNEST);
 }
 
 static struct tst_test test = {
 	.test_all = run,
 	.needs_root = 1,
+	.setup = setup,
 };
-- 
2.35.3



More information about the ltp mailing list