procfs.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <math.h>
  6. #include <sched.h>
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include <stdlib.h>
  10. #include <sys/stat.h>
  11. #include <sys/syscall.h>
  12. #include <sys/types.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15. #include "log.h"
  16. #include "timens.h"
  17. /*
  18. * Test shouldn't be run for a day, so add 10 days to child
  19. * time and check parent's time to be in the same day.
  20. */
  21. #define MAX_TEST_TIME_SEC (60*5)
  22. #define DAY_IN_SEC (60*60*24)
  23. #define TEN_DAYS_IN_SEC (10*DAY_IN_SEC)
  24. static int child_ns, parent_ns;
  25. static int switch_ns(int fd)
  26. {
  27. if (setns(fd, CLONE_NEWTIME))
  28. return pr_perror("setns()");
  29. return 0;
  30. }
  31. static int init_namespaces(void)
  32. {
  33. char path[] = "/proc/self/ns/time_for_children";
  34. struct stat st1, st2;
  35. parent_ns = open(path, O_RDONLY);
  36. if (parent_ns <= 0)
  37. return pr_perror("Unable to open %s", path);
  38. if (fstat(parent_ns, &st1))
  39. return pr_perror("Unable to stat the parent timens");
  40. if (unshare_timens())
  41. return -1;
  42. child_ns = open(path, O_RDONLY);
  43. if (child_ns <= 0)
  44. return pr_perror("Unable to open %s", path);
  45. if (fstat(child_ns, &st2))
  46. return pr_perror("Unable to stat the timens");
  47. if (st1.st_ino == st2.st_ino)
  48. return pr_err("The same child_ns after CLONE_NEWTIME");
  49. if (_settime(CLOCK_BOOTTIME, TEN_DAYS_IN_SEC))
  50. return -1;
  51. return 0;
  52. }
  53. static int read_proc_uptime(struct timespec *uptime)
  54. {
  55. unsigned long up_sec, up_nsec;
  56. FILE *proc;
  57. proc = fopen("/proc/uptime", "r");
  58. if (proc == NULL) {
  59. pr_perror("Unable to open /proc/uptime");
  60. return -1;
  61. }
  62. if (fscanf(proc, "%lu.%02lu", &up_sec, &up_nsec) != 2) {
  63. if (errno) {
  64. pr_perror("fscanf");
  65. return -errno;
  66. }
  67. pr_err("failed to parse /proc/uptime");
  68. return -1;
  69. }
  70. fclose(proc);
  71. uptime->tv_sec = up_sec;
  72. uptime->tv_nsec = up_nsec;
  73. return 0;
  74. }
  75. static int read_proc_stat_btime(unsigned long long *boottime_sec)
  76. {
  77. FILE *proc;
  78. char line_buf[2048];
  79. proc = fopen("/proc/stat", "r");
  80. if (proc == NULL) {
  81. pr_perror("Unable to open /proc/stat");
  82. return -1;
  83. }
  84. while (fgets(line_buf, 2048, proc)) {
  85. if (sscanf(line_buf, "btime %llu", boottime_sec) != 1)
  86. continue;
  87. fclose(proc);
  88. return 0;
  89. }
  90. if (errno) {
  91. pr_perror("fscanf");
  92. fclose(proc);
  93. return -errno;
  94. }
  95. pr_err("failed to parse /proc/stat");
  96. fclose(proc);
  97. return -1;
  98. }
  99. static int check_uptime(void)
  100. {
  101. struct timespec uptime_new, uptime_old;
  102. time_t uptime_expected;
  103. double prec = MAX_TEST_TIME_SEC;
  104. if (switch_ns(parent_ns))
  105. return pr_err("switch_ns(%d)", parent_ns);
  106. if (read_proc_uptime(&uptime_old))
  107. return 1;
  108. if (switch_ns(child_ns))
  109. return pr_err("switch_ns(%d)", child_ns);
  110. if (read_proc_uptime(&uptime_new))
  111. return 1;
  112. uptime_expected = uptime_old.tv_sec + TEN_DAYS_IN_SEC;
  113. if (fabs(difftime(uptime_new.tv_sec, uptime_expected)) > prec) {
  114. pr_fail("uptime in /proc/uptime: old %ld, new %ld [%ld]",
  115. uptime_old.tv_sec, uptime_new.tv_sec,
  116. uptime_old.tv_sec + TEN_DAYS_IN_SEC);
  117. return 1;
  118. }
  119. ksft_test_result_pass("Passed for /proc/uptime\n");
  120. return 0;
  121. }
  122. static int check_stat_btime(void)
  123. {
  124. unsigned long long btime_new, btime_old;
  125. unsigned long long btime_expected;
  126. if (switch_ns(parent_ns))
  127. return pr_err("switch_ns(%d)", parent_ns);
  128. if (read_proc_stat_btime(&btime_old))
  129. return 1;
  130. if (switch_ns(child_ns))
  131. return pr_err("switch_ns(%d)", child_ns);
  132. if (read_proc_stat_btime(&btime_new))
  133. return 1;
  134. btime_expected = btime_old - TEN_DAYS_IN_SEC;
  135. if (btime_new != btime_expected) {
  136. pr_fail("btime in /proc/stat: old %llu, new %llu [%llu]",
  137. btime_old, btime_new, btime_expected);
  138. return 1;
  139. }
  140. ksft_test_result_pass("Passed for /proc/stat btime\n");
  141. return 0;
  142. }
  143. int main(int argc, char *argv[])
  144. {
  145. int ret = 0;
  146. nscheck();
  147. ksft_set_plan(2);
  148. if (init_namespaces())
  149. return 1;
  150. ret |= check_uptime();
  151. ret |= check_stat_btime();
  152. if (ret)
  153. ksft_exit_fail();
  154. ksft_exit_pass();
  155. return ret;
  156. }