1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 Linaro Limited. All rights reserved.
4 * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
5 */
6
7 /*
8 * Basic test for clock_settime(2) on REALTIME clock:
9 *
10 * 1) advance DELTA_SEC seconds
11 * 2) go backwards DELTA_SEC seconds
12 *
13 * Restore wall clock at the end of test.
14 */
15
16 #include "config.h"
17 #include "tst_timer.h"
18 #include "tst_safe_clocks.h"
19
20 #define DELTA_SEC 10
21 #define DELTA_US (long long) (DELTA_SEC * 1000000)
22 #define DELTA_EPS (long long) (DELTA_US * 0.1)
23
24 static struct tst_ts *begin, *change, *end;
25
26 static struct test_variants {
27 int (*gettime)(clockid_t clk_id, void *ts);
28 int (*settime)(clockid_t clk_id, void *ts);
29 enum tst_ts_type type;
30 char *desc;
31 } variants[] = {
32 { .gettime = libc_clock_gettime, .settime = libc_clock_settime, .type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"},
33
34 #if (__NR_clock_settime != __LTP__NR_INVALID_SYSCALL)
35 { .gettime = sys_clock_gettime, .settime = sys_clock_settime, .type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
36 #endif
37
38 #if (__NR_clock_settime64 != __LTP__NR_INVALID_SYSCALL)
39 { .gettime = sys_clock_gettime64, .settime = sys_clock_settime64, .type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
40 #endif
41 };
42
setup(void)43 static void setup(void)
44 {
45 begin->type = change->type = end->type = variants[tst_variant].type;
46 tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
47 }
48
do_clock_gettime(struct test_variants * tv,struct tst_ts * ts)49 static void do_clock_gettime(struct test_variants *tv, struct tst_ts *ts)
50 {
51 int ret;
52
53 ret = tv->gettime(CLOCK_REALTIME, tst_ts_get(ts));
54 if (ret == -1)
55 tst_brk(TBROK | TERRNO, "clock_settime(CLOCK_REALTIME) failed");
56 }
57
verify_clock_settime(void)58 static void verify_clock_settime(void)
59 {
60 struct test_variants *tv = &variants[tst_variant];
61 long long elapsed;
62
63 /* test 01: move forward */
64 do_clock_gettime(tv, begin);
65
66 *change = tst_ts_add_us(*begin, DELTA_US);
67
68 TEST(tv->settime(CLOCK_REALTIME, tst_ts_get(change)));
69 if (TST_RET == -1) {
70 tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s",
71 tst_clock_name(CLOCK_REALTIME));
72 return;
73 }
74
75 do_clock_gettime(tv, end);
76
77 elapsed = tst_ts_diff_us(*end, *begin);
78
79 if (elapsed >= DELTA_US && elapsed < (DELTA_US + DELTA_EPS))
80 tst_res(TPASS, "clock_settime(2): was able to advance time");
81 else
82 tst_res(TFAIL, "clock_settime(2): could not advance time");
83
84 /* test 02: move backward */
85 do_clock_gettime(tv, begin);
86
87 *change = tst_ts_sub_us(*begin, DELTA_US);
88
89 TEST(tv->settime(CLOCK_REALTIME, tst_ts_get(change)));
90 if (TST_RET == -1) {
91 tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s",
92 tst_clock_name(CLOCK_REALTIME));
93 return;
94 }
95
96 do_clock_gettime(tv, end);
97
98 elapsed = tst_ts_diff_us(*end, *begin);
99
100 if (~(elapsed) <= DELTA_US && ~(elapsed) > (DELTA_US - DELTA_EPS))
101 tst_res(TPASS, "clock_settime(2): was able to recede time");
102 else
103 tst_res(TFAIL, "clock_settime(2): could not recede time");
104 }
105
106 static struct tst_test test = {
107 .test_all = verify_clock_settime,
108 .test_variants = ARRAY_SIZE(variants),
109 .setup = setup,
110 .needs_root = 1,
111 .restore_wallclock = 1,
112 .bufs = (struct tst_buffers []) {
113 {&begin, .size = sizeof(*begin)},
114 {&change, .size = sizeof(*change)},
115 {&end, .size = sizeof(*end)},
116 {},
117 }
118 };
119