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