2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-2001 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999-2017 The strace developers.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <linux/ioctl.h>
34 #include "xlat/ioctl_dirs.h"
36 #ifdef HAVE_LINUX_INPUT_H
37 # include <linux/input.h>
40 #include "xlat/evdev_abs.h"
41 #include "xlat/evdev_ev.h"
44 compare(const void *a, const void *b)
46 const unsigned int code1 = (const uintptr_t) a;
47 const unsigned int code2 = ((struct_ioctlent *) b)->code;
48 return (code1 > code2) ? 1 : (code1 < code2) ? -1 : 0;
51 static const struct_ioctlent *
52 ioctl_lookup(const unsigned int code)
56 iop = bsearch((const void *) (const uintptr_t) code, ioctlent,
57 nioctlents, sizeof(ioctlent[0]), compare);
58 while (iop > ioctlent) {
60 if (iop->code != code) {
68 static const struct_ioctlent *
69 ioctl_next_match(const struct_ioctlent *iop)
71 const unsigned int code = iop->code;
73 if (iop < ioctlent + nioctlents && iop->code == code)
79 ioctl_print_code(const unsigned int code)
82 printflags(ioctl_dirs, _IOC_DIR(code), "_IOC_???");
83 tprintf(", %#x, %#x, %#x)",
84 _IOC_TYPE(code), _IOC_NR(code), _IOC_SIZE(code));
88 evdev_decode_number(const unsigned int code)
90 const unsigned int nr = _IOC_NR(code);
92 if (_IOC_DIR(code) == _IOC_WRITE) {
93 if (nr >= 0xc0 && nr <= 0xc0 + 0x3f) {
94 tprints("EVIOCSABS(");
95 printxval(evdev_abs, nr - 0xc0, "ABS_???");
101 if (_IOC_DIR(code) != _IOC_READ)
104 if (nr >= 0x20 && nr <= 0x20 + 0x1f) {
105 tprints("EVIOCGBIT(");
106 printxval(evdev_ev, nr - 0x20, "EV_???");
107 tprintf(", %u)", _IOC_SIZE(code));
109 } else if (nr >= 0x40 && nr <= 0x40 + 0x3f) {
110 tprints("EVIOCGABS(");
111 printxval(evdev_abs, nr - 0x40, "ABS_???");
116 switch (_IOC_NR(nr)) {
118 tprintf("EVIOCGNAME(%u)", _IOC_SIZE(code));
121 tprintf("EVIOCGPHYS(%u)", _IOC_SIZE(code));
124 tprintf("EVIOCGUNIQ(%u)", _IOC_SIZE(code));
127 tprintf("EVIOCGPROP(%u)", _IOC_SIZE(code));
130 tprintf("EVIOCGMTSLOTS(%u)", _IOC_SIZE(code));
133 tprintf("EVIOCGKEY(%u)", _IOC_SIZE(code));
136 tprintf("EVIOCGLED(%u)", _IOC_SIZE(code));
139 tprintf("EVIOCGSND(%u)", _IOC_SIZE(code));
142 tprintf("EVIOCGSW(%u)", _IOC_SIZE(code));
150 hiddev_decode_number(const unsigned int code)
152 if (_IOC_DIR(code) == _IOC_READ) {
153 switch (_IOC_NR(code)) {
155 tprintf("HIDIOCGRAWNAME(%u)", _IOC_SIZE(code));
158 tprintf("HIDIOCGRAWPHYS(%u)", _IOC_SIZE(code));
161 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
164 tprintf("HIDIOCGPHYS(%u)", _IOC_SIZE(code));
169 } else if (_IOC_DIR(code) == (_IOC_READ | _IOC_WRITE)) {
170 switch (_IOC_NR(code)) {
172 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
175 tprintf("HIDIOCGFEATURE(%u)", _IOC_SIZE(code));
186 ioctl_decode_command_number(struct tcb *tcp)
188 const unsigned int code = tcp->u_arg[1];
190 switch (_IOC_TYPE(code)) {
192 return evdev_decode_number(code);
194 return hiddev_decode_number(code);
196 if (_IOC_DIR(code) == _IOC_WRITE) {
197 tprintf("MIXER_WRITE(%u)", _IOC_NR(code));
199 } else if (_IOC_DIR(code) == _IOC_READ) {
200 tprintf("MIXER_READ(%u)", _IOC_NR(code));
205 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x2c) {
206 tprintf("UI_GET_SYSNAME(%u)", _IOC_SIZE(code));
211 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x13) {
212 tprintf("JSIOCGNAME(%u)", _IOC_SIZE(code));
217 if (_IOC_DIR(code) == _IOC_WRITE && _IOC_NR(code) == 0) {
218 tprintf("SPI_IOC_MESSAGE(%u)", _IOC_SIZE(code));
228 ioctl_decode(struct tcb *tcp)
230 const unsigned int code = tcp->u_arg[1];
231 const kernel_ulong_t arg = tcp->u_arg[2];
233 switch (_IOC_TYPE(code)) {
234 #if defined(ALPHA) || defined(POWERPC)
236 int ret = file_ioctl(tcp, code, arg);
237 if (ret != RVAL_DECODED)
242 return term_ioctl(tcp, code, arg);
245 return file_ioctl(tcp, code, arg);
248 return term_ioctl(tcp, code, arg);
250 return sock_ioctl(tcp, code, arg);
252 return rtc_ioctl(tcp, code, arg);
254 return hdio_ioctl(tcp, code, arg);
256 return block_ioctl(tcp, code, arg);
258 return fs_x_ioctl(tcp, code, arg);
260 return scsi_ioctl(tcp, code, arg);
262 return loop_ioctl(tcp, code, arg);
264 return mtd_ioctl(tcp, code, arg);
267 return ubi_ioctl(tcp, code, arg);
269 return v4l2_ioctl(tcp, code, arg);
271 return ptp_ioctl(tcp, code, arg);
272 #ifdef HAVE_LINUX_INPUT_H
274 return evdev_ioctl(tcp, code, arg);
276 #ifdef HAVE_LINUX_USERFAULTFD_H
278 return uffdio_ioctl(tcp, code, arg);
280 #ifdef HAVE_LINUX_BTRFS_H
282 return btrfs_ioctl(tcp, code, arg);
285 return nsfs_ioctl(tcp, code, arg);
286 #ifdef HAVE_LINUX_DM_IOCTL_H
288 return dm_ioctl(tcp, code, arg);
298 const struct_ioctlent *iop;
302 printfd(tcp, tcp->u_arg[0]);
304 ret = ioctl_decode_command_number(tcp);
305 if (!(ret & IOCTL_NUMBER_STOP_LOOKUP)) {
306 iop = ioctl_lookup(tcp->u_arg[1]);
310 tprints(iop->symbol);
311 while ((iop = ioctl_next_match(iop)))
312 tprintf(" or %s", iop->symbol);
314 ioctl_print_code(tcp->u_arg[1]);
317 ret = ioctl_decode(tcp);
319 ret = ioctl_decode(tcp) | RVAL_DECODED;
322 if (ret & RVAL_DECODED) {
323 ret &= ~RVAL_DECODED;
327 tprintf(", %#" PRI_klx, tcp->u_arg[2]);