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"
37 compare(const void *a, const void *b)
39 const unsigned int code1 = (const uintptr_t) a;
40 const unsigned int code2 = ((struct_ioctlent *) b)->code;
41 return (code1 > code2) ? 1 : (code1 < code2) ? -1 : 0;
44 static const struct_ioctlent *
45 ioctl_lookup(const unsigned int code)
49 iop = bsearch((const void *) (const uintptr_t) code, ioctlent,
50 nioctlents, sizeof(ioctlent[0]), compare);
51 while (iop > ioctlent) {
53 if (iop->code != code) {
61 static const struct_ioctlent *
62 ioctl_next_match(const struct_ioctlent *iop)
64 const unsigned int code = iop->code;
66 if (iop < ioctlent + nioctlents && iop->code == code)
72 ioctl_print_code(const unsigned int code)
75 printflags(ioctl_dirs, _IOC_DIR(code), "_IOC_???");
76 tprintf(", %#x, %#x, %#x)",
77 _IOC_TYPE(code), _IOC_NR(code), _IOC_SIZE(code));
81 evdev_decode_number(const unsigned int code)
83 const unsigned int nr = _IOC_NR(code);
85 if (_IOC_DIR(code) == _IOC_WRITE) {
86 if (nr >= 0xc0 && nr <= 0xc0 + 0x3f) {
87 tprints("EVIOCSABS(");
88 printxval_indexn(evdev_abs, evdev_abs_size, nr - 0xc0,
95 if (_IOC_DIR(code) != _IOC_READ)
98 if (nr >= 0x20 && nr <= 0x20 + 0x1f) {
99 tprints("EVIOCGBIT(");
103 printxval(evdev_ev, nr - 0x20, "EV_???");
104 tprintf(", %u)", _IOC_SIZE(code));
106 } else if (nr >= 0x40 && nr <= 0x40 + 0x3f) {
107 tprints("EVIOCGABS(");
108 printxval_indexn(evdev_abs, evdev_abs_size, nr - 0x40,
114 switch (_IOC_NR(nr)) {
116 tprintf("EVIOCGNAME(%u)", _IOC_SIZE(code));
119 tprintf("EVIOCGPHYS(%u)", _IOC_SIZE(code));
122 tprintf("EVIOCGUNIQ(%u)", _IOC_SIZE(code));
125 tprintf("EVIOCGPROP(%u)", _IOC_SIZE(code));
128 tprintf("EVIOCGMTSLOTS(%u)", _IOC_SIZE(code));
131 tprintf("EVIOCGKEY(%u)", _IOC_SIZE(code));
134 tprintf("EVIOCGLED(%u)", _IOC_SIZE(code));
137 tprintf("EVIOCGSND(%u)", _IOC_SIZE(code));
140 tprintf("EVIOCGSW(%u)", _IOC_SIZE(code));
148 hiddev_decode_number(const unsigned int code)
150 if (_IOC_DIR(code) == _IOC_READ) {
151 switch (_IOC_NR(code)) {
153 tprintf("HIDIOCGRAWNAME(%u)", _IOC_SIZE(code));
156 tprintf("HIDIOCGRAWPHYS(%u)", _IOC_SIZE(code));
159 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
162 tprintf("HIDIOCGPHYS(%u)", _IOC_SIZE(code));
167 } else if (_IOC_DIR(code) == (_IOC_READ | _IOC_WRITE)) {
168 switch (_IOC_NR(code)) {
170 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
173 tprintf("HIDIOCGFEATURE(%u)", _IOC_SIZE(code));
184 ioctl_decode_command_number(struct tcb *tcp)
186 const unsigned int code = tcp->u_arg[1];
188 switch (_IOC_TYPE(code)) {
190 return evdev_decode_number(code);
192 return hiddev_decode_number(code);
194 if (_IOC_DIR(code) == _IOC_WRITE) {
195 tprintf("MIXER_WRITE(%u)", _IOC_NR(code));
197 } else if (_IOC_DIR(code) == _IOC_READ) {
198 tprintf("MIXER_READ(%u)", _IOC_NR(code));
203 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x2c) {
204 tprintf("UI_GET_SYSNAME(%u)", _IOC_SIZE(code));
209 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x13) {
210 tprintf("JSIOCGNAME(%u)", _IOC_SIZE(code));
215 if (_IOC_DIR(code) == _IOC_WRITE && _IOC_NR(code) == 0) {
216 tprintf("SPI_IOC_MESSAGE(%u)", _IOC_SIZE(code));
226 * Decode arg parameter of the ioctl call.
228 * @return There are two flags of the return value important for the purposes of
229 * processing by SYS_FUNC(ioctl):
230 * - RVAL_IOCTL_DECODED: indicates that ioctl decoder code
231 * has printed arg parameter;
232 * - RVAL_DECODED: indicates that decoding is done.
233 * As a result, the following behaviour is expected:
235 * - 0: decoding should be continued on exiting;
236 * - RVAL_IOCTL_DECODED: decoding on exiting is not needed
237 * and decoder has printed arg value;
238 * - RVAL_DECODED: decoding on exiting is not needed
239 * and generic handler should print arg value.
241 * - 0: generic handler should print arg value;
242 * - RVAL_IOCTL_DECODED: decoder has printed arg value.
244 * Note that it makes no sense to return just RVAL_DECODED on exiting,
245 * but, of course, it is not prohibited (for example, it may be useful
246 * in cases where the return path is common on entering and on exiting
249 * SYS_FUNC(ioctl) converts RVAL_IOCTL_DECODED flag to RVAL_DECODED,
250 * and passes all other bits of ioctl_decode return value unchanged.
253 ioctl_decode(struct tcb *tcp)
255 const unsigned int code = tcp->u_arg[1];
256 const kernel_ulong_t arg = tcp->u_arg[2];
258 switch (_IOC_TYPE(code)) {
260 return perf_ioctl(tcp, code, arg);
261 #if defined(ALPHA) || defined(POWERPC)
263 int ret = file_ioctl(tcp, code, arg);
264 if (ret != RVAL_DECODED)
266 ATTRIBUTE_FALLTHROUGH;
270 return term_ioctl(tcp, code, arg);
273 return file_ioctl(tcp, code, arg);
276 return term_ioctl(tcp, code, arg);
278 return sock_ioctl(tcp, code, arg);
280 return rtc_ioctl(tcp, code, arg);
282 return hdio_ioctl(tcp, code, arg);
284 return block_ioctl(tcp, code, arg);
286 return fs_x_ioctl(tcp, code, arg);
288 return scsi_ioctl(tcp, code, arg);
290 return loop_ioctl(tcp, code, arg);
291 #ifdef HAVE_STRUCT_MTD_WRITE_REQ
293 return mtd_ioctl(tcp, code, arg);
295 #ifdef HAVE_STRUCT_UBI_ATTACH_REQ_MAX_BEB_PER1024
298 return ubi_ioctl(tcp, code, arg);
301 return v4l2_ioctl(tcp, code, arg);
302 #ifdef HAVE_STRUCT_PTP_SYS_OFFSET
304 return ptp_ioctl(tcp, code, arg);
306 #ifdef HAVE_LINUX_INPUT_H
308 return evdev_ioctl(tcp, code, arg);
310 #ifdef HAVE_LINUX_USERFAULTFD_H
312 return uffdio_ioctl(tcp, code, arg);
314 #ifdef HAVE_LINUX_BTRFS_H
316 return btrfs_ioctl(tcp, code, arg);
319 return nsfs_ioctl(tcp, code, arg);
320 #ifdef HAVE_LINUX_DM_IOCTL_H
322 return dm_ioctl(tcp, code, arg);
324 #ifdef HAVE_LINUX_KVM_H
326 return kvm_ioctl(tcp, code, arg);
329 return inotify_ioctl(tcp, code, arg);
338 const struct_ioctlent *iop;
342 printfd(tcp, tcp->u_arg[0]);
345 if (xlat_verbosity != XLAT_STYLE_ABBREV)
346 tprintf("%#x", (unsigned int) tcp->u_arg[1]);
347 if (xlat_verbosity == XLAT_STYLE_VERBOSE)
349 if (xlat_verbosity != XLAT_STYLE_RAW) {
350 ret = ioctl_decode_command_number(tcp);
351 if (!(ret & IOCTL_NUMBER_STOP_LOOKUP)) {
352 iop = ioctl_lookup(tcp->u_arg[1]);
356 tprints(iop->symbol);
357 while ((iop = ioctl_next_match(iop)))
358 tprintf(" or %s", iop->symbol);
360 ioctl_print_code(tcp->u_arg[1]);
364 if (xlat_verbosity == XLAT_STYLE_VERBOSE)
367 ret = ioctl_decode(tcp);
369 ret = ioctl_decode(tcp) | RVAL_DECODED;
372 if (ret & RVAL_IOCTL_DECODED) {
373 ret &= ~RVAL_IOCTL_DECODED;
375 } else if (ret & RVAL_DECODED) {
376 tprintf(", %#" PRI_klx, tcp->u_arg[2]);