tty_io.c 44.2 KB
Newer Older
1
/*
Linus Torvalds's avatar
Linus Torvalds committed
2
 *  linux/drivers/char/tty_io.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5 6
 */

7 8
/*
 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
Linus Torvalds's avatar
Linus Torvalds committed
9 10
 * or rs-channels. It also implements echoing, cooked mode etc.
 *
Linus Torvalds's avatar
Linus Torvalds committed
11
 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
12 13 14 15 16 17 18 19 20 21 22
 *
 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
 * tty_struct and tty_queue structures.  Previously there was a array
 * of 256 tty_struct's which was statically allocated, and the
 * tty_queue structures were allocated at boot time.  Both are now
 * dynamically allocated only when the tty is open.
 *
 * Also restructured routines so that there is more of a separation
 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
 * the low-level tty routines (serial.c, pty.c, console.c).  This
 * makes for cleaner and more compact code.  -TYT, 9/17/92 
23 24 25 26 27
 *
 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
 * which can be dynamically activated and de-activated by the line
 * discipline handling modules (like SLIP).
 *
Linus Torvalds's avatar
Linus Torvalds committed
28
 * NOTE: pay no attention to the line discipline code (yet); its
29 30
 * interface is still subject to change in this version...
 * -- TYT, 1/31/92
31 32 33 34
 *
 * Added functionality to the OPOST tty handling.  No delays, but all
 * other bits should be there.
 *	-- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
Linus Torvalds's avatar
Linus Torvalds committed
35 36 37
 *
 * Rewrote canonical mode and added more termios flags.
 * 	-- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
38
 */
Linus Torvalds's avatar
Linus Torvalds committed
39

40
#include <linux/types.h>
41
#include <linux/major.h>
42 43 44
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/fcntl.h>
45
#include <linux/sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
46
#include <linux/interrupt.h>
47
#include <linux/tty.h>
Linus Torvalds's avatar
Linus Torvalds committed
48
#include <linux/tty_flip.h>
49
#include <linux/timer.h>
50
#include <linux/ctype.h>
51
#include <linux/kd.h>
52 53
#include <linux/mm.h>
#include <linux/string.h>
Linus Torvalds's avatar
Linus Torvalds committed
54
#include <linux/malloc.h>
Linus Torvalds's avatar
Linus Torvalds committed
55
#include <linux/config.h>
56

57 58
#include <asm/segment.h>
#include <asm/system.h>
59
#include <asm/bitops.h>
60

Linus Torvalds's avatar
Linus Torvalds committed
61 62
#include <linux/scc.h>

Linus Torvalds's avatar
Linus Torvalds committed
63
#include "kbd_kern.h"
64
#include "vt_kern.h"
Linus Torvalds's avatar
Linus Torvalds committed
65
#include "selection.h"
66

Linus Torvalds's avatar
Linus Torvalds committed
67
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
Linus Torvalds's avatar
Linus Torvalds committed
68
#define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
Linus Torvalds's avatar
Linus Torvalds committed
69

Linus Torvalds's avatar
Linus Torvalds committed
70
#undef TTY_DEBUG_HANGUP
71

Linus Torvalds's avatar
Linus Torvalds committed
72 73 74
#define TTY_PARANOIA_CHECK
#define CHECK_TTY_COUNT

Linus Torvalds's avatar
Linus Torvalds committed
75 76
extern void do_blank_screen(int nopowersave);
extern void do_unblank_screen(void);
Linus Torvalds's avatar
Linus Torvalds committed
77
extern void set_vesa_blanking(const unsigned long arg);
Linus Torvalds's avatar
Linus Torvalds committed
78 79

struct termios tty_std_termios;		/* for the benefit of tty drivers  */
Linus Torvalds's avatar
Linus Torvalds committed
80
struct tty_driver *tty_drivers = NULL;	/* linked list of tty drivers */
81
struct tty_ldisc ldiscs[NR_LDISCS];	/* line disc dispatch table	*/
Linus Torvalds's avatar
Linus Torvalds committed
82

83 84
/*
 * fg_console is the current virtual console,
Linus Torvalds's avatar
Linus Torvalds committed
85
 * last_console is the last used one
86 87 88
 * redirect is the pseudo-tty that console output
 * is redirected to if asked by TIOCCONS.
 */
Linus Torvalds's avatar
Linus Torvalds committed
89
int fg_console = 0;
Linus Torvalds's avatar
Linus Torvalds committed
90
int last_console = 0;
91
struct tty_struct * redirect = NULL;
92
struct wait_queue * keypress_wait = NULL;
93

Linus Torvalds's avatar
Linus Torvalds committed
94
static void initialize_tty_struct(struct tty_struct *tty);
95

96 97 98 99 100
static int tty_read(struct inode *, struct file *, char *, int);
static int tty_write(struct inode *, struct file *, char *, int);
static int tty_select(struct inode *, struct file *, int, select_table *);
static int tty_open(struct inode *, struct file *);
static void tty_release(struct inode *, struct file *);
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104
static int tty_ioctl(struct inode * inode, struct file * file,
		     unsigned int cmd, unsigned long arg);
static int tty_fasync(struct inode * inode, struct file * filp, int on);

Linus Torvalds's avatar
Linus Torvalds committed
105 106 107
extern void reset_palette(int currcons) ;
extern void set_palette(void) ;

Linus Torvalds's avatar
Linus Torvalds committed
108 109 110 111 112 113 114 115 116 117 118
#ifndef MIN
#define MIN(a,b)	((a) < (b) ? (a) : (b))
#endif

/*
 * These two routines return the name of tty.  tty_name() should NOT
 * be used in interrupt drivers, since it's not re-entrant.  Use
 * _tty_name() instead.
 */
char *_tty_name(struct tty_struct *tty, char *buf)
{
Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123 124
	if (tty)
		sprintf(buf, "%s%d", tty->driver.name,
			MINOR(tty->device) - tty->driver.minor_start +
			tty->driver.name_base);
	else
		strcpy(buf, "NULL tty");
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	return buf;
}

char *tty_name(struct tty_struct *tty)
{
	static char buf[64];

	return(_tty_name(tty, buf));
}

inline int tty_paranoia_check(struct tty_struct *tty, dev_t device,
			      const char *routine)
{
#ifdef TTY_PARANOIA_CHECK
	static const char *badmagic =
		"Warning: bad magic number for tty struct (%d, %d) in %s\n";
	static const char *badtty =
		"Warning: null TTY for (%d, %d) in %s\n";

	if (!tty) {
		printk(badtty, MAJOR(device), MINOR(device), routine);
		return 1;
	}
	if (tty->magic != TTY_MAGIC) {
		printk(badmagic, MAJOR(device), MINOR(device), routine);
		return 1;
	}
#endif
	return 0;
}
155

Linus Torvalds's avatar
Linus Torvalds committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
static int check_tty_count(struct tty_struct *tty, const char *routine)
{
#ifdef CHECK_TTY_COUNT
	struct file *f;
	int i, count = 0;
	
	for (f = first_file, i=0; i<nr_files; i++, f = f->f_next) {
		if (!f->f_count)
			continue;
		if (f->private_data == tty) {
			count++;
		}
	}
	if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
	    tty->driver.subtype == PTY_TYPE_SLAVE &&
	    tty->link && tty->link->count)
		count++;
	if (tty->count != count) {
		printk("Warning: dev (%d, %d) tty->count(%d) != #fd's(%d) in %s\n",
		       MAJOR(tty->device), MINOR(tty->device), tty->count,
		       count, routine);
		return count;
       }	
#endif
	return 0;
}

183
int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
184 185 186 187
{
	if (disc < N_TTY || disc >= NR_LDISCS)
		return -EINVAL;
	
188 189
	if (new_ldisc) {
		ldiscs[disc] = *new_ldisc;
190
		ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
Linus Torvalds's avatar
Linus Torvalds committed
191
		ldiscs[disc].num = disc;
192 193 194 195 196 197
	} else
		memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
	
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
198 199
/* Set the discipline of a tty line. */
static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
200
{
Linus Torvalds's avatar
Linus Torvalds committed
201
	int	retval = 0;
Linus Torvalds's avatar
Linus Torvalds committed
202
	struct	tty_ldisc o_ldisc;
203

Linus Torvalds's avatar
Linus Torvalds committed
204 205 206
	if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS) ||
	    !(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
		return -EINVAL;
207

Linus Torvalds's avatar
Linus Torvalds committed
208 209 210
	if (tty->ldisc.num == ldisc)
		return 0;	/* We are already in the desired discipline */
	o_ldisc = tty->ldisc;
211

Linus Torvalds's avatar
Linus Torvalds committed
212 213
	tty_wait_until_sent(tty, 0);
	
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216 217 218 219 220
	/* Shutdown the current discipline. */
	if (tty->ldisc.close)
		(tty->ldisc.close)(tty);

	/* Now set up the new line discipline. */
	tty->ldisc = ldiscs[ldisc];
	tty->termios->c_line = ldisc;
Linus Torvalds's avatar
Linus Torvalds committed
221
	if (tty->ldisc.open)
Linus Torvalds's avatar
Linus Torvalds committed
222
		retval = (tty->ldisc.open)(tty);
Linus Torvalds's avatar
Linus Torvalds committed
223
	if (retval < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
224 225
		tty->ldisc = o_ldisc;
		tty->termios->c_line = tty->ldisc.num;
Linus Torvalds's avatar
Linus Torvalds committed
226
		if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
Linus Torvalds's avatar
Linus Torvalds committed
227 228 229 230 231
			tty->ldisc = ldiscs[N_TTY];
			tty->termios->c_line = N_TTY;
			if (tty->ldisc.open) {
				int r = tty->ldisc.open(tty);

Linus Torvalds's avatar
Linus Torvalds committed
232
				if (r < 0)
Linus Torvalds's avatar
Linus Torvalds committed
233 234 235 236 237
					panic("Couldn't open N_TTY ldisc for "
					      "%s --- error %d.",
					      tty_name(tty), r);
			}
		}
238
	}
Linus Torvalds's avatar
Linus Torvalds committed
239 240 241
	if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
		tty->driver.set_ldisc(tty);
	return retval;
242 243
}

