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