2 * n_tty.c --- implements the N_TTY line discipline.
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,
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
11 * to N_TTY if it can not switch to any other line discipline.
13 * Written by Theodore Ts'o, Copyright 1994.
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
18 * This file may be redistributed under the terms of the GNU General Public
21 * Reduced memory usage for older ARM systems - Russell King.
23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
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
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>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/poll.h>
47 #include <linux/bitops.h>
48 #include <linux/audit.h>
49 #include <linux/file.h>
51 #include <asm/uaccess.h>
52 #include <asm/system.h>
54 /* number of characters left in xmit buffer before select has we have room */
55 #define WAKEUP_CHARS 256
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.
62 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
63 #define TTY_THRESHOLD_UNTHROTTLE 128
65 static inline unsigned char *alloc_buf(void)
67 gfp_t prio
= in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
;
69 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
70 return kmalloc(N_TTY_BUF_SIZE
, prio
);
72 return (unsigned char *)__get_free_page(prio
);
75 static inline void free_buf(unsigned char *buf
)
77 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
80 free_page((unsigned long) buf
);
83 static inline int tty_put_user(struct tty_struct
*tty
, unsigned char x
,
84 unsigned char __user
*ptr
)
86 tty_audit_add_data(tty
, &x
, 1);
87 return put_user(x
, ptr
);
91 * n_tty_set__room - receive space
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
100 static void n_tty_set_room(struct tty_struct
*tty
)
102 int left
= N_TTY_BUF_SIZE
- tty
->read_cnt
- 1;
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.
111 left
= tty
->icanon
&& !tty
->canon_data
;
112 tty
->receive_room
= left
;
115 static void put_tty_queue_nolock(unsigned char c
, struct tty_struct
*tty
)
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);
124 static void put_tty_queue(unsigned char c
, struct tty_struct
*tty
)
128 * The problem of stomping on the buffers ends here.
129 * Why didn't anyone see this one coming? --AJK
131 spin_lock_irqsave(&tty
->read_lock
, flags
);
132 put_tty_queue_nolock(c
, tty
);
133 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
137 * check_unthrottle - allow new receive data
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.
144 * Can sleep, may be called under the atomic_read_lock mutex but
145 * this is not guaranteed.
148 static void check_unthrottle(struct tty_struct
*tty
)
155 * reset_buffer_flags - reset buffer state
156 * @tty: terminal to reset
158 * Reset the read buffer counters, clear the flags,
159 * and make sure the driver is unthrottled. Called
160 * from n_tty_open() and n_tty_flush_buffer().
162 static void reset_buffer_flags(struct tty_struct
*tty
)
166 spin_lock_irqsave(&tty
->read_lock
, flags
);
167 tty
->read_head
= tty
->read_tail
= tty
->read_cnt
= 0;
168 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
169 tty
->canon_head
= tty
->canon_data
= tty
->erasing
= 0;
170 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
172 check_unthrottle(tty
);
176 * n_tty_flush_buffer - clean input queue
177 * @tty: terminal device
179 * Flush the input buffer. Called when the line discipline is
180 * being closed, when the tty layer wants the buffer flushed (eg
181 * at hangup) or when the N_TTY line discipline internally has to
182 * clean the pending queue (for example some signals).
187 static void n_tty_flush_buffer(struct tty_struct
*tty
)
190 /* clear everything and unthrottle the driver */
191 reset_buffer_flags(tty
);
196 spin_lock_irqsave(&tty
->ctrl_lock
, flags
);
197 if (tty
->link
->packet
) {
198 tty
->ctrl_status
|= TIOCPKT_FLUSHREAD
;
199 wake_up_interruptible(&tty
->link
->read_wait
);
201 spin_unlock_irqrestore(&tty
->ctrl_lock
, flags
);
205 * n_tty_chars_in_buffer - report available bytes
208 * Report the number of characters buffered to be delivered to user
209 * at this instant in time.
212 static ssize_t
n_tty_chars_in_buffer(struct tty_struct
*tty
)
217 spin_lock_irqsave(&tty
->read_lock
, flags
);
220 } else if (tty
->canon_data
) {
221 n
= (tty
->canon_head
> tty
->read_tail
) ?
222 tty
->canon_head
- tty
->read_tail
:
223 tty
->canon_head
+ (N_TTY_BUF_SIZE
- tty
->read_tail
);
225 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
230 * is_utf8_continuation - utf8 multibyte check
233 * Returns true if the utf8 character 'c' is a multibyte continuation
234 * character. We use this to correctly compute the on screen size
235 * of the character when printing
238 static inline int is_utf8_continuation(unsigned char c
)
240 return (c
& 0xc0) == 0x80;
244 * is_continuation - multibyte check
247 * Returns true if the utf8 character 'c' is a multibyte continuation
248 * character and the terminal is in unicode mode.
251 static inline int is_continuation(unsigned char c
, struct tty_struct
*tty
)
253 return I_IUTF8(tty
) && is_utf8_continuation(c
);
257 * opost - output post processor
258 * @c: character (or partial unicode symbol)
259 * @tty: terminal device
261 * Perform OPOST processing. Returns -1 when the output device is
262 * full and the character must be retried. Note that Linux currently
263 * ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't
264 * relevant in the world today. If you ever need them, add them here.
266 * Called from both the receive and transmit sides and can be called
267 * re-entrantly. Relies on lock_kernel() for tty->column state.
270 static int opost(unsigned char c
, struct tty_struct
*tty
)
274 space
= tty_write_room(tty
);
289 tty_put_char(tty
, '\r');
292 tty
->canon_column
= tty
->column
;
295 if (O_ONOCR(tty
) && tty
->column
== 0) {
302 tty
->canon_column
= tty
->column
= 0;
305 tty
->canon_column
= tty
->column
= 0;
308 spaces
= 8 - (tty
->column
& 7);
309 if (O_TABDLY(tty
) == XTABS
) {
310 if (space
< spaces
) {
314 tty
->column
+= spaces
;
315 tty
->ops
->write(tty
, " ", spaces
);
319 tty
->column
+= spaces
;
328 if (!iscntrl(c
) && !is_continuation(c
, tty
))
333 tty_put_char(tty
, c
);
339 * opost_block - block postprocess
340 * @tty: terminal device
341 * @inbuf: user buffer
342 * @nr: number of bytes
344 * This path is used to speed up block console writes, among other
345 * things when processing blocks of output data. It handles only
346 * the simple cases normally found and helps to generate blocks of
347 * symbols for the console driver and thus improve performance.
349 * Called from write_chan under the tty layer write lock. Relies
350 * on lock_kernel for the tty->column state.
353 static ssize_t
opost_block(struct tty_struct
*tty
,
354 const unsigned char *buf
, unsigned int nr
)
358 const unsigned char *cp
;
360 space
= tty_write_room(tty
);
367 for (i
= 0, cp
= buf
; i
< nr
; i
++, cp
++) {
374 tty
->canon_column
= tty
->column
;
377 if (O_ONOCR(tty
) && tty
->column
== 0)
381 tty
->canon_column
= tty
->column
= 0;
398 if (tty
->ops
->flush_chars
)
399 tty
->ops
->flush_chars(tty
);
400 i
= tty
->ops
->write(tty
, buf
, i
);
407 * echo_char - echo characters
408 * @c: unicode byte to echo
409 * @tty: terminal device
411 * Echo user input back onto the screen. This must be called only when
412 * L_ECHO(tty) is true. Called from the driver receive_buf path.
415 static void echo_char(unsigned char c
, struct tty_struct
*tty
)
417 if (L_ECHOCTL(tty
) && iscntrl(c
) && c
!= '\t') {
418 tty_put_char(tty
, '^');
419 tty_put_char(tty
, c
^ 0100);
425 static inline void finish_erasing(struct tty_struct
*tty
)
428 tty_put_char(tty
, '/');
435 * eraser - handle erase function
436 * @c: character input
437 * @tty: terminal device
439 * Perform erase and necessary output when an erase character is
440 * present in the stream from the driver layer. Handles the complexities
441 * of UTF-8 multibyte symbols.
444 static void eraser(unsigned char c
, struct tty_struct
*tty
)
446 enum { ERASE
, WERASE
, KILL
} kill_type
;
447 int head
, seen_alnums
, cnt
;
450 if (tty
->read_head
== tty
->canon_head
) {
451 /* opost('\a', tty); */ /* what do you think? */
454 if (c
== ERASE_CHAR(tty
))
456 else if (c
== WERASE_CHAR(tty
))
460 spin_lock_irqsave(&tty
->read_lock
, flags
);
461 tty
->read_cnt
-= ((tty
->read_head
- tty
->canon_head
) &
462 (N_TTY_BUF_SIZE
- 1));
463 tty
->read_head
= tty
->canon_head
;
464 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
467 if (!L_ECHOK(tty
) || !L_ECHOKE(tty
) || !L_ECHOE(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
);
474 echo_char(KILL_CHAR(tty
), tty
);
475 /* Add a newline if ECHOK is on and ECHOKE is off. */
484 while (tty
->read_head
!= tty
->canon_head
) {
485 head
= tty
->read_head
;
487 /* erase a single possibly multibyte character */
489 head
= (head
- 1) & (N_TTY_BUF_SIZE
-1);
490 c
= tty
->read_buf
[head
];
491 } while (is_continuation(c
, tty
) && head
!= tty
->canon_head
);
493 /* do not partially erase */
494 if (is_continuation(c
, tty
))
497 if (kill_type
== WERASE
) {
498 /* Equivalent to BSD's ALTWERASE. */
499 if (isalnum(c
) || c
== '_')
501 else if (seen_alnums
)
504 cnt
= (tty
->read_head
- head
) & (N_TTY_BUF_SIZE
-1);
505 spin_lock_irqsave(&tty
->read_lock
, flags
);
506 tty
->read_head
= head
;
507 tty
->read_cnt
-= cnt
;
508 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
510 if (L_ECHOPRT(tty
)) {
512 tty_put_char(tty
, '\\');
516 /* if cnt > 1, output a multi-byte character */
519 head
= (head
+1) & (N_TTY_BUF_SIZE
-1);
520 tty_put_char(tty
, tty
->read_buf
[head
]);
522 } else if (kill_type
== ERASE
&& !L_ECHOE(tty
)) {
523 echo_char(ERASE_CHAR(tty
), tty
);
524 } else if (c
== '\t') {
525 unsigned int col
= tty
->canon_column
;
526 unsigned long tail
= tty
->canon_head
;
528 /* Find the column of the last char. */
529 while (tail
!= tty
->read_head
) {
530 c
= tty
->read_buf
[tail
];
533 else if (iscntrl(c
)) {
536 } else if (!is_continuation(c
, tty
))
538 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
541 /* should never happen */
542 if (tty
->column
> 0x80000000)
545 /* Now backup to that column. */
546 while (tty
->column
> col
) {
547 /* Can't use opost here. */
548 tty_put_char(tty
, '\b');
553 if (iscntrl(c
) && L_ECHOCTL(tty
)) {
554 tty_put_char(tty
, '\b');
555 tty_put_char(tty
, ' ');
556 tty_put_char(tty
, '\b');
560 if (!iscntrl(c
) || L_ECHOCTL(tty
)) {
561 tty_put_char(tty
, '\b');
562 tty_put_char(tty
, ' ');
563 tty_put_char(tty
, '\b');
569 if (kill_type
== ERASE
)
572 if (tty
->read_head
== tty
->canon_head
)
577 * isig - handle the ISIG optio
580 * @flush: force flush
582 * Called when a signal is being sent due to terminal input. This
583 * may caus terminal flushing to take place according to the termios
584 * settings and character used. Called from the driver receive_buf
585 * path so serialized.
588 static inline void isig(int sig
, struct tty_struct
*tty
, int flush
)
591 kill_pgrp(tty
->pgrp
, sig
, 1);
592 if (flush
|| !L_NOFLSH(tty
)) {
593 n_tty_flush_buffer(tty
);
594 tty_driver_flush_buffer(tty
);
599 * n_tty_receive_break - handle break
602 * An RS232 break event has been hit in the incoming bitstream. This
603 * can cause a variety of events depending upon the termios settings.
605 * Called from the receive_buf path so single threaded.
608 static inline void n_tty_receive_break(struct tty_struct
*tty
)
613 isig(SIGINT
, tty
, 1);
617 put_tty_queue('\377', tty
);
618 put_tty_queue('\0', tty
);
620 put_tty_queue('\0', tty
);
621 wake_up_interruptible(&tty
->read_wait
);
625 * n_tty_receive_overrun - handle overrun reporting
628 * Data arrived faster than we could process it. While the tty
629 * driver has flagged this the bits that were missed are gone
632 * Called from the receive_buf path so single threaded. Does not
633 * need locking as num_overrun and overrun_time are function
637 static inline void n_tty_receive_overrun(struct tty_struct
*tty
)
642 if (time_before(tty
->overrun_time
, jiffies
- HZ
) ||
643 time_after(tty
->overrun_time
, jiffies
)) {
644 printk(KERN_WARNING
"%s: %d input overrun(s)\n",
647 tty
->overrun_time
= jiffies
;
648 tty
->num_overrun
= 0;
653 * n_tty_receive_parity_error - error notifier
654 * @tty: terminal device
657 * Process a parity error and queue the right data to indicate
658 * the error case if necessary. Locking as per n_tty_receive_buf.
660 static inline void n_tty_receive_parity_error(struct tty_struct
*tty
,
666 put_tty_queue('\377', tty
);
667 put_tty_queue('\0', tty
);
668 put_tty_queue(c
, tty
);
669 } else if (I_INPCK(tty
))
670 put_tty_queue('\0', tty
);
672 put_tty_queue(c
, tty
);
673 wake_up_interruptible(&tty
->read_wait
);
677 * n_tty_receive_char - perform processing
678 * @tty: terminal device
681 * Process an individual character of input received from the driver.
682 * This is serialized with respect to itself by the rules for the
686 static inline void n_tty_receive_char(struct tty_struct
*tty
, unsigned char c
)
691 put_tty_queue(c
, tty
);
697 if (I_IUCLC(tty
) && L_IEXTEN(tty
))
700 if (tty
->stopped
&& !tty
->flow_stopped
&& I_IXON(tty
) &&
701 ((I_IXANY(tty
) && c
!= START_CHAR(tty
) && c
!= STOP_CHAR(tty
)) ||
702 c
== INTR_CHAR(tty
) || c
== QUIT_CHAR(tty
) || c
== SUSP_CHAR(tty
)))
707 if (c
== START_CHAR(tty
))
709 else if (c
== STOP_CHAR(tty
))
716 * If the previous character was LNEXT, or we know that this
717 * character is not one of the characters that we'll have to
718 * handle specially, do shortcut processing to speed things
721 if (!test_bit(c
, tty
->process_char_map
) || tty
->lnext
) {
725 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
726 tty_put_char(tty
, '\a'); /* beep if no space */
729 /* Record the column of first canon char. */
730 if (tty
->canon_head
== tty
->read_head
)
731 tty
->canon_column
= tty
->column
;
734 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
735 put_tty_queue(c
, tty
);
736 put_tty_queue(c
, tty
);
741 if (c
== START_CHAR(tty
)) {
745 if (c
== STOP_CHAR(tty
)) {
754 if (c
== INTR_CHAR(tty
))
757 if (c
== QUIT_CHAR(tty
))
760 if (c
== SUSP_CHAR(tty
)) {
763 * Echo character, and then send the signal.
764 * Note that we do not use isig() here because we want
766 * 1) flush, 2) echo, 3) signal
768 if (!L_NOFLSH(tty
)) {
769 n_tty_flush_buffer(tty
);
770 tty_driver_flush_buffer(tty
);
775 kill_pgrp(tty
->pgrp
, signal
, 1);
785 } else if (c
== '\n' && I_INLCR(tty
))
789 if (c
== ERASE_CHAR(tty
) || c
== KILL_CHAR(tty
) ||
790 (c
== WERASE_CHAR(tty
) && L_IEXTEN(tty
))) {
794 if (c
== LNEXT_CHAR(tty
) && L_IEXTEN(tty
)) {
798 if (L_ECHOCTL(tty
)) {
799 tty_put_char(tty
, '^');
800 tty_put_char(tty
, '\b');
805 if (c
== REPRINT_CHAR(tty
) && L_ECHO(tty
) &&
807 unsigned long tail
= tty
->canon_head
;
812 while (tail
!= tty
->read_head
) {
813 echo_char(tty
->read_buf
[tail
], tty
);
814 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
819 if (L_ECHO(tty
) || L_ECHONL(tty
)) {
820 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1)
821 tty_put_char(tty
, '\a');
826 if (c
== EOF_CHAR(tty
)) {
827 if (tty
->canon_head
!= tty
->read_head
)
828 set_bit(TTY_PUSH
, &tty
->flags
);
832 if ((c
== EOL_CHAR(tty
)) ||
833 (c
== EOL2_CHAR(tty
) && L_IEXTEN(tty
))) {
835 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
838 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1)
839 tty_put_char(tty
, '\a');
840 /* Record the column of first canon char. */
841 if (tty
->canon_head
== tty
->read_head
)
842 tty
->canon_column
= tty
->column
;
846 * XXX does PARMRK doubling happen for
847 * EOL_CHAR and EOL2_CHAR?
849 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
850 put_tty_queue(c
, tty
);
853 spin_lock_irqsave(&tty
->read_lock
, flags
);
854 set_bit(tty
->read_head
, tty
->read_flags
);
855 put_tty_queue_nolock(c
, tty
);
856 tty
->canon_head
= tty
->read_head
;
858 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
859 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
860 if (waitqueue_active(&tty
->read_wait
))
861 wake_up_interruptible(&tty
->read_wait
);
868 if (tty
->read_cnt
>= N_TTY_BUF_SIZE
-1) {
869 tty_put_char(tty
, '\a'); /* beep if no space */
875 /* Record the column of first canon char. */
876 if (tty
->canon_head
== tty
->read_head
)
877 tty
->canon_column
= tty
->column
;
882 if (I_PARMRK(tty
) && c
== (unsigned char) '\377')
883 put_tty_queue(c
, tty
);
885 put_tty_queue(c
, tty
);
890 * n_tty_write_wakeup - asynchronous I/O notifier
893 * Required for the ptys, serial driver etc. since processes
894 * that attach themselves to the master and rely on ASYNC
895 * IO must be woken up
898 static void n_tty_write_wakeup(struct tty_struct
*tty
)
901 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
902 kill_fasync(&tty
->fasync
, SIGIO
, POLL_OUT
);
907 * n_tty_receive_buf - data receive
908 * @tty: terminal device
913 * Called by the terminal driver when a block of characters has
914 * been received. This function must be called from soft contexts
915 * not from interrupt context. The driver is responsible for making
916 * calls one at a time and in order (or using flush_to_ldisc)
919 static void n_tty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
922 const unsigned char *p
;
923 char *f
, flags
= TTY_NORMAL
;
926 unsigned long cpuflags
;
932 spin_lock_irqsave(&tty
->read_lock
, cpuflags
);
933 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
934 N_TTY_BUF_SIZE
- tty
->read_head
);
936 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
937 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
942 i
= min(N_TTY_BUF_SIZE
- tty
->read_cnt
,
943 N_TTY_BUF_SIZE
- tty
->read_head
);
945 memcpy(tty
->read_buf
+ tty
->read_head
, cp
, i
);
946 tty
->read_head
= (tty
->read_head
+ i
) & (N_TTY_BUF_SIZE
-1);
948 spin_unlock_irqrestore(&tty
->read_lock
, cpuflags
);
950 for (i
= count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
955 n_tty_receive_char(tty
, *p
);
958 n_tty_receive_break(tty
);
962 n_tty_receive_parity_error(tty
, *p
);
965 n_tty_receive_overrun(tty
);
968 printk(KERN_ERR
"%s: unknown flag %d\n",
969 tty_name(tty
, buf
), flags
);
973 if (tty
->ops
->flush_chars
)
974 tty
->ops
->flush_chars(tty
);
979 if (!tty
->icanon
&& (tty
->read_cnt
>= tty
->minimum_to_wake
)) {
980 kill_fasync(&tty
->fasync
, SIGIO
, POLL_IN
);
981 if (waitqueue_active(&tty
->read_wait
))
982 wake_up_interruptible(&tty
->read_wait
);
986 * Check the remaining room for the input canonicalization
987 * mode. We don't want to throttle the driver if we're in
988 * canonical mode and don't have a newline yet!
990 if (tty
->receive_room
< TTY_THRESHOLD_THROTTLE
)
994 int is_ignored(int sig
)
996 return (sigismember(¤t
->blocked
, sig
) ||
997 current
->sighand
->action
[sig
-1].sa
.sa_handler
== SIG_IGN
);
1001 * n_tty_set_termios - termios data changed
1003 * @old: previous data
1005 * Called by the tty layer when the user changes termios flags so
1006 * that the line discipline can plan ahead. This function cannot sleep
1007 * and is protected from re-entry by the tty layer. The user is
1008 * guaranteed that this function will not be re-entered or in progress
1009 * when the ldisc is closed.
1012 static void n_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1014 int canon_change
= 1;
1018 canon_change
= (old
->c_lflag
^ tty
->termios
->c_lflag
) & ICANON
;
1020 memset(&tty
->read_flags
, 0, sizeof tty
->read_flags
);
1021 tty
->canon_head
= tty
->read_tail
;
1022 tty
->canon_data
= 0;
1026 if (canon_change
&& !L_ICANON(tty
) && tty
->read_cnt
)
1027 wake_up_interruptible(&tty
->read_wait
);
1029 tty
->icanon
= (L_ICANON(tty
) != 0);
1030 if (test_bit(TTY_HW_COOK_IN
, &tty
->flags
)) {
1033 n_tty_set_room(tty
);
1036 if (I_ISTRIP(tty
) || I_IUCLC(tty
) || I_IGNCR(tty
) ||
1037 I_ICRNL(tty
) || I_INLCR(tty
) || L_ICANON(tty
) ||
1038 I_IXON(tty
) || L_ISIG(tty
) || L_ECHO(tty
) ||
1040 memset(tty
->process_char_map
, 0, 256/8);
1042 if (I_IGNCR(tty
) || I_ICRNL(tty
))
1043 set_bit('\r', tty
->process_char_map
);
1045 set_bit('\n', tty
->process_char_map
);
1047 if (L_ICANON(tty
)) {
1048 set_bit(ERASE_CHAR(tty
), tty
->process_char_map
);
1049 set_bit(KILL_CHAR(tty
), tty
->process_char_map
);
1050 set_bit(EOF_CHAR(tty
), tty
->process_char_map
);
1051 set_bit('\n', tty
->process_char_map
);
1052 set_bit(EOL_CHAR(tty
), tty
->process_char_map
);
1053 if (L_IEXTEN(tty
)) {
1054 set_bit(WERASE_CHAR(tty
),
1055 tty
->process_char_map
);
1056 set_bit(LNEXT_CHAR(tty
),
1057 tty
->process_char_map
);
1058 set_bit(EOL2_CHAR(tty
),
1059 tty
->process_char_map
);
1061 set_bit(REPRINT_CHAR(tty
),
1062 tty
->process_char_map
);
1066 set_bit(START_CHAR(tty
), tty
->process_char_map
);
1067 set_bit(STOP_CHAR(tty
), tty
->process_char_map
);
1070 set_bit(INTR_CHAR(tty
), tty
->process_char_map
);
1071 set_bit(QUIT_CHAR(tty
), tty
->process_char_map
);
1072 set_bit(SUSP_CHAR(tty
), tty
->process_char_map
);
1074 clear_bit(__DISABLED_CHAR
, tty
->process_char_map
);
1079 if ((I_IGNBRK(tty
) || (!I_BRKINT(tty
) && !I_PARMRK(tty
))) &&
1080 (I_IGNPAR(tty
) || !I_INPCK(tty
)) &&
1081 (tty
->driver
->flags
& TTY_DRIVER_REAL_RAW
))
1086 n_tty_set_room(tty
);
1087 /* The termios change make the tty ready for I/O */
1088 wake_up_interruptible(&tty
->write_wait
);
1089 wake_up_interruptible(&tty
->read_wait
);
1093 * n_tty_close - close the ldisc for this tty
1096 * Called from the terminal layer when this line discipline is
1097 * being shut down, either because of a close or becsuse of a
1098 * discipline change. The function will not be called while other
1099 * ldisc methods are in progress.
1102 static void n_tty_close(struct tty_struct
*tty
)
1104 n_tty_flush_buffer(tty
);
1105 if (tty
->read_buf
) {
1106 free_buf(tty
->read_buf
);
1107 tty
->read_buf
= NULL
;
1112 * n_tty_open - open an ldisc
1113 * @tty: terminal to open
1115 * Called when this line discipline is being attached to the
1116 * terminal device. Can sleep. Called serialized so that no
1117 * other events will occur in parallel. No further open will occur
1121 static int n_tty_open(struct tty_struct
*tty
)
1126 /* This one is ugly. Currently a malloc failure here can panic */
1127 if (!tty
->read_buf
) {
1128 tty
->read_buf
= alloc_buf();
1132 memset(tty
->read_buf
, 0, N_TTY_BUF_SIZE
);
1133 reset_buffer_flags(tty
);
1135 n_tty_set_termios(tty
, NULL
);
1136 tty
->minimum_to_wake
= 1;
1141 static inline int input_available_p(struct tty_struct
*tty
, int amt
)
1144 if (tty
->canon_data
)
1146 } else if (tty
->read_cnt
>= (amt
? amt
: 1))
1153 * copy_from_read_buf - copy read data directly
1154 * @tty: terminal device
1158 * Helper function to speed up read_chan. It is only called when
1159 * ICANON is off; it copies characters straight from the tty queue to
1160 * user space directly. It can be profitably called twice; once to
1161 * drain the space from the tail pointer to the (physical) end of the
1162 * buffer, and once to drain the space from the (physical) beginning of
1163 * the buffer to head pointer.
1165 * Called under the tty->atomic_read_lock sem
1169 static int copy_from_read_buf(struct tty_struct
*tty
,
1170 unsigned char __user
**b
,
1176 unsigned long flags
;
1179 spin_lock_irqsave(&tty
->read_lock
, flags
);
1180 n
= min(tty
->read_cnt
, N_TTY_BUF_SIZE
- tty
->read_tail
);
1182 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1184 retval
= copy_to_user(*b
, &tty
->read_buf
[tty
->read_tail
], n
);
1186 tty_audit_add_data(tty
, &tty
->read_buf
[tty
->read_tail
], n
);
1187 spin_lock_irqsave(&tty
->read_lock
, flags
);
1188 tty
->read_tail
= (tty
->read_tail
+ n
) & (N_TTY_BUF_SIZE
-1);
1190 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1197 extern ssize_t
redirected_tty_write(struct file
*, const char __user
*,
1201 * job_control - check job control
1203 * @file: file handle
1205 * Perform job control management checks on this file/tty descriptor
1206 * and if appropriate send any needed signals and return a negative
1207 * error code if action should be taken.
1210 * Locking: None - redirected write test is safe, testing
1211 * current->signal should possibly lock current->sighand
1215 static int job_control(struct tty_struct
*tty
, struct file
*file
)
1217 /* Job control check -- must be done at start and after
1218 every sleep (POSIX.1 7.1.1.4). */
1219 /* NOTE: not yet done after every sleep pending a thorough
1220 check of the logic of this change. -- jlc */
1221 /* don't stop on /dev/console */
1222 if (file
->f_op
->write
!= redirected_tty_write
&&
1223 current
->signal
->tty
== tty
) {
1225 printk(KERN_ERR
"read_chan: no tty->pgrp!\n");
1226 else if (task_pgrp(current
) != tty
->pgrp
) {
1227 if (is_ignored(SIGTTIN
) ||
1228 is_current_pgrp_orphaned())
1230 kill_pgrp(task_pgrp(current
), SIGTTIN
, 1);
1231 set_thread_flag(TIF_SIGPENDING
);
1232 return -ERESTARTSYS
;
1240 * read_chan - read function for tty
1242 * @file: file object
1243 * @buf: userspace buffer pointer
1246 * Perform reads for the line discipline. We are guaranteed that the
1247 * line discipline will not be closed under us but we may get multiple
1248 * parallel readers and must handle this ourselves. We may also get
1249 * a hangup. Always called in user context, may sleep.
1251 * This code must be sure never to sleep through a hangup.
1254 static ssize_t
read_chan(struct tty_struct
*tty
, struct file
*file
,
1255 unsigned char __user
*buf
, size_t nr
)
1257 unsigned char __user
*b
= buf
;
1258 DECLARE_WAITQUEUE(wait
, current
);
1264 unsigned long flags
;
1269 if (!tty
->read_buf
) {
1270 printk(KERN_ERR
"n_tty_read_chan: read_buf == NULL?!?\n");
1274 c
= job_control(tty
, file
);
1279 timeout
= MAX_SCHEDULE_TIMEOUT
;
1281 time
= (HZ
/ 10) * TIME_CHAR(tty
);
1282 minimum
= MIN_CHAR(tty
);
1285 tty
->minimum_to_wake
= 1;
1286 else if (!waitqueue_active(&tty
->read_wait
) ||
1287 (tty
->minimum_to_wake
> minimum
))
1288 tty
->minimum_to_wake
= minimum
;
1295 tty
->minimum_to_wake
= minimum
= 1;
1300 * Internal serialization of reads.
1302 if (file
->f_flags
& O_NONBLOCK
) {
1303 if (!mutex_trylock(&tty
->atomic_read_lock
))
1306 if (mutex_lock_interruptible(&tty
->atomic_read_lock
))
1307 return -ERESTARTSYS
;
1309 packet
= tty
->packet
;
1311 add_wait_queue(&tty
->read_wait
, &wait
);
1313 /* First test for status change. */
1314 if (packet
&& tty
->link
->ctrl_status
) {
1318 spin_lock_irqsave(&tty
->link
->ctrl_lock
, flags
);
1319 cs
= tty
->link
->ctrl_status
;
1320 tty
->link
->ctrl_status
= 0;
1321 spin_unlock_irqrestore(&tty
->link
->ctrl_lock
, flags
);
1322 if (tty_put_user(tty
, cs
, b
++)) {
1330 /* This statement must be first before checking for input
1331 so that any interrupt will set the state back to
1333 set_current_state(TASK_INTERRUPTIBLE
);
1335 if (((minimum
- (b
- buf
)) < tty
->minimum_to_wake
) &&
1336 ((minimum
- (b
- buf
)) >= 1))
1337 tty
->minimum_to_wake
= (minimum
- (b
- buf
));
1339 if (!input_available_p(tty
, 0)) {
1340 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
)) {
1344 if (tty_hung_up_p(file
))
1348 if (file
->f_flags
& O_NONBLOCK
) {
1352 if (signal_pending(current
)) {
1353 retval
= -ERESTARTSYS
;
1356 /* FIXME: does n_tty_set_room need locking ? */
1357 n_tty_set_room(tty
);
1358 timeout
= schedule_timeout(timeout
);
1361 __set_current_state(TASK_RUNNING
);
1363 /* Deal with packet mode. */
1364 if (packet
&& b
== buf
) {
1365 if (tty_put_user(tty
, TIOCPKT_DATA
, b
++)) {
1374 /* N.B. avoid overrun if nr == 0 */
1375 while (nr
&& tty
->read_cnt
) {
1378 eol
= test_and_clear_bit(tty
->read_tail
,
1380 c
= tty
->read_buf
[tty
->read_tail
];
1381 spin_lock_irqsave(&tty
->read_lock
, flags
);
1382 tty
->read_tail
= ((tty
->read_tail
+1) &
1383 (N_TTY_BUF_SIZE
-1));
1386 /* this test should be redundant:
1387 * we shouldn't be reading data if
1390 if (--tty
->canon_data
< 0)
1391 tty
->canon_data
= 0;
1393 spin_unlock_irqrestore(&tty
->read_lock
, flags
);
1395 if (!eol
|| (c
!= __DISABLED_CHAR
)) {
1396 if (tty_put_user(tty
, c
, b
++)) {
1404 tty_audit_push(tty
);
1412 /* The copy function takes the read lock and handles
1413 locking internally for this case */
1414 uncopied
= copy_from_read_buf(tty
, &b
, &nr
);
1415 uncopied
+= copy_from_read_buf(tty
, &b
, &nr
);
1422 /* If there is enough space in the read buffer now, let the
1423 * low-level driver know. We use n_tty_chars_in_buffer() to
1424 * check the buffer, as it now knows about canonical mode.
1425 * Otherwise, if the driver is throttled and the line is
1426 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1427 * we won't get any more characters.
1429 if (n_tty_chars_in_buffer(tty
) <= TTY_THRESHOLD_UNTHROTTLE
) {
1430 n_tty_set_room(tty
);
1431 check_unthrottle(tty
);
1434 if (b
- buf
>= minimum
)
1439 mutex_unlock(&tty
->atomic_read_lock
);
1440 remove_wait_queue(&tty
->read_wait
, &wait
);
1442 if (!waitqueue_active(&tty
->read_wait
))
1443 tty
->minimum_to_wake
= minimum
;
1445 __set_current_state(TASK_RUNNING
);
1450 clear_bit(TTY_PUSH
, &tty
->flags
);
1451 } else if (test_and_clear_bit(TTY_PUSH
, &tty
->flags
))
1454 n_tty_set_room(tty
);
1459 * write_chan - write function for tty
1461 * @file: file object
1462 * @buf: userspace buffer pointer
1465 * Write function of the terminal device. This is serialized with
1466 * respect to other write callers but not to termios changes, reads
1467 * and other such events. We must be careful with N_TTY as the receive
1468 * code will echo characters, thus calling driver write methods.
1470 * This code must be sure never to sleep through a hangup.
1473 static ssize_t
write_chan(struct tty_struct
*tty
, struct file
*file
,
1474 const unsigned char *buf
, size_t nr
)
1476 const unsigned char *b
= buf
;
1477 DECLARE_WAITQUEUE(wait
, current
);
1481 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1482 if (L_TOSTOP(tty
) && file
->f_op
->write
!= redirected_tty_write
) {
1483 retval
= tty_check_change(tty
);
1488 add_wait_queue(&tty
->write_wait
, &wait
);
1490 set_current_state(TASK_INTERRUPTIBLE
);
1491 if (signal_pending(current
)) {
1492 retval
= -ERESTARTSYS
;
1495 if (tty_hung_up_p(file
) || (tty
->link
&& !tty
->link
->count
)) {
1499 if (O_OPOST(tty
) && !(test_bit(TTY_HW_COOK_OUT
, &tty
->flags
))) {
1501 ssize_t num
= opost_block(tty
, b
, nr
);
1513 if (opost(c
, tty
) < 0)
1517 if (tty
->ops
->flush_chars
)
1518 tty
->ops
->flush_chars(tty
);
1521 c
= tty
->ops
->write(tty
, b
, nr
);
1534 if (file
->f_flags
& O_NONBLOCK
) {
1541 __set_current_state(TASK_RUNNING
);
1542 remove_wait_queue(&tty
->write_wait
, &wait
);
1543 return (b
- buf
) ? b
- buf
: retval
;
1547 * normal_poll - poll method for N_TTY
1548 * @tty: terminal device
1549 * @file: file accessing it
1552 * Called when the line discipline is asked to poll() for data or
1553 * for special events. This code is not serialized with respect to
1554 * other events save open/close.
1556 * This code must be sure never to sleep through a hangup.
1557 * Called without the kernel lock held - fine
1560 static unsigned int normal_poll(struct tty_struct
*tty
, struct file
*file
,
1563 unsigned int mask
= 0;
1565 poll_wait(file
, &tty
->read_wait
, wait
);
1566 poll_wait(file
, &tty
->write_wait
, wait
);
1567 if (input_available_p(tty
, TIME_CHAR(tty
) ? 0 : MIN_CHAR(tty
)))
1568 mask
|= POLLIN
| POLLRDNORM
;
1569 if (tty
->packet
&& tty
->link
->ctrl_status
)
1570 mask
|= POLLPRI
| POLLIN
| POLLRDNORM
;
1571 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
))
1573 if (tty_hung_up_p(file
))
1575 if (!(mask
& (POLLHUP
| POLLIN
| POLLRDNORM
))) {
1576 if (MIN_CHAR(tty
) && !TIME_CHAR(tty
))
1577 tty
->minimum_to_wake
= MIN_CHAR(tty
);
1579 tty
->minimum_to_wake
= 1;
1581 if (tty
->ops
->write
&& !tty_is_writelocked(tty
) &&
1582 tty_chars_in_buffer(tty
) < WAKEUP_CHARS
&&
1583 tty_write_room(tty
) > 0)
1584 mask
|= POLLOUT
| POLLWRNORM
;
1588 static unsigned long inq_canon(struct tty_struct
*tty
)
1592 if (!tty
->canon_data
|| !tty
->read_buf
)
1594 head
= tty
->canon_head
;
1595 tail
= tty
->read_tail
;
1596 nr
= (head
- tail
) & (N_TTY_BUF_SIZE
-1);
1597 /* Skip EOF-chars.. */
1598 while (head
!= tail
) {
1599 if (test_bit(tail
, tty
->read_flags
) &&
1600 tty
->read_buf
[tail
] == __DISABLED_CHAR
)
1602 tail
= (tail
+1) & (N_TTY_BUF_SIZE
-1);
1607 static int n_tty_ioctl(struct tty_struct
*tty
, struct file
*file
,
1608 unsigned int cmd
, unsigned long arg
)
1614 return put_user(tty_chars_in_buffer(tty
), (int __user
*) arg
);
1616 retval
= tty
->read_cnt
;
1618 retval
= inq_canon(tty
);
1619 return put_user(retval
, (unsigned int __user
*) arg
);
1621 return n_tty_ioctl_helper(tty
, file
, cmd
, arg
);
1625 struct tty_ldisc_ops tty_ldisc_N_TTY
= {
1626 .magic
= TTY_LDISC_MAGIC
,
1629 .close
= n_tty_close
,
1630 .flush_buffer
= n_tty_flush_buffer
,
1631 .chars_in_buffer
= n_tty_chars_in_buffer
,
1633 .write
= write_chan
,
1634 .ioctl
= n_tty_ioctl
,
1635 .set_termios
= n_tty_set_termios
,
1636 .poll
= normal_poll
,
1637 .receive_buf
= n_tty_receive_buf
,
1638 .write_wakeup
= n_tty_write_wakeup