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
32static struct tst_ts timenow;
33static struct tst_its new_set, old_set;
34static kernel_timer_t timer;
35
36static 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
49static 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
59static volatile int caught_signal;
60
61static 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
78static void sighandler(int sig)
79{
80	caught_signal = sig;
81}
82
83static void setup(void)
84{
85	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
86	SAFE_SIGNAL(SIGALRM, sighandler);
87}
88
89static 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		TEST(tst_syscall(__NR_timer_create, clock, NULL, &timer));
102		if (TST_RET != 0) {
103			if (possibly_unsupported(clock) &&
104				(TST_ERR == EINVAL || TST_ERR == ENOTSUP)) {
105				tst_res(TCONF | TTERRNO, "%s unsupported",
106					get_clock_str(clock));
107			} else {
108				tst_res(TFAIL | TTERRNO,
109					"timer_create(%s) failed",
110					get_clock_str(clock));
111			}
112			continue;
113		}
114
115		memset(&new_set, 0, sizeof(new_set));
116		memset(&old_set, 0, sizeof(old_set));
117
118		new_set.type = old_set.type = tv->ts_type;
119		val = tc->it_value_tv_usec;
120
121		if (tc->flag & TIMER_ABSTIME) {
122			timenow.type = tv->ts_type;
123			if (tv->clock_gettime(clock, tst_ts_get(&timenow)) < 0) {
124				tst_res(TFAIL,
125					"clock_gettime(%s) failed - skipping the test",
126					get_clock_str(clock));
127				continue;
128			}
129			tst_ts_add_us(timenow, val);
130			tst_its_set_value_from_ts(&new_set, timenow);
131		} else {
132			tst_its_set_value_from_us(&new_set, val);
133		}
134
135		tst_its_set_interval_from_us(&new_set, tc->it_interval_tv_usec);
136
137		TEST(tv->timer_settime(timer, tc->flag, tst_its_get(&new_set), tst_its_get(tc->old_ptr)));
138
139		if (TST_RET != 0) {
140			tst_res(TFAIL | TTERRNO, "timer_settime(%s) failed",
141				get_clock_str(clock));
142		}
143
144		TEST(tv->timer_gettime(timer, tst_its_get(&new_set)));
145		if (TST_RET != 0) {
146			tst_res(TFAIL | TTERRNO, "timer_gettime(%s) failed",
147				get_clock_str(clock));
148		} else if ((tst_its_get_interval_nsec(new_set) !=
149				tc->it_interval_tv_usec * 1000) ||
150			   (tst_its_get_value_nsec(new_set) >
151				MAX(tc->it_value_tv_usec * 1000, tc->it_interval_tv_usec * 1000))) {
152			tst_res(TFAIL | TTERRNO,
153				"timer_gettime(%s) reported bad values (%llu: %llu)",
154				get_clock_str(clock),
155				tst_its_get_interval_nsec(new_set),
156				tst_its_get_value_nsec(new_set));
157		}
158
159		clear_signal();
160
161		/* Wait for another event when interval was set */
162		if (tc->it_interval_tv_usec)
163			clear_signal();
164
165		tst_res(TPASS, "timer_settime(%s) passed",
166			get_clock_str(clock));
167
168		TEST(tst_syscall(__NR_timer_delete, timer));
169		if (TST_RET != 0)
170			tst_res(TFAIL | TTERRNO, "timer_delete() failed!");
171	}
172}
173
174static struct tst_test test = {
175	.test = run,
176	.needs_root = 1,
177	.tcnt = ARRAY_SIZE(tcases),
178	.test_variants = ARRAY_SIZE(variants),
179	.setup = setup,
180	.tags = (const struct tst_tag[]) {
181		{"linux-git", "f18ddc13af98"},
182		{"linux-git", "e86fea764991"},
183		{}
184	}
185};
186