init_ino_test.c 1.7 KB

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