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-2018 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 #include "xlat/evdev_ev.h"
39 compare(const void *a, const void *b)
41 const unsigned int code1 = (const uintptr_t) a;
42 const unsigned int code2 = ((struct_ioctlent *) b)->code;
43 return (code1 > code2) ? 1 : (code1 < code2) ? -1 : 0;
46 static const struct_ioctlent *
47 ioctl_lookup(const unsigned int code)
51 iop = bsearch((const void *) (const uintptr_t) code, ioctlent,
52 nioctlents, sizeof(ioctlent[0]), compare);
53 while (iop > ioctlent) {
55 if (iop->code != code) {
63 static const struct_ioctlent *
64 ioctl_next_match(const struct_ioctlent *iop)
66 const unsigned int code = iop->code;
68 if (iop < ioctlent + nioctlents && iop->code == code)
74 ioctl_print_code(const unsigned int code)
77 printflags(ioctl_dirs, _IOC_DIR(code), "_IOC_???");
78 tprintf(", %#x, %#x, %#x)",
79 _IOC_TYPE(code), _IOC_NR(code), _IOC_SIZE(code));
83 evdev_decode_number(const unsigned int code)
85 const unsigned int nr = _IOC_NR(code);
87 if (_IOC_DIR(code) == _IOC_WRITE) {
88 if (nr >= 0xc0 && nr <= 0xc0 + 0x3f) {
89 tprints("EVIOCSABS(");
90 printxval_indexn(evdev_abs, evdev_abs_size, nr - 0xc0,
97 if (_IOC_DIR(code) != _IOC_READ)
100 if (nr >= 0x20 && nr <= 0x20 + 0x1f) {
101 tprints("EVIOCGBIT(");
102 printxval(evdev_ev, nr - 0x20, "EV_???");
103 tprintf(", %u)", _IOC_SIZE(code));
105 } else if (nr >= 0x40 && nr <= 0x40 + 0x3f) {
106 tprints("EVIOCGABS(");
107 printxval_indexn(evdev_abs, evdev_abs_size, nr - 0x40,
113 switch (_IOC_NR(nr)) {
115 tprintf("EVIOCGNAME(%u)", _IOC_SIZE(code));
118 tprintf("EVIOCGPHYS(%u)", _IOC_SIZE(code));
121 tprintf("EVIOCGUNIQ(%u)", _IOC_SIZE(code));
124 tprintf("EVIOCGPROP(%u)", _IOC_SIZE(code));
127 tprintf("EVIOCGMTSLOTS(%u)", _IOC_SIZE(code));
130 tprintf("EVIOCGKEY(%u)", _IOC_SIZE(code));
133 tprintf("EVIOCGLED(%u)", _IOC_SIZE(code));
136 tprintf("EVIOCGSND(%u)", _IOC_SIZE(code));
139 tprintf("EVIOCGSW(%u)", _IOC_SIZE(code));
147 hiddev_decode_number(const unsigned int code)
149 if (_IOC_DIR(code) == _IOC_READ) {
150 switch (_IOC_NR(code)) {
152 tprintf("HIDIOCGRAWNAME(%u)", _IOC_SIZE(code));
155 tprintf("HIDIOCGRAWPHYS(%u)", _IOC_SIZE(code));
158 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
161 tprintf("HIDIOCGPHYS(%u)", _IOC_SIZE(code));
166 } else if (_IOC_DIR(code) == (_IOC_READ | _IOC_WRITE)) {
167 switch (_IOC_NR(code)) {
169 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
172 tprintf("HIDIOCGFEATURE(%u)", _IOC_SIZE(code));
183 ioctl_decode_command_number(struct tcb *tcp)
185 const unsigned int code = tcp->u_arg[1];
187 switch (_IOC_TYPE(code)) {
189 return evdev_decode_number(code);
191 return hiddev_decode_number(code);
193 if (_IOC_DIR(code) == _IOC_WRITE) {
194 tprintf("MIXER_WRITE(%u)", _IOC_NR(code));
196 } else if (_IOC_DIR(code) == _IOC_READ) {
197 tprintf("MIXER_READ(%u)", _IOC_NR(code));
202 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x2c) {
203 tprintf("UI_GET_SYSNAME(%u)", _IOC_SIZE(code));
208 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x13) {
209 tprintf("JSIOCGNAME(%u)", _IOC_SIZE(code));
214 if (_IOC_DIR(code) == _IOC_WRITE && _IOC_NR(code) == 0) {
215 tprintf("SPI_IOC_MESSAGE(%u)", _IOC_SIZE(code));
225 * Decode arg parameter of the ioctl call.
227 * @return There are two flags of the return value important for the purposes of
228 * processing by SYS_FUNC(ioctl):
229 * - RVAL_IOCTL_DECODED: indicates that ioctl decoder code
230 * has printed arg parameter;
231 * - RVAL_DECODED: indicates that decoding is done.
232 * As a result, the following behaviour is expected:
234 * - 0: decoding should be continued on exiting;
235 * - RVAL_IOCTL_DECODED: decoding on exiting is not needed
236 * and decoder has printed arg value;
237 * - RVAL_DECODED: decoding on exiting is not needed
238 * and generic handler should print arg value.
240 * - 0: generic handler should print arg value;
241 * - RVAL_IOCTL_DECODED: decoder has printed arg value.
243 * Note that it makes no sense to return just RVAL_DECODED on exiting,
244 * but, of course, it is not prohibited (for example, it may be useful
245 * in cases where the return path is common on entering and on exiting
248 * SYS_FUNC(ioctl) converts RVAL_IOCTL_DECODED flag to RVAL_DECODED,
249 * and passes all other bits of ioctl_decode return value unchanged.
252 ioctl_decode(struct tcb *tcp)
254 const unsigned int code = tcp->u_arg[1];
255 const kernel_ulong_t arg = tcp->u_arg[2];
257 switch (_IOC_TYPE(code)) {
258 #if defined(ALPHA) || defined(POWERPC)
260 int ret = file_ioctl(tcp, code, arg);
261 if (ret != RVAL_DECODED)
263 ATTRIBUTE_FALLTHROUGH;
267 return term_ioctl(tcp, code, arg);
270 return file_ioctl(tcp, code, arg);
273 return term_ioctl(tcp, code, arg);
275 return sock_ioctl(tcp, code, arg);
277 return rtc_ioctl(tcp, code, arg);
279 return hdio_ioctl(tcp, code, arg);
281 return block_ioctl(tcp, code, arg);
283 return fs_x_ioctl(tcp, code, arg);
285 return scsi_ioctl(tcp, code, arg);
287 return loop_ioctl(tcp, code, arg);
288 #ifdef HAVE_STRUCT_MTD_WRITE_REQ
290 return mtd_ioctl(tcp, code, arg);
292 #ifdef HAVE_STRUCT_UBI_ATTACH_REQ_MAX_BEB_PER1024
295 return ubi_ioctl(tcp, code, arg);
298 return v4l2_ioctl(tcp, code, arg);
299 #ifdef HAVE_STRUCT_PTP_SYS_OFFSET
301 return ptp_ioctl(tcp, code, arg);
303 #ifdef HAVE_LINUX_INPUT_H
305 return evdev_ioctl(tcp, code, arg);
307 #ifdef HAVE_LINUX_USERFAULTFD_H
309 return uffdio_ioctl(tcp, code, arg);
311 #ifdef HAVE_LINUX_BTRFS_H
313 return btrfs_ioctl(tcp, code, arg);
316 return nsfs_ioctl(tcp, code, arg);
317 #ifdef HAVE_LINUX_DM_IOCTL_H
319 return dm_ioctl(tcp, code, arg);
321 #ifdef HAVE_LINUX_KVM_H
323 return kvm_ioctl(tcp, code, arg);
333 const struct_ioctlent *iop;
337 printfd(tcp, tcp->u_arg[0]);
340 if (xlat_verbosity != XLAT_STYLE_ABBREV)
341 tprintf("%#x", (unsigned int) tcp->u_arg[1]);
342 if (xlat_verbosity == XLAT_STYLE_VERBOSE)
344 if (xlat_verbosity != XLAT_STYLE_RAW) {
345 ret = ioctl_decode_command_number(tcp);
346 if (!(ret & IOCTL_NUMBER_STOP_LOOKUP)) {
347 iop = ioctl_lookup(tcp->u_arg[1]);
351 tprints(iop->symbol);
352 while ((iop = ioctl_next_match(iop)))
353 tprintf(" or %s", iop->symbol);
355 ioctl_print_code(tcp->u_arg[1]);
359 if (xlat_verbosity == XLAT_STYLE_VERBOSE)
362 ret = ioctl_decode(tcp);
364 ret = ioctl_decode(tcp) | RVAL_DECODED;
367 if (ret & RVAL_IOCTL_DECODED) {
368 ret &= ~RVAL_IOCTL_DECODED;
370 } else if (ret & RVAL_DECODED) {
371 tprintf(", %#" PRI_klx, tcp->u_arg[2]);