1 // SPDX-License-Identifier: GPL-1.0+ 2 /* 3 * n_tty.c --- implements the N_TTY line discipline. 4 * 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, 8 * anyway...) 9 * 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. 13 * 14 * Written by Theodore Ts'o, Copyright 1994. 15 * 16 * This file also contains code originally written by Linus Torvalds, 17 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994. 18 * 19 * Reduced memory usage for older ARM systems - Russell King. 20 * 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. 24 * 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 28 * EAGAIN 29 */ 30 31 #include <linux/bitmap.h> 32 #include <linux/bitops.h> 33 #include <linux/ctype.h> 34 #include <linux/errno.h> 35 #include <linux/export.h> 36 #include <linux/fcntl.h> 37 #include <linux/file.h> 38 #include <linux/jiffies.h> 39 #include <linux/math.h> 40 #include <linux/poll.h> 41 #include <linux/ratelimit.h> 42 #include <linux/sched.h> 43 #include <linux/signal.h> 44 #include <linux/slab.h> 45 #include <linux/string.h> 46 #include <linux/tty.h> 47 #include <linux/types.h> 48 #include <linux/uaccess.h> 49 #include <linux/vmalloc.h> 50 51 #include "tty.h" 52 53 /* 54 * Until this number of characters is queued in the xmit buffer, select will 55 * return "we have room for writes". 56 */ 57 #define WAKEUP_CHARS 256 58 59 /* 60 * This defines the low- and high-watermarks for throttling and 61 * unthrottling the TTY driver. These watermarks are used for 62 * controlling the space in the read buffer. 63 */ 64 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */ 65 #define TTY_THRESHOLD_UNTHROTTLE 128 66 67 /* 68 * Special byte codes used in the echo buffer to represent operations 69 * or special handling of characters. Bytes in the echo buffer that 70 * are not part of such special blocks are treated as normal character 71 * codes. 72 */ 73 #define ECHO_OP_START 0xff 74 #define ECHO_OP_MOVE_BACK_COL 0x80 75 #define ECHO_OP_SET_CANON_COL 0x81 76 #define ECHO_OP_ERASE_TAB 0x82 77 78 #define ECHO_COMMIT_WATERMARK 256 79 #define ECHO_BLOCK 256 80 #define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32) 81 82 83 #undef N_TTY_TRACE 84 #ifdef N_TTY_TRACE 85 # define n_tty_trace(f, args...) trace_printk(f, ##args) 86 #else 87 # define n_tty_trace(f, args...) no_printk(f, ##args) 88 #endif 89 90 struct n_tty_data { 91 /* producer-published */ 92 size_t read_head; 93 size_t commit_head; 94 size_t canon_head; 95 size_t echo_head; 96 size_t echo_commit; 97 size_t echo_mark; 98 DECLARE_BITMAP(char_map, 256); 99 100 /* private to n_tty_receive_overrun (single-threaded) */ 101 unsigned long overrun_time; 102 int num_overrun; 103 104 /* non-atomic */ 105 bool no_room; 106 107 /* must hold exclusive termios_rwsem to reset these */ 108 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1; 109 unsigned char push:1; 110 111 /* shared by producer and consumer */ 112 char read_buf[N_TTY_BUF_SIZE]; 113 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE); 114 unsigned char echo_buf[N_TTY_BUF_SIZE]; 115 116 /* consumer-published */ 117 size_t read_tail; 118 size_t line_start; 119 120 /* # of chars looked ahead (to find software flow control chars) */ 121 size_t lookahead_count; 122 123 /* protected by output lock */ 124 unsigned int column; 125 unsigned int canon_column; 126 size_t echo_tail; 127 128 struct mutex atomic_read_lock; 129 struct mutex output_lock; 130 }; 131 132 #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1)) 133 134 static inline size_t read_cnt(struct n_tty_data *ldata) 135 { 136 return ldata->read_head - ldata->read_tail; 137 } 138 139 static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i) 140 { 141 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)]; 142 } 143 144 static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i) 145 { 146 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)]; 147 } 148 149 static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i) 150 { 151 smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */ 152 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)]; 153 } 154 155 static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i) 156 { 157 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)]; 158 } 159 160 /* If we are not echoing the data, perhaps this is a secret so erase it */ 161 static void zero_buffer(const struct tty_struct *tty, u8 *buffer, size_t size) 162 { 163 if (L_ICANON(tty) && !L_ECHO(tty)) 164 memset(buffer, 0, size); 165 } 166 167 static void tty_copy(const struct tty_struct *tty, void *to, size_t tail, 168 size_t n) 169 { 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); 173 174 if (n > size) { 175 tty_audit_add_data(tty, from, size); 176 memcpy(to, from, size); 177 zero_buffer(tty, from, size); 178 to += size; 179 n -= size; 180 from = ldata->read_buf; 181 } 182 183 tty_audit_add_data(tty, from, n); 184 memcpy(to, from, n); 185 zero_buffer(tty, from, n); 186 } 187 188 /** 189 * n_tty_kick_worker - start input worker (if required) 190 * @tty: terminal 191 * 192 * Re-schedules the flip buffer work if it may have stopped. 193 * 194 * Locking: 195 * * Caller holds exclusive %termios_rwsem, or 196 * * n_tty_read()/consumer path: 197 * holds non-exclusive %termios_rwsem 198 */ 199 static void n_tty_kick_worker(const struct tty_struct *tty) 200 { 201 struct n_tty_data *ldata = tty->disc_data; 202 203 /* Did the input worker stop? Restart it */ 204 if (unlikely(READ_ONCE(ldata->no_room))) { 205 WRITE_ONCE(ldata->no_room, 0); 206 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 212 */ 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); 216 } 217 } 218 219 static ssize_t chars_in_buffer(const struct tty_struct *tty) 220 { 221 const struct n_tty_data *ldata = tty->disc_data; 222 ssize_t n = 0; 223 224 if (!ldata->icanon) 225 n = ldata->commit_head - ldata->read_tail; 226 else 227 n = ldata->canon_head - ldata->read_tail; 228 return n; 229 } 230 231 /** 232 * n_tty_write_wakeup - asynchronous I/O notifier 233 * @tty: tty device 234 * 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. 237 */ 238 static void n_tty_write_wakeup(struct tty_struct *tty) 239 { 240 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 241 kill_fasync(&tty->fasync, SIGIO, POLL_OUT); 242 } 243 244 static void n_tty_check_throttle(struct tty_struct *tty) 245 { 246 struct n_tty_data *ldata = tty->disc_data; 247 248 /* 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! 252 */ 253 if (ldata->icanon && ldata->canon_head == ldata->read_tail) 254 return; 255 256 while (1) { 257 int throttled; 258 tty_set_flow_change(tty, TTY_THROTTLE_SAFE); 259 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE) 260 break; 261 throttled = tty_throttle_safe(tty); 262 if (!throttled) 263 break; 264 } 265 __tty_set_flow_change(tty, 0); 266 } 267 268 static void n_tty_check_unthrottle(struct tty_struct *tty) 269 { 270 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) { 271 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE) 272 return; 273 n_tty_kick_worker(tty); 274 tty_wakeup(tty->link); 275 return; 276 } 277 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. 284 */ 285 286 while (1) { 287 int unthrottled; 288 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE); 289 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE) 290 break; 291 n_tty_kick_worker(tty); 292 unthrottled = tty_unthrottle_safe(tty); 293 if (!unthrottled) 294 break; 295 } 296 __tty_set_flow_change(tty, 0); 297 } 298 299 /** 300 * put_tty_queue - add character to tty 301 * @c: character 302 * @ldata: n_tty data 303 * 304 * Add a character to the tty read_buf queue. 305 * 306 * Locking: 307 * * n_tty_receive_buf()/producer path: 308 * caller holds non-exclusive %termios_rwsem 309 */ 310 static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata) 311 { 312 *read_buf_addr(ldata, ldata->read_head) = c; 313 ldata->read_head++; 314 } 315 316 /** 317 * reset_buffer_flags - reset buffer state 318 * @ldata: line disc data to reset 319 * 320 * Reset the read buffer counters and clear the flags. Called from 321 * n_tty_open() and n_tty_flush_buffer(). 322 * 323 * Locking: 324 * * caller holds exclusive %termios_rwsem, or 325 * * (locking is not required) 326 */ 327 static void reset_buffer_flags(struct n_tty_data *ldata) 328 { 329 ldata->read_head = ldata->canon_head = ldata->read_tail = 0; 330 ldata->commit_head = 0; 331 ldata->line_start = 0; 332 333 ldata->erasing = 0; 334 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 335 ldata->push = 0; 336 337 ldata->lookahead_count = 0; 338 } 339 340 static void n_tty_packet_mode_flush(struct tty_struct *tty) 341 { 342 unsigned long flags; 343 344 if (tty->link->ctrl.packet) { 345 spin_lock_irqsave(&tty->ctrl.lock, flags); 346 tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD; 347 spin_unlock_irqrestore(&tty->ctrl.lock, flags); 348 wake_up_interruptible(&tty->link->read_wait); 349 } 350 } 351 352 /** 353 * n_tty_flush_buffer - clean input queue 354 * @tty: terminal device 355 * 356 * Flush the input buffer. Called when the tty layer wants the buffer flushed 357 * (eg at hangup) or when the %N_TTY line discipline internally has to clean 358 * the pending queue (for example some signals). 359 * 360 * Holds %termios_rwsem to exclude producer/consumer while buffer indices are 361 * reset. 362 * 363 * Locking: %ctrl.lock, exclusive %termios_rwsem 364 */ 365 static void n_tty_flush_buffer(struct tty_struct *tty) 366 { 367 down_write(&tty->termios_rwsem); 368 reset_buffer_flags(tty->disc_data); 369 n_tty_kick_worker(tty); 370 371 if (tty->link) 372 n_tty_packet_mode_flush(tty); 373 up_write(&tty->termios_rwsem); 374 } 375 376 /** 377 * is_utf8_continuation - utf8 multibyte check 378 * @c: byte to check 379 * 380 * Returns: true if the utf8 character @c is a multibyte continuation 381 * character. We use this to correctly compute the on-screen size of the 382 * character when printing. 383 */ 384 static inline int is_utf8_continuation(unsigned char c) 385 { 386 return (c & 0xc0) == 0x80; 387 } 388 389 /** 390 * is_continuation - multibyte check 391 * @c: byte to check 392 * @tty: terminal device 393 * 394 * Returns: true if the utf8 character @c is a multibyte continuation character 395 * and the terminal is in unicode mode. 396 */ 397 static inline int is_continuation(unsigned char c, const struct tty_struct *tty) 398 { 399 return I_IUTF8(tty) && is_utf8_continuation(c); 400 } 401 402 /** 403 * do_output_char - output one character 404 * @c: character (or partial unicode symbol) 405 * @tty: terminal device 406 * @space: space available in tty driver write buffer 407 * 408 * This is a helper function that handles one output character (including 409 * special characters like TAB, CR, LF, etc.), doing OPOST processing and 410 * putting the results in the tty driver's write buffer. 411 * 412 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. 413 * They simply aren't relevant in the world today. If you ever need them, add 414 * them here. 415 * 416 * Returns: the number of bytes of buffer space used or -1 if no space left. 417 * 418 * Locking: should be called under the %output_lock to protect the column state 419 * and space left in the buffer. 420 */ 421 static int do_output_char(unsigned char c, struct tty_struct *tty, int space) 422 { 423 struct n_tty_data *ldata = tty->disc_data; 424 int spaces; 425 426 if (!space) 427 return -1; 428 429 switch (c) { 430 case '\n': 431 if (O_ONLRET(tty)) 432 ldata->column = 0; 433 if (O_ONLCR(tty)) { 434 if (space < 2) 435 return -1; 436 ldata->canon_column = ldata->column = 0; 437 tty->ops->write(tty, "\r\n", 2); 438 return 2; 439 } 440 ldata->canon_column = ldata->column; 441 break; 442 case '\r': 443 if (O_ONOCR(tty) && ldata->column == 0) 444 return 0; 445 if (O_OCRNL(tty)) { 446 c = '\n'; 447 if (O_ONLRET(tty)) 448 ldata->canon_column = ldata->column = 0; 449 break; 450 } 451 ldata->canon_column = ldata->column = 0; 452 break; 453 case '\t': 454 spaces = 8 - (ldata->column & 7); 455 if (O_TABDLY(tty) == XTABS) { 456 if (space < spaces) 457 return -1; 458 ldata->column += spaces; 459 tty->ops->write(tty, " ", spaces); 460 return spaces; 461 } 462 ldata->column += spaces; 463 break; 464 case '\b': 465 if (ldata->column > 0) 466 ldata->column--; 467 break; 468 default: 469 if (!iscntrl(c)) { 470 if (O_OLCUC(tty)) 471 c = toupper(c); 472 if (!is_continuation(c, tty)) 473 ldata->column++; 474 } 475 break; 476 } 477 478 tty_put_char(tty, c); 479 return 1; 480 } 481 482 /** 483 * process_output - output post processor 484 * @c: character (or partial unicode symbol) 485 * @tty: terminal device 486 * 487 * Output one character with OPOST processing. 488 * 489 * Returns: -1 when the output device is full and the character must be 490 * retried. 491 * 492 * Locking: %output_lock to protect column state and space left (also, this is 493 *called from n_tty_write() under the tty layer write lock). 494 */ 495 static int process_output(unsigned char c, struct tty_struct *tty) 496 { 497 struct n_tty_data *ldata = tty->disc_data; 498 int space, retval; 499 500 mutex_lock(&ldata->output_lock); 501 502 space = tty_write_room(tty); 503 retval = do_output_char(c, tty, space); 504 505 mutex_unlock(&ldata->output_lock); 506 if (retval < 0) 507 return -1; 508 else 509 return 0; 510 } 511 512 /** 513 * process_output_block - block post processor 514 * @tty: terminal device 515 * @buf: character buffer 516 * @nr: number of bytes to output 517 * 518 * Output a block of characters with OPOST processing. 519 * 520 * This path is used to speed up block console writes, among other things when 521 * processing blocks of output data. It handles only the simple cases normally 522 * found and helps to generate blocks of symbols for the console driver and 523 * thus improve performance. 524 * 525 * Returns: the number of characters output. 526 * 527 * Locking: %output_lock to protect column state and space left (also, this is 528 * called from n_tty_write() under the tty layer write lock). 529 */ 530 static ssize_t process_output_block(struct tty_struct *tty, 531 const unsigned char *buf, unsigned int nr) 532 { 533 struct n_tty_data *ldata = tty->disc_data; 534 int space; 535 int i; 536 const unsigned char *cp; 537 538 mutex_lock(&ldata->output_lock); 539 540 space = tty_write_room(tty); 541 if (space <= 0) { 542 mutex_unlock(&ldata->output_lock); 543 return space; 544 } 545 if (nr > space) 546 nr = space; 547 548 for (i = 0, cp = buf; i < nr; i++, cp++) { 549 unsigned char c = *cp; 550 551 switch (c) { 552 case '\n': 553 if (O_ONLRET(tty)) 554 ldata->column = 0; 555 if (O_ONLCR(tty)) 556 goto break_out; 557 ldata->canon_column = ldata->column; 558 break; 559 case '\r': 560 if (O_ONOCR(tty) && ldata->column == 0) 561 goto break_out; 562 if (O_OCRNL(tty)) 563 goto break_out; 564 ldata->canon_column = ldata->column = 0; 565 break; 566 case '\t': 567 goto break_out; 568 case '\b': 569 if (ldata->column > 0) 570 ldata->column--; 571 break; 572 default: 573 if (!iscntrl(c)) { 574 if (O_OLCUC(tty)) 575 goto break_out; 576 if (!is_continuation(c, tty)) 577 ldata->column++; 578 } 579 break; 580 } 581 } 582 break_out: 583 i = tty->ops->write(tty, buf, i); 584 585 mutex_unlock(&ldata->output_lock); 586 return i; 587 } 588 589 /** 590 * __process_echoes - write pending echo characters 591 * @tty: terminal device 592 * 593 * Write previously buffered echo (and other ldisc-generated) characters to the 594 * tty. 595 * 596 * Characters generated by the ldisc (including echoes) need to be buffered 597 * because the driver's write buffer can fill during heavy program output. 598 * Echoing straight to the driver will often fail under these conditions, 599 * causing lost characters and resulting mismatches of ldisc state information. 600 * 601 * Since the ldisc state must represent the characters actually sent to the 602 * driver at the time of the write, operations like certain changes in column 603 * state are also saved in the buffer and executed here. 604 * 605 * A circular fifo buffer is used so that the most recent characters are 606 * prioritized. Also, when control characters are echoed with a prefixed "^", 607 * the pair is treated atomically and thus not separated. 608 * 609 * Locking: callers must hold %output_lock. 610 */ 611 static size_t __process_echoes(struct tty_struct *tty) 612 { 613 struct n_tty_data *ldata = tty->disc_data; 614 int space, old_space; 615 size_t tail; 616 unsigned char c; 617 618 old_space = space = tty_write_room(tty); 619 620 tail = ldata->echo_tail; 621 while (MASK(ldata->echo_commit) != MASK(tail)) { 622 c = echo_buf(ldata, tail); 623 if (c == ECHO_OP_START) { 624 unsigned char op; 625 bool space_left = true; 626 627 /* 628 * Since add_echo_byte() is called without holding 629 * output_lock, we might see only portion of multi-byte 630 * operation. 631 */ 632 if (MASK(ldata->echo_commit) == MASK(tail + 1)) 633 goto not_yet_stored; 634 /* 635 * If the buffer byte is the start of a multi-byte 636 * operation, get the next byte, which is either the 637 * op code or a control character value. 638 */ 639 op = echo_buf(ldata, tail + 1); 640 641 switch (op) { 642 case ECHO_OP_ERASE_TAB: { 643 unsigned int num_chars, num_bs; 644 645 if (MASK(ldata->echo_commit) == MASK(tail + 2)) 646 goto not_yet_stored; 647 num_chars = echo_buf(ldata, tail + 2); 648 649 /* 650 * Determine how many columns to go back 651 * in order to erase the tab. 652 * This depends on the number of columns 653 * used by other characters within the tab 654 * area. If this (modulo 8) count is from 655 * the start of input rather than from a 656 * previous tab, we offset by canon column. 657 * Otherwise, tab spacing is normal. 658 */ 659 if (!(num_chars & 0x80)) 660 num_chars += ldata->canon_column; 661 num_bs = 8 - (num_chars & 7); 662 663 if (num_bs > space) { 664 space_left = false; 665 break; 666 } 667 space -= num_bs; 668 while (num_bs--) { 669 tty_put_char(tty, '\b'); 670 if (ldata->column > 0) 671 ldata->column--; 672 } 673 tail += 3; 674 break; 675 } 676 case ECHO_OP_SET_CANON_COL: 677 ldata->canon_column = ldata->column; 678 tail += 2; 679 break; 680 681 case ECHO_OP_MOVE_BACK_COL: 682 if (ldata->column > 0) 683 ldata->column--; 684 tail += 2; 685 break; 686 687 case ECHO_OP_START: 688 /* This is an escaped echo op start code */ 689 if (!space) { 690 space_left = false; 691 break; 692 } 693 tty_put_char(tty, ECHO_OP_START); 694 ldata->column++; 695 space--; 696 tail += 2; 697 break; 698 699 default: 700 /* 701 * If the op is not a special byte code, 702 * it is a ctrl char tagged to be echoed 703 * as "^X" (where X is the letter 704 * representing the control char). 705 * Note that we must ensure there is 706 * enough space for the whole ctrl pair. 707 * 708 */ 709 if (space < 2) { 710 space_left = false; 711 break; 712 } 713 tty_put_char(tty, '^'); 714 tty_put_char(tty, op ^ 0100); 715 ldata->column += 2; 716 space -= 2; 717 tail += 2; 718 } 719 720 if (!space_left) 721 break; 722 } else { 723 if (O_OPOST(tty)) { 724 int retval = do_output_char(c, tty, space); 725 if (retval < 0) 726 break; 727 space -= retval; 728 } else { 729 if (!space) 730 break; 731 tty_put_char(tty, c); 732 space -= 1; 733 } 734 tail += 1; 735 } 736 } 737 738 /* If the echo buffer is nearly full (so that the possibility exists 739 * of echo overrun before the next commit), then discard enough 740 * data at the tail to prevent a subsequent overrun */ 741 while (ldata->echo_commit > tail && 742 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) { 743 if (echo_buf(ldata, tail) == ECHO_OP_START) { 744 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB) 745 tail += 3; 746 else 747 tail += 2; 748 } else 749 tail++; 750 } 751 752 not_yet_stored: 753 ldata->echo_tail = tail; 754 return old_space - space; 755 } 756 757 static void commit_echoes(struct tty_struct *tty) 758 { 759 struct n_tty_data *ldata = tty->disc_data; 760 size_t nr, old, echoed; 761 size_t head; 762 763 mutex_lock(&ldata->output_lock); 764 head = ldata->echo_head; 765 ldata->echo_mark = head; 766 old = ldata->echo_commit - ldata->echo_tail; 767 768 /* Process committed echoes if the accumulated # of bytes 769 * is over the threshold (and try again each time another 770 * block is accumulated) */ 771 nr = head - ldata->echo_tail; 772 if (nr < ECHO_COMMIT_WATERMARK || 773 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) { 774 mutex_unlock(&ldata->output_lock); 775 return; 776 } 777 778 ldata->echo_commit = head; 779 echoed = __process_echoes(tty); 780 mutex_unlock(&ldata->output_lock); 781 782 if (echoed && tty->ops->flush_chars) 783 tty->ops->flush_chars(tty); 784 } 785 786 static void process_echoes(struct tty_struct *tty) 787 { 788 struct n_tty_data *ldata = tty->disc_data; 789 size_t echoed; 790 791 if (ldata->echo_mark == ldata->echo_tail) 792 return; 793 794 mutex_lock(&ldata->output_lock); 795 ldata->echo_commit = ldata->echo_mark; 796 echoed = __process_echoes(tty); 797 mutex_unlock(&ldata->output_lock); 798 799 if (echoed && tty->ops->flush_chars) 800 tty->ops->flush_chars(tty); 801 } 802 803 /* NB: echo_mark and echo_head should be equivalent here */ 804 static void flush_echoes(struct tty_struct *tty) 805 { 806 struct n_tty_data *ldata = tty->disc_data; 807 808 if ((!L_ECHO(tty) && !L_ECHONL(tty)) || 809 ldata->echo_commit == ldata->echo_head) 810 return; 811 812 mutex_lock(&ldata->output_lock); 813 ldata->echo_commit = ldata->echo_head; 814 __process_echoes(tty); 815 mutex_unlock(&ldata->output_lock); 816 } 817 818 /** 819 * add_echo_byte - add a byte to the echo buffer 820 * @c: unicode byte to echo 821 * @ldata: n_tty data 822 * 823 * Add a character or operation byte to the echo buffer. 824 */ 825 static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata) 826 { 827 *echo_buf_addr(ldata, ldata->echo_head) = c; 828 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */ 829 ldata->echo_head++; 830 } 831 832 /** 833 * echo_move_back_col - add operation to move back a column 834 * @ldata: n_tty data 835 * 836 * Add an operation to the echo buffer to move back one column. 837 */ 838 static void echo_move_back_col(struct n_tty_data *ldata) 839 { 840 add_echo_byte(ECHO_OP_START, ldata); 841 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata); 842 } 843 844 /** 845 * echo_set_canon_col - add operation to set the canon column 846 * @ldata: n_tty data 847 * 848 * Add an operation to the echo buffer to set the canon column to the current 849 * column. 850 */ 851 static void echo_set_canon_col(struct n_tty_data *ldata) 852 { 853 add_echo_byte(ECHO_OP_START, ldata); 854 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata); 855 } 856 857 /** 858 * echo_erase_tab - add operation to erase a tab 859 * @num_chars: number of character columns already used 860 * @after_tab: true if num_chars starts after a previous tab 861 * @ldata: n_tty data 862 * 863 * Add an operation to the echo buffer to erase a tab. 864 * 865 * Called by the eraser function, which knows how many character columns have 866 * been used since either a previous tab or the start of input. This 867 * information will be used later, along with canon column (if applicable), to 868 * go back the correct number of columns. 869 */ 870 static void echo_erase_tab(unsigned int num_chars, int after_tab, 871 struct n_tty_data *ldata) 872 { 873 add_echo_byte(ECHO_OP_START, ldata); 874 add_echo_byte(ECHO_OP_ERASE_TAB, ldata); 875 876 /* We only need to know this modulo 8 (tab spacing) */ 877 num_chars &= 7; 878 879 /* Set the high bit as a flag if num_chars is after a previous tab */ 880 if (after_tab) 881 num_chars |= 0x80; 882 883 add_echo_byte(num_chars, ldata); 884 } 885 886 /** 887 * echo_char_raw - echo a character raw 888 * @c: unicode byte to echo 889 * @ldata: line disc data 890 * 891 * Echo user input back onto the screen. This must be called only when 892 * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path. 893 * 894 * This variant does not treat control characters specially. 895 */ 896 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata) 897 { 898 if (c == ECHO_OP_START) { 899 add_echo_byte(ECHO_OP_START, ldata); 900 add_echo_byte(ECHO_OP_START, ldata); 901 } else { 902 add_echo_byte(c, ldata); 903 } 904 } 905 906 /** 907 * echo_char - echo a character 908 * @c: unicode byte to echo 909 * @tty: terminal device 910 * 911 * Echo user input back onto the screen. This must be called only when 912 * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path. 913 * 914 * This variant tags control characters to be echoed as "^X" (where X is the 915 * letter representing the control char). 916 */ 917 static void echo_char(unsigned char c, const struct tty_struct *tty) 918 { 919 struct n_tty_data *ldata = tty->disc_data; 920 921 if (c == ECHO_OP_START) { 922 add_echo_byte(ECHO_OP_START, ldata); 923 add_echo_byte(ECHO_OP_START, ldata); 924 } else { 925 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') 926 add_echo_byte(ECHO_OP_START, ldata); 927 add_echo_byte(c, ldata); 928 } 929 } 930 931 /** 932 * finish_erasing - complete erase 933 * @ldata: n_tty data 934 */ 935 static inline void finish_erasing(struct n_tty_data *ldata) 936 { 937 if (ldata->erasing) { 938 echo_char_raw('/', ldata); 939 ldata->erasing = 0; 940 } 941 } 942 943 /** 944 * eraser - handle erase function 945 * @c: character input 946 * @tty: terminal device 947 * 948 * Perform erase and necessary output when an erase character is present in the 949 * stream from the driver layer. Handles the complexities of UTF-8 multibyte 950 * symbols. 951 * 952 * Locking: n_tty_receive_buf()/producer path: 953 * caller holds non-exclusive %termios_rwsem 954 */ 955 static void eraser(unsigned char c, const struct tty_struct *tty) 956 { 957 struct n_tty_data *ldata = tty->disc_data; 958 enum { ERASE, WERASE, KILL } kill_type; 959 size_t head; 960 size_t cnt; 961 int seen_alnums; 962 963 if (ldata->read_head == ldata->canon_head) { 964 /* process_output('\a', tty); */ /* what do you think? */ 965 return; 966 } 967 if (c == ERASE_CHAR(tty)) 968 kill_type = ERASE; 969 else if (c == WERASE_CHAR(tty)) 970 kill_type = WERASE; 971 else { 972 if (!L_ECHO(tty)) { 973 ldata->read_head = ldata->canon_head; 974 return; 975 } 976 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { 977 ldata->read_head = ldata->canon_head; 978 finish_erasing(ldata); 979 echo_char(KILL_CHAR(tty), tty); 980 /* Add a newline if ECHOK is on and ECHOKE is off. */ 981 if (L_ECHOK(tty)) 982 echo_char_raw('\n', ldata); 983 return; 984 } 985 kill_type = KILL; 986 } 987 988 seen_alnums = 0; 989 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) { 990 head = ldata->read_head; 991 992 /* erase a single possibly multibyte character */ 993 do { 994 head--; 995 c = read_buf(ldata, head); 996 } while (is_continuation(c, tty) && 997 MASK(head) != MASK(ldata->canon_head)); 998 999 /* do not partially erase */ 1000 if (is_continuation(c, tty)) 1001 break; 1002 1003 if (kill_type == WERASE) { 1004 /* Equivalent to BSD's ALTWERASE. */ 1005 if (isalnum(c) || c == '_') 1006 seen_alnums++; 1007 else if (seen_alnums) 1008 break; 1009 } 1010 cnt = ldata->read_head - head; 1011 ldata->read_head = head; 1012 if (L_ECHO(tty)) { 1013 if (L_ECHOPRT(tty)) { 1014 if (!ldata->erasing) { 1015 echo_char_raw('\\', ldata); 1016 ldata->erasing = 1; 1017 } 1018 /* if cnt > 1, output a multi-byte character */ 1019 echo_char(c, tty); 1020 while (--cnt > 0) { 1021 head++; 1022 echo_char_raw(read_buf(ldata, head), ldata); 1023 echo_move_back_col(ldata); 1024 } 1025 } else if (kill_type == ERASE && !L_ECHOE(tty)) { 1026 echo_char(ERASE_CHAR(tty), tty); 1027 } else if (c == '\t') { 1028 unsigned int num_chars = 0; 1029 int after_tab = 0; 1030 size_t tail = ldata->read_head; 1031 1032 /* 1033 * Count the columns used for characters 1034 * since the start of input or after a 1035 * previous tab. 1036 * This info is used to go back the correct 1037 * number of columns. 1038 */ 1039 while (MASK(tail) != MASK(ldata->canon_head)) { 1040 tail--; 1041 c = read_buf(ldata, tail); 1042 if (c == '\t') { 1043 after_tab = 1; 1044 break; 1045 } else if (iscntrl(c)) { 1046 if (L_ECHOCTL(tty)) 1047 num_chars += 2; 1048 } else if (!is_continuation(c, tty)) { 1049 num_chars++; 1050 } 1051 } 1052 echo_erase_tab(num_chars, after_tab, ldata); 1053 } else { 1054 if (iscntrl(c) && L_ECHOCTL(tty)) { 1055 echo_char_raw('\b', ldata); 1056 echo_char_raw(' ', ldata); 1057 echo_char_raw('\b', ldata); 1058 } 1059 if (!iscntrl(c) || L_ECHOCTL(tty)) { 1060 echo_char_raw('\b', ldata); 1061 echo_char_raw(' ', ldata); 1062 echo_char_raw('\b', ldata); 1063 } 1064 } 1065 } 1066 if (kill_type == ERASE) 1067 break; 1068 } 1069 if (ldata->read_head == ldata->canon_head && L_ECHO(tty)) 1070 finish_erasing(ldata); 1071 } 1072 1073 1074 static void __isig(int sig, struct tty_struct *tty) 1075 { 1076 struct pid *tty_pgrp = tty_get_pgrp(tty); 1077 if (tty_pgrp) { 1078 kill_pgrp(tty_pgrp, sig, 1); 1079 put_pid(tty_pgrp); 1080 } 1081 } 1082 1083 /** 1084 * isig - handle the ISIG optio 1085 * @sig: signal 1086 * @tty: terminal 1087 * 1088 * Called when a signal is being sent due to terminal input. Called from the 1089 * &tty_driver.receive_buf() path, so serialized. 1090 * 1091 * Performs input and output flush if !NOFLSH. In this context, the echo 1092 * buffer is 'output'. The signal is processed first to alert any current 1093 * readers or writers to discontinue and exit their i/o loops. 1094 * 1095 * Locking: %ctrl.lock 1096 */ 1097 static void isig(int sig, struct tty_struct *tty) 1098 { 1099 struct n_tty_data *ldata = tty->disc_data; 1100 1101 if (L_NOFLSH(tty)) { 1102 /* signal only */ 1103 __isig(sig, tty); 1104 1105 } else { /* signal and flush */ 1106 up_read(&tty->termios_rwsem); 1107 down_write(&tty->termios_rwsem); 1108 1109 __isig(sig, tty); 1110 1111 /* clear echo buffer */ 1112 mutex_lock(&ldata->output_lock); 1113 ldata->echo_head = ldata->echo_tail = 0; 1114 ldata->echo_mark = ldata->echo_commit = 0; 1115 mutex_unlock(&ldata->output_lock); 1116 1117 /* clear output buffer */ 1118 tty_driver_flush_buffer(tty); 1119 1120 /* clear input buffer */ 1121 reset_buffer_flags(tty->disc_data); 1122 1123 /* notify pty master of flush */ 1124 if (tty->link) 1125 n_tty_packet_mode_flush(tty); 1126 1127 up_write(&tty->termios_rwsem); 1128 down_read(&tty->termios_rwsem); 1129 } 1130 } 1131 1132 /** 1133 * n_tty_receive_break - handle break 1134 * @tty: terminal 1135 * 1136 * An RS232 break event has been hit in the incoming bitstream. This can cause 1137 * a variety of events depending upon the termios settings. 1138 * 1139 * Locking: n_tty_receive_buf()/producer path: 1140 * caller holds non-exclusive termios_rwsem 1141 * 1142 * Note: may get exclusive %termios_rwsem if flushing input buffer 1143 */ 1144 static void n_tty_receive_break(struct tty_struct *tty) 1145 { 1146 struct n_tty_data *ldata = tty->disc_data; 1147 1148 if (I_IGNBRK(tty)) 1149 return; 1150 if (I_BRKINT(tty)) { 1151 isig(SIGINT, tty); 1152 return; 1153 } 1154 if (I_PARMRK(tty)) { 1155 put_tty_queue('\377', ldata); 1156 put_tty_queue('\0', ldata); 1157 } 1158 put_tty_queue('\0', ldata); 1159 } 1160 1161 /** 1162 * n_tty_receive_overrun - handle overrun reporting 1163 * @tty: terminal 1164 * 1165 * Data arrived faster than we could process it. While the tty driver has 1166 * flagged this the bits that were missed are gone forever. 1167 * 1168 * Called from the receive_buf path so single threaded. Does not need locking 1169 * as num_overrun and overrun_time are function private. 1170 */ 1171 static void n_tty_receive_overrun(const struct tty_struct *tty) 1172 { 1173 struct n_tty_data *ldata = tty->disc_data; 1174 1175 ldata->num_overrun++; 1176 if (time_after(jiffies, ldata->overrun_time + HZ) || 1177 time_after(ldata->overrun_time, jiffies)) { 1178 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun); 1179 ldata->overrun_time = jiffies; 1180 ldata->num_overrun = 0; 1181 } 1182 } 1183 1184 /** 1185 * n_tty_receive_parity_error - error notifier 1186 * @tty: terminal device 1187 * @c: character 1188 * 1189 * Process a parity error and queue the right data to indicate the error case 1190 * if necessary. 1191 * 1192 * Locking: n_tty_receive_buf()/producer path: 1193 * caller holds non-exclusive %termios_rwsem 1194 */ 1195 static void n_tty_receive_parity_error(const struct tty_struct *tty, 1196 unsigned char c) 1197 { 1198 struct n_tty_data *ldata = tty->disc_data; 1199 1200 if (I_INPCK(tty)) { 1201 if (I_IGNPAR(tty)) 1202 return; 1203 if (I_PARMRK(tty)) { 1204 put_tty_queue('\377', ldata); 1205 put_tty_queue('\0', ldata); 1206 put_tty_queue(c, ldata); 1207 } else 1208 put_tty_queue('\0', ldata); 1209 } else 1210 put_tty_queue(c, ldata); 1211 } 1212 1213 static void 1214 n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) 1215 { 1216 isig(signal, tty); 1217 if (I_IXON(tty)) 1218 start_tty(tty); 1219 if (L_ECHO(tty)) { 1220 echo_char(c, tty); 1221 commit_echoes(tty); 1222 } else 1223 process_echoes(tty); 1224 } 1225 1226 static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, unsigned char c) 1227 { 1228 return c == START_CHAR(tty) || c == STOP_CHAR(tty); 1229 } 1230 1231 /** 1232 * n_tty_receive_char_flow_ctrl - receive flow control chars 1233 * @tty: terminal device 1234 * @c: character 1235 * @lookahead_done: lookahead has processed this character already 1236 * 1237 * Receive and process flow control character actions. 1238 * 1239 * In case lookahead for flow control chars already handled the character in 1240 * advance to the normal receive, the actions are skipped during normal 1241 * receive. 1242 * 1243 * Returns true if @c is consumed as flow-control character, the character 1244 * must not be treated as normal character. 1245 */ 1246 static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, unsigned char c, 1247 bool lookahead_done) 1248 { 1249 if (!n_tty_is_char_flow_ctrl(tty, c)) 1250 return false; 1251 1252 if (lookahead_done) 1253 return true; 1254 1255 if (c == START_CHAR(tty)) { 1256 start_tty(tty); 1257 process_echoes(tty); 1258 return true; 1259 } 1260 1261 /* STOP_CHAR */ 1262 stop_tty(tty); 1263 return true; 1264 } 1265 1266 static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c, 1267 bool lookahead_done) 1268 { 1269 struct n_tty_data *ldata = tty->disc_data; 1270 1271 if (I_IXON(tty) && n_tty_receive_char_flow_ctrl(tty, c, lookahead_done)) 1272 return; 1273 1274 if (L_ISIG(tty)) { 1275 if (c == INTR_CHAR(tty)) { 1276 n_tty_receive_signal_char(tty, SIGINT, c); 1277 return; 1278 } else if (c == QUIT_CHAR(tty)) { 1279 n_tty_receive_signal_char(tty, SIGQUIT, c); 1280 return; 1281 } else if (c == SUSP_CHAR(tty)) { 1282 n_tty_receive_signal_char(tty, SIGTSTP, c); 1283 return; 1284 } 1285 } 1286 1287 if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) { 1288 start_tty(tty); 1289 process_echoes(tty); 1290 } 1291 1292 if (c == '\r') { 1293 if (I_IGNCR(tty)) 1294 return; 1295 if (I_ICRNL(tty)) 1296 c = '\n'; 1297 } else if (c == '\n' && I_INLCR(tty)) 1298 c = '\r'; 1299 1300 if (ldata->icanon) { 1301 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || 1302 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { 1303 eraser(c, tty); 1304 commit_echoes(tty); 1305 return; 1306 } 1307 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { 1308 ldata->lnext = 1; 1309 if (L_ECHO(tty)) { 1310 finish_erasing(ldata); 1311 if (L_ECHOCTL(tty)) { 1312 echo_char_raw('^', ldata); 1313 echo_char_raw('\b', ldata); 1314 commit_echoes(tty); 1315 } 1316 } 1317 return; 1318 } 1319 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) { 1320 size_t tail = ldata->canon_head; 1321 1322 finish_erasing(ldata); 1323 echo_char(c, tty); 1324 echo_char_raw('\n', ldata); 1325 while (MASK(tail) != MASK(ldata->read_head)) { 1326 echo_char(read_buf(ldata, tail), tty); 1327 tail++; 1328 } 1329 commit_echoes(tty); 1330 return; 1331 } 1332 if (c == '\n') { 1333 if (L_ECHO(tty) || L_ECHONL(tty)) { 1334 echo_char_raw('\n', ldata); 1335 commit_echoes(tty); 1336 } 1337 goto handle_newline; 1338 } 1339 if (c == EOF_CHAR(tty)) { 1340 c = __DISABLED_CHAR; 1341 goto handle_newline; 1342 } 1343 if ((c == EOL_CHAR(tty)) || 1344 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { 1345 /* 1346 * XXX are EOL_CHAR and EOL2_CHAR echoed?!? 1347 */ 1348 if (L_ECHO(tty)) { 1349 /* Record the column of first canon char. */ 1350 if (ldata->canon_head == ldata->read_head) 1351 echo_set_canon_col(ldata); 1352 echo_char(c, tty); 1353 commit_echoes(tty); 1354 } 1355 /* 1356 * XXX does PARMRK doubling happen for 1357 * EOL_CHAR and EOL2_CHAR? 1358 */ 1359 if (c == (unsigned char) '\377' && I_PARMRK(tty)) 1360 put_tty_queue(c, ldata); 1361 1362 handle_newline: 1363 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags); 1364 put_tty_queue(c, ldata); 1365 smp_store_release(&ldata->canon_head, ldata->read_head); 1366 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1367 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM); 1368 return; 1369 } 1370 } 1371 1372 if (L_ECHO(tty)) { 1373 finish_erasing(ldata); 1374 if (c == '\n') 1375 echo_char_raw('\n', ldata); 1376 else { 1377 /* Record the column of first canon char. */ 1378 if (ldata->canon_head == ldata->read_head) 1379 echo_set_canon_col(ldata); 1380 echo_char(c, tty); 1381 } 1382 commit_echoes(tty); 1383 } 1384 1385 /* PARMRK doubling check */ 1386 if (c == (unsigned char) '\377' && I_PARMRK(tty)) 1387 put_tty_queue(c, ldata); 1388 1389 put_tty_queue(c, ldata); 1390 } 1391 1392 /** 1393 * n_tty_receive_char - perform processing 1394 * @tty: terminal device 1395 * @c: character 1396 * 1397 * Process an individual character of input received from the driver. This is 1398 * serialized with respect to itself by the rules for the driver above. 1399 * 1400 * Locking: n_tty_receive_buf()/producer path: 1401 * caller holds non-exclusive %termios_rwsem 1402 * publishes canon_head if canonical mode is active 1403 */ 1404 static void n_tty_receive_char(struct tty_struct *tty, unsigned char c) 1405 { 1406 struct n_tty_data *ldata = tty->disc_data; 1407 1408 if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) { 1409 start_tty(tty); 1410 process_echoes(tty); 1411 } 1412 if (L_ECHO(tty)) { 1413 finish_erasing(ldata); 1414 /* Record the column of first canon char. */ 1415 if (ldata->canon_head == ldata->read_head) 1416 echo_set_canon_col(ldata); 1417 echo_char(c, tty); 1418 commit_echoes(tty); 1419 } 1420 /* PARMRK doubling check */ 1421 if (c == (unsigned char) '\377' && I_PARMRK(tty)) 1422 put_tty_queue(c, ldata); 1423 put_tty_queue(c, ldata); 1424 } 1425 1426 static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c, 1427 bool lookahead_done) 1428 { 1429 if (I_ISTRIP(tty)) 1430 c &= 0x7f; 1431 if (I_IUCLC(tty) && L_IEXTEN(tty)) 1432 c = tolower(c); 1433 1434 if (I_IXON(tty)) { 1435 if (!n_tty_receive_char_flow_ctrl(tty, c, lookahead_done) && 1436 tty->flow.stopped && !tty->flow.tco_stopped && I_IXANY(tty) && 1437 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && 1438 c != SUSP_CHAR(tty)) { 1439 start_tty(tty); 1440 process_echoes(tty); 1441 } 1442 } 1443 } 1444 1445 static void 1446 n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag) 1447 { 1448 switch (flag) { 1449 case TTY_BREAK: 1450 n_tty_receive_break(tty); 1451 break; 1452 case TTY_PARITY: 1453 case TTY_FRAME: 1454 n_tty_receive_parity_error(tty, c); 1455 break; 1456 case TTY_OVERRUN: 1457 n_tty_receive_overrun(tty); 1458 break; 1459 default: 1460 tty_err(tty, "unknown flag %d\n", flag); 1461 break; 1462 } 1463 } 1464 1465 static void 1466 n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag) 1467 { 1468 struct n_tty_data *ldata = tty->disc_data; 1469 1470 ldata->lnext = 0; 1471 if (likely(flag == TTY_NORMAL)) { 1472 if (I_ISTRIP(tty)) 1473 c &= 0x7f; 1474 if (I_IUCLC(tty) && L_IEXTEN(tty)) 1475 c = tolower(c); 1476 n_tty_receive_char(tty, c); 1477 } else 1478 n_tty_receive_char_flagged(tty, c, flag); 1479 } 1480 1481 /* Caller must ensure count > 0 */ 1482 static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const unsigned char *cp, 1483 const unsigned char *fp, size_t count) 1484 { 1485 struct n_tty_data *ldata = tty->disc_data; 1486 unsigned char flag = TTY_NORMAL; 1487 1488 ldata->lookahead_count += count; 1489 1490 if (!I_IXON(tty)) 1491 return; 1492 1493 while (count--) { 1494 if (fp) 1495 flag = *fp++; 1496 if (likely(flag == TTY_NORMAL)) 1497 n_tty_receive_char_flow_ctrl(tty, *cp, false); 1498 cp++; 1499 } 1500 } 1501 1502 static void 1503 n_tty_receive_buf_real_raw(const struct tty_struct *tty, 1504 const unsigned char *cp, int count) 1505 { 1506 struct n_tty_data *ldata = tty->disc_data; 1507 size_t n, head; 1508 1509 head = ldata->read_head & (N_TTY_BUF_SIZE - 1); 1510 n = min_t(size_t, count, N_TTY_BUF_SIZE - head); 1511 memcpy(read_buf_addr(ldata, head), cp, n); 1512 ldata->read_head += n; 1513 cp += n; 1514 count -= n; 1515 1516 head = ldata->read_head & (N_TTY_BUF_SIZE - 1); 1517 n = min_t(size_t, count, N_TTY_BUF_SIZE - head); 1518 memcpy(read_buf_addr(ldata, head), cp, n); 1519 ldata->read_head += n; 1520 } 1521 1522 static void 1523 n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp, 1524 const char *fp, int count) 1525 { 1526 struct n_tty_data *ldata = tty->disc_data; 1527 char flag = TTY_NORMAL; 1528 1529 while (count--) { 1530 if (fp) 1531 flag = *fp++; 1532 if (likely(flag == TTY_NORMAL)) 1533 put_tty_queue(*cp++, ldata); 1534 else 1535 n_tty_receive_char_flagged(tty, *cp++, flag); 1536 } 1537 } 1538 1539 static void 1540 n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp, 1541 const char *fp, int count, bool lookahead_done) 1542 { 1543 char flag = TTY_NORMAL; 1544 1545 while (count--) { 1546 if (fp) 1547 flag = *fp++; 1548 if (likely(flag == TTY_NORMAL)) 1549 n_tty_receive_char_closing(tty, *cp++, lookahead_done); 1550 } 1551 } 1552 1553 static void n_tty_receive_buf_standard(struct tty_struct *tty, 1554 const unsigned char *cp, const char *fp, int count, bool lookahead_done) 1555 { 1556 struct n_tty_data *ldata = tty->disc_data; 1557 char flag = TTY_NORMAL; 1558 1559 while (count--) { 1560 unsigned char c = *cp++; 1561 1562 if (fp) 1563 flag = *fp++; 1564 1565 if (ldata->lnext) { 1566 n_tty_receive_char_lnext(tty, c, flag); 1567 continue; 1568 } 1569 1570 if (unlikely(flag != TTY_NORMAL)) { 1571 n_tty_receive_char_flagged(tty, c, flag); 1572 continue; 1573 } 1574 1575 if (I_ISTRIP(tty)) 1576 c &= 0x7f; 1577 if (I_IUCLC(tty) && L_IEXTEN(tty)) 1578 c = tolower(c); 1579 if (L_EXTPROC(tty)) { 1580 put_tty_queue(c, ldata); 1581 continue; 1582 } 1583 1584 if (test_bit(c, ldata->char_map)) 1585 n_tty_receive_char_special(tty, c, lookahead_done); 1586 else 1587 n_tty_receive_char(tty, c); 1588 } 1589 } 1590 1591 static void __receive_buf(struct tty_struct *tty, const unsigned char *cp, 1592 const char *fp, int count) 1593 { 1594 struct n_tty_data *ldata = tty->disc_data; 1595 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty)); 1596 size_t la_count = min_t(size_t, ldata->lookahead_count, count); 1597 1598 if (ldata->real_raw) 1599 n_tty_receive_buf_real_raw(tty, cp, count); 1600 else if (ldata->raw || (L_EXTPROC(tty) && !preops)) 1601 n_tty_receive_buf_raw(tty, cp, fp, count); 1602 else if (tty->closing && !L_EXTPROC(tty)) { 1603 if (la_count > 0) 1604 n_tty_receive_buf_closing(tty, cp, fp, la_count, true); 1605 if (count > la_count) 1606 n_tty_receive_buf_closing(tty, cp, fp, count - la_count, false); 1607 } else { 1608 if (la_count > 0) 1609 n_tty_receive_buf_standard(tty, cp, fp, la_count, true); 1610 if (count > la_count) 1611 n_tty_receive_buf_standard(tty, cp, fp, count - la_count, false); 1612 1613 flush_echoes(tty); 1614 if (tty->ops->flush_chars) 1615 tty->ops->flush_chars(tty); 1616 } 1617 1618 ldata->lookahead_count -= la_count; 1619 1620 if (ldata->icanon && !L_EXTPROC(tty)) 1621 return; 1622 1623 /* publish read_head to consumer */ 1624 smp_store_release(&ldata->commit_head, ldata->read_head); 1625 1626 if (read_cnt(ldata)) { 1627 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 1628 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM); 1629 } 1630 } 1631 1632 /** 1633 * n_tty_receive_buf_common - process input 1634 * @tty: device to receive input 1635 * @cp: input chars 1636 * @fp: flags for each char (if %NULL, all chars are %TTY_NORMAL) 1637 * @count: number of input chars in @cp 1638 * @flow: enable flow control 1639 * 1640 * Called by the terminal driver when a block of characters has been received. 1641 * This function must be called from soft contexts not from interrupt context. 1642 * The driver is responsible for making calls one at a time and in order (or 1643 * using flush_to_ldisc()). 1644 * 1645 * Returns: the # of input chars from @cp which were processed. 1646 * 1647 * In canonical mode, the maximum line length is 4096 chars (including the line 1648 * termination char); lines longer than 4096 chars are truncated. After 4095 1649 * chars, input data is still processed but not stored. Overflow processing 1650 * ensures the tty can always receive more input until at least one line can be 1651 * read. 1652 * 1653 * In non-canonical mode, the read buffer will only accept 4095 chars; this 1654 * provides the necessary space for a newline char if the input mode is 1655 * switched to canonical. 1656 * 1657 * Note it is possible for the read buffer to _contain_ 4096 chars in 1658 * non-canonical mode: the read buffer could already contain the maximum canon 1659 * line of 4096 chars when the mode is switched to non-canonical. 1660 * 1661 * Locking: n_tty_receive_buf()/producer path: 1662 * claims non-exclusive %termios_rwsem 1663 * publishes commit_head or canon_head 1664 */ 1665 static size_t 1666 n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp, 1667 const char *fp, int count, int flow) 1668 { 1669 struct n_tty_data *ldata = tty->disc_data; 1670 size_t rcvd = 0; 1671 int room, n, overflow; 1672 1673 down_read(&tty->termios_rwsem); 1674 1675 do { 1676 /* 1677 * When PARMRK is set, each input char may take up to 3 chars 1678 * in the read buf; reduce the buffer space avail by 3x 1679 * 1680 * If we are doing input canonicalization, and there are no 1681 * pending newlines, let characters through without limit, so 1682 * that erase characters will be handled. Other excess 1683 * characters will be beeped. 1684 * 1685 * paired with store in *_copy_from_read_buf() -- guarantees 1686 * the consumer has loaded the data in read_buf up to the new 1687 * read_tail (so this producer will not overwrite unread data) 1688 */ 1689 size_t tail = smp_load_acquire(&ldata->read_tail); 1690 1691 room = N_TTY_BUF_SIZE - (ldata->read_head - tail); 1692 if (I_PARMRK(tty)) 1693 room = DIV_ROUND_UP(room, 3); 1694 room--; 1695 if (room <= 0) { 1696 overflow = ldata->icanon && ldata->canon_head == tail; 1697 if (overflow && room < 0) 1698 ldata->read_head--; 1699 room = overflow; 1700 WRITE_ONCE(ldata->no_room, flow && !room); 1701 } else 1702 overflow = 0; 1703 1704 n = min(count, room); 1705 if (!n) 1706 break; 1707 1708 /* ignore parity errors if handling overflow */ 1709 if (!overflow || !fp || *fp != TTY_PARITY) 1710 __receive_buf(tty, cp, fp, n); 1711 1712 cp += n; 1713 if (fp) 1714 fp += n; 1715 count -= n; 1716 rcvd += n; 1717 } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags)); 1718 1719 tty->receive_room = room; 1720 1721 /* Unthrottle if handling overflow on pty */ 1722 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) { 1723 if (overflow) { 1724 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE); 1725 tty_unthrottle_safe(tty); 1726 __tty_set_flow_change(tty, 0); 1727 } 1728 } else 1729 n_tty_check_throttle(tty); 1730 1731 if (unlikely(ldata->no_room)) { 1732 /* 1733 * Barrier here is to ensure to read the latest read_tail in 1734 * chars_in_buffer() and to make sure that read_tail is not loaded 1735 * before ldata->no_room is set. 1736 */ 1737 smp_mb(); 1738 if (!chars_in_buffer(tty)) 1739 n_tty_kick_worker(tty); 1740 } 1741 1742 up_read(&tty->termios_rwsem); 1743 1744 return rcvd; 1745 } 1746 1747 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, 1748 const char *fp, size_t count) 1749 { 1750 n_tty_receive_buf_common(tty, cp, fp, count, 0); 1751 } 1752 1753 static size_t n_tty_receive_buf2(struct tty_struct *tty, 1754 const unsigned char *cp, const char *fp, 1755 size_t count) 1756 { 1757 return n_tty_receive_buf_common(tty, cp, fp, count, 1); 1758 } 1759 1760 /** 1761 * n_tty_set_termios - termios data changed 1762 * @tty: terminal 1763 * @old: previous data 1764 * 1765 * Called by the tty layer when the user changes termios flags so that the line 1766 * discipline can plan ahead. This function cannot sleep and is protected from 1767 * re-entry by the tty layer. The user is guaranteed that this function will 1768 * not be re-entered or in progress when the ldisc is closed. 1769 * 1770 * Locking: Caller holds @tty->termios_rwsem 1771 */ 1772 static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old) 1773 { 1774 struct n_tty_data *ldata = tty->disc_data; 1775 1776 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) { 1777 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE); 1778 ldata->line_start = ldata->read_tail; 1779 if (!L_ICANON(tty) || !read_cnt(ldata)) { 1780 ldata->canon_head = ldata->read_tail; 1781 ldata->push = 0; 1782 } else { 1783 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1), 1784 ldata->read_flags); 1785 ldata->canon_head = ldata->read_head; 1786 ldata->push = 1; 1787 } 1788 ldata->commit_head = ldata->read_head; 1789 ldata->erasing = 0; 1790 ldata->lnext = 0; 1791 } 1792 1793 ldata->icanon = (L_ICANON(tty) != 0); 1794 1795 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || 1796 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || 1797 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || 1798 I_PARMRK(tty)) { 1799 bitmap_zero(ldata->char_map, 256); 1800 1801 if (I_IGNCR(tty) || I_ICRNL(tty)) 1802 set_bit('\r', ldata->char_map); 1803 if (I_INLCR(tty)) 1804 set_bit('\n', ldata->char_map); 1805 1806 if (L_ICANON(tty)) { 1807 set_bit(ERASE_CHAR(tty), ldata->char_map); 1808 set_bit(KILL_CHAR(tty), ldata->char_map); 1809 set_bit(EOF_CHAR(tty), ldata->char_map); 1810 set_bit('\n', ldata->char_map); 1811 set_bit(EOL_CHAR(tty), ldata->char_map); 1812 if (L_IEXTEN(tty)) { 1813 set_bit(WERASE_CHAR(tty), ldata->char_map); 1814 set_bit(LNEXT_CHAR(tty), ldata->char_map); 1815 set_bit(EOL2_CHAR(tty), ldata->char_map); 1816 if (L_ECHO(tty)) 1817 set_bit(REPRINT_CHAR(tty), 1818 ldata->char_map); 1819 } 1820 } 1821 if (I_IXON(tty)) { 1822 set_bit(START_CHAR(tty), ldata->char_map); 1823 set_bit(STOP_CHAR(tty), ldata->char_map); 1824 } 1825 if (L_ISIG(tty)) { 1826 set_bit(INTR_CHAR(tty), ldata->char_map); 1827 set_bit(QUIT_CHAR(tty), ldata->char_map); 1828 set_bit(SUSP_CHAR(tty), ldata->char_map); 1829 } 1830 clear_bit(__DISABLED_CHAR, ldata->char_map); 1831 ldata->raw = 0; 1832 ldata->real_raw = 0; 1833 } else { 1834 ldata->raw = 1; 1835 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && 1836 (I_IGNPAR(tty) || !I_INPCK(tty)) && 1837 (tty->driver->flags & TTY_DRIVER_REAL_RAW)) 1838 ldata->real_raw = 1; 1839 else 1840 ldata->real_raw = 0; 1841 } 1842 /* 1843 * Fix tty hang when I_IXON(tty) is cleared, but the tty 1844 * been stopped by STOP_CHAR(tty) before it. 1845 */ 1846 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) { 1847 start_tty(tty); 1848 process_echoes(tty); 1849 } 1850 1851 /* The termios change make the tty ready for I/O */ 1852 wake_up_interruptible(&tty->write_wait); 1853 wake_up_interruptible(&tty->read_wait); 1854 } 1855 1856 /** 1857 * n_tty_close - close the ldisc for this tty 1858 * @tty: device 1859 * 1860 * Called from the terminal layer when this line discipline is being shut down, 1861 * either because of a close or becsuse of a discipline change. The function 1862 * will not be called while other ldisc methods are in progress. 1863 */ 1864 static void n_tty_close(struct tty_struct *tty) 1865 { 1866 struct n_tty_data *ldata = tty->disc_data; 1867 1868 if (tty->link) 1869 n_tty_packet_mode_flush(tty); 1870 1871 down_write(&tty->termios_rwsem); 1872 vfree(ldata); 1873 tty->disc_data = NULL; 1874 up_write(&tty->termios_rwsem); 1875 } 1876 1877 /** 1878 * n_tty_open - open an ldisc 1879 * @tty: terminal to open 1880 * 1881 * Called when this line discipline is being attached to the terminal device. 1882 * Can sleep. Called serialized so that no other events will occur in parallel. 1883 * No further open will occur until a close. 1884 */ 1885 static int n_tty_open(struct tty_struct *tty) 1886 { 1887 struct n_tty_data *ldata; 1888 1889 /* Currently a malloc failure here can panic */ 1890 ldata = vzalloc(sizeof(*ldata)); 1891 if (!ldata) 1892 return -ENOMEM; 1893 1894 ldata->overrun_time = jiffies; 1895 mutex_init(&ldata->atomic_read_lock); 1896 mutex_init(&ldata->output_lock); 1897 1898 tty->disc_data = ldata; 1899 tty->closing = 0; 1900 /* indicate buffer work may resume */ 1901 clear_bit(TTY_LDISC_HALTED, &tty->flags); 1902 n_tty_set_termios(tty, NULL); 1903 tty_unthrottle(tty); 1904 return 0; 1905 } 1906 1907 static inline int input_available_p(const struct tty_struct *tty, int poll) 1908 { 1909 const struct n_tty_data *ldata = tty->disc_data; 1910 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1; 1911 1912 if (ldata->icanon && !L_EXTPROC(tty)) 1913 return ldata->canon_head != ldata->read_tail; 1914 else 1915 return ldata->commit_head - ldata->read_tail >= amt; 1916 } 1917 1918 /** 1919 * copy_from_read_buf - copy read data directly 1920 * @tty: terminal device 1921 * @kbp: data 1922 * @nr: size of data 1923 * 1924 * Helper function to speed up n_tty_read(). It is only called when %ICANON is 1925 * off; it copies characters straight from the tty queue. 1926 * 1927 * Returns: true if it successfully copied data, but there is still more data 1928 * to be had. 1929 * 1930 * Locking: 1931 * * called under the @ldata->atomic_read_lock sem 1932 * * n_tty_read()/consumer path: 1933 * caller holds non-exclusive %termios_rwsem; 1934 * read_tail published 1935 */ 1936 static bool copy_from_read_buf(const struct tty_struct *tty, 1937 unsigned char **kbp, 1938 size_t *nr) 1939 1940 { 1941 struct n_tty_data *ldata = tty->disc_data; 1942 size_t n; 1943 bool is_eof; 1944 size_t head = smp_load_acquire(&ldata->commit_head); 1945 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); 1946 1947 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail); 1948 n = min(*nr, n); 1949 if (n) { 1950 unsigned char *from = read_buf_addr(ldata, tail); 1951 memcpy(*kbp, from, n); 1952 is_eof = n == 1 && *from == EOF_CHAR(tty); 1953 tty_audit_add_data(tty, from, n); 1954 zero_buffer(tty, from, n); 1955 smp_store_release(&ldata->read_tail, ldata->read_tail + n); 1956 /* Turn single EOF into zero-length read */ 1957 if (L_EXTPROC(tty) && ldata->icanon && is_eof && 1958 (head == ldata->read_tail)) 1959 return false; 1960 *kbp += n; 1961 *nr -= n; 1962 1963 /* If we have more to copy, let the caller know */ 1964 return head != ldata->read_tail; 1965 } 1966 return false; 1967 } 1968 1969 /** 1970 * canon_copy_from_read_buf - copy read data in canonical mode 1971 * @tty: terminal device 1972 * @kbp: data 1973 * @nr: size of data 1974 * 1975 * Helper function for n_tty_read(). It is only called when %ICANON is on; it 1976 * copies one line of input up to and including the line-delimiting character 1977 * into the result buffer. 1978 * 1979 * Note: When termios is changed from non-canonical to canonical mode and the 1980 * read buffer contains data, n_tty_set_termios() simulates an EOF push (as if 1981 * C-d were input) _without_ the %DISABLED_CHAR in the buffer. This causes data 1982 * already processed as input to be immediately available as input although a 1983 * newline has not been received. 1984 * 1985 * Locking: 1986 * * called under the %atomic_read_lock mutex 1987 * * n_tty_read()/consumer path: 1988 * caller holds non-exclusive %termios_rwsem; 1989 * read_tail published 1990 */ 1991 static bool canon_copy_from_read_buf(const struct tty_struct *tty, 1992 unsigned char **kbp, 1993 size_t *nr) 1994 { 1995 struct n_tty_data *ldata = tty->disc_data; 1996 size_t n, size, more, c; 1997 size_t eol; 1998 size_t tail, canon_head; 1999 int found = 0; 2000 2001 /* N.B. avoid overrun if nr == 0 */ 2002 if (!*nr) 2003 return false; 2004 2005 canon_head = smp_load_acquire(&ldata->canon_head); 2006 n = min(*nr, canon_head - ldata->read_tail); 2007 2008 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); 2009 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE); 2010 2011 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n", 2012 __func__, *nr, tail, n, size); 2013 2014 eol = find_next_bit(ldata->read_flags, size, tail); 2015 more = n - (size - tail); 2016 if (eol == N_TTY_BUF_SIZE && more) { 2017 /* scan wrapped without finding set bit */ 2018 eol = find_first_bit(ldata->read_flags, more); 2019 found = eol != more; 2020 } else 2021 found = eol != size; 2022 2023 n = eol - tail; 2024 if (n > N_TTY_BUF_SIZE) 2025 n += N_TTY_BUF_SIZE; 2026 c = n + found; 2027 2028 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) 2029 n = c; 2030 2031 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n", 2032 __func__, eol, found, n, c, tail, more); 2033 2034 tty_copy(tty, *kbp, tail, n); 2035 *kbp += n; 2036 *nr -= n; 2037 2038 if (found) 2039 clear_bit(eol, ldata->read_flags); 2040 smp_store_release(&ldata->read_tail, ldata->read_tail + c); 2041 2042 if (found) { 2043 if (!ldata->push) 2044 ldata->line_start = ldata->read_tail; 2045 else 2046 ldata->push = 0; 2047 tty_audit_push(); 2048 return false; 2049 } 2050 2051 /* No EOL found - do a continuation retry if there is more data */ 2052 return ldata->read_tail != canon_head; 2053 } 2054 2055 /* 2056 * If we finished a read at the exact location of an 2057 * EOF (special EOL character that's a __DISABLED_CHAR) 2058 * in the stream, silently eat the EOF. 2059 */ 2060 static void canon_skip_eof(struct n_tty_data *ldata) 2061 { 2062 size_t tail, canon_head; 2063 2064 canon_head = smp_load_acquire(&ldata->canon_head); 2065 tail = ldata->read_tail; 2066 2067 // No data? 2068 if (tail == canon_head) 2069 return; 2070 2071 // See if the tail position is EOF in the circular buffer 2072 tail &= (N_TTY_BUF_SIZE - 1); 2073 if (!test_bit(tail, ldata->read_flags)) 2074 return; 2075 if (read_buf(ldata, tail) != __DISABLED_CHAR) 2076 return; 2077 2078 // Clear the EOL bit, skip the EOF char. 2079 clear_bit(tail, ldata->read_flags); 2080 smp_store_release(&ldata->read_tail, ldata->read_tail + 1); 2081 } 2082 2083 /** 2084 * job_control - check job control 2085 * @tty: tty 2086 * @file: file handle 2087 * 2088 * Perform job control management checks on this @file/@tty descriptor and if 2089 * appropriate send any needed signals and return a negative error code if 2090 * action should be taken. 2091 * 2092 * Locking: 2093 * * redirected write test is safe 2094 * * current->signal->tty check is safe 2095 * * ctrl.lock to safely reference @tty->ctrl.pgrp 2096 */ 2097 static int job_control(struct tty_struct *tty, struct file *file) 2098 { 2099 /* Job control check -- must be done at start and after 2100 every sleep (POSIX.1 7.1.1.4). */ 2101 /* NOTE: not yet done after every sleep pending a thorough 2102 check of the logic of this change. -- jlc */ 2103 /* don't stop on /dev/console */ 2104 if (file->f_op->write_iter == redirected_tty_write) 2105 return 0; 2106 2107 return __tty_check_change(tty, SIGTTIN); 2108 } 2109 2110 2111 /** 2112 * n_tty_read - read function for tty 2113 * @tty: tty device 2114 * @file: file object 2115 * @kbuf: kernelspace buffer pointer 2116 * @nr: size of I/O 2117 * @cookie: if non-%NULL, this is a continuation read 2118 * @offset: where to continue reading from (unused in n_tty) 2119 * 2120 * Perform reads for the line discipline. We are guaranteed that the line 2121 * discipline will not be closed under us but we may get multiple parallel 2122 * readers and must handle this ourselves. We may also get a hangup. Always 2123 * called in user context, may sleep. 2124 * 2125 * This code must be sure never to sleep through a hangup. 2126 * 2127 * Locking: n_tty_read()/consumer path: 2128 * claims non-exclusive termios_rwsem; 2129 * publishes read_tail 2130 */ 2131 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, 2132 unsigned char *kbuf, size_t nr, 2133 void **cookie, unsigned long offset) 2134 { 2135 struct n_tty_data *ldata = tty->disc_data; 2136 unsigned char *kb = kbuf; 2137 DEFINE_WAIT_FUNC(wait, woken_wake_function); 2138 int c; 2139 int minimum, time; 2140 ssize_t retval = 0; 2141 long timeout; 2142 bool packet; 2143 size_t old_tail; 2144 2145 /* 2146 * Is this a continuation of a read started earler? 2147 * 2148 * If so, we still hold the atomic_read_lock and the 2149 * termios_rwsem, and can just continue to copy data. 2150 */ 2151 if (*cookie) { 2152 if (ldata->icanon && !L_EXTPROC(tty)) { 2153 /* 2154 * If we have filled the user buffer, see 2155 * if we should skip an EOF character before 2156 * releasing the lock and returning done. 2157 */ 2158 if (!nr) 2159 canon_skip_eof(ldata); 2160 else if (canon_copy_from_read_buf(tty, &kb, &nr)) 2161 return kb - kbuf; 2162 } else { 2163 if (copy_from_read_buf(tty, &kb, &nr)) 2164 return kb - kbuf; 2165 } 2166 2167 /* No more data - release locks and stop retries */ 2168 n_tty_kick_worker(tty); 2169 n_tty_check_unthrottle(tty); 2170 up_read(&tty->termios_rwsem); 2171 mutex_unlock(&ldata->atomic_read_lock); 2172 *cookie = NULL; 2173 return kb - kbuf; 2174 } 2175 2176 c = job_control(tty, file); 2177 if (c < 0) 2178 return c; 2179 2180 /* 2181 * Internal serialization of reads. 2182 */ 2183 if (file->f_flags & O_NONBLOCK) { 2184 if (!mutex_trylock(&ldata->atomic_read_lock)) 2185 return -EAGAIN; 2186 } else { 2187 if (mutex_lock_interruptible(&ldata->atomic_read_lock)) 2188 return -ERESTARTSYS; 2189 } 2190 2191 down_read(&tty->termios_rwsem); 2192 2193 minimum = time = 0; 2194 timeout = MAX_SCHEDULE_TIMEOUT; 2195 if (!ldata->icanon) { 2196 minimum = MIN_CHAR(tty); 2197 if (minimum) { 2198 time = (HZ / 10) * TIME_CHAR(tty); 2199 } else { 2200 timeout = (HZ / 10) * TIME_CHAR(tty); 2201 minimum = 1; 2202 } 2203 } 2204 2205 packet = tty->ctrl.packet; 2206 old_tail = ldata->read_tail; 2207 2208 add_wait_queue(&tty->read_wait, &wait); 2209 while (nr) { 2210 /* First test for status change. */ 2211 if (packet && tty->link->ctrl.pktstatus) { 2212 unsigned char cs; 2213 if (kb != kbuf) 2214 break; 2215 spin_lock_irq(&tty->link->ctrl.lock); 2216 cs = tty->link->ctrl.pktstatus; 2217 tty->link->ctrl.pktstatus = 0; 2218 spin_unlock_irq(&tty->link->ctrl.lock); 2219 *kb++ = cs; 2220 nr--; 2221 break; 2222 } 2223 2224 if (!input_available_p(tty, 0)) { 2225 up_read(&tty->termios_rwsem); 2226 tty_buffer_flush_work(tty->port); 2227 down_read(&tty->termios_rwsem); 2228 if (!input_available_p(tty, 0)) { 2229 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { 2230 retval = -EIO; 2231 break; 2232 } 2233 if (tty_hung_up_p(file)) 2234 break; 2235 /* 2236 * Abort readers for ttys which never actually 2237 * get hung up. See __tty_hangup(). 2238 */ 2239 if (test_bit(TTY_HUPPING, &tty->flags)) 2240 break; 2241 if (!timeout) 2242 break; 2243 if (tty_io_nonblock(tty, file)) { 2244 retval = -EAGAIN; 2245 break; 2246 } 2247 if (signal_pending(current)) { 2248 retval = -ERESTARTSYS; 2249 break; 2250 } 2251 up_read(&tty->termios_rwsem); 2252 2253 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE, 2254 timeout); 2255 2256 down_read(&tty->termios_rwsem); 2257 continue; 2258 } 2259 } 2260 2261 if (ldata->icanon && !L_EXTPROC(tty)) { 2262 if (canon_copy_from_read_buf(tty, &kb, &nr)) 2263 goto more_to_be_read; 2264 } else { 2265 /* Deal with packet mode. */ 2266 if (packet && kb == kbuf) { 2267 *kb++ = TIOCPKT_DATA; 2268 nr--; 2269 } 2270 2271 /* 2272 * Copy data, and if there is more to be had 2273 * and we have nothing more to wait for, then 2274 * let's mark us for retries. 2275 * 2276 * NOTE! We return here with both the termios_sem 2277 * and atomic_read_lock still held, the retries 2278 * will release them when done. 2279 */ 2280 if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum) { 2281 more_to_be_read: 2282 remove_wait_queue(&tty->read_wait, &wait); 2283 *cookie = cookie; 2284 return kb - kbuf; 2285 } 2286 } 2287 2288 n_tty_check_unthrottle(tty); 2289 2290 if (kb - kbuf >= minimum) 2291 break; 2292 if (time) 2293 timeout = time; 2294 } 2295 if (old_tail != ldata->read_tail) { 2296 /* 2297 * Make sure no_room is not read in n_tty_kick_worker() 2298 * before setting ldata->read_tail in copy_from_read_buf(). 2299 */ 2300 smp_mb(); 2301 n_tty_kick_worker(tty); 2302 } 2303 up_read(&tty->termios_rwsem); 2304 2305 remove_wait_queue(&tty->read_wait, &wait); 2306 mutex_unlock(&ldata->atomic_read_lock); 2307 2308 if (kb - kbuf) 2309 retval = kb - kbuf; 2310 2311 return retval; 2312 } 2313 2314 /** 2315 * n_tty_write - write function for tty 2316 * @tty: tty device 2317 * @file: file object 2318 * @buf: userspace buffer pointer 2319 * @nr: size of I/O 2320 * 2321 * Write function of the terminal device. This is serialized with respect to 2322 * other write callers but not to termios changes, reads and other such events. 2323 * Since the receive code will echo characters, thus calling driver write 2324 * methods, the %output_lock is used in the output processing functions called 2325 * here as well as in the echo processing function to protect the column state 2326 * and space left in the buffer. 2327 * 2328 * This code must be sure never to sleep through a hangup. 2329 * 2330 * Locking: output_lock to protect column state and space left 2331 * (note that the process_output*() functions take this lock themselves) 2332 */ 2333 2334 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, 2335 const unsigned char *buf, size_t nr) 2336 { 2337 const unsigned char *b = buf; 2338 DEFINE_WAIT_FUNC(wait, woken_wake_function); 2339 int c; 2340 ssize_t retval = 0; 2341 2342 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ 2343 if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) { 2344 retval = tty_check_change(tty); 2345 if (retval) 2346 return retval; 2347 } 2348 2349 down_read(&tty->termios_rwsem); 2350 2351 /* Write out any echoed characters that are still pending */ 2352 process_echoes(tty); 2353 2354 add_wait_queue(&tty->write_wait, &wait); 2355 while (1) { 2356 if (signal_pending(current)) { 2357 retval = -ERESTARTSYS; 2358 break; 2359 } 2360 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { 2361 retval = -EIO; 2362 break; 2363 } 2364 if (O_OPOST(tty)) { 2365 while (nr > 0) { 2366 ssize_t num = process_output_block(tty, b, nr); 2367 if (num < 0) { 2368 if (num == -EAGAIN) 2369 break; 2370 retval = num; 2371 goto break_out; 2372 } 2373 b += num; 2374 nr -= num; 2375 if (nr == 0) 2376 break; 2377 c = *b; 2378 if (process_output(c, tty) < 0) 2379 break; 2380 b++; nr--; 2381 } 2382 if (tty->ops->flush_chars) 2383 tty->ops->flush_chars(tty); 2384 } else { 2385 struct n_tty_data *ldata = tty->disc_data; 2386 2387 while (nr > 0) { 2388 mutex_lock(&ldata->output_lock); 2389 c = tty->ops->write(tty, b, nr); 2390 mutex_unlock(&ldata->output_lock); 2391 if (c < 0) { 2392 retval = c; 2393 goto break_out; 2394 } 2395 if (!c) 2396 break; 2397 b += c; 2398 nr -= c; 2399 } 2400 } 2401 if (!nr) 2402 break; 2403 if (tty_io_nonblock(tty, file)) { 2404 retval = -EAGAIN; 2405 break; 2406 } 2407 up_read(&tty->termios_rwsem); 2408 2409 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 2410 2411 down_read(&tty->termios_rwsem); 2412 } 2413 break_out: 2414 remove_wait_queue(&tty->write_wait, &wait); 2415 if (nr && tty->fasync) 2416 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 2417 up_read(&tty->termios_rwsem); 2418 return (b - buf) ? b - buf : retval; 2419 } 2420 2421 /** 2422 * n_tty_poll - poll method for N_TTY 2423 * @tty: terminal device 2424 * @file: file accessing it 2425 * @wait: poll table 2426 * 2427 * Called when the line discipline is asked to poll() for data or for special 2428 * events. This code is not serialized with respect to other events save 2429 * open/close. 2430 * 2431 * This code must be sure never to sleep through a hangup. 2432 * 2433 * Locking: called without the kernel lock held -- fine. 2434 */ 2435 static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file, 2436 poll_table *wait) 2437 { 2438 __poll_t mask = 0; 2439 2440 poll_wait(file, &tty->read_wait, wait); 2441 poll_wait(file, &tty->write_wait, wait); 2442 if (input_available_p(tty, 1)) 2443 mask |= EPOLLIN | EPOLLRDNORM; 2444 else { 2445 tty_buffer_flush_work(tty->port); 2446 if (input_available_p(tty, 1)) 2447 mask |= EPOLLIN | EPOLLRDNORM; 2448 } 2449 if (tty->ctrl.packet && tty->link->ctrl.pktstatus) 2450 mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM; 2451 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) 2452 mask |= EPOLLHUP; 2453 if (tty_hung_up_p(file)) 2454 mask |= EPOLLHUP; 2455 if (tty->ops->write && !tty_is_writelocked(tty) && 2456 tty_chars_in_buffer(tty) < WAKEUP_CHARS && 2457 tty_write_room(tty) > 0) 2458 mask |= EPOLLOUT | EPOLLWRNORM; 2459 return mask; 2460 } 2461 2462 static unsigned long inq_canon(struct n_tty_data *ldata) 2463 { 2464 size_t nr, head, tail; 2465 2466 if (ldata->canon_head == ldata->read_tail) 2467 return 0; 2468 head = ldata->canon_head; 2469 tail = ldata->read_tail; 2470 nr = head - tail; 2471 /* Skip EOF-chars.. */ 2472 while (MASK(head) != MASK(tail)) { 2473 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) && 2474 read_buf(ldata, tail) == __DISABLED_CHAR) 2475 nr--; 2476 tail++; 2477 } 2478 return nr; 2479 } 2480 2481 static int n_tty_ioctl(struct tty_struct *tty, unsigned int cmd, 2482 unsigned long arg) 2483 { 2484 struct n_tty_data *ldata = tty->disc_data; 2485 int retval; 2486 2487 switch (cmd) { 2488 case TIOCOUTQ: 2489 return put_user(tty_chars_in_buffer(tty), (int __user *) arg); 2490 case TIOCINQ: 2491 down_write(&tty->termios_rwsem); 2492 if (L_ICANON(tty) && !L_EXTPROC(tty)) 2493 retval = inq_canon(ldata); 2494 else 2495 retval = read_cnt(ldata); 2496 up_write(&tty->termios_rwsem); 2497 return put_user(retval, (unsigned int __user *) arg); 2498 default: 2499 return n_tty_ioctl_helper(tty, cmd, arg); 2500 } 2501 } 2502 2503 static struct tty_ldisc_ops n_tty_ops = { 2504 .owner = THIS_MODULE, 2505 .num = N_TTY, 2506 .name = "n_tty", 2507 .open = n_tty_open, 2508 .close = n_tty_close, 2509 .flush_buffer = n_tty_flush_buffer, 2510 .read = n_tty_read, 2511 .write = n_tty_write, 2512 .ioctl = n_tty_ioctl, 2513 .set_termios = n_tty_set_termios, 2514 .poll = n_tty_poll, 2515 .receive_buf = n_tty_receive_buf, 2516 .write_wakeup = n_tty_write_wakeup, 2517 .receive_buf2 = n_tty_receive_buf2, 2518 .lookahead_buf = n_tty_lookahead_flow_ctrl, 2519 }; 2520 2521 /** 2522 * n_tty_inherit_ops - inherit N_TTY methods 2523 * @ops: struct tty_ldisc_ops where to save N_TTY methods 2524 * 2525 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods. 2526 */ 2527 2528 void n_tty_inherit_ops(struct tty_ldisc_ops *ops) 2529 { 2530 *ops = n_tty_ops; 2531 ops->owner = NULL; 2532 } 2533 EXPORT_SYMBOL_GPL(n_tty_inherit_ops); 2534 2535 void __init n_tty_init(void) 2536 { 2537 tty_register_ldisc(&n_tty_ops); 2538 } 2539