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>. | |
29 | * Also fixed a bug in BLOCKING mode where write_chan returns | |
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> | |
1da177e4 LT |
50 | |
51 | #include <asm/uaccess.h> | |
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 | ||
65 | static inline unsigned char *alloc_buf(void) | |
66 | { | |
b4e3ca1a | 67 | gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL; |
1da177e4 LT |
68 | |
69 | if (PAGE_SIZE != N_TTY_BUF_SIZE) | |
70 | return kmalloc(N_TTY_BUF_SIZE, prio); | |
71 | else | |
72 | return (unsigned char *)__get_free_page(prio); | |
73 | } | |
74 | ||
75 | static inline void free_buf(unsigned char *buf) | |
76 | { | |
77 | if (PAGE_SIZE != N_TTY_BUF_SIZE) | |
78 | kfree(buf); | |
79 | else | |
80 | free_page((unsigned long) buf); | |
81 | } | |
82 | ||
522ed776 MT |
83 | static inline int tty_put_user(struct tty_struct *tty, unsigned char x, |
84 | unsigned char __user *ptr) | |
85 | { | |
86 | tty_audit_add_data(tty, &x, 1); | |
87 | return put_user(x, ptr); | |
88 | } | |
89 | ||
33f0f88f AC |
90 | /** |
91 | * n_tty_set__room - receive space | |
92 | * @tty: terminal | |
93 | * | |
94 | * Called by the driver to find out how much data it is | |
95 | * permitted to feed to the line discipline without any being lost | |
96 | * and thus to manage flow control. Not serialized. Answers for the | |
97 | * "instant". | |
98 | */ | |
99 | ||
100 | static void n_tty_set_room(struct tty_struct *tty) | |
101 | { | |
102 | int left = N_TTY_BUF_SIZE - tty->read_cnt - 1; | |
103 | ||
104 | /* | |
105 | * If we are doing input canonicalization, and there are no | |
106 | * pending newlines, let characters through without limit, so | |
107 | * that erase characters will be handled. Other excess | |
108 | * characters will be beeped. | |
109 | */ | |
110 | if (left <= 0) | |
111 | left = tty->icanon && !tty->canon_data; | |
112 | tty->receive_room = left; | |
113 | } | |
114 | ||
115 | static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty) | |
1da177e4 LT |
116 | { |
117 | if (tty->read_cnt < N_TTY_BUF_SIZE) { | |
118 | tty->read_buf[tty->read_head] = c; | |
119 | tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1); | |
120 | tty->read_cnt++; | |
121 | } | |
122 | } | |
123 | ||
33f0f88f | 124 | static void put_tty_queue(unsigned char c, struct tty_struct *tty) |
1da177e4 LT |
125 | { |
126 | unsigned long flags; | |
127 | /* | |
128 | * The problem of stomping on the buffers ends here. | |
129 | * Why didn't anyone see this one coming? --AJK | |
130 | */ | |
131 | spin_lock_irqsave(&tty->read_lock, flags); | |
132 | put_tty_queue_nolock(c, tty); | |
133 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
134 | } | |
135 | ||
136 | /** | |
137 | * check_unthrottle - allow new receive data | |
138 | * @tty; tty device | |
139 | * | |
140 | * Check whether to call the driver.unthrottle function. | |
141 | * We test the TTY_THROTTLED bit first so that it always | |
142 | * indicates the current state. The decision about whether | |
143 | * it is worth allowing more input has been taken by the caller. | |
70522e12 | 144 | * Can sleep, may be called under the atomic_read_lock mutex but |
1da177e4 LT |
145 | * this is not guaranteed. |
146 | */ | |
4edf1827 AC |
147 | |
148 | static void check_unthrottle(struct tty_struct *tty) | |
1da177e4 LT |
149 | { |
150 | if (tty->count && | |
4edf1827 | 151 | test_and_clear_bit(TTY_THROTTLED, &tty->flags) && |
1da177e4 LT |
152 | tty->driver->unthrottle) |
153 | tty->driver->unthrottle(tty); | |
154 | } | |
155 | ||
156 | /** | |
157 | * reset_buffer_flags - reset buffer state | |
158 | * @tty: terminal to reset | |
159 | * | |
4edf1827 | 160 | * Reset the read buffer counters, clear the flags, |
1da177e4 LT |
161 | * and make sure the driver is unthrottled. Called |
162 | * from n_tty_open() and n_tty_flush_buffer(). | |
163 | */ | |
164 | static void reset_buffer_flags(struct tty_struct *tty) | |
165 | { | |
166 | unsigned long flags; | |
167 | ||
168 | spin_lock_irqsave(&tty->read_lock, flags); | |
169 | tty->read_head = tty->read_tail = tty->read_cnt = 0; | |
170 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
171 | tty->canon_head = tty->canon_data = tty->erasing = 0; | |
172 | memset(&tty->read_flags, 0, sizeof tty->read_flags); | |
33f0f88f | 173 | n_tty_set_room(tty); |
1da177e4 LT |
174 | check_unthrottle(tty); |
175 | } | |
176 | ||
177 | /** | |
178 | * n_tty_flush_buffer - clean input queue | |
179 | * @tty: terminal device | |
180 | * | |
181 | * Flush the input buffer. Called when the line discipline is | |
182 | * being closed, when the tty layer wants the buffer flushed (eg | |
183 | * at hangup) or when the N_TTY line discipline internally has to | |
184 | * clean the pending queue (for example some signals). | |
185 | * | |
04f378b1 | 186 | * Locking: ctrl_lock |
1da177e4 | 187 | */ |
4edf1827 AC |
188 | |
189 | static void n_tty_flush_buffer(struct tty_struct *tty) | |
1da177e4 | 190 | { |
04f378b1 | 191 | unsigned long flags; |
1da177e4 LT |
192 | /* clear everything and unthrottle the driver */ |
193 | reset_buffer_flags(tty); | |
4edf1827 | 194 | |
1da177e4 LT |
195 | if (!tty->link) |
196 | return; | |
197 | ||
04f378b1 | 198 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
1da177e4 LT |
199 | if (tty->link->packet) { |
200 | tty->ctrl_status |= TIOCPKT_FLUSHREAD; | |
201 | wake_up_interruptible(&tty->link->read_wait); | |
202 | } | |
04f378b1 | 203 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
1da177e4 LT |
204 | } |
205 | ||
206 | /** | |
207 | * n_tty_chars_in_buffer - report available bytes | |
208 | * @tty: tty device | |
209 | * | |
210 | * Report the number of characters buffered to be delivered to user | |
4edf1827 | 211 | * at this instant in time. |
1da177e4 | 212 | */ |
4edf1827 | 213 | |
1da177e4 LT |
214 | static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) |
215 | { | |
216 | unsigned long flags; | |
217 | ssize_t n = 0; | |
218 | ||
219 | spin_lock_irqsave(&tty->read_lock, flags); | |
220 | if (!tty->icanon) { | |
221 | n = tty->read_cnt; | |
222 | } else if (tty->canon_data) { | |
223 | n = (tty->canon_head > tty->read_tail) ? | |
224 | tty->canon_head - tty->read_tail : | |
225 | tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail); | |
226 | } | |
227 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
228 | return n; | |
229 | } | |
230 | ||
231 | /** | |
232 | * is_utf8_continuation - utf8 multibyte check | |
233 | * @c: byte to check | |
234 | * | |
235 | * Returns true if the utf8 character 'c' is a multibyte continuation | |
236 | * character. We use this to correctly compute the on screen size | |
237 | * of the character when printing | |
238 | */ | |
4edf1827 | 239 | |
1da177e4 LT |
240 | static inline int is_utf8_continuation(unsigned char c) |
241 | { | |
242 | return (c & 0xc0) == 0x80; | |
243 | } | |
244 | ||
245 | /** | |
246 | * is_continuation - multibyte check | |
247 | * @c: byte to check | |
248 | * | |
249 | * Returns true if the utf8 character 'c' is a multibyte continuation | |
250 | * character and the terminal is in unicode mode. | |
251 | */ | |
4edf1827 | 252 | |
1da177e4 LT |
253 | static inline int is_continuation(unsigned char c, struct tty_struct *tty) |
254 | { | |
255 | return I_IUTF8(tty) && is_utf8_continuation(c); | |
256 | } | |
257 | ||
258 | /** | |
259 | * opost - output post processor | |
260 | * @c: character (or partial unicode symbol) | |
261 | * @tty: terminal device | |
262 | * | |
263 | * Perform OPOST processing. Returns -1 when the output device is | |
264 | * full and the character must be retried. Note that Linux currently | |
265 | * ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't | |
266 | * relevant in the world today. If you ever need them, add them here. | |
267 | * | |
268 | * Called from both the receive and transmit sides and can be called | |
04f378b1 | 269 | * re-entrantly. Relies on lock_kernel() for tty->column state. |
1da177e4 | 270 | */ |
4edf1827 | 271 | |
1da177e4 LT |
272 | static int opost(unsigned char c, struct tty_struct *tty) |
273 | { | |
274 | int space, spaces; | |
275 | ||
276 | space = tty->driver->write_room(tty); | |
277 | if (!space) | |
278 | return -1; | |
279 | ||
04f378b1 | 280 | lock_kernel(); |
1da177e4 LT |
281 | if (O_OPOST(tty)) { |
282 | switch (c) { | |
283 | case '\n': | |
284 | if (O_ONLRET(tty)) | |
285 | tty->column = 0; | |
286 | if (O_ONLCR(tty)) { | |
287 | if (space < 2) | |
288 | return -1; | |
289 | tty->driver->put_char(tty, '\r'); | |
290 | tty->column = 0; | |
291 | } | |
292 | tty->canon_column = tty->column; | |
293 | break; | |
294 | case '\r': | |
295 | if (O_ONOCR(tty) && tty->column == 0) | |
296 | return 0; | |
297 | if (O_OCRNL(tty)) { | |
298 | c = '\n'; | |
299 | if (O_ONLRET(tty)) | |
300 | tty->canon_column = tty->column = 0; | |
301 | break; | |
302 | } | |
303 | tty->canon_column = tty->column = 0; | |
304 | break; | |
305 | case '\t': | |
306 | spaces = 8 - (tty->column & 7); | |
307 | if (O_TABDLY(tty) == XTABS) { | |
308 | if (space < spaces) | |
309 | return -1; | |
310 | tty->column += spaces; | |
311 | tty->driver->write(tty, " ", spaces); | |
312 | return 0; | |
313 | } | |
314 | tty->column += spaces; | |
315 | break; | |
316 | case '\b': | |
317 | if (tty->column > 0) | |
318 | tty->column--; | |
319 | break; | |
320 | default: | |
321 | if (O_OLCUC(tty)) | |
322 | c = toupper(c); | |
323 | if (!iscntrl(c) && !is_continuation(c, tty)) | |
324 | tty->column++; | |
325 | break; | |
326 | } | |
327 | } | |
328 | tty->driver->put_char(tty, c); | |
04f378b1 | 329 | unlock_kernel(); |
1da177e4 LT |
330 | return 0; |
331 | } | |
332 | ||
333 | /** | |
334 | * opost_block - block postprocess | |
335 | * @tty: terminal device | |
336 | * @inbuf: user buffer | |
337 | * @nr: number of bytes | |
338 | * | |
339 | * This path is used to speed up block console writes, among other | |
340 | * things when processing blocks of output data. It handles only | |
341 | * the simple cases normally found and helps to generate blocks of | |
342 | * symbols for the console driver and thus improve performance. | |
343 | * | |
04f378b1 AC |
344 | * Called from write_chan under the tty layer write lock. Relies |
345 | * on lock_kernel for the tty->column state. | |
1da177e4 | 346 | */ |
4edf1827 AC |
347 | |
348 | static ssize_t opost_block(struct tty_struct *tty, | |
349 | const unsigned char *buf, unsigned int nr) | |
1da177e4 LT |
350 | { |
351 | int space; | |
352 | int i; | |
353 | const unsigned char *cp; | |
354 | ||
355 | space = tty->driver->write_room(tty); | |
356 | if (!space) | |
357 | return 0; | |
358 | if (nr > space) | |
359 | nr = space; | |
360 | ||
04f378b1 | 361 | lock_kernel(); |
1da177e4 LT |
362 | for (i = 0, cp = buf; i < nr; i++, cp++) { |
363 | switch (*cp) { | |
364 | case '\n': | |
365 | if (O_ONLRET(tty)) | |
366 | tty->column = 0; | |
367 | if (O_ONLCR(tty)) | |
368 | goto break_out; | |
369 | tty->canon_column = tty->column; | |
370 | break; | |
371 | case '\r': | |
372 | if (O_ONOCR(tty) && tty->column == 0) | |
373 | goto break_out; | |
374 | if (O_OCRNL(tty)) | |
375 | goto break_out; | |
376 | tty->canon_column = tty->column = 0; | |
377 | break; | |
378 | case '\t': | |
379 | goto break_out; | |
380 | case '\b': | |
381 | if (tty->column > 0) | |
382 | tty->column--; | |
383 | break; | |
384 | default: | |
385 | if (O_OLCUC(tty)) | |
386 | goto break_out; | |
387 | if (!iscntrl(*cp)) | |
388 | tty->column++; | |
389 | break; | |
390 | } | |
391 | } | |
392 | break_out: | |
393 | if (tty->driver->flush_chars) | |
394 | tty->driver->flush_chars(tty); | |
4edf1827 | 395 | i = tty->driver->write(tty, buf, i); |
04f378b1 | 396 | unlock_kernel(); |
1da177e4 LT |
397 | return i; |
398 | } | |
399 | ||
400 | ||
401 | /** | |
402 | * put_char - write character to driver | |
403 | * @c: character (or part of unicode symbol) | |
404 | * @tty: terminal device | |
405 | * | |
406 | * Queue a byte to the driver layer for output | |
407 | */ | |
4edf1827 | 408 | |
1da177e4 LT |
409 | static inline void put_char(unsigned char c, struct tty_struct *tty) |
410 | { | |
411 | tty->driver->put_char(tty, c); | |
412 | } | |
413 | ||
414 | /** | |
415 | * echo_char - echo characters | |
416 | * @c: unicode byte to echo | |
417 | * @tty: terminal device | |
418 | * | |
4edf1827 | 419 | * Echo user input back onto the screen. This must be called only when |
1da177e4 LT |
420 | * L_ECHO(tty) is true. Called from the driver receive_buf path. |
421 | */ | |
422 | ||
423 | static void echo_char(unsigned char c, struct tty_struct *tty) | |
424 | { | |
425 | if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') { | |
426 | put_char('^', tty); | |
427 | put_char(c ^ 0100, tty); | |
428 | tty->column += 2; | |
429 | } else | |
430 | opost(c, tty); | |
431 | } | |
432 | ||
433 | static inline void finish_erasing(struct tty_struct *tty) | |
434 | { | |
435 | if (tty->erasing) { | |
436 | put_char('/', tty); | |
437 | tty->column++; | |
438 | tty->erasing = 0; | |
439 | } | |
440 | } | |
441 | ||
442 | /** | |
443 | * eraser - handle erase function | |
444 | * @c: character input | |
445 | * @tty: terminal device | |
446 | * | |
3a4fa0a2 | 447 | * Perform erase and necessary output when an erase character is |
1da177e4 LT |
448 | * present in the stream from the driver layer. Handles the complexities |
449 | * of UTF-8 multibyte symbols. | |
450 | */ | |
4edf1827 | 451 | |
1da177e4 LT |
452 | static void eraser(unsigned char c, struct tty_struct *tty) |
453 | { | |
454 | enum { ERASE, WERASE, KILL } kill_type; | |
455 | int head, seen_alnums, cnt; | |
456 | unsigned long flags; | |
457 | ||
458 | if (tty->read_head == tty->canon_head) { | |
459 | /* opost('\a', tty); */ /* what do you think? */ | |
460 | return; | |
461 | } | |
462 | if (c == ERASE_CHAR(tty)) | |
463 | kill_type = ERASE; | |
464 | else if (c == WERASE_CHAR(tty)) | |
465 | kill_type = WERASE; | |
466 | else { | |
467 | if (!L_ECHO(tty)) { | |
468 | spin_lock_irqsave(&tty->read_lock, flags); | |
469 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & | |
470 | (N_TTY_BUF_SIZE - 1)); | |
471 | tty->read_head = tty->canon_head; | |
472 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
473 | return; | |
474 | } | |
475 | if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { | |
476 | spin_lock_irqsave(&tty->read_lock, flags); | |
477 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & | |
478 | (N_TTY_BUF_SIZE - 1)); | |
479 | tty->read_head = tty->canon_head; | |
480 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
481 | finish_erasing(tty); | |
482 | echo_char(KILL_CHAR(tty), tty); | |
483 | /* Add a newline if ECHOK is on and ECHOKE is off. */ | |
484 | if (L_ECHOK(tty)) | |
485 | opost('\n', tty); | |
486 | return; | |
487 | } | |
488 | kill_type = KILL; | |
489 | } | |
490 | ||
491 | seen_alnums = 0; | |
492 | while (tty->read_head != tty->canon_head) { | |
493 | head = tty->read_head; | |
494 | ||
495 | /* erase a single possibly multibyte character */ | |
496 | do { | |
497 | head = (head - 1) & (N_TTY_BUF_SIZE-1); | |
498 | c = tty->read_buf[head]; | |
499 | } while (is_continuation(c, tty) && head != tty->canon_head); | |
500 | ||
501 | /* do not partially erase */ | |
502 | if (is_continuation(c, tty)) | |
503 | break; | |
504 | ||
505 | if (kill_type == WERASE) { | |
506 | /* Equivalent to BSD's ALTWERASE. */ | |
507 | if (isalnum(c) || c == '_') | |
508 | seen_alnums++; | |
509 | else if (seen_alnums) | |
510 | break; | |
511 | } | |
512 | cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1); | |
513 | spin_lock_irqsave(&tty->read_lock, flags); | |
514 | tty->read_head = head; | |
515 | tty->read_cnt -= cnt; | |
516 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
517 | if (L_ECHO(tty)) { | |
518 | if (L_ECHOPRT(tty)) { | |
519 | if (!tty->erasing) { | |
520 | put_char('\\', tty); | |
521 | tty->column++; | |
522 | tty->erasing = 1; | |
523 | } | |
524 | /* if cnt > 1, output a multi-byte character */ | |
525 | echo_char(c, tty); | |
526 | while (--cnt > 0) { | |
527 | head = (head+1) & (N_TTY_BUF_SIZE-1); | |
528 | put_char(tty->read_buf[head], tty); | |
529 | } | |
530 | } else if (kill_type == ERASE && !L_ECHOE(tty)) { | |
531 | echo_char(ERASE_CHAR(tty), tty); | |
532 | } else if (c == '\t') { | |
533 | unsigned int col = tty->canon_column; | |
534 | unsigned long tail = tty->canon_head; | |
535 | ||
536 | /* Find the column of the last char. */ | |
537 | while (tail != tty->read_head) { | |
538 | c = tty->read_buf[tail]; | |
539 | if (c == '\t') | |
540 | col = (col | 7) + 1; | |
541 | else if (iscntrl(c)) { | |
542 | if (L_ECHOCTL(tty)) | |
543 | col += 2; | |
544 | } else if (!is_continuation(c, tty)) | |
545 | col++; | |
546 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); | |
547 | } | |
548 | ||
549 | /* should never happen */ | |
550 | if (tty->column > 0x80000000) | |
4edf1827 | 551 | tty->column = 0; |
1da177e4 LT |
552 | |
553 | /* Now backup to that column. */ | |
554 | while (tty->column > col) { | |
555 | /* Can't use opost here. */ | |
556 | put_char('\b', tty); | |
557 | if (tty->column > 0) | |
558 | tty->column--; | |
559 | } | |
560 | } else { | |
561 | if (iscntrl(c) && L_ECHOCTL(tty)) { | |
562 | put_char('\b', tty); | |
563 | put_char(' ', tty); | |
564 | put_char('\b', tty); | |
565 | if (tty->column > 0) | |
566 | tty->column--; | |
567 | } | |
568 | if (!iscntrl(c) || L_ECHOCTL(tty)) { | |
569 | put_char('\b', tty); | |
570 | put_char(' ', tty); | |
571 | put_char('\b', tty); | |
572 | if (tty->column > 0) | |
573 | tty->column--; | |
574 | } | |
575 | } | |
576 | } | |
577 | if (kill_type == ERASE) | |
578 | break; | |
579 | } | |
580 | if (tty->read_head == tty->canon_head) | |
581 | finish_erasing(tty); | |
582 | } | |
583 | ||
584 | /** | |
585 | * isig - handle the ISIG optio | |
586 | * @sig: signal | |
587 | * @tty: terminal | |
588 | * @flush: force flush | |
589 | * | |
590 | * Called when a signal is being sent due to terminal input. This | |
591 | * may caus terminal flushing to take place according to the termios | |
592 | * settings and character used. Called from the driver receive_buf | |
593 | * path so serialized. | |
594 | */ | |
4edf1827 | 595 | |
1da177e4 LT |
596 | static inline void isig(int sig, struct tty_struct *tty, int flush) |
597 | { | |
ab521dc0 EB |
598 | if (tty->pgrp) |
599 | kill_pgrp(tty->pgrp, sig, 1); | |
1da177e4 LT |
600 | if (flush || !L_NOFLSH(tty)) { |
601 | n_tty_flush_buffer(tty); | |
602 | if (tty->driver->flush_buffer) | |
603 | tty->driver->flush_buffer(tty); | |
604 | } | |
605 | } | |
606 | ||
607 | /** | |
608 | * n_tty_receive_break - handle break | |
609 | * @tty: terminal | |
610 | * | |
611 | * An RS232 break event has been hit in the incoming bitstream. This | |
612 | * can cause a variety of events depending upon the termios settings. | |
613 | * | |
614 | * Called from the receive_buf path so single threaded. | |
615 | */ | |
4edf1827 | 616 | |
1da177e4 LT |
617 | static inline void n_tty_receive_break(struct tty_struct *tty) |
618 | { | |
619 | if (I_IGNBRK(tty)) | |
620 | return; | |
621 | if (I_BRKINT(tty)) { | |
622 | isig(SIGINT, tty, 1); | |
623 | return; | |
624 | } | |
625 | if (I_PARMRK(tty)) { | |
626 | put_tty_queue('\377', tty); | |
627 | put_tty_queue('\0', tty); | |
628 | } | |
629 | put_tty_queue('\0', tty); | |
630 | wake_up_interruptible(&tty->read_wait); | |
631 | } | |
632 | ||
633 | /** | |
634 | * n_tty_receive_overrun - handle overrun reporting | |
635 | * @tty: terminal | |
636 | * | |
637 | * Data arrived faster than we could process it. While the tty | |
638 | * driver has flagged this the bits that were missed are gone | |
639 | * forever. | |
640 | * | |
641 | * Called from the receive_buf path so single threaded. Does not | |
642 | * need locking as num_overrun and overrun_time are function | |
643 | * private. | |
644 | */ | |
4edf1827 | 645 | |
1da177e4 LT |
646 | static inline void n_tty_receive_overrun(struct tty_struct *tty) |
647 | { | |
648 | char buf[64]; | |
649 | ||
650 | tty->num_overrun++; | |
651 | if (time_before(tty->overrun_time, jiffies - HZ) || | |
652 | time_after(tty->overrun_time, jiffies)) { | |
653 | printk(KERN_WARNING "%s: %d input overrun(s)\n", | |
654 | tty_name(tty, buf), | |
655 | tty->num_overrun); | |
656 | tty->overrun_time = jiffies; | |
657 | tty->num_overrun = 0; | |
658 | } | |
659 | } | |
660 | ||
661 | /** | |
662 | * n_tty_receive_parity_error - error notifier | |
663 | * @tty: terminal device | |
664 | * @c: character | |
665 | * | |
666 | * Process a parity error and queue the right data to indicate | |
3a4fa0a2 | 667 | * the error case if necessary. Locking as per n_tty_receive_buf. |
1da177e4 LT |
668 | */ |
669 | static inline void n_tty_receive_parity_error(struct tty_struct *tty, | |
670 | unsigned char c) | |
671 | { | |
4edf1827 | 672 | if (I_IGNPAR(tty)) |
1da177e4 | 673 | return; |
1da177e4 LT |
674 | if (I_PARMRK(tty)) { |
675 | put_tty_queue('\377', tty); | |
676 | put_tty_queue('\0', tty); | |
677 | put_tty_queue(c, tty); | |
678 | } else if (I_INPCK(tty)) | |
679 | put_tty_queue('\0', tty); | |
680 | else | |
681 | put_tty_queue(c, tty); | |
682 | wake_up_interruptible(&tty->read_wait); | |
683 | } | |
684 | ||
685 | /** | |
686 | * n_tty_receive_char - perform processing | |
687 | * @tty: terminal device | |
688 | * @c: character | |
689 | * | |
690 | * Process an individual character of input received from the driver. | |
4edf1827 | 691 | * This is serialized with respect to itself by the rules for the |
1da177e4 LT |
692 | * driver above. |
693 | */ | |
694 | ||
695 | static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) | |
696 | { | |
697 | unsigned long flags; | |
698 | ||
699 | if (tty->raw) { | |
700 | put_tty_queue(c, tty); | |
701 | return; | |
702 | } | |
4edf1827 | 703 | |
1da177e4 LT |
704 | if (I_ISTRIP(tty)) |
705 | c &= 0x7f; | |
706 | if (I_IUCLC(tty) && L_IEXTEN(tty)) | |
707 | c=tolower(c); | |
708 | ||
54d2a37e JP |
709 | if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && |
710 | ((I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty)) || | |
575537b3 | 711 | c == INTR_CHAR(tty) || c == QUIT_CHAR(tty) || c == SUSP_CHAR(tty))) |
54d2a37e JP |
712 | start_tty(tty); |
713 | ||
1da177e4 LT |
714 | if (tty->closing) { |
715 | if (I_IXON(tty)) { | |
716 | if (c == START_CHAR(tty)) | |
717 | start_tty(tty); | |
718 | else if (c == STOP_CHAR(tty)) | |
719 | stop_tty(tty); | |
720 | } | |
721 | return; | |
722 | } | |
723 | ||
724 | /* | |
725 | * If the previous character was LNEXT, or we know that this | |
726 | * character is not one of the characters that we'll have to | |
727 | * handle specially, do shortcut processing to speed things | |
728 | * up. | |
729 | */ | |
730 | if (!test_bit(c, tty->process_char_map) || tty->lnext) { | |
731 | finish_erasing(tty); | |
732 | tty->lnext = 0; | |
733 | if (L_ECHO(tty)) { | |
734 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) { | |
735 | put_char('\a', tty); /* beep if no space */ | |
736 | return; | |
737 | } | |
738 | /* Record the column of first canon char. */ | |
739 | if (tty->canon_head == tty->read_head) | |
740 | tty->canon_column = tty->column; | |
741 | echo_char(c, tty); | |
742 | } | |
743 | if (I_PARMRK(tty) && c == (unsigned char) '\377') | |
744 | put_tty_queue(c, tty); | |
745 | put_tty_queue(c, tty); | |
746 | return; | |
747 | } | |
4edf1827 | 748 | |
1da177e4 LT |
749 | if (I_IXON(tty)) { |
750 | if (c == START_CHAR(tty)) { | |
751 | start_tty(tty); | |
752 | return; | |
753 | } | |
754 | if (c == STOP_CHAR(tty)) { | |
755 | stop_tty(tty); | |
756 | return; | |
757 | } | |
758 | } | |
575537b3 | 759 | |
1da177e4 LT |
760 | if (L_ISIG(tty)) { |
761 | int signal; | |
762 | signal = SIGINT; | |
763 | if (c == INTR_CHAR(tty)) | |
764 | goto send_signal; | |
765 | signal = SIGQUIT; | |
766 | if (c == QUIT_CHAR(tty)) | |
767 | goto send_signal; | |
768 | signal = SIGTSTP; | |
769 | if (c == SUSP_CHAR(tty)) { | |
770 | send_signal: | |
ec5b1157 JP |
771 | /* |
772 | * Echo character, and then send the signal. | |
773 | * Note that we do not use isig() here because we want | |
774 | * the order to be: | |
775 | * 1) flush, 2) echo, 3) signal | |
776 | */ | |
777 | if (!L_NOFLSH(tty)) { | |
778 | n_tty_flush_buffer(tty); | |
779 | if (tty->driver->flush_buffer) | |
780 | tty->driver->flush_buffer(tty); | |
781 | } | |
782 | if (L_ECHO(tty)) | |
783 | echo_char(c, tty); | |
784 | if (tty->pgrp) | |
785 | kill_pgrp(tty->pgrp, signal, 1); | |
1da177e4 LT |
786 | return; |
787 | } | |
788 | } | |
575537b3 JP |
789 | |
790 | if (c == '\r') { | |
791 | if (I_IGNCR(tty)) | |
792 | return; | |
793 | if (I_ICRNL(tty)) | |
794 | c = '\n'; | |
795 | } else if (c == '\n' && I_INLCR(tty)) | |
796 | c = '\r'; | |
797 | ||
1da177e4 LT |
798 | if (tty->icanon) { |
799 | if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || | |
800 | (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { | |
801 | eraser(c, tty); | |
802 | return; | |
803 | } | |
804 | if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { | |
805 | tty->lnext = 1; | |
806 | if (L_ECHO(tty)) { | |
807 | finish_erasing(tty); | |
808 | if (L_ECHOCTL(tty)) { | |
809 | put_char('^', tty); | |
810 | put_char('\b', tty); | |
811 | } | |
812 | } | |
813 | return; | |
814 | } | |
815 | if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && | |
816 | L_IEXTEN(tty)) { | |
817 | unsigned long tail = tty->canon_head; | |
818 | ||
819 | finish_erasing(tty); | |
820 | echo_char(c, tty); | |
821 | opost('\n', tty); | |
822 | while (tail != tty->read_head) { | |
823 | echo_char(tty->read_buf[tail], tty); | |
824 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); | |
825 | } | |
826 | return; | |
827 | } | |
828 | if (c == '\n') { | |
829 | if (L_ECHO(tty) || L_ECHONL(tty)) { | |
d6afe27b | 830 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) |
1da177e4 | 831 | put_char('\a', tty); |
1da177e4 LT |
832 | opost('\n', tty); |
833 | } | |
834 | goto handle_newline; | |
835 | } | |
836 | if (c == EOF_CHAR(tty)) { | |
4edf1827 AC |
837 | if (tty->canon_head != tty->read_head) |
838 | set_bit(TTY_PUSH, &tty->flags); | |
1da177e4 LT |
839 | c = __DISABLED_CHAR; |
840 | goto handle_newline; | |
841 | } | |
842 | if ((c == EOL_CHAR(tty)) || | |
843 | (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { | |
844 | /* | |
845 | * XXX are EOL_CHAR and EOL2_CHAR echoed?!? | |
846 | */ | |
847 | if (L_ECHO(tty)) { | |
d6afe27b | 848 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) |
1da177e4 | 849 | put_char('\a', tty); |
1da177e4 LT |
850 | /* Record the column of first canon char. */ |
851 | if (tty->canon_head == tty->read_head) | |
852 | tty->canon_column = tty->column; | |
853 | echo_char(c, tty); | |
854 | } | |
855 | /* | |
856 | * XXX does PARMRK doubling happen for | |
857 | * EOL_CHAR and EOL2_CHAR? | |
858 | */ | |
859 | if (I_PARMRK(tty) && c == (unsigned char) '\377') | |
860 | put_tty_queue(c, tty); | |
861 | ||
4edf1827 | 862 | handle_newline: |
1da177e4 LT |
863 | spin_lock_irqsave(&tty->read_lock, flags); |
864 | set_bit(tty->read_head, tty->read_flags); | |
865 | put_tty_queue_nolock(c, tty); | |
866 | tty->canon_head = tty->read_head; | |
867 | tty->canon_data++; | |
868 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
869 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | |
870 | if (waitqueue_active(&tty->read_wait)) | |
871 | wake_up_interruptible(&tty->read_wait); | |
872 | return; | |
873 | } | |
874 | } | |
4edf1827 | 875 | |
1da177e4 LT |
876 | finish_erasing(tty); |
877 | if (L_ECHO(tty)) { | |
878 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) { | |
879 | put_char('\a', tty); /* beep if no space */ | |
880 | return; | |
881 | } | |
882 | if (c == '\n') | |
883 | opost('\n', tty); | |
884 | else { | |
885 | /* Record the column of first canon char. */ | |
886 | if (tty->canon_head == tty->read_head) | |
887 | tty->canon_column = tty->column; | |
888 | echo_char(c, tty); | |
889 | } | |
890 | } | |
891 | ||
892 | if (I_PARMRK(tty) && c == (unsigned char) '\377') | |
893 | put_tty_queue(c, tty); | |
894 | ||
895 | put_tty_queue(c, tty); | |
4edf1827 | 896 | } |
1da177e4 | 897 | |
1da177e4 LT |
898 | |
899 | /** | |
900 | * n_tty_write_wakeup - asynchronous I/O notifier | |
901 | * @tty: tty device | |
902 | * | |
903 | * Required for the ptys, serial driver etc. since processes | |
904 | * that attach themselves to the master and rely on ASYNC | |
905 | * IO must be woken up | |
906 | */ | |
907 | ||
908 | static void n_tty_write_wakeup(struct tty_struct *tty) | |
909 | { | |
4edf1827 AC |
910 | if (tty->fasync) { |
911 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | |
1da177e4 LT |
912 | kill_fasync(&tty->fasync, SIGIO, POLL_OUT); |
913 | } | |
1da177e4 LT |
914 | } |
915 | ||
916 | /** | |
917 | * n_tty_receive_buf - data receive | |
918 | * @tty: terminal device | |
919 | * @cp: buffer | |
920 | * @fp: flag buffer | |
921 | * @count: characters | |
922 | * | |
923 | * Called by the terminal driver when a block of characters has | |
924 | * been received. This function must be called from soft contexts | |
925 | * not from interrupt context. The driver is responsible for making | |
926 | * calls one at a time and in order (or using flush_to_ldisc) | |
927 | */ | |
4edf1827 | 928 | |
1da177e4 LT |
929 | static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, |
930 | char *fp, int count) | |
931 | { | |
932 | const unsigned char *p; | |
933 | char *f, flags = TTY_NORMAL; | |
934 | int i; | |
935 | char buf[64]; | |
936 | unsigned long cpuflags; | |
937 | ||
938 | if (!tty->read_buf) | |
939 | return; | |
940 | ||
941 | if (tty->real_raw) { | |
942 | spin_lock_irqsave(&tty->read_lock, cpuflags); | |
943 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, | |
944 | N_TTY_BUF_SIZE - tty->read_head); | |
945 | i = min(count, i); | |
946 | memcpy(tty->read_buf + tty->read_head, cp, i); | |
947 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); | |
948 | tty->read_cnt += i; | |
949 | cp += i; | |
950 | count -= i; | |
951 | ||
952 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, | |
953 | N_TTY_BUF_SIZE - tty->read_head); | |
954 | i = min(count, i); | |
955 | memcpy(tty->read_buf + tty->read_head, cp, i); | |
956 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); | |
957 | tty->read_cnt += i; | |
958 | spin_unlock_irqrestore(&tty->read_lock, cpuflags); | |
959 | } else { | |
4edf1827 | 960 | for (i = count, p = cp, f = fp; i; i--, p++) { |
1da177e4 LT |
961 | if (f) |
962 | flags = *f++; | |
963 | switch (flags) { | |
964 | case TTY_NORMAL: | |
965 | n_tty_receive_char(tty, *p); | |
966 | break; | |
967 | case TTY_BREAK: | |
968 | n_tty_receive_break(tty); | |
969 | break; | |
970 | case TTY_PARITY: | |
971 | case TTY_FRAME: | |
972 | n_tty_receive_parity_error(tty, *p); | |
973 | break; | |
974 | case TTY_OVERRUN: | |
975 | n_tty_receive_overrun(tty); | |
976 | break; | |
977 | default: | |
4edf1827 | 978 | printk(KERN_ERR "%s: unknown flag %d\n", |
1da177e4 LT |
979 | tty_name(tty, buf), flags); |
980 | break; | |
981 | } | |
982 | } | |
983 | if (tty->driver->flush_chars) | |
984 | tty->driver->flush_chars(tty); | |
985 | } | |
986 | ||
33f0f88f AC |
987 | n_tty_set_room(tty); |
988 | ||
1da177e4 LT |
989 | if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) { |
990 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); | |
991 | if (waitqueue_active(&tty->read_wait)) | |
992 | wake_up_interruptible(&tty->read_wait); | |
993 | } | |
994 | ||
995 | /* | |
996 | * Check the remaining room for the input canonicalization | |
997 | * mode. We don't want to throttle the driver if we're in | |
998 | * canonical mode and don't have a newline yet! | |
999 | */ | |
33f0f88f | 1000 | if (tty->receive_room < TTY_THRESHOLD_THROTTLE) { |
1da177e4 LT |
1001 | /* check TTY_THROTTLED first so it indicates our state */ |
1002 | if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) && | |
1003 | tty->driver->throttle) | |
1004 | tty->driver->throttle(tty); | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | int is_ignored(int sig) | |
1009 | { | |
1010 | return (sigismember(¤t->blocked, sig) || | |
4edf1827 | 1011 | current->sighand->action[sig-1].sa.sa_handler == SIG_IGN); |
1da177e4 LT |
1012 | } |
1013 | ||
1014 | /** | |
1015 | * n_tty_set_termios - termios data changed | |
1016 | * @tty: terminal | |
1017 | * @old: previous data | |
1018 | * | |
1019 | * Called by the tty layer when the user changes termios flags so | |
1020 | * that the line discipline can plan ahead. This function cannot sleep | |
4edf1827 | 1021 | * and is protected from re-entry by the tty layer. The user is |
1da177e4 LT |
1022 | * guaranteed that this function will not be re-entered or in progress |
1023 | * when the ldisc is closed. | |
1024 | */ | |
4edf1827 AC |
1025 | |
1026 | static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) | |
1da177e4 LT |
1027 | { |
1028 | if (!tty) | |
1029 | return; | |
4edf1827 | 1030 | |
1da177e4 LT |
1031 | tty->icanon = (L_ICANON(tty) != 0); |
1032 | if (test_bit(TTY_HW_COOK_IN, &tty->flags)) { | |
1033 | tty->raw = 1; | |
1034 | tty->real_raw = 1; | |
33f0f88f | 1035 | n_tty_set_room(tty); |
1da177e4 LT |
1036 | return; |
1037 | } | |
1038 | if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || | |
1039 | I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || | |
1040 | I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || | |
1041 | I_PARMRK(tty)) { | |
1042 | memset(tty->process_char_map, 0, 256/8); | |
1043 | ||
1044 | if (I_IGNCR(tty) || I_ICRNL(tty)) | |
1045 | set_bit('\r', tty->process_char_map); | |
1046 | if (I_INLCR(tty)) | |
1047 | set_bit('\n', tty->process_char_map); | |
1048 | ||
1049 | if (L_ICANON(tty)) { | |
1050 | set_bit(ERASE_CHAR(tty), tty->process_char_map); | |
1051 | set_bit(KILL_CHAR(tty), tty->process_char_map); | |
1052 | set_bit(EOF_CHAR(tty), tty->process_char_map); | |
1053 | set_bit('\n', tty->process_char_map); | |
1054 | set_bit(EOL_CHAR(tty), tty->process_char_map); | |
1055 | if (L_IEXTEN(tty)) { | |
1056 | set_bit(WERASE_CHAR(tty), | |
1057 | tty->process_char_map); | |
1058 | set_bit(LNEXT_CHAR(tty), | |
1059 | tty->process_char_map); | |
1060 | set_bit(EOL2_CHAR(tty), | |
1061 | tty->process_char_map); | |
1062 | if (L_ECHO(tty)) | |
1063 | set_bit(REPRINT_CHAR(tty), | |
1064 | tty->process_char_map); | |
1065 | } | |
1066 | } | |
1067 | if (I_IXON(tty)) { | |
1068 | set_bit(START_CHAR(tty), tty->process_char_map); | |
1069 | set_bit(STOP_CHAR(tty), tty->process_char_map); | |
1070 | } | |
1071 | if (L_ISIG(tty)) { | |
1072 | set_bit(INTR_CHAR(tty), tty->process_char_map); | |
1073 | set_bit(QUIT_CHAR(tty), tty->process_char_map); | |
1074 | set_bit(SUSP_CHAR(tty), tty->process_char_map); | |
1075 | } | |
1076 | clear_bit(__DISABLED_CHAR, tty->process_char_map); | |
1077 | tty->raw = 0; | |
1078 | tty->real_raw = 0; | |
1079 | } else { | |
1080 | tty->raw = 1; | |
1081 | if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && | |
1082 | (I_IGNPAR(tty) || !I_INPCK(tty)) && | |
1083 | (tty->driver->flags & TTY_DRIVER_REAL_RAW)) | |
1084 | tty->real_raw = 1; | |
1085 | else | |
1086 | tty->real_raw = 0; | |
1087 | } | |
33f0f88f | 1088 | n_tty_set_room(tty); |
1da177e4 LT |
1089 | } |
1090 | ||
1091 | /** | |
1092 | * n_tty_close - close the ldisc for this tty | |
1093 | * @tty: device | |
1094 | * | |
4edf1827 AC |
1095 | * Called from the terminal layer when this line discipline is |
1096 | * being shut down, either because of a close or becsuse of a | |
1da177e4 LT |
1097 | * discipline change. The function will not be called while other |
1098 | * ldisc methods are in progress. | |
1099 | */ | |
4edf1827 | 1100 | |
1da177e4 LT |
1101 | static void n_tty_close(struct tty_struct *tty) |
1102 | { | |
1103 | n_tty_flush_buffer(tty); | |
1104 | if (tty->read_buf) { | |
1105 | free_buf(tty->read_buf); | |
1106 | tty->read_buf = NULL; | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | /** | |
1111 | * n_tty_open - open an ldisc | |
1112 | * @tty: terminal to open | |
1113 | * | |
4edf1827 | 1114 | * Called when this line discipline is being attached to the |
1da177e4 LT |
1115 | * terminal device. Can sleep. Called serialized so that no |
1116 | * other events will occur in parallel. No further open will occur | |
1117 | * until a close. | |
1118 | */ | |
1119 | ||
1120 | static int n_tty_open(struct tty_struct *tty) | |
1121 | { | |
1122 | if (!tty) | |
1123 | return -EINVAL; | |
1124 | ||
1125 | /* This one is ugly. Currently a malloc failure here can panic */ | |
1126 | if (!tty->read_buf) { | |
1127 | tty->read_buf = alloc_buf(); | |
1128 | if (!tty->read_buf) | |
1129 | return -ENOMEM; | |
1130 | } | |
1131 | memset(tty->read_buf, 0, N_TTY_BUF_SIZE); | |
1132 | reset_buffer_flags(tty); | |
1133 | tty->column = 0; | |
1134 | n_tty_set_termios(tty, NULL); | |
1135 | tty->minimum_to_wake = 1; | |
1136 | tty->closing = 0; | |
1137 | return 0; | |
1138 | } | |
1139 | ||
1140 | static inline int input_available_p(struct tty_struct *tty, int amt) | |
1141 | { | |
1142 | if (tty->icanon) { | |
1143 | if (tty->canon_data) | |
1144 | return 1; | |
1145 | } else if (tty->read_cnt >= (amt ? amt : 1)) | |
1146 | return 1; | |
1147 | ||
1148 | return 0; | |
1149 | } | |
1150 | ||
1151 | /** | |
1152 | * copy_from_read_buf - copy read data directly | |
1153 | * @tty: terminal device | |
1154 | * @b: user data | |
1155 | * @nr: size of data | |
1156 | * | |
1157 | * Helper function to speed up read_chan. It is only called when | |
1158 | * ICANON is off; it copies characters straight from the tty queue to | |
1159 | * user space directly. It can be profitably called twice; once to | |
1160 | * drain the space from the tail pointer to the (physical) end of the | |
1161 | * buffer, and once to drain the space from the (physical) beginning of | |
1162 | * the buffer to head pointer. | |
1163 | * | |
817d6d3b | 1164 | * Called under the tty->atomic_read_lock sem |
1da177e4 LT |
1165 | * |
1166 | */ | |
4edf1827 | 1167 | |
33f0f88f | 1168 | static int copy_from_read_buf(struct tty_struct *tty, |
1da177e4 LT |
1169 | unsigned char __user **b, |
1170 | size_t *nr) | |
1171 | ||
1172 | { | |
1173 | int retval; | |
1174 | size_t n; | |
1175 | unsigned long flags; | |
1176 | ||
1177 | retval = 0; | |
1178 | spin_lock_irqsave(&tty->read_lock, flags); | |
1179 | n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail); | |
1180 | n = min(*nr, n); | |
1181 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
1182 | if (n) { | |
1da177e4 LT |
1183 | retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n); |
1184 | n -= retval; | |
522ed776 | 1185 | tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n); |
1da177e4 LT |
1186 | spin_lock_irqsave(&tty->read_lock, flags); |
1187 | tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1); | |
1188 | tty->read_cnt -= n; | |
1189 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
1190 | *b += n; | |
1191 | *nr -= n; | |
1192 | } | |
1193 | return retval; | |
1194 | } | |
1195 | ||
cc4191dc | 1196 | extern ssize_t redirected_tty_write(struct file *, const char __user *, |
4edf1827 | 1197 | size_t, loff_t *); |
1da177e4 LT |
1198 | |
1199 | /** | |
1200 | * job_control - check job control | |
1201 | * @tty: tty | |
1202 | * @file: file handle | |
1203 | * | |
1204 | * Perform job control management checks on this file/tty descriptor | |
4edf1827 | 1205 | * and if appropriate send any needed signals and return a negative |
1da177e4 | 1206 | * error code if action should be taken. |
04f378b1 AC |
1207 | * |
1208 | * FIXME: | |
1209 | * Locking: None - redirected write test is safe, testing | |
1210 | * current->signal should possibly lock current->sighand | |
1211 | * pgrp locking ? | |
1da177e4 | 1212 | */ |
4edf1827 | 1213 | |
1da177e4 LT |
1214 | static int job_control(struct tty_struct *tty, struct file *file) |
1215 | { | |
1216 | /* Job control check -- must be done at start and after | |
1217 | every sleep (POSIX.1 7.1.1.4). */ | |
1218 | /* NOTE: not yet done after every sleep pending a thorough | |
1219 | check of the logic of this change. -- jlc */ | |
1220 | /* don't stop on /dev/console */ | |
1221 | if (file->f_op->write != redirected_tty_write && | |
1222 | current->signal->tty == tty) { | |
ab521dc0 | 1223 | if (!tty->pgrp) |
4edf1827 | 1224 | printk(KERN_ERR "read_chan: no tty->pgrp!\n"); |
ab521dc0 | 1225 | else if (task_pgrp(current) != tty->pgrp) { |
1da177e4 | 1226 | if (is_ignored(SIGTTIN) || |
3e7cd6c4 | 1227 | is_current_pgrp_orphaned()) |
1da177e4 | 1228 | return -EIO; |
ab521dc0 | 1229 | kill_pgrp(task_pgrp(current), SIGTTIN, 1); |
040b6362 | 1230 | set_thread_flag(TIF_SIGPENDING); |
1da177e4 LT |
1231 | return -ERESTARTSYS; |
1232 | } | |
1233 | } | |
1234 | return 0; | |
1235 | } | |
4edf1827 | 1236 | |
1da177e4 LT |
1237 | |
1238 | /** | |
1239 | * read_chan - read function for tty | |
1240 | * @tty: tty device | |
1241 | * @file: file object | |
1242 | * @buf: userspace buffer pointer | |
1243 | * @nr: size of I/O | |
1244 | * | |
1245 | * Perform reads for the line discipline. We are guaranteed that the | |
1246 | * line discipline will not be closed under us but we may get multiple | |
1247 | * parallel readers and must handle this ourselves. We may also get | |
1248 | * a hangup. Always called in user context, may sleep. | |
1249 | * | |
1250 | * This code must be sure never to sleep through a hangup. | |
1251 | */ | |
4edf1827 | 1252 | |
1da177e4 LT |
1253 | static ssize_t read_chan(struct tty_struct *tty, struct file *file, |
1254 | unsigned char __user *buf, size_t nr) | |
1255 | { | |
1256 | unsigned char __user *b = buf; | |
1257 | DECLARE_WAITQUEUE(wait, current); | |
1258 | int c; | |
1259 | int minimum, time; | |
1260 | ssize_t retval = 0; | |
1261 | ssize_t size; | |
1262 | long timeout; | |
1263 | unsigned long flags; | |
04f378b1 | 1264 | int packet; |
1da177e4 LT |
1265 | |
1266 | do_it_again: | |
1267 | ||
1268 | if (!tty->read_buf) { | |
4edf1827 | 1269 | printk(KERN_ERR "n_tty_read_chan: read_buf == NULL?!?\n"); |
1da177e4 LT |
1270 | return -EIO; |
1271 | } | |
1272 | ||
1273 | c = job_control(tty, file); | |
4edf1827 | 1274 | if (c < 0) |
1da177e4 | 1275 | return c; |
4edf1827 | 1276 | |
1da177e4 LT |
1277 | minimum = time = 0; |
1278 | timeout = MAX_SCHEDULE_TIMEOUT; | |
1279 | if (!tty->icanon) { | |
1280 | time = (HZ / 10) * TIME_CHAR(tty); | |
1281 | minimum = MIN_CHAR(tty); | |
1282 | if (minimum) { | |
1283 | if (time) | |
1284 | tty->minimum_to_wake = 1; | |
1285 | else if (!waitqueue_active(&tty->read_wait) || | |
1286 | (tty->minimum_to_wake > minimum)) | |
1287 | tty->minimum_to_wake = minimum; | |
1288 | } else { | |
1289 | timeout = 0; | |
1290 | if (time) { | |
1291 | timeout = time; | |
1292 | time = 0; | |
1293 | } | |
1294 | tty->minimum_to_wake = minimum = 1; | |
1295 | } | |
1296 | } | |
1297 | ||
1298 | /* | |
1299 | * Internal serialization of reads. | |
1300 | */ | |
1301 | if (file->f_flags & O_NONBLOCK) { | |
70522e12 | 1302 | if (!mutex_trylock(&tty->atomic_read_lock)) |
1da177e4 | 1303 | return -EAGAIN; |
4edf1827 | 1304 | } else { |
70522e12 | 1305 | if (mutex_lock_interruptible(&tty->atomic_read_lock)) |
1da177e4 LT |
1306 | return -ERESTARTSYS; |
1307 | } | |
04f378b1 | 1308 | packet = tty->packet; |
1da177e4 LT |
1309 | |
1310 | add_wait_queue(&tty->read_wait, &wait); | |
1da177e4 LT |
1311 | while (nr) { |
1312 | /* First test for status change. */ | |
04f378b1 | 1313 | if (packet && tty->link->ctrl_status) { |
1da177e4 LT |
1314 | unsigned char cs; |
1315 | if (b != buf) | |
1316 | break; | |
04f378b1 | 1317 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); |
1da177e4 LT |
1318 | cs = tty->link->ctrl_status; |
1319 | tty->link->ctrl_status = 0; | |
04f378b1 | 1320 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); |
522ed776 | 1321 | if (tty_put_user(tty, cs, b++)) { |
1da177e4 LT |
1322 | retval = -EFAULT; |
1323 | b--; | |
1324 | break; | |
1325 | } | |
1326 | nr--; | |
1327 | break; | |
1328 | } | |
1329 | /* This statement must be first before checking for input | |
1330 | so that any interrupt will set the state back to | |
1331 | TASK_RUNNING. */ | |
1332 | set_current_state(TASK_INTERRUPTIBLE); | |
4edf1827 | 1333 | |
1da177e4 LT |
1334 | if (((minimum - (b - buf)) < tty->minimum_to_wake) && |
1335 | ((minimum - (b - buf)) >= 1)) | |
1336 | tty->minimum_to_wake = (minimum - (b - buf)); | |
4edf1827 | 1337 | |
1da177e4 LT |
1338 | if (!input_available_p(tty, 0)) { |
1339 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { | |
1340 | retval = -EIO; | |
1341 | break; | |
1342 | } | |
1343 | if (tty_hung_up_p(file)) | |
1344 | break; | |
1345 | if (!timeout) | |
1346 | break; | |
1347 | if (file->f_flags & O_NONBLOCK) { | |
1348 | retval = -EAGAIN; | |
1349 | break; | |
1350 | } | |
1351 | if (signal_pending(current)) { | |
1352 | retval = -ERESTARTSYS; | |
1353 | break; | |
1354 | } | |
04f378b1 | 1355 | /* FIXME: does n_tty_set_room need locking ? */ |
33f0f88f | 1356 | n_tty_set_room(tty); |
1da177e4 | 1357 | timeout = schedule_timeout(timeout); |
1da177e4 LT |
1358 | continue; |
1359 | } | |
1360 | __set_current_state(TASK_RUNNING); | |
1361 | ||
1362 | /* Deal with packet mode. */ | |
04f378b1 | 1363 | if (packet && b == buf) { |
522ed776 | 1364 | if (tty_put_user(tty, TIOCPKT_DATA, b++)) { |
1da177e4 LT |
1365 | retval = -EFAULT; |
1366 | b--; | |
1367 | break; | |
1368 | } | |
1369 | nr--; | |
1370 | } | |
1371 | ||
1372 | if (tty->icanon) { | |
1373 | /* N.B. avoid overrun if nr == 0 */ | |
1374 | while (nr && tty->read_cnt) { | |
4edf1827 | 1375 | int eol; |
1da177e4 LT |
1376 | |
1377 | eol = test_and_clear_bit(tty->read_tail, | |
1378 | tty->read_flags); | |
1379 | c = tty->read_buf[tty->read_tail]; | |
1380 | spin_lock_irqsave(&tty->read_lock, flags); | |
1381 | tty->read_tail = ((tty->read_tail+1) & | |
1382 | (N_TTY_BUF_SIZE-1)); | |
1383 | tty->read_cnt--; | |
1384 | if (eol) { | |
1385 | /* this test should be redundant: | |
1386 | * we shouldn't be reading data if | |
1387 | * canon_data is 0 | |
1388 | */ | |
1389 | if (--tty->canon_data < 0) | |
1390 | tty->canon_data = 0; | |
1391 | } | |
1392 | spin_unlock_irqrestore(&tty->read_lock, flags); | |
1393 | ||
1394 | if (!eol || (c != __DISABLED_CHAR)) { | |
522ed776 | 1395 | if (tty_put_user(tty, c, b++)) { |
1da177e4 LT |
1396 | retval = -EFAULT; |
1397 | b--; | |
1398 | break; | |
1399 | } | |
1400 | nr--; | |
1401 | } | |
522ed776 MT |
1402 | if (eol) { |
1403 | tty_audit_push(tty); | |
1da177e4 | 1404 | break; |
522ed776 | 1405 | } |
1da177e4 LT |
1406 | } |
1407 | if (retval) | |
1408 | break; | |
1409 | } else { | |
1410 | int uncopied; | |
04f378b1 AC |
1411 | /* The copy function takes the read lock and handles |
1412 | locking internally for this case */ | |
1da177e4 LT |
1413 | uncopied = copy_from_read_buf(tty, &b, &nr); |
1414 | uncopied += copy_from_read_buf(tty, &b, &nr); | |
1415 | if (uncopied) { | |
1416 | retval = -EFAULT; | |
1417 | break; | |
1418 | } | |
1419 | } | |
1420 | ||
1421 | /* If there is enough space in the read buffer now, let the | |
1422 | * low-level driver know. We use n_tty_chars_in_buffer() to | |
1423 | * check the buffer, as it now knows about canonical mode. | |
1424 | * Otherwise, if the driver is throttled and the line is | |
1425 | * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, | |
1426 | * we won't get any more characters. | |
1427 | */ | |
289a1e99 PM |
1428 | if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) { |
1429 | n_tty_set_room(tty); | |
1da177e4 | 1430 | check_unthrottle(tty); |
289a1e99 | 1431 | } |
1da177e4 LT |
1432 | |
1433 | if (b - buf >= minimum) | |
1434 | break; | |
1435 | if (time) | |
1436 | timeout = time; | |
1437 | } | |
70522e12 | 1438 | mutex_unlock(&tty->atomic_read_lock); |
1da177e4 LT |
1439 | remove_wait_queue(&tty->read_wait, &wait); |
1440 | ||
1441 | if (!waitqueue_active(&tty->read_wait)) | |
1442 | tty->minimum_to_wake = minimum; | |
1443 | ||
1444 | __set_current_state(TASK_RUNNING); | |
1445 | size = b - buf; | |
1446 | if (size) { | |
1447 | retval = size; | |
1448 | if (nr) | |
4edf1827 | 1449 | clear_bit(TTY_PUSH, &tty->flags); |
1da177e4 LT |
1450 | } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) |
1451 | goto do_it_again; | |
1452 | ||
33f0f88f | 1453 | n_tty_set_room(tty); |
1da177e4 LT |
1454 | return retval; |
1455 | } | |
1456 | ||
1457 | /** | |
1458 | * write_chan - write function for tty | |
1459 | * @tty: tty device | |
1460 | * @file: file object | |
1461 | * @buf: userspace buffer pointer | |
1462 | * @nr: size of I/O | |
1463 | * | |
1464 | * Write function of the terminal device. This is serialized with | |
1465 | * respect to other write callers but not to termios changes, reads | |
1466 | * and other such events. We must be careful with N_TTY as the receive | |
1467 | * code will echo characters, thus calling driver write methods. | |
1468 | * | |
1469 | * This code must be sure never to sleep through a hangup. | |
1470 | */ | |
4edf1827 AC |
1471 | |
1472 | static ssize_t write_chan(struct tty_struct *tty, struct file *file, | |
1473 | const unsigned char *buf, size_t nr) | |
1da177e4 LT |
1474 | { |
1475 | const unsigned char *b = buf; | |
1476 | DECLARE_WAITQUEUE(wait, current); | |
1477 | int c; | |
1478 | ssize_t retval = 0; | |
1479 | ||
1480 | /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ | |
1481 | if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { | |
1482 | retval = tty_check_change(tty); | |
1483 | if (retval) | |
1484 | return retval; | |
1485 | } | |
1486 | ||
1487 | add_wait_queue(&tty->write_wait, &wait); | |
1488 | while (1) { | |
1489 | set_current_state(TASK_INTERRUPTIBLE); | |
1490 | if (signal_pending(current)) { | |
1491 | retval = -ERESTARTSYS; | |
1492 | break; | |
1493 | } | |
1494 | if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { | |
1495 | retval = -EIO; | |
1496 | break; | |
1497 | } | |
1498 | if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) { | |
1499 | while (nr > 0) { | |
1500 | ssize_t num = opost_block(tty, b, nr); | |
1501 | if (num < 0) { | |
1502 | if (num == -EAGAIN) | |
1503 | break; | |
1504 | retval = num; | |
1505 | goto break_out; | |
1506 | } | |
1507 | b += num; | |
1508 | nr -= num; | |
1509 | if (nr == 0) | |
1510 | break; | |
1511 | c = *b; | |
1512 | if (opost(c, tty) < 0) | |
1513 | break; | |
1514 | b++; nr--; | |
1515 | } | |
1516 | if (tty->driver->flush_chars) | |
1517 | tty->driver->flush_chars(tty); | |
1518 | } else { | |
d6afe27b RZ |
1519 | while (nr > 0) { |
1520 | c = tty->driver->write(tty, b, nr); | |
1521 | if (c < 0) { | |
1522 | retval = c; | |
1523 | goto break_out; | |
1524 | } | |
1525 | if (!c) | |
1526 | break; | |
1527 | b += c; | |
1528 | nr -= c; | |
1da177e4 | 1529 | } |
1da177e4 LT |
1530 | } |
1531 | if (!nr) | |
1532 | break; | |
1533 | if (file->f_flags & O_NONBLOCK) { | |
1534 | retval = -EAGAIN; | |
1535 | break; | |
1536 | } | |
1537 | schedule(); | |
1538 | } | |
1539 | break_out: | |
1540 | __set_current_state(TASK_RUNNING); | |
1541 | remove_wait_queue(&tty->write_wait, &wait); | |
1542 | return (b - buf) ? b - buf : retval; | |
1543 | } | |
1544 | ||
1545 | /** | |
1546 | * normal_poll - poll method for N_TTY | |
1547 | * @tty: terminal device | |
1548 | * @file: file accessing it | |
1549 | * @wait: poll table | |
1550 | * | |
1551 | * Called when the line discipline is asked to poll() for data or | |
1552 | * for special events. This code is not serialized with respect to | |
1553 | * other events save open/close. | |
1554 | * | |
1555 | * This code must be sure never to sleep through a hangup. | |
1556 | * Called without the kernel lock held - fine | |
1557 | * | |
1558 | * FIXME: if someone changes the VMIN or discipline settings for the | |
1559 | * terminal while another process is in poll() the poll does not | |
1560 | * recompute the new limits. Possibly set_termios should issue | |
1561 | * a read wakeup to fix this bug. | |
1562 | */ | |
4edf1827 AC |
1563 | |
1564 | static unsigned int normal_poll(struct tty_struct *tty, struct file *file, | |
1565 | poll_table *wait) | |
1da177e4 LT |
1566 | { |
1567 | unsigned int mask = 0; | |
1568 | ||
1569 | poll_wait(file, &tty->read_wait, wait); | |
1570 | poll_wait(file, &tty->write_wait, wait); | |
1571 | if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty))) | |
1572 | mask |= POLLIN | POLLRDNORM; | |
1573 | if (tty->packet && tty->link->ctrl_status) | |
1574 | mask |= POLLPRI | POLLIN | POLLRDNORM; | |
1575 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | |
1576 | mask |= POLLHUP; | |
1577 | if (tty_hung_up_p(file)) | |
1578 | mask |= POLLHUP; | |
1579 | if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) { | |
1580 | if (MIN_CHAR(tty) && !TIME_CHAR(tty)) | |
1581 | tty->minimum_to_wake = MIN_CHAR(tty); | |
1582 | else | |
1583 | tty->minimum_to_wake = 1; | |
1584 | } | |
9c1729db AC |
1585 | if (!tty_is_writelocked(tty) && |
1586 | tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS && | |
1da177e4 LT |
1587 | tty->driver->write_room(tty) > 0) |
1588 | mask |= POLLOUT | POLLWRNORM; | |
1589 | return mask; | |
1590 | } | |
1591 | ||
1592 | struct tty_ldisc tty_ldisc_N_TTY = { | |
e10cc1df PF |
1593 | .magic = TTY_LDISC_MAGIC, |
1594 | .name = "n_tty", | |
1595 | .open = n_tty_open, | |
1596 | .close = n_tty_close, | |
1597 | .flush_buffer = n_tty_flush_buffer, | |
1598 | .chars_in_buffer = n_tty_chars_in_buffer, | |
1599 | .read = read_chan, | |
1600 | .write = write_chan, | |
1601 | .ioctl = n_tty_ioctl, | |
1602 | .set_termios = n_tty_set_termios, | |
1603 | .poll = normal_poll, | |
1604 | .receive_buf = n_tty_receive_buf, | |
1605 | .write_wakeup = n_tty_write_wakeup | |
1da177e4 LT |
1606 | }; |
1607 |