]> granicus.if.org Git - strace/blob - ioctl.c
Cache tcp->u_nargs in a local variable for for() loops
[strace] / ioctl.c
1 /*
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  * 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.
29  *
30  *      $Id$
31  */
32
33 #include "defs.h"
34
35 #ifdef LINUX
36 #include <asm/ioctl.h>
37 #endif
38
39 static int
40 compare(const void *a, const void *b)
41 {
42         unsigned long code1 = ((struct ioctlent *) a)->code;
43         unsigned long code2 = ((struct ioctlent *) b)->code;
44         return (code1 > code2) ? 1 : (code1 < code2) ? -1 : 0;
45 }
46
47 const struct ioctlent *
48 ioctl_lookup(long code)
49 {
50         struct ioctlent *iop, ioent;
51
52         ioent.code = code;
53 #ifdef LINUX
54         ioent.code &= (_IOC_NRMASK<<_IOC_NRSHIFT) | (_IOC_TYPEMASK<<_IOC_TYPESHIFT);
55 #endif
56         iop = (struct ioctlent *) bsearch((char *) &ioent, (char *) ioctlent,
57                         nioctlents, sizeof(struct ioctlent), compare);
58         while (iop > ioctlent)
59                 if ((--iop)->code != ioent.code) {
60                         iop++;
61                         break;
62                 }
63         return iop;
64 }
65
66 const struct ioctlent *
67 ioctl_next_match(const struct ioctlent *iop)
68 {
69         long code;
70
71         code = (iop++)->code;
72         if (iop < ioctlent + nioctlents && iop->code == code)
73                 return iop;
74         return NULL;
75 }
76
77 int
78 ioctl_decode(struct tcb *tcp, long code, long arg)
79 {
80         switch ((code >> 8) & 0xff) {
81 #ifdef LINUX
82 #if defined(ALPHA) || defined(POWERPC)
83         case 'f': case 't': case 'T':
84 #else /* !ALPHA */
85         case 0x54:
86 #endif /* !ALPHA */
87 #else /* !LINUX */
88         case 'f': case 't': case 'T':
89 #endif /* !LINUX */
90                 return term_ioctl(tcp, code, arg);
91 #ifdef LINUX
92         case 0x89:
93 #else /* !LINUX */
94         case 'r': case 's': case 'i':
95 #ifndef FREEBSD
96         case 'p':
97 #endif
98 #endif /* !LINUX */
99                 return sock_ioctl(tcp, code, arg);
100 #ifdef USE_PROCFS
101 #ifndef HAVE_MP_PROCFS
102 #ifndef FREEBSD
103         case 'q':
104 #else
105         case 'p':
106 #endif
107                 return proc_ioctl(tcp, code, arg);
108 #endif
109 #endif /* USE_PROCFS */
110 #ifdef HAVE_SYS_STREAM_H
111         case 'S':
112                 return stream_ioctl(tcp, code, arg);
113 #endif /* HAVE_SYS_STREAM_H */
114 #ifdef LINUX
115         case 'p':
116                 return rtc_ioctl(tcp, code, arg);
117         case 0x03:
118         case 0x12:
119                 return block_ioctl(tcp, code, arg);
120         case 0x22:
121                 return scsi_ioctl(tcp, code, arg);
122 #endif
123         default:
124                 break;
125         }
126         return 0;
127 }
128
129 /*
130  * Registry of ioctl characters, culled from
131  *      @(#)ioccom.h 1.7 89/06/16 SMI; from UCB ioctl.h 7.1 6/4/86
132  *
133  * char file where defined              notes
134  * ---- ------------------              -----
135  *   F  sun/fbio.h
136  *   G  sun/gpio.h
137  *   H  vaxif/if_hy.h
138  *   M  sundev/mcpcmd.h                 *overlap*
139  *   M  sys/modem.h                     *overlap*
140  *   S  sys/stropts.h
141  *   T  sys/termio.h                    -no overlap-
142  *   T  sys/termios.h                   -no overlap-
143  *   V  sundev/mdreg.h
144  *   a  vaxuba/adreg.h
145  *   d  sun/dkio.h                      -no overlap with sys/des.h-
146  *   d  sys/des.h                       (possible overlap)
147  *   d  vax/dkio.h                      (possible overlap)
148  *   d  vaxuba/rxreg.h                  (possible overlap)
149  *   f  sys/filio.h
150  *   g  sunwindow/win_ioctl.h           -no overlap-
151  *   g  sunwindowdev/winioctl.c         !no manifest constant! -no overlap-
152  *   h  sundev/hrc_common.h
153  *   i  sys/sockio.h                    *overlap*
154  *   i  vaxuba/ikreg.h                  *overlap*
155  *   k  sundev/kbio.h
156  *   m  sundev/msio.h                   (possible overlap)
157  *   m  sundev/msreg.h                  (possible overlap)
158  *   m  sys/mtio.h                      (possible overlap)
159  *   n  sun/ndio.h
160  *   p  net/nit_buf.h                   (possible overlap)
161  *   p  net/nit_if.h                    (possible overlap)
162  *   p  net/nit_pf.h                    (possible overlap)
163  *   p  sundev/fpareg.h                 (possible overlap)
164  *   p  sys/sockio.h                    (possible overlap)
165  *   p  vaxuba/psreg.h                  (possible overlap)
166  *   q  sun/sqz.h
167  *   r  sys/sockio.h
168  *   s  sys/sockio.h
169  *   t  sys/ttold.h                     (possible overlap)
170  *   t  sys/ttycom.h                    (possible overlap)
171  *   v  sundev/vuid_event.h             *overlap*
172  *   v  sys/vcmd.h                      *overlap*
173  *
174  * End of Registry
175  */