tty_io.c 80 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  linux/drivers/char/tty_io.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

/*
 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
 * or rs-channels. It also implements echoing, cooked mode etc.
 *
 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
 *
 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
 * tty_struct and tty_queue structures.  Previously there was an 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
22
 * makes for cleaner and more compact code.  -TYT, 9/17/92
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * 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).
 *
 * NOTE: pay no attention to the line discipline code (yet); its
 * interface is still subject to change in this version...
 * -- TYT, 1/31/92
 *
 * 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.
 *
 * Rewrote canonical mode and added more termios flags.
 * 	-- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
 *
 * Reorganized FASYNC support so mouse code can share it.
 *	-- ctm@ardi.com, 9Sep95
 *
 * New TIOCLINUX variants added.
 *	-- mj@k332.feld.cvut.cz, 19-Nov-95
44
 *
Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * Restrict vt switching via ioctl()
 *      -- grif@cs.ucr.edu, 5-Dec-95
 *
 * Move console and virtual terminal code to more appropriate files,
 * implement CONFIG_VT and generalize console device interface.
 *	-- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
 *
 * Rewrote init_dev and release_dev to eliminate races.
 *	-- Bill Hawes <whawes@star.net>, June 97
 *
 * Added devfs support.
 *      -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
 *
 * Added support for a Unix98-style ptmx device.
 *      -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
 *
 * Reduced memory usage for older ARM systems
 *      -- Russell King <rmk@arm.linux.org.uk>
 *
 * Move do_SAK() into process context.  Less stack use in devfs functions.
65 66
 * alloc_tty_struct() always uses kmalloc()
 *			 -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80
 */

#include <linux/types.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/devpts_fs.h>
#include <linux/file.h>
Al Viro's avatar
Al Viro committed
81
#include <linux/fdtable.h>
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#include <linux/console.h>
#include <linux/timer.h>
#include <linux/ctype.h>
#include <linux/kd.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/smp_lock.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/bitops.h>
97
#include <linux/delay.h>
Alan Cox's avatar
Alan Cox committed
98
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
99

Alan Cox's avatar
Alan Cox committed
100
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104 105 106 107
#include <asm/system.h>

#include <linux/kbd_kern.h>
#include <linux/vt_kern.h>
#include <linux/selection.h>

#include <linux/kmod.h>
108
#include <linux/nsproxy.h>
Linus Torvalds's avatar
Linus Torvalds committed
109 110 111 112 113 114

#undef TTY_DEBUG_HANGUP

#define TTY_PARANOIA_CHECK 1
#define CHECK_TTY_COUNT 1

115
struct ktermios tty_std_termios = {	/* for the benefit of tty drivers  */
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119 120
	.c_iflag = ICRNL | IXON,
	.c_oflag = OPOST | ONLCR,
	.c_cflag = B38400 | CS8 | CREAD | HUPCL,
	.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
		   ECHOCTL | ECHOKE | IEXTEN,
121 122 123
	.c_cc = INIT_C_CC,
	.c_ispeed = 38400,
	.c_ospeed = 38400
Linus Torvalds's avatar
Linus Torvalds committed
124 125 126 127 128 129 130
};

EXPORT_SYMBOL(tty_std_termios);

/* This list gets poked at by procfs and various bits of boot up code. This
   could do with some rationalisation such as pulling the tty proc function
   into this file */
131

Linus Torvalds's avatar
Linus Torvalds committed
132 133
LIST_HEAD(tty_drivers);			/* linked list of tty drivers */

134
/* Mutex to protect creating and releasing a tty. This is shared with
Linus Torvalds's avatar
Linus Torvalds committed
135
   vt.c for deeply disgusting hack reasons */
Ingo Molnar's avatar
Ingo Molnar committed
136
DEFINE_MUTEX(tty_mutex);
137
EXPORT_SYMBOL(tty_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140 141 142 143 144 145 146 147

#ifdef CONFIG_UNIX98_PTYS
extern struct tty_driver *ptm_driver;	/* Unix98 pty masters; for /dev/ptmx */
static int ptmx_open(struct inode *, struct file *);
#endif

static void initialize_tty_struct(struct tty_struct *tty);

static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
148 149
ssize_t redirected_tty_write(struct file *, const char __user *,
							size_t, loff_t *);
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152
static unsigned int tty_poll(struct file *, poll_table *);
static int tty_open(struct inode *, struct file *);
static int tty_release(struct inode *, struct file *);
Alan Cox's avatar
Alan Cox committed
153
long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghum's avatar
Paul Fulghum committed
154
#ifdef CONFIG_COMPAT
155
static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghum's avatar
Paul Fulghum committed
156 157 158 159
				unsigned long arg);
#else
#define tty_compat_ioctl NULL
#endif
160
static int tty_fasync(int fd, struct file *filp, int on);
161
static void release_tty(struct tty_struct *tty, int idx);
162
static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
163
static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Linus Torvalds's avatar
Linus Torvalds committed
164

165 166 167 168 169 170 171 172
/**
 *	alloc_tty_struct	-	allocate a tty object
 *
 *	Return a new empty tty structure. The data fields have not
 *	been initialized in any way but has been zeroed
 *
 *	Locking: none
 */
Linus Torvalds's avatar
Linus Torvalds committed
173 174 175

static struct tty_struct *alloc_tty_struct(void)
{
176
	return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
177 178
}

