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