Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * n_tty.c --- implements the N_TTY line discipline. | |
4edf1827 | 3 | * |
1da177e4 LT |
4 | * This code used to be in tty_io.c, but things are getting hairy |
5 | * enough that it made sense to split things off. (The N_TTY | |
6 | * processing has changed so much that it's hardly recognizable, | |
7 | * anyway...) | |
8 | * | |
9 | * Note that the open routine for N_TTY is guaranteed never to return | |
10 | * an error. This is because Linux will fall back to setting a line | |
4edf1827 | 11 | * to N_TTY if it can not switch to any other line discipline. |
1da177e4 LT |
12 | * |
13 | * Written by Theodore Ts'o, Copyright 1994. | |
4edf1827 | 14 | * |
1da177e4 LT |
15 | * This file also contains code originally written by Linus Torvalds, |
16 | * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994. | |
4edf1827 | 17 | * |
1da177e4 LT |
18 | * This file may be redistributed under the terms of the GNU General Public |
19 | * License. | |
20 | * | |
21 | * Reduced memory usage for older ARM systems - Russell King. | |
22 | * | |
4edf1827 | 23 | * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of |
1da177e4 LT |
24 | * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu> |
25 | * who actually finally proved there really was a race. | |
26 | * | |
27 | * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to | |
28 | * waiting writing processes-Sapan Bhatia <sapan@corewars.org>. | |
11a96d18 | 29 | * Also fixed a bug in BLOCKING mode where n_tty_write returns |
1da177e4 LT |
30 | * EAGAIN |
31 | */ | |
32 | ||
33 | #include <linux/types.h> | |
34 | #include <linux/major.h> | |
35 | #include <linux/errno.h> | |
36 | #include <linux/signal.h> | |
37 | #include <linux/fcntl.h> | |
38 | #include <linux/sched.h> | |
39 | #include <linux/interrupt.h> | |
40 | #include <linux/tty.h> | |
41 | #include <linux/timer.h> | |
42 | #include <linux/ctype.h> | |
43 | #include <linux/mm.h> | |
44 | #include <linux/string.h> | |
45 | #include <linux/slab.h> | |
46 | #include <linux/poll.h> | |
47 | #include <linux/bitops.h> | |
522ed776 MT |
48 | #include <linux/audit.h> |
49 | #include <linux/file.h> | |
300a6204 | 50 | #include <linux/uaccess.h> |
1da177e4 | 51 | |
1da177e4 LT |
52 | #include <asm/system.h> |
53 | ||
54 | /* number of characters left in xmit buffer before select has we have room */ | |
55 | #define WAKEUP_CHARS 256 | |
56 | ||
57 | /* | |
58 | * This defines the low- and high-watermarks for throttling and | |
59 | * unthrottling the TTY driver. These watermarks are used for | |
60 | * controlling the space in the read buffer. | |
61 | */ | |
62 | #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */ | |
63 | #define TTY_THRESHOLD_UNTHROTTLE 128 | |
64 | ||
a88a69c9 JP |
65 | /* |
66 | * Special byte codes used in the echo buffer to represent operations | |
67 | * or special handling of characters. Bytes in the echo buffer that | |
68 | * are not part of such special blocks are treated as normal character | |
69 | * codes. | |
70 | */ | |
71 | #define ECHO_OP_START 0xff | |
72 | #define ECHO_OP_MOVE_BACK_COL 0x80 | |
73 | #define ECHO_OP_SET_CANON_COL 0x81 | |
74 | #define ECHO_OP_ERASE_TAB 0x82 | |
75 | ||
1da177e4 LT |
76 | static inline unsigned char *alloc_buf(void) |
77 | { | |
b4e3ca1a | 78 | gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL; |
c481c707 | 79 | return kmalloc(N_TTY_BUF_SIZE, prio); |
1da177e4 LT |
80 | } |
81 | ||
82 | static inline void free_buf(unsigned char *buf) | |
83 | { | |
c481c707 | 84 | kfree(buf); |
1da177e4 LT |
85 | } |
86 | ||
522ed776 MT |
87 | static inline int tty_put_user(struct tty_struct *tty, unsigned char x, |
88 | unsigned char __user *ptr) | |
89 | { | |
90 | tty_audit_add_data(tty, &x, 1); | |
91 | return put_user(x, ptr); | |
92 | } | |
93 | ||
33f0f88f AC |
94 | /** |
95 | * n_tty_set__room - receive space | |
96 | * @tty: terminal | |
97 | * | |
98 | * Called by the driver to find out how much data it is | |
99 | * permitted to feed to the line discipline without any being lost | |
100 | * and thus to manage flow control. Not serialized. Answers for the | |
101 | * "instant". | |
102 | */ | |
103 | ||
104 | static void n_tty_set_room(struct tty_struct *tty) | |
105 | { | |
17b82060 | 106 | /* tty->read_cnt is not read locked ? */ |
33f0f88f AC |
107 | int left = N_TTY_BUF_SIZE - tty->read_cnt - 1; |
108 | ||
109 | /* | |
110 | * If we are doing input canonicalization, and there are no | |
111 | * pending newlines, let characters through without limit, so | |
112 | * that erase characters will be handled. Other excess | |
113 | * characters will be beeped. | |
114 | */ | |
115 | if (left <= 0) | |
116 | left = tty->icanon && !tty->canon_data; | |
117 | tty->receive_room = left; | |
118 | } | |
119 | ||
120 | static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty) | |
1da177e4 LT |
121 | { |
122 | if (tty->read_cnt < N_TTY_BUF_SIZE) { | |
123 | tty->read_buf[tty->read_head] = c; | |
124 | tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1); | |
125 | tty->read_cnt++; | |
126 | } | |
127 | } | |
128 | ||
17b82060 AC |
129 | /** |
130 | * put_tty_queue - add character to tty | |
131 | * @c: character | |
132 | * @tty: tty device | |
133 | * | |
134 | * Add a character to the tty read_buf queue. This is done under the | |
135 | * read_lock to serialize character addition and also to protect us | |
136 | * against parallel reads or flushes | |
137 | */ | |
138 | ||
33f0f88f | 139 | static void put_tty_queue(unsigned char c, struct tty_struct *tty) |
1da177e4 LT |
140 | { |
141 | unsigned long flags; | |
142 | /* | |
143 | * The problem of stomping on the buffers ends here. | |
144 | * Why didn't anyone see this one coming? --AJK | |
145 | */ | |
146 | spin_lock_irqsave(&tty->read_lock, flags); | |
147 | put_tty_queue_nolock(c, tty); | |
148 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
149 | } | |
150 | ||
151 | /** | |
152 | * check_unthrottle - allow new receive data | |
153 | * @tty; tty device | |
154 | * | |
17b82060 AC |
155 | * Check whether to call the driver unthrottle functions |
156 | * | |
70522e12 | 157 | * Can sleep, may be called under the atomic_read_lock mutex but |
1da177e4 LT |
158 | * this is not guaranteed. |
159 | */ | |
4edf1827 | 160 | static void check_unthrottle(struct tty_struct *tty) |
1da177e4 | 161 | { |
39c2e60f AC |
162 | if (tty->count) |
163 | tty_unthrottle(tty); | |
1da177e4 LT |
164 | } |
165 | ||
166 | /** | |
167 | * reset_buffer_flags - reset buffer state | |
168 | * @tty: terminal to reset | |
169 | * | |
4edf1827 | 170 | * Reset the read buffer counters, clear the flags, |
1da177e4 LT |
171 | * and make sure the driver is unthrottled. Called |
172 | * from n_tty_open() and n_tty_flush_buffer(). | |
17b82060 AC |
173 | * |
174 | * Locking: tty_read_lock for read fields. | |
1da177e4 | 175 | */ |
a88a69c9 | 176 | |
1da177e4 LT |
177 | static void reset_buffer_flags(struct tty_struct *tty) |
178 | { | |
179 | unsigned long flags; | |
180 | ||
181 | spin_lock_irqsave(&tty->read_lock, flags); | |
182 | tty->read_head = tty->read_tail = tty->read_cnt = 0; | |
183 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
a88a69c9 JP |
184 | |
185 | mutex_lock(&tty->echo_lock); | |
186 | tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0; | |
187 | mutex_unlock(&tty->echo_lock); | |
188 | ||
1da177e4 LT |
189 | tty->canon_head = tty->canon_data = tty->erasing = 0; |
190 | memset(&tty->read_flags, 0, sizeof tty->read_flags); | |
33f0f88f | 191 | n_tty_set_room(tty); |
1da177e4 LT |
192 | check_unthrottle(tty); |
193 | } | |
194 | ||
195 | /** | |
196 | * n_tty_flush_buffer - clean input queue | |
197 | * @tty: terminal device | |
198 | * | |
199 | * Flush the input buffer. Called when the line discipline is | |
200 | * being closed, when the tty layer wants the buffer flushed (eg | |
201 | * at hangup) or when the N_TTY line discipline internally has to | |
202 | * clean the pending queue (for example some signals). | |
203 | * | |
17b82060 | 204 | * Locking: ctrl_lock, read_lock. |
1da177e4 | 205 | */ |
4edf1827 AC |
206 | |
207 | static void n_tty_flush_buffer(struct tty_struct *tty) | |
1da177e4 | 208 | { |
04f378b1 | 209 | unsigned long flags; |
1da177e4 LT |
210 | /* clear everything and unthrottle the driver */ |
211 | reset_buffer_flags(tty); | |
4edf1827 | 212 | |
1da177e4 LT |
213 | if (!tty->link) |
214 | return; | |
215 | ||
04f378b1 | 216 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
1da177e4 LT |
217 | if (tty->link->packet) { |
218 | tty->ctrl_status |= TIOCPKT_FLUSHREAD; | |
219 | wake_up_interruptible(&tty->link->read_wait); | |
220 | } | |
04f378b1 | 221 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
1da177e4 LT |
222 | } |
223 | ||
224 | /** | |
225 | * n_tty_chars_in_buffer - report available bytes | |
226 | * @tty: tty device | |
227 | * | |
228 | * Report the number of characters buffered to be delivered to user | |
4edf1827 | 229 | * at this instant in time. |
17b82060 AC |
230 | * |
231 | * Locking: read_lock | |
1da177e4 | 232 | */ |
4edf1827 | 233 | |
1da177e4 LT |
234 | static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) |
235 | { | |
236 | unsigned long flags; | |
237 | ssize_t n = 0; | |
238 | ||
239 | spin_lock_irqsave(&tty->read_lock, flags); | |
240 | if (!tty->icanon) { | |
241 | n = tty->read_cnt; | |
242 | } else if (tty->canon_data) { | |
243 | n = (tty->canon_head > tty->read_tail) ? | |
244 | tty->canon_head - tty->read_tail : | |
245 | tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail); | |
246 | } | |
247 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
248 | return n; | |
249 | } | |
250 | ||
251 | /** | |
252 | * is_utf8_continuation - utf8 multibyte check | |
253 | * @c: byte to check | |
254 | * | |
255 | * Returns true if the utf8 character 'c' is a multibyte continuation | |
256 | * character. We use this to correctly compute the on screen size | |
257 | * of the character when printing | |
258 | */ | |
4edf1827 | 259 | |
1da177e4 LT |
260 | static inline int is_utf8_continuation(unsigned char c) |
261 | { | |
262 | return (c & 0xc0) == 0x80; | |
263 | } | |
264 | ||
265 | /** | |
266 | * is_continuation - multibyte check | |
267 | * @c: byte to check | |
268 | * | |
269 | * Returns true if the utf8 character 'c' is a multibyte continuation | |
270 | * character and the terminal is in unicode mode. | |
271 | */ | |
4edf1827 | 272 | |
1da177e4 LT |
273 | static inline int is_continuation(unsigned char c, struct tty_struct *tty) |
274 | { | |
275 | return I_IUTF8(tty) && is_utf8_continuation(c); | |
276 | } | |
277 | ||
278 | /** | |
a88a69c9 | 279 | * do_output_char - output one character |
1da177e4 LT |
280 | * @c: character (or partial unicode symbol) |
281 | * @tty: terminal device | |
a88a69c9 | 282 | * @space: space available in tty driver write buffer |
1da177e4 | 283 | * |
a88a69c9 JP |
284 | * This is a helper function that handles one output character |
285 | * (including special characters like TAB, CR, LF, etc.), | |
286 | * putting the results in the tty driver's write buffer. | |
287 | * | |
288 | * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY | |
289 | * and NLDLY. They simply aren't relevant in the world today. | |
290 | * If you ever need them, add them here. | |
1da177e4 | 291 | * |
a88a69c9 JP |
292 | * Returns the number of bytes of buffer space used or -1 if |
293 | * no space left. | |
294 | * | |
295 | * Locking: should be called under the output_lock to protect | |
296 | * the column state and space left in the buffer | |
1da177e4 | 297 | */ |
4edf1827 | 298 | |
a88a69c9 | 299 | static int do_output_char(unsigned char c, struct tty_struct *tty, int space) |
1da177e4 | 300 | { |
a88a69c9 | 301 | int spaces; |
1da177e4 | 302 | |
1da177e4 LT |
303 | if (!space) |
304 | return -1; | |
300a6204 | 305 | |
a88a69c9 JP |
306 | switch (c) { |
307 | case '\n': | |
308 | if (O_ONLRET(tty)) | |
309 | tty->column = 0; | |
310 | if (O_ONLCR(tty)) { | |
311 | if (space < 2) | |
312 | return -1; | |
1da177e4 | 313 | tty->canon_column = tty->column = 0; |
a88a69c9 JP |
314 | tty_put_char(tty, '\r'); |
315 | tty_put_char(tty, c); | |
316 | return 2; | |
317 | } | |
318 | tty->canon_column = tty->column; | |
319 | break; | |
320 | case '\r': | |
321 | if (O_ONOCR(tty) && tty->column == 0) | |
322 | return 0; | |
323 | if (O_OCRNL(tty)) { | |
324 | c = '\n'; | |
325 | if (O_ONLRET(tty)) | |
326 | tty->canon_column = tty->column = 0; | |
1da177e4 | 327 | break; |
a88a69c9 JP |
328 | } |
329 | tty->canon_column = tty->column = 0; | |
330 | break; | |
331 | case '\t': | |
332 | spaces = 8 - (tty->column & 7); | |
333 | if (O_TABDLY(tty) == XTABS) { | |
334 | if (space < spaces) | |
335 | return -1; | |
1da177e4 | 336 | tty->column += spaces; |
a88a69c9 JP |
337 | tty->ops->write(tty, " ", spaces); |
338 | return spaces; | |
1da177e4 | 339 | } |
a88a69c9 JP |
340 | tty->column += spaces; |
341 | break; | |
342 | case '\b': | |
343 | if (tty->column > 0) | |
344 | tty->column--; | |
345 | break; | |
346 | default: | |
a59c0d6f JP |
347 | if (!iscntrl(c)) { |
348 | if (O_OLCUC(tty)) | |
349 | c = toupper(c); | |
350 | if (!is_continuation(c, tty)) | |
351 | tty->column++; | |
352 | } | |
a88a69c9 | 353 | break; |
1da177e4 | 354 | } |
a88a69c9 | 355 | |
f34d7a5b | 356 | tty_put_char(tty, c); |
a88a69c9 JP |
357 | return 1; |
358 | } | |
359 | ||
360 | /** | |
361 | * process_output - output post processor | |
362 | * @c: character (or partial unicode symbol) | |
363 | * @tty: terminal device | |
364 | * | |
365 | * Perform OPOST processing. Returns -1 when the output device is | |
366 | * full and the character must be retried. | |
367 | * | |
368 | * Locking: output_lock to protect column state and space left | |
369 | * (also, this is called from n_tty_write under the | |
370 | * tty layer write lock) | |
371 | */ | |
372 | ||
373 | static int process_output(unsigned char c, struct tty_struct *tty) | |
374 | { | |
375 | int space, retval; | |
376 | ||
377 | mutex_lock(&tty->output_lock); | |
378 | ||
379 | space = tty_write_room(tty); | |
380 | retval = do_output_char(c, tty, space); | |
381 | ||
382 | mutex_unlock(&tty->output_lock); | |
383 | if (retval < 0) | |
384 | return -1; | |
385 | else | |
386 | return 0; | |
1da177e4 LT |
387 | } |
388 | ||
389 | /** | |
a88a69c9 | 390 | * process_output_block - block post processor |
1da177e4 LT |
391 | * @tty: terminal device |
392 | * @inbuf: user buffer | |
393 | * @nr: number of bytes | |
394 | * | |
395 | * This path is used to speed up block console writes, among other | |
396 | * things when processing blocks of output data. It handles only | |
397 | * the simple cases normally found and helps to generate blocks of | |
398 | * symbols for the console driver and thus improve performance. | |
399 | * | |
a88a69c9 JP |
400 | * Locking: output_lock to protect column state and space left |
401 | * (also, this is called from n_tty_write under the | |
402 | * tty layer write lock) | |
1da177e4 | 403 | */ |
4edf1827 | 404 | |
a88a69c9 JP |
405 | static ssize_t process_output_block(struct tty_struct *tty, |
406 | const unsigned char *buf, unsigned int nr) | |
1da177e4 LT |
407 | { |
408 | int space; | |
409 | int i; | |
410 | const unsigned char *cp; | |
411 | ||
a88a69c9 JP |
412 | mutex_lock(&tty->output_lock); |
413 | ||
f34d7a5b | 414 | space = tty_write_room(tty); |
300a6204 | 415 | if (!space) { |
a88a69c9 | 416 | mutex_unlock(&tty->output_lock); |
1da177e4 | 417 | return 0; |
a88a69c9 | 418 | } |
1da177e4 LT |
419 | if (nr > space) |
420 | nr = space; | |
421 | ||
422 | for (i = 0, cp = buf; i < nr; i++, cp++) { | |
a59c0d6f JP |
423 | unsigned char c = *cp; |
424 | ||
425 | switch (c) { | |
1da177e4 LT |
426 | case '\n': |
427 | if (O_ONLRET(tty)) | |
428 | tty->column = 0; | |
429 | if (O_ONLCR(tty)) | |
430 | goto break_out; | |
431 | tty->canon_column = tty->column; | |
432 | break; | |
433 | case '\r': | |
434 | if (O_ONOCR(tty) && tty->column == 0) | |
435 | goto break_out; | |
436 | if (O_OCRNL(tty)) | |
437 | goto break_out; | |
438 | tty->canon_column = tty->column = 0; | |
439 | break; | |
440 | case '\t': | |
441 | goto break_out; | |
442 | case '\b': | |
443 | if (tty->column > 0) | |
444 | tty->column--; | |
445 | break; | |
446 | default: | |
a59c0d6f JP |
447 | if (!iscntrl(c)) { |
448 | if (O_OLCUC(tty)) | |
449 | goto break_out; | |
450 | if (!is_continuation(c, tty)) | |
451 | tty->column++; | |
452 | } | |
1da177e4 LT |
453 | break; |
454 | } | |
455 | } | |
456 | break_out: | |
f34d7a5b | 457 | i = tty->ops->write(tty, buf, i); |
a88a69c9 JP |
458 | |
459 | mutex_unlock(&tty->output_lock); | |
1da177e4 LT |
460 | return i; |
461 | } | |
462 | ||
a88a69c9 JP |
463 | /** |
464 | * process_echoes - write pending echo characters | |
465 | * @tty: terminal device | |
466 | * | |
467 | * Write previously buffered echo (and other ldisc-generated) | |
468 | * characters to the tty. | |
469 | * | |
470 | * Characters generated by the ldisc (including echoes) need to | |
471 | * be buffered because the driver's write buffer can fill during | |
472 | * heavy program output. Echoing straight to the driver will | |
473 | * often fail under these conditions, causing lost characters and | |
474 | * resulting mismatches of ldisc state information. | |
475 | * | |
476 | * Since the ldisc state must represent the characters actually sent | |
477 | * to the driver at the time of the write, operations like certain | |
478 | * changes in column state are also saved in the buffer and executed | |
479 | * here. | |
480 | * | |
481 | * A circular fifo buffer is used so that the most recent characters | |
482 | * are prioritized. Also, when control characters are echoed with a | |
483 | * prefixed "^", the pair is treated atomically and thus not separated. | |
484 | * | |
485 | * Locking: output_lock to protect column state and space left, | |
486 | * echo_lock to protect the echo buffer | |
487 | */ | |
488 | ||
489 | static void process_echoes(struct tty_struct *tty) | |
490 | { | |
491 | int space, nr; | |
492 | unsigned char c; | |
493 | unsigned char *cp, *buf_end; | |
494 | ||
495 | if (!tty->echo_cnt) | |
496 | return; | |
497 | ||
498 | mutex_lock(&tty->output_lock); | |
499 | mutex_lock(&tty->echo_lock); | |
500 | ||
501 | space = tty_write_room(tty); | |
502 | ||
503 | buf_end = tty->echo_buf + N_TTY_BUF_SIZE; | |
504 | cp = tty->echo_buf + tty->echo_pos; | |
505 | nr = tty->echo_cnt; | |
506 | while (nr > 0) { | |
507 | c = *cp; | |
508 | if (c == ECHO_OP_START) { | |
509 | unsigned char op; | |
510 | unsigned char *opp; | |
511 | int no_space_left = 0; | |
512 | ||
513 | /* | |
514 | * If the buffer byte is the start of a multi-byte | |
515 | * operation, get the next byte, which is either the | |
516 | * op code or a control character value. | |
517 | */ | |
518 | opp = cp + 1; | |
519 | if (opp == buf_end) | |
520 | opp -= N_TTY_BUF_SIZE; | |
521 | op = *opp; | |
300a6204 | 522 | |
a88a69c9 JP |
523 | switch (op) { |
524 | unsigned int num_chars, num_bs; | |
525 | ||
526 | case ECHO_OP_ERASE_TAB: | |
527 | if (++opp == buf_end) | |
528 | opp -= N_TTY_BUF_SIZE; | |
529 | num_chars = *opp; | |
530 | ||
531 | /* | |
532 | * Determine how many columns to go back | |
533 | * in order to erase the tab. | |
534 | * This depends on the number of columns | |
535 | * used by other characters within the tab | |
536 | * area. If this (modulo 8) count is from | |
537 | * the start of input rather than from a | |
538 | * previous tab, we offset by canon column. | |
539 | * Otherwise, tab spacing is normal. | |
540 | */ | |
541 | if (!(num_chars & 0x80)) | |
542 | num_chars += tty->canon_column; | |
543 | num_bs = 8 - (num_chars & 7); | |
544 | ||
545 | if (num_bs > space) { | |
546 | no_space_left = 1; | |
547 | break; | |
548 | } | |
549 | space -= num_bs; | |
550 | while (num_bs--) { | |
551 | tty_put_char(tty, '\b'); | |
552 | if (tty->column > 0) | |
553 | tty->column--; | |
554 | } | |
555 | cp += 3; | |
556 | nr -= 3; | |
557 | break; | |
558 | ||
559 | case ECHO_OP_SET_CANON_COL: | |
560 | tty->canon_column = tty->column; | |
561 | cp += 2; | |
562 | nr -= 2; | |
563 | break; | |
564 | ||
565 | case ECHO_OP_MOVE_BACK_COL: | |
566 | if (tty->column > 0) | |
567 | tty->column--; | |
568 | cp += 2; | |
569 | nr -= 2; | |
570 | break; | |
571 | ||
572 | case ECHO_OP_START: | |
573 | /* This is an escaped echo op start code */ | |
574 | if (!space) { | |
575 | no_space_left = 1; | |
576 | break; | |
577 | } | |
578 | tty_put_char(tty, ECHO_OP_START); | |
579 | tty->column++; | |
580 | space--; | |
581 | cp += 2; | |
582 | nr -= 2; | |
583 | break; | |
584 | ||
585 | default: | |
586 | if (iscntrl(op)) { | |
587 | if (L_ECHOCTL(tty)) { | |
588 | /* | |
589 | * Ensure there is enough space | |
590 | * for the whole ctrl pair. | |
591 | */ | |
592 | if (space < 2) { | |
593 | no_space_left = 1; | |
594 | break; | |
595 | } | |
596 | tty_put_char(tty, '^'); | |
597 | tty_put_char(tty, op ^ 0100); | |
598 | tty->column += 2; | |
599 | space -= 2; | |
600 | } else { | |
601 | if (!space) { | |
602 | no_space_left = 1; | |
603 | break; | |
604 | } | |
605 | tty_put_char(tty, op); | |
606 | space--; | |
607 | } | |
608 | } | |
609 | /* | |
610 | * If above falls through, this was an | |
611 | * undefined op. | |
612 | */ | |
613 | cp += 2; | |
614 | nr -= 2; | |
615 | } | |
616 | ||
617 | if (no_space_left) | |
618 | break; | |
619 | } else { | |
620 | int retval; | |
621 | ||
300a6204 AC |
622 | retval = do_output_char(c, tty, space); |
623 | if (retval < 0) | |
a88a69c9 JP |
624 | break; |
625 | space -= retval; | |
626 | cp += 1; | |
627 | nr -= 1; | |
628 | } | |
629 | ||
630 | /* When end of circular buffer reached, wrap around */ | |
631 | if (cp >= buf_end) | |
632 | cp -= N_TTY_BUF_SIZE; | |
633 | } | |
634 | ||
635 | if (nr == 0) { | |
636 | tty->echo_pos = 0; | |
637 | tty->echo_cnt = 0; | |
638 | tty->echo_overrun = 0; | |
639 | } else { | |
640 | int num_processed = tty->echo_cnt - nr; | |
641 | tty->echo_pos += num_processed; | |
642 | tty->echo_pos &= N_TTY_BUF_SIZE - 1; | |
643 | tty->echo_cnt = nr; | |
644 | if (num_processed > 0) | |
645 | tty->echo_overrun = 0; | |
646 | } | |
647 | ||
648 | mutex_unlock(&tty->echo_lock); | |
649 | mutex_unlock(&tty->output_lock); | |
650 | ||
651 | if (tty->ops->flush_chars) | |
652 | tty->ops->flush_chars(tty); | |
653 | } | |
654 | ||
655 | /** | |
656 | * add_echo_byte - add a byte to the echo buffer | |
657 | * @c: unicode byte to echo | |
658 | * @tty: terminal device | |
659 | * | |
660 | * Add a character or operation byte to the echo buffer. | |
661 | * | |
662 | * Should be called under the echo lock to protect the echo buffer. | |
663 | */ | |
664 | ||
665 | static void add_echo_byte(unsigned char c, struct tty_struct *tty) | |
666 | { | |
667 | int new_byte_pos; | |
668 | ||
669 | if (tty->echo_cnt == N_TTY_BUF_SIZE) { | |
670 | /* Circular buffer is already at capacity */ | |
671 | new_byte_pos = tty->echo_pos; | |
672 | ||
673 | /* | |
674 | * Since the buffer start position needs to be advanced, | |
675 | * be sure to step by a whole operation byte group. | |
676 | */ | |
300a6204 | 677 | if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) { |
a88a69c9 JP |
678 | if (tty->echo_buf[(tty->echo_pos + 1) & |
679 | (N_TTY_BUF_SIZE - 1)] == | |
680 | ECHO_OP_ERASE_TAB) { | |
681 | tty->echo_pos += 3; | |
682 | tty->echo_cnt -= 2; | |
683 | } else { | |
684 | tty->echo_pos += 2; | |
685 | tty->echo_cnt -= 1; | |
686 | } | |
687 | } else { | |
688 | tty->echo_pos++; | |
689 | } | |
690 | tty->echo_pos &= N_TTY_BUF_SIZE - 1; | |
691 | ||
692 | tty->echo_overrun = 1; | |
693 | } else { | |
694 | new_byte_pos = tty->echo_pos + tty->echo_cnt; | |
695 | new_byte_pos &= N_TTY_BUF_SIZE - 1; | |
696 | tty->echo_cnt++; | |
697 | } | |
698 | ||
699 | tty->echo_buf[new_byte_pos] = c; | |
700 | } | |
701 | ||
702 | /** | |
703 | * echo_move_back_col - add operation to move back a column | |
704 | * @tty: terminal device | |
705 | * | |
706 | * Add an operation to the echo buffer to move back one column. | |
707 | * | |
708 | * Locking: echo_lock to protect the echo buffer | |
709 | */ | |
710 | ||
711 | static void echo_move_back_col(struct tty_struct *tty) | |
712 | { | |
713 | mutex_lock(&tty->echo_lock); | |
714 | ||
715 | add_echo_byte(ECHO_OP_START, tty); | |
716 | add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty); | |
717 | ||
718 | mutex_unlock(&tty->echo_lock); | |
719 | } | |
720 | ||
721 | /** | |
722 | * echo_set_canon_col - add operation to set the canon column | |
723 | * @tty: terminal device | |
724 | * | |
725 | * Add an operation to the echo buffer to set the canon column | |
726 | * to the current column. | |
727 | * | |
728 | * Locking: echo_lock to protect the echo buffer | |
729 | */ | |
730 | ||
731 | static void echo_set_canon_col(struct tty_struct *tty) | |
732 | { | |
733 | mutex_lock(&tty->echo_lock); | |
734 | ||
735 | add_echo_byte(ECHO_OP_START, tty); | |
736 | add_echo_byte(ECHO_OP_SET_CANON_COL, tty); | |
737 | ||
738 | mutex_unlock(&tty->echo_lock); | |
739 | } | |
740 | ||
741 | /** | |
742 | * echo_erase_tab - add operation to erase a tab | |
743 | * @num_chars: number of character columns already used | |
744 | * @after_tab: true if num_chars starts after a previous tab | |
745 | * @tty: terminal device | |
746 | * | |
747 | * Add an operation to the echo buffer to erase a tab. | |
748 | * | |
749 | * Called by the eraser function, which knows how many character | |
750 | * columns have been used since either a previous tab or the start | |
751 | * of input. This information will be used later, along with | |
752 | * canon column (if applicable), to go back the correct number | |
753 | * of columns. | |
754 | * | |
755 | * Locking: echo_lock to protect the echo buffer | |
756 | */ | |
757 | ||
758 | static void echo_erase_tab(unsigned int num_chars, int after_tab, | |
759 | struct tty_struct *tty) | |
760 | { | |
761 | mutex_lock(&tty->echo_lock); | |
762 | ||
763 | add_echo_byte(ECHO_OP_START, tty); | |
764 | add_echo_byte(ECHO_OP_ERASE_TAB, tty); | |
765 | ||
766 | /* We only need to know this modulo 8 (tab spacing) */ | |
767 | num_chars &= 7; | |
768 | ||
769 | /* Set the high bit as a flag if num_chars is after a previous tab */ | |
770 | if (after_tab) | |
771 | num_chars |= 0x80; | |
300a6204 | 772 | |
a88a69c9 JP |
773 | add_echo_byte(num_chars, tty); |
774 | ||
775 | mutex_unlock(&tty->echo_lock); | |
776 | } | |
777 | ||
778 | /** | |
779 | * echo_char_raw - echo a character raw | |
780 | * @c: unicode byte to echo | |
781 | * @tty: terminal device | |
782 | * | |
783 | * Echo user input back onto the screen. This must be called only when | |
784 | * L_ECHO(tty) is true. Called from the driver receive_buf path. | |
785 | * | |
786 | * This variant does not treat control characters specially. | |
787 | * | |
788 | * Locking: echo_lock to protect the echo buffer | |
789 | */ | |
790 | ||
791 | static void echo_char_raw(unsigned char c, struct tty_struct *tty) | |
792 | { | |
793 | mutex_lock(&tty->echo_lock); | |
794 | ||
795 | if (c == ECHO_OP_START) { | |
796 | add_echo_byte(ECHO_OP_START, tty); | |
797 | add_echo_byte(ECHO_OP_START, tty); | |
798 | } else { | |
799 | add_echo_byte(c, tty); | |
800 | } | |
801 | ||
802 | mutex_unlock(&tty->echo_lock); | |
803 | } | |
1da177e4 | 804 | |
1da177e4 | 805 | /** |
a88a69c9 | 806 | * echo_char - echo a character |
1da177e4 LT |
807 | * @c: unicode byte to echo |
808 | * @tty: terminal device | |
809 | * | |
4edf1827 | 810 | * Echo user input back onto the screen. This must be called only when |
1da177e4 | 811 | * L_ECHO(tty) is true. Called from the driver receive_buf path. |
17b82060 | 812 | * |
a88a69c9 JP |
813 | * This variant tags control characters to be possibly echoed as |
814 | * as "^X" (where X is the letter representing the control char). | |
815 | * | |
816 | * Locking: echo_lock to protect the echo buffer | |
1da177e4 LT |
817 | */ |
818 | ||
819 | static void echo_char(unsigned char c, struct tty_struct *tty) | |
820 | { | |
a88a69c9 JP |
821 | mutex_lock(&tty->echo_lock); |
822 | ||
823 | if (c == ECHO_OP_START) { | |
824 | add_echo_byte(ECHO_OP_START, tty); | |
825 | add_echo_byte(ECHO_OP_START, tty); | |
826 | } else { | |
827 | if (iscntrl(c) && c != '\t') | |
828 | add_echo_byte(ECHO_OP_START, tty); | |
829 | add_echo_byte(c, tty); | |
830 | } | |
831 | ||
832 | mutex_unlock(&tty->echo_lock); | |
1da177e4 LT |
833 | } |
834 | ||
17b82060 | 835 | /** |
a88a69c9 | 836 | * finish_erasing - complete erase |
17b82060 | 837 | * @tty: tty doing the erase |
17b82060 | 838 | */ |
a88a69c9 | 839 | |
1da177e4 LT |
840 | static inline void finish_erasing(struct tty_struct *tty) |
841 | { | |
842 | if (tty->erasing) { | |
a88a69c9 | 843 | echo_char_raw('/', tty); |
1da177e4 LT |
844 | tty->erasing = 0; |
845 | } | |
846 | } | |
847 | ||
848 | /** | |
849 | * eraser - handle erase function | |
850 | * @c: character input | |
851 | * @tty: terminal device | |
852 | * | |
3a4fa0a2 | 853 | * Perform erase and necessary output when an erase character is |
1da177e4 LT |
854 | * present in the stream from the driver layer. Handles the complexities |
855 | * of UTF-8 multibyte symbols. | |
17b82060 | 856 | * |
a88a69c9 | 857 | * Locking: read_lock for tty buffers |
1da177e4 | 858 | */ |
4edf1827 | 859 | |
1da177e4 LT |
860 | static void eraser(unsigned char c, struct tty_struct *tty) |
861 | { | |
862 | enum { ERASE, WERASE, KILL } kill_type; | |
863 | int head, seen_alnums, cnt; | |
864 | unsigned long flags; | |
865 | ||
17b82060 | 866 | /* FIXME: locking needed ? */ |
1da177e4 | 867 | if (tty->read_head == tty->canon_head) { |
7e94b1d9 | 868 | /* process_output('\a', tty); */ /* what do you think? */ |
1da177e4 LT |
869 | return; |
870 | } | |
871 | if (c == ERASE_CHAR(tty)) | |
872 | kill_type = ERASE; | |
873 | else if (c == WERASE_CHAR(tty)) | |
874 | kill_type = WERASE; | |
875 | else { | |
876 | if (!L_ECHO(tty)) { | |
877 | spin_lock_irqsave(&tty->read_lock, flags); | |
878 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & | |
879 | (N_TTY_BUF_SIZE - 1)); | |
880 | tty->read_head = tty->canon_head; | |
881 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
882 | return; | |
883 | } | |
884 | if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { | |
885 | spin_lock_irqsave(&tty->read_lock, flags); | |
886 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & | |
887 | (N_TTY_BUF_SIZE - 1)); | |
888 | tty->read_head = tty->canon_head; | |
889 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
890 | finish_erasing(tty); | |
891 | echo_char(KILL_CHAR(tty), tty); | |
892 | /* Add a newline if ECHOK is on and ECHOKE is off. */ | |
893 | if (L_ECHOK(tty)) | |
a88a69c9 | 894 | echo_char_raw('\n', tty); |
1da177e4 LT |
895 | return; |
896 | } | |
897 | kill_type = KILL; | |
898 | } | |
899 | ||
900 | seen_alnums = 0; | |
17b82060 | 901 | /* FIXME: Locking ?? */ |
1da177e4 LT |
902 | while (tty->read_head != tty->canon_head) { |
903 | head = tty->read_head; | |
904 | ||
905 | /* erase a single possibly multibyte character */ | |
906 | do { | |
907 | head = (head - 1) & (N_TTY_BUF_SIZE-1); | |
908 | c = tty->read_buf[head]; | |
909 | } while (is_continuation(c, tty) && head != tty->canon_head); | |
910 | ||
911 | /* do not partially erase */ | |
912 | if (is_continuation(c, tty)) | |
913 | break; | |
914 | ||
915 | if (kill_type == WERASE) { | |
916 | /* Equivalent to BSD's ALTWERASE. */ | |
917 | if (isalnum(c) || c == '_') | |
918 | seen_alnums++; | |
919 | else if (seen_alnums) | |
920 | break; | |
921 | } | |
922 | cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1); | |
923 | spin_lock_irqsave(&tty->read_lock, flags); | |
924 | tty->read_head = head; | |
925 | tty->read_cnt -= cnt; | |
926 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
927 | if (L_ECHO(tty)) { | |
928 | if (L_ECHOPRT(tty)) { | |
929 | if (!tty->erasing) { | |
a88a69c9 | 930 | echo_char_raw('\\', tty); |
1da177e4 LT |
931 | tty->erasing = 1; |
932 | } | |
933 | /* if cnt > 1, output a multi-byte character */ | |
934 | echo_char(c, tty); | |
935 | while (--cnt > 0) { | |
936 | head = (head+1) & (N_TTY_BUF_SIZE-1); | |
a88a69c9 JP |
937 | echo_char_raw(tty->read_buf[head], tty); |
938 | echo_move_back_col(tty); | |
1da177e4 LT |
939 | } |
940 | } else if (kill_type == ERASE && !L_ECHOE(tty)) { | |
941 | echo_char(ERASE_CHAR(tty), tty); | |
942 | } else if (c == '\t') { | |
a88a69c9 JP |
943 | unsigned int num_chars = 0; |
944 | int after_tab = 0; | |
945 | unsigned long tail = tty->read_head; | |
946 | ||
947 | /* | |
948 | * Count the columns used for characters | |
949 | * since the start of input or after a | |
950 | * previous tab. | |
951 | * This info is used to go back the correct | |
952 | * number of columns. | |
953 | */ | |
954 | while (tail != tty->canon_head) { | |
955 | tail = (tail-1) & (N_TTY_BUF_SIZE-1); | |
1da177e4 | 956 | c = tty->read_buf[tail]; |
a88a69c9 JP |
957 | if (c == '\t') { |
958 | after_tab = 1; | |
959 | break; | |
300a6204 | 960 | } else if (iscntrl(c)) { |
1da177e4 | 961 | if (L_ECHOCTL(tty)) |
a88a69c9 JP |
962 | num_chars += 2; |
963 | } else if (!is_continuation(c, tty)) { | |
964 | num_chars++; | |
965 | } | |
1da177e4 | 966 | } |
a88a69c9 | 967 | echo_erase_tab(num_chars, after_tab, tty); |
1da177e4 LT |
968 | } else { |
969 | if (iscntrl(c) && L_ECHOCTL(tty)) { | |
a88a69c9 JP |
970 | echo_char_raw('\b', tty); |
971 | echo_char_raw(' ', tty); | |
972 | echo_char_raw('\b', tty); | |
1da177e4 LT |
973 | } |
974 | if (!iscntrl(c) || L_ECHOCTL(tty)) { | |
a88a69c9 JP |
975 | echo_char_raw('\b', tty); |
976 | echo_char_raw(' ', tty); | |
977 | echo_char_raw('\b', tty); | |
1da177e4 LT |
978 | } |
979 | } | |
980 | } | |
981 | if (kill_type == ERASE) | |
982 | break; | |
983 | } | |
a88a69c9 | 984 | if (tty->read_head == tty->canon_head && L_ECHO(tty)) |
1da177e4 LT |
985 | finish_erasing(tty); |
986 | } | |
987 | ||
988 | /** | |
989 | * isig - handle the ISIG optio | |
990 | * @sig: signal | |
991 | * @tty: terminal | |
992 | * @flush: force flush | |
993 | * | |
994 | * Called when a signal is being sent due to terminal input. This | |
995 | * may caus terminal flushing to take place according to the termios | |
996 | * settings and character used. Called from the driver receive_buf | |
997 | * path so serialized. | |
17b82060 AC |
998 | * |
999 | * Locking: ctrl_lock, read_lock (both via flush buffer) | |
1da177e4 | 1000 | */ |
4edf1827 | 1001 | |
1da177e4 LT |
1002 | static inline void isig(int sig, struct tty_struct *tty, int flush) |
1003 | { | |
ab521dc0 EB |
1004 | if (tty->pgrp) |
1005 | kill_pgrp(tty->pgrp, sig, 1); | |
1da177e4 LT |
1006 | if (flush || !L_NOFLSH(tty)) { |
1007 | n_tty_flush_buffer(tty); | |
f34d7a5b | 1008 | tty_driver_flush_buffer(tty); |
1da177e4 LT |
1009 | } |
1010 | } | |
1011 | ||
1012 | /** | |
1013 | * n_tty_receive_break - handle break | |
1014 | * @tty: terminal | |
1015 | * | |
1016 | * An RS232 break event has been hit in the incoming bitstream. This | |
1017 | * can cause a variety of events depending upon the termios settings. | |
1018 | * | |
1019 | * Called from the receive_buf path so single threaded. | |
1020 | */ | |
4edf1827 | 1021 | |
1da177e4 LT |
1022 | static inline void n_tty_receive_break(struct tty_struct *tty) |
1023 | { | |
1024 | if (I_IGNBRK(tty)) | |
1025 | return; | |
1026 | if (I_BRKINT(tty)) { | |
1027 | isig(SIGINT, tty, 1); | |
1028 | return; | |
1029 | } | |
1030 | if (I_PARMRK(tty)) { | |
1031 | put_tty_queue('\377', tty); | |
1032 | put_tty_queue('\0', tty); | |
1033 | } | |
1034 | put_tty_queue('\0', tty); | |
1035 | wake_up_interruptible(&tty->read_wait); | |
1036 | } | |
1037 | ||
1038 | /** | |
1039 | * n_tty_receive_overrun - handle overrun reporting | |
1040 | * @tty: terminal | |
1041 | * | |
1042 | * Data arrived faster than we could process it. While the tty | |
1043 | * driver has flagged this the bits that were missed are gone | |
1044 | * forever. | |
1045 | * | |
1046 | * Called from the receive_buf path so single threaded. Does not | |
1047 | * need locking as num_overrun and overrun_time are function | |
1048 | * private. | |
1049 | */ | |
4edf1827 | 1050 | |
1da177e4 LT |
1051 | static inline void n_tty_receive_overrun(struct tty_struct *tty) |
1052 | { | |
1053 | char buf[64]; | |
1054 | ||
1055 | tty->num_overrun++; | |
1056 | if (time_before(tty->overrun_time, jiffies - HZ) || | |
1057 | time_after(tty->overrun_time, jiffies)) { | |
1058 | printk(KERN_WARNING "%s: %d input overrun(s)\n", | |
1059 | tty_name(tty, buf), | |
1060 | tty->num_overrun); | |
1061 | tty->overrun_time = jiffies; | |
1062 | tty->num_overrun = 0; | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | /** | |
1067 | * n_tty_receive_parity_error - error notifier | |
1068 | * @tty: terminal device | |
1069 | * @c: character | |
1070 | * | |
1071 | * Process a parity error and queue the right data to indicate | |
3a4fa0a2 | 1072 | * the error case if necessary. Locking as per n_tty_receive_buf. |
1da177e4 LT |
1073 | */ |
1074 | static inline void n_tty_receive_parity_error(struct tty_struct *tty, | |
1075 | unsigned char c) | |
1076 | { | |
4edf1827 | 1077 | if (I_IGNPAR(tty)) |
1da177e4 | 1078 | return; |
1da177e4 LT |
1079 | if (I_PARMRK(tty)) { |
1080 | put_tty_queue('\377', tty); | |
1081 | put_tty_queue('\0', tty); | |
1082 | put_tty_queue(c, tty); | |
1083 | } else if (I_INPCK(tty)) | |
1084 | put_tty_queue('\0', tty); | |
1085 | else | |
1086 | put_tty_queue(c, tty); | |
1087 | wake_up_interruptible(&tty->read_wait); | |
1088 | } | |
1089 | ||
1090 | /** | |
1091 | * n_tty_receive_char - perform processing | |
1092 | * @tty: terminal device | |
1093 | * @c: character | |
1094 | * | |
1095 | * Process an individual character of input received from the driver. | |
4edf1827 | 1096 | * This is serialized with respect to itself by the rules for the |
1da177e4 LT |
1097 | * driver above. |
1098 | */ | |
1099 | ||
1100 | static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) | |
1101 | { | |
1102 | unsigned long flags; | |
acc71bba | 1103 | int parmrk; |
1da177e4 LT |
1104 | |
1105 | if (tty->raw) { | |
1106 | put_tty_queue(c, tty); | |
1107 | return; | |
1108 | } | |
4edf1827 | 1109 | |
1da177e4 LT |
1110 | if (I_ISTRIP(tty)) |
1111 | c &= 0x7f; | |
1112 | if (I_IUCLC(tty) && L_IEXTEN(tty)) | |
300a6204 | 1113 | c = tolower(c); |
1da177e4 | 1114 | |
54d2a37e | 1115 | if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && |
a88a69c9 JP |
1116 | I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) && |
1117 | c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) { | |
54d2a37e | 1118 | start_tty(tty); |
a88a69c9 JP |
1119 | process_echoes(tty); |
1120 | } | |
54d2a37e | 1121 | |
1da177e4 LT |
1122 | if (tty->closing) { |
1123 | if (I_IXON(tty)) { | |
a88a69c9 | 1124 | if (c == START_CHAR(tty)) { |
1da177e4 | 1125 | start_tty(tty); |
a88a69c9 | 1126 | process_echoes(tty); |
300a6204 | 1127 | } else if (c == STOP_CHAR(tty)) |
1da177e4 LT |
1128 | stop_tty(tty); |
1129 | } | |
1130 | return; | |
1131 | } | |
1132 | ||
1133 | /* | |
1134 | * If the previous character was LNEXT, or we know that this | |
1135 | * character is not one of the characters that we'll have to | |
1136 | * handle specially, do shortcut processing to speed things | |
1137 | * up. | |
1138 | */ | |
1139 | if (!test_bit(c, tty->process_char_map) || tty->lnext) { | |
1da177e4 | 1140 | tty->lnext = 0; |
acc71bba JP |
1141 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; |
1142 | if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) { | |
1143 | /* beep if no space */ | |
7e94b1d9 JP |
1144 | if (L_ECHO(tty)) |
1145 | process_output('\a', tty); | |
acc71bba JP |
1146 | return; |
1147 | } | |
1148 | if (L_ECHO(tty)) { | |
1149 | finish_erasing(tty); | |
1da177e4 LT |
1150 | /* Record the column of first canon char. */ |
1151 | if (tty->canon_head == tty->read_head) | |
a88a69c9 | 1152 | echo_set_canon_col(tty); |
1da177e4 | 1153 | echo_char(c, tty); |
a88a69c9 | 1154 | process_echoes(tty); |
1da177e4 | 1155 | } |
acc71bba | 1156 | if (parmrk) |
1da177e4 LT |
1157 | put_tty_queue(c, tty); |
1158 | put_tty_queue(c, tty); | |
1159 | return; | |
1160 | } | |
4edf1827 | 1161 | |
1da177e4 LT |
1162 | if (I_IXON(tty)) { |
1163 | if (c == START_CHAR(tty)) { | |
1164 | start_tty(tty); | |
a88a69c9 | 1165 | process_echoes(tty); |
1da177e4 LT |
1166 | return; |
1167 | } | |
1168 | if (c == STOP_CHAR(tty)) { | |
1169 | stop_tty(tty); | |
1170 | return; | |
1171 | } | |
1172 | } | |
575537b3 | 1173 | |
1da177e4 LT |
1174 | if (L_ISIG(tty)) { |
1175 | int signal; | |
1176 | signal = SIGINT; | |
1177 | if (c == INTR_CHAR(tty)) | |
1178 | goto send_signal; | |
1179 | signal = SIGQUIT; | |
1180 | if (c == QUIT_CHAR(tty)) | |
1181 | goto send_signal; | |
1182 | signal = SIGTSTP; | |
1183 | if (c == SUSP_CHAR(tty)) { | |
1184 | send_signal: | |
ec5b1157 | 1185 | /* |
ec5b1157 JP |
1186 | * Note that we do not use isig() here because we want |
1187 | * the order to be: | |
1188 | * 1) flush, 2) echo, 3) signal | |
1189 | */ | |
1190 | if (!L_NOFLSH(tty)) { | |
1191 | n_tty_flush_buffer(tty); | |
f34d7a5b | 1192 | tty_driver_flush_buffer(tty); |
ec5b1157 | 1193 | } |
a88a69c9 JP |
1194 | if (I_IXON(tty)) |
1195 | start_tty(tty); | |
1196 | if (L_ECHO(tty)) { | |
ec5b1157 | 1197 | echo_char(c, tty); |
a88a69c9 JP |
1198 | process_echoes(tty); |
1199 | } | |
ec5b1157 JP |
1200 | if (tty->pgrp) |
1201 | kill_pgrp(tty->pgrp, signal, 1); | |
1da177e4 LT |
1202 | return; |
1203 | } | |
1204 | } | |
575537b3 JP |
1205 | |
1206 | if (c == '\r') { | |
1207 | if (I_IGNCR(tty)) | |
1208 | return; | |
1209 | if (I_ICRNL(tty)) | |
1210 | c = '\n'; | |
1211 | } else if (c == '\n' && I_INLCR(tty)) | |
1212 | c = '\r'; | |
1213 | ||
1da177e4 LT |
1214 | if (tty->icanon) { |
1215 | if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || | |
1216 | (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { | |
1217 | eraser(c, tty); | |
a88a69c9 | 1218 | process_echoes(tty); |
1da177e4 LT |
1219 | return; |
1220 | } | |
1221 | if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { | |
1222 | tty->lnext = 1; | |
1223 | if (L_ECHO(tty)) { | |
1224 | finish_erasing(tty); | |
1225 | if (L_ECHOCTL(tty)) { | |
a88a69c9 JP |
1226 | echo_char_raw('^', tty); |
1227 | echo_char_raw('\b', tty); | |
1228 | process_echoes(tty); | |
1da177e4 LT |
1229 | } |
1230 | } | |
1231 | return; | |
1232 | } | |
1233 | if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && | |
1234 | L_IEXTEN(tty)) { | |
1235 | unsigned long tail = tty->canon_head; | |
1236 | ||
1237 | finish_erasing(tty); | |
1238 | echo_char(c, tty); | |
a88a69c9 | 1239 | echo_char_raw('\n', tty); |
1da177e4 LT |
1240 | while (tail != tty->read_head) { |
1241 | echo_char(tty->read_buf[tail], tty); | |
1242 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); | |
1243 | } | |
a88a69c9 | 1244 | process_echoes(tty); |
1da177e4 LT |
1245 | return; |
1246 | } | |
1247 | if (c == '\n') { | |
acc71bba | 1248 | if (tty->read_cnt >= N_TTY_BUF_SIZE) { |
7e94b1d9 JP |
1249 | if (L_ECHO(tty)) |
1250 | process_output('\a', tty); | |
acc71bba JP |
1251 | return; |
1252 | } | |
1253 | if (L_ECHO(tty) || L_ECHONL(tty)) { | |
a88a69c9 JP |
1254 | echo_char_raw('\n', tty); |
1255 | process_echoes(tty); | |
1da177e4 LT |
1256 | } |
1257 | goto handle_newline; | |
1258 | } | |
1259 | if (c == EOF_CHAR(tty)) { | |
acc71bba JP |
1260 | if (tty->read_cnt >= N_TTY_BUF_SIZE) |
1261 | return; | |
4edf1827 AC |
1262 | if (tty->canon_head != tty->read_head) |
1263 | set_bit(TTY_PUSH, &tty->flags); | |
1da177e4 LT |
1264 | c = __DISABLED_CHAR; |
1265 | goto handle_newline; | |
1266 | } | |
1267 | if ((c == EOL_CHAR(tty)) || | |
1268 | (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { | |
acc71bba JP |
1269 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) |
1270 | ? 1 : 0; | |
1271 | if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) { | |
7e94b1d9 JP |
1272 | if (L_ECHO(tty)) |
1273 | process_output('\a', tty); | |
acc71bba JP |
1274 | return; |
1275 | } | |
1da177e4 LT |
1276 | /* |
1277 | * XXX are EOL_CHAR and EOL2_CHAR echoed?!? | |
1278 | */ | |
1279 | if (L_ECHO(tty)) { | |
1da177e4 LT |
1280 | /* Record the column of first canon char. */ |
1281 | if (tty->canon_head == tty->read_head) | |
a88a69c9 | 1282 | echo_set_canon_col(tty); |
1da177e4 | 1283 | echo_char(c, tty); |
a88a69c9 | 1284 | process_echoes(tty); |
1da177e4 LT |
1285 | } |
1286 | /* | |
1287 | * XXX does PARMRK doubling happen for | |
1288 | * EOL_CHAR and EOL2_CHAR? | |
1289 | */ | |
acc71bba | 1290 | if (parmrk) |
1da177e4 LT |
1291 | put_tty_queue(c, tty); |
1292 | ||
4edf1827 | 1293 | handle_newline: |
1da177e4 LT |
1294 | spin_lock_irqsave(&tty->read_lock, flags); |
1295 | set_bit(tty->read_head, tty->read_flags); | |
1296 | put_tty_queue_nolock(c, tty); | |
1297 | tty->canon_head = tty->read_head; | |
1298 | tty->canon_data++; | |
1299 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
1300 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | |
1301 | if (waitqueue_active(&tty->read_wait)) | |
1302 | wake_up_interruptible(&tty->read_wait); | |
1303 | return; | |
1304 | } | |
1305 | } | |
4edf1827 | 1306 | |
acc71bba JP |
1307 | parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0; |
1308 | if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) { | |
1309 | /* beep if no space */ | |
7e94b1d9 JP |
1310 | if (L_ECHO(tty)) |
1311 | process_output('\a', tty); | |
acc71bba JP |
1312 | return; |
1313 | } | |
1314 | if (L_ECHO(tty)) { | |
1315 | finish_erasing(tty); | |
1da177e4 | 1316 | if (c == '\n') |
a88a69c9 | 1317 | echo_char_raw('\n', tty); |
1da177e4 LT |
1318 | else { |
1319 | /* Record the column of first canon char. */ | |
1320 | if (tty->canon_head == tty->read_head) | |
a88a69c9 | 1321 | echo_set_canon_col(tty); |
1da177e4 LT |
1322 | echo_char(c, tty); |
1323 | } | |
a88a69c9 | 1324 | process_echoes(tty); |
1da177e4 LT |
1325 | } |
1326 | ||
acc71bba | 1327 | if (parmrk) |
1da177e4 LT |
1328 | put_tty_queue(c, tty); |
1329 | ||
1330 | put_tty_queue(c, tty); | |
4edf1827 | 1331 | } |
1da177e4 | 1332 | |
1da177e4 LT |
1333 | |
1334 | /** | |
1335 | * n_tty_write_wakeup - asynchronous I/O notifier | |
1336 | * @tty: tty device | |
1337 | * | |
1338 | * Required for the ptys, serial driver etc. since processes | |
1339 | * that attach themselves to the master and rely on ASYNC | |
1340 | * IO must be woken up | |
1341 | */ | |
1342 | ||
1343 | static void n_tty_write_wakeup(struct tty_struct *tty) | |
1344 | { | |
a88a69c9 JP |
1345 | /* Write out any echoed characters that are still pending */ |
1346 | process_echoes(tty); | |
300a6204 | 1347 | |
ff8cb0fd | 1348 | if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) |
1da177e4 | 1349 | kill_fasync(&tty->fasync, SIGIO, POLL_OUT); |
1da177e4 LT |
1350 | } |
1351 | ||
1352 | /** | |
1353 | * n_tty_receive_buf - data receive | |
1354 | * @tty: terminal device | |
1355 | * @cp: buffer | |
1356 | * @fp: flag buffer | |
1357 | * @count: characters | |
1358 | * | |
1359 | * Called by the terminal driver when a block of characters has | |
1360 | * been received. This function must be called from soft contexts | |
1361 | * not from interrupt context. The driver is responsible for making | |
1362 | * calls one at a time and in order (or using flush_to_ldisc) | |
1363 | */ | |
4edf1827 | 1364 | |
1da177e4 LT |
1365 | static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, |
1366 | char *fp, int count) | |
1367 | { | |
1368 | const unsigned char *p; | |
1369 | char *f, flags = TTY_NORMAL; | |
1370 | int i; | |
1371 | char buf[64]; | |
1372 | unsigned long cpuflags; | |
1373 | ||
1374 | if (!tty->read_buf) | |
1375 | return; | |
1376 | ||
1377 | if (tty->real_raw) { | |
1378 | spin_lock_irqsave(&tty->read_lock, cpuflags); | |
1379 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, | |
1380 | N_TTY_BUF_SIZE - tty->read_head); | |
1381 | i = min(count, i); | |
1382 | memcpy(tty->read_buf + tty->read_head, cp, i); | |
1383 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); | |
1384 | tty->read_cnt += i; | |
1385 | cp += i; | |
1386 | count -= i; | |
1387 | ||
1388 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, | |
1389 | N_TTY_BUF_SIZE - tty->read_head); | |
1390 | i = min(count, i); | |
1391 | memcpy(tty->read_buf + tty->read_head, cp, i); | |
1392 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); | |
1393 | tty->read_cnt += i; | |
1394 | spin_unlock_irqrestore(&tty->read_lock, cpuflags); | |
1395 | } else { | |
4edf1827 | 1396 | for (i = count, p = cp, f = fp; i; i--, p++) { |
1da177e4 LT |
1397 | if (f) |
1398 | flags = *f++; | |
1399 | switch (flags) { | |
1400 | case TTY_NORMAL: | |
1401 | n_tty_receive_char(tty, *p); | |
1402 | break; | |
1403 | case TTY_BREAK: | |
1404 | n_tty_receive_break(tty); | |
1405 | break; | |
1406 | case TTY_PARITY: | |
1407 | case TTY_FRAME: | |
1408 | n_tty_receive_parity_error(tty, *p); | |
1409 | break; | |
1410 | case TTY_OVERRUN: | |
1411 | n_tty_receive_overrun(tty); | |
1412 | break; | |
1413 | default: | |
4edf1827 | 1414 | printk(KERN_ERR "%s: unknown flag %d\n", |
1da177e4 LT |
1415 | tty_name(tty, buf), flags); |
1416 | break; | |
1417 | } | |
1418 | } | |
f34d7a5b AC |
1419 | if (tty->ops->flush_chars) |
1420 | tty->ops->flush_chars(tty); | |
1da177e4 LT |
1421 | } |
1422 | ||
33f0f88f AC |
1423 | n_tty_set_room(tty); |
1424 | ||
1da177e4 LT |
1425 | if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) { |
1426 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | |
1427 | if (waitqueue_active(&tty->read_wait)) | |
1428 | wake_up_interruptible(&tty->read_wait); | |
1429 | } | |
1430 | ||
1431 | /* | |
1432 | * Check the remaining room for the input canonicalization | |
1433 | * mode. We don't want to throttle the driver if we're in | |
1434 | * canonical mode and don't have a newline yet! | |
1435 | */ | |
39c2e60f AC |
1436 | if (tty->receive_room < TTY_THRESHOLD_THROTTLE) |
1437 | tty_throttle(tty); | |
1da177e4 LT |
1438 | } |
1439 | ||
1440 | int is_ignored(int sig) | |
1441 | { | |
1442 | return (sigismember(¤t->blocked, sig) || | |
4edf1827 | 1443 | current->sighand->action[sig-1].sa.sa_handler == SIG_IGN); |
1da177e4 LT |
1444 | } |
1445 | ||
1446 | /** | |
1447 | * n_tty_set_termios - termios data changed | |
1448 | * @tty: terminal | |
1449 | * @old: previous data | |
1450 | * | |
1451 | * Called by the tty layer when the user changes termios flags so | |
1452 | * that the line discipline can plan ahead. This function cannot sleep | |
4edf1827 | 1453 | * and is protected from re-entry by the tty layer. The user is |
1da177e4 LT |
1454 | * guaranteed that this function will not be re-entered or in progress |
1455 | * when the ldisc is closed. | |
17b82060 AC |
1456 | * |
1457 | * Locking: Caller holds tty->termios_mutex | |
1da177e4 | 1458 | */ |
4edf1827 AC |
1459 | |
1460 | static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) | |
1da177e4 | 1461 | { |
47afa7a5 AC |
1462 | int canon_change = 1; |
1463 | BUG_ON(!tty); | |
1464 | ||
1465 | if (old) | |
1466 | canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON; | |
1467 | if (canon_change) { | |
1468 | memset(&tty->read_flags, 0, sizeof tty->read_flags); | |
1469 | tty->canon_head = tty->read_tail; | |
1470 | tty->canon_data = 0; | |
1471 | tty->erasing = 0; | |
1472 | } | |
1473 | ||
1474 | if (canon_change && !L_ICANON(tty) && tty->read_cnt) | |
1475 | wake_up_interruptible(&tty->read_wait); | |
4edf1827 | 1476 | |
1da177e4 LT |
1477 | tty->icanon = (L_ICANON(tty) != 0); |
1478 | if (test_bit(TTY_HW_COOK_IN, &tty->flags)) { | |
1479 | tty->raw = 1; | |
1480 | tty->real_raw = 1; | |
33f0f88f | 1481 | n_tty_set_room(tty); |
1da177e4 LT |
1482 | return; |
1483 | } | |
1484 | if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || | |
1485 | I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || | |
1486 | I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || | |
1487 | I_PARMRK(tty)) { | |
1488 | memset(tty->process_char_map, 0, 256/8); | |
1489 | ||
1490 | if (I_IGNCR(tty) || I_ICRNL(tty)) | |
1491 | set_bit('\r', tty->process_char_map); | |
1492 | if (I_INLCR(tty)) | |
1493 | set_bit('\n', tty->process_char_map); | |
1494 | ||
1495 | if (L_ICANON(tty)) { | |
1496 | set_bit(ERASE_CHAR(tty), tty->process_char_map); | |
1497 | set_bit(KILL_CHAR(tty), tty->process_char_map); | |
1498 | set_bit(EOF_CHAR(tty), tty->process_char_map); | |
1499 | set_bit('\n', tty->process_char_map); | |
1500 | set_bit(EOL_CHAR(tty), tty->process_char_map); | |
1501 | if (L_IEXTEN(tty)) { | |
1502 | set_bit(WERASE_CHAR(tty), | |
1503 | tty->process_char_map); | |
1504 | set_bit(LNEXT_CHAR(tty), | |
1505 | tty->process_char_map); | |
1506 | set_bit(EOL2_CHAR(tty), | |
1507 | tty->process_char_map); | |
1508 | if (L_ECHO(tty)) | |
1509 | set_bit(REPRINT_CHAR(tty), | |
1510 | tty->process_char_map); | |
1511 | } | |
1512 | } | |
1513 | if (I_IXON(tty)) { | |
1514 | set_bit(START_CHAR(tty), tty->process_char_map); | |
1515 | set_bit(STOP_CHAR(tty), tty->process_char_map); | |
1516 | } | |
1517 | if (L_ISIG(tty)) { | |
1518 | set_bit(INTR_CHAR(tty), tty->process_char_map); | |
1519 | set_bit(QUIT_CHAR(tty), tty->process_char_map); | |
1520 | set_bit(SUSP_CHAR(tty), tty->process_char_map); | |
1521 | } | |
1522 | clear_bit(__DISABLED_CHAR, tty->process_char_map); | |
1523 | tty->raw = 0; | |
1524 | tty->real_raw = 0; | |
1525 | } else { | |
1526 | tty->raw = 1; | |
1527 | if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && | |
1528 | (I_IGNPAR(tty) || !I_INPCK(tty)) && | |
1529 | (tty->driver->flags & TTY_DRIVER_REAL_RAW)) | |
1530 | tty->real_raw = 1; | |
1531 | else | |
1532 | tty->real_raw = 0; | |
1533 | } | |
33f0f88f | 1534 | n_tty_set_room(tty); |
f34d7a5b AC |
1535 | /* The termios change make the tty ready for I/O */ |
1536 | wake_up_interruptible(&tty->write_wait); | |
1537 | wake_up_interruptible(&tty->read_wait); | |
1da177e4 LT |
1538 | } |
1539 | ||
1540 | /** | |
1541 | * n_tty_close - close the ldisc for this tty | |
1542 | * @tty: device | |
1543 | * | |
4edf1827 AC |
1544 | * Called from the terminal layer when this line discipline is |
1545 | * being shut down, either because of a close or becsuse of a | |
1da177e4 LT |
1546 | * discipline change. The function will not be called while other |
1547 | * ldisc methods are in progress. | |
1548 | */ | |
4edf1827 | 1549 | |
1da177e4 LT |
1550 | static void n_tty_close(struct tty_struct *tty) |
1551 | { | |
1552 | n_tty_flush_buffer(tty); | |
1553 | if (tty->read_buf) { | |
1554 | free_buf(tty->read_buf); | |
1555 | tty->read_buf = NULL; | |
1556 | } | |
a88a69c9 JP |
1557 | if (tty->echo_buf) { |
1558 | free_buf(tty->echo_buf); | |
1559 | tty->echo_buf = NULL; | |
1560 | } | |
1da177e4 LT |
1561 | } |
1562 | ||
1563 | /** | |
1564 | * n_tty_open - open an ldisc | |
1565 | * @tty: terminal to open | |
1566 | * | |
4edf1827 | 1567 | * Called when this line discipline is being attached to the |
1da177e4 LT |
1568 | * terminal device. Can sleep. Called serialized so that no |
1569 | * other events will occur in parallel. No further open will occur | |
1570 | * until a close. | |
1571 | */ | |
1572 | ||
1573 | static int n_tty_open(struct tty_struct *tty) | |
1574 | { | |
1575 | if (!tty) | |
1576 | return -EINVAL; | |
1577 | ||
a88a69c9 | 1578 | /* These are ugly. Currently a malloc failure here can panic */ |
1da177e4 LT |
1579 | if (!tty->read_buf) { |
1580 | tty->read_buf = alloc_buf(); | |
1581 | if (!tty->read_buf) | |
1582 | return -ENOMEM; | |
1583 | } | |
a88a69c9 JP |
1584 | if (!tty->echo_buf) { |
1585 | tty->echo_buf = alloc_buf(); | |
1586 | if (!tty->echo_buf) | |
1587 | return -ENOMEM; | |
1588 | } | |
1da177e4 | 1589 | memset(tty->read_buf, 0, N_TTY_BUF_SIZE); |
a88a69c9 | 1590 | memset(tty->echo_buf, 0, N_TTY_BUF_SIZE); |
1da177e4 LT |
1591 | reset_buffer_flags(tty); |
1592 | tty->column = 0; | |
1593 | n_tty_set_termios(tty, NULL); | |
1594 | tty->minimum_to_wake = 1; | |
1595 | tty->closing = 0; | |
1596 | return 0; | |
1597 | } | |
1598 | ||
1599 | static inline int input_available_p(struct tty_struct *tty, int amt) | |
1600 | { | |
1601 | if (tty->icanon) { | |
1602 | if (tty->canon_data) | |
1603 | return 1; | |
1604 | } else if (tty->read_cnt >= (amt ? amt : 1)) | |
1605 | return 1; | |
1606 | ||
1607 | return 0; | |
1608 | } | |
1609 | ||
1610 | /** | |
1611 | * copy_from_read_buf - copy read data directly | |
1612 | * @tty: terminal device | |
1613 | * @b: user data | |
1614 | * @nr: size of data | |
1615 | * | |
11a96d18 | 1616 | * Helper function to speed up n_tty_read. It is only called when |
1da177e4 LT |
1617 | * ICANON is off; it copies characters straight from the tty queue to |
1618 | * user space directly. It can be profitably called twice; once to | |
1619 | * drain the space from the tail pointer to the (physical) end of the | |
1620 | * buffer, and once to drain the space from the (physical) beginning of | |
1621 | * the buffer to head pointer. | |
1622 | * | |
817d6d3b | 1623 | * Called under the tty->atomic_read_lock sem |
1da177e4 LT |
1624 | * |
1625 | */ | |
4edf1827 | 1626 | |
33f0f88f | 1627 | static int copy_from_read_buf(struct tty_struct *tty, |
1da177e4 LT |
1628 | unsigned char __user **b, |
1629 | size_t *nr) | |
1630 | ||
1631 | { | |
1632 | int retval; | |
1633 | size_t n; | |
1634 | unsigned long flags; | |
1635 | ||
1636 | retval = 0; | |
1637 | spin_lock_irqsave(&tty->read_lock, flags); | |
1638 | n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail); | |
1639 | n = min(*nr, n); | |
1640 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
1641 | if (n) { | |
1da177e4 LT |
1642 | retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n); |
1643 | n -= retval; | |
522ed776 | 1644 | tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n); |
1da177e4 LT |
1645 | spin_lock_irqsave(&tty->read_lock, flags); |
1646 | tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1); | |
1647 | tty->read_cnt -= n; | |
1648 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
1649 | *b += n; | |
1650 | *nr -= n; | |
1651 | } | |
1652 | return retval; | |
1653 | } | |
1654 | ||
cc4191dc | 1655 | extern ssize_t redirected_tty_write(struct file *, const char __user *, |
4edf1827 | 1656 | size_t, loff_t *); |
1da177e4 LT |
1657 | |
1658 | /** | |
1659 | * job_control - check job control | |
1660 | * @tty: tty | |
1661 | * @file: file handle | |
1662 | * | |
1663 | * Perform job control management checks on this file/tty descriptor | |
4edf1827 | 1664 | * and if appropriate send any needed signals and return a negative |
1da177e4 | 1665 | * error code if action should be taken. |
04f378b1 AC |
1666 | * |
1667 | * FIXME: | |
1668 | * Locking: None - redirected write test is safe, testing | |
1669 | * current->signal should possibly lock current->sighand | |
1670 | * pgrp locking ? | |
1da177e4 | 1671 | */ |
4edf1827 | 1672 | |
1da177e4 LT |
1673 | static int job_control(struct tty_struct *tty, struct file *file) |
1674 | { | |
1675 | /* Job control check -- must be done at start and after | |
1676 | every sleep (POSIX.1 7.1.1.4). */ | |
1677 | /* NOTE: not yet done after every sleep pending a thorough | |
1678 | check of the logic of this change. -- jlc */ | |
1679 | /* don't stop on /dev/console */ | |
1680 | if (file->f_op->write != redirected_tty_write && | |
1681 | current->signal->tty == tty) { | |
ab521dc0 | 1682 | if (!tty->pgrp) |
11a96d18 | 1683 | printk(KERN_ERR "n_tty_read: no tty->pgrp!\n"); |
ab521dc0 | 1684 | else if (task_pgrp(current) != tty->pgrp) { |
1da177e4 | 1685 | if (is_ignored(SIGTTIN) || |
3e7cd6c4 | 1686 | is_current_pgrp_orphaned()) |
1da177e4 | 1687 | return -EIO; |
ab521dc0 | 1688 | kill_pgrp(task_pgrp(current), SIGTTIN, 1); |
040b6362 | 1689 | set_thread_flag(TIF_SIGPENDING); |
1da177e4 LT |
1690 | return -ERESTARTSYS; |
1691 | } | |
1692 | } | |
1693 | return 0; | |
1694 | } | |
4edf1827 | 1695 | |
1da177e4 LT |
1696 | |
1697 | /** | |
11a96d18 | 1698 | * n_tty_read - read function for tty |
1da177e4 LT |
1699 | * @tty: tty device |
1700 | * @file: file object | |
1701 | * @buf: userspace buffer pointer | |
1702 | * @nr: size of I/O | |
1703 | * | |
1704 | * Perform reads for the line discipline. We are guaranteed that the | |
1705 | * line discipline will not be closed under us but we may get multiple | |
1706 | * parallel readers and must handle this ourselves. We may also get | |
1707 | * a hangup. Always called in user context, may sleep. | |
1708 | * | |
1709 | * This code must be sure never to sleep through a hangup. | |
1710 | */ | |
4edf1827 | 1711 | |
11a96d18 | 1712 | static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, |
1da177e4 LT |
1713 | unsigned char __user *buf, size_t nr) |
1714 | { | |
1715 | unsigned char __user *b = buf; | |
1716 | DECLARE_WAITQUEUE(wait, current); | |
1717 | int c; | |
1718 | int minimum, time; | |
1719 | ssize_t retval = 0; | |
1720 | ssize_t size; | |
1721 | long timeout; | |
1722 | unsigned long flags; | |
04f378b1 | 1723 | int packet; |
1da177e4 LT |
1724 | |
1725 | do_it_again: | |
1726 | ||
17b82060 | 1727 | BUG_ON(!tty->read_buf); |
1da177e4 LT |
1728 | |
1729 | c = job_control(tty, file); | |
4edf1827 | 1730 | if (c < 0) |
1da177e4 | 1731 | return c; |
4edf1827 | 1732 | |
1da177e4 LT |
1733 | minimum = time = 0; |
1734 | timeout = MAX_SCHEDULE_TIMEOUT; | |
1735 | if (!tty->icanon) { | |
1736 | time = (HZ / 10) * TIME_CHAR(tty); | |
1737 | minimum = MIN_CHAR(tty); | |
1738 | if (minimum) { | |
1739 | if (time) | |
1740 | tty->minimum_to_wake = 1; | |
1741 | else if (!waitqueue_active(&tty->read_wait) || | |
1742 | (tty->minimum_to_wake > minimum)) | |
1743 | tty->minimum_to_wake = minimum; | |
1744 | } else { | |
1745 | timeout = 0; | |
1746 | if (time) { | |
1747 | timeout = time; | |
1748 | time = 0; | |
1749 | } | |
1750 | tty->minimum_to_wake = minimum = 1; | |
1751 | } | |
1752 | } | |
1753 | ||
1754 | /* | |
1755 | * Internal serialization of reads. | |
1756 | */ | |
1757 | if (file->f_flags & O_NONBLOCK) { | |
70522e12 | 1758 | if (!mutex_trylock(&tty->atomic_read_lock)) |
1da177e4 | 1759 | return -EAGAIN; |
4edf1827 | 1760 | } else { |
70522e12 | 1761 | if (mutex_lock_interruptible(&tty->atomic_read_lock)) |
1da177e4 LT |
1762 | return -ERESTARTSYS; |
1763 | } | |
04f378b1 | 1764 | packet = tty->packet; |
1da177e4 LT |
1765 | |
1766 | add_wait_queue(&tty->read_wait, &wait); | |
1da177e4 LT |
1767 | while (nr) { |
1768 | /* First test for status change. */ | |
04f378b1 | 1769 | if (packet && tty->link->ctrl_status) { |
1da177e4 LT |
1770 | unsigned char cs; |
1771 | if (b != buf) | |
1772 | break; | |
04f378b1 | 1773 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); |
1da177e4 LT |
1774 | cs = tty->link->ctrl_status; |
1775 | tty->link->ctrl_status = 0; | |
04f378b1 | 1776 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); |
522ed776 | 1777 | if (tty_put_user(tty, cs, b++)) { |
1da177e4 LT |
1778 | retval = -EFAULT; |
1779 | b--; | |
1780 | break; | |
1781 | } | |
1782 | nr--; | |
1783 | break; | |
1784 | } | |
1785 | /* This statement must be first before checking for input | |
1786 | so that any interrupt will set the state back to | |
1787 | TASK_RUNNING. */ | |
1788 | set_current_state(TASK_INTERRUPTIBLE); | |
4edf1827 | 1789 | |
1da177e4 LT |
1790 | if (((minimum - (b - buf)) < tty->minimum_to_wake) && |
1791 | ((minimum - (b - buf)) >= 1)) | |
1792 | tty->minimum_to_wake = (minimum - (b - buf)); | |
4edf1827 | 1793 | |
1da177e4 LT |
1794 | if (!input_available_p(tty, 0)) { |
1795 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { | |
1796 | retval = -EIO; | |
1797 | break; | |
1798 | } | |
1799 | if (tty_hung_up_p(file)) | |
1800 | break; | |
1801 | if (!timeout) | |
1802 | break; | |
1803 | if (file->f_flags & O_NONBLOCK) { | |
1804 | retval = -EAGAIN; | |
1805 | break; | |
1806 | } | |
1807 | if (signal_pending(current)) { | |
1808 | retval = -ERESTARTSYS; | |
1809 | break; | |
1810 | } | |
04f378b1 | 1811 | /* FIXME: does n_tty_set_room need locking ? */ |
33f0f88f | 1812 | n_tty_set_room(tty); |
1da177e4 | 1813 | timeout = schedule_timeout(timeout); |
1da177e4 LT |
1814 | continue; |
1815 | } | |
1816 | __set_current_state(TASK_RUNNING); | |
1817 | ||
1818 | /* Deal with packet mode. */ | |
04f378b1 | 1819 | if (packet && b == buf) { |
522ed776 | 1820 | if (tty_put_user(tty, TIOCPKT_DATA, b++)) { |
1da177e4 LT |
1821 | retval = -EFAULT; |
1822 | b--; | |
1823 | break; | |
1824 | } | |
1825 | nr--; | |
1826 | } | |
1827 | ||
1828 | if (tty->icanon) { | |
1829 | /* N.B. avoid overrun if nr == 0 */ | |
1830 | while (nr && tty->read_cnt) { | |
4edf1827 | 1831 | int eol; |
1da177e4 LT |
1832 | |
1833 | eol = test_and_clear_bit(tty->read_tail, | |
1834 | tty->read_flags); | |
1835 | c = tty->read_buf[tty->read_tail]; | |
1836 | spin_lock_irqsave(&tty->read_lock, flags); | |
1837 | tty->read_tail = ((tty->read_tail+1) & | |
1838 | (N_TTY_BUF_SIZE-1)); | |
1839 | tty->read_cnt--; | |
1840 | if (eol) { | |
1841 | /* this test should be redundant: | |
1842 | * we shouldn't be reading data if | |
1843 | * canon_data is 0 | |
1844 | */ | |
1845 | if (--tty->canon_data < 0) | |
1846 | tty->canon_data = 0; | |
1847 | } | |
1848 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
1849 | ||
1850 | if (!eol || (c != __DISABLED_CHAR)) { | |
522ed776 | 1851 | if (tty_put_user(tty, c, b++)) { |
1da177e4 LT |
1852 | retval = -EFAULT; |
1853 | b--; | |
1854 | break; | |
1855 | } | |
1856 | nr--; | |
1857 | } | |
522ed776 MT |
1858 | if (eol) { |
1859 | tty_audit_push(tty); | |
1da177e4 | 1860 | break; |
522ed776 | 1861 | } |
1da177e4 LT |
1862 | } |
1863 | if (retval) | |
1864 | break; | |
1865 | } else { | |
1866 | int uncopied; | |
04f378b1 AC |
1867 | /* The copy function takes the read lock and handles |
1868 | locking internally for this case */ | |
1da177e4 LT |
1869 | uncopied = copy_from_read_buf(tty, &b, &nr); |
1870 | uncopied += copy_from_read_buf(tty, &b, &nr); | |
1871 | if (uncopied) { | |
1872 | retval = -EFAULT; | |
1873 | break; | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | /* If there is enough space in the read buffer now, let the | |
1878 | * low-level driver know. We use n_tty_chars_in_buffer() to | |
1879 | * check the buffer, as it now knows about canonical mode. | |
1880 | * Otherwise, if the driver is throttled and the line is | |
1881 | * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, | |
1882 | * we won't get any more characters. | |
1883 | */ | |
289a1e99 PM |
1884 | if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) { |
1885 | n_tty_set_room(tty); | |
1da177e4 | 1886 | check_unthrottle(tty); |
289a1e99 | 1887 | } |
1da177e4 LT |
1888 | |
1889 | if (b - buf >= minimum) | |
1890 | break; | |
1891 | if (time) | |
1892 | timeout = time; | |
1893 | } | |
70522e12 | 1894 | mutex_unlock(&tty->atomic_read_lock); |
1da177e4 LT |
1895 | remove_wait_queue(&tty->read_wait, &wait); |
1896 | ||
1897 | if (!waitqueue_active(&tty->read_wait)) | |
1898 | tty->minimum_to_wake = minimum; | |
1899 | ||
1900 | __set_current_state(TASK_RUNNING); | |
1901 | size = b - buf; | |
1902 | if (size) { | |
1903 | retval = size; | |
1904 | if (nr) | |
4edf1827 | 1905 | clear_bit(TTY_PUSH, &tty->flags); |
1da177e4 LT |
1906 | } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) |
1907 | goto do_it_again; | |
1908 | ||
33f0f88f | 1909 | n_tty_set_room(tty); |
1da177e4 LT |
1910 | return retval; |
1911 | } | |
1912 | ||
1913 | /** | |
11a96d18 | 1914 | * n_tty_write - write function for tty |
1da177e4 LT |
1915 | * @tty: tty device |
1916 | * @file: file object | |
1917 | * @buf: userspace buffer pointer | |
1918 | * @nr: size of I/O | |
1919 | * | |
a88a69c9 | 1920 | * Write function of the terminal device. This is serialized with |
1da177e4 | 1921 | * respect to other write callers but not to termios changes, reads |
a88a69c9 JP |
1922 | * and other such events. Since the receive code will echo characters, |
1923 | * thus calling driver write methods, the output_lock is used in | |
1924 | * the output processing functions called here as well as in the | |
1925 | * echo processing function to protect the column state and space | |
1926 | * left in the buffer. | |
1da177e4 LT |
1927 | * |
1928 | * This code must be sure never to sleep through a hangup. | |
a88a69c9 JP |
1929 | * |
1930 | * Locking: output_lock to protect column state and space left | |
1931 | * (note that the process_output*() functions take this | |
1932 | * lock themselves) | |
1da177e4 | 1933 | */ |
4edf1827 | 1934 | |
11a96d18 | 1935 | static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, |
a88a69c9 | 1936 | const unsigned char *buf, size_t nr) |
1da177e4 LT |
1937 | { |
1938 | const unsigned char *b = buf; | |
1939 | DECLARE_WAITQUEUE(wait, current); | |
1940 | int c; | |
1941 | ssize_t retval = 0; | |
1942 | ||
1943 | /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ | |
1944 | if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { | |
1945 | retval = tty_check_change(tty); | |
1946 | if (retval) | |
1947 | return retval; | |
1948 | } | |
1949 | ||
a88a69c9 JP |
1950 | /* Write out any echoed characters that are still pending */ |
1951 | process_echoes(tty); | |
300a6204 | 1952 | |
1da177e4 LT |
1953 | add_wait_queue(&tty->write_wait, &wait); |
1954 | while (1) { | |
1955 | set_current_state(TASK_INTERRUPTIBLE); | |
1956 | if (signal_pending(current)) { | |
1957 | retval = -ERESTARTSYS; | |
1958 | break; | |
1959 | } | |
1960 | if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { | |
1961 | retval = -EIO; | |
1962 | break; | |
1963 | } | |
1964 | if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) { | |
1965 | while (nr > 0) { | |
a88a69c9 | 1966 | ssize_t num = process_output_block(tty, b, nr); |
1da177e4 LT |
1967 | if (num < 0) { |
1968 | if (num == -EAGAIN) | |
1969 | break; | |
1970 | retval = num; | |
1971 | goto break_out; | |
1972 | } | |
1973 | b += num; | |
1974 | nr -= num; | |
1975 | if (nr == 0) | |
1976 | break; | |
1977 | c = *b; | |
a88a69c9 | 1978 | if (process_output(c, tty) < 0) |
1da177e4 LT |
1979 | break; |
1980 | b++; nr--; | |
1981 | } | |
f34d7a5b AC |
1982 | if (tty->ops->flush_chars) |
1983 | tty->ops->flush_chars(tty); | |
1da177e4 | 1984 | } else { |
d6afe27b | 1985 | while (nr > 0) { |
f34d7a5b | 1986 | c = tty->ops->write(tty, b, nr); |
d6afe27b RZ |
1987 | if (c < 0) { |
1988 | retval = c; | |
1989 | goto break_out; | |
1990 | } | |
1991 | if (!c) | |
1992 | break; | |
1993 | b += c; | |
1994 | nr -= c; | |
1da177e4 | 1995 | } |
1da177e4 LT |
1996 | } |
1997 | if (!nr) | |
1998 | break; | |
1999 | if (file->f_flags & O_NONBLOCK) { | |
2000 | retval = -EAGAIN; | |
2001 | break; | |
2002 | } | |
2003 | schedule(); | |
2004 | } | |
2005 | break_out: | |
2006 | __set_current_state(TASK_RUNNING); | |
2007 | remove_wait_queue(&tty->write_wait, &wait); | |
ff8cb0fd TP |
2008 | if (b - buf != nr && tty->fasync) |
2009 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | |
1da177e4 LT |
2010 | return (b - buf) ? b - buf : retval; |
2011 | } | |
2012 | ||
2013 | /** | |
11a96d18 | 2014 | * n_tty_poll - poll method for N_TTY |
1da177e4 LT |
2015 | * @tty: terminal device |
2016 | * @file: file accessing it | |
2017 | * @wait: poll table | |
2018 | * | |
2019 | * Called when the line discipline is asked to poll() for data or | |
2020 | * for special events. This code is not serialized with respect to | |
2021 | * other events save open/close. | |
2022 | * | |
2023 | * This code must be sure never to sleep through a hangup. | |
2024 | * Called without the kernel lock held - fine | |
1da177e4 | 2025 | */ |
4edf1827 | 2026 | |
11a96d18 | 2027 | static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file, |
4edf1827 | 2028 | poll_table *wait) |
1da177e4 LT |
2029 | { |
2030 | unsigned int mask = 0; | |
2031 | ||
2032 | poll_wait(file, &tty->read_wait, wait); | |
2033 | poll_wait(file, &tty->write_wait, wait); | |
2034 | if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty))) | |
2035 | mask |= POLLIN | POLLRDNORM; | |
2036 | if (tty->packet && tty->link->ctrl_status) | |
2037 | mask |= POLLPRI | POLLIN | POLLRDNORM; | |
2038 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | |
2039 | mask |= POLLHUP; | |
2040 | if (tty_hung_up_p(file)) | |
2041 | mask |= POLLHUP; | |
2042 | if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) { | |
2043 | if (MIN_CHAR(tty) && !TIME_CHAR(tty)) | |
2044 | tty->minimum_to_wake = MIN_CHAR(tty); | |
2045 | else | |
2046 | tty->minimum_to_wake = 1; | |
2047 | } | |
f34d7a5b AC |
2048 | if (tty->ops->write && !tty_is_writelocked(tty) && |
2049 | tty_chars_in_buffer(tty) < WAKEUP_CHARS && | |
2050 | tty_write_room(tty) > 0) | |
1da177e4 LT |
2051 | mask |= POLLOUT | POLLWRNORM; |
2052 | return mask; | |
2053 | } | |
2054 | ||
47afa7a5 AC |
2055 | static unsigned long inq_canon(struct tty_struct *tty) |
2056 | { | |
2057 | int nr, head, tail; | |
2058 | ||
17b82060 | 2059 | if (!tty->canon_data) |
47afa7a5 AC |
2060 | return 0; |
2061 | head = tty->canon_head; | |
2062 | tail = tty->read_tail; | |
2063 | nr = (head - tail) & (N_TTY_BUF_SIZE-1); | |
2064 | /* Skip EOF-chars.. */ | |
2065 | while (head != tail) { | |
2066 | if (test_bit(tail, tty->read_flags) && | |
2067 | tty->read_buf[tail] == __DISABLED_CHAR) | |
2068 | nr--; | |
2069 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); | |
2070 | } | |
2071 | return nr; | |
2072 | } | |
2073 | ||
2074 | static int n_tty_ioctl(struct tty_struct *tty, struct file *file, | |
2075 | unsigned int cmd, unsigned long arg) | |
2076 | { | |
2077 | int retval; | |
2078 | ||
2079 | switch (cmd) { | |
2080 | case TIOCOUTQ: | |
2081 | return put_user(tty_chars_in_buffer(tty), (int __user *) arg); | |
2082 | case TIOCINQ: | |
17b82060 | 2083 | /* FIXME: Locking */ |
47afa7a5 AC |
2084 | retval = tty->read_cnt; |
2085 | if (L_ICANON(tty)) | |
2086 | retval = inq_canon(tty); | |
2087 | return put_user(retval, (unsigned int __user *) arg); | |
2088 | default: | |
2089 | return n_tty_ioctl_helper(tty, file, cmd, arg); | |
2090 | } | |
2091 | } | |
2092 | ||
a352def2 | 2093 | struct tty_ldisc_ops tty_ldisc_N_TTY = { |
e10cc1df PF |
2094 | .magic = TTY_LDISC_MAGIC, |
2095 | .name = "n_tty", | |
2096 | .open = n_tty_open, | |
2097 | .close = n_tty_close, | |
2098 | .flush_buffer = n_tty_flush_buffer, | |
2099 | .chars_in_buffer = n_tty_chars_in_buffer, | |
11a96d18 AC |
2100 | .read = n_tty_read, |
2101 | .write = n_tty_write, | |
e10cc1df PF |
2102 | .ioctl = n_tty_ioctl, |
2103 | .set_termios = n_tty_set_termios, | |
11a96d18 | 2104 | .poll = n_tty_poll, |
e10cc1df PF |
2105 | .receive_buf = n_tty_receive_buf, |
2106 | .write_wakeup = n_tty_write_wakeup | |
1da177e4 | 2107 | }; |