1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/types.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/signal.h>
37#include <linux/fcntl.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/tty.h>
41#include <linux/timer.h>
42#include <linux/ctype.h>
43#include <linux/mm.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/poll.h>
47#include <linux/bitops.h>
48#include <linux/audit.h>
49#include <linux/file.h>
50#include <linux/uaccess.h>
51#include <linux/module.h>
52#include <linux/ratelimit.h>
53
54
55
56#define WAKEUP_CHARS 256
57
58
59
60
61
62
63#define TTY_THRESHOLD_THROTTLE 128
64#define TTY_THRESHOLD_UNTHROTTLE 128
65
66
67
68
69
70
71
72#define ECHO_OP_START 0xff
73#define ECHO_OP_MOVE_BACK_COL 0x80
74#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82
76
77struct n_tty_data {
78 unsigned int column;
79 unsigned long overrun_time;
80 int num_overrun;
81
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
84
85 DECLARE_BITMAP(process_char_map, 256);
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
87
88 char *read_buf;
89 int read_head;
90 int read_tail;
91 int read_cnt;
92 int minimum_to_wake;
93
94 unsigned char *echo_buf;
95 unsigned int echo_pos;
96 unsigned int echo_cnt;
97
98 int canon_data;
99 unsigned long canon_head;
100 unsigned int canon_column;
101
102 struct mutex atomic_read_lock;
103 struct mutex output_lock;
104 struct mutex echo_lock;
105 raw_spinlock_t read_lock;
106};
107
108static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
109 unsigned char __user *ptr)
110{
111 struct n_tty_data *ldata = tty->disc_data;
112
113 tty_audit_add_data(tty, &x, 1, ldata->icanon);
114 return put_user(x, ptr);
115}
116
117
118
119
120
121
122
123
124
125
126
127
128static int set_room(struct tty_struct *tty)
129{
130 struct n_tty_data *ldata = tty->disc_data;
131 int left;
132 int old_left;
133 unsigned long flags;
134
135 raw_spin_lock_irqsave(&ldata->read_lock, flags);
136
137 if (I_PARMRK(tty)) {
138
139
140
141 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
142 } else
143 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
144
145
146
147
148
149
150
151 if (left <= 0)
152 left = ldata->icanon && !ldata->canon_data;
153 old_left = tty->receive_room;
154 tty->receive_room = left;
155
156 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
157
158 return left && !old_left;
159}
160
161static void n_tty_set_room(struct tty_struct *tty)
162{
163
164 if (set_room(tty)) {
165 WARN_RATELIMIT(tty->port->itty == NULL,
166 "scheduling with invalid itty\n");
167
168
169
170
171 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
172 "scheduling buffer work for halted ldisc\n");
173 schedule_work(&tty->port->buf.work);
174 }
175}
176
177static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
178{
179 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
180 ldata->read_buf[ldata->read_head] = c;
181 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
182 ldata->read_cnt++;
183 }
184}
185
186
187
188
189
190
191
192
193
194
195
196static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
197{
198 unsigned long flags;
199
200
201
202
203 raw_spin_lock_irqsave(&ldata->read_lock, flags);
204 put_tty_queue_nolock(c, ldata);
205 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
206}
207
208
209
210
211
212
213
214
215
216
217
218static void reset_buffer_flags(struct n_tty_data *ldata)
219{
220 unsigned long flags;
221
222 raw_spin_lock_irqsave(&ldata->read_lock, flags);
223 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
224 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
225
226 mutex_lock(&ldata->echo_lock);
227 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
228 mutex_unlock(&ldata->echo_lock);
229
230 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
231 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
232}
233
234static void n_tty_packet_mode_flush(struct tty_struct *tty)
235{
236 unsigned long flags;
237
238 spin_lock_irqsave(&tty->ctrl_lock, flags);
239 if (tty->link->packet) {
240 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
241 wake_up_interruptible(&tty->link->read_wait);
242 }
243 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
244}
245
246
247
248
249
250
251
252
253
254
255
256
257static void n_tty_flush_buffer(struct tty_struct *tty)
258{
259 reset_buffer_flags(tty->disc_data);
260 n_tty_set_room(tty);
261
262 if (tty->link)
263 n_tty_packet_mode_flush(tty);
264}
265
266
267
268
269
270
271
272
273
274
275
276static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
277{
278 struct n_tty_data *ldata = tty->disc_data;
279 unsigned long flags;
280 ssize_t n = 0;
281
282 raw_spin_lock_irqsave(&ldata->read_lock, flags);
283 if (!ldata->icanon) {
284 n = ldata->read_cnt;
285 } else if (ldata->canon_data) {
286 n = (ldata->canon_head > ldata->read_tail) ?
287 ldata->canon_head - ldata->read_tail :
288 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
289 }
290 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
291 return n;
292}
293
294
295
296
297
298
299
300
301
302
303static inline int is_utf8_continuation(unsigned char c)
304{
305 return (c & 0xc0) == 0x80;
306}
307
308
309
310
311
312
313
314
315
316static inline int is_continuation(unsigned char c, struct tty_struct *tty)
317{
318 return I_IUTF8(tty) && is_utf8_continuation(c);
319}
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
344{
345 struct n_tty_data *ldata = tty->disc_data;
346 int spaces;
347
348 if (!space)
349 return -1;
350
351 switch (c) {
352 case '\n':
353 if (O_ONLRET(tty))
354 ldata->column = 0;
355 if (O_ONLCR(tty)) {
356 if (space < 2)
357 return -1;
358 ldata->canon_column = ldata->column = 0;
359 tty->ops->write(tty, "\r\n", 2);
360 return 2;
361 }
362 ldata->canon_column = ldata->column;
363 break;
364 case '\r':
365 if (O_ONOCR(tty) && ldata->column == 0)
366 return 0;
367 if (O_OCRNL(tty)) {
368 c = '\n';
369 if (O_ONLRET(tty))
370 ldata->canon_column = ldata->column = 0;
371 break;
372 }
373 ldata->canon_column = ldata->column = 0;
374 break;
375 case '\t':
376 spaces = 8 - (ldata->column & 7);
377 if (O_TABDLY(tty) == XTABS) {
378 if (space < spaces)
379 return -1;
380 ldata->column += spaces;
381 tty->ops->write(tty, " ", spaces);
382 return spaces;
383 }
384 ldata->column += spaces;
385 break;
386 case '\b':
387 if (ldata->column > 0)
388 ldata->column--;
389 break;
390 default:
391 if (!iscntrl(c)) {
392 if (O_OLCUC(tty))
393 c = toupper(c);
394 if (!is_continuation(c, tty))
395 ldata->column++;
396 }
397 break;
398 }
399
400 tty_put_char(tty, c);
401 return 1;
402}
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418static int process_output(unsigned char c, struct tty_struct *tty)
419{
420 struct n_tty_data *ldata = tty->disc_data;
421 int space, retval;
422
423 mutex_lock(&ldata->output_lock);
424
425 space = tty_write_room(tty);
426 retval = do_output_char(c, tty, space);
427
428 mutex_unlock(&ldata->output_lock);
429 if (retval < 0)
430 return -1;
431 else
432 return 0;
433}
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454static ssize_t process_output_block(struct tty_struct *tty,
455 const unsigned char *buf, unsigned int nr)
456{
457 struct n_tty_data *ldata = tty->disc_data;
458 int space;
459 int i;
460 const unsigned char *cp;
461
462 mutex_lock(&ldata->output_lock);
463
464 space = tty_write_room(tty);
465 if (!space) {
466 mutex_unlock(&ldata->output_lock);
467 return 0;
468 }
469 if (nr > space)
470 nr = space;
471
472 for (i = 0, cp = buf; i < nr; i++, cp++) {
473 unsigned char c = *cp;
474
475 switch (c) {
476 case '\n':
477 if (O_ONLRET(tty))
478 ldata->column = 0;
479 if (O_ONLCR(tty))
480 goto break_out;
481 ldata->canon_column = ldata->column;
482 break;
483 case '\r':
484 if (O_ONOCR(tty) && ldata->column == 0)
485 goto break_out;
486 if (O_OCRNL(tty))
487 goto break_out;
488 ldata->canon_column = ldata->column = 0;
489 break;
490 case '\t':
491 goto break_out;
492 case '\b':
493 if (ldata->column > 0)
494 ldata->column--;
495 break;
496 default:
497 if (!iscntrl(c)) {
498 if (O_OLCUC(tty))
499 goto break_out;
500 if (!is_continuation(c, tty))
501 ldata->column++;
502 }
503 break;
504 }
505 }
506break_out:
507 i = tty->ops->write(tty, buf, i);
508
509 mutex_unlock(&ldata->output_lock);
510 return i;
511}
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539static void process_echoes(struct tty_struct *tty)
540{
541 struct n_tty_data *ldata = tty->disc_data;
542 int space, nr;
543 unsigned char c;
544 unsigned char *cp, *buf_end;
545
546 if (!ldata->echo_cnt)
547 return;
548
549 mutex_lock(&ldata->output_lock);
550 mutex_lock(&ldata->echo_lock);
551
552 space = tty_write_room(tty);
553
554 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
555 cp = ldata->echo_buf + ldata->echo_pos;
556 nr = ldata->echo_cnt;
557 while (nr > 0) {
558 c = *cp;
559 if (c == ECHO_OP_START) {
560 unsigned char op;
561 unsigned char *opp;
562 int no_space_left = 0;
563
564
565
566
567
568
569 opp = cp + 1;
570 if (opp == buf_end)
571 opp -= N_TTY_BUF_SIZE;
572 op = *opp;
573
574 switch (op) {
575 unsigned int num_chars, num_bs;
576
577 case ECHO_OP_ERASE_TAB:
578 if (++opp == buf_end)
579 opp -= N_TTY_BUF_SIZE;
580 num_chars = *opp;
581
582
583
584
585
586
587
588
589
590
591
592 if (!(num_chars & 0x80))
593 num_chars += ldata->canon_column;
594 num_bs = 8 - (num_chars & 7);
595
596 if (num_bs > space) {
597 no_space_left = 1;
598 break;
599 }
600 space -= num_bs;
601 while (num_bs--) {
602 tty_put_char(tty, '\b');
603 if (ldata->column > 0)
604 ldata->column--;
605 }
606 cp += 3;
607 nr -= 3;
608 break;
609
610 case ECHO_OP_SET_CANON_COL:
611 ldata->canon_column = ldata->column;
612 cp += 2;
613 nr -= 2;
614 break;
615
616 case ECHO_OP_MOVE_BACK_COL:
617 if (ldata->column > 0)
618 ldata->column--;
619 cp += 2;
620 nr -= 2;
621 break;
622
623 case ECHO_OP_START:
624
625 if (!space) {
626 no_space_left = 1;
627 break;
628 }
629 tty_put_char(tty, ECHO_OP_START);
630 ldata->column++;
631 space--;
632 cp += 2;
633 nr -= 2;
634 break;
635
636 default:
637
638
639
640
641
642
643
644
645
646 if (space < 2) {
647 no_space_left = 1;
648 break;
649 }
650 tty_put_char(tty, '^');
651 tty_put_char(tty, op ^ 0100);
652 ldata->column += 2;
653 space -= 2;
654 cp += 2;
655 nr -= 2;
656 }
657
658 if (no_space_left)
659 break;
660 } else {
661 if (O_OPOST(tty)) {
662 int retval = do_output_char(c, tty, space);
663 if (retval < 0)
664 break;
665 space -= retval;
666 } else {
667 if (!space)
668 break;
669 tty_put_char(tty, c);
670 space -= 1;
671 }
672 cp += 1;
673 nr -= 1;
674 }
675
676
677 if (cp >= buf_end)
678 cp -= N_TTY_BUF_SIZE;
679 }
680
681 if (nr == 0) {
682 ldata->echo_pos = 0;
683 ldata->echo_cnt = 0;
684 ldata->echo_overrun = 0;
685 } else {
686 int num_processed = ldata->echo_cnt - nr;
687 ldata->echo_pos += num_processed;
688 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
689 ldata->echo_cnt = nr;
690 if (num_processed > 0)
691 ldata->echo_overrun = 0;
692 }
693
694 mutex_unlock(&ldata->echo_lock);
695 mutex_unlock(&ldata->output_lock);
696
697 if (tty->ops->flush_chars)
698 tty->ops->flush_chars(tty);
699}
700
701
702
703
704
705
706
707
708
709
710
711static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
712{
713 int new_byte_pos;
714
715 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
716
717 new_byte_pos = ldata->echo_pos;
718
719
720
721
722
723 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
724 if (ldata->echo_buf[(ldata->echo_pos + 1) &
725 (N_TTY_BUF_SIZE - 1)] ==
726 ECHO_OP_ERASE_TAB) {
727 ldata->echo_pos += 3;
728 ldata->echo_cnt -= 2;
729 } else {
730 ldata->echo_pos += 2;
731 ldata->echo_cnt -= 1;
732 }
733 } else {
734 ldata->echo_pos++;
735 }
736 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
737
738 ldata->echo_overrun = 1;
739 } else {
740 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
741 new_byte_pos &= N_TTY_BUF_SIZE - 1;
742 ldata->echo_cnt++;
743 }
744
745 ldata->echo_buf[new_byte_pos] = c;
746}
747
748
749
750
751
752
753
754
755
756
757static void echo_move_back_col(struct n_tty_data *ldata)
758{
759 mutex_lock(&ldata->echo_lock);
760 add_echo_byte(ECHO_OP_START, ldata);
761 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
762 mutex_unlock(&ldata->echo_lock);
763}
764
765
766
767
768
769
770
771
772
773
774
775static void echo_set_canon_col(struct n_tty_data *ldata)
776{
777 mutex_lock(&ldata->echo_lock);
778 add_echo_byte(ECHO_OP_START, ldata);
779 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
780 mutex_unlock(&ldata->echo_lock);
781}
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800static void echo_erase_tab(unsigned int num_chars, int after_tab,
801 struct n_tty_data *ldata)
802{
803 mutex_lock(&ldata->echo_lock);
804
805 add_echo_byte(ECHO_OP_START, ldata);
806 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
807
808
809 num_chars &= 7;
810
811
812 if (after_tab)
813 num_chars |= 0x80;
814
815 add_echo_byte(num_chars, ldata);
816
817 mutex_unlock(&ldata->echo_lock);
818}
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
834{
835 mutex_lock(&ldata->echo_lock);
836 if (c == ECHO_OP_START) {
837 add_echo_byte(ECHO_OP_START, ldata);
838 add_echo_byte(ECHO_OP_START, ldata);
839 } else {
840 add_echo_byte(c, ldata);
841 }
842 mutex_unlock(&ldata->echo_lock);
843}
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859static void echo_char(unsigned char c, struct tty_struct *tty)
860{
861 struct n_tty_data *ldata = tty->disc_data;
862
863 mutex_lock(&ldata->echo_lock);
864
865 if (c == ECHO_OP_START) {
866 add_echo_byte(ECHO_OP_START, ldata);
867 add_echo_byte(ECHO_OP_START, ldata);
868 } else {
869 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
870 add_echo_byte(ECHO_OP_START, ldata);
871 add_echo_byte(c, ldata);
872 }
873
874 mutex_unlock(&ldata->echo_lock);
875}
876
877
878
879
880
881
882static inline void finish_erasing(struct n_tty_data *ldata)
883{
884 if (ldata->erasing) {
885 echo_char_raw('/', ldata);
886 ldata->erasing = 0;
887 }
888}
889
890
891
892
893
894
895
896
897
898
899
900
901
902static void eraser(unsigned char c, struct tty_struct *tty)
903{
904 struct n_tty_data *ldata = tty->disc_data;
905 enum { ERASE, WERASE, KILL } kill_type;
906 int head, seen_alnums, cnt;
907 unsigned long flags;
908
909
910 if (ldata->read_head == ldata->canon_head) {
911
912 return;
913 }
914 if (c == ERASE_CHAR(tty))
915 kill_type = ERASE;
916 else if (c == WERASE_CHAR(tty))
917 kill_type = WERASE;
918 else {
919 if (!L_ECHO(tty)) {
920 raw_spin_lock_irqsave(&ldata->read_lock, flags);
921 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
922 (N_TTY_BUF_SIZE - 1));
923 ldata->read_head = ldata->canon_head;
924 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
925 return;
926 }
927 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
928 raw_spin_lock_irqsave(&ldata->read_lock, flags);
929 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
930 (N_TTY_BUF_SIZE - 1));
931 ldata->read_head = ldata->canon_head;
932 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
933 finish_erasing(ldata);
934 echo_char(KILL_CHAR(tty), tty);
935
936 if (L_ECHOK(tty))
937 echo_char_raw('\n', ldata);
938 return;
939 }
940 kill_type = KILL;
941 }
942
943 seen_alnums = 0;
944
945 while (ldata->read_head != ldata->canon_head) {
946 head = ldata->read_head;
947
948
949 do {
950 head = (head - 1) & (N_TTY_BUF_SIZE-1);
951 c = ldata->read_buf[head];
952 } while (is_continuation(c, tty) && head != ldata->canon_head);
953
954
955 if (is_continuation(c, tty))
956 break;
957
958 if (kill_type == WERASE) {
959
960 if (isalnum(c) || c == '_')
961 seen_alnums++;
962 else if (seen_alnums)
963 break;
964 }
965 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
966 raw_spin_lock_irqsave(&ldata->read_lock, flags);
967 ldata->read_head = head;
968 ldata->read_cnt -= cnt;
969 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
970 if (L_ECHO(tty)) {
971 if (L_ECHOPRT(tty)) {
972 if (!ldata->erasing) {
973 echo_char_raw('\\', ldata);
974 ldata->erasing = 1;
975 }
976
977 echo_char(c, tty);
978 while (--cnt > 0) {
979 head = (head+1) & (N_TTY_BUF_SIZE-1);
980 echo_char_raw(ldata->read_buf[head],
981 ldata);
982 echo_move_back_col(ldata);
983 }
984 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
985 echo_char(ERASE_CHAR(tty), tty);
986 } else if (c == '\t') {
987 unsigned int num_chars = 0;
988 int after_tab = 0;
989 unsigned long tail = ldata->read_head;
990
991
992
993
994
995
996
997
998 while (tail != ldata->canon_head) {
999 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
1000 c = ldata->read_buf[tail];
1001 if (c == '\t') {
1002 after_tab = 1;
1003 break;
1004 } else if (iscntrl(c)) {
1005 if (L_ECHOCTL(tty))
1006 num_chars += 2;
1007 } else if (!is_continuation(c, tty)) {
1008 num_chars++;
1009 }
1010 }
1011 echo_erase_tab(num_chars, after_tab, ldata);
1012 } else {
1013 if (iscntrl(c) && L_ECHOCTL(tty)) {
1014 echo_char_raw('\b', ldata);
1015 echo_char_raw(' ', ldata);
1016 echo_char_raw('\b', ldata);
1017 }
1018 if (!iscntrl(c) || L_ECHOCTL(tty)) {
1019 echo_char_raw('\b', ldata);
1020 echo_char_raw(' ', ldata);
1021 echo_char_raw('\b', ldata);
1022 }
1023 }
1024 }
1025 if (kill_type == ERASE)
1026 break;
1027 }
1028 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1029 finish_erasing(ldata);
1030}
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043static inline void isig(int sig, struct tty_struct *tty)
1044{
1045 struct pid *tty_pgrp = tty_get_pgrp(tty);
1046 if (tty_pgrp) {
1047 kill_pgrp(tty_pgrp, sig, 1);
1048 put_pid(tty_pgrp);
1049 }
1050}
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062static inline void n_tty_receive_break(struct tty_struct *tty)
1063{
1064 struct n_tty_data *ldata = tty->disc_data;
1065
1066 if (I_IGNBRK(tty))
1067 return;
1068 if (I_BRKINT(tty)) {
1069 isig(SIGINT, tty);
1070 if (!L_NOFLSH(tty)) {
1071 n_tty_flush_buffer(tty);
1072 tty_driver_flush_buffer(tty);
1073 }
1074 return;
1075 }
1076 if (I_PARMRK(tty)) {
1077 put_tty_queue('\377', ldata);
1078 put_tty_queue('\0', ldata);
1079 }
1080 put_tty_queue('\0', ldata);
1081 wake_up_interruptible(&tty->read_wait);
1082}
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097static inline void n_tty_receive_overrun(struct tty_struct *tty)
1098{
1099 struct n_tty_data *ldata = tty->disc_data;
1100 char buf[64];
1101
1102 ldata->num_overrun++;
1103 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1104 time_after(ldata->overrun_time, jiffies)) {
1105 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1106 tty_name(tty, buf),
1107 ldata->num_overrun);
1108 ldata->overrun_time = jiffies;
1109 ldata->num_overrun = 0;
1110 }
1111}
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1122 unsigned char c)
1123{
1124 struct n_tty_data *ldata = tty->disc_data;
1125
1126 if (I_IGNPAR(tty))
1127 return;
1128 if (I_PARMRK(tty)) {
1129 put_tty_queue('\377', ldata);
1130 put_tty_queue('\0', ldata);
1131 put_tty_queue(c, ldata);
1132 } else if (I_INPCK(tty))
1133 put_tty_queue('\0', ldata);
1134 else
1135 put_tty_queue(c, ldata);
1136 wake_up_interruptible(&tty->read_wait);
1137}
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1150{
1151 struct n_tty_data *ldata = tty->disc_data;
1152 unsigned long flags;
1153 int parmrk;
1154
1155 if (ldata->raw) {
1156 put_tty_queue(c, ldata);
1157 return;
1158 }
1159
1160 if (I_ISTRIP(tty))
1161 c &= 0x7f;
1162 if (I_IUCLC(tty) && L_IEXTEN(tty))
1163 c = tolower(c);
1164
1165 if (L_EXTPROC(tty)) {
1166 put_tty_queue(c, ldata);
1167 return;
1168 }
1169
1170 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1171 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1172 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1173 start_tty(tty);
1174 process_echoes(tty);
1175 }
1176
1177 if (tty->closing) {
1178 if (I_IXON(tty)) {
1179 if (c == START_CHAR(tty)) {
1180 start_tty(tty);
1181 process_echoes(tty);
1182 } else if (c == STOP_CHAR(tty))
1183 stop_tty(tty);
1184 }
1185 return;
1186 }
1187
1188
1189
1190
1191
1192
1193
1194 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
1195 ldata->lnext = 0;
1196 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1197 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1198
1199 if (L_ECHO(tty))
1200 process_output('\a', tty);
1201 return;
1202 }
1203 if (L_ECHO(tty)) {
1204 finish_erasing(ldata);
1205
1206 if (ldata->canon_head == ldata->read_head)
1207 echo_set_canon_col(ldata);
1208 echo_char(c, tty);
1209 process_echoes(tty);
1210 }
1211 if (parmrk)
1212 put_tty_queue(c, ldata);
1213 put_tty_queue(c, ldata);
1214 return;
1215 }
1216
1217 if (I_IXON(tty)) {
1218 if (c == START_CHAR(tty)) {
1219 start_tty(tty);
1220 process_echoes(tty);
1221 return;
1222 }
1223 if (c == STOP_CHAR(tty)) {
1224 stop_tty(tty);
1225 return;
1226 }
1227 }
1228
1229 if (L_ISIG(tty)) {
1230 int signal;
1231 signal = SIGINT;
1232 if (c == INTR_CHAR(tty))
1233 goto send_signal;
1234 signal = SIGQUIT;
1235 if (c == QUIT_CHAR(tty))
1236 goto send_signal;
1237 signal = SIGTSTP;
1238 if (c == SUSP_CHAR(tty)) {
1239send_signal:
1240 if (!L_NOFLSH(tty)) {
1241 n_tty_flush_buffer(tty);
1242 tty_driver_flush_buffer(tty);
1243 }
1244 if (I_IXON(tty))
1245 start_tty(tty);
1246 if (L_ECHO(tty)) {
1247 echo_char(c, tty);
1248 process_echoes(tty);
1249 }
1250 isig(signal, tty);
1251 return;
1252 }
1253 }
1254
1255 if (c == '\r') {
1256 if (I_IGNCR(tty))
1257 return;
1258 if (I_ICRNL(tty))
1259 c = '\n';
1260 } else if (c == '\n' && I_INLCR(tty))
1261 c = '\r';
1262
1263 if (ldata->icanon) {
1264 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1265 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1266 eraser(c, tty);
1267 process_echoes(tty);
1268 return;
1269 }
1270 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1271 ldata->lnext = 1;
1272 if (L_ECHO(tty)) {
1273 finish_erasing(ldata);
1274 if (L_ECHOCTL(tty)) {
1275 echo_char_raw('^', ldata);
1276 echo_char_raw('\b', ldata);
1277 process_echoes(tty);
1278 }
1279 }
1280 return;
1281 }
1282 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1283 L_IEXTEN(tty)) {
1284 unsigned long tail = ldata->canon_head;
1285
1286 finish_erasing(ldata);
1287 echo_char(c, tty);
1288 echo_char_raw('\n', ldata);
1289 while (tail != ldata->read_head) {
1290 echo_char(ldata->read_buf[tail], tty);
1291 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1292 }
1293 process_echoes(tty);
1294 return;
1295 }
1296 if (c == '\n') {
1297 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
1298 if (L_ECHO(tty))
1299 process_output('\a', tty);
1300 return;
1301 }
1302 if (L_ECHO(tty) || L_ECHONL(tty)) {
1303 echo_char_raw('\n', ldata);
1304 process_echoes(tty);
1305 }
1306 goto handle_newline;
1307 }
1308 if (c == EOF_CHAR(tty)) {
1309 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
1310 return;
1311 if (ldata->canon_head != ldata->read_head)
1312 set_bit(TTY_PUSH, &tty->flags);
1313 c = __DISABLED_CHAR;
1314 goto handle_newline;
1315 }
1316 if ((c == EOL_CHAR(tty)) ||
1317 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1318 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1319 ? 1 : 0;
1320 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1321 if (L_ECHO(tty))
1322 process_output('\a', tty);
1323 return;
1324 }
1325
1326
1327
1328 if (L_ECHO(tty)) {
1329
1330 if (ldata->canon_head == ldata->read_head)
1331 echo_set_canon_col(ldata);
1332 echo_char(c, tty);
1333 process_echoes(tty);
1334 }
1335
1336
1337
1338
1339 if (parmrk)
1340 put_tty_queue(c, ldata);
1341
1342handle_newline:
1343 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1344 set_bit(ldata->read_head, ldata->read_flags);
1345 put_tty_queue_nolock(c, ldata);
1346 ldata->canon_head = ldata->read_head;
1347 ldata->canon_data++;
1348 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1349 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1350 if (waitqueue_active(&tty->read_wait))
1351 wake_up_interruptible(&tty->read_wait);
1352 return;
1353 }
1354 }
1355
1356 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1357 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1358
1359 if (L_ECHO(tty))
1360 process_output('\a', tty);
1361 return;
1362 }
1363 if (L_ECHO(tty)) {
1364 finish_erasing(ldata);
1365 if (c == '\n')
1366 echo_char_raw('\n', ldata);
1367 else {
1368
1369 if (ldata->canon_head == ldata->read_head)
1370 echo_set_canon_col(ldata);
1371 echo_char(c, tty);
1372 }
1373 process_echoes(tty);
1374 }
1375
1376 if (parmrk)
1377 put_tty_queue(c, ldata);
1378
1379 put_tty_queue(c, ldata);
1380}
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392static void n_tty_write_wakeup(struct tty_struct *tty)
1393{
1394 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1395 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1396}
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1412 char *fp, int count)
1413{
1414 struct n_tty_data *ldata = tty->disc_data;
1415 const unsigned char *p;
1416 char *f, flags = TTY_NORMAL;
1417 int i;
1418 char buf[64];
1419 unsigned long cpuflags;
1420
1421 if (ldata->real_raw) {
1422 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
1423 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1424 N_TTY_BUF_SIZE - ldata->read_head);
1425 i = min(count, i);
1426 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1427 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1428 ldata->read_cnt += i;
1429 cp += i;
1430 count -= i;
1431
1432 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1433 N_TTY_BUF_SIZE - ldata->read_head);
1434 i = min(count, i);
1435 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1436 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1437 ldata->read_cnt += i;
1438 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
1439 } else {
1440 for (i = count, p = cp, f = fp; i; i--, p++) {
1441 if (f)
1442 flags = *f++;
1443 switch (flags) {
1444 case TTY_NORMAL:
1445 n_tty_receive_char(tty, *p);
1446 break;
1447 case TTY_BREAK:
1448 n_tty_receive_break(tty);
1449 break;
1450 case TTY_PARITY:
1451 case TTY_FRAME:
1452 n_tty_receive_parity_error(tty, *p);
1453 break;
1454 case TTY_OVERRUN:
1455 n_tty_receive_overrun(tty);
1456 break;
1457 default:
1458 printk(KERN_ERR "%s: unknown flag %d\n",
1459 tty_name(tty, buf), flags);
1460 break;
1461 }
1462 }
1463 if (tty->ops->flush_chars)
1464 tty->ops->flush_chars(tty);
1465 }
1466
1467 set_room(tty);
1468
1469 if ((!ldata->icanon && (ldata->read_cnt >= ldata->minimum_to_wake)) ||
1470 L_EXTPROC(tty)) {
1471 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1472 if (waitqueue_active(&tty->read_wait))
1473 wake_up_interruptible(&tty->read_wait);
1474 }
1475
1476
1477
1478
1479
1480
1481 while (1) {
1482 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1483 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1484 break;
1485 if (!tty_throttle_safe(tty))
1486 break;
1487 }
1488 __tty_set_flow_change(tty, 0);
1489}
1490
1491int is_ignored(int sig)
1492{
1493 return (sigismember(¤t->blocked, sig) ||
1494 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1495}
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1512{
1513 struct n_tty_data *ldata = tty->disc_data;
1514 int canon_change = 1;
1515
1516 if (old)
1517 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
1518 if (canon_change) {
1519 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1520 ldata->canon_head = ldata->read_tail;
1521 ldata->canon_data = 0;
1522 ldata->erasing = 0;
1523 }
1524
1525 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
1526 wake_up_interruptible(&tty->read_wait);
1527
1528 ldata->icanon = (L_ICANON(tty) != 0);
1529
1530 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1531 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1532 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1533 I_PARMRK(tty)) {
1534 bitmap_zero(ldata->process_char_map, 256);
1535
1536 if (I_IGNCR(tty) || I_ICRNL(tty))
1537 set_bit('\r', ldata->process_char_map);
1538 if (I_INLCR(tty))
1539 set_bit('\n', ldata->process_char_map);
1540
1541 if (L_ICANON(tty)) {
1542 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1543 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1544 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1545 set_bit('\n', ldata->process_char_map);
1546 set_bit(EOL_CHAR(tty), ldata->process_char_map);
1547 if (L_IEXTEN(tty)) {
1548 set_bit(WERASE_CHAR(tty),
1549 ldata->process_char_map);
1550 set_bit(LNEXT_CHAR(tty),
1551 ldata->process_char_map);
1552 set_bit(EOL2_CHAR(tty),
1553 ldata->process_char_map);
1554 if (L_ECHO(tty))
1555 set_bit(REPRINT_CHAR(tty),
1556 ldata->process_char_map);
1557 }
1558 }
1559 if (I_IXON(tty)) {
1560 set_bit(START_CHAR(tty), ldata->process_char_map);
1561 set_bit(STOP_CHAR(tty), ldata->process_char_map);
1562 }
1563 if (L_ISIG(tty)) {
1564 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1565 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1566 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
1567 }
1568 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
1569 ldata->raw = 0;
1570 ldata->real_raw = 0;
1571 } else {
1572 ldata->raw = 1;
1573 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1574 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1575 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1576 ldata->real_raw = 1;
1577 else
1578 ldata->real_raw = 0;
1579 }
1580 n_tty_set_room(tty);
1581
1582
1583
1584
1585 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1586 start_tty(tty);
1587 }
1588
1589
1590 wake_up_interruptible(&tty->write_wait);
1591 wake_up_interruptible(&tty->read_wait);
1592}
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604static void n_tty_close(struct tty_struct *tty)
1605{
1606 struct n_tty_data *ldata = tty->disc_data;
1607
1608 if (tty->link)
1609 n_tty_packet_mode_flush(tty);
1610
1611 kfree(ldata->read_buf);
1612 kfree(ldata->echo_buf);
1613 kfree(ldata);
1614 tty->disc_data = NULL;
1615}
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627static int n_tty_open(struct tty_struct *tty)
1628{
1629 struct n_tty_data *ldata;
1630
1631 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1632 if (!ldata)
1633 goto err;
1634
1635 ldata->overrun_time = jiffies;
1636 mutex_init(&ldata->atomic_read_lock);
1637 mutex_init(&ldata->output_lock);
1638 mutex_init(&ldata->echo_lock);
1639 raw_spin_lock_init(&ldata->read_lock);
1640
1641
1642 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1643 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1644 if (!ldata->read_buf || !ldata->echo_buf)
1645 goto err_free_bufs;
1646
1647 tty->disc_data = ldata;
1648 reset_buffer_flags(tty->disc_data);
1649 ldata->column = 0;
1650 ldata->minimum_to_wake = 1;
1651 tty->closing = 0;
1652
1653 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1654 n_tty_set_termios(tty, NULL);
1655 tty_unthrottle(tty);
1656
1657 return 0;
1658err_free_bufs:
1659 kfree(ldata->read_buf);
1660 kfree(ldata->echo_buf);
1661 kfree(ldata);
1662err:
1663 return -ENOMEM;
1664}
1665
1666static inline int input_available_p(struct tty_struct *tty, int amt)
1667{
1668 struct n_tty_data *ldata = tty->disc_data;
1669
1670 tty_flush_to_ldisc(tty);
1671 if (ldata->icanon && !L_EXTPROC(tty)) {
1672 if (ldata->canon_data)
1673 return 1;
1674 } else if (ldata->read_cnt >= (amt ? amt : 1))
1675 return 1;
1676
1677 return 0;
1678}
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697static int copy_from_read_buf(struct tty_struct *tty,
1698 unsigned char __user **b,
1699 size_t *nr)
1700
1701{
1702 struct n_tty_data *ldata = tty->disc_data;
1703 int retval;
1704 size_t n;
1705 unsigned long flags;
1706 bool is_eof;
1707
1708 retval = 0;
1709 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1710 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
1711 n = min(*nr, n);
1712 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1713 if (n) {
1714 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
1715 n -= retval;
1716 is_eof = n == 1 &&
1717 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1718 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
1719 ldata->icanon);
1720 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1721 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1722 ldata->read_cnt -= n;
1723
1724 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
1725 n = 0;
1726 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1727 *b += n;
1728 *nr -= n;
1729 }
1730 return retval;
1731}
1732
1733extern ssize_t redirected_tty_write(struct file *, const char __user *,
1734 size_t, loff_t *);
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750static int job_control(struct tty_struct *tty, struct file *file)
1751{
1752
1753
1754
1755
1756
1757 if (file->f_op->write == redirected_tty_write ||
1758 current->signal->tty != tty)
1759 return 0;
1760
1761 spin_lock_irq(&tty->ctrl_lock);
1762 if (!tty->pgrp)
1763 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1764 else if (task_pgrp(current) != tty->pgrp) {
1765 spin_unlock_irq(&tty->ctrl_lock);
1766 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1767 return -EIO;
1768 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1769 set_thread_flag(TIF_SIGPENDING);
1770 return -ERESTARTSYS;
1771 }
1772 spin_unlock_irq(&tty->ctrl_lock);
1773 return 0;
1774}
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1793 unsigned char __user *buf, size_t nr)
1794{
1795 struct n_tty_data *ldata = tty->disc_data;
1796 unsigned char __user *b = buf;
1797 DECLARE_WAITQUEUE(wait, current);
1798 int c;
1799 int minimum, time;
1800 ssize_t retval = 0;
1801 ssize_t size;
1802 long timeout;
1803 unsigned long flags;
1804 int packet;
1805
1806do_it_again:
1807 c = job_control(tty, file);
1808 if (c < 0)
1809 return c;
1810
1811 minimum = time = 0;
1812 timeout = MAX_SCHEDULE_TIMEOUT;
1813 if (!ldata->icanon) {
1814 minimum = MIN_CHAR(tty);
1815 if (minimum) {
1816 time = (HZ / 10) * TIME_CHAR(tty);
1817 if (time)
1818 ldata->minimum_to_wake = 1;
1819 else if (!waitqueue_active(&tty->read_wait) ||
1820 (ldata->minimum_to_wake > minimum))
1821 ldata->minimum_to_wake = minimum;
1822 } else {
1823 timeout = (HZ / 10) * TIME_CHAR(tty);
1824 ldata->minimum_to_wake = minimum = 1;
1825 }
1826 }
1827
1828
1829
1830
1831 if (file->f_flags & O_NONBLOCK) {
1832 if (!mutex_trylock(&ldata->atomic_read_lock))
1833 return -EAGAIN;
1834 } else {
1835 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
1836 return -ERESTARTSYS;
1837 }
1838 packet = tty->packet;
1839
1840 add_wait_queue(&tty->read_wait, &wait);
1841 while (nr) {
1842
1843 if (packet && tty->link->ctrl_status) {
1844 unsigned char cs;
1845 if (b != buf)
1846 break;
1847 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1848 cs = tty->link->ctrl_status;
1849 tty->link->ctrl_status = 0;
1850 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1851 if (tty_put_user(tty, cs, b++)) {
1852 retval = -EFAULT;
1853 b--;
1854 break;
1855 }
1856 nr--;
1857 break;
1858 }
1859
1860
1861
1862 set_current_state(TASK_INTERRUPTIBLE);
1863
1864 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
1865 ((minimum - (b - buf)) >= 1))
1866 ldata->minimum_to_wake = (minimum - (b - buf));
1867
1868 if (!input_available_p(tty, 0)) {
1869 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1870 retval = -EIO;
1871 break;
1872 }
1873 if (tty_hung_up_p(file))
1874 break;
1875 if (!timeout)
1876 break;
1877 if (file->f_flags & O_NONBLOCK) {
1878 retval = -EAGAIN;
1879 break;
1880 }
1881 if (signal_pending(current)) {
1882 retval = -ERESTARTSYS;
1883 break;
1884 }
1885 n_tty_set_room(tty);
1886 timeout = schedule_timeout(timeout);
1887 continue;
1888 }
1889 __set_current_state(TASK_RUNNING);
1890
1891
1892 if (packet && b == buf) {
1893 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1894 retval = -EFAULT;
1895 b--;
1896 break;
1897 }
1898 nr--;
1899 }
1900
1901 if (ldata->icanon && !L_EXTPROC(tty)) {
1902
1903 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1904 while (nr && ldata->read_cnt) {
1905 int eol;
1906
1907 eol = test_and_clear_bit(ldata->read_tail,
1908 ldata->read_flags);
1909 c = ldata->read_buf[ldata->read_tail];
1910 ldata->read_tail = ((ldata->read_tail+1) &
1911 (N_TTY_BUF_SIZE-1));
1912 ldata->read_cnt--;
1913 if (eol) {
1914
1915
1916
1917
1918 if (--ldata->canon_data < 0)
1919 ldata->canon_data = 0;
1920 }
1921 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1922
1923 if (!eol || (c != __DISABLED_CHAR)) {
1924 if (tty_put_user(tty, c, b++)) {
1925 retval = -EFAULT;
1926 b--;
1927 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1928 break;
1929 }
1930 nr--;
1931 }
1932 if (eol) {
1933 tty_audit_push(tty);
1934 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1935 break;
1936 }
1937 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1938 }
1939 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1940 if (retval)
1941 break;
1942 } else {
1943 int uncopied;
1944
1945
1946 uncopied = copy_from_read_buf(tty, &b, &nr);
1947 uncopied += copy_from_read_buf(tty, &b, &nr);
1948 if (uncopied) {
1949 retval = -EFAULT;
1950 break;
1951 }
1952 }
1953
1954
1955
1956
1957
1958
1959
1960
1961 while (1) {
1962 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1963 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1964 break;
1965 if (!tty->count)
1966 break;
1967 n_tty_set_room(tty);
1968 if (!tty_unthrottle_safe(tty))
1969 break;
1970 }
1971 __tty_set_flow_change(tty, 0);
1972
1973 if (b - buf >= minimum)
1974 break;
1975 if (time)
1976 timeout = time;
1977 }
1978 mutex_unlock(&ldata->atomic_read_lock);
1979 remove_wait_queue(&tty->read_wait, &wait);
1980
1981 if (!waitqueue_active(&tty->read_wait))
1982 ldata->minimum_to_wake = minimum;
1983
1984 __set_current_state(TASK_RUNNING);
1985 size = b - buf;
1986 if (size) {
1987 retval = size;
1988 if (nr)
1989 clear_bit(TTY_PUSH, &tty->flags);
1990 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1991 goto do_it_again;
1992
1993 n_tty_set_room(tty);
1994 return retval;
1995}
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2020 const unsigned char *buf, size_t nr)
2021{
2022 const unsigned char *b = buf;
2023 DECLARE_WAITQUEUE(wait, current);
2024 int c;
2025 ssize_t retval = 0;
2026
2027
2028 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2029 retval = tty_check_change(tty);
2030 if (retval)
2031 return retval;
2032 }
2033
2034
2035 process_echoes(tty);
2036
2037 add_wait_queue(&tty->write_wait, &wait);
2038 while (1) {
2039 set_current_state(TASK_INTERRUPTIBLE);
2040 if (signal_pending(current)) {
2041 retval = -ERESTARTSYS;
2042 break;
2043 }
2044 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2045 retval = -EIO;
2046 break;
2047 }
2048 if (O_OPOST(tty)) {
2049 while (nr > 0) {
2050 ssize_t num = process_output_block(tty, b, nr);
2051 if (num < 0) {
2052 if (num == -EAGAIN)
2053 break;
2054 retval = num;
2055 goto break_out;
2056 }
2057 b += num;
2058 nr -= num;
2059 if (nr == 0)
2060 break;
2061 c = *b;
2062 if (process_output(c, tty) < 0)
2063 break;
2064 b++; nr--;
2065 }
2066 if (tty->ops->flush_chars)
2067 tty->ops->flush_chars(tty);
2068 } else {
2069 while (nr > 0) {
2070 c = tty->ops->write(tty, b, nr);
2071 if (c < 0) {
2072 retval = c;
2073 goto break_out;
2074 }
2075 if (!c)
2076 break;
2077 b += c;
2078 nr -= c;
2079 }
2080 }
2081 if (!nr)
2082 break;
2083 if (file->f_flags & O_NONBLOCK) {
2084 retval = -EAGAIN;
2085 break;
2086 }
2087 schedule();
2088 }
2089break_out:
2090 __set_current_state(TASK_RUNNING);
2091 remove_wait_queue(&tty->write_wait, &wait);
2092 if (b - buf != nr && tty->fasync)
2093 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2094 return (b - buf) ? b - buf : retval;
2095}
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2112 poll_table *wait)
2113{
2114 struct n_tty_data *ldata = tty->disc_data;
2115 unsigned int mask = 0;
2116
2117 poll_wait(file, &tty->read_wait, wait);
2118 poll_wait(file, &tty->write_wait, wait);
2119 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2120 mask |= POLLIN | POLLRDNORM;
2121 if (tty->packet && tty->link->ctrl_status)
2122 mask |= POLLPRI | POLLIN | POLLRDNORM;
2123 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2124 mask |= POLLHUP;
2125 if (tty_hung_up_p(file))
2126 mask |= POLLHUP;
2127 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2128 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2129 ldata->minimum_to_wake = MIN_CHAR(tty);
2130 else
2131 ldata->minimum_to_wake = 1;
2132 }
2133 if (tty->ops->write && !tty_is_writelocked(tty) &&
2134 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2135 tty_write_room(tty) > 0)
2136 mask |= POLLOUT | POLLWRNORM;
2137 return mask;
2138}
2139
2140static unsigned long inq_canon(struct n_tty_data *ldata)
2141{
2142 int nr, head, tail;
2143
2144 if (!ldata->canon_data)
2145 return 0;
2146 head = ldata->canon_head;
2147 tail = ldata->read_tail;
2148 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2149
2150 while (head != tail) {
2151 if (test_bit(tail, ldata->read_flags) &&
2152 ldata->read_buf[tail] == __DISABLED_CHAR)
2153 nr--;
2154 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2155 }
2156 return nr;
2157}
2158
2159static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2160 unsigned int cmd, unsigned long arg)
2161{
2162 struct n_tty_data *ldata = tty->disc_data;
2163 int retval;
2164
2165 switch (cmd) {
2166 case TIOCOUTQ:
2167 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2168 case TIOCINQ:
2169
2170 retval = ldata->read_cnt;
2171 if (L_ICANON(tty))
2172 retval = inq_canon(ldata);
2173 return put_user(retval, (unsigned int __user *) arg);
2174 default:
2175 return n_tty_ioctl_helper(tty, file, cmd, arg);
2176 }
2177}
2178
2179static void n_tty_fasync(struct tty_struct *tty, int on)
2180{
2181 struct n_tty_data *ldata = tty->disc_data;
2182
2183 if (!waitqueue_active(&tty->read_wait)) {
2184 if (on)
2185 ldata->minimum_to_wake = 1;
2186 else if (!tty->fasync)
2187 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2188 }
2189}
2190
2191struct tty_ldisc_ops tty_ldisc_N_TTY = {
2192 .magic = TTY_LDISC_MAGIC,
2193 .name = "n_tty",
2194 .open = n_tty_open,
2195 .close = n_tty_close,
2196 .flush_buffer = n_tty_flush_buffer,
2197 .chars_in_buffer = n_tty_chars_in_buffer,
2198 .read = n_tty_read,
2199 .write = n_tty_write,
2200 .ioctl = n_tty_ioctl,
2201 .set_termios = n_tty_set_termios,
2202 .poll = n_tty_poll,
2203 .receive_buf = n_tty_receive_buf,
2204 .write_wakeup = n_tty_write_wakeup,
2205 .fasync = n_tty_fasync,
2206};
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2217{
2218 *ops = tty_ldisc_N_TTY;
2219 ops->owner = NULL;
2220 ops->refcount = ops->flags = 0;
2221}
2222EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
2223