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
- console_init
- 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, struct tty_struct *tty);
70 extern int paste_selection(struct tty_struct *tty);
71 extern int sel_loadlut(const int arg);
72 extern int mouse_reporting(void);
73 extern int shift_state;
74 #endif
75 extern int do_screendump(int arg);
76
77 struct termios tty_std_termios;
78 struct tty_driver *tty_drivers = NULL;
79 struct tty_ldisc ldiscs[NR_LDISCS];
80
81
82
83
84
85
86 int fg_console = 0;
87 struct tty_struct * redirect = NULL;
88 struct wait_queue * keypress_wait = NULL;
89
90 static void initialize_tty_struct(struct tty_struct *tty);
91
92 static int tty_read(struct inode *, struct file *, char *, int);
93 static int tty_write(struct inode *, struct file *, char *, int);
94 static int tty_select(struct inode *, struct file *, int, select_table *);
95 static int tty_open(struct inode *, struct file *);
96 static void tty_release(struct inode *, struct file *);
97 static int tty_ioctl(struct inode * inode, struct file * file,
98 unsigned int cmd, unsigned long arg);
99 static int tty_fasync(struct inode * inode, struct file * filp, int on);
100
101 #ifndef MIN
102 #define MIN(a,b) ((a) < (b) ? (a) : (b))
103 #endif
104
105
106
107
108
109
110 char *_tty_name(struct tty_struct *tty, char *buf)
111 {
112 sprintf(buf, "%s%d", tty->driver.name,
113 MINOR(tty->device) - tty->driver.minor_start +
114 tty->driver.name_base);
115 return buf;
116 }
117
118 char *tty_name(struct tty_struct *tty)
119 {
120 static char buf[64];
121
122 return(_tty_name(tty, buf));
123 }
124
125 #define TTY_PARANOIA_CHECK
126
127 inline int tty_paranoia_check(struct tty_struct *tty, dev_t device,
128 const char *routine)
129 {
130 #ifdef TTY_PARANOIA_CHECK
131 static const char *badmagic =
132 "Warning: bad magic number for tty struct (%d, %d) in %s\n";
133 static const char *badtty =
134 "Warning: null TTY for (%d, %d) in %s\n";
135
136 if (!tty) {
137 printk(badtty, MAJOR(device), MINOR(device), routine);
138 return 1;
139 }
140 if (tty->magic != TTY_MAGIC) {
141 printk(badmagic, MAJOR(device), MINOR(device), routine);
142 return 1;
143 }
144 #endif
145 return 0;
146 }
147
148 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
149 {
150 if (disc < N_TTY || disc >= NR_LDISCS)
151 return -EINVAL;
152
153 if (new_ldisc) {
154 ldiscs[disc] = *new_ldisc;
155 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
156 ldiscs[disc].num = disc;
157 } else
158 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
159
160 return 0;
161 }
162
163
164 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
165 {
166 int retval = 0;
167 struct tty_ldisc o_ldisc;
168
169 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS) ||
170 !(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
171 return -EINVAL;
172
173 if (tty->ldisc.num == ldisc)
174 return 0;
175 o_ldisc = tty->ldisc;
176
177
178 if (tty->ldisc.close)
179 (tty->ldisc.close)(tty);
180
181
182 tty->ldisc = ldiscs[ldisc];
183 tty->termios->c_line = ldisc;
184 if (tty->ldisc.open)
185 retval = (tty->ldisc.open)(tty);
186 if (retval < 0) {
187 tty->ldisc = o_ldisc;
188 tty->termios->c_line = tty->ldisc.num;
189 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
190 tty->ldisc = ldiscs[N_TTY];
191 tty->termios->c_line = N_TTY;
192 if (tty->ldisc.open) {
193 int r = tty->ldisc.open(tty);
194
195 if (r < 0)
196 panic("Couldn't open N_TTY ldisc for "
197 "%s --- error %d.",
198 tty_name(tty), r);
199 }
200 }
201 }
202 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
203 tty->driver.set_ldisc(tty);
204 return retval;
205 }
206
207
208
209
210 struct tty_driver *get_tty_driver(dev_t device)
211 {
212 int major, minor;
213 struct tty_driver *p;
214
215 minor = MINOR(device);
216 major = MAJOR(device);
217
218 for (p = tty_drivers; p; p = p->next) {
219 if (p->major != major)
220 continue;
221 if (minor < p->minor_start)
222 continue;
223 if (minor >= p->minor_start + p->num)
224 continue;
225 return p;
226 }
227 return NULL;
228 }
229
230
231
232
233
234
235 int tty_check_change(struct tty_struct * tty)
236 {
237 if (current->tty != tty)
238 return 0;
239 if (tty->pgrp <= 0) {
240 printk("tty_check_change: tty->pgrp <= 0!\n");
241 return 0;
242 }
243 if (current->pgrp == tty->pgrp)
244 return 0;
245 if (is_ignored(SIGTTOU))
246 return 0;
247 if (is_orphaned_pgrp(current->pgrp))
248 return -EIO;
249 (void) kill_pg(current->pgrp,SIGTTOU,1);
250 return -ERESTARTSYS;
251 }
252
253 static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
254 {
255 return 0;
256 }
257
258 static int hung_up_tty_write(struct inode * inode, struct file * file, char * buf, int count)
259 {
260 return -EIO;
261 }
262
263 static int hung_up_tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
264 {
265 return 1;
266 }
267
268 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
269 unsigned int cmd, unsigned long arg)
270 {
271 return -EIO;
272 }
273
274 static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
275 {
276 return -ESPIPE;
277 }
278
279 static struct file_operations tty_fops = {
280 tty_lseek,
281 tty_read,
282 tty_write,
283 NULL,
284 tty_select,
285 tty_ioctl,
286 NULL,
287 tty_open,
288 tty_release,
289 NULL,
290 tty_fasync
291 };
292
293 static struct file_operations hung_up_tty_fops = {
294 tty_lseek,
295 hung_up_tty_read,
296 hung_up_tty_write,
297 NULL,
298 hung_up_tty_select,
299 hung_up_tty_ioctl,
300 NULL,
301 NULL,
302 tty_release,
303 NULL,
304 NULL
305 };
306
307 void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
308 {
309 int i;
310 struct file * filp;
311 struct task_struct *p;
312
313 if (!tty)
314 return;
315 for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
316 if (!filp->f_count)
317 continue;
318 if (filp->private_data != tty)
319 continue;
320 if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
321 continue;
322 if (filp->f_op != &tty_fops)
323 continue;
324 tty_fasync(filp->f_inode, filp, 0);
325 filp->f_op = fops;
326 }
327
328 if (tty->ldisc.flush_buffer)
329 tty->ldisc.flush_buffer(tty);
330 if (tty->driver.flush_buffer)
331 tty->driver.flush_buffer(tty);
332 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
333 tty->ldisc.write_wakeup)
334 (tty->ldisc.write_wakeup)(tty);
335 wake_up_interruptible(&tty->write_wait);
336 wake_up_interruptible(&tty->read_wait);
337
338
339
340
341
342 if (tty->ldisc.num != ldiscs[N_TTY].num) {
343 if (tty->ldisc.close)
344 (tty->ldisc.close)(tty);
345 tty->ldisc = ldiscs[N_TTY];
346 tty->termios->c_line = N_TTY;
347 if (tty->ldisc.open) {
348 i = (tty->ldisc.open)(tty);
349 if (i < 0)
350 printk("do_tty_hangup: N_TTY open: error %d\n",
351 -i);
352 }
353 }
354
355 if (tty->session > 0) {
356 kill_sl(tty->session,SIGHUP,1);
357 kill_sl(tty->session,SIGCONT,1);
358 }
359 tty->flags = 0;
360 tty->session = 0;
361 tty->pgrp = -1;
362 for_each_task(p) {
363 if (p->tty == tty)
364 p->tty = NULL;
365 }
366 if (tty->driver.hangup)
367 (tty->driver.hangup)(tty);
368 }
369
370 void tty_hangup(struct tty_struct * tty)
371 {
372 #ifdef TTY_DEBUG_HANGUP
373 printk("%s hangup...\n", tty_name(tty));
374 #endif
375 do_tty_hangup(tty, &hung_up_tty_fops);
376 }
377
378 void tty_vhangup(struct tty_struct * tty)
379 {
380 #ifdef TTY_DEBUG_HANGUP
381 printk("%s vhangup...\n", tty_name(tty));
382 #endif
383 do_tty_hangup(tty, &hung_up_tty_fops);
384 }
385
386 int tty_hung_up_p(struct file * filp)
387 {
388 return (filp->f_op == &hung_up_tty_fops);
389 }
390
391
392
393
394
395
396
397
398
399
400
401 void disassociate_ctty(int priv)
402 {
403 struct tty_struct *tty = current->tty;
404 struct task_struct *p;
405
406 if (!tty)
407 return;
408
409 if (tty->pgrp > 0) {
410 kill_pg(tty->pgrp, SIGHUP, priv);
411 kill_pg(tty->pgrp, SIGCONT, priv);
412 }
413 tty->session = 0;
414 tty->pgrp = -1;
415
416 for_each_task(p)
417 if (p->session == current->session)
418 p->tty = NULL;
419 }
420
421
422
423
424
425
426
427
428 static struct wait_queue *vt_activate_queue = NULL;
429
430
431
432
433
434 int vt_waitactive(void)
435 {
436 interruptible_sleep_on(&vt_activate_queue);
437 return (current->signal & ~current->blocked) ? -1 : 0;
438 }
439
440 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
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 = (struct tty_struct *)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 = (struct tty_struct *)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 repeat:
735 retval = -EAGAIN;
736 if (driver->type == TTY_DRIVER_TYPE_PTY &&
737 driver->subtype == PTY_TYPE_MASTER &&
738 *tty_loc && (*tty_loc)->count)
739 goto end_init;
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 (*tty_loc)->count++;
817 if (tty->ldisc.open) {
818 retval = (tty->ldisc.open)(tty);
819 if (retval < 0) {
820 (*tty_loc)->count--;
821 tty = NULL;
822 goto end_init;
823 }
824 }
825 tty = NULL;
826 } else
827 (*tty_loc)->count++;
828 if (driver->type == TTY_DRIVER_TYPE_PTY) {
829 if (!*o_tp_loc) {
830 *o_tp_loc = o_tp;
831 o_tp = NULL;
832 }
833 if (!*o_ltp_loc) {
834 *o_ltp_loc = o_ltp;
835 o_ltp = NULL;
836 }
837 if (!*o_tty_loc) {
838 o_tty->termios = *o_tp_loc;
839 o_tty->termios_locked = *o_ltp_loc;
840 *o_tty_loc = o_tty;
841 (*driver->other->refcount)++;
842 if (o_tty->ldisc.open) {
843 retval = (o_tty->ldisc.open)(o_tty);
844 if (retval < 0) {
845 (*tty_loc)->count--;
846 o_tty = NULL;
847 goto end_init;
848 }
849 }
850 o_tty = NULL;
851 }
852 (*tty_loc)->link = *o_tty_loc;
853 (*o_tty_loc)->link = *tty_loc;
854 if (driver->subtype == PTY_TYPE_MASTER)
855 (*o_tty_loc)->count++;
856 }
857 (*tty_loc)->driver = *driver;
858 *ret_tty = *tty_loc;
859 retval = 0;
860 end_init:
861 if (tty)
862 free_page((unsigned long) tty);
863 if (o_tty)
864 free_page((unsigned long) o_tty);
865 if (tp)
866 kfree_s(tp, sizeof(struct termios));
867 if (o_tp)
868 kfree_s(o_tp, sizeof(struct termios));
869 if (ltp)
870 kfree_s(ltp, sizeof(struct termios));
871 if (o_ltp)
872 kfree_s(o_ltp, sizeof(struct termios));
873 return retval;
874 }
875
876
877
878
879
880
881 static void release_dev(struct file * filp)
882 {
883 struct tty_struct *tty, *o_tty;
884 struct termios *tp, *o_tp, *ltp, *o_ltp;
885 struct task_struct **p;
886 int idx;
887
888
889 tty = (struct tty_struct *)filp->private_data;
890 if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "release_dev"))
891 return;
892
893 tty_fasync(filp->f_inode, filp, 0);
894
895 tp = tty->termios;
896 ltp = tty->termios_locked;
897
898 idx = MINOR(tty->device) - tty->driver.minor_start;
899 #ifdef TTY_PARANOIA_CHECK
900 if (idx < 0 || idx >= tty->driver.num) {
901 printk("release_dev: bad idx when trying to free (%d, %d)\n",
902 MAJOR(tty->device), MINOR(tty->device));
903 return;
904 }
905 if (tty != tty->driver.table[idx]) {
906 printk("release_dev: driver.table[%d] not tty for (%d, %d)\n",
907 idx, MAJOR(tty->device), MINOR(tty->device));
908 return;
909 }
910 if (tp != tty->driver.termios[idx]) {
911 printk("release_dev: driver.termios[%d] not termios for (%d, %d)\n",
912 idx, MAJOR(tty->device), MINOR(tty->device));
913 return;
914 }
915 if (ltp != tty->driver.termios_locked[idx]) {
916 printk("release_dev: driver.termios_locked[%d] not termios_locked for (%d, %d)\n",
917 idx, MAJOR(tty->device), MINOR(tty->device));
918 return;
919 }
920 #endif
921
922 #ifdef TTY_DEBUG_HANGUP
923 printk("release_dev of %s (tty count=%d)...", tty_name(tty),
924 tty->count);
925 #endif
926
927 o_tty = tty->link;
928 o_tp = (o_tty) ? o_tty->termios : NULL;
929 o_ltp = (o_tty) ? o_tty->termios_locked : NULL;
930
931 #ifdef TTY_PARANOIA_CHECK
932 if (tty->driver.other) {
933 if (o_tty != tty->driver.other->table[idx]) {
934 printk("release_dev: other->table[%d] not o_tty for (%d, %d)\n",
935 idx, MAJOR(tty->device), MINOR(tty->device));
936 return;
937 }
938 if (o_tp != tty->driver.other->termios[idx]) {
939 printk("release_dev: other->termios[%d] not o_termios for (%d, %d)\n",
940 idx, MAJOR(tty->device), MINOR(tty->device));
941 return;
942 }
943 if (o_ltp != tty->driver.other->termios_locked[idx]) {
944 printk("release_dev: other->termios_locked[%d] not o_termios_locked for (%d, %d)\n",
945 idx, MAJOR(tty->device), MINOR(tty->device));
946 return;
947 }
948
949 if (o_tty->link != tty) {
950 printk("release_dev: bad pty pointers\n");
951 return;
952 }
953 }
954 #endif
955
956 if (tty->driver.close)
957 tty->driver.close(tty, filp);
958 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
959 tty->driver.subtype == PTY_TYPE_MASTER) {
960 if (--tty->link->count < 0) {
961 printk("release_dev: bad pty slave count (%d) for %s\n",
962 tty->count, tty_name(tty));
963 tty->link->count = 0;
964 }
965 }
966 if (--tty->count < 0) {
967 printk("release_dev: bad tty->count (%d) for %s\n",
968 tty->count, tty_name(tty));
969 tty->count = 0;
970 }
971 if (tty->count)
972 return;
973
974
975
976
977
978 for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
979 if ((*p) && (*p)->tty == tty)
980 (*p)->tty = NULL;
981 }
982
983 if (o_tty) {
984 if (o_tty->count)
985 return;
986 tty->driver.other->table[idx] = NULL;
987 tty->driver.other->termios[idx] = NULL;
988 tty->driver.other->termios_locked[idx] = NULL;
989 }
990
991 #ifdef TTY_DEBUG_HANGUP
992 printk("freeing tty structure...");
993 #endif
994
995
996
997
998
999 if (tty->ldisc.close)
1000 (tty->ldisc.close)(tty);
1001 tty->ldisc = ldiscs[N_TTY];
1002 tty->termios->c_line = N_TTY;
1003
1004 tty->driver.table[idx] = NULL;
1005 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1006 tty->driver.termios[idx] = NULL;
1007 tty->driver.termios_locked[idx] = NULL;
1008 kfree_s(tp, sizeof(struct termios));
1009 kfree_s(ltp, sizeof(struct termios));
1010 }
1011 if (tty == redirect || o_tty == redirect)
1012 redirect = NULL;
1013
1014
1015
1016
1017 cli();
1018 if (tty->flip.tqueue.sync) {
1019 struct tq_struct *tq, *prev;
1020
1021 for (tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
1022 if (tq == &tty->flip.tqueue) {
1023 if (prev)
1024 prev->next = tq->next;
1025 else
1026 tq_timer = tq->next;
1027 break;
1028 }
1029 }
1030 }
1031 sti();
1032 tty->magic = 0;
1033 (*tty->driver.refcount)--;
1034 free_page((unsigned long) tty);
1035 if (o_tty) {
1036 o_tty->magic = 0;
1037 (*o_tty->driver.refcount)--;
1038 free_page((unsigned long) o_tty);
1039 }
1040 if (o_tp)
1041 kfree_s(o_tp, sizeof(struct termios));
1042 if (o_ltp)
1043 kfree_s(o_ltp, sizeof(struct termios));
1044 }
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058 static int tty_open(struct inode * inode, struct file * filp)
1059 {
1060 struct tty_struct *tty;
1061 int minor;
1062 int noctty, retval;
1063 dev_t device;
1064
1065 retry_open:
1066 noctty = filp->f_flags & O_NOCTTY;
1067 device = inode->i_rdev;
1068 if (device == TTY_DEV) {
1069 if (!current->tty)
1070 return -ENXIO;
1071 device = current->tty->device;
1072
1073 }
1074 if (device == CONSOLE_DEV) {
1075 device = MKDEV(TTY_MAJOR, fg_console+1);
1076 noctty = 1;
1077 }
1078 minor = MINOR(device);
1079
1080 retval = init_dev(device, &tty);
1081 filp->private_data = tty;
1082 if (retval)
1083 return retval;
1084 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1085 tty->driver.subtype == PTY_TYPE_MASTER)
1086 noctty = 1;
1087 #ifdef TTY_DEBUG_HANGUP
1088 printk("opening %s...", tty_name(tty));
1089 #endif
1090 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1091 retval = -EBUSY;
1092 else if (tty->driver.open)
1093 retval = tty->driver.open(tty, filp);
1094 else
1095 retval = -ENODEV;
1096
1097 if (retval) {
1098 #ifdef TTY_DEBUG_HANGUP
1099 printk("error %d in opening %s...", retval, tty_name(tty));
1100 #endif
1101
1102 release_dev(filp);
1103 if (retval != -ERESTARTSYS)
1104 return retval;
1105 if (current->signal & ~current->blocked)
1106 return retval;
1107 schedule();
1108
1109
1110
1111 filp->f_op = &tty_fops;
1112 goto retry_open;
1113 }
1114 if (!noctty &&
1115 current->leader &&
1116 !current->tty &&
1117 tty->session == 0) {
1118 current->tty = tty;
1119 tty->session = current->session;
1120 tty->pgrp = current->pgrp;
1121 }
1122 return 0;
1123 }
1124
1125
1126
1127
1128
1129
1130 static void tty_release(struct inode * inode, struct file * filp)
1131 {
1132 release_dev(filp);
1133 }
1134
1135 static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
1136 {
1137 struct tty_struct * tty;
1138
1139 tty = (struct tty_struct *)filp->private_data;
1140 if (tty_paranoia_check(tty, inode->i_rdev, "tty_select"))
1141 return 0;
1142
1143 if (tty->ldisc.select)
1144 return (tty->ldisc.select)(tty, inode, filp, sel_type, wait);
1145 return 0;
1146 }
1147
1148 static int tty_fasync(struct inode * inode, struct file * filp, int on)
1149 {
1150 struct tty_struct * tty;
1151 struct fasync_struct *fa, *prev;
1152
1153 tty = (struct tty_struct *)filp->private_data;
1154 if (tty_paranoia_check(tty, inode->i_rdev, "tty_fasync"))
1155 return 0;
1156
1157 for (fa = tty->fasync, prev = 0; fa; prev= fa, fa = fa->fa_next) {
1158 if (fa->fa_file == filp)
1159 break;
1160 }
1161
1162 if (on) {
1163 if (fa)
1164 return 0;
1165 fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1166 if (!fa)
1167 return -ENOMEM;
1168 fa->magic = FASYNC_MAGIC;
1169 fa->fa_file = filp;
1170 fa->fa_next = tty->fasync;
1171 tty->fasync = fa;
1172 if (!tty->read_wait)
1173 tty->minimum_to_wake = 1;
1174 } else {
1175 if (!fa)
1176 return 0;
1177 if (prev)
1178 prev->fa_next = fa->fa_next;
1179 else
1180 tty->fasync = fa->fa_next;
1181 kfree_s(fa, sizeof(struct fasync_struct));
1182 if (!tty->fasync && !tty->read_wait)
1183 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1184 }
1185 return 0;
1186 }
1187
1188
1189
1190
1191 static int do_get_ps_info(int arg)
1192 {
1193 struct tstruct {
1194 int flag;
1195 int present[NR_TASKS];
1196 struct task_struct tasks[NR_TASKS];
1197 };
1198 struct tstruct *ts = (struct tstruct *)arg;
1199 struct task_struct **p;
1200 char *c, *d;
1201 int i, n = 0;
1202
1203 i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct tstruct));
1204 if (i)
1205 return i;
1206 for (p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
1207 if (*p)
1208 {
1209 c = (char *)(*p);
1210 d = (char *)(ts->tasks+n);
1211 for (i=0 ; i<sizeof(struct task_struct) ; i++)
1212 put_fs_byte(*c++, d++);
1213 put_fs_long(1, (unsigned long *)(ts->present+n));
1214 }
1215 else
1216 put_fs_long(0, (unsigned long *)(ts->present+n));
1217 return(0);
1218 }
1219
1220 static int tty_ioctl(struct inode * inode, struct file * file,
1221 unsigned int cmd, unsigned long arg)
1222 {
1223 int retval;
1224 struct tty_struct * tty;
1225 struct tty_struct * real_tty;
1226 struct winsize tmp_ws;
1227 pid_t pgrp;
1228 unsigned char ch;
1229 char mbz = 0;
1230
1231 tty = (struct tty_struct *)file->private_data;
1232 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1233 return -EINVAL;
1234
1235 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1236 tty->driver.subtype == PTY_TYPE_MASTER)
1237 real_tty = tty->link;
1238 else
1239 real_tty = tty;
1240
1241 switch (cmd) {
1242 case TIOCSTI:
1243 if ((current->tty != tty) && !suser())
1244 return -EPERM;
1245 ch = get_fs_byte((char *) arg);
1246 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1247 return 0;
1248 case TIOCGWINSZ:
1249 retval = verify_area(VERIFY_WRITE, (void *) arg,
1250 sizeof (struct winsize));
1251 if (retval)
1252 return retval;
1253 memcpy_tofs((struct winsize *) arg, &tty->winsize,
1254 sizeof (struct winsize));
1255 return 0;
1256 case TIOCSWINSZ:
1257 memcpy_fromfs(&tmp_ws, (struct winsize *) arg,
1258 sizeof (struct winsize));
1259 if (memcmp(&tmp_ws, &tty->winsize,
1260 sizeof(struct winsize))) {
1261 if (tty->pgrp > 0)
1262 kill_pg(tty->pgrp, SIGWINCH, 1);
1263 if ((real_tty->pgrp != tty->pgrp) &&
1264 (real_tty->pgrp > 0))
1265 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1266 }
1267 tty->winsize = tmp_ws;
1268 real_tty->winsize = tmp_ws;
1269 return 0;
1270 case TIOCCONS:
1271 if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
1272 if (!suser())
1273 return -EPERM;
1274 redirect = NULL;
1275 return 0;
1276 }
1277 if (redirect)
1278 return -EBUSY;
1279 redirect = real_tty;
1280 return 0;
1281 case FIONBIO:
1282 arg = get_fs_long((unsigned long *) arg);
1283 if (arg)
1284 file->f_flags |= O_NONBLOCK;
1285 else
1286 file->f_flags &= ~O_NONBLOCK;
1287 return 0;
1288 case TIOCEXCL:
1289 set_bit(TTY_EXCLUSIVE, &tty->flags);
1290 return 0;
1291 case TIOCNXCL:
1292 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1293 return 0;
1294 case TIOCNOTTY:
1295 if (current->tty != tty)
1296 return -ENOTTY;
1297 if (current->leader)
1298 disassociate_ctty(0);
1299 current->tty = NULL;
1300 return 0;
1301 case TIOCSCTTY:
1302 if (current->leader &&
1303 (current->session == tty->session))
1304 return 0;
1305
1306
1307
1308
1309 if (!current->leader || current->tty)
1310 return -EPERM;
1311 if (tty->session > 0) {
1312
1313
1314
1315
1316 if ((arg == 1) && suser()) {
1317
1318
1319
1320 struct task_struct *p;
1321
1322 for_each_task(p)
1323 if (p->tty == tty)
1324 p->tty = NULL;
1325 } else
1326 return -EPERM;
1327 }
1328 current->tty = tty;
1329 tty->session = current->session;
1330 tty->pgrp = current->pgrp;
1331 return 0;
1332 case TIOCGPGRP:
1333
1334
1335
1336
1337 if (tty == real_tty && current->tty != real_tty)
1338 return -ENOTTY;
1339 retval = verify_area(VERIFY_WRITE, (void *) arg,
1340 sizeof (pid_t));
1341 if (retval)
1342 return retval;
1343 put_fs_long(real_tty->pgrp, (pid_t *) arg);
1344 return 0;
1345 case TIOCSPGRP:
1346 retval = tty_check_change(real_tty);
1347 if (retval)
1348 return retval;
1349 if (!current->tty ||
1350 (current->tty != real_tty) ||
1351 (real_tty->session != current->session))
1352 return -ENOTTY;
1353 pgrp = get_fs_long((pid_t *) arg);
1354 if (pgrp < 0)
1355 return -EINVAL;
1356 if (session_of_pgrp(pgrp) != current->session)
1357 return -EPERM;
1358 real_tty->pgrp = pgrp;
1359 return 0;
1360 case TIOCGETD:
1361 retval = verify_area(VERIFY_WRITE, (void *) arg,
1362 sizeof (unsigned long));
1363 if (retval)
1364 return retval;
1365 put_fs_long(tty->ldisc.num, (unsigned long *) arg);
1366 return 0;
1367 case TIOCSETD:
1368 retval = tty_check_change(tty);
1369 if (retval)
1370 return retval;
1371 arg = get_fs_long((unsigned long *) arg);
1372 return tty_set_ldisc(tty, arg);
1373 case TIOCLINUX:
1374 switch (get_fs_byte((char *)arg))
1375 {
1376 case 0:
1377 return do_screendump(arg);
1378 case 1:
1379 printk("Deprecated TIOCLINUX (1) ioctl\n");
1380 return do_get_ps_info(arg);
1381 #ifdef CONFIG_SELECTION
1382 case 2:
1383 return set_selection(arg, tty);
1384 case 3:
1385 return paste_selection(tty);
1386 case 4:
1387 unblank_screen();
1388 return 0;
1389 case 5:
1390 return sel_loadlut(arg);
1391 case 6:
1392 put_fs_byte(shift_state,arg);
1393 return 0;
1394 case 7:
1395 put_fs_byte(mouse_reporting(),arg);
1396 return 0;
1397 #endif
1398 default:
1399 return -EINVAL;
1400 }
1401 default:
1402 if (tty->driver.ioctl) {
1403 retval = (tty->driver.ioctl)(tty, file,
1404 cmd, arg);
1405 if (retval != -ENOIOCTLCMD)
1406 return retval;
1407 }
1408 if (tty->ldisc.ioctl) {
1409 retval = (tty->ldisc.ioctl)(tty, file,
1410 cmd, arg);
1411 if (retval != -ENOIOCTLCMD)
1412 return retval;
1413 }
1414 return -EINVAL;
1415 }
1416 }
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431 void do_SAK( struct tty_struct *tty)
1432 {
1433 #ifdef TTY_SOFT_SAK
1434 tty_hangup(tty);
1435 #else
1436 struct task_struct **p;
1437 int session = tty->session;
1438 int i;
1439 struct file *filp;
1440
1441 if (tty->ldisc.flush_buffer)
1442 tty->ldisc.flush_buffer(tty);
1443 if (tty->driver.flush_buffer)
1444 tty->driver.flush_buffer(tty);
1445 for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1446 if (!(*p))
1447 continue;
1448 if (((*p)->tty == tty) ||
1449 ((session > 0) && ((*p)->session == session)))
1450 send_sig(SIGKILL, *p, 1);
1451 else {
1452 for (i=0; i < NR_OPEN; i++) {
1453 filp = (*p)->files->fd[i];
1454 if (filp && (filp->f_op == &tty_fops) &&
1455 (filp->private_data == tty)) {
1456 send_sig(SIGKILL, *p, 1);
1457 break;
1458 }
1459 }
1460 }
1461 }
1462 #endif
1463 }
1464
1465
1466
1467
1468
1469 static void flush_to_ldisc(void *private_)
1470 {
1471 struct tty_struct *tty = (struct tty_struct *) private_;
1472 unsigned char *cp;
1473 char *fp;
1474 int count;
1475
1476 if (tty->flip.buf_num) {
1477 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1478 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1479 tty->flip.buf_num = 0;
1480
1481 cli();
1482 tty->flip.char_buf_ptr = tty->flip.char_buf;
1483 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1484 } else {
1485 cp = tty->flip.char_buf;
1486 fp = tty->flip.flag_buf;
1487 tty->flip.buf_num = 1;
1488
1489 cli();
1490 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1491 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1492 }
1493 count = tty->flip.count;
1494 tty->flip.count = 0;
1495 sti();
1496
1497 #if 0
1498 if (count > tty->max_flip_cnt)
1499 tty->max_flip_cnt = count;
1500 #endif
1501 tty->ldisc.receive_buf(tty, cp, fp, count);
1502 }
1503
1504
1505
1506
1507 static void initialize_tty_struct(struct tty_struct *tty)
1508 {
1509 memset(tty, 0, sizeof(struct tty_struct));
1510 tty->magic = TTY_MAGIC;
1511 tty->ldisc = ldiscs[N_TTY];
1512 tty->pgrp = -1;
1513 tty->flip.char_buf_ptr = tty->flip.char_buf;
1514 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1515 tty->flip.tqueue.routine = flush_to_ldisc;
1516 tty->flip.tqueue.data = tty;
1517 }
1518
1519
1520
1521
1522 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
1523 {
1524 tty->driver.write(tty, 0, &ch, 1);
1525 }
1526
1527
1528
1529
1530 int tty_register_driver(struct tty_driver *driver)
1531 {
1532 int error;
1533
1534 if (driver->flags & TTY_DRIVER_INSTALLED)
1535 return 0;
1536
1537 error = register_chrdev(driver->major, driver->name, &tty_fops);
1538 if (error < 0)
1539 return error;
1540 else if(driver->major == 0)
1541 driver->major = error;
1542
1543 if (!driver->put_char)
1544 driver->put_char = tty_default_put_char;
1545
1546 driver->prev = 0;
1547 driver->next = tty_drivers;
1548 tty_drivers->prev = driver;
1549 tty_drivers = driver;
1550 return error;
1551 }
1552
1553
1554
1555
1556
1557
1558
1559 long console_init(long kmem_start, long kmem_end)
1560 {
1561
1562 memset(ldiscs, 0, sizeof(ldiscs));
1563 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1564
1565
1566
1567
1568
1569 memset(&tty_std_termios, 0, sizeof(struct termios));
1570 memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
1571 tty_std_termios.c_iflag = ICRNL | IXON;
1572 tty_std_termios.c_oflag = OPOST | ONLCR;
1573 tty_std_termios.c_cflag = B38400 | CS8 | CREAD;
1574 tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1575 ECHOCTL | ECHOKE | IEXTEN;
1576
1577
1578
1579
1580
1581 return con_init(kmem_start);
1582 }
1583
1584
1585
1586
1587
1588 long tty_init(long kmem_start)
1589 {
1590 if (sizeof(struct tty_struct) > PAGE_SIZE)
1591 panic("size of tty structure > PAGE_SIZE!");
1592 if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
1593 panic("unable to get major %d for tty device", TTY_MAJOR);
1594 if (register_chrdev(TTYAUX_MAJOR,"cua",&tty_fops))
1595 panic("unable to get major %d for tty device", TTYAUX_MAJOR);
1596
1597 kmem_start = kbd_init(kmem_start);
1598 kmem_start = rs_init(kmem_start);
1599 kmem_start = pty_init(kmem_start);
1600 return kmem_start;
1601 }