1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // Copyright (c) 2025 Christian Brauner <brauner@kernel.org>
3
4 #define _GNU_SOURCE
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <linux/nsfs.h>
13
14 #include "../kselftest_harness.h"
15
16 struct ns_info {
17 const char *name;
18 const char *proc_path;
19 unsigned int expected_ino;
20 };
21
22 static struct ns_info namespaces[] = {
23 { "ipc", "/proc/1/ns/ipc", IPC_NS_INIT_INO },
24 { "uts", "/proc/1/ns/uts", UTS_NS_INIT_INO },
25 { "user", "/proc/1/ns/user", USER_NS_INIT_INO },
26 { "pid", "/proc/1/ns/pid", PID_NS_INIT_INO },
27 { "cgroup", "/proc/1/ns/cgroup", CGROUP_NS_INIT_INO },
28 { "time", "/proc/1/ns/time", TIME_NS_INIT_INO },
29 { "net", "/proc/1/ns/net", NET_NS_INIT_INO },
30 { "mnt", "/proc/1/ns/mnt", MNT_NS_INIT_INO },
31 };
32
TEST(init_namespace_inodes)33 TEST(init_namespace_inodes)
34 {
35 struct stat st;
36
37 for (int i = 0; i < sizeof(namespaces) / sizeof(namespaces[0]); i++) {
38 int ret = stat(namespaces[i].proc_path, &st);
39
40 /* Some namespaces might not be available (e.g., time namespace on older kernels) */
41 if (ret < 0) {
42 if (errno == ENOENT) {
43 ksft_test_result_skip("%s namespace not available\n",
44 namespaces[i].name);
45 continue;
46 }
47 ASSERT_GE(ret, 0)
48 TH_LOG("Failed to stat %s: %s",
49 namespaces[i].proc_path, strerror(errno));
50 }
51
52 ASSERT_EQ(st.st_ino, namespaces[i].expected_ino)
53 TH_LOG("Namespace %s has inode 0x%lx, expected 0x%x",
54 namespaces[i].name, st.st_ino, namespaces[i].expected_ino);
55
56 ksft_print_msg("Namespace %s: inode 0x%lx matches expected 0x%x\n",
57 namespaces[i].name, st.st_ino, namespaces[i].expected_ino);
58 }
59 }
60
61 TEST_HARNESS_MAIN
62