1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
4 */
5 /*
6
7 Test if CLOCK_BOOTTIME namespace offset is applied to sysinfo uptime and that
8 it's consistent with /proc/uptime as well.
9
10 After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the
11 process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC
12 and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'.
13
14 */
15
16 #include <sys/sysinfo.h>
17 #include "lapi/posix_clocks.h"
18 #include "tst_test.h"
19 #include "lapi/sched.h"
20
21 static int offsets[] = {
22 10,
23 -10,
24 3600,
25 };
26
read_proc_uptime(void)27 static long read_proc_uptime(void)
28 {
29 long sec, sec_rem;
30
31 SAFE_FILE_SCANF("/proc/uptime", "%li.%li", &sec, &sec_rem);
32
33 return sec + (sec_rem ? 1 : 0);
34 }
35
verify_sysinfo(unsigned int n)36 static void verify_sysinfo(unsigned int n)
37 {
38 struct sysinfo si;
39 long uptime;
40 int off = offsets[n];
41
42 SAFE_UNSHARE(CLONE_NEWTIME);
43
44 SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0",
45 CLOCK_BOOTTIME, off);
46
47 sysinfo(&si);
48
49 uptime = si.uptime;
50
51 if (!SAFE_FORK()) {
52 sysinfo(&si);
53 long proc_uptime = read_proc_uptime();
54
55 long diff = si.uptime - uptime;
56
57 if (diff < off || diff > off + 1)
58 tst_res(TFAIL, "Wrong sysinfo uptime offset %li", diff);
59 else
60 tst_res(TPASS, "Correct sysinfo uptime offset %i", off);
61
62 if (si.uptime < proc_uptime || si.uptime > proc_uptime + 1) {
63 tst_res(TFAIL, "/proc/uptime %li differs from sysinfo %li",
64 proc_uptime, si.uptime);
65 } else {
66 tst_res(TPASS, "/proc/uptime is consistent with sysinfo");
67 }
68 }
69 }
70
71 static struct tst_test test = {
72 .tcnt = ARRAY_SIZE(offsets),
73 .test = verify_sysinfo,
74 .needs_root = 1,
75 .forks_child = 1,
76 .needs_kconfigs = (const char *[]) {
77 "CONFIG_TIME_NS=y",
78 NULL
79 },
80 .tags = (const struct tst_tag[]) {
81 {"linux-git", "ecc421e05bab"},
82 {}
83 }
84 };
85