244
/*
Linus Torvalds's avatar
Linus Torvalds committed
245
 * This routine returns a tty driver structure, given a device number
246
 */
Linus Torvalds's avatar
Linus Torvalds committed
247
struct tty_driver *get_tty_driver(dev_t device)
248
{
Linus Torvalds's avatar
Linus Torvalds committed
249 250 251 252 253
	int	major, minor;
	struct tty_driver *p;
	
	minor = MINOR(device);
	major = MAJOR(device);
254

Linus Torvalds's avatar
Linus Torvalds committed
255 256 257 258 259 260 261 262
	for (p = tty_drivers; p; p = p->next) {
		if (p->major != major)
			continue;
		if (minor < p->minor_start)
			continue;
		if (minor >= p->minor_start + p->num)
			continue;
		return p;
263
	}
Linus Torvalds's avatar
Linus Torvalds committed
264
	return NULL;
265 266
}

Linus Torvalds's avatar
Linus Torvalds committed
267 268 269 270 271 272
/*
 * If we try to write to, or set the state of, a terminal and we're
 * not in the foreground, send a SIGTTOU.  If the signal is blocked or
 * ignored, go ahead and perform the operation.  (POSIX 7.2)
 */
int tty_check_change(struct tty_struct * tty)
273
{
Linus Torvalds's avatar
Linus Torvalds committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287
	if (current->tty != tty)
		return 0;
	if (tty->pgrp <= 0) {
		printk("tty_check_change: tty->pgrp <= 0!\n");
		return 0;
	}
	if (current->pgrp == tty->pgrp)
		return 0;
	if (is_ignored(SIGTTOU))
		return 0;
	if (is_orphaned_pgrp(current->pgrp))
		return -EIO;
	(void) kill_pg(current->pgrp,SIGTTOU,1);
	return -ERESTARTSYS;
288
}
289

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
{
	return 0;
}

static int hung_up_tty_write(struct inode * inode, struct file * file, char * buf, int count)
{
	return -EIO;
}

static int hung_up_tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
{
	return 1;
}

305
static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
306
			     unsigned int cmd, unsigned long arg)
307 308 309 310
{
	return -EIO;
}

311 312
static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
{
313
	return -ESPIPE;
314 315 316 317 318 319 320 321 322 323 324
}

static struct file_operations tty_fops = {
	tty_lseek,
	tty_read,
	tty_write,
	NULL,		/* tty_readdir */
	tty_select,
	tty_ioctl,
	NULL,		/* tty_mmap */
	tty_open,
Linus Torvalds's avatar
Linus Torvalds committed
325 326 327
	tty_release,
	NULL,		/* tty_fsync */
	tty_fasync
328 329 330
};

static struct file_operations hung_up_tty_fops = {
331 332 333 334 335 336 337
	tty_lseek,
	hung_up_tty_read,
	hung_up_tty_write,
	NULL,		/* hung_up_tty_readdir */
	hung_up_tty_select,
	hung_up_tty_ioctl,
	NULL,		/* hung_up_tty_mmap */
Linus Torvalds's avatar
Linus Torvalds committed
338
	NULL,		/* hung_up_tty_open */
Linus Torvalds's avatar
Linus Torvalds committed
339 340 341
	tty_release,	/* hung_up_tty_release */
	NULL,		/* hung_up_tty_fsync  */
	NULL		/* hung_up_tty_fasync */
342 343 344
};

void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
345
{
346
	int i;
347
	struct file * filp;
Linus Torvalds's avatar
Linus Torvalds committed
348
	struct task_struct *p;
349 350

	if (!tty)
351
		return;
Linus Torvalds's avatar
Linus Torvalds committed
352
	check_tty_count(tty, "do_tty_hangup");
353
	for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
Linus Torvalds's avatar
Linus Torvalds committed
354
		if (!filp->f_count)
355
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
356
		if (filp->private_data != tty)
357
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
358
		if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
359
			continue;
360 361
		if (filp->f_op != &tty_fops)
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
362
		tty_fasync(filp->f_inode, filp, 0);
363
		filp->f_op = fops;
364
	}
Linus Torvalds's avatar
Linus Torvalds committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	
	if (tty->ldisc.flush_buffer)
		tty->ldisc.flush_buffer(tty);
	if (tty->driver.flush_buffer)
		tty->driver.flush_buffer(tty);
	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
	    tty->ldisc.write_wakeup)
		(tty->ldisc.write_wakeup)(tty);
	wake_up_interruptible(&tty->write_wait);
	wake_up_interruptible(&tty->read_wait);

	/*
	 * Shutdown the current line discipline, and reset it to
	 * N_TTY.
	 */
	if (tty->ldisc.num != ldiscs[N_TTY].num) {
		if (tty->ldisc.close)
			(tty->ldisc.close)(tty);
		tty->ldisc = ldiscs[N_TTY];
		tty->termios->c_line = N_TTY;
		if (tty->ldisc.open) {
			i = (tty->ldisc.open)(tty);
Linus Torvalds's avatar
Linus Torvalds committed
387
			if (i < 0)
Linus Torvalds's avatar
Linus Torvalds committed
388
				printk("do_tty_hangup: N_TTY open: error %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
389
				       -i);
Linus Torvalds's avatar
Linus Torvalds committed
390 391 392
		}
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
393 394 395 396 397 398 399 400 401 402
 	for_each_task(p) {
		if ((tty->session > 0) && (p->session == tty->session) &&
		    p->leader) {
			send_sig(SIGHUP,p,1);
			send_sig(SIGCONT,p,1);
			if (tty->pgrp > 0)
				p->tty_old_pgrp = tty->pgrp;
		}
		if (p->tty == tty)
			p->tty = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
403
	}
Linus Torvalds's avatar
Linus Torvalds committed
404
	tty->flags = 0;
405 406
	tty->session = 0;
	tty->pgrp = -1;
Linus Torvalds's avatar
Linus Torvalds committed
407
	tty->ctrl_status = 0;
Linus Torvalds's avatar
Linus Torvalds committed
408 409
	if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
		*tty->termios = tty->driver.init_termios;
Linus Torvalds's avatar
Linus Torvalds committed
410 411
	if (tty->driver.hangup)
		(tty->driver.hangup)(tty);
412 413
}

414 415
void tty_hangup(struct tty_struct * tty)
{
Linus Torvalds's avatar
Linus Torvalds committed
416
#ifdef TTY_DEBUG_HANGUP
Linus Torvalds's avatar
Linus Torvalds committed
417
	printk("%s hangup...\n", tty_name(tty));
Linus Torvalds's avatar
Linus Torvalds committed
418
#endif
419 420 421 422 423
	do_tty_hangup(tty, &hung_up_tty_fops);
}

void tty_vhangup(struct tty_struct * tty)
{
Linus Torvalds's avatar
Linus Torvalds committed
424
#ifdef TTY_DEBUG_HANGUP
Linus Torvalds's avatar
Linus Torvalds committed
425
	printk("%s vhangup...\n", tty_name(tty));
Linus Torvalds's avatar
Linus Torvalds committed
426 427
#endif
	do_tty_hangup(tty, &hung_up_tty_fops);
428 429
}

Linus Torvalds's avatar
Linus Torvalds committed
430
int tty_hung_up_p(struct file * filp)
431
{
Linus Torvalds's avatar
Linus Torvalds committed
432
	return (filp->f_op == &hung_up_tty_fops);
433 434
}

Linus Torvalds's avatar
Linus Torvalds committed
435 436
/*
 * This function is typically called only by the session leader, when
Linus Torvalds's avatar
Linus Torvalds committed
437
 * it wants to disassociate itself from its controlling tty.
Linus Torvalds's avatar
Linus Torvalds committed
438 439
 *
 * It performs the following functions:
Linus Torvalds's avatar
Linus Torvalds committed
440
 * 	(1)  Sends a SIGHUP and SIGCONT to the foreground process group
Linus Torvalds's avatar
Linus Torvalds committed
441 442 443 444 445
 * 	(2)  Clears the tty from being controlling the session
 * 	(3)  Clears the controlling tty for all processes in the
 * 		session group.
 */
