This source file includes following definitions.
- _tty_name
- tty_name
- tty_paranoia_check
- tty_register_ldisc
- tty_set_ldisc
- get_tty_driver
- tty_check_change
- hung_up_tty_read
- hung_up_tty_write
- hung_up_tty_select
- hung_up_tty_ioctl
- tty_lseek
- do_tty_hangup
- tty_hangup
- tty_vhangup
- tty_hung_up_p
- disassociate_ctty
- vt_waitactive
- complete_change_console
- change_console
- wait_for_keypress
- stop_tty
- start_tty
- tty_read
- tty_write
- init_dev
- release_dev
- tty_open
- tty_release
- tty_select
- tty_fasync
- do_get_ps_info
- tty_ioctl
- do_SAK
- flush_to_ldisc
- initialize_tty_struct
- tty_default_put_char
- tty_register_driver
- tty_init
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 #include <linux/types.h>
41 #include <linux/major.h>
42 #include <linux/errno.h>
43 #include <linux/signal.h>
44 #include <linux/fcntl.h>
45 #include <linux/sched.h>
46 #include <linux/interrupt.h>
47 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/timer.h>
50 #include <linux/ctype.h>
51 #include <linux/kd.h>
52 #include <linux/mm.h>
53 #include <linux/string.h>
54 #include <linux/malloc.h>
55
56 #include <asm/segment.h>
57 #include <asm/system.h>
58 #include <asm/bitops.h>
59
60 #include "kbd_kern.h"
61 #include "vt_kern.h"
62
63 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
64 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
65
66 #undef TTY_DEBUG_HANGUP
67
68 #ifdef CONFIG_SELECTION
69 extern int set_selection(const int arg);
70 extern int paste_selection(struct tty_struct *tty);
71 extern int sel_loadlut(const int arg);
72 extern int shift_state;
73 #endif
74 extern int do_screendump(int arg);
75
76 struct termios tty_std_termios;
77 struct tty_driver *tty_drivers;
78 struct tty_ldisc ldiscs[NR_LDISCS];
79
80
81
82
83
84
85 int fg_console = 0;
86 struct tty_struct * redirect = NULL;
87 struct wait_queue * keypress_wait = NULL;
88
89 static void initialize_tty_struct(struct tty_struct *tty);
90
91 static int tty_read(struct inode *, struct file *, char *, int);
92 static int tty_write(struct inode *, struct file *, char *, int);
93 static int tty_select(struct inode *, struct file *, int, select_table *);
94 static int tty_open(struct inode *, struct file *);
95 static void tty_release(struct inode *, struct file *);
96 static int tty_ioctl(struct inode * inode, struct file * file,
97 unsigned int cmd, unsigned long arg);
98 static int tty_fasync(struct inode * inode, struct file * filp, int on);
99
100 #ifndef MIN
101 #define MIN(a,b) ((a) < (b) ? (a) : (b))
102 #endif
103
104
105
106
107
108
109 char *_tty_name(struct tty_struct *tty, char *buf)
110 {
111 sprintf(buf, "%s%d", tty->driver.name,
112 MINOR(tty->device) - tty->driver.minor_start +
113 tty->driver.name_base);
114 return buf;
115 }
116
117 char *tty_name(struct tty_struct *tty)
118 {
119 static char buf[64];
120
121 return(_tty_name(tty, buf));
122 }
123
124 #define TTY_PARANOIA_CHECK
125
126 inline int tty_paranoia_check(struct tty_struct *tty, dev_t device,
127 const char *routine)
128 {
129 #ifdef TTY_PARANOIA_CHECK
130 static const char *badmagic =
131 "Warning: bad magic number for tty struct (%d, %d) in %s\n";
132 static const char *badtty =
133 "Warning: null TTY for (%d, %d) in %s\n";
134
135 if (!tty) {
136 printk(badtty, MAJOR(device), MINOR(device), routine);
137 return 1;
138 }
139 if (tty->magic != TTY_MAGIC) {
140 printk(badmagic, MAJOR(device), MINOR(device), routine);
141 return 1;
142 }
143 #endif
144 return 0;
145 }
146
147 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
148 {
149 if (disc < N_TTY || disc >= NR_LDISCS)
150 return -EINVAL;
151
152 if (new_ldisc) {
153 ldiscs[disc] = *new_ldisc;
154 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
155 ldiscs[disc].num = disc;
156 } else
157 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
158
159 return 0;
160 }
161
162
163 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
164 {
165 int retval = 0;
166 struct tty_ldisc o_ldisc;
167
168 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS) ||
169 !(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
170 return -EINVAL;
171
172 if (tty->ldisc.num == ldisc)
173 return 0;
174 o_ldisc = tty->ldisc;
175
176
177 if (tty->ldisc.close)
178 (tty->ldisc.close)(tty);
179
180
181 tty->ldisc = ldiscs[ldisc];
182 tty->termios->c_line = ldisc;
183 if (tty->ldisc.open)
184 retval = (tty->ldisc.open)(tty);
185 if (retval < 0) {
186 tty->ldisc = o_ldisc;
187 tty->termios->c_line = tty->ldisc.num;
188 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
189 tty->ldisc = ldiscs[N_TTY];
190 tty->termios->c_line = N_TTY;
191 if (tty->ldisc.open) {
192 int r = tty->ldisc.open(tty);
193
194 if (r < 0)
195 panic("Couldn't open N_TTY ldisc for "
196 "%s --- error %d.",
197 tty_name(tty), r);
198 }
199 }
200 }
201 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
202 tty->driver.set_ldisc(tty);
203 return retval;
204 }
205
206
207
208
209 struct tty_driver *get_tty_driver(dev_t device)
210 {
211 int major, minor;
212 struct tty_driver *p;
213
214 minor = MINOR(device);
215 major = MAJOR(device);
216
217 for (p = tty_drivers; p; p = p->next) {
218 if (p->major != major)
219 continue;
220 if (minor < p->minor_start)
221 continue;
222 if (minor >= p->minor_start + p->num)
223 continue;
224 return p;
225 }
226 return NULL;
227 }
228
229
230
231
232
233
234 int tty_check_change(struct tty_struct * tty)
235 {
236 if (current->tty != tty)
237 return 0;
238 if (tty->pgrp <= 0) {
239 printk("tty_check_change: tty->pgrp <= 0!\n");
240 return 0;
241 }
242 if (current->pgrp == tty->pgrp)
243 return 0;
244 if (is_ignored(SIGTTOU))
245 return 0;
246 if (is_orphaned_pgrp(current->pgrp))
247 return -EIO;
248 (void) kill_pg(current->pgrp,SIGTTOU,1);
249 return -ERESTARTSYS;
250 }
251
252 static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
253 {
254 return 0;
255 }
256
257 static int hung_up_tty_write(struct inode * inode, struct file * file, char * buf, int count)
258 {
259 return -EIO;
260 }
261
262 static int hung_up_tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
263 {
264 return 1;
265 }
266
267 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
268 unsigned int cmd, unsigned long arg)
269 {
270 return -EIO;
271 }
272
273 static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
274 {
275 return -ESPIPE;
276 }
277
278 static struct file_operations tty_fops = {
279 tty_lseek,
280 tty_read,
281 tty_write,
282 NULL,
283 tty_select,
284 tty_ioctl,
285 NULL,
286 tty_open,
287 tty_release,
288 NULL,
289 tty_fasync
290 };
291
292 static struct file_operations hung_up_tty_fops = {
293 tty_lseek,
294 hung_up_tty_read,
295 hung_up_tty_write,
296 NULL,
297 hung_up_tty_select,
298 hung_up_tty_ioctl,
299 NULL,
300 NULL,
301 tty_release,
302 NULL,
303 NULL
304 };
305
306 void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
307 {
308 int i;
309 struct file * filp;
310 struct task_struct *p;
311
312 if (!tty)
313 return;
314 for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
315 if (!filp->f_count)
316 continue;
317 if (filp->private_data != tty)
318 continue;
319 if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
320 continue;
321 if (filp->f_op != &tty_fops)
322 continue;
323 tty_fasync(filp->f_inode, filp, 0);
324 filp->f_op = fops;
325 }
326
327 if (tty->ldisc.flush_buffer)
328 tty->ldisc.flush_buffer(tty);
329 if (tty->driver.flush_buffer)
330 tty->driver.flush_buffer(tty);
331 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
332 tty->ldisc.write_wakeup)
333 (tty->ldisc.write_wakeup)(tty);
334 wake_up_interruptible(&tty->write_wait);
335 wake_up_interruptible(&tty->read_wait);
336
337
338
339
340
341 if (tty->ldisc.num != ldiscs[N_TTY].num) {
342 if (tty->ldisc.close)
343 (tty->ldisc.close)(tty);
344 tty->ldisc = ldiscs[N_TTY];
345 tty->termios->c_line = N_TTY;
346 if (tty->ldisc.open) {
347 i = (tty->ldisc.open)(tty);
348 if (i < 0)
349 printk("do_tty_hangup: N_TTY open: error %d\n",
350 -i);
351 }
352 }
353
354 if (tty->session > 0) {
355 kill_sl(tty->session,SIGHUP,1);
356 kill_sl(tty->session,SIGCONT,1);
357 }
358 tty->flags = 0;
359 tty->session = 0;
360 tty->pgrp = -1;
361 for_each_task(p) {
362 if (p->tty == tty)
363 p->tty = NULL;
364 }
365 if (tty->driver.hangup)
366 (tty->driver.hangup)(tty);
367 }
368
369 void tty_hangup(struct tty_struct * tty)
370 {
371 #ifdef TTY_DEBUG_HANGUP
372 printk("%s hangup...\n", tty_name(tty));
373 #endif
374 do_tty_hangup(tty, &hung_up_tty_fops);
375 }
376
377 void tty_vhangup(struct tty_struct * tty)
378 {
379 #ifdef TTY_DEBUG_HANGUP
380 printk("%s vhangup...\n", tty_name(tty));
381 #endif
382 do_tty_hangup(tty, &hung_up_tty_fops);
383 }
384
385 int tty_hung_up_p(struct file * filp)
386 {
387 return (filp->f_op == &hung_up_tty_fops);
388 }
389
390
391
392
393
394
395
396
397
398
399
400 void disassociate_ctty(int priv)
401 {
402 struct tty_struct *tty = current->tty;
403 struct task_struct *p;
404
405 if (!tty)
406 return;
407
408 if (tty->pgrp > 0) {
409 kill_pg(tty->pgrp, SIGHUP, priv);
410 kill_pg(tty->pgrp, SIGCONT, priv);
411 }
412 tty->session = 0;
413 tty->pgrp = -1;
414
415 for_each_task(p)
416 if (p->session == current->session)
417 p->tty = NULL;
418 }
419
420
421
422
423
424
425
426
427 static struct wait_queue *vt_activate_queue = NULL;
428
429
430
431
432
433 int vt_waitactive(void)
434 {
435 interruptible_sleep_on(&vt_activate_queue);
436 return (current->signal & ~current->blocked) ? -1 : 0;
437 }
438
439 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
440
441 extern int kill_proc(int pid, int sig, int priv);
442
443
444
445
446 void complete_change_console(unsigned int new_console)
447 {
448 unsigned char old_vc_mode;
449
450 if (new_console == fg_console || new_console >= NR_CONSOLES)
451 return;
452
453
454
455
456
457
458 old_vc_mode = vt_cons[fg_console].vc_mode;
459 update_screen(new_console);
460
461
462
463
464
465
466 if (vt_cons[new_console].vt_mode.mode == VT_PROCESS)
467 {
468
469
470
471
472
473 if (kill_proc(vt_cons[new_console].vt_pid,
474 vt_cons[new_console].vt_mode.acqsig,
475 1) != 0)
476 {
477
478
479
480
481
482
483
484
485
486 vt_cons[new_console].vc_mode = KD_TEXT;
487 clr_vc_kbd_mode(kbd_table + new_console, VC_RAW);
488 clr_vc_kbd_mode(kbd_table + new_console, VC_MEDIUMRAW);
489 vt_cons[new_console].vt_mode.mode = VT_AUTO;
490 vt_cons[new_console].vt_mode.waitv = 0;
491 vt_cons[new_console].vt_mode.relsig = 0;
492 vt_cons[new_console].vt_mode.acqsig = 0;
493 vt_cons[new_console].vt_mode.frsig = 0;
494 vt_cons[new_console].vt_pid = -1;
495 vt_cons[new_console].vt_newvt = -1;
496 }
497 }
498
499
500
501
502
503 if (old_vc_mode != vt_cons[new_console].vc_mode)
504 {
505 if (vt_cons[new_console].vc_mode == KD_TEXT)
506 unblank_screen();
507 else {
508 timer_active &= ~(1<<BLANK_TIMER);
509 blank_screen();
510 }
511 }
512
513
514
515
516 vt_wake_waitactive();
517 return;
518 }
519
520
521
522
523 void change_console(unsigned int new_console)
524 {
525 if (new_console == fg_console || new_console >= NR_CONSOLES)
526 return;
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543 if (vt_cons[fg_console].vt_mode.mode == VT_PROCESS)
544 {
545
546
547
548
549
550 if (kill_proc(vt_cons[fg_console].vt_pid,
551 vt_cons[fg_console].vt_mode.relsig,
552 1) == 0)
553 {
554
555
556
557
558
559 vt_cons[fg_console].vt_newvt = new_console;
560 return;
561 }
562
563
564
565
566
567
568
569
570
571
572 vt_cons[fg_console].vc_mode = KD_TEXT;
573 clr_vc_kbd_mode(kbd_table + fg_console, VC_RAW);
574 clr_vc_kbd_mode(kbd_table + fg_console, VC_MEDIUMRAW);
575 vt_cons[fg_console].vt_mode.mode = VT_AUTO;
576 vt_cons[fg_console].vt_mode.waitv = 0;
577 vt_cons[fg_console].vt_mode.relsig = 0;
578 vt_cons[fg_console].vt_mode.acqsig = 0;
579 vt_cons[fg_console].vt_mode.frsig = 0;
580 vt_cons[fg_console].vt_pid = -1;
581 vt_cons[fg_console].vt_newvt = -1;
582
583
584
585 }
586
587
588
589
590 if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
591 return;
592
593 complete_change_console(new_console);
594 }
595
596 void wait_for_keypress(void)
597 {
598 sleep_on(&keypress_wait);
599 }
600
601 void stop_tty(struct tty_struct *tty)
602 {
603 if (tty->stopped)
604 return;
605 tty->stopped = 1;
606 if (tty->link && tty->link->packet) {
607 tty->ctrl_status &= ~TIOCPKT_START;
608 tty->ctrl_status |= TIOCPKT_STOP;
609 wake_up_interruptible(&tty->link->read_wait);
610 }
611 if (tty->driver.stop)
612 (tty->driver.stop)(tty);
613 }
614
615 void start_tty(struct tty_struct *tty)
616 {
617 if (!tty->stopped)
618 return;
619 tty->stopped = 0;
620 if (tty->link && tty->link->packet) {
621 tty->ctrl_status &= ~TIOCPKT_STOP;
622 tty->ctrl_status |= TIOCPKT_START;
623 wake_up_interruptible(&tty->link->read_wait);
624 }
625 if (tty->driver.start)
626 (tty->driver.start)(tty);
627 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
628 tty->ldisc.write_wakeup)
629 (tty->ldisc.write_wakeup)(tty);
630 wake_up_interruptible(&tty->write_wait);
631 }
632
633 static int tty_read(struct inode * inode, struct file * file, char * buf, int count)
634 {
635 int i;
636 struct tty_struct * tty;
637
638 tty = file->private_data;
639 if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
640 return -EIO;
641 if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
642 return -EIO;
643
644
645
646
647
648 #if 0
649 if ((inode->i_rdev != CONSOLE_DEV) &&
650 (tty->pgrp > 0) &&
651 (current->tty == tty) &&
652 (tty->pgrp != current->pgrp))
653 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
654 return -EIO;
655 else {
656 (void) kill_pg(current->pgrp, SIGTTIN, 1);
657 return -ERESTARTSYS;
658 }
659 #endif
660 if (tty->ldisc.read)
661
662 i = (tty->ldisc.read)(tty,file,(unsigned char *)buf,(unsigned int)count);
663 else
664 i = -EIO;
665 if (i > 0)
666 inode->i_atime = CURRENT_TIME;
667 return i;
668 }
669
670 static int tty_write(struct inode * inode, struct file * file, char * buf, int count)
671 {
672 int i, is_console;
673 struct tty_struct * tty;
674
675 is_console = (inode->i_rdev == CONSOLE_DEV);
676
677 if (is_console && redirect)
678 tty = redirect;
679 else
680 tty = file->private_data;
681 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
682 return -EIO;
683 if (!tty || !tty->driver.write || (tty->flags & (1 << TTY_IO_ERROR)))
684 return -EIO;
685 #if 0
686 if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
687 (current->tty == tty) && (tty->pgrp != current->pgrp)) {
688 if (is_orphaned_pgrp(current->pgrp))
689 return -EIO;
690 if (!is_ignored(SIGTTOU)) {
691 (void) kill_pg(current->pgrp, SIGTTOU, 1);
692 return -ERESTARTSYS;
693 }
694 }
695 #endif
696 if (tty->ldisc.write)
697
698 i = (tty->ldisc.write)(tty,file,(unsigned char *)buf,(unsigned int)count);
699 else
700 i = -EIO;
701 if (i > 0)
702 inode->i_mtime = CURRENT_TIME;
703 return i;
704 }
705
706
707
708
709
710
711 static int init_dev(dev_t device, struct tty_struct **ret_tty)
712 {
713 struct tty_struct *tty, **tty_loc, *o_tty, **o_tty_loc;
714 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
715 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
716 struct tty_driver *driver;
717 int retval;
718 int idx;
719
720 driver = get_tty_driver(device);
721 if (!driver)
722 return -ENODEV;
723
724 idx = MINOR(device) - driver->minor_start;
725 tty = o_tty = NULL;
726 tp = o_tp = NULL;
727 ltp = o_ltp = NULL;
728 o_tty_loc = NULL;
729 o_tp_loc = o_ltp_loc = NULL;
730
731 tty_loc = &driver->table[idx];
732 tp_loc = &driver->termios[idx];
733 ltp_loc = &driver->termios_locked[idx];
734
735 repeat:
736 retval = -EAGAIN;
737 if (driver->type == TTY_DRIVER_TYPE_PTY &&
738 driver->subtype == PTY_TYPE_MASTER &&
739 *tty_loc && (*tty_loc)->count)
740 goto end_init;
741 retval = -ENOMEM;
742 if (!*tty_loc && !tty) {
743 if (!(tty = (struct tty_struct*) get_free_page(GFP_KERNEL)))
744 goto end_init;
745 initialize_tty_struct(tty);
746 tty->device = device;
747 tty->driver = *driver;
748 goto repeat;
749 }
750 if (!*tp_loc && !tp) {
751 tp = (struct termios *) kmalloc(sizeof(struct termios),
752 GFP_KERNEL);
753 if (!tp)
754 goto end_init;
755 *tp = driver->init_termios;
756 goto repeat;
757 }
758 if (!*ltp_loc && !ltp) {
759 ltp = (struct termios *) kmalloc(sizeof(struct termios),
760 GFP_KERNEL);
761 if (!ltp)
762 goto end_init;
763 memset(ltp, 0, sizeof(struct termios));
764 goto repeat;
765 }
766 if (driver->type == TTY_DRIVER_TYPE_PTY) {
767 o_tty_loc = &driver->other->table[idx];
768 o_tp_loc = &driver->other->termios[idx];
769 o_ltp_loc = &driver->other->termios_locked[idx];
770
771 if (!*o_tty_loc && !o_tty) {
772 dev_t o_device;
773
774 o_tty = (struct tty_struct *)
775 get_free_page(GFP_KERNEL);
776 if (!o_tty)
777 goto end_init;
778 o_device = MKDEV(driver->other->major,
779 driver->other->minor_start + idx);
780 initialize_tty_struct(o_tty);
781 o_tty->device = o_device;
782 o_tty->driver = *driver->other;
783 goto repeat;
784 }
785 if (!*o_tp_loc && !o_tp) {
786 o_tp = (struct termios *)
787 kmalloc(sizeof(struct termios), GFP_KERNEL);
788 if (!o_tp)
789 goto end_init;
790 *o_tp = driver->other->init_termios;
791 goto repeat;
792 }
793 if (!*o_ltp_loc && !o_ltp) {
794 o_ltp = (struct termios *)
795 kmalloc(sizeof(struct termios), GFP_KERNEL);
796 if (!o_ltp)
797 goto end_init;
798 memset(o_ltp, 0, sizeof(struct termios));
799 goto repeat;
800 }
801
802 }
803
804 if (!*tp_loc) {
805 *tp_loc = tp;
806 tp = NULL;
807 }
808 if (!*ltp_loc) {
809 *ltp_loc = ltp;
810 ltp = NULL;
811 }
812 if (!*tty_loc) {
813 tty->termios = *tp_loc;
814 tty->termios_locked = *ltp_loc;
815 *tty_loc = tty;
816 (*driver->refcount)++;
817 (*tty_loc)->count++;
818 if (tty->ldisc.open) {
819 retval = (tty->ldisc.open)(tty);
820 if (retval < 0) {
821 (*tty_loc)->count--;
822 tty = NULL;
823 goto end_init;
824 }
825 }
826 tty = NULL;
827 } else
828 (*tty_loc)->count++;
829 if (driver->type == TTY_DRIVER_TYPE_PTY) {
830 if (!*o_tp_loc) {
831 *o_tp_loc = o_tp;
832 o_tp = NULL;
833 }
834 if (!*o_ltp_loc) {
835 *o_ltp_loc = o_ltp;
836 o_ltp = NULL;
837 }
838 if (!*o_tty_loc) {
839 o_tty->termios = *o_tp_loc;
840 o_tty->termios_locked = *o_ltp_loc;
841 *o_tty_loc = o_tty;
842 (*driver->other->refcount)++;
843 if (o_tty->ldisc.open) {
844 retval = (o_tty->ldisc.open)(o_tty);
845 if (retval < 0) {
846 (*tty_loc)->count--;
847 o_tty = NULL;
848 goto end_init;
849 }
850 }
851 o_tty = NULL;
852 }
853 (*tty_loc)->link = *o_tty_loc;
854 (*o_tty_loc)->link = *tty_loc;
855 if (driver->subtype == PTY_TYPE_MASTER)
856 (*o_tty_loc)->count++;
857 }
858 (*tty_loc)->driver = *driver;
859 *ret_tty = *tty_loc;
860 retval = 0;
861 end_init:
862 if (tty)
863 free_page((unsigned long) tty);
864 if (o_tty)
865 free_page((unsigned long) o_tty);
866 if (tp)
867 kfree_s(tp, sizeof(struct termios));
868 if (o_tp)
869 kfree_s(o_tp, sizeof(struct termios));
870 if (ltp)
871 kfree_s(ltp, sizeof(struct termios));
872 if (o_ltp)
873 kfree_s(o_ltp, sizeof(struct termios));
874 return retval;
875 }
876
877
878
879
880
881
882 static void release_dev(struct file * filp)
883 {
884 struct tty_struct *tty, *o_tty;
885 struct termios *tp, *o_tp, *ltp, *o_ltp;
886 struct task_struct **p;
887 int idx;
888
889
890 tty = filp->private_data;
891 if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "release_dev"))
892 return;
893
894 tty_fasync(filp->f_inode, filp, 0);
895
896 tp = tty->termios;
897 ltp = tty->termios_locked;
898
899 idx = MINOR(tty->device) - tty->driver.minor_start;
900 #ifdef TTY_PARANOIA_CHECK
901 if (idx < 0 || idx >= tty->driver.num) {
902 printk("release_dev: bad idx when trying to free (%d, %d)\n",
903 MAJOR(tty->device), MINOR(tty->device));
904 return;
905 }
906 if (tty != tty->driver.table[idx]) {
907 printk("release_dev: driver.table[%d] not tty for (%d, %d)\n",
908 idx, MAJOR(tty->device), MINOR(tty->device));
909 return;
910 }
911 if (tp != tty->driver.termios[idx]) {
912 printk("release_dev: driver.termios[%d] not termios for (%d, %d)\n",
913 idx, MAJOR(tty->device), MINOR(tty->device));
914 return;
915 }
916 if (ltp != tty->driver.termios_locked[idx]) {
917 printk("release_dev: driver.termios_locked[%d] not termios_locked for (%d, %d)\n",
918 idx, MAJOR(tty->device), MINOR(tty->device));
919 return;
920 }
921 #endif
922
923 #ifdef TTY_DEBUG_HANGUP
924 printk("release_dev of %s (tty count=%d)...", tty_name(tty),
925 tty->count);
926 #endif
927
928 o_tty = tty->link;
929 o_tp = (o_tty) ? o_tty->termios : NULL;
930 o_ltp = (o_tty) ? o_tty->termios_locked : NULL;
931
932 #ifdef TTY_PARANOIA_CHECK
933 if (tty->driver.other) {
934 if (o_tty != tty->driver.other->table[idx]) {
935 printk("release_dev: other->table[%d] not o_tty for (%d, %d)\n",
936 idx, MAJOR(tty->device), MINOR(tty->device));
937 return;
938 }
939 if (o_tp != tty->driver.other->termios[idx]) {
940 printk("release_dev: other->termios[%d] not o_termios for (%d, %d)\n",
941 idx, MAJOR(tty->device), MINOR(tty->device));
942 return;
943 }
944 if (o_ltp != tty->driver.other->termios_locked[idx]) {
945 printk("release_dev: other->termios_locked[%d] not o_termios_locked for (%d, %d)\n",
946 idx, MAJOR(tty->device), MINOR(tty->device));
947 return;
948 }
949
950 if (o_tty->link != tty) {
951 printk("release_dev: bad pty pointers\n");
952 return;
953 }
954 }
955 #endif
956
957 if (tty->driver.close)
958 tty->driver.close(tty, filp);
959 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
960 tty->driver.subtype == PTY_TYPE_MASTER) {
961 if (--tty->link->count < 0) {
962 printk("release_dev: bad pty slave count (%d) for %s\n",
963 tty->count, tty_name(tty));
964 tty->link->count = 0;
965 }
966 }
967 if (--tty->count < 0) {
968 printk("release_dev: bad tty->count (%d) for %s\n",
969 tty->count, tty_name(tty));
970 tty->count = 0;
971 }
972 if (tty->count)
973 return;
974
975
976
977
978
979 for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
980 if ((*p) && (*p)->tty == tty)
981 (*p)->tty = NULL;
982 }
983
984 if (o_tty) {
985 if (o_tty->count)
986 return;
987 tty->driver.other->table[idx] = NULL;
988 tty->driver.other->termios[idx] = NULL;
989 tty->driver.other->termios_locked[idx] = NULL;
990 }
991
992 #ifdef TTY_DEBUG_HANGUP
993 printk("freeing tty structure...");
994 #endif
995
996
997
998
999
1000 if (tty->ldisc.close)
1001 (tty->ldisc.close)(tty);
1002 tty->ldisc = ldiscs[N_TTY];
1003 tty->termios->c_line = N_TTY;
1004
1005 tty->driver.table[idx] = NULL;
1006 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1007 tty->driver.termios[idx] = NULL;
1008 tty->driver.termios_locked[idx] = NULL;
1009 kfree_s(tp, sizeof(struct termios));
1010 kfree_s(ltp, sizeof(struct termios));
1011 }
1012 if (tty == redirect || o_tty == redirect)
1013 redirect = NULL;
1014
1015
1016
1017
1018 cli();
1019 if (tty->flip.tqueue.sync) {
1020 struct tq_struct *tq, *prev;
1021
1022 for (tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
1023 if (tq == &tty->flip.tqueue) {
1024 if (prev)
1025 prev->next = tq->next;
1026 else
1027 tq_timer = tq->next;
1028 break;
1029 }
1030 }
1031 }
1032 sti();
1033 tty->magic = 0;
1034 (*tty->driver.refcount)--;
1035 free_page((unsigned long) tty);
1036 if (o_tty) {
1037 o_tty->magic = 0;
1038 (*o_tty->driver.refcount)--;
1039 free_page((unsigned long) o_tty);
1040 }
1041 if (o_tp)
1042 kfree_s(o_tp, sizeof(struct termios));
1043 if (o_ltp)
1044 kfree_s(o_ltp, sizeof(struct termios));
1045 }
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059 static int tty_open(struct inode * inode, struct file * filp)
1060 {
1061 struct tty_struct *tty;
1062 int minor;
1063 int noctty, retval;
1064 dev_t device;
1065
1066 retry_open:
1067 noctty = filp->f_flags & O_NOCTTY;
1068 device = inode->i_rdev;
1069 if (device == TTY_DEV) {
1070 if (!current->tty)
1071 return -ENXIO;
1072 device = current->tty->device;
1073
1074 }
1075 if (device == CONSOLE_DEV) {
1076 device = MKDEV(TTY_MAJOR, fg_console+1);
1077 noctty = 1;
1078 }
1079 minor = MINOR(device);
1080
1081 retval = init_dev(device, &tty);
1082 filp->private_data = tty;
1083 if (retval)
1084 return retval;
1085 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1086 tty->driver.subtype == PTY_TYPE_MASTER)
1087 noctty = 1;
1088 #ifdef TTY_DEBUG_HANGUP
1089 printk("opening %s...", tty_name(tty));
1090 #endif
1091 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1092 retval = -EBUSY;
1093 else if (tty->driver.open)
1094 retval = tty->driver.open(tty, filp);
1095 else
1096 retval = -ENODEV;
1097
1098 if (retval) {
1099 #ifdef TTY_DEBUG_HANGUP
1100 printk("error %d in opening %s...", retval, tty_name(tty));
1101 #endif
1102
1103 release_dev(filp);
1104 if (retval != -ERESTARTSYS)
1105 return retval;
1106 if (current->signal & ~current->blocked)
1107 return retval;
1108 schedule();
1109
1110
1111
1112 filp->f_op = &tty_fops;
1113 goto retry_open;
1114 }
1115 if (!noctty &&
1116 current->leader &&
1117 !current->tty &&
1118 tty->session == 0) {
1119 current->tty = tty;
1120 tty->session = current->session;
1121 tty->pgrp = current->pgrp;
1122 }
1123 return 0;
1124 }
1125
1126
1127
1128
1129
1130
1131 static void tty_release(struct inode * inode, struct file * filp)
1132 {
1133 release_dev(filp);
1134 }
1135
1136 static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
1137 {
1138 struct tty_struct * tty;
1139
1140 tty = filp->private_data;
1141 if (tty_paranoia_check(tty, inode->i_rdev, "tty_select"))
1142 return 0;
1143
1144 if (tty->ldisc.select)
1145 return (tty->ldisc.select)(tty, inode, filp, sel_type, wait);
1146 return 0;
1147 }
1148
1149 static int tty_fasync(struct inode * inode, struct file * filp, int on)
1150 {
1151 struct tty_struct * tty;
1152 struct fasync_struct *fa, *prev;
1153
1154 tty = filp->private_data;
1155 if (tty_paranoia_check(tty, inode->i_rdev, "tty_fasync"))
1156 return 0;
1157
1158 for (fa = tty->fasync, prev = 0; fa; prev= fa, fa = fa->fa_next) {
1159 if (fa->fa_file == filp)
1160 break;
1161 }
1162
1163 if (on) {
1164 if (fa)
1165 return 0;
1166 fa = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1167 if (!fa)
1168 return -ENOMEM;
1169 fa->magic = FASYNC_MAGIC;
1170 fa->fa_file = filp;
1171 fa->fa_next = tty->fasync;
1172 tty->fasync = fa;
1173 if (!tty->read_wait)
1174 tty->minimum_to_wake = 1;
1175 } else {
1176 if (!fa)
1177 return 0;
1178 if (prev)
1179 prev->fa_next = fa->fa_next;
1180 else
1181 tty->fasync = fa->fa_next;
1182 kfree_s(fa, sizeof(struct fasync_struct));
1183 if (!tty->fasync && !tty->read_wait)
1184 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1185 }
1186 return 0;
1187 }
1188
1189
1190
1191
1192 static int do_get_ps_info(int arg)
1193 {
1194 struct tstruct {
1195 int flag;
1196 int present[NR_TASKS];
1197 struct task_struct tasks[NR_TASKS];
1198 };
1199 struct tstruct *ts = (struct tstruct *)arg;
1200 struct task_struct **p;
1201 char *c, *d;
1202 int i, n = 0;
1203
1204 i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct tstruct));
1205 if (i)
1206 return i;
1207 for (p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
1208 if (*p)
1209 {
1210 c = (char *)(*p);
1211 d = (char *)(ts->tasks+n);
1212 for (i=0 ; i<sizeof(struct task_struct) ; i++)
1213 put_fs_byte(*c++, d++);
1214 put_fs_long(1, (unsigned long *)(ts->present+n));
1215 }
1216 else
1217 put_fs_long(0, (unsigned long *)(ts->present+n));
1218 return(0);
1219 }
1220
1221 static int tty_ioctl(struct inode * inode, struct file * file,
1222 unsigned int cmd, unsigned long arg)
1223 {
1224 int retval;
1225 struct tty_struct * tty;
1226 struct tty_struct * real_tty;
1227 struct winsize tmp_ws;
1228 pid_t pgrp;
1229 unsigned char ch;
1230 char mbz = 0;
1231
1232 tty = file->private_data;
1233 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1234 return -EINVAL;
1235
1236 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1237 tty->driver.subtype == PTY_TYPE_MASTER)
1238 real_tty = tty->link;
1239 else
1240 real_tty = tty;
1241
1242 switch (cmd) {
1243 case TIOCSTI:
1244 if ((current->tty != tty) && !suser())
1245 return -EPERM;
1246 ch = get_fs_byte((char *) arg);
1247 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1248 return 0;
1249 case TIOCGWINSZ:
1250 retval = verify_area(VERIFY_WRITE, (void *) arg,
1251 sizeof (struct winsize));
1252 if (retval)
1253 return retval;
1254 memcpy_tofs((struct winsize *) arg, &tty->winsize,
1255 sizeof (struct winsize));
1256 return 0;
1257 case TIOCSWINSZ:
1258 memcpy_fromfs(&tmp_ws, (struct winsize *) arg,
1259 sizeof (struct winsize));
1260 if (memcmp(&tmp_ws, &tty->winsize,
1261 sizeof(struct winsize))) {
1262 if (tty->pgrp > 0)
1263 kill_pg(tty->pgrp, SIGWINCH, 1);
1264 if ((real_tty->pgrp != tty->pgrp) &&
1265 (real_tty->pgrp > 0))
1266 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1267 }
1268 tty->winsize = tmp_ws;
1269 real_tty->winsize = tmp_ws;
1270 return 0;
1271 case TIOCCONS:
1272 if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
1273 if (!suser())
1274 return -EPERM;
1275 redirect = NULL;
1276 return 0;
1277 }
1278 if (redirect)
1279 return -EBUSY;
1280 redirect = real_tty;
1281 return 0;
1282 case FIONBIO:
1283 arg = get_fs_long((unsigned long *) arg);
1284 if (arg)
1285 file->f_flags |= O_NONBLOCK;
1286 else
1287 file->f_flags &= ~O_NONBLOCK;
1288 return 0;
1289 case TIOCEXCL:
1290 set_bit(TTY_EXCLUSIVE, &tty->flags);
1291 return 0;
1292 case TIOCNXCL:
1293 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1294 return 0;
1295 case TIOCNOTTY:
1296 if (current->tty != tty)
1297 return -ENOTTY;
1298 if (current->leader)
1299 disassociate_ctty(0);
1300 current->tty = NULL;
1301 return 0;
1302 case TIOCSCTTY:
1303 if (current->leader &&
1304 (current->session == tty->session))
1305 return 0;
1306
1307
1308
1309
1310 if (!current->leader || current->tty)
1311 return -EPERM;
1312 if (tty->session > 0) {
1313
1314
1315
1316
1317 if ((arg == 1) && suser()) {
1318
1319
1320
1321 struct task_struct *p;
1322
1323 for_each_task(p)
1324 if (p->tty == tty)
1325 p->tty = NULL;
1326 } else
1327 return -EPERM;
1328 }
1329 current->tty = tty;
1330 tty->session = current->session;
1331 tty->pgrp = current->pgrp;
1332 return 0;
1333 case TIOCGPGRP:
1334
1335
1336
1337
1338 if (tty == real_tty && current->tty != real_tty)
1339 return -ENOTTY;
1340 retval = verify_area(VERIFY_WRITE, (void *) arg,
1341 sizeof (pid_t));
1342 if (retval)
1343 return retval;
1344 put_fs_long(real_tty->pgrp, (pid_t *) arg);
1345 return 0;
1346 case TIOCSPGRP:
1347 retval = tty_check_change(real_tty);
1348 if (retval)
1349 return retval;
1350 if (!current->tty ||
1351 (current->tty != real_tty) ||
1352 (real_tty->session != current->session))
1353 return -ENOTTY;
1354 pgrp = get_fs_long((pid_t *) arg);
1355 if (pgrp < 0)
1356 return -EINVAL;
1357 if (session_of_pgrp(pgrp) != current->session)
1358 return -EPERM;
1359 real_tty->pgrp = pgrp;
1360 return 0;
1361 case TIOCGETD:
1362 retval = verify_area(VERIFY_WRITE, (void *) arg,
1363 sizeof (unsigned long));
1364 if (retval)
1365 return retval;
1366 put_fs_long(tty->ldisc.num, (unsigned long *) arg);
1367 return 0;
1368 case TIOCSETD:
1369 retval = tty_check_change(tty);
1370 if (retval)
1371 return retval;
1372 arg = get_fs_long((unsigned long *) arg);
1373 return tty_set_ldisc(tty, arg);
1374 case TIOCLINUX:
1375 switch (get_fs_byte((char *)arg))
1376 {
1377 case 0:
1378 return do_screendump(arg);
1379 case 1:
1380 printk("Deprecated TIOCLINUX (1) ioctl\n");
1381 return do_get_ps_info(arg);
1382 #ifdef CONFIG_SELECTION
1383 case 2:
1384 return set_selection(arg);
1385 case 3:
1386 return paste_selection(tty);
1387 case 4:
1388 unblank_screen();
1389 return 0;
1390 case 5:
1391 return sel_loadlut(arg);
1392 case 6:
1393 put_fs_byte(shift_state,arg);
1394 return 0;
1395 #endif
1396 default:
1397 return -EINVAL;
1398 }
1399 default:
1400 if (tty->driver.ioctl) {
1401 retval = (tty->driver.ioctl)(tty, file,
1402 cmd, arg);
1403 if (retval != -ENOIOCTLCMD)
1404 return retval;
1405 }
1406 if (tty->ldisc.ioctl) {
1407 retval = (tty->ldisc.ioctl)(tty, file,
1408 cmd, arg);
1409 if (retval != -ENOIOCTLCMD)
1410 return retval;
1411 }
1412 return -EINVAL;
1413 }
1414 }
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429 void do_SAK( struct tty_struct *tty)
1430 {
1431 #ifdef TTY_SOFT_SAK
1432 tty_hangup(tty);
1433 #else
1434 struct task_struct **p;
1435 int session = tty->session;
1436 int i;
1437 struct file *filp;
1438
1439 if (tty->ldisc.flush_buffer)
1440 tty->ldisc.flush_buffer(tty);
1441 if (tty->driver.flush_buffer)
1442 tty->driver.flush_buffer(tty);
1443 for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1444 if (!(*p))
1445 continue;
1446 if (((*p)->tty == tty) ||
1447 ((session > 0) && ((*p)->session == session)))
1448 send_sig(SIGKILL, *p, 1);
1449 else {
1450 for (i=0; i < NR_OPEN; i++) {
1451 filp = (*p)->files->fd[i];
1452 if (filp && (filp->f_op == &tty_fops) &&
1453 (filp->private_data == tty)) {
1454 send_sig(SIGKILL, *p, 1);
1455 break;
1456 }
1457 }
1458 }
1459 }
1460 #endif
1461 }
1462
1463
1464
1465
1466
1467 static void flush_to_ldisc(void *private)
1468 {
1469 struct tty_struct *tty = private;
1470 unsigned char *cp;
1471 char *fp;
1472 int count;
1473
1474 if (tty->flip.buf_num) {
1475 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1476 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1477 tty->flip.buf_num = 0;
1478
1479 cli();
1480 tty->flip.char_buf_ptr = tty->flip.char_buf;
1481 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1482 } else {
1483 cp = tty->flip.char_buf;
1484 fp = tty->flip.flag_buf;
1485 tty->flip.buf_num = 1;
1486
1487 cli();
1488 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1489 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1490 }
1491 count = tty->flip.count;
1492 tty->flip.count = 0;
1493 sti();
1494
1495 #if 0
1496 if (count > tty->max_flip_cnt)
1497 tty->max_flip_cnt = count;
1498 #endif
1499 tty->ldisc.receive_buf(tty, cp, fp, count);
1500 }
1501
1502
1503
1504
1505 static void initialize_tty_struct(struct tty_struct *tty)
1506 {
1507 memset(tty, 0, sizeof(struct tty_struct));
1508 tty->magic = TTY_MAGIC;
1509 tty->ldisc = ldiscs[N_TTY];
1510 tty->pgrp = -1;
1511 tty->flip.char_buf_ptr = tty->flip.char_buf;
1512 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1513 tty->flip.tqueue.routine = flush_to_ldisc;
1514 tty->flip.tqueue.data = tty;
1515 }
1516
1517
1518
1519
1520 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
1521 {
1522 tty->driver.write(tty, 0, &ch, 1);
1523 }
1524
1525
1526
1527
1528 int tty_register_driver(struct tty_driver *driver)
1529 {
1530 if (driver->flags & TTY_DRIVER_INSTALLED)
1531 return 0;
1532
1533
1534
1535
1536 (void) register_chrdev(driver->major, driver->name, &tty_fops);
1537
1538 if (!driver->put_char)
1539 driver->put_char = tty_default_put_char;
1540
1541 driver->prev = 0;
1542 driver->next = tty_drivers;
1543 tty_drivers->prev = driver;
1544 tty_drivers = driver;
1545 return 0;
1546 }
1547
1548 long tty_init(long kmem_start)
1549 {
1550 if (sizeof(struct tty_struct) > PAGE_SIZE)
1551 panic("size of tty structure > PAGE_SIZE!");
1552 if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
1553 panic("unable to get major %d for tty device", TTY_MAJOR);
1554 if (register_chrdev(TTYAUX_MAJOR,"tty",&tty_fops))
1555 panic("unable to get major %d for tty device", TTYAUX_MAJOR);
1556 tty_drivers = 0;
1557
1558
1559 memset(ldiscs, 0, sizeof(ldiscs));
1560 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1561
1562
1563
1564
1565
1566 memset(&tty_std_termios, 0, sizeof(struct termios));
1567 memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
1568 tty_std_termios.c_iflag = ICRNL | IXON;
1569 tty_std_termios.c_oflag = OPOST | ONLCR;
1570 tty_std_termios.c_cflag = B38400 | CS8 | CREAD;
1571 tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1572 ECHOCTL | ECHOKE | IEXTEN;
1573
1574 kmem_start = con_init(kmem_start);
1575 kmem_start = kbd_init(kmem_start);
1576 kmem_start = rs_init(kmem_start);
1577 kmem_start = pty_init(kmem_start);
1578 return kmem_start;
1579 }