]> granicus.if.org Git - strace/blob - tests/ioctl_evdev.c
tests: check decoding of EVIOC* ioctls
[strace] / tests / ioctl_evdev.c
1 /*
2  * This file is part of ioctl_block strace test.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31
32 #ifdef HAVE_LINUX_INPUT_H
33
34 # include <errno.h>
35 # include <inttypes.h>
36 # include <stdio.h>
37 # include <string.h>
38 # include <sys/ioctl.h>
39 # include <linux/input.h>
40
41 static const unsigned int magic = 0xdeadbeef;
42 static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0ded;
43
44 static void
45 init_magic(void *addr, const unsigned int size)
46 {
47         unsigned int *p = addr;
48         const unsigned int *end = addr + size - sizeof(int);
49
50         for (; p <= end; ++p)
51                 *(unsigned int *) p = magic;
52 }
53
54 # ifdef VERBOSE_IOCTL
55 static void
56 print_envelope(const struct ff_envelope *const e)
57 {
58         printf(", envelope={attack_length=%hu, attack_level=%hu"
59                ", fade_length=%hu, fade_level=%#hx}",
60                e->attack_length, e->attack_level,
61                e->fade_length, e->fade_level);
62 }
63 # endif /* VERBOSE_IOCTL */
64
65 static void
66 print_ffe_common(const struct ff_effect *const ffe, const char *const type_str)
67 {
68         printf("ioctl(-1, EVIOCSFF, {type=%s, id=%" PRIu16
69                ", direction=%" PRIu16 ", ",
70                type_str, ffe->id, ffe->direction);
71 # ifdef VERBOSE_IOCTL
72         printf("trigger={button=%hu, interval=%hu}"
73                ", replay={length=%hu, delay=%hu}",
74                ffe->trigger.button, ffe->trigger.interval,
75                ffe->replay.length, ffe->replay.delay);
76 # endif /* VERBOSE_IOCTL */
77 }
78
79 # define TEST_NULL_ARG(cmd) \
80         ioctl(-1, cmd, 0); \
81         printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd)
82
83 int
84 main(void)
85 {
86         TEST_NULL_ARG(EVIOCGVERSION);
87         TEST_NULL_ARG(EVIOCGEFFECTS);
88         TEST_NULL_ARG(EVIOCGID);
89         TEST_NULL_ARG(EVIOCGKEYCODE);
90         TEST_NULL_ARG(EVIOCSKEYCODE);
91         TEST_NULL_ARG(EVIOCSFF);
92 # ifdef EVIOCGKEYCODE_V2
93         TEST_NULL_ARG(EVIOCGKEYCODE_V2);
94 # endif
95 # ifdef EVIOCSKEYCODE_V2
96         TEST_NULL_ARG(EVIOCSKEYCODE_V2);
97 # endif
98 # ifdef EVIOCGREP
99         TEST_NULL_ARG(EVIOCGREP);
100 # endif
101 # ifdef EVIOCSREP
102         TEST_NULL_ARG(EVIOCSREP);
103 # endif
104 # ifdef EVIOCSCLOCKID
105         TEST_NULL_ARG(EVIOCSCLOCKID);
106 # endif
107
108         TEST_NULL_ARG(EVIOCGNAME(0));
109         TEST_NULL_ARG(EVIOCGPHYS(0));
110         TEST_NULL_ARG(EVIOCGKEY(0));
111         TEST_NULL_ARG(EVIOCGLED(0));
112 # ifdef EVIOCGMTSLOTS
113         TEST_NULL_ARG(EVIOCGMTSLOTS(0));
114 # endif
115 # ifdef EVIOCGPROP
116         TEST_NULL_ARG(EVIOCGPROP(0));
117 # endif
118         TEST_NULL_ARG(EVIOCGSND(0));
119 # ifdef EVIOCGSW
120         TEST_NULL_ARG(EVIOCGSW(0));
121 # endif
122
123         TEST_NULL_ARG(EVIOCGABS(ABS_X));
124         TEST_NULL_ARG(EVIOCSABS(ABS_X));
125
126         TEST_NULL_ARG(EVIOCGBIT(EV_SYN, 0));
127         TEST_NULL_ARG(EVIOCGBIT(EV_KEY, 1));
128         TEST_NULL_ARG(EVIOCGBIT(EV_REL, 2));
129         TEST_NULL_ARG(EVIOCGBIT(EV_ABS, 3));
130         TEST_NULL_ARG(EVIOCGBIT(EV_MSC, 4));
131 # ifdef EV_SW
132         TEST_NULL_ARG(EVIOCGBIT(EV_SW, 5));
133 # endif
134         TEST_NULL_ARG(EVIOCGBIT(EV_LED, 6));
135         TEST_NULL_ARG(EVIOCGBIT(EV_SND, 7));
136         TEST_NULL_ARG(EVIOCGBIT(EV_REP, 8));
137         TEST_NULL_ARG(EVIOCGBIT(EV_FF, 9));
138         TEST_NULL_ARG(EVIOCGBIT(EV_PWR, 10));
139         TEST_NULL_ARG(EVIOCGBIT(EV_FF_STATUS, 11));
140
141         ioctl(-1, EVIOCGBIT(EV_MAX, 42), 0);
142         printf("ioctl(-1, EVIOCGBIT(%#x /* EV_??? */, 42), NULL)"
143                " = -1 EBADF (%m)\n", EV_MAX);
144
145         ioctl(-1, EVIOCRMFF, lmagic);
146         printf("ioctl(-1, EVIOCRMFF, %d) = -1 EBADF (%m)\n", (int) lmagic);
147
148         ioctl(-1, EVIOCGRAB, lmagic);
149         printf("ioctl(-1, EVIOCGRAB, %lu) = -1 EBADF (%m)\n", lmagic);
150
151 # ifdef EVIOCREVOKE
152         ioctl(-1, EVIOCREVOKE, lmagic);
153         printf("ioctl(-1, EVIOCREVOKE, %lu) = -1 EBADF (%m)\n", lmagic);
154 # endif
155
156         const unsigned int size = get_page_size();
157         void *const page = tail_alloc(size);
158         init_magic(page, size);
159
160         int *const val_int = tail_alloc(sizeof(*val_int));
161         *val_int = magic;
162
163 # ifdef EVIOCSCLOCKID
164         ioctl(-1, EVIOCSCLOCKID, val_int);
165         printf("ioctl(-1, EVIOCSCLOCKID, [%u]) = -1 EBADF (%m)\n", *val_int);
166 # endif
167
168         int *pair_int = tail_alloc(sizeof(*pair_int) * 2);
169         pair_int[0] = 0xdeadbeef;
170         pair_int[1] = 0xbadc0ded;
171
172 # ifdef EVIOSGREP
173         ioctl(-1, EVIOCSREP, pair_int);
174         printf("ioctl(-1, EVIOCSREP, [%u, %u]) = -1 EBADF (%m)\n",
175                pair_int[0], pair_int[1]);
176 # endif
177
178         pair_int[1] = 1;
179         ioctl(-1, EVIOCSKEYCODE, pair_int);
180         printf("ioctl(-1, EVIOCSKEYCODE, [%u, %s]) = -1 EBADF (%m)\n",
181                pair_int[0], "KEY_ESC");
182
183 # ifdef EVIOCSKEYCODE_V2
184         struct input_keymap_entry *const ike = tail_alloc(sizeof(*ike));
185         init_magic(ike, sizeof(*ike));
186         ike->keycode = 2;
187
188         ioctl(-1, EVIOCSKEYCODE_V2, ike);
189         printf("ioctl(-1, EVIOCSKEYCODE_V2, {flags=%" PRIu8
190                ", len=%" PRIu8 ", ", ike->flags, ike->len);
191 #  ifdef VERBOSE_IOCTL
192         printf("index=%" PRIu16 ", keycode=%s, scancode=[",
193                ike->index, "KEY_1");
194         unsigned int i;
195         for (i = 0; i < ARRAY_SIZE(ike->scancode); ++i) {
196                 if (i > 0)
197                         printf(", ");
198                 printf("%" PRIx8, ike->scancode[i]);
199         }
200         printf("]");
201 #  else
202         printf("...");
203 #  endif
204         errno = EBADF;
205         printf("}) = -1 EBADF (%m)\n");
206 # endif
207
208         struct ff_effect *const ffe = tail_alloc(sizeof(*ffe));
209         init_magic(ffe, sizeof(*ffe));
210
211         ffe->type = FF_CONSTANT;
212         ioctl(-1, EVIOCSFF, ffe);
213         print_ffe_common(ffe, "FF_CONSTANT");
214
215 #  ifdef VERBOSE_IOCTL
216         printf(", constant={level=%hd", ffe->u.constant.level);
217         print_envelope(&ffe->u.constant.envelope);
218         printf("}");
219 #  else
220         printf("...");
221 #  endif
222         errno = EBADF;
223         printf("}) = -1 EBADF (%m)\n");
224
225 #  ifdef VERBOSE_IOCTL
226         ffe->type = FF_RAMP;
227         ioctl(-1, EVIOCSFF, ffe);
228         print_ffe_common(ffe, "FF_RAMP");
229         printf(", ramp={start_level=%hd, end_level=%hd",
230                ffe->u.ramp.start_level, ffe->u.ramp.end_level);
231         print_envelope(&ffe->u.ramp.envelope);
232         errno = EBADF;
233         printf("}}) = -1 EBADF (%m)\n");
234
235         ffe->type = FF_PERIODIC;
236         ioctl(-1, EVIOCSFF, ffe);
237         print_ffe_common(ffe, "FF_PERIODIC");
238         printf(", periodic={waveform=%hu, period=%hu, magnitude=%hd"
239                ", offset=%hd, phase=%hu",
240                ffe->u.periodic.waveform, ffe->u.periodic.period,
241                ffe->u.periodic.magnitude, ffe->u.periodic.offset,
242                ffe->u.periodic.phase);
243         print_envelope(&ffe->u.periodic.envelope);
244         printf(", custom_len=%u, custom_data=%p}",
245                ffe->u.periodic.custom_len, ffe->u.periodic.custom_data);
246         errno = EBADF;
247         printf("}) = -1 EBADF (%m)\n");
248
249         ffe->type = FF_RUMBLE;
250         ioctl(-1, EVIOCSFF, ffe);
251         print_ffe_common(ffe, "FF_RUMBLE");
252         printf(", rumble={strong_magnitude=%hu, weak_magnitude=%hu}",
253                ffe->u.rumble.strong_magnitude, ffe->u.rumble.weak_magnitude);
254         errno = EBADF;
255         printf("}) = -1 EBADF (%m)\n");
256
257         ffe->type = 0xff;
258         ioctl(-1, EVIOCSFF, ffe);
259         print_ffe_common(ffe, "0xff /* FF_??? */");
260         errno = EBADF;
261         printf("}) = -1 EBADF (%m)\n");
262 #  endif
263
264         ioctl(-1, _IOC(_IOC_READ, 0x45, 0x01, 0xff), lmagic);
265         printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
266                "_IOC(_IOC_READ, 0x45, 0x01, 0xff)", lmagic);
267
268         ioctl(-1, _IOC(_IOC_WRITE, 0x45, 0x01, 0xff), lmagic);
269         printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
270                "_IOC(_IOC_WRITE, 0x45, 0x01, 0xff)", lmagic);
271
272         ioctl(-1, _IOC(_IOC_READ|_IOC_WRITE, 0x45, 0xfe, 0xff), lmagic);
273         printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
274                "_IOC(_IOC_READ|_IOC_WRITE, 0x45, 0xfe, 0xff)", lmagic);
275
276         puts("+++ exited with 0 +++");
277         return 0;
278 }
279 #else
280
281 SKIP_MAIN_UNDEFINED("HAVE_LINUX_INPUT_H")
282
283 #endif