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