blob: 264c7c02c71cafdd49d8b5baf189daf68d9b69ea [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
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>
Wichert Akkermanc6c1ea52001-08-03 21:52:13 +00005 * Copyright (c) 1996-2001 Wichert Akkerman <wichert@cistron.nl>
Elliott Hughes39bac052017-05-25 16:56:11 -07006 * Copyright (c) 1999-2017 The strace developers.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00007 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
19 *
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.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000030 */
31
32#include "defs.h"
Felix Janda3460dc42015-03-28 18:40:13 +010033#include <linux/ioctl.h>
Dmitry V. Levin924996a2015-01-20 00:24:36 +000034#include "xlat/ioctl_dirs.h"
Wichert Akkermana9667852001-03-17 17:26:34 +000035
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010036#ifdef HAVE_LINUX_INPUT_H
37# include <linux/input.h>
38#endif
39
40#include "xlat/evdev_abs.h"
41#include "xlat/evdev_ev.h"
42
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000043static int
Denys Vlasenko12014262011-05-30 14:00:14 +020044compare(const void *a, const void *b)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000045{
Elliott Hughesd35df492017-02-15 15:19:05 -080046 const unsigned int code1 = (const uintptr_t) a;
Dmitry V. Levinc7afb482015-01-19 18:44:21 +000047 const unsigned int code2 = ((struct_ioctlent *) b)->code;
Denys Vlasenkofeb40c42012-03-20 13:11:51 +010048 return (code1 > code2) ? 1 : (code1 < code2) ? -1 : 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000049}
50
Dmitry V. Levin044eef22015-07-02 21:37:23 +000051static const struct_ioctlent *
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +000052ioctl_lookup(const unsigned int code)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000053{
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010054 struct_ioctlent *iop;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000055
Elliott Hughesd35df492017-02-15 15:19:05 -080056 iop = bsearch((const void *) (const uintptr_t) code, ioctlent,
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010057 nioctlents, sizeof(ioctlent[0]), compare);
58 while (iop > ioctlent) {
59 iop--;
60 if (iop->code != code) {
Roland McGrath2843a4e2003-11-14 02:54:03 +000061 iop++;
62 break;
63 }
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010064 }
Roland McGrath2843a4e2003-11-14 02:54:03 +000065 return iop;
66}
67
Dmitry V. Levin044eef22015-07-02 21:37:23 +000068static const struct_ioctlent *
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010069ioctl_next_match(const struct_ioctlent *iop)
Roland McGrath2843a4e2003-11-14 02:54:03 +000070{
Dmitry V. Levinc7afb482015-01-19 18:44:21 +000071 const unsigned int code = iop->code;
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010072 iop++;
Roland McGrath2843a4e2003-11-14 02:54:03 +000073 if (iop < ioctlent + nioctlents && iop->code == code)
74 return iop;
75 return NULL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000076}
77
Dmitry V. Levin044eef22015-07-02 21:37:23 +000078static void
Dmitry V. Levin924996a2015-01-20 00:24:36 +000079ioctl_print_code(const unsigned int code)
80{
81 tprints("_IOC(");
82 printflags(ioctl_dirs, _IOC_DIR(code), "_IOC_???");
Elliott Hughesd35df492017-02-15 15:19:05 -080083 tprintf(", %#x, %#x, %#x)",
Dmitry V. Levin924996a2015-01-20 00:24:36 +000084 _IOC_TYPE(code), _IOC_NR(code), _IOC_SIZE(code));
85}
86
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010087static int
Dmitry V. Levin2803e752015-07-02 22:23:53 +000088evdev_decode_number(const unsigned int code)
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010089{
Dmitry V. Levin2803e752015-07-02 22:23:53 +000090 const unsigned int nr = _IOC_NR(code);
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010091
Dmitry V. Levin2803e752015-07-02 22:23:53 +000092 if (_IOC_DIR(code) == _IOC_WRITE) {
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010093 if (nr >= 0xc0 && nr <= 0xc0 + 0x3f) {
94 tprints("EVIOCSABS(");
Dmitry V. Levin1f1559e2016-05-27 01:11:40 +000095 printxval(evdev_abs, nr - 0xc0, "ABS_???");
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010096 tprints(")");
97 return 1;
98 }
99 }
100
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000101 if (_IOC_DIR(code) != _IOC_READ)
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100102 return 0;
103
104 if (nr >= 0x20 && nr <= 0x20 + 0x1f) {
105 tprints("EVIOCGBIT(");
106 printxval(evdev_ev, nr - 0x20, "EV_???");
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000107 tprintf(", %u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100108 return 1;
109 } else if (nr >= 0x40 && nr <= 0x40 + 0x3f) {
110 tprints("EVIOCGABS(");
111 printxval(evdev_abs, nr - 0x40, "ABS_???");
112 tprints(")");
113 return 1;
114 }
115
116 switch (_IOC_NR(nr)) {
117 case 0x06:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000118 tprintf("EVIOCGNAME(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100119 return 1;
120 case 0x07:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000121 tprintf("EVIOCGPHYS(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100122 return 1;
123 case 0x08:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000124 tprintf("EVIOCGUNIQ(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100125 return 1;
126 case 0x09:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000127 tprintf("EVIOCGPROP(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100128 return 1;
129 case 0x0a:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000130 tprintf("EVIOCGMTSLOTS(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100131 return 1;
132 case 0x18:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000133 tprintf("EVIOCGKEY(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100134 return 1;
135 case 0x19:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000136 tprintf("EVIOCGLED(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100137 return 1;
138 case 0x1a:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000139 tprintf("EVIOCGSND(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100140 return 1;
141 case 0x1b:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000142 tprintf("EVIOCGSW(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100143 return 1;
144 default:
145 return 0;
146 }
147}
148
149static int
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000150hiddev_decode_number(const unsigned int code)
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100151{
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000152 if (_IOC_DIR(code) == _IOC_READ) {
153 switch (_IOC_NR(code)) {
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100154 case 0x04:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000155 tprintf("HIDIOCGRAWNAME(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100156 return 1;
157 case 0x05:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000158 tprintf("HIDIOCGRAWPHYS(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100159 return 1;
160 case 0x06:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000161 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100162 return 1;
163 case 0x12:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000164 tprintf("HIDIOCGPHYS(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100165 return 1;
166 default:
167 return 0;
168 }
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000169 } else if (_IOC_DIR(code) == (_IOC_READ | _IOC_WRITE)) {
170 switch (_IOC_NR(code)) {
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100171 case 0x06:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000172 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100173 return 1;
174 case 0x07:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000175 tprintf("HIDIOCGFEATURE(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100176 return 1;
177 default:
178 return 0;
179 }
180 }
181
182 return 0;
183}
184
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000185static int
Dmitry V. Levin4f63c112015-07-02 22:31:11 +0000186ioctl_decode_command_number(struct tcb *tcp)
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100187{
Dmitry V. Levin4f63c112015-07-02 22:31:11 +0000188 const unsigned int code = tcp->u_arg[1];
189
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000190 switch (_IOC_TYPE(code)) {
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100191 case 'E':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000192 return evdev_decode_number(code);
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100193 case 'H':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000194 return hiddev_decode_number(code);
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100195 case 'M':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000196 if (_IOC_DIR(code) == _IOC_WRITE) {
197 tprintf("MIXER_WRITE(%u)", _IOC_NR(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100198 return 1;
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000199 } else if (_IOC_DIR(code) == _IOC_READ) {
200 tprintf("MIXER_READ(%u)", _IOC_NR(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100201 return 1;
202 }
203 return 0;
204 case 'U':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000205 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x2c) {
206 tprintf("UI_GET_SYSNAME(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100207 return 1;
208 }
209 return 0;
210 case 'j':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000211 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x13) {
212 tprintf("JSIOCGNAME(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100213 return 1;
214 }
215 return 0;
216 case 'k':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000217 if (_IOC_DIR(code) == _IOC_WRITE && _IOC_NR(code) == 0) {
218 tprintf("SPI_IOC_MESSAGE(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100219 return 1;
220 }
221 return 0;
222 default:
223 return 0;
224 }
225}
226
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000227static int
Dmitry V. Levin802c9422015-07-02 22:27:51 +0000228ioctl_decode(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000229{
Dmitry V. Levin802c9422015-07-02 22:27:51 +0000230 const unsigned int code = tcp->u_arg[1];
Elliott Hughesd35df492017-02-15 15:19:05 -0800231 const kernel_ulong_t arg = tcp->u_arg[2];
Dmitry V. Levin802c9422015-07-02 22:27:51 +0000232
Dmitry V. Levinc7afb482015-01-19 18:44:21 +0000233 switch (_IOC_TYPE(code)) {
Roland McGrathbb3af522003-01-14 07:53:34 +0000234#if defined(ALPHA) || defined(POWERPC)
Dmitry V. Levin25f37582016-05-24 00:19:03 +0000235 case 'f': {
236 int ret = file_ioctl(tcp, code, arg);
Jeff Mahoney398fe042016-05-18 18:09:40 -0400237 if (ret != RVAL_DECODED)
238 return ret;
Dmitry V. Levin25f37582016-05-24 00:19:03 +0000239 }
240 case 't':
Jeff Mahoney398fe042016-05-18 18:09:40 -0400241 case 'T':
242 return term_ioctl(tcp, code, arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000243#else /* !ALPHA */
Jeff Mahoney398fe042016-05-18 18:09:40 -0400244 case 'f':
245 return file_ioctl(tcp, code, arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000246 case 0x54:
247#endif /* !ALPHA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000248 return term_ioctl(tcp, code, arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000249 case 0x89:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000250 return sock_ioctl(tcp, code, arg);
Roland McGrathd83c50b2004-10-06 22:27:43 +0000251 case 'p':
252 return rtc_ioctl(tcp, code, arg);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000253 case 0x03:
Dmitry V. Levinb3f00402016-05-26 00:07:17 +0000254 return hdio_ioctl(tcp, code, arg);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000255 case 0x12:
256 return block_ioctl(tcp, code, arg);
Dmitry V. Levin4dd9f3f2016-05-26 00:37:26 +0000257 case 'X':
258 return fs_x_ioctl(tcp, code, arg);
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000259 case 0x22:
260 return scsi_ioctl(tcp, code, arg);
Mike Frysingerebee04c2012-04-17 22:19:31 -0400261 case 'L':
262 return loop_ioctl(tcp, code, arg);
Mike Frysinger0cbed352012-04-04 22:22:01 -0400263 case 'M':
264 return mtd_ioctl(tcp, code, arg);
Mike Frysingerd648f292013-05-01 23:35:30 -0400265 case 'o':
266 case 'O':
267 return ubi_ioctl(tcp, code, arg);
Philippe De Muyter0cc96142014-11-03 21:27:40 +0100268 case 'V':
269 return v4l2_ioctl(tcp, code, arg);
Stefan Sørensenb88a6f82014-01-31 12:01:01 +0100270 case '=':
271 return ptp_ioctl(tcp, code, arg);
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100272#ifdef HAVE_LINUX_INPUT_H
273 case 'E':
274 return evdev_ioctl(tcp, code, arg);
275#endif
Dr. David Alan Gilbert5d1216a2016-05-10 11:49:02 +0100276#ifdef HAVE_LINUX_USERFAULTFD_H
277 case 0xaa:
278 return uffdio_ioctl(tcp, code, arg);
279#endif
Jeff Mahoney8cc69622016-05-18 18:09:39 -0400280#ifdef HAVE_LINUX_BTRFS_H
281 case 0x94:
282 return btrfs_ioctl(tcp, code, arg);
283#endif
Elliott Hughes39bac052017-05-25 16:56:11 -0700284 case 0xb7:
285 return nsfs_ioctl(tcp, code, arg);
Elliott Hughesd35df492017-02-15 15:19:05 -0800286#ifdef HAVE_LINUX_DM_IOCTL_H
287 case 0xfd:
288 return dm_ioctl(tcp, code, arg);
289#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000290 default:
291 break;
292 }
293 return 0;
294}
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000295
296SYS_FUNC(ioctl)
297{
298 const struct_ioctlent *iop;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000299 int ret;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000300
301 if (entering(tcp)) {
302 printfd(tcp, tcp->u_arg[0]);
303 tprints(", ");
Gabriel Laskar9c4fc342015-09-23 10:11:55 +0200304 ret = ioctl_decode_command_number(tcp);
Gabriel Laskar8b6046a2015-12-04 01:07:33 +0100305 if (!(ret & IOCTL_NUMBER_STOP_LOOKUP)) {
306 iop = ioctl_lookup(tcp->u_arg[1]);
307 if (iop) {
308 if (ret)
309 tprints(" or ");
310 tprints(iop->symbol);
311 while ((iop = ioctl_next_match(iop)))
312 tprintf(" or %s", iop->symbol);
313 } else if (!ret) {
314 ioctl_print_code(tcp->u_arg[1]);
315 }
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000316 }
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000317 ret = ioctl_decode(tcp);
318 } else {
319 ret = ioctl_decode(tcp) | RVAL_DECODED;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000320 }
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000321
322 if (ret & RVAL_DECODED) {
323 ret &= ~RVAL_DECODED;
324 if (ret)
325 --ret;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000326 else
Elliott Hughesd35df492017-02-15 15:19:05 -0800327 tprintf(", %#" PRI_klx, tcp->u_arg[2]);
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000328 ret |= RVAL_DECODED;
329 } else {
330 if (ret)
331 --ret;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000332 }
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000333
334 return ret;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000335}