1 // SPDX-License-Identifier: GPL-2.0
11 #include <sys/syscall.h>
12 #include <sys/types.h>
20 * Test shouldn't be run for a day, so add 10 days to child
21 * time and check parent's time to be in the same day.
23 #define MAX_TEST_TIME_SEC (60*5)
24 #define DAY_IN_SEC (60*60*24)
25 #define TEN_DAYS_IN_SEC (10*DAY_IN_SEC)
27 static int child_ns, parent_ns;
29 static int switch_ns(int fd)
31 if (setns(fd, CLONE_NEWTIME))
32 return pr_perror("setns()");
37 static int init_namespaces(void)
39 char path[] = "/proc/self/ns/time_for_children";
42 parent_ns = open(path, O_RDONLY);
44 return pr_perror("Unable to open %s", path);
46 if (fstat(parent_ns, &st1))
47 return pr_perror("Unable to stat the parent timens");
52 child_ns = open(path, O_RDONLY);
54 return pr_perror("Unable to open %s", path);
56 if (fstat(child_ns, &st2))
57 return pr_perror("Unable to stat the timens");
59 if (st1.st_ino == st2.st_ino)
60 return pr_err("The same child_ns after CLONE_NEWTIME");
62 if (_settime(CLOCK_BOOTTIME, TEN_DAYS_IN_SEC))
68 static int read_proc_uptime(struct timespec *uptime)
70 unsigned long up_sec, up_nsec;
73 proc = fopen("/proc/uptime", "r");
75 pr_perror("Unable to open /proc/uptime");
79 if (fscanf(proc, "%lu.%02lu", &up_sec, &up_nsec) != 2) {
84 pr_err("failed to parse /proc/uptime");
89 uptime->tv_sec = up_sec;
90 uptime->tv_nsec = up_nsec;
94 static int read_proc_stat_btime(unsigned long long *boottime_sec)
99 proc = fopen("/proc/stat", "r");
101 pr_perror("Unable to open /proc/stat");
105 while (fgets(line_buf, 2048, proc)) {
106 if (sscanf(line_buf, "btime %llu", boottime_sec) != 1)
116 pr_err("failed to parse /proc/stat");
121 static int check_uptime(void)
123 struct timespec uptime_new, uptime_old;
124 time_t uptime_expected;
125 double prec = MAX_TEST_TIME_SEC;
127 if (switch_ns(parent_ns))
128 return pr_err("switch_ns(%d)", parent_ns);
130 if (read_proc_uptime(&uptime_old))
133 if (switch_ns(child_ns))
134 return pr_err("switch_ns(%d)", child_ns);
136 if (read_proc_uptime(&uptime_new))
139 uptime_expected = uptime_old.tv_sec + TEN_DAYS_IN_SEC;
140 if (fabs(difftime(uptime_new.tv_sec, uptime_expected)) > prec) {
141 pr_fail("uptime in /proc/uptime: old %ld, new %ld [%ld]",
142 uptime_old.tv_sec, uptime_new.tv_sec,
143 uptime_old.tv_sec + TEN_DAYS_IN_SEC);
147 ksft_test_result_pass("Passed for /proc/uptime\n");
151 static int check_stat_btime(void)
153 unsigned long long btime_new, btime_old;
154 unsigned long long btime_expected;
156 if (switch_ns(parent_ns))
157 return pr_err("switch_ns(%d)", parent_ns);
159 if (read_proc_stat_btime(&btime_old))
162 if (switch_ns(child_ns))
163 return pr_err("switch_ns(%d)", child_ns);
165 if (read_proc_stat_btime(&btime_new))
168 btime_expected = btime_old - TEN_DAYS_IN_SEC;
169 if (btime_new != btime_expected) {
170 pr_fail("btime in /proc/stat: old %llu, new %llu [%llu]",
171 btime_old, btime_new, btime_expected);
175 ksft_test_result_pass("Passed for /proc/stat btime\n");
179 int main(int argc, char *argv[])
187 if (init_namespaces())
190 ret |= check_uptime();
191 ret |= check_stat_btime();