xref: /linux/tools/testing/selftests/namespaces/listns_test.c (revision e2ff8d88649c2e4303f2dbb80659ae0312c30846)
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <limits.h>
6 #include <sched.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <linux/nsfs.h>
11 #include <sys/ioctl.h>
12 #include <sys/stat.h>
13 #include <sys/syscall.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <unistd.h>
17 #include "../kselftest_harness.h"
18 #include "../filesystems/utils.h"
19 #include "wrappers.h"
20 
21 /*
22  * Test basic listns() functionality with the unified namespace tree.
23  * List all active namespaces globally.
24  */
25 TEST(listns_basic_unified)
26 {
27 	struct ns_id_req req = {
28 		.size = sizeof(req),
29 		.spare = 0,
30 		.ns_id = 0,
31 		.ns_type = 0,  /* All types */
32 		.spare2 = 0,
33 		.user_ns_id = 0,  /* Global listing */
34 	};
35 	__u64 ns_ids[100];
36 	ssize_t ret;
37 
38 	ret = sys_listns(&req, ns_ids, ARRAY_SIZE(ns_ids), 0);
39 	if (ret < 0) {
40 		if (errno == ENOSYS)
41 			SKIP(return, "listns() not supported");
42 		TH_LOG("listns failed: %s (errno=%d)", strerror(errno), errno);
43 		ASSERT_TRUE(false);
44 	}
45 
46 	/* Should find at least the initial namespaces */
47 	ASSERT_GT(ret, 0);
48 	TH_LOG("Found %zd active namespaces", ret);
49 
50 	/* Verify all returned IDs are non-zero */
51 	for (ssize_t i = 0; i < ret; i++) {
52 		ASSERT_NE(ns_ids[i], 0);
53 		TH_LOG("  [%zd] ns_id: %llu", i, (unsigned long long)ns_ids[i]);
54 	}
55 }
56 
57 TEST_HARNESS_MAIN
58