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 
16 #include "log.h"
17 #include "timens.h"
18 
19 /*
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.
22  */
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)
26 
27 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
28 
29 static int child_ns, parent_ns;
30 
31 static int switch_ns(int fd)
32 {
33 	if (setns(fd, CLONE_NEWTIME))
34 		return pr_perror("setns()");
35 
36 	return 0;
37 }
38 
39 static int init_namespaces(void)
40 {
41 	char path[] = "/proc/self/ns/time_for_children";
42 	struct stat st1, st2;
43 
44 	parent_ns = open(path, O_RDONLY);
45 	if (parent_ns <= 0)
46 		return pr_perror("Unable to open %s", path);
47 
48 	if (fstat(parent_ns, &st1))
49 		return pr_perror("Unable to stat the parent timens");
50 
51 	if (unshare_timens())
52 		return -1;
53 
54 	child_ns = open(path, O_RDONLY);
55 	if (child_ns <= 0)
56 		return pr_perror("Unable to open %s", path);
57 
58 	if (fstat(child_ns, &st2))
59 		return pr_perror("Unable to stat the timens");
60 
61 	if (st1.st_ino == st2.st_ino)
62 		return pr_err("The same child_ns after CLONE_NEWTIME");
63 
64 	if (_settime(CLOCK_BOOTTIME, TEN_DAYS_IN_SEC))
65 		return -1;
66 
67 	return 0;
68 }
69 
70 static int read_proc_uptime(struct timespec *uptime)
71 {
72 	unsigned long up_sec, up_nsec;
73 	FILE *proc;
74 
75 	proc = fopen("/proc/uptime", "r");
76 	if (proc == NULL) {
77 		pr_perror("Unable to open /proc/uptime");
78 		return -1;
79 	}
80 
81 	if (fscanf(proc, "%lu.%02lu", &up_sec, &up_nsec) != 2) {
82 		if (errno) {
83 			pr_perror("fscanf");
84 			return -errno;
85 		}
86 		pr_err("failed to parse /proc/uptime");
87 		return -1;
88 	}
89 	fclose(proc);
90 
91 	uptime->tv_sec = up_sec;
92 	uptime->tv_nsec = up_nsec;
93 	return 0;
94 }
95 
96 static int read_proc_stat_btime(unsigned long long *boottime_sec)
97 {
98 	FILE *proc;
99 	char line_buf[2048];
100 
101 	proc = fopen("/proc/stat", "r");
102 	if (proc == NULL) {
103 		pr_perror("Unable to open /proc/stat");
104 		return -1;
105 	}
106 
107 	while (fgets(line_buf, 2048, proc)) {
108 		if (sscanf(line_buf, "btime %llu", boottime_sec) != 1)
109 			continue;
110 		fclose(proc);
111 		return 0;
112 	}
113 	if (errno) {
114 		pr_perror("fscanf");
115 		fclose(proc);
116 		return -errno;
117 	}
118 	pr_err("failed to parse /proc/stat");
119 	fclose(proc);
120 	return -1;
121 }
122 
123 static int check_uptime(void)
124 {
125 	struct timespec uptime_new, uptime_old;
126 	time_t uptime_expected;
127 	double prec = MAX_TEST_TIME_SEC;
128 
129 	if (switch_ns(parent_ns))
130 		return pr_err("switch_ns(%d)", parent_ns);
131 
132 	if (read_proc_uptime(&uptime_old))
133 		return 1;
134 
135 	if (switch_ns(child_ns))
136 		return pr_err("switch_ns(%d)", child_ns);
137 
138 	if (read_proc_uptime(&uptime_new))
139 		return 1;
140 
141 	uptime_expected = uptime_old.tv_sec + TEN_DAYS_IN_SEC;
142 	if (fabs(difftime(uptime_new.tv_sec, uptime_expected)) > prec) {
143 		pr_fail("uptime in /proc/uptime: old %ld, new %ld [%ld]",
144 			uptime_old.tv_sec, uptime_new.tv_sec,
145 			uptime_old.tv_sec + TEN_DAYS_IN_SEC);
146 		return 1;
147 	}
148 
149 	ksft_test_result_pass("Passed for /proc/uptime\n");
150 	return 0;
151 }
152 
153 static int check_stat_btime(void)
154 {
155 	unsigned long long btime_new, btime_old;
156 	unsigned long long btime_expected;
157 
158 	if (switch_ns(parent_ns))
159 		return pr_err("switch_ns(%d)", parent_ns);
160 
161 	if (read_proc_stat_btime(&btime_old))
162 		return 1;
163 
164 	if (switch_ns(child_ns))
165 		return pr_err("switch_ns(%d)", child_ns);
166 
167 	if (read_proc_stat_btime(&btime_new))
168 		return 1;
169 
170 	btime_expected = btime_old - TEN_DAYS_IN_SEC;
171 	if (btime_new != btime_expected) {
172 		pr_fail("btime in /proc/stat: old %llu, new %llu [%llu]",
173 			btime_old, btime_new, btime_expected);
174 		return 1;
175 	}
176 
177 	ksft_test_result_pass("Passed for /proc/stat btime\n");
178 	return 0;
179 }
180 
181 int main(int argc, char *argv[])
182 {
183 	int ret = 0;
184 
185 	nscheck();
186 
187 	ksft_set_plan(2);
188 
189 	if (init_namespaces())
190 		return 1;
191 
192 	ret |= check_uptime();
193 	ret |= check_stat_btime();
194 
195 	if (ret)
196 		ksft_exit_fail();
197 	ksft_exit_pass();
198 	return ret;
199 }
200