]> granicus.if.org Git - strace/blob - evdev.c
xlat: treat socketlayers as a sorted array
[strace] / evdev.c
1 /*
2  * Copyright (c) 2015 Etienne Gemsa <etienne.gemsa@lse.epita.fr>
3  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2015-2018 The strace developers.
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 "defs.h"
31
32 #include "xlat/evdev_abs.h"
33
34 #ifdef HAVE_LINUX_INPUT_H
35
36 # include <linux/ioctl.h>
37 # include <linux/input.h>
38
39 # include "xlat/evdev_autorepeat.h"
40 # include "xlat/evdev_ff_status.h"
41 # include "xlat/evdev_ff_types.h"
42 # include "xlat/evdev_keycode.h"
43 # include "xlat/evdev_leds.h"
44 # include "xlat/evdev_misc.h"
45 # include "xlat/evdev_mtslots.h"
46 # include "xlat/evdev_prop.h"
47 # include "xlat/evdev_relative_axes.h"
48 # include "xlat/evdev_snd.h"
49 # include "xlat/evdev_switch.h"
50 # include "xlat/evdev_sync.h"
51
52 # ifndef SYN_MAX
53 #  define SYN_MAX 0xf
54 # endif
55
56 const size_t evdev_abs_size = ARRAY_SIZE(evdev_abs) - 1;
57
58 static int
59 abs_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
60 {
61         tprints(", ");
62
63         struct input_absinfo absinfo;
64
65         if (!umove_or_printaddr(tcp, arg, &absinfo)) {
66                 tprintf("{value=%u"
67                         ", minimum=%u, ",
68                         absinfo.value,
69                         absinfo.minimum);
70
71                 if (!abbrev(tcp)) {
72                         tprintf("maximum=%u"
73                                 ", fuzz=%u"
74                                 ", flat=%u",
75                                 absinfo.maximum,
76                                 absinfo.fuzz,
77                                 absinfo.flat);
78 # ifdef HAVE_STRUCT_INPUT_ABSINFO_RESOLUTION
79                         tprintf(", resolution=%u",
80                                 absinfo.resolution);
81 # endif
82                 } else {
83                         tprints("...");
84                 }
85
86                 tprints("}");
87         }
88
89         return RVAL_IOCTL_DECODED;
90 }
91
92 static int
93 keycode_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
94 {
95         tprints(", ");
96
97         unsigned int keycode[2];
98
99         if (!umove_or_printaddr(tcp, arg, &keycode)) {
100                 tprintf("[%u, ", keycode[0]);
101                 printxval_index(evdev_keycode, keycode[1], "KEY_???");
102                 tprints("]");
103         }
104
105         return RVAL_IOCTL_DECODED;
106 }
107
108 # ifdef EVIOCGKEYCODE_V2
109 static int
110 keycode_V2_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
111 {
112         tprints(", ");
113
114         struct input_keymap_entry ike;
115
116         if (umove_or_printaddr(tcp, arg, &ike))
117                 return RVAL_IOCTL_DECODED;
118
119         tprintf("{flags=%" PRIu8
120                 ", len=%" PRIu8 ", ",
121                 ike.flags,
122                 ike.len);
123
124         if (!abbrev(tcp)) {
125                 unsigned int i;
126
127                 tprintf("index=%" PRIu16 ", keycode=", ike.index);
128                 printxval_index(evdev_keycode, ike.keycode, "KEY_???");
129                 tprints(", scancode=[");
130                 for (i = 0; i < ARRAY_SIZE(ike.scancode); i++) {
131                         if (i > 0)
132                                 tprints(", ");
133                         tprintf("%" PRIx8, ike.scancode[i]);
134                 }
135                 tprints("]");
136         } else {
137                 tprints("...");
138         }
139
140         tprints("}");
141
142         return RVAL_IOCTL_DECODED;
143 }
144 # endif /* EVIOCGKEYCODE_V2 */
145
146 static int
147 getid_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
148 {
149         tprints(", ");
150
151         struct input_id id;
152
153         if (!umove_or_printaddr(tcp, arg, &id))
154                 tprintf("{ID_BUS=%" PRIu16
155                         ", ID_VENDOR=%" PRIu16
156                         ", ID_PRODUCT=%" PRIu16
157                         ", ID_VERSION=%" PRIu16 "}",
158                         id.bustype,
159                         id.vendor,
160                         id.product,
161                         id.version);
162
163         return RVAL_IOCTL_DECODED;
164 }
165
166 enum xlat_type {
167         XT_SORTED,
168         XT_INDEXED,
169 };
170
171 static void
172 printxval_dispatch(const struct xlat *xlat, size_t xlat_size, uint64_t val,
173                    const char *dflt, enum xlat_type xt)
174 {
175         switch (xt) {
176         case XT_SORTED:
177                 printxval_searchn(xlat, xlat_size, val, dflt);
178                 break;
179
180         case XT_INDEXED:
181                 printxval_indexn(xlat, xlat_size, val, dflt);
182                 break;
183         }
184 }
185
186 static int
187 decode_bitset_(struct tcb *const tcp, const kernel_ulong_t arg,
188                const struct xlat decode_nr[], const unsigned int max_nr,
189                const char *const dflt, size_t decode_nr_size, enum xlat_type xt)
190 {
191         tprints(", ");
192
193         unsigned int size;
194         if ((kernel_ulong_t) tcp->u_rval > max_nr)
195                 size = max_nr;
196         else
197                 size = tcp->u_rval;
198         char decoded_arg[size];
199
200         if (umove_or_printaddr(tcp, arg, &decoded_arg))
201                 return RVAL_IOCTL_DECODED;
202
203         tprints("[");
204
205         int bit_displayed = 0;
206         int i = next_set_bit(decoded_arg, 0, size);
207         if (i < 0) {
208                 tprints(" 0 ");
209         } else {
210                 printxval_dispatch(decode_nr, decode_nr_size, i, dflt, xt);
211
212                 while ((i = next_set_bit(decoded_arg, i + 1, size)) > 0) {
213                         if (abbrev(tcp) && bit_displayed >= 3) {
214                                 tprints(", ...");
215                                 break;
216                         }
217                         tprints(", ");
218                         printxval_dispatch(decode_nr, decode_nr_size, i, dflt,
219                                            xt);
220                         bit_displayed++;
221                 }
222         }
223
224         tprints("]");
225
226         return RVAL_IOCTL_DECODED;
227 }
228
229 #define decode_bitset(tcp_, arg_, decode_nr_, max_nr_, dflt_, xt_) \
230         decode_bitset_((tcp_), (arg_), (decode_nr_), (max_nr_), \
231                        (dflt_), ARRAY_SIZE(decode_nr_), (xt_))
232
233 # ifdef EVIOCGMTSLOTS
234 static int
235 mtslots_ioctl(struct tcb *const tcp, const unsigned int code,
236               const kernel_ulong_t arg)
237 {
238         tprints(", ");
239
240         const size_t size = _IOC_SIZE(code) / sizeof(int);
241         if (!size) {
242                 printaddr(arg);
243                 return RVAL_IOCTL_DECODED;
244         }
245
246         int buffer[size];
247
248         if (umove_or_printaddr(tcp, arg, &buffer))
249                 return RVAL_IOCTL_DECODED;
250
251         tprints("{code=");
252         printxval(evdev_mtslots, buffer[0], "ABS_MT_???");
253
254         tprints(", values=[");
255
256         unsigned int i;
257         for (i = 1; i < ARRAY_SIZE(buffer); i++)
258                 tprintf("%s%d", i > 1 ? ", " : "", buffer[i]);
259
260         tprints("]}");
261
262         return RVAL_IOCTL_DECODED;
263 }
264 # endif /* EVIOCGMTSLOTS */
265
266 # if defined EVIOCGREP || defined EVIOCSREP
267 static int
268 repeat_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
269 {
270         tprints(", ");
271         printpair_int(tcp, arg, "%u");
272         return RVAL_IOCTL_DECODED;
273 }
274 # endif /* EVIOCGREP || EVIOCSREP */
275
276 static int
277 bit_ioctl(struct tcb *const tcp, const unsigned int ev_nr,
278           const kernel_ulong_t arg)
279 {
280         switch (ev_nr) {
281                 case EV_SYN:
282                         return decode_bitset(tcp, arg, evdev_sync,
283                                              SYN_MAX, "SYN_???", XT_INDEXED);
284                 case EV_KEY:
285                         return decode_bitset(tcp, arg, evdev_keycode,
286                                              KEY_MAX, "KEY_???", XT_INDEXED);
287                 case EV_REL:
288                         return decode_bitset(tcp, arg, evdev_relative_axes,
289                                              REL_MAX, "REL_???", XT_INDEXED);
290                 case EV_ABS:
291                         return decode_bitset(tcp, arg, evdev_abs,
292                                              ABS_MAX, "ABS_???", XT_INDEXED);
293                 case EV_MSC:
294                         return decode_bitset(tcp, arg, evdev_misc,
295                                              MSC_MAX, "MSC_???", XT_INDEXED);
296                 case EV_SW:
297                         return decode_bitset(tcp, arg, evdev_switch,
298                                              SW_MAX, "SW_???", XT_INDEXED);
299                 case EV_LED:
300                         return decode_bitset(tcp, arg, evdev_leds,
301                                              LED_MAX, "LED_???", XT_INDEXED);
302                 case EV_SND:
303                         return decode_bitset(tcp, arg, evdev_snd,
304                                              SND_MAX, "SND_???", XT_INDEXED);
305                 case EV_REP:
306                         return decode_bitset(tcp, arg, evdev_autorepeat,
307                                              REP_MAX, "REP_???", XT_INDEXED);
308                 case EV_FF:
309                         return decode_bitset(tcp, arg, evdev_ff_types,
310                                              FF_MAX, "FF_???", XT_SORTED);
311                 case EV_PWR:
312                         tprints(", ");
313                         printnum_int(tcp, arg, "%d");
314                         return RVAL_IOCTL_DECODED;
315                 case EV_FF_STATUS:
316                         return decode_bitset(tcp, arg, evdev_ff_status,
317                                              FF_STATUS_MAX, "FF_STATUS_???",
318                                              XT_INDEXED);
319                 default:
320                         tprints(", ");
321                         printaddr(arg);
322                         return RVAL_IOCTL_DECODED;
323         }
324 }
325
326 static int
327 evdev_read_ioctl(struct tcb *const tcp, const unsigned int code,
328                  const kernel_ulong_t arg)
329 {
330         /* fixed-number fixed-length commands */
331         switch (code) {
332                 case EVIOCGVERSION:
333                         tprints(", ");
334                         printnum_int(tcp, arg, "%#x");
335                         return RVAL_IOCTL_DECODED;
336                 case EVIOCGEFFECTS:
337                         tprints(", ");
338                         printnum_int(tcp, arg, "%u");
339                         return RVAL_IOCTL_DECODED;
340                 case EVIOCGID:
341                         return getid_ioctl(tcp, arg);
342 # ifdef EVIOCGREP
343                 case EVIOCGREP:
344                         return repeat_ioctl(tcp, arg);
345 # endif
346                 case EVIOCGKEYCODE:
347                         return keycode_ioctl(tcp, arg);
348 # ifdef EVIOCGKEYCODE_V2
349                 case EVIOCGKEYCODE_V2:
350                         return keycode_V2_ioctl(tcp, arg);
351 # endif
352         }
353
354         /* fixed-number variable-length commands */
355         switch (_IOC_NR(code)) {
356 # ifdef EVIOCGMTSLOTS
357                 case _IOC_NR(EVIOCGMTSLOTS(0)):
358                         return mtslots_ioctl(tcp, code, arg);
359 # endif
360                 case _IOC_NR(EVIOCGNAME(0)):
361                 case _IOC_NR(EVIOCGPHYS(0)):
362                 case _IOC_NR(EVIOCGUNIQ(0)):
363                         tprints(", ");
364                         if (syserror(tcp))
365                                 printaddr(arg);
366                         else
367                                 printstrn(tcp, arg, tcp->u_rval);
368                         return RVAL_IOCTL_DECODED;
369 # ifdef EVIOCGPROP
370                 case _IOC_NR(EVIOCGPROP(0)):
371                         return decode_bitset(tcp, arg, evdev_prop,
372                                              INPUT_PROP_MAX, "PROP_???",
373                                              XT_INDEXED);
374 # endif
375                 case _IOC_NR(EVIOCGSND(0)):
376                         return decode_bitset(tcp, arg, evdev_snd,
377                                              SND_MAX, "SND_???", XT_INDEXED);
378 # ifdef EVIOCGSW
379                 case _IOC_NR(EVIOCGSW(0)):
380                         return decode_bitset(tcp, arg, evdev_switch,
381                                              SW_MAX, "SW_???", XT_INDEXED);
382 # endif
383                 case _IOC_NR(EVIOCGKEY(0)):
384                         return decode_bitset(tcp, arg, evdev_keycode,
385                                              KEY_MAX, "KEY_???", XT_INDEXED);
386                 case _IOC_NR(EVIOCGLED(0)):
387                         return decode_bitset(tcp, arg, evdev_leds,
388                                              LED_MAX, "LED_???", XT_INDEXED);
389         }
390
391         /* multi-number fixed-length commands */
392         if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
393                 return abs_ioctl(tcp, arg);
394
395         /* multi-number variable-length commands */
396         if ((_IOC_NR(code) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
397                 return bit_ioctl(tcp, _IOC_NR(code) & EV_MAX, arg);
398
399         return 0;
400 }
401
402 static int
403 evdev_write_ioctl(struct tcb *const tcp, const unsigned int code,
404                   const kernel_ulong_t arg)
405 {
406         /* fixed-number fixed-length commands */
407         switch (code) {
408 # ifdef EVIOCSREP
409                 case EVIOCSREP:
410                         return repeat_ioctl(tcp, arg);
411 # endif
412                 case EVIOCSKEYCODE:
413                         return keycode_ioctl(tcp, arg);
414 # ifdef EVIOCSKEYCODE_V2
415                 case EVIOCSKEYCODE_V2:
416                         return keycode_V2_ioctl(tcp, arg);
417 # endif
418                 case EVIOCRMFF:
419                         tprintf(", %d", (int) arg);
420                         return RVAL_IOCTL_DECODED;
421                 case EVIOCGRAB:
422 # ifdef EVIOCREVOKE
423                 case EVIOCREVOKE:
424 # endif
425                         tprintf(", %" PRI_klu, arg);
426                         return RVAL_IOCTL_DECODED;
427 # ifdef EVIOCSCLOCKID
428                 case EVIOCSCLOCKID:
429                         tprints(", ");
430                         printnum_int(tcp, arg, "%u");
431                         return RVAL_IOCTL_DECODED;
432 # endif
433                 default: {
434                         int rc = evdev_write_ioctl_mpers(tcp, code, arg);
435
436                         if (rc != RVAL_DECODED)
437                                 return rc;
438                 }
439         }
440
441         /* multi-number fixed-length commands */
442         if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0)))
443                 return abs_ioctl(tcp, arg);
444
445         return 0;
446 }
447
448 void
449 print_evdev_ff_type(const kernel_ulong_t val)
450 {
451         printxval(evdev_ff_types, val, "FF_???");
452 }
453
454 int
455 evdev_ioctl(struct tcb *const tcp,
456             const unsigned int code, const kernel_ulong_t arg)
457 {
458         switch (_IOC_DIR(code)) {
459                 case _IOC_READ:
460                         if (entering(tcp))
461                                 return 0;
462                         return evdev_read_ioctl(tcp, code, arg);
463                 case _IOC_WRITE:
464                         return evdev_write_ioctl(tcp, code, arg) | RVAL_DECODED;
465                 default:
466                         return RVAL_DECODED;
467         }
468 }
469
470 #endif /* HAVE_LINUX_INPUT_H */