179 180 181 182 183 184 185 186 187
/**
 *	free_tty_struct		-	free a disused tty
 *	@tty: tty struct to free
 *
 *	Free the write buffers, tty queue and tty memory itself.
 *
 *	Locking: none. Must be called after tty is definitely unused
 */

Linus Torvalds's avatar
Linus Torvalds committed
188 189 190
static inline void free_tty_struct(struct tty_struct *tty)
{
	kfree(tty->write_buf);
Alan Cox's avatar
Alan Cox committed
191
	tty_buffer_free_all(tty);
Linus Torvalds's avatar
Linus Torvalds committed
192 193 194 195 196
	kfree(tty);
}

#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)

197 198 199 200 201 202 203 204 205 206 207
/**
 *	tty_name	-	return tty naming
 *	@tty: tty structure
 *	@buf: buffer for output
 *
 *	Convert a tty structure into a name. The name reflects the kernel
 *	naming policy and if udev is in use may not reflect user space
 *
 *	Locking: none
 */

Linus Torvalds's avatar
Linus Torvalds committed
208 209 210 211 212 213 214 215 216 217 218
char *tty_name(struct tty_struct *tty, char *buf)
{
	if (!tty) /* Hmm.  NULL pointer.  That's fun. */
		strcpy(buf, "NULL tty");
	else
		strcpy(buf, tty->name);
	return buf;
}

EXPORT_SYMBOL(tty_name);

219
int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
			      const char *routine)
{
#ifdef TTY_PARANOIA_CHECK
	if (!tty) {
		printk(KERN_WARNING
			"null TTY for (%d:%d) in %s\n",
			imajor(inode), iminor(inode), routine);
		return 1;
	}
	if (tty->magic != TTY_MAGIC) {
		printk(KERN_WARNING
			"bad magic number for tty struct (%d:%d) in %s\n",
			imajor(inode), iminor(inode), routine);
		return 1;
	}
#endif
	return 0;
}

static int check_tty_count(struct tty_struct *tty, const char *routine)
{
#ifdef CHECK_TTY_COUNT
	struct list_head *p;
	int count = 0;
244

Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258
	file_list_lock();
	list_for_each(p, &tty->tty_files) {
		count++;
	}
	file_list_unlock();
	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(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
				    "!= #fd's(%d) in %s\n",
		       tty->name, tty->count, count, routine);
		return count;
259
	}
Linus Torvalds's avatar
Linus Torvalds committed
260 261 262 263
#endif
	return 0;
}

264 265 266 267 268 269 270 271 272
/**
 *	get_tty_driver		-	find device of a tty
 *	@dev_t: device identifier
 *	@index: returns the index of the tty
 *
 *	This routine returns a tty driver structure, given a device number
 *	and also passes back the index number.
 *
 *	Locking: caller must hold tty_mutex
Linus Torvalds's avatar
Linus Torvalds committed
273
 */
274

Linus Torvalds's avatar
Linus Torvalds committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288
static struct tty_driver *get_tty_driver(dev_t device, int *index)
{
	struct tty_driver *p;

	list_for_each_entry(p, &tty_drivers, tty_drivers) {
		dev_t base = MKDEV(p->major, p->minor_start);
		if (device < base || device >= base + p->num)
			continue;
		*index = device - base;
		return p;
	}
	return NULL;
}

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
#ifdef CONFIG_CONSOLE_POLL

/**
 *	tty_find_polling_driver	-	find device of a polled tty
 *	@name: name string to match
 *	@line: pointer to resulting tty line nr
 *
 *	This routine returns a tty driver structure, given a name
 *	and the condition that the tty driver is capable of polled
 *	operation.
 */
struct tty_driver *tty_find_polling_driver(char *name, int *line)
{
	struct tty_driver *p, *res = NULL;
	int tty_line = 0;
304
	int len;
305 306
	char *str;

307 308 309 310 311 312 313 314 315
	for (str = name; *str; str++)
		if ((*str >= '0' && *str <= '9') || *str == ',')
			break;
	if (!*str)
		return NULL;

	len = str - name;
	tty_line = simple_strtoul(str, &str, 10);

316 317 318
	mutex_lock(&tty_mutex);
	/* Search through the tty devices to look for a match */
	list_for_each_entry(p, &tty_drivers, tty_drivers) {
319 320
		if (strncmp(name, p->name, len) != 0)
			continue;
321 322 323
		if (*str == ',')
			str++;
		if (*str == '\0')
324
			str = NULL;
325

Alan Cox's avatar
Alan Cox committed
326 327
		if (tty_line >= 0 && tty_line <= p->num && p->ops &&
		    p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
328 329 330 331 332 333 334 335 336 337 338 339
			res = p;
			*line = tty_line;
			break;
		}
	}
	mutex_unlock(&tty_mutex);

	return res;
}
EXPORT_SYMBOL_GPL(tty_find_polling_driver);
#endif

340 341 342 343 344 345 346 347
/**
 *	tty_check_change	-	check for POSIX terminal changes
 *	@tty: tty to check
 *
 *	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)
 *
348
 *	Locking: ctrl_lock
Linus Torvalds's avatar
Linus Torvalds committed
349
 */
350

