xref: /openbmc/linux/drivers/tty/tty_io.c (revision 5e00bbfb)
196fd7ce5SGreg Kroah-Hartman /*
296fd7ce5SGreg Kroah-Hartman  *  Copyright (C) 1991, 1992  Linus Torvalds
396fd7ce5SGreg Kroah-Hartman  */
496fd7ce5SGreg Kroah-Hartman 
596fd7ce5SGreg Kroah-Hartman /*
696fd7ce5SGreg Kroah-Hartman  * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
796fd7ce5SGreg Kroah-Hartman  * or rs-channels. It also implements echoing, cooked mode etc.
896fd7ce5SGreg Kroah-Hartman  *
996fd7ce5SGreg Kroah-Hartman  * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
1096fd7ce5SGreg Kroah-Hartman  *
1196fd7ce5SGreg Kroah-Hartman  * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
1296fd7ce5SGreg Kroah-Hartman  * tty_struct and tty_queue structures.  Previously there was an array
1396fd7ce5SGreg Kroah-Hartman  * of 256 tty_struct's which was statically allocated, and the
1496fd7ce5SGreg Kroah-Hartman  * tty_queue structures were allocated at boot time.  Both are now
1596fd7ce5SGreg Kroah-Hartman  * dynamically allocated only when the tty is open.
1696fd7ce5SGreg Kroah-Hartman  *
1796fd7ce5SGreg Kroah-Hartman  * Also restructured routines so that there is more of a separation
1896fd7ce5SGreg Kroah-Hartman  * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
1996fd7ce5SGreg Kroah-Hartman  * the low-level tty routines (serial.c, pty.c, console.c).  This
2096fd7ce5SGreg Kroah-Hartman  * makes for cleaner and more compact code.  -TYT, 9/17/92
2196fd7ce5SGreg Kroah-Hartman  *
2296fd7ce5SGreg Kroah-Hartman  * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
2396fd7ce5SGreg Kroah-Hartman  * which can be dynamically activated and de-activated by the line
2496fd7ce5SGreg Kroah-Hartman  * discipline handling modules (like SLIP).
2596fd7ce5SGreg Kroah-Hartman  *
2696fd7ce5SGreg Kroah-Hartman  * NOTE: pay no attention to the line discipline code (yet); its
2796fd7ce5SGreg Kroah-Hartman  * interface is still subject to change in this version...
2896fd7ce5SGreg Kroah-Hartman  * -- TYT, 1/31/92
2996fd7ce5SGreg Kroah-Hartman  *
3096fd7ce5SGreg Kroah-Hartman  * Added functionality to the OPOST tty handling.  No delays, but all
3196fd7ce5SGreg Kroah-Hartman  * other bits should be there.
3296fd7ce5SGreg Kroah-Hartman  *	-- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
3396fd7ce5SGreg Kroah-Hartman  *
3496fd7ce5SGreg Kroah-Hartman  * Rewrote canonical mode and added more termios flags.
3596fd7ce5SGreg Kroah-Hartman  * 	-- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
3696fd7ce5SGreg Kroah-Hartman  *
3796fd7ce5SGreg Kroah-Hartman  * Reorganized FASYNC support so mouse code can share it.
3896fd7ce5SGreg Kroah-Hartman  *	-- ctm@ardi.com, 9Sep95
3996fd7ce5SGreg Kroah-Hartman  *
4096fd7ce5SGreg Kroah-Hartman  * New TIOCLINUX variants added.
4196fd7ce5SGreg Kroah-Hartman  *	-- mj@k332.feld.cvut.cz, 19-Nov-95
4296fd7ce5SGreg Kroah-Hartman  *
4396fd7ce5SGreg Kroah-Hartman  * Restrict vt switching via ioctl()
4496fd7ce5SGreg Kroah-Hartman  *      -- grif@cs.ucr.edu, 5-Dec-95
4596fd7ce5SGreg Kroah-Hartman  *
4696fd7ce5SGreg Kroah-Hartman  * Move console and virtual terminal code to more appropriate files,
4796fd7ce5SGreg Kroah-Hartman  * implement CONFIG_VT and generalize console device interface.
4896fd7ce5SGreg Kroah-Hartman  *	-- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
4996fd7ce5SGreg Kroah-Hartman  *
5096fd7ce5SGreg Kroah-Hartman  * Rewrote tty_init_dev and tty_release_dev to eliminate races.
5196fd7ce5SGreg Kroah-Hartman  *	-- Bill Hawes <whawes@star.net>, June 97
5296fd7ce5SGreg Kroah-Hartman  *
5396fd7ce5SGreg Kroah-Hartman  * Added devfs support.
5496fd7ce5SGreg Kroah-Hartman  *      -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
5596fd7ce5SGreg Kroah-Hartman  *
5696fd7ce5SGreg Kroah-Hartman  * Added support for a Unix98-style ptmx device.
5796fd7ce5SGreg Kroah-Hartman  *      -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
5896fd7ce5SGreg Kroah-Hartman  *
5996fd7ce5SGreg Kroah-Hartman  * Reduced memory usage for older ARM systems
6096fd7ce5SGreg Kroah-Hartman  *      -- Russell King <rmk@arm.linux.org.uk>
6196fd7ce5SGreg Kroah-Hartman  *
6296fd7ce5SGreg Kroah-Hartman  * Move do_SAK() into process context.  Less stack use in devfs functions.
6396fd7ce5SGreg Kroah-Hartman  * alloc_tty_struct() always uses kmalloc()
6496fd7ce5SGreg Kroah-Hartman  *			 -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
6596fd7ce5SGreg Kroah-Hartman  */
6696fd7ce5SGreg Kroah-Hartman 
6796fd7ce5SGreg Kroah-Hartman #include <linux/types.h>
6896fd7ce5SGreg Kroah-Hartman #include <linux/major.h>
6996fd7ce5SGreg Kroah-Hartman #include <linux/errno.h>
7096fd7ce5SGreg Kroah-Hartman #include <linux/signal.h>
7196fd7ce5SGreg Kroah-Hartman #include <linux/fcntl.h>
7296fd7ce5SGreg Kroah-Hartman #include <linux/sched.h>
7396fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h>
7496fd7ce5SGreg Kroah-Hartman #include <linux/tty.h>
7596fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h>
7696fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h>
7796fd7ce5SGreg Kroah-Hartman #include <linux/devpts_fs.h>
7896fd7ce5SGreg Kroah-Hartman #include <linux/file.h>
7996fd7ce5SGreg Kroah-Hartman #include <linux/fdtable.h>
8096fd7ce5SGreg Kroah-Hartman #include <linux/console.h>
8196fd7ce5SGreg Kroah-Hartman #include <linux/timer.h>
8296fd7ce5SGreg Kroah-Hartman #include <linux/ctype.h>
8396fd7ce5SGreg Kroah-Hartman #include <linux/kd.h>
8496fd7ce5SGreg Kroah-Hartman #include <linux/mm.h>
8596fd7ce5SGreg Kroah-Hartman #include <linux/string.h>
8696fd7ce5SGreg Kroah-Hartman #include <linux/slab.h>
8796fd7ce5SGreg Kroah-Hartman #include <linux/poll.h>
8896fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h>
8996fd7ce5SGreg Kroah-Hartman #include <linux/init.h>
9096fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
9196fd7ce5SGreg Kroah-Hartman #include <linux/device.h>
9296fd7ce5SGreg Kroah-Hartman #include <linux/wait.h>
9396fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h>
9496fd7ce5SGreg Kroah-Hartman #include <linux/delay.h>
9596fd7ce5SGreg Kroah-Hartman #include <linux/seq_file.h>
9696fd7ce5SGreg Kroah-Hartman #include <linux/serial.h>
975a3c6b25SManuel Zerpies #include <linux/ratelimit.h>
9896fd7ce5SGreg Kroah-Hartman 
9996fd7ce5SGreg Kroah-Hartman #include <linux/uaccess.h>
10096fd7ce5SGreg Kroah-Hartman 
10196fd7ce5SGreg Kroah-Hartman #include <linux/kbd_kern.h>
10296fd7ce5SGreg Kroah-Hartman #include <linux/vt_kern.h>
10396fd7ce5SGreg Kroah-Hartman #include <linux/selection.h>
10496fd7ce5SGreg Kroah-Hartman 
10596fd7ce5SGreg Kroah-Hartman #include <linux/kmod.h>
10696fd7ce5SGreg Kroah-Hartman #include <linux/nsproxy.h>
10796fd7ce5SGreg Kroah-Hartman 
10896fd7ce5SGreg Kroah-Hartman #undef TTY_DEBUG_HANGUP
109accff793SPeter Hurley #ifdef TTY_DEBUG_HANGUP
110accff793SPeter Hurley # define tty_debug_hangup(tty, f, args...)	tty_debug(tty, f, ##args)
111accff793SPeter Hurley #else
112accff793SPeter Hurley # define tty_debug_hangup(tty, f, args...)	do { } while (0)
113accff793SPeter Hurley #endif
11496fd7ce5SGreg Kroah-Hartman 
11596fd7ce5SGreg Kroah-Hartman #define TTY_PARANOIA_CHECK 1
11696fd7ce5SGreg Kroah-Hartman #define CHECK_TTY_COUNT 1
11796fd7ce5SGreg Kroah-Hartman 
11896fd7ce5SGreg Kroah-Hartman struct ktermios tty_std_termios = {	/* for the benefit of tty drivers  */
11996fd7ce5SGreg Kroah-Hartman 	.c_iflag = ICRNL | IXON,
12096fd7ce5SGreg Kroah-Hartman 	.c_oflag = OPOST | ONLCR,
12196fd7ce5SGreg Kroah-Hartman 	.c_cflag = B38400 | CS8 | CREAD | HUPCL,
12296fd7ce5SGreg Kroah-Hartman 	.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
12396fd7ce5SGreg Kroah-Hartman 		   ECHOCTL | ECHOKE | IEXTEN,
12496fd7ce5SGreg Kroah-Hartman 	.c_cc = INIT_C_CC,
12596fd7ce5SGreg Kroah-Hartman 	.c_ispeed = 38400,
126133b1306SPeter Hurley 	.c_ospeed = 38400,
127133b1306SPeter Hurley 	/* .c_line = N_TTY, */
12896fd7ce5SGreg Kroah-Hartman };
12996fd7ce5SGreg Kroah-Hartman 
13096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_std_termios);
13196fd7ce5SGreg Kroah-Hartman 
13296fd7ce5SGreg Kroah-Hartman /* This list gets poked at by procfs and various bits of boot up code. This
13396fd7ce5SGreg Kroah-Hartman    could do with some rationalisation such as pulling the tty proc function
13496fd7ce5SGreg Kroah-Hartman    into this file */
13596fd7ce5SGreg Kroah-Hartman 
13696fd7ce5SGreg Kroah-Hartman LIST_HEAD(tty_drivers);			/* linked list of tty drivers */
13796fd7ce5SGreg Kroah-Hartman 
138d1d027efSPeter Hurley /* Mutex to protect creating and releasing a tty */
13996fd7ce5SGreg Kroah-Hartman DEFINE_MUTEX(tty_mutex);
14096fd7ce5SGreg Kroah-Hartman 
14196fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
14296fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
14396fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *, const char __user *,
14496fd7ce5SGreg Kroah-Hartman 							size_t, loff_t *);
14596fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *, poll_table *);
14696fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *, struct file *);
14796fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
14896fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT
14996fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd,
15096fd7ce5SGreg Kroah-Hartman 				unsigned long arg);
15196fd7ce5SGreg Kroah-Hartman #else
15296fd7ce5SGreg Kroah-Hartman #define tty_compat_ioctl NULL
15396fd7ce5SGreg Kroah-Hartman #endif
15496fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on);
15596fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on);
15696fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx);
15796fd7ce5SGreg Kroah-Hartman 
15896fd7ce5SGreg Kroah-Hartman /**
15996fd7ce5SGreg Kroah-Hartman  *	free_tty_struct		-	free a disused tty
16096fd7ce5SGreg Kroah-Hartman  *	@tty: tty struct to free
16196fd7ce5SGreg Kroah-Hartman  *
16296fd7ce5SGreg Kroah-Hartman  *	Free the write buffers, tty queue and tty memory itself.
16396fd7ce5SGreg Kroah-Hartman  *
16496fd7ce5SGreg Kroah-Hartman  *	Locking: none. Must be called after tty is definitely unused
16596fd7ce5SGreg Kroah-Hartman  */
16696fd7ce5SGreg Kroah-Hartman 
167a3123fd0SPeter Hurley static void free_tty_struct(struct tty_struct *tty)
16896fd7ce5SGreg Kroah-Hartman {
169c8b710b3SPeter Hurley 	tty_ldisc_deinit(tty);
17096fd7ce5SGreg Kroah-Hartman 	put_device(tty->dev);
17196fd7ce5SGreg Kroah-Hartman 	kfree(tty->write_buf);
17289c8d91eSAlan Cox 	tty->magic = 0xDEADDEAD;
17396fd7ce5SGreg Kroah-Hartman 	kfree(tty);
17496fd7ce5SGreg Kroah-Hartman }
17596fd7ce5SGreg Kroah-Hartman 
17696fd7ce5SGreg Kroah-Hartman static inline struct tty_struct *file_tty(struct file *file)
17796fd7ce5SGreg Kroah-Hartman {
17896fd7ce5SGreg Kroah-Hartman 	return ((struct tty_file_private *)file->private_data)->tty;
17996fd7ce5SGreg Kroah-Hartman }
18096fd7ce5SGreg Kroah-Hartman 
181fa90e1c9SJiri Slaby int tty_alloc_file(struct file *file)
18296fd7ce5SGreg Kroah-Hartman {
18396fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv;
18496fd7ce5SGreg Kroah-Hartman 
18596fd7ce5SGreg Kroah-Hartman 	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
18696fd7ce5SGreg Kroah-Hartman 	if (!priv)
18796fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
18896fd7ce5SGreg Kroah-Hartman 
189fa90e1c9SJiri Slaby 	file->private_data = priv;
190fa90e1c9SJiri Slaby 
191fa90e1c9SJiri Slaby 	return 0;
192fa90e1c9SJiri Slaby }
193fa90e1c9SJiri Slaby 
194fa90e1c9SJiri Slaby /* Associate a new file with the tty structure */
195fa90e1c9SJiri Slaby void tty_add_file(struct tty_struct *tty, struct file *file)
196fa90e1c9SJiri Slaby {
197fa90e1c9SJiri Slaby 	struct tty_file_private *priv = file->private_data;
198fa90e1c9SJiri Slaby 
19996fd7ce5SGreg Kroah-Hartman 	priv->tty = tty;
20096fd7ce5SGreg Kroah-Hartman 	priv->file = file;
20196fd7ce5SGreg Kroah-Hartman 
2024a510969SPeter Hurley 	spin_lock(&tty->files_lock);
20396fd7ce5SGreg Kroah-Hartman 	list_add(&priv->list, &tty->tty_files);
2044a510969SPeter Hurley 	spin_unlock(&tty->files_lock);
205fa90e1c9SJiri Slaby }
20696fd7ce5SGreg Kroah-Hartman 
207fa90e1c9SJiri Slaby /**
208fa90e1c9SJiri Slaby  * tty_free_file - free file->private_data
209fa90e1c9SJiri Slaby  *
210fa90e1c9SJiri Slaby  * This shall be used only for fail path handling when tty_add_file was not
211fa90e1c9SJiri Slaby  * called yet.
212fa90e1c9SJiri Slaby  */
213fa90e1c9SJiri Slaby void tty_free_file(struct file *file)
214fa90e1c9SJiri Slaby {
215fa90e1c9SJiri Slaby 	struct tty_file_private *priv = file->private_data;
216fa90e1c9SJiri Slaby 
217fa90e1c9SJiri Slaby 	file->private_data = NULL;
218fa90e1c9SJiri Slaby 	kfree(priv);
21996fd7ce5SGreg Kroah-Hartman }
22096fd7ce5SGreg Kroah-Hartman 
22196fd7ce5SGreg Kroah-Hartman /* Delete file from its tty */
2222520e274SJosh Triplett static void tty_del_file(struct file *file)
22396fd7ce5SGreg Kroah-Hartman {
22496fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv = file->private_data;
2254a510969SPeter Hurley 	struct tty_struct *tty = priv->tty;
22696fd7ce5SGreg Kroah-Hartman 
2274a510969SPeter Hurley 	spin_lock(&tty->files_lock);
22896fd7ce5SGreg Kroah-Hartman 	list_del(&priv->list);
2294a510969SPeter Hurley 	spin_unlock(&tty->files_lock);
230fa90e1c9SJiri Slaby 	tty_free_file(file);
23196fd7ce5SGreg Kroah-Hartman }
23296fd7ce5SGreg Kroah-Hartman 
23396fd7ce5SGreg Kroah-Hartman 
23496fd7ce5SGreg Kroah-Hartman #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
23596fd7ce5SGreg Kroah-Hartman 
23696fd7ce5SGreg Kroah-Hartman /**
23796fd7ce5SGreg Kroah-Hartman  *	tty_name	-	return tty naming
23896fd7ce5SGreg Kroah-Hartman  *	@tty: tty structure
23996fd7ce5SGreg Kroah-Hartman  *
24096fd7ce5SGreg Kroah-Hartman  *	Convert a tty structure into a name. The name reflects the kernel
24196fd7ce5SGreg Kroah-Hartman  *	naming policy and if udev is in use may not reflect user space
24296fd7ce5SGreg Kroah-Hartman  *
24396fd7ce5SGreg Kroah-Hartman  *	Locking: none
24496fd7ce5SGreg Kroah-Hartman  */
24596fd7ce5SGreg Kroah-Hartman 
246429b4749SRasmus Villemoes const char *tty_name(const struct tty_struct *tty)
24796fd7ce5SGreg Kroah-Hartman {
24896fd7ce5SGreg Kroah-Hartman 	if (!tty) /* Hmm.  NULL pointer.  That's fun. */
249917162c9SRasmus Villemoes 		return "NULL tty";
250917162c9SRasmus Villemoes 	return tty->name;
25196fd7ce5SGreg Kroah-Hartman }
25296fd7ce5SGreg Kroah-Hartman 
25396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_name);
25496fd7ce5SGreg Kroah-Hartman 
2550a083eddSPeter Hurley const char *tty_driver_name(const struct tty_struct *tty)
2560a083eddSPeter Hurley {
2570a083eddSPeter Hurley 	if (!tty || !tty->driver)
2580a083eddSPeter Hurley 		return "";
2590a083eddSPeter Hurley 	return tty->driver->name;
2600a083eddSPeter Hurley }
2610a083eddSPeter Hurley 
26282b8f888SPeter Hurley static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
26396fd7ce5SGreg Kroah-Hartman 			      const char *routine)
26496fd7ce5SGreg Kroah-Hartman {
26596fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK
26696fd7ce5SGreg Kroah-Hartman 	if (!tty) {
26789222e62SPeter Hurley 		pr_warn("(%d:%d): %s: NULL tty\n",
26896fd7ce5SGreg Kroah-Hartman 			imajor(inode), iminor(inode), routine);
26996fd7ce5SGreg Kroah-Hartman 		return 1;
27096fd7ce5SGreg Kroah-Hartman 	}
27196fd7ce5SGreg Kroah-Hartman 	if (tty->magic != TTY_MAGIC) {
27289222e62SPeter Hurley 		pr_warn("(%d:%d): %s: bad magic number\n",
27396fd7ce5SGreg Kroah-Hartman 			imajor(inode), iminor(inode), routine);
27496fd7ce5SGreg Kroah-Hartman 		return 1;
27596fd7ce5SGreg Kroah-Hartman 	}
27696fd7ce5SGreg Kroah-Hartman #endif
27796fd7ce5SGreg Kroah-Hartman 	return 0;
27896fd7ce5SGreg Kroah-Hartman }
27996fd7ce5SGreg Kroah-Hartman 
280deb287e7SPeter Hurley /* Caller must hold tty_lock */
28196fd7ce5SGreg Kroah-Hartman static int check_tty_count(struct tty_struct *tty, const char *routine)
28296fd7ce5SGreg Kroah-Hartman {
28396fd7ce5SGreg Kroah-Hartman #ifdef CHECK_TTY_COUNT
28496fd7ce5SGreg Kroah-Hartman 	struct list_head *p;
28596fd7ce5SGreg Kroah-Hartman 	int count = 0;
28696fd7ce5SGreg Kroah-Hartman 
2874a510969SPeter Hurley 	spin_lock(&tty->files_lock);
28896fd7ce5SGreg Kroah-Hartman 	list_for_each(p, &tty->tty_files) {
28996fd7ce5SGreg Kroah-Hartman 		count++;
29096fd7ce5SGreg Kroah-Hartman 	}
2914a510969SPeter Hurley 	spin_unlock(&tty->files_lock);
29296fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
29396fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_SLAVE &&
29496fd7ce5SGreg Kroah-Hartman 	    tty->link && tty->link->count)
29596fd7ce5SGreg Kroah-Hartman 		count++;
29696fd7ce5SGreg Kroah-Hartman 	if (tty->count != count) {
297339f36baSPeter Hurley 		tty_warn(tty, "%s: tty->count(%d) != #fd's(%d)\n",
298339f36baSPeter Hurley 			 routine, tty->count, count);
29996fd7ce5SGreg Kroah-Hartman 		return count;
30096fd7ce5SGreg Kroah-Hartman 	}
30196fd7ce5SGreg Kroah-Hartman #endif
30296fd7ce5SGreg Kroah-Hartman 	return 0;
30396fd7ce5SGreg Kroah-Hartman }
30496fd7ce5SGreg Kroah-Hartman 
30596fd7ce5SGreg Kroah-Hartman /**
30696fd7ce5SGreg Kroah-Hartman  *	get_tty_driver		-	find device of a tty
30796fd7ce5SGreg Kroah-Hartman  *	@dev_t: device identifier
30896fd7ce5SGreg Kroah-Hartman  *	@index: returns the index of the tty
30996fd7ce5SGreg Kroah-Hartman  *
31096fd7ce5SGreg Kroah-Hartman  *	This routine returns a tty driver structure, given a device number
31196fd7ce5SGreg Kroah-Hartman  *	and also passes back the index number.
31296fd7ce5SGreg Kroah-Hartman  *
31396fd7ce5SGreg Kroah-Hartman  *	Locking: caller must hold tty_mutex
31496fd7ce5SGreg Kroah-Hartman  */
31596fd7ce5SGreg Kroah-Hartman 
31696fd7ce5SGreg Kroah-Hartman static struct tty_driver *get_tty_driver(dev_t device, int *index)
31796fd7ce5SGreg Kroah-Hartman {
31896fd7ce5SGreg Kroah-Hartman 	struct tty_driver *p;
31996fd7ce5SGreg Kroah-Hartman 
32096fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(p, &tty_drivers, tty_drivers) {
32196fd7ce5SGreg Kroah-Hartman 		dev_t base = MKDEV(p->major, p->minor_start);
32296fd7ce5SGreg Kroah-Hartman 		if (device < base || device >= base + p->num)
32396fd7ce5SGreg Kroah-Hartman 			continue;
32496fd7ce5SGreg Kroah-Hartman 		*index = device - base;
32596fd7ce5SGreg Kroah-Hartman 		return tty_driver_kref_get(p);
32696fd7ce5SGreg Kroah-Hartman 	}
32796fd7ce5SGreg Kroah-Hartman 	return NULL;
32896fd7ce5SGreg Kroah-Hartman }
32996fd7ce5SGreg Kroah-Hartman 
33096fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
33196fd7ce5SGreg Kroah-Hartman 
33296fd7ce5SGreg Kroah-Hartman /**
33396fd7ce5SGreg Kroah-Hartman  *	tty_find_polling_driver	-	find device of a polled tty
33496fd7ce5SGreg Kroah-Hartman  *	@name: name string to match
33596fd7ce5SGreg Kroah-Hartman  *	@line: pointer to resulting tty line nr
33696fd7ce5SGreg Kroah-Hartman  *
33796fd7ce5SGreg Kroah-Hartman  *	This routine returns a tty driver structure, given a name
33896fd7ce5SGreg Kroah-Hartman  *	and the condition that the tty driver is capable of polled
33996fd7ce5SGreg Kroah-Hartman  *	operation.
34096fd7ce5SGreg Kroah-Hartman  */
34196fd7ce5SGreg Kroah-Hartman struct tty_driver *tty_find_polling_driver(char *name, int *line)
34296fd7ce5SGreg Kroah-Hartman {
34396fd7ce5SGreg Kroah-Hartman 	struct tty_driver *p, *res = NULL;
34496fd7ce5SGreg Kroah-Hartman 	int tty_line = 0;
34596fd7ce5SGreg Kroah-Hartman 	int len;
34696fd7ce5SGreg Kroah-Hartman 	char *str, *stp;
34796fd7ce5SGreg Kroah-Hartman 
34896fd7ce5SGreg Kroah-Hartman 	for (str = name; *str; str++)
34996fd7ce5SGreg Kroah-Hartman 		if ((*str >= '0' && *str <= '9') || *str == ',')
35096fd7ce5SGreg Kroah-Hartman 			break;
35196fd7ce5SGreg Kroah-Hartman 	if (!*str)
35296fd7ce5SGreg Kroah-Hartman 		return NULL;
35396fd7ce5SGreg Kroah-Hartman 
35496fd7ce5SGreg Kroah-Hartman 	len = str - name;
35596fd7ce5SGreg Kroah-Hartman 	tty_line = simple_strtoul(str, &str, 10);
35696fd7ce5SGreg Kroah-Hartman 
35796fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
35896fd7ce5SGreg Kroah-Hartman 	/* Search through the tty devices to look for a match */
35996fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(p, &tty_drivers, tty_drivers) {
36096fd7ce5SGreg Kroah-Hartman 		if (strncmp(name, p->name, len) != 0)
36196fd7ce5SGreg Kroah-Hartman 			continue;
36296fd7ce5SGreg Kroah-Hartman 		stp = str;
36396fd7ce5SGreg Kroah-Hartman 		if (*stp == ',')
36496fd7ce5SGreg Kroah-Hartman 			stp++;
36596fd7ce5SGreg Kroah-Hartman 		if (*stp == '\0')
36696fd7ce5SGreg Kroah-Hartman 			stp = NULL;
36796fd7ce5SGreg Kroah-Hartman 
36896fd7ce5SGreg Kroah-Hartman 		if (tty_line >= 0 && tty_line < p->num && p->ops &&
36996fd7ce5SGreg Kroah-Hartman 		    p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
37096fd7ce5SGreg Kroah-Hartman 			res = tty_driver_kref_get(p);
37196fd7ce5SGreg Kroah-Hartman 			*line = tty_line;
37296fd7ce5SGreg Kroah-Hartman 			break;
37396fd7ce5SGreg Kroah-Hartman 		}
37496fd7ce5SGreg Kroah-Hartman 	}
37596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
37696fd7ce5SGreg Kroah-Hartman 
37796fd7ce5SGreg Kroah-Hartman 	return res;
37896fd7ce5SGreg Kroah-Hartman }
37996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_find_polling_driver);
38096fd7ce5SGreg Kroah-Hartman #endif
38196fd7ce5SGreg Kroah-Hartman 
382e802ca0eSPeter Hurley static int is_ignored(int sig)
383e802ca0eSPeter Hurley {
384e802ca0eSPeter Hurley 	return (sigismember(&current->blocked, sig) ||
385e802ca0eSPeter Hurley 		current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
386e802ca0eSPeter Hurley }
387e802ca0eSPeter Hurley 
38896fd7ce5SGreg Kroah-Hartman /**
38996fd7ce5SGreg Kroah-Hartman  *	tty_check_change	-	check for POSIX terminal changes
39096fd7ce5SGreg Kroah-Hartman  *	@tty: tty to check
39196fd7ce5SGreg Kroah-Hartman  *
39296fd7ce5SGreg Kroah-Hartman  *	If we try to write to, or set the state of, a terminal and we're
39396fd7ce5SGreg Kroah-Hartman  *	not in the foreground, send a SIGTTOU.  If the signal is blocked or
39496fd7ce5SGreg Kroah-Hartman  *	ignored, go ahead and perform the operation.  (POSIX 7.2)
39596fd7ce5SGreg Kroah-Hartman  *
39696fd7ce5SGreg Kroah-Hartman  *	Locking: ctrl_lock
39796fd7ce5SGreg Kroah-Hartman  */
39896fd7ce5SGreg Kroah-Hartman 
3992812d9e9SPeter Hurley int __tty_check_change(struct tty_struct *tty, int sig)
40096fd7ce5SGreg Kroah-Hartman {
40196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
4022812d9e9SPeter Hurley 	struct pid *pgrp, *tty_pgrp;
40396fd7ce5SGreg Kroah-Hartman 	int ret = 0;
40496fd7ce5SGreg Kroah-Hartman 
40596fd7ce5SGreg Kroah-Hartman 	if (current->signal->tty != tty)
40696fd7ce5SGreg Kroah-Hartman 		return 0;
40796fd7ce5SGreg Kroah-Hartman 
4086719693cSPatrick Donnelly 	rcu_read_lock();
4096719693cSPatrick Donnelly 	pgrp = task_pgrp(current);
4106719693cSPatrick Donnelly 
41196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
4122812d9e9SPeter Hurley 	tty_pgrp = tty->pgrp;
41396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
4146719693cSPatrick Donnelly 
4152812d9e9SPeter Hurley 	if (tty_pgrp && pgrp != tty->pgrp) {
4162812d9e9SPeter Hurley 		if (is_ignored(sig)) {
4172812d9e9SPeter Hurley 			if (sig == SIGTTIN)
41896fd7ce5SGreg Kroah-Hartman 				ret = -EIO;
4192812d9e9SPeter Hurley 		} else if (is_current_pgrp_orphaned())
4202812d9e9SPeter Hurley 			ret = -EIO;
4212812d9e9SPeter Hurley 		else {
4222812d9e9SPeter Hurley 			kill_pgrp(pgrp, sig, 1);
42396fd7ce5SGreg Kroah-Hartman 			set_thread_flag(TIF_SIGPENDING);
42496fd7ce5SGreg Kroah-Hartman 			ret = -ERESTARTSYS;
4252812d9e9SPeter Hurley 		}
4262812d9e9SPeter Hurley 	}
4276719693cSPatrick Donnelly 	rcu_read_unlock();
4282812d9e9SPeter Hurley 
429339f36baSPeter Hurley 	if (!tty_pgrp)
430339f36baSPeter Hurley 		tty_warn(tty, "sig=%d, tty->pgrp == NULL!\n", sig);
4312812d9e9SPeter Hurley 
43296fd7ce5SGreg Kroah-Hartman 	return ret;
43396fd7ce5SGreg Kroah-Hartman }
43496fd7ce5SGreg Kroah-Hartman 
4352812d9e9SPeter Hurley int tty_check_change(struct tty_struct *tty)
4362812d9e9SPeter Hurley {
4372812d9e9SPeter Hurley 	return __tty_check_change(tty, SIGTTOU);
4382812d9e9SPeter Hurley }
43996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_check_change);
44096fd7ce5SGreg Kroah-Hartman 
44196fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
44296fd7ce5SGreg Kroah-Hartman 				size_t count, loff_t *ppos)
44396fd7ce5SGreg Kroah-Hartman {
44496fd7ce5SGreg Kroah-Hartman 	return 0;
44596fd7ce5SGreg Kroah-Hartman }
44696fd7ce5SGreg Kroah-Hartman 
44796fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
44896fd7ce5SGreg Kroah-Hartman 				 size_t count, loff_t *ppos)
44996fd7ce5SGreg Kroah-Hartman {
45096fd7ce5SGreg Kroah-Hartman 	return -EIO;
45196fd7ce5SGreg Kroah-Hartman }
45296fd7ce5SGreg Kroah-Hartman 
45396fd7ce5SGreg Kroah-Hartman /* No kernel lock held - none needed ;) */
45496fd7ce5SGreg Kroah-Hartman static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
45596fd7ce5SGreg Kroah-Hartman {
45696fd7ce5SGreg Kroah-Hartman 	return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
45796fd7ce5SGreg Kroah-Hartman }
45896fd7ce5SGreg Kroah-Hartman 
45996fd7ce5SGreg Kroah-Hartman static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
46096fd7ce5SGreg Kroah-Hartman 		unsigned long arg)
46196fd7ce5SGreg Kroah-Hartman {
46296fd7ce5SGreg Kroah-Hartman 	return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
46396fd7ce5SGreg Kroah-Hartman }
46496fd7ce5SGreg Kroah-Hartman 
46596fd7ce5SGreg Kroah-Hartman static long hung_up_tty_compat_ioctl(struct file *file,
46696fd7ce5SGreg Kroah-Hartman 				     unsigned int cmd, unsigned long arg)
46796fd7ce5SGreg Kroah-Hartman {
46896fd7ce5SGreg Kroah-Hartman 	return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
46996fd7ce5SGreg Kroah-Hartman }
47096fd7ce5SGreg Kroah-Hartman 
471f557474cSPeter Hurley static int hung_up_tty_fasync(int fd, struct file *file, int on)
472f557474cSPeter Hurley {
473f557474cSPeter Hurley 	return -ENOTTY;
474f557474cSPeter Hurley }
475f557474cSPeter Hurley 
47696fd7ce5SGreg Kroah-Hartman static const struct file_operations tty_fops = {
47796fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
47896fd7ce5SGreg Kroah-Hartman 	.read		= tty_read,
47996fd7ce5SGreg Kroah-Hartman 	.write		= tty_write,
48096fd7ce5SGreg Kroah-Hartman 	.poll		= tty_poll,
48196fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= tty_ioctl,
48296fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= tty_compat_ioctl,
48396fd7ce5SGreg Kroah-Hartman 	.open		= tty_open,
48496fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
48596fd7ce5SGreg Kroah-Hartman 	.fasync		= tty_fasync,
48696fd7ce5SGreg Kroah-Hartman };
48796fd7ce5SGreg Kroah-Hartman 
48896fd7ce5SGreg Kroah-Hartman static const struct file_operations console_fops = {
48996fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
49096fd7ce5SGreg Kroah-Hartman 	.read		= tty_read,
49196fd7ce5SGreg Kroah-Hartman 	.write		= redirected_tty_write,
49296fd7ce5SGreg Kroah-Hartman 	.poll		= tty_poll,
49396fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= tty_ioctl,
49496fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= tty_compat_ioctl,
49596fd7ce5SGreg Kroah-Hartman 	.open		= tty_open,
49696fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
49796fd7ce5SGreg Kroah-Hartman 	.fasync		= tty_fasync,
49896fd7ce5SGreg Kroah-Hartman };
49996fd7ce5SGreg Kroah-Hartman 
50096fd7ce5SGreg Kroah-Hartman static const struct file_operations hung_up_tty_fops = {
50196fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
50296fd7ce5SGreg Kroah-Hartman 	.read		= hung_up_tty_read,
50396fd7ce5SGreg Kroah-Hartman 	.write		= hung_up_tty_write,
50496fd7ce5SGreg Kroah-Hartman 	.poll		= hung_up_tty_poll,
50596fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= hung_up_tty_ioctl,
50696fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= hung_up_tty_compat_ioctl,
50796fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
508f557474cSPeter Hurley 	.fasync		= hung_up_tty_fasync,
50996fd7ce5SGreg Kroah-Hartman };
51096fd7ce5SGreg Kroah-Hartman 
51196fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(redirect_lock);
51296fd7ce5SGreg Kroah-Hartman static struct file *redirect;
51396fd7ce5SGreg Kroah-Hartman 
51411d9befdSPeter Hurley 
51511d9befdSPeter Hurley void proc_clear_tty(struct task_struct *p)
51611d9befdSPeter Hurley {
51711d9befdSPeter Hurley 	unsigned long flags;
51811d9befdSPeter Hurley 	struct tty_struct *tty;
51911d9befdSPeter Hurley 	spin_lock_irqsave(&p->sighand->siglock, flags);
52011d9befdSPeter Hurley 	tty = p->signal->tty;
52111d9befdSPeter Hurley 	p->signal->tty = NULL;
52211d9befdSPeter Hurley 	spin_unlock_irqrestore(&p->sighand->siglock, flags);
52311d9befdSPeter Hurley 	tty_kref_put(tty);
52411d9befdSPeter Hurley }
52511d9befdSPeter Hurley 
5262c411c11SPeter Hurley /**
5272c411c11SPeter Hurley  * proc_set_tty -  set the controlling terminal
5282c411c11SPeter Hurley  *
5292c411c11SPeter Hurley  * Only callable by the session leader and only if it does not already have
5302c411c11SPeter Hurley  * a controlling terminal.
5312c411c11SPeter Hurley  *
532e218eb32SPeter Hurley  * Caller must hold:  tty_lock()
533e218eb32SPeter Hurley  *		      a readlock on tasklist_lock
5342c411c11SPeter Hurley  *		      sighand lock
5352c411c11SPeter Hurley  */
536bce65f18SPeter Hurley static void __proc_set_tty(struct tty_struct *tty)
53711d9befdSPeter Hurley {
53811d9befdSPeter Hurley 	unsigned long flags;
539ae28fa72SPeter Hurley 
54011d9befdSPeter Hurley 	spin_lock_irqsave(&tty->ctrl_lock, flags);
541a361858dSPeter Hurley 	/*
542a361858dSPeter Hurley 	 * The session and fg pgrp references will be non-NULL if
543a361858dSPeter Hurley 	 * tiocsctty() is stealing the controlling tty
544a361858dSPeter Hurley 	 */
54511d9befdSPeter Hurley 	put_pid(tty->session);
54611d9befdSPeter Hurley 	put_pid(tty->pgrp);
547bce65f18SPeter Hurley 	tty->pgrp = get_pid(task_pgrp(current));
54811d9befdSPeter Hurley 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
549bce65f18SPeter Hurley 	tty->session = get_pid(task_session(current));
550bce65f18SPeter Hurley 	if (current->signal->tty) {
551e2dfa3d3SPeter Hurley 		tty_debug(tty, "current tty %s not NULL!!\n",
552e2dfa3d3SPeter Hurley 			  current->signal->tty->name);
553bce65f18SPeter Hurley 		tty_kref_put(current->signal->tty);
55411d9befdSPeter Hurley 	}
555bce65f18SPeter Hurley 	put_pid(current->signal->tty_old_pgrp);
556bce65f18SPeter Hurley 	current->signal->tty = tty_kref_get(tty);
557bce65f18SPeter Hurley 	current->signal->tty_old_pgrp = NULL;
55811d9befdSPeter Hurley }
55911d9befdSPeter Hurley 
560bce65f18SPeter Hurley static void proc_set_tty(struct tty_struct *tty)
56111d9befdSPeter Hurley {
562bce65f18SPeter Hurley 	spin_lock_irq(&current->sighand->siglock);
563bce65f18SPeter Hurley 	__proc_set_tty(tty);
564bce65f18SPeter Hurley 	spin_unlock_irq(&current->sighand->siglock);
56511d9befdSPeter Hurley }
56611d9befdSPeter Hurley 
56711d9befdSPeter Hurley struct tty_struct *get_current_tty(void)
56811d9befdSPeter Hurley {
56911d9befdSPeter Hurley 	struct tty_struct *tty;
57011d9befdSPeter Hurley 	unsigned long flags;
57111d9befdSPeter Hurley 
57211d9befdSPeter Hurley 	spin_lock_irqsave(&current->sighand->siglock, flags);
57311d9befdSPeter Hurley 	tty = tty_kref_get(current->signal->tty);
57411d9befdSPeter Hurley 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
57511d9befdSPeter Hurley 	return tty;
57611d9befdSPeter Hurley }
57711d9befdSPeter Hurley EXPORT_SYMBOL_GPL(get_current_tty);
57811d9befdSPeter Hurley 
57911d9befdSPeter Hurley static void session_clear_tty(struct pid *session)
58011d9befdSPeter Hurley {
58111d9befdSPeter Hurley 	struct task_struct *p;
58211d9befdSPeter Hurley 	do_each_pid_task(session, PIDTYPE_SID, p) {
58311d9befdSPeter Hurley 		proc_clear_tty(p);
58411d9befdSPeter Hurley 	} while_each_pid_task(session, PIDTYPE_SID, p);
58511d9befdSPeter Hurley }
58611d9befdSPeter Hurley 
58796fd7ce5SGreg Kroah-Hartman /**
58896fd7ce5SGreg Kroah-Hartman  *	tty_wakeup	-	request more data
58996fd7ce5SGreg Kroah-Hartman  *	@tty: terminal
59096fd7ce5SGreg Kroah-Hartman  *
59196fd7ce5SGreg Kroah-Hartman  *	Internal and external helper for wakeups of tty. This function
59296fd7ce5SGreg Kroah-Hartman  *	informs the line discipline if present that the driver is ready
59396fd7ce5SGreg Kroah-Hartman  *	to receive more output data.
59496fd7ce5SGreg Kroah-Hartman  */
59596fd7ce5SGreg Kroah-Hartman 
59696fd7ce5SGreg Kroah-Hartman void tty_wakeup(struct tty_struct *tty)
59796fd7ce5SGreg Kroah-Hartman {
59896fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
59996fd7ce5SGreg Kroah-Hartman 
60096fd7ce5SGreg Kroah-Hartman 	if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
60196fd7ce5SGreg Kroah-Hartman 		ld = tty_ldisc_ref(tty);
60296fd7ce5SGreg Kroah-Hartman 		if (ld) {
60396fd7ce5SGreg Kroah-Hartman 			if (ld->ops->write_wakeup)
60496fd7ce5SGreg Kroah-Hartman 				ld->ops->write_wakeup(tty);
60596fd7ce5SGreg Kroah-Hartman 			tty_ldisc_deref(ld);
60696fd7ce5SGreg Kroah-Hartman 		}
60796fd7ce5SGreg Kroah-Hartman 	}
60896fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
60996fd7ce5SGreg Kroah-Hartman }
61096fd7ce5SGreg Kroah-Hartman 
61196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_wakeup);
61296fd7ce5SGreg Kroah-Hartman 
61396fd7ce5SGreg Kroah-Hartman /**
614ea648a47SPeter Hurley  *	tty_signal_session_leader	- sends SIGHUP to session leader
615f91e2590SPeter Hurley  *	@tty		controlling tty
616f91e2590SPeter Hurley  *	@exit_session	if non-zero, signal all foreground group processes
617ea648a47SPeter Hurley  *
618f91e2590SPeter Hurley  *	Send SIGHUP and SIGCONT to the session leader and its process group.
619f91e2590SPeter Hurley  *	Optionally, signal all processes in the foreground process group.
620ea648a47SPeter Hurley  *
621ea648a47SPeter Hurley  *	Returns the number of processes in the session with this tty
622ea648a47SPeter Hurley  *	as their controlling terminal. This value is used to drop
623ea648a47SPeter Hurley  *	tty references for those processes.
624ea648a47SPeter Hurley  */
625f91e2590SPeter Hurley static int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
626ea648a47SPeter Hurley {
627ea648a47SPeter Hurley 	struct task_struct *p;
628ea648a47SPeter Hurley 	int refs = 0;
629f91e2590SPeter Hurley 	struct pid *tty_pgrp = NULL;
630ea648a47SPeter Hurley 
631ea648a47SPeter Hurley 	read_lock(&tasklist_lock);
632ea648a47SPeter Hurley 	if (tty->session) {
633ea648a47SPeter Hurley 		do_each_pid_task(tty->session, PIDTYPE_SID, p) {
634ea648a47SPeter Hurley 			spin_lock_irq(&p->sighand->siglock);
635ea648a47SPeter Hurley 			if (p->signal->tty == tty) {
636ea648a47SPeter Hurley 				p->signal->tty = NULL;
637ea648a47SPeter Hurley 				/* We defer the dereferences outside fo
638ea648a47SPeter Hurley 				   the tasklist lock */
639ea648a47SPeter Hurley 				refs++;
640ea648a47SPeter Hurley 			}
641ea648a47SPeter Hurley 			if (!p->signal->leader) {
642ea648a47SPeter Hurley 				spin_unlock_irq(&p->sighand->siglock);
643ea648a47SPeter Hurley 				continue;
644ea648a47SPeter Hurley 			}
645ea648a47SPeter Hurley 			__group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
646ea648a47SPeter Hurley 			__group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
647ea648a47SPeter Hurley 			put_pid(p->signal->tty_old_pgrp);  /* A noop */
648bc30c3b2SPeter Hurley 			spin_lock(&tty->ctrl_lock);
649f91e2590SPeter Hurley 			tty_pgrp = get_pid(tty->pgrp);
650ea648a47SPeter Hurley 			if (tty->pgrp)
651ea648a47SPeter Hurley 				p->signal->tty_old_pgrp = get_pid(tty->pgrp);
652bc30c3b2SPeter Hurley 			spin_unlock(&tty->ctrl_lock);
653ea648a47SPeter Hurley 			spin_unlock_irq(&p->sighand->siglock);
654ea648a47SPeter Hurley 		} while_each_pid_task(tty->session, PIDTYPE_SID, p);
655ea648a47SPeter Hurley 	}
656ea648a47SPeter Hurley 	read_unlock(&tasklist_lock);
657ea648a47SPeter Hurley 
658f91e2590SPeter Hurley 	if (tty_pgrp) {
659f91e2590SPeter Hurley 		if (exit_session)
660f91e2590SPeter Hurley 			kill_pgrp(tty_pgrp, SIGHUP, exit_session);
661f91e2590SPeter Hurley 		put_pid(tty_pgrp);
662f91e2590SPeter Hurley 	}
663f91e2590SPeter Hurley 
664ea648a47SPeter Hurley 	return refs;
665ea648a47SPeter Hurley }
666ea648a47SPeter Hurley 
667ea648a47SPeter Hurley /**
66896fd7ce5SGreg Kroah-Hartman  *	__tty_hangup		-	actual handler for hangup events
66996fd7ce5SGreg Kroah-Hartman  *	@work: tty device
67096fd7ce5SGreg Kroah-Hartman  *
671ef4f527cSKevin Cernekee  *	This can be called by a "kworker" kernel thread.  That is process
67296fd7ce5SGreg Kroah-Hartman  *	synchronous but doesn't hold any locks, so we need to make sure we
67396fd7ce5SGreg Kroah-Hartman  *	have the appropriate locks for what we're doing.
67496fd7ce5SGreg Kroah-Hartman  *
67596fd7ce5SGreg Kroah-Hartman  *	The hangup event clears any pending redirections onto the hung up
67696fd7ce5SGreg Kroah-Hartman  *	device. It ensures future writes will error and it does the needed
67796fd7ce5SGreg Kroah-Hartman  *	line discipline hangup and signal delivery. The tty object itself
67896fd7ce5SGreg Kroah-Hartman  *	remains intact.
67996fd7ce5SGreg Kroah-Hartman  *
68096fd7ce5SGreg Kroah-Hartman  *	Locking:
68196fd7ce5SGreg Kroah-Hartman  *		BTM
68296fd7ce5SGreg Kroah-Hartman  *		  redirect lock for undoing redirection
68396fd7ce5SGreg Kroah-Hartman  *		  file list lock for manipulating list of ttys
684137084bbSPeter Hurley  *		  tty_ldiscs_lock from called functions
6856a1c0680SPeter Hurley  *		  termios_rwsem resetting termios data
68696fd7ce5SGreg Kroah-Hartman  *		  tasklist_lock to walk task list for hangup event
68796fd7ce5SGreg Kroah-Hartman  *		    ->siglock to protect ->signal/->sighand
68896fd7ce5SGreg Kroah-Hartman  */
689f91e2590SPeter Hurley static void __tty_hangup(struct tty_struct *tty, int exit_session)
69096fd7ce5SGreg Kroah-Hartman {
69196fd7ce5SGreg Kroah-Hartman 	struct file *cons_filp = NULL;
69296fd7ce5SGreg Kroah-Hartman 	struct file *filp, *f = NULL;
69396fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv;
69496fd7ce5SGreg Kroah-Hartman 	int    closecount = 0, n;
695ea648a47SPeter Hurley 	int refs;
69696fd7ce5SGreg Kroah-Hartman 
69796fd7ce5SGreg Kroah-Hartman 	if (!tty)
69896fd7ce5SGreg Kroah-Hartman 		return;
69996fd7ce5SGreg Kroah-Hartman 
70096fd7ce5SGreg Kroah-Hartman 
70196fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
70296fd7ce5SGreg Kroah-Hartman 	if (redirect && file_tty(redirect) == tty) {
70396fd7ce5SGreg Kroah-Hartman 		f = redirect;
70496fd7ce5SGreg Kroah-Hartman 		redirect = NULL;
70596fd7ce5SGreg Kroah-Hartman 	}
70696fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
70796fd7ce5SGreg Kroah-Hartman 
70889c8d91eSAlan Cox 	tty_lock(tty);
70996fd7ce5SGreg Kroah-Hartman 
710cb50e523SPeter Hurley 	if (test_bit(TTY_HUPPED, &tty->flags)) {
711cb50e523SPeter Hurley 		tty_unlock(tty);
712cb50e523SPeter Hurley 		return;
713cb50e523SPeter Hurley 	}
714cb50e523SPeter Hurley 
71596fd7ce5SGreg Kroah-Hartman 	/* inuse_filps is protected by the single tty lock,
71696fd7ce5SGreg Kroah-Hartman 	   this really needs to change if we want to flush the
71796fd7ce5SGreg Kroah-Hartman 	   workqueue with the lock held */
71896fd7ce5SGreg Kroah-Hartman 	check_tty_count(tty, "tty_hangup");
71996fd7ce5SGreg Kroah-Hartman 
7204a510969SPeter Hurley 	spin_lock(&tty->files_lock);
72196fd7ce5SGreg Kroah-Hartman 	/* This breaks for file handles being sent over AF_UNIX sockets ? */
72296fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(priv, &tty->tty_files, list) {
72396fd7ce5SGreg Kroah-Hartman 		filp = priv->file;
72496fd7ce5SGreg Kroah-Hartman 		if (filp->f_op->write == redirected_tty_write)
72596fd7ce5SGreg Kroah-Hartman 			cons_filp = filp;
72696fd7ce5SGreg Kroah-Hartman 		if (filp->f_op->write != tty_write)
72796fd7ce5SGreg Kroah-Hartman 			continue;
72896fd7ce5SGreg Kroah-Hartman 		closecount++;
72996fd7ce5SGreg Kroah-Hartman 		__tty_fasync(-1, filp, 0);	/* can't block */
73096fd7ce5SGreg Kroah-Hartman 		filp->f_op = &hung_up_tty_fops;
73196fd7ce5SGreg Kroah-Hartman 	}
7324a510969SPeter Hurley 	spin_unlock(&tty->files_lock);
73396fd7ce5SGreg Kroah-Hartman 
73425fdf243SPeter Hurley 	refs = tty_signal_session_leader(tty, exit_session);
73525fdf243SPeter Hurley 	/* Account for the p->signal references we killed */
73625fdf243SPeter Hurley 	while (refs--)
73725fdf243SPeter Hurley 		tty_kref_put(tty);
73825fdf243SPeter Hurley 
739892d1fa7SPeter Hurley 	tty_ldisc_hangup(tty, cons_filp != NULL);
74096fd7ce5SGreg Kroah-Hartman 
74120cc225bSPeter Hurley 	spin_lock_irq(&tty->ctrl_lock);
74296fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_THROTTLED, &tty->flags);
74396fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
74496fd7ce5SGreg Kroah-Hartman 	put_pid(tty->session);
74596fd7ce5SGreg Kroah-Hartman 	put_pid(tty->pgrp);
74696fd7ce5SGreg Kroah-Hartman 	tty->session = NULL;
74796fd7ce5SGreg Kroah-Hartman 	tty->pgrp = NULL;
74896fd7ce5SGreg Kroah-Hartman 	tty->ctrl_status = 0;
74920cc225bSPeter Hurley 	spin_unlock_irq(&tty->ctrl_lock);
75096fd7ce5SGreg Kroah-Hartman 
75196fd7ce5SGreg Kroah-Hartman 	/*
75296fd7ce5SGreg Kroah-Hartman 	 * If one of the devices matches a console pointer, we
75396fd7ce5SGreg Kroah-Hartman 	 * cannot just call hangup() because that will cause
75496fd7ce5SGreg Kroah-Hartman 	 * tty->count and state->count to go out of sync.
75596fd7ce5SGreg Kroah-Hartman 	 * So we just call close() the right number of times.
75696fd7ce5SGreg Kroah-Hartman 	 */
75796fd7ce5SGreg Kroah-Hartman 	if (cons_filp) {
75896fd7ce5SGreg Kroah-Hartman 		if (tty->ops->close)
75996fd7ce5SGreg Kroah-Hartman 			for (n = 0; n < closecount; n++)
76096fd7ce5SGreg Kroah-Hartman 				tty->ops->close(tty, cons_filp);
76196fd7ce5SGreg Kroah-Hartman 	} else if (tty->ops->hangup)
7627c6d340fSPeter Hurley 		tty->ops->hangup(tty);
76396fd7ce5SGreg Kroah-Hartman 	/*
764892d1fa7SPeter Hurley 	 * We don't want to have driver/ldisc interactions beyond the ones
765892d1fa7SPeter Hurley 	 * we did here. The driver layer expects no calls after ->hangup()
766892d1fa7SPeter Hurley 	 * from the ldisc side, which is now guaranteed.
76796fd7ce5SGreg Kroah-Hartman 	 */
76896fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_HUPPED, &tty->flags);
76989c8d91eSAlan Cox 	tty_unlock(tty);
77096fd7ce5SGreg Kroah-Hartman 
77196fd7ce5SGreg Kroah-Hartman 	if (f)
77296fd7ce5SGreg Kroah-Hartman 		fput(f);
77396fd7ce5SGreg Kroah-Hartman }
77496fd7ce5SGreg Kroah-Hartman 
77596fd7ce5SGreg Kroah-Hartman static void do_tty_hangup(struct work_struct *work)
77696fd7ce5SGreg Kroah-Hartman {
77796fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
77896fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, hangup_work);
77996fd7ce5SGreg Kroah-Hartman 
780f91e2590SPeter Hurley 	__tty_hangup(tty, 0);
78196fd7ce5SGreg Kroah-Hartman }
78296fd7ce5SGreg Kroah-Hartman 
78396fd7ce5SGreg Kroah-Hartman /**
78496fd7ce5SGreg Kroah-Hartman  *	tty_hangup		-	trigger a hangup event
78596fd7ce5SGreg Kroah-Hartman  *	@tty: tty to hangup
78696fd7ce5SGreg Kroah-Hartman  *
78796fd7ce5SGreg Kroah-Hartman  *	A carrier loss (virtual or otherwise) has occurred on this like
78896fd7ce5SGreg Kroah-Hartman  *	schedule a hangup sequence to run after this event.
78996fd7ce5SGreg Kroah-Hartman  */
79096fd7ce5SGreg Kroah-Hartman 
79196fd7ce5SGreg Kroah-Hartman void tty_hangup(struct tty_struct *tty)
79296fd7ce5SGreg Kroah-Hartman {
793d435cefeSPeter Hurley 	tty_debug_hangup(tty, "hangup\n");
79496fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->hangup_work);
79596fd7ce5SGreg Kroah-Hartman }
79696fd7ce5SGreg Kroah-Hartman 
79796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hangup);
79896fd7ce5SGreg Kroah-Hartman 
79996fd7ce5SGreg Kroah-Hartman /**
80096fd7ce5SGreg Kroah-Hartman  *	tty_vhangup		-	process vhangup
80196fd7ce5SGreg Kroah-Hartman  *	@tty: tty to hangup
80296fd7ce5SGreg Kroah-Hartman  *
80396fd7ce5SGreg Kroah-Hartman  *	The user has asked via system call for the terminal to be hung up.
80496fd7ce5SGreg Kroah-Hartman  *	We do this synchronously so that when the syscall returns the process
80596fd7ce5SGreg Kroah-Hartman  *	is complete. That guarantee is necessary for security reasons.
80696fd7ce5SGreg Kroah-Hartman  */
80796fd7ce5SGreg Kroah-Hartman 
80896fd7ce5SGreg Kroah-Hartman void tty_vhangup(struct tty_struct *tty)
80996fd7ce5SGreg Kroah-Hartman {
810d435cefeSPeter Hurley 	tty_debug_hangup(tty, "vhangup\n");
811f91e2590SPeter Hurley 	__tty_hangup(tty, 0);
81296fd7ce5SGreg Kroah-Hartman }
81396fd7ce5SGreg Kroah-Hartman 
81496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_vhangup);
81596fd7ce5SGreg Kroah-Hartman 
81696fd7ce5SGreg Kroah-Hartman 
81796fd7ce5SGreg Kroah-Hartman /**
81896fd7ce5SGreg Kroah-Hartman  *	tty_vhangup_self	-	process vhangup for own ctty
81996fd7ce5SGreg Kroah-Hartman  *
82096fd7ce5SGreg Kroah-Hartman  *	Perform a vhangup on the current controlling tty
82196fd7ce5SGreg Kroah-Hartman  */
82296fd7ce5SGreg Kroah-Hartman 
82396fd7ce5SGreg Kroah-Hartman void tty_vhangup_self(void)
82496fd7ce5SGreg Kroah-Hartman {
82596fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
82696fd7ce5SGreg Kroah-Hartman 
82796fd7ce5SGreg Kroah-Hartman 	tty = get_current_tty();
82896fd7ce5SGreg Kroah-Hartman 	if (tty) {
82996fd7ce5SGreg Kroah-Hartman 		tty_vhangup(tty);
83096fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty);
83196fd7ce5SGreg Kroah-Hartman 	}
83296fd7ce5SGreg Kroah-Hartman }
83396fd7ce5SGreg Kroah-Hartman 
83496fd7ce5SGreg Kroah-Hartman /**
835f91e2590SPeter Hurley  *	tty_vhangup_session		-	hangup session leader exit
836f91e2590SPeter Hurley  *	@tty: tty to hangup
837f91e2590SPeter Hurley  *
838f91e2590SPeter Hurley  *	The session leader is exiting and hanging up its controlling terminal.
839f91e2590SPeter Hurley  *	Every process in the foreground process group is signalled SIGHUP.
840f91e2590SPeter Hurley  *
841f91e2590SPeter Hurley  *	We do this synchronously so that when the syscall returns the process
842f91e2590SPeter Hurley  *	is complete. That guarantee is necessary for security reasons.
843f91e2590SPeter Hurley  */
844f91e2590SPeter Hurley 
84544a459fdSChanglong Xie static void tty_vhangup_session(struct tty_struct *tty)
846f91e2590SPeter Hurley {
847d435cefeSPeter Hurley 	tty_debug_hangup(tty, "session hangup\n");
848f91e2590SPeter Hurley 	__tty_hangup(tty, 1);
849f91e2590SPeter Hurley }
850f91e2590SPeter Hurley 
851f91e2590SPeter Hurley /**
85296fd7ce5SGreg Kroah-Hartman  *	tty_hung_up_p		-	was tty hung up
85396fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer of tty
85496fd7ce5SGreg Kroah-Hartman  *
85596fd7ce5SGreg Kroah-Hartman  *	Return true if the tty has been subject to a vhangup or a carrier
85696fd7ce5SGreg Kroah-Hartman  *	loss
85796fd7ce5SGreg Kroah-Hartman  */
85896fd7ce5SGreg Kroah-Hartman 
85996fd7ce5SGreg Kroah-Hartman int tty_hung_up_p(struct file *filp)
86096fd7ce5SGreg Kroah-Hartman {
86196fd7ce5SGreg Kroah-Hartman 	return (filp->f_op == &hung_up_tty_fops);
86296fd7ce5SGreg Kroah-Hartman }
86396fd7ce5SGreg Kroah-Hartman 
86496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hung_up_p);
86596fd7ce5SGreg Kroah-Hartman 
86696fd7ce5SGreg Kroah-Hartman /**
86796fd7ce5SGreg Kroah-Hartman  *	disassociate_ctty	-	disconnect controlling tty
86896fd7ce5SGreg Kroah-Hartman  *	@on_exit: true if exiting so need to "hang up" the session
86996fd7ce5SGreg Kroah-Hartman  *
87096fd7ce5SGreg Kroah-Hartman  *	This function is typically called only by the session leader, when
87196fd7ce5SGreg Kroah-Hartman  *	it wants to disassociate itself from its controlling tty.
87296fd7ce5SGreg Kroah-Hartman  *
87396fd7ce5SGreg Kroah-Hartman  *	It performs the following functions:
87496fd7ce5SGreg Kroah-Hartman  * 	(1)  Sends a SIGHUP and SIGCONT to the foreground process group
87596fd7ce5SGreg Kroah-Hartman  * 	(2)  Clears the tty from being controlling the session
87696fd7ce5SGreg Kroah-Hartman  * 	(3)  Clears the controlling tty for all processes in the
87796fd7ce5SGreg Kroah-Hartman  * 		session group.
87896fd7ce5SGreg Kroah-Hartman  *
87996fd7ce5SGreg Kroah-Hartman  *	The argument on_exit is set to 1 if called when a process is
88096fd7ce5SGreg Kroah-Hartman  *	exiting; it is 0 if called by the ioctl TIOCNOTTY.
88196fd7ce5SGreg Kroah-Hartman  *
88296fd7ce5SGreg Kroah-Hartman  *	Locking:
88396fd7ce5SGreg Kroah-Hartman  *		BTM is taken for hysterical raisins, and held when
88496fd7ce5SGreg Kroah-Hartman  *		  called from no_tty().
88596fd7ce5SGreg Kroah-Hartman  *		  tty_mutex is taken to protect tty
88696fd7ce5SGreg Kroah-Hartman  *		  ->siglock is taken to protect ->signal/->sighand
88796fd7ce5SGreg Kroah-Hartman  *		  tasklist_lock is taken to walk process list for sessions
88896fd7ce5SGreg Kroah-Hartman  *		    ->siglock is taken to protect ->signal/->sighand
88996fd7ce5SGreg Kroah-Hartman  */
89096fd7ce5SGreg Kroah-Hartman 
89196fd7ce5SGreg Kroah-Hartman void disassociate_ctty(int on_exit)
89296fd7ce5SGreg Kroah-Hartman {
89396fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
89496fd7ce5SGreg Kroah-Hartman 
89596fd7ce5SGreg Kroah-Hartman 	if (!current->signal->leader)
89696fd7ce5SGreg Kroah-Hartman 		return;
89796fd7ce5SGreg Kroah-Hartman 
89896fd7ce5SGreg Kroah-Hartman 	tty = get_current_tty();
89996fd7ce5SGreg Kroah-Hartman 	if (tty) {
900f91e2590SPeter Hurley 		if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
901f91e2590SPeter Hurley 			tty_vhangup_session(tty);
902f91e2590SPeter Hurley 		} else {
903f91e2590SPeter Hurley 			struct pid *tty_pgrp = tty_get_pgrp(tty);
9041411dc4aSJiri Slaby 			if (tty_pgrp) {
9051411dc4aSJiri Slaby 				kill_pgrp(tty_pgrp, SIGHUP, on_exit);
90603e12617SOleg Nesterov 				if (!on_exit)
9071411dc4aSJiri Slaby 					kill_pgrp(tty_pgrp, SIGCONT, on_exit);
9081411dc4aSJiri Slaby 				put_pid(tty_pgrp);
9091411dc4aSJiri Slaby 			}
910f91e2590SPeter Hurley 		}
911f91e2590SPeter Hurley 		tty_kref_put(tty);
912f91e2590SPeter Hurley 
91396fd7ce5SGreg Kroah-Hartman 	} else if (on_exit) {
91496fd7ce5SGreg Kroah-Hartman 		struct pid *old_pgrp;
91596fd7ce5SGreg Kroah-Hartman 		spin_lock_irq(&current->sighand->siglock);
91696fd7ce5SGreg Kroah-Hartman 		old_pgrp = current->signal->tty_old_pgrp;
91796fd7ce5SGreg Kroah-Hartman 		current->signal->tty_old_pgrp = NULL;
91896fd7ce5SGreg Kroah-Hartman 		spin_unlock_irq(&current->sighand->siglock);
91996fd7ce5SGreg Kroah-Hartman 		if (old_pgrp) {
92096fd7ce5SGreg Kroah-Hartman 			kill_pgrp(old_pgrp, SIGHUP, on_exit);
92196fd7ce5SGreg Kroah-Hartman 			kill_pgrp(old_pgrp, SIGCONT, on_exit);
92296fd7ce5SGreg Kroah-Hartman 			put_pid(old_pgrp);
92396fd7ce5SGreg Kroah-Hartman 		}
92496fd7ce5SGreg Kroah-Hartman 		return;
92596fd7ce5SGreg Kroah-Hartman 	}
92696fd7ce5SGreg Kroah-Hartman 
92796fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&current->sighand->siglock);
92896fd7ce5SGreg Kroah-Hartman 	put_pid(current->signal->tty_old_pgrp);
92996fd7ce5SGreg Kroah-Hartman 	current->signal->tty_old_pgrp = NULL;
93096fd7ce5SGreg Kroah-Hartman 
931c70dbb1eSChen Tingjie 	tty = tty_kref_get(current->signal->tty);
93296fd7ce5SGreg Kroah-Hartman 	if (tty) {
93396fd7ce5SGreg Kroah-Hartman 		unsigned long flags;
93496fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&tty->ctrl_lock, flags);
93596fd7ce5SGreg Kroah-Hartman 		put_pid(tty->session);
93696fd7ce5SGreg Kroah-Hartman 		put_pid(tty->pgrp);
93796fd7ce5SGreg Kroah-Hartman 		tty->session = NULL;
93896fd7ce5SGreg Kroah-Hartman 		tty->pgrp = NULL;
93996fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
94096fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty);
941accff793SPeter Hurley 	} else
942accff793SPeter Hurley 		tty_debug_hangup(tty, "no current tty\n");
94396fd7ce5SGreg Kroah-Hartman 
944c70dbb1eSChen Tingjie 	spin_unlock_irq(&current->sighand->siglock);
94596fd7ce5SGreg Kroah-Hartman 	/* Now clear signal->tty under the lock */
94696fd7ce5SGreg Kroah-Hartman 	read_lock(&tasklist_lock);
94796fd7ce5SGreg Kroah-Hartman 	session_clear_tty(task_session(current));
94896fd7ce5SGreg Kroah-Hartman 	read_unlock(&tasklist_lock);
94996fd7ce5SGreg Kroah-Hartman }
95096fd7ce5SGreg Kroah-Hartman 
95196fd7ce5SGreg Kroah-Hartman /**
95296fd7ce5SGreg Kroah-Hartman  *
95396fd7ce5SGreg Kroah-Hartman  *	no_tty	- Ensure the current process does not have a controlling tty
95496fd7ce5SGreg Kroah-Hartman  */
95596fd7ce5SGreg Kroah-Hartman void no_tty(void)
95696fd7ce5SGreg Kroah-Hartman {
9573af502b9SAlan Cox 	/* FIXME: Review locking here. The tty_lock never covered any race
9583af502b9SAlan Cox 	   between a new association and proc_clear_tty but possible we need
9593af502b9SAlan Cox 	   to protect against this anyway */
96096fd7ce5SGreg Kroah-Hartman 	struct task_struct *tsk = current;
96196fd7ce5SGreg Kroah-Hartman 	disassociate_ctty(0);
96296fd7ce5SGreg Kroah-Hartman 	proc_clear_tty(tsk);
96396fd7ce5SGreg Kroah-Hartman }
96496fd7ce5SGreg Kroah-Hartman 
96596fd7ce5SGreg Kroah-Hartman 
96696fd7ce5SGreg Kroah-Hartman /**
96796fd7ce5SGreg Kroah-Hartman  *	stop_tty	-	propagate flow control
96896fd7ce5SGreg Kroah-Hartman  *	@tty: tty to stop
96996fd7ce5SGreg Kroah-Hartman  *
97001adc807SPeter Hurley  *	Perform flow control to the driver. May be called
97196fd7ce5SGreg Kroah-Hartman  *	on an already stopped device and will not re-call the driver
97296fd7ce5SGreg Kroah-Hartman  *	method.
97396fd7ce5SGreg Kroah-Hartman  *
97496fd7ce5SGreg Kroah-Hartman  *	This functionality is used by both the line disciplines for
97596fd7ce5SGreg Kroah-Hartman  *	halting incoming flow and by the driver. It may therefore be
97696fd7ce5SGreg Kroah-Hartman  *	called from any context, may be under the tty atomic_write_lock
97796fd7ce5SGreg Kroah-Hartman  *	but not always.
97896fd7ce5SGreg Kroah-Hartman  *
97996fd7ce5SGreg Kroah-Hartman  *	Locking:
980f9e053dcSPeter Hurley  *		flow_lock
98196fd7ce5SGreg Kroah-Hartman  */
98296fd7ce5SGreg Kroah-Hartman 
983f9e053dcSPeter Hurley void __stop_tty(struct tty_struct *tty)
98496fd7ce5SGreg Kroah-Hartman {
985f9e053dcSPeter Hurley 	if (tty->stopped)
98696fd7ce5SGreg Kroah-Hartman 		return;
98796fd7ce5SGreg Kroah-Hartman 	tty->stopped = 1;
98896fd7ce5SGreg Kroah-Hartman 	if (tty->ops->stop)
989c961bfb1SPeter Hurley 		tty->ops->stop(tty);
99096fd7ce5SGreg Kroah-Hartman }
99196fd7ce5SGreg Kroah-Hartman 
992f9e053dcSPeter Hurley void stop_tty(struct tty_struct *tty)
993f9e053dcSPeter Hurley {
994f9e053dcSPeter Hurley 	unsigned long flags;
995f9e053dcSPeter Hurley 
996f9e053dcSPeter Hurley 	spin_lock_irqsave(&tty->flow_lock, flags);
997f9e053dcSPeter Hurley 	__stop_tty(tty);
998f9e053dcSPeter Hurley 	spin_unlock_irqrestore(&tty->flow_lock, flags);
999f9e053dcSPeter Hurley }
100096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(stop_tty);
100196fd7ce5SGreg Kroah-Hartman 
100296fd7ce5SGreg Kroah-Hartman /**
100396fd7ce5SGreg Kroah-Hartman  *	start_tty	-	propagate flow control
100496fd7ce5SGreg Kroah-Hartman  *	@tty: tty to start
100596fd7ce5SGreg Kroah-Hartman  *
100601adc807SPeter Hurley  *	Start a tty that has been stopped if at all possible. If this
100701adc807SPeter Hurley  *	tty was previous stopped and is now being started, the driver
100801adc807SPeter Hurley  *	start method is invoked and the line discipline woken.
100996fd7ce5SGreg Kroah-Hartman  *
101096fd7ce5SGreg Kroah-Hartman  *	Locking:
1011f9e053dcSPeter Hurley  *		flow_lock
101296fd7ce5SGreg Kroah-Hartman  */
101396fd7ce5SGreg Kroah-Hartman 
1014f9e053dcSPeter Hurley void __start_tty(struct tty_struct *tty)
101596fd7ce5SGreg Kroah-Hartman {
1016f9e053dcSPeter Hurley 	if (!tty->stopped || tty->flow_stopped)
101796fd7ce5SGreg Kroah-Hartman 		return;
101896fd7ce5SGreg Kroah-Hartman 	tty->stopped = 0;
101996fd7ce5SGreg Kroah-Hartman 	if (tty->ops->start)
1020c961bfb1SPeter Hurley 		tty->ops->start(tty);
102196fd7ce5SGreg Kroah-Hartman 	tty_wakeup(tty);
102296fd7ce5SGreg Kroah-Hartman }
102396fd7ce5SGreg Kroah-Hartman 
1024f9e053dcSPeter Hurley void start_tty(struct tty_struct *tty)
1025f9e053dcSPeter Hurley {
1026f9e053dcSPeter Hurley 	unsigned long flags;
1027f9e053dcSPeter Hurley 
1028f9e053dcSPeter Hurley 	spin_lock_irqsave(&tty->flow_lock, flags);
1029f9e053dcSPeter Hurley 	__start_tty(tty);
1030f9e053dcSPeter Hurley 	spin_unlock_irqrestore(&tty->flow_lock, flags);
1031f9e053dcSPeter Hurley }
103296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(start_tty);
103396fd7ce5SGreg Kroah-Hartman 
103437b7f3c7SJiri Slaby static void tty_update_time(struct timespec *time)
103537b7f3c7SJiri Slaby {
1036f0bf0bd0SJiri Slaby 	unsigned long sec = get_seconds();
1037fbf47635SGreg Kroah-Hartman 
1038fbf47635SGreg Kroah-Hartman 	/*
1039fbf47635SGreg Kroah-Hartman 	 * We only care if the two values differ in anything other than the
1040fbf47635SGreg Kroah-Hartman 	 * lower three bits (i.e every 8 seconds).  If so, then we can update
1041fbf47635SGreg Kroah-Hartman 	 * the time of the tty device, otherwise it could be construded as a
1042fbf47635SGreg Kroah-Hartman 	 * security leak to let userspace know the exact timing of the tty.
1043fbf47635SGreg Kroah-Hartman 	 */
1044fbf47635SGreg Kroah-Hartman 	if ((sec ^ time->tv_sec) & ~7)
104537b7f3c7SJiri Slaby 		time->tv_sec = sec;
104637b7f3c7SJiri Slaby }
104737b7f3c7SJiri Slaby 
104896fd7ce5SGreg Kroah-Hartman /**
104996fd7ce5SGreg Kroah-Hartman  *	tty_read	-	read method for tty device files
105096fd7ce5SGreg Kroah-Hartman  *	@file: pointer to tty file
105196fd7ce5SGreg Kroah-Hartman  *	@buf: user buffer
105296fd7ce5SGreg Kroah-Hartman  *	@count: size of user buffer
105396fd7ce5SGreg Kroah-Hartman  *	@ppos: unused
105496fd7ce5SGreg Kroah-Hartman  *
105596fd7ce5SGreg Kroah-Hartman  *	Perform the read system call function on this terminal device. Checks
105696fd7ce5SGreg Kroah-Hartman  *	for hung up devices before calling the line discipline method.
105796fd7ce5SGreg Kroah-Hartman  *
105896fd7ce5SGreg Kroah-Hartman  *	Locking:
105996fd7ce5SGreg Kroah-Hartman  *		Locks the line discipline internally while needed. Multiple
106096fd7ce5SGreg Kroah-Hartman  *	read calls may be outstanding in parallel.
106196fd7ce5SGreg Kroah-Hartman  */
106296fd7ce5SGreg Kroah-Hartman 
106396fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
106496fd7ce5SGreg Kroah-Hartman 			loff_t *ppos)
106596fd7ce5SGreg Kroah-Hartman {
106696fd7ce5SGreg Kroah-Hartman 	int i;
106737b7f3c7SJiri Slaby 	struct inode *inode = file_inode(file);
106896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
106996fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
107096fd7ce5SGreg Kroah-Hartman 
107137b7f3c7SJiri Slaby 	if (tty_paranoia_check(tty, inode, "tty_read"))
107296fd7ce5SGreg Kroah-Hartman 		return -EIO;
107396fd7ce5SGreg Kroah-Hartman 	if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
107496fd7ce5SGreg Kroah-Hartman 		return -EIO;
107596fd7ce5SGreg Kroah-Hartman 
107696fd7ce5SGreg Kroah-Hartman 	/* We want to wait for the line discipline to sort out in this
107796fd7ce5SGreg Kroah-Hartman 	   situation */
107896fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
1079e55afd11SPeter Hurley 	if (!ld)
1080e55afd11SPeter Hurley 		return hung_up_tty_read(file, buf, count, ppos);
108196fd7ce5SGreg Kroah-Hartman 	if (ld->ops->read)
1082c961bfb1SPeter Hurley 		i = ld->ops->read(tty, file, buf, count);
108396fd7ce5SGreg Kroah-Hartman 	else
108496fd7ce5SGreg Kroah-Hartman 		i = -EIO;
108596fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
1086b0de59b5SJiri Slaby 
108737b7f3c7SJiri Slaby 	if (i > 0)
108837b7f3c7SJiri Slaby 		tty_update_time(&inode->i_atime);
108937b7f3c7SJiri Slaby 
109096fd7ce5SGreg Kroah-Hartman 	return i;
109196fd7ce5SGreg Kroah-Hartman }
109296fd7ce5SGreg Kroah-Hartman 
1093136d5258SPeter Hurley static void tty_write_unlock(struct tty_struct *tty)
109496fd7ce5SGreg Kroah-Hartman {
109596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->atomic_write_lock);
109696fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
109796fd7ce5SGreg Kroah-Hartman }
109896fd7ce5SGreg Kroah-Hartman 
1099136d5258SPeter Hurley static int tty_write_lock(struct tty_struct *tty, int ndelay)
110096fd7ce5SGreg Kroah-Hartman {
110196fd7ce5SGreg Kroah-Hartman 	if (!mutex_trylock(&tty->atomic_write_lock)) {
110296fd7ce5SGreg Kroah-Hartman 		if (ndelay)
110396fd7ce5SGreg Kroah-Hartman 			return -EAGAIN;
110496fd7ce5SGreg Kroah-Hartman 		if (mutex_lock_interruptible(&tty->atomic_write_lock))
110596fd7ce5SGreg Kroah-Hartman 			return -ERESTARTSYS;
110696fd7ce5SGreg Kroah-Hartman 	}
110796fd7ce5SGreg Kroah-Hartman 	return 0;
110896fd7ce5SGreg Kroah-Hartman }
110996fd7ce5SGreg Kroah-Hartman 
111096fd7ce5SGreg Kroah-Hartman /*
111196fd7ce5SGreg Kroah-Hartman  * Split writes up in sane blocksizes to avoid
111296fd7ce5SGreg Kroah-Hartman  * denial-of-service type attacks
111396fd7ce5SGreg Kroah-Hartman  */
111496fd7ce5SGreg Kroah-Hartman static inline ssize_t do_tty_write(
111596fd7ce5SGreg Kroah-Hartman 	ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
111696fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty,
111796fd7ce5SGreg Kroah-Hartman 	struct file *file,
111896fd7ce5SGreg Kroah-Hartman 	const char __user *buf,
111996fd7ce5SGreg Kroah-Hartman 	size_t count)
112096fd7ce5SGreg Kroah-Hartman {
112196fd7ce5SGreg Kroah-Hartman 	ssize_t ret, written = 0;
112296fd7ce5SGreg Kroah-Hartman 	unsigned int chunk;
112396fd7ce5SGreg Kroah-Hartman 
112496fd7ce5SGreg Kroah-Hartman 	ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
112596fd7ce5SGreg Kroah-Hartman 	if (ret < 0)
112696fd7ce5SGreg Kroah-Hartman 		return ret;
112796fd7ce5SGreg Kroah-Hartman 
112896fd7ce5SGreg Kroah-Hartman 	/*
112996fd7ce5SGreg Kroah-Hartman 	 * We chunk up writes into a temporary buffer. This
113096fd7ce5SGreg Kroah-Hartman 	 * simplifies low-level drivers immensely, since they
113196fd7ce5SGreg Kroah-Hartman 	 * don't have locking issues and user mode accesses.
113296fd7ce5SGreg Kroah-Hartman 	 *
113396fd7ce5SGreg Kroah-Hartman 	 * But if TTY_NO_WRITE_SPLIT is set, we should use a
113496fd7ce5SGreg Kroah-Hartman 	 * big chunk-size..
113596fd7ce5SGreg Kroah-Hartman 	 *
113696fd7ce5SGreg Kroah-Hartman 	 * The default chunk-size is 2kB, because the NTTY
113796fd7ce5SGreg Kroah-Hartman 	 * layer has problems with bigger chunks. It will
113896fd7ce5SGreg Kroah-Hartman 	 * claim to be able to handle more characters than
113996fd7ce5SGreg Kroah-Hartman 	 * it actually does.
114096fd7ce5SGreg Kroah-Hartman 	 *
114196fd7ce5SGreg Kroah-Hartman 	 * FIXME: This can probably go away now except that 64K chunks
114296fd7ce5SGreg Kroah-Hartman 	 * are too likely to fail unless switched to vmalloc...
114396fd7ce5SGreg Kroah-Hartman 	 */
114496fd7ce5SGreg Kroah-Hartman 	chunk = 2048;
114596fd7ce5SGreg Kroah-Hartman 	if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
114696fd7ce5SGreg Kroah-Hartman 		chunk = 65536;
114796fd7ce5SGreg Kroah-Hartman 	if (count < chunk)
114896fd7ce5SGreg Kroah-Hartman 		chunk = count;
114996fd7ce5SGreg Kroah-Hartman 
115096fd7ce5SGreg Kroah-Hartman 	/* write_buf/write_cnt is protected by the atomic_write_lock mutex */
115196fd7ce5SGreg Kroah-Hartman 	if (tty->write_cnt < chunk) {
115296fd7ce5SGreg Kroah-Hartman 		unsigned char *buf_chunk;
115396fd7ce5SGreg Kroah-Hartman 
115496fd7ce5SGreg Kroah-Hartman 		if (chunk < 1024)
115596fd7ce5SGreg Kroah-Hartman 			chunk = 1024;
115696fd7ce5SGreg Kroah-Hartman 
115796fd7ce5SGreg Kroah-Hartman 		buf_chunk = kmalloc(chunk, GFP_KERNEL);
115896fd7ce5SGreg Kroah-Hartman 		if (!buf_chunk) {
115996fd7ce5SGreg Kroah-Hartman 			ret = -ENOMEM;
116096fd7ce5SGreg Kroah-Hartman 			goto out;
116196fd7ce5SGreg Kroah-Hartman 		}
116296fd7ce5SGreg Kroah-Hartman 		kfree(tty->write_buf);
116396fd7ce5SGreg Kroah-Hartman 		tty->write_cnt = chunk;
116496fd7ce5SGreg Kroah-Hartman 		tty->write_buf = buf_chunk;
116596fd7ce5SGreg Kroah-Hartman 	}
116696fd7ce5SGreg Kroah-Hartman 
116796fd7ce5SGreg Kroah-Hartman 	/* Do the write .. */
116896fd7ce5SGreg Kroah-Hartman 	for (;;) {
116996fd7ce5SGreg Kroah-Hartman 		size_t size = count;
117096fd7ce5SGreg Kroah-Hartman 		if (size > chunk)
117196fd7ce5SGreg Kroah-Hartman 			size = chunk;
117296fd7ce5SGreg Kroah-Hartman 		ret = -EFAULT;
117396fd7ce5SGreg Kroah-Hartman 		if (copy_from_user(tty->write_buf, buf, size))
117496fd7ce5SGreg Kroah-Hartman 			break;
117596fd7ce5SGreg Kroah-Hartman 		ret = write(tty, file, tty->write_buf, size);
117696fd7ce5SGreg Kroah-Hartman 		if (ret <= 0)
117796fd7ce5SGreg Kroah-Hartman 			break;
117896fd7ce5SGreg Kroah-Hartman 		written += ret;
117996fd7ce5SGreg Kroah-Hartman 		buf += ret;
118096fd7ce5SGreg Kroah-Hartman 		count -= ret;
118196fd7ce5SGreg Kroah-Hartman 		if (!count)
118296fd7ce5SGreg Kroah-Hartman 			break;
118396fd7ce5SGreg Kroah-Hartman 		ret = -ERESTARTSYS;
118496fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
118596fd7ce5SGreg Kroah-Hartman 			break;
118696fd7ce5SGreg Kroah-Hartman 		cond_resched();
118796fd7ce5SGreg Kroah-Hartman 	}
118837b7f3c7SJiri Slaby 	if (written) {
118937b7f3c7SJiri Slaby 		tty_update_time(&file_inode(file)->i_mtime);
119096fd7ce5SGreg Kroah-Hartman 		ret = written;
119137b7f3c7SJiri Slaby 	}
119296fd7ce5SGreg Kroah-Hartman out:
119396fd7ce5SGreg Kroah-Hartman 	tty_write_unlock(tty);
119496fd7ce5SGreg Kroah-Hartman 	return ret;
119596fd7ce5SGreg Kroah-Hartman }
119696fd7ce5SGreg Kroah-Hartman 
119796fd7ce5SGreg Kroah-Hartman /**
119896fd7ce5SGreg Kroah-Hartman  * tty_write_message - write a message to a certain tty, not just the console.
119996fd7ce5SGreg Kroah-Hartman  * @tty: the destination tty_struct
120096fd7ce5SGreg Kroah-Hartman  * @msg: the message to write
120196fd7ce5SGreg Kroah-Hartman  *
120296fd7ce5SGreg Kroah-Hartman  * This is used for messages that need to be redirected to a specific tty.
120396fd7ce5SGreg Kroah-Hartman  * We don't put it into the syslog queue right now maybe in the future if
120496fd7ce5SGreg Kroah-Hartman  * really needed.
120596fd7ce5SGreg Kroah-Hartman  *
120696fd7ce5SGreg Kroah-Hartman  * We must still hold the BTM and test the CLOSING flag for the moment.
120796fd7ce5SGreg Kroah-Hartman  */
120896fd7ce5SGreg Kroah-Hartman 
120996fd7ce5SGreg Kroah-Hartman void tty_write_message(struct tty_struct *tty, char *msg)
121096fd7ce5SGreg Kroah-Hartman {
121196fd7ce5SGreg Kroah-Hartman 	if (tty) {
121296fd7ce5SGreg Kroah-Hartman 		mutex_lock(&tty->atomic_write_lock);
121389c8d91eSAlan Cox 		tty_lock(tty);
12144b41b953SPeter Hurley 		if (tty->ops->write && tty->count > 0)
121596fd7ce5SGreg Kroah-Hartman 			tty->ops->write(tty, msg, strlen(msg));
121689c8d91eSAlan Cox 		tty_unlock(tty);
121796fd7ce5SGreg Kroah-Hartman 		tty_write_unlock(tty);
121896fd7ce5SGreg Kroah-Hartman 	}
121996fd7ce5SGreg Kroah-Hartman 	return;
122096fd7ce5SGreg Kroah-Hartman }
122196fd7ce5SGreg Kroah-Hartman 
122296fd7ce5SGreg Kroah-Hartman 
122396fd7ce5SGreg Kroah-Hartman /**
122496fd7ce5SGreg Kroah-Hartman  *	tty_write		-	write method for tty device file
122596fd7ce5SGreg Kroah-Hartman  *	@file: tty file pointer
122696fd7ce5SGreg Kroah-Hartman  *	@buf: user data to write
122796fd7ce5SGreg Kroah-Hartman  *	@count: bytes to write
122896fd7ce5SGreg Kroah-Hartman  *	@ppos: unused
122996fd7ce5SGreg Kroah-Hartman  *
123096fd7ce5SGreg Kroah-Hartman  *	Write data to a tty device via the line discipline.
123196fd7ce5SGreg Kroah-Hartman  *
123296fd7ce5SGreg Kroah-Hartman  *	Locking:
123396fd7ce5SGreg Kroah-Hartman  *		Locks the line discipline as required
123496fd7ce5SGreg Kroah-Hartman  *		Writes to the tty driver are serialized by the atomic_write_lock
123596fd7ce5SGreg Kroah-Hartman  *	and are then processed in chunks to the device. The line discipline
123696fd7ce5SGreg Kroah-Hartman  *	write method will not be invoked in parallel for each device.
123796fd7ce5SGreg Kroah-Hartman  */
123896fd7ce5SGreg Kroah-Hartman 
123996fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *file, const char __user *buf,
124096fd7ce5SGreg Kroah-Hartman 						size_t count, loff_t *ppos)
124196fd7ce5SGreg Kroah-Hartman {
124296fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
124396fd7ce5SGreg Kroah-Hartman  	struct tty_ldisc *ld;
124496fd7ce5SGreg Kroah-Hartman 	ssize_t ret;
124596fd7ce5SGreg Kroah-Hartman 
12466131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
124796fd7ce5SGreg Kroah-Hartman 		return -EIO;
124896fd7ce5SGreg Kroah-Hartman 	if (!tty || !tty->ops->write ||
124996fd7ce5SGreg Kroah-Hartman 		(test_bit(TTY_IO_ERROR, &tty->flags)))
125096fd7ce5SGreg Kroah-Hartman 			return -EIO;
125196fd7ce5SGreg Kroah-Hartman 	/* Short term debug to catch buggy drivers */
125296fd7ce5SGreg Kroah-Hartman 	if (tty->ops->write_room == NULL)
1253339f36baSPeter Hurley 		tty_err(tty, "missing write_room method\n");
125496fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
1255e55afd11SPeter Hurley 	if (!ld)
1256e55afd11SPeter Hurley 		return hung_up_tty_write(file, buf, count, ppos);
125796fd7ce5SGreg Kroah-Hartman 	if (!ld->ops->write)
125896fd7ce5SGreg Kroah-Hartman 		ret = -EIO;
125996fd7ce5SGreg Kroah-Hartman 	else
126096fd7ce5SGreg Kroah-Hartman 		ret = do_tty_write(ld->ops->write, tty, file, buf, count);
126196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
126296fd7ce5SGreg Kroah-Hartman 	return ret;
126396fd7ce5SGreg Kroah-Hartman }
126496fd7ce5SGreg Kroah-Hartman 
126596fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *file, const char __user *buf,
126696fd7ce5SGreg Kroah-Hartman 						size_t count, loff_t *ppos)
126796fd7ce5SGreg Kroah-Hartman {
126896fd7ce5SGreg Kroah-Hartman 	struct file *p = NULL;
126996fd7ce5SGreg Kroah-Hartman 
127096fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
1271cb0942b8SAl Viro 	if (redirect)
1272cb0942b8SAl Viro 		p = get_file(redirect);
127396fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
127496fd7ce5SGreg Kroah-Hartman 
127596fd7ce5SGreg Kroah-Hartman 	if (p) {
127696fd7ce5SGreg Kroah-Hartman 		ssize_t res;
127796fd7ce5SGreg Kroah-Hartman 		res = vfs_write(p, buf, count, &p->f_pos);
127896fd7ce5SGreg Kroah-Hartman 		fput(p);
127996fd7ce5SGreg Kroah-Hartman 		return res;
128096fd7ce5SGreg Kroah-Hartman 	}
128196fd7ce5SGreg Kroah-Hartman 	return tty_write(file, buf, count, ppos);
128296fd7ce5SGreg Kroah-Hartman }
128396fd7ce5SGreg Kroah-Hartman 
1284136d5258SPeter Hurley /**
1285136d5258SPeter Hurley  *	tty_send_xchar	-	send priority character
1286136d5258SPeter Hurley  *
1287136d5258SPeter Hurley  *	Send a high priority character to the tty even if stopped
1288136d5258SPeter Hurley  *
1289136d5258SPeter Hurley  *	Locking: none for xchar method, write ordering for write method.
1290136d5258SPeter Hurley  */
1291136d5258SPeter Hurley 
1292136d5258SPeter Hurley int tty_send_xchar(struct tty_struct *tty, char ch)
1293136d5258SPeter Hurley {
1294136d5258SPeter Hurley 	int	was_stopped = tty->stopped;
1295136d5258SPeter Hurley 
1296136d5258SPeter Hurley 	if (tty->ops->send_xchar) {
1297ee0c1a65SPeter Hurley 		down_read(&tty->termios_rwsem);
1298136d5258SPeter Hurley 		tty->ops->send_xchar(tty, ch);
1299ee0c1a65SPeter Hurley 		up_read(&tty->termios_rwsem);
1300136d5258SPeter Hurley 		return 0;
1301136d5258SPeter Hurley 	}
1302136d5258SPeter Hurley 
1303136d5258SPeter Hurley 	if (tty_write_lock(tty, 0) < 0)
1304136d5258SPeter Hurley 		return -ERESTARTSYS;
1305136d5258SPeter Hurley 
1306ee0c1a65SPeter Hurley 	down_read(&tty->termios_rwsem);
1307136d5258SPeter Hurley 	if (was_stopped)
1308136d5258SPeter Hurley 		start_tty(tty);
1309136d5258SPeter Hurley 	tty->ops->write(tty, &ch, 1);
1310136d5258SPeter Hurley 	if (was_stopped)
1311136d5258SPeter Hurley 		stop_tty(tty);
1312ee0c1a65SPeter Hurley 	up_read(&tty->termios_rwsem);
1313136d5258SPeter Hurley 	tty_write_unlock(tty);
1314136d5258SPeter Hurley 	return 0;
1315136d5258SPeter Hurley }
1316136d5258SPeter Hurley 
131796fd7ce5SGreg Kroah-Hartman static char ptychar[] = "pqrstuvwxyzabcde";
131896fd7ce5SGreg Kroah-Hartman 
131996fd7ce5SGreg Kroah-Hartman /**
132096fd7ce5SGreg Kroah-Hartman  *	pty_line_name	-	generate name for a pty
132196fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver in use
132296fd7ce5SGreg Kroah-Hartman  *	@index: the minor number
132396fd7ce5SGreg Kroah-Hartman  *	@p: output buffer of at least 6 bytes
132496fd7ce5SGreg Kroah-Hartman  *
132596fd7ce5SGreg Kroah-Hartman  *	Generate a name from a driver reference and write it to the output
132696fd7ce5SGreg Kroah-Hartman  *	buffer.
132796fd7ce5SGreg Kroah-Hartman  *
132896fd7ce5SGreg Kroah-Hartman  *	Locking: None
132996fd7ce5SGreg Kroah-Hartman  */
133096fd7ce5SGreg Kroah-Hartman static void pty_line_name(struct tty_driver *driver, int index, char *p)
133196fd7ce5SGreg Kroah-Hartman {
133296fd7ce5SGreg Kroah-Hartman 	int i = index + driver->name_base;
133396fd7ce5SGreg Kroah-Hartman 	/* ->name is initialized to "ttyp", but "tty" is expected */
133496fd7ce5SGreg Kroah-Hartman 	sprintf(p, "%s%c%x",
133596fd7ce5SGreg Kroah-Hartman 		driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
133696fd7ce5SGreg Kroah-Hartman 		ptychar[i >> 4 & 0xf], i & 0xf);
133796fd7ce5SGreg Kroah-Hartman }
133896fd7ce5SGreg Kroah-Hartman 
133996fd7ce5SGreg Kroah-Hartman /**
134096fd7ce5SGreg Kroah-Hartman  *	tty_line_name	-	generate name for a tty
134196fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver in use
134296fd7ce5SGreg Kroah-Hartman  *	@index: the minor number
134396fd7ce5SGreg Kroah-Hartman  *	@p: output buffer of at least 7 bytes
134496fd7ce5SGreg Kroah-Hartman  *
134596fd7ce5SGreg Kroah-Hartman  *	Generate a name from a driver reference and write it to the output
13465c0a2450SGreg Kroah-Hartman  *	buffer.
134796fd7ce5SGreg Kroah-Hartman  *
134896fd7ce5SGreg Kroah-Hartman  *	Locking: None
134996fd7ce5SGreg Kroah-Hartman  */
1350723abd87SHannes Reinecke static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
135196fd7ce5SGreg Kroah-Hartman {
13520019b408SJiri Slaby 	if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
1353723abd87SHannes Reinecke 		return sprintf(p, "%s", driver->name);
13540019b408SJiri Slaby 	else
1355723abd87SHannes Reinecke 		return sprintf(p, "%s%d", driver->name,
1356723abd87SHannes Reinecke 			       index + driver->name_base);
135796fd7ce5SGreg Kroah-Hartman }
135896fd7ce5SGreg Kroah-Hartman 
135996fd7ce5SGreg Kroah-Hartman /**
136096fd7ce5SGreg Kroah-Hartman  *	tty_driver_lookup_tty() - find an existing tty, if any
136196fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
136296fd7ce5SGreg Kroah-Hartman  *	@idx:	 the minor number
136396fd7ce5SGreg Kroah-Hartman  *
1364aa3cb814SPeter Hurley  *	Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1365aa3cb814SPeter Hurley  *	driver lookup() method returns an error.
136696fd7ce5SGreg Kroah-Hartman  *
1367aa3cb814SPeter Hurley  *	Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
136896fd7ce5SGreg Kroah-Hartman  */
136996fd7ce5SGreg Kroah-Hartman static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
137096fd7ce5SGreg Kroah-Hartman 		struct inode *inode, int idx)
137196fd7ce5SGreg Kroah-Hartman {
1372aa3cb814SPeter Hurley 	struct tty_struct *tty;
137396fd7ce5SGreg Kroah-Hartman 
1374aa3cb814SPeter Hurley 	if (driver->ops->lookup)
1375aa3cb814SPeter Hurley 		tty = driver->ops->lookup(driver, inode, idx);
1376aa3cb814SPeter Hurley 	else
1377aa3cb814SPeter Hurley 		tty = driver->ttys[idx];
1378aa3cb814SPeter Hurley 
1379aa3cb814SPeter Hurley 	if (!IS_ERR(tty))
1380aa3cb814SPeter Hurley 		tty_kref_get(tty);
1381aa3cb814SPeter Hurley 	return tty;
138296fd7ce5SGreg Kroah-Hartman }
138396fd7ce5SGreg Kroah-Hartman 
138496fd7ce5SGreg Kroah-Hartman /**
138596fd7ce5SGreg Kroah-Hartman  *	tty_init_termios	-  helper for termios setup
138696fd7ce5SGreg Kroah-Hartman  *	@tty: the tty to set up
138796fd7ce5SGreg Kroah-Hartman  *
138896fd7ce5SGreg Kroah-Hartman  *	Initialise the termios structures for this tty. Thus runs under
138996fd7ce5SGreg Kroah-Hartman  *	the tty_mutex currently so we can be relaxed about ordering.
139096fd7ce5SGreg Kroah-Hartman  */
139196fd7ce5SGreg Kroah-Hartman 
1392a3123fd0SPeter Hurley void tty_init_termios(struct tty_struct *tty)
139396fd7ce5SGreg Kroah-Hartman {
139496fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
139596fd7ce5SGreg Kroah-Hartman 	int idx = tty->index;
139696fd7ce5SGreg Kroah-Hartman 
139736b3c070SAlan Cox 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
139836b3c070SAlan Cox 		tty->termios = tty->driver->init_termios;
139936b3c070SAlan Cox 	else {
140036b3c070SAlan Cox 		/* Check for lazy saved data */
140196fd7ce5SGreg Kroah-Hartman 		tp = tty->driver->termios[idx];
1402ece53405SPeter Hurley 		if (tp != NULL) {
1403adc8d746SAlan Cox 			tty->termios = *tp;
1404ece53405SPeter Hurley 			tty->termios.c_line  = tty->driver->init_termios.c_line;
1405ece53405SPeter Hurley 		} else
140636b3c070SAlan Cox 			tty->termios = tty->driver->init_termios;
140736b3c070SAlan Cox 	}
140896fd7ce5SGreg Kroah-Hartman 	/* Compatibility until drivers always set this */
1409adc8d746SAlan Cox 	tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1410adc8d746SAlan Cox 	tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
141196fd7ce5SGreg Kroah-Hartman }
141296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_init_termios);
141396fd7ce5SGreg Kroah-Hartman 
141466d450e8SJiri Slaby int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
141566d450e8SJiri Slaby {
1416a3123fd0SPeter Hurley 	tty_init_termios(tty);
141766d450e8SJiri Slaby 	tty_driver_kref_get(driver);
141866d450e8SJiri Slaby 	tty->count++;
141966d450e8SJiri Slaby 	driver->ttys[tty->index] = tty;
142066d450e8SJiri Slaby 	return 0;
142166d450e8SJiri Slaby }
142266d450e8SJiri Slaby EXPORT_SYMBOL_GPL(tty_standard_install);
142366d450e8SJiri Slaby 
142496fd7ce5SGreg Kroah-Hartman /**
142596fd7ce5SGreg Kroah-Hartman  *	tty_driver_install_tty() - install a tty entry in the driver
142696fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
142796fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
142896fd7ce5SGreg Kroah-Hartman  *
142996fd7ce5SGreg Kroah-Hartman  *	Install a tty object into the driver tables. The tty->index field
143096fd7ce5SGreg Kroah-Hartman  *	will be set by the time this is called. This method is responsible
143196fd7ce5SGreg Kroah-Hartman  *	for ensuring any need additional structures are allocated and
143296fd7ce5SGreg Kroah-Hartman  *	configured.
143396fd7ce5SGreg Kroah-Hartman  *
143496fd7ce5SGreg Kroah-Hartman  *	Locking: tty_mutex for now
143596fd7ce5SGreg Kroah-Hartman  */
143696fd7ce5SGreg Kroah-Hartman static int tty_driver_install_tty(struct tty_driver *driver,
143796fd7ce5SGreg Kroah-Hartman 						struct tty_struct *tty)
143896fd7ce5SGreg Kroah-Hartman {
143966d450e8SJiri Slaby 	return driver->ops->install ? driver->ops->install(driver, tty) :
144066d450e8SJiri Slaby 		tty_standard_install(driver, tty);
144196fd7ce5SGreg Kroah-Hartman }
144296fd7ce5SGreg Kroah-Hartman 
144396fd7ce5SGreg Kroah-Hartman /**
144496fd7ce5SGreg Kroah-Hartman  *	tty_driver_remove_tty() - remove a tty from the driver tables
144596fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
144696fd7ce5SGreg Kroah-Hartman  *	@idx:	 the minor number
144796fd7ce5SGreg Kroah-Hartman  *
144896fd7ce5SGreg Kroah-Hartman  *	Remvoe a tty object from the driver tables. The tty->index field
144996fd7ce5SGreg Kroah-Hartman  *	will be set by the time this is called.
145096fd7ce5SGreg Kroah-Hartman  *
145196fd7ce5SGreg Kroah-Hartman  *	Locking: tty_mutex for now
145296fd7ce5SGreg Kroah-Hartman  */
145305de87edSPeter Hurley static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
145496fd7ce5SGreg Kroah-Hartman {
145596fd7ce5SGreg Kroah-Hartman 	if (driver->ops->remove)
145696fd7ce5SGreg Kroah-Hartman 		driver->ops->remove(driver, tty);
145796fd7ce5SGreg Kroah-Hartman 	else
145896fd7ce5SGreg Kroah-Hartman 		driver->ttys[tty->index] = NULL;
145996fd7ce5SGreg Kroah-Hartman }
146096fd7ce5SGreg Kroah-Hartman 
146196fd7ce5SGreg Kroah-Hartman /*
146296fd7ce5SGreg Kroah-Hartman  * 	tty_reopen()	- fast re-open of an open tty
146396fd7ce5SGreg Kroah-Hartman  * 	@tty	- the tty to open
146496fd7ce5SGreg Kroah-Hartman  *
146596fd7ce5SGreg Kroah-Hartman  *	Return 0 on success, -errno on error.
14665d93e748SPeter Hurley  *	Re-opens on master ptys are not allowed and return -EIO.
146796fd7ce5SGreg Kroah-Hartman  *
14685d93e748SPeter Hurley  *	Locking: Caller must hold tty_lock
146996fd7ce5SGreg Kroah-Hartman  */
147096fd7ce5SGreg Kroah-Hartman static int tty_reopen(struct tty_struct *tty)
147196fd7ce5SGreg Kroah-Hartman {
147296fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = tty->driver;
147396fd7ce5SGreg Kroah-Hartman 
147496fd7ce5SGreg Kroah-Hartman 	if (driver->type == TTY_DRIVER_TYPE_PTY &&
14755d93e748SPeter Hurley 	    driver->subtype == PTY_TYPE_MASTER)
147696fd7ce5SGreg Kroah-Hartman 		return -EIO;
147796fd7ce5SGreg Kroah-Hartman 
14787f22f6c9SPeter Hurley 	if (!tty->count)
14797f22f6c9SPeter Hurley 		return -EAGAIN;
14807f22f6c9SPeter Hurley 
148186f2c00fSPeter Hurley 	if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
148286f2c00fSPeter Hurley 		return -EBUSY;
148386f2c00fSPeter Hurley 
148496fd7ce5SGreg Kroah-Hartman 	tty->count++;
148596fd7ce5SGreg Kroah-Hartman 
1486892d1fa7SPeter Hurley 	if (!tty->ldisc)
1487892d1fa7SPeter Hurley 		return tty_ldisc_reinit(tty, tty->termios.c_line);
148896fd7ce5SGreg Kroah-Hartman 
148996fd7ce5SGreg Kroah-Hartman 	return 0;
149096fd7ce5SGreg Kroah-Hartman }
149196fd7ce5SGreg Kroah-Hartman 
149296fd7ce5SGreg Kroah-Hartman /**
149396fd7ce5SGreg Kroah-Hartman  *	tty_init_dev		-	initialise a tty device
149496fd7ce5SGreg Kroah-Hartman  *	@driver: tty driver we are opening a device on
149596fd7ce5SGreg Kroah-Hartman  *	@idx: device index
149696fd7ce5SGreg Kroah-Hartman  *	@ret_tty: returned tty structure
149796fd7ce5SGreg Kroah-Hartman  *
149896fd7ce5SGreg Kroah-Hartman  *	Prepare a tty device. This may not be a "new" clean device but
149996fd7ce5SGreg Kroah-Hartman  *	could also be an active device. The pty drivers require special
150096fd7ce5SGreg Kroah-Hartman  *	handling because of this.
150196fd7ce5SGreg Kroah-Hartman  *
150296fd7ce5SGreg Kroah-Hartman  *	Locking:
150396fd7ce5SGreg Kroah-Hartman  *		The function is called under the tty_mutex, which
150496fd7ce5SGreg Kroah-Hartman  *	protects us from the tty struct or driver itself going away.
150596fd7ce5SGreg Kroah-Hartman  *
150696fd7ce5SGreg Kroah-Hartman  *	On exit the tty device has the line discipline attached and
150796fd7ce5SGreg Kroah-Hartman  *	a reference count of 1. If a pair was created for pty/tty use
150896fd7ce5SGreg Kroah-Hartman  *	and the other was a pty master then it too has a reference count of 1.
150996fd7ce5SGreg Kroah-Hartman  *
151096fd7ce5SGreg Kroah-Hartman  * WSH 06/09/97: Rewritten to remove races and properly clean up after a
151196fd7ce5SGreg Kroah-Hartman  * failed open.  The new code protects the open with a mutex, so it's
151296fd7ce5SGreg Kroah-Hartman  * really quite straightforward.  The mutex locking can probably be
151396fd7ce5SGreg Kroah-Hartman  * relaxed for the (most common) case of reopening a tty.
151496fd7ce5SGreg Kroah-Hartman  */
151596fd7ce5SGreg Kroah-Hartman 
1516593a27c4SKonstantin Khlebnikov struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
151796fd7ce5SGreg Kroah-Hartman {
151896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
151996fd7ce5SGreg Kroah-Hartman 	int retval;
152096fd7ce5SGreg Kroah-Hartman 
152196fd7ce5SGreg Kroah-Hartman 	/*
152296fd7ce5SGreg Kroah-Hartman 	 * First time open is complex, especially for PTY devices.
152396fd7ce5SGreg Kroah-Hartman 	 * This code guarantees that either everything succeeds and the
152496fd7ce5SGreg Kroah-Hartman 	 * TTY is ready for operation, or else the table slots are vacated
152596fd7ce5SGreg Kroah-Hartman 	 * and the allocated memory released.  (Except that the termios
152696fd7ce5SGreg Kroah-Hartman 	 * and locked termios may be retained.)
152796fd7ce5SGreg Kroah-Hartman 	 */
152896fd7ce5SGreg Kroah-Hartman 
152996fd7ce5SGreg Kroah-Hartman 	if (!try_module_get(driver->owner))
153096fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-ENODEV);
153196fd7ce5SGreg Kroah-Hartman 
15322c964a2fSRasmus Villemoes 	tty = alloc_tty_struct(driver, idx);
1533d5543503SJiri Slaby 	if (!tty) {
1534d5543503SJiri Slaby 		retval = -ENOMEM;
1535d5543503SJiri Slaby 		goto err_module_put;
1536d5543503SJiri Slaby 	}
153796fd7ce5SGreg Kroah-Hartman 
153889c8d91eSAlan Cox 	tty_lock(tty);
153996fd7ce5SGreg Kroah-Hartman 	retval = tty_driver_install_tty(driver, tty);
1540d5543503SJiri Slaby 	if (retval < 0)
1541c8b710b3SPeter Hurley 		goto err_free_tty;
154296fd7ce5SGreg Kroah-Hartman 
154304831dc1SJiri Slaby 	if (!tty->port)
154404831dc1SJiri Slaby 		tty->port = driver->ports[idx];
154504831dc1SJiri Slaby 
15465d4121c0SJiri Slaby 	WARN_RATELIMIT(!tty->port,
15475d4121c0SJiri Slaby 			"%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
15485d4121c0SJiri Slaby 			__func__, tty->driver->name);
15495d4121c0SJiri Slaby 
1550967fab69SJiri Slaby 	tty->port->itty = tty;
1551967fab69SJiri Slaby 
155296fd7ce5SGreg Kroah-Hartman 	/*
155396fd7ce5SGreg Kroah-Hartman 	 * Structures all installed ... call the ldisc open routines.
155496fd7ce5SGreg Kroah-Hartman 	 * If we fail here just call release_tty to clean up.  No need
155596fd7ce5SGreg Kroah-Hartman 	 * to decrement the use counts, as release_tty doesn't care.
155696fd7ce5SGreg Kroah-Hartman 	 */
155796fd7ce5SGreg Kroah-Hartman 	retval = tty_ldisc_setup(tty, tty->link);
155896fd7ce5SGreg Kroah-Hartman 	if (retval)
1559d5543503SJiri Slaby 		goto err_release_tty;
156089c8d91eSAlan Cox 	/* Return the tty locked so that it cannot vanish under the caller */
156196fd7ce5SGreg Kroah-Hartman 	return tty;
156296fd7ce5SGreg Kroah-Hartman 
1563c8b710b3SPeter Hurley err_free_tty:
156489c8d91eSAlan Cox 	tty_unlock(tty);
1565d5543503SJiri Slaby 	free_tty_struct(tty);
1566d5543503SJiri Slaby err_module_put:
156796fd7ce5SGreg Kroah-Hartman 	module_put(driver->owner);
1568d5543503SJiri Slaby 	return ERR_PTR(retval);
156996fd7ce5SGreg Kroah-Hartman 
157096fd7ce5SGreg Kroah-Hartman 	/* call the tty release_tty routine to clean out this slot */
1571d5543503SJiri Slaby err_release_tty:
157289c8d91eSAlan Cox 	tty_unlock(tty);
1573339f36baSPeter Hurley 	tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1574339f36baSPeter Hurley 			     retval, idx);
157596fd7ce5SGreg Kroah-Hartman 	release_tty(tty, idx);
157696fd7ce5SGreg Kroah-Hartman 	return ERR_PTR(retval);
157796fd7ce5SGreg Kroah-Hartman }
157896fd7ce5SGreg Kroah-Hartman 
1579a3123fd0SPeter Hurley static void tty_free_termios(struct tty_struct *tty)
158096fd7ce5SGreg Kroah-Hartman {
158196fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
158296fd7ce5SGreg Kroah-Hartman 	int idx = tty->index;
158336b3c070SAlan Cox 
158436b3c070SAlan Cox 	/* If the port is going to reset then it has no termios to save */
158536b3c070SAlan Cox 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
158636b3c070SAlan Cox 		return;
158736b3c070SAlan Cox 
158836b3c070SAlan Cox 	/* Stash the termios data */
1589adc8d746SAlan Cox 	tp = tty->driver->termios[idx];
159036b3c070SAlan Cox 	if (tp == NULL) {
159136b3c070SAlan Cox 		tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
159225080652SPeter Hurley 		if (tp == NULL)
159336b3c070SAlan Cox 			return;
15944ac5d705SDan Carpenter 		tty->driver->termios[idx] = tp;
159536b3c070SAlan Cox 	}
159636b3c070SAlan Cox 	*tp = tty->termios;
159796fd7ce5SGreg Kroah-Hartman }
159896fd7ce5SGreg Kroah-Hartman 
1599a2965b7bSPeter Hurley /**
1600949aa64fSPeter Hurley  *	tty_flush_works		-	flush all works of a tty/pty pair
1601949aa64fSPeter Hurley  *	@tty: tty device to flush works for (or either end of a pty pair)
1602a2965b7bSPeter Hurley  *
1603949aa64fSPeter Hurley  *	Sync flush all works belonging to @tty (and the 'other' tty).
1604a2965b7bSPeter Hurley  */
1605a2965b7bSPeter Hurley static void tty_flush_works(struct tty_struct *tty)
1606a2965b7bSPeter Hurley {
1607a2965b7bSPeter Hurley 	flush_work(&tty->SAK_work);
1608a2965b7bSPeter Hurley 	flush_work(&tty->hangup_work);
1609949aa64fSPeter Hurley 	if (tty->link) {
1610949aa64fSPeter Hurley 		flush_work(&tty->link->SAK_work);
1611949aa64fSPeter Hurley 		flush_work(&tty->link->hangup_work);
1612949aa64fSPeter Hurley 	}
1613a2965b7bSPeter Hurley }
161496fd7ce5SGreg Kroah-Hartman 
161596fd7ce5SGreg Kroah-Hartman /**
161696fd7ce5SGreg Kroah-Hartman  *	release_one_tty		-	release tty structure memory
161796fd7ce5SGreg Kroah-Hartman  *	@kref: kref of tty we are obliterating
161896fd7ce5SGreg Kroah-Hartman  *
161996fd7ce5SGreg Kroah-Hartman  *	Releases memory associated with a tty structure, and clears out the
162096fd7ce5SGreg Kroah-Hartman  *	driver table slots. This function is called when a device is no longer
162196fd7ce5SGreg Kroah-Hartman  *	in use. It also gets called when setup of a device fails.
162296fd7ce5SGreg Kroah-Hartman  *
162396fd7ce5SGreg Kroah-Hartman  *	Locking:
162496fd7ce5SGreg Kroah-Hartman  *		takes the file list lock internally when working on the list
162596fd7ce5SGreg Kroah-Hartman  *	of ttys that the driver keeps.
162696fd7ce5SGreg Kroah-Hartman  *
162796fd7ce5SGreg Kroah-Hartman  *	This method gets called from a work queue so that the driver private
162896fd7ce5SGreg Kroah-Hartman  *	cleanup ops can sleep (needed for USB at least)
162996fd7ce5SGreg Kroah-Hartman  */
163096fd7ce5SGreg Kroah-Hartman static void release_one_tty(struct work_struct *work)
163196fd7ce5SGreg Kroah-Hartman {
163296fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
163396fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, hangup_work);
163496fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = tty->driver;
1635b216df53SCyrill Gorcunov 	struct module *owner = driver->owner;
163696fd7ce5SGreg Kroah-Hartman 
163796fd7ce5SGreg Kroah-Hartman 	if (tty->ops->cleanup)
163896fd7ce5SGreg Kroah-Hartman 		tty->ops->cleanup(tty);
163996fd7ce5SGreg Kroah-Hartman 
164096fd7ce5SGreg Kroah-Hartman 	tty->magic = 0;
164196fd7ce5SGreg Kroah-Hartman 	tty_driver_kref_put(driver);
1642b216df53SCyrill Gorcunov 	module_put(owner);
164396fd7ce5SGreg Kroah-Hartman 
16444a510969SPeter Hurley 	spin_lock(&tty->files_lock);
164596fd7ce5SGreg Kroah-Hartman 	list_del_init(&tty->tty_files);
16464a510969SPeter Hurley 	spin_unlock(&tty->files_lock);
164796fd7ce5SGreg Kroah-Hartman 
164896fd7ce5SGreg Kroah-Hartman 	put_pid(tty->pgrp);
164996fd7ce5SGreg Kroah-Hartman 	put_pid(tty->session);
165096fd7ce5SGreg Kroah-Hartman 	free_tty_struct(tty);
165196fd7ce5SGreg Kroah-Hartman }
165296fd7ce5SGreg Kroah-Hartman 
165396fd7ce5SGreg Kroah-Hartman static void queue_release_one_tty(struct kref *kref)
165496fd7ce5SGreg Kroah-Hartman {
165596fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
165696fd7ce5SGreg Kroah-Hartman 
165796fd7ce5SGreg Kroah-Hartman 	/* The hangup queue is now free so we can reuse it rather than
165896fd7ce5SGreg Kroah-Hartman 	   waste a chunk of memory for each port */
165996fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->hangup_work, release_one_tty);
166096fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->hangup_work);
166196fd7ce5SGreg Kroah-Hartman }
166296fd7ce5SGreg Kroah-Hartman 
166396fd7ce5SGreg Kroah-Hartman /**
166496fd7ce5SGreg Kroah-Hartman  *	tty_kref_put		-	release a tty kref
166596fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
166696fd7ce5SGreg Kroah-Hartman  *
166796fd7ce5SGreg Kroah-Hartman  *	Release a reference to a tty device and if need be let the kref
166896fd7ce5SGreg Kroah-Hartman  *	layer destruct the object for us
166996fd7ce5SGreg Kroah-Hartman  */
167096fd7ce5SGreg Kroah-Hartman 
167196fd7ce5SGreg Kroah-Hartman void tty_kref_put(struct tty_struct *tty)
167296fd7ce5SGreg Kroah-Hartman {
167396fd7ce5SGreg Kroah-Hartman 	if (tty)
167496fd7ce5SGreg Kroah-Hartman 		kref_put(&tty->kref, queue_release_one_tty);
167596fd7ce5SGreg Kroah-Hartman }
167696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_kref_put);
167796fd7ce5SGreg Kroah-Hartman 
167896fd7ce5SGreg Kroah-Hartman /**
167996fd7ce5SGreg Kroah-Hartman  *	release_tty		-	release tty structure memory
168096fd7ce5SGreg Kroah-Hartman  *
168196fd7ce5SGreg Kroah-Hartman  *	Release both @tty and a possible linked partner (think pty pair),
168296fd7ce5SGreg Kroah-Hartman  *	and decrement the refcount of the backing module.
168396fd7ce5SGreg Kroah-Hartman  *
168496fd7ce5SGreg Kroah-Hartman  *	Locking:
1685d155255aSAlan Cox  *		tty_mutex
168696fd7ce5SGreg Kroah-Hartman  *		takes the file list lock internally when working on the list
168796fd7ce5SGreg Kroah-Hartman  *	of ttys that the driver keeps.
168896fd7ce5SGreg Kroah-Hartman  *
168996fd7ce5SGreg Kroah-Hartman  */
169096fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx)
169196fd7ce5SGreg Kroah-Hartman {
169296fd7ce5SGreg Kroah-Hartman 	/* This should always be true but check for the moment */
169396fd7ce5SGreg Kroah-Hartman 	WARN_ON(tty->index != idx);
1694d155255aSAlan Cox 	WARN_ON(!mutex_is_locked(&tty_mutex));
169536b3c070SAlan Cox 	if (tty->ops->shutdown)
169636b3c070SAlan Cox 		tty->ops->shutdown(tty);
169736b3c070SAlan Cox 	tty_free_termios(tty);
169836b3c070SAlan Cox 	tty_driver_remove_tty(tty->driver, tty);
1699967fab69SJiri Slaby 	tty->port->itty = NULL;
170064e377dcSPeter Hurley 	if (tty->link)
170164e377dcSPeter Hurley 		tty->link->port->itty = NULL;
1702e176058fSPeter Hurley 	tty_buffer_cancel_work(tty->port);
170336b3c070SAlan Cox 
170496fd7ce5SGreg Kroah-Hartman 	tty_kref_put(tty->link);
170596fd7ce5SGreg Kroah-Hartman 	tty_kref_put(tty);
170696fd7ce5SGreg Kroah-Hartman }
170796fd7ce5SGreg Kroah-Hartman 
170896fd7ce5SGreg Kroah-Hartman /**
1709955787caSJiri Slaby  *	tty_release_checks - check a tty before real release
1710955787caSJiri Slaby  *	@tty: tty to check
1711955787caSJiri Slaby  *	@o_tty: link of @tty (if any)
1712955787caSJiri Slaby  *	@idx: index of the tty
1713955787caSJiri Slaby  *
1714955787caSJiri Slaby  *	Performs some paranoid checking before true release of the @tty.
1715955787caSJiri Slaby  *	This is a no-op unless TTY_PARANOIA_CHECK is defined.
1716955787caSJiri Slaby  */
1717359b9fb5SPeter Hurley static int tty_release_checks(struct tty_struct *tty, int idx)
1718955787caSJiri Slaby {
1719955787caSJiri Slaby #ifdef TTY_PARANOIA_CHECK
1720955787caSJiri Slaby 	if (idx < 0 || idx >= tty->driver->num) {
1721e2dfa3d3SPeter Hurley 		tty_debug(tty, "bad idx %d\n", idx);
1722955787caSJiri Slaby 		return -1;
1723955787caSJiri Slaby 	}
1724955787caSJiri Slaby 
1725955787caSJiri Slaby 	/* not much to check for devpts */
1726955787caSJiri Slaby 	if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1727955787caSJiri Slaby 		return 0;
1728955787caSJiri Slaby 
1729955787caSJiri Slaby 	if (tty != tty->driver->ttys[idx]) {
1730e2dfa3d3SPeter Hurley 		tty_debug(tty, "bad driver table[%d] = %p\n",
1731e2dfa3d3SPeter Hurley 			  idx, tty->driver->ttys[idx]);
1732955787caSJiri Slaby 		return -1;
1733955787caSJiri Slaby 	}
1734955787caSJiri Slaby 	if (tty->driver->other) {
1735359b9fb5SPeter Hurley 		struct tty_struct *o_tty = tty->link;
1736359b9fb5SPeter Hurley 
1737955787caSJiri Slaby 		if (o_tty != tty->driver->other->ttys[idx]) {
1738e2dfa3d3SPeter Hurley 			tty_debug(tty, "bad other table[%d] = %p\n",
1739e2dfa3d3SPeter Hurley 				  idx, tty->driver->other->ttys[idx]);
1740955787caSJiri Slaby 			return -1;
1741955787caSJiri Slaby 		}
1742955787caSJiri Slaby 		if (o_tty->link != tty) {
1743e2dfa3d3SPeter Hurley 			tty_debug(tty, "bad link = %p\n", o_tty->link);
1744955787caSJiri Slaby 			return -1;
1745955787caSJiri Slaby 		}
1746955787caSJiri Slaby 	}
1747955787caSJiri Slaby #endif
1748955787caSJiri Slaby 	return 0;
1749955787caSJiri Slaby }
1750955787caSJiri Slaby 
1751955787caSJiri Slaby /**
175296fd7ce5SGreg Kroah-Hartman  *	tty_release		-	vfs callback for close
175396fd7ce5SGreg Kroah-Hartman  *	@inode: inode of tty
175496fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer for handle to tty
175596fd7ce5SGreg Kroah-Hartman  *
175696fd7ce5SGreg Kroah-Hartman  *	Called the last time each file handle is closed that references
175796fd7ce5SGreg Kroah-Hartman  *	this tty. There may however be several such references.
175896fd7ce5SGreg Kroah-Hartman  *
175996fd7ce5SGreg Kroah-Hartman  *	Locking:
176096fd7ce5SGreg Kroah-Hartman  *		Takes bkl. See tty_release_dev
176196fd7ce5SGreg Kroah-Hartman  *
176296fd7ce5SGreg Kroah-Hartman  * Even releasing the tty structures is a tricky business.. We have
176396fd7ce5SGreg Kroah-Hartman  * to be very careful that the structures are all released at the
176496fd7ce5SGreg Kroah-Hartman  * same time, as interrupts might otherwise get the wrong pointers.
176596fd7ce5SGreg Kroah-Hartman  *
176696fd7ce5SGreg Kroah-Hartman  * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
176796fd7ce5SGreg Kroah-Hartman  * lead to double frees or releasing memory still in use.
176896fd7ce5SGreg Kroah-Hartman  */
176996fd7ce5SGreg Kroah-Hartman 
177096fd7ce5SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp)
177196fd7ce5SGreg Kroah-Hartman {
177296fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
17737ffb6da9SPeter Hurley 	struct tty_struct *o_tty = NULL;
17747ffb6da9SPeter Hurley 	int	do_sleep, final;
177596fd7ce5SGreg Kroah-Hartman 	int	idx;
177637b16457SPeter Hurley 	long	timeout = 0;
1777494c1eacSPeter Hurley 	int	once = 1;
177896fd7ce5SGreg Kroah-Hartman 
17799de44bd6SJiri Slaby 	if (tty_paranoia_check(tty, inode, __func__))
178096fd7ce5SGreg Kroah-Hartman 		return 0;
178196fd7ce5SGreg Kroah-Hartman 
178289c8d91eSAlan Cox 	tty_lock(tty);
17839de44bd6SJiri Slaby 	check_tty_count(tty, __func__);
178496fd7ce5SGreg Kroah-Hartman 
178596fd7ce5SGreg Kroah-Hartman 	__tty_fasync(-1, filp, 0);
178696fd7ce5SGreg Kroah-Hartman 
178796fd7ce5SGreg Kroah-Hartman 	idx = tty->index;
17887ffb6da9SPeter Hurley 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
17897ffb6da9SPeter Hurley 	    tty->driver->subtype == PTY_TYPE_MASTER)
179096fd7ce5SGreg Kroah-Hartman 		o_tty = tty->link;
179196fd7ce5SGreg Kroah-Hartman 
1792359b9fb5SPeter Hurley 	if (tty_release_checks(tty, idx)) {
179389c8d91eSAlan Cox 		tty_unlock(tty);
179496fd7ce5SGreg Kroah-Hartman 		return 0;
179596fd7ce5SGreg Kroah-Hartman 	}
179696fd7ce5SGreg Kroah-Hartman 
1797d435cefeSPeter Hurley 	tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
179896fd7ce5SGreg Kroah-Hartman 
179996fd7ce5SGreg Kroah-Hartman 	if (tty->ops->close)
180096fd7ce5SGreg Kroah-Hartman 		tty->ops->close(tty, filp);
180196fd7ce5SGreg Kroah-Hartman 
18022aff5e2bSPeter Hurley 	/* If tty is pty master, lock the slave pty (stable lock order) */
18032aff5e2bSPeter Hurley 	tty_lock_slave(o_tty);
18042aff5e2bSPeter Hurley 
180596fd7ce5SGreg Kroah-Hartman 	/*
180696fd7ce5SGreg Kroah-Hartman 	 * Sanity check: if tty->count is going to zero, there shouldn't be
180796fd7ce5SGreg Kroah-Hartman 	 * any waiters on tty->read_wait or tty->write_wait.  We test the
180896fd7ce5SGreg Kroah-Hartman 	 * wait queues and kick everyone out _before_ actually starting to
180996fd7ce5SGreg Kroah-Hartman 	 * close.  This ensures that we won't block while releasing the tty
181096fd7ce5SGreg Kroah-Hartman 	 * structure.
181196fd7ce5SGreg Kroah-Hartman 	 *
181296fd7ce5SGreg Kroah-Hartman 	 * The test for the o_tty closing is necessary, since the master and
181396fd7ce5SGreg Kroah-Hartman 	 * slave sides may close in any order.  If the slave side closes out
181496fd7ce5SGreg Kroah-Hartman 	 * first, its count will be one, since the master side holds an open.
1815324c1650SPeter Hurley 	 * Thus this test wouldn't be triggered at the time the slave closed,
181696fd7ce5SGreg Kroah-Hartman 	 * so we do it now.
181796fd7ce5SGreg Kroah-Hartman 	 */
1818d5e370a4SPeter Hurley 	while (1) {
181996fd7ce5SGreg Kroah-Hartman 		do_sleep = 0;
182096fd7ce5SGreg Kroah-Hartman 
1821324c1650SPeter Hurley 		if (tty->count <= 1) {
182296fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&tty->read_wait)) {
182396fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&tty->read_wait, POLLIN);
182496fd7ce5SGreg Kroah-Hartman 				do_sleep++;
182596fd7ce5SGreg Kroah-Hartman 			}
182696fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&tty->write_wait)) {
182796fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&tty->write_wait, POLLOUT);
182896fd7ce5SGreg Kroah-Hartman 				do_sleep++;
182996fd7ce5SGreg Kroah-Hartman 			}
183096fd7ce5SGreg Kroah-Hartman 		}
18317ffb6da9SPeter Hurley 		if (o_tty && o_tty->count <= 1) {
183296fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&o_tty->read_wait)) {
183396fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&o_tty->read_wait, POLLIN);
183496fd7ce5SGreg Kroah-Hartman 				do_sleep++;
183596fd7ce5SGreg Kroah-Hartman 			}
183696fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&o_tty->write_wait)) {
183796fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&o_tty->write_wait, POLLOUT);
183896fd7ce5SGreg Kroah-Hartman 				do_sleep++;
183996fd7ce5SGreg Kroah-Hartman 			}
184096fd7ce5SGreg Kroah-Hartman 		}
184196fd7ce5SGreg Kroah-Hartman 		if (!do_sleep)
184296fd7ce5SGreg Kroah-Hartman 			break;
184396fd7ce5SGreg Kroah-Hartman 
1844494c1eacSPeter Hurley 		if (once) {
1845494c1eacSPeter Hurley 			once = 0;
1846339f36baSPeter Hurley 			tty_warn(tty, "read/write wait queue active!\n");
1847494c1eacSPeter Hurley 		}
184837b16457SPeter Hurley 		schedule_timeout_killable(timeout);
184937b16457SPeter Hurley 		if (timeout < 120 * HZ)
185037b16457SPeter Hurley 			timeout = 2 * timeout + 1;
185137b16457SPeter Hurley 		else
185237b16457SPeter Hurley 			timeout = MAX_SCHEDULE_TIMEOUT;
185396fd7ce5SGreg Kroah-Hartman 	}
185496fd7ce5SGreg Kroah-Hartman 
18557ffb6da9SPeter Hurley 	if (o_tty) {
185696fd7ce5SGreg Kroah-Hartman 		if (--o_tty->count < 0) {
1857339f36baSPeter Hurley 			tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
185896fd7ce5SGreg Kroah-Hartman 			o_tty->count = 0;
185996fd7ce5SGreg Kroah-Hartman 		}
186096fd7ce5SGreg Kroah-Hartman 	}
186196fd7ce5SGreg Kroah-Hartman 	if (--tty->count < 0) {
1862339f36baSPeter Hurley 		tty_warn(tty, "bad tty->count (%d)\n", tty->count);
186396fd7ce5SGreg Kroah-Hartman 		tty->count = 0;
186496fd7ce5SGreg Kroah-Hartman 	}
186596fd7ce5SGreg Kroah-Hartman 
186696fd7ce5SGreg Kroah-Hartman 	/*
186796fd7ce5SGreg Kroah-Hartman 	 * We've decremented tty->count, so we need to remove this file
186896fd7ce5SGreg Kroah-Hartman 	 * descriptor off the tty->tty_files list; this serves two
186996fd7ce5SGreg Kroah-Hartman 	 * purposes:
187096fd7ce5SGreg Kroah-Hartman 	 *  - check_tty_count sees the correct number of file descriptors
187196fd7ce5SGreg Kroah-Hartman 	 *    associated with this tty.
187296fd7ce5SGreg Kroah-Hartman 	 *  - do_tty_hangup no longer sees this file descriptor as
187396fd7ce5SGreg Kroah-Hartman 	 *    something that needs to be handled for hangups.
187496fd7ce5SGreg Kroah-Hartman 	 */
187596fd7ce5SGreg Kroah-Hartman 	tty_del_file(filp);
187696fd7ce5SGreg Kroah-Hartman 
187796fd7ce5SGreg Kroah-Hartman 	/*
187896fd7ce5SGreg Kroah-Hartman 	 * Perform some housekeeping before deciding whether to return.
187996fd7ce5SGreg Kroah-Hartman 	 *
188096fd7ce5SGreg Kroah-Hartman 	 * If _either_ side is closing, make sure there aren't any
188196fd7ce5SGreg Kroah-Hartman 	 * processes that still think tty or o_tty is their controlling
188296fd7ce5SGreg Kroah-Hartman 	 * tty.
188396fd7ce5SGreg Kroah-Hartman 	 */
1884324c1650SPeter Hurley 	if (!tty->count) {
188596fd7ce5SGreg Kroah-Hartman 		read_lock(&tasklist_lock);
188696fd7ce5SGreg Kroah-Hartman 		session_clear_tty(tty->session);
18877ffb6da9SPeter Hurley 		if (o_tty)
188896fd7ce5SGreg Kroah-Hartman 			session_clear_tty(o_tty->session);
188996fd7ce5SGreg Kroah-Hartman 		read_unlock(&tasklist_lock);
189096fd7ce5SGreg Kroah-Hartman 	}
189196fd7ce5SGreg Kroah-Hartman 
1892324c1650SPeter Hurley 	/* check whether both sides are closing ... */
18937ffb6da9SPeter Hurley 	final = !tty->count && !(o_tty && o_tty->count);
1894324c1650SPeter Hurley 
18952aff5e2bSPeter Hurley 	tty_unlock_slave(o_tty);
18962aff5e2bSPeter Hurley 	tty_unlock(tty);
18972aff5e2bSPeter Hurley 
189804980706SPeter Hurley 	/* At this point, the tty->count == 0 should ensure a dead tty
1899d155255aSAlan Cox 	   cannot be re-opened by a racing opener */
190096fd7ce5SGreg Kroah-Hartman 
1901324c1650SPeter Hurley 	if (!final)
190296fd7ce5SGreg Kroah-Hartman 		return 0;
190396fd7ce5SGreg Kroah-Hartman 
1904accff793SPeter Hurley 	tty_debug_hangup(tty, "final close\n");
190596fd7ce5SGreg Kroah-Hartman 	/*
190696fd7ce5SGreg Kroah-Hartman 	 * Ask the line discipline code to release its structures
190796fd7ce5SGreg Kroah-Hartman 	 */
190862462aefSPeter Hurley 	tty_ldisc_release(tty);
1909a2965b7bSPeter Hurley 
1910a2965b7bSPeter Hurley 	/* Wait for pending work before tty destruction commmences */
1911a2965b7bSPeter Hurley 	tty_flush_works(tty);
1912a2965b7bSPeter Hurley 
1913d435cefeSPeter Hurley 	tty_debug_hangup(tty, "freeing structure\n");
191496fd7ce5SGreg Kroah-Hartman 	/*
191596fd7ce5SGreg Kroah-Hartman 	 * The release_tty function takes care of the details of clearing
191689c8d91eSAlan Cox 	 * the slots and preserving the termios structure. The tty_unlock_pair
191789c8d91eSAlan Cox 	 * should be safe as we keep a kref while the tty is locked (so the
191889c8d91eSAlan Cox 	 * unlock never unlocks a freed tty).
191996fd7ce5SGreg Kroah-Hartman 	 */
1920d155255aSAlan Cox 	mutex_lock(&tty_mutex);
192196fd7ce5SGreg Kroah-Hartman 	release_tty(tty, idx);
1922d155255aSAlan Cox 	mutex_unlock(&tty_mutex);
192396fd7ce5SGreg Kroah-Hartman 
192496fd7ce5SGreg Kroah-Hartman 	return 0;
192596fd7ce5SGreg Kroah-Hartman }
192696fd7ce5SGreg Kroah-Hartman 
192796fd7ce5SGreg Kroah-Hartman /**
192852494eebSPeter Hurley  *	tty_open_current_tty - get locked tty of current task
1929b82154acSJiri Slaby  *	@device: device number
1930b82154acSJiri Slaby  *	@filp: file pointer to tty
193152494eebSPeter Hurley  *	@return: locked tty of the current task iff @device is /dev/tty
193252494eebSPeter Hurley  *
193352494eebSPeter Hurley  *	Performs a re-open of the current task's controlling tty.
1934b82154acSJiri Slaby  *
1935b82154acSJiri Slaby  *	We cannot return driver and index like for the other nodes because
1936b82154acSJiri Slaby  *	devpts will not work then. It expects inodes to be from devpts FS.
1937b82154acSJiri Slaby  */
1938b82154acSJiri Slaby static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1939b82154acSJiri Slaby {
1940b82154acSJiri Slaby 	struct tty_struct *tty;
194152494eebSPeter Hurley 	int retval;
1942b82154acSJiri Slaby 
1943b82154acSJiri Slaby 	if (device != MKDEV(TTYAUX_MAJOR, 0))
1944b82154acSJiri Slaby 		return NULL;
1945b82154acSJiri Slaby 
1946b82154acSJiri Slaby 	tty = get_current_tty();
1947b82154acSJiri Slaby 	if (!tty)
1948b82154acSJiri Slaby 		return ERR_PTR(-ENXIO);
1949b82154acSJiri Slaby 
1950b82154acSJiri Slaby 	filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1951b82154acSJiri Slaby 	/* noctty = 1; */
195252494eebSPeter Hurley 	tty_lock(tty);
195352494eebSPeter Hurley 	tty_kref_put(tty);	/* safe to drop the kref now */
195452494eebSPeter Hurley 
195552494eebSPeter Hurley 	retval = tty_reopen(tty);
195652494eebSPeter Hurley 	if (retval < 0) {
195752494eebSPeter Hurley 		tty_unlock(tty);
195852494eebSPeter Hurley 		tty = ERR_PTR(retval);
195952494eebSPeter Hurley 	}
1960b82154acSJiri Slaby 	return tty;
1961b82154acSJiri Slaby }
1962b82154acSJiri Slaby 
1963b82154acSJiri Slaby /**
19645b5e7040SJiri Slaby  *	tty_lookup_driver - lookup a tty driver for a given device file
19655b5e7040SJiri Slaby  *	@device: device number
19665b5e7040SJiri Slaby  *	@filp: file pointer to tty
19675b5e7040SJiri Slaby  *	@noctty: set if the device should not become a controlling tty
19685b5e7040SJiri Slaby  *	@index: index for the device in the @return driver
19695b5e7040SJiri Slaby  *	@return: driver for this inode (with increased refcount)
19705b5e7040SJiri Slaby  *
19715b5e7040SJiri Slaby  * 	If @return is not erroneous, the caller is responsible to decrement the
19725b5e7040SJiri Slaby  * 	refcount by tty_driver_kref_put.
19735b5e7040SJiri Slaby  *
19745b5e7040SJiri Slaby  *	Locking: tty_mutex protects get_tty_driver
19755b5e7040SJiri Slaby  */
19765b5e7040SJiri Slaby static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
197711e1d4aaSPeter Hurley 		int *index)
19785b5e7040SJiri Slaby {
19795b5e7040SJiri Slaby 	struct tty_driver *driver;
19805b5e7040SJiri Slaby 
19812cd0050cSJiri Slaby 	switch (device) {
19825b5e7040SJiri Slaby #ifdef CONFIG_VT
19832cd0050cSJiri Slaby 	case MKDEV(TTY_MAJOR, 0): {
19845b5e7040SJiri Slaby 		extern struct tty_driver *console_driver;
19855b5e7040SJiri Slaby 		driver = tty_driver_kref_get(console_driver);
19865b5e7040SJiri Slaby 		*index = fg_console;
19872cd0050cSJiri Slaby 		break;
19885b5e7040SJiri Slaby 	}
19895b5e7040SJiri Slaby #endif
19902cd0050cSJiri Slaby 	case MKDEV(TTYAUX_MAJOR, 1): {
19915b5e7040SJiri Slaby 		struct tty_driver *console_driver = console_device(index);
19925b5e7040SJiri Slaby 		if (console_driver) {
19935b5e7040SJiri Slaby 			driver = tty_driver_kref_get(console_driver);
19945b5e7040SJiri Slaby 			if (driver) {
19955b5e7040SJiri Slaby 				/* Don't let /dev/console block */
19965b5e7040SJiri Slaby 				filp->f_flags |= O_NONBLOCK;
19972cd0050cSJiri Slaby 				break;
19985b5e7040SJiri Slaby 			}
19995b5e7040SJiri Slaby 		}
20005b5e7040SJiri Slaby 		return ERR_PTR(-ENODEV);
20015b5e7040SJiri Slaby 	}
20022cd0050cSJiri Slaby 	default:
20035b5e7040SJiri Slaby 		driver = get_tty_driver(device, index);
20045b5e7040SJiri Slaby 		if (!driver)
20055b5e7040SJiri Slaby 			return ERR_PTR(-ENODEV);
20062cd0050cSJiri Slaby 		break;
20072cd0050cSJiri Slaby 	}
20085b5e7040SJiri Slaby 	return driver;
20095b5e7040SJiri Slaby }
20105b5e7040SJiri Slaby 
20115b5e7040SJiri Slaby /**
2012d6203d0cSPeter Hurley  *	tty_open_by_driver	-	open a tty device
2013d6203d0cSPeter Hurley  *	@device: dev_t of device to open
2014d6203d0cSPeter Hurley  *	@inode: inode of device file
2015d6203d0cSPeter Hurley  *	@filp: file pointer to tty
2016d6203d0cSPeter Hurley  *
2017d6203d0cSPeter Hurley  *	Performs the driver lookup, checks for a reopen, or otherwise
2018d6203d0cSPeter Hurley  *	performs the first-time tty initialization.
2019d6203d0cSPeter Hurley  *
2020d6203d0cSPeter Hurley  *	Returns the locked initialized or re-opened &tty_struct
2021d6203d0cSPeter Hurley  *
2022d6203d0cSPeter Hurley  *	Claims the global tty_mutex to serialize:
2023d6203d0cSPeter Hurley  *	  - concurrent first-time tty initialization
2024d6203d0cSPeter Hurley  *	  - concurrent tty driver removal w/ lookup
2025d6203d0cSPeter Hurley  *	  - concurrent tty removal from driver table
2026d6203d0cSPeter Hurley  */
2027d6203d0cSPeter Hurley static struct tty_struct *tty_open_by_driver(dev_t device, struct inode *inode,
2028d6203d0cSPeter Hurley 					     struct file *filp)
2029d6203d0cSPeter Hurley {
2030d6203d0cSPeter Hurley 	struct tty_struct *tty;
2031d6203d0cSPeter Hurley 	struct tty_driver *driver = NULL;
2032d6203d0cSPeter Hurley 	int index = -1;
2033d6203d0cSPeter Hurley 	int retval;
2034d6203d0cSPeter Hurley 
2035d6203d0cSPeter Hurley 	mutex_lock(&tty_mutex);
2036d6203d0cSPeter Hurley 	driver = tty_lookup_driver(device, filp, &index);
2037d6203d0cSPeter Hurley 	if (IS_ERR(driver)) {
2038d6203d0cSPeter Hurley 		mutex_unlock(&tty_mutex);
2039d6203d0cSPeter Hurley 		return ERR_CAST(driver);
2040d6203d0cSPeter Hurley 	}
2041d6203d0cSPeter Hurley 
2042d6203d0cSPeter Hurley 	/* check whether we're reopening an existing tty */
2043d6203d0cSPeter Hurley 	tty = tty_driver_lookup_tty(driver, inode, index);
2044d6203d0cSPeter Hurley 	if (IS_ERR(tty)) {
2045d6203d0cSPeter Hurley 		mutex_unlock(&tty_mutex);
2046d6203d0cSPeter Hurley 		goto out;
2047d6203d0cSPeter Hurley 	}
2048d6203d0cSPeter Hurley 
2049d6203d0cSPeter Hurley 	if (tty) {
2050d6203d0cSPeter Hurley 		mutex_unlock(&tty_mutex);
2051d6203d0cSPeter Hurley 		retval = tty_lock_interruptible(tty);
20525e00bbfbSPeter Hurley 		tty_kref_put(tty);  /* drop kref from tty_driver_lookup_tty() */
2053d6203d0cSPeter Hurley 		if (retval) {
2054d6203d0cSPeter Hurley 			if (retval == -EINTR)
2055d6203d0cSPeter Hurley 				retval = -ERESTARTSYS;
2056d6203d0cSPeter Hurley 			tty = ERR_PTR(retval);
2057d6203d0cSPeter Hurley 			goto out;
2058d6203d0cSPeter Hurley 		}
2059d6203d0cSPeter Hurley 		retval = tty_reopen(tty);
2060d6203d0cSPeter Hurley 		if (retval < 0) {
2061d6203d0cSPeter Hurley 			tty_unlock(tty);
2062d6203d0cSPeter Hurley 			tty = ERR_PTR(retval);
2063d6203d0cSPeter Hurley 		}
2064d6203d0cSPeter Hurley 	} else { /* Returns with the tty_lock held for now */
2065d6203d0cSPeter Hurley 		tty = tty_init_dev(driver, index);
2066d6203d0cSPeter Hurley 		mutex_unlock(&tty_mutex);
2067d6203d0cSPeter Hurley 	}
2068d6203d0cSPeter Hurley out:
2069d6203d0cSPeter Hurley 	tty_driver_kref_put(driver);
2070d6203d0cSPeter Hurley 	return tty;
2071d6203d0cSPeter Hurley }
2072d6203d0cSPeter Hurley 
2073d6203d0cSPeter Hurley /**
207496fd7ce5SGreg Kroah-Hartman  *	tty_open		-	open a tty device
207596fd7ce5SGreg Kroah-Hartman  *	@inode: inode of device file
207696fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer to tty
207796fd7ce5SGreg Kroah-Hartman  *
207896fd7ce5SGreg Kroah-Hartman  *	tty_open and tty_release keep up the tty count that contains the
207996fd7ce5SGreg Kroah-Hartman  *	number of opens done on a tty. We cannot use the inode-count, as
208096fd7ce5SGreg Kroah-Hartman  *	different inodes might point to the same tty.
208196fd7ce5SGreg Kroah-Hartman  *
208296fd7ce5SGreg Kroah-Hartman  *	Open-counting is needed for pty masters, as well as for keeping
208396fd7ce5SGreg Kroah-Hartman  *	track of serial lines: DTR is dropped when the last close happens.
208496fd7ce5SGreg Kroah-Hartman  *	(This is not done solely through tty->count, now.  - Ted 1/27/92)
208596fd7ce5SGreg Kroah-Hartman  *
208696fd7ce5SGreg Kroah-Hartman  *	The termios state of a pty is reset on first open so that
208796fd7ce5SGreg Kroah-Hartman  *	settings don't persist across reuse.
208896fd7ce5SGreg Kroah-Hartman  *
20895b5e7040SJiri Slaby  *	Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
209096fd7ce5SGreg Kroah-Hartman  *		 tty->count should protect the rest.
209196fd7ce5SGreg Kroah-Hartman  *		 ->siglock protects ->signal/->sighand
209289c8d91eSAlan Cox  *
209389c8d91eSAlan Cox  *	Note: the tty_unlock/lock cases without a ref are only safe due to
209489c8d91eSAlan Cox  *	tty_mutex
209596fd7ce5SGreg Kroah-Hartman  */
209696fd7ce5SGreg Kroah-Hartman 
209796fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *inode, struct file *filp)
209896fd7ce5SGreg Kroah-Hartman {
2099b82154acSJiri Slaby 	struct tty_struct *tty;
210096fd7ce5SGreg Kroah-Hartman 	int noctty, retval;
210196fd7ce5SGreg Kroah-Hartman 	dev_t device = inode->i_rdev;
210296fd7ce5SGreg Kroah-Hartman 	unsigned saved_flags = filp->f_flags;
210396fd7ce5SGreg Kroah-Hartman 
210496fd7ce5SGreg Kroah-Hartman 	nonseekable_open(inode, filp);
210596fd7ce5SGreg Kroah-Hartman 
210696fd7ce5SGreg Kroah-Hartman retry_open:
2107fa90e1c9SJiri Slaby 	retval = tty_alloc_file(filp);
2108fa90e1c9SJiri Slaby 	if (retval)
2109fa90e1c9SJiri Slaby 		return -ENOMEM;
2110fa90e1c9SJiri Slaby 
2111b82154acSJiri Slaby 	tty = tty_open_current_tty(device, filp);
2112d6203d0cSPeter Hurley 	if (!tty)
2113d6203d0cSPeter Hurley 		tty = tty_open_by_driver(device, inode, filp);
211452494eebSPeter Hurley 
211596fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(tty)) {
2116d6203d0cSPeter Hurley 		tty_free_file(filp);
2117ba5db448SJiri Slaby 		retval = PTR_ERR(tty);
21187f22f6c9SPeter Hurley 		if (retval != -EAGAIN || signal_pending(current))
2119d6203d0cSPeter Hurley 			return retval;
21207f22f6c9SPeter Hurley 		schedule();
21217f22f6c9SPeter Hurley 		goto retry_open;
212296fd7ce5SGreg Kroah-Hartman 	}
212396fd7ce5SGreg Kroah-Hartman 
2124fa90e1c9SJiri Slaby 	tty_add_file(tty, filp);
212596fd7ce5SGreg Kroah-Hartman 
21269de44bd6SJiri Slaby 	check_tty_count(tty, __func__);
2127d435cefeSPeter Hurley 	tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
2128accff793SPeter Hurley 
212996fd7ce5SGreg Kroah-Hartman 	if (tty->ops->open)
213096fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->open(tty, filp);
213196fd7ce5SGreg Kroah-Hartman 	else
213296fd7ce5SGreg Kroah-Hartman 		retval = -ENODEV;
213396fd7ce5SGreg Kroah-Hartman 	filp->f_flags = saved_flags;
213496fd7ce5SGreg Kroah-Hartman 
213596fd7ce5SGreg Kroah-Hartman 	if (retval) {
2136d435cefeSPeter Hurley 		tty_debug_hangup(tty, "open error %d, releasing\n", retval);
2137accff793SPeter Hurley 
213889c8d91eSAlan Cox 		tty_unlock(tty); /* need to call tty_release without BTM */
213996fd7ce5SGreg Kroah-Hartman 		tty_release(inode, filp);
214096fd7ce5SGreg Kroah-Hartman 		if (retval != -ERESTARTSYS)
214196fd7ce5SGreg Kroah-Hartman 			return retval;
214296fd7ce5SGreg Kroah-Hartman 
214396fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
214496fd7ce5SGreg Kroah-Hartman 			return retval;
214596fd7ce5SGreg Kroah-Hartman 
214696fd7ce5SGreg Kroah-Hartman 		schedule();
214796fd7ce5SGreg Kroah-Hartman 		/*
214896fd7ce5SGreg Kroah-Hartman 		 * Need to reset f_op in case a hangup happened.
214996fd7ce5SGreg Kroah-Hartman 		 */
21501256937fSPeter Hurley 		if (tty_hung_up_p(filp))
215196fd7ce5SGreg Kroah-Hartman 			filp->f_op = &tty_fops;
215296fd7ce5SGreg Kroah-Hartman 		goto retry_open;
215396fd7ce5SGreg Kroah-Hartman 	}
2154d4855e1fSPeter Hurley 	clear_bit(TTY_HUPPED, &tty->flags);
215596fd7ce5SGreg Kroah-Hartman 
215696fd7ce5SGreg Kroah-Hartman 
21572c411c11SPeter Hurley 	read_lock(&tasklist_lock);
215896fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&current->sighand->siglock);
215911e1d4aaSPeter Hurley 	noctty = (filp->f_flags & O_NOCTTY) ||
2160da5a0fc6SPeter Hurley 			(IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) ||
216111e1d4aaSPeter Hurley 			device == MKDEV(TTYAUX_MAJOR, 1) ||
216211e1d4aaSPeter Hurley 			(tty->driver->type == TTY_DRIVER_TYPE_PTY &&
216311e1d4aaSPeter Hurley 			 tty->driver->subtype == PTY_TYPE_MASTER);
216411e1d4aaSPeter Hurley 
216596fd7ce5SGreg Kroah-Hartman 	if (!noctty &&
216696fd7ce5SGreg Kroah-Hartman 	    current->signal->leader &&
216796fd7ce5SGreg Kroah-Hartman 	    !current->signal->tty &&
21680c556271SJann Horn 	    tty->session == NULL) {
21690c556271SJann Horn 		/*
21700c556271SJann Horn 		 * Don't let a process that only has write access to the tty
21710c556271SJann Horn 		 * obtain the privileges associated with having a tty as
21720c556271SJann Horn 		 * controlling terminal (being able to reopen it with full
21730c556271SJann Horn 		 * access through /dev/tty, being able to perform pushback).
21740c556271SJann Horn 		 * Many distributions set the group of all ttys to "tty" and
21750c556271SJann Horn 		 * grant write-only access to all terminals for setgid tty
21760c556271SJann Horn 		 * binaries, which should not imply full privileges on all ttys.
21770c556271SJann Horn 		 *
21780c556271SJann Horn 		 * This could theoretically break old code that performs open()
21790c556271SJann Horn 		 * on a write-only file descriptor. In that case, it might be
21800c556271SJann Horn 		 * necessary to also permit this if
21810c556271SJann Horn 		 * inode_permission(inode, MAY_READ) == 0.
21820c556271SJann Horn 		 */
21830c556271SJann Horn 		if (filp->f_mode & FMODE_READ)
2184bce65f18SPeter Hurley 			__proc_set_tty(tty);
21850c556271SJann Horn 	}
218696fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&current->sighand->siglock);
21872c411c11SPeter Hurley 	read_unlock(&tasklist_lock);
218889c8d91eSAlan Cox 	tty_unlock(tty);
218996fd7ce5SGreg Kroah-Hartman 	return 0;
219096fd7ce5SGreg Kroah-Hartman }
219196fd7ce5SGreg Kroah-Hartman 
219296fd7ce5SGreg Kroah-Hartman 
219396fd7ce5SGreg Kroah-Hartman 
219496fd7ce5SGreg Kroah-Hartman /**
219596fd7ce5SGreg Kroah-Hartman  *	tty_poll	-	check tty status
219696fd7ce5SGreg Kroah-Hartman  *	@filp: file being polled
219796fd7ce5SGreg Kroah-Hartman  *	@wait: poll wait structures to update
219896fd7ce5SGreg Kroah-Hartman  *
219996fd7ce5SGreg Kroah-Hartman  *	Call the line discipline polling method to obtain the poll
220096fd7ce5SGreg Kroah-Hartman  *	status of the device.
220196fd7ce5SGreg Kroah-Hartman  *
220296fd7ce5SGreg Kroah-Hartman  *	Locking: locks called line discipline but ldisc poll method
220396fd7ce5SGreg Kroah-Hartman  *	may be re-entered freely by other callers.
220496fd7ce5SGreg Kroah-Hartman  */
220596fd7ce5SGreg Kroah-Hartman 
220696fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *filp, poll_table *wait)
220796fd7ce5SGreg Kroah-Hartman {
220896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
220996fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
221096fd7ce5SGreg Kroah-Hartman 	int ret = 0;
221196fd7ce5SGreg Kroah-Hartman 
22126131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
221396fd7ce5SGreg Kroah-Hartman 		return 0;
221496fd7ce5SGreg Kroah-Hartman 
221596fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
2216e55afd11SPeter Hurley 	if (!ld)
2217e55afd11SPeter Hurley 		return hung_up_tty_poll(filp, wait);
221896fd7ce5SGreg Kroah-Hartman 	if (ld->ops->poll)
2219c961bfb1SPeter Hurley 		ret = ld->ops->poll(tty, filp, wait);
222096fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
222196fd7ce5SGreg Kroah-Hartman 	return ret;
222296fd7ce5SGreg Kroah-Hartman }
222396fd7ce5SGreg Kroah-Hartman 
222496fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on)
222596fd7ce5SGreg Kroah-Hartman {
222696fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
222796fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
222896fd7ce5SGreg Kroah-Hartman 	int retval = 0;
222996fd7ce5SGreg Kroah-Hartman 
22306131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
223196fd7ce5SGreg Kroah-Hartman 		goto out;
223296fd7ce5SGreg Kroah-Hartman 
223396fd7ce5SGreg Kroah-Hartman 	retval = fasync_helper(fd, filp, on, &tty->fasync);
223496fd7ce5SGreg Kroah-Hartman 	if (retval <= 0)
223596fd7ce5SGreg Kroah-Hartman 		goto out;
223696fd7ce5SGreg Kroah-Hartman 
223796fd7ce5SGreg Kroah-Hartman 	if (on) {
223896fd7ce5SGreg Kroah-Hartman 		enum pid_type type;
223996fd7ce5SGreg Kroah-Hartman 		struct pid *pid;
2240f6c8dbe6SPeter Hurley 
224196fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&tty->ctrl_lock, flags);
224296fd7ce5SGreg Kroah-Hartman 		if (tty->pgrp) {
224396fd7ce5SGreg Kroah-Hartman 			pid = tty->pgrp;
224496fd7ce5SGreg Kroah-Hartman 			type = PIDTYPE_PGID;
224596fd7ce5SGreg Kroah-Hartman 		} else {
224696fd7ce5SGreg Kroah-Hartman 			pid = task_pid(current);
224796fd7ce5SGreg Kroah-Hartman 			type = PIDTYPE_PID;
224896fd7ce5SGreg Kroah-Hartman 		}
224996fd7ce5SGreg Kroah-Hartman 		get_pid(pid);
225096fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2251e0b93eddSJeff Layton 		__f_setown(filp, pid, type, 0);
225296fd7ce5SGreg Kroah-Hartman 		put_pid(pid);
2253e0b93eddSJeff Layton 		retval = 0;
225496fd7ce5SGreg Kroah-Hartman 	}
225596fd7ce5SGreg Kroah-Hartman out:
225696fd7ce5SGreg Kroah-Hartman 	return retval;
225796fd7ce5SGreg Kroah-Hartman }
225896fd7ce5SGreg Kroah-Hartman 
225996fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on)
226096fd7ce5SGreg Kroah-Hartman {
226189c8d91eSAlan Cox 	struct tty_struct *tty = file_tty(filp);
2262a8f3a297SPeter Hurley 	int retval = -ENOTTY;
226389c8d91eSAlan Cox 
226489c8d91eSAlan Cox 	tty_lock(tty);
2265a8f3a297SPeter Hurley 	if (!tty_hung_up_p(filp))
226696fd7ce5SGreg Kroah-Hartman 		retval = __tty_fasync(fd, filp, on);
226789c8d91eSAlan Cox 	tty_unlock(tty);
226889c8d91eSAlan Cox 
226996fd7ce5SGreg Kroah-Hartman 	return retval;
227096fd7ce5SGreg Kroah-Hartman }
227196fd7ce5SGreg Kroah-Hartman 
227296fd7ce5SGreg Kroah-Hartman /**
227396fd7ce5SGreg Kroah-Hartman  *	tiocsti			-	fake input character
227496fd7ce5SGreg Kroah-Hartman  *	@tty: tty to fake input into
227596fd7ce5SGreg Kroah-Hartman  *	@p: pointer to character
227696fd7ce5SGreg Kroah-Hartman  *
227796fd7ce5SGreg Kroah-Hartman  *	Fake input to a tty device. Does the necessary locking and
227896fd7ce5SGreg Kroah-Hartman  *	input management.
227996fd7ce5SGreg Kroah-Hartman  *
228096fd7ce5SGreg Kroah-Hartman  *	FIXME: does not honour flow control ??
228196fd7ce5SGreg Kroah-Hartman  *
228296fd7ce5SGreg Kroah-Hartman  *	Locking:
2283137084bbSPeter Hurley  *		Called functions take tty_ldiscs_lock
228496fd7ce5SGreg Kroah-Hartman  *		current->signal->tty check is safe without locks
228596fd7ce5SGreg Kroah-Hartman  *
228696fd7ce5SGreg Kroah-Hartman  *	FIXME: may race normal receive processing
228796fd7ce5SGreg Kroah-Hartman  */
228896fd7ce5SGreg Kroah-Hartman 
228996fd7ce5SGreg Kroah-Hartman static int tiocsti(struct tty_struct *tty, char __user *p)
229096fd7ce5SGreg Kroah-Hartman {
229196fd7ce5SGreg Kroah-Hartman 	char ch, mbz = 0;
229296fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
229396fd7ce5SGreg Kroah-Hartman 
229496fd7ce5SGreg Kroah-Hartman 	if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
229596fd7ce5SGreg Kroah-Hartman 		return -EPERM;
229696fd7ce5SGreg Kroah-Hartman 	if (get_user(ch, p))
229796fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
229896fd7ce5SGreg Kroah-Hartman 	tty_audit_tiocsti(tty, ch);
229996fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
2300e55afd11SPeter Hurley 	if (!ld)
2301e55afd11SPeter Hurley 		return -EIO;
230296fd7ce5SGreg Kroah-Hartman 	ld->ops->receive_buf(tty, &ch, &mbz, 1);
230396fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
230496fd7ce5SGreg Kroah-Hartman 	return 0;
230596fd7ce5SGreg Kroah-Hartman }
230696fd7ce5SGreg Kroah-Hartman 
230796fd7ce5SGreg Kroah-Hartman /**
230896fd7ce5SGreg Kroah-Hartman  *	tiocgwinsz		-	implement window query ioctl
230996fd7ce5SGreg Kroah-Hartman  *	@tty; tty
231096fd7ce5SGreg Kroah-Hartman  *	@arg: user buffer for result
231196fd7ce5SGreg Kroah-Hartman  *
231296fd7ce5SGreg Kroah-Hartman  *	Copies the kernel idea of the window size into the user buffer.
231396fd7ce5SGreg Kroah-Hartman  *
2314dee4a0beSPeter Hurley  *	Locking: tty->winsize_mutex is taken to ensure the winsize data
231596fd7ce5SGreg Kroah-Hartman  *		is consistent.
231696fd7ce5SGreg Kroah-Hartman  */
231796fd7ce5SGreg Kroah-Hartman 
231896fd7ce5SGreg Kroah-Hartman static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
231996fd7ce5SGreg Kroah-Hartman {
232096fd7ce5SGreg Kroah-Hartman 	int err;
232196fd7ce5SGreg Kroah-Hartman 
2322dee4a0beSPeter Hurley 	mutex_lock(&tty->winsize_mutex);
232396fd7ce5SGreg Kroah-Hartman 	err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2324dee4a0beSPeter Hurley 	mutex_unlock(&tty->winsize_mutex);
232596fd7ce5SGreg Kroah-Hartman 
232696fd7ce5SGreg Kroah-Hartman 	return err ? -EFAULT: 0;
232796fd7ce5SGreg Kroah-Hartman }
232896fd7ce5SGreg Kroah-Hartman 
232996fd7ce5SGreg Kroah-Hartman /**
233096fd7ce5SGreg Kroah-Hartman  *	tty_do_resize		-	resize event
233196fd7ce5SGreg Kroah-Hartman  *	@tty: tty being resized
233296fd7ce5SGreg Kroah-Hartman  *	@rows: rows (character)
233396fd7ce5SGreg Kroah-Hartman  *	@cols: cols (character)
233496fd7ce5SGreg Kroah-Hartman  *
233596fd7ce5SGreg Kroah-Hartman  *	Update the termios variables and send the necessary signals to
233696fd7ce5SGreg Kroah-Hartman  *	peform a terminal resize correctly
233796fd7ce5SGreg Kroah-Hartman  */
233896fd7ce5SGreg Kroah-Hartman 
233996fd7ce5SGreg Kroah-Hartman int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
234096fd7ce5SGreg Kroah-Hartman {
234196fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
234296fd7ce5SGreg Kroah-Hartman 
234396fd7ce5SGreg Kroah-Hartman 	/* Lock the tty */
2344dee4a0beSPeter Hurley 	mutex_lock(&tty->winsize_mutex);
234596fd7ce5SGreg Kroah-Hartman 	if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
234696fd7ce5SGreg Kroah-Hartman 		goto done;
234796fd7ce5SGreg Kroah-Hartman 
23485b239542SPeter Hurley 	/* Signal the foreground process group */
23495b239542SPeter Hurley 	pgrp = tty_get_pgrp(tty);
235096fd7ce5SGreg Kroah-Hartman 	if (pgrp)
235196fd7ce5SGreg Kroah-Hartman 		kill_pgrp(pgrp, SIGWINCH, 1);
235296fd7ce5SGreg Kroah-Hartman 	put_pid(pgrp);
235396fd7ce5SGreg Kroah-Hartman 
235496fd7ce5SGreg Kroah-Hartman 	tty->winsize = *ws;
235596fd7ce5SGreg Kroah-Hartman done:
2356dee4a0beSPeter Hurley 	mutex_unlock(&tty->winsize_mutex);
235796fd7ce5SGreg Kroah-Hartman 	return 0;
235896fd7ce5SGreg Kroah-Hartman }
23594d334fd1SMartin Schwidefsky EXPORT_SYMBOL(tty_do_resize);
236096fd7ce5SGreg Kroah-Hartman 
236196fd7ce5SGreg Kroah-Hartman /**
236296fd7ce5SGreg Kroah-Hartman  *	tiocswinsz		-	implement window size set ioctl
236396fd7ce5SGreg Kroah-Hartman  *	@tty; tty side of tty
236496fd7ce5SGreg Kroah-Hartman  *	@arg: user buffer for result
236596fd7ce5SGreg Kroah-Hartman  *
236696fd7ce5SGreg Kroah-Hartman  *	Copies the user idea of the window size to the kernel. Traditionally
236796fd7ce5SGreg Kroah-Hartman  *	this is just advisory information but for the Linux console it
236896fd7ce5SGreg Kroah-Hartman  *	actually has driver level meaning and triggers a VC resize.
236996fd7ce5SGreg Kroah-Hartman  *
237096fd7ce5SGreg Kroah-Hartman  *	Locking:
237125985edcSLucas De Marchi  *		Driver dependent. The default do_resize method takes the
237296fd7ce5SGreg Kroah-Hartman  *	tty termios mutex and ctrl_lock. The console takes its own lock
237396fd7ce5SGreg Kroah-Hartman  *	then calls into the default method.
237496fd7ce5SGreg Kroah-Hartman  */
237596fd7ce5SGreg Kroah-Hartman 
237696fd7ce5SGreg Kroah-Hartman static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
237796fd7ce5SGreg Kroah-Hartman {
237896fd7ce5SGreg Kroah-Hartman 	struct winsize tmp_ws;
237996fd7ce5SGreg Kroah-Hartman 	if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
238096fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
238196fd7ce5SGreg Kroah-Hartman 
238296fd7ce5SGreg Kroah-Hartman 	if (tty->ops->resize)
238396fd7ce5SGreg Kroah-Hartman 		return tty->ops->resize(tty, &tmp_ws);
238496fd7ce5SGreg Kroah-Hartman 	else
238596fd7ce5SGreg Kroah-Hartman 		return tty_do_resize(tty, &tmp_ws);
238696fd7ce5SGreg Kroah-Hartman }
238796fd7ce5SGreg Kroah-Hartman 
238896fd7ce5SGreg Kroah-Hartman /**
238996fd7ce5SGreg Kroah-Hartman  *	tioccons	-	allow admin to move logical console
239096fd7ce5SGreg Kroah-Hartman  *	@file: the file to become console
239196fd7ce5SGreg Kroah-Hartman  *
239225985edcSLucas De Marchi  *	Allow the administrator to move the redirected console device
239396fd7ce5SGreg Kroah-Hartman  *
239496fd7ce5SGreg Kroah-Hartman  *	Locking: uses redirect_lock to guard the redirect information
239596fd7ce5SGreg Kroah-Hartman  */
239696fd7ce5SGreg Kroah-Hartman 
239796fd7ce5SGreg Kroah-Hartman static int tioccons(struct file *file)
239896fd7ce5SGreg Kroah-Hartman {
239996fd7ce5SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN))
240096fd7ce5SGreg Kroah-Hartman 		return -EPERM;
240196fd7ce5SGreg Kroah-Hartman 	if (file->f_op->write == redirected_tty_write) {
240296fd7ce5SGreg Kroah-Hartman 		struct file *f;
240396fd7ce5SGreg Kroah-Hartman 		spin_lock(&redirect_lock);
240496fd7ce5SGreg Kroah-Hartman 		f = redirect;
240596fd7ce5SGreg Kroah-Hartman 		redirect = NULL;
240696fd7ce5SGreg Kroah-Hartman 		spin_unlock(&redirect_lock);
240796fd7ce5SGreg Kroah-Hartman 		if (f)
240896fd7ce5SGreg Kroah-Hartman 			fput(f);
240996fd7ce5SGreg Kroah-Hartman 		return 0;
241096fd7ce5SGreg Kroah-Hartman 	}
241196fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
241296fd7ce5SGreg Kroah-Hartman 	if (redirect) {
241396fd7ce5SGreg Kroah-Hartman 		spin_unlock(&redirect_lock);
241496fd7ce5SGreg Kroah-Hartman 		return -EBUSY;
241596fd7ce5SGreg Kroah-Hartman 	}
2416cb0942b8SAl Viro 	redirect = get_file(file);
241796fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
241896fd7ce5SGreg Kroah-Hartman 	return 0;
241996fd7ce5SGreg Kroah-Hartman }
242096fd7ce5SGreg Kroah-Hartman 
242196fd7ce5SGreg Kroah-Hartman /**
242296fd7ce5SGreg Kroah-Hartman  *	fionbio		-	non blocking ioctl
242396fd7ce5SGreg Kroah-Hartman  *	@file: file to set blocking value
242496fd7ce5SGreg Kroah-Hartman  *	@p: user parameter
242596fd7ce5SGreg Kroah-Hartman  *
242696fd7ce5SGreg Kroah-Hartman  *	Historical tty interfaces had a blocking control ioctl before
242796fd7ce5SGreg Kroah-Hartman  *	the generic functionality existed. This piece of history is preserved
242896fd7ce5SGreg Kroah-Hartman  *	in the expected tty API of posix OS's.
242996fd7ce5SGreg Kroah-Hartman  *
243096fd7ce5SGreg Kroah-Hartman  *	Locking: none, the open file handle ensures it won't go away.
243196fd7ce5SGreg Kroah-Hartman  */
243296fd7ce5SGreg Kroah-Hartman 
243396fd7ce5SGreg Kroah-Hartman static int fionbio(struct file *file, int __user *p)
243496fd7ce5SGreg Kroah-Hartman {
243596fd7ce5SGreg Kroah-Hartman 	int nonblock;
243696fd7ce5SGreg Kroah-Hartman 
243796fd7ce5SGreg Kroah-Hartman 	if (get_user(nonblock, p))
243896fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
243996fd7ce5SGreg Kroah-Hartman 
244096fd7ce5SGreg Kroah-Hartman 	spin_lock(&file->f_lock);
244196fd7ce5SGreg Kroah-Hartman 	if (nonblock)
244296fd7ce5SGreg Kroah-Hartman 		file->f_flags |= O_NONBLOCK;
244396fd7ce5SGreg Kroah-Hartman 	else
244496fd7ce5SGreg Kroah-Hartman 		file->f_flags &= ~O_NONBLOCK;
244596fd7ce5SGreg Kroah-Hartman 	spin_unlock(&file->f_lock);
244696fd7ce5SGreg Kroah-Hartman 	return 0;
244796fd7ce5SGreg Kroah-Hartman }
244896fd7ce5SGreg Kroah-Hartman 
244996fd7ce5SGreg Kroah-Hartman /**
245096fd7ce5SGreg Kroah-Hartman  *	tiocsctty	-	set controlling tty
245196fd7ce5SGreg Kroah-Hartman  *	@tty: tty structure
245296fd7ce5SGreg Kroah-Hartman  *	@arg: user argument
245396fd7ce5SGreg Kroah-Hartman  *
245496fd7ce5SGreg Kroah-Hartman  *	This ioctl is used to manage job control. It permits a session
245596fd7ce5SGreg Kroah-Hartman  *	leader to set this tty as the controlling tty for the session.
245696fd7ce5SGreg Kroah-Hartman  *
245796fd7ce5SGreg Kroah-Hartman  *	Locking:
2458e218eb32SPeter Hurley  *		Takes tty_lock() to serialize proc_set_tty() for this tty
245996fd7ce5SGreg Kroah-Hartman  *		Takes tasklist_lock internally to walk sessions
246096fd7ce5SGreg Kroah-Hartman  *		Takes ->siglock() when updating signal->tty
246196fd7ce5SGreg Kroah-Hartman  */
246296fd7ce5SGreg Kroah-Hartman 
24630c556271SJann Horn static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
246496fd7ce5SGreg Kroah-Hartman {
246596fd7ce5SGreg Kroah-Hartman 	int ret = 0;
246696fd7ce5SGreg Kroah-Hartman 
2467e218eb32SPeter Hurley 	tty_lock(tty);
24682c411c11SPeter Hurley 	read_lock(&tasklist_lock);
24692c411c11SPeter Hurley 
24702c411c11SPeter Hurley 	if (current->signal->leader && (task_session(current) == tty->session))
24712c411c11SPeter Hurley 		goto unlock;
24722c411c11SPeter Hurley 
247396fd7ce5SGreg Kroah-Hartman 	/*
247496fd7ce5SGreg Kroah-Hartman 	 * The process must be a session leader and
247596fd7ce5SGreg Kroah-Hartman 	 * not have a controlling tty already.
247696fd7ce5SGreg Kroah-Hartman 	 */
247796fd7ce5SGreg Kroah-Hartman 	if (!current->signal->leader || current->signal->tty) {
247896fd7ce5SGreg Kroah-Hartman 		ret = -EPERM;
247996fd7ce5SGreg Kroah-Hartman 		goto unlock;
248096fd7ce5SGreg Kroah-Hartman 	}
248196fd7ce5SGreg Kroah-Hartman 
248296fd7ce5SGreg Kroah-Hartman 	if (tty->session) {
248396fd7ce5SGreg Kroah-Hartman 		/*
248496fd7ce5SGreg Kroah-Hartman 		 * This tty is already the controlling
248596fd7ce5SGreg Kroah-Hartman 		 * tty for another session group!
248696fd7ce5SGreg Kroah-Hartman 		 */
248796fd7ce5SGreg Kroah-Hartman 		if (arg == 1 && capable(CAP_SYS_ADMIN)) {
248896fd7ce5SGreg Kroah-Hartman 			/*
248996fd7ce5SGreg Kroah-Hartman 			 * Steal it away
249096fd7ce5SGreg Kroah-Hartman 			 */
249196fd7ce5SGreg Kroah-Hartman 			session_clear_tty(tty->session);
249296fd7ce5SGreg Kroah-Hartman 		} else {
249396fd7ce5SGreg Kroah-Hartman 			ret = -EPERM;
249496fd7ce5SGreg Kroah-Hartman 			goto unlock;
249596fd7ce5SGreg Kroah-Hartman 		}
249696fd7ce5SGreg Kroah-Hartman 	}
24970c556271SJann Horn 
24980c556271SJann Horn 	/* See the comment in tty_open(). */
24990c556271SJann Horn 	if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
25000c556271SJann Horn 		ret = -EPERM;
25010c556271SJann Horn 		goto unlock;
25020c556271SJann Horn 	}
25030c556271SJann Horn 
2504bce65f18SPeter Hurley 	proc_set_tty(tty);
250596fd7ce5SGreg Kroah-Hartman unlock:
25062c411c11SPeter Hurley 	read_unlock(&tasklist_lock);
2507e218eb32SPeter Hurley 	tty_unlock(tty);
250896fd7ce5SGreg Kroah-Hartman 	return ret;
250996fd7ce5SGreg Kroah-Hartman }
251096fd7ce5SGreg Kroah-Hartman 
251196fd7ce5SGreg Kroah-Hartman /**
251296fd7ce5SGreg Kroah-Hartman  *	tty_get_pgrp	-	return a ref counted pgrp pid
251396fd7ce5SGreg Kroah-Hartman  *	@tty: tty to read
251496fd7ce5SGreg Kroah-Hartman  *
251596fd7ce5SGreg Kroah-Hartman  *	Returns a refcounted instance of the pid struct for the process
251696fd7ce5SGreg Kroah-Hartman  *	group controlling the tty.
251796fd7ce5SGreg Kroah-Hartman  */
251896fd7ce5SGreg Kroah-Hartman 
251996fd7ce5SGreg Kroah-Hartman struct pid *tty_get_pgrp(struct tty_struct *tty)
252096fd7ce5SGreg Kroah-Hartman {
252196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
252296fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
252396fd7ce5SGreg Kroah-Hartman 
252496fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
252596fd7ce5SGreg Kroah-Hartman 	pgrp = get_pid(tty->pgrp);
252696fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
252796fd7ce5SGreg Kroah-Hartman 
252896fd7ce5SGreg Kroah-Hartman 	return pgrp;
252996fd7ce5SGreg Kroah-Hartman }
253096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_get_pgrp);
253196fd7ce5SGreg Kroah-Hartman 
2532e1c2296cSPeter Hurley /*
2533e1c2296cSPeter Hurley  * This checks not only the pgrp, but falls back on the pid if no
2534e1c2296cSPeter Hurley  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
2535e1c2296cSPeter Hurley  * without this...
2536e1c2296cSPeter Hurley  *
2537e1c2296cSPeter Hurley  * The caller must hold rcu lock or the tasklist lock.
2538e1c2296cSPeter Hurley  */
2539e1c2296cSPeter Hurley static struct pid *session_of_pgrp(struct pid *pgrp)
2540e1c2296cSPeter Hurley {
2541e1c2296cSPeter Hurley 	struct task_struct *p;
2542e1c2296cSPeter Hurley 	struct pid *sid = NULL;
2543e1c2296cSPeter Hurley 
2544e1c2296cSPeter Hurley 	p = pid_task(pgrp, PIDTYPE_PGID);
2545e1c2296cSPeter Hurley 	if (p == NULL)
2546e1c2296cSPeter Hurley 		p = pid_task(pgrp, PIDTYPE_PID);
2547e1c2296cSPeter Hurley 	if (p != NULL)
2548e1c2296cSPeter Hurley 		sid = task_session(p);
2549e1c2296cSPeter Hurley 
2550e1c2296cSPeter Hurley 	return sid;
2551e1c2296cSPeter Hurley }
2552e1c2296cSPeter Hurley 
255396fd7ce5SGreg Kroah-Hartman /**
255496fd7ce5SGreg Kroah-Hartman  *	tiocgpgrp		-	get process group
255596fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
255625985edcSLucas De Marchi  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
255796fd7ce5SGreg Kroah-Hartman  *	@p: returned pid
255896fd7ce5SGreg Kroah-Hartman  *
255996fd7ce5SGreg Kroah-Hartman  *	Obtain the process group of the tty. If there is no process group
256096fd7ce5SGreg Kroah-Hartman  *	return an error.
256196fd7ce5SGreg Kroah-Hartman  *
256296fd7ce5SGreg Kroah-Hartman  *	Locking: none. Reference to current->signal->tty is safe.
256396fd7ce5SGreg Kroah-Hartman  */
256496fd7ce5SGreg Kroah-Hartman 
256596fd7ce5SGreg Kroah-Hartman static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
256696fd7ce5SGreg Kroah-Hartman {
256796fd7ce5SGreg Kroah-Hartman 	struct pid *pid;
256896fd7ce5SGreg Kroah-Hartman 	int ret;
256996fd7ce5SGreg Kroah-Hartman 	/*
257096fd7ce5SGreg Kroah-Hartman 	 * (tty == real_tty) is a cheap way of
257196fd7ce5SGreg Kroah-Hartman 	 * testing if the tty is NOT a master pty.
257296fd7ce5SGreg Kroah-Hartman 	 */
257396fd7ce5SGreg Kroah-Hartman 	if (tty == real_tty && current->signal->tty != real_tty)
257496fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
257596fd7ce5SGreg Kroah-Hartman 	pid = tty_get_pgrp(real_tty);
257696fd7ce5SGreg Kroah-Hartman 	ret =  put_user(pid_vnr(pid), p);
257796fd7ce5SGreg Kroah-Hartman 	put_pid(pid);
257896fd7ce5SGreg Kroah-Hartman 	return ret;
257996fd7ce5SGreg Kroah-Hartman }
258096fd7ce5SGreg Kroah-Hartman 
258196fd7ce5SGreg Kroah-Hartman /**
258296fd7ce5SGreg Kroah-Hartman  *	tiocspgrp		-	attempt to set process group
258396fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
258496fd7ce5SGreg Kroah-Hartman  *	@real_tty: tty side device matching tty passed by user
258596fd7ce5SGreg Kroah-Hartman  *	@p: pid pointer
258696fd7ce5SGreg Kroah-Hartman  *
258796fd7ce5SGreg Kroah-Hartman  *	Set the process group of the tty to the session passed. Only
258896fd7ce5SGreg Kroah-Hartman  *	permitted where the tty session is our session.
258996fd7ce5SGreg Kroah-Hartman  *
259096fd7ce5SGreg Kroah-Hartman  *	Locking: RCU, ctrl lock
259196fd7ce5SGreg Kroah-Hartman  */
259296fd7ce5SGreg Kroah-Hartman 
259396fd7ce5SGreg Kroah-Hartman static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
259496fd7ce5SGreg Kroah-Hartman {
259596fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
259696fd7ce5SGreg Kroah-Hartman 	pid_t pgrp_nr;
259796fd7ce5SGreg Kroah-Hartman 	int retval = tty_check_change(real_tty);
259896fd7ce5SGreg Kroah-Hartman 
259996fd7ce5SGreg Kroah-Hartman 	if (retval == -EIO)
260096fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
260196fd7ce5SGreg Kroah-Hartman 	if (retval)
260296fd7ce5SGreg Kroah-Hartman 		return retval;
260396fd7ce5SGreg Kroah-Hartman 	if (!current->signal->tty ||
260496fd7ce5SGreg Kroah-Hartman 	    (current->signal->tty != real_tty) ||
260596fd7ce5SGreg Kroah-Hartman 	    (real_tty->session != task_session(current)))
260696fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
260796fd7ce5SGreg Kroah-Hartman 	if (get_user(pgrp_nr, p))
260896fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
260996fd7ce5SGreg Kroah-Hartman 	if (pgrp_nr < 0)
261096fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
261196fd7ce5SGreg Kroah-Hartman 	rcu_read_lock();
261296fd7ce5SGreg Kroah-Hartman 	pgrp = find_vpid(pgrp_nr);
261396fd7ce5SGreg Kroah-Hartman 	retval = -ESRCH;
261496fd7ce5SGreg Kroah-Hartman 	if (!pgrp)
261596fd7ce5SGreg Kroah-Hartman 		goto out_unlock;
261696fd7ce5SGreg Kroah-Hartman 	retval = -EPERM;
261796fd7ce5SGreg Kroah-Hartman 	if (session_of_pgrp(pgrp) != task_session(current))
261896fd7ce5SGreg Kroah-Hartman 		goto out_unlock;
261996fd7ce5SGreg Kroah-Hartman 	retval = 0;
26201e86b5bfSPeter Hurley 	spin_lock_irq(&tty->ctrl_lock);
262196fd7ce5SGreg Kroah-Hartman 	put_pid(real_tty->pgrp);
262296fd7ce5SGreg Kroah-Hartman 	real_tty->pgrp = get_pid(pgrp);
26231e86b5bfSPeter Hurley 	spin_unlock_irq(&tty->ctrl_lock);
262496fd7ce5SGreg Kroah-Hartman out_unlock:
262596fd7ce5SGreg Kroah-Hartman 	rcu_read_unlock();
262696fd7ce5SGreg Kroah-Hartman 	return retval;
262796fd7ce5SGreg Kroah-Hartman }
262896fd7ce5SGreg Kroah-Hartman 
262996fd7ce5SGreg Kroah-Hartman /**
263096fd7ce5SGreg Kroah-Hartman  *	tiocgsid		-	get session id
263196fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
263225985edcSLucas De Marchi  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
263396fd7ce5SGreg Kroah-Hartman  *	@p: pointer to returned session id
263496fd7ce5SGreg Kroah-Hartman  *
263596fd7ce5SGreg Kroah-Hartman  *	Obtain the session id of the tty. If there is no session
263696fd7ce5SGreg Kroah-Hartman  *	return an error.
263796fd7ce5SGreg Kroah-Hartman  *
263896fd7ce5SGreg Kroah-Hartman  *	Locking: none. Reference to current->signal->tty is safe.
263996fd7ce5SGreg Kroah-Hartman  */
264096fd7ce5SGreg Kroah-Hartman 
264196fd7ce5SGreg Kroah-Hartman static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
264296fd7ce5SGreg Kroah-Hartman {
264396fd7ce5SGreg Kroah-Hartman 	/*
264496fd7ce5SGreg Kroah-Hartman 	 * (tty == real_tty) is a cheap way of
264596fd7ce5SGreg Kroah-Hartman 	 * testing if the tty is NOT a master pty.
264696fd7ce5SGreg Kroah-Hartman 	*/
264796fd7ce5SGreg Kroah-Hartman 	if (tty == real_tty && current->signal->tty != real_tty)
264896fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
264996fd7ce5SGreg Kroah-Hartman 	if (!real_tty->session)
265096fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
265196fd7ce5SGreg Kroah-Hartman 	return put_user(pid_vnr(real_tty->session), p);
265296fd7ce5SGreg Kroah-Hartman }
265396fd7ce5SGreg Kroah-Hartman 
265496fd7ce5SGreg Kroah-Hartman /**
265596fd7ce5SGreg Kroah-Hartman  *	tiocsetd	-	set line discipline
265696fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
265796fd7ce5SGreg Kroah-Hartman  *	@p: pointer to user data
265896fd7ce5SGreg Kroah-Hartman  *
265996fd7ce5SGreg Kroah-Hartman  *	Set the line discipline according to user request.
266096fd7ce5SGreg Kroah-Hartman  *
266196fd7ce5SGreg Kroah-Hartman  *	Locking: see tty_set_ldisc, this function is just a helper
266296fd7ce5SGreg Kroah-Hartman  */
266396fd7ce5SGreg Kroah-Hartman 
266496fd7ce5SGreg Kroah-Hartman static int tiocsetd(struct tty_struct *tty, int __user *p)
266596fd7ce5SGreg Kroah-Hartman {
2666c12da96fSPeter Hurley 	int disc;
266796fd7ce5SGreg Kroah-Hartman 	int ret;
266896fd7ce5SGreg Kroah-Hartman 
2669c12da96fSPeter Hurley 	if (get_user(disc, p))
267096fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
267196fd7ce5SGreg Kroah-Hartman 
2672c12da96fSPeter Hurley 	ret = tty_set_ldisc(tty, disc);
267396fd7ce5SGreg Kroah-Hartman 
267496fd7ce5SGreg Kroah-Hartman 	return ret;
267596fd7ce5SGreg Kroah-Hartman }
267696fd7ce5SGreg Kroah-Hartman 
267796fd7ce5SGreg Kroah-Hartman /**
26785c17c861SPeter Hurley  *	tiocgetd	-	get line discipline
26795c17c861SPeter Hurley  *	@tty: tty device
26805c17c861SPeter Hurley  *	@p: pointer to user data
26815c17c861SPeter Hurley  *
26825c17c861SPeter Hurley  *	Retrieves the line discipline id directly from the ldisc.
26835c17c861SPeter Hurley  *
26845c17c861SPeter Hurley  *	Locking: waits for ldisc reference (in case the line discipline
26855c17c861SPeter Hurley  *		is changing or the tty is being hungup)
26865c17c861SPeter Hurley  */
26875c17c861SPeter Hurley 
26885c17c861SPeter Hurley static int tiocgetd(struct tty_struct *tty, int __user *p)
26895c17c861SPeter Hurley {
26905c17c861SPeter Hurley 	struct tty_ldisc *ld;
26915c17c861SPeter Hurley 	int ret;
26925c17c861SPeter Hurley 
26935c17c861SPeter Hurley 	ld = tty_ldisc_ref_wait(tty);
2694e55afd11SPeter Hurley 	if (!ld)
2695e55afd11SPeter Hurley 		return -EIO;
26965c17c861SPeter Hurley 	ret = put_user(ld->ops->num, p);
26975c17c861SPeter Hurley 	tty_ldisc_deref(ld);
26985c17c861SPeter Hurley 	return ret;
26995c17c861SPeter Hurley }
27005c17c861SPeter Hurley 
27015c17c861SPeter Hurley /**
270296fd7ce5SGreg Kroah-Hartman  *	send_break	-	performed time break
270396fd7ce5SGreg Kroah-Hartman  *	@tty: device to break on
270496fd7ce5SGreg Kroah-Hartman  *	@duration: timeout in mS
270596fd7ce5SGreg Kroah-Hartman  *
270696fd7ce5SGreg Kroah-Hartman  *	Perform a timed break on hardware that lacks its own driver level
270796fd7ce5SGreg Kroah-Hartman  *	timed break functionality.
270896fd7ce5SGreg Kroah-Hartman  *
270996fd7ce5SGreg Kroah-Hartman  *	Locking:
271096fd7ce5SGreg Kroah-Hartman  *		atomic_write_lock serializes
271196fd7ce5SGreg Kroah-Hartman  *
271296fd7ce5SGreg Kroah-Hartman  */
271396fd7ce5SGreg Kroah-Hartman 
271496fd7ce5SGreg Kroah-Hartman static int send_break(struct tty_struct *tty, unsigned int duration)
271596fd7ce5SGreg Kroah-Hartman {
271696fd7ce5SGreg Kroah-Hartman 	int retval;
271796fd7ce5SGreg Kroah-Hartman 
271896fd7ce5SGreg Kroah-Hartman 	if (tty->ops->break_ctl == NULL)
271996fd7ce5SGreg Kroah-Hartman 		return 0;
272096fd7ce5SGreg Kroah-Hartman 
272196fd7ce5SGreg Kroah-Hartman 	if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
272296fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, duration);
272396fd7ce5SGreg Kroah-Hartman 	else {
272496fd7ce5SGreg Kroah-Hartman 		/* Do the work ourselves */
272596fd7ce5SGreg Kroah-Hartman 		if (tty_write_lock(tty, 0) < 0)
272696fd7ce5SGreg Kroah-Hartman 			return -EINTR;
272796fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, -1);
272896fd7ce5SGreg Kroah-Hartman 		if (retval)
272996fd7ce5SGreg Kroah-Hartman 			goto out;
273096fd7ce5SGreg Kroah-Hartman 		if (!signal_pending(current))
273196fd7ce5SGreg Kroah-Hartman 			msleep_interruptible(duration);
273296fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, 0);
273396fd7ce5SGreg Kroah-Hartman out:
273496fd7ce5SGreg Kroah-Hartman 		tty_write_unlock(tty);
273596fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
273696fd7ce5SGreg Kroah-Hartman 			retval = -EINTR;
273796fd7ce5SGreg Kroah-Hartman 	}
273896fd7ce5SGreg Kroah-Hartman 	return retval;
273996fd7ce5SGreg Kroah-Hartman }
274096fd7ce5SGreg Kroah-Hartman 
274196fd7ce5SGreg Kroah-Hartman /**
274296fd7ce5SGreg Kroah-Hartman  *	tty_tiocmget		-	get modem status
274396fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
274496fd7ce5SGreg Kroah-Hartman  *	@file: user file pointer
274596fd7ce5SGreg Kroah-Hartman  *	@p: pointer to result
274696fd7ce5SGreg Kroah-Hartman  *
274796fd7ce5SGreg Kroah-Hartman  *	Obtain the modem status bits from the tty driver if the feature
274896fd7ce5SGreg Kroah-Hartman  *	is supported. Return -EINVAL if it is not available.
274996fd7ce5SGreg Kroah-Hartman  *
275096fd7ce5SGreg Kroah-Hartman  *	Locking: none (up to the driver)
275196fd7ce5SGreg Kroah-Hartman  */
275296fd7ce5SGreg Kroah-Hartman 
275360b33c13SAlan Cox static int tty_tiocmget(struct tty_struct *tty, int __user *p)
275496fd7ce5SGreg Kroah-Hartman {
275596fd7ce5SGreg Kroah-Hartman 	int retval = -EINVAL;
275696fd7ce5SGreg Kroah-Hartman 
275796fd7ce5SGreg Kroah-Hartman 	if (tty->ops->tiocmget) {
275860b33c13SAlan Cox 		retval = tty->ops->tiocmget(tty);
275996fd7ce5SGreg Kroah-Hartman 
276096fd7ce5SGreg Kroah-Hartman 		if (retval >= 0)
276196fd7ce5SGreg Kroah-Hartman 			retval = put_user(retval, p);
276296fd7ce5SGreg Kroah-Hartman 	}
276396fd7ce5SGreg Kroah-Hartman 	return retval;
276496fd7ce5SGreg Kroah-Hartman }
276596fd7ce5SGreg Kroah-Hartman 
276696fd7ce5SGreg Kroah-Hartman /**
276796fd7ce5SGreg Kroah-Hartman  *	tty_tiocmset		-	set modem status
276896fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
276996fd7ce5SGreg Kroah-Hartman  *	@cmd: command - clear bits, set bits or set all
277096fd7ce5SGreg Kroah-Hartman  *	@p: pointer to desired bits
277196fd7ce5SGreg Kroah-Hartman  *
277296fd7ce5SGreg Kroah-Hartman  *	Set the modem status bits from the tty driver if the feature
277396fd7ce5SGreg Kroah-Hartman  *	is supported. Return -EINVAL if it is not available.
277496fd7ce5SGreg Kroah-Hartman  *
277596fd7ce5SGreg Kroah-Hartman  *	Locking: none (up to the driver)
277696fd7ce5SGreg Kroah-Hartman  */
277796fd7ce5SGreg Kroah-Hartman 
277820b9d177SAlan Cox static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
277996fd7ce5SGreg Kroah-Hartman 	     unsigned __user *p)
278096fd7ce5SGreg Kroah-Hartman {
278196fd7ce5SGreg Kroah-Hartman 	int retval;
278296fd7ce5SGreg Kroah-Hartman 	unsigned int set, clear, val;
278396fd7ce5SGreg Kroah-Hartman 
278496fd7ce5SGreg Kroah-Hartman 	if (tty->ops->tiocmset == NULL)
278596fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
278696fd7ce5SGreg Kroah-Hartman 
278796fd7ce5SGreg Kroah-Hartman 	retval = get_user(val, p);
278896fd7ce5SGreg Kroah-Hartman 	if (retval)
278996fd7ce5SGreg Kroah-Hartman 		return retval;
279096fd7ce5SGreg Kroah-Hartman 	set = clear = 0;
279196fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
279296fd7ce5SGreg Kroah-Hartman 	case TIOCMBIS:
279396fd7ce5SGreg Kroah-Hartman 		set = val;
279496fd7ce5SGreg Kroah-Hartman 		break;
279596fd7ce5SGreg Kroah-Hartman 	case TIOCMBIC:
279696fd7ce5SGreg Kroah-Hartman 		clear = val;
279796fd7ce5SGreg Kroah-Hartman 		break;
279896fd7ce5SGreg Kroah-Hartman 	case TIOCMSET:
279996fd7ce5SGreg Kroah-Hartman 		set = val;
280096fd7ce5SGreg Kroah-Hartman 		clear = ~val;
280196fd7ce5SGreg Kroah-Hartman 		break;
280296fd7ce5SGreg Kroah-Hartman 	}
280396fd7ce5SGreg Kroah-Hartman 	set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
280496fd7ce5SGreg Kroah-Hartman 	clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
280520b9d177SAlan Cox 	return tty->ops->tiocmset(tty, set, clear);
280696fd7ce5SGreg Kroah-Hartman }
280796fd7ce5SGreg Kroah-Hartman 
280896fd7ce5SGreg Kroah-Hartman static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
280996fd7ce5SGreg Kroah-Hartman {
281096fd7ce5SGreg Kroah-Hartman 	int retval = -EINVAL;
281196fd7ce5SGreg Kroah-Hartman 	struct serial_icounter_struct icount;
281296fd7ce5SGreg Kroah-Hartman 	memset(&icount, 0, sizeof(icount));
281396fd7ce5SGreg Kroah-Hartman 	if (tty->ops->get_icount)
281496fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->get_icount(tty, &icount);
281596fd7ce5SGreg Kroah-Hartman 	if (retval != 0)
281696fd7ce5SGreg Kroah-Hartman 		return retval;
281796fd7ce5SGreg Kroah-Hartman 	if (copy_to_user(arg, &icount, sizeof(icount)))
281896fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
281996fd7ce5SGreg Kroah-Hartman 	return 0;
282096fd7ce5SGreg Kroah-Hartman }
282196fd7ce5SGreg Kroah-Hartman 
28228a8ae62fSJiri Slaby static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
28238a8ae62fSJiri Slaby {
28248a8ae62fSJiri Slaby 	static DEFINE_RATELIMIT_STATE(depr_flags,
28258a8ae62fSJiri Slaby 			DEFAULT_RATELIMIT_INTERVAL,
28268a8ae62fSJiri Slaby 			DEFAULT_RATELIMIT_BURST);
28278a8ae62fSJiri Slaby 	char comm[TASK_COMM_LEN];
28288a8ae62fSJiri Slaby 	int flags;
28298a8ae62fSJiri Slaby 
28308a8ae62fSJiri Slaby 	if (get_user(flags, &ss->flags))
28318a8ae62fSJiri Slaby 		return;
28328a8ae62fSJiri Slaby 
28338a8ae62fSJiri Slaby 	flags &= ASYNC_DEPRECATED;
28348a8ae62fSJiri Slaby 
28358a8ae62fSJiri Slaby 	if (flags && __ratelimit(&depr_flags))
28368a8ae62fSJiri Slaby 		pr_warning("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
28378a8ae62fSJiri Slaby 				__func__, get_task_comm(comm, current), flags);
28388a8ae62fSJiri Slaby }
28398a8ae62fSJiri Slaby 
28408f166e00SPeter Hurley /*
28418f166e00SPeter Hurley  * if pty, return the slave side (real_tty)
28428f166e00SPeter Hurley  * otherwise, return self
28438f166e00SPeter Hurley  */
28448f166e00SPeter Hurley static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
284596fd7ce5SGreg Kroah-Hartman {
284696fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
284796fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_MASTER)
284896fd7ce5SGreg Kroah-Hartman 		tty = tty->link;
284996fd7ce5SGreg Kroah-Hartman 	return tty;
285096fd7ce5SGreg Kroah-Hartman }
285196fd7ce5SGreg Kroah-Hartman 
285296fd7ce5SGreg Kroah-Hartman /*
285396fd7ce5SGreg Kroah-Hartman  * Split this up, as gcc can choke on it otherwise..
285496fd7ce5SGreg Kroah-Hartman  */
285596fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
285696fd7ce5SGreg Kroah-Hartman {
285796fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
285896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *real_tty;
285996fd7ce5SGreg Kroah-Hartman 	void __user *p = (void __user *)arg;
286096fd7ce5SGreg Kroah-Hartman 	int retval;
286196fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
286296fd7ce5SGreg Kroah-Hartman 
28636131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
286496fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
286596fd7ce5SGreg Kroah-Hartman 
286696fd7ce5SGreg Kroah-Hartman 	real_tty = tty_pair_get_tty(tty);
286796fd7ce5SGreg Kroah-Hartman 
286896fd7ce5SGreg Kroah-Hartman 	/*
286996fd7ce5SGreg Kroah-Hartman 	 * Factor out some common prep work
287096fd7ce5SGreg Kroah-Hartman 	 */
287196fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
287296fd7ce5SGreg Kroah-Hartman 	case TIOCSETD:
287396fd7ce5SGreg Kroah-Hartman 	case TIOCSBRK:
287496fd7ce5SGreg Kroah-Hartman 	case TIOCCBRK:
287596fd7ce5SGreg Kroah-Hartman 	case TCSBRK:
287696fd7ce5SGreg Kroah-Hartman 	case TCSBRKP:
287796fd7ce5SGreg Kroah-Hartman 		retval = tty_check_change(tty);
287896fd7ce5SGreg Kroah-Hartman 		if (retval)
287996fd7ce5SGreg Kroah-Hartman 			return retval;
288096fd7ce5SGreg Kroah-Hartman 		if (cmd != TIOCCBRK) {
288196fd7ce5SGreg Kroah-Hartman 			tty_wait_until_sent(tty, 0);
288296fd7ce5SGreg Kroah-Hartman 			if (signal_pending(current))
288396fd7ce5SGreg Kroah-Hartman 				return -EINTR;
288496fd7ce5SGreg Kroah-Hartman 		}
288596fd7ce5SGreg Kroah-Hartman 		break;
288696fd7ce5SGreg Kroah-Hartman 	}
288796fd7ce5SGreg Kroah-Hartman 
288896fd7ce5SGreg Kroah-Hartman 	/*
288996fd7ce5SGreg Kroah-Hartman 	 *	Now do the stuff.
289096fd7ce5SGreg Kroah-Hartman 	 */
289196fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
289296fd7ce5SGreg Kroah-Hartman 	case TIOCSTI:
289396fd7ce5SGreg Kroah-Hartman 		return tiocsti(tty, p);
289496fd7ce5SGreg Kroah-Hartman 	case TIOCGWINSZ:
289596fd7ce5SGreg Kroah-Hartman 		return tiocgwinsz(real_tty, p);
289696fd7ce5SGreg Kroah-Hartman 	case TIOCSWINSZ:
289796fd7ce5SGreg Kroah-Hartman 		return tiocswinsz(real_tty, p);
289896fd7ce5SGreg Kroah-Hartman 	case TIOCCONS:
289996fd7ce5SGreg Kroah-Hartman 		return real_tty != tty ? -EINVAL : tioccons(file);
290096fd7ce5SGreg Kroah-Hartman 	case FIONBIO:
290196fd7ce5SGreg Kroah-Hartman 		return fionbio(file, p);
290296fd7ce5SGreg Kroah-Hartman 	case TIOCEXCL:
290396fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_EXCLUSIVE, &tty->flags);
290496fd7ce5SGreg Kroah-Hartman 		return 0;
290596fd7ce5SGreg Kroah-Hartman 	case TIOCNXCL:
290696fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_EXCLUSIVE, &tty->flags);
290796fd7ce5SGreg Kroah-Hartman 		return 0;
290884fd7bdfSCyrill Gorcunov 	case TIOCGEXCL:
290984fd7bdfSCyrill Gorcunov 	{
291084fd7bdfSCyrill Gorcunov 		int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
291184fd7bdfSCyrill Gorcunov 		return put_user(excl, (int __user *)p);
291284fd7bdfSCyrill Gorcunov 	}
291396fd7ce5SGreg Kroah-Hartman 	case TIOCNOTTY:
291496fd7ce5SGreg Kroah-Hartman 		if (current->signal->tty != tty)
291596fd7ce5SGreg Kroah-Hartman 			return -ENOTTY;
291696fd7ce5SGreg Kroah-Hartman 		no_tty();
291796fd7ce5SGreg Kroah-Hartman 		return 0;
291896fd7ce5SGreg Kroah-Hartman 	case TIOCSCTTY:
2919d1d3a0f7SPeter Hurley 		return tiocsctty(real_tty, file, arg);
292096fd7ce5SGreg Kroah-Hartman 	case TIOCGPGRP:
292196fd7ce5SGreg Kroah-Hartman 		return tiocgpgrp(tty, real_tty, p);
292296fd7ce5SGreg Kroah-Hartman 	case TIOCSPGRP:
292396fd7ce5SGreg Kroah-Hartman 		return tiocspgrp(tty, real_tty, p);
292496fd7ce5SGreg Kroah-Hartman 	case TIOCGSID:
292596fd7ce5SGreg Kroah-Hartman 		return tiocgsid(tty, real_tty, p);
292696fd7ce5SGreg Kroah-Hartman 	case TIOCGETD:
29275c17c861SPeter Hurley 		return tiocgetd(tty, p);
292896fd7ce5SGreg Kroah-Hartman 	case TIOCSETD:
292996fd7ce5SGreg Kroah-Hartman 		return tiocsetd(tty, p);
29303c95c985SKay Sievers 	case TIOCVHANGUP:
29313c95c985SKay Sievers 		if (!capable(CAP_SYS_ADMIN))
29323c95c985SKay Sievers 			return -EPERM;
29333c95c985SKay Sievers 		tty_vhangup(tty);
29343c95c985SKay Sievers 		return 0;
2935b7b8de08SWerner Fink 	case TIOCGDEV:
2936b7b8de08SWerner Fink 	{
2937b7b8de08SWerner Fink 		unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2938b7b8de08SWerner Fink 		return put_user(ret, (unsigned int __user *)p);
2939b7b8de08SWerner Fink 	}
294096fd7ce5SGreg Kroah-Hartman 	/*
294196fd7ce5SGreg Kroah-Hartman 	 * Break handling
294296fd7ce5SGreg Kroah-Hartman 	 */
294396fd7ce5SGreg Kroah-Hartman 	case TIOCSBRK:	/* Turn break on, unconditionally */
294496fd7ce5SGreg Kroah-Hartman 		if (tty->ops->break_ctl)
294596fd7ce5SGreg Kroah-Hartman 			return tty->ops->break_ctl(tty, -1);
294696fd7ce5SGreg Kroah-Hartman 		return 0;
294796fd7ce5SGreg Kroah-Hartman 	case TIOCCBRK:	/* Turn break off, unconditionally */
294896fd7ce5SGreg Kroah-Hartman 		if (tty->ops->break_ctl)
294996fd7ce5SGreg Kroah-Hartman 			return tty->ops->break_ctl(tty, 0);
295096fd7ce5SGreg Kroah-Hartman 		return 0;
295196fd7ce5SGreg Kroah-Hartman 	case TCSBRK:   /* SVID version: non-zero arg --> no break */
295296fd7ce5SGreg Kroah-Hartman 		/* non-zero arg means wait for all output data
295396fd7ce5SGreg Kroah-Hartman 		 * to be sent (performed above) but don't send break.
295496fd7ce5SGreg Kroah-Hartman 		 * This is used by the tcdrain() termios function.
295596fd7ce5SGreg Kroah-Hartman 		 */
295696fd7ce5SGreg Kroah-Hartman 		if (!arg)
295796fd7ce5SGreg Kroah-Hartman 			return send_break(tty, 250);
295896fd7ce5SGreg Kroah-Hartman 		return 0;
295996fd7ce5SGreg Kroah-Hartman 	case TCSBRKP:	/* support for POSIX tcsendbreak() */
296096fd7ce5SGreg Kroah-Hartman 		return send_break(tty, arg ? arg*100 : 250);
296196fd7ce5SGreg Kroah-Hartman 
296296fd7ce5SGreg Kroah-Hartman 	case TIOCMGET:
296360b33c13SAlan Cox 		return tty_tiocmget(tty, p);
296496fd7ce5SGreg Kroah-Hartman 	case TIOCMSET:
296596fd7ce5SGreg Kroah-Hartman 	case TIOCMBIC:
296696fd7ce5SGreg Kroah-Hartman 	case TIOCMBIS:
296720b9d177SAlan Cox 		return tty_tiocmset(tty, cmd, p);
296896fd7ce5SGreg Kroah-Hartman 	case TIOCGICOUNT:
296996fd7ce5SGreg Kroah-Hartman 		retval = tty_tiocgicount(tty, p);
297096fd7ce5SGreg Kroah-Hartman 		/* For the moment allow fall through to the old method */
297196fd7ce5SGreg Kroah-Hartman         	if (retval != -EINVAL)
297296fd7ce5SGreg Kroah-Hartman 			return retval;
297396fd7ce5SGreg Kroah-Hartman 		break;
297496fd7ce5SGreg Kroah-Hartman 	case TCFLSH:
297596fd7ce5SGreg Kroah-Hartman 		switch (arg) {
297696fd7ce5SGreg Kroah-Hartman 		case TCIFLUSH:
297796fd7ce5SGreg Kroah-Hartman 		case TCIOFLUSH:
297896fd7ce5SGreg Kroah-Hartman 		/* flush tty buffer and allow ldisc to process ioctl */
297986c80a8eSPeter Hurley 			tty_buffer_flush(tty, NULL);
298096fd7ce5SGreg Kroah-Hartman 			break;
298196fd7ce5SGreg Kroah-Hartman 		}
298296fd7ce5SGreg Kroah-Hartman 		break;
29838a8ae62fSJiri Slaby 	case TIOCSSERIAL:
29848a8ae62fSJiri Slaby 		tty_warn_deprecated_flags(p);
29858a8ae62fSJiri Slaby 		break;
298696fd7ce5SGreg Kroah-Hartman 	}
298796fd7ce5SGreg Kroah-Hartman 	if (tty->ops->ioctl) {
2988c961bfb1SPeter Hurley 		retval = tty->ops->ioctl(tty, cmd, arg);
298996fd7ce5SGreg Kroah-Hartman 		if (retval != -ENOIOCTLCMD)
299096fd7ce5SGreg Kroah-Hartman 			return retval;
299196fd7ce5SGreg Kroah-Hartman 	}
299296fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
2993e55afd11SPeter Hurley 	if (!ld)
2994e55afd11SPeter Hurley 		return hung_up_tty_ioctl(file, cmd, arg);
299596fd7ce5SGreg Kroah-Hartman 	retval = -EINVAL;
299696fd7ce5SGreg Kroah-Hartman 	if (ld->ops->ioctl) {
299796fd7ce5SGreg Kroah-Hartman 		retval = ld->ops->ioctl(tty, file, cmd, arg);
299896fd7ce5SGreg Kroah-Hartman 		if (retval == -ENOIOCTLCMD)
2999bbb63c51SWanlong Gao 			retval = -ENOTTY;
300096fd7ce5SGreg Kroah-Hartman 	}
300196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
300296fd7ce5SGreg Kroah-Hartman 	return retval;
300396fd7ce5SGreg Kroah-Hartman }
300496fd7ce5SGreg Kroah-Hartman 
300596fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT
300696fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd,
300796fd7ce5SGreg Kroah-Hartman 				unsigned long arg)
300896fd7ce5SGreg Kroah-Hartman {
300996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
301096fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
301196fd7ce5SGreg Kroah-Hartman 	int retval = -ENOIOCTLCMD;
301296fd7ce5SGreg Kroah-Hartman 
30136131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
301496fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
301596fd7ce5SGreg Kroah-Hartman 
301696fd7ce5SGreg Kroah-Hartman 	if (tty->ops->compat_ioctl) {
3017c961bfb1SPeter Hurley 		retval = tty->ops->compat_ioctl(tty, cmd, arg);
301896fd7ce5SGreg Kroah-Hartman 		if (retval != -ENOIOCTLCMD)
301996fd7ce5SGreg Kroah-Hartman 			return retval;
302096fd7ce5SGreg Kroah-Hartman 	}
302196fd7ce5SGreg Kroah-Hartman 
302296fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
3023e55afd11SPeter Hurley 	if (!ld)
3024e55afd11SPeter Hurley 		return hung_up_tty_compat_ioctl(file, cmd, arg);
302596fd7ce5SGreg Kroah-Hartman 	if (ld->ops->compat_ioctl)
302696fd7ce5SGreg Kroah-Hartman 		retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
30278193c429SThomas Meyer 	else
30288193c429SThomas Meyer 		retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
302996fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
303096fd7ce5SGreg Kroah-Hartman 
303196fd7ce5SGreg Kroah-Hartman 	return retval;
303296fd7ce5SGreg Kroah-Hartman }
303396fd7ce5SGreg Kroah-Hartman #endif
303496fd7ce5SGreg Kroah-Hartman 
3035c3c073f8SAl Viro static int this_tty(const void *t, struct file *file, unsigned fd)
3036c3c073f8SAl Viro {
3037c3c073f8SAl Viro 	if (likely(file->f_op->read != tty_read))
3038c3c073f8SAl Viro 		return 0;
3039c3c073f8SAl Viro 	return file_tty(file) != t ? 0 : fd + 1;
3040c3c073f8SAl Viro }
3041c3c073f8SAl Viro 
304296fd7ce5SGreg Kroah-Hartman /*
304396fd7ce5SGreg Kroah-Hartman  * This implements the "Secure Attention Key" ---  the idea is to
304496fd7ce5SGreg Kroah-Hartman  * prevent trojan horses by killing all processes associated with this
304596fd7ce5SGreg Kroah-Hartman  * tty when the user hits the "Secure Attention Key".  Required for
304696fd7ce5SGreg Kroah-Hartman  * super-paranoid applications --- see the Orange Book for more details.
304796fd7ce5SGreg Kroah-Hartman  *
304896fd7ce5SGreg Kroah-Hartman  * This code could be nicer; ideally it should send a HUP, wait a few
304996fd7ce5SGreg Kroah-Hartman  * seconds, then send a INT, and then a KILL signal.  But you then
305096fd7ce5SGreg Kroah-Hartman  * have to coordinate with the init process, since all processes associated
305196fd7ce5SGreg Kroah-Hartman  * with the current tty must be dead before the new getty is allowed
305296fd7ce5SGreg Kroah-Hartman  * to spawn.
305396fd7ce5SGreg Kroah-Hartman  *
305496fd7ce5SGreg Kroah-Hartman  * Now, if it would be correct ;-/ The current code has a nasty hole -
305596fd7ce5SGreg Kroah-Hartman  * it doesn't catch files in flight. We may send the descriptor to ourselves
305696fd7ce5SGreg Kroah-Hartman  * via AF_UNIX socket, close it and later fetch from socket. FIXME.
305796fd7ce5SGreg Kroah-Hartman  *
305896fd7ce5SGreg Kroah-Hartman  * Nasty bug: do_SAK is being called in interrupt context.  This can
305996fd7ce5SGreg Kroah-Hartman  * deadlock.  We punt it up to process context.  AKPM - 16Mar2001
306096fd7ce5SGreg Kroah-Hartman  */
306196fd7ce5SGreg Kroah-Hartman void __do_SAK(struct tty_struct *tty)
306296fd7ce5SGreg Kroah-Hartman {
306396fd7ce5SGreg Kroah-Hartman #ifdef TTY_SOFT_SAK
306496fd7ce5SGreg Kroah-Hartman 	tty_hangup(tty);
306596fd7ce5SGreg Kroah-Hartman #else
306696fd7ce5SGreg Kroah-Hartman 	struct task_struct *g, *p;
306796fd7ce5SGreg Kroah-Hartman 	struct pid *session;
306896fd7ce5SGreg Kroah-Hartman 	int		i;
306996fd7ce5SGreg Kroah-Hartman 
307096fd7ce5SGreg Kroah-Hartman 	if (!tty)
307196fd7ce5SGreg Kroah-Hartman 		return;
307296fd7ce5SGreg Kroah-Hartman 	session = tty->session;
307396fd7ce5SGreg Kroah-Hartman 
307496fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
307596fd7ce5SGreg Kroah-Hartman 
307696fd7ce5SGreg Kroah-Hartman 	tty_driver_flush_buffer(tty);
307796fd7ce5SGreg Kroah-Hartman 
307896fd7ce5SGreg Kroah-Hartman 	read_lock(&tasklist_lock);
307996fd7ce5SGreg Kroah-Hartman 	/* Kill the entire session */
308096fd7ce5SGreg Kroah-Hartman 	do_each_pid_task(session, PIDTYPE_SID, p) {
30819b42bb75SPeter Hurley 		tty_notice(tty, "SAK: killed process %d (%s): by session\n",
308296fd7ce5SGreg Kroah-Hartman 			   task_pid_nr(p), p->comm);
308396fd7ce5SGreg Kroah-Hartman 		send_sig(SIGKILL, p, 1);
308496fd7ce5SGreg Kroah-Hartman 	} while_each_pid_task(session, PIDTYPE_SID, p);
30859b42bb75SPeter Hurley 
30869b42bb75SPeter Hurley 	/* Now kill any processes that happen to have the tty open */
308796fd7ce5SGreg Kroah-Hartman 	do_each_thread(g, p) {
308896fd7ce5SGreg Kroah-Hartman 		if (p->signal->tty == tty) {
30899b42bb75SPeter Hurley 			tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
309096fd7ce5SGreg Kroah-Hartman 				   task_pid_nr(p), p->comm);
309196fd7ce5SGreg Kroah-Hartman 			send_sig(SIGKILL, p, 1);
309296fd7ce5SGreg Kroah-Hartman 			continue;
309396fd7ce5SGreg Kroah-Hartman 		}
309496fd7ce5SGreg Kroah-Hartman 		task_lock(p);
3095c3c073f8SAl Viro 		i = iterate_fd(p->files, 0, this_tty, tty);
3096c3c073f8SAl Viro 		if (i != 0) {
30979b42bb75SPeter Hurley 			tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
3098c3c073f8SAl Viro 				   task_pid_nr(p), p->comm, i - 1);
309996fd7ce5SGreg Kroah-Hartman 			force_sig(SIGKILL, p);
310096fd7ce5SGreg Kroah-Hartman 		}
310196fd7ce5SGreg Kroah-Hartman 		task_unlock(p);
310296fd7ce5SGreg Kroah-Hartman 	} while_each_thread(g, p);
310396fd7ce5SGreg Kroah-Hartman 	read_unlock(&tasklist_lock);
310496fd7ce5SGreg Kroah-Hartman #endif
310596fd7ce5SGreg Kroah-Hartman }
310696fd7ce5SGreg Kroah-Hartman 
310796fd7ce5SGreg Kroah-Hartman static void do_SAK_work(struct work_struct *work)
310896fd7ce5SGreg Kroah-Hartman {
310996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
311096fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, SAK_work);
311196fd7ce5SGreg Kroah-Hartman 	__do_SAK(tty);
311296fd7ce5SGreg Kroah-Hartman }
311396fd7ce5SGreg Kroah-Hartman 
311496fd7ce5SGreg Kroah-Hartman /*
311596fd7ce5SGreg Kroah-Hartman  * The tq handling here is a little racy - tty->SAK_work may already be queued.
311696fd7ce5SGreg Kroah-Hartman  * Fortunately we don't need to worry, because if ->SAK_work is already queued,
311796fd7ce5SGreg Kroah-Hartman  * the values which we write to it will be identical to the values which it
311896fd7ce5SGreg Kroah-Hartman  * already has. --akpm
311996fd7ce5SGreg Kroah-Hartman  */
312096fd7ce5SGreg Kroah-Hartman void do_SAK(struct tty_struct *tty)
312196fd7ce5SGreg Kroah-Hartman {
312296fd7ce5SGreg Kroah-Hartman 	if (!tty)
312396fd7ce5SGreg Kroah-Hartman 		return;
312496fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->SAK_work);
312596fd7ce5SGreg Kroah-Hartman }
312696fd7ce5SGreg Kroah-Hartman 
312796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(do_SAK);
312896fd7ce5SGreg Kroah-Hartman 
31296e9430acSGreg Kroah-Hartman static int dev_match_devt(struct device *dev, const void *data)
313096fd7ce5SGreg Kroah-Hartman {
31316e9430acSGreg Kroah-Hartman 	const dev_t *devt = data;
313296fd7ce5SGreg Kroah-Hartman 	return dev->devt == *devt;
313396fd7ce5SGreg Kroah-Hartman }
313496fd7ce5SGreg Kroah-Hartman 
313596fd7ce5SGreg Kroah-Hartman /* Must put_device() after it's unused! */
313696fd7ce5SGreg Kroah-Hartman static struct device *tty_get_device(struct tty_struct *tty)
313796fd7ce5SGreg Kroah-Hartman {
313896fd7ce5SGreg Kroah-Hartman 	dev_t devt = tty_devnum(tty);
313996fd7ce5SGreg Kroah-Hartman 	return class_find_device(tty_class, NULL, &devt, dev_match_devt);
314096fd7ce5SGreg Kroah-Hartman }
314196fd7ce5SGreg Kroah-Hartman 
314296fd7ce5SGreg Kroah-Hartman 
314396fd7ce5SGreg Kroah-Hartman /**
31442c964a2fSRasmus Villemoes  *	alloc_tty_struct
314596fd7ce5SGreg Kroah-Hartman  *
31462c964a2fSRasmus Villemoes  *	This subroutine allocates and initializes a tty structure.
314796fd7ce5SGreg Kroah-Hartman  *
31482c964a2fSRasmus Villemoes  *	Locking: none - tty in question is not exposed at this point
314996fd7ce5SGreg Kroah-Hartman  */
315096fd7ce5SGreg Kroah-Hartman 
31512c964a2fSRasmus Villemoes struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
315296fd7ce5SGreg Kroah-Hartman {
31532c964a2fSRasmus Villemoes 	struct tty_struct *tty;
31542c964a2fSRasmus Villemoes 
31552c964a2fSRasmus Villemoes 	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
31562c964a2fSRasmus Villemoes 	if (!tty)
31572c964a2fSRasmus Villemoes 		return NULL;
31582c964a2fSRasmus Villemoes 
315996fd7ce5SGreg Kroah-Hartman 	kref_init(&tty->kref);
316096fd7ce5SGreg Kroah-Hartman 	tty->magic = TTY_MAGIC;
316196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_init(tty);
316296fd7ce5SGreg Kroah-Hartman 	tty->session = NULL;
316396fd7ce5SGreg Kroah-Hartman 	tty->pgrp = NULL;
316489c8d91eSAlan Cox 	mutex_init(&tty->legacy_mutex);
3165d8c1f929SPeter Hurley 	mutex_init(&tty->throttle_mutex);
31666a1c0680SPeter Hurley 	init_rwsem(&tty->termios_rwsem);
3167dee4a0beSPeter Hurley 	mutex_init(&tty->winsize_mutex);
316836697529SPeter Hurley 	init_ldsem(&tty->ldisc_sem);
316996fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&tty->write_wait);
317096fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&tty->read_wait);
317196fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->hangup_work, do_tty_hangup);
317296fd7ce5SGreg Kroah-Hartman 	mutex_init(&tty->atomic_write_lock);
317396fd7ce5SGreg Kroah-Hartman 	spin_lock_init(&tty->ctrl_lock);
3174f9e053dcSPeter Hurley 	spin_lock_init(&tty->flow_lock);
31754a510969SPeter Hurley 	spin_lock_init(&tty->files_lock);
317696fd7ce5SGreg Kroah-Hartman 	INIT_LIST_HEAD(&tty->tty_files);
317796fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->SAK_work, do_SAK_work);
317896fd7ce5SGreg Kroah-Hartman 
317996fd7ce5SGreg Kroah-Hartman 	tty->driver = driver;
318096fd7ce5SGreg Kroah-Hartman 	tty->ops = driver->ops;
318196fd7ce5SGreg Kroah-Hartman 	tty->index = idx;
318296fd7ce5SGreg Kroah-Hartman 	tty_line_name(driver, idx, tty->name);
318396fd7ce5SGreg Kroah-Hartman 	tty->dev = tty_get_device(tty);
31842c964a2fSRasmus Villemoes 
31852c964a2fSRasmus Villemoes 	return tty;
318696fd7ce5SGreg Kroah-Hartman }
318796fd7ce5SGreg Kroah-Hartman 
318896fd7ce5SGreg Kroah-Hartman /**
318996fd7ce5SGreg Kroah-Hartman  *	tty_put_char	-	write one character to a tty
319096fd7ce5SGreg Kroah-Hartman  *	@tty: tty
319196fd7ce5SGreg Kroah-Hartman  *	@ch: character
319296fd7ce5SGreg Kroah-Hartman  *
319396fd7ce5SGreg Kroah-Hartman  *	Write one byte to the tty using the provided put_char method
319496fd7ce5SGreg Kroah-Hartman  *	if present. Returns the number of characters successfully output.
319596fd7ce5SGreg Kroah-Hartman  *
319696fd7ce5SGreg Kroah-Hartman  *	Note: the specific put_char operation in the driver layer may go
319796fd7ce5SGreg Kroah-Hartman  *	away soon. Don't call it directly, use this method
319896fd7ce5SGreg Kroah-Hartman  */
319996fd7ce5SGreg Kroah-Hartman 
320096fd7ce5SGreg Kroah-Hartman int tty_put_char(struct tty_struct *tty, unsigned char ch)
320196fd7ce5SGreg Kroah-Hartman {
320296fd7ce5SGreg Kroah-Hartman 	if (tty->ops->put_char)
320396fd7ce5SGreg Kroah-Hartman 		return tty->ops->put_char(tty, ch);
320496fd7ce5SGreg Kroah-Hartman 	return tty->ops->write(tty, &ch, 1);
320596fd7ce5SGreg Kroah-Hartman }
320696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_put_char);
320796fd7ce5SGreg Kroah-Hartman 
320896fd7ce5SGreg Kroah-Hartman struct class *tty_class;
320996fd7ce5SGreg Kroah-Hartman 
32107e73eca6SJiri Slaby static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
32117e73eca6SJiri Slaby 		unsigned int index, unsigned int count)
32127e73eca6SJiri Slaby {
3213c1a752baSLeon Yu 	int err;
3214c1a752baSLeon Yu 
32157e73eca6SJiri Slaby 	/* init here, since reused cdevs cause crashes */
3216a3a10ce3SRichard Watts 	driver->cdevs[index] = cdev_alloc();
3217a3a10ce3SRichard Watts 	if (!driver->cdevs[index])
3218a3a10ce3SRichard Watts 		return -ENOMEM;
3219c1a752baSLeon Yu 	driver->cdevs[index]->ops = &tty_fops;
3220a3a10ce3SRichard Watts 	driver->cdevs[index]->owner = driver->owner;
3221c1a752baSLeon Yu 	err = cdev_add(driver->cdevs[index], dev, count);
3222c1a752baSLeon Yu 	if (err)
3223c1a752baSLeon Yu 		kobject_put(&driver->cdevs[index]->kobj);
3224c1a752baSLeon Yu 	return err;
32257e73eca6SJiri Slaby }
32267e73eca6SJiri Slaby 
322796fd7ce5SGreg Kroah-Hartman /**
322896fd7ce5SGreg Kroah-Hartman  *	tty_register_device - register a tty device
322996fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver that describes the tty device
323096fd7ce5SGreg Kroah-Hartman  *	@index: the index in the tty driver for this tty device
323196fd7ce5SGreg Kroah-Hartman  *	@device: a struct device that is associated with this tty device.
323296fd7ce5SGreg Kroah-Hartman  *		This field is optional, if there is no known struct device
323396fd7ce5SGreg Kroah-Hartman  *		for this tty device it can be set to NULL safely.
323496fd7ce5SGreg Kroah-Hartman  *
323596fd7ce5SGreg Kroah-Hartman  *	Returns a pointer to the struct device for this tty device
323696fd7ce5SGreg Kroah-Hartman  *	(or ERR_PTR(-EFOO) on error).
323796fd7ce5SGreg Kroah-Hartman  *
323896fd7ce5SGreg Kroah-Hartman  *	This call is required to be made to register an individual tty device
323996fd7ce5SGreg Kroah-Hartman  *	if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set.  If
324096fd7ce5SGreg Kroah-Hartman  *	that bit is not set, this function should not be called by a tty
324196fd7ce5SGreg Kroah-Hartman  *	driver.
324296fd7ce5SGreg Kroah-Hartman  *
324396fd7ce5SGreg Kroah-Hartman  *	Locking: ??
324496fd7ce5SGreg Kroah-Hartman  */
324596fd7ce5SGreg Kroah-Hartman 
324696fd7ce5SGreg Kroah-Hartman struct device *tty_register_device(struct tty_driver *driver, unsigned index,
324796fd7ce5SGreg Kroah-Hartman 				   struct device *device)
324896fd7ce5SGreg Kroah-Hartman {
32496915c0e4STomas Hlavacek 	return tty_register_device_attr(driver, index, device, NULL, NULL);
32506915c0e4STomas Hlavacek }
32516915c0e4STomas Hlavacek EXPORT_SYMBOL(tty_register_device);
32526915c0e4STomas Hlavacek 
3253b1b79916STomas Hlavacek static void tty_device_create_release(struct device *dev)
3254b1b79916STomas Hlavacek {
325583db1df4SPeter Hurley 	dev_dbg(dev, "releasing...\n");
3256b1b79916STomas Hlavacek 	kfree(dev);
3257b1b79916STomas Hlavacek }
3258b1b79916STomas Hlavacek 
32596915c0e4STomas Hlavacek /**
32606915c0e4STomas Hlavacek  *	tty_register_device_attr - register a tty device
32616915c0e4STomas Hlavacek  *	@driver: the tty driver that describes the tty device
32626915c0e4STomas Hlavacek  *	@index: the index in the tty driver for this tty device
32636915c0e4STomas Hlavacek  *	@device: a struct device that is associated with this tty device.
32646915c0e4STomas Hlavacek  *		This field is optional, if there is no known struct device
32656915c0e4STomas Hlavacek  *		for this tty device it can be set to NULL safely.
32666915c0e4STomas Hlavacek  *	@drvdata: Driver data to be set to device.
32676915c0e4STomas Hlavacek  *	@attr_grp: Attribute group to be set on device.
32686915c0e4STomas Hlavacek  *
32696915c0e4STomas Hlavacek  *	Returns a pointer to the struct device for this tty device
32706915c0e4STomas Hlavacek  *	(or ERR_PTR(-EFOO) on error).
32716915c0e4STomas Hlavacek  *
32726915c0e4STomas Hlavacek  *	This call is required to be made to register an individual tty device
32736915c0e4STomas Hlavacek  *	if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set.  If
32746915c0e4STomas Hlavacek  *	that bit is not set, this function should not be called by a tty
32756915c0e4STomas Hlavacek  *	driver.
32766915c0e4STomas Hlavacek  *
32776915c0e4STomas Hlavacek  *	Locking: ??
32786915c0e4STomas Hlavacek  */
32796915c0e4STomas Hlavacek struct device *tty_register_device_attr(struct tty_driver *driver,
32806915c0e4STomas Hlavacek 				   unsigned index, struct device *device,
32816915c0e4STomas Hlavacek 				   void *drvdata,
32826915c0e4STomas Hlavacek 				   const struct attribute_group **attr_grp)
32836915c0e4STomas Hlavacek {
328496fd7ce5SGreg Kroah-Hartman 	char name[64];
32856915c0e4STomas Hlavacek 	dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
32866915c0e4STomas Hlavacek 	struct device *dev = NULL;
32876915c0e4STomas Hlavacek 	int retval = -ENODEV;
32887e73eca6SJiri Slaby 	bool cdev = false;
328996fd7ce5SGreg Kroah-Hartman 
329096fd7ce5SGreg Kroah-Hartman 	if (index >= driver->num) {
3291656fb867SPeter Hurley 		pr_err("%s: Attempt to register invalid tty line number (%d)\n",
3292656fb867SPeter Hurley 		       driver->name, index);
329396fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-EINVAL);
329496fd7ce5SGreg Kroah-Hartman 	}
329596fd7ce5SGreg Kroah-Hartman 
329696fd7ce5SGreg Kroah-Hartman 	if (driver->type == TTY_DRIVER_TYPE_PTY)
329796fd7ce5SGreg Kroah-Hartman 		pty_line_name(driver, index, name);
329896fd7ce5SGreg Kroah-Hartman 	else
329996fd7ce5SGreg Kroah-Hartman 		tty_line_name(driver, index, name);
330096fd7ce5SGreg Kroah-Hartman 
33017e73eca6SJiri Slaby 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
33026915c0e4STomas Hlavacek 		retval = tty_cdev_add(driver, devt, index, 1);
33036915c0e4STomas Hlavacek 		if (retval)
33046915c0e4STomas Hlavacek 			goto error;
33057e73eca6SJiri Slaby 		cdev = true;
33067e73eca6SJiri Slaby 	}
33077e73eca6SJiri Slaby 
33086915c0e4STomas Hlavacek 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
33096915c0e4STomas Hlavacek 	if (!dev) {
33106915c0e4STomas Hlavacek 		retval = -ENOMEM;
33116915c0e4STomas Hlavacek 		goto error;
331296fd7ce5SGreg Kroah-Hartman 	}
33136915c0e4STomas Hlavacek 
33146915c0e4STomas Hlavacek 	dev->devt = devt;
33156915c0e4STomas Hlavacek 	dev->class = tty_class;
33166915c0e4STomas Hlavacek 	dev->parent = device;
3317b1b79916STomas Hlavacek 	dev->release = tty_device_create_release;
33186915c0e4STomas Hlavacek 	dev_set_name(dev, "%s", name);
33196915c0e4STomas Hlavacek 	dev->groups = attr_grp;
33206915c0e4STomas Hlavacek 	dev_set_drvdata(dev, drvdata);
33216915c0e4STomas Hlavacek 
33226915c0e4STomas Hlavacek 	retval = device_register(dev);
33236915c0e4STomas Hlavacek 	if (retval)
33246915c0e4STomas Hlavacek 		goto error;
33256915c0e4STomas Hlavacek 
33266915c0e4STomas Hlavacek 	return dev;
33276915c0e4STomas Hlavacek 
33286915c0e4STomas Hlavacek error:
33296915c0e4STomas Hlavacek 	put_device(dev);
3330a3a10ce3SRichard Watts 	if (cdev) {
3331a3a10ce3SRichard Watts 		cdev_del(driver->cdevs[index]);
3332a3a10ce3SRichard Watts 		driver->cdevs[index] = NULL;
3333a3a10ce3SRichard Watts 	}
33346915c0e4STomas Hlavacek 	return ERR_PTR(retval);
33356915c0e4STomas Hlavacek }
33366915c0e4STomas Hlavacek EXPORT_SYMBOL_GPL(tty_register_device_attr);
333796fd7ce5SGreg Kroah-Hartman 
333896fd7ce5SGreg Kroah-Hartman /**
333996fd7ce5SGreg Kroah-Hartman  * 	tty_unregister_device - unregister a tty device
334096fd7ce5SGreg Kroah-Hartman  * 	@driver: the tty driver that describes the tty device
334196fd7ce5SGreg Kroah-Hartman  * 	@index: the index in the tty driver for this tty device
334296fd7ce5SGreg Kroah-Hartman  *
334396fd7ce5SGreg Kroah-Hartman  * 	If a tty device is registered with a call to tty_register_device() then
334496fd7ce5SGreg Kroah-Hartman  *	this function must be called when the tty device is gone.
334596fd7ce5SGreg Kroah-Hartman  *
334696fd7ce5SGreg Kroah-Hartman  *	Locking: ??
334796fd7ce5SGreg Kroah-Hartman  */
334896fd7ce5SGreg Kroah-Hartman 
334996fd7ce5SGreg Kroah-Hartman void tty_unregister_device(struct tty_driver *driver, unsigned index)
335096fd7ce5SGreg Kroah-Hartman {
335196fd7ce5SGreg Kroah-Hartman 	device_destroy(tty_class,
335296fd7ce5SGreg Kroah-Hartman 		MKDEV(driver->major, driver->minor_start) + index);
3353a3a10ce3SRichard Watts 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3354a3a10ce3SRichard Watts 		cdev_del(driver->cdevs[index]);
3355a3a10ce3SRichard Watts 		driver->cdevs[index] = NULL;
3356a3a10ce3SRichard Watts 	}
335796fd7ce5SGreg Kroah-Hartman }
335896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_device);
335996fd7ce5SGreg Kroah-Hartman 
33607f0bc6a6SJiri Slaby /**
33617f0bc6a6SJiri Slaby  * __tty_alloc_driver -- allocate tty driver
33627f0bc6a6SJiri Slaby  * @lines: count of lines this driver can handle at most
33637f0bc6a6SJiri Slaby  * @owner: module which is repsonsible for this driver
33647f0bc6a6SJiri Slaby  * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
33657f0bc6a6SJiri Slaby  *
33667f0bc6a6SJiri Slaby  * This should not be called directly, some of the provided macros should be
33677f0bc6a6SJiri Slaby  * used instead. Use IS_ERR and friends on @retval.
33687f0bc6a6SJiri Slaby  */
33697f0bc6a6SJiri Slaby struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
33707f0bc6a6SJiri Slaby 		unsigned long flags)
337196fd7ce5SGreg Kroah-Hartman {
337296fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver;
33737e73eca6SJiri Slaby 	unsigned int cdevs = 1;
337416a02081SJiri Slaby 	int err;
337596fd7ce5SGreg Kroah-Hartman 
33760019b408SJiri Slaby 	if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
33777f0bc6a6SJiri Slaby 		return ERR_PTR(-EINVAL);
33787f0bc6a6SJiri Slaby 
337996fd7ce5SGreg Kroah-Hartman 	driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
33807f0bc6a6SJiri Slaby 	if (!driver)
33817f0bc6a6SJiri Slaby 		return ERR_PTR(-ENOMEM);
33827f0bc6a6SJiri Slaby 
338396fd7ce5SGreg Kroah-Hartman 	kref_init(&driver->kref);
338496fd7ce5SGreg Kroah-Hartman 	driver->magic = TTY_DRIVER_MAGIC;
338596fd7ce5SGreg Kroah-Hartman 	driver->num = lines;
33861a54a76dSJiri Slaby 	driver->owner = owner;
33877f0bc6a6SJiri Slaby 	driver->flags = flags;
338816a02081SJiri Slaby 
338916a02081SJiri Slaby 	if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
339016a02081SJiri Slaby 		driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
339116a02081SJiri Slaby 				GFP_KERNEL);
339216a02081SJiri Slaby 		driver->termios = kcalloc(lines, sizeof(*driver->termios),
339316a02081SJiri Slaby 				GFP_KERNEL);
339416a02081SJiri Slaby 		if (!driver->ttys || !driver->termios) {
339516a02081SJiri Slaby 			err = -ENOMEM;
339616a02081SJiri Slaby 			goto err_free_all;
339716a02081SJiri Slaby 		}
339816a02081SJiri Slaby 	}
339916a02081SJiri Slaby 
340016a02081SJiri Slaby 	if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
340116a02081SJiri Slaby 		driver->ports = kcalloc(lines, sizeof(*driver->ports),
340216a02081SJiri Slaby 				GFP_KERNEL);
340316a02081SJiri Slaby 		if (!driver->ports) {
340416a02081SJiri Slaby 			err = -ENOMEM;
340516a02081SJiri Slaby 			goto err_free_all;
340616a02081SJiri Slaby 		}
34077e73eca6SJiri Slaby 		cdevs = lines;
34087e73eca6SJiri Slaby 	}
34097e73eca6SJiri Slaby 
34107e73eca6SJiri Slaby 	driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
34117e73eca6SJiri Slaby 	if (!driver->cdevs) {
34127e73eca6SJiri Slaby 		err = -ENOMEM;
34137e73eca6SJiri Slaby 		goto err_free_all;
341416a02081SJiri Slaby 	}
34157f0bc6a6SJiri Slaby 
341696fd7ce5SGreg Kroah-Hartman 	return driver;
341716a02081SJiri Slaby err_free_all:
341816a02081SJiri Slaby 	kfree(driver->ports);
341916a02081SJiri Slaby 	kfree(driver->ttys);
342016a02081SJiri Slaby 	kfree(driver->termios);
3421a3a10ce3SRichard Watts 	kfree(driver->cdevs);
342216a02081SJiri Slaby 	kfree(driver);
342316a02081SJiri Slaby 	return ERR_PTR(err);
342496fd7ce5SGreg Kroah-Hartman }
34257f0bc6a6SJiri Slaby EXPORT_SYMBOL(__tty_alloc_driver);
342696fd7ce5SGreg Kroah-Hartman 
342796fd7ce5SGreg Kroah-Hartman static void destruct_tty_driver(struct kref *kref)
342896fd7ce5SGreg Kroah-Hartman {
342996fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
343096fd7ce5SGreg Kroah-Hartman 	int i;
343196fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
343296fd7ce5SGreg Kroah-Hartman 
343396fd7ce5SGreg Kroah-Hartman 	if (driver->flags & TTY_DRIVER_INSTALLED) {
343496fd7ce5SGreg Kroah-Hartman 		/*
343596fd7ce5SGreg Kroah-Hartman 		 * Free the termios and termios_locked structures because
343696fd7ce5SGreg Kroah-Hartman 		 * we don't want to get memory leaks when modular tty
343796fd7ce5SGreg Kroah-Hartman 		 * drivers are removed from the kernel.
343896fd7ce5SGreg Kroah-Hartman 		 */
343996fd7ce5SGreg Kroah-Hartman 		for (i = 0; i < driver->num; i++) {
344096fd7ce5SGreg Kroah-Hartman 			tp = driver->termios[i];
344196fd7ce5SGreg Kroah-Hartman 			if (tp) {
344296fd7ce5SGreg Kroah-Hartman 				driver->termios[i] = NULL;
344396fd7ce5SGreg Kroah-Hartman 				kfree(tp);
344496fd7ce5SGreg Kroah-Hartman 			}
344596fd7ce5SGreg Kroah-Hartman 			if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
344696fd7ce5SGreg Kroah-Hartman 				tty_unregister_device(driver, i);
344796fd7ce5SGreg Kroah-Hartman 		}
344896fd7ce5SGreg Kroah-Hartman 		proc_tty_unregister_driver(driver);
34497e73eca6SJiri Slaby 		if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
3450a3a10ce3SRichard Watts 			cdev_del(driver->cdevs[0]);
345196fd7ce5SGreg Kroah-Hartman 	}
34527e73eca6SJiri Slaby 	kfree(driver->cdevs);
345304831dc1SJiri Slaby 	kfree(driver->ports);
345416a02081SJiri Slaby 	kfree(driver->termios);
345516a02081SJiri Slaby 	kfree(driver->ttys);
345696fd7ce5SGreg Kroah-Hartman 	kfree(driver);
345796fd7ce5SGreg Kroah-Hartman }
345896fd7ce5SGreg Kroah-Hartman 
345996fd7ce5SGreg Kroah-Hartman void tty_driver_kref_put(struct tty_driver *driver)
346096fd7ce5SGreg Kroah-Hartman {
346196fd7ce5SGreg Kroah-Hartman 	kref_put(&driver->kref, destruct_tty_driver);
346296fd7ce5SGreg Kroah-Hartman }
346396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_driver_kref_put);
346496fd7ce5SGreg Kroah-Hartman 
346596fd7ce5SGreg Kroah-Hartman void tty_set_operations(struct tty_driver *driver,
346696fd7ce5SGreg Kroah-Hartman 			const struct tty_operations *op)
346796fd7ce5SGreg Kroah-Hartman {
346896fd7ce5SGreg Kroah-Hartman 	driver->ops = op;
346996fd7ce5SGreg Kroah-Hartman };
347096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_set_operations);
347196fd7ce5SGreg Kroah-Hartman 
347296fd7ce5SGreg Kroah-Hartman void put_tty_driver(struct tty_driver *d)
347396fd7ce5SGreg Kroah-Hartman {
347496fd7ce5SGreg Kroah-Hartman 	tty_driver_kref_put(d);
347596fd7ce5SGreg Kroah-Hartman }
347696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(put_tty_driver);
347796fd7ce5SGreg Kroah-Hartman 
347896fd7ce5SGreg Kroah-Hartman /*
347996fd7ce5SGreg Kroah-Hartman  * Called by a tty driver to register itself.
348096fd7ce5SGreg Kroah-Hartman  */
348196fd7ce5SGreg Kroah-Hartman int tty_register_driver(struct tty_driver *driver)
348296fd7ce5SGreg Kroah-Hartman {
348396fd7ce5SGreg Kroah-Hartman 	int error;
348496fd7ce5SGreg Kroah-Hartman 	int i;
348596fd7ce5SGreg Kroah-Hartman 	dev_t dev;
348696fd7ce5SGreg Kroah-Hartman 	struct device *d;
348796fd7ce5SGreg Kroah-Hartman 
348896fd7ce5SGreg Kroah-Hartman 	if (!driver->major) {
348996fd7ce5SGreg Kroah-Hartman 		error = alloc_chrdev_region(&dev, driver->minor_start,
349096fd7ce5SGreg Kroah-Hartman 						driver->num, driver->name);
349196fd7ce5SGreg Kroah-Hartman 		if (!error) {
349296fd7ce5SGreg Kroah-Hartman 			driver->major = MAJOR(dev);
349396fd7ce5SGreg Kroah-Hartman 			driver->minor_start = MINOR(dev);
349496fd7ce5SGreg Kroah-Hartman 		}
349596fd7ce5SGreg Kroah-Hartman 	} else {
349696fd7ce5SGreg Kroah-Hartman 		dev = MKDEV(driver->major, driver->minor_start);
349796fd7ce5SGreg Kroah-Hartman 		error = register_chrdev_region(dev, driver->num, driver->name);
349896fd7ce5SGreg Kroah-Hartman 	}
34999bb8a3d4SJiri Slaby 	if (error < 0)
350016a02081SJiri Slaby 		goto err;
350196fd7ce5SGreg Kroah-Hartman 
35027e73eca6SJiri Slaby 	if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
35037e73eca6SJiri Slaby 		error = tty_cdev_add(driver, dev, 0, driver->num);
35049bb8a3d4SJiri Slaby 		if (error)
35059bb8a3d4SJiri Slaby 			goto err_unreg_char;
35067e73eca6SJiri Slaby 	}
350796fd7ce5SGreg Kroah-Hartman 
350896fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
350996fd7ce5SGreg Kroah-Hartman 	list_add(&driver->tty_drivers, &tty_drivers);
351096fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
351196fd7ce5SGreg Kroah-Hartman 
351296fd7ce5SGreg Kroah-Hartman 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
351396fd7ce5SGreg Kroah-Hartman 		for (i = 0; i < driver->num; i++) {
351496fd7ce5SGreg Kroah-Hartman 			d = tty_register_device(driver, i, NULL);
351596fd7ce5SGreg Kroah-Hartman 			if (IS_ERR(d)) {
351696fd7ce5SGreg Kroah-Hartman 				error = PTR_ERR(d);
351716a02081SJiri Slaby 				goto err_unreg_devs;
351896fd7ce5SGreg Kroah-Hartman 			}
351996fd7ce5SGreg Kroah-Hartman 		}
352096fd7ce5SGreg Kroah-Hartman 	}
352196fd7ce5SGreg Kroah-Hartman 	proc_tty_register_driver(driver);
352296fd7ce5SGreg Kroah-Hartman 	driver->flags |= TTY_DRIVER_INSTALLED;
352396fd7ce5SGreg Kroah-Hartman 	return 0;
352496fd7ce5SGreg Kroah-Hartman 
352516a02081SJiri Slaby err_unreg_devs:
352696fd7ce5SGreg Kroah-Hartman 	for (i--; i >= 0; i--)
352796fd7ce5SGreg Kroah-Hartman 		tty_unregister_device(driver, i);
352896fd7ce5SGreg Kroah-Hartman 
352996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
353096fd7ce5SGreg Kroah-Hartman 	list_del(&driver->tty_drivers);
353196fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
353296fd7ce5SGreg Kroah-Hartman 
35339bb8a3d4SJiri Slaby err_unreg_char:
353496fd7ce5SGreg Kroah-Hartman 	unregister_chrdev_region(dev, driver->num);
353516a02081SJiri Slaby err:
353696fd7ce5SGreg Kroah-Hartman 	return error;
353796fd7ce5SGreg Kroah-Hartman }
353896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_driver);
353996fd7ce5SGreg Kroah-Hartman 
354096fd7ce5SGreg Kroah-Hartman /*
354196fd7ce5SGreg Kroah-Hartman  * Called by a tty driver to unregister itself.
354296fd7ce5SGreg Kroah-Hartman  */
354396fd7ce5SGreg Kroah-Hartman int tty_unregister_driver(struct tty_driver *driver)
354496fd7ce5SGreg Kroah-Hartman {
354596fd7ce5SGreg Kroah-Hartman #if 0
354696fd7ce5SGreg Kroah-Hartman 	/* FIXME */
354796fd7ce5SGreg Kroah-Hartman 	if (driver->refcount)
354896fd7ce5SGreg Kroah-Hartman 		return -EBUSY;
354996fd7ce5SGreg Kroah-Hartman #endif
355096fd7ce5SGreg Kroah-Hartman 	unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
355196fd7ce5SGreg Kroah-Hartman 				driver->num);
355296fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
355396fd7ce5SGreg Kroah-Hartman 	list_del(&driver->tty_drivers);
355496fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
355596fd7ce5SGreg Kroah-Hartman 	return 0;
355696fd7ce5SGreg Kroah-Hartman }
355796fd7ce5SGreg Kroah-Hartman 
355896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_driver);
355996fd7ce5SGreg Kroah-Hartman 
356096fd7ce5SGreg Kroah-Hartman dev_t tty_devnum(struct tty_struct *tty)
356196fd7ce5SGreg Kroah-Hartman {
356296fd7ce5SGreg Kroah-Hartman 	return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
356396fd7ce5SGreg Kroah-Hartman }
356496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_devnum);
356596fd7ce5SGreg Kroah-Hartman 
356696fd7ce5SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops)
356796fd7ce5SGreg Kroah-Hartman {
356896fd7ce5SGreg Kroah-Hartman 	*fops = tty_fops;
356996fd7ce5SGreg Kroah-Hartman }
357096fd7ce5SGreg Kroah-Hartman 
357196fd7ce5SGreg Kroah-Hartman /*
357296fd7ce5SGreg Kroah-Hartman  * Initialize the console device. This is called *early*, so
357396fd7ce5SGreg Kroah-Hartman  * we can't necessarily depend on lots of kernel help here.
357496fd7ce5SGreg Kroah-Hartman  * Just do some early initializations, and do the complex setup
357596fd7ce5SGreg Kroah-Hartman  * later.
357696fd7ce5SGreg Kroah-Hartman  */
357796fd7ce5SGreg Kroah-Hartman void __init console_init(void)
357896fd7ce5SGreg Kroah-Hartman {
357996fd7ce5SGreg Kroah-Hartman 	initcall_t *call;
358096fd7ce5SGreg Kroah-Hartman 
358196fd7ce5SGreg Kroah-Hartman 	/* Setup the default TTY line discipline. */
358227228732SPeter Hurley 	n_tty_init();
358396fd7ce5SGreg Kroah-Hartman 
358496fd7ce5SGreg Kroah-Hartman 	/*
358596fd7ce5SGreg Kroah-Hartman 	 * set up the console device so that later boot sequences can
358696fd7ce5SGreg Kroah-Hartman 	 * inform about problems etc..
358796fd7ce5SGreg Kroah-Hartman 	 */
358896fd7ce5SGreg Kroah-Hartman 	call = __con_initcall_start;
358996fd7ce5SGreg Kroah-Hartman 	while (call < __con_initcall_end) {
359096fd7ce5SGreg Kroah-Hartman 		(*call)();
359196fd7ce5SGreg Kroah-Hartman 		call++;
359296fd7ce5SGreg Kroah-Hartman 	}
359396fd7ce5SGreg Kroah-Hartman }
359496fd7ce5SGreg Kroah-Hartman 
35952c9ede55SAl Viro static char *tty_devnode(struct device *dev, umode_t *mode)
359696fd7ce5SGreg Kroah-Hartman {
359796fd7ce5SGreg Kroah-Hartman 	if (!mode)
359896fd7ce5SGreg Kroah-Hartman 		return NULL;
359996fd7ce5SGreg Kroah-Hartman 	if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
360096fd7ce5SGreg Kroah-Hartman 	    dev->devt == MKDEV(TTYAUX_MAJOR, 2))
360196fd7ce5SGreg Kroah-Hartman 		*mode = 0666;
360296fd7ce5SGreg Kroah-Hartman 	return NULL;
360396fd7ce5SGreg Kroah-Hartman }
360496fd7ce5SGreg Kroah-Hartman 
360596fd7ce5SGreg Kroah-Hartman static int __init tty_class_init(void)
360696fd7ce5SGreg Kroah-Hartman {
360796fd7ce5SGreg Kroah-Hartman 	tty_class = class_create(THIS_MODULE, "tty");
360896fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(tty_class))
360996fd7ce5SGreg Kroah-Hartman 		return PTR_ERR(tty_class);
361096fd7ce5SGreg Kroah-Hartman 	tty_class->devnode = tty_devnode;
361196fd7ce5SGreg Kroah-Hartman 	return 0;
361296fd7ce5SGreg Kroah-Hartman }
361396fd7ce5SGreg Kroah-Hartman 
361496fd7ce5SGreg Kroah-Hartman postcore_initcall(tty_class_init);
361596fd7ce5SGreg Kroah-Hartman 
361696fd7ce5SGreg Kroah-Hartman /* 3/2004 jmc: why do these devices exist? */
361796fd7ce5SGreg Kroah-Hartman static struct cdev tty_cdev, console_cdev;
361896fd7ce5SGreg Kroah-Hartman 
3619fbc92a34SKay Sievers static ssize_t show_cons_active(struct device *dev,
3620fbc92a34SKay Sievers 				struct device_attribute *attr, char *buf)
3621fbc92a34SKay Sievers {
3622fbc92a34SKay Sievers 	struct console *cs[16];
3623fbc92a34SKay Sievers 	int i = 0;
3624fbc92a34SKay Sievers 	struct console *c;
3625fbc92a34SKay Sievers 	ssize_t count = 0;
3626fbc92a34SKay Sievers 
3627ac751efaSTorben Hohn 	console_lock();
3628a2a6a822SKay Sievers 	for_each_console(c) {
3629fbc92a34SKay Sievers 		if (!c->device)
3630fbc92a34SKay Sievers 			continue;
3631fbc92a34SKay Sievers 		if (!c->write)
3632fbc92a34SKay Sievers 			continue;
3633fbc92a34SKay Sievers 		if ((c->flags & CON_ENABLED) == 0)
3634fbc92a34SKay Sievers 			continue;
3635fbc92a34SKay Sievers 		cs[i++] = c;
3636fbc92a34SKay Sievers 		if (i >= ARRAY_SIZE(cs))
3637fbc92a34SKay Sievers 			break;
3638fbc92a34SKay Sievers 	}
3639723abd87SHannes Reinecke 	while (i--) {
3640723abd87SHannes Reinecke 		int index = cs[i]->index;
3641723abd87SHannes Reinecke 		struct tty_driver *drv = cs[i]->device(cs[i], &index);
3642723abd87SHannes Reinecke 
3643723abd87SHannes Reinecke 		/* don't resolve tty0 as some programs depend on it */
3644723abd87SHannes Reinecke 		if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3645723abd87SHannes Reinecke 			count += tty_line_name(drv, index, buf + count);
3646723abd87SHannes Reinecke 		else
3647723abd87SHannes Reinecke 			count += sprintf(buf + count, "%s%d",
3648723abd87SHannes Reinecke 					 cs[i]->name, cs[i]->index);
3649723abd87SHannes Reinecke 
3650723abd87SHannes Reinecke 		count += sprintf(buf + count, "%c", i ? ' ':'\n');
3651723abd87SHannes Reinecke 	}
3652ac751efaSTorben Hohn 	console_unlock();
3653fbc92a34SKay Sievers 
3654fbc92a34SKay Sievers 	return count;
3655fbc92a34SKay Sievers }
3656fbc92a34SKay Sievers static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3657fbc92a34SKay Sievers 
36581083a7beSTakashi Iwai static struct attribute *cons_dev_attrs[] = {
36591083a7beSTakashi Iwai 	&dev_attr_active.attr,
36601083a7beSTakashi Iwai 	NULL
36611083a7beSTakashi Iwai };
36621083a7beSTakashi Iwai 
36631083a7beSTakashi Iwai ATTRIBUTE_GROUPS(cons_dev);
36641083a7beSTakashi Iwai 
3665fbc92a34SKay Sievers static struct device *consdev;
3666fbc92a34SKay Sievers 
3667fbc92a34SKay Sievers void console_sysfs_notify(void)
3668fbc92a34SKay Sievers {
3669fbc92a34SKay Sievers 	if (consdev)
3670fbc92a34SKay Sievers 		sysfs_notify(&consdev->kobj, NULL, "active");
3671fbc92a34SKay Sievers }
3672fbc92a34SKay Sievers 
367396fd7ce5SGreg Kroah-Hartman /*
367496fd7ce5SGreg Kroah-Hartman  * Ok, now we can initialize the rest of the tty devices and can count
367596fd7ce5SGreg Kroah-Hartman  * on memory allocations, interrupts etc..
367696fd7ce5SGreg Kroah-Hartman  */
367796fd7ce5SGreg Kroah-Hartman int __init tty_init(void)
367896fd7ce5SGreg Kroah-Hartman {
367996fd7ce5SGreg Kroah-Hartman 	cdev_init(&tty_cdev, &tty_fops);
368096fd7ce5SGreg Kroah-Hartman 	if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
368196fd7ce5SGreg Kroah-Hartman 	    register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
368296fd7ce5SGreg Kroah-Hartman 		panic("Couldn't register /dev/tty driver\n");
3683fbc92a34SKay Sievers 	device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
368496fd7ce5SGreg Kroah-Hartman 
368596fd7ce5SGreg Kroah-Hartman 	cdev_init(&console_cdev, &console_fops);
368696fd7ce5SGreg Kroah-Hartman 	if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
368796fd7ce5SGreg Kroah-Hartman 	    register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
368896fd7ce5SGreg Kroah-Hartman 		panic("Couldn't register /dev/console driver\n");
36891083a7beSTakashi Iwai 	consdev = device_create_with_groups(tty_class, NULL,
36901083a7beSTakashi Iwai 					    MKDEV(TTYAUX_MAJOR, 1), NULL,
36911083a7beSTakashi Iwai 					    cons_dev_groups, "console");
3692fbc92a34SKay Sievers 	if (IS_ERR(consdev))
3693fbc92a34SKay Sievers 		consdev = NULL;
369496fd7ce5SGreg Kroah-Hartman 
369596fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT
369696fd7ce5SGreg Kroah-Hartman 	vty_init(&console_fops);
369796fd7ce5SGreg Kroah-Hartman #endif
369896fd7ce5SGreg Kroah-Hartman 	return 0;
369996fd7ce5SGreg Kroah-Hartman }
370096fd7ce5SGreg Kroah-Hartman 
3701