void disassociate_ctty(int priv)
446
{
Linus Torvalds's avatar
Linus Torvalds committed
447
	struct tty_struct *tty = current->tty;
Linus Torvalds's avatar
Linus Torvalds committed
448 449
	struct task_struct *p;

Linus Torvalds's avatar
Linus Torvalds committed
450 451 452 453 454
	if (!tty) {
		if (current->tty_old_pgrp) {
			kill_pg(current->tty_old_pgrp, SIGHUP, priv);
			kill_pg(current->tty_old_pgrp, SIGCONT, priv);
		}
Linus Torvalds's avatar
Linus Torvalds committed
455
		return;
Linus Torvalds's avatar
Linus Torvalds committed
456
	}
Linus Torvalds's avatar
Linus Torvalds committed
457 458 459
	if (tty->pgrp > 0) {
		kill_pg(tty->pgrp, SIGHUP, priv);
		kill_pg(tty->pgrp, SIGCONT, priv);
Linus Torvalds's avatar
Linus Torvalds committed
460
	}
Linus Torvalds's avatar
Linus Torvalds committed
461

Linus Torvalds's avatar
Linus Torvalds committed
462
	current->tty_old_pgrp = 0;
Linus Torvalds's avatar
Linus Torvalds committed
463 464
	tty->session = 0;
	tty->pgrp = -1;
Linus Torvalds's avatar
Linus Torvalds committed
465 466 467

	for_each_task(p)
	  	if (p->session == current->session)
Linus Torvalds's avatar
Linus Torvalds committed
468
			p->tty = NULL;
469 470
}

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
/*
 * Sometimes we want to wait until a particular VT has been activated. We
 * do it in a very simple manner. Everybody waits on a single queue and
 * get woken up at once. Those that are satisfied go on with their business,
 * while those not ready go back to sleep. Seems overkill to add a wait
 * to each vt just for this - usually this does nothing!
 */
static struct wait_queue *vt_activate_queue = NULL;

/*
 * Sleeps until a vt is activated, or the task is interrupted. Returns
 * 0 if activation, -1 if interrupted.
 */
int vt_waitactive(void)
{
	interruptible_sleep_on(&vt_activate_queue);
	return (current->signal & ~current->blocked) ? -1 : 0;
}

#define vt_wake_waitactive() wake_up(&vt_activate_queue)

Linus Torvalds's avatar
Linus Torvalds committed
492 493 494 495 496 497 498 499 500 501 502
void reset_vc(unsigned int new_console)
{
	vt_cons[new_console]->vc_mode = KD_TEXT;
	kbd_table[new_console].kbdmode = VC_XLATE;
	vt_cons[new_console]->vt_mode.mode = VT_AUTO;
	vt_cons[new_console]->vt_mode.waitv = 0;
	vt_cons[new_console]->vt_mode.relsig = 0;
	vt_cons[new_console]->vt_mode.acqsig = 0;
	vt_cons[new_console]->vt_mode.frsig = 0;
	vt_cons[new_console]->vt_pid = -1;
	vt_cons[new_console]->vt_newvt = -1;
Linus Torvalds's avatar
Linus Torvalds committed
503
	reset_palette (new_console) ;
Linus Torvalds's avatar
Linus Torvalds committed
504 505
}

506 507 508 509 510 511 512
/*
 * Performs the back end of a vt switch
 */
void complete_change_console(unsigned int new_console)
{
	unsigned char old_vc_mode;

Linus Torvalds's avatar
Linus Torvalds committed
513 514 515 516
        if (new_console == fg_console)
                return;
        if (!vc_cons_allocated(new_console))
                return;
Linus Torvalds's avatar
Linus Torvalds committed
517
	last_console = fg_console;
518 519 520 521 522 523

	/*
	 * If we're switching, we could be going from KD_GRAPHICS to
	 * KD_TEXT mode or vice versa, which means we need to blank or
	 * unblank the screen later.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
524
	old_vc_mode = vt_cons[fg_console]->vc_mode;
525
	update_screen(new_console);
526 527 528 529 530 531

	/*
	 * If this new console is under process control, send it a signal
	 * telling it that it has acquired. Also check if it has died and
	 * clean up (similar to logic employed in change_console())
	 */
Linus Torvalds's avatar
Linus Torvalds committed
532
	if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
533 534 535 536 537 538
	{
		/*
		 * Send the signal as privileged - kill_proc() will
		 * tell us if the process has gone or something else
		 * is awry
		 */
Linus Torvalds's avatar
Linus Torvalds committed
539 540
		if (kill_proc(vt_cons[new_console]->vt_pid,
			      vt_cons[new_console]->vt_mode.acqsig,
541 542 543 544 545 546 547 548 549 550 551
			      1) != 0)
		{
		/*
		 * The controlling process has died, so we revert back to
		 * normal operation. In this case, we'll also change back
		 * to KD_TEXT mode. I'm not sure if this is strictly correct
		 * but it saves the agony when the X server dies and the screen
		 * remains blanked due to KD_GRAPHICS! It would be nice to do
		 * this outside of VT_PROCESS but there is no single process
		 * to account for and tracking tty count may be undesirable.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
552
		        reset_vc(new_console);
553 554 555 556 557 558 559
		}
	}

	/*
	 * We do this here because the controlling process above may have
	 * gone, and so there is now a new vc_mode
	 */
Linus Torvalds's avatar
Linus Torvalds committed
560
	if (old_vc_mode != vt_cons[new_console]->vc_mode)
561
	{
Linus Torvalds's avatar
Linus Torvalds committed
562
		if (vt_cons[new_console]->vc_mode == KD_TEXT)
Linus Torvalds's avatar
Linus Torvalds committed
563 564 565
			do_unblank_screen();
		else
			do_blank_screen(1);
566 567
	}

Linus Torvalds's avatar
Linus Torvalds committed
568 569 570 571
	/* Set the colour palette for this VT */
	if (vt_cons[new_console]->vc_mode == KD_TEXT)
		set_palette() ;
	
572 573 574 575
	/*
	 * Wake anyone waiting for their VT to activate
	 */
	vt_wake_waitactive();
576 577 578 579 580 581 582 583
	return;
}

/*
 * Performs the front-end of a vt switch
 */
void change_console(unsigned int new_console)
{
Linus Torvalds's avatar
Linus Torvalds committed
584 585 586
        if (new_console == fg_console)
                return;
        if (!vc_cons_allocated(new_console))
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
		return;

	/*
	 * If this vt is in process mode, then we need to handshake with
	 * that process before switching. Essentially, we store where that
	 * vt wants to switch to and wait for it to tell us when it's done
	 * (via VT_RELDISP ioctl).
	 *
	 * We also check to see if the controlling process still exists.
	 * If it doesn't, we reset this vt to auto mode and continue.
	 * This is a cheap way to track process control. The worst thing
	 * that can happen is: we send a signal to a process, it dies, and
	 * the switch gets "lost" waiting for a response; hopefully, the
	 * user will try again, we'll detect the process is gone (unless
	 * the user waits just the right amount of time :-) and revert the
	 * vt to auto control.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
604
	if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
605 606 607 608 609 610
	{
		/*
		 * Send the signal as privileged - kill_proc() will
		 * tell us if the process has gone or something else
		 * is awry
		 */
Linus Torvalds's avatar
Linus Torvalds committed
611 612
		if (kill_proc(vt_cons[fg_console]->vt_pid,
			      vt_cons[fg_console]->vt_mode.relsig,
613 614 615 616 617 618 619
			      1) == 0)
		{
			/*
			 * It worked. Mark the vt to switch to and
			 * return. The process needs to send us a
			 * VT_RELDISP ioctl to complete the switch.
			 */
Linus Torvalds's avatar
Linus Torvalds committed
620
			vt_cons[fg_console]->vt_newvt = new_console;
621 622 623 624 625 626 627 628 629 630 631 632
			return;
		}

		/*
		 * The controlling process has died, so we revert back to
		 * normal operation. In this case, we'll also change back
		 * to KD_TEXT mode. I'm not sure if this is strictly correct
		 * but it saves the agony when the X server dies and the screen
		 * remains blanked due to KD_GRAPHICS! It would be nice to do
		 * this outside of VT_PROCESS but there is no single process
		 * to account for and tracking tty count may be undesirable.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
633 634
		reset_vc(fg_console);

635 636 637 638 639 640 641 642
		/*
		 * Fall through to normal (VT_AUTO) handling of the switch...
		 */
	}

	/*
	 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
	 */
Linus Torvalds's avatar
Linus Torvalds committed
643
	if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
644 645 646
		return;

	complete_change_console(new_console);
647 648
}

649 650
void wait_for_keypress(void)
{
651
	sleep_on(&keypress_wait);
652 653
}

Linus Torvalds's avatar
Linus Torvalds committed
654 655 656 657 658 659 660 661
void stop_tty(struct tty_struct *tty)
{
	if (tty->stopped)
		return;
	tty->stopped = 1;
	if (tty->link && tty->link->packet) {
		tty->ctrl_status &= ~TIOCPKT_START;
		tty->ctrl_status |= TIOCPKT_STOP;
Linus Torvalds's avatar
Linus Torvalds committed
662
		wake_up_interruptible(&tty->link->read_wait);
Linus Torvalds's avatar
Linus Torvalds committed
663
	}
Linus Torvalds's avatar
Linus Torvalds committed
664 665
	if (tty->driver.stop)
		(tty->driver.stop)(tty);
Linus Torvalds's avatar
Linus Torvalds committed
666 667 668 669 670 671 672 673 674 675
}

void start_tty(struct tty_struct *tty)
{
	if (!tty->stopped)
		return;
	tty->stopped = 0;
	if (tty->link && tty->link->packet) {
		tty->ctrl_status &= ~TIOCPKT_STOP;
		tty->ctrl_status |= TIOCPKT_START;
Linus Torvalds's avatar
Linus Torvalds committed
676
		wake_up_interruptible(&tty->link->read_wait);
Linus Torvalds's avatar
Linus Torvalds committed
677
	}
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680 681 682 683
	if (tty->driver.start)
		(tty->driver.start)(tty);
	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
	    tty->ldisc.write_wakeup)
		(tty->ldisc.write_wakeup)(tty);
	wake_up_interruptible(&tty->write_wait);
684 685
}

