1 // SPDX-License-Identifier: GPL-1.0+
3 * n_tty.c --- implements the N_TTY line discipline.
5 * This code used to be in tty_io.c, but things are getting hairy
6 * enough that it made sense to split things off. (The N_TTY
7 * processing has changed so much that it's hardly recognizable,
10 * Note that the open routine for N_TTY is guaranteed never to return
11 * an error. This is because Linux will fall back to setting a line
12 * to N_TTY if it can not switch to any other line discipline.
14 * Written by Theodore Ts'o, Copyright 1994.
16 * This file also contains code originally written by Linus Torvalds,
17 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
19 * Reduced memory usage for older ARM systems - Russell King.
21 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
22 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
23 * who actually finally proved there really was a race.
25 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
26 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
27 * Also fixed a bug in BLOCKING mode where n_tty_write returns
31 #include <linux/types.h>
32 #include <linux/major.h>
33 #include <linux/errno.h>
34 #include <linux/signal.h>
35 #include <linux/fcntl.h>
36 #include <linux/sched.h>
37 #include <linux/interrupt.h>
38 #include <linux/tty.h>
39 #include <linux/timer.h>
40 #include <linux/ctype.h>
42 #include <linux/string.h>
43 #include <linux/slab.h>
44 #include <linux/poll.h>
45 #include <linux/bitops.h>
46 #include <linux/audit.h>
47 #include <linux/file.h>
48 #include <linux/uaccess.h>
49 #include <linux/module.h>
50 #include <linux/ratelimit.h>
51 #include <linux/vmalloc.h>
55 * Until this number of characters is queued in the xmit buffer, select will
56 * return "we have room for writes".
58 #define WAKEUP_CHARS 256
61 * This defines the low- and high-watermarks for throttling and
62 * unthrottling the TTY driver. These watermarks are used for
63 * controlling the space in the read buffer.
65 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
66 #define TTY_THRESHOLD_UNTHROTTLE 128
69 * Special byte codes used in the echo buffer to represent operations
70 * or special handling of characters. Bytes in the echo buffer that
71 * are not part of such special blocks are treated as normal character
74 #define ECHO_OP_START 0xff
75 #define ECHO_OP_MOVE_BACK_COL 0x80
76 #define ECHO_OP_SET_CANON_COL 0x81
77 #define ECHO_OP_ERASE_TAB 0x82
79 #define ECHO_COMMIT_WATERMARK 256
80 #define ECHO_BLOCK 256
81 #define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
86 # define n_tty_trace(f, args...) trace_printk(f, ##args)
88 # define n_tty_trace(f, args...) no_printk(f, ##args)
92 /* producer-published */
99 DECLARE_BITMAP(char_map, 256);
101 /* private to n_tty_receive_overrun (single-threaded) */
102 unsigned long overrun_time;
108 /* must hold exclusive termios_rwsem to reset these */
109 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
110 unsigned char push:1;
112 /* shared by producer and consumer */
113 char read_buf[N_TTY_BUF_SIZE];
114 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
115 unsigned char echo_buf[N_TTY_BUF_SIZE];
117 /* consumer-published */
121 /* protected by output lock */
123 unsigned int canon_column;
126 struct mutex atomic_read_lock;
127 struct mutex output_lock;
130 #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
132 static inline size_t read_cnt(struct n_tty_data *ldata)
134 return ldata->read_head - ldata->read_tail;
137 static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
139 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
142 static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
144 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
147 static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
149 smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
150 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
153 static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
155 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
158 /* If we are not echoing the data, perhaps this is a secret so erase it */
159 static void zero_buffer(struct tty_struct *tty, u8 *buffer, int size)
161 bool icanon = !!L_ICANON(tty);
162 bool no_echo = !L_ECHO(tty);
164 if (icanon && no_echo)
165 memset(buffer, 0x00, size);
168 static void tty_copy(struct tty_struct *tty, void *to, size_t tail, size_t n)
170 struct n_tty_data *ldata = tty->disc_data;
171 size_t size = N_TTY_BUF_SIZE - tail;
172 void *from = read_buf_addr(ldata, tail);
175 tty_audit_add_data(tty, from, size);
176 memcpy(to, from, size);
177 zero_buffer(tty, from, size);
180 from = ldata->read_buf;
183 tty_audit_add_data(tty, from, n);
185 zero_buffer(tty, from, n);
189 * n_tty_kick_worker - start input worker (if required)
192 * Re-schedules the flip buffer work if it may have stopped.
195 * * Caller holds exclusive %termios_rwsem, or
196 * * n_tty_read()/consumer path:
197 * holds non-exclusive %termios_rwsem
199 static void n_tty_kick_worker(struct tty_struct *tty)
201 struct n_tty_data *ldata = tty->disc_data;
203 /* Did the input worker stop? Restart it */
204 if (unlikely(ldata->no_room)) {
207 WARN_RATELIMIT(tty->port->itty == NULL,
208 "scheduling with invalid itty\n");
209 /* see if ldisc has been killed - if so, this means that
210 * even though the ldisc has been halted and ->buf.work
211 * cancelled, ->buf.work is about to be rescheduled
213 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
214 "scheduling buffer work for halted ldisc\n");
215 tty_buffer_restart_work(tty->port);
219 static ssize_t chars_in_buffer(struct tty_struct *tty)
221 struct n_tty_data *ldata = tty->disc_data;
225 n = ldata->commit_head - ldata->read_tail;
227 n = ldata->canon_head - ldata->read_tail;
232 * n_tty_write_wakeup - asynchronous I/O notifier
235 * Required for the ptys, serial driver etc. since processes that attach
236 * themselves to the master and rely on ASYNC IO must be woken up.
238 static void n_tty_write_wakeup(struct tty_struct *tty)
240 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
241 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
244 static void n_tty_check_throttle(struct tty_struct *tty)
246 struct n_tty_data *ldata = tty->disc_data;
249 * Check the remaining room for the input canonicalization
250 * mode. We don't want to throttle the driver if we're in
251 * canonical mode and don't have a newline yet!
253 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
258 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
259 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
261 throttled = tty_throttle_safe(tty);
265 __tty_set_flow_change(tty, 0);
268 static void n_tty_check_unthrottle(struct tty_struct *tty)
270 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
271 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
273 n_tty_kick_worker(tty);
274 tty_wakeup(tty->link);
278 /* If there is enough space in the read buffer now, let the
279 * low-level driver know. We use chars_in_buffer() to
280 * check the buffer, as it now knows about canonical mode.
281 * Otherwise, if the driver is throttled and the line is
282 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
283 * we won't get any more characters.
288 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
289 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
291 n_tty_kick_worker(tty);
292 unthrottled = tty_unthrottle_safe(tty);
296 __tty_set_flow_change(tty, 0);
300 * put_tty_queue - add character to tty
304 * Add a character to the tty read_buf queue.
307 * * n_tty_receive_buf()/producer path:
308 * caller holds non-exclusive %termios_rwsem
310 static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
312 *read_buf_addr(ldata, ldata->read_head) = c;
317 * reset_buffer_flags - reset buffer state
318 * @ldata: line disc data to reset
320 * Reset the read buffer counters and clear the flags. Called from
321 * n_tty_open() and n_tty_flush_buffer().
324 * * caller holds exclusive %termios_rwsem, or
325 * * (locking is not required)
327 static void reset_buffer_flags(struct n_tty_data *ldata)
329 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
330 ldata->commit_head = 0;
331 ldata->line_start = 0;
334 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
338 static void n_tty_packet_mode_flush(struct tty_struct *tty)
342 if (tty->link->ctrl.packet) {
343 spin_lock_irqsave(&tty->ctrl.lock, flags);
344 tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD;
345 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
346 wake_up_interruptible(&tty->link->read_wait);
351 * n_tty_flush_buffer - clean input queue
352 * @tty: terminal device
354 * Flush the input buffer. Called when the tty layer wants the buffer flushed
355 * (eg at hangup) or when the %N_TTY line discipline internally has to clean
356 * the pending queue (for example some signals).
358 * Holds %termios_rwsem to exclude producer/consumer while buffer indices are
361 * Locking: %ctrl.lock, exclusive %termios_rwsem
363 static void n_tty_flush_buffer(struct tty_struct *tty)
365 down_write(&tty->termios_rwsem);
366 reset_buffer_flags(tty->disc_data);
367 n_tty_kick_worker(tty);
370 n_tty_packet_mode_flush(tty);
371 up_write(&tty->termios_rwsem);
375 * is_utf8_continuation - utf8 multibyte check
378 * Returns: true if the utf8 character @c is a multibyte continuation
379 * character. We use this to correctly compute the on-screen size of the
380 * character when printing.
382 static inline int is_utf8_continuation(unsigned char c)
384 return (c & 0xc0) == 0x80;
388 * is_continuation - multibyte check
390 * @tty: terminal device
392 * Returns: true if the utf8 character @c is a multibyte continuation character
393 * and the terminal is in unicode mode.
395 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
397 return I_IUTF8(tty) && is_utf8_continuation(c);
401 * do_output_char - output one character
402 * @c: character (or partial unicode symbol)
403 * @tty: terminal device
404 * @space: space available in tty driver write buffer
406 * This is a helper function that handles one output character (including
407 * special characters like TAB, CR, LF, etc.), doing OPOST processing and
408 * putting the results in the tty driver's write buffer.
410 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY.
411 * They simply aren't relevant in the world today. If you ever need them, add
414 * Returns: the number of bytes of buffer space used or -1 if no space left.
416 * Locking: should be called under the %output_lock to protect the column state
417 * and space left in the buffer.
419 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
421 struct n_tty_data *ldata = tty->disc_data;
434 ldata->canon_column = ldata->column = 0;
435 tty->ops->write(tty, "\r\n", 2);
438 ldata->canon_column = ldata->column;
441 if (O_ONOCR(tty) && ldata->column == 0)
446 ldata->canon_column = ldata->column = 0;
449 ldata->canon_column = ldata->column = 0;
452 spaces = 8 - (ldata->column & 7);
453 if (O_TABDLY(tty) == XTABS) {
456 ldata->column += spaces;
457 tty->ops->write(tty, " ", spaces);
460 ldata->column += spaces;
463 if (ldata->column > 0)
470 if (!is_continuation(c, tty))
476 tty_put_char(tty, c);
481 * process_output - output post processor
482 * @c: character (or partial unicode symbol)
483 * @tty: terminal device
485 * Output one character with OPOST processing.
487 * Returns: -1 when the output device is full and the character must be
490 * Locking: %output_lock to protect column state and space left (also, this is
491 *called from n_tty_write() under the tty layer write lock).
493 static int process_output(unsigned char c, struct tty_struct *tty)
495 struct n_tty_data *ldata = tty->disc_data;
498 mutex_lock(&ldata->output_lock);
500 space = tty_write_room(tty);
501 retval = do_output_char(c, tty, space);
503 mutex_unlock(&ldata->output_lock);
511 * process_output_block - block post processor
512 * @tty: terminal device
513 * @buf: character buffer
514 * @nr: number of bytes to output
516 * Output a block of characters with OPOST processing.
518 * This path is used to speed up block console writes, among other things when
519 * processing blocks of output data. It handles only the simple cases normally
520 * found and helps to generate blocks of symbols for the console driver and
521 * thus improve performance.
523 * Returns: the number of characters output.
525 * Locking: %output_lock to protect column state and space left (also, this is
526 * called from n_tty_write() under the tty layer write lock).
528 static ssize_t process_output_block(struct tty_struct *tty,
529 const unsigned char *buf, unsigned int nr)
531 struct n_tty_data *ldata = tty->disc_data;
534 const unsigned char *cp;
536 mutex_lock(&ldata->output_lock);
538 space = tty_write_room(tty);
540 mutex_unlock(&ldata->output_lock);
546 for (i = 0, cp = buf; i < nr; i++, cp++) {
547 unsigned char c = *cp;
555 ldata->canon_column = ldata->column;
558 if (O_ONOCR(tty) && ldata->column == 0)
562 ldata->canon_column = ldata->column = 0;
567 if (ldata->column > 0)
574 if (!is_continuation(c, tty))
581 i = tty->ops->write(tty, buf, i);
583 mutex_unlock(&ldata->output_lock);
588 * __process_echoes - write pending echo characters
589 * @tty: terminal device
591 * Write previously buffered echo (and other ldisc-generated) characters to the
594 * Characters generated by the ldisc (including echoes) need to be buffered
595 * because the driver's write buffer can fill during heavy program output.
596 * Echoing straight to the driver will often fail under these conditions,
597 * causing lost characters and resulting mismatches of ldisc state information.
599 * Since the ldisc state must represent the characters actually sent to the
600 * driver at the time of the write, operations like certain changes in column
601 * state are also saved in the buffer and executed here.
603 * A circular fifo buffer is used so that the most recent characters are
604 * prioritized. Also, when control characters are echoed with a prefixed "^",
605 * the pair is treated atomically and thus not separated.
607 * Locking: callers must hold %output_lock.
609 static size_t __process_echoes(struct tty_struct *tty)
611 struct n_tty_data *ldata = tty->disc_data;
612 int space, old_space;
616 old_space = space = tty_write_room(tty);
618 tail = ldata->echo_tail;
619 while (MASK(ldata->echo_commit) != MASK(tail)) {
620 c = echo_buf(ldata, tail);
621 if (c == ECHO_OP_START) {
623 int no_space_left = 0;
626 * Since add_echo_byte() is called without holding
627 * output_lock, we might see only portion of multi-byte
630 if (MASK(ldata->echo_commit) == MASK(tail + 1))
633 * If the buffer byte is the start of a multi-byte
634 * operation, get the next byte, which is either the
635 * op code or a control character value.
637 op = echo_buf(ldata, tail + 1);
640 case ECHO_OP_ERASE_TAB: {
641 unsigned int num_chars, num_bs;
643 if (MASK(ldata->echo_commit) == MASK(tail + 2))
645 num_chars = echo_buf(ldata, tail + 2);
648 * Determine how many columns to go back
649 * in order to erase the tab.
650 * This depends on the number of columns
651 * used by other characters within the tab
652 * area. If this (modulo 8) count is from
653 * the start of input rather than from a
654 * previous tab, we offset by canon column.
655 * Otherwise, tab spacing is normal.
657 if (!(num_chars & 0x80))
658 num_chars += ldata->canon_column;
659 num_bs = 8 - (num_chars & 7);
661 if (num_bs > space) {
667 tty_put_char(tty, '\b');
668 if (ldata->column > 0)
674 case ECHO_OP_SET_CANON_COL:
675 ldata->canon_column = ldata->column;
679 case ECHO_OP_MOVE_BACK_COL:
680 if (ldata->column > 0)
686 /* This is an escaped echo op start code */
691 tty_put_char(tty, ECHO_OP_START);
699 * If the op is not a special byte code,
700 * it is a ctrl char tagged to be echoed
701 * as "^X" (where X is the letter
702 * representing the control char).
703 * Note that we must ensure there is
704 * enough space for the whole ctrl pair.
711 tty_put_char(tty, '^');
712 tty_put_char(tty, op ^ 0100);
722 int retval = do_output_char(c, tty, space);
729 tty_put_char(tty, c);
736 /* If the echo buffer is nearly full (so that the possibility exists
737 * of echo overrun before the next commit), then discard enough
738 * data at the tail to prevent a subsequent overrun */
739 while (ldata->echo_commit > tail &&
740 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
741 if (echo_buf(ldata, tail) == ECHO_OP_START) {
742 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
751 ldata->echo_tail = tail;
752 return old_space - space;
755 static void commit_echoes(struct tty_struct *tty)
757 struct n_tty_data *ldata = tty->disc_data;
758 size_t nr, old, echoed;
761 mutex_lock(&ldata->output_lock);
762 head = ldata->echo_head;
763 ldata->echo_mark = head;
764 old = ldata->echo_commit - ldata->echo_tail;
766 /* Process committed echoes if the accumulated # of bytes
767 * is over the threshold (and try again each time another
768 * block is accumulated) */
769 nr = head - ldata->echo_tail;
770 if (nr < ECHO_COMMIT_WATERMARK ||
771 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
772 mutex_unlock(&ldata->output_lock);
776 ldata->echo_commit = head;
777 echoed = __process_echoes(tty);
778 mutex_unlock(&ldata->output_lock);
780 if (echoed && tty->ops->flush_chars)
781 tty->ops->flush_chars(tty);
784 static void process_echoes(struct tty_struct *tty)
786 struct n_tty_data *ldata = tty->disc_data;
789 if (ldata->echo_mark == ldata->echo_tail)
792 mutex_lock(&ldata->output_lock);
793 ldata->echo_commit = ldata->echo_mark;
794 echoed = __process_echoes(tty);
795 mutex_unlock(&ldata->output_lock);
797 if (echoed && tty->ops->flush_chars)
798 tty->ops->flush_chars(tty);
801 /* NB: echo_mark and echo_head should be equivalent here */
802 static void flush_echoes(struct tty_struct *tty)
804 struct n_tty_data *ldata = tty->disc_data;
806 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
807 ldata->echo_commit == ldata->echo_head)
810 mutex_lock(&ldata->output_lock);
811 ldata->echo_commit = ldata->echo_head;
812 __process_echoes(tty);
813 mutex_unlock(&ldata->output_lock);
817 * add_echo_byte - add a byte to the echo buffer
818 * @c: unicode byte to echo
821 * Add a character or operation byte to the echo buffer.
823 static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
825 *echo_buf_addr(ldata, ldata->echo_head) = c;
826 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
831 * echo_move_back_col - add operation to move back a column
834 * Add an operation to the echo buffer to move back one column.
836 static void echo_move_back_col(struct n_tty_data *ldata)
838 add_echo_byte(ECHO_OP_START, ldata);
839 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
843 * echo_set_canon_col - add operation to set the canon column
846 * Add an operation to the echo buffer to set the canon column to the current
849 static void echo_set_canon_col(struct n_tty_data *ldata)
851 add_echo_byte(ECHO_OP_START, ldata);
852 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
856 * echo_erase_tab - add operation to erase a tab
857 * @num_chars: number of character columns already used
858 * @after_tab: true if num_chars starts after a previous tab
861 * Add an operation to the echo buffer to erase a tab.
863 * Called by the eraser function, which knows how many character columns have
864 * been used since either a previous tab or the start of input. This
865 * information will be used later, along with canon column (if applicable), to
866 * go back the correct number of columns.
868 static void echo_erase_tab(unsigned int num_chars, int after_tab,
869 struct n_tty_data *ldata)
871 add_echo_byte(ECHO_OP_START, ldata);
872 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
874 /* We only need to know this modulo 8 (tab spacing) */
877 /* Set the high bit as a flag if num_chars is after a previous tab */
881 add_echo_byte(num_chars, ldata);
885 * echo_char_raw - echo a character raw
886 * @c: unicode byte to echo
887 * @ldata: line disc data
889 * Echo user input back onto the screen. This must be called only when
890 * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
892 * This variant does not treat control characters specially.
894 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
896 if (c == ECHO_OP_START) {
897 add_echo_byte(ECHO_OP_START, ldata);
898 add_echo_byte(ECHO_OP_START, ldata);
900 add_echo_byte(c, ldata);
905 * echo_char - echo a character
906 * @c: unicode byte to echo
907 * @tty: terminal device
909 * Echo user input back onto the screen. This must be called only when
910 * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
912 * This variant tags control characters to be echoed as "^X" (where X is the
913 * letter representing the control char).
915 static void echo_char(unsigned char c, struct tty_struct *tty)
917 struct n_tty_data *ldata = tty->disc_data;
919 if (c == ECHO_OP_START) {
920 add_echo_byte(ECHO_OP_START, ldata);
921 add_echo_byte(ECHO_OP_START, ldata);
923 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
924 add_echo_byte(ECHO_OP_START, ldata);
925 add_echo_byte(c, ldata);
930 * finish_erasing - complete erase
933 static inline void finish_erasing(struct n_tty_data *ldata)
935 if (ldata->erasing) {
936 echo_char_raw('/', ldata);
942 * eraser - handle erase function
943 * @c: character input
944 * @tty: terminal device
946 * Perform erase and necessary output when an erase character is present in the
947 * stream from the driver layer. Handles the complexities of UTF-8 multibyte
950 * Locking: n_tty_receive_buf()/producer path:
951 * caller holds non-exclusive %termios_rwsem
953 static void eraser(unsigned char c, struct tty_struct *tty)
955 struct n_tty_data *ldata = tty->disc_data;
956 enum { ERASE, WERASE, KILL } kill_type;
961 if (ldata->read_head == ldata->canon_head) {
962 /* process_output('\a', tty); */ /* what do you think? */
965 if (c == ERASE_CHAR(tty))
967 else if (c == WERASE_CHAR(tty))
971 ldata->read_head = ldata->canon_head;
974 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
975 ldata->read_head = ldata->canon_head;
976 finish_erasing(ldata);
977 echo_char(KILL_CHAR(tty), tty);
978 /* Add a newline if ECHOK is on and ECHOKE is off. */
980 echo_char_raw('\n', ldata);
987 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
988 head = ldata->read_head;
990 /* erase a single possibly multibyte character */
993 c = read_buf(ldata, head);
994 } while (is_continuation(c, tty) &&
995 MASK(head) != MASK(ldata->canon_head));
997 /* do not partially erase */
998 if (is_continuation(c, tty))
1001 if (kill_type == WERASE) {
1002 /* Equivalent to BSD's ALTWERASE. */
1003 if (isalnum(c) || c == '_')
1005 else if (seen_alnums)
1008 cnt = ldata->read_head - head;
1009 ldata->read_head = head;
1011 if (L_ECHOPRT(tty)) {
1012 if (!ldata->erasing) {
1013 echo_char_raw('\\', ldata);
1016 /* if cnt > 1, output a multi-byte character */
1020 echo_char_raw(read_buf(ldata, head), ldata);
1021 echo_move_back_col(ldata);
1023 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1024 echo_char(ERASE_CHAR(tty), tty);
1025 } else if (c == '\t') {
1026 unsigned int num_chars = 0;
1028 size_t tail = ldata->read_head;
1031 * Count the columns used for characters
1032 * since the start of input or after a
1034 * This info is used to go back the correct
1035 * number of columns.
1037 while (MASK(tail) != MASK(ldata->canon_head)) {
1039 c = read_buf(ldata, tail);
1043 } else if (iscntrl(c)) {
1046 } else if (!is_continuation(c, tty)) {
1050 echo_erase_tab(num_chars, after_tab, ldata);
1052 if (iscntrl(c) && L_ECHOCTL(tty)) {
1053 echo_char_raw('\b', ldata);
1054 echo_char_raw(' ', ldata);
1055 echo_char_raw('\b', ldata);
1057 if (!iscntrl(c) || L_ECHOCTL(tty)) {
1058 echo_char_raw('\b', ldata);
1059 echo_char_raw(' ', ldata);
1060 echo_char_raw('\b', ldata);
1064 if (kill_type == ERASE)
1067 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1068 finish_erasing(ldata);
1072 static void __isig(int sig, struct tty_struct *tty)
1074 struct pid *tty_pgrp = tty_get_pgrp(tty);
1076 kill_pgrp(tty_pgrp, sig, 1);
1082 * isig - handle the ISIG optio
1086 * Called when a signal is being sent due to terminal input. Called from the
1087 * &tty_driver.receive_buf() path, so serialized.
1089 * Performs input and output flush if !NOFLSH. In this context, the echo
1090 * buffer is 'output'. The signal is processed first to alert any current
1091 * readers or writers to discontinue and exit their i/o loops.
1093 * Locking: %ctrl.lock
1095 static void isig(int sig, struct tty_struct *tty)
1097 struct n_tty_data *ldata = tty->disc_data;
1099 if (L_NOFLSH(tty)) {
1103 } else { /* signal and flush */
1104 up_read(&tty->termios_rwsem);
1105 down_write(&tty->termios_rwsem);
1109 /* clear echo buffer */
1110 mutex_lock(&ldata->output_lock);
1111 ldata->echo_head = ldata->echo_tail = 0;
1112 ldata->echo_mark = ldata->echo_commit = 0;
1113 mutex_unlock(&ldata->output_lock);
1115 /* clear output buffer */
1116 tty_driver_flush_buffer(tty);
1118 /* clear input buffer */
1119 reset_buffer_flags(tty->disc_data);
1121 /* notify pty master of flush */
1123 n_tty_packet_mode_flush(tty);
1125 up_write(&tty->termios_rwsem);
1126 down_read(&tty->termios_rwsem);
1131 * n_tty_receive_break - handle break
1134 * An RS232 break event has been hit in the incoming bitstream. This can cause
1135 * a variety of events depending upon the termios settings.
1137 * Locking: n_tty_receive_buf()/producer path:
1138 * caller holds non-exclusive termios_rwsem
1140 * Note: may get exclusive %termios_rwsem if flushing input buffer
1142 static void n_tty_receive_break(struct tty_struct *tty)
1144 struct n_tty_data *ldata = tty->disc_data;
1148 if (I_BRKINT(tty)) {
1152 if (I_PARMRK(tty)) {
1153 put_tty_queue('\377', ldata);
1154 put_tty_queue('\0', ldata);
1156 put_tty_queue('\0', ldata);
1160 * n_tty_receive_overrun - handle overrun reporting
1163 * Data arrived faster than we could process it. While the tty driver has
1164 * flagged this the bits that were missed are gone forever.
1166 * Called from the receive_buf path so single threaded. Does not need locking
1167 * as num_overrun and overrun_time are function private.
1169 static void n_tty_receive_overrun(struct tty_struct *tty)
1171 struct n_tty_data *ldata = tty->disc_data;
1173 ldata->num_overrun++;
1174 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1175 time_after(ldata->overrun_time, jiffies)) {
1176 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
1177 ldata->overrun_time = jiffies;
1178 ldata->num_overrun = 0;
1183 * n_tty_receive_parity_error - error notifier
1184 * @tty: terminal device
1187 * Process a parity error and queue the right data to indicate the error case
1190 * Locking: n_tty_receive_buf()/producer path:
1191 * caller holds non-exclusive %termios_rwsem
1193 static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
1195 struct n_tty_data *ldata = tty->disc_data;
1200 if (I_PARMRK(tty)) {
1201 put_tty_queue('\377', ldata);
1202 put_tty_queue('\0', ldata);
1203 put_tty_queue(c, ldata);
1205 put_tty_queue('\0', ldata);
1207 put_tty_queue(c, ldata);
1211 n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1220 process_echoes(tty);
1223 static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
1225 struct n_tty_data *ldata = tty->disc_data;
1228 if (c == START_CHAR(tty)) {
1230 process_echoes(tty);
1233 if (c == STOP_CHAR(tty)) {
1240 if (c == INTR_CHAR(tty)) {
1241 n_tty_receive_signal_char(tty, SIGINT, c);
1243 } else if (c == QUIT_CHAR(tty)) {
1244 n_tty_receive_signal_char(tty, SIGQUIT, c);
1246 } else if (c == SUSP_CHAR(tty)) {
1247 n_tty_receive_signal_char(tty, SIGTSTP, c);
1252 if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
1254 process_echoes(tty);
1262 } else if (c == '\n' && I_INLCR(tty))
1265 if (ldata->icanon) {
1266 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1267 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1272 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1275 finish_erasing(ldata);
1276 if (L_ECHOCTL(tty)) {
1277 echo_char_raw('^', ldata);
1278 echo_char_raw('\b', ldata);
1284 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
1285 size_t tail = ldata->canon_head;
1287 finish_erasing(ldata);
1289 echo_char_raw('\n', ldata);
1290 while (MASK(tail) != MASK(ldata->read_head)) {
1291 echo_char(read_buf(ldata, tail), tty);
1298 if (L_ECHO(tty) || L_ECHONL(tty)) {
1299 echo_char_raw('\n', ldata);
1302 goto handle_newline;
1304 if (c == EOF_CHAR(tty)) {
1305 c = __DISABLED_CHAR;
1306 goto handle_newline;
1308 if ((c == EOL_CHAR(tty)) ||
1309 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1311 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1314 /* Record the column of first canon char. */
1315 if (ldata->canon_head == ldata->read_head)
1316 echo_set_canon_col(ldata);
1321 * XXX does PARMRK doubling happen for
1322 * EOL_CHAR and EOL2_CHAR?
1324 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1325 put_tty_queue(c, ldata);
1328 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
1329 put_tty_queue(c, ldata);
1330 smp_store_release(&ldata->canon_head, ldata->read_head);
1331 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1332 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
1338 finish_erasing(ldata);
1340 echo_char_raw('\n', ldata);
1342 /* Record the column of first canon char. */
1343 if (ldata->canon_head == ldata->read_head)
1344 echo_set_canon_col(ldata);
1350 /* PARMRK doubling check */
1351 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1352 put_tty_queue(c, ldata);
1354 put_tty_queue(c, ldata);
1358 * n_tty_receive_char - perform processing
1359 * @tty: terminal device
1362 * Process an individual character of input received from the driver. This is
1363 * serialized with respect to itself by the rules for the driver above.
1365 * Locking: n_tty_receive_buf()/producer path:
1366 * caller holds non-exclusive %termios_rwsem
1367 * publishes canon_head if canonical mode is active
1369 static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1371 struct n_tty_data *ldata = tty->disc_data;
1373 if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
1375 process_echoes(tty);
1378 finish_erasing(ldata);
1379 /* Record the column of first canon char. */
1380 if (ldata->canon_head == ldata->read_head)
1381 echo_set_canon_col(ldata);
1385 /* PARMRK doubling check */
1386 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1387 put_tty_queue(c, ldata);
1388 put_tty_queue(c, ldata);
1391 static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
1395 if (I_IUCLC(tty) && L_IEXTEN(tty))
1399 if (c == STOP_CHAR(tty))
1401 else if (c == START_CHAR(tty) ||
1402 (tty->flow.stopped && !tty->flow.tco_stopped && I_IXANY(tty) &&
1403 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1404 c != SUSP_CHAR(tty))) {
1406 process_echoes(tty);
1412 n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1416 n_tty_receive_break(tty);
1420 n_tty_receive_parity_error(tty, c);
1423 n_tty_receive_overrun(tty);
1426 tty_err(tty, "unknown flag %d\n", flag);
1432 n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1434 struct n_tty_data *ldata = tty->disc_data;
1437 if (likely(flag == TTY_NORMAL)) {
1440 if (I_IUCLC(tty) && L_IEXTEN(tty))
1442 n_tty_receive_char(tty, c);
1444 n_tty_receive_char_flagged(tty, c, flag);
1448 n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1449 const char *fp, int count)
1451 struct n_tty_data *ldata = tty->disc_data;
1454 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1455 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
1456 memcpy(read_buf_addr(ldata, head), cp, n);
1457 ldata->read_head += n;
1461 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1462 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
1463 memcpy(read_buf_addr(ldata, head), cp, n);
1464 ldata->read_head += n;
1468 n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1469 const char *fp, int count)
1471 struct n_tty_data *ldata = tty->disc_data;
1472 char flag = TTY_NORMAL;
1477 if (likely(flag == TTY_NORMAL))
1478 put_tty_queue(*cp++, ldata);
1480 n_tty_receive_char_flagged(tty, *cp++, flag);
1485 n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1486 const char *fp, int count)
1488 char flag = TTY_NORMAL;
1493 if (likely(flag == TTY_NORMAL))
1494 n_tty_receive_char_closing(tty, *cp++);
1498 static void n_tty_receive_buf_standard(struct tty_struct *tty,
1499 const unsigned char *cp, const char *fp, int count)
1501 struct n_tty_data *ldata = tty->disc_data;
1502 char flag = TTY_NORMAL;
1505 unsigned char c = *cp++;
1511 n_tty_receive_char_lnext(tty, c, flag);
1515 if (unlikely(flag != TTY_NORMAL)) {
1516 n_tty_receive_char_flagged(tty, c, flag);
1522 if (I_IUCLC(tty) && L_IEXTEN(tty))
1524 if (L_EXTPROC(tty)) {
1525 put_tty_queue(c, ldata);
1529 if (test_bit(c, ldata->char_map))
1530 n_tty_receive_char_special(tty, c);
1532 n_tty_receive_char(tty, c);
1536 static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1537 const char *fp, int count)
1539 struct n_tty_data *ldata = tty->disc_data;
1540 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
1542 if (ldata->real_raw)
1543 n_tty_receive_buf_real_raw(tty, cp, fp, count);
1544 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
1545 n_tty_receive_buf_raw(tty, cp, fp, count);
1546 else if (tty->closing && !L_EXTPROC(tty))
1547 n_tty_receive_buf_closing(tty, cp, fp, count);
1549 n_tty_receive_buf_standard(tty, cp, fp, count);
1552 if (tty->ops->flush_chars)
1553 tty->ops->flush_chars(tty);
1556 if (ldata->icanon && !L_EXTPROC(tty))
1559 /* publish read_head to consumer */
1560 smp_store_release(&ldata->commit_head, ldata->read_head);
1562 if (read_cnt(ldata)) {
1563 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1564 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
1569 * n_tty_receive_buf_common - process input
1570 * @tty: device to receive input
1572 * @fp: flags for each char (if %NULL, all chars are %TTY_NORMAL)
1573 * @count: number of input chars in @cp
1574 * @flow: enable flow control
1576 * Called by the terminal driver when a block of characters has been received.
1577 * This function must be called from soft contexts not from interrupt context.
1578 * The driver is responsible for making calls one at a time and in order (or
1579 * using flush_to_ldisc()).
1581 * Returns: the # of input chars from @cp which were processed.
1583 * In canonical mode, the maximum line length is 4096 chars (including the line
1584 * termination char); lines longer than 4096 chars are truncated. After 4095
1585 * chars, input data is still processed but not stored. Overflow processing
1586 * ensures the tty can always receive more input until at least one line can be
1589 * In non-canonical mode, the read buffer will only accept 4095 chars; this
1590 * provides the necessary space for a newline char if the input mode is
1591 * switched to canonical.
1593 * Note it is possible for the read buffer to _contain_ 4096 chars in
1594 * non-canonical mode: the read buffer could already contain the maximum canon
1595 * line of 4096 chars when the mode is switched to non-canonical.
1597 * Locking: n_tty_receive_buf()/producer path:
1598 * claims non-exclusive %termios_rwsem
1599 * publishes commit_head or canon_head
1602 n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1603 const char *fp, int count, int flow)
1605 struct n_tty_data *ldata = tty->disc_data;
1606 int room, n, rcvd = 0, overflow;
1608 down_read(&tty->termios_rwsem);
1612 * When PARMRK is set, each input char may take up to 3 chars
1613 * in the read buf; reduce the buffer space avail by 3x
1615 * If we are doing input canonicalization, and there are no
1616 * pending newlines, let characters through without limit, so
1617 * that erase characters will be handled. Other excess
1618 * characters will be beeped.
1620 * paired with store in *_copy_from_read_buf() -- guarantees
1621 * the consumer has loaded the data in read_buf up to the new
1622 * read_tail (so this producer will not overwrite unread data)
1624 size_t tail = smp_load_acquire(&ldata->read_tail);
1626 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
1628 room = (room + 2) / 3;
1631 overflow = ldata->icanon && ldata->canon_head == tail;
1632 if (overflow && room < 0)
1635 ldata->no_room = flow && !room;
1639 n = min(count, room);
1643 /* ignore parity errors if handling overflow */
1644 if (!overflow || !fp || *fp != TTY_PARITY)
1645 __receive_buf(tty, cp, fp, n);
1652 } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags));
1654 tty->receive_room = room;
1656 /* Unthrottle if handling overflow on pty */
1657 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1659 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1660 tty_unthrottle_safe(tty);
1661 __tty_set_flow_change(tty, 0);
1664 n_tty_check_throttle(tty);
1666 up_read(&tty->termios_rwsem);
1671 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1672 const char *fp, int count)
1674 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1677 static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1678 const char *fp, int count)
1680 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1684 * n_tty_set_termios - termios data changed
1686 * @old: previous data
1688 * Called by the tty layer when the user changes termios flags so that the line
1689 * discipline can plan ahead. This function cannot sleep and is protected from
1690 * re-entry by the tty layer. The user is guaranteed that this function will
1691 * not be re-entered or in progress when the ldisc is closed.
1693 * Locking: Caller holds @tty->termios_rwsem
1695 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1697 struct n_tty_data *ldata = tty->disc_data;
1699 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
1700 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1701 ldata->line_start = ldata->read_tail;
1702 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1703 ldata->canon_head = ldata->read_tail;
1706 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1708 ldata->canon_head = ldata->read_head;
1711 ldata->commit_head = ldata->read_head;
1716 ldata->icanon = (L_ICANON(tty) != 0);
1718 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1719 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1720 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1722 bitmap_zero(ldata->char_map, 256);
1724 if (I_IGNCR(tty) || I_ICRNL(tty))
1725 set_bit('\r', ldata->char_map);
1727 set_bit('\n', ldata->char_map);
1729 if (L_ICANON(tty)) {
1730 set_bit(ERASE_CHAR(tty), ldata->char_map);
1731 set_bit(KILL_CHAR(tty), ldata->char_map);
1732 set_bit(EOF_CHAR(tty), ldata->char_map);
1733 set_bit('\n', ldata->char_map);
1734 set_bit(EOL_CHAR(tty), ldata->char_map);
1735 if (L_IEXTEN(tty)) {
1736 set_bit(WERASE_CHAR(tty), ldata->char_map);
1737 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1738 set_bit(EOL2_CHAR(tty), ldata->char_map);
1740 set_bit(REPRINT_CHAR(tty),
1745 set_bit(START_CHAR(tty), ldata->char_map);
1746 set_bit(STOP_CHAR(tty), ldata->char_map);
1749 set_bit(INTR_CHAR(tty), ldata->char_map);
1750 set_bit(QUIT_CHAR(tty), ldata->char_map);
1751 set_bit(SUSP_CHAR(tty), ldata->char_map);
1753 clear_bit(__DISABLED_CHAR, ldata->char_map);
1755 ldata->real_raw = 0;
1758 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1759 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1760 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1761 ldata->real_raw = 1;
1763 ldata->real_raw = 0;
1766 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1767 * been stopped by STOP_CHAR(tty) before it.
1769 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) {
1771 process_echoes(tty);
1774 /* The termios change make the tty ready for I/O */
1775 wake_up_interruptible(&tty->write_wait);
1776 wake_up_interruptible(&tty->read_wait);
1780 * n_tty_close - close the ldisc for this tty
1783 * Called from the terminal layer when this line discipline is being shut down,
1784 * either because of a close or becsuse of a discipline change. The function
1785 * will not be called while other ldisc methods are in progress.
1787 static void n_tty_close(struct tty_struct *tty)
1789 struct n_tty_data *ldata = tty->disc_data;
1792 n_tty_packet_mode_flush(tty);
1794 down_write(&tty->termios_rwsem);
1796 tty->disc_data = NULL;
1797 up_write(&tty->termios_rwsem);
1801 * n_tty_open - open an ldisc
1802 * @tty: terminal to open
1804 * Called when this line discipline is being attached to the terminal device.
1805 * Can sleep. Called serialized so that no other events will occur in parallel.
1806 * No further open will occur until a close.
1808 static int n_tty_open(struct tty_struct *tty)
1810 struct n_tty_data *ldata;
1812 /* Currently a malloc failure here can panic */
1813 ldata = vzalloc(sizeof(*ldata));
1817 ldata->overrun_time = jiffies;
1818 mutex_init(&ldata->atomic_read_lock);
1819 mutex_init(&ldata->output_lock);
1821 tty->disc_data = ldata;
1823 /* indicate buffer work may resume */
1824 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1825 n_tty_set_termios(tty, NULL);
1826 tty_unthrottle(tty);
1830 static inline int input_available_p(struct tty_struct *tty, int poll)
1832 struct n_tty_data *ldata = tty->disc_data;
1833 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
1835 if (ldata->icanon && !L_EXTPROC(tty))
1836 return ldata->canon_head != ldata->read_tail;
1838 return ldata->commit_head - ldata->read_tail >= amt;
1842 * copy_from_read_buf - copy read data directly
1843 * @tty: terminal device
1847 * Helper function to speed up n_tty_read(). It is only called when %ICANON is
1848 * off; it copies characters straight from the tty queue.
1850 * Returns: true if it successfully copied data, but there is still more data
1854 * * called under the @ldata->atomic_read_lock sem
1855 * * n_tty_read()/consumer path:
1856 * caller holds non-exclusive %termios_rwsem;
1857 * read_tail published
1859 static bool copy_from_read_buf(struct tty_struct *tty,
1860 unsigned char **kbp,
1864 struct n_tty_data *ldata = tty->disc_data;
1867 size_t head = smp_load_acquire(&ldata->commit_head);
1868 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1870 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
1873 unsigned char *from = read_buf_addr(ldata, tail);
1874 memcpy(*kbp, from, n);
1875 is_eof = n == 1 && *from == EOF_CHAR(tty);
1876 tty_audit_add_data(tty, from, n);
1877 zero_buffer(tty, from, n);
1878 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
1879 /* Turn single EOF into zero-length read */
1880 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
1881 (head == ldata->read_tail))
1886 /* If we have more to copy, let the caller know */
1887 return head != ldata->read_tail;
1893 * canon_copy_from_read_buf - copy read data in canonical mode
1894 * @tty: terminal device
1898 * Helper function for n_tty_read(). It is only called when %ICANON is on; it
1899 * copies one line of input up to and including the line-delimiting character
1900 * into the result buffer.
1902 * Note: When termios is changed from non-canonical to canonical mode and the
1903 * read buffer contains data, n_tty_set_termios() simulates an EOF push (as if
1904 * C-d were input) _without_ the %DISABLED_CHAR in the buffer. This causes data
1905 * already processed as input to be immediately available as input although a
1906 * newline has not been received.
1909 * * called under the %atomic_read_lock mutex
1910 * * n_tty_read()/consumer path:
1911 * caller holds non-exclusive %termios_rwsem;
1912 * read_tail published
1914 static bool canon_copy_from_read_buf(struct tty_struct *tty,
1915 unsigned char **kbp,
1918 struct n_tty_data *ldata = tty->disc_data;
1919 size_t n, size, more, c;
1921 size_t tail, canon_head;
1924 /* N.B. avoid overrun if nr == 0 */
1928 canon_head = smp_load_acquire(&ldata->canon_head);
1929 n = min(*nr + 1, canon_head - ldata->read_tail);
1931 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1932 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
1934 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
1935 __func__, *nr, tail, n, size);
1937 eol = find_next_bit(ldata->read_flags, size, tail);
1938 more = n - (size - tail);
1939 if (eol == N_TTY_BUF_SIZE && more) {
1940 /* scan wrapped without finding set bit */
1941 eol = find_first_bit(ldata->read_flags, more);
1942 found = eol != more;
1944 found = eol != size;
1947 if (n > N_TTY_BUF_SIZE)
1948 n += N_TTY_BUF_SIZE;
1951 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
1956 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
1957 __func__, eol, found, n, c, tail, more);
1959 tty_copy(tty, *kbp, tail, n);
1964 clear_bit(eol, ldata->read_flags);
1965 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
1969 ldata->line_start = ldata->read_tail;
1976 /* No EOL found - do a continuation retry if there is more data */
1977 return ldata->read_tail != canon_head;
1981 * job_control - check job control
1983 * @file: file handle
1985 * Perform job control management checks on this @file/@tty descriptor and if
1986 * appropriate send any needed signals and return a negative error code if
1987 * action should be taken.
1990 * * redirected write test is safe
1991 * * current->signal->tty check is safe
1992 * * ctrl.lock to safely reference @tty->ctrl.pgrp
1994 static int job_control(struct tty_struct *tty, struct file *file)
1996 /* Job control check -- must be done at start and after
1997 every sleep (POSIX.1 7.1.1.4). */
1998 /* NOTE: not yet done after every sleep pending a thorough
1999 check of the logic of this change. -- jlc */
2000 /* don't stop on /dev/console */
2001 if (file->f_op->write_iter == redirected_tty_write)
2004 return __tty_check_change(tty, SIGTTIN);
2009 * n_tty_read - read function for tty
2011 * @file: file object
2012 * @kbuf: kernelspace buffer pointer
2014 * @cookie: if non-%NULL, this is a continuation read
2015 * @offset: where to continue reading from (unused in n_tty)
2017 * Perform reads for the line discipline. We are guaranteed that the line
2018 * discipline will not be closed under us but we may get multiple parallel
2019 * readers and must handle this ourselves. We may also get a hangup. Always
2020 * called in user context, may sleep.
2022 * This code must be sure never to sleep through a hangup.
2024 * Locking: n_tty_read()/consumer path:
2025 * claims non-exclusive termios_rwsem;
2026 * publishes read_tail
2028 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
2029 unsigned char *kbuf, size_t nr,
2030 void **cookie, unsigned long offset)
2032 struct n_tty_data *ldata = tty->disc_data;
2033 unsigned char *kb = kbuf;
2034 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2043 * Is this a continuation of a read started earler?
2045 * If so, we still hold the atomic_read_lock and the
2046 * termios_rwsem, and can just continue to copy data.
2049 if (ldata->icanon && !L_EXTPROC(tty)) {
2050 if (canon_copy_from_read_buf(tty, &kb, &nr))
2053 if (copy_from_read_buf(tty, &kb, &nr))
2057 /* No more data - release locks and stop retries */
2058 n_tty_kick_worker(tty);
2059 n_tty_check_unthrottle(tty);
2060 up_read(&tty->termios_rwsem);
2061 mutex_unlock(&ldata->atomic_read_lock);
2066 c = job_control(tty, file);
2071 * Internal serialization of reads.
2073 if (file->f_flags & O_NONBLOCK) {
2074 if (!mutex_trylock(&ldata->atomic_read_lock))
2077 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2078 return -ERESTARTSYS;
2081 down_read(&tty->termios_rwsem);
2084 timeout = MAX_SCHEDULE_TIMEOUT;
2085 if (!ldata->icanon) {
2086 minimum = MIN_CHAR(tty);
2088 time = (HZ / 10) * TIME_CHAR(tty);
2090 timeout = (HZ / 10) * TIME_CHAR(tty);
2095 packet = tty->ctrl.packet;
2096 tail = ldata->read_tail;
2098 add_wait_queue(&tty->read_wait, &wait);
2100 /* First test for status change. */
2101 if (packet && tty->link->ctrl.pktstatus) {
2105 spin_lock_irq(&tty->link->ctrl.lock);
2106 cs = tty->link->ctrl.pktstatus;
2107 tty->link->ctrl.pktstatus = 0;
2108 spin_unlock_irq(&tty->link->ctrl.lock);
2114 if (!input_available_p(tty, 0)) {
2115 up_read(&tty->termios_rwsem);
2116 tty_buffer_flush_work(tty->port);
2117 down_read(&tty->termios_rwsem);
2118 if (!input_available_p(tty, 0)) {
2119 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2123 if (tty_hung_up_p(file))
2126 * Abort readers for ttys which never actually
2127 * get hung up. See __tty_hangup().
2129 if (test_bit(TTY_HUPPING, &tty->flags))
2133 if (tty_io_nonblock(tty, file)) {
2137 if (signal_pending(current)) {
2138 retval = -ERESTARTSYS;
2141 up_read(&tty->termios_rwsem);
2143 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2146 down_read(&tty->termios_rwsem);
2151 if (ldata->icanon && !L_EXTPROC(tty)) {
2152 if (canon_copy_from_read_buf(tty, &kb, &nr))
2153 goto more_to_be_read;
2155 /* Deal with packet mode. */
2156 if (packet && kb == kbuf) {
2157 *kb++ = TIOCPKT_DATA;
2162 * Copy data, and if there is more to be had
2163 * and we have nothing more to wait for, then
2164 * let's mark us for retries.
2166 * NOTE! We return here with both the termios_sem
2167 * and atomic_read_lock still held, the retries
2168 * will release them when done.
2170 if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum) {
2172 remove_wait_queue(&tty->read_wait, &wait);
2178 n_tty_check_unthrottle(tty);
2180 if (kb - kbuf >= minimum)
2185 if (tail != ldata->read_tail)
2186 n_tty_kick_worker(tty);
2187 up_read(&tty->termios_rwsem);
2189 remove_wait_queue(&tty->read_wait, &wait);
2190 mutex_unlock(&ldata->atomic_read_lock);
2199 * n_tty_write - write function for tty
2201 * @file: file object
2202 * @buf: userspace buffer pointer
2205 * Write function of the terminal device. This is serialized with respect to
2206 * other write callers but not to termios changes, reads and other such events.
2207 * Since the receive code will echo characters, thus calling driver write
2208 * methods, the %output_lock is used in the output processing functions called
2209 * here as well as in the echo processing function to protect the column state
2210 * and space left in the buffer.
2212 * This code must be sure never to sleep through a hangup.
2214 * Locking: output_lock to protect column state and space left
2215 * (note that the process_output*() functions take this lock themselves)
2218 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2219 const unsigned char *buf, size_t nr)
2221 const unsigned char *b = buf;
2222 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2226 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2227 if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) {
2228 retval = tty_check_change(tty);
2233 down_read(&tty->termios_rwsem);
2235 /* Write out any echoed characters that are still pending */
2236 process_echoes(tty);
2238 add_wait_queue(&tty->write_wait, &wait);
2240 if (signal_pending(current)) {
2241 retval = -ERESTARTSYS;
2244 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2250 ssize_t num = process_output_block(tty, b, nr);
2262 if (process_output(c, tty) < 0)
2266 if (tty->ops->flush_chars)
2267 tty->ops->flush_chars(tty);
2269 struct n_tty_data *ldata = tty->disc_data;
2272 mutex_lock(&ldata->output_lock);
2273 c = tty->ops->write(tty, b, nr);
2274 mutex_unlock(&ldata->output_lock);
2287 if (tty_io_nonblock(tty, file)) {
2291 up_read(&tty->termios_rwsem);
2293 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2295 down_read(&tty->termios_rwsem);
2298 remove_wait_queue(&tty->write_wait, &wait);
2299 if (nr && tty->fasync)
2300 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2301 up_read(&tty->termios_rwsem);
2302 return (b - buf) ? b - buf : retval;
2306 * n_tty_poll - poll method for N_TTY
2307 * @tty: terminal device
2308 * @file: file accessing it
2311 * Called when the line discipline is asked to poll() for data or for special
2312 * events. This code is not serialized with respect to other events save
2315 * This code must be sure never to sleep through a hangup.
2317 * Locking: called without the kernel lock held -- fine.
2319 static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
2324 poll_wait(file, &tty->read_wait, wait);
2325 poll_wait(file, &tty->write_wait, wait);
2326 if (input_available_p(tty, 1))
2327 mask |= EPOLLIN | EPOLLRDNORM;
2329 tty_buffer_flush_work(tty->port);
2330 if (input_available_p(tty, 1))
2331 mask |= EPOLLIN | EPOLLRDNORM;
2333 if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
2334 mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
2335 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2337 if (tty_hung_up_p(file))
2339 if (tty->ops->write && !tty_is_writelocked(tty) &&
2340 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2341 tty_write_room(tty) > 0)
2342 mask |= EPOLLOUT | EPOLLWRNORM;
2346 static unsigned long inq_canon(struct n_tty_data *ldata)
2348 size_t nr, head, tail;
2350 if (ldata->canon_head == ldata->read_tail)
2352 head = ldata->canon_head;
2353 tail = ldata->read_tail;
2355 /* Skip EOF-chars.. */
2356 while (MASK(head) != MASK(tail)) {
2357 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2358 read_buf(ldata, tail) == __DISABLED_CHAR)
2365 static int n_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2368 struct n_tty_data *ldata = tty->disc_data;
2373 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2375 down_write(&tty->termios_rwsem);
2376 if (L_ICANON(tty) && !L_EXTPROC(tty))
2377 retval = inq_canon(ldata);
2379 retval = read_cnt(ldata);
2380 up_write(&tty->termios_rwsem);
2381 return put_user(retval, (unsigned int __user *) arg);
2383 return n_tty_ioctl_helper(tty, cmd, arg);
2387 static struct tty_ldisc_ops n_tty_ops = {
2388 .owner = THIS_MODULE,
2392 .close = n_tty_close,
2393 .flush_buffer = n_tty_flush_buffer,
2395 .write = n_tty_write,
2396 .ioctl = n_tty_ioctl,
2397 .set_termios = n_tty_set_termios,
2399 .receive_buf = n_tty_receive_buf,
2400 .write_wakeup = n_tty_write_wakeup,
2401 .receive_buf2 = n_tty_receive_buf2,
2405 * n_tty_inherit_ops - inherit N_TTY methods
2406 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2408 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
2411 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2416 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
2418 void __init n_tty_init(void)
2420 tty_register_ldisc(&n_tty_ops);