351
int tty_check_change(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
352
{
Alan Cox's avatar
Alan Cox committed
353 354 355
	unsigned long flags;
	int ret = 0;

Linus Torvalds's avatar
Linus Torvalds committed
356 357
	if (current->signal->tty != tty)
		return 0;
Alan Cox's avatar
Alan Cox committed
358 359 360

	spin_lock_irqsave(&tty->ctrl_lock, flags);

361 362
	if (!tty->pgrp) {
		printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
363
		goto out_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
364
	}
365
	if (task_pgrp(current) == tty->pgrp)
366 367
		goto out_unlock;
	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
368
	if (is_ignored(SIGTTOU))
Alan Cox's avatar
Alan Cox committed
369 370 371 372 373
		goto out;
	if (is_current_pgrp_orphaned()) {
		ret = -EIO;
		goto out;
	}
374 375
	kill_pgrp(task_pgrp(current), SIGTTOU, 1);
	set_thread_flag(TIF_SIGPENDING);
Alan Cox's avatar
Alan Cox committed
376 377
	ret = -ERESTARTSYS;
out:
378 379
	return ret;
out_unlock:
Alan Cox's avatar
Alan Cox committed
380 381
	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
382 383 384 385
}

EXPORT_SYMBOL(tty_check_change);

386
static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds's avatar
Linus Torvalds committed
387 388 389 390 391
				size_t count, loff_t *ppos)
{
	return 0;
}

392
static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds's avatar
Linus Torvalds committed
393 394 395 396 397 398
				 size_t count, loff_t *ppos)
{
	return -EIO;
}

/* No kernel lock held - none needed ;) */
399
static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds's avatar
Linus Torvalds committed
400 401 402 403
{
	return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
}

Alan Cox's avatar
Alan Cox committed
404 405
static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
		unsigned long arg)
406 407 408 409
{
	return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
}

410
static long hung_up_tty_compat_ioctl(struct file *file,
411
				     unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
412 413 414 415
{
	return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
}

416
static const struct file_operations tty_fops = {
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419 420
	.llseek		= no_llseek,
	.read		= tty_read,
	.write		= tty_write,
	.poll		= tty_poll,
Alan Cox's avatar
Alan Cox committed
421
	.unlocked_ioctl	= tty_ioctl,
Paul Fulghum's avatar
Paul Fulghum committed
422
	.compat_ioctl	= tty_compat_ioctl,
Linus Torvalds's avatar
Linus Torvalds committed
423 424 425 426 427 428
	.open		= tty_open,
	.release	= tty_release,
	.fasync		= tty_fasync,
};

#ifdef CONFIG_UNIX98_PTYS
429
static const struct file_operations ptmx_fops = {
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433
	.llseek		= no_llseek,
	.read		= tty_read,
	.write		= tty_write,
	.poll		= tty_poll,
Alan Cox's avatar
Alan Cox committed
434
	.unlocked_ioctl	= tty_ioctl,
Paul Fulghum's avatar
Paul Fulghum committed
435
	.compat_ioctl	= tty_compat_ioctl,
Linus Torvalds's avatar
Linus Torvalds committed
436 437 438 439 440 441
	.open		= ptmx_open,
	.release	= tty_release,
	.fasync		= tty_fasync,
};
#endif

442
static const struct file_operations console_fops = {
Linus Torvalds's avatar
Linus Torvalds committed
443 444 445 446
	.llseek		= no_llseek,
	.read		= tty_read,
	.write		= redirected_tty_write,
	.poll		= tty_poll,
Alan Cox's avatar
Alan Cox committed
447
	.unlocked_ioctl	= tty_ioctl,
Paul Fulghum's avatar
Paul Fulghum committed
448
	.compat_ioctl	= tty_compat_ioctl,
Linus Torvalds's avatar
Linus Torvalds committed
449 450 451 452 453
	.open		= tty_open,
	.release	= tty_release,
	.fasync		= tty_fasync,
};

454
static const struct file_operations hung_up_tty_fops = {
Linus Torvalds's avatar
Linus Torvalds committed
455 456 457 458
	.llseek		= no_llseek,
	.read		= hung_up_tty_read,
	.write		= hung_up_tty_write,
	.poll		= hung_up_tty_poll,
Alan Cox's avatar
Alan Cox committed
459
	.unlocked_ioctl	= hung_up_tty_ioctl,
460
	.compat_ioctl	= hung_up_tty_compat_ioctl,
Linus Torvalds's avatar
Linus Torvalds committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474
	.release	= tty_release,
};

static DEFINE_SPINLOCK(redirect_lock);
static struct file *redirect;

/**
 *	tty_wakeup	-	request more data
 *	@tty: terminal
 *
 *	Internal and external helper for wakeups of tty. This function
 *	informs the line discipline if present that the driver is ready
 *	to receive more output data.
 */
475

Linus Torvalds's avatar
Linus Torvalds committed
476 477 478
void tty_wakeup(struct tty_struct *tty)
{
	struct tty_ldisc *ld;
479

Linus Torvalds's avatar
Linus Torvalds committed
480 481
	if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
		ld = tty_ldisc_ref(tty);
482
		if (ld) {
Alan Cox's avatar
Alan Cox committed
483 484
			if (ld->ops->write_wakeup)
				ld->ops->write_wakeup(tty);
Linus Torvalds's avatar
Linus Torvalds committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
			tty_ldisc_deref(ld);
		}
	}
	wake_up_interruptible(&tty->write_wait);
}

