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