blob: e4b20d97c7445c4cc12c06b06c960149f19f59c5 [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>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Felix Janda3460dc42015-03-28 18:40:13 +010032#include <linux/ioctl.h>
Dmitry V. Levin924996a2015-01-20 00:24:36 +000033#include "xlat/ioctl_dirs.h"
Wichert Akkermana9667852001-03-17 17:26:34 +000034
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010035#ifdef HAVE_LINUX_INPUT_H
36# include <linux/input.h>
37#endif
38
39#include "xlat/evdev_abs.h"
40#include "xlat/evdev_ev.h"
41
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000042static int
Denys Vlasenko12014262011-05-30 14:00:14 +020043compare(const void *a, const void *b)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000044{
Dmitry V. Levinc7afb482015-01-19 18:44:21 +000045 const unsigned int code1 = (const unsigned long) a;
46 const unsigned int code2 = ((struct_ioctlent *) b)->code;
Denys Vlasenkofeb40c42012-03-20 13:11:51 +010047 return (code1 > code2) ? 1 : (code1 < code2) ? -1 : 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000048}
49
Dmitry V. Levin044eef22015-07-02 21:37:23 +000050static const struct_ioctlent *
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +000051ioctl_lookup(const unsigned int code)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000052{
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010053 struct_ioctlent *iop;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000054
Dmitry V. Levinc7afb482015-01-19 18:44:21 +000055 iop = bsearch((const void *) (const unsigned long) code, ioctlent,
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010056 nioctlents, sizeof(ioctlent[0]), compare);
57 while (iop > ioctlent) {
58 iop--;
59 if (iop->code != code) {
Roland McGrath2843a4e2003-11-14 02:54:03 +000060 iop++;
61 break;
62 }
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010063 }
Roland McGrath2843a4e2003-11-14 02:54:03 +000064 return iop;
65}
66
Dmitry V. Levin044eef22015-07-02 21:37:23 +000067static const struct_ioctlent *
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010068ioctl_next_match(const struct_ioctlent *iop)
Roland McGrath2843a4e2003-11-14 02:54:03 +000069{
Dmitry V. Levinc7afb482015-01-19 18:44:21 +000070 const unsigned int code = iop->code;
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010071 iop++;
Roland McGrath2843a4e2003-11-14 02:54:03 +000072 if (iop < ioctlent + nioctlents && iop->code == code)
73 return iop;
74 return NULL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000075}
76
Dmitry V. Levin044eef22015-07-02 21:37:23 +000077static void
Dmitry V. Levin924996a2015-01-20 00:24:36 +000078ioctl_print_code(const unsigned int code)
79{
80 tprints("_IOC(");
81 printflags(ioctl_dirs, _IOC_DIR(code), "_IOC_???");
82 tprintf(", 0x%02x, 0x%02x, 0x%02x)",
83 _IOC_TYPE(code), _IOC_NR(code), _IOC_SIZE(code));
84}
85
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010086static int
Dmitry V. Levin2803e752015-07-02 22:23:53 +000087evdev_decode_number(const unsigned int code)
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010088{
Dmitry V. Levin2803e752015-07-02 22:23:53 +000089 const unsigned int nr = _IOC_NR(code);
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010090
Dmitry V. Levin2803e752015-07-02 22:23:53 +000091 if (_IOC_DIR(code) == _IOC_WRITE) {
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010092 if (nr >= 0xc0 && nr <= 0xc0 + 0x3f) {
93 tprints("EVIOCSABS(");
Dmitry V. Levin1f1559e2016-05-27 01:11:40 +000094 printxval(evdev_abs, nr - 0xc0, "ABS_???");
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +010095 tprints(")");
96 return 1;
97 }
98 }
99
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000100 if (_IOC_DIR(code) != _IOC_READ)
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100101 return 0;
102
103 if (nr >= 0x20 && nr <= 0x20 + 0x1f) {
104 tprints("EVIOCGBIT(");
105 printxval(evdev_ev, nr - 0x20, "EV_???");
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000106 tprintf(", %u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100107 return 1;
108 } else if (nr >= 0x40 && nr <= 0x40 + 0x3f) {
109 tprints("EVIOCGABS(");
110 printxval(evdev_abs, nr - 0x40, "ABS_???");
111 tprints(")");
112 return 1;
113 }
114
115 switch (_IOC_NR(nr)) {
116 case 0x06:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000117 tprintf("EVIOCGNAME(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100118 return 1;
119 case 0x07:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000120 tprintf("EVIOCGPHYS(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100121 return 1;
122 case 0x08:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000123 tprintf("EVIOCGUNIQ(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100124 return 1;
125 case 0x09:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000126 tprintf("EVIOCGPROP(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100127 return 1;
128 case 0x0a:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000129 tprintf("EVIOCGMTSLOTS(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100130 return 1;
131 case 0x18:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000132 tprintf("EVIOCGKEY(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100133 return 1;
134 case 0x19:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000135 tprintf("EVIOCGLED(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100136 return 1;
137 case 0x1a:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000138 tprintf("EVIOCGSND(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100139 return 1;
140 case 0x1b:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000141 tprintf("EVIOCGSW(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100142 return 1;
143 default:
144 return 0;
145 }
146}
147
148static int
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000149hiddev_decode_number(const unsigned int code)
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100150{
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000151 if (_IOC_DIR(code) == _IOC_READ) {
152 switch (_IOC_NR(code)) {
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100153 case 0x04:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000154 tprintf("HIDIOCGRAWNAME(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100155 return 1;
156 case 0x05:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000157 tprintf("HIDIOCGRAWPHYS(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100158 return 1;
159 case 0x06:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000160 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100161 return 1;
162 case 0x12:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000163 tprintf("HIDIOCGPHYS(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100164 return 1;
165 default:
166 return 0;
167 }
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000168 } else if (_IOC_DIR(code) == (_IOC_READ | _IOC_WRITE)) {
169 switch (_IOC_NR(code)) {
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100170 case 0x06:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000171 tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100172 return 1;
173 case 0x07:
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000174 tprintf("HIDIOCGFEATURE(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100175 return 1;
176 default:
177 return 0;
178 }
179 }
180
181 return 0;
182}
183
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000184static int
Dmitry V. Levin4f63c112015-07-02 22:31:11 +0000185ioctl_decode_command_number(struct tcb *tcp)
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100186{
Dmitry V. Levin4f63c112015-07-02 22:31:11 +0000187 const unsigned int code = tcp->u_arg[1];
188
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000189 switch (_IOC_TYPE(code)) {
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100190 case 'E':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000191 return evdev_decode_number(code);
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100192 case 'H':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000193 return hiddev_decode_number(code);
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100194 case 'M':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000195 if (_IOC_DIR(code) == _IOC_WRITE) {
196 tprintf("MIXER_WRITE(%u)", _IOC_NR(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100197 return 1;
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000198 } else if (_IOC_DIR(code) == _IOC_READ) {
199 tprintf("MIXER_READ(%u)", _IOC_NR(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100200 return 1;
201 }
202 return 0;
203 case 'U':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000204 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x2c) {
205 tprintf("UI_GET_SYSNAME(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100206 return 1;
207 }
208 return 0;
209 case 'j':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000210 if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x13) {
211 tprintf("JSIOCGNAME(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100212 return 1;
213 }
214 return 0;
215 case 'k':
Dmitry V. Levin2803e752015-07-02 22:23:53 +0000216 if (_IOC_DIR(code) == _IOC_WRITE && _IOC_NR(code) == 0) {
217 tprintf("SPI_IOC_MESSAGE(%u)", _IOC_SIZE(code));
Gabriel Laskar6f9a01c2015-01-21 23:35:27 +0100218 return 1;
219 }
220 return 0;
221 default:
222 return 0;
223 }
224}
225
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000226static int
Dmitry V. Levin802c9422015-07-02 22:27:51 +0000227ioctl_decode(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000228{
Dmitry V. Levin802c9422015-07-02 22:27:51 +0000229 const unsigned int code = tcp->u_arg[1];
230 const long arg = tcp->u_arg[2];
231
Dmitry V. Levinc7afb482015-01-19 18:44:21 +0000232 switch (_IOC_TYPE(code)) {
Roland McGrathbb3af522003-01-14 07:53:34 +0000233#if defined(ALPHA) || defined(POWERPC)
Dmitry V. Levin25f37582016-05-24 00:19:03 +0000234 case 'f': {
235 int ret = file_ioctl(tcp, code, arg);
Jeff Mahoney398fe042016-05-18 18:09:40 -0400236 if (ret != RVAL_DECODED)
237 return ret;
Dmitry V. Levin25f37582016-05-24 00:19:03 +0000238 }
239 case 't':
Jeff Mahoney398fe042016-05-18 18:09:40 -0400240 case 'T':
241 return term_ioctl(tcp, code, arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000242#else /* !ALPHA */
Jeff Mahoney398fe042016-05-18 18:09:40 -0400243 case 'f':
244 return file_ioctl(tcp, code, arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000245 case 0x54:
246#endif /* !ALPHA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000247 return term_ioctl(tcp, code, arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000248 case 0x89:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000249 return sock_ioctl(tcp, code, arg);
Roland McGrathd83c50b2004-10-06 22:27:43 +0000250 case 'p':
251 return rtc_ioctl(tcp, code, arg);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000252 case 0x03:
Dmitry V. Levinb3f00402016-05-26 00:07:17 +0000253 return hdio_ioctl(tcp, code, arg);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000254 case 0x12:
255 return block_ioctl(tcp, code, arg);
Dmitry V. Levin4dd9f3f2016-05-26 00:37:26 +0000256 case 'X':
257 return fs_x_ioctl(tcp, code, arg);
Dmitry V. Levinfdb896e2014-02-25 23:04:55 +0000258#ifdef HAVE_SCSI_SG_H
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000259 case 0x22:
260 return scsi_ioctl(tcp, code, arg);
Dmitry V. Levinfdb896e2014-02-25 23:04:55 +0000261#endif
Mike Frysingerebee04c2012-04-17 22:19:31 -0400262 case 'L':
263 return loop_ioctl(tcp, code, arg);
Mike Frysinger0cbed352012-04-04 22:22:01 -0400264 case 'M':
265 return mtd_ioctl(tcp, code, arg);
Mike Frysingerd648f292013-05-01 23:35:30 -0400266 case 'o':
267 case 'O':
268 return ubi_ioctl(tcp, code, arg);
Philippe De Muyter0cc96142014-11-03 21:27:40 +0100269 case 'V':
270 return v4l2_ioctl(tcp, code, arg);
Stefan Sørensenb88a6f82014-01-31 12:01:01 +0100271 case '=':
272 return ptp_ioctl(tcp, code, arg);
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100273#ifdef HAVE_LINUX_INPUT_H
274 case 'E':
275 return evdev_ioctl(tcp, code, arg);
276#endif
Dr. David Alan Gilbert5d1216a2016-05-10 11:49:02 +0100277#ifdef HAVE_LINUX_USERFAULTFD_H
278 case 0xaa:
279 return uffdio_ioctl(tcp, code, arg);
280#endif
Jeff Mahoney8cc69622016-05-18 18:09:39 -0400281#ifdef HAVE_LINUX_BTRFS_H
282 case 0x94:
283 return btrfs_ioctl(tcp, code, arg);
284#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000285 default:
286 break;
287 }
288 return 0;
289}
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000290
291SYS_FUNC(ioctl)
292{
293 const struct_ioctlent *iop;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000294 int ret;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000295
296 if (entering(tcp)) {
297 printfd(tcp, tcp->u_arg[0]);
298 tprints(", ");
Gabriel Laskar9c4fc342015-09-23 10:11:55 +0200299 ret = ioctl_decode_command_number(tcp);
Gabriel Laskar8b6046a2015-12-04 01:07:33 +0100300 if (!(ret & IOCTL_NUMBER_STOP_LOOKUP)) {
301 iop = ioctl_lookup(tcp->u_arg[1]);
302 if (iop) {
303 if (ret)
304 tprints(" or ");
305 tprints(iop->symbol);
306 while ((iop = ioctl_next_match(iop)))
307 tprintf(" or %s", iop->symbol);
308 } else if (!ret) {
309 ioctl_print_code(tcp->u_arg[1]);
310 }
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000311 }
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000312 ret = ioctl_decode(tcp);
313 } else {
314 ret = ioctl_decode(tcp) | RVAL_DECODED;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000315 }
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000316
317 if (ret & RVAL_DECODED) {
318 ret &= ~RVAL_DECODED;
319 if (ret)
320 --ret;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000321 else
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000322 tprintf(", %#lx", tcp->u_arg[2]);
323 ret |= RVAL_DECODED;
324 } else {
325 if (ret)
326 --ret;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000327 }
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000328
329 return ret;
Dmitry V. Levin044eef22015-07-02 21:37:23 +0000330}