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 * Basic error handling test for timer_create(2):
12 *
13 * Passes invalid parameters when calling the syscall and checks
14 * if it fails with EFAULT/EINVAL:
15 * 1) Pass an invalid pointer for the sigevent structure parameter
16 * 2) Pass an invalid pointer for the timer ID parameter
17 * 3) Pass invalid clock type
18 * 4) Pass a sigevent with invalid sigev_notify
19 * 5) Pass a sigevent with invalid sigev_signo
20 */
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <time.h>
25 #include <signal.h>
26 #include "tst_test.h"
27 #include "lapi/common_timers.h"
28
29 static struct sigevent sig_ev = {
30 .sigev_notify = SIGEV_NONE,
31 .sigev_signo = SIGALRM,
32 };
33
34 static struct sigevent sig_ev_inv_not = {
35 .sigev_notify = INT_MAX,
36 .sigev_signo = SIGALRM,
37 };
38
39 static struct sigevent sig_ev_inv_sig = {
40 .sigev_notify = SIGEV_SIGNAL,
41 .sigev_signo = INT_MAX,
42 };
43
44 static kernel_timer_t timer_id;
45
46 static struct testcase {
47 clock_t clock;
48 struct sigevent *ev_ptr;
49 kernel_timer_t *kt_ptr;
50 int error;
51 char *desc;
52 } tcases[] = {
53 {CLOCK_REALTIME, NULL, &timer_id, EFAULT, "invalid sigevent struct"},
54 {CLOCK_REALTIME, &sig_ev, NULL, EFAULT, "invalid timer ID"},
55 {MAX_CLOCKS, &sig_ev, &timer_id, EINVAL, "invalid clock"},
56 {CLOCK_REALTIME, &sig_ev_inv_not, &timer_id, EINVAL, "wrong sigev_notify"},
57 {CLOCK_REALTIME, &sig_ev_inv_sig, &timer_id, EINVAL, "wrong sigev_signo"},
58 };
59
run(unsigned int n)60 static void run(unsigned int n)
61 {
62 struct testcase *tc = &tcases[n];
63
64 TEST(tst_syscall(__NR_timer_create, tc->clock, tc->ev_ptr, tc->kt_ptr));
65
66 if (TST_RET != -1 || TST_ERR != tc->error) {
67 tst_res(TFAIL | TTERRNO,
68 "%s idn't fail as expected (%s) - Got",
69 tc->desc, tst_strerrno(tc->error));
70 return;
71 }
72
73 tst_res(TPASS | TTERRNO, "%s failed as expected", tc->desc);
74 }
75
setup(void)76 static void setup(void)
77 {
78 unsigned int i;
79
80 for (i = 0; i < ARRAY_SIZE(tcases); i++) {
81 if (!tcases[i].ev_ptr)
82 tcases[i].ev_ptr = tst_get_bad_addr(NULL);
83
84 if (!tcases[i].kt_ptr)
85 tcases[i].kt_ptr = tst_get_bad_addr(NULL);
86 }
87 }
88
89 static struct tst_test test = {
90 .test = run,
91 .tcnt = ARRAY_SIZE(tcases),
92 .setup = setup,
93 };
94