EXPORT_SYMBOL_GPL(tty_wakeup);

/**
 *	tty_ldisc_flush	-	flush line discipline queue
 *	@tty: tty
 *
 *	Flush the line discipline queue (if any) for this tty. If there
 *	is no line discipline active this is a no-op.
 */
500

Linus Torvalds's avatar
Linus Torvalds committed
501 502 503
void tty_ldisc_flush(struct tty_struct *tty)
{
	struct tty_ldisc *ld = tty_ldisc_ref(tty);
504
	if (ld) {
Alan Cox's avatar
Alan Cox committed
505 506
		if (ld->ops->flush_buffer)
			ld->ops->flush_buffer(tty);
Linus Torvalds's avatar
Linus Torvalds committed
507 508
		tty_ldisc_deref(ld);
	}
509
	tty_buffer_flush(tty);
Linus Torvalds's avatar
Linus Torvalds committed
510 511 512
}

EXPORT_SYMBOL_GPL(tty_ldisc_flush);
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

/**
 *	tty_reset_termios	-	reset terminal state
 *	@tty: tty to reset
 *
 *	Restore a terminal to the driver default state
 */

static void tty_reset_termios(struct tty_struct *tty)
{
	mutex_lock(&tty->termios_mutex);
	*tty->termios = tty->driver->init_termios;
	tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
	tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
	mutex_unlock(&tty->termios_mutex);
}
529

530 531
/**
 *	do_tty_hangup		-	actual handler for hangup events
532
 *	@work: tty device
533
 *
Alan Cox's avatar
Alan Cox committed
534
 *	This can be called by the "eventd" kernel thread.  That is process
535 536 537 538 539 540 541 542 543 544
 *	synchronous but doesn't hold any locks, so we need to make sure we
 *	have the appropriate locks for what we're doing.
 *
 *	The hangup event clears any pending redirections onto the hung up
 *	device. It ensures future writes will error and it does the needed
 *	line discipline hangup and signal delivery. The tty object itself
 *	remains intact.
 *
 *	Locking:
 *		BKL
545 546 547 548 549 550
 *		  redirect lock for undoing redirection
 *		  file list lock for manipulating list of ttys
 *		  tty_ldisc_lock from called functions
 *		  termios_mutex resetting termios data
 *		  tasklist_lock to walk task list for hangup event
 *		    ->siglock to protect ->signal/->sighand
Linus Torvalds's avatar
Linus Torvalds committed
551
 */
552
static void do_tty_hangup(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
553
{
554 555
	struct tty_struct *tty =
		container_of(work, struct tty_struct, hangup_work);
556
	struct file *cons_filp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
557 558 559 560
	struct file *filp, *f = NULL;
	struct task_struct *p;
	struct tty_ldisc *ld;
	int    closecount = 0, n;
Alan Cox's avatar
Alan Cox committed
561
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
562 563 564 565 566 567 568 569 570 571 572 573 574

	if (!tty)
		return;

	/* inuse_filps is protected by the single kernel lock */
	lock_kernel();

	spin_lock(&redirect_lock);
	if (redirect && redirect->private_data == tty) {
		f = redirect;
		redirect = NULL;
	}
	spin_unlock(&redirect_lock);
575

Linus Torvalds's avatar
Linus Torvalds committed
576 577 578
	check_tty_count(tty, "do_tty_hangup");
	file_list_lock();
	/* This breaks for file handles being sent over AF_UNIX sockets ? */
579
	list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
Linus Torvalds's avatar
Linus Torvalds committed
580 581 582 583 584 585 586 587 588
		if (filp->f_op->write == redirected_tty_write)
			cons_filp = filp;
		if (filp->f_op->write != tty_write)
			continue;
		closecount++;
		tty_fasync(-1, filp, 0);	/* can't block */
		filp->f_op = &hung_up_tty_fops;
	}
	file_list_unlock();
589 590 591 592 593
	/*
	 * FIXME! What are the locking issues here? This may me overdoing
	 * things... This question is especially important now that we've
	 * removed the irqlock.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
594
	ld = tty_ldisc_ref(tty);
595 596
	if (ld != NULL) {
		/* We may have no line discipline at this point */
Alan Cox's avatar
Alan Cox committed
597 598
		if (ld->ops->flush_buffer)
			ld->ops->flush_buffer(tty);
Alan Cox's avatar
Alan Cox committed
599
		tty_driver_flush_buffer(tty);
Linus Torvalds's avatar
Linus Torvalds committed
600
		if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
Alan Cox's avatar
Alan Cox committed
601 602 603 604
		    ld->ops->write_wakeup)
			ld->ops->write_wakeup(tty);
		if (ld->ops->hangup)
			ld->ops->hangup(tty);
Linus Torvalds's avatar
Linus Torvalds committed
605
	}
606 607 608 609
	/*
	 * FIXME: Once we trust the LDISC code better we can wait here for
	 * ldisc completion and fix the driver call race
	 */
Linus Torvalds's avatar
Linus Torvalds committed
610 611 612 613 614 615 616
	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->driver->flags & TTY_DRIVER_RESET_TERMIOS)
617
		tty_reset_termios(tty);
Linus Torvalds's avatar
Linus Torvalds committed
618 619
	/* Defer ldisc switch */
	/* tty_deferred_ldisc_switch(N_TTY);
620

Linus Torvalds's avatar
Linus Torvalds committed
621 622
	  This should get done automatically when the port closes and
	  tty_release is called */
