]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/namespaces: first listns() test
authorChristian Brauner <brauner@kernel.org>
Wed, 29 Oct 2025 12:20:52 +0000 (13:20 +0100)
committerChristian Brauner <brauner@kernel.org>
Mon, 3 Nov 2025 16:41:20 +0000 (17:41 +0100)
Test basic listns() functionality with the unified namespace tree.
List all active namespaces globally.

Link: https://patch.msgid.link/20251029-work-namespace-nstree-listns-v4-39-2e6f823ebdc0@kernel.org
Tested-by: syzbot@syzkaller.appspotmail.com
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
tools/testing/selftests/namespaces/.gitignore
tools/testing/selftests/namespaces/Makefile
tools/testing/selftests/namespaces/listns_test.c [new file with mode: 0644]

index 100cc5bfef0491ea450457bacd44915706ada520..5065f07e92c97080c3400d24dee094d3a0d865a3 100644 (file)
@@ -2,3 +2,4 @@ nsid_test
 file_handle_test
 init_ino_test
 ns_active_ref_test
+listns_test
index 5cea938cdde8b90017fe1a673c5616f4f433182e..de708f4df1596105fd6a3dae3f459e8f0049ab5e 100644 (file)
@@ -2,9 +2,10 @@
 CFLAGS += -Wall -O0 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
 LDLIBS += -lcap
 
-TEST_GEN_PROGS := nsid_test file_handle_test init_ino_test ns_active_ref_test
+TEST_GEN_PROGS := nsid_test file_handle_test init_ino_test ns_active_ref_test listns_test
 
 include ../lib.mk
 
 $(OUTPUT)/ns_active_ref_test: ../filesystems/utils.c
+$(OUTPUT)/listns_test: ../filesystems/utils.c
 
diff --git a/tools/testing/selftests/namespaces/listns_test.c b/tools/testing/selftests/namespaces/listns_test.c
new file mode 100644 (file)
index 0000000..cb42827
--- /dev/null
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <linux/nsfs.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include "../kselftest_harness.h"
+#include "../filesystems/utils.h"
+#include "wrappers.h"
+
+/*
+ * Test basic listns() functionality with the unified namespace tree.
+ * List all active namespaces globally.
+ */
+TEST(listns_basic_unified)
+{
+       struct ns_id_req req = {
+               .size = sizeof(req),
+               .spare = 0,
+               .ns_id = 0,
+               .ns_type = 0,  /* All types */
+               .spare2 = 0,
+               .user_ns_id = 0,  /* Global listing */
+       };
+       __u64 ns_ids[100];
+       ssize_t ret;
+
+       ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
+       if (ret < 0) {
+               if (errno == ENOSYS)
+                       SKIP(return, "listns() not supported");
+               TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
+               ASSERT_TRUE(false);
+       }
+
+       /* Should find at least the initial namespaces */
+       ASSERT_GT(ret, 0);
+       TH_LOG("Found %zd active namespaces", ret);
+
+       /* Verify all returned IDs are non-zero */
+       for (ssize_t i = 0; i < ret; i++) {
+               ASSERT_NE(ns_ids[i], 0);
+               TH_LOG("  [%zd] ns_id: %llu", i, (unsigned long long)ns_ids[i]);
+       }
+}
+
+TEST_HARNESS_MAIN