• 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 basic error handling of the timer_settime(2) syscall:
12  *
13  * 1) Setting pointer to new settings to NULL -> EINVAL
14  * 2) Setting tv_nsec of the itimerspec structure to a negative value
15  *    -> EINVAL
16  * 3) Setting tv_nsec of the itimerspec structure to something larger
17  *    than NSEC_PER_SEC -> EINVAL
18  * 4) Passing an invalid timer -> EINVAL
19  * 5) Passing an invalid address for new_value -> EFAULT
20  * 6) Passing an invalid address for old_value -> EFAULT
21  *
22  * This is also regression test for commit:
23  * f18ddc13af98 ("alarmtimer: Use EOPNOTSUPP instead of ENOTSUPP")
24  */
25 
26 #include <errno.h>
27 #include <time.h>
28 #include "tst_test.h"
29 #include "lapi/common_timers.h"
30 
31 static struct itimerspec new_set, old_set;
32 static kernel_timer_t timer;
33 static kernel_timer_t timer_inval = -1;
34 
35 /* separate description-array to (hopefully) improve readability */
36 static const char * const descriptions[] = {
37 	"setting new_set pointer to NULL",
38 	"setting tv_nsec to a negative value",
39 	"setting tv_nsec value too high",
40 	"passing pointer to invalid timer_id",
41 	"passing invalid address for new_value",
42 	"passing invalid address for old_value",
43 };
44 
45 static struct testcase {
46 	kernel_timer_t		*timer_id;
47 	struct itimerspec	*new_ptr;
48 	struct itimerspec	*old_ptr;
49 	int			it_value_tv_nsec;
50 	int			error;
51 } tcases[] = {
52 	{&timer, NULL, &old_set, 0, EINVAL},
53 	{&timer, &new_set, &old_set, -1, EINVAL},
54 	{&timer, &new_set, &old_set, NSEC_PER_SEC + 1, EINVAL},
55 	{&timer_inval, &new_set, &old_set, 0, EINVAL},
56 	{&timer, (struct itimerspec *) -1, &old_set, 0, EFAULT},
57 	{&timer, &new_set, (struct itimerspec *) -1, 0, EFAULT},
58 };
59 
run(unsigned int n)60 static void run(unsigned int n)
61 {
62 	unsigned int i;
63 	struct testcase *tc = &tcases[n];
64 
65 	tst_res(TINFO, "Testing for %s:", descriptions[n]);
66 
67 	for (i = 0; i < CLOCKS_DEFINED; ++i) {
68 		clock_t clock = clock_list[i];
69 
70 		if (clock == CLOCK_PROCESS_CPUTIME_ID ||
71 			clock == CLOCK_THREAD_CPUTIME_ID) {
72 			if (!have_cputime_timers())
73 				continue;
74 		}
75 
76 		/* Init temporary timer */
77 		TEST(tst_syscall(__NR_timer_create, clock, NULL, &timer));
78 		if (TST_RET != 0) {
79 			if (possibly_unsupported(clock) &&
80 				(TST_ERR == EINVAL || TST_ERR == ENOTSUP)) {
81 				tst_res(TCONF | TTERRNO, "%s unsupported",
82 					get_clock_str(clock));
83 			} else {
84 				tst_res(TFAIL | TTERRNO,
85 					"timer_create(%s) failed",
86 					get_clock_str(clock));
87 			}
88 			continue;
89 		}
90 
91 		memset(&new_set, 0, sizeof(new_set));
92 		memset(&old_set, 0, sizeof(old_set));
93 
94 		new_set.it_value.tv_sec  = 5;
95 		new_set.it_value.tv_nsec = tc->it_value_tv_nsec;
96 
97 		TEST(tst_syscall(__NR_timer_settime, *tc->timer_id,
98 					0, tc->new_ptr,	tc->old_ptr));
99 
100 		if (tc->error != TST_ERR) {
101 			tst_res(TFAIL | TTERRNO,
102 				 "%s didn't fail as expected. Expected: %s - Got",
103 				 get_clock_str(clock),
104 				 tst_strerrno(tc->error));
105 		} else {
106 			tst_res(TPASS | TTERRNO,
107 				"%s failed as expected",
108 				get_clock_str(clock));
109 		}
110 
111 		/* Delete temporary timer */
112 		TEST(tst_syscall(__NR_timer_delete, timer));
113 		if (TST_RET != 0)
114 			tst_res(TFAIL | TTERRNO, "timer_delete() failed!");
115 	}
116 }
117 
118 static struct tst_test test = {
119 	.test = run,
120 	.needs_root = 1,
121 	.tcnt = ARRAY_SIZE(tcases),
122 	.tags = (const struct tst_tag[]) {
123 		{"linux-git", "f18ddc13af98"},
124 		{}
125 	}
126 };
127