623

Linus Torvalds's avatar
Linus Torvalds committed
624
	read_lock(&tasklist_lock);
625 626
	if (tty->session) {
		do_each_pid_task(tty->session, PIDTYPE_SID, p) {
627
			spin_lock_irq(&p->sighand->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
628 629
			if (p->signal->tty == tty)
				p->signal->tty = NULL;
630 631
			if (!p->signal->leader) {
				spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds's avatar
Linus Torvalds committed
632
				continue;
633 634 635
			}
			__group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
			__group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
636
			put_pid(p->signal->tty_old_pgrp);  /* A noop */
Alan Cox's avatar
Alan Cox committed
637
			spin_lock_irqsave(&tty->ctrl_lock, flags);
638 639
			if (tty->pgrp)
				p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Alan Cox's avatar
Alan Cox committed
640
			spin_unlock_irqrestore(&tty->ctrl_lock, flags);
641
			spin_unlock_irq(&p->sighand->siglock);
642
		} while_each_pid_task(tty->session, PIDTYPE_SID, p);
Linus Torvalds's avatar
Linus Torvalds committed
643 644 645
	}
	read_unlock(&tasklist_lock);

Alan Cox's avatar
Alan Cox committed
646
	spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
647
	tty->flags = 0;
648 649
	put_pid(tty->session);
	put_pid(tty->pgrp);
650 651
	tty->session = NULL;
	tty->pgrp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
652
	tty->ctrl_status = 0;
Alan Cox's avatar
Alan Cox committed
653 654
	spin_unlock_irqrestore(&tty->ctrl_lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
655
	/*
656 657 658 659
	 * If one of the devices matches a console pointer, we
	 * cannot just call hangup() because that will cause
	 * tty->count and state->count to go out of sync.
	 * So we just call close() the right number of times.
Linus Torvalds's avatar
Linus Torvalds committed
660 661
	 */
	if (cons_filp) {
Alan Cox's avatar
Alan Cox committed
662
		if (tty->ops->close)
Linus Torvalds's avatar
Linus Torvalds committed
663
			for (n = 0; n < closecount; n++)
Alan Cox's avatar
Alan Cox committed
664 665 666
				tty->ops->close(tty, cons_filp);
	} else if (tty->ops->hangup)
		(tty->ops->hangup)(tty);
667 668 669 670 671 672
	/*
	 * We don't want to have driver/ldisc interactions beyond
	 * the ones we did here. The driver layer expects no
	 * calls after ->hangup() from the ldisc side. However we
	 * can't yet guarantee all that.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
673 674 675 676 677 678 679 680 681 682
	set_bit(TTY_HUPPED, &tty->flags);
	if (ld) {
		tty_ldisc_enable(tty);
		tty_ldisc_deref(ld);
	}
	unlock_kernel();
	if (f)
		fput(f);
}

683 684 685 686 687 688 689 690
/**
 *	tty_hangup		-	trigger a hangup event
 *	@tty: tty to hangup
 *
 *	A carrier loss (virtual or otherwise) has occurred on this like
 *	schedule a hangup sequence to run after this event.
 */

691
void tty_hangup(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
692 693 694 695 696 697 698 699 700 701
{
#ifdef TTY_DEBUG_HANGUP
	char	buf[64];
	printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
#endif
	schedule_work(&tty->hangup_work);
}

EXPORT_SYMBOL(tty_hangup);

702 703 704 705 706 707
/**
 *	tty_vhangup		-	process vhangup
 *	@tty: tty to hangup
 *
 *	The user has asked via system call for the terminal to be hung up.
 *	We do this synchronously so that when the syscall returns the process
708
 *	is complete. That guarantee is necessary for security reasons.
709 710
 */

711
void tty_vhangup(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
712 713 714 715 716 717
{
#ifdef TTY_DEBUG_HANGUP
	char	buf[64];

	printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
#endif
718
	do_tty_hangup(&tty->hangup_work);
Linus Torvalds's avatar
Linus Torvalds committed
719
}
720

Linus Torvalds's avatar
Linus Torvalds committed
721 722
EXPORT_SYMBOL(tty_vhangup);

723 724 725 726 727 728 729 730
/**
 *	tty_hung_up_p		-	was tty hung up
 *	@filp: file pointer of tty
 *
 *	Return true if the tty has been subject to a vhangup or a carrier
 *	loss
 */

731
int tty_hung_up_p(struct file *filp)
Linus Torvalds's avatar
Linus Torvalds committed
732 733 734 735 736 737
{
	return (filp->f_op == &hung_up_tty_fops);
}

EXPORT_SYMBOL(tty_hung_up_p);

738
static void session_clear_tty(struct pid *session)
739 740
{
	struct task_struct *p;
741
	do_each_pid_task(session, PIDTYPE_SID, p) {
742
		proc_clear_tty(p);
743
	} while_each_pid_task(session, PIDTYPE_SID, p);
744 745
}

746 747 748
/**
 *	disassociate_ctty	-	disconnect controlling tty
 *	@on_exit: true if exiting so need to "hang up" the session
Linus Torvalds's avatar
Linus Torvalds committed
749
 *
750 751 752 753
 *	This function is typically called only by the session leader, when
 *	it wants to disassociate itself from its controlling tty.
 *
 *	It performs the following functions:
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756 757 758
 * 	(1)  Sends a SIGHUP and SIGCONT to the foreground process group
 * 	(2)  Clears the tty from being controlling the session
 * 	(3)  Clears the controlling tty for all processes in the
 * 		session group.
 *
759 760 761
 *	The argument on_exit is set to 1 if called when a process is
 *	exiting; it is 0 if called by the ioctl TIOCNOTTY.
 *
762
 *	Locking:
763
 *		BKL is taken for hysterical raisins
764 765 766 767
 *		  tty_mutex is taken to protect tty
 *		  ->siglock is taken to protect ->signal/->sighand
 *		  tasklist_lock is taken to walk process list for sessions
 *		    ->siglock is taken to protect ->signal/->sighand
Linus Torvalds's avatar
Linus Torvalds committed
768
 */
769

Linus Torvalds's avatar
Linus Torvalds committed
770 771 772
void disassociate_ctty(int on_exit)
{
	struct tty_struct *tty;
773
	struct pid *tty_pgrp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
774 775


Ingo Molnar's avatar
Ingo Molnar committed
776
	mutex_lock(&tty_mutex);
777
	tty = get_current_tty();
Linus Torvalds's avatar
Linus Torvalds committed
778
	if (tty) {
779
		tty_pgrp = get_pid(tty->pgrp);
Alan Cox's avatar
Alan Cox committed
780
		lock_kernel();
781
		mutex_unlock(&tty_mutex);
782
		/* XXX: here we race, there is nothing protecting tty */
Linus Torvalds's avatar
Linus Torvalds committed
783 784
		if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
			tty_vhangup(tty);
Alan Cox's avatar
Alan Cox committed
785
		unlock_kernel();
786
	} else if (on_exit) {
787
		struct pid *old_pgrp;
788 789
		spin_lock_irq(&current->sighand->siglock);
		old_pgrp = current->signal->tty_old_pgrp;
790
		current->signal->tty_old_pgrp = NULL;
791
		spin_unlock_irq(&current->sighand->siglock);
792
		if (old_pgrp) {
793 794 795
			kill_pgrp(old_pgrp, SIGHUP, on_exit);
			kill_pgrp(old_pgrp, SIGCONT, on_exit);
			put_pid(old_pgrp);
Linus Torvalds's avatar
Linus Torvalds committed
796
		}
Ingo Molnar's avatar
Ingo Molnar committed
797
		mutex_unlock(&tty_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
798 799
		return;
	}
800 801
	if (tty_pgrp) {
		kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Linus Torvalds's avatar
Linus Torvalds committed
802
		if (!on_exit)
803 804
			kill_pgrp(tty_pgrp, SIGCONT, on_exit);
		put_pid(tty_pgrp);
Linus Torvalds's avatar
Linus Torvalds committed
805 806
	}

807
	spin_lock_irq(&current->sighand->siglock);
808
	put_pid(current->signal->tty_old_pgrp);
Randy Dunlap's avatar
Randy Dunlap committed
809
	current->signal->tty_old_pgrp = NULL;
810 811 812 813 814 815
	spin_unlock_irq(&current->sighand->siglock);

	mutex_lock(&tty_mutex);
	/* It is possible that do_tty_hangup has free'd this tty */
	tty = get_current_tty();
	if (tty) {
Alan Cox's avatar
Alan Cox committed
816 817
		unsigned long flags;
		spin_lock_irqsave(&tty->ctrl_lock, flags);
818 819 820 821
		put_pid(tty->session);
		put_pid(tty->pgrp);
		tty->session = NULL;
		tty->pgrp = NULL;
Alan Cox's avatar
Alan Cox committed
822
		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
823 824 825 826 827 828 829
	} else {
#ifdef TTY_DEBUG_HANGUP
		printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
		       " = NULL", tty);
#endif
	}
	mutex_unlock(&tty_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
830 831 832

	/* Now clear signal->tty under the lock */
	read_lock(&tasklist_lock);
833
	session_clear_tty(task_session(current));
Linus Torvalds's avatar
Linus Torvalds committed
834 835 836
	read_unlock(&tasklist_lock);
}

837 838 839 840 841 842 843
/**
 *
 *	no_tty	- Ensure the current process does not have a controlling tty
 */
void no_tty(void)
{
	struct task_struct *tsk = current;
Alan Cox's avatar
Alan Cox committed
844
	lock_kernel();
845 846
	if (tsk->signal->leader)
		disassociate_ctty(0);
Alan Cox's avatar
Alan Cox committed
847
	unlock_kernel();
848 849 850
	proc_clear_tty(tsk);
}

851 852

/**
853
 *	stop_tty	-	propagate flow control
854 855 856
 *	@tty: tty to stop
 *
 *	Perform flow control to the driver. For PTY/TTY pairs we
857
 *	must also propagate the TIOCKPKT status. May be called
858 859 860 861 862 863 864 865 866
 *	on an already stopped device and will not re-call the driver
 *	method.
 *
 *	This functionality is used by both the line disciplines for
 *	halting incoming flow and by the driver. It may therefore be
 *	called from any context, may be under the tty atomic_write_lock
 *	but not always.
 *
 *	Locking:
Alan Cox's avatar
Alan Cox committed
867
 *		Uses the tty control lock internally
868 869
 */

Linus Torvalds's avatar
Linus Torvalds committed
870 871
void stop_tty(struct tty_struct *tty)
{
Alan Cox's avatar
Alan Cox committed
872 873 874 875
	unsigned long flags;
	spin_lock_irqsave(&tty->ctrl_lock, flags);
	if (tty->stopped) {
		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
876
		return;
Alan Cox's avatar
Alan Cox committed
877
	}
Linus Torvalds's avatar
Linus Torvalds committed
878 879 880 881 882 883
	tty->stopped = 1;
	if (tty->link && tty->link->packet) {
		tty->ctrl_status &= ~TIOCPKT_START;
		tty->ctrl_status |= TIOCPKT_STOP;
		wake_up_interruptible(&tty->link->read_wait);
	}
Alan Cox's avatar
Alan Cox committed
884
	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox's avatar
Alan Cox committed
885 886
	if (tty->ops->stop)
		(tty->ops->stop)(tty);
Linus Torvalds's avatar
Linus Torvalds committed
887 888 889 890
}

EXPORT_SYMBOL(stop_tty);

891
/**
892
 *	start_tty	-	propagate flow control
893 894 895
 *	@tty: tty to start
 *
 *	Start a tty that has been stopped if at all possible. Perform
896
 *	any necessary wakeups and propagate the TIOCPKT status. If this
897 898 899 900
 *	is the tty was previous stopped and is being started then the
 *	driver start method is invoked and the line discipline woken.
 *
 *	Locking:
Alan Cox's avatar
Alan Cox committed
901
 *		ctrl_lock
902 903
 */

Linus Torvalds's avatar
Linus Torvalds committed
904 905
void start_tty(struct tty_struct *tty)
{
Alan Cox's avatar
Alan Cox committed
906 907 908 909
	unsigned long flags;
	spin_lock_irqsave(&tty->ctrl_lock, flags);
	if (!tty->stopped || tty->flow_stopped) {
		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
910
		return;
Alan Cox's avatar
Alan Cox committed
911
	}
Linus Torvalds's avatar
Linus Torvalds committed
912 913 914 915 916 917
	tty->stopped = 0;
	if (tty->link && tty->link->packet) {
		tty->ctrl_status &= ~TIOCPKT_STOP;
		tty->ctrl_status |= TIOCPKT_START;
		wake_up_interruptible(&tty->link->read_wait);
	}
Alan Cox's avatar
Alan Cox committed
918
	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox's avatar
Alan Cox committed
919 920
	if (tty->ops->start)
		(tty->ops->start)(tty);
Linus Torvalds's avatar
Linus Torvalds committed
921 922 923 924 925 926
	/* If we have a running line discipline it may need kicking */
	tty_wakeup(tty);
}

EXPORT_SYMBOL(start_tty);

927 928 929 930 931 932 933 934 935 936 937
/**
 *	tty_read	-	read method for tty device files
 *	@file: pointer to tty file
 *	@buf: user buffer
 *	@count: size of user buffer
 *	@ppos: unused
 *
 *	Perform the read system call function on this terminal device. Checks
 *	for hung up devices before calling the line discipline method.
 *
 *	Locking:
Alan Cox's avatar
Alan Cox committed
938 939
 *		Locks the line discipline internally while needed. Multiple
 *	read calls may be outstanding in parallel.
940 941
 */

942
static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds's avatar
Linus Torvalds committed
943 944 945
			loff_t *ppos)
{
	int i;
946
	struct tty_struct *tty;
Linus Torvalds's avatar
Linus Torvalds committed
947 948 949 950
	struct inode *inode;
	struct tty_ldisc *ld;

	tty = (struct tty_struct *)file->private_data;
951
	inode = file->f_path.dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955 956 957 958 959
	if (tty_paranoia_check(tty, inode, "tty_read"))
		return -EIO;
	if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
		return -EIO;

	/* We want to wait for the line discipline to sort out in this
	   situation */
	ld = tty_ldisc_ref_wait(tty);
Alan Cox's avatar
Alan Cox committed
960 961
	if (ld->ops->read)
		i = (ld->ops->read)(tty, file, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
962 963 964 965 966 967 968 969
	else
		i = -EIO;
	tty_ldisc_deref(ld);
	if (i > 0)
		inode->i_atime = current_fs_time(inode->i_sb);
	return i;
}

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
void tty_write_unlock(struct tty_struct *tty)
{
	mutex_unlock(&tty->atomic_write_lock);
	wake_up_interruptible(&tty->write_wait);
}

int tty_write_lock(struct tty_struct *tty, int ndelay)
{
	if (!mutex_trylock(&tty->atomic_write_lock)) {
		if (ndelay)
			return -EAGAIN;
		if (mutex_lock_interruptible(&tty->atomic_write_lock))
			return -ERESTARTSYS;
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
987 988 989 990 991 992 993 994 995 996 997
/*
 * Split writes up in sane blocksizes to avoid
 * denial-of-service type attacks
 */
static inline ssize_t do_tty_write(
	ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
	struct tty_struct *tty,
	struct file *file,
	const char __user *buf,
	size_t count)
{
998
	ssize_t ret, written = 0;
Linus Torvalds's avatar
Linus Torvalds committed
999
	unsigned int chunk;
1000

1001 1002 1003
	ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
	if (ret < 0)
		return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

	/*
	 * We chunk up writes into a temporary buffer. This
	 * simplifies low-level drivers immensely, since they
	 * don't have locking issues and user mode accesses.
	 *
	 * But if TTY_NO_WRITE_SPLIT is set, we should use a
	 * big chunk-size..
	 *
	 * The default chunk-size is 2kB, because the NTTY
	 * layer has problems with bigger chunks. It will
	 * claim to be able to handle more characters than
	 * it actually does.
1017 1018 1019
	 *
	 * FIXME: This can probably go away now except that 64K chunks
	 * are too likely to fail unless switched to vmalloc...
Linus Torvalds's avatar
Linus Torvalds committed
1020 1021 1022 1023 1024 1025 1026
	 */
	chunk = 2048;
	if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
		chunk = 65536;
	if (count < chunk)
		chunk = count;

Ingo Molnar's avatar
Ingo Molnar committed
1027
	/* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds's avatar
Linus Torvalds committed
1028 1029 1030 1031 1032 1033 1034 1035
	if (tty->write_cnt < chunk) {
		unsigned char *buf;

		if (chunk < 1024)
			chunk = 1024;

		buf = kmalloc(chunk, GFP_KERNEL);
		if (!buf) {
1036 1037
			ret = -ENOMEM;
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
		}
		kfree(tty->write_buf);
		tty->write_cnt = chunk;
		tty->write_buf = buf;
	}

	/* Do the write .. */
	for (;;) {
		size_t size = count;
		if (size > chunk)
			size = chunk;
		ret = -EFAULT;
		if (copy_from_user(tty->write_buf, buf, size))
			break;
		ret = write(tty, file, tty->write_buf, size);
		if (ret <= 0)
			break;
		written += ret;
		buf += ret;
		count -= ret;
		if (!count)
			break;
		ret = -ERESTARTSYS;
		if (signal_pending(current))
			break;
		cond_resched();
	}
	if (written) {
1066
		struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
1067 1068 1069
		inode->i_mtime = current_fs_time(inode->i_sb);
		ret = written;
	}
1070 1071
out:
	tty_write_unlock(tty);
Linus Torvalds's avatar
Linus Torvalds committed
1072 1073 1074 1075
	return ret;
}


1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
/**
 *	tty_write		-	write method for tty device file
 *	@file: tty file pointer
 *	@buf: user data to write
 *	@count: bytes to write
 *	@ppos: unused
 *
 *	Write data to a tty device via the line discipline.
 *
 *	Locking:
 *		Locks the line discipline as required
 *		Writes to the tty driver are serialized by the atomic_write_lock
 *	and are then processed in chunks to the device. The line discipline
 *	write method will not be involked in parallel for each device
 *		The line discipline write method is called under the big
 *	kernel lock for historical reasons. New code should not rely on this.
 */

1094 1095
static ssize_t tty_write(struct file *file, const char __user *buf,
						size_t count, loff_t *ppos)
Linus Torvalds's avatar
Linus Torvalds committed
1096
{
1097
	struct tty_struct *tty;
1098
	struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
1099 1100
	ssize_t ret;
	struct tty_ldisc *ld;
1101

Linus Torvalds's avatar
Linus Torvalds committed
1102 1103 1104
	tty = (struct tty_struct *)file->private_data;
	if (tty_paranoia_check(tty, inode, "tty_write"))
		return -EIO;
Alan Cox's avatar
Alan Cox committed
1105
	if (!tty || !tty->ops->write ||
1106 1107
		(test_bit(TTY_IO_ERROR, &tty->flags)))
			return -EIO;
Alan Cox's avatar
Alan Cox committed
1108 1109 1110 1111
	/* Short term debug to catch buggy drivers */
	if (tty->ops->write_room == NULL)
		printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
			tty->driver->name);
1112
	ld = tty_ldisc_ref_wait(tty);
Alan Cox's avatar
Alan Cox committed
1113
	if (!ld->ops->write)
Linus Torvalds's avatar
Linus Torvalds committed
1114 1115
		ret = -EIO;
	else
Alan Cox's avatar
Alan Cox committed
1116
		ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds's avatar
Linus Torvalds committed
1117 1118 1119 1120
	tty_ldisc_deref(ld);
	return ret;
}

1121 1122
ssize_t redirected_tty_write(struct file *file, const char __user *buf,
						size_t count, loff_t *ppos)
Linus Torvalds's avatar
Linus Torvalds committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
{
	struct file *p = NULL;

	spin_lock(&redirect_lock);
	if (redirect) {
		get_file(redirect);
		p = redirect;
	}
	spin_unlock(&redirect_lock);

	if (p) {
		ssize_t res;
		res = vfs_write(p, buf, count, &p->f_pos);
		fput(p);
		return res;
	}
	return tty_write(file, buf, count, ppos);
}

1142 1143 1144 1145 1146 1147
void tty_port_init(struct tty_port *port)
{
	memset(port, 0, sizeof(*port));
	init_waitqueue_head(&port->open_wait);
	init_waitqueue_head(&port->close_wait);
	mutex_init(&port->mutex);
Alan Cox's avatar
Alan Cox committed
1148 1149
	port->close_delay = (50 * HZ) / 100;
	port->closing_wait = (3000 * HZ) / 100;
1150 1151 1152 1153