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