Import 2.1.15
[davej-history.git] / drivers / char / tty_io.c
blob5e3f3d0b04a6241d89d5f7838803245673b93c90
1 /*
2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9 * or rs-channels. It also implements echoing, cooked mode etc.
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14 * tty_struct and tty_queue structures. Previously there was an array
15 * of 256 tty_struct's which was statically allocated, and the
16 * tty_queue structures were allocated at boot time. Both are now
17 * dynamically allocated only when the tty is open.
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
22 * makes for cleaner and more compact code. -TYT, 9/17/92
24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25 * which can be dynamically activated and de-activated by the line
26 * discipline handling modules (like SLIP).
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
30 * -- TYT, 1/31/92
32 * Added functionality to the OPOST tty handling. No delays, but all
33 * other bits should be there.
34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
49 #include <linux/config.h>
50 #include <linux/types.h>
51 #include <linux/major.h>
52 #include <linux/errno.h>
53 #include <linux/signal.h>
54 #include <linux/fcntl.h>
55 #include <linux/sched.h>
56 #include <linux/interrupt.h>
57 #include <linux/tty.h>
58 #include <linux/tty_flip.h>
59 #include <linux/timer.h>
60 #include <linux/ctype.h>
61 #include <linux/kd.h>
62 #include <linux/mm.h>
63 #include <linux/string.h>
64 #include <linux/malloc.h>
66 #include <asm/uaccess.h>
67 #include <asm/system.h>
68 #include <asm/bitops.h>
70 #include"kbd_kern.h"
71 #include"vt_kern.h"
72 #include"selection.h"
74 #ifdef CONFIG_KERNELD
75 #include <linux/kerneld.h>
76 #endif
78 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
79 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
81 #undef TTY_DEBUG_HANGUP
83 #define TTY_PARANOIA_CHECK
84 #define CHECK_TTY_COUNT
86 externvoiddo_blank_screen(int nopowersave);
87 externvoidset_vesa_blanking(const unsigned long arg);
89 struct termios tty_std_termios;/* for the benefit of tty drivers */
90 struct tty_driver *tty_drivers = NULL;/* linked list of tty drivers */
91 struct tty_ldisc ldiscs[NR_LDISCS];/* line disc dispatch table */
94 * fg_console is the current virtual console,
95 * last_console is the last used one,
96 * want_console is the console we want to switch to,
97 * kmsg_redirect is the console for kernel messages,
98 * redirect is the pseudo-tty that console output
99 * is redirected to if asked by TIOCCONS.
101 int fg_console =0;
102 int last_console =0;
103 int want_console = -1;
104 int kmsg_redirect =0;
105 struct tty_struct * redirect = NULL;
106 struct wait_queue * keypress_wait = NULL;
107 char vt_dont_switch =0;
109 static voidinitialize_tty_struct(struct tty_struct *tty);
111 static longtty_read(struct inode *,struct file *,char*,unsigned long);
112 static longtty_write(struct inode *,struct file *,const char*,unsigned long);
113 static inttty_select(struct inode *,struct file *,int, select_table *);
114 static inttty_open(struct inode *,struct file *);
115 static voidtty_release(struct inode *,struct file *);
116 static inttty_ioctl(struct inode * inode,struct file * file,
117 unsigned int cmd,unsigned long arg);
118 static inttty_fasync(struct inode * inode,struct file * filp,int on);
120 externvoidreset_palette(int currcons) ;
121 externvoidset_palette(void) ;
123 #ifndef MIN
124 #define MIN(a,b) ((a) < (b) ? (a) : (b))
125 #endif
128 * These two routines return the name of tty. tty_name() should NOT
129 * be used in interrupt drivers, since it's not re-entrant. Use
130 * _tty_name() instead.
132 char*_tty_name(struct tty_struct *tty,char*buf)
134 if(tty)
135 sprintf(buf,"%s%d", tty->driver.name,
136 MINOR(tty->device) - tty->driver.minor_start +
137 tty->driver.name_base);
138 else
139 strcpy(buf,"NULL tty");
140 return buf;
143 char*tty_name(struct tty_struct *tty)
145 static char buf[64];
147 return(_tty_name(tty, buf));
150 inlineinttty_paranoia_check(struct tty_struct *tty, kdev_t device,
151 const char*routine)
153 #ifdef TTY_PARANOIA_CHECK
154 static const char*badmagic =
155 "Warning: bad magic number for tty struct (%s) in %s\n";
156 static const char*badtty =
157 "Warning: null TTY for (%s) in %s\n";
159 if(!tty) {
160 printk(badtty,kdevname(device), routine);
161 return1;
163 if(tty->magic != TTY_MAGIC) {
164 printk(badmagic,kdevname(device), routine);
165 return1;
167 #endif
168 return0;
171 static intcheck_tty_count(struct tty_struct *tty,const char*routine)
173 #ifdef CHECK_TTY_COUNT
174 struct file *f;
175 int i, count =0;
177 for(f = first_file, i=0; i<nr_files; i++, f = f->f_next) {
178 if(!f->f_count)
179 continue;
180 if(f->private_data == tty) {
181 count++;
184 if(tty->driver.type == TTY_DRIVER_TYPE_PTY &&
185 tty->driver.subtype == PTY_TYPE_SLAVE &&
186 tty->link && tty->link->count)
187 count++;
188 if(tty->count != count) {
189 printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
190 kdevname(tty->device), tty->count, count, routine);
191 return count;
193 #endif
194 return0;
197 inttty_register_ldisc(int disc,struct tty_ldisc *new_ldisc)
199 if(disc < N_TTY || disc >= NR_LDISCS)
200 return-EINVAL;
202 if(new_ldisc) {
203 ldiscs[disc] = *new_ldisc;
204 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
205 ldiscs[disc].num = disc;
206 }else
207 memset(&ldiscs[disc],0,sizeof(struct tty_ldisc));
209 return0;
212 /* Set the discipline of a tty line. */
213 static inttty_set_ldisc(struct tty_struct *tty,int ldisc)
215 int retval =0;
216 struct tty_ldisc o_ldisc;
218 if((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
219 return-EINVAL;
220 #ifdef CONFIG_KERNELD
221 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
222 if(!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
223 char modname [20];
224 sprintf(modname,"tty-ldisc-%d", ldisc);
225 request_module(modname);
227 #endif
228 if(!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
229 return-EINVAL;
231 if(tty->ldisc.num == ldisc)
232 return0;/* We are already in the desired discipline */
233 o_ldisc = tty->ldisc;
235 tty_wait_until_sent(tty,0);
237 /* Shutdown the current discipline. */
238 if(tty->ldisc.close)
239 (tty->ldisc.close)(tty);
241 /* Now set up the new line discipline. */
242 tty->ldisc = ldiscs[ldisc];
243 tty->termios->c_line = ldisc;
244 if(tty->ldisc.open)
245 retval = (tty->ldisc.open)(tty);
246 if(retval <0) {
247 tty->ldisc = o_ldisc;
248 tty->termios->c_line = tty->ldisc.num;
249 if(tty->ldisc.open && (tty->ldisc.open(tty) <0)) {
250 tty->ldisc = ldiscs[N_TTY];
251 tty->termios->c_line = N_TTY;
252 if(tty->ldisc.open) {
253 int r = tty->ldisc.open(tty);
255 if(r <0)
256 panic("Couldn't open N_TTY ldisc for "
257 "%s --- error %d.",
258 tty_name(tty), r);
262 if(tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
263 tty->driver.set_ldisc(tty);
264 return retval;
268 * This routine returns a tty driver structure, given a device number
270 struct tty_driver *get_tty_driver(kdev_t device)
272 int major, minor;
273 struct tty_driver *p;
275 minor =MINOR(device);
276 major =MAJOR(device);
278 for(p = tty_drivers; p; p = p->next) {
279 if(p->major != major)
280 continue;
281 if(minor < p->minor_start)
282 continue;
283 if(minor >= p->minor_start + p->num)
284 continue;
285 return p;
287 return NULL;
291 * If we try to write to, or set the state of, a terminal and we're
292 * not in the foreground, send a SIGTTOU. If the signal is blocked or
293 * ignored, go ahead and perform the operation. (POSIX 7.2)
295 inttty_check_change(struct tty_struct * tty)
297 if(current->tty != tty)
298 return0;
299 if(tty->pgrp <=0) {
300 printk("tty_check_change: tty->pgrp <= 0!\n");
301 return0;
303 if(current->pgrp == tty->pgrp)
304 return0;
305 if(is_ignored(SIGTTOU))
306 return0;
307 if(is_orphaned_pgrp(current->pgrp))
308 return-EIO;
309 (void)kill_pg(current->pgrp,SIGTTOU,1);
310 return-ERESTARTSYS;
313 static longhung_up_tty_read(struct inode * inode,struct file * file,
314 char* buf,unsigned long count)
316 return0;
319 static longhung_up_tty_write(struct inode * inode,
320 struct file * file,const char* buf,unsigned long count)
322 return-EIO;
325 static inthung_up_tty_select(struct inode * inode,struct file * filp,
326 int sel_type, select_table * wait)
328 return1;
331 static inthung_up_tty_ioctl(struct inode * inode,struct file * file,
332 unsigned int cmd,unsigned long arg)
334 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
337 static long longtty_lseek(struct inode * inode,struct file * file,
338 long long offset,int orig)
340 return-ESPIPE;
343 static struct file_operations tty_fops = {
344 tty_lseek,
345 tty_read,
346 tty_write,
347 NULL,/* tty_readdir */
348 tty_select,
349 tty_ioctl,
350 NULL,/* tty_mmap */
351 tty_open,
352 tty_release,
353 NULL,/* tty_fsync */
354 tty_fasync
357 static struct file_operations hung_up_tty_fops = {
358 tty_lseek,
359 hung_up_tty_read,
360 hung_up_tty_write,
361 NULL,/* hung_up_tty_readdir */
362 hung_up_tty_select,
363 hung_up_tty_ioctl,
364 NULL,/* hung_up_tty_mmap */
365 NULL,/* hung_up_tty_open */
366 tty_release,/* hung_up_tty_release */
367 NULL,/* hung_up_tty_fsync */
368 NULL /* hung_up_tty_fasync */
371 voiddo_tty_hangup(struct tty_struct * tty,struct file_operations *fops)
373 int i;
374 struct file * filp;
375 struct task_struct *p;
377 if(!tty)
378 return;
379 check_tty_count(tty,"do_tty_hangup");
380 for(filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
381 if(!filp->f_count)
382 continue;
383 if(filp->private_data != tty)
384 continue;
385 if(!filp->f_inode)
386 continue;
387 if(filp->f_inode->i_rdev == CONSOLE_DEV)
388 continue;
389 if(filp->f_op != &tty_fops)
390 continue;
391 tty_fasync(filp->f_inode, filp,0);
392 filp->f_op = fops;
395 if(tty->ldisc.flush_buffer)
396 tty->ldisc.flush_buffer(tty);
397 if(tty->driver.flush_buffer)
398 tty->driver.flush_buffer(tty);
399 if((tty->flags & (1<< TTY_DO_WRITE_WAKEUP)) &&
400 tty->ldisc.write_wakeup)
401 (tty->ldisc.write_wakeup)(tty);
402 wake_up_interruptible(&tty->write_wait);
403 wake_up_interruptible(&tty->read_wait);
406 * Shutdown the current line discipline, and reset it to
407 * N_TTY.
409 if(tty->ldisc.num != ldiscs[N_TTY].num) {
410 if(tty->ldisc.close)
411 (tty->ldisc.close)(tty);
412 tty->ldisc = ldiscs[N_TTY];
413 tty->termios->c_line = N_TTY;
414 if(tty->ldisc.open) {
415 i = (tty->ldisc.open)(tty);
416 if(i <0)
417 printk("do_tty_hangup: N_TTY open: error %d\n",
418 -i);
422 for_each_task(p) {
423 if((tty->session >0) && (p->session == tty->session) &&
424 p->leader) {
425 send_sig(SIGHUP,p,1);
426 send_sig(SIGCONT,p,1);
427 if(tty->pgrp >0)
428 p->tty_old_pgrp = tty->pgrp;
430 if(p->tty == tty)
431 p->tty = NULL;
433 tty->flags =0;
434 tty->session =0;
435 tty->pgrp = -1;
436 tty->ctrl_status =0;
437 if(tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
438 *tty->termios = tty->driver.init_termios;
439 if(tty->driver.hangup)
440 (tty->driver.hangup)(tty);
443 voidtty_hangup(struct tty_struct * tty)
445 #ifdef TTY_DEBUG_HANGUP
446 printk("%s hangup...\n",tty_name(tty));
447 #endif
448 do_tty_hangup(tty, &hung_up_tty_fops);
451 voidtty_vhangup(struct tty_struct * tty)
453 #ifdef TTY_DEBUG_HANGUP
454 printk("%s vhangup...\n",tty_name(tty));
455 #endif
456 do_tty_hangup(tty, &hung_up_tty_fops);
459 inttty_hung_up_p(struct file * filp)
461 return(filp->f_op == &hung_up_tty_fops);
465 * This function is typically called only by the session leader, when
466 * it wants to disassociate itself from its controlling tty.
468 * It performs the following functions:
469 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
470 * (2) Clears the tty from being controlling the session
471 * (3) Clears the controlling tty for all processes in the
472 * session group.
474 * The argument on_exit is set to 1 if called when a process is
475 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
477 voiddisassociate_ctty(int on_exit)
479 struct tty_struct *tty = current->tty;
480 struct task_struct *p;
481 int tty_pgrp = -1;
483 if(tty) {
484 tty_pgrp = tty->pgrp;
485 if(on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
486 tty_vhangup(tty);
487 }else{
488 if(current->tty_old_pgrp) {
489 kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
490 kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
492 return;
494 if(tty_pgrp >0) {
495 kill_pg(tty_pgrp, SIGHUP, on_exit);
496 if(!on_exit)
497 kill_pg(tty_pgrp, SIGCONT, on_exit);
500 current->tty_old_pgrp =0;
501 tty->session =0;
502 tty->pgrp = -1;
504 for_each_task(p)
505 if(p->session == current->session)
506 p->tty = NULL;
510 * Sometimes we want to wait until a particular VT has been activated. We
511 * do it in a very simple manner. Everybody waits on a single queue and
512 * get woken up at once. Those that are satisfied go on with their business,
513 * while those not ready go back to sleep. Seems overkill to add a wait
514 * to each vt just for this - usually this does nothing!
516 static struct wait_queue *vt_activate_queue = NULL;
519 * Sleeps until a vt is activated, or the task is interrupted. Returns
520 * 0 if activation, -1 if interrupted.
522 intvt_waitactive(void)
524 interruptible_sleep_on(&vt_activate_queue);
525 return(current->signal & ~current->blocked) ? -1:0;
528 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
530 voidreset_vc(unsigned int new_console)
532 vt_cons[new_console]->vc_mode = KD_TEXT;
533 kbd_table[new_console].kbdmode = VC_XLATE;
534 vt_cons[new_console]->vt_mode.mode = VT_AUTO;
535 vt_cons[new_console]->vt_mode.waitv =0;
536 vt_cons[new_console]->vt_mode.relsig =0;
537 vt_cons[new_console]->vt_mode.acqsig =0;
538 vt_cons[new_console]->vt_mode.frsig =0;
539 vt_cons[new_console]->vt_pid = -1;
540 vt_cons[new_console]->vt_newvt = -1;
541 reset_palette(new_console) ;
545 * Performs the back end of a vt switch
547 voidcomplete_change_console(unsigned int new_console)
549 unsigned char old_vc_mode;
551 if((new_console == fg_console) || (vt_dont_switch))
552 return;
553 if(!vc_cons_allocated(new_console))
554 return;
555 last_console = fg_console;
558 * If we're switching, we could be going from KD_GRAPHICS to
559 * KD_TEXT mode or vice versa, which means we need to blank or
560 * unblank the screen later.
562 old_vc_mode = vt_cons[fg_console]->vc_mode;
563 update_screen(new_console);
566 * If this new console is under process control, send it a signal
567 * telling it that it has acquired. Also check if it has died and
568 * clean up (similar to logic employed in change_console())
570 if(vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
573 * Send the signal as privileged - kill_proc() will
574 * tell us if the process has gone or something else
575 * is awry
577 if(kill_proc(vt_cons[new_console]->vt_pid,
578 vt_cons[new_console]->vt_mode.acqsig,
579 1) !=0)
582 * The controlling process has died, so we revert back to
583 * normal operation. In this case, we'll also change back
584 * to KD_TEXT mode. I'm not sure if this is strictly correct
585 * but it saves the agony when the X server dies and the screen
586 * remains blanked due to KD_GRAPHICS! It would be nice to do
587 * this outside of VT_PROCESS but there is no single process
588 * to account for and tracking tty count may be undesirable.
590 reset_vc(new_console);
595 * We do this here because the controlling process above may have
596 * gone, and so there is now a new vc_mode
598 if(old_vc_mode != vt_cons[new_console]->vc_mode)
600 if(vt_cons[new_console]->vc_mode == KD_TEXT)
601 do_unblank_screen();
602 else
603 do_blank_screen(1);
606 /* Set the colour palette for this VT */
607 if(vt_cons[new_console]->vc_mode == KD_TEXT)
608 set_palette() ;
611 * Wake anyone waiting for their VT to activate
613 vt_wake_waitactive();
614 return;
618 * Performs the front-end of a vt switch
620 voidchange_console(unsigned int new_console)
622 if((new_console == fg_console) || (vt_dont_switch))
623 return;
624 if(!vc_cons_allocated(new_console))
625 return;
628 * If this vt is in process mode, then we need to handshake with
629 * that process before switching. Essentially, we store where that
630 * vt wants to switch to and wait for it to tell us when it's done
631 * (via VT_RELDISP ioctl).
633 * We also check to see if the controlling process still exists.
634 * If it doesn't, we reset this vt to auto mode and continue.
635 * This is a cheap way to track process control. The worst thing
636 * that can happen is: we send a signal to a process, it dies, and
637 * the switch gets "lost" waiting for a response; hopefully, the
638 * user will try again, we'll detect the process is gone (unless
639 * the user waits just the right amount of time :-) and revert the
640 * vt to auto control.
642 if(vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
645 * Send the signal as privileged - kill_proc() will
646 * tell us if the process has gone or something else
647 * is awry
649 if(kill_proc(vt_cons[fg_console]->vt_pid,
650 vt_cons[fg_console]->vt_mode.relsig,
651 1) ==0)
654 * It worked. Mark the vt to switch to and
655 * return. The process needs to send us a
656 * VT_RELDISP ioctl to complete the switch.
658 vt_cons[fg_console]->vt_newvt = new_console;
659 return;
663 * The controlling process has died, so we revert back to
664 * normal operation. In this case, we'll also change back
665 * to KD_TEXT mode. I'm not sure if this is strictly correct
666 * but it saves the agony when the X server dies and the screen
667 * remains blanked due to KD_GRAPHICS! It would be nice to do
668 * this outside of VT_PROCESS but there is no single process
669 * to account for and tracking tty count may be undesirable.
671 reset_vc(fg_console);
674 * Fall through to normal (VT_AUTO) handling of the switch...
679 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
681 if(vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
682 return;
684 complete_change_console(new_console);
687 voidwait_for_keypress(void)
689 sleep_on(&keypress_wait);
692 voidstop_tty(struct tty_struct *tty)
694 if(tty->stopped)
695 return;
696 tty->stopped =1;
697 if(tty->link && tty->link->packet) {
698 tty->ctrl_status &= ~TIOCPKT_START;
699 tty->ctrl_status |= TIOCPKT_STOP;
700 wake_up_interruptible(&tty->link->read_wait);
702 if(tty->driver.stop)
703 (tty->driver.stop)(tty);
706 voidstart_tty(struct tty_struct *tty)
708 if(!tty->stopped || tty->flow_stopped)
709 return;
710 tty->stopped =0;
711 if(tty->link && tty->link->packet) {
712 tty->ctrl_status &= ~TIOCPKT_STOP;
713 tty->ctrl_status |= TIOCPKT_START;
714 wake_up_interruptible(&tty->link->read_wait);
716 if(tty->driver.start)
717 (tty->driver.start)(tty);
718 if((tty->flags & (1<< TTY_DO_WRITE_WAKEUP)) &&
719 tty->ldisc.write_wakeup)
720 (tty->ldisc.write_wakeup)(tty);
721 wake_up_interruptible(&tty->write_wait);
724 static longtty_read(struct inode * inode,struct file * file,
725 char* buf,unsigned long count)
727 int i;
728 struct tty_struct * tty;
730 tty = (struct tty_struct *)file->private_data;
731 if(tty_paranoia_check(tty, inode->i_rdev,"tty_read"))
732 return-EIO;
733 if(!tty || (tty->flags & (1<< TTY_IO_ERROR)))
734 return-EIO;
736 /* This check not only needs to be done before reading, but also
737 whenever read_chan() gets woken up after sleeping, so I've
738 moved it to there. This should only be done for the N_TTY
739 line discipline, anyway. Same goes for write_chan(). -- jlc. */
740 #if 0
741 if((inode->i_rdev != CONSOLE_DEV) &&/* don't stop on /dev/console */
742 (tty->pgrp >0) &&
743 (current->tty == tty) &&
744 (tty->pgrp != current->pgrp))
745 if(is_ignored(SIGTTIN) ||is_orphaned_pgrp(current->pgrp))
746 return-EIO;
747 else{
748 (void)kill_pg(current->pgrp, SIGTTIN,1);
749 return-ERESTARTSYS;
751 #endif
752 if(tty->ldisc.read)
753 i = (tty->ldisc.read)(tty,file,buf,count);
754 else
755 i = -EIO;
756 if(i >0)
757 inode->i_atime = CURRENT_TIME;
758 return i;
762 * Split writes up in sane blocksizes to avoid
763 * denial-of-service type attacks
765 staticinlineintdo_tty_write(
766 int(*write)(struct tty_struct *,struct file *,const unsigned char*,unsigned int),
767 struct inode *inode,
768 struct tty_struct *tty,
769 struct file *file,
770 const unsigned char*buf,
771 unsigned int count)
773 int ret =0, written =0;
775 for(;;) {
776 unsigned long size = PAGE_SIZE*2;
777 if(size > count)
778 size = count;
779 ret =write(tty, file, buf, size);
780 if(ret <=0)
781 break;
782 written += ret;
783 buf += ret;
784 count -= ret;
785 if(!count)
786 break;
787 ret = -ERESTARTSYS;
788 if(current->signal & ~current->blocked)
789 break;
790 if(need_resched)
791 schedule();
793 if(written) {
794 inode->i_mtime = CURRENT_TIME;
795 ret = written;
797 return ret;
801 static longtty_write(struct inode * inode,struct file * file,
802 const char* buf,unsigned long count)
804 int is_console;
805 struct tty_struct * tty;
807 is_console = (inode->i_rdev == CONSOLE_DEV);
809 if(is_console && redirect)
810 tty = redirect;
811 else
812 tty = (struct tty_struct *)file->private_data;
813 if(tty_paranoia_check(tty, inode->i_rdev,"tty_write"))
814 return-EIO;
815 if(!tty || !tty->driver.write || (tty->flags & (1<< TTY_IO_ERROR)))
816 return-EIO;
817 #if 0
818 if(!is_console &&L_TOSTOP(tty) && (tty->pgrp >0) &&
819 (current->tty == tty) && (tty->pgrp != current->pgrp)) {
820 if(is_orphaned_pgrp(current->pgrp))
821 return-EIO;
822 if(!is_ignored(SIGTTOU)) {
823 (void)kill_pg(current->pgrp, SIGTTOU,1);
824 return-ERESTARTSYS;
827 #endif
828 if(!tty->ldisc.write)
829 return-EIO;
830 returndo_tty_write(tty->ldisc.write,
831 inode, tty, file,
832 (const unsigned char*)buf,
833 (unsigned int)count);
837 * This is so ripe with races that you should *really* not touch this
838 * unless you know exactly what you are doing. All the changes have to be
839 * made atomically, or there may be incorrect pointers all over the place.
841 static intinit_dev(kdev_t device,struct tty_struct **ret_tty)
843 struct tty_struct *tty, **tty_loc, *o_tty, **o_tty_loc;
844 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
845 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
846 struct tty_driver *driver;
847 int retval;
848 int idx;
850 driver =get_tty_driver(device);
851 if(!driver)
852 return-ENODEV;
854 idx =MINOR(device) - driver->minor_start;
855 tty = o_tty = NULL;
856 tp = o_tp = NULL;
857 ltp = o_ltp = NULL;
858 o_tty_loc = NULL;
859 o_tp_loc = o_ltp_loc = NULL;
861 tty_loc = &driver->table[idx];
862 tp_loc = &driver->termios[idx];
863 ltp_loc = &driver->termios_locked[idx];
865 repeat:
866 retval = -EIO;
867 if(driver->type == TTY_DRIVER_TYPE_PTY &&
868 driver->subtype == PTY_TYPE_MASTER &&
869 *tty_loc && (*tty_loc)->count)
870 goto end_init;
871 retval = -ENOMEM;
872 if(!*tty_loc && !tty) {
873 if(!(tty = (struct tty_struct*)get_free_page(GFP_KERNEL)))
874 goto end_init;
875 initialize_tty_struct(tty);
876 tty->device = device;
877 tty->driver = *driver;
878 goto repeat;
880 if(!*tp_loc && !tp) {
881 tp = (struct termios *)kmalloc(sizeof(struct termios),
882 GFP_KERNEL);
883 if(!tp)
884 goto end_init;
885 *tp = driver->init_termios;
886 goto repeat;
888 if(!*ltp_loc && !ltp) {
889 ltp = (struct termios *)kmalloc(sizeof(struct termios),
890 GFP_KERNEL);
891 if(!ltp)
892 goto end_init;
893 memset(ltp,0,sizeof(struct termios));
894 goto repeat;
896 if(driver->type == TTY_DRIVER_TYPE_PTY) {
897 o_tty_loc = &driver->other->table[idx];
898 o_tp_loc = &driver->other->termios[idx];
899 o_ltp_loc = &driver->other->termios_locked[idx];
901 if(!*o_tty_loc && !o_tty) {
902 kdev_t o_device;
904 o_tty = (struct tty_struct *)
905 get_free_page(GFP_KERNEL);
906 if(!o_tty)
907 goto end_init;
908 o_device =MKDEV(driver->other->major,
909 driver->other->minor_start + idx);
910 initialize_tty_struct(o_tty);
911 o_tty->device = o_device;
912 o_tty->driver = *driver->other;
913 goto repeat;
915 if(!*o_tp_loc && !o_tp) {
916 o_tp = (struct termios *)
917 kmalloc(sizeof(struct termios), GFP_KERNEL);
918 if(!o_tp)
919 goto end_init;
920 *o_tp = driver->other->init_termios;
921 goto repeat;
923 if(!*o_ltp_loc && !o_ltp) {
924 o_ltp = (struct termios *)
925 kmalloc(sizeof(struct termios), GFP_KERNEL);
926 if(!o_ltp)
927 goto end_init;
928 memset(o_ltp,0,sizeof(struct termios));
929 goto repeat;
933 /* Now we have allocated all the structures: update all the pointers.. */
934 if(!*tp_loc) {
935 *tp_loc = tp;
936 tp = NULL;
938 if(!*ltp_loc) {
939 *ltp_loc = ltp;
940 ltp = NULL;
942 if(!*tty_loc) {
943 tty->termios = *tp_loc;
944 tty->termios_locked = *ltp_loc;
945 *tty_loc = tty;
946 (*driver->refcount)++;
947 (*tty_loc)->count++;
948 if(tty->ldisc.open) {
949 retval = (tty->ldisc.open)(tty);
950 if(retval <0) {
951 (*tty_loc)->count--;
952 tty = NULL;
953 goto end_init;
956 tty = NULL;
957 }else{
958 if((*tty_loc)->flags & (1<< TTY_CLOSING)) {
959 printk("Attempt to open closing tty %s.\n",
960 tty_name(*tty_loc));
961 printk("Ack!!!! This should never happen!!\n");
962 return-EINVAL;
964 (*tty_loc)->count++;
966 if(driver->type == TTY_DRIVER_TYPE_PTY) {
967 if(!*o_tp_loc) {
968 *o_tp_loc = o_tp;
969 o_tp = NULL;
971 if(!*o_ltp_loc) {
972 *o_ltp_loc = o_ltp;
973 o_ltp = NULL;
975 if(!*o_tty_loc) {
976 o_tty->termios = *o_tp_loc;
977 o_tty->termios_locked = *o_ltp_loc;
978 *o_tty_loc = o_tty;
979 (*driver->other->refcount)++;
980 if(o_tty->ldisc.open) {
981 retval = (o_tty->ldisc.open)(o_tty);
982 if(retval <0) {
983 (*tty_loc)->count--;
984 o_tty = NULL;
985 goto end_init;
988 o_tty = NULL;
990 (*tty_loc)->link = *o_tty_loc;
991 (*o_tty_loc)->link = *tty_loc;
992 if(driver->subtype == PTY_TYPE_MASTER)
993 (*o_tty_loc)->count++;
995 (*tty_loc)->driver = *driver;
996 *ret_tty = *tty_loc;
997 retval =0;
998 end_init:
999 if(tty)
1000 free_page((unsigned long) tty);
1001 if(o_tty)
1002 free_page((unsigned long) o_tty);
1003 if(tp)
1004 kfree_s(tp,sizeof(struct termios));
1005 if(o_tp)
1006 kfree_s(o_tp,sizeof(struct termios));
1007 if(ltp)
1008 kfree_s(ltp,sizeof(struct termios));
1009 if(o_ltp)
1010 kfree_s(o_ltp,sizeof(struct termios));
1011 return retval;
1015 * Even releasing the tty structures is a tricky business.. We have
1016 * to be very careful that the structures are all released at the
1017 * same time, as interrupts might otherwise get the wrong pointers.
1019 static voidrelease_dev(struct file * filp)
1021 struct tty_struct *tty, *o_tty;
1022 struct termios *tp, *o_tp, *ltp, *o_ltp;
1023 struct task_struct **p;
1024 int idx;
1026 tty = (struct tty_struct *)filp->private_data;
1027 if(tty_paranoia_check(tty, filp->f_inode->i_rdev,"release_dev"))
1028 return;
1030 check_tty_count(tty,"release_dev");
1032 tty_fasync(filp->f_inode, filp,0);
1034 tp = tty->termios;
1035 ltp = tty->termios_locked;
1037 idx =MINOR(tty->device) - tty->driver.minor_start;
1038 #ifdef TTY_PARANOIA_CHECK
1039 if(idx <0|| idx >= tty->driver.num) {
1040 printk("release_dev: bad idx when trying to free (%s)\n",
1041 kdevname(tty->device));
1042 return;
1044 if(tty != tty->driver.table[idx]) {
1045 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1046 idx,kdevname(tty->device));
1047 return;
1049 if(tp != tty->driver.termios[idx]) {
1050 printk("release_dev: driver.termios[%d] not termios for ("
1051 "%s)\n",
1052 idx,kdevname(tty->device));
1053 return;
1055 if(ltp != tty->driver.termios_locked[idx]) {
1056 printk("release_dev: driver.termios_locked[%d] not termios_locked for ("
1057 "%s)\n",
1058 idx,kdevname(tty->device));
1059 return;
1061 #endif
1063 #ifdef TTY_DEBUG_HANGUP
1064 printk("release_dev of %s (tty count=%d)...",tty_name(tty),
1065 tty->count);
1066 #endif
1068 o_tty = tty->link;
1069 o_tp = (o_tty) ? o_tty->termios : NULL;
1070 o_ltp = (o_tty) ? o_tty->termios_locked : NULL;
1072 #ifdef TTY_PARANOIA_CHECK
1073 if(tty->driver.other) {
1074 if(o_tty != tty->driver.other->table[idx]) {
1075 printk("release_dev: other->table[%d] not o_tty for ("
1076 "%s)\n",
1077 idx,kdevname(tty->device));
1078 return;
1080 if(o_tp != tty->driver.other->termios[idx]) {
1081 printk("release_dev: other->termios[%d] not o_termios for ("
1082 "%s)\n",
1083 idx,kdevname(tty->device));
1084 return;
1086 if(o_ltp != tty->driver.other->termios_locked[idx]) {
1087 printk("release_dev: other->termios_locked[%d] not o_termios_locked for ("
1088 "%s)\n",
1089 idx,kdevname(tty->device));
1090 return;
1093 if(o_tty->link != tty) {
1094 printk("release_dev: bad pty pointers\n");
1095 return;
1098 #endif
1100 if(tty->driver.close)
1101 tty->driver.close(tty, filp);
1102 if(tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1103 tty->driver.subtype == PTY_TYPE_MASTER) {
1104 if(--tty->link->count <0) {
1105 printk("release_dev: bad pty slave count (%d) for %s\n",
1106 tty->count,tty_name(tty));
1107 tty->link->count =0;
1110 if(--tty->count <0) {
1111 printk("release_dev: bad tty->count (%d) for %s\n",
1112 tty->count,tty_name(tty));
1113 tty->count =0;
1115 if(tty->count)
1116 return;
1119 * We're committed; at this point, we must not block!
1121 if(o_tty) {
1122 if(o_tty->count)
1123 return;
1124 tty->driver.other->table[idx] = NULL;
1125 tty->driver.other->termios[idx] = NULL;
1126 kfree_s(o_tp,sizeof(struct termios));
1129 #ifdef TTY_DEBUG_HANGUP
1130 printk("freeing tty structure...");
1131 #endif
1132 tty->flags |= (1<< TTY_CLOSING);
1135 * Make sure there aren't any processes that still think this
1136 * tty is their controlling tty.
1138 for(p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1139 if(*p ==0)
1140 continue;
1141 if((*p)->tty == tty)
1142 (*p)->tty = NULL;
1143 if(o_tty && (*p)->tty == o_tty)
1144 (*p)->tty = NULL;
1148 * Shutdown the current line discipline, and reset it to
1149 * N_TTY.
1151 if(tty->ldisc.close)
1152 (tty->ldisc.close)(tty);
1153 tty->ldisc = ldiscs[N_TTY];
1154 tty->termios->c_line = N_TTY;
1155 if(o_tty) {
1156 if(o_tty->ldisc.close)
1157 (o_tty->ldisc.close)(o_tty);
1158 o_tty->ldisc = ldiscs[N_TTY];
1161 tty->driver.table[idx] = NULL;
1162 if(tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
1163 tty->driver.termios[idx] = NULL;
1164 kfree_s(tp,sizeof(struct termios));
1166 if(tty == redirect || o_tty == redirect)
1167 redirect = NULL;
1169 * Make sure that the tty's task queue isn't activated. If it
1170 * is, take it out of the linked list.
1172 cli();
1173 if(tty->flip.tqueue.sync) {
1174 struct tq_struct *tq, *prev;
1176 for(tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
1177 if(tq == &tty->flip.tqueue) {
1178 if(prev)
1179 prev->next = tq->next;
1180 else
1181 tq_timer = tq->next;
1182 break;
1186 sti();
1187 tty->magic =0;
1188 (*tty->driver.refcount)--;
1189 free_page((unsigned long) tty);
1190 filp->private_data =0;
1191 if(o_tty) {
1192 o_tty->magic =0;
1193 (*o_tty->driver.refcount)--;
1194 free_page((unsigned long) o_tty);
1199 * tty_open and tty_release keep up the tty count that contains the
1200 * number of opens done on a tty. We cannot use the inode-count, as
1201 * different inodes might point to the same tty.
1203 * Open-counting is needed for pty masters, as well as for keeping
1204 * track of serial lines: DTR is dropped when the last close happens.
1205 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1207 * The termios state of a pty is reset on first open so that
1208 * settings don't persist across reuse.
1210 static inttty_open(struct inode * inode,struct file * filp)
1212 struct tty_struct *tty;
1213 int minor;
1214 int noctty, retval;
1215 kdev_t device;
1217 retry_open:
1218 noctty = filp->f_flags & O_NOCTTY;
1219 device = inode->i_rdev;
1220 if(device == TTY_DEV) {
1221 if(!current->tty)
1222 return-ENXIO;
1223 device = current->tty->device;
1224 /* noctty = 1; */
1226 if(device == CONSOLE_DEV) {
1227 device =MKDEV(TTY_MAJOR, fg_console+1);
1228 noctty =1;
1230 minor =MINOR(device);
1232 retval =init_dev(device, &tty);
1233 if(retval)
1234 return retval;
1235 filp->private_data = tty;
1236 check_tty_count(tty,"tty_open");
1237 if(tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1238 tty->driver.subtype == PTY_TYPE_MASTER)
1239 noctty =1;
1240 #ifdef TTY_DEBUG_HANGUP
1241 printk("opening %s...",tty_name(tty));
1242 #endif
1243 if(tty->driver.open)
1244 retval = tty->driver.open(tty, filp);
1245 else
1246 retval = -ENODEV;
1248 if(!retval &&test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1249 retval = -EBUSY;
1251 if(retval) {
1252 #ifdef TTY_DEBUG_HANGUP
1253 printk("error %d in opening %s...", retval,tty_name(tty));
1254 #endif
1256 release_dev(filp);
1257 if(retval != -ERESTARTSYS)
1258 return retval;
1259 if(current->signal & ~current->blocked)
1260 return retval;
1261 schedule();
1263 * Need to reset f_op in case a hangup happened.
1265 filp->f_op = &tty_fops;
1266 goto retry_open;
1268 if(!noctty &&
1269 current->leader &&
1270 !current->tty &&
1271 tty->session ==0) {
1272 current->tty = tty;
1273 current->tty_old_pgrp =0;
1274 tty->session = current->session;
1275 tty->pgrp = current->pgrp;
1277 return0;
1281 * Note that releasing a pty master also releases the child, so
1282 * we have to make the redirection checks after that and on both
1283 * sides of a pty.
1285 static voidtty_release(struct inode * inode,struct file * filp)
1287 release_dev(filp);
1290 static inttty_select(struct inode * inode,struct file * filp,int sel_type, select_table * wait)
1292 struct tty_struct * tty;
1294 tty = (struct tty_struct *)filp->private_data;
1295 if(tty_paranoia_check(tty, inode->i_rdev,"tty_select"))
1296 return0;
1298 if(tty->ldisc.select)
1299 return(tty->ldisc.select)(tty, inode, filp, sel_type, wait);
1300 return0;
1304 * fasync_helper() is used by some character device drivers (mainly mice)
1305 * to set up the fasync queue. It returns negative on error, 0 if it did
1306 * no changes and positive if it added/deleted the entry.
1308 intfasync_helper(struct inode * inode,struct file * filp,int on,struct fasync_struct **fapp)
1310 struct fasync_struct *fa, **fp;
1311 unsigned long flags;
1313 for(fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
1314 if(fa->fa_file == filp)
1315 break;
1318 if(on) {
1319 if(fa)
1320 return0;
1321 fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1322 if(!fa)
1323 return-ENOMEM;
1324 fa->magic = FASYNC_MAGIC;
1325 fa->fa_file = filp;
1326 save_flags(flags);
1327 cli();
1328 fa->fa_next = *fapp;
1329 *fapp = fa;
1330 restore_flags(flags);
1331 return1;
1333 if(!fa)
1334 return0;
1335 save_flags(flags);
1336 cli();
1337 *fp = fa->fa_next;
1338 restore_flags(flags);
1339 kfree(fa);
1340 return1;
1343 static inttty_fasync(struct inode * inode,struct file * filp,int on)
1345 struct tty_struct * tty;
1346 int retval;
1348 tty = (struct tty_struct *)filp->private_data;
1349 if(tty_paranoia_check(tty, inode->i_rdev,"tty_fasync"))
1350 return0;
1352 retval =fasync_helper(inode, filp, on, &tty->fasync);
1353 if(retval <=0)
1354 return retval;
1356 if(on) {
1357 if(!waitqueue_active(&tty->read_wait))
1358 tty->minimum_to_wake =1;
1359 if(filp->f_owner ==0) {
1360 if(tty->pgrp)
1361 filp->f_owner = -tty->pgrp;
1362 else
1363 filp->f_owner = current->pid;
1365 }else{
1366 if(!tty->fasync && !waitqueue_active(&tty->read_wait))
1367 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1369 return0;
1372 #if 0
1374 * XXX does anyone use this anymore?!?
1376 static intdo_get_ps_info(unsigned long arg)
1378 struct tstruct {
1379 int flag;
1380 int present[NR_TASKS];
1381 struct task_struct tasks[NR_TASKS];
1383 struct tstruct *ts = (struct tstruct *)arg;
1384 struct task_struct **p;
1385 char*c, *d;
1386 int i, n =0;
1388 i =verify_area(VERIFY_WRITE, (void*)arg,sizeof(struct tstruct));
1389 if(i)
1390 return i;
1391 for(p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
1392 if(*p)
1394 c = (char*)(*p);
1395 d = (char*)(ts->tasks+n);
1396 for(i=0; i<sizeof(struct task_struct) ; i++)
1397 put_user(*c++, d++);
1398 put_user(1, ts->present+n);
1400 else
1401 put_user(0, ts->present+n);
1402 return(0);
1404 #endif
1406 static inttiocsti(struct tty_struct *tty,char* arg)
1408 char ch, mbz =0;
1410 if((current->tty != tty) && !suser())
1411 return-EPERM;
1412 if(get_user(ch, arg))
1413 return-EFAULT;
1414 tty->ldisc.receive_buf(tty, &ch, &mbz,1);
1415 return0;
1418 static inttiocgwinsz(struct tty_struct *tty,struct winsize * arg)
1420 if(copy_to_user(arg, &tty->winsize,sizeof(*arg)))
1421 return-EFAULT;
1422 return0;
1425 static inttiocswinsz(struct tty_struct *tty,struct tty_struct *real_tty,
1426 struct winsize * arg)
1428 struct winsize tmp_ws;
1430 if(copy_from_user(&tmp_ws, arg,sizeof(*arg)))
1431 return-EFAULT;
1432 if(!memcmp(&tmp_ws, &tty->winsize,sizeof(*arg)))
1433 return0;
1434 if(tty->pgrp >0)
1435 kill_pg(tty->pgrp, SIGWINCH,1);
1436 if((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp >0))
1437 kill_pg(real_tty->pgrp, SIGWINCH,1);
1438 tty->winsize = tmp_ws;
1439 real_tty->winsize = tmp_ws;
1440 return0;
1443 static inttioccons(struct tty_struct *tty,struct tty_struct *real_tty)
1445 if(tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
1446 if(!suser())
1447 return-EPERM;
1448 redirect = NULL;
1449 return0;
1451 if(redirect)
1452 return-EBUSY;
1453 redirect = real_tty;
1454 return0;
1458 static intfionbio(struct file *file,int*arg)
1460 int nonblock;
1462 if(get_user(nonblock, arg))
1463 return-EFAULT;
1465 if(nonblock)
1466 file->f_flags |= O_NONBLOCK;
1467 else
1468 file->f_flags &= ~O_NONBLOCK;
1469 return0;
1472 static inttiocsctty(struct tty_struct *tty,int arg)
1474 if(current->leader &&
1475 (current->session == tty->session))
1476 return0;
1478 * The process must be a session leader and
1479 * not have a controlling tty already.
1481 if(!current->leader || current->tty)
1482 return-EPERM;
1483 if(tty->session >0) {
1485 * This tty is already the controlling
1486 * tty for another session group!
1488 if((arg ==1) &&suser()) {
1490 * Steal it away
1492 struct task_struct *p;
1494 for_each_task(p)
1495 if(p->tty == tty)
1496 p->tty = NULL;
1497 }else
1498 return-EPERM;
1500 current->tty = tty;
1501 current->tty_old_pgrp =0;
1502 tty->session = current->session;
1503 tty->pgrp = current->pgrp;
1504 return0;
1507 static inttiocgpgrp(struct tty_struct *tty,struct tty_struct *real_tty, pid_t *arg)
1510 * (tty == real_tty) is a cheap way of
1511 * testing if the tty is NOT a master pty.
1513 if(tty == real_tty && current->tty != real_tty)
1514 return-ENOTTY;
1515 returnput_user(real_tty->pgrp, arg);
1518 static inttiocspgrp(struct tty_struct *tty,struct tty_struct *real_tty, pid_t *arg)
1520 pid_t pgrp;
1521 int retval =tty_check_change(real_tty);
1523 if(retval == -EIO)
1524 return-ENOTTY;
1525 if(retval)
1526 return retval;
1527 if(!current->tty ||
1528 (current->tty != real_tty) ||
1529 (real_tty->session != current->session))
1530 return-ENOTTY;
1531 get_user(pgrp, (pid_t *) arg);
1532 if(pgrp <0)
1533 return-EINVAL;
1534 if(session_of_pgrp(pgrp) != current->session)
1535 return-EPERM;
1536 real_tty->pgrp = pgrp;
1537 return0;
1540 static inttioclinux(struct tty_struct *tty,unsigned long arg)
1542 char type, data;
1544 if(tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
1545 return-EINVAL;
1546 if(current->tty != tty && !suser())
1547 return-EPERM;
1548 if(get_user(type, (char*)arg))
1549 return-EFAULT;
1550 switch(type)
1552 case2:
1553 returnset_selection(arg, tty,1);
1554 case3:
1555 returnpaste_selection(tty);
1556 case4:
1557 do_unblank_screen();
1558 return0;
1559 case5:
1560 returnsel_loadlut(arg);
1561 case6:
1564 * Make it possible to react to Shift+Mousebutton.
1565 * Note that 'shift_state' is an undocumented
1566 * kernel-internal variable; programs not closely
1567 * related to the kernel should not use this.
1569 data = shift_state;
1570 returnput_user(data, (char*) arg);
1571 case7:
1572 data =mouse_reporting();
1573 returnput_user(data, (char*) arg);
1574 case10:
1575 set_vesa_blanking(arg);
1576 return0;
1577 case11:/* set kmsg redirect */
1578 if(!suser())
1579 return-EPERM;
1580 if(get_user(data, (char*)arg+1))
1581 return-EFAULT;
1582 kmsg_redirect = data;
1583 return0;
1584 case12:/* get fg_console */
1585 return fg_console;
1587 return-EINVAL;
1590 static inttiocttygstruct(struct tty_struct *tty,struct tty_struct *arg)
1592 if(copy_to_user(arg, tty,sizeof(*arg)))
1593 return-EFAULT;
1594 return0;
1597 static inttiocsetd(struct tty_struct *tty,int*arg)
1599 int retval, ldisc;
1601 retval =tty_check_change(tty);
1602 if(retval)
1603 return retval;
1604 retval =get_user(ldisc, arg);
1605 if(retval)
1606 return retval;
1607 returntty_set_ldisc(tty, ldisc);
1611 * Split this up, as gcc can choke on it otherwise..
1613 static inttty_ioctl(struct inode * inode,struct file * file,
1614 unsigned int cmd,unsigned long arg)
1616 struct tty_struct *tty, *real_tty;
1618 tty = (struct tty_struct *)file->private_data;
1619 if(tty_paranoia_check(tty, inode->i_rdev,"tty_ioctl"))
1620 return-EINVAL;
1622 real_tty = tty;
1623 if(tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1624 tty->driver.subtype == PTY_TYPE_MASTER)
1625 real_tty = tty->link;
1627 switch(cmd) {
1628 case TIOCSTI:
1629 returntiocsti(tty, (char*)arg);
1630 case TIOCGWINSZ:
1631 returntiocgwinsz(tty, (struct winsize *) arg);
1632 case TIOCSWINSZ:
1633 returntiocswinsz(tty, real_tty, (struct winsize *) arg);
1634 case TIOCCONS:
1635 returntioccons(tty, real_tty);
1636 case FIONBIO:
1637 returnfionbio(file, (int*) arg);
1638 case TIOCEXCL:
1639 set_bit(TTY_EXCLUSIVE, &tty->flags);
1640 return0;
1641 case TIOCNXCL:
1642 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1643 return0;
1644 case TIOCNOTTY:
1645 if(current->tty != tty)
1646 return-ENOTTY;
1647 if(current->leader)
1648 disassociate_ctty(0);
1649 current->tty = NULL;
1650 return0;
1651 case TIOCSCTTY:
1652 returntiocsctty(tty, arg);
1653 case TIOCGPGRP:
1654 returntiocgpgrp(tty, real_tty, (pid_t *) arg);
1655 case TIOCSPGRP:
1656 returntiocspgrp(tty, real_tty, (pid_t *) arg);
1657 case TIOCGETD:
1658 returnput_user(tty->ldisc.num, (int*) arg);
1659 case TIOCSETD:
1660 returntiocsetd(tty, (int*) arg);
1661 case TIOCLINUX:
1662 returntioclinux(tty, arg);
1663 case TIOCTTYGSTRUCT:
1664 returntiocttygstruct(tty, (struct tty_struct *) arg);
1666 if(tty->driver.ioctl) {
1667 int retval = (tty->driver.ioctl)(tty, file, cmd, arg);
1668 if(retval != -ENOIOCTLCMD)
1669 return retval;
1671 if(tty->ldisc.ioctl) {
1672 int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1673 if(retval != -ENOIOCTLCMD)
1674 return retval;
1676 return-EINVAL;
1681 * This implements the "Secure Attention Key" --- the idea is to
1682 * prevent trojan horses by killing all processes associated with this
1683 * tty when the user hits the "Secure Attention Key". Required for
1684 * super-paranoid applications --- see the Orange Book for more details.
1686 * This code could be nicer; ideally it should send a HUP, wait a few
1687 * seconds, then send a INT, and then a KILL signal. But you then
1688 * have to coordinate with the init process, since all processes associated
1689 * with the current tty must be dead before the new getty is allowed
1690 * to spawn.
1692 voiddo_SAK(struct tty_struct *tty)
1694 #ifdef TTY_SOFT_SAK
1695 tty_hangup(tty);
1696 #else
1697 struct task_struct **p;
1698 int session;
1699 int i;
1700 struct file *filp;
1702 if(!tty)
1703 return;
1704 session = tty->session;
1705 if(tty->ldisc.flush_buffer)
1706 tty->ldisc.flush_buffer(tty);
1707 if(tty->driver.flush_buffer)
1708 tty->driver.flush_buffer(tty);
1709 for(p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1710 if(!(*p))
1711 continue;
1712 if(((*p)->tty == tty) ||
1713 ((session >0) && ((*p)->session == session)))
1714 send_sig(SIGKILL, *p,1);
1715 else if((*p)->files) {
1716 for(i=0; i < NR_OPEN; i++) {
1717 filp = (*p)->files->fd[i];
1718 if(filp && (filp->f_op == &tty_fops) &&
1719 (filp->private_data == tty)) {
1720 send_sig(SIGKILL, *p,1);
1721 break;
1726 #endif
1730 * This routine is called out of the software interrupt to flush data
1731 * from the flip buffer to the line discipline.
1733 static voidflush_to_ldisc(void*private_)
1735 struct tty_struct *tty = (struct tty_struct *) private_;
1736 unsigned char*cp;
1737 char*fp;
1738 int count;
1740 if(tty->flip.buf_num) {
1741 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1742 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1743 tty->flip.buf_num =0;
1745 cli();
1746 tty->flip.char_buf_ptr = tty->flip.char_buf;
1747 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1748 }else{
1749 cp = tty->flip.char_buf;
1750 fp = tty->flip.flag_buf;
1751 tty->flip.buf_num =1;
1753 cli();
1754 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1755 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1757 count = tty->flip.count;
1758 tty->flip.count =0;
1759 sti();
1761 #if 0
1762 if(count > tty->max_flip_cnt)
1763 tty->max_flip_cnt = count;
1764 #endif
1765 tty->ldisc.receive_buf(tty, cp, fp, count);
1769 * This subroutine initializes a tty structure.
1771 static voidinitialize_tty_struct(struct tty_struct *tty)
1773 memset(tty,0,sizeof(struct tty_struct));
1774 tty->magic = TTY_MAGIC;
1775 tty->ldisc = ldiscs[N_TTY];
1776 tty->pgrp = -1;
1777 tty->flip.char_buf_ptr = tty->flip.char_buf;
1778 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1779 tty->flip.tqueue.routine = flush_to_ldisc;
1780 tty->flip.tqueue.data = tty;
1784 * The default put_char routine if the driver did not define one.
1786 voidtty_default_put_char(struct tty_struct *tty,unsigned char ch)
1788 tty->driver.write(tty,0, &ch,1);
1792 * Called by a tty driver to register itself.
1794 inttty_register_driver(struct tty_driver *driver)
1796 int error;
1798 if(driver->flags & TTY_DRIVER_INSTALLED)
1799 return0;
1801 error =register_chrdev(driver->major, driver->name, &tty_fops);
1802 if(error <0)
1803 return error;
1804 else if(driver->major ==0)
1805 driver->major = error;
1807 if(!driver->put_char)
1808 driver->put_char = tty_default_put_char;
1810 driver->prev =0;
1811 driver->next = tty_drivers;
1812 if(tty_drivers) tty_drivers->prev = driver;
1813 tty_drivers = driver;
1814 return error;
1818 * Called by a tty driver to unregister itself.
1820 inttty_unregister_driver(struct tty_driver *driver)
1822 int retval;
1823 struct tty_driver *p;
1824 int found =0;
1825 const char*othername = NULL;
1827 if(*driver->refcount)
1828 return-EBUSY;
1830 for(p = tty_drivers; p; p = p->next) {
1831 if(p == driver)
1832 found++;
1833 else if(p->major == driver->major)
1834 othername = p->name;
1837 if(!found)
1838 return-ENOENT;
1840 if(othername == NULL) {
1841 retval =unregister_chrdev(driver->major, driver->name);
1842 if(retval)
1843 return retval;
1844 }else
1845 register_chrdev(driver->major, othername, &tty_fops);
1847 if(driver->prev)
1848 driver->prev->next = driver->next;
1849 else
1850 tty_drivers = driver->next;
1852 if(driver->next)
1853 driver->next->prev = driver->prev;
1855 return0;
1860 * Initialize the console device. This is called *early*, so
1861 * we can't necessarily depend on lots of kernel help here.
1862 * Just do some early initializations, and do the complex setup
1863 * later.
1865 longconsole_init(long kmem_start,long kmem_end)
1867 /* Setup the default TTY line discipline. */
1868 memset(ldiscs,0,sizeof(ldiscs));
1869 (void)tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1872 * Set up the standard termios. Individual tty drivers may
1873 * deviate from this; this is used as a template.
1875 memset(&tty_std_termios,0,sizeof(struct termios));
1876 memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
1877 tty_std_termios.c_iflag = ICRNL | IXON;
1878 tty_std_termios.c_oflag = OPOST | ONLCR;
1879 tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
1880 tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1881 ECHOCTL | ECHOKE | IEXTEN;
1884 * set up the console device so that later boot sequences can
1885 * inform about problems etc..
1887 returncon_init(kmem_start);
1890 static struct tty_driver dev_tty_driver, dev_console_driver;
1893 * Ok, now we can initialize the rest of the tty devices and can count
1894 * on memory allocations, interrupts etc..
1896 inttty_init(void)
1898 if(sizeof(struct tty_struct) > PAGE_SIZE)
1899 panic("size of tty structure > PAGE_SIZE!");
1902 * dev_tty_driver and dev_console_driver are actually magic
1903 * devices which get redirected at open time. Nevertheless,
1904 * we register them so that register_chrdev is called
1905 * appropriately.
1907 memset(&dev_tty_driver,0,sizeof(struct tty_driver));
1908 dev_tty_driver.magic = TTY_DRIVER_MAGIC;
1909 dev_tty_driver.name ="tty";
1910 dev_tty_driver.name_base =0;
1911 dev_tty_driver.major = TTY_MAJOR;
1912 dev_tty_driver.minor_start =0;
1913 dev_tty_driver.num =1;
1915 if(tty_register_driver(&dev_tty_driver))
1916 panic("Couldn't register /dev/tty driver\n");
1918 dev_console_driver = dev_tty_driver;
1919 dev_console_driver.name ="console";
1920 dev_console_driver.major = TTYAUX_MAJOR;
1922 if(tty_register_driver(&dev_console_driver))
1923 panic("Couldn't register /dev/console driver\n");
1925 kbd_init();
1926 #ifdef CONFIG_ESP/* init ESP before rs, so rs doesn't see the port */
1927 esp_init();
1928 #endif
1929 #ifdef CONFIG_SERIAL
1930 rs_init();
1931 #endif
1932 #ifdef CONFIG_CYCLADES
1933 cy_init();
1934 #endif
1935 #ifdef CONFIG_STALLION
1936 stl_init();
1937 #endif
1938 #ifdef CONFIG_ISTALLION
1939 stli_init();
1940 #endif
1941 #ifdef CONFIG_DIGI
1942 pcxe_init();
1943 #endif
1944 #ifdef CONFIG_RISCOM8
1945 riscom8_init();
1946 #endif
1947 pty_init();
1948 vcs_init();
1949 return0;
close