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