1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) Wipro Technologies Ltd, 2003. All Rights Reserved.
4 *
5 * Author: Aniruddha Marathe <aniruddha.marathe@wipro.com>
6 *
7 * Ported to new library:
8 * 07/2019 Christian Amann <camann@suse.com>
9 */
10 /*
11 * This tests the timer_settime(2) syscall under various conditions:
12 *
13 * 1) General initialization: No old_value, no flags
14 * 2) Setting a pointer to a itimerspec struct as old_set parameter
15 * 3) Using a periodic timer
16 * 4) Using absolute time
17 *
18 * All of these tests are supposed to be successful.
19 *
20 * This is also regression test for commit:
21 * f18ddc13af98 ("alarmtimer: Use EOPNOTSUPP instead of ENOTSUPP")
22 * e86fea764991 ("alarmtimer: Return relative times in timer_gettime")
23 */
24
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <time.h>
28 #include <signal.h>
29 #include "time64_variants.h"
30 #include "tst_timer.h"
31
32 static struct tst_ts timenow;
33 static struct tst_its new_set, old_set;
34 static kernel_timer_t timer;
35
36 static struct testcase {
37 struct tst_its *old_ptr;
38 int it_value_tv_usec;
39 int it_interval_tv_usec;
40 int flag;
41 char *description;
42 } tcases[] = {
43 {NULL, 50000, 0, 0, "general initialization"},
44 {&old_set, 50000, 0, 0, "setting old_value"},
45 {&old_set, 50000, 50000, 0, "using periodic timer"},
46 {&old_set, 50000, 0, TIMER_ABSTIME, "using absolute time"},
47 };
48
49 static struct time64_variants variants[] = {
50 #if (__NR_timer_settime != __LTP__NR_INVALID_SYSCALL)
51 { .clock_gettime = sys_clock_gettime, .timer_gettime = sys_timer_gettime, .timer_settime = sys_timer_settime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
52 #endif
53
54 #if (__NR_timer_settime64 != __LTP__NR_INVALID_SYSCALL)
55 { .clock_gettime = sys_clock_gettime64, .timer_gettime = sys_timer_gettime64, .timer_settime = sys_timer_settime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
56 #endif
57 };
58
59 static volatile int caught_signal;
60
clear_signal(void)61 static void clear_signal(void)
62 {
63 /*
64 * The busy loop is intentional. The signal is sent after X
65 * seconds of CPU time has been accumulated for the process and
66 * thread specific clocks.
67 */
68 while (!caught_signal);
69
70 if (caught_signal != SIGALRM) {
71 tst_res(TFAIL, "Received incorrect signal: %s",
72 tst_strsig(caught_signal));
73 }
74
75 caught_signal = 0;
76 }
77
sighandler(int sig)78 static void sighandler(int sig)
79 {
80 caught_signal = sig;
81 }
82
setup(void)83 static void setup(void)
84 {
85 tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
86 SAFE_SIGNAL(SIGALRM, sighandler);
87 }
88
run(unsigned int n)89 static void run(unsigned int n)
90 {
91 struct time64_variants *tv = &variants[tst_variant];
92 struct testcase *tc = &tcases[n];
93 long long val;
94 unsigned int i;
95
96 tst_res(TINFO, "Testing for %s:", tc->description);
97
98 for (i = 0; i < CLOCKS_DEFINED; ++i) {
99 clock_t clock = clock_list[i];
100
101 if (clock == CLOCK_PROCESS_CPUTIME_ID ||
102 clock == CLOCK_THREAD_CPUTIME_ID) {
103 if (!have_cputime_timers())
104 continue;
105 }
106
107 TEST(tst_syscall(__NR_timer_create, clock, NULL, &timer));
108 if (TST_RET != 0) {
109 if (possibly_unsupported(clock) &&
110 (TST_ERR == EINVAL || TST_ERR == ENOTSUP)) {
111 tst_res(TCONF | TTERRNO, "%s unsupported",
112 get_clock_str(clock));
113 } else {
114 tst_res(TFAIL | TTERRNO,
115 "timer_create(%s) failed",
116 get_clock_str(clock));
117 }
118 continue;
119 }
120
121 memset(&new_set, 0, sizeof(new_set));
122 memset(&old_set, 0, sizeof(old_set));
123
124 new_set.type = old_set.type = tv->ts_type;
125 val = tc->it_value_tv_usec;
126
127 if (tc->flag & TIMER_ABSTIME) {
128 timenow.type = tv->ts_type;
129 if (tv->clock_gettime(clock, tst_ts_get(&timenow)) < 0) {
130 tst_res(TFAIL,
131 "clock_gettime(%s) failed - skipping the test",
132 get_clock_str(clock));
133 continue;
134 }
135 tst_ts_add_us(timenow, val);
136 tst_its_set_value_from_ts(&new_set, timenow);
137 } else {
138 tst_its_set_value_from_us(&new_set, val);
139 }
140
141 tst_its_set_interval_from_us(&new_set, tc->it_interval_tv_usec);
142
143 TEST(tv->timer_settime(timer, tc->flag, tst_its_get(&new_set), tst_its_get(tc->old_ptr)));
144
145 if (TST_RET != 0) {
146 tst_res(TFAIL | TTERRNO, "timer_settime(%s) failed",
147 get_clock_str(clock));
148 }
149
150 TEST(tv->timer_gettime(timer, tst_its_get(&new_set)));
151 if (TST_RET != 0) {
152 tst_res(TFAIL | TTERRNO, "timer_gettime(%s) failed",
153 get_clock_str(clock));
154 } else if ((tst_its_get_interval_nsec(new_set) !=
155 tc->it_interval_tv_usec * 1000) ||
156 (tst_its_get_value_nsec(new_set) >
157 MAX(tc->it_value_tv_usec * 1000, tc->it_interval_tv_usec * 1000))) {
158 tst_res(TFAIL | TTERRNO,
159 "timer_gettime(%s) reported bad values (%llu: %llu)",
160 get_clock_str(clock),
161 tst_its_get_interval_nsec(new_set),
162 tst_its_get_value_nsec(new_set));
163 }
164
165 clear_signal();
166
167 /* Wait for another event when interval was set */
168 if (tc->it_interval_tv_usec)
169 clear_signal();
170
171 tst_res(TPASS, "timer_settime(%s) passed",
172 get_clock_str(clock));
173
174 TEST(tst_syscall(__NR_timer_delete, timer));
175 if (TST_RET != 0)
176 tst_res(TFAIL | TTERRNO, "timer_delete() failed!");
177 }
178 }
179
180 static struct tst_test test = {
181 .test = run,
182 .needs_root = 1,
183 .tcnt = ARRAY_SIZE(tcases),
184 .test_variants = ARRAY_SIZE(variants),
185 .setup = setup,
186 .tags = (const struct tst_tag[]) {
187 {"linux-git", "f18ddc13af98"},
188 {"linux-git", "e86fea764991"},
189 {}
190 }
191 };
192