• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "time64_variants.h"
18 #include "tst_timer.h"
19 #include "tst_safe_clocks.h"
20 
21 #define DELTA_SEC 10
22 #define DELTA_US (long long) (DELTA_SEC * 1000000)
23 #define DELTA_EPS (long long) (DELTA_US * 0.1)
24 
25 static struct tst_ts *begin, *change, *end;
26 
27 static struct time64_variants variants[] = {
28 	{ .clock_gettime = libc_clock_gettime, .clock_settime = libc_clock_settime, .ts_type = TST_LIBC_TIMESPEC, .desc = "vDSO or syscall with libc spec"},
29 
30 #if (__NR_clock_settime != __LTP__NR_INVALID_SYSCALL)
31 	{ .clock_gettime = sys_clock_gettime, .clock_settime = sys_clock_settime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
32 #endif
33 
34 #if (__NR_clock_settime64 != __LTP__NR_INVALID_SYSCALL)
35 	{ .clock_gettime = sys_clock_gettime64, .clock_settime = sys_clock_settime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
36 #endif
37 };
38 
setup(void)39 static void setup(void)
40 {
41 	begin->type = change->type = end->type = variants[tst_variant].ts_type;
42 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
43 }
44 
do_clock_gettime(struct time64_variants * tv,struct tst_ts * ts)45 static void do_clock_gettime(struct time64_variants *tv, struct tst_ts *ts)
46 {
47 	int ret;
48 
49 	ret = tv->clock_gettime(CLOCK_REALTIME, tst_ts_get(ts));
50 	if (ret == -1)
51 		tst_brk(TBROK | TERRNO, "clock_settime(CLOCK_REALTIME) failed");
52 }
53 
verify_clock_settime(void)54 static void verify_clock_settime(void)
55 {
56 	struct time64_variants *tv = &variants[tst_variant];
57 	long long elapsed;
58 
59 	/* test 01: move forward */
60 	do_clock_gettime(tv, begin);
61 
62 	*change = tst_ts_add_us(*begin, DELTA_US);
63 
64 	TEST(tv->clock_settime(CLOCK_REALTIME, tst_ts_get(change)));
65 	if (TST_RET == -1) {
66 		tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s",
67 			tst_clock_name(CLOCK_REALTIME));
68 		return;
69 	}
70 
71 	do_clock_gettime(tv, end);
72 
73 	elapsed = tst_ts_diff_us(*end, *begin);
74 
75 	if (elapsed >= DELTA_US && elapsed < (DELTA_US + DELTA_EPS))
76 		tst_res(TPASS, "clock_settime(2): was able to advance time");
77 	else
78 		tst_res(TFAIL, "clock_settime(2): could not advance time");
79 
80 	/* test 02: move backward */
81 	do_clock_gettime(tv, begin);
82 
83 	*change = tst_ts_sub_us(*begin, DELTA_US);
84 
85 	TEST(tv->clock_settime(CLOCK_REALTIME, tst_ts_get(change)));
86 	if (TST_RET == -1) {
87 		tst_res(TFAIL | TTERRNO, "clock_settime(2) failed for clock %s",
88 			tst_clock_name(CLOCK_REALTIME));
89 		return;
90 	}
91 
92 	do_clock_gettime(tv, end);
93 
94 	elapsed = tst_ts_diff_us(*end, *begin);
95 
96 	if (~(elapsed) <= DELTA_US && ~(elapsed) > (DELTA_US - DELTA_EPS))
97 		tst_res(TPASS, "clock_settime(2): was able to recede time");
98 	else
99 		tst_res(TFAIL, "clock_settime(2): could not recede time");
100 }
101 
102 static struct tst_test test = {
103 	.test_all = verify_clock_settime,
104 	.test_variants = ARRAY_SIZE(variants),
105 	.setup = setup,
106 	.needs_root = 1,
107 	.restore_wallclock = 1,
108 	.bufs = (struct tst_buffers []) {
109 		{&begin, .size = sizeof(*begin)},
110 		{&change, .size = sizeof(*change)},
111 		{&end, .size = sizeof(*end)},
112 		{},
113 	}
114 };
115