• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 5-second-timer
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  */
23 
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <time.h>
27 #include <signal.h>
28 #include "tst_test.h"
29 #include "lapi/common_timers.h"
30 
31 static struct timespec timenow;
32 static struct itimerspec new_set, old_set;
33 static kernel_timer_t timer;
34 
35 static struct testcase {
36 	struct itimerspec	*old_ptr;
37 	int			it_value_tv_sec;
38 	int			it_interval_tv_sec;
39 	int			flag;
40 	char			*description;
41 } tcases[] = {
42 	{NULL,     5, 0, 0, "general initialization"},
43 	{&old_set, 5, 0, 0, "setting old_value"},
44 	{&old_set, 0, 5, 0, "using periodic timer"},
45 	{&old_set, 5, 0, TIMER_ABSTIME, "using absolute time"},
46 };
47 
run(unsigned int n)48 static void run(unsigned int n)
49 {
50 	unsigned int i;
51 	struct testcase *tc = &tcases[n];
52 
53 	tst_res(TINFO, "Testing for %s:", tc->description);
54 
55 	for (i = 0; i < CLOCKS_DEFINED; ++i) {
56 		clock_t clock = clock_list[i];
57 
58 		if (clock == CLOCK_PROCESS_CPUTIME_ID ||
59 			clock == CLOCK_THREAD_CPUTIME_ID) {
60 			if (!have_cputime_timers())
61 				continue;
62 		}
63 
64 		TEST(tst_syscall(__NR_timer_create, clock, NULL, &timer));
65 		if (TST_RET != 0) {
66 			if (possibly_unsupported(clock) &&
67 				(TST_ERR == EINVAL || TST_ERR == ENOTSUP)) {
68 				tst_res(TCONF | TTERRNO, "%s unsupported",
69 					get_clock_str(clock));
70 			} else {
71 				tst_res(TFAIL | TTERRNO,
72 					"timer_create(%s) failed",
73 					get_clock_str(clock));
74 			}
75 			continue;
76 		}
77 
78 		memset(&new_set, 0, sizeof(new_set));
79 		memset(&old_set, 0, sizeof(old_set));
80 
81 		new_set.it_value.tv_sec = tc->it_value_tv_sec;
82 		new_set.it_interval.tv_sec = tc->it_interval_tv_sec;
83 
84 		if (tc->flag & TIMER_ABSTIME) {
85 			if (clock_gettime(clock, &timenow) < 0) {
86 				tst_res(TFAIL,
87 					"clock_gettime(%s) failed - skipping the test",
88 					get_clock_str(clock));
89 				continue;
90 			}
91 			new_set.it_value.tv_sec += timenow.tv_sec;
92 		}
93 
94 		TEST(tst_syscall(__NR_timer_settime, timer,
95 			tc->flag, &new_set, tc->old_ptr));
96 
97 		if (TST_RET != 0) {
98 			tst_res(TFAIL | TTERRNO, "%s failed",
99 					get_clock_str(clock));
100 		} else {
101 			tst_res(TPASS, "%s was successful",
102 					get_clock_str(clock));
103 		}
104 
105 		TEST(tst_syscall(__NR_timer_delete, timer));
106 		if (TST_RET != 0)
107 			tst_res(TFAIL | TTERRNO, "timer_delete() failed!");
108 	}
109 }
110 
sighandler(int sig)111 static void sighandler(int sig)
112 {
113 	/* sighandler for CLOCK_*_ALARM */
114 	tst_res(TINFO, "Caught signal %s", tst_strsig(sig));
115 }
116 
setup(void)117 static void setup(void)
118 {
119 	SAFE_SIGNAL(SIGALRM, sighandler);
120 }
121 
122 static struct tst_test test = {
123 	.test = run,
124 	.needs_root = 1,
125 	.tcnt = ARRAY_SIZE(tcases),
126 	.setup = setup,
127 	.tags = (const struct tst_tag[]) {
128 		{"linux-git", "f18ddc13af98"},
129 		{}
130 	}
131 };
132