All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v1] Refactor pidns32 test using new LTP API
@ 2022-08-10  9:48 Andrea Cervesato via ltp
  2022-08-10 14:06 ` Cyril Hrubis
  0 siblings, 1 reply; 4+ messages in thread
From: Andrea Cervesato via ltp @ 2022-08-10  9:48 UTC (permalink / raw)
  To: ltp

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


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

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

* Re: [LTP] [PATCH v1] Refactor pidns32 test using new LTP API
  2022-08-10  9:48 Andrea Cervesato via ltp
@ 2022-08-10 14:06 ` Cyril Hrubis
  0 siblings, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2022-08-10 14:06 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hi!
Looks like this patch should be applied over changes that were not send
to the list.

-- 
Cyril Hrubis
chrubis@suse.cz

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

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

* [LTP] [PATCH v1] Refactor pidns32 test using new LTP API
@ 2022-08-11 11:36 Andrea Cervesato via ltp
  2022-08-15 14:49 ` Cyril Hrubis
  0 siblings, 1 reply; 4+ messages in thread
From: Andrea Cervesato via ltp @ 2022-08-11 11:36 UTC (permalink / raw)
  To: ltp

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

diff --git a/testcases/kernel/containers/pidns/pidns32.c b/testcases/kernel/containers/pidns/pidns32.c
index 316c5d195..cbc91df68 100644
--- a/testcases/kernel/containers/pidns/pidns32.c
+++ b/testcases/kernel/containers/pidns/pidns32.c
@@ -1,101 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Huawei Technologies Co., Ltd., 2015
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
  */
 
-/*
- * Verify that:
- * The kernel imposes a limit of 32 nested levels of pid namespaces.
+/*\
+ * [Description]
+ *
+ * Clone a process with CLONE_NEWPID flag and check for the maxium amount of
+ * nested containers.
  */
 
 #define _GNU_SOURCE
-#include <sys/wait.h>
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include "pidns_helper.h"
-#include "test.h"
 
-#define MAXNEST 32
-
-char *TCID = "pidns32";
-int TST_TOTAL = 1;
+#include <sys/mman.h>
+#include "tst_test.h"
+#include "lapi/namespaces_constants.h"
 
-static void setup(void)
-{
-	tst_require_root();
-	check_newpid();
-	tst_tmpdir();
-}
+#define MAXNEST 32
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static int *level;
 
-static int child_fn1(void *arg)
+static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
 {
-	pid_t cpid1;
-	long level = (long)arg;
+	pid_t cpid;
 	int status;
 
-	if (level == MAXNEST)
+	if (*level == MAXNEST)
 		return 0;
-	cpid1 = ltp_clone_quick(CLONE_NEWPID | SIGCHLD,
-		(void *)child_fn1, (void *)(level + 1));
-	if (cpid1 < 0) {
-		printf("level %ld:unexpected error: (%d) %s\n",
-			level, errno, strerror(errno));
-		return 1;
-	}
-	if (waitpid(cpid1, &status, 0) == -1)
-		return 1;
-
-	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
-		printf("child exited abnormally\n");
-		return 1;
-	} else if (WIFSIGNALED(status)) {
-		printf("child was killed with signal = %d", WTERMSIG(status));
-		return 1;
-	}
+
+	(*level)++;
+
+	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
+	if (cpid < 0)
+		tst_brk(TBROK | TERRNO, "clone failed");
+
+	SAFE_WAITPID(cpid, &status, 0);
+
 	return 0;
 }
 
-static void test_max_nest(void)
+static void setup(void)
 {
-	pid_t cpid1;
-
-	cpid1 = ltp_clone_quick(CLONE_NEWPID | SIGCHLD,
-		(void *)child_fn1, (void *)1);
-	if (cpid1 < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
-
-	tst_record_childstatus(cleanup, cpid1);
+	level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	*level = 1;
 }
 
-int main(int argc, char *argv[])
+static void run(void)
 {
-	int lc;
+	int ret, status;
 
-	setup();
-	tst_parse_opts(argc, argv, NULL, NULL);
+	ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
+	if (ret < 0)
+		tst_brk(TBROK | TERRNO, "clone failed");
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		test_max_nest();
+	SAFE_WAITPID(ret, &status, 0);
+
+	if (*level < MAXNEST) {
+		tst_res(TFAIL, "Nested containers should be %d, but they are %d", MAXNEST, *level);
+		return;
 	}
 
-	cleanup();
-	tst_exit();
+	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


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

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

* Re: [LTP] [PATCH v1] Refactor pidns32 test using new LTP API
  2022-08-11 11:36 [LTP] [PATCH v1] Refactor pidns32 test using new LTP API Andrea Cervesato via ltp
@ 2022-08-15 14:49 ` Cyril Hrubis
  0 siblings, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2022-08-15 14:49 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hi!
Pushed with one fix, thanks.

I moved the *nested = 1 from setup to the run() function, otherwise the
test would be no-op on second and subsequent iterations with -i option.

Diff:

diff --git a/testcases/kernel/containers/pidns/pidns32.c b/testcases/kernel/cont
index cbc91df68..333e1fcad 100644
--- a/testcases/kernel/containers/pidns/pidns32.c
+++ b/testcases/kernel/containers/pidns/pidns32.c
@@ -43,13 +43,14 @@ static int child_func(LTP_ATTRIBUTE_UNUSED void *arg)
 static void setup(void)
 {
        level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED
-       *level = 1;
 }

 static void run(void)
 {
        int ret, status;

+       *level = 1;
+
        ret = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_func, 0);
        if (ret < 0)
                tst_brk(TBROK | TERRNO, "clone failed");


-- 
Cyril Hrubis
chrubis@suse.cz

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

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

end of thread, other threads:[~2022-08-15 14:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-11 11:36 [LTP] [PATCH v1] Refactor pidns32 test using new LTP API Andrea Cervesato via ltp
2022-08-15 14:49 ` Cyril Hrubis
  -- strict thread matches above, loose matches on Subject: below --
2022-08-10  9:48 Andrea Cervesato via ltp
2022-08-10 14:06 ` Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.