[LTP] [PATCH v1 04/10] Refactor userns04 test

Andrea Cervesato andrea.cervesato@suse.com
Wed Feb 15 11:16:09 CET 2023


Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/userns04.c | 45 +++++++------------
 1 file changed, 17 insertions(+), 28 deletions(-)

diff --git a/testcases/kernel/containers/userns/userns04.c b/testcases/kernel/containers/userns/userns04.c
index d8639502e..58ae7c302 100644
--- a/testcases/kernel/containers/userns/userns04.c
+++ b/testcases/kernel/containers/userns/userns04.c
@@ -15,56 +15,46 @@
 #define _GNU_SOURCE
 
 #include <stdio.h>
-#include "common.h"
 #include "tst_test.h"
-#include "lapi/syscalls.h"
+#include "lapi/sched.h"
 
-static void setup(void)
-{
-	check_newuser();
-	tst_syscall(__NR_setns, -1, 0);
-}
-
-static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
+static void child_fn1(void)
 {
 	TST_CHECKPOINT_WAIT(0);
-	return 0;
 }
 
-static int child_fn2(void *arg)
+static void child_fn2(int fd)
 {
-	TEST(tst_syscall(__NR_setns, ((long)arg), CLONE_NEWUSER));
-	if (TST_RET != -1 || TST_ERR != EPERM)
-		tst_res(TFAIL | TERRNO, "child2 setns() error");
-	else
-		tst_res(TPASS, "child2 setns() failed as expected");
-
+	TST_EXP_FAIL(setns(fd, CLONE_NEWUSER), EPERM);
 	TST_CHECKPOINT_WAIT(1);
-
-	return 0;
 }
 
 static void run(void)
 {
+	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
 	pid_t cpid1, cpid2, cpid3;
 	char path[BUFSIZ];
 	int fd;
 
-	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, NULL);
-	if (cpid1 < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
+	cpid1 = SAFE_CLONE(&args);
+	if (!cpid1) {
+		child_fn1();
+		return;
+	}
 
 	sprintf(path, "/proc/%d/ns/user", cpid1);
 
 	fd = SAFE_OPEN(path, O_RDONLY, 0644);
-	cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn2, (void *)((long)fd));
-	if (cpid2 < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
 
-	/* child 3 - throw-away process changing ns to child1 */
+	cpid2 = SAFE_CLONE(&args);
+	if (!cpid2) {
+		child_fn2(fd);
+		return;
+	}
+
 	cpid3 = SAFE_FORK();
 	if (!cpid3) {
-		TST_EXP_PASS(tst_syscall(__NR_setns, fd, CLONE_NEWUSER));
+		TST_EXP_PASS(setns(fd, CLONE_NEWUSER));
 		return;
 	}
 
@@ -75,7 +65,6 @@ static void run(void)
 }
 
 static struct tst_test test = {
-	.setup = setup,
 	.test_all = run,
 	.needs_root = 1,
 	.forks_child = 1,
-- 
2.35.3



More information about the ltp mailing list