1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "defs.h"
29 /*
30  * The C library's definition of struct termios might differ from
31  * the kernel one, and we need to use the kernel layout.
32  */
33 #include <linux/termios.h>
34 
35 #include "xlat/tcxonc_options.h"
36 #include "xlat/tcflsh_options.h"
37 #include "xlat/baud_options.h"
38 #include "xlat/modem_flags.h"
39 
40 static void
decode_termios(struct tcb * tcp,const long addr)41 decode_termios(struct tcb *tcp, const long addr)
42 {
43 	struct termios tios;
44 	int i;
45 
46 	if (!verbose(tcp))
47 		return;
48 
49 	tprints(", ");
50 	if (umove_or_printaddr(tcp, addr, &tios))
51 		return;
52 	if (abbrev(tcp)) {
53 		tprints("{");
54 		printxval(baud_options, tios.c_cflag & CBAUD, "B???");
55 		tprintf(" %sopost %sisig %sicanon %secho ...}",
56 			(tios.c_oflag & OPOST) ? "" : "-",
57 			(tios.c_lflag & ISIG) ? "" : "-",
58 			(tios.c_lflag & ICANON) ? "" : "-",
59 			(tios.c_lflag & ECHO) ? "" : "-");
60 		return;
61 	}
62 	tprintf("{c_iflags=%#lx, c_oflags=%#lx, ",
63 		(long) tios.c_iflag, (long) tios.c_oflag);
64 	tprintf("c_cflags=%#lx, c_lflags=%#lx, ",
65 		(long) tios.c_cflag, (long) tios.c_lflag);
66 	tprintf("c_line=%u, ", tios.c_line);
67 	if (!(tios.c_lflag & ICANON))
68 		tprintf("c_cc[VMIN]=%d, c_cc[VTIME]=%d, ",
69 			tios.c_cc[VMIN], tios.c_cc[VTIME]);
70 	tprintf("c_cc=\"");
71 	for (i = 0; i < NCCS; i++)
72 		tprintf("\\x%02x", tios.c_cc[i]);
73 	tprintf("\"}");
74 }
75 
76 static void
decode_termio(struct tcb * tcp,const long addr)77 decode_termio(struct tcb *tcp, const long addr)
78 {
79 	struct termio tio;
80 	int i;
81 
82 	if (!verbose(tcp))
83 		return;
84 
85 	tprints(", ");
86 	if (umove_or_printaddr(tcp, addr, &tio))
87 		return;
88 	if (abbrev(tcp)) {
89 		tprints("{");
90 		printxval(baud_options, tio.c_cflag & CBAUD, "B???");
91 		tprintf(" %sopost %sisig %sicanon %secho ...}",
92 			(tio.c_oflag & OPOST) ? "" : "-",
93 			(tio.c_lflag & ISIG) ? "" : "-",
94 			(tio.c_lflag & ICANON) ? "" : "-",
95 			(tio.c_lflag & ECHO) ? "" : "-");
96 		return;
97 	}
98 	tprintf("{c_iflags=%#lx, c_oflags=%#lx, ",
99 		(long) tio.c_iflag, (long) tio.c_oflag);
100 	tprintf("c_cflags=%#lx, c_lflags=%#lx, ",
101 		(long) tio.c_cflag, (long) tio.c_lflag);
102 	tprintf("c_line=%u, ", tio.c_line);
103 #ifdef _VMIN
104 	if (!(tio.c_lflag & ICANON))
105 		tprintf("c_cc[_VMIN]=%d, c_cc[_VTIME]=%d, ",
106 			tio.c_cc[_VMIN], tio.c_cc[_VTIME]);
107 #else /* !_VMIN */
108 	if (!(tio.c_lflag & ICANON))
109 		tprintf("c_cc[VMIN]=%d, c_cc[VTIME]=%d, ",
110 			tio.c_cc[VMIN], tio.c_cc[VTIME]);
111 #endif /* !_VMIN */
112 	tprintf("c_cc=\"");
113 	for (i = 0; i < NCC; i++)
114 		tprintf("\\x%02x", tio.c_cc[i]);
115 	tprintf("\"}");
116 }
117 
118 static void
decode_winsize(struct tcb * tcp,const long addr)119 decode_winsize(struct tcb *tcp, const long addr)
120 {
121 	struct winsize ws;
122 
123 	if (!verbose(tcp))
124 		return;
125 
126 	tprints(", ");
127 	if (umove_or_printaddr(tcp, addr, &ws))
128 		return;
129 	tprintf("{ws_row=%d, ws_col=%d, ws_xpixel=%d, ws_ypixel=%d}",
130 		ws.ws_row, ws.ws_col, ws.ws_xpixel, ws.ws_ypixel);
131 }
132 
133 #ifdef TIOCGSIZE
134 static void
decode_ttysize(struct tcb * tcp,const long addr)135 decode_ttysize(struct tcb *tcp, const long addr)
136 {
137 	struct ttysize ts;
138 
139 	if (!verbose(tcp))
140 		return;
141 
142 	tprints(", ");
143 	if (umove_or_printaddr(tcp, addr, &ts))
144 		return;
145 	tprintf("{ts_lines=%d, ts_cols=%d}",
146 		ts.ts_lines, ts.ts_cols);
147 }
148 #endif
149 
150 static void
decode_modem_flags(struct tcb * tcp,const long addr)151 decode_modem_flags(struct tcb *tcp, const long addr)
152 {
153 	int i;
154 
155 	if (!verbose(tcp))
156 		return;
157 
158 	tprints(", ");
159 	if (umove_or_printaddr(tcp, addr, &i))
160 		return;
161 	tprints("[");
162 	printflags(modem_flags, i, "TIOCM_???");
163 	tprints("]");
164 }
165 
166 int
term_ioctl(struct tcb * tcp,const unsigned int code,const long arg)167 term_ioctl(struct tcb *tcp, const unsigned int code, const long arg)
168 {
169 	switch (code) {
170 	/* struct termios */
171 	case TCGETS:
172 #ifdef TCGETS2
173 	case TCGETS2:
174 #endif
175 	case TIOCGLCKTRMIOS:
176 		if (entering(tcp))
177 			return 0;
178 	case TCSETS:
179 #ifdef TCSETS2
180 	case TCSETS2:
181 #endif
182 	case TCSETSW:
183 #ifdef TCSETSW2
184 	case TCSETSW2:
185 #endif
186 	case TCSETSF:
187 #ifdef TCSETSF2
188 	case TCSETSF2:
189 #endif
190 	case TIOCSLCKTRMIOS:
191 		decode_termios(tcp, arg);
192 		break;
193 
194 	/* struct termio */
195 	case TCGETA:
196 		if (entering(tcp))
197 			return 0;
198 	case TCSETA:
199 	case TCSETAW:
200 	case TCSETAF:
201 		decode_termio(tcp, arg);
202 		break;
203 
204 	/* struct winsize */
205 	case TIOCGWINSZ:
206 		if (entering(tcp))
207 			return 0;
208 	case TIOCSWINSZ:
209 		decode_winsize(tcp, arg);
210 		break;
211 
212 	/* struct ttysize */
213 #ifdef TIOCGSIZE
214 	case TIOCGSIZE:
215 		if (entering(tcp))
216 			return 0;
217 	case TIOCSSIZE:
218 		decode_ttysize(tcp, arg);
219 		break;
220 #endif
221 
222 	/* ioctls with a direct decodable arg */
223 	case TCXONC:
224 		tprints(", ");
225 		printxval(tcxonc_options, arg, "TC???");
226 		break;
227 	case TCFLSH:
228 		tprints(", ");
229 		printxval(tcflsh_options, arg, "TC???");
230 		break;
231 	case TCSBRK:
232 	case TCSBRKP:
233 	case TIOCSCTTY:
234 		tprintf(", %d", (int) arg);
235 		break;
236 
237 	/* ioctls with an indirect parameter displayed as modem flags */
238 	case TIOCMGET:
239 		if (entering(tcp))
240 			return 0;
241 	case TIOCMBIS:
242 	case TIOCMBIC:
243 	case TIOCMSET:
244 		decode_modem_flags(tcp, arg);
245 		break;
246 
247 	/* ioctls with an indirect parameter displayed in decimal */
248 	case TIOCGPGRP:
249 	case TIOCGSID:
250 	case TIOCGETD:
251 	case TIOCGSOFTCAR:
252 	case TIOCGPTN:
253 	case FIONREAD:
254 	case TIOCOUTQ:
255 #ifdef TIOCGEXCL
256 	case TIOCGEXCL:
257 #endif
258 #ifdef TIOCGDEV
259 	case TIOCGDEV:
260 #endif
261 		if (entering(tcp))
262 			return 0;
263 	case TIOCSPGRP:
264 	case TIOCSETD:
265 	case FIONBIO:
266 	case FIOASYNC:
267 	case TIOCPKT:
268 	case TIOCSSOFTCAR:
269 	case TIOCSPTLCK:
270 		tprints(", ");
271 		printnum_int(tcp, arg, "%d");
272 		break;
273 
274 	/* ioctls with an indirect parameter displayed as a char */
275 	case TIOCSTI:
276 		tprints(", ");
277 		printstr(tcp, arg, 1);
278 		break;
279 
280 	/* ioctls with no parameters */
281 
282 	case TIOCSBRK:
283 	case TIOCCBRK:
284 	case TIOCCONS:
285 	case TIOCNOTTY:
286 	case TIOCEXCL:
287 	case TIOCNXCL:
288 	case FIOCLEX:
289 	case FIONCLEX:
290 #ifdef TIOCVHANGUP
291 	case TIOCVHANGUP:
292 #endif
293 #ifdef TIOCSSERIAL
294 	case TIOCSSERIAL:
295 #endif
296 		break;
297 
298 	/* ioctls which are unknown */
299 
300 	default:
301 		return RVAL_DECODED;
302 	}
303 
304 	return RVAL_DECODED | 1;
305 }
306