686 687
static int tty_read(struct inode * inode, struct file * file, char * buf, int count)
{
Linus Torvalds's avatar
Linus Torvalds committed
688
	int i;
689
	struct tty_struct * tty;
690

Linus Torvalds's avatar
Linus Torvalds committed
691
	tty = (struct tty_struct *)file->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
692 693
	if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
		return -EIO;
694
	if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
695
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
696 697 698 699 700 701

	/* This check not only needs to be done before reading, but also
	   whenever read_chan() gets woken up after sleeping, so I've
	   moved it to there.  This should only be done for the N_TTY
	   line discipline, anyway.  Same goes for write_chan(). -- jlc. */
#if 0
Linus Torvalds's avatar
Linus Torvalds committed
702
	if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
703
	    (tty->pgrp > 0) &&
Linus Torvalds's avatar
Linus Torvalds committed
704
	    (current->tty == tty) &&
705 706 707 708 709 710 711
	    (tty->pgrp != current->pgrp))
		if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
			return -EIO;
		else {
			(void) kill_pg(current->pgrp, SIGTTIN, 1);
			return -ERESTARTSYS;
		}
Linus Torvalds's avatar
Linus Torvalds committed
712
#endif
Linus Torvalds's avatar
Linus Torvalds committed
713
	if (tty->ldisc.read)
Linus Torvalds's avatar
Linus Torvalds committed
714
		/* XXX casts are for what kernel-wide prototypes should be. */
Linus Torvalds's avatar
Linus Torvalds committed
715
		i = (tty->ldisc.read)(tty,file,(unsigned char *)buf,(unsigned int)count);
716 717
	else
		i = -EIO;
718 719 720
	if (i > 0)
		inode->i_atime = CURRENT_TIME;
	return i;
721 722
}

723
static int tty_write(struct inode * inode, struct file * file, char * buf, int count)
724
{
Linus Torvalds's avatar
Linus Torvalds committed
725
	int i, is_console;
726 727
	struct tty_struct * tty;

Linus Torvalds's avatar
Linus Torvalds committed
728
	is_console = (inode->i_rdev == CONSOLE_DEV);
Linus Torvalds's avatar
Linus Torvalds committed
729

730
	if (is_console && redirect)
731 732
		tty = redirect;
	else
Linus Torvalds's avatar
Linus Torvalds committed
733
		tty = (struct tty_struct *)file->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736
	if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
		return -EIO;
	if (!tty || !tty->driver.write || (tty->flags & (1 << TTY_IO_ERROR)))
737
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
738
#if 0
739
	if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
Linus Torvalds's avatar
Linus Torvalds committed
740
	    (current->tty == tty) && (tty->pgrp != current->pgrp)) {
741
		if (is_orphaned_pgrp(current->pgrp))
742 743 744 745 746 747
			return -EIO;
		if (!is_ignored(SIGTTOU)) {
			(void) kill_pg(current->pgrp, SIGTTOU, 1);
			return -ERESTARTSYS;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
748
#endif
Linus Torvalds's avatar
Linus Torvalds committed
749
	if (tty->ldisc.write)
Linus Torvalds's avatar
Linus Torvalds committed
750
		/* XXX casts are for what kernel-wide prototypes should be. */
Linus Torvalds's avatar
Linus Torvalds committed
751
		i = (tty->ldisc.write)(tty,file,(unsigned char *)buf,(unsigned int)count);
752 753
	else
		i = -EIO;
754 755 756
	if (i > 0)
		inode->i_mtime = CURRENT_TIME;
	return i;
757 758
}

759 760 761 762 763
/*
 * This is so ripe with races that you should *really* not touch this
 * unless you know exactly what you are doing. All the changes have to be
 * made atomically, or there may be incorrect pointers all over the place.
 */
Linus Torvalds's avatar
Linus Torvalds committed
764
static int init_dev(dev_t device, struct tty_struct **ret_tty)
765
{
Linus Torvalds's avatar
Linus Torvalds committed
766 767 768 769
	struct tty_struct *tty, **tty_loc, *o_tty, **o_tty_loc;
	struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
	struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
	struct tty_driver *driver;	
770
	int retval;
Linus Torvalds's avatar
Linus Torvalds committed
771 772 773 774 775
	int idx;

	driver = get_tty_driver(device);
	if (!driver)
		return -ENODEV;
776

Linus Torvalds's avatar
Linus Torvalds committed
777
	idx = MINOR(device) - driver->minor_start;
778 779
	tty = o_tty = NULL;
	tp = o_tp = NULL;
780
	ltp = o_ltp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
781 782 783 784 785 786 787
	o_tty_loc = NULL;
	o_tp_loc = o_ltp_loc = NULL;

	tty_loc = &driver->table[idx];
	tp_loc = &driver->termios[idx];
	ltp_loc = &driver->termios_locked[idx];

Linus Torvalds's avatar
Linus Torvalds committed
788
repeat:
789
	retval = -EAGAIN;
Linus Torvalds's avatar
Linus Torvalds committed
790 791 792
	if (driver->type == TTY_DRIVER_TYPE_PTY &&
	    driver->subtype == PTY_TYPE_MASTER &&
	    *tty_loc && (*tty_loc)->count)
793 794
		goto end_init;
	retval = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
795
	if (!*tty_loc && !tty) {
796
		if (!(tty = (struct tty_struct*) get_free_page(GFP_KERNEL)))
797
			goto end_init;
Linus Torvalds's avatar
Linus Torvalds committed
798 799 800
		initialize_tty_struct(tty);
		tty->device = device;
		tty->driver = *driver;
801 802
		goto repeat;
	}
Linus Torvalds's avatar
Linus Torvalds committed
803
	if (!*tp_loc && !tp) {
804 805
		tp = (struct termios *) kmalloc(sizeof(struct termios),
						GFP_KERNEL);
806 807
		if (!tp)
			goto end_init;
Linus Torvalds's avatar
Linus Torvalds committed
808
		*tp = driver->init_termios;
809 810
		goto repeat;
	}
Linus Torvalds's avatar
Linus Torvalds committed
811
	if (!*ltp_loc && !ltp) {
812 813 814 815 816 817 818
		ltp = (struct termios *) kmalloc(sizeof(struct termios),
						 GFP_KERNEL);
		if (!ltp)
			goto end_init;
		memset(ltp, 0, sizeof(struct termios));
		goto repeat;
	}
Linus Torvalds's avatar
Linus Torvalds committed
819 820 821 822 823 824 825 826
	if (driver->type == TTY_DRIVER_TYPE_PTY) {
		o_tty_loc = &driver->other->table[idx];
		o_tp_loc = &driver->other->termios[idx];
		o_ltp_loc = &driver->other->termios_locked[idx];

		if (!*o_tty_loc && !o_tty) {
			dev_t 	o_device;
			
827 828
			o_tty = (struct tty_struct *)
				get_free_page(GFP_KERNEL);
829 830
			if (!o_tty)
				goto end_init;
Linus Torvalds's avatar
Linus Torvalds committed
831 832 833 834 835
			o_device = MKDEV(driver->other->major,
					 driver->other->minor_start + idx);
			initialize_tty_struct(o_tty);
			o_tty->device = o_device;
			o_tty->driver = *driver->other;
836 837
			goto repeat;
		}
Linus Torvalds's avatar
Linus Torvalds committed
838
		if (!*o_tp_loc && !o_tp) {
839 840
			o_tp = (struct termios *)
				kmalloc(sizeof(struct termios), GFP_KERNEL);
841 842
			if (!o_tp)
				goto end_init;
Linus Torvalds's avatar
Linus Torvalds committed
843
			*o_tp = driver->other->init_termios;
844 845
			goto repeat;
		}
Linus Torvalds's avatar
Linus Torvalds committed
846
		if (!*o_ltp_loc && !o_ltp) {
847 848 849 850 851 852 853 854
			o_ltp = (struct termios *)
				kmalloc(sizeof(struct termios), GFP_KERNEL);
			if (!o_ltp)
				goto end_init;
			memset(o_ltp, 0, sizeof(struct termios));
			goto repeat;
		}
		
855 856
	}
	/* Now we have allocated all the structures: update all the pointers.. */
Linus Torvalds's avatar
Linus Torvalds committed
857 858
	if (!*tp_loc) {
		*tp_loc = tp;
859 860
		tp = NULL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
861 862
	if (!*ltp_loc) {
		*ltp_loc = ltp;
863 864
		ltp = NULL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
865 866 867 868 869
	if (!*tty_loc) {
		tty->termios = *tp_loc;
		tty->termios_locked = *ltp_loc;
		*tty_loc = tty;
		(*driver->refcount)++;
Linus Torvalds's avatar
Linus Torvalds committed
870
		(*tty_loc)->count++;
Linus Torvalds's avatar
Linus Torvalds committed
871 872
		if (tty->ldisc.open) {
			retval = (tty->ldisc.open)(tty);
Linus Torvalds's avatar
Linus Torvalds committed
873 874 875
			if (retval < 0) {
				(*tty_loc)->count--;
				tty = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
876
				goto end_init;
Linus Torvalds's avatar
Linus Torvalds committed
877
			}
Linus Torvalds's avatar
Linus Torvalds committed
878 879
		}
		tty = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
880 881 882 883 884 885 886
	} else {
		if ((*tty_loc)->flags & (1 << TTY_CLOSING)) {
			printk("Attempt to open closing tty %s.\n",
			       tty_name(*tty_loc));
			printk("Ack!!!!  This should never happen!!\n");
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
887
		(*tty_loc)->count++;
Linus Torvalds's avatar
Linus Torvalds committed
888
	}
Linus Torvalds's avatar
Linus Torvalds committed
889 890 891
	if (driver->type == TTY_DRIVER_TYPE_PTY) {
		if (!*o_tp_loc) {
			*o_tp_loc = o_tp;
892 893
			o_tp = NULL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
894 895
		if (!*o_ltp_loc) {
			*o_ltp_loc = o_ltp;
896 897
			o_ltp = NULL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
898 899 900 901 902 903 904
		if (!*o_tty_loc) {
			o_tty->termios = *o_tp_loc;
			o_tty->termios_locked = *o_ltp_loc;
			*o_tty_loc = o_tty;
			(*driver->other->refcount)++;
			if (o_tty->ldisc.open) {
				retval = (o_tty->ldisc.open)(o_tty);
Linus Torvalds's avatar
Linus Torvalds committed
905 906 907
				if (retval < 0) {
					(*tty_loc)->count--;
					o_tty = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
908
					goto end_init;
Linus Torvalds's avatar
Linus Torvalds committed
909
				}
Linus Torvalds's avatar
Linus Torvalds committed
910
			}
911 912
			o_tty = NULL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
913 914
		(*tty_loc)->link = *o_tty_loc;
		(*o_tty_loc)->link = *tty_loc;
Linus Torvalds's avatar
Linus Torvalds committed
915 916
		if (driver->subtype == PTY_TYPE_MASTER)
			(*o_tty_loc)->count++;
917
	}
Linus Torvalds's avatar
Linus Torvalds committed
918
	(*tty_loc)->driver = *driver;
Linus Torvalds's avatar
Linus Torvalds committed
919
	*ret_tty = *tty_loc;
920 921 922 923 924
	retval = 0;
end_init:
	if (tty)
		free_page((unsigned long) tty);
	if (o_tty)
925
		free_page((unsigned long) o_tty);
926 927 928 929
	if (tp)
		kfree_s(tp, sizeof(struct termios));
	if (o_tp)
		kfree_s(o_tp, sizeof(struct termios));
930 931 932 933
	if (ltp)
		kfree_s(ltp, sizeof(struct termios));
	if (o_ltp)
		kfree_s(o_ltp, sizeof(struct termios));
934 935 936 937 938 939 940 941
	return retval;
}

/*
 * Even releasing the tty structures is a tricky business.. We have
 * to be very careful that the structures are all released at the
 * same time, as interrupts might otherwise get the wrong pointers.
 */
Linus Torvalds's avatar
Linus Torvalds committed
942
static void release_dev(struct file * filp)
943 944
{
	struct tty_struct *tty, *o_tty;
Linus Torvalds's avatar
Linus Torvalds committed
945
	struct termios *tp, *o_tp, *ltp, *o_ltp;
946
	struct task_struct **p;
Linus Torvalds's avatar
Linus Torvalds committed
947 948
	int	idx;
	
Linus Torvalds's avatar
Linus Torvalds committed
949
	tty = (struct tty_struct *)filp->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
950 951 952
	if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "release_dev"))
		return;

Linus Torvalds's avatar
Linus Torvalds committed
953 954
	check_tty_count(tty, "release_dev");

Linus Torvalds's avatar
Linus Torvalds committed
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	tty_fasync(filp->f_inode, filp, 0);

	tp = tty->termios;
	ltp = tty->termios_locked;

	idx = MINOR(tty->device) - tty->driver.minor_start;
#ifdef TTY_PARANOIA_CHECK
	if (idx < 0 || idx >= tty->driver.num) {
		printk("release_dev: bad idx when trying to free (%d, %d)\n",
		       MAJOR(tty->device), MINOR(tty->device));
		return;
	}
	if (tty != tty->driver.table[idx]) {
		printk("release_dev: driver.table[%d] not tty for (%d, %d)\n",
		       idx, MAJOR(tty->device), MINOR(tty->device));
970 971
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
972 973 974
	if (tp != tty->driver.termios[idx]) {
		printk("release_dev: driver.termios[%d] not termios for (%d, %d)\n",
		       idx, MAJOR(tty->device), MINOR(tty->device));
975 976
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979 980 981 982 983
	if (ltp != tty->driver.termios_locked[idx]) {
		printk("release_dev: driver.termios_locked[%d] not termios_locked for (%d, %d)\n",
		       idx, MAJOR(tty->device), MINOR(tty->device));
		return;
	}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
984
#ifdef TTY_DEBUG_HANGUP
Linus Torvalds's avatar
Linus Torvalds committed
985 986
	printk("release_dev of %s (tty count=%d)...", tty_name(tty),
	       tty->count);
Linus Torvalds's avatar
Linus Torvalds committed
987
#endif
Linus Torvalds's avatar
Linus Torvalds committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002

	o_tty = tty->link;
	o_tp = (o_tty) ? o_tty->termios : NULL;
	o_ltp = (o_tty) ? o_tty->termios_locked : NULL;

#ifdef TTY_PARANOIA_CHECK
	if (tty->driver.other) {
		if (o_tty != tty->driver.other->table[idx]) {
			printk("release_dev: other->table[%d] not o_tty for (%d, %d)\n",
			       idx, MAJOR(tty->device), MINOR(tty->device));
			return;
		}
		if (o_tp != tty->driver.other->termios[idx]) {
			printk("release_dev: other->termios[%d] not o_termios for (%d, %d)\n",
			       idx, MAJOR(tty->device), MINOR(tty->device));
1003 1004
			return;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1005 1006 1007
		if (o_ltp != tty->driver.other->termios_locked[idx]) {
			printk("release_dev: other->termios_locked[%d] not o_termios_locked for (%d, %d)\n",
			       idx, MAJOR(tty->device), MINOR(tty->device));
1008 1009
			return;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1010 1011

		if (o_tty->link != tty) {
1012 1013 1014 1015
			printk("release_dev: bad pty pointers\n");
			return;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1016 1017 1018 1019 1020 1021
#endif
	
	if (tty->driver.close)
		tty->driver.close(tty, filp);
	if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
	    tty->driver.subtype == PTY_TYPE_MASTER) {
1022
		if (--tty->link->count < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024
			printk("release_dev: bad pty slave count (%d) for %s\n",
			       tty->count, tty_name(tty));
1025 1026 1027 1028
			tty->link->count = 0;
		}
	}
	if (--tty->count < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1029 1030
		printk("release_dev: bad tty->count (%d) for %s\n",
		       tty->count, tty_name(tty));
1031 1032 1033 1034
		tty->count = 0;
	}
	if (tty->count)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1035

Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038
	/*
	 * We're committed; at this point, we must not block!
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1039 1040 1041 1042 1043
	if (o_tty) {
		if (o_tty->count)
			return;
		tty->driver.other->table[idx] = NULL;
		tty->driver.other->termios[idx] = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1044
		kfree_s(o_tp, sizeof(struct termios));
1045
	}
Linus Torvalds's avatar
Linus Torvalds committed
1046 1047 1048 1049
	
#ifdef TTY_DEBUG_HANGUP
	printk("freeing tty structure...");
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1050
	tty->flags |= (1 << TTY_CLOSING);
1051

Linus Torvalds's avatar
Linus Torvalds committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	/*
	 * Make sure there aren't any processes that still think this
	 * tty is their controlling tty.
	 */
	for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
		if (*p == 0)
			continue;
		if ((*p)->tty == tty)
			(*p)->tty = NULL;
		if (o_tty && (*p)->tty == o_tty)
			(*p)->tty = NULL;
	}

Linus Torvalds's avatar
Linus Torvalds committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	/*
	 * Shutdown the current line discipline, and reset it to
	 * N_TTY.
	 */
	if (tty->ldisc.close)
		(tty->ldisc.close)(tty);
	tty->ldisc = ldiscs[N_TTY];
	tty->termios->c_line = N_TTY;
	if (o_tty) {
		if (o_tty->ldisc.close)
			(o_tty->ldisc.close)(o_tty);
		o_tty->ldisc = ldiscs[N_TTY];
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
1079 1080 1081
	tty->driver.table[idx] = NULL;
	if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
		tty->driver.termios[idx] = NULL;
1082 1083
		kfree_s(tp, sizeof(struct termios));
	}
1084 1085
	if (tty == redirect || o_tty == redirect)
		redirect = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
	/*
	 * Make sure that the tty's task queue isn't activated.  If it
	 * is, take it out of the linked list.
	 */
	cli();
	if (tty->flip.tqueue.sync) {
		struct tq_struct *tq, *prev;

		for (tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
			if (tq == &tty->flip.tqueue) {
				if (prev)
					prev->next = tq->next;
				else
					tq_timer = tq->next;
				break;
			}
		}
	}
	sti();
	tty->magic = 0;
	(*tty->driver.refcount)--;
1107
	free_page((unsigned long) tty);
Linus Torvalds's avatar
Linus Torvalds committed
1108
	filp->private_data = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1109 1110 1111
	if (o_tty) {
		o_tty->magic = 0;
		(*o_tty->driver.refcount)--;
1112
		free_page((unsigned long) o_tty);
Linus Torvalds's avatar
Linus Torvalds committed
1113
	}
1114 1115
}

1116 1117 1118 1119 1120 1121 1122
/*
 * tty_open and tty_release keep up the tty count that contains the
 * number of opens done on a tty. We cannot use the inode-count, as
 * different inodes might point to the same tty.
 *
 * Open-counting is needed for pty masters, as well as for keeping
 * track of serial lines: DTR is dropped when the last close happens.
1123
 * (This is not done solely through tty->count, now.  - Ted 1/27/92)
1124 1125 1126
 *
 * The termios state of a pty is reset on first open so that
 * settings don't persist across reuse.
1127 1128
 */
static int tty_open(struct inode * inode, struct file * filp)
1129
{
1130
	struct tty_struct *tty;
Linus Torvalds's avatar
Linus Torvalds committed
1131
	int minor;
1132
	int noctty, retval;
Linus Torvalds's avatar
Linus Torvalds committed
1133
	dev_t device;
1134

Linus Torvalds's avatar
Linus Torvalds committed
1135
retry_open:
1136
	noctty = filp->f_flags & O_NOCTTY;
Linus Torvalds's avatar
Linus Torvalds committed
1137 1138 1139 1140 1141
	device = inode->i_rdev;
	if (device == TTY_DEV) {
		if (!current->tty)
			return -ENXIO;
		device = current->tty->device;
Linus Torvalds's avatar
Linus Torvalds committed
1142
		/* noctty = 1; */
1143
	}
Linus Torvalds's avatar
Linus Torvalds committed
1144 1145
	if (device == CONSOLE_DEV) {
		device = MKDEV(TTY_MAJOR, fg_console+1);
1146
		noctty = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1147 1148 1149 1150
	}
	minor = MINOR(device);
	
	retval = init_dev(device, &tty);
1151 1152
	if (retval)
		return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1153 1154
	filp->private_data = tty;
	check_tty_count(tty, "tty_open");
Linus Torvalds's avatar
Linus Torvalds committed
1155 1156 1157
	if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
	    tty->driver.subtype == PTY_TYPE_MASTER)
		noctty = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1158
#ifdef TTY_DEBUG_HANGUP
Linus Torvalds's avatar
Linus Torvalds committed
1159
	printk("opening %s...", tty_name(tty));
Linus Torvalds's avatar
Linus Torvalds committed
1160
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1161
	if (tty->driver.open)
Linus Torvalds's avatar
Linus Torvalds committed
1162 1163
		retval = tty->driver.open(tty, filp);
	else
1164
		retval = -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
1165

Linus Torvalds's avatar
Linus Torvalds committed
1166 1167 1168
	if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
		retval = -EBUSY;

1169
	if (retval) {
Linus Torvalds's avatar
Linus Torvalds committed
1170
#ifdef TTY_DEBUG_HANGUP
Linus Torvalds's avatar
Linus Torvalds committed
1171
		printk("error %d in opening %s...", retval, tty_name(tty));
Linus Torvalds's avatar
Linus Torvalds committed
1172 1173
#endif

Linus Torvalds's avatar
Linus Torvalds committed
1174
		release_dev(filp);
Linus Torvalds's avatar
Linus Torvalds committed
1175 1176 1177 1178 1179
		if (retval != -ERESTARTSYS)
			return retval;
		if (current->signal & ~current->blocked)
			return retval;
		schedule();
Linus Torvalds's avatar
Linus Torvalds committed
1180 1181 1182 1183
		/*
		 * Need to reset f_op in case a hangup happened.
		 */
		filp->f_op = &tty_fops;
Linus Torvalds's avatar
Linus Torvalds committed
1184
		goto retry_open;
1185
	}
1186
	if (!noctty &&
1187
	    current->leader &&
Linus Torvalds's avatar
Linus Torvalds committed
1188 1189 1190
	    !current->tty &&
	    tty->session == 0) {
		current->tty = tty;
Linus Torvalds's avatar
Linus Torvalds committed
1191
		current->tty_old_pgrp = 0;
1192 1193 1194
		tty->session = current->session;
		tty->pgrp = current->pgrp;
	}
1195
	return 0;
1196 1197
}

1198 1199 1200 1201 1202 1203
/*
 * Note that releasing a pty master also releases the child, so
 * we have to make the redirection checks after that and on both
 * sides of a pty.
 */
static void tty_release(struct inode * inode, struct file * filp)
1204
{
Linus Torvalds's avatar
Linus Torvalds committed
1205
	release_dev(filp);
1206
}
Linus Torvalds's avatar
Linus Torvalds committed
1207

1208
static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
Linus Torvalds's avatar
Linus Torvalds committed
1209
{
1210 1211
	struct tty_struct * tty;

Linus Torvalds's avatar
Linus Torvalds committed
1212
	tty = (struct tty_struct *)filp->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1213
	if (tty_paranoia_check(tty, inode->i_rdev, "tty_select"))
1214
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1215 1216 1217

	if (tty->ldisc.select)
		return (tty->ldisc.select)(tty, inode, filp, sel_type, wait);
Linus Torvalds's avatar
Linus Torvalds committed
1218 1219 1220
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1221
static int tty_fasync(struct inode * inode, struct file * filp, int on)
Linus Torvalds's avatar
Linus Torvalds committed
1222
{
Linus Torvalds's avatar
Linus Torvalds committed
1223 1224 1225
	struct tty_struct * tty;
	struct fasync_struct *fa, *prev;

Linus Torvalds's avatar
Linus Torvalds committed
1226
	tty = (struct tty_struct *)filp->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
	if (tty_paranoia_check(tty, inode->i_rdev, "tty_fasync"))
		return 0;

	for (fa = tty->fasync, prev = 0; fa; prev= fa, fa = fa->fa_next) {
		if (fa->fa_file == filp)
			break;
	}

	if (on) {
		if (fa)
1237
			return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1238
		fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1239 1240 1241 1242 1243 1244 1245 1246
		if (!fa)
			return -ENOMEM;
		fa->magic = FASYNC_MAGIC;
		fa->fa_file = filp;
		fa->fa_next = tty->fasync;
		tty->fasync = fa;
		if (!tty->read_wait)
			tty->minimum_to_wake = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1247 1248 1249 1250 1251 1252
		if (filp->f_owner == 0) {
			if (tty->pgrp)
				filp->f_owner = -tty->pgrp;
			else
				filp->f_owner = current->pid;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1253 1254
	} else {
		if (!fa)
1255
			return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1256 1257 1258 1259 1260 1261 1262
		if (prev)
			prev->fa_next = fa->fa_next;
		else
			tty->fasync = fa->fa_next;
		kfree_s(fa, sizeof(struct fasync_struct));
		if (!tty->fasync && !tty->read_wait)
			tty->minimum_to_wake = N_TTY_BUF_SIZE;
1263
	}
Linus Torvalds's avatar
Linus Torvalds committed
1264 1265 1266
	return 0;	
}

Linus Torvalds's avatar
Linus Torvalds committed
1267
#if 0
Linus Torvalds's avatar
Linus Torvalds committed
1268 1269 1270
/*
 * XXX does anyone use this anymore?!?
 */
Linus Torvalds's avatar
Linus Torvalds committed
1271
static int do_get_ps_info(unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
{
	struct tstruct {
		int flag;
		int present[NR_TASKS];
		struct task_struct tasks[NR_TASKS];
	};
	struct tstruct *ts = (struct tstruct *)arg;
	struct task_struct **p;
	char *c, *d;
	int i, n = 0;
	
	i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct tstruct));
	if (i)
		return i;
	for (p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
		if (*p)
		{
			c = (char *)(*p);
			d = (char *)(ts->tasks+n);
			for (i=0 ; i<sizeof(struct task_struct) ; i++)
				put_fs_byte(*c++, d++);
			put_fs_long(1, (unsigned long *)(ts->present+n));
		}
		else	
			put_fs_long(0, (unsigned long *)(ts->present+n));
	return(0);			
}
Linus Torvalds's avatar
Linus Torvalds committed
1299
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311

static int tty_ioctl(struct inode * inode, struct file * file,
		     unsigned int cmd, unsigned long arg)
{
	int	retval;
	struct tty_struct * tty;
	struct tty_struct * real_tty;
	struct winsize tmp_ws;
	pid_t pgrp;
	unsigned char	ch;
	char	mbz = 0;
	
Linus Torvalds's avatar
Linus Torvalds committed
1312
	tty = (struct tty_struct *)file->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
	if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
		return -EINVAL;

	if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
	    tty->driver.subtype == PTY_TYPE_MASTER)
		real_tty = tty->link;
	else
		real_tty = tty;

	switch (cmd) {
		case TIOCSTI:
			if ((current->tty != tty) && !suser())
				return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
1326 1327 1328
			retval = verify_area(VERIFY_READ, (void *) arg, 1);
			if (retval)
				return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
			ch = get_fs_byte((char *) arg);
			tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
			return 0;
		case TIOCGWINSZ:
			retval = verify_area(VERIFY_WRITE, (void *) arg,
					     sizeof (struct winsize));
			if (retval)
				return retval;
			memcpy_tofs((struct winsize *) arg, &tty->winsize,
				    sizeof (struct winsize));
			return 0;
		case TIOCSWINSZ:
Linus Torvalds's avatar
Linus Torvalds committed
1341 1342 1343 1344
			retval = verify_area(VERIFY_READ, (void *) arg,
					     sizeof (struct winsize));
			if (retval)
				return retval;			
Linus Torvalds's avatar
Linus Torvalds committed
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
			memcpy_fromfs(&tmp_ws, (struct winsize *) arg,
				      sizeof (struct winsize));
			if (memcmp(&tmp_ws, &tty->winsize,
				   sizeof(struct winsize))) {
				if (tty->pgrp > 0)
					kill_pg(tty->pgrp, SIGWINCH, 1);
				if ((real_tty->pgrp != tty->pgrp) &&
				    (real_tty->pgrp > 0))
					kill_pg(real_tty->pgrp, SIGWINCH, 1);
			}
			tty->winsize = tmp_ws;
			real_tty->winsize = tmp_ws;
			return 0;
		case TIOCCONS:
			if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
				if (!suser())
					return -EPERM;
				redirect = NULL;
				return 0;
			}
			if (redirect)
				return -EBUSY;
			redirect = real_tty;
			return 0;
		case FIONBIO:
Linus Torvalds's avatar
Linus Torvalds committed
1370 1371 1372
			retval = verify_area(VERIFY_READ, (void *) arg, sizeof(long));
			if (retval)
				return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
			arg = get_fs_long((unsigned long *) arg);
			if (arg)
				file->f_flags |= O_NONBLOCK;
			else
				file->f_flags &= ~O_NONBLOCK;
			return 0;
		case TIOCEXCL:
			set_bit(TTY_EXCLUSIVE, &tty->flags);
			return 0;
		case TIOCNXCL:
			clear_bit(TTY_EXCLUSIVE, &tty->flags);
			return 0;
		case TIOCNOTTY:
			if (current->tty != tty)
				return -ENOTTY;
			if (current->leader)
				disassociate_ctty(0);
			current->tty = NULL;
			return 0;
		case TIOCSCTTY:
			if (current->leader &&
			    (current->session == tty->session))
				return 0;
			/*
			 * The process must be a session leader and
			 * not have a controlling tty already.
			 */
			if (!current->leader || current->tty)
				return -EPERM;
			if (tty->session > 0) {
				/*
				 * This tty is already the controlling
				 * tty for another session group!
				 */
				if ((arg == 1) && suser()) {
					/*
					 * Steal it away
					 */
					struct task_struct *p;

					for_each_task(p)
						if (p->tty == tty)
							p->tty = NULL;
				} else
					return -EPERM;
			}
			current->tty = tty;
Linus Torvalds's avatar
Linus Torvalds committed
1420
			current->tty_old_pgrp = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
			tty->session = current->session;
			tty->pgrp = current->pgrp;
			return 0;
		case TIOCGPGRP:
			/*
			 * (tty == real_tty) is a cheap way of
			 * testing if the tty is NOT a master pty.
			 */
			if (tty == real_tty && current->tty != real_tty)
				return -ENOTTY;
			retval = verify_area(VERIFY_WRITE, (void *) arg,
					     sizeof (pid_t));
			if (retval)
				return retval;
			put_fs_long(real_tty->pgrp, (pid_t *) arg);
			return 0;
		case TIOCSPGRP:
			retval = tty_check_change(real_tty);
			if (retval)
				return retval;
			if (!current->tty ||
			    (current->tty != real_tty) ||
			    (real_tty->session != current->session))
				return -ENOTTY;
			pgrp = get_fs_long((pid_t *) arg);
			if (pgrp < 0)
				return -EINVAL;
			if (session_of_pgrp(pgrp) != current->session)
				return -EPERM;
			real_tty->pgrp = pgrp;
			return 0;
		case TIOCGETD:
			retval = verify_area(VERIFY_WRITE, (void *) arg,
					     sizeof (unsigned long));
			if (retval)
				return retval;
			put_fs_long(tty->ldisc.num, (unsigned long *) arg);
			return 0;
		case TIOCSETD:
			retval = tty_check_change(tty);
			if (retval)
				return retval;
			arg = get_fs_long((unsigned long *) arg);
			return tty_set_ldisc(tty, arg);
		case TIOCLINUX:
Linus Torvalds's avatar
Linus Torvalds committed
1466 1467 1468
			if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
				return -EINVAL;
			if (current->tty != tty && !suser())
Linus Torvalds's avatar
Linus Torvalds committed
1469
				return -EPERM;
Linus Torvalds's avatar
Linus Torvalds committed
1470 1471 1472
			retval = verify_area(VERIFY_READ, (void *) arg, 1);
			if (retval)
				return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1473
			switch (retval = get_fs_byte((char *)arg))
Linus Torvalds's avatar
Linus Torvalds committed
1474
			{
Linus Torvalds's avatar
Linus Torvalds committed
1475 1476 1477 1478 1479 1480
				case 0:
				case 8:
				case 9:
					printk("TIOCLINUX (0/8/9) ioctl is gone - use /dev/vcs\n");
					return -EINVAL;
#if 0
Linus Torvalds's avatar
Linus Torvalds committed
1481 1482 1483
				case 1:
					printk("Deprecated TIOCLINUX (1) ioctl\n");
					return do_get_ps_info(arg);
Linus Torvalds's avatar
Linus Torvalds committed
1484
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1485
				case 2:
Linus Torvalds's avatar
Linus Torvalds committed
1486
					return set_selection(arg, tty);
Linus Torvalds's avatar
Linus Torvalds committed
1487 1488 1489
				case 3:
					return paste_selection(tty);
				case 4:
Linus Torvalds's avatar
Linus Torvalds committed
1490
					do_unblank_screen();
Linus Torvalds's avatar
Linus Torvalds committed
1491
					return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1492 1493 1494
				case 5:
					return sel_loadlut(arg);
				case 6:
Linus Torvalds's avatar
Linus Torvalds committed
1495 1496 1497 1498 1499 1500
			/*
			 * Make it possible to react to Shift+Mousebutton.
			 * Note that 'shift_state' is an undocumented
			 * kernel-internal variable; programs not closely
			 * related to the kernel should not use this.
			 */
Linus Torvalds's avatar
Linus Torvalds committed
1501 1502
					put_fs_byte(shift_state,arg);
					return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1503
				case 7:
Linus Torvalds's avatar
Linus Torvalds committed
1504 1505
					put_fs_byte(mouse_reporting(),arg);
					return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1506 1507 1508
				case 10:
					set_vesa_blanking(arg);
					return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1509 1510 1511
				default: 
					return -EINVAL;
			}
Linus Torvalds's avatar
Linus Torvalds committed
1512

Linus Torvalds's avatar
Linus Torvalds committed
1513 1514 1515 1516 1517 1518 1519 1520
		case TIOCTTYGSTRUCT:
			retval = verify_area(VERIFY_WRITE, (void *) arg,
						sizeof(struct tty_struct));
			if (retval)
				return retval;
			memcpy_tofs((struct tty_struct *) arg,
				    tty, sizeof(struct tty_struct));
			return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
		default:
			if (tty->driver.ioctl) {
				retval = (tty->driver.ioctl)(tty, file,
							     cmd, arg);
				if (retval != -ENOIOCTLCMD)
					return retval;
			}
			if (tty->ldisc.ioctl) {
				retval = (tty->ldisc.ioctl)(tty, file,
							    cmd, arg);
				if (retval != -ENOIOCTLCMD)
					return retval;
			}
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1536 1537
}

Linus Torvalds's avatar
Linus Torvalds committed
1538

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
/*
 * This implements the "Secure Attention Key" ---  the idea is to
 * prevent trojan horses by killing all processes associated with this
 * tty when the user hits the "Secure Attention Key".  Required for
 * super-paranoid applications --- see the Orange Book for more details.
 * 
 * This code could be nicer; ideally it should send a HUP, wait a few
 * seconds, then send a INT, and then a KILL signal.  But you then
 * have to coordinate with the init process, since all processes associated
 * with the current tty must be dead before the new getty is allowed
 * to spawn.
 */
void do_SAK( struct tty_struct *tty)
{
Linus Torvalds's avatar
Linus Torvalds committed
1553 1554 1555
#ifdef TTY_SOFT_SAK
	tty_hangup(tty);
#else
1556
	struct task_struct **p;
Linus Torvalds's avatar
Linus Torvalds committed
1557
	int session;
1558 1559 1560
	int		i;
	struct file	*filp;
	
Linus Torvalds's avatar
Linus Torvalds committed
1561 1562
	if (!tty)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1563
	session  = tty->session;
Linus Torvalds's avatar
Linus Torvalds committed
1564 1565 1566 1567
	if (tty->ldisc.flush_buffer)
		tty->ldisc.flush_buffer(tty);
	if (tty->driver.flush_buffer)
		tty->driver.flush_buffer(tty);
1568 1569 1570
 	for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
		if (!(*p))
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
1571
		if (((*p)->tty == tty) ||
1572 1573 1574
		    ((session > 0) && ((*p)->session == session)))
			send_sig(SIGKILL, *p, 1);
		else {
1575
			for (i=0; i < NR_OPEN; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1576
				filp = (*p)->files->fd[i];
1577
				if (filp && (filp->f_op == &tty_fops) &&
Linus Torvalds's avatar
Linus Torvalds committed
1578
				    (filp->private_data == tty)) {
1579 1580 1581 1582 1583 1584
					send_sig(SIGKILL, *p, 1);
					break;
				}
			}
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1585
#endif
1586 1587
}

1588
/*
Linus Torvalds's avatar
Linus Torvalds committed
1589
 * This routine is called out of the software interrupt to flush data
Linus Torvalds's avatar
Linus Torvalds committed
1590
 * from the flip buffer to the line discipline.
1591
 */
Linus Torvalds's avatar
Linus Torvalds committed
1592
static void flush_to_ldisc(void *private_)
1593
{
Linus Torvalds's avatar
Linus Torvalds committed
1594
	struct tty_struct *tty = (struct tty_struct *) private_;
Linus Torvalds's avatar
Linus Torvalds committed
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
	unsigned char	*cp;
	char		*fp;
	int		count;

	if (tty->flip.buf_num) {
		cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
		fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
		tty->flip.buf_num = 0;

		cli();
		tty->flip.char_buf_ptr = tty->flip.char_buf;
		tty->flip.flag_buf_ptr = tty->flip.flag_buf;
	} else {
		cp = tty->flip.char_buf;
		fp = tty->flip.flag_buf;
		tty->flip.buf_num = 1;
1611

Linus Torvalds's avatar
Linus Torvalds committed
1612 1613 1614
		cli();
		tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
		tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1615
	}
Linus Torvalds's avatar
Linus Torvalds committed
1616 1617 1618
	count = tty->flip.count;
	tty->flip.count = 0;
	sti();
1619
	
Linus Torvalds's avatar
Linus Torvalds committed
1620 1621 1622 1623 1624
#if 0
	if (count > tty->max_flip_cnt)
		tty->max_flip_cnt = count;
#endif
	tty->ldisc.receive_buf(tty, cp, fp, count);
1625 1626
}

1627
/*
Linus Torvalds's avatar
Linus Torvalds committed
1628
 * This subroutine initializes a tty structure.
1629
 */
Linus Torvalds's avatar
Linus Torvalds committed
1630
static void initialize_tty_struct(struct tty_struct *tty)
1631 1632
{
	memset(tty, 0, sizeof(struct tty_struct));
Linus Torvalds's avatar
Linus Torvalds committed
1633 1634
	tty->magic = TTY_MAGIC;
	tty->ldisc = ldiscs[N_TTY];
1635
	tty->pgrp = -1;
Linus Torvalds's avatar
Linus Torvalds committed
1636 1637 1638 1639
	tty->flip.char_buf_ptr = tty->flip.char_buf;
	tty->flip.flag_buf_ptr = tty->flip.flag_buf;
	tty->flip.tqueue.routine = flush_to_ldisc;
	tty->flip.tqueue.data = tty;
1640 1641
}

Linus Torvalds's avatar
Linus Torvalds committed
1642 1643 1644 1645
/*
 * The default put_char routine if the driver did not define one.
 */
void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
1646
{
Linus Torvalds's avatar
Linus Torvalds committed
1647
	tty->driver.write(tty, 0, &ch, 1);
1648
}
1649

Linus Torvalds's avatar
Linus Torvalds committed
1650 1651 1652 1653 1654
/*
 * Called by a tty driver to register itself.
 */
int tty_register_driver(struct tty_driver *driver)
{
Linus Torvalds's avatar
Linus Torvalds committed
1655 1656
	int error;

Linus Torvalds's avatar
Linus Torvalds committed
1657 1658
	if (driver->flags & TTY_DRIVER_INSTALLED)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1659 1660

	error = register_chrdev(driver->major, driver->name, &tty_fops);
Linus Torvalds's avatar
Linus Torvalds committed
1661
	if (error < 0)
Linus Torvalds's avatar
Linus Torvalds committed
1662
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
1663 1664
	else if(driver->major == 0)
		driver->major = error;
1665

Linus Torvalds's avatar
Linus Torvalds committed
1666 1667
	if (!driver->put_char)
		driver->put_char = tty_default_put_char;
1668
	
Linus Torvalds's avatar
Linus Torvalds committed
1669 1670
	driver->prev = 0;
	driver->next = tty_drivers;
Linus Torvalds's avatar
Linus Torvalds committed
1671
	if (tty_drivers) tty_drivers->prev = driver;
Linus Torvalds's avatar
Linus Torvalds committed
1672
	tty_drivers = driver;
Linus Torvalds's avatar
Linus Torvalds committed
1673
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
1674 1675
}

Linus Torvalds's avatar
Linus Torvalds committed
1676 1677 1678 1679 1680 1681 1682 1683
/*
 * Called by a tty driver to unregister itself.
 */
int tty_unregister_driver(struct tty_driver *driver)
{
	int	retval;
	struct tty_driver *p;
	int	found = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1684
	char *othername = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1685
	
Linus Torvalds's avatar
Linus Torvalds committed
1686
	if (*driver->refcount)
Linus Torvalds's avatar
Linus Torvalds committed
1687 1688 1689 1690 1691 1692
		return -EBUSY;

	for (p = tty_drivers; p; p = p->next) {
		if (p == driver)
			found++;
		else if (p->major == driver->major)
Linus Torvalds's avatar
Linus Torvalds committed
1693
			othername = p->name;
Linus Torvalds's avatar
Linus Torvalds committed
1694 1695
	}

Linus Torvalds's avatar
Linus Torvalds committed
1696
	if (othername == NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
1697 1698 1699
		retval = unregister_chrdev(driver->major, driver->name);
		if (retval)
			return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1700 1701
	} else
		register_chrdev(driver->major, othername, &tty_fops);
Linus Torvalds's avatar
Linus Torvalds committed
1702 1703 1704 1705 1706 1707 1708

	if (driver->prev)
		driver->prev->next = driver->next;
	else
		tty_drivers = driver->next;
	
	if (driver->next)
Linus Torvalds's avatar
Linus Torvalds committed
1709
		driver->next->prev = driver->prev;
Linus Torvalds's avatar
Linus Torvalds committed
1710 1711 1712 1713 1714

	return 0;
}


Linus Torvalds's avatar
Linus Torvalds committed
1715 1716 1717
/*
 * Initialize the console device. This is called *early*, so
 * we can't necessarily depend on lots of kernel help here.
Linus Torvalds's avatar
Linus Torvalds committed
1718
 * Just do some early initializations, and do the complex setup
Linus Torvalds's avatar
Linus Torvalds committed
1719 1720 1721
 * later.
 */
long console_init(long kmem_start, long kmem_end)
Linus Torvalds's avatar
Linus Torvalds committed
1722
{
1723 1724 1725 1726
	/* Setup the default TTY line discipline. */
	memset(ldiscs, 0, sizeof(ldiscs));
	(void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);

Linus Torvalds's avatar
Linus Torvalds committed
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
	/*
	 * Set up the standard termios.  Individual tty drivers may 
	 * deviate from this; this is used as a template.
	 */
	memset(&tty_std_termios, 0, sizeof(struct termios));
	memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
	tty_std_termios.c_iflag = ICRNL | IXON;
	tty_std_termios.c_oflag = OPOST | ONLCR;
	tty_std_termios.c_cflag = B38400 | CS8 | CREAD;
	tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
		ECHOCTL | ECHOKE | IEXTEN;
Linus Torvalds's avatar
Linus Torvalds committed
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755

	/*
	 * set up the console device so that later boot sequences can 
	 * inform about problems etc..
	 */
	return con_init(kmem_start);
}

/*
 * Ok, now we can initialize the rest of the tty devices and can count
 * on memory allocations, interrupts etc..
 */
long tty_init(long kmem_start)
{
	if (sizeof(struct tty_struct) > PAGE_SIZE)
		panic("size of tty structure > PAGE_SIZE!");
	if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
		panic("unable to get major %d for tty device", TTY_MAJOR);
Linus Torvalds's avatar
Linus Torvalds committed
1756
	if (register_chrdev(TTYAUX_MAJOR,"cua",&tty_fops))
Linus Torvalds's avatar
Linus Torvalds committed
1757 1758
		panic("unable to get major %d for tty device", TTYAUX_MAJOR);

Linus Torvalds's avatar
Linus Torvalds committed
1759
	kmem_start = kbd_init(kmem_start);
1760
	kmem_start = rs_init(kmem_start);
Linus Torvalds's avatar
Linus Torvalds committed
1761 1762 1763
#ifdef CONFIG_SCC
	kmem_start = scc_init(kmem_start);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1764 1765 1766
#ifdef CONFIG_CYCLADES
	kmem_start = cy_init(kmem_start);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1767
	kmem_start = pty_init(kmem_start);
Linus Torvalds's avatar
Linus Torvalds committed
1768
	kmem_start = vcs_init(kmem_start);
1769
	return kmem_start;
Linus Torvalds's avatar
Linus Torvalds committed
1770
}