]> granicus.if.org Git - strace/blob - tests/ioctl_evdev.c
tests/ioctl_evdev.c: fix typo in comment
[strace] / tests / ioctl_evdev.c
1 /*
2  * This file is part of ioctl_evdev 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(EVIOCGUNIQ(0));
111         TEST_NULL_ARG(EVIOCGKEY(0));
112         TEST_NULL_ARG(EVIOCGLED(0));
113 # ifdef EVIOCGMTSLOTS
114         TEST_NULL_ARG(EVIOCGMTSLOTS(0));
115 # endif
116 # ifdef EVIOCGPROP
117         TEST_NULL_ARG(EVIOCGPROP(0));
118 # endif
119         TEST_NULL_ARG(EVIOCGSND(0));
120 # ifdef EVIOCGSW
121         TEST_NULL_ARG(EVIOCGSW(0));
122 # endif
123
124         TEST_NULL_ARG(EVIOCGABS(ABS_X));
125         TEST_NULL_ARG(EVIOCSABS(ABS_X));
126
127         TEST_NULL_ARG(EVIOCGBIT(EV_SYN, 0));
128         TEST_NULL_ARG(EVIOCGBIT(EV_KEY, 1));
129         TEST_NULL_ARG(EVIOCGBIT(EV_REL, 2));
130         TEST_NULL_ARG(EVIOCGBIT(EV_ABS, 3));
131         TEST_NULL_ARG(EVIOCGBIT(EV_MSC, 4));
132 # ifdef EV_SW
133         TEST_NULL_ARG(EVIOCGBIT(EV_SW, 5));
134 # endif
135         TEST_NULL_ARG(EVIOCGBIT(EV_LED, 6));
136         TEST_NULL_ARG(EVIOCGBIT(EV_SND, 7));
137         TEST_NULL_ARG(EVIOCGBIT(EV_REP, 8));
138         TEST_NULL_ARG(EVIOCGBIT(EV_FF, 9));
139         TEST_NULL_ARG(EVIOCGBIT(EV_PWR, 10));
140         TEST_NULL_ARG(EVIOCGBIT(EV_FF_STATUS, 11));
141
142         ioctl(-1, EVIOCGBIT(EV_MAX, 42), 0);
143         printf("ioctl(-1, EVIOCGBIT(%#x /* EV_??? */, 42), NULL)"
144                " = -1 EBADF (%m)\n", EV_MAX);
145
146         ioctl(-1, EVIOCRMFF, lmagic);
147         printf("ioctl(-1, EVIOCRMFF, %d) = -1 EBADF (%m)\n", (int) lmagic);
148
149         ioctl(-1, EVIOCGRAB, lmagic);
150         printf("ioctl(-1, EVIOCGRAB, %lu) = -1 EBADF (%m)\n", lmagic);
151
152 # ifdef EVIOCREVOKE
153         ioctl(-1, EVIOCREVOKE, lmagic);
154         printf("ioctl(-1, EVIOCREVOKE, %lu) = -1 EBADF (%m)\n", lmagic);
155 # endif
156
157         const unsigned int size = get_page_size();
158         void *const page = tail_alloc(size);
159         init_magic(page, size);
160
161         int *const val_int = tail_alloc(sizeof(*val_int));
162         *val_int = magic;
163
164 # ifdef EVIOCSCLOCKID
165         ioctl(-1, EVIOCSCLOCKID, val_int);
166         printf("ioctl(-1, EVIOCSCLOCKID, [%u]) = -1 EBADF (%m)\n", *val_int);
167 # endif
168
169         int *pair_int = tail_alloc(sizeof(*pair_int) * 2);
170         pair_int[0] = 0xdeadbeef;
171         pair_int[1] = 0xbadc0ded;
172
173 # ifdef EVIOSGREP
174         ioctl(-1, EVIOCSREP, pair_int);
175         printf("ioctl(-1, EVIOCSREP, [%u, %u]) = -1 EBADF (%m)\n",
176                pair_int[0], pair_int[1]);
177 # endif
178
179         pair_int[1] = 1;
180         ioctl(-1, EVIOCSKEYCODE, pair_int);
181         printf("ioctl(-1, EVIOCSKEYCODE, [%u, %s]) = -1 EBADF (%m)\n",
182                pair_int[0], "KEY_ESC");
183
184 # ifdef EVIOCSKEYCODE_V2
185         struct input_keymap_entry *const ike = tail_alloc(sizeof(*ike));
186         init_magic(ike, sizeof(*ike));
187         ike->keycode = 2;
188
189         ioctl(-1, EVIOCSKEYCODE_V2, ike);
190         printf("ioctl(-1, EVIOCSKEYCODE_V2, {flags=%" PRIu8
191                ", len=%" PRIu8 ", ", ike->flags, ike->len);
192 #  ifdef VERBOSE_IOCTL
193         printf("index=%" PRIu16 ", keycode=%s, scancode=[",
194                ike->index, "KEY_1");
195         unsigned int i;
196         for (i = 0; i < ARRAY_SIZE(ike->scancode); ++i) {
197                 if (i > 0)
198                         printf(", ");
199                 printf("%" PRIx8, ike->scancode[i]);
200         }
201         printf("]");
202 #  else
203         printf("...");
204 #  endif
205         errno = EBADF;
206         printf("}) = -1 EBADF (%m)\n");
207 # endif
208
209         struct ff_effect *const ffe = tail_alloc(sizeof(*ffe));
210         init_magic(ffe, sizeof(*ffe));
211
212         ffe->type = FF_CONSTANT;
213         ioctl(-1, EVIOCSFF, ffe);
214         print_ffe_common(ffe, "FF_CONSTANT");
215
216 #  ifdef VERBOSE_IOCTL
217         printf(", constant={level=%hd", ffe->u.constant.level);
218         print_envelope(&ffe->u.constant.envelope);
219         printf("}");
220 #  else
221         printf("...");
222 #  endif
223         errno = EBADF;
224         printf("}) = -1 EBADF (%m)\n");
225
226 #  ifdef VERBOSE_IOCTL
227         ffe->type = FF_RAMP;
228         ioctl(-1, EVIOCSFF, ffe);
229         print_ffe_common(ffe, "FF_RAMP");
230         printf(", ramp={start_level=%hd, end_level=%hd",
231                ffe->u.ramp.start_level, ffe->u.ramp.end_level);
232         print_envelope(&ffe->u.ramp.envelope);
233         errno = EBADF;
234         printf("}}) = -1 EBADF (%m)\n");
235
236         ffe->type = FF_PERIODIC;
237         ioctl(-1, EVIOCSFF, ffe);
238         print_ffe_common(ffe, "FF_PERIODIC");
239         printf(", periodic={waveform=%hu, period=%hu, magnitude=%hd"
240                ", offset=%hd, phase=%hu",
241                ffe->u.periodic.waveform, ffe->u.periodic.period,
242                ffe->u.periodic.magnitude, ffe->u.periodic.offset,
243                ffe->u.periodic.phase);
244         print_envelope(&ffe->u.periodic.envelope);
245         printf(", custom_len=%u, custom_data=%p}",
246                ffe->u.periodic.custom_len, ffe->u.periodic.custom_data);
247         errno = EBADF;
248         printf("}) = -1 EBADF (%m)\n");
249
250         ffe->type = FF_RUMBLE;
251         ioctl(-1, EVIOCSFF, ffe);
252         print_ffe_common(ffe, "FF_RUMBLE");
253         printf(", rumble={strong_magnitude=%hu, weak_magnitude=%hu}",
254                ffe->u.rumble.strong_magnitude, ffe->u.rumble.weak_magnitude);
255         errno = EBADF;
256         printf("}) = -1 EBADF (%m)\n");
257
258         ffe->type = 0xff;
259         ioctl(-1, EVIOCSFF, ffe);
260         print_ffe_common(ffe, "0xff /* FF_??? */");
261         errno = EBADF;
262         printf("}) = -1 EBADF (%m)\n");
263 #  endif
264
265         ioctl(-1, _IOC(_IOC_READ, 0x45, 0x01, 0xff), lmagic);
266         printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
267                "_IOC(_IOC_READ, 0x45, 0x01, 0xff)", lmagic);
268
269         ioctl(-1, _IOC(_IOC_WRITE, 0x45, 0x01, 0xff), lmagic);
270         printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
271                "_IOC(_IOC_WRITE, 0x45, 0x01, 0xff)", lmagic);
272
273         ioctl(-1, _IOC(_IOC_READ|_IOC_WRITE, 0x45, 0xfe, 0xff), lmagic);
274         printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
275                "_IOC(_IOC_READ|_IOC_WRITE, 0x45, 0xfe, 0xff)", lmagic);
276
277         puts("+++ exited with 0 +++");
278         return 0;
279 }
280 #else
281
282 SKIP_MAIN_UNDEFINED("HAVE_LINUX_INPUT_H")
283
284 #endif