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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67#include <linux/types.h>
68#include <linux/major.h>
69#include <linux/errno.h>
70#include <linux/signal.h>
71#include <linux/fcntl.h>
72#include <linux/sched.h>
73#include <linux/interrupt.h>
74#include <linux/tty.h>
75#include <linux/tty_driver.h>
76#include <linux/tty_flip.h>
77#include <linux/devpts_fs.h>
78#include <linux/file.h>
79#include <linux/fdtable.h>
80#include <linux/console.h>
81#include <linux/timer.h>
82#include <linux/ctype.h>
83#include <linux/kd.h>
84#include <linux/mm.h>
85#include <linux/string.h>
86#include <linux/slab.h>
87#include <linux/poll.h>
88#include <linux/proc_fs.h>
89#include <linux/init.h>
90#include <linux/module.h>
91#include <linux/device.h>
92#include <linux/wait.h>
93#include <linux/bitops.h>
94#include <linux/delay.h>
95#include <linux/seq_file.h>
96#include <linux/serial.h>
97#include <linux/ratelimit.h>
98
99#include <linux/uaccess.h>
100
101#include <linux/kbd_kern.h>
102#include <linux/vt_kern.h>
103#include <linux/selection.h>
104
105#include <linux/kmod.h>
106#include <linux/nsproxy.h>
107
108#undef TTY_DEBUG_HANGUP
109
110#define TTY_PARANOIA_CHECK 1
111#define CHECK_TTY_COUNT 1
112
113struct ktermios tty_std_termios = {
114 .c_iflag = ICRNL | IXON,
115 .c_oflag = OPOST | ONLCR,
116 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
117 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
118 ECHOCTL | ECHOKE | IEXTEN,
119 .c_cc = INIT_C_CC,
120 .c_ispeed = 38400,
121 .c_ospeed = 38400
122};
123
124EXPORT_SYMBOL(tty_std_termios);
125
126
127
128
129
130LIST_HEAD(tty_drivers);
131
132
133
134DEFINE_MUTEX(tty_mutex);
135EXPORT_SYMBOL(tty_mutex);
136
137
138DEFINE_SPINLOCK(tty_files_lock);
139
140static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
141static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
142ssize_t redirected_tty_write(struct file *, const char __user *,
143 size_t, loff_t *);
144static unsigned int tty_poll(struct file *, poll_table *);
145static int tty_open(struct inode *, struct file *);
146long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
147#ifdef CONFIG_COMPAT
148static long tty_compat_ioctl(struct file *file, unsigned int cmd,
149 unsigned long arg);
150#else
151#define tty_compat_ioctl NULL
152#endif
153static int __tty_fasync(int fd, struct file *filp, int on);
154static int tty_fasync(int fd, struct file *filp, int on);
155static void release_tty(struct tty_struct *tty, int idx);
156
157
158
159
160
161
162
163
164
165
166void free_tty_struct(struct tty_struct *tty)
167{
168 if (!tty)
169 return;
170 put_device(tty->dev);
171 kfree(tty->write_buf);
172 tty->magic = 0xDEADDEAD;
173 kfree(tty);
174}
175
176static inline struct tty_struct *file_tty(struct file *file)
177{
178 return ((struct tty_file_private *)file->private_data)->tty;
179}
180
181int tty_alloc_file(struct file *file)
182{
183 struct tty_file_private *priv;
184
185 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
186 if (!priv)
187 return -ENOMEM;
188
189 file->private_data = priv;
190
191 return 0;
192}
193
194
195void tty_add_file(struct tty_struct *tty, struct file *file)
196{
197 struct tty_file_private *priv = file->private_data;
198
199 priv->tty = tty;
200 priv->file = file;
201
202 spin_lock(&tty_files_lock);
203 list_add(&priv->list, &tty->tty_files);
204 spin_unlock(&tty_files_lock);
205}
206
207
208
209
210
211
212
213void tty_free_file(struct file *file)
214{
215 struct tty_file_private *priv = file->private_data;
216
217 file->private_data = NULL;
218 kfree(priv);
219}
220
221
222static void tty_del_file(struct file *file)
223{
224 struct tty_file_private *priv = file->private_data;
225
226 spin_lock(&tty_files_lock);
227 list_del(&priv->list);
228 spin_unlock(&tty_files_lock);
229 tty_free_file(file);
230}
231
232
233#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
234
235
236
237
238
239
240
241
242
243
244
245
246char *tty_name(struct tty_struct *tty, char *buf)
247{
248 if (!tty)
249 strcpy(buf, "NULL tty");
250 else
251 strcpy(buf, tty->name);
252 return buf;
253}
254
255EXPORT_SYMBOL(tty_name);
256
257int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
258 const char *routine)
259{
260#ifdef TTY_PARANOIA_CHECK
261 if (!tty) {
262 printk(KERN_WARNING
263 "null TTY for (%d:%d) in %s\n",
264 imajor(inode), iminor(inode), routine);
265 return 1;
266 }
267 if (tty->magic != TTY_MAGIC) {
268 printk(KERN_WARNING
269 "bad magic number for tty struct (%d:%d) in %s\n",
270 imajor(inode), iminor(inode), routine);
271 return 1;
272 }
273#endif
274 return 0;
275}
276
277
278static int check_tty_count(struct tty_struct *tty, const char *routine)
279{
280#ifdef CHECK_TTY_COUNT
281 struct list_head *p;
282 int count = 0;
283
284 spin_lock(&tty_files_lock);
285 list_for_each(p, &tty->tty_files) {
286 count++;
287 }
288 spin_unlock(&tty_files_lock);
289 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
290 tty->driver->subtype == PTY_TYPE_SLAVE &&
291 tty->link && tty->link->count)
292 count++;
293 if (tty->count != count) {
294 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
295 "!= #fd's(%d) in %s\n",
296 tty->name, tty->count, count, routine);
297 return count;
298 }
299#endif
300 return 0;
301}
302
303
304
305
306
307
308
309
310
311
312
313
314static struct tty_driver *get_tty_driver(dev_t device, int *index)
315{
316 struct tty_driver *p;
317
318 list_for_each_entry(p, &tty_drivers, tty_drivers) {
319 dev_t base = MKDEV(p->major, p->minor_start);
320 if (device < base || device >= base + p->num)
321 continue;
322 *index = device - base;
323 return tty_driver_kref_get(p);
324 }
325 return NULL;
326}
327
328#ifdef CONFIG_CONSOLE_POLL
329
330
331
332
333
334
335
336
337
338
339struct tty_driver *tty_find_polling_driver(char *name, int *line)
340{
341 struct tty_driver *p, *res = NULL;
342 int tty_line = 0;
343 int len;
344 char *str, *stp;
345
346 for (str = name; *str; str++)
347 if ((*str >= '0' && *str <= '9') || *str == ',')
348 break;
349 if (!*str)
350 return NULL;
351
352 len = str - name;
353 tty_line = simple_strtoul(str, &str, 10);
354
355 mutex_lock(&tty_mutex);
356
357 list_for_each_entry(p, &tty_drivers, tty_drivers) {
358 if (strncmp(name, p->name, len) != 0)
359 continue;
360 stp = str;
361 if (*stp == ',')
362 stp++;
363 if (*stp == '\0')
364 stp = NULL;
365
366 if (tty_line >= 0 && tty_line < p->num && p->ops &&
367 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
368 res = tty_driver_kref_get(p);
369 *line = tty_line;
370 break;
371 }
372 }
373 mutex_unlock(&tty_mutex);
374
375 return res;
376}
377EXPORT_SYMBOL_GPL(tty_find_polling_driver);
378#endif
379
380
381
382
383
384
385
386
387
388
389
390
391int tty_check_change(struct tty_struct *tty)
392{
393 unsigned long flags;
394 int ret = 0;
395
396 if (current->signal->tty != tty)
397 return 0;
398
399 spin_lock_irqsave(&tty->ctrl_lock, flags);
400
401 if (!tty->pgrp) {
402 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
403 goto out_unlock;
404 }
405 if (task_pgrp(current) == tty->pgrp)
406 goto out_unlock;
407 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
408 if (is_ignored(SIGTTOU))
409 goto out;
410 if (is_current_pgrp_orphaned()) {
411 ret = -EIO;
412 goto out;
413 }
414 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
415 set_thread_flag(TIF_SIGPENDING);
416 ret = -ERESTARTSYS;
417out:
418 return ret;
419out_unlock:
420 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
421 return ret;
422}
423
424EXPORT_SYMBOL(tty_check_change);
425
426static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
427 size_t count, loff_t *ppos)
428{
429 return 0;
430}
431
432static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
433 size_t count, loff_t *ppos)
434{
435 return -EIO;
436}
437
438
439static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
440{
441 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
442}
443
444static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
445 unsigned long arg)
446{
447 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
448}
449
450static long hung_up_tty_compat_ioctl(struct file *file,
451 unsigned int cmd, unsigned long arg)
452{
453 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
454}
455
456static const struct file_operations tty_fops = {
457 .llseek = no_llseek,
458 .read = tty_read,
459 .write = tty_write,
460 .poll = tty_poll,
461 .unlocked_ioctl = tty_ioctl,
462 .compat_ioctl = tty_compat_ioctl,
463 .open = tty_open,
464 .release = tty_release,
465 .fasync = tty_fasync,
466};
467
468static const struct file_operations console_fops = {
469 .llseek = no_llseek,
470 .read = tty_read,
471 .write = redirected_tty_write,
472 .poll = tty_poll,
473 .unlocked_ioctl = tty_ioctl,
474 .compat_ioctl = tty_compat_ioctl,
475 .open = tty_open,
476 .release = tty_release,
477 .fasync = tty_fasync,
478};
479
480static const struct file_operations hung_up_tty_fops = {
481 .llseek = no_llseek,
482 .read = hung_up_tty_read,
483 .write = hung_up_tty_write,
484 .poll = hung_up_tty_poll,
485 .unlocked_ioctl = hung_up_tty_ioctl,
486 .compat_ioctl = hung_up_tty_compat_ioctl,
487 .release = tty_release,
488};
489
490static DEFINE_SPINLOCK(redirect_lock);
491static struct file *redirect;
492
493
494void proc_clear_tty(struct task_struct *p)
495{
496 unsigned long flags;
497 struct tty_struct *tty;
498 spin_lock_irqsave(&p->sighand->siglock, flags);
499 tty = p->signal->tty;
500 p->signal->tty = NULL;
501 spin_unlock_irqrestore(&p->sighand->siglock, flags);
502 tty_kref_put(tty);
503}
504
505
506
507
508
509
510
511
512
513
514
515static void __proc_set_tty(struct tty_struct *tty)
516{
517 unsigned long flags;
518
519 spin_lock_irqsave(&tty->ctrl_lock, flags);
520
521
522
523
524 put_pid(tty->session);
525 put_pid(tty->pgrp);
526 tty->pgrp = get_pid(task_pgrp(current));
527 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
528 tty->session = get_pid(task_session(current));
529 if (current->signal->tty) {
530 printk(KERN_DEBUG "tty not NULL!!\n");
531 tty_kref_put(current->signal->tty);
532 }
533 put_pid(current->signal->tty_old_pgrp);
534 current->signal->tty = tty_kref_get(tty);
535 current->signal->tty_old_pgrp = NULL;
536}
537
538static void proc_set_tty(struct tty_struct *tty)
539{
540 spin_lock_irq(¤t->sighand->siglock);
541 __proc_set_tty(tty);
542 spin_unlock_irq(¤t->sighand->siglock);
543}
544
545struct tty_struct *get_current_tty(void)
546{
547 struct tty_struct *tty;
548 unsigned long flags;
549
550 spin_lock_irqsave(¤t->sighand->siglock, flags);
551 tty = tty_kref_get(current->signal->tty);
552 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
553 return tty;
554}
555EXPORT_SYMBOL_GPL(get_current_tty);
556
557static void session_clear_tty(struct pid *session)
558{
559 struct task_struct *p;
560 do_each_pid_task(session, PIDTYPE_SID, p) {
561 proc_clear_tty(p);
562 } while_each_pid_task(session, PIDTYPE_SID, p);
563}
564
565
566
567
568
569
570
571
572
573
574void tty_wakeup(struct tty_struct *tty)
575{
576 struct tty_ldisc *ld;
577
578 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
579 ld = tty_ldisc_ref(tty);
580 if (ld) {
581 if (ld->ops->write_wakeup)
582 ld->ops->write_wakeup(tty);
583 tty_ldisc_deref(ld);
584 }
585 }
586 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
587}
588
589EXPORT_SYMBOL_GPL(tty_wakeup);
590
591
592
593
594
595
596
597
598
599
600
601
602
603static int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
604{
605 struct task_struct *p;
606 int refs = 0;
607 struct pid *tty_pgrp = NULL;
608
609 read_lock(&tasklist_lock);
610 if (tty->session) {
611 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
612 spin_lock_irq(&p->sighand->siglock);
613 if (p->signal->tty == tty) {
614 p->signal->tty = NULL;
615
616
617 refs++;
618 }
619 if (!p->signal->leader) {
620 spin_unlock_irq(&p->sighand->siglock);
621 continue;
622 }
623 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
624 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
625 put_pid(p->signal->tty_old_pgrp);
626 spin_lock(&tty->ctrl_lock);
627 tty_pgrp = get_pid(tty->pgrp);
628 if (tty->pgrp)
629 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
630 spin_unlock(&tty->ctrl_lock);
631 spin_unlock_irq(&p->sighand->siglock);
632 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
633 }
634 read_unlock(&tasklist_lock);
635
636 if (tty_pgrp) {
637 if (exit_session)
638 kill_pgrp(tty_pgrp, SIGHUP, exit_session);
639 put_pid(tty_pgrp);
640 }
641
642 return refs;
643}
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667static void __tty_hangup(struct tty_struct *tty, int exit_session)
668{
669 struct file *cons_filp = NULL;
670 struct file *filp, *f = NULL;
671 struct tty_file_private *priv;
672 int closecount = 0, n;
673 int refs;
674
675 if (!tty)
676 return;
677
678
679 spin_lock(&redirect_lock);
680 if (redirect && file_tty(redirect) == tty) {
681 f = redirect;
682 redirect = NULL;
683 }
684 spin_unlock(&redirect_lock);
685
686 tty_lock(tty);
687
688 if (test_bit(TTY_HUPPED, &tty->flags)) {
689 tty_unlock(tty);
690 return;
691 }
692
693
694
695
696 check_tty_count(tty, "tty_hangup");
697
698 spin_lock(&tty_files_lock);
699
700 list_for_each_entry(priv, &tty->tty_files, list) {
701 filp = priv->file;
702 if (filp->f_op->write == redirected_tty_write)
703 cons_filp = filp;
704 if (filp->f_op->write != tty_write)
705 continue;
706 closecount++;
707 __tty_fasync(-1, filp, 0);
708 filp->f_op = &hung_up_tty_fops;
709 }
710 spin_unlock(&tty_files_lock);
711
712 refs = tty_signal_session_leader(tty, exit_session);
713
714 while (refs--)
715 tty_kref_put(tty);
716
717 tty_ldisc_hangup(tty);
718
719 spin_lock_irq(&tty->ctrl_lock);
720 clear_bit(TTY_THROTTLED, &tty->flags);
721 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
722 put_pid(tty->session);
723 put_pid(tty->pgrp);
724 tty->session = NULL;
725 tty->pgrp = NULL;
726 tty->ctrl_status = 0;
727 spin_unlock_irq(&tty->ctrl_lock);
728
729
730
731
732
733
734
735 if (cons_filp) {
736 if (tty->ops->close)
737 for (n = 0; n < closecount; n++)
738 tty->ops->close(tty, cons_filp);
739 } else if (tty->ops->hangup)
740 tty->ops->hangup(tty);
741
742
743
744
745
746
747 set_bit(TTY_HUPPED, &tty->flags);
748 tty_unlock(tty);
749
750 if (f)
751 fput(f);
752}
753
754static void do_tty_hangup(struct work_struct *work)
755{
756 struct tty_struct *tty =
757 container_of(work, struct tty_struct, hangup_work);
758
759 __tty_hangup(tty, 0);
760}
761
762
763
764
765
766
767
768
769
770void tty_hangup(struct tty_struct *tty)
771{
772#ifdef TTY_DEBUG_HANGUP
773 char buf[64];
774 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
775#endif
776 schedule_work(&tty->hangup_work);
777}
778
779EXPORT_SYMBOL(tty_hangup);
780
781
782
783
784
785
786
787
788
789
790void tty_vhangup(struct tty_struct *tty)
791{
792#ifdef TTY_DEBUG_HANGUP
793 char buf[64];
794
795 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
796#endif
797 __tty_hangup(tty, 0);
798}
799
800EXPORT_SYMBOL(tty_vhangup);
801
802
803
804
805
806
807
808
809void tty_vhangup_self(void)
810{
811 struct tty_struct *tty;
812
813 tty = get_current_tty();
814 if (tty) {
815 tty_vhangup(tty);
816 tty_kref_put(tty);
817 }
818}
819
820
821
822
823
824
825
826
827
828
829
830
831static void tty_vhangup_session(struct tty_struct *tty)
832{
833#ifdef TTY_DEBUG_HANGUP
834 char buf[64];
835
836 printk(KERN_DEBUG "%s vhangup session...\n", tty_name(tty, buf));
837#endif
838 __tty_hangup(tty, 1);
839}
840
841
842
843
844
845
846
847
848
849int tty_hung_up_p(struct file *filp)
850{
851 return (filp->f_op == &hung_up_tty_fops);
852}
853
854EXPORT_SYMBOL(tty_hung_up_p);
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881void disassociate_ctty(int on_exit)
882{
883 struct tty_struct *tty;
884
885 if (!current->signal->leader)
886 return;
887
888 tty = get_current_tty();
889 if (tty) {
890 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
891 tty_vhangup_session(tty);
892 } else {
893 struct pid *tty_pgrp = tty_get_pgrp(tty);
894 if (tty_pgrp) {
895 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
896 if (!on_exit)
897 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
898 put_pid(tty_pgrp);
899 }
900 }
901 tty_kref_put(tty);
902
903 } else if (on_exit) {
904 struct pid *old_pgrp;
905 spin_lock_irq(¤t->sighand->siglock);
906 old_pgrp = current->signal->tty_old_pgrp;
907 current->signal->tty_old_pgrp = NULL;
908 spin_unlock_irq(¤t->sighand->siglock);
909 if (old_pgrp) {
910 kill_pgrp(old_pgrp, SIGHUP, on_exit);
911 kill_pgrp(old_pgrp, SIGCONT, on_exit);
912 put_pid(old_pgrp);
913 }
914 return;
915 }
916
917 spin_lock_irq(¤t->sighand->siglock);
918 put_pid(current->signal->tty_old_pgrp);
919 current->signal->tty_old_pgrp = NULL;
920
921 tty = tty_kref_get(current->signal->tty);
922 if (tty) {
923 unsigned long flags;
924 spin_lock_irqsave(&tty->ctrl_lock, flags);
925 put_pid(tty->session);
926 put_pid(tty->pgrp);
927 tty->session = NULL;
928 tty->pgrp = NULL;
929 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
930 tty_kref_put(tty);
931 } else {
932#ifdef TTY_DEBUG_HANGUP
933 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
934 " = NULL", tty);
935#endif
936 }
937
938 spin_unlock_irq(¤t->sighand->siglock);
939
940 read_lock(&tasklist_lock);
941 session_clear_tty(task_session(current));
942 read_unlock(&tasklist_lock);
943}
944
945
946
947
948
949void no_tty(void)
950{
951
952
953
954 struct task_struct *tsk = current;
955 disassociate_ctty(0);
956 proc_clear_tty(tsk);
957}
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977void __stop_tty(struct tty_struct *tty)
978{
979 if (tty->stopped)
980 return;
981 tty->stopped = 1;
982 if (tty->ops->stop)
983 tty->ops->stop(tty);
984}
985
986void stop_tty(struct tty_struct *tty)
987{
988 unsigned long flags;
989
990 spin_lock_irqsave(&tty->flow_lock, flags);
991 __stop_tty(tty);
992 spin_unlock_irqrestore(&tty->flow_lock, flags);
993}
994EXPORT_SYMBOL(stop_tty);
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008void __start_tty(struct tty_struct *tty)
1009{
1010 if (!tty->stopped || tty->flow_stopped)
1011 return;
1012 tty->stopped = 0;
1013 if (tty->ops->start)
1014 tty->ops->start(tty);
1015 tty_wakeup(tty);
1016}
1017
1018void start_tty(struct tty_struct *tty)
1019{
1020 unsigned long flags;
1021
1022 spin_lock_irqsave(&tty->flow_lock, flags);
1023 __start_tty(tty);
1024 spin_unlock_irqrestore(&tty->flow_lock, flags);
1025}
1026EXPORT_SYMBOL(start_tty);
1027
1028static void tty_update_time(struct timespec *time)
1029{
1030 unsigned long sec = get_seconds();
1031
1032
1033
1034
1035
1036
1037
1038 if ((sec ^ time->tv_sec) & ~7)
1039 time->tv_sec = sec;
1040}
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
1058 loff_t *ppos)
1059{
1060 int i;
1061 struct inode *inode = file_inode(file);
1062 struct tty_struct *tty = file_tty(file);
1063 struct tty_ldisc *ld;
1064
1065 if (tty_paranoia_check(tty, inode, "tty_read"))
1066 return -EIO;
1067 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1068 return -EIO;
1069
1070
1071
1072 ld = tty_ldisc_ref_wait(tty);
1073 if (ld->ops->read)
1074 i = ld->ops->read(tty, file, buf, count);
1075 else
1076 i = -EIO;
1077 tty_ldisc_deref(ld);
1078
1079 if (i > 0)
1080 tty_update_time(&inode->i_atime);
1081
1082 return i;
1083}
1084
1085static void tty_write_unlock(struct tty_struct *tty)
1086{
1087 mutex_unlock(&tty->atomic_write_lock);
1088 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
1089}
1090
1091static int tty_write_lock(struct tty_struct *tty, int ndelay)
1092{
1093 if (!mutex_trylock(&tty->atomic_write_lock)) {
1094 if (ndelay)
1095 return -EAGAIN;
1096 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1097 return -ERESTARTSYS;
1098 }
1099 return 0;
1100}
1101
1102
1103
1104
1105
1106static inline ssize_t do_tty_write(
1107 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1108 struct tty_struct *tty,
1109 struct file *file,
1110 const char __user *buf,
1111 size_t count)
1112{
1113 ssize_t ret, written = 0;
1114 unsigned int chunk;
1115
1116 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1117 if (ret < 0)
1118 return ret;
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136 chunk = 2048;
1137 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1138 chunk = 65536;
1139 if (count < chunk)
1140 chunk = count;
1141
1142
1143 if (tty->write_cnt < chunk) {
1144 unsigned char *buf_chunk;
1145
1146 if (chunk < 1024)
1147 chunk = 1024;
1148
1149 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1150 if (!buf_chunk) {
1151 ret = -ENOMEM;
1152 goto out;
1153 }
1154 kfree(tty->write_buf);
1155 tty->write_cnt = chunk;
1156 tty->write_buf = buf_chunk;
1157 }
1158
1159
1160 for (;;) {
1161 size_t size = count;
1162 if (size > chunk)
1163 size = chunk;
1164 ret = -EFAULT;
1165 if (copy_from_user(tty->write_buf, buf, size))
1166 break;
1167 ret = write(tty, file, tty->write_buf, size);
1168 if (ret <= 0)
1169 break;
1170 written += ret;
1171 buf += ret;
1172 count -= ret;
1173 if (!count)
1174 break;
1175 ret = -ERESTARTSYS;
1176 if (signal_pending(current))
1177 break;
1178 cond_resched();
1179 }
1180 if (written) {
1181 tty_update_time(&file_inode(file)->i_mtime);
1182 ret = written;
1183 }
1184out:
1185 tty_write_unlock(tty);
1186 return ret;
1187}
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201void tty_write_message(struct tty_struct *tty, char *msg)
1202{
1203 if (tty) {
1204 mutex_lock(&tty->atomic_write_lock);
1205 tty_lock(tty);
1206 if (tty->ops->write && tty->count > 0) {
1207 tty_unlock(tty);
1208 tty->ops->write(tty, msg, strlen(msg));
1209 } else
1210 tty_unlock(tty);
1211 tty_write_unlock(tty);
1212 }
1213 return;
1214}
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233static ssize_t tty_write(struct file *file, const char __user *buf,
1234 size_t count, loff_t *ppos)
1235{
1236 struct tty_struct *tty = file_tty(file);
1237 struct tty_ldisc *ld;
1238 ssize_t ret;
1239
1240 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
1241 return -EIO;
1242 if (!tty || !tty->ops->write ||
1243 (test_bit(TTY_IO_ERROR, &tty->flags)))
1244 return -EIO;
1245
1246 if (tty->ops->write_room == NULL)
1247 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1248 tty->driver->name);
1249 ld = tty_ldisc_ref_wait(tty);
1250 if (!ld->ops->write)
1251 ret = -EIO;
1252 else
1253 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
1254 tty_ldisc_deref(ld);
1255 return ret;
1256}
1257
1258ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1259 size_t count, loff_t *ppos)
1260{
1261 struct file *p = NULL;
1262
1263 spin_lock(&redirect_lock);
1264 if (redirect)
1265 p = get_file(redirect);
1266 spin_unlock(&redirect_lock);
1267
1268 if (p) {
1269 ssize_t res;
1270 res = vfs_write(p, buf, count, &p->f_pos);
1271 fput(p);
1272 return res;
1273 }
1274 return tty_write(file, buf, count, ppos);
1275}
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285int tty_send_xchar(struct tty_struct *tty, char ch)
1286{
1287 int was_stopped = tty->stopped;
1288
1289 if (tty->ops->send_xchar) {
1290 tty->ops->send_xchar(tty, ch);
1291 return 0;
1292 }
1293
1294 if (tty_write_lock(tty, 0) < 0)
1295 return -ERESTARTSYS;
1296
1297 if (was_stopped)
1298 start_tty(tty);
1299 tty->ops->write(tty, &ch, 1);
1300 if (was_stopped)
1301 stop_tty(tty);
1302 tty_write_unlock(tty);
1303 return 0;
1304}
1305
1306static char ptychar[] = "pqrstuvwxyzabcde";
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319static void pty_line_name(struct tty_driver *driver, int index, char *p)
1320{
1321 int i = index + driver->name_base;
1322
1323 sprintf(p, "%s%c%x",
1324 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1325 ptychar[i >> 4 & 0xf], i & 0xf);
1326}
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
1340{
1341 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
1342 return sprintf(p, "%s", driver->name);
1343 else
1344 return sprintf(p, "%s%d", driver->name,
1345 index + driver->name_base);
1346}
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
1359 struct inode *inode, int idx)
1360{
1361 struct tty_struct *tty;
1362
1363 if (driver->ops->lookup)
1364 tty = driver->ops->lookup(driver, inode, idx);
1365 else
1366 tty = driver->ttys[idx];
1367
1368 if (!IS_ERR(tty))
1369 tty_kref_get(tty);
1370 return tty;
1371}
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381int tty_init_termios(struct tty_struct *tty)
1382{
1383 struct ktermios *tp;
1384 int idx = tty->index;
1385
1386 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1387 tty->termios = tty->driver->init_termios;
1388 else {
1389
1390 tp = tty->driver->termios[idx];
1391 if (tp != NULL)
1392 tty->termios = *tp;
1393 else
1394 tty->termios = tty->driver->init_termios;
1395 }
1396
1397 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1398 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
1399 return 0;
1400}
1401EXPORT_SYMBOL_GPL(tty_init_termios);
1402
1403int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1404{
1405 int ret = tty_init_termios(tty);
1406 if (ret)
1407 return ret;
1408
1409 tty_driver_kref_get(driver);
1410 tty->count++;
1411 driver->ttys[tty->index] = tty;
1412 return 0;
1413}
1414EXPORT_SYMBOL_GPL(tty_standard_install);
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428static int tty_driver_install_tty(struct tty_driver *driver,
1429 struct tty_struct *tty)
1430{
1431 return driver->ops->install ? driver->ops->install(driver, tty) :
1432 tty_standard_install(driver, tty);
1433}
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
1446{
1447 if (driver->ops->remove)
1448 driver->ops->remove(driver, tty);
1449 else
1450 driver->ttys[tty->index] = NULL;
1451}
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462static int tty_reopen(struct tty_struct *tty)
1463{
1464 struct tty_driver *driver = tty->driver;
1465
1466 if (!tty->count)
1467 return -EIO;
1468
1469 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1470 driver->subtype == PTY_TYPE_MASTER)
1471 return -EIO;
1472
1473 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1474 return -EBUSY;
1475
1476 tty->count++;
1477
1478 WARN_ON(!tty->ldisc);
1479
1480 return 0;
1481}
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
1508{
1509 struct tty_struct *tty;
1510 int retval;
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520 if (!try_module_get(driver->owner))
1521 return ERR_PTR(-ENODEV);
1522
1523 tty = alloc_tty_struct(driver, idx);
1524 if (!tty) {
1525 retval = -ENOMEM;
1526 goto err_module_put;
1527 }
1528
1529 tty_lock(tty);
1530 retval = tty_driver_install_tty(driver, tty);
1531 if (retval < 0)
1532 goto err_deinit_tty;
1533
1534 if (!tty->port)
1535 tty->port = driver->ports[idx];
1536
1537 WARN_RATELIMIT(!tty->port,
1538 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1539 __func__, tty->driver->name);
1540
1541 tty->port->itty = tty;
1542
1543
1544
1545
1546
1547
1548 retval = tty_ldisc_setup(tty, tty->link);
1549 if (retval)
1550 goto err_release_tty;
1551
1552 return tty;
1553
1554err_deinit_tty:
1555 tty_unlock(tty);
1556 deinitialize_tty_struct(tty);
1557 free_tty_struct(tty);
1558err_module_put:
1559 module_put(driver->owner);
1560 return ERR_PTR(retval);
1561
1562
1563err_release_tty:
1564 tty_unlock(tty);
1565 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
1566 "clearing slot %d\n", idx);
1567 release_tty(tty, idx);
1568 return ERR_PTR(retval);
1569}
1570
1571void tty_free_termios(struct tty_struct *tty)
1572{
1573 struct ktermios *tp;
1574 int idx = tty->index;
1575
1576
1577 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1578 return;
1579
1580
1581 tp = tty->driver->termios[idx];
1582 if (tp == NULL) {
1583 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1584 if (tp == NULL) {
1585 pr_warn("tty: no memory to save termios state.\n");
1586 return;
1587 }
1588 tty->driver->termios[idx] = tp;
1589 }
1590 *tp = tty->termios;
1591}
1592EXPORT_SYMBOL(tty_free_termios);
1593
1594
1595
1596
1597
1598
1599
1600static void tty_flush_works(struct tty_struct *tty)
1601{
1602 flush_work(&tty->SAK_work);
1603 flush_work(&tty->hangup_work);
1604 if (tty->link) {
1605 flush_work(&tty->link->SAK_work);
1606 flush_work(&tty->link->hangup_work);
1607 }
1608}
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625static void release_one_tty(struct work_struct *work)
1626{
1627 struct tty_struct *tty =
1628 container_of(work, struct tty_struct, hangup_work);
1629 struct tty_driver *driver = tty->driver;
1630 struct module *owner = driver->owner;
1631
1632 if (tty->ops->cleanup)
1633 tty->ops->cleanup(tty);
1634
1635 tty->magic = 0;
1636 tty_driver_kref_put(driver);
1637 module_put(owner);
1638
1639 spin_lock(&tty_files_lock);
1640 list_del_init(&tty->tty_files);
1641 spin_unlock(&tty_files_lock);
1642
1643 put_pid(tty->pgrp);
1644 put_pid(tty->session);
1645 free_tty_struct(tty);
1646}
1647
1648static void queue_release_one_tty(struct kref *kref)
1649{
1650 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
1651
1652
1653
1654 INIT_WORK(&tty->hangup_work, release_one_tty);
1655 schedule_work(&tty->hangup_work);
1656}
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666void tty_kref_put(struct tty_struct *tty)
1667{
1668 if (tty)
1669 kref_put(&tty->kref, queue_release_one_tty);
1670}
1671EXPORT_SYMBOL(tty_kref_put);
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685static void release_tty(struct tty_struct *tty, int idx)
1686{
1687
1688 WARN_ON(tty->index != idx);
1689 WARN_ON(!mutex_is_locked(&tty_mutex));
1690 if (tty->ops->shutdown)
1691 tty->ops->shutdown(tty);
1692 tty_free_termios(tty);
1693 tty_driver_remove_tty(tty->driver, tty);
1694 tty->port->itty = NULL;
1695 if (tty->link)
1696 tty->link->port->itty = NULL;
1697 cancel_work_sync(&tty->port->buf.work);
1698
1699 tty_kref_put(tty->link);
1700 tty_kref_put(tty);
1701}
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712static int tty_release_checks(struct tty_struct *tty, int idx)
1713{
1714#ifdef TTY_PARANOIA_CHECK
1715 if (idx < 0 || idx >= tty->driver->num) {
1716 printk(KERN_DEBUG "%s: bad idx when trying to free (%s)\n",
1717 __func__, tty->name);
1718 return -1;
1719 }
1720
1721
1722 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1723 return 0;
1724
1725 if (tty != tty->driver->ttys[idx]) {
1726 printk(KERN_DEBUG "%s: driver.table[%d] not tty for (%s)\n",
1727 __func__, idx, tty->name);
1728 return -1;
1729 }
1730 if (tty->driver->other) {
1731 struct tty_struct *o_tty = tty->link;
1732
1733 if (o_tty != tty->driver->other->ttys[idx]) {
1734 printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n",
1735 __func__, idx, tty->name);
1736 return -1;
1737 }
1738 if (o_tty->link != tty) {
1739 printk(KERN_DEBUG "%s: bad pty pointers\n", __func__);
1740 return -1;
1741 }
1742 }
1743#endif
1744 return 0;
1745}
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766int tty_release(struct inode *inode, struct file *filp)
1767{
1768 struct tty_struct *tty = file_tty(filp);
1769 struct tty_struct *o_tty = NULL;
1770 int do_sleep, final;
1771 int idx;
1772 char buf[64];
1773 long timeout = 0;
1774 int once = 1;
1775
1776 if (tty_paranoia_check(tty, inode, __func__))
1777 return 0;
1778
1779 tty_lock(tty);
1780 check_tty_count(tty, __func__);
1781
1782 __tty_fasync(-1, filp, 0);
1783
1784 idx = tty->index;
1785 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1786 tty->driver->subtype == PTY_TYPE_MASTER)
1787 o_tty = tty->link;
1788
1789 if (tty_release_checks(tty, idx)) {
1790 tty_unlock(tty);
1791 return 0;
1792 }
1793
1794#ifdef TTY_DEBUG_HANGUP
1795 printk(KERN_DEBUG "%s: %s (tty count=%d)...\n", __func__,
1796 tty_name(tty, buf), tty->count);
1797#endif
1798
1799 if (tty->ops->close)
1800 tty->ops->close(tty, filp);
1801
1802
1803 tty_lock_slave(o_tty);
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818 while (1) {
1819 do_sleep = 0;
1820
1821 if (tty->count <= 1) {
1822 if (waitqueue_active(&tty->read_wait)) {
1823 wake_up_poll(&tty->read_wait, POLLIN);
1824 do_sleep++;
1825 }
1826 if (waitqueue_active(&tty->write_wait)) {
1827 wake_up_poll(&tty->write_wait, POLLOUT);
1828 do_sleep++;
1829 }
1830 }
1831 if (o_tty && o_tty->count <= 1) {
1832 if (waitqueue_active(&o_tty->read_wait)) {
1833 wake_up_poll(&o_tty->read_wait, POLLIN);
1834 do_sleep++;
1835 }
1836 if (waitqueue_active(&o_tty->write_wait)) {
1837 wake_up_poll(&o_tty->write_wait, POLLOUT);
1838 do_sleep++;
1839 }
1840 }
1841 if (!do_sleep)
1842 break;
1843
1844 if (once) {
1845 once = 0;
1846 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
1847 __func__, tty_name(tty, buf));
1848 }
1849 schedule_timeout_killable(timeout);
1850 if (timeout < 120 * HZ)
1851 timeout = 2 * timeout + 1;
1852 else
1853 timeout = MAX_SCHEDULE_TIMEOUT;
1854 }
1855
1856 if (o_tty) {
1857 if (--o_tty->count < 0) {
1858 printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n",
1859 __func__, o_tty->count, tty_name(o_tty, buf));
1860 o_tty->count = 0;
1861 }
1862 }
1863 if (--tty->count < 0) {
1864 printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n",
1865 __func__, tty->count, tty_name(tty, buf));
1866 tty->count = 0;
1867 }
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878 tty_del_file(filp);
1879
1880
1881
1882
1883
1884
1885
1886
1887 if (!tty->count) {
1888 read_lock(&tasklist_lock);
1889 session_clear_tty(tty->session);
1890 if (o_tty)
1891 session_clear_tty(o_tty->session);
1892 read_unlock(&tasklist_lock);
1893 }
1894
1895
1896 final = !tty->count && !(o_tty && o_tty->count);
1897
1898 tty_unlock_slave(o_tty);
1899 tty_unlock(tty);
1900
1901
1902
1903
1904 if (!final)
1905 return 0;
1906
1907#ifdef TTY_DEBUG_HANGUP
1908 printk(KERN_DEBUG "%s: %s: final close\n", __func__, tty_name(tty, buf));
1909#endif
1910
1911
1912
1913 tty_ldisc_release(tty);
1914
1915
1916 tty_flush_works(tty);
1917
1918#ifdef TTY_DEBUG_HANGUP
1919 printk(KERN_DEBUG "%s: %s: freeing structure...\n", __func__, tty_name(tty, buf));
1920#endif
1921
1922
1923
1924
1925
1926
1927 mutex_lock(&tty_mutex);
1928 release_tty(tty, idx);
1929 mutex_unlock(&tty_mutex);
1930
1931 return 0;
1932}
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1946{
1947 struct tty_struct *tty;
1948 int retval;
1949
1950 if (device != MKDEV(TTYAUX_MAJOR, 0))
1951 return NULL;
1952
1953 tty = get_current_tty();
1954 if (!tty)
1955 return ERR_PTR(-ENXIO);
1956
1957 filp->f_flags |= O_NONBLOCK;
1958
1959 tty_lock(tty);
1960 tty_kref_put(tty);
1961
1962 retval = tty_reopen(tty);
1963 if (retval < 0) {
1964 tty_unlock(tty);
1965 tty = ERR_PTR(retval);
1966 }
1967 return tty;
1968}
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1984 int *noctty, int *index)
1985{
1986 struct tty_driver *driver;
1987
1988 switch (device) {
1989#ifdef CONFIG_VT
1990 case MKDEV(TTY_MAJOR, 0): {
1991 extern struct tty_driver *console_driver;
1992 driver = tty_driver_kref_get(console_driver);
1993 *index = fg_console;
1994 *noctty = 1;
1995 break;
1996 }
1997#endif
1998 case MKDEV(TTYAUX_MAJOR, 1): {
1999 struct tty_driver *console_driver = console_device(index);
2000 if (console_driver) {
2001 driver = tty_driver_kref_get(console_driver);
2002 if (driver) {
2003
2004 filp->f_flags |= O_NONBLOCK;
2005 *noctty = 1;
2006 break;
2007 }
2008 }
2009 return ERR_PTR(-ENODEV);
2010 }
2011 default:
2012 driver = get_tty_driver(device, index);
2013 if (!driver)
2014 return ERR_PTR(-ENODEV);
2015 break;
2016 }
2017 return driver;
2018}
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044static int tty_open(struct inode *inode, struct file *filp)
2045{
2046 struct tty_struct *tty;
2047 int noctty, retval;
2048 struct tty_driver *driver = NULL;
2049 int index;
2050 dev_t device = inode->i_rdev;
2051 unsigned saved_flags = filp->f_flags;
2052
2053 nonseekable_open(inode, filp);
2054
2055retry_open:
2056 retval = tty_alloc_file(filp);
2057 if (retval)
2058 return -ENOMEM;
2059
2060 noctty = filp->f_flags & O_NOCTTY;
2061 index = -1;
2062 retval = 0;
2063
2064 tty = tty_open_current_tty(device, filp);
2065 if (!tty) {
2066 mutex_lock(&tty_mutex);
2067 driver = tty_lookup_driver(device, filp, &noctty, &index);
2068 if (IS_ERR(driver)) {
2069 retval = PTR_ERR(driver);
2070 goto err_unlock;
2071 }
2072
2073
2074 tty = tty_driver_lookup_tty(driver, inode, index);
2075 if (IS_ERR(tty)) {
2076 retval = PTR_ERR(tty);
2077 goto err_unlock;
2078 }
2079
2080 if (tty) {
2081 mutex_unlock(&tty_mutex);
2082 tty_lock(tty);
2083
2084 tty_kref_put(tty);
2085 retval = tty_reopen(tty);
2086 if (retval < 0) {
2087 tty_unlock(tty);
2088 tty = ERR_PTR(retval);
2089 }
2090 } else {
2091 tty = tty_init_dev(driver, index);
2092 mutex_unlock(&tty_mutex);
2093 }
2094
2095 tty_driver_kref_put(driver);
2096 }
2097
2098 if (IS_ERR(tty)) {
2099 retval = PTR_ERR(tty);
2100 goto err_file;
2101 }
2102
2103 tty_add_file(tty, filp);
2104
2105 check_tty_count(tty, __func__);
2106 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2107 tty->driver->subtype == PTY_TYPE_MASTER)
2108 noctty = 1;
2109#ifdef TTY_DEBUG_HANGUP
2110 printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name);
2111#endif
2112 if (tty->ops->open)
2113 retval = tty->ops->open(tty, filp);
2114 else
2115 retval = -ENODEV;
2116 filp->f_flags = saved_flags;
2117
2118 if (retval) {
2119#ifdef TTY_DEBUG_HANGUP
2120 printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__,
2121 retval, tty->name);
2122#endif
2123 tty_unlock(tty);
2124 tty_release(inode, filp);
2125 if (retval != -ERESTARTSYS)
2126 return retval;
2127
2128 if (signal_pending(current))
2129 return retval;
2130
2131 schedule();
2132
2133
2134
2135 if (tty_hung_up_p(filp))
2136 filp->f_op = &tty_fops;
2137 goto retry_open;
2138 }
2139 clear_bit(TTY_HUPPED, &tty->flags);
2140
2141
2142 read_lock(&tasklist_lock);
2143 spin_lock_irq(¤t->sighand->siglock);
2144 if (!noctty &&
2145 current->signal->leader &&
2146 !current->signal->tty &&
2147 tty->session == NULL)
2148 __proc_set_tty(tty);
2149 spin_unlock_irq(¤t->sighand->siglock);
2150 read_unlock(&tasklist_lock);
2151 tty_unlock(tty);
2152 return 0;
2153err_unlock:
2154 mutex_unlock(&tty_mutex);
2155
2156 if (!IS_ERR_OR_NULL(driver))
2157 tty_driver_kref_put(driver);
2158err_file:
2159 tty_free_file(filp);
2160 return retval;
2161}
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177static unsigned int tty_poll(struct file *filp, poll_table *wait)
2178{
2179 struct tty_struct *tty = file_tty(filp);
2180 struct tty_ldisc *ld;
2181 int ret = 0;
2182
2183 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
2184 return 0;
2185
2186 ld = tty_ldisc_ref_wait(tty);
2187 if (ld->ops->poll)
2188 ret = ld->ops->poll(tty, filp, wait);
2189 tty_ldisc_deref(ld);
2190 return ret;
2191}
2192
2193static int __tty_fasync(int fd, struct file *filp, int on)
2194{
2195 struct tty_struct *tty = file_tty(filp);
2196 struct tty_ldisc *ldisc;
2197 unsigned long flags;
2198 int retval = 0;
2199
2200 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
2201 goto out;
2202
2203 retval = fasync_helper(fd, filp, on, &tty->fasync);
2204 if (retval <= 0)
2205 goto out;
2206
2207 ldisc = tty_ldisc_ref(tty);
2208 if (ldisc) {
2209 if (ldisc->ops->fasync)
2210 ldisc->ops->fasync(tty, on);
2211 tty_ldisc_deref(ldisc);
2212 }
2213
2214 if (on) {
2215 enum pid_type type;
2216 struct pid *pid;
2217
2218 spin_lock_irqsave(&tty->ctrl_lock, flags);
2219 if (tty->pgrp) {
2220 pid = tty->pgrp;
2221 type = PIDTYPE_PGID;
2222 } else {
2223 pid = task_pid(current);
2224 type = PIDTYPE_PID;
2225 }
2226 get_pid(pid);
2227 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2228 __f_setown(filp, pid, type, 0);
2229 put_pid(pid);
2230 retval = 0;
2231 }
2232out:
2233 return retval;
2234}
2235
2236static int tty_fasync(int fd, struct file *filp, int on)
2237{
2238 struct tty_struct *tty = file_tty(filp);
2239 int retval;
2240
2241 tty_lock(tty);
2242 retval = __tty_fasync(fd, filp, on);
2243 tty_unlock(tty);
2244
2245 return retval;
2246}
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265static int tiocsti(struct tty_struct *tty, char __user *p)
2266{
2267 char ch, mbz = 0;
2268 struct tty_ldisc *ld;
2269
2270 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2271 return -EPERM;
2272 if (get_user(ch, p))
2273 return -EFAULT;
2274 tty_audit_tiocsti(tty, ch);
2275 ld = tty_ldisc_ref_wait(tty);
2276 ld->ops->receive_buf(tty, &ch, &mbz, 1);
2277 tty_ldisc_deref(ld);
2278 return 0;
2279}
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2293{
2294 int err;
2295
2296 mutex_lock(&tty->winsize_mutex);
2297 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2298 mutex_unlock(&tty->winsize_mutex);
2299
2300 return err ? -EFAULT: 0;
2301}
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2314{
2315 struct pid *pgrp;
2316
2317
2318 mutex_lock(&tty->winsize_mutex);
2319 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2320 goto done;
2321
2322
2323 pgrp = tty_get_pgrp(tty);
2324 if (pgrp)
2325 kill_pgrp(pgrp, SIGWINCH, 1);
2326 put_pid(pgrp);
2327
2328 tty->winsize = *ws;
2329done:
2330 mutex_unlock(&tty->winsize_mutex);
2331 return 0;
2332}
2333EXPORT_SYMBOL(tty_do_resize);
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
2351{
2352 struct winsize tmp_ws;
2353 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2354 return -EFAULT;
2355
2356 if (tty->ops->resize)
2357 return tty->ops->resize(tty, &tmp_ws);
2358 else
2359 return tty_do_resize(tty, &tmp_ws);
2360}
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371static int tioccons(struct file *file)
2372{
2373 if (!capable(CAP_SYS_ADMIN))
2374 return -EPERM;
2375 if (file->f_op->write == redirected_tty_write) {
2376 struct file *f;
2377 spin_lock(&redirect_lock);
2378 f = redirect;
2379 redirect = NULL;
2380 spin_unlock(&redirect_lock);
2381 if (f)
2382 fput(f);
2383 return 0;
2384 }
2385 spin_lock(&redirect_lock);
2386 if (redirect) {
2387 spin_unlock(&redirect_lock);
2388 return -EBUSY;
2389 }
2390 redirect = get_file(file);
2391 spin_unlock(&redirect_lock);
2392 return 0;
2393}
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407static int fionbio(struct file *file, int __user *p)
2408{
2409 int nonblock;
2410
2411 if (get_user(nonblock, p))
2412 return -EFAULT;
2413
2414 spin_lock(&file->f_lock);
2415 if (nonblock)
2416 file->f_flags |= O_NONBLOCK;
2417 else
2418 file->f_flags &= ~O_NONBLOCK;
2419 spin_unlock(&file->f_lock);
2420 return 0;
2421}
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437static int tiocsctty(struct tty_struct *tty, int arg)
2438{
2439 int ret = 0;
2440
2441 tty_lock(tty);
2442 read_lock(&tasklist_lock);
2443
2444 if (current->signal->leader && (task_session(current) == tty->session))
2445 goto unlock;
2446
2447
2448
2449
2450
2451 if (!current->signal->leader || current->signal->tty) {
2452 ret = -EPERM;
2453 goto unlock;
2454 }
2455
2456 if (tty->session) {
2457
2458
2459
2460
2461 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
2462
2463
2464
2465 session_clear_tty(tty->session);
2466 } else {
2467 ret = -EPERM;
2468 goto unlock;
2469 }
2470 }
2471 proc_set_tty(tty);
2472unlock:
2473 read_unlock(&tasklist_lock);
2474 tty_unlock(tty);
2475 return ret;
2476}
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486struct pid *tty_get_pgrp(struct tty_struct *tty)
2487{
2488 unsigned long flags;
2489 struct pid *pgrp;
2490
2491 spin_lock_irqsave(&tty->ctrl_lock, flags);
2492 pgrp = get_pid(tty->pgrp);
2493 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2494
2495 return pgrp;
2496}
2497EXPORT_SYMBOL_GPL(tty_get_pgrp);
2498
2499
2500
2501
2502
2503
2504
2505
2506static struct pid *session_of_pgrp(struct pid *pgrp)
2507{
2508 struct task_struct *p;
2509 struct pid *sid = NULL;
2510
2511 p = pid_task(pgrp, PIDTYPE_PGID);
2512 if (p == NULL)
2513 p = pid_task(pgrp, PIDTYPE_PID);
2514 if (p != NULL)
2515 sid = task_session(p);
2516
2517 return sid;
2518}
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2533{
2534 struct pid *pid;
2535 int ret;
2536
2537
2538
2539
2540 if (tty == real_tty && current->signal->tty != real_tty)
2541 return -ENOTTY;
2542 pid = tty_get_pgrp(real_tty);
2543 ret = put_user(pid_vnr(pid), p);
2544 put_pid(pid);
2545 return ret;
2546}
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2561{
2562 struct pid *pgrp;
2563 pid_t pgrp_nr;
2564 int retval = tty_check_change(real_tty);
2565 unsigned long flags;
2566
2567 if (retval == -EIO)
2568 return -ENOTTY;
2569 if (retval)
2570 return retval;
2571 if (!current->signal->tty ||
2572 (current->signal->tty != real_tty) ||
2573 (real_tty->session != task_session(current)))
2574 return -ENOTTY;
2575 if (get_user(pgrp_nr, p))
2576 return -EFAULT;
2577 if (pgrp_nr < 0)
2578 return -EINVAL;
2579 rcu_read_lock();
2580 pgrp = find_vpid(pgrp_nr);
2581 retval = -ESRCH;
2582 if (!pgrp)
2583 goto out_unlock;
2584 retval = -EPERM;
2585 if (session_of_pgrp(pgrp) != task_session(current))
2586 goto out_unlock;
2587 retval = 0;
2588 spin_lock_irqsave(&tty->ctrl_lock, flags);
2589 put_pid(real_tty->pgrp);
2590 real_tty->pgrp = get_pid(pgrp);
2591 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2592out_unlock:
2593 rcu_read_unlock();
2594 return retval;
2595}
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2610{
2611
2612
2613
2614
2615 if (tty == real_tty && current->signal->tty != real_tty)
2616 return -ENOTTY;
2617 if (!real_tty->session)
2618 return -ENOTTY;
2619 return put_user(pid_vnr(real_tty->session), p);
2620}
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632static int tiocsetd(struct tty_struct *tty, int __user *p)
2633{
2634 int ldisc;
2635 int ret;
2636
2637 if (get_user(ldisc, p))
2638 return -EFAULT;
2639
2640 ret = tty_set_ldisc(tty, ldisc);
2641
2642 return ret;
2643}
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658static int send_break(struct tty_struct *tty, unsigned int duration)
2659{
2660 int retval;
2661
2662 if (tty->ops->break_ctl == NULL)
2663 return 0;
2664
2665 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2666 retval = tty->ops->break_ctl(tty, duration);
2667 else {
2668
2669 if (tty_write_lock(tty, 0) < 0)
2670 return -EINTR;
2671 retval = tty->ops->break_ctl(tty, -1);
2672 if (retval)
2673 goto out;
2674 if (!signal_pending(current))
2675 msleep_interruptible(duration);
2676 retval = tty->ops->break_ctl(tty, 0);
2677out:
2678 tty_write_unlock(tty);
2679 if (signal_pending(current))
2680 retval = -EINTR;
2681 }
2682 return retval;
2683}
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697static int tty_tiocmget(struct tty_struct *tty, int __user *p)
2698{
2699 int retval = -EINVAL;
2700
2701 if (tty->ops->tiocmget) {
2702 retval = tty->ops->tiocmget(tty);
2703
2704 if (retval >= 0)
2705 retval = put_user(retval, p);
2706 }
2707 return retval;
2708}
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
2723 unsigned __user *p)
2724{
2725 int retval;
2726 unsigned int set, clear, val;
2727
2728 if (tty->ops->tiocmset == NULL)
2729 return -EINVAL;
2730
2731 retval = get_user(val, p);
2732 if (retval)
2733 return retval;
2734 set = clear = 0;
2735 switch (cmd) {
2736 case TIOCMBIS:
2737 set = val;
2738 break;
2739 case TIOCMBIC:
2740 clear = val;
2741 break;
2742 case TIOCMSET:
2743 set = val;
2744 clear = ~val;
2745 break;
2746 }
2747 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2748 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2749 return tty->ops->tiocmset(tty, set, clear);
2750}
2751
2752static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2753{
2754 int retval = -EINVAL;
2755 struct serial_icounter_struct icount;
2756 memset(&icount, 0, sizeof(icount));
2757 if (tty->ops->get_icount)
2758 retval = tty->ops->get_icount(tty, &icount);
2759 if (retval != 0)
2760 return retval;
2761 if (copy_to_user(arg, &icount, sizeof(icount)))
2762 return -EFAULT;
2763 return 0;
2764}
2765
2766static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
2767{
2768 static DEFINE_RATELIMIT_STATE(depr_flags,
2769 DEFAULT_RATELIMIT_INTERVAL,
2770 DEFAULT_RATELIMIT_BURST);
2771 char comm[TASK_COMM_LEN];
2772 int flags;
2773
2774 if (get_user(flags, &ss->flags))
2775 return;
2776
2777 flags &= ASYNC_DEPRECATED;
2778
2779 if (flags && __ratelimit(&depr_flags))
2780 pr_warning("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2781 __func__, get_task_comm(comm, current), flags);
2782}
2783
2784
2785
2786
2787
2788static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2789{
2790 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2791 tty->driver->subtype == PTY_TYPE_MASTER)
2792 tty = tty->link;
2793 return tty;
2794}
2795
2796
2797
2798
2799long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2800{
2801 struct tty_struct *tty = file_tty(file);
2802 struct tty_struct *real_tty;
2803 void __user *p = (void __user *)arg;
2804 int retval;
2805 struct tty_ldisc *ld;
2806
2807 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2808 return -EINVAL;
2809
2810 real_tty = tty_pair_get_tty(tty);
2811
2812
2813
2814
2815 switch (cmd) {
2816 case TIOCSETD:
2817 case TIOCSBRK:
2818 case TIOCCBRK:
2819 case TCSBRK:
2820 case TCSBRKP:
2821 retval = tty_check_change(tty);
2822 if (retval)
2823 return retval;
2824 if (cmd != TIOCCBRK) {
2825 tty_wait_until_sent(tty, 0);
2826 if (signal_pending(current))
2827 return -EINTR;
2828 }
2829 break;
2830 }
2831
2832
2833
2834
2835 switch (cmd) {
2836 case TIOCSTI:
2837 return tiocsti(tty, p);
2838 case TIOCGWINSZ:
2839 return tiocgwinsz(real_tty, p);
2840 case TIOCSWINSZ:
2841 return tiocswinsz(real_tty, p);
2842 case TIOCCONS:
2843 return real_tty != tty ? -EINVAL : tioccons(file);
2844 case FIONBIO:
2845 return fionbio(file, p);
2846 case TIOCEXCL:
2847 set_bit(TTY_EXCLUSIVE, &tty->flags);
2848 return 0;
2849 case TIOCNXCL:
2850 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2851 return 0;
2852 case TIOCGEXCL:
2853 {
2854 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2855 return put_user(excl, (int __user *)p);
2856 }
2857 case TIOCNOTTY:
2858 if (current->signal->tty != tty)
2859 return -ENOTTY;
2860 no_tty();
2861 return 0;
2862 case TIOCSCTTY:
2863 return tiocsctty(tty, arg);
2864 case TIOCGPGRP:
2865 return tiocgpgrp(tty, real_tty, p);
2866 case TIOCSPGRP:
2867 return tiocspgrp(tty, real_tty, p);
2868 case TIOCGSID:
2869 return tiocgsid(tty, real_tty, p);
2870 case TIOCGETD:
2871 return put_user(tty->ldisc->ops->num, (int __user *)p);
2872 case TIOCSETD:
2873 return tiocsetd(tty, p);
2874 case TIOCVHANGUP:
2875 if (!capable(CAP_SYS_ADMIN))
2876 return -EPERM;
2877 tty_vhangup(tty);
2878 return 0;
2879 case TIOCGDEV:
2880 {
2881 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2882 return put_user(ret, (unsigned int __user *)p);
2883 }
2884
2885
2886
2887 case TIOCSBRK:
2888 if (tty->ops->break_ctl)
2889 return tty->ops->break_ctl(tty, -1);
2890 return 0;
2891 case TIOCCBRK:
2892 if (tty->ops->break_ctl)
2893 return tty->ops->break_ctl(tty, 0);
2894 return 0;
2895 case TCSBRK:
2896
2897
2898
2899
2900 if (!arg)
2901 return send_break(tty, 250);
2902 return 0;
2903 case TCSBRKP:
2904 return send_break(tty, arg ? arg*100 : 250);
2905
2906 case TIOCMGET:
2907 return tty_tiocmget(tty, p);
2908 case TIOCMSET:
2909 case TIOCMBIC:
2910 case TIOCMBIS:
2911 return tty_tiocmset(tty, cmd, p);
2912 case TIOCGICOUNT:
2913 retval = tty_tiocgicount(tty, p);
2914
2915 if (retval != -EINVAL)
2916 return retval;
2917 break;
2918 case TCFLSH:
2919 switch (arg) {
2920 case TCIFLUSH:
2921 case TCIOFLUSH:
2922
2923 tty_buffer_flush(tty, NULL);
2924 break;
2925 }
2926 break;
2927 case TIOCSSERIAL:
2928 tty_warn_deprecated_flags(p);
2929 break;
2930 }
2931 if (tty->ops->ioctl) {
2932 retval = tty->ops->ioctl(tty, cmd, arg);
2933 if (retval != -ENOIOCTLCMD)
2934 return retval;
2935 }
2936 ld = tty_ldisc_ref_wait(tty);
2937 retval = -EINVAL;
2938 if (ld->ops->ioctl) {
2939 retval = ld->ops->ioctl(tty, file, cmd, arg);
2940 if (retval == -ENOIOCTLCMD)
2941 retval = -ENOTTY;
2942 }
2943 tty_ldisc_deref(ld);
2944 return retval;
2945}
2946
2947#ifdef CONFIG_COMPAT
2948static long tty_compat_ioctl(struct file *file, unsigned int cmd,
2949 unsigned long arg)
2950{
2951 struct tty_struct *tty = file_tty(file);
2952 struct tty_ldisc *ld;
2953 int retval = -ENOIOCTLCMD;
2954
2955 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2956 return -EINVAL;
2957
2958 if (tty->ops->compat_ioctl) {
2959 retval = tty->ops->compat_ioctl(tty, cmd, arg);
2960 if (retval != -ENOIOCTLCMD)
2961 return retval;
2962 }
2963
2964 ld = tty_ldisc_ref_wait(tty);
2965 if (ld->ops->compat_ioctl)
2966 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
2967 else
2968 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
2969 tty_ldisc_deref(ld);
2970
2971 return retval;
2972}
2973#endif
2974
2975static int this_tty(const void *t, struct file *file, unsigned fd)
2976{
2977 if (likely(file->f_op->read != tty_read))
2978 return 0;
2979 return file_tty(file) != t ? 0 : fd + 1;
2980}
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001void __do_SAK(struct tty_struct *tty)
3002{
3003#ifdef TTY_SOFT_SAK
3004 tty_hangup(tty);
3005#else
3006 struct task_struct *g, *p;
3007 struct pid *session;
3008 int i;
3009
3010 if (!tty)
3011 return;
3012 session = tty->session;
3013
3014 tty_ldisc_flush(tty);
3015
3016 tty_driver_flush_buffer(tty);
3017
3018 read_lock(&tasklist_lock);
3019
3020 do_each_pid_task(session, PIDTYPE_SID, p) {
3021 printk(KERN_NOTICE "SAK: killed process %d"
3022 " (%s): task_session(p)==tty->session\n",
3023 task_pid_nr(p), p->comm);
3024 send_sig(SIGKILL, p, 1);
3025 } while_each_pid_task(session, PIDTYPE_SID, p);
3026
3027
3028
3029 do_each_thread(g, p) {
3030 if (p->signal->tty == tty) {
3031 printk(KERN_NOTICE "SAK: killed process %d"
3032 " (%s): task_session(p)==tty->session\n",
3033 task_pid_nr(p), p->comm);
3034 send_sig(SIGKILL, p, 1);
3035 continue;
3036 }
3037 task_lock(p);
3038 i = iterate_fd(p->files, 0, this_tty, tty);
3039 if (i != 0) {
3040 printk(KERN_NOTICE "SAK: killed process %d"
3041 " (%s): fd#%d opened to the tty\n",
3042 task_pid_nr(p), p->comm, i - 1);
3043 force_sig(SIGKILL, p);
3044 }
3045 task_unlock(p);
3046 } while_each_thread(g, p);
3047 read_unlock(&tasklist_lock);
3048#endif
3049}
3050
3051static void do_SAK_work(struct work_struct *work)
3052{
3053 struct tty_struct *tty =
3054 container_of(work, struct tty_struct, SAK_work);
3055 __do_SAK(tty);
3056}
3057
3058
3059
3060
3061
3062
3063
3064void do_SAK(struct tty_struct *tty)
3065{
3066 if (!tty)
3067 return;
3068 schedule_work(&tty->SAK_work);
3069}
3070
3071EXPORT_SYMBOL(do_SAK);
3072
3073static int dev_match_devt(struct device *dev, const void *data)
3074{
3075 const dev_t *devt = data;
3076 return dev->devt == *devt;
3077}
3078
3079
3080static struct device *tty_get_device(struct tty_struct *tty)
3081{
3082 dev_t devt = tty_devnum(tty);
3083 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
3084}
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
3096{
3097 struct tty_struct *tty;
3098
3099 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
3100 if (!tty)
3101 return NULL;
3102
3103 kref_init(&tty->kref);
3104 tty->magic = TTY_MAGIC;
3105 tty_ldisc_init(tty);
3106 tty->session = NULL;
3107 tty->pgrp = NULL;
3108 mutex_init(&tty->legacy_mutex);
3109 mutex_init(&tty->throttle_mutex);
3110 init_rwsem(&tty->termios_rwsem);
3111 mutex_init(&tty->winsize_mutex);
3112 init_ldsem(&tty->ldisc_sem);
3113 init_waitqueue_head(&tty->write_wait);
3114 init_waitqueue_head(&tty->read_wait);
3115 INIT_WORK(&tty->hangup_work, do_tty_hangup);
3116 mutex_init(&tty->atomic_write_lock);
3117 spin_lock_init(&tty->ctrl_lock);
3118 spin_lock_init(&tty->flow_lock);
3119 INIT_LIST_HEAD(&tty->tty_files);
3120 INIT_WORK(&tty->SAK_work, do_SAK_work);
3121
3122 tty->driver = driver;
3123 tty->ops = driver->ops;
3124 tty->index = idx;
3125 tty_line_name(driver, idx, tty->name);
3126 tty->dev = tty_get_device(tty);
3127
3128 return tty;
3129}
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140void deinitialize_tty_struct(struct tty_struct *tty)
3141{
3142 tty_ldisc_deinit(tty);
3143}
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157int tty_put_char(struct tty_struct *tty, unsigned char ch)
3158{
3159 if (tty->ops->put_char)
3160 return tty->ops->put_char(tty, ch);
3161 return tty->ops->write(tty, &ch, 1);
3162}
3163EXPORT_SYMBOL_GPL(tty_put_char);
3164
3165struct class *tty_class;
3166
3167static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3168 unsigned int index, unsigned int count)
3169{
3170
3171 cdev_init(&driver->cdevs[index], &tty_fops);
3172 driver->cdevs[index].owner = driver->owner;
3173 return cdev_add(&driver->cdevs[index], dev, count);
3174}
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3196 struct device *device)
3197{
3198 return tty_register_device_attr(driver, index, device, NULL, NULL);
3199}
3200EXPORT_SYMBOL(tty_register_device);
3201
3202static void tty_device_create_release(struct device *dev)
3203{
3204 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3205 kfree(dev);
3206}
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228struct device *tty_register_device_attr(struct tty_driver *driver,
3229 unsigned index, struct device *device,
3230 void *drvdata,
3231 const struct attribute_group **attr_grp)
3232{
3233 char name[64];
3234 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
3235 struct device *dev = NULL;
3236 int retval = -ENODEV;
3237 bool cdev = false;
3238
3239 if (index >= driver->num) {
3240 printk(KERN_ERR "Attempt to register invalid tty line number "
3241 " (%d).\n", index);
3242 return ERR_PTR(-EINVAL);
3243 }
3244
3245 if (driver->type == TTY_DRIVER_TYPE_PTY)
3246 pty_line_name(driver, index, name);
3247 else
3248 tty_line_name(driver, index, name);
3249
3250 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3251 retval = tty_cdev_add(driver, devt, index, 1);
3252 if (retval)
3253 goto error;
3254 cdev = true;
3255 }
3256
3257 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3258 if (!dev) {
3259 retval = -ENOMEM;
3260 goto error;
3261 }
3262
3263 dev->devt = devt;
3264 dev->class = tty_class;
3265 dev->parent = device;
3266 dev->release = tty_device_create_release;
3267 dev_set_name(dev, "%s", name);
3268 dev->groups = attr_grp;
3269 dev_set_drvdata(dev, drvdata);
3270
3271 retval = device_register(dev);
3272 if (retval)
3273 goto error;
3274
3275 return dev;
3276
3277error:
3278 put_device(dev);
3279 if (cdev)
3280 cdev_del(&driver->cdevs[index]);
3281 return ERR_PTR(retval);
3282}
3283EXPORT_SYMBOL_GPL(tty_register_device_attr);
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296void tty_unregister_device(struct tty_driver *driver, unsigned index)
3297{
3298 device_destroy(tty_class,
3299 MKDEV(driver->major, driver->minor_start) + index);
3300 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC))
3301 cdev_del(&driver->cdevs[index]);
3302}
3303EXPORT_SYMBOL(tty_unregister_device);
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3315 unsigned long flags)
3316{
3317 struct tty_driver *driver;
3318 unsigned int cdevs = 1;
3319 int err;
3320
3321 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
3322 return ERR_PTR(-EINVAL);
3323
3324 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
3325 if (!driver)
3326 return ERR_PTR(-ENOMEM);
3327
3328 kref_init(&driver->kref);
3329 driver->magic = TTY_DRIVER_MAGIC;
3330 driver->num = lines;
3331 driver->owner = owner;
3332 driver->flags = flags;
3333
3334 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3335 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3336 GFP_KERNEL);
3337 driver->termios = kcalloc(lines, sizeof(*driver->termios),
3338 GFP_KERNEL);
3339 if (!driver->ttys || !driver->termios) {
3340 err = -ENOMEM;
3341 goto err_free_all;
3342 }
3343 }
3344
3345 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3346 driver->ports = kcalloc(lines, sizeof(*driver->ports),
3347 GFP_KERNEL);
3348 if (!driver->ports) {
3349 err = -ENOMEM;
3350 goto err_free_all;
3351 }
3352 cdevs = lines;
3353 }
3354
3355 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3356 if (!driver->cdevs) {
3357 err = -ENOMEM;
3358 goto err_free_all;
3359 }
3360
3361 return driver;
3362err_free_all:
3363 kfree(driver->ports);
3364 kfree(driver->ttys);
3365 kfree(driver->termios);
3366 kfree(driver);
3367 return ERR_PTR(err);
3368}
3369EXPORT_SYMBOL(__tty_alloc_driver);
3370
3371static void destruct_tty_driver(struct kref *kref)
3372{
3373 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3374 int i;
3375 struct ktermios *tp;
3376
3377 if (driver->flags & TTY_DRIVER_INSTALLED) {
3378
3379
3380
3381
3382
3383 for (i = 0; i < driver->num; i++) {
3384 tp = driver->termios[i];
3385 if (tp) {
3386 driver->termios[i] = NULL;
3387 kfree(tp);
3388 }
3389 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3390 tty_unregister_device(driver, i);
3391 }
3392 proc_tty_unregister_driver(driver);
3393 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
3394 cdev_del(&driver->cdevs[0]);
3395 }
3396 kfree(driver->cdevs);
3397 kfree(driver->ports);
3398 kfree(driver->termios);
3399 kfree(driver->ttys);
3400 kfree(driver);
3401}
3402
3403void tty_driver_kref_put(struct tty_driver *driver)
3404{
3405 kref_put(&driver->kref, destruct_tty_driver);
3406}
3407EXPORT_SYMBOL(tty_driver_kref_put);
3408
3409void tty_set_operations(struct tty_driver *driver,
3410 const struct tty_operations *op)
3411{
3412 driver->ops = op;
3413};
3414EXPORT_SYMBOL(tty_set_operations);
3415
3416void put_tty_driver(struct tty_driver *d)
3417{
3418 tty_driver_kref_put(d);
3419}
3420EXPORT_SYMBOL(put_tty_driver);
3421
3422
3423
3424
3425int tty_register_driver(struct tty_driver *driver)
3426{
3427 int error;
3428 int i;
3429 dev_t dev;
3430 struct device *d;
3431
3432 if (!driver->major) {
3433 error = alloc_chrdev_region(&dev, driver->minor_start,
3434 driver->num, driver->name);
3435 if (!error) {
3436 driver->major = MAJOR(dev);
3437 driver->minor_start = MINOR(dev);
3438 }
3439 } else {
3440 dev = MKDEV(driver->major, driver->minor_start);
3441 error = register_chrdev_region(dev, driver->num, driver->name);
3442 }
3443 if (error < 0)
3444 goto err;
3445
3446 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3447 error = tty_cdev_add(driver, dev, 0, driver->num);
3448 if (error)
3449 goto err_unreg_char;
3450 }
3451
3452 mutex_lock(&tty_mutex);
3453 list_add(&driver->tty_drivers, &tty_drivers);
3454 mutex_unlock(&tty_mutex);
3455
3456 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3457 for (i = 0; i < driver->num; i++) {
3458 d = tty_register_device(driver, i, NULL);
3459 if (IS_ERR(d)) {
3460 error = PTR_ERR(d);
3461 goto err_unreg_devs;
3462 }
3463 }
3464 }
3465 proc_tty_register_driver(driver);
3466 driver->flags |= TTY_DRIVER_INSTALLED;
3467 return 0;
3468
3469err_unreg_devs:
3470 for (i--; i >= 0; i--)
3471 tty_unregister_device(driver, i);
3472
3473 mutex_lock(&tty_mutex);
3474 list_del(&driver->tty_drivers);
3475 mutex_unlock(&tty_mutex);
3476
3477err_unreg_char:
3478 unregister_chrdev_region(dev, driver->num);
3479err:
3480 return error;
3481}
3482EXPORT_SYMBOL(tty_register_driver);
3483
3484
3485
3486
3487int tty_unregister_driver(struct tty_driver *driver)
3488{
3489#if 0
3490
3491 if (driver->refcount)
3492 return -EBUSY;
3493#endif
3494 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3495 driver->num);
3496 mutex_lock(&tty_mutex);
3497 list_del(&driver->tty_drivers);
3498 mutex_unlock(&tty_mutex);
3499 return 0;
3500}
3501
3502EXPORT_SYMBOL(tty_unregister_driver);
3503
3504dev_t tty_devnum(struct tty_struct *tty)
3505{
3506 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3507}
3508EXPORT_SYMBOL(tty_devnum);
3509
3510void tty_default_fops(struct file_operations *fops)
3511{
3512 *fops = tty_fops;
3513}
3514
3515
3516
3517
3518
3519
3520
3521void __init console_init(void)
3522{
3523 initcall_t *call;
3524
3525
3526 tty_ldisc_begin();
3527
3528
3529
3530
3531
3532 call = __con_initcall_start;
3533 while (call < __con_initcall_end) {
3534 (*call)();
3535 call++;
3536 }
3537}
3538
3539static char *tty_devnode(struct device *dev, umode_t *mode)
3540{
3541 if (!mode)
3542 return NULL;
3543 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3544 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3545 *mode = 0666;
3546 return NULL;
3547}
3548
3549static int __init tty_class_init(void)
3550{
3551 tty_class = class_create(THIS_MODULE, "tty");
3552 if (IS_ERR(tty_class))
3553 return PTR_ERR(tty_class);
3554 tty_class->devnode = tty_devnode;
3555 return 0;
3556}
3557
3558postcore_initcall(tty_class_init);
3559
3560
3561static struct cdev tty_cdev, console_cdev;
3562
3563static ssize_t show_cons_active(struct device *dev,
3564 struct device_attribute *attr, char *buf)
3565{
3566 struct console *cs[16];
3567 int i = 0;
3568 struct console *c;
3569 ssize_t count = 0;
3570
3571 console_lock();
3572 for_each_console(c) {
3573 if (!c->device)
3574 continue;
3575 if (!c->write)
3576 continue;
3577 if ((c->flags & CON_ENABLED) == 0)
3578 continue;
3579 cs[i++] = c;
3580 if (i >= ARRAY_SIZE(cs))
3581 break;
3582 }
3583 while (i--) {
3584 int index = cs[i]->index;
3585 struct tty_driver *drv = cs[i]->device(cs[i], &index);
3586
3587
3588 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3589 count += tty_line_name(drv, index, buf + count);
3590 else
3591 count += sprintf(buf + count, "%s%d",
3592 cs[i]->name, cs[i]->index);
3593
3594 count += sprintf(buf + count, "%c", i ? ' ':'\n');
3595 }
3596 console_unlock();
3597
3598 return count;
3599}
3600static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3601
3602static struct attribute *cons_dev_attrs[] = {
3603 &dev_attr_active.attr,
3604 NULL
3605};
3606
3607ATTRIBUTE_GROUPS(cons_dev);
3608
3609static struct device *consdev;
3610
3611void console_sysfs_notify(void)
3612{
3613 if (consdev)
3614 sysfs_notify(&consdev->kobj, NULL, "active");
3615}
3616
3617
3618
3619
3620
3621int __init tty_init(void)
3622{
3623 cdev_init(&tty_cdev, &tty_fops);
3624 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3625 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3626 panic("Couldn't register /dev/tty driver\n");
3627 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
3628
3629 cdev_init(&console_cdev, &console_fops);
3630 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3631 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3632 panic("Couldn't register /dev/console driver\n");
3633 consdev = device_create_with_groups(tty_class, NULL,
3634 MKDEV(TTYAUX_MAJOR, 1), NULL,
3635 cons_dev_groups, "console");
3636 if (IS_ERR(consdev))
3637 consdev = NULL;
3638
3639#ifdef CONFIG_VT
3640 vty_init(&console_fops);
3641#endif
3642 return 0;
3643}
3644
3645