xref: /openbmc/linux/drivers/tty/tty_io.c (revision 83db1df4)
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,
12696fd7ce5SGreg Kroah-Hartman 	.c_ospeed = 38400
12796fd7ce5SGreg Kroah-Hartman };
12896fd7ce5SGreg Kroah-Hartman 
12996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_std_termios);
13096fd7ce5SGreg Kroah-Hartman 
13196fd7ce5SGreg Kroah-Hartman /* This list gets poked at by procfs and various bits of boot up code. This
13296fd7ce5SGreg Kroah-Hartman    could do with some rationalisation such as pulling the tty proc function
13396fd7ce5SGreg Kroah-Hartman    into this file */
13496fd7ce5SGreg Kroah-Hartman 
13596fd7ce5SGreg Kroah-Hartman LIST_HEAD(tty_drivers);			/* linked list of tty drivers */
13696fd7ce5SGreg Kroah-Hartman 
13796fd7ce5SGreg Kroah-Hartman /* Mutex to protect creating and releasing a tty. This is shared with
13896fd7ce5SGreg Kroah-Hartman    vt.c for deeply disgusting hack reasons */
13996fd7ce5SGreg Kroah-Hartman DEFINE_MUTEX(tty_mutex);
14096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_mutex);
14196fd7ce5SGreg Kroah-Hartman 
14296fd7ce5SGreg Kroah-Hartman /* Spinlock to protect the tty->tty_files list */
14396fd7ce5SGreg Kroah-Hartman DEFINE_SPINLOCK(tty_files_lock);
14496fd7ce5SGreg Kroah-Hartman 
14596fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
14696fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
14796fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *, const char __user *,
14896fd7ce5SGreg Kroah-Hartman 							size_t, loff_t *);
14996fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *, poll_table *);
15096fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *, struct file *);
15196fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
15296fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT
15396fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd,
15496fd7ce5SGreg Kroah-Hartman 				unsigned long arg);
15596fd7ce5SGreg Kroah-Hartman #else
15696fd7ce5SGreg Kroah-Hartman #define tty_compat_ioctl NULL
15796fd7ce5SGreg Kroah-Hartman #endif
15896fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on);
15996fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on);
16096fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx);
16196fd7ce5SGreg Kroah-Hartman 
16296fd7ce5SGreg Kroah-Hartman /**
16396fd7ce5SGreg Kroah-Hartman  *	free_tty_struct		-	free a disused tty
16496fd7ce5SGreg Kroah-Hartman  *	@tty: tty struct to free
16596fd7ce5SGreg Kroah-Hartman  *
16696fd7ce5SGreg Kroah-Hartman  *	Free the write buffers, tty queue and tty memory itself.
16796fd7ce5SGreg Kroah-Hartman  *
16896fd7ce5SGreg Kroah-Hartman  *	Locking: none. Must be called after tty is definitely unused
16996fd7ce5SGreg Kroah-Hartman  */
17096fd7ce5SGreg Kroah-Hartman 
17196fd7ce5SGreg Kroah-Hartman void free_tty_struct(struct tty_struct *tty)
17296fd7ce5SGreg Kroah-Hartman {
173dc6802a7SDan Carpenter 	if (!tty)
174dc6802a7SDan Carpenter 		return;
17596fd7ce5SGreg Kroah-Hartman 	put_device(tty->dev);
17696fd7ce5SGreg Kroah-Hartman 	kfree(tty->write_buf);
17789c8d91eSAlan Cox 	tty->magic = 0xDEADDEAD;
17896fd7ce5SGreg Kroah-Hartman 	kfree(tty);
17996fd7ce5SGreg Kroah-Hartman }
18096fd7ce5SGreg Kroah-Hartman 
18196fd7ce5SGreg Kroah-Hartman static inline struct tty_struct *file_tty(struct file *file)
18296fd7ce5SGreg Kroah-Hartman {
18396fd7ce5SGreg Kroah-Hartman 	return ((struct tty_file_private *)file->private_data)->tty;
18496fd7ce5SGreg Kroah-Hartman }
18596fd7ce5SGreg Kroah-Hartman 
186fa90e1c9SJiri Slaby int tty_alloc_file(struct file *file)
18796fd7ce5SGreg Kroah-Hartman {
18896fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv;
18996fd7ce5SGreg Kroah-Hartman 
19096fd7ce5SGreg Kroah-Hartman 	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
19196fd7ce5SGreg Kroah-Hartman 	if (!priv)
19296fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
19396fd7ce5SGreg Kroah-Hartman 
194fa90e1c9SJiri Slaby 	file->private_data = priv;
195fa90e1c9SJiri Slaby 
196fa90e1c9SJiri Slaby 	return 0;
197fa90e1c9SJiri Slaby }
198fa90e1c9SJiri Slaby 
199fa90e1c9SJiri Slaby /* Associate a new file with the tty structure */
200fa90e1c9SJiri Slaby void tty_add_file(struct tty_struct *tty, struct file *file)
201fa90e1c9SJiri Slaby {
202fa90e1c9SJiri Slaby 	struct tty_file_private *priv = file->private_data;
203fa90e1c9SJiri Slaby 
20496fd7ce5SGreg Kroah-Hartman 	priv->tty = tty;
20596fd7ce5SGreg Kroah-Hartman 	priv->file = file;
20696fd7ce5SGreg Kroah-Hartman 
20796fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
20896fd7ce5SGreg Kroah-Hartman 	list_add(&priv->list, &tty->tty_files);
20996fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
210fa90e1c9SJiri Slaby }
21196fd7ce5SGreg Kroah-Hartman 
212fa90e1c9SJiri Slaby /**
213fa90e1c9SJiri Slaby  * tty_free_file - free file->private_data
214fa90e1c9SJiri Slaby  *
215fa90e1c9SJiri Slaby  * This shall be used only for fail path handling when tty_add_file was not
216fa90e1c9SJiri Slaby  * called yet.
217fa90e1c9SJiri Slaby  */
218fa90e1c9SJiri Slaby void tty_free_file(struct file *file)
219fa90e1c9SJiri Slaby {
220fa90e1c9SJiri Slaby 	struct tty_file_private *priv = file->private_data;
221fa90e1c9SJiri Slaby 
222fa90e1c9SJiri Slaby 	file->private_data = NULL;
223fa90e1c9SJiri Slaby 	kfree(priv);
22496fd7ce5SGreg Kroah-Hartman }
22596fd7ce5SGreg Kroah-Hartman 
22696fd7ce5SGreg Kroah-Hartman /* Delete file from its tty */
2272520e274SJosh Triplett static void tty_del_file(struct file *file)
22896fd7ce5SGreg Kroah-Hartman {
22996fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv = file->private_data;
23096fd7ce5SGreg Kroah-Hartman 
23196fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
23296fd7ce5SGreg Kroah-Hartman 	list_del(&priv->list);
23396fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
234fa90e1c9SJiri Slaby 	tty_free_file(file);
23596fd7ce5SGreg Kroah-Hartman }
23696fd7ce5SGreg Kroah-Hartman 
23796fd7ce5SGreg Kroah-Hartman 
23896fd7ce5SGreg Kroah-Hartman #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
23996fd7ce5SGreg Kroah-Hartman 
24096fd7ce5SGreg Kroah-Hartman /**
24196fd7ce5SGreg Kroah-Hartman  *	tty_name	-	return tty naming
24296fd7ce5SGreg Kroah-Hartman  *	@tty: tty structure
24396fd7ce5SGreg Kroah-Hartman  *
24496fd7ce5SGreg Kroah-Hartman  *	Convert a tty structure into a name. The name reflects the kernel
24596fd7ce5SGreg Kroah-Hartman  *	naming policy and if udev is in use may not reflect user space
24696fd7ce5SGreg Kroah-Hartman  *
24796fd7ce5SGreg Kroah-Hartman  *	Locking: none
24896fd7ce5SGreg Kroah-Hartman  */
24996fd7ce5SGreg Kroah-Hartman 
250429b4749SRasmus Villemoes const char *tty_name(const struct tty_struct *tty)
25196fd7ce5SGreg Kroah-Hartman {
25296fd7ce5SGreg Kroah-Hartman 	if (!tty) /* Hmm.  NULL pointer.  That's fun. */
253917162c9SRasmus Villemoes 		return "NULL tty";
254917162c9SRasmus Villemoes 	return tty->name;
25596fd7ce5SGreg Kroah-Hartman }
25696fd7ce5SGreg Kroah-Hartman 
25796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_name);
25896fd7ce5SGreg Kroah-Hartman 
2590a083eddSPeter Hurley const char *tty_driver_name(const struct tty_struct *tty)
2600a083eddSPeter Hurley {
2610a083eddSPeter Hurley 	if (!tty || !tty->driver)
2620a083eddSPeter Hurley 		return "";
2630a083eddSPeter Hurley 	return tty->driver->name;
2640a083eddSPeter Hurley }
2650a083eddSPeter Hurley 
26682b8f888SPeter Hurley static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
26796fd7ce5SGreg Kroah-Hartman 			      const char *routine)
26896fd7ce5SGreg Kroah-Hartman {
26996fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK
27096fd7ce5SGreg Kroah-Hartman 	if (!tty) {
27189222e62SPeter Hurley 		pr_warn("(%d:%d): %s: NULL tty\n",
27296fd7ce5SGreg Kroah-Hartman 			imajor(inode), iminor(inode), routine);
27396fd7ce5SGreg Kroah-Hartman 		return 1;
27496fd7ce5SGreg Kroah-Hartman 	}
27596fd7ce5SGreg Kroah-Hartman 	if (tty->magic != TTY_MAGIC) {
27689222e62SPeter Hurley 		pr_warn("(%d:%d): %s: bad magic number\n",
27796fd7ce5SGreg Kroah-Hartman 			imajor(inode), iminor(inode), routine);
27896fd7ce5SGreg Kroah-Hartman 		return 1;
27996fd7ce5SGreg Kroah-Hartman 	}
28096fd7ce5SGreg Kroah-Hartman #endif
28196fd7ce5SGreg Kroah-Hartman 	return 0;
28296fd7ce5SGreg Kroah-Hartman }
28396fd7ce5SGreg Kroah-Hartman 
284deb287e7SPeter Hurley /* Caller must hold tty_lock */
28596fd7ce5SGreg Kroah-Hartman static int check_tty_count(struct tty_struct *tty, const char *routine)
28696fd7ce5SGreg Kroah-Hartman {
28796fd7ce5SGreg Kroah-Hartman #ifdef CHECK_TTY_COUNT
28896fd7ce5SGreg Kroah-Hartman 	struct list_head *p;
28996fd7ce5SGreg Kroah-Hartman 	int count = 0;
29096fd7ce5SGreg Kroah-Hartman 
29196fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
29296fd7ce5SGreg Kroah-Hartman 	list_for_each(p, &tty->tty_files) {
29396fd7ce5SGreg Kroah-Hartman 		count++;
29496fd7ce5SGreg Kroah-Hartman 	}
29596fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
29696fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
29796fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_SLAVE &&
29896fd7ce5SGreg Kroah-Hartman 	    tty->link && tty->link->count)
29996fd7ce5SGreg Kroah-Hartman 		count++;
30096fd7ce5SGreg Kroah-Hartman 	if (tty->count != count) {
301339f36baSPeter Hurley 		tty_warn(tty, "%s: tty->count(%d) != #fd's(%d)\n",
302339f36baSPeter Hurley 			 routine, tty->count, count);
30396fd7ce5SGreg Kroah-Hartman 		return count;
30496fd7ce5SGreg Kroah-Hartman 	}
30596fd7ce5SGreg Kroah-Hartman #endif
30696fd7ce5SGreg Kroah-Hartman 	return 0;
30796fd7ce5SGreg Kroah-Hartman }
30896fd7ce5SGreg Kroah-Hartman 
30996fd7ce5SGreg Kroah-Hartman /**
31096fd7ce5SGreg Kroah-Hartman  *	get_tty_driver		-	find device of a tty
31196fd7ce5SGreg Kroah-Hartman  *	@dev_t: device identifier
31296fd7ce5SGreg Kroah-Hartman  *	@index: returns the index of the tty
31396fd7ce5SGreg Kroah-Hartman  *
31496fd7ce5SGreg Kroah-Hartman  *	This routine returns a tty driver structure, given a device number
31596fd7ce5SGreg Kroah-Hartman  *	and also passes back the index number.
31696fd7ce5SGreg Kroah-Hartman  *
31796fd7ce5SGreg Kroah-Hartman  *	Locking: caller must hold tty_mutex
31896fd7ce5SGreg Kroah-Hartman  */
31996fd7ce5SGreg Kroah-Hartman 
32096fd7ce5SGreg Kroah-Hartman static struct tty_driver *get_tty_driver(dev_t device, int *index)
32196fd7ce5SGreg Kroah-Hartman {
32296fd7ce5SGreg Kroah-Hartman 	struct tty_driver *p;
32396fd7ce5SGreg Kroah-Hartman 
32496fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(p, &tty_drivers, tty_drivers) {
32596fd7ce5SGreg Kroah-Hartman 		dev_t base = MKDEV(p->major, p->minor_start);
32696fd7ce5SGreg Kroah-Hartman 		if (device < base || device >= base + p->num)
32796fd7ce5SGreg Kroah-Hartman 			continue;
32896fd7ce5SGreg Kroah-Hartman 		*index = device - base;
32996fd7ce5SGreg Kroah-Hartman 		return tty_driver_kref_get(p);
33096fd7ce5SGreg Kroah-Hartman 	}
33196fd7ce5SGreg Kroah-Hartman 	return NULL;
33296fd7ce5SGreg Kroah-Hartman }
33396fd7ce5SGreg Kroah-Hartman 
33496fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
33596fd7ce5SGreg Kroah-Hartman 
33696fd7ce5SGreg Kroah-Hartman /**
33796fd7ce5SGreg Kroah-Hartman  *	tty_find_polling_driver	-	find device of a polled tty
33896fd7ce5SGreg Kroah-Hartman  *	@name: name string to match
33996fd7ce5SGreg Kroah-Hartman  *	@line: pointer to resulting tty line nr
34096fd7ce5SGreg Kroah-Hartman  *
34196fd7ce5SGreg Kroah-Hartman  *	This routine returns a tty driver structure, given a name
34296fd7ce5SGreg Kroah-Hartman  *	and the condition that the tty driver is capable of polled
34396fd7ce5SGreg Kroah-Hartman  *	operation.
34496fd7ce5SGreg Kroah-Hartman  */
34596fd7ce5SGreg Kroah-Hartman struct tty_driver *tty_find_polling_driver(char *name, int *line)
34696fd7ce5SGreg Kroah-Hartman {
34796fd7ce5SGreg Kroah-Hartman 	struct tty_driver *p, *res = NULL;
34896fd7ce5SGreg Kroah-Hartman 	int tty_line = 0;
34996fd7ce5SGreg Kroah-Hartman 	int len;
35096fd7ce5SGreg Kroah-Hartman 	char *str, *stp;
35196fd7ce5SGreg Kroah-Hartman 
35296fd7ce5SGreg Kroah-Hartman 	for (str = name; *str; str++)
35396fd7ce5SGreg Kroah-Hartman 		if ((*str >= '0' && *str <= '9') || *str == ',')
35496fd7ce5SGreg Kroah-Hartman 			break;
35596fd7ce5SGreg Kroah-Hartman 	if (!*str)
35696fd7ce5SGreg Kroah-Hartman 		return NULL;
35796fd7ce5SGreg Kroah-Hartman 
35896fd7ce5SGreg Kroah-Hartman 	len = str - name;
35996fd7ce5SGreg Kroah-Hartman 	tty_line = simple_strtoul(str, &str, 10);
36096fd7ce5SGreg Kroah-Hartman 
36196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
36296fd7ce5SGreg Kroah-Hartman 	/* Search through the tty devices to look for a match */
36396fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(p, &tty_drivers, tty_drivers) {
36496fd7ce5SGreg Kroah-Hartman 		if (strncmp(name, p->name, len) != 0)
36596fd7ce5SGreg Kroah-Hartman 			continue;
36696fd7ce5SGreg Kroah-Hartman 		stp = str;
36796fd7ce5SGreg Kroah-Hartman 		if (*stp == ',')
36896fd7ce5SGreg Kroah-Hartman 			stp++;
36996fd7ce5SGreg Kroah-Hartman 		if (*stp == '\0')
37096fd7ce5SGreg Kroah-Hartman 			stp = NULL;
37196fd7ce5SGreg Kroah-Hartman 
37296fd7ce5SGreg Kroah-Hartman 		if (tty_line >= 0 && tty_line < p->num && p->ops &&
37396fd7ce5SGreg Kroah-Hartman 		    p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
37496fd7ce5SGreg Kroah-Hartman 			res = tty_driver_kref_get(p);
37596fd7ce5SGreg Kroah-Hartman 			*line = tty_line;
37696fd7ce5SGreg Kroah-Hartman 			break;
37796fd7ce5SGreg Kroah-Hartman 		}
37896fd7ce5SGreg Kroah-Hartman 	}
37996fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
38096fd7ce5SGreg Kroah-Hartman 
38196fd7ce5SGreg Kroah-Hartman 	return res;
38296fd7ce5SGreg Kroah-Hartman }
38396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_find_polling_driver);
38496fd7ce5SGreg Kroah-Hartman #endif
38596fd7ce5SGreg Kroah-Hartman 
38696fd7ce5SGreg Kroah-Hartman /**
38796fd7ce5SGreg Kroah-Hartman  *	tty_check_change	-	check for POSIX terminal changes
38896fd7ce5SGreg Kroah-Hartman  *	@tty: tty to check
38996fd7ce5SGreg Kroah-Hartman  *
39096fd7ce5SGreg Kroah-Hartman  *	If we try to write to, or set the state of, a terminal and we're
39196fd7ce5SGreg Kroah-Hartman  *	not in the foreground, send a SIGTTOU.  If the signal is blocked or
39296fd7ce5SGreg Kroah-Hartman  *	ignored, go ahead and perform the operation.  (POSIX 7.2)
39396fd7ce5SGreg Kroah-Hartman  *
39496fd7ce5SGreg Kroah-Hartman  *	Locking: ctrl_lock
39596fd7ce5SGreg Kroah-Hartman  */
39696fd7ce5SGreg Kroah-Hartman 
3972812d9e9SPeter Hurley int __tty_check_change(struct tty_struct *tty, int sig)
39896fd7ce5SGreg Kroah-Hartman {
39996fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
4002812d9e9SPeter Hurley 	struct pid *pgrp, *tty_pgrp;
40196fd7ce5SGreg Kroah-Hartman 	int ret = 0;
40296fd7ce5SGreg Kroah-Hartman 
40396fd7ce5SGreg Kroah-Hartman 	if (current->signal->tty != tty)
40496fd7ce5SGreg Kroah-Hartman 		return 0;
40596fd7ce5SGreg Kroah-Hartman 
4066719693cSPatrick Donnelly 	rcu_read_lock();
4076719693cSPatrick Donnelly 	pgrp = task_pgrp(current);
4086719693cSPatrick Donnelly 
40996fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
4102812d9e9SPeter Hurley 	tty_pgrp = tty->pgrp;
41196fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
4126719693cSPatrick Donnelly 
4132812d9e9SPeter Hurley 	if (tty_pgrp && pgrp != tty->pgrp) {
4142812d9e9SPeter Hurley 		if (is_ignored(sig)) {
4152812d9e9SPeter Hurley 			if (sig == SIGTTIN)
41696fd7ce5SGreg Kroah-Hartman 				ret = -EIO;
4172812d9e9SPeter Hurley 		} else if (is_current_pgrp_orphaned())
4182812d9e9SPeter Hurley 			ret = -EIO;
4192812d9e9SPeter Hurley 		else {
4202812d9e9SPeter Hurley 			kill_pgrp(pgrp, sig, 1);
42196fd7ce5SGreg Kroah-Hartman 			set_thread_flag(TIF_SIGPENDING);
42296fd7ce5SGreg Kroah-Hartman 			ret = -ERESTARTSYS;
4232812d9e9SPeter Hurley 		}
4242812d9e9SPeter Hurley 	}
4256719693cSPatrick Donnelly 	rcu_read_unlock();
4262812d9e9SPeter Hurley 
427339f36baSPeter Hurley 	if (!tty_pgrp)
428339f36baSPeter Hurley 		tty_warn(tty, "sig=%d, tty->pgrp == NULL!\n", sig);
4292812d9e9SPeter Hurley 
43096fd7ce5SGreg Kroah-Hartman 	return ret;
43196fd7ce5SGreg Kroah-Hartman }
43296fd7ce5SGreg Kroah-Hartman 
4332812d9e9SPeter Hurley int tty_check_change(struct tty_struct *tty)
4342812d9e9SPeter Hurley {
4352812d9e9SPeter Hurley 	return __tty_check_change(tty, SIGTTOU);
4362812d9e9SPeter Hurley }
43796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_check_change);
43896fd7ce5SGreg Kroah-Hartman 
43996fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
44096fd7ce5SGreg Kroah-Hartman 				size_t count, loff_t *ppos)
44196fd7ce5SGreg Kroah-Hartman {
44296fd7ce5SGreg Kroah-Hartman 	return 0;
44396fd7ce5SGreg Kroah-Hartman }
44496fd7ce5SGreg Kroah-Hartman 
44596fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
44696fd7ce5SGreg Kroah-Hartman 				 size_t count, loff_t *ppos)
44796fd7ce5SGreg Kroah-Hartman {
44896fd7ce5SGreg Kroah-Hartman 	return -EIO;
44996fd7ce5SGreg Kroah-Hartman }
45096fd7ce5SGreg Kroah-Hartman 
45196fd7ce5SGreg Kroah-Hartman /* No kernel lock held - none needed ;) */
45296fd7ce5SGreg Kroah-Hartman static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
45396fd7ce5SGreg Kroah-Hartman {
45496fd7ce5SGreg Kroah-Hartman 	return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
45596fd7ce5SGreg Kroah-Hartman }
45696fd7ce5SGreg Kroah-Hartman 
45796fd7ce5SGreg Kroah-Hartman static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
45896fd7ce5SGreg Kroah-Hartman 		unsigned long arg)
45996fd7ce5SGreg Kroah-Hartman {
46096fd7ce5SGreg Kroah-Hartman 	return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
46196fd7ce5SGreg Kroah-Hartman }
46296fd7ce5SGreg Kroah-Hartman 
46396fd7ce5SGreg Kroah-Hartman static long hung_up_tty_compat_ioctl(struct file *file,
46496fd7ce5SGreg Kroah-Hartman 				     unsigned int cmd, unsigned long arg)
46596fd7ce5SGreg Kroah-Hartman {
46696fd7ce5SGreg Kroah-Hartman 	return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
46796fd7ce5SGreg Kroah-Hartman }
46896fd7ce5SGreg Kroah-Hartman 
46996fd7ce5SGreg Kroah-Hartman static const struct file_operations tty_fops = {
47096fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
47196fd7ce5SGreg Kroah-Hartman 	.read		= tty_read,
47296fd7ce5SGreg Kroah-Hartman 	.write		= tty_write,
47396fd7ce5SGreg Kroah-Hartman 	.poll		= tty_poll,
47496fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= tty_ioctl,
47596fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= tty_compat_ioctl,
47696fd7ce5SGreg Kroah-Hartman 	.open		= tty_open,
47796fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
47896fd7ce5SGreg Kroah-Hartman 	.fasync		= tty_fasync,
47996fd7ce5SGreg Kroah-Hartman };
48096fd7ce5SGreg Kroah-Hartman 
48196fd7ce5SGreg Kroah-Hartman static const struct file_operations console_fops = {
48296fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
48396fd7ce5SGreg Kroah-Hartman 	.read		= tty_read,
48496fd7ce5SGreg Kroah-Hartman 	.write		= redirected_tty_write,
48596fd7ce5SGreg Kroah-Hartman 	.poll		= tty_poll,
48696fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= tty_ioctl,
48796fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= tty_compat_ioctl,
48896fd7ce5SGreg Kroah-Hartman 	.open		= tty_open,
48996fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
49096fd7ce5SGreg Kroah-Hartman 	.fasync		= tty_fasync,
49196fd7ce5SGreg Kroah-Hartman };
49296fd7ce5SGreg Kroah-Hartman 
49396fd7ce5SGreg Kroah-Hartman static const struct file_operations hung_up_tty_fops = {
49496fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
49596fd7ce5SGreg Kroah-Hartman 	.read		= hung_up_tty_read,
49696fd7ce5SGreg Kroah-Hartman 	.write		= hung_up_tty_write,
49796fd7ce5SGreg Kroah-Hartman 	.poll		= hung_up_tty_poll,
49896fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= hung_up_tty_ioctl,
49996fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= hung_up_tty_compat_ioctl,
50096fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
50196fd7ce5SGreg Kroah-Hartman };
50296fd7ce5SGreg Kroah-Hartman 
50396fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(redirect_lock);
50496fd7ce5SGreg Kroah-Hartman static struct file *redirect;
50596fd7ce5SGreg Kroah-Hartman 
50611d9befdSPeter Hurley 
50711d9befdSPeter Hurley void proc_clear_tty(struct task_struct *p)
50811d9befdSPeter Hurley {
50911d9befdSPeter Hurley 	unsigned long flags;
51011d9befdSPeter Hurley 	struct tty_struct *tty;
51111d9befdSPeter Hurley 	spin_lock_irqsave(&p->sighand->siglock, flags);
51211d9befdSPeter Hurley 	tty = p->signal->tty;
51311d9befdSPeter Hurley 	p->signal->tty = NULL;
51411d9befdSPeter Hurley 	spin_unlock_irqrestore(&p->sighand->siglock, flags);
51511d9befdSPeter Hurley 	tty_kref_put(tty);
51611d9befdSPeter Hurley }
51711d9befdSPeter Hurley 
5182c411c11SPeter Hurley /**
5192c411c11SPeter Hurley  * proc_set_tty -  set the controlling terminal
5202c411c11SPeter Hurley  *
5212c411c11SPeter Hurley  * Only callable by the session leader and only if it does not already have
5222c411c11SPeter Hurley  * a controlling terminal.
5232c411c11SPeter Hurley  *
524e218eb32SPeter Hurley  * Caller must hold:  tty_lock()
525e218eb32SPeter Hurley  *		      a readlock on tasklist_lock
5262c411c11SPeter Hurley  *		      sighand lock
5272c411c11SPeter Hurley  */
528bce65f18SPeter Hurley static void __proc_set_tty(struct tty_struct *tty)
52911d9befdSPeter Hurley {
53011d9befdSPeter Hurley 	unsigned long flags;
531ae28fa72SPeter Hurley 
53211d9befdSPeter Hurley 	spin_lock_irqsave(&tty->ctrl_lock, flags);
533a361858dSPeter Hurley 	/*
534a361858dSPeter Hurley 	 * The session and fg pgrp references will be non-NULL if
535a361858dSPeter Hurley 	 * tiocsctty() is stealing the controlling tty
536a361858dSPeter Hurley 	 */
53711d9befdSPeter Hurley 	put_pid(tty->session);
53811d9befdSPeter Hurley 	put_pid(tty->pgrp);
539bce65f18SPeter Hurley 	tty->pgrp = get_pid(task_pgrp(current));
54011d9befdSPeter Hurley 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
541bce65f18SPeter Hurley 	tty->session = get_pid(task_session(current));
542bce65f18SPeter Hurley 	if (current->signal->tty) {
543e2dfa3d3SPeter Hurley 		tty_debug(tty, "current tty %s not NULL!!\n",
544e2dfa3d3SPeter Hurley 			  current->signal->tty->name);
545bce65f18SPeter Hurley 		tty_kref_put(current->signal->tty);
54611d9befdSPeter Hurley 	}
547bce65f18SPeter Hurley 	put_pid(current->signal->tty_old_pgrp);
548bce65f18SPeter Hurley 	current->signal->tty = tty_kref_get(tty);
549bce65f18SPeter Hurley 	current->signal->tty_old_pgrp = NULL;
55011d9befdSPeter Hurley }
55111d9befdSPeter Hurley 
552bce65f18SPeter Hurley static void proc_set_tty(struct tty_struct *tty)
55311d9befdSPeter Hurley {
554bce65f18SPeter Hurley 	spin_lock_irq(&current->sighand->siglock);
555bce65f18SPeter Hurley 	__proc_set_tty(tty);
556bce65f18SPeter Hurley 	spin_unlock_irq(&current->sighand->siglock);
55711d9befdSPeter Hurley }
55811d9befdSPeter Hurley 
55911d9befdSPeter Hurley struct tty_struct *get_current_tty(void)
56011d9befdSPeter Hurley {
56111d9befdSPeter Hurley 	struct tty_struct *tty;
56211d9befdSPeter Hurley 	unsigned long flags;
56311d9befdSPeter Hurley 
56411d9befdSPeter Hurley 	spin_lock_irqsave(&current->sighand->siglock, flags);
56511d9befdSPeter Hurley 	tty = tty_kref_get(current->signal->tty);
56611d9befdSPeter Hurley 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
56711d9befdSPeter Hurley 	return tty;
56811d9befdSPeter Hurley }
56911d9befdSPeter Hurley EXPORT_SYMBOL_GPL(get_current_tty);
57011d9befdSPeter Hurley 
57111d9befdSPeter Hurley static void session_clear_tty(struct pid *session)
57211d9befdSPeter Hurley {
57311d9befdSPeter Hurley 	struct task_struct *p;
57411d9befdSPeter Hurley 	do_each_pid_task(session, PIDTYPE_SID, p) {
57511d9befdSPeter Hurley 		proc_clear_tty(p);
57611d9befdSPeter Hurley 	} while_each_pid_task(session, PIDTYPE_SID, p);
57711d9befdSPeter Hurley }
57811d9befdSPeter Hurley 
57996fd7ce5SGreg Kroah-Hartman /**
58096fd7ce5SGreg Kroah-Hartman  *	tty_wakeup	-	request more data
58196fd7ce5SGreg Kroah-Hartman  *	@tty: terminal
58296fd7ce5SGreg Kroah-Hartman  *
58396fd7ce5SGreg Kroah-Hartman  *	Internal and external helper for wakeups of tty. This function
58496fd7ce5SGreg Kroah-Hartman  *	informs the line discipline if present that the driver is ready
58596fd7ce5SGreg Kroah-Hartman  *	to receive more output data.
58696fd7ce5SGreg Kroah-Hartman  */
58796fd7ce5SGreg Kroah-Hartman 
58896fd7ce5SGreg Kroah-Hartman void tty_wakeup(struct tty_struct *tty)
58996fd7ce5SGreg Kroah-Hartman {
59096fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
59196fd7ce5SGreg Kroah-Hartman 
59296fd7ce5SGreg Kroah-Hartman 	if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
59396fd7ce5SGreg Kroah-Hartman 		ld = tty_ldisc_ref(tty);
59496fd7ce5SGreg Kroah-Hartman 		if (ld) {
59596fd7ce5SGreg Kroah-Hartman 			if (ld->ops->write_wakeup)
59696fd7ce5SGreg Kroah-Hartman 				ld->ops->write_wakeup(tty);
59796fd7ce5SGreg Kroah-Hartman 			tty_ldisc_deref(ld);
59896fd7ce5SGreg Kroah-Hartman 		}
59996fd7ce5SGreg Kroah-Hartman 	}
60096fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
60196fd7ce5SGreg Kroah-Hartman }
60296fd7ce5SGreg Kroah-Hartman 
60396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_wakeup);
60496fd7ce5SGreg Kroah-Hartman 
60596fd7ce5SGreg Kroah-Hartman /**
606ea648a47SPeter Hurley  *	tty_signal_session_leader	- sends SIGHUP to session leader
607f91e2590SPeter Hurley  *	@tty		controlling tty
608f91e2590SPeter Hurley  *	@exit_session	if non-zero, signal all foreground group processes
609ea648a47SPeter Hurley  *
610f91e2590SPeter Hurley  *	Send SIGHUP and SIGCONT to the session leader and its process group.
611f91e2590SPeter Hurley  *	Optionally, signal all processes in the foreground process group.
612ea648a47SPeter Hurley  *
613ea648a47SPeter Hurley  *	Returns the number of processes in the session with this tty
614ea648a47SPeter Hurley  *	as their controlling terminal. This value is used to drop
615ea648a47SPeter Hurley  *	tty references for those processes.
616ea648a47SPeter Hurley  */
617f91e2590SPeter Hurley static int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
618ea648a47SPeter Hurley {
619ea648a47SPeter Hurley 	struct task_struct *p;
620ea648a47SPeter Hurley 	int refs = 0;
621f91e2590SPeter Hurley 	struct pid *tty_pgrp = NULL;
622ea648a47SPeter Hurley 
623ea648a47SPeter Hurley 	read_lock(&tasklist_lock);
624ea648a47SPeter Hurley 	if (tty->session) {
625ea648a47SPeter Hurley 		do_each_pid_task(tty->session, PIDTYPE_SID, p) {
626ea648a47SPeter Hurley 			spin_lock_irq(&p->sighand->siglock);
627ea648a47SPeter Hurley 			if (p->signal->tty == tty) {
628ea648a47SPeter Hurley 				p->signal->tty = NULL;
629ea648a47SPeter Hurley 				/* We defer the dereferences outside fo
630ea648a47SPeter Hurley 				   the tasklist lock */
631ea648a47SPeter Hurley 				refs++;
632ea648a47SPeter Hurley 			}
633ea648a47SPeter Hurley 			if (!p->signal->leader) {
634ea648a47SPeter Hurley 				spin_unlock_irq(&p->sighand->siglock);
635ea648a47SPeter Hurley 				continue;
636ea648a47SPeter Hurley 			}
637ea648a47SPeter Hurley 			__group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
638ea648a47SPeter Hurley 			__group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
639ea648a47SPeter Hurley 			put_pid(p->signal->tty_old_pgrp);  /* A noop */
640bc30c3b2SPeter Hurley 			spin_lock(&tty->ctrl_lock);
641f91e2590SPeter Hurley 			tty_pgrp = get_pid(tty->pgrp);
642ea648a47SPeter Hurley 			if (tty->pgrp)
643ea648a47SPeter Hurley 				p->signal->tty_old_pgrp = get_pid(tty->pgrp);
644bc30c3b2SPeter Hurley 			spin_unlock(&tty->ctrl_lock);
645ea648a47SPeter Hurley 			spin_unlock_irq(&p->sighand->siglock);
646ea648a47SPeter Hurley 		} while_each_pid_task(tty->session, PIDTYPE_SID, p);
647ea648a47SPeter Hurley 	}
648ea648a47SPeter Hurley 	read_unlock(&tasklist_lock);
649ea648a47SPeter Hurley 
650f91e2590SPeter Hurley 	if (tty_pgrp) {
651f91e2590SPeter Hurley 		if (exit_session)
652f91e2590SPeter Hurley 			kill_pgrp(tty_pgrp, SIGHUP, exit_session);
653f91e2590SPeter Hurley 		put_pid(tty_pgrp);
654f91e2590SPeter Hurley 	}
655f91e2590SPeter Hurley 
656ea648a47SPeter Hurley 	return refs;
657ea648a47SPeter Hurley }
658ea648a47SPeter Hurley 
659ea648a47SPeter Hurley /**
66096fd7ce5SGreg Kroah-Hartman  *	__tty_hangup		-	actual handler for hangup events
66196fd7ce5SGreg Kroah-Hartman  *	@work: tty device
66296fd7ce5SGreg Kroah-Hartman  *
663ef4f527cSKevin Cernekee  *	This can be called by a "kworker" kernel thread.  That is process
66496fd7ce5SGreg Kroah-Hartman  *	synchronous but doesn't hold any locks, so we need to make sure we
66596fd7ce5SGreg Kroah-Hartman  *	have the appropriate locks for what we're doing.
66696fd7ce5SGreg Kroah-Hartman  *
66796fd7ce5SGreg Kroah-Hartman  *	The hangup event clears any pending redirections onto the hung up
66896fd7ce5SGreg Kroah-Hartman  *	device. It ensures future writes will error and it does the needed
66996fd7ce5SGreg Kroah-Hartman  *	line discipline hangup and signal delivery. The tty object itself
67096fd7ce5SGreg Kroah-Hartman  *	remains intact.
67196fd7ce5SGreg Kroah-Hartman  *
67296fd7ce5SGreg Kroah-Hartman  *	Locking:
67396fd7ce5SGreg Kroah-Hartman  *		BTM
67496fd7ce5SGreg Kroah-Hartman  *		  redirect lock for undoing redirection
67596fd7ce5SGreg Kroah-Hartman  *		  file list lock for manipulating list of ttys
676137084bbSPeter Hurley  *		  tty_ldiscs_lock from called functions
6776a1c0680SPeter Hurley  *		  termios_rwsem resetting termios data
67896fd7ce5SGreg Kroah-Hartman  *		  tasklist_lock to walk task list for hangup event
67996fd7ce5SGreg Kroah-Hartman  *		    ->siglock to protect ->signal/->sighand
68096fd7ce5SGreg Kroah-Hartman  */
681f91e2590SPeter Hurley static void __tty_hangup(struct tty_struct *tty, int exit_session)
68296fd7ce5SGreg Kroah-Hartman {
68396fd7ce5SGreg Kroah-Hartman 	struct file *cons_filp = NULL;
68496fd7ce5SGreg Kroah-Hartman 	struct file *filp, *f = NULL;
68596fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv;
68696fd7ce5SGreg Kroah-Hartman 	int    closecount = 0, n;
687ea648a47SPeter Hurley 	int refs;
68896fd7ce5SGreg Kroah-Hartman 
68996fd7ce5SGreg Kroah-Hartman 	if (!tty)
69096fd7ce5SGreg Kroah-Hartman 		return;
69196fd7ce5SGreg Kroah-Hartman 
69296fd7ce5SGreg Kroah-Hartman 
69396fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
69496fd7ce5SGreg Kroah-Hartman 	if (redirect && file_tty(redirect) == tty) {
69596fd7ce5SGreg Kroah-Hartman 		f = redirect;
69696fd7ce5SGreg Kroah-Hartman 		redirect = NULL;
69796fd7ce5SGreg Kroah-Hartman 	}
69896fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
69996fd7ce5SGreg Kroah-Hartman 
70089c8d91eSAlan Cox 	tty_lock(tty);
70196fd7ce5SGreg Kroah-Hartman 
702cb50e523SPeter Hurley 	if (test_bit(TTY_HUPPED, &tty->flags)) {
703cb50e523SPeter Hurley 		tty_unlock(tty);
704cb50e523SPeter Hurley 		return;
705cb50e523SPeter Hurley 	}
706cb50e523SPeter Hurley 
70796fd7ce5SGreg Kroah-Hartman 	/* inuse_filps is protected by the single tty lock,
70896fd7ce5SGreg Kroah-Hartman 	   this really needs to change if we want to flush the
70996fd7ce5SGreg Kroah-Hartman 	   workqueue with the lock held */
71096fd7ce5SGreg Kroah-Hartman 	check_tty_count(tty, "tty_hangup");
71196fd7ce5SGreg Kroah-Hartman 
71296fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
71396fd7ce5SGreg Kroah-Hartman 	/* This breaks for file handles being sent over AF_UNIX sockets ? */
71496fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(priv, &tty->tty_files, list) {
71596fd7ce5SGreg Kroah-Hartman 		filp = priv->file;
71696fd7ce5SGreg Kroah-Hartman 		if (filp->f_op->write == redirected_tty_write)
71796fd7ce5SGreg Kroah-Hartman 			cons_filp = filp;
71896fd7ce5SGreg Kroah-Hartman 		if (filp->f_op->write != tty_write)
71996fd7ce5SGreg Kroah-Hartman 			continue;
72096fd7ce5SGreg Kroah-Hartman 		closecount++;
72196fd7ce5SGreg Kroah-Hartman 		__tty_fasync(-1, filp, 0);	/* can't block */
72296fd7ce5SGreg Kroah-Hartman 		filp->f_op = &hung_up_tty_fops;
72396fd7ce5SGreg Kroah-Hartman 	}
72496fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
72596fd7ce5SGreg Kroah-Hartman 
72625fdf243SPeter Hurley 	refs = tty_signal_session_leader(tty, exit_session);
72725fdf243SPeter Hurley 	/* Account for the p->signal references we killed */
72825fdf243SPeter Hurley 	while (refs--)
72925fdf243SPeter Hurley 		tty_kref_put(tty);
73025fdf243SPeter Hurley 
73196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_hangup(tty);
73296fd7ce5SGreg Kroah-Hartman 
73320cc225bSPeter Hurley 	spin_lock_irq(&tty->ctrl_lock);
73496fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_THROTTLED, &tty->flags);
73596fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
73696fd7ce5SGreg Kroah-Hartman 	put_pid(tty->session);
73796fd7ce5SGreg Kroah-Hartman 	put_pid(tty->pgrp);
73896fd7ce5SGreg Kroah-Hartman 	tty->session = NULL;
73996fd7ce5SGreg Kroah-Hartman 	tty->pgrp = NULL;
74096fd7ce5SGreg Kroah-Hartman 	tty->ctrl_status = 0;
74120cc225bSPeter Hurley 	spin_unlock_irq(&tty->ctrl_lock);
74296fd7ce5SGreg Kroah-Hartman 
74396fd7ce5SGreg Kroah-Hartman 	/*
74496fd7ce5SGreg Kroah-Hartman 	 * If one of the devices matches a console pointer, we
74596fd7ce5SGreg Kroah-Hartman 	 * cannot just call hangup() because that will cause
74696fd7ce5SGreg Kroah-Hartman 	 * tty->count and state->count to go out of sync.
74796fd7ce5SGreg Kroah-Hartman 	 * So we just call close() the right number of times.
74896fd7ce5SGreg Kroah-Hartman 	 */
74996fd7ce5SGreg Kroah-Hartman 	if (cons_filp) {
75096fd7ce5SGreg Kroah-Hartman 		if (tty->ops->close)
75196fd7ce5SGreg Kroah-Hartman 			for (n = 0; n < closecount; n++)
75296fd7ce5SGreg Kroah-Hartman 				tty->ops->close(tty, cons_filp);
75396fd7ce5SGreg Kroah-Hartman 	} else if (tty->ops->hangup)
7547c6d340fSPeter Hurley 		tty->ops->hangup(tty);
75596fd7ce5SGreg Kroah-Hartman 	/*
75696fd7ce5SGreg Kroah-Hartman 	 * We don't want to have driver/ldisc interactions beyond
75796fd7ce5SGreg Kroah-Hartman 	 * the ones we did here. The driver layer expects no
75896fd7ce5SGreg Kroah-Hartman 	 * calls after ->hangup() from the ldisc side. However we
75996fd7ce5SGreg Kroah-Hartman 	 * can't yet guarantee all that.
76096fd7ce5SGreg Kroah-Hartman 	 */
76196fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_HUPPED, &tty->flags);
76289c8d91eSAlan Cox 	tty_unlock(tty);
76396fd7ce5SGreg Kroah-Hartman 
76496fd7ce5SGreg Kroah-Hartman 	if (f)
76596fd7ce5SGreg Kroah-Hartman 		fput(f);
76696fd7ce5SGreg Kroah-Hartman }
76796fd7ce5SGreg Kroah-Hartman 
76896fd7ce5SGreg Kroah-Hartman static void do_tty_hangup(struct work_struct *work)
76996fd7ce5SGreg Kroah-Hartman {
77096fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
77196fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, hangup_work);
77296fd7ce5SGreg Kroah-Hartman 
773f91e2590SPeter Hurley 	__tty_hangup(tty, 0);
77496fd7ce5SGreg Kroah-Hartman }
77596fd7ce5SGreg Kroah-Hartman 
77696fd7ce5SGreg Kroah-Hartman /**
77796fd7ce5SGreg Kroah-Hartman  *	tty_hangup		-	trigger a hangup event
77896fd7ce5SGreg Kroah-Hartman  *	@tty: tty to hangup
77996fd7ce5SGreg Kroah-Hartman  *
78096fd7ce5SGreg Kroah-Hartman  *	A carrier loss (virtual or otherwise) has occurred on this like
78196fd7ce5SGreg Kroah-Hartman  *	schedule a hangup sequence to run after this event.
78296fd7ce5SGreg Kroah-Hartman  */
78396fd7ce5SGreg Kroah-Hartman 
78496fd7ce5SGreg Kroah-Hartman void tty_hangup(struct tty_struct *tty)
78596fd7ce5SGreg Kroah-Hartman {
786d435cefeSPeter Hurley 	tty_debug_hangup(tty, "hangup\n");
78796fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->hangup_work);
78896fd7ce5SGreg Kroah-Hartman }
78996fd7ce5SGreg Kroah-Hartman 
79096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hangup);
79196fd7ce5SGreg Kroah-Hartman 
79296fd7ce5SGreg Kroah-Hartman /**
79396fd7ce5SGreg Kroah-Hartman  *	tty_vhangup		-	process vhangup
79496fd7ce5SGreg Kroah-Hartman  *	@tty: tty to hangup
79596fd7ce5SGreg Kroah-Hartman  *
79696fd7ce5SGreg Kroah-Hartman  *	The user has asked via system call for the terminal to be hung up.
79796fd7ce5SGreg Kroah-Hartman  *	We do this synchronously so that when the syscall returns the process
79896fd7ce5SGreg Kroah-Hartman  *	is complete. That guarantee is necessary for security reasons.
79996fd7ce5SGreg Kroah-Hartman  */
80096fd7ce5SGreg Kroah-Hartman 
80196fd7ce5SGreg Kroah-Hartman void tty_vhangup(struct tty_struct *tty)
80296fd7ce5SGreg Kroah-Hartman {
803d435cefeSPeter Hurley 	tty_debug_hangup(tty, "vhangup\n");
804f91e2590SPeter Hurley 	__tty_hangup(tty, 0);
80596fd7ce5SGreg Kroah-Hartman }
80696fd7ce5SGreg Kroah-Hartman 
80796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_vhangup);
80896fd7ce5SGreg Kroah-Hartman 
80996fd7ce5SGreg Kroah-Hartman 
81096fd7ce5SGreg Kroah-Hartman /**
81196fd7ce5SGreg Kroah-Hartman  *	tty_vhangup_self	-	process vhangup for own ctty
81296fd7ce5SGreg Kroah-Hartman  *
81396fd7ce5SGreg Kroah-Hartman  *	Perform a vhangup on the current controlling tty
81496fd7ce5SGreg Kroah-Hartman  */
81596fd7ce5SGreg Kroah-Hartman 
81696fd7ce5SGreg Kroah-Hartman void tty_vhangup_self(void)
81796fd7ce5SGreg Kroah-Hartman {
81896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
81996fd7ce5SGreg Kroah-Hartman 
82096fd7ce5SGreg Kroah-Hartman 	tty = get_current_tty();
82196fd7ce5SGreg Kroah-Hartman 	if (tty) {
82296fd7ce5SGreg Kroah-Hartman 		tty_vhangup(tty);
82396fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty);
82496fd7ce5SGreg Kroah-Hartman 	}
82596fd7ce5SGreg Kroah-Hartman }
82696fd7ce5SGreg Kroah-Hartman 
82796fd7ce5SGreg Kroah-Hartman /**
828f91e2590SPeter Hurley  *	tty_vhangup_session		-	hangup session leader exit
829f91e2590SPeter Hurley  *	@tty: tty to hangup
830f91e2590SPeter Hurley  *
831f91e2590SPeter Hurley  *	The session leader is exiting and hanging up its controlling terminal.
832f91e2590SPeter Hurley  *	Every process in the foreground process group is signalled SIGHUP.
833f91e2590SPeter Hurley  *
834f91e2590SPeter Hurley  *	We do this synchronously so that when the syscall returns the process
835f91e2590SPeter Hurley  *	is complete. That guarantee is necessary for security reasons.
836f91e2590SPeter Hurley  */
837f91e2590SPeter Hurley 
83844a459fdSChanglong Xie static void tty_vhangup_session(struct tty_struct *tty)
839f91e2590SPeter Hurley {
840d435cefeSPeter Hurley 	tty_debug_hangup(tty, "session hangup\n");
841f91e2590SPeter Hurley 	__tty_hangup(tty, 1);
842f91e2590SPeter Hurley }
843f91e2590SPeter Hurley 
844f91e2590SPeter Hurley /**
84596fd7ce5SGreg Kroah-Hartman  *	tty_hung_up_p		-	was tty hung up
84696fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer of tty
84796fd7ce5SGreg Kroah-Hartman  *
84896fd7ce5SGreg Kroah-Hartman  *	Return true if the tty has been subject to a vhangup or a carrier
84996fd7ce5SGreg Kroah-Hartman  *	loss
85096fd7ce5SGreg Kroah-Hartman  */
85196fd7ce5SGreg Kroah-Hartman 
85296fd7ce5SGreg Kroah-Hartman int tty_hung_up_p(struct file *filp)
85396fd7ce5SGreg Kroah-Hartman {
85496fd7ce5SGreg Kroah-Hartman 	return (filp->f_op == &hung_up_tty_fops);
85596fd7ce5SGreg Kroah-Hartman }
85696fd7ce5SGreg Kroah-Hartman 
85796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hung_up_p);
85896fd7ce5SGreg Kroah-Hartman 
85996fd7ce5SGreg Kroah-Hartman /**
86096fd7ce5SGreg Kroah-Hartman  *	disassociate_ctty	-	disconnect controlling tty
86196fd7ce5SGreg Kroah-Hartman  *	@on_exit: true if exiting so need to "hang up" the session
86296fd7ce5SGreg Kroah-Hartman  *
86396fd7ce5SGreg Kroah-Hartman  *	This function is typically called only by the session leader, when
86496fd7ce5SGreg Kroah-Hartman  *	it wants to disassociate itself from its controlling tty.
86596fd7ce5SGreg Kroah-Hartman  *
86696fd7ce5SGreg Kroah-Hartman  *	It performs the following functions:
86796fd7ce5SGreg Kroah-Hartman  * 	(1)  Sends a SIGHUP and SIGCONT to the foreground process group
86896fd7ce5SGreg Kroah-Hartman  * 	(2)  Clears the tty from being controlling the session
86996fd7ce5SGreg Kroah-Hartman  * 	(3)  Clears the controlling tty for all processes in the
87096fd7ce5SGreg Kroah-Hartman  * 		session group.
87196fd7ce5SGreg Kroah-Hartman  *
87296fd7ce5SGreg Kroah-Hartman  *	The argument on_exit is set to 1 if called when a process is
87396fd7ce5SGreg Kroah-Hartman  *	exiting; it is 0 if called by the ioctl TIOCNOTTY.
87496fd7ce5SGreg Kroah-Hartman  *
87596fd7ce5SGreg Kroah-Hartman  *	Locking:
87696fd7ce5SGreg Kroah-Hartman  *		BTM is taken for hysterical raisins, and held when
87796fd7ce5SGreg Kroah-Hartman  *		  called from no_tty().
87896fd7ce5SGreg Kroah-Hartman  *		  tty_mutex is taken to protect tty
87996fd7ce5SGreg Kroah-Hartman  *		  ->siglock is taken to protect ->signal/->sighand
88096fd7ce5SGreg Kroah-Hartman  *		  tasklist_lock is taken to walk process list for sessions
88196fd7ce5SGreg Kroah-Hartman  *		    ->siglock is taken to protect ->signal/->sighand
88296fd7ce5SGreg Kroah-Hartman  */
88396fd7ce5SGreg Kroah-Hartman 
88496fd7ce5SGreg Kroah-Hartman void disassociate_ctty(int on_exit)
88596fd7ce5SGreg Kroah-Hartman {
88696fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
88796fd7ce5SGreg Kroah-Hartman 
88896fd7ce5SGreg Kroah-Hartman 	if (!current->signal->leader)
88996fd7ce5SGreg Kroah-Hartman 		return;
89096fd7ce5SGreg Kroah-Hartman 
89196fd7ce5SGreg Kroah-Hartman 	tty = get_current_tty();
89296fd7ce5SGreg Kroah-Hartman 	if (tty) {
893f91e2590SPeter Hurley 		if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
894f91e2590SPeter Hurley 			tty_vhangup_session(tty);
895f91e2590SPeter Hurley 		} else {
896f91e2590SPeter Hurley 			struct pid *tty_pgrp = tty_get_pgrp(tty);
8971411dc4aSJiri Slaby 			if (tty_pgrp) {
8981411dc4aSJiri Slaby 				kill_pgrp(tty_pgrp, SIGHUP, on_exit);
89903e12617SOleg Nesterov 				if (!on_exit)
9001411dc4aSJiri Slaby 					kill_pgrp(tty_pgrp, SIGCONT, on_exit);
9011411dc4aSJiri Slaby 				put_pid(tty_pgrp);
9021411dc4aSJiri Slaby 			}
903f91e2590SPeter Hurley 		}
904f91e2590SPeter Hurley 		tty_kref_put(tty);
905f91e2590SPeter Hurley 
90696fd7ce5SGreg Kroah-Hartman 	} else if (on_exit) {
90796fd7ce5SGreg Kroah-Hartman 		struct pid *old_pgrp;
90896fd7ce5SGreg Kroah-Hartman 		spin_lock_irq(&current->sighand->siglock);
90996fd7ce5SGreg Kroah-Hartman 		old_pgrp = current->signal->tty_old_pgrp;
91096fd7ce5SGreg Kroah-Hartman 		current->signal->tty_old_pgrp = NULL;
91196fd7ce5SGreg Kroah-Hartman 		spin_unlock_irq(&current->sighand->siglock);
91296fd7ce5SGreg Kroah-Hartman 		if (old_pgrp) {
91396fd7ce5SGreg Kroah-Hartman 			kill_pgrp(old_pgrp, SIGHUP, on_exit);
91496fd7ce5SGreg Kroah-Hartman 			kill_pgrp(old_pgrp, SIGCONT, on_exit);
91596fd7ce5SGreg Kroah-Hartman 			put_pid(old_pgrp);
91696fd7ce5SGreg Kroah-Hartman 		}
91796fd7ce5SGreg Kroah-Hartman 		return;
91896fd7ce5SGreg Kroah-Hartman 	}
91996fd7ce5SGreg Kroah-Hartman 
92096fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&current->sighand->siglock);
92196fd7ce5SGreg Kroah-Hartman 	put_pid(current->signal->tty_old_pgrp);
92296fd7ce5SGreg Kroah-Hartman 	current->signal->tty_old_pgrp = NULL;
92396fd7ce5SGreg Kroah-Hartman 
924c70dbb1eSChen Tingjie 	tty = tty_kref_get(current->signal->tty);
92596fd7ce5SGreg Kroah-Hartman 	if (tty) {
92696fd7ce5SGreg Kroah-Hartman 		unsigned long flags;
92796fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&tty->ctrl_lock, flags);
92896fd7ce5SGreg Kroah-Hartman 		put_pid(tty->session);
92996fd7ce5SGreg Kroah-Hartman 		put_pid(tty->pgrp);
93096fd7ce5SGreg Kroah-Hartman 		tty->session = NULL;
93196fd7ce5SGreg Kroah-Hartman 		tty->pgrp = NULL;
93296fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
93396fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty);
934accff793SPeter Hurley 	} else
935accff793SPeter Hurley 		tty_debug_hangup(tty, "no current tty\n");
93696fd7ce5SGreg Kroah-Hartman 
937c70dbb1eSChen Tingjie 	spin_unlock_irq(&current->sighand->siglock);
93896fd7ce5SGreg Kroah-Hartman 	/* Now clear signal->tty under the lock */
93996fd7ce5SGreg Kroah-Hartman 	read_lock(&tasklist_lock);
94096fd7ce5SGreg Kroah-Hartman 	session_clear_tty(task_session(current));
94196fd7ce5SGreg Kroah-Hartman 	read_unlock(&tasklist_lock);
94296fd7ce5SGreg Kroah-Hartman }
94396fd7ce5SGreg Kroah-Hartman 
94496fd7ce5SGreg Kroah-Hartman /**
94596fd7ce5SGreg Kroah-Hartman  *
94696fd7ce5SGreg Kroah-Hartman  *	no_tty	- Ensure the current process does not have a controlling tty
94796fd7ce5SGreg Kroah-Hartman  */
94896fd7ce5SGreg Kroah-Hartman void no_tty(void)
94996fd7ce5SGreg Kroah-Hartman {
9503af502b9SAlan Cox 	/* FIXME: Review locking here. The tty_lock never covered any race
9513af502b9SAlan Cox 	   between a new association and proc_clear_tty but possible we need
9523af502b9SAlan Cox 	   to protect against this anyway */
95396fd7ce5SGreg Kroah-Hartman 	struct task_struct *tsk = current;
95496fd7ce5SGreg Kroah-Hartman 	disassociate_ctty(0);
95596fd7ce5SGreg Kroah-Hartman 	proc_clear_tty(tsk);
95696fd7ce5SGreg Kroah-Hartman }
95796fd7ce5SGreg Kroah-Hartman 
95896fd7ce5SGreg Kroah-Hartman 
95996fd7ce5SGreg Kroah-Hartman /**
96096fd7ce5SGreg Kroah-Hartman  *	stop_tty	-	propagate flow control
96196fd7ce5SGreg Kroah-Hartman  *	@tty: tty to stop
96296fd7ce5SGreg Kroah-Hartman  *
96301adc807SPeter Hurley  *	Perform flow control to the driver. May be called
96496fd7ce5SGreg Kroah-Hartman  *	on an already stopped device and will not re-call the driver
96596fd7ce5SGreg Kroah-Hartman  *	method.
96696fd7ce5SGreg Kroah-Hartman  *
96796fd7ce5SGreg Kroah-Hartman  *	This functionality is used by both the line disciplines for
96896fd7ce5SGreg Kroah-Hartman  *	halting incoming flow and by the driver. It may therefore be
96996fd7ce5SGreg Kroah-Hartman  *	called from any context, may be under the tty atomic_write_lock
97096fd7ce5SGreg Kroah-Hartman  *	but not always.
97196fd7ce5SGreg Kroah-Hartman  *
97296fd7ce5SGreg Kroah-Hartman  *	Locking:
973f9e053dcSPeter Hurley  *		flow_lock
97496fd7ce5SGreg Kroah-Hartman  */
97596fd7ce5SGreg Kroah-Hartman 
976f9e053dcSPeter Hurley void __stop_tty(struct tty_struct *tty)
97796fd7ce5SGreg Kroah-Hartman {
978f9e053dcSPeter Hurley 	if (tty->stopped)
97996fd7ce5SGreg Kroah-Hartman 		return;
98096fd7ce5SGreg Kroah-Hartman 	tty->stopped = 1;
98196fd7ce5SGreg Kroah-Hartman 	if (tty->ops->stop)
982c961bfb1SPeter Hurley 		tty->ops->stop(tty);
98396fd7ce5SGreg Kroah-Hartman }
98496fd7ce5SGreg Kroah-Hartman 
985f9e053dcSPeter Hurley void stop_tty(struct tty_struct *tty)
986f9e053dcSPeter Hurley {
987f9e053dcSPeter Hurley 	unsigned long flags;
988f9e053dcSPeter Hurley 
989f9e053dcSPeter Hurley 	spin_lock_irqsave(&tty->flow_lock, flags);
990f9e053dcSPeter Hurley 	__stop_tty(tty);
991f9e053dcSPeter Hurley 	spin_unlock_irqrestore(&tty->flow_lock, flags);
992f9e053dcSPeter Hurley }
99396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(stop_tty);
99496fd7ce5SGreg Kroah-Hartman 
99596fd7ce5SGreg Kroah-Hartman /**
99696fd7ce5SGreg Kroah-Hartman  *	start_tty	-	propagate flow control
99796fd7ce5SGreg Kroah-Hartman  *	@tty: tty to start
99896fd7ce5SGreg Kroah-Hartman  *
99901adc807SPeter Hurley  *	Start a tty that has been stopped if at all possible. If this
100001adc807SPeter Hurley  *	tty was previous stopped and is now being started, the driver
100101adc807SPeter Hurley  *	start method is invoked and the line discipline woken.
100296fd7ce5SGreg Kroah-Hartman  *
100396fd7ce5SGreg Kroah-Hartman  *	Locking:
1004f9e053dcSPeter Hurley  *		flow_lock
100596fd7ce5SGreg Kroah-Hartman  */
100696fd7ce5SGreg Kroah-Hartman 
1007f9e053dcSPeter Hurley void __start_tty(struct tty_struct *tty)
100896fd7ce5SGreg Kroah-Hartman {
1009f9e053dcSPeter Hurley 	if (!tty->stopped || tty->flow_stopped)
101096fd7ce5SGreg Kroah-Hartman 		return;
101196fd7ce5SGreg Kroah-Hartman 	tty->stopped = 0;
101296fd7ce5SGreg Kroah-Hartman 	if (tty->ops->start)
1013c961bfb1SPeter Hurley 		tty->ops->start(tty);
101496fd7ce5SGreg Kroah-Hartman 	tty_wakeup(tty);
101596fd7ce5SGreg Kroah-Hartman }
101696fd7ce5SGreg Kroah-Hartman 
1017f9e053dcSPeter Hurley void start_tty(struct tty_struct *tty)
1018f9e053dcSPeter Hurley {
1019f9e053dcSPeter Hurley 	unsigned long flags;
1020f9e053dcSPeter Hurley 
1021f9e053dcSPeter Hurley 	spin_lock_irqsave(&tty->flow_lock, flags);
1022f9e053dcSPeter Hurley 	__start_tty(tty);
1023f9e053dcSPeter Hurley 	spin_unlock_irqrestore(&tty->flow_lock, flags);
1024f9e053dcSPeter Hurley }
102596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(start_tty);
102696fd7ce5SGreg Kroah-Hartman 
102737b7f3c7SJiri Slaby static void tty_update_time(struct timespec *time)
102837b7f3c7SJiri Slaby {
1029f0bf0bd0SJiri Slaby 	unsigned long sec = get_seconds();
1030fbf47635SGreg Kroah-Hartman 
1031fbf47635SGreg Kroah-Hartman 	/*
1032fbf47635SGreg Kroah-Hartman 	 * We only care if the two values differ in anything other than the
1033fbf47635SGreg Kroah-Hartman 	 * lower three bits (i.e every 8 seconds).  If so, then we can update
1034fbf47635SGreg Kroah-Hartman 	 * the time of the tty device, otherwise it could be construded as a
1035fbf47635SGreg Kroah-Hartman 	 * security leak to let userspace know the exact timing of the tty.
1036fbf47635SGreg Kroah-Hartman 	 */
1037fbf47635SGreg Kroah-Hartman 	if ((sec ^ time->tv_sec) & ~7)
103837b7f3c7SJiri Slaby 		time->tv_sec = sec;
103937b7f3c7SJiri Slaby }
104037b7f3c7SJiri Slaby 
104196fd7ce5SGreg Kroah-Hartman /**
104296fd7ce5SGreg Kroah-Hartman  *	tty_read	-	read method for tty device files
104396fd7ce5SGreg Kroah-Hartman  *	@file: pointer to tty file
104496fd7ce5SGreg Kroah-Hartman  *	@buf: user buffer
104596fd7ce5SGreg Kroah-Hartman  *	@count: size of user buffer
104696fd7ce5SGreg Kroah-Hartman  *	@ppos: unused
104796fd7ce5SGreg Kroah-Hartman  *
104896fd7ce5SGreg Kroah-Hartman  *	Perform the read system call function on this terminal device. Checks
104996fd7ce5SGreg Kroah-Hartman  *	for hung up devices before calling the line discipline method.
105096fd7ce5SGreg Kroah-Hartman  *
105196fd7ce5SGreg Kroah-Hartman  *	Locking:
105296fd7ce5SGreg Kroah-Hartman  *		Locks the line discipline internally while needed. Multiple
105396fd7ce5SGreg Kroah-Hartman  *	read calls may be outstanding in parallel.
105496fd7ce5SGreg Kroah-Hartman  */
105596fd7ce5SGreg Kroah-Hartman 
105696fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
105796fd7ce5SGreg Kroah-Hartman 			loff_t *ppos)
105896fd7ce5SGreg Kroah-Hartman {
105996fd7ce5SGreg Kroah-Hartman 	int i;
106037b7f3c7SJiri Slaby 	struct inode *inode = file_inode(file);
106196fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
106296fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
106396fd7ce5SGreg Kroah-Hartman 
106437b7f3c7SJiri Slaby 	if (tty_paranoia_check(tty, inode, "tty_read"))
106596fd7ce5SGreg Kroah-Hartman 		return -EIO;
106696fd7ce5SGreg Kroah-Hartman 	if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
106796fd7ce5SGreg Kroah-Hartman 		return -EIO;
106896fd7ce5SGreg Kroah-Hartman 
106996fd7ce5SGreg Kroah-Hartman 	/* We want to wait for the line discipline to sort out in this
107096fd7ce5SGreg Kroah-Hartman 	   situation */
107196fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
107296fd7ce5SGreg Kroah-Hartman 	if (ld->ops->read)
1073c961bfb1SPeter Hurley 		i = ld->ops->read(tty, file, buf, count);
107496fd7ce5SGreg Kroah-Hartman 	else
107596fd7ce5SGreg Kroah-Hartman 		i = -EIO;
107696fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
1077b0de59b5SJiri Slaby 
107837b7f3c7SJiri Slaby 	if (i > 0)
107937b7f3c7SJiri Slaby 		tty_update_time(&inode->i_atime);
108037b7f3c7SJiri Slaby 
108196fd7ce5SGreg Kroah-Hartman 	return i;
108296fd7ce5SGreg Kroah-Hartman }
108396fd7ce5SGreg Kroah-Hartman 
1084136d5258SPeter Hurley static void tty_write_unlock(struct tty_struct *tty)
108596fd7ce5SGreg Kroah-Hartman {
108696fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->atomic_write_lock);
108796fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
108896fd7ce5SGreg Kroah-Hartman }
108996fd7ce5SGreg Kroah-Hartman 
1090136d5258SPeter Hurley static int tty_write_lock(struct tty_struct *tty, int ndelay)
109196fd7ce5SGreg Kroah-Hartman {
109296fd7ce5SGreg Kroah-Hartman 	if (!mutex_trylock(&tty->atomic_write_lock)) {
109396fd7ce5SGreg Kroah-Hartman 		if (ndelay)
109496fd7ce5SGreg Kroah-Hartman 			return -EAGAIN;
109596fd7ce5SGreg Kroah-Hartman 		if (mutex_lock_interruptible(&tty->atomic_write_lock))
109696fd7ce5SGreg Kroah-Hartman 			return -ERESTARTSYS;
109796fd7ce5SGreg Kroah-Hartman 	}
109896fd7ce5SGreg Kroah-Hartman 	return 0;
109996fd7ce5SGreg Kroah-Hartman }
110096fd7ce5SGreg Kroah-Hartman 
110196fd7ce5SGreg Kroah-Hartman /*
110296fd7ce5SGreg Kroah-Hartman  * Split writes up in sane blocksizes to avoid
110396fd7ce5SGreg Kroah-Hartman  * denial-of-service type attacks
110496fd7ce5SGreg Kroah-Hartman  */
110596fd7ce5SGreg Kroah-Hartman static inline ssize_t do_tty_write(
110696fd7ce5SGreg Kroah-Hartman 	ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
110796fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty,
110896fd7ce5SGreg Kroah-Hartman 	struct file *file,
110996fd7ce5SGreg Kroah-Hartman 	const char __user *buf,
111096fd7ce5SGreg Kroah-Hartman 	size_t count)
111196fd7ce5SGreg Kroah-Hartman {
111296fd7ce5SGreg Kroah-Hartman 	ssize_t ret, written = 0;
111396fd7ce5SGreg Kroah-Hartman 	unsigned int chunk;
111496fd7ce5SGreg Kroah-Hartman 
111596fd7ce5SGreg Kroah-Hartman 	ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
111696fd7ce5SGreg Kroah-Hartman 	if (ret < 0)
111796fd7ce5SGreg Kroah-Hartman 		return ret;
111896fd7ce5SGreg Kroah-Hartman 
111996fd7ce5SGreg Kroah-Hartman 	/*
112096fd7ce5SGreg Kroah-Hartman 	 * We chunk up writes into a temporary buffer. This
112196fd7ce5SGreg Kroah-Hartman 	 * simplifies low-level drivers immensely, since they
112296fd7ce5SGreg Kroah-Hartman 	 * don't have locking issues and user mode accesses.
112396fd7ce5SGreg Kroah-Hartman 	 *
112496fd7ce5SGreg Kroah-Hartman 	 * But if TTY_NO_WRITE_SPLIT is set, we should use a
112596fd7ce5SGreg Kroah-Hartman 	 * big chunk-size..
112696fd7ce5SGreg Kroah-Hartman 	 *
112796fd7ce5SGreg Kroah-Hartman 	 * The default chunk-size is 2kB, because the NTTY
112896fd7ce5SGreg Kroah-Hartman 	 * layer has problems with bigger chunks. It will
112996fd7ce5SGreg Kroah-Hartman 	 * claim to be able to handle more characters than
113096fd7ce5SGreg Kroah-Hartman 	 * it actually does.
113196fd7ce5SGreg Kroah-Hartman 	 *
113296fd7ce5SGreg Kroah-Hartman 	 * FIXME: This can probably go away now except that 64K chunks
113396fd7ce5SGreg Kroah-Hartman 	 * are too likely to fail unless switched to vmalloc...
113496fd7ce5SGreg Kroah-Hartman 	 */
113596fd7ce5SGreg Kroah-Hartman 	chunk = 2048;
113696fd7ce5SGreg Kroah-Hartman 	if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
113796fd7ce5SGreg Kroah-Hartman 		chunk = 65536;
113896fd7ce5SGreg Kroah-Hartman 	if (count < chunk)
113996fd7ce5SGreg Kroah-Hartman 		chunk = count;
114096fd7ce5SGreg Kroah-Hartman 
114196fd7ce5SGreg Kroah-Hartman 	/* write_buf/write_cnt is protected by the atomic_write_lock mutex */
114296fd7ce5SGreg Kroah-Hartman 	if (tty->write_cnt < chunk) {
114396fd7ce5SGreg Kroah-Hartman 		unsigned char *buf_chunk;
114496fd7ce5SGreg Kroah-Hartman 
114596fd7ce5SGreg Kroah-Hartman 		if (chunk < 1024)
114696fd7ce5SGreg Kroah-Hartman 			chunk = 1024;
114796fd7ce5SGreg Kroah-Hartman 
114896fd7ce5SGreg Kroah-Hartman 		buf_chunk = kmalloc(chunk, GFP_KERNEL);
114996fd7ce5SGreg Kroah-Hartman 		if (!buf_chunk) {
115096fd7ce5SGreg Kroah-Hartman 			ret = -ENOMEM;
115196fd7ce5SGreg Kroah-Hartman 			goto out;
115296fd7ce5SGreg Kroah-Hartman 		}
115396fd7ce5SGreg Kroah-Hartman 		kfree(tty->write_buf);
115496fd7ce5SGreg Kroah-Hartman 		tty->write_cnt = chunk;
115596fd7ce5SGreg Kroah-Hartman 		tty->write_buf = buf_chunk;
115696fd7ce5SGreg Kroah-Hartman 	}
115796fd7ce5SGreg Kroah-Hartman 
115896fd7ce5SGreg Kroah-Hartman 	/* Do the write .. */
115996fd7ce5SGreg Kroah-Hartman 	for (;;) {
116096fd7ce5SGreg Kroah-Hartman 		size_t size = count;
116196fd7ce5SGreg Kroah-Hartman 		if (size > chunk)
116296fd7ce5SGreg Kroah-Hartman 			size = chunk;
116396fd7ce5SGreg Kroah-Hartman 		ret = -EFAULT;
116496fd7ce5SGreg Kroah-Hartman 		if (copy_from_user(tty->write_buf, buf, size))
116596fd7ce5SGreg Kroah-Hartman 			break;
116696fd7ce5SGreg Kroah-Hartman 		ret = write(tty, file, tty->write_buf, size);
116796fd7ce5SGreg Kroah-Hartman 		if (ret <= 0)
116896fd7ce5SGreg Kroah-Hartman 			break;
116996fd7ce5SGreg Kroah-Hartman 		written += ret;
117096fd7ce5SGreg Kroah-Hartman 		buf += ret;
117196fd7ce5SGreg Kroah-Hartman 		count -= ret;
117296fd7ce5SGreg Kroah-Hartman 		if (!count)
117396fd7ce5SGreg Kroah-Hartman 			break;
117496fd7ce5SGreg Kroah-Hartman 		ret = -ERESTARTSYS;
117596fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
117696fd7ce5SGreg Kroah-Hartman 			break;
117796fd7ce5SGreg Kroah-Hartman 		cond_resched();
117896fd7ce5SGreg Kroah-Hartman 	}
117937b7f3c7SJiri Slaby 	if (written) {
118037b7f3c7SJiri Slaby 		tty_update_time(&file_inode(file)->i_mtime);
118196fd7ce5SGreg Kroah-Hartman 		ret = written;
118237b7f3c7SJiri Slaby 	}
118396fd7ce5SGreg Kroah-Hartman out:
118496fd7ce5SGreg Kroah-Hartman 	tty_write_unlock(tty);
118596fd7ce5SGreg Kroah-Hartman 	return ret;
118696fd7ce5SGreg Kroah-Hartman }
118796fd7ce5SGreg Kroah-Hartman 
118896fd7ce5SGreg Kroah-Hartman /**
118996fd7ce5SGreg Kroah-Hartman  * tty_write_message - write a message to a certain tty, not just the console.
119096fd7ce5SGreg Kroah-Hartman  * @tty: the destination tty_struct
119196fd7ce5SGreg Kroah-Hartman  * @msg: the message to write
119296fd7ce5SGreg Kroah-Hartman  *
119396fd7ce5SGreg Kroah-Hartman  * This is used for messages that need to be redirected to a specific tty.
119496fd7ce5SGreg Kroah-Hartman  * We don't put it into the syslog queue right now maybe in the future if
119596fd7ce5SGreg Kroah-Hartman  * really needed.
119696fd7ce5SGreg Kroah-Hartman  *
119796fd7ce5SGreg Kroah-Hartman  * We must still hold the BTM and test the CLOSING flag for the moment.
119896fd7ce5SGreg Kroah-Hartman  */
119996fd7ce5SGreg Kroah-Hartman 
120096fd7ce5SGreg Kroah-Hartman void tty_write_message(struct tty_struct *tty, char *msg)
120196fd7ce5SGreg Kroah-Hartman {
120296fd7ce5SGreg Kroah-Hartman 	if (tty) {
120396fd7ce5SGreg Kroah-Hartman 		mutex_lock(&tty->atomic_write_lock);
120489c8d91eSAlan Cox 		tty_lock(tty);
12054b41b953SPeter Hurley 		if (tty->ops->write && tty->count > 0)
120696fd7ce5SGreg Kroah-Hartman 			tty->ops->write(tty, msg, strlen(msg));
120789c8d91eSAlan Cox 		tty_unlock(tty);
120896fd7ce5SGreg Kroah-Hartman 		tty_write_unlock(tty);
120996fd7ce5SGreg Kroah-Hartman 	}
121096fd7ce5SGreg Kroah-Hartman 	return;
121196fd7ce5SGreg Kroah-Hartman }
121296fd7ce5SGreg Kroah-Hartman 
121396fd7ce5SGreg Kroah-Hartman 
121496fd7ce5SGreg Kroah-Hartman /**
121596fd7ce5SGreg Kroah-Hartman  *	tty_write		-	write method for tty device file
121696fd7ce5SGreg Kroah-Hartman  *	@file: tty file pointer
121796fd7ce5SGreg Kroah-Hartman  *	@buf: user data to write
121896fd7ce5SGreg Kroah-Hartman  *	@count: bytes to write
121996fd7ce5SGreg Kroah-Hartman  *	@ppos: unused
122096fd7ce5SGreg Kroah-Hartman  *
122196fd7ce5SGreg Kroah-Hartman  *	Write data to a tty device via the line discipline.
122296fd7ce5SGreg Kroah-Hartman  *
122396fd7ce5SGreg Kroah-Hartman  *	Locking:
122496fd7ce5SGreg Kroah-Hartman  *		Locks the line discipline as required
122596fd7ce5SGreg Kroah-Hartman  *		Writes to the tty driver are serialized by the atomic_write_lock
122696fd7ce5SGreg Kroah-Hartman  *	and are then processed in chunks to the device. The line discipline
122796fd7ce5SGreg Kroah-Hartman  *	write method will not be invoked in parallel for each device.
122896fd7ce5SGreg Kroah-Hartman  */
122996fd7ce5SGreg Kroah-Hartman 
123096fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *file, const char __user *buf,
123196fd7ce5SGreg Kroah-Hartman 						size_t count, loff_t *ppos)
123296fd7ce5SGreg Kroah-Hartman {
123396fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
123496fd7ce5SGreg Kroah-Hartman  	struct tty_ldisc *ld;
123596fd7ce5SGreg Kroah-Hartman 	ssize_t ret;
123696fd7ce5SGreg Kroah-Hartman 
12376131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
123896fd7ce5SGreg Kroah-Hartman 		return -EIO;
123996fd7ce5SGreg Kroah-Hartman 	if (!tty || !tty->ops->write ||
124096fd7ce5SGreg Kroah-Hartman 		(test_bit(TTY_IO_ERROR, &tty->flags)))
124196fd7ce5SGreg Kroah-Hartman 			return -EIO;
124296fd7ce5SGreg Kroah-Hartman 	/* Short term debug to catch buggy drivers */
124396fd7ce5SGreg Kroah-Hartman 	if (tty->ops->write_room == NULL)
1244339f36baSPeter Hurley 		tty_err(tty, "missing write_room method\n");
124596fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
124696fd7ce5SGreg Kroah-Hartman 	if (!ld->ops->write)
124796fd7ce5SGreg Kroah-Hartman 		ret = -EIO;
124896fd7ce5SGreg Kroah-Hartman 	else
124996fd7ce5SGreg Kroah-Hartman 		ret = do_tty_write(ld->ops->write, tty, file, buf, count);
125096fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
125196fd7ce5SGreg Kroah-Hartman 	return ret;
125296fd7ce5SGreg Kroah-Hartman }
125396fd7ce5SGreg Kroah-Hartman 
125496fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *file, const char __user *buf,
125596fd7ce5SGreg Kroah-Hartman 						size_t count, loff_t *ppos)
125696fd7ce5SGreg Kroah-Hartman {
125796fd7ce5SGreg Kroah-Hartman 	struct file *p = NULL;
125896fd7ce5SGreg Kroah-Hartman 
125996fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
1260cb0942b8SAl Viro 	if (redirect)
1261cb0942b8SAl Viro 		p = get_file(redirect);
126296fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
126396fd7ce5SGreg Kroah-Hartman 
126496fd7ce5SGreg Kroah-Hartman 	if (p) {
126596fd7ce5SGreg Kroah-Hartman 		ssize_t res;
126696fd7ce5SGreg Kroah-Hartman 		res = vfs_write(p, buf, count, &p->f_pos);
126796fd7ce5SGreg Kroah-Hartman 		fput(p);
126896fd7ce5SGreg Kroah-Hartman 		return res;
126996fd7ce5SGreg Kroah-Hartman 	}
127096fd7ce5SGreg Kroah-Hartman 	return tty_write(file, buf, count, ppos);
127196fd7ce5SGreg Kroah-Hartman }
127296fd7ce5SGreg Kroah-Hartman 
1273136d5258SPeter Hurley /**
1274136d5258SPeter Hurley  *	tty_send_xchar	-	send priority character
1275136d5258SPeter Hurley  *
1276136d5258SPeter Hurley  *	Send a high priority character to the tty even if stopped
1277136d5258SPeter Hurley  *
1278136d5258SPeter Hurley  *	Locking: none for xchar method, write ordering for write method.
1279136d5258SPeter Hurley  */
1280136d5258SPeter Hurley 
1281136d5258SPeter Hurley int tty_send_xchar(struct tty_struct *tty, char ch)
1282136d5258SPeter Hurley {
1283136d5258SPeter Hurley 	int	was_stopped = tty->stopped;
1284136d5258SPeter Hurley 
1285136d5258SPeter Hurley 	if (tty->ops->send_xchar) {
1286ee0c1a65SPeter Hurley 		down_read(&tty->termios_rwsem);
1287136d5258SPeter Hurley 		tty->ops->send_xchar(tty, ch);
1288ee0c1a65SPeter Hurley 		up_read(&tty->termios_rwsem);
1289136d5258SPeter Hurley 		return 0;
1290136d5258SPeter Hurley 	}
1291136d5258SPeter Hurley 
1292136d5258SPeter Hurley 	if (tty_write_lock(tty, 0) < 0)
1293136d5258SPeter Hurley 		return -ERESTARTSYS;
1294136d5258SPeter Hurley 
1295ee0c1a65SPeter Hurley 	down_read(&tty->termios_rwsem);
1296136d5258SPeter Hurley 	if (was_stopped)
1297136d5258SPeter Hurley 		start_tty(tty);
1298136d5258SPeter Hurley 	tty->ops->write(tty, &ch, 1);
1299136d5258SPeter Hurley 	if (was_stopped)
1300136d5258SPeter Hurley 		stop_tty(tty);
1301ee0c1a65SPeter Hurley 	up_read(&tty->termios_rwsem);
1302136d5258SPeter Hurley 	tty_write_unlock(tty);
1303136d5258SPeter Hurley 	return 0;
1304136d5258SPeter Hurley }
1305136d5258SPeter Hurley 
130696fd7ce5SGreg Kroah-Hartman static char ptychar[] = "pqrstuvwxyzabcde";
130796fd7ce5SGreg Kroah-Hartman 
130896fd7ce5SGreg Kroah-Hartman /**
130996fd7ce5SGreg Kroah-Hartman  *	pty_line_name	-	generate name for a pty
131096fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver in use
131196fd7ce5SGreg Kroah-Hartman  *	@index: the minor number
131296fd7ce5SGreg Kroah-Hartman  *	@p: output buffer of at least 6 bytes
131396fd7ce5SGreg Kroah-Hartman  *
131496fd7ce5SGreg Kroah-Hartman  *	Generate a name from a driver reference and write it to the output
131596fd7ce5SGreg Kroah-Hartman  *	buffer.
131696fd7ce5SGreg Kroah-Hartman  *
131796fd7ce5SGreg Kroah-Hartman  *	Locking: None
131896fd7ce5SGreg Kroah-Hartman  */
131996fd7ce5SGreg Kroah-Hartman static void pty_line_name(struct tty_driver *driver, int index, char *p)
132096fd7ce5SGreg Kroah-Hartman {
132196fd7ce5SGreg Kroah-Hartman 	int i = index + driver->name_base;
132296fd7ce5SGreg Kroah-Hartman 	/* ->name is initialized to "ttyp", but "tty" is expected */
132396fd7ce5SGreg Kroah-Hartman 	sprintf(p, "%s%c%x",
132496fd7ce5SGreg Kroah-Hartman 		driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
132596fd7ce5SGreg Kroah-Hartman 		ptychar[i >> 4 & 0xf], i & 0xf);
132696fd7ce5SGreg Kroah-Hartman }
132796fd7ce5SGreg Kroah-Hartman 
132896fd7ce5SGreg Kroah-Hartman /**
132996fd7ce5SGreg Kroah-Hartman  *	tty_line_name	-	generate name for a tty
133096fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver in use
133196fd7ce5SGreg Kroah-Hartman  *	@index: the minor number
133296fd7ce5SGreg Kroah-Hartman  *	@p: output buffer of at least 7 bytes
133396fd7ce5SGreg Kroah-Hartman  *
133496fd7ce5SGreg Kroah-Hartman  *	Generate a name from a driver reference and write it to the output
13355c0a2450SGreg Kroah-Hartman  *	buffer.
133696fd7ce5SGreg Kroah-Hartman  *
133796fd7ce5SGreg Kroah-Hartman  *	Locking: None
133896fd7ce5SGreg Kroah-Hartman  */
1339723abd87SHannes Reinecke static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
134096fd7ce5SGreg Kroah-Hartman {
13410019b408SJiri Slaby 	if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
1342723abd87SHannes Reinecke 		return sprintf(p, "%s", driver->name);
13430019b408SJiri Slaby 	else
1344723abd87SHannes Reinecke 		return sprintf(p, "%s%d", driver->name,
1345723abd87SHannes Reinecke 			       index + driver->name_base);
134696fd7ce5SGreg Kroah-Hartman }
134796fd7ce5SGreg Kroah-Hartman 
134896fd7ce5SGreg Kroah-Hartman /**
134996fd7ce5SGreg Kroah-Hartman  *	tty_driver_lookup_tty() - find an existing tty, if any
135096fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
135196fd7ce5SGreg Kroah-Hartman  *	@idx:	 the minor number
135296fd7ce5SGreg Kroah-Hartman  *
1353aa3cb814SPeter Hurley  *	Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1354aa3cb814SPeter Hurley  *	driver lookup() method returns an error.
135596fd7ce5SGreg Kroah-Hartman  *
1356aa3cb814SPeter Hurley  *	Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
135796fd7ce5SGreg Kroah-Hartman  */
135896fd7ce5SGreg Kroah-Hartman static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
135996fd7ce5SGreg Kroah-Hartman 		struct inode *inode, int idx)
136096fd7ce5SGreg Kroah-Hartman {
1361aa3cb814SPeter Hurley 	struct tty_struct *tty;
136296fd7ce5SGreg Kroah-Hartman 
1363aa3cb814SPeter Hurley 	if (driver->ops->lookup)
1364aa3cb814SPeter Hurley 		tty = driver->ops->lookup(driver, inode, idx);
1365aa3cb814SPeter Hurley 	else
1366aa3cb814SPeter Hurley 		tty = driver->ttys[idx];
1367aa3cb814SPeter Hurley 
1368aa3cb814SPeter Hurley 	if (!IS_ERR(tty))
1369aa3cb814SPeter Hurley 		tty_kref_get(tty);
1370aa3cb814SPeter Hurley 	return tty;
137196fd7ce5SGreg Kroah-Hartman }
137296fd7ce5SGreg Kroah-Hartman 
137396fd7ce5SGreg Kroah-Hartman /**
137496fd7ce5SGreg Kroah-Hartman  *	tty_init_termios	-  helper for termios setup
137596fd7ce5SGreg Kroah-Hartman  *	@tty: the tty to set up
137696fd7ce5SGreg Kroah-Hartman  *
137796fd7ce5SGreg Kroah-Hartman  *	Initialise the termios structures for this tty. Thus runs under
137896fd7ce5SGreg Kroah-Hartman  *	the tty_mutex currently so we can be relaxed about ordering.
137996fd7ce5SGreg Kroah-Hartman  */
138096fd7ce5SGreg Kroah-Hartman 
138196fd7ce5SGreg Kroah-Hartman int tty_init_termios(struct tty_struct *tty)
138296fd7ce5SGreg Kroah-Hartman {
138396fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
138496fd7ce5SGreg Kroah-Hartman 	int idx = tty->index;
138596fd7ce5SGreg Kroah-Hartman 
138636b3c070SAlan Cox 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
138736b3c070SAlan Cox 		tty->termios = tty->driver->init_termios;
138836b3c070SAlan Cox 	else {
138936b3c070SAlan Cox 		/* Check for lazy saved data */
139096fd7ce5SGreg Kroah-Hartman 		tp = tty->driver->termios[idx];
139136b3c070SAlan Cox 		if (tp != NULL)
1392adc8d746SAlan Cox 			tty->termios = *tp;
139336b3c070SAlan Cox 		else
139436b3c070SAlan Cox 			tty->termios = tty->driver->init_termios;
139536b3c070SAlan Cox 	}
139696fd7ce5SGreg Kroah-Hartman 	/* Compatibility until drivers always set this */
1397adc8d746SAlan Cox 	tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1398adc8d746SAlan Cox 	tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
139996fd7ce5SGreg Kroah-Hartman 	return 0;
140096fd7ce5SGreg Kroah-Hartman }
140196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_init_termios);
140296fd7ce5SGreg Kroah-Hartman 
140366d450e8SJiri Slaby int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
140466d450e8SJiri Slaby {
140566d450e8SJiri Slaby 	int ret = tty_init_termios(tty);
140666d450e8SJiri Slaby 	if (ret)
140766d450e8SJiri Slaby 		return ret;
140866d450e8SJiri Slaby 
140966d450e8SJiri Slaby 	tty_driver_kref_get(driver);
141066d450e8SJiri Slaby 	tty->count++;
141166d450e8SJiri Slaby 	driver->ttys[tty->index] = tty;
141266d450e8SJiri Slaby 	return 0;
141366d450e8SJiri Slaby }
141466d450e8SJiri Slaby EXPORT_SYMBOL_GPL(tty_standard_install);
141566d450e8SJiri Slaby 
141696fd7ce5SGreg Kroah-Hartman /**
141796fd7ce5SGreg Kroah-Hartman  *	tty_driver_install_tty() - install a tty entry in the driver
141896fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
141996fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
142096fd7ce5SGreg Kroah-Hartman  *
142196fd7ce5SGreg Kroah-Hartman  *	Install a tty object into the driver tables. The tty->index field
142296fd7ce5SGreg Kroah-Hartman  *	will be set by the time this is called. This method is responsible
142396fd7ce5SGreg Kroah-Hartman  *	for ensuring any need additional structures are allocated and
142496fd7ce5SGreg Kroah-Hartman  *	configured.
142596fd7ce5SGreg Kroah-Hartman  *
142696fd7ce5SGreg Kroah-Hartman  *	Locking: tty_mutex for now
142796fd7ce5SGreg Kroah-Hartman  */
142896fd7ce5SGreg Kroah-Hartman static int tty_driver_install_tty(struct tty_driver *driver,
142996fd7ce5SGreg Kroah-Hartman 						struct tty_struct *tty)
143096fd7ce5SGreg Kroah-Hartman {
143166d450e8SJiri Slaby 	return driver->ops->install ? driver->ops->install(driver, tty) :
143266d450e8SJiri Slaby 		tty_standard_install(driver, tty);
143396fd7ce5SGreg Kroah-Hartman }
143496fd7ce5SGreg Kroah-Hartman 
143596fd7ce5SGreg Kroah-Hartman /**
143696fd7ce5SGreg Kroah-Hartman  *	tty_driver_remove_tty() - remove a tty from the driver tables
143796fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
143896fd7ce5SGreg Kroah-Hartman  *	@idx:	 the minor number
143996fd7ce5SGreg Kroah-Hartman  *
144096fd7ce5SGreg Kroah-Hartman  *	Remvoe a tty object from the driver tables. The tty->index field
144196fd7ce5SGreg Kroah-Hartman  *	will be set by the time this is called.
144296fd7ce5SGreg Kroah-Hartman  *
144396fd7ce5SGreg Kroah-Hartman  *	Locking: tty_mutex for now
144496fd7ce5SGreg Kroah-Hartman  */
144524d406a6SJiri Slaby void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
144696fd7ce5SGreg Kroah-Hartman {
144796fd7ce5SGreg Kroah-Hartman 	if (driver->ops->remove)
144896fd7ce5SGreg Kroah-Hartman 		driver->ops->remove(driver, tty);
144996fd7ce5SGreg Kroah-Hartman 	else
145096fd7ce5SGreg Kroah-Hartman 		driver->ttys[tty->index] = NULL;
145196fd7ce5SGreg Kroah-Hartman }
145296fd7ce5SGreg Kroah-Hartman 
145396fd7ce5SGreg Kroah-Hartman /*
145496fd7ce5SGreg Kroah-Hartman  * 	tty_reopen()	- fast re-open of an open tty
145596fd7ce5SGreg Kroah-Hartman  * 	@tty	- the tty to open
145696fd7ce5SGreg Kroah-Hartman  *
145796fd7ce5SGreg Kroah-Hartman  *	Return 0 on success, -errno on error.
14585d93e748SPeter Hurley  *	Re-opens on master ptys are not allowed and return -EIO.
145996fd7ce5SGreg Kroah-Hartman  *
14605d93e748SPeter Hurley  *	Locking: Caller must hold tty_lock
146196fd7ce5SGreg Kroah-Hartman  */
146296fd7ce5SGreg Kroah-Hartman static int tty_reopen(struct tty_struct *tty)
146396fd7ce5SGreg Kroah-Hartman {
146496fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = tty->driver;
146596fd7ce5SGreg Kroah-Hartman 
1466216030ecSPeter Hurley 	if (!tty->count)
146796fd7ce5SGreg Kroah-Hartman 		return -EIO;
146896fd7ce5SGreg Kroah-Hartman 
146996fd7ce5SGreg Kroah-Hartman 	if (driver->type == TTY_DRIVER_TYPE_PTY &&
14705d93e748SPeter Hurley 	    driver->subtype == PTY_TYPE_MASTER)
147196fd7ce5SGreg Kroah-Hartman 		return -EIO;
147296fd7ce5SGreg Kroah-Hartman 
147386f2c00fSPeter Hurley 	if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
147486f2c00fSPeter Hurley 		return -EBUSY;
147586f2c00fSPeter Hurley 
147696fd7ce5SGreg Kroah-Hartman 	tty->count++;
147796fd7ce5SGreg Kroah-Hartman 
147836697529SPeter Hurley 	WARN_ON(!tty->ldisc);
147996fd7ce5SGreg Kroah-Hartman 
148096fd7ce5SGreg Kroah-Hartman 	return 0;
148196fd7ce5SGreg Kroah-Hartman }
148296fd7ce5SGreg Kroah-Hartman 
148396fd7ce5SGreg Kroah-Hartman /**
148496fd7ce5SGreg Kroah-Hartman  *	tty_init_dev		-	initialise a tty device
148596fd7ce5SGreg Kroah-Hartman  *	@driver: tty driver we are opening a device on
148696fd7ce5SGreg Kroah-Hartman  *	@idx: device index
148796fd7ce5SGreg Kroah-Hartman  *	@ret_tty: returned tty structure
148896fd7ce5SGreg Kroah-Hartman  *
148996fd7ce5SGreg Kroah-Hartman  *	Prepare a tty device. This may not be a "new" clean device but
149096fd7ce5SGreg Kroah-Hartman  *	could also be an active device. The pty drivers require special
149196fd7ce5SGreg Kroah-Hartman  *	handling because of this.
149296fd7ce5SGreg Kroah-Hartman  *
149396fd7ce5SGreg Kroah-Hartman  *	Locking:
149496fd7ce5SGreg Kroah-Hartman  *		The function is called under the tty_mutex, which
149596fd7ce5SGreg Kroah-Hartman  *	protects us from the tty struct or driver itself going away.
149696fd7ce5SGreg Kroah-Hartman  *
149796fd7ce5SGreg Kroah-Hartman  *	On exit the tty device has the line discipline attached and
149896fd7ce5SGreg Kroah-Hartman  *	a reference count of 1. If a pair was created for pty/tty use
149996fd7ce5SGreg Kroah-Hartman  *	and the other was a pty master then it too has a reference count of 1.
150096fd7ce5SGreg Kroah-Hartman  *
150196fd7ce5SGreg Kroah-Hartman  * WSH 06/09/97: Rewritten to remove races and properly clean up after a
150296fd7ce5SGreg Kroah-Hartman  * failed open.  The new code protects the open with a mutex, so it's
150396fd7ce5SGreg Kroah-Hartman  * really quite straightforward.  The mutex locking can probably be
150496fd7ce5SGreg Kroah-Hartman  * relaxed for the (most common) case of reopening a tty.
150596fd7ce5SGreg Kroah-Hartman  */
150696fd7ce5SGreg Kroah-Hartman 
1507593a27c4SKonstantin Khlebnikov struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
150896fd7ce5SGreg Kroah-Hartman {
150996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
151096fd7ce5SGreg Kroah-Hartman 	int retval;
151196fd7ce5SGreg Kroah-Hartman 
151296fd7ce5SGreg Kroah-Hartman 	/*
151396fd7ce5SGreg Kroah-Hartman 	 * First time open is complex, especially for PTY devices.
151496fd7ce5SGreg Kroah-Hartman 	 * This code guarantees that either everything succeeds and the
151596fd7ce5SGreg Kroah-Hartman 	 * TTY is ready for operation, or else the table slots are vacated
151696fd7ce5SGreg Kroah-Hartman 	 * and the allocated memory released.  (Except that the termios
151796fd7ce5SGreg Kroah-Hartman 	 * and locked termios may be retained.)
151896fd7ce5SGreg Kroah-Hartman 	 */
151996fd7ce5SGreg Kroah-Hartman 
152096fd7ce5SGreg Kroah-Hartman 	if (!try_module_get(driver->owner))
152196fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-ENODEV);
152296fd7ce5SGreg Kroah-Hartman 
15232c964a2fSRasmus Villemoes 	tty = alloc_tty_struct(driver, idx);
1524d5543503SJiri Slaby 	if (!tty) {
1525d5543503SJiri Slaby 		retval = -ENOMEM;
1526d5543503SJiri Slaby 		goto err_module_put;
1527d5543503SJiri Slaby 	}
152896fd7ce5SGreg Kroah-Hartman 
152989c8d91eSAlan Cox 	tty_lock(tty);
153096fd7ce5SGreg Kroah-Hartman 	retval = tty_driver_install_tty(driver, tty);
1531d5543503SJiri Slaby 	if (retval < 0)
1532a9dccddbSJiri Slaby 		goto err_deinit_tty;
153396fd7ce5SGreg Kroah-Hartman 
153404831dc1SJiri Slaby 	if (!tty->port)
153504831dc1SJiri Slaby 		tty->port = driver->ports[idx];
153604831dc1SJiri Slaby 
15375d4121c0SJiri Slaby 	WARN_RATELIMIT(!tty->port,
15385d4121c0SJiri Slaby 			"%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
15395d4121c0SJiri Slaby 			__func__, tty->driver->name);
15405d4121c0SJiri Slaby 
1541967fab69SJiri Slaby 	tty->port->itty = tty;
1542967fab69SJiri Slaby 
154396fd7ce5SGreg Kroah-Hartman 	/*
154496fd7ce5SGreg Kroah-Hartman 	 * Structures all installed ... call the ldisc open routines.
154596fd7ce5SGreg Kroah-Hartman 	 * If we fail here just call release_tty to clean up.  No need
154696fd7ce5SGreg Kroah-Hartman 	 * to decrement the use counts, as release_tty doesn't care.
154796fd7ce5SGreg Kroah-Hartman 	 */
154896fd7ce5SGreg Kroah-Hartman 	retval = tty_ldisc_setup(tty, tty->link);
154996fd7ce5SGreg Kroah-Hartman 	if (retval)
1550d5543503SJiri Slaby 		goto err_release_tty;
155189c8d91eSAlan Cox 	/* Return the tty locked so that it cannot vanish under the caller */
155296fd7ce5SGreg Kroah-Hartman 	return tty;
155396fd7ce5SGreg Kroah-Hartman 
1554a9dccddbSJiri Slaby err_deinit_tty:
155589c8d91eSAlan Cox 	tty_unlock(tty);
1556a9dccddbSJiri Slaby 	deinitialize_tty_struct(tty);
1557d5543503SJiri Slaby 	free_tty_struct(tty);
1558d5543503SJiri Slaby err_module_put:
155996fd7ce5SGreg Kroah-Hartman 	module_put(driver->owner);
1560d5543503SJiri Slaby 	return ERR_PTR(retval);
156196fd7ce5SGreg Kroah-Hartman 
156296fd7ce5SGreg Kroah-Hartman 	/* call the tty release_tty routine to clean out this slot */
1563d5543503SJiri Slaby err_release_tty:
156489c8d91eSAlan Cox 	tty_unlock(tty);
1565339f36baSPeter Hurley 	tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1566339f36baSPeter Hurley 			     retval, idx);
156796fd7ce5SGreg Kroah-Hartman 	release_tty(tty, idx);
156896fd7ce5SGreg Kroah-Hartman 	return ERR_PTR(retval);
156996fd7ce5SGreg Kroah-Hartman }
157096fd7ce5SGreg Kroah-Hartman 
157196fd7ce5SGreg Kroah-Hartman void tty_free_termios(struct tty_struct *tty)
157296fd7ce5SGreg Kroah-Hartman {
157396fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
157496fd7ce5SGreg Kroah-Hartman 	int idx = tty->index;
157536b3c070SAlan Cox 
157636b3c070SAlan Cox 	/* If the port is going to reset then it has no termios to save */
157736b3c070SAlan Cox 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
157836b3c070SAlan Cox 		return;
157936b3c070SAlan Cox 
158036b3c070SAlan Cox 	/* Stash the termios data */
1581adc8d746SAlan Cox 	tp = tty->driver->termios[idx];
158236b3c070SAlan Cox 	if (tp == NULL) {
158336b3c070SAlan Cox 		tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
158425080652SPeter Hurley 		if (tp == NULL)
158536b3c070SAlan Cox 			return;
15864ac5d705SDan Carpenter 		tty->driver->termios[idx] = tp;
158736b3c070SAlan Cox 	}
158836b3c070SAlan Cox 	*tp = tty->termios;
158996fd7ce5SGreg Kroah-Hartman }
159096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_free_termios);
159196fd7ce5SGreg Kroah-Hartman 
1592a2965b7bSPeter Hurley /**
1593949aa64fSPeter Hurley  *	tty_flush_works		-	flush all works of a tty/pty pair
1594949aa64fSPeter Hurley  *	@tty: tty device to flush works for (or either end of a pty pair)
1595a2965b7bSPeter Hurley  *
1596949aa64fSPeter Hurley  *	Sync flush all works belonging to @tty (and the 'other' tty).
1597a2965b7bSPeter Hurley  */
1598a2965b7bSPeter Hurley static void tty_flush_works(struct tty_struct *tty)
1599a2965b7bSPeter Hurley {
1600a2965b7bSPeter Hurley 	flush_work(&tty->SAK_work);
1601a2965b7bSPeter Hurley 	flush_work(&tty->hangup_work);
1602949aa64fSPeter Hurley 	if (tty->link) {
1603949aa64fSPeter Hurley 		flush_work(&tty->link->SAK_work);
1604949aa64fSPeter Hurley 		flush_work(&tty->link->hangup_work);
1605949aa64fSPeter Hurley 	}
1606a2965b7bSPeter Hurley }
160796fd7ce5SGreg Kroah-Hartman 
160896fd7ce5SGreg Kroah-Hartman /**
160996fd7ce5SGreg Kroah-Hartman  *	release_one_tty		-	release tty structure memory
161096fd7ce5SGreg Kroah-Hartman  *	@kref: kref of tty we are obliterating
161196fd7ce5SGreg Kroah-Hartman  *
161296fd7ce5SGreg Kroah-Hartman  *	Releases memory associated with a tty structure, and clears out the
161396fd7ce5SGreg Kroah-Hartman  *	driver table slots. This function is called when a device is no longer
161496fd7ce5SGreg Kroah-Hartman  *	in use. It also gets called when setup of a device fails.
161596fd7ce5SGreg Kroah-Hartman  *
161696fd7ce5SGreg Kroah-Hartman  *	Locking:
161796fd7ce5SGreg Kroah-Hartman  *		takes the file list lock internally when working on the list
161896fd7ce5SGreg Kroah-Hartman  *	of ttys that the driver keeps.
161996fd7ce5SGreg Kroah-Hartman  *
162096fd7ce5SGreg Kroah-Hartman  *	This method gets called from a work queue so that the driver private
162196fd7ce5SGreg Kroah-Hartman  *	cleanup ops can sleep (needed for USB at least)
162296fd7ce5SGreg Kroah-Hartman  */
162396fd7ce5SGreg Kroah-Hartman static void release_one_tty(struct work_struct *work)
162496fd7ce5SGreg Kroah-Hartman {
162596fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
162696fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, hangup_work);
162796fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = tty->driver;
1628b216df53SCyrill Gorcunov 	struct module *owner = driver->owner;
162996fd7ce5SGreg Kroah-Hartman 
163096fd7ce5SGreg Kroah-Hartman 	if (tty->ops->cleanup)
163196fd7ce5SGreg Kroah-Hartman 		tty->ops->cleanup(tty);
163296fd7ce5SGreg Kroah-Hartman 
163396fd7ce5SGreg Kroah-Hartman 	tty->magic = 0;
163496fd7ce5SGreg Kroah-Hartman 	tty_driver_kref_put(driver);
1635b216df53SCyrill Gorcunov 	module_put(owner);
163696fd7ce5SGreg Kroah-Hartman 
163796fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
163896fd7ce5SGreg Kroah-Hartman 	list_del_init(&tty->tty_files);
163996fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
164096fd7ce5SGreg Kroah-Hartman 
164196fd7ce5SGreg Kroah-Hartman 	put_pid(tty->pgrp);
164296fd7ce5SGreg Kroah-Hartman 	put_pid(tty->session);
164396fd7ce5SGreg Kroah-Hartman 	free_tty_struct(tty);
164496fd7ce5SGreg Kroah-Hartman }
164596fd7ce5SGreg Kroah-Hartman 
164696fd7ce5SGreg Kroah-Hartman static void queue_release_one_tty(struct kref *kref)
164796fd7ce5SGreg Kroah-Hartman {
164896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
164996fd7ce5SGreg Kroah-Hartman 
165096fd7ce5SGreg Kroah-Hartman 	/* The hangup queue is now free so we can reuse it rather than
165196fd7ce5SGreg Kroah-Hartman 	   waste a chunk of memory for each port */
165296fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->hangup_work, release_one_tty);
165396fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->hangup_work);
165496fd7ce5SGreg Kroah-Hartman }
165596fd7ce5SGreg Kroah-Hartman 
165696fd7ce5SGreg Kroah-Hartman /**
165796fd7ce5SGreg Kroah-Hartman  *	tty_kref_put		-	release a tty kref
165896fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
165996fd7ce5SGreg Kroah-Hartman  *
166096fd7ce5SGreg Kroah-Hartman  *	Release a reference to a tty device and if need be let the kref
166196fd7ce5SGreg Kroah-Hartman  *	layer destruct the object for us
166296fd7ce5SGreg Kroah-Hartman  */
166396fd7ce5SGreg Kroah-Hartman 
166496fd7ce5SGreg Kroah-Hartman void tty_kref_put(struct tty_struct *tty)
166596fd7ce5SGreg Kroah-Hartman {
166696fd7ce5SGreg Kroah-Hartman 	if (tty)
166796fd7ce5SGreg Kroah-Hartman 		kref_put(&tty->kref, queue_release_one_tty);
166896fd7ce5SGreg Kroah-Hartman }
166996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_kref_put);
167096fd7ce5SGreg Kroah-Hartman 
167196fd7ce5SGreg Kroah-Hartman /**
167296fd7ce5SGreg Kroah-Hartman  *	release_tty		-	release tty structure memory
167396fd7ce5SGreg Kroah-Hartman  *
167496fd7ce5SGreg Kroah-Hartman  *	Release both @tty and a possible linked partner (think pty pair),
167596fd7ce5SGreg Kroah-Hartman  *	and decrement the refcount of the backing module.
167696fd7ce5SGreg Kroah-Hartman  *
167796fd7ce5SGreg Kroah-Hartman  *	Locking:
1678d155255aSAlan Cox  *		tty_mutex
167996fd7ce5SGreg Kroah-Hartman  *		takes the file list lock internally when working on the list
168096fd7ce5SGreg Kroah-Hartman  *	of ttys that the driver keeps.
168196fd7ce5SGreg Kroah-Hartman  *
168296fd7ce5SGreg Kroah-Hartman  */
168396fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx)
168496fd7ce5SGreg Kroah-Hartman {
168596fd7ce5SGreg Kroah-Hartman 	/* This should always be true but check for the moment */
168696fd7ce5SGreg Kroah-Hartman 	WARN_ON(tty->index != idx);
1687d155255aSAlan Cox 	WARN_ON(!mutex_is_locked(&tty_mutex));
168836b3c070SAlan Cox 	if (tty->ops->shutdown)
168936b3c070SAlan Cox 		tty->ops->shutdown(tty);
169036b3c070SAlan Cox 	tty_free_termios(tty);
169136b3c070SAlan Cox 	tty_driver_remove_tty(tty->driver, tty);
1692967fab69SJiri Slaby 	tty->port->itty = NULL;
169364e377dcSPeter Hurley 	if (tty->link)
169464e377dcSPeter Hurley 		tty->link->port->itty = NULL;
1695e176058fSPeter Hurley 	tty_buffer_cancel_work(tty->port);
169636b3c070SAlan Cox 
169796fd7ce5SGreg Kroah-Hartman 	tty_kref_put(tty->link);
169896fd7ce5SGreg Kroah-Hartman 	tty_kref_put(tty);
169996fd7ce5SGreg Kroah-Hartman }
170096fd7ce5SGreg Kroah-Hartman 
170196fd7ce5SGreg Kroah-Hartman /**
1702955787caSJiri Slaby  *	tty_release_checks - check a tty before real release
1703955787caSJiri Slaby  *	@tty: tty to check
1704955787caSJiri Slaby  *	@o_tty: link of @tty (if any)
1705955787caSJiri Slaby  *	@idx: index of the tty
1706955787caSJiri Slaby  *
1707955787caSJiri Slaby  *	Performs some paranoid checking before true release of the @tty.
1708955787caSJiri Slaby  *	This is a no-op unless TTY_PARANOIA_CHECK is defined.
1709955787caSJiri Slaby  */
1710359b9fb5SPeter Hurley static int tty_release_checks(struct tty_struct *tty, int idx)
1711955787caSJiri Slaby {
1712955787caSJiri Slaby #ifdef TTY_PARANOIA_CHECK
1713955787caSJiri Slaby 	if (idx < 0 || idx >= tty->driver->num) {
1714e2dfa3d3SPeter Hurley 		tty_debug(tty, "bad idx %d\n", idx);
1715955787caSJiri Slaby 		return -1;
1716955787caSJiri Slaby 	}
1717955787caSJiri Slaby 
1718955787caSJiri Slaby 	/* not much to check for devpts */
1719955787caSJiri Slaby 	if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1720955787caSJiri Slaby 		return 0;
1721955787caSJiri Slaby 
1722955787caSJiri Slaby 	if (tty != tty->driver->ttys[idx]) {
1723e2dfa3d3SPeter Hurley 		tty_debug(tty, "bad driver table[%d] = %p\n",
1724e2dfa3d3SPeter Hurley 			  idx, tty->driver->ttys[idx]);
1725955787caSJiri Slaby 		return -1;
1726955787caSJiri Slaby 	}
1727955787caSJiri Slaby 	if (tty->driver->other) {
1728359b9fb5SPeter Hurley 		struct tty_struct *o_tty = tty->link;
1729359b9fb5SPeter Hurley 
1730955787caSJiri Slaby 		if (o_tty != tty->driver->other->ttys[idx]) {
1731e2dfa3d3SPeter Hurley 			tty_debug(tty, "bad other table[%d] = %p\n",
1732e2dfa3d3SPeter Hurley 				  idx, tty->driver->other->ttys[idx]);
1733955787caSJiri Slaby 			return -1;
1734955787caSJiri Slaby 		}
1735955787caSJiri Slaby 		if (o_tty->link != tty) {
1736e2dfa3d3SPeter Hurley 			tty_debug(tty, "bad link = %p\n", o_tty->link);
1737955787caSJiri Slaby 			return -1;
1738955787caSJiri Slaby 		}
1739955787caSJiri Slaby 	}
1740955787caSJiri Slaby #endif
1741955787caSJiri Slaby 	return 0;
1742955787caSJiri Slaby }
1743955787caSJiri Slaby 
1744955787caSJiri Slaby /**
174596fd7ce5SGreg Kroah-Hartman  *	tty_release		-	vfs callback for close
174696fd7ce5SGreg Kroah-Hartman  *	@inode: inode of tty
174796fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer for handle to tty
174896fd7ce5SGreg Kroah-Hartman  *
174996fd7ce5SGreg Kroah-Hartman  *	Called the last time each file handle is closed that references
175096fd7ce5SGreg Kroah-Hartman  *	this tty. There may however be several such references.
175196fd7ce5SGreg Kroah-Hartman  *
175296fd7ce5SGreg Kroah-Hartman  *	Locking:
175396fd7ce5SGreg Kroah-Hartman  *		Takes bkl. See tty_release_dev
175496fd7ce5SGreg Kroah-Hartman  *
175596fd7ce5SGreg Kroah-Hartman  * Even releasing the tty structures is a tricky business.. We have
175696fd7ce5SGreg Kroah-Hartman  * to be very careful that the structures are all released at the
175796fd7ce5SGreg Kroah-Hartman  * same time, as interrupts might otherwise get the wrong pointers.
175896fd7ce5SGreg Kroah-Hartman  *
175996fd7ce5SGreg Kroah-Hartman  * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
176096fd7ce5SGreg Kroah-Hartman  * lead to double frees or releasing memory still in use.
176196fd7ce5SGreg Kroah-Hartman  */
176296fd7ce5SGreg Kroah-Hartman 
176396fd7ce5SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp)
176496fd7ce5SGreg Kroah-Hartman {
176596fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
17667ffb6da9SPeter Hurley 	struct tty_struct *o_tty = NULL;
17677ffb6da9SPeter Hurley 	int	do_sleep, final;
176896fd7ce5SGreg Kroah-Hartman 	int	idx;
176937b16457SPeter Hurley 	long	timeout = 0;
1770494c1eacSPeter Hurley 	int	once = 1;
177196fd7ce5SGreg Kroah-Hartman 
17729de44bd6SJiri Slaby 	if (tty_paranoia_check(tty, inode, __func__))
177396fd7ce5SGreg Kroah-Hartman 		return 0;
177496fd7ce5SGreg Kroah-Hartman 
177589c8d91eSAlan Cox 	tty_lock(tty);
17769de44bd6SJiri Slaby 	check_tty_count(tty, __func__);
177796fd7ce5SGreg Kroah-Hartman 
177896fd7ce5SGreg Kroah-Hartman 	__tty_fasync(-1, filp, 0);
177996fd7ce5SGreg Kroah-Hartman 
178096fd7ce5SGreg Kroah-Hartman 	idx = tty->index;
17817ffb6da9SPeter Hurley 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
17827ffb6da9SPeter Hurley 	    tty->driver->subtype == PTY_TYPE_MASTER)
178396fd7ce5SGreg Kroah-Hartman 		o_tty = tty->link;
178496fd7ce5SGreg Kroah-Hartman 
1785359b9fb5SPeter Hurley 	if (tty_release_checks(tty, idx)) {
178689c8d91eSAlan Cox 		tty_unlock(tty);
178796fd7ce5SGreg Kroah-Hartman 		return 0;
178896fd7ce5SGreg Kroah-Hartman 	}
178996fd7ce5SGreg Kroah-Hartman 
1790d435cefeSPeter Hurley 	tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
179196fd7ce5SGreg Kroah-Hartman 
179296fd7ce5SGreg Kroah-Hartman 	if (tty->ops->close)
179396fd7ce5SGreg Kroah-Hartman 		tty->ops->close(tty, filp);
179496fd7ce5SGreg Kroah-Hartman 
17952aff5e2bSPeter Hurley 	/* If tty is pty master, lock the slave pty (stable lock order) */
17962aff5e2bSPeter Hurley 	tty_lock_slave(o_tty);
17972aff5e2bSPeter Hurley 
179896fd7ce5SGreg Kroah-Hartman 	/*
179996fd7ce5SGreg Kroah-Hartman 	 * Sanity check: if tty->count is going to zero, there shouldn't be
180096fd7ce5SGreg Kroah-Hartman 	 * any waiters on tty->read_wait or tty->write_wait.  We test the
180196fd7ce5SGreg Kroah-Hartman 	 * wait queues and kick everyone out _before_ actually starting to
180296fd7ce5SGreg Kroah-Hartman 	 * close.  This ensures that we won't block while releasing the tty
180396fd7ce5SGreg Kroah-Hartman 	 * structure.
180496fd7ce5SGreg Kroah-Hartman 	 *
180596fd7ce5SGreg Kroah-Hartman 	 * The test for the o_tty closing is necessary, since the master and
180696fd7ce5SGreg Kroah-Hartman 	 * slave sides may close in any order.  If the slave side closes out
180796fd7ce5SGreg Kroah-Hartman 	 * first, its count will be one, since the master side holds an open.
1808324c1650SPeter Hurley 	 * Thus this test wouldn't be triggered at the time the slave closed,
180996fd7ce5SGreg Kroah-Hartman 	 * so we do it now.
181096fd7ce5SGreg Kroah-Hartman 	 */
1811d5e370a4SPeter Hurley 	while (1) {
181296fd7ce5SGreg Kroah-Hartman 		do_sleep = 0;
181396fd7ce5SGreg Kroah-Hartman 
1814324c1650SPeter Hurley 		if (tty->count <= 1) {
181596fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&tty->read_wait)) {
181696fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&tty->read_wait, POLLIN);
181796fd7ce5SGreg Kroah-Hartman 				do_sleep++;
181896fd7ce5SGreg Kroah-Hartman 			}
181996fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&tty->write_wait)) {
182096fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&tty->write_wait, POLLOUT);
182196fd7ce5SGreg Kroah-Hartman 				do_sleep++;
182296fd7ce5SGreg Kroah-Hartman 			}
182396fd7ce5SGreg Kroah-Hartman 		}
18247ffb6da9SPeter Hurley 		if (o_tty && o_tty->count <= 1) {
182596fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&o_tty->read_wait)) {
182696fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&o_tty->read_wait, POLLIN);
182796fd7ce5SGreg Kroah-Hartman 				do_sleep++;
182896fd7ce5SGreg Kroah-Hartman 			}
182996fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&o_tty->write_wait)) {
183096fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&o_tty->write_wait, POLLOUT);
183196fd7ce5SGreg Kroah-Hartman 				do_sleep++;
183296fd7ce5SGreg Kroah-Hartman 			}
183396fd7ce5SGreg Kroah-Hartman 		}
183496fd7ce5SGreg Kroah-Hartman 		if (!do_sleep)
183596fd7ce5SGreg Kroah-Hartman 			break;
183696fd7ce5SGreg Kroah-Hartman 
1837494c1eacSPeter Hurley 		if (once) {
1838494c1eacSPeter Hurley 			once = 0;
1839339f36baSPeter Hurley 			tty_warn(tty, "read/write wait queue active!\n");
1840494c1eacSPeter Hurley 		}
184137b16457SPeter Hurley 		schedule_timeout_killable(timeout);
184237b16457SPeter Hurley 		if (timeout < 120 * HZ)
184337b16457SPeter Hurley 			timeout = 2 * timeout + 1;
184437b16457SPeter Hurley 		else
184537b16457SPeter Hurley 			timeout = MAX_SCHEDULE_TIMEOUT;
184696fd7ce5SGreg Kroah-Hartman 	}
184796fd7ce5SGreg Kroah-Hartman 
18487ffb6da9SPeter Hurley 	if (o_tty) {
184996fd7ce5SGreg Kroah-Hartman 		if (--o_tty->count < 0) {
1850339f36baSPeter Hurley 			tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
185196fd7ce5SGreg Kroah-Hartman 			o_tty->count = 0;
185296fd7ce5SGreg Kroah-Hartman 		}
185396fd7ce5SGreg Kroah-Hartman 	}
185496fd7ce5SGreg Kroah-Hartman 	if (--tty->count < 0) {
1855339f36baSPeter Hurley 		tty_warn(tty, "bad tty->count (%d)\n", tty->count);
185696fd7ce5SGreg Kroah-Hartman 		tty->count = 0;
185796fd7ce5SGreg Kroah-Hartman 	}
185896fd7ce5SGreg Kroah-Hartman 
185996fd7ce5SGreg Kroah-Hartman 	/*
186096fd7ce5SGreg Kroah-Hartman 	 * We've decremented tty->count, so we need to remove this file
186196fd7ce5SGreg Kroah-Hartman 	 * descriptor off the tty->tty_files list; this serves two
186296fd7ce5SGreg Kroah-Hartman 	 * purposes:
186396fd7ce5SGreg Kroah-Hartman 	 *  - check_tty_count sees the correct number of file descriptors
186496fd7ce5SGreg Kroah-Hartman 	 *    associated with this tty.
186596fd7ce5SGreg Kroah-Hartman 	 *  - do_tty_hangup no longer sees this file descriptor as
186696fd7ce5SGreg Kroah-Hartman 	 *    something that needs to be handled for hangups.
186796fd7ce5SGreg Kroah-Hartman 	 */
186896fd7ce5SGreg Kroah-Hartman 	tty_del_file(filp);
186996fd7ce5SGreg Kroah-Hartman 
187096fd7ce5SGreg Kroah-Hartman 	/*
187196fd7ce5SGreg Kroah-Hartman 	 * Perform some housekeeping before deciding whether to return.
187296fd7ce5SGreg Kroah-Hartman 	 *
187396fd7ce5SGreg Kroah-Hartman 	 * If _either_ side is closing, make sure there aren't any
187496fd7ce5SGreg Kroah-Hartman 	 * processes that still think tty or o_tty is their controlling
187596fd7ce5SGreg Kroah-Hartman 	 * tty.
187696fd7ce5SGreg Kroah-Hartman 	 */
1877324c1650SPeter Hurley 	if (!tty->count) {
187896fd7ce5SGreg Kroah-Hartman 		read_lock(&tasklist_lock);
187996fd7ce5SGreg Kroah-Hartman 		session_clear_tty(tty->session);
18807ffb6da9SPeter Hurley 		if (o_tty)
188196fd7ce5SGreg Kroah-Hartman 			session_clear_tty(o_tty->session);
188296fd7ce5SGreg Kroah-Hartman 		read_unlock(&tasklist_lock);
188396fd7ce5SGreg Kroah-Hartman 	}
188496fd7ce5SGreg Kroah-Hartman 
1885324c1650SPeter Hurley 	/* check whether both sides are closing ... */
18867ffb6da9SPeter Hurley 	final = !tty->count && !(o_tty && o_tty->count);
1887324c1650SPeter Hurley 
18882aff5e2bSPeter Hurley 	tty_unlock_slave(o_tty);
18892aff5e2bSPeter Hurley 	tty_unlock(tty);
18902aff5e2bSPeter Hurley 
189104980706SPeter Hurley 	/* At this point, the tty->count == 0 should ensure a dead tty
1892d155255aSAlan Cox 	   cannot be re-opened by a racing opener */
189396fd7ce5SGreg Kroah-Hartman 
1894324c1650SPeter Hurley 	if (!final)
189596fd7ce5SGreg Kroah-Hartman 		return 0;
189696fd7ce5SGreg Kroah-Hartman 
1897accff793SPeter Hurley 	tty_debug_hangup(tty, "final close\n");
189896fd7ce5SGreg Kroah-Hartman 	/*
189996fd7ce5SGreg Kroah-Hartman 	 * Ask the line discipline code to release its structures
190096fd7ce5SGreg Kroah-Hartman 	 */
190162462aefSPeter Hurley 	tty_ldisc_release(tty);
1902a2965b7bSPeter Hurley 
1903a2965b7bSPeter Hurley 	/* Wait for pending work before tty destruction commmences */
1904a2965b7bSPeter Hurley 	tty_flush_works(tty);
1905a2965b7bSPeter Hurley 
1906d435cefeSPeter Hurley 	tty_debug_hangup(tty, "freeing structure\n");
190796fd7ce5SGreg Kroah-Hartman 	/*
190896fd7ce5SGreg Kroah-Hartman 	 * The release_tty function takes care of the details of clearing
190989c8d91eSAlan Cox 	 * the slots and preserving the termios structure. The tty_unlock_pair
191089c8d91eSAlan Cox 	 * should be safe as we keep a kref while the tty is locked (so the
191189c8d91eSAlan Cox 	 * unlock never unlocks a freed tty).
191296fd7ce5SGreg Kroah-Hartman 	 */
1913d155255aSAlan Cox 	mutex_lock(&tty_mutex);
191496fd7ce5SGreg Kroah-Hartman 	release_tty(tty, idx);
1915d155255aSAlan Cox 	mutex_unlock(&tty_mutex);
191696fd7ce5SGreg Kroah-Hartman 
191796fd7ce5SGreg Kroah-Hartman 	return 0;
191896fd7ce5SGreg Kroah-Hartman }
191996fd7ce5SGreg Kroah-Hartman 
192096fd7ce5SGreg Kroah-Hartman /**
192152494eebSPeter Hurley  *	tty_open_current_tty - get locked tty of current task
1922b82154acSJiri Slaby  *	@device: device number
1923b82154acSJiri Slaby  *	@filp: file pointer to tty
192452494eebSPeter Hurley  *	@return: locked tty of the current task iff @device is /dev/tty
192552494eebSPeter Hurley  *
192652494eebSPeter Hurley  *	Performs a re-open of the current task's controlling tty.
1927b82154acSJiri Slaby  *
1928b82154acSJiri Slaby  *	We cannot return driver and index like for the other nodes because
1929b82154acSJiri Slaby  *	devpts will not work then. It expects inodes to be from devpts FS.
1930b82154acSJiri Slaby  */
1931b82154acSJiri Slaby static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1932b82154acSJiri Slaby {
1933b82154acSJiri Slaby 	struct tty_struct *tty;
193452494eebSPeter Hurley 	int retval;
1935b82154acSJiri Slaby 
1936b82154acSJiri Slaby 	if (device != MKDEV(TTYAUX_MAJOR, 0))
1937b82154acSJiri Slaby 		return NULL;
1938b82154acSJiri Slaby 
1939b82154acSJiri Slaby 	tty = get_current_tty();
1940b82154acSJiri Slaby 	if (!tty)
1941b82154acSJiri Slaby 		return ERR_PTR(-ENXIO);
1942b82154acSJiri Slaby 
1943b82154acSJiri Slaby 	filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1944b82154acSJiri Slaby 	/* noctty = 1; */
194552494eebSPeter Hurley 	tty_lock(tty);
194652494eebSPeter Hurley 	tty_kref_put(tty);	/* safe to drop the kref now */
194752494eebSPeter Hurley 
194852494eebSPeter Hurley 	retval = tty_reopen(tty);
194952494eebSPeter Hurley 	if (retval < 0) {
195052494eebSPeter Hurley 		tty_unlock(tty);
195152494eebSPeter Hurley 		tty = ERR_PTR(retval);
195252494eebSPeter Hurley 	}
1953b82154acSJiri Slaby 	return tty;
1954b82154acSJiri Slaby }
1955b82154acSJiri Slaby 
1956b82154acSJiri Slaby /**
19575b5e7040SJiri Slaby  *	tty_lookup_driver - lookup a tty driver for a given device file
19585b5e7040SJiri Slaby  *	@device: device number
19595b5e7040SJiri Slaby  *	@filp: file pointer to tty
19605b5e7040SJiri Slaby  *	@noctty: set if the device should not become a controlling tty
19615b5e7040SJiri Slaby  *	@index: index for the device in the @return driver
19625b5e7040SJiri Slaby  *	@return: driver for this inode (with increased refcount)
19635b5e7040SJiri Slaby  *
19645b5e7040SJiri Slaby  * 	If @return is not erroneous, the caller is responsible to decrement the
19655b5e7040SJiri Slaby  * 	refcount by tty_driver_kref_put.
19665b5e7040SJiri Slaby  *
19675b5e7040SJiri Slaby  *	Locking: tty_mutex protects get_tty_driver
19685b5e7040SJiri Slaby  */
19695b5e7040SJiri Slaby static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
19705b5e7040SJiri Slaby 		int *noctty, int *index)
19715b5e7040SJiri Slaby {
19725b5e7040SJiri Slaby 	struct tty_driver *driver;
19735b5e7040SJiri Slaby 
19742cd0050cSJiri Slaby 	switch (device) {
19755b5e7040SJiri Slaby #ifdef CONFIG_VT
19762cd0050cSJiri Slaby 	case MKDEV(TTY_MAJOR, 0): {
19775b5e7040SJiri Slaby 		extern struct tty_driver *console_driver;
19785b5e7040SJiri Slaby 		driver = tty_driver_kref_get(console_driver);
19795b5e7040SJiri Slaby 		*index = fg_console;
19805b5e7040SJiri Slaby 		*noctty = 1;
19812cd0050cSJiri Slaby 		break;
19825b5e7040SJiri Slaby 	}
19835b5e7040SJiri Slaby #endif
19842cd0050cSJiri Slaby 	case MKDEV(TTYAUX_MAJOR, 1): {
19855b5e7040SJiri Slaby 		struct tty_driver *console_driver = console_device(index);
19865b5e7040SJiri Slaby 		if (console_driver) {
19875b5e7040SJiri Slaby 			driver = tty_driver_kref_get(console_driver);
19885b5e7040SJiri Slaby 			if (driver) {
19895b5e7040SJiri Slaby 				/* Don't let /dev/console block */
19905b5e7040SJiri Slaby 				filp->f_flags |= O_NONBLOCK;
19915b5e7040SJiri Slaby 				*noctty = 1;
19922cd0050cSJiri Slaby 				break;
19935b5e7040SJiri Slaby 			}
19945b5e7040SJiri Slaby 		}
19955b5e7040SJiri Slaby 		return ERR_PTR(-ENODEV);
19965b5e7040SJiri Slaby 	}
19972cd0050cSJiri Slaby 	default:
19985b5e7040SJiri Slaby 		driver = get_tty_driver(device, index);
19995b5e7040SJiri Slaby 		if (!driver)
20005b5e7040SJiri Slaby 			return ERR_PTR(-ENODEV);
20012cd0050cSJiri Slaby 		break;
20022cd0050cSJiri Slaby 	}
20035b5e7040SJiri Slaby 	return driver;
20045b5e7040SJiri Slaby }
20055b5e7040SJiri Slaby 
20065b5e7040SJiri Slaby /**
200796fd7ce5SGreg Kroah-Hartman  *	tty_open		-	open a tty device
200896fd7ce5SGreg Kroah-Hartman  *	@inode: inode of device file
200996fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer to tty
201096fd7ce5SGreg Kroah-Hartman  *
201196fd7ce5SGreg Kroah-Hartman  *	tty_open and tty_release keep up the tty count that contains the
201296fd7ce5SGreg Kroah-Hartman  *	number of opens done on a tty. We cannot use the inode-count, as
201396fd7ce5SGreg Kroah-Hartman  *	different inodes might point to the same tty.
201496fd7ce5SGreg Kroah-Hartman  *
201596fd7ce5SGreg Kroah-Hartman  *	Open-counting is needed for pty masters, as well as for keeping
201696fd7ce5SGreg Kroah-Hartman  *	track of serial lines: DTR is dropped when the last close happens.
201796fd7ce5SGreg Kroah-Hartman  *	(This is not done solely through tty->count, now.  - Ted 1/27/92)
201896fd7ce5SGreg Kroah-Hartman  *
201996fd7ce5SGreg Kroah-Hartman  *	The termios state of a pty is reset on first open so that
202096fd7ce5SGreg Kroah-Hartman  *	settings don't persist across reuse.
202196fd7ce5SGreg Kroah-Hartman  *
20225b5e7040SJiri Slaby  *	Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
202396fd7ce5SGreg Kroah-Hartman  *		 tty->count should protect the rest.
202496fd7ce5SGreg Kroah-Hartman  *		 ->siglock protects ->signal/->sighand
202589c8d91eSAlan Cox  *
202689c8d91eSAlan Cox  *	Note: the tty_unlock/lock cases without a ref are only safe due to
202789c8d91eSAlan Cox  *	tty_mutex
202896fd7ce5SGreg Kroah-Hartman  */
202996fd7ce5SGreg Kroah-Hartman 
203096fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *inode, struct file *filp)
203196fd7ce5SGreg Kroah-Hartman {
2032b82154acSJiri Slaby 	struct tty_struct *tty;
203396fd7ce5SGreg Kroah-Hartman 	int noctty, retval;
2034b82154acSJiri Slaby 	struct tty_driver *driver = NULL;
203596fd7ce5SGreg Kroah-Hartman 	int index;
203696fd7ce5SGreg Kroah-Hartman 	dev_t device = inode->i_rdev;
203796fd7ce5SGreg Kroah-Hartman 	unsigned saved_flags = filp->f_flags;
203896fd7ce5SGreg Kroah-Hartman 
203996fd7ce5SGreg Kroah-Hartman 	nonseekable_open(inode, filp);
204096fd7ce5SGreg Kroah-Hartman 
204196fd7ce5SGreg Kroah-Hartman retry_open:
2042fa90e1c9SJiri Slaby 	retval = tty_alloc_file(filp);
2043fa90e1c9SJiri Slaby 	if (retval)
2044fa90e1c9SJiri Slaby 		return -ENOMEM;
2045fa90e1c9SJiri Slaby 
204696fd7ce5SGreg Kroah-Hartman 	noctty = filp->f_flags & O_NOCTTY;
204796fd7ce5SGreg Kroah-Hartman 	index  = -1;
204896fd7ce5SGreg Kroah-Hartman 	retval = 0;
204996fd7ce5SGreg Kroah-Hartman 
2050b82154acSJiri Slaby 	tty = tty_open_current_tty(device, filp);
205152494eebSPeter Hurley 	if (!tty) {
205252494eebSPeter Hurley 		mutex_lock(&tty_mutex);
20535b5e7040SJiri Slaby 		driver = tty_lookup_driver(device, filp, &noctty, &index);
20545b5e7040SJiri Slaby 		if (IS_ERR(driver)) {
2055ba5db448SJiri Slaby 			retval = PTR_ERR(driver);
2056ba5db448SJiri Slaby 			goto err_unlock;
205796fd7ce5SGreg Kroah-Hartman 		}
205896fd7ce5SGreg Kroah-Hartman 
205996fd7ce5SGreg Kroah-Hartman 		/* check whether we're reopening an existing tty */
206096fd7ce5SGreg Kroah-Hartman 		tty = tty_driver_lookup_tty(driver, inode, index);
206196fd7ce5SGreg Kroah-Hartman 		if (IS_ERR(tty)) {
2062ba5db448SJiri Slaby 			retval = PTR_ERR(tty);
2063ba5db448SJiri Slaby 			goto err_unlock;
206496fd7ce5SGreg Kroah-Hartman 		}
206596fd7ce5SGreg Kroah-Hartman 
206696fd7ce5SGreg Kroah-Hartman 		if (tty) {
2067aa3cb814SPeter Hurley 			mutex_unlock(&tty_mutex);
206889c8d91eSAlan Cox 			tty_lock(tty);
2069aa3cb814SPeter Hurley 			/* safe to drop the kref from tty_driver_lookup_tty() */
2070aa3cb814SPeter Hurley 			tty_kref_put(tty);
207196fd7ce5SGreg Kroah-Hartman 			retval = tty_reopen(tty);
207289c8d91eSAlan Cox 			if (retval < 0) {
207389c8d91eSAlan Cox 				tty_unlock(tty);
207496fd7ce5SGreg Kroah-Hartman 				tty = ERR_PTR(retval);
207589c8d91eSAlan Cox 			}
2076aa3cb814SPeter Hurley 		} else { /* Returns with the tty_lock held for now */
2077593a27c4SKonstantin Khlebnikov 			tty = tty_init_dev(driver, index);
207896fd7ce5SGreg Kroah-Hartman 			mutex_unlock(&tty_mutex);
2079aa3cb814SPeter Hurley 		}
2080aa3cb814SPeter Hurley 
208196fd7ce5SGreg Kroah-Hartman 		tty_driver_kref_put(driver);
208252494eebSPeter Hurley 	}
208352494eebSPeter Hurley 
208496fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(tty)) {
2085ba5db448SJiri Slaby 		retval = PTR_ERR(tty);
2086ba5db448SJiri Slaby 		goto err_file;
208796fd7ce5SGreg Kroah-Hartman 	}
208896fd7ce5SGreg Kroah-Hartman 
2089fa90e1c9SJiri Slaby 	tty_add_file(tty, filp);
209096fd7ce5SGreg Kroah-Hartman 
20919de44bd6SJiri Slaby 	check_tty_count(tty, __func__);
209296fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
209396fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_MASTER)
209496fd7ce5SGreg Kroah-Hartman 		noctty = 1;
2095accff793SPeter Hurley 
2096d435cefeSPeter Hurley 	tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
2097accff793SPeter Hurley 
209896fd7ce5SGreg Kroah-Hartman 	if (tty->ops->open)
209996fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->open(tty, filp);
210096fd7ce5SGreg Kroah-Hartman 	else
210196fd7ce5SGreg Kroah-Hartman 		retval = -ENODEV;
210296fd7ce5SGreg Kroah-Hartman 	filp->f_flags = saved_flags;
210396fd7ce5SGreg Kroah-Hartman 
210496fd7ce5SGreg Kroah-Hartman 	if (retval) {
2105d435cefeSPeter Hurley 		tty_debug_hangup(tty, "open error %d, releasing\n", retval);
2106accff793SPeter Hurley 
210789c8d91eSAlan Cox 		tty_unlock(tty); /* need to call tty_release without BTM */
210896fd7ce5SGreg Kroah-Hartman 		tty_release(inode, filp);
210996fd7ce5SGreg Kroah-Hartman 		if (retval != -ERESTARTSYS)
211096fd7ce5SGreg Kroah-Hartman 			return retval;
211196fd7ce5SGreg Kroah-Hartman 
211296fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
211396fd7ce5SGreg Kroah-Hartman 			return retval;
211496fd7ce5SGreg Kroah-Hartman 
211596fd7ce5SGreg Kroah-Hartman 		schedule();
211696fd7ce5SGreg Kroah-Hartman 		/*
211796fd7ce5SGreg Kroah-Hartman 		 * Need to reset f_op in case a hangup happened.
211896fd7ce5SGreg Kroah-Hartman 		 */
21191256937fSPeter Hurley 		if (tty_hung_up_p(filp))
212096fd7ce5SGreg Kroah-Hartman 			filp->f_op = &tty_fops;
212196fd7ce5SGreg Kroah-Hartman 		goto retry_open;
212296fd7ce5SGreg Kroah-Hartman 	}
2123d4855e1fSPeter Hurley 	clear_bit(TTY_HUPPED, &tty->flags);
212496fd7ce5SGreg Kroah-Hartman 
212596fd7ce5SGreg Kroah-Hartman 
21262c411c11SPeter Hurley 	read_lock(&tasklist_lock);
212796fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&current->sighand->siglock);
212896fd7ce5SGreg Kroah-Hartman 	if (!noctty &&
212996fd7ce5SGreg Kroah-Hartman 	    current->signal->leader &&
213096fd7ce5SGreg Kroah-Hartman 	    !current->signal->tty &&
21310c556271SJann Horn 	    tty->session == NULL) {
21320c556271SJann Horn 		/*
21330c556271SJann Horn 		 * Don't let a process that only has write access to the tty
21340c556271SJann Horn 		 * obtain the privileges associated with having a tty as
21350c556271SJann Horn 		 * controlling terminal (being able to reopen it with full
21360c556271SJann Horn 		 * access through /dev/tty, being able to perform pushback).
21370c556271SJann Horn 		 * Many distributions set the group of all ttys to "tty" and
21380c556271SJann Horn 		 * grant write-only access to all terminals for setgid tty
21390c556271SJann Horn 		 * binaries, which should not imply full privileges on all ttys.
21400c556271SJann Horn 		 *
21410c556271SJann Horn 		 * This could theoretically break old code that performs open()
21420c556271SJann Horn 		 * on a write-only file descriptor. In that case, it might be
21430c556271SJann Horn 		 * necessary to also permit this if
21440c556271SJann Horn 		 * inode_permission(inode, MAY_READ) == 0.
21450c556271SJann Horn 		 */
21460c556271SJann Horn 		if (filp->f_mode & FMODE_READ)
2147bce65f18SPeter Hurley 			__proc_set_tty(tty);
21480c556271SJann Horn 	}
214996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&current->sighand->siglock);
21502c411c11SPeter Hurley 	read_unlock(&tasklist_lock);
215189c8d91eSAlan Cox 	tty_unlock(tty);
215296fd7ce5SGreg Kroah-Hartman 	return 0;
2153ba5db448SJiri Slaby err_unlock:
2154ba5db448SJiri Slaby 	mutex_unlock(&tty_mutex);
2155ba5db448SJiri Slaby 	/* after locks to avoid deadlock */
2156ba5db448SJiri Slaby 	if (!IS_ERR_OR_NULL(driver))
2157ba5db448SJiri Slaby 		tty_driver_kref_put(driver);
2158ba5db448SJiri Slaby err_file:
2159ba5db448SJiri Slaby 	tty_free_file(filp);
2160ba5db448SJiri Slaby 	return retval;
216196fd7ce5SGreg Kroah-Hartman }
216296fd7ce5SGreg Kroah-Hartman 
216396fd7ce5SGreg Kroah-Hartman 
216496fd7ce5SGreg Kroah-Hartman 
216596fd7ce5SGreg Kroah-Hartman /**
216696fd7ce5SGreg Kroah-Hartman  *	tty_poll	-	check tty status
216796fd7ce5SGreg Kroah-Hartman  *	@filp: file being polled
216896fd7ce5SGreg Kroah-Hartman  *	@wait: poll wait structures to update
216996fd7ce5SGreg Kroah-Hartman  *
217096fd7ce5SGreg Kroah-Hartman  *	Call the line discipline polling method to obtain the poll
217196fd7ce5SGreg Kroah-Hartman  *	status of the device.
217296fd7ce5SGreg Kroah-Hartman  *
217396fd7ce5SGreg Kroah-Hartman  *	Locking: locks called line discipline but ldisc poll method
217496fd7ce5SGreg Kroah-Hartman  *	may be re-entered freely by other callers.
217596fd7ce5SGreg Kroah-Hartman  */
217696fd7ce5SGreg Kroah-Hartman 
217796fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *filp, poll_table *wait)
217896fd7ce5SGreg Kroah-Hartman {
217996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
218096fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
218196fd7ce5SGreg Kroah-Hartman 	int ret = 0;
218296fd7ce5SGreg Kroah-Hartman 
21836131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
218496fd7ce5SGreg Kroah-Hartman 		return 0;
218596fd7ce5SGreg Kroah-Hartman 
218696fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
218796fd7ce5SGreg Kroah-Hartman 	if (ld->ops->poll)
2188c961bfb1SPeter Hurley 		ret = ld->ops->poll(tty, filp, wait);
218996fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
219096fd7ce5SGreg Kroah-Hartman 	return ret;
219196fd7ce5SGreg Kroah-Hartman }
219296fd7ce5SGreg Kroah-Hartman 
219396fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on)
219496fd7ce5SGreg Kroah-Hartman {
219596fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
2196f6c8dbe6SPeter Hurley 	struct tty_ldisc *ldisc;
219796fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
219896fd7ce5SGreg Kroah-Hartman 	int retval = 0;
219996fd7ce5SGreg Kroah-Hartman 
22006131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
220196fd7ce5SGreg Kroah-Hartman 		goto out;
220296fd7ce5SGreg Kroah-Hartman 
220396fd7ce5SGreg Kroah-Hartman 	retval = fasync_helper(fd, filp, on, &tty->fasync);
220496fd7ce5SGreg Kroah-Hartman 	if (retval <= 0)
220596fd7ce5SGreg Kroah-Hartman 		goto out;
220696fd7ce5SGreg Kroah-Hartman 
2207f6c8dbe6SPeter Hurley 	ldisc = tty_ldisc_ref(tty);
2208f6c8dbe6SPeter Hurley 	if (ldisc) {
2209f6c8dbe6SPeter Hurley 		if (ldisc->ops->fasync)
2210f6c8dbe6SPeter Hurley 			ldisc->ops->fasync(tty, on);
2211f6c8dbe6SPeter Hurley 		tty_ldisc_deref(ldisc);
2212f6c8dbe6SPeter Hurley 	}
2213f6c8dbe6SPeter Hurley 
221496fd7ce5SGreg Kroah-Hartman 	if (on) {
221596fd7ce5SGreg Kroah-Hartman 		enum pid_type type;
221696fd7ce5SGreg Kroah-Hartman 		struct pid *pid;
2217f6c8dbe6SPeter Hurley 
221896fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&tty->ctrl_lock, flags);
221996fd7ce5SGreg Kroah-Hartman 		if (tty->pgrp) {
222096fd7ce5SGreg Kroah-Hartman 			pid = tty->pgrp;
222196fd7ce5SGreg Kroah-Hartman 			type = PIDTYPE_PGID;
222296fd7ce5SGreg Kroah-Hartman 		} else {
222396fd7ce5SGreg Kroah-Hartman 			pid = task_pid(current);
222496fd7ce5SGreg Kroah-Hartman 			type = PIDTYPE_PID;
222596fd7ce5SGreg Kroah-Hartman 		}
222696fd7ce5SGreg Kroah-Hartman 		get_pid(pid);
222796fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2228e0b93eddSJeff Layton 		__f_setown(filp, pid, type, 0);
222996fd7ce5SGreg Kroah-Hartman 		put_pid(pid);
2230e0b93eddSJeff Layton 		retval = 0;
223196fd7ce5SGreg Kroah-Hartman 	}
223296fd7ce5SGreg Kroah-Hartman out:
223396fd7ce5SGreg Kroah-Hartman 	return retval;
223496fd7ce5SGreg Kroah-Hartman }
223596fd7ce5SGreg Kroah-Hartman 
223696fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on)
223796fd7ce5SGreg Kroah-Hartman {
223889c8d91eSAlan Cox 	struct tty_struct *tty = file_tty(filp);
223996fd7ce5SGreg Kroah-Hartman 	int retval;
224089c8d91eSAlan Cox 
224189c8d91eSAlan Cox 	tty_lock(tty);
224296fd7ce5SGreg Kroah-Hartman 	retval = __tty_fasync(fd, filp, on);
224389c8d91eSAlan Cox 	tty_unlock(tty);
224489c8d91eSAlan Cox 
224596fd7ce5SGreg Kroah-Hartman 	return retval;
224696fd7ce5SGreg Kroah-Hartman }
224796fd7ce5SGreg Kroah-Hartman 
224896fd7ce5SGreg Kroah-Hartman /**
224996fd7ce5SGreg Kroah-Hartman  *	tiocsti			-	fake input character
225096fd7ce5SGreg Kroah-Hartman  *	@tty: tty to fake input into
225196fd7ce5SGreg Kroah-Hartman  *	@p: pointer to character
225296fd7ce5SGreg Kroah-Hartman  *
225396fd7ce5SGreg Kroah-Hartman  *	Fake input to a tty device. Does the necessary locking and
225496fd7ce5SGreg Kroah-Hartman  *	input management.
225596fd7ce5SGreg Kroah-Hartman  *
225696fd7ce5SGreg Kroah-Hartman  *	FIXME: does not honour flow control ??
225796fd7ce5SGreg Kroah-Hartman  *
225896fd7ce5SGreg Kroah-Hartman  *	Locking:
2259137084bbSPeter Hurley  *		Called functions take tty_ldiscs_lock
226096fd7ce5SGreg Kroah-Hartman  *		current->signal->tty check is safe without locks
226196fd7ce5SGreg Kroah-Hartman  *
226296fd7ce5SGreg Kroah-Hartman  *	FIXME: may race normal receive processing
226396fd7ce5SGreg Kroah-Hartman  */
226496fd7ce5SGreg Kroah-Hartman 
226596fd7ce5SGreg Kroah-Hartman static int tiocsti(struct tty_struct *tty, char __user *p)
226696fd7ce5SGreg Kroah-Hartman {
226796fd7ce5SGreg Kroah-Hartman 	char ch, mbz = 0;
226896fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
226996fd7ce5SGreg Kroah-Hartman 
227096fd7ce5SGreg Kroah-Hartman 	if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
227196fd7ce5SGreg Kroah-Hartman 		return -EPERM;
227296fd7ce5SGreg Kroah-Hartman 	if (get_user(ch, p))
227396fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
227496fd7ce5SGreg Kroah-Hartman 	tty_audit_tiocsti(tty, ch);
227596fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
227696fd7ce5SGreg Kroah-Hartman 	ld->ops->receive_buf(tty, &ch, &mbz, 1);
227796fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
227896fd7ce5SGreg Kroah-Hartman 	return 0;
227996fd7ce5SGreg Kroah-Hartman }
228096fd7ce5SGreg Kroah-Hartman 
228196fd7ce5SGreg Kroah-Hartman /**
228296fd7ce5SGreg Kroah-Hartman  *	tiocgwinsz		-	implement window query ioctl
228396fd7ce5SGreg Kroah-Hartman  *	@tty; tty
228496fd7ce5SGreg Kroah-Hartman  *	@arg: user buffer for result
228596fd7ce5SGreg Kroah-Hartman  *
228696fd7ce5SGreg Kroah-Hartman  *	Copies the kernel idea of the window size into the user buffer.
228796fd7ce5SGreg Kroah-Hartman  *
2288dee4a0beSPeter Hurley  *	Locking: tty->winsize_mutex is taken to ensure the winsize data
228996fd7ce5SGreg Kroah-Hartman  *		is consistent.
229096fd7ce5SGreg Kroah-Hartman  */
229196fd7ce5SGreg Kroah-Hartman 
229296fd7ce5SGreg Kroah-Hartman static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
229396fd7ce5SGreg Kroah-Hartman {
229496fd7ce5SGreg Kroah-Hartman 	int err;
229596fd7ce5SGreg Kroah-Hartman 
2296dee4a0beSPeter Hurley 	mutex_lock(&tty->winsize_mutex);
229796fd7ce5SGreg Kroah-Hartman 	err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2298dee4a0beSPeter Hurley 	mutex_unlock(&tty->winsize_mutex);
229996fd7ce5SGreg Kroah-Hartman 
230096fd7ce5SGreg Kroah-Hartman 	return err ? -EFAULT: 0;
230196fd7ce5SGreg Kroah-Hartman }
230296fd7ce5SGreg Kroah-Hartman 
230396fd7ce5SGreg Kroah-Hartman /**
230496fd7ce5SGreg Kroah-Hartman  *	tty_do_resize		-	resize event
230596fd7ce5SGreg Kroah-Hartman  *	@tty: tty being resized
230696fd7ce5SGreg Kroah-Hartman  *	@rows: rows (character)
230796fd7ce5SGreg Kroah-Hartman  *	@cols: cols (character)
230896fd7ce5SGreg Kroah-Hartman  *
230996fd7ce5SGreg Kroah-Hartman  *	Update the termios variables and send the necessary signals to
231096fd7ce5SGreg Kroah-Hartman  *	peform a terminal resize correctly
231196fd7ce5SGreg Kroah-Hartman  */
231296fd7ce5SGreg Kroah-Hartman 
231396fd7ce5SGreg Kroah-Hartman int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
231496fd7ce5SGreg Kroah-Hartman {
231596fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
231696fd7ce5SGreg Kroah-Hartman 
231796fd7ce5SGreg Kroah-Hartman 	/* Lock the tty */
2318dee4a0beSPeter Hurley 	mutex_lock(&tty->winsize_mutex);
231996fd7ce5SGreg Kroah-Hartman 	if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
232096fd7ce5SGreg Kroah-Hartman 		goto done;
232196fd7ce5SGreg Kroah-Hartman 
23225b239542SPeter Hurley 	/* Signal the foreground process group */
23235b239542SPeter Hurley 	pgrp = tty_get_pgrp(tty);
232496fd7ce5SGreg Kroah-Hartman 	if (pgrp)
232596fd7ce5SGreg Kroah-Hartman 		kill_pgrp(pgrp, SIGWINCH, 1);
232696fd7ce5SGreg Kroah-Hartman 	put_pid(pgrp);
232796fd7ce5SGreg Kroah-Hartman 
232896fd7ce5SGreg Kroah-Hartman 	tty->winsize = *ws;
232996fd7ce5SGreg Kroah-Hartman done:
2330dee4a0beSPeter Hurley 	mutex_unlock(&tty->winsize_mutex);
233196fd7ce5SGreg Kroah-Hartman 	return 0;
233296fd7ce5SGreg Kroah-Hartman }
23334d334fd1SMartin Schwidefsky EXPORT_SYMBOL(tty_do_resize);
233496fd7ce5SGreg Kroah-Hartman 
233596fd7ce5SGreg Kroah-Hartman /**
233696fd7ce5SGreg Kroah-Hartman  *	tiocswinsz		-	implement window size set ioctl
233796fd7ce5SGreg Kroah-Hartman  *	@tty; tty side of tty
233896fd7ce5SGreg Kroah-Hartman  *	@arg: user buffer for result
233996fd7ce5SGreg Kroah-Hartman  *
234096fd7ce5SGreg Kroah-Hartman  *	Copies the user idea of the window size to the kernel. Traditionally
234196fd7ce5SGreg Kroah-Hartman  *	this is just advisory information but for the Linux console it
234296fd7ce5SGreg Kroah-Hartman  *	actually has driver level meaning and triggers a VC resize.
234396fd7ce5SGreg Kroah-Hartman  *
234496fd7ce5SGreg Kroah-Hartman  *	Locking:
234525985edcSLucas De Marchi  *		Driver dependent. The default do_resize method takes the
234696fd7ce5SGreg Kroah-Hartman  *	tty termios mutex and ctrl_lock. The console takes its own lock
234796fd7ce5SGreg Kroah-Hartman  *	then calls into the default method.
234896fd7ce5SGreg Kroah-Hartman  */
234996fd7ce5SGreg Kroah-Hartman 
235096fd7ce5SGreg Kroah-Hartman static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
235196fd7ce5SGreg Kroah-Hartman {
235296fd7ce5SGreg Kroah-Hartman 	struct winsize tmp_ws;
235396fd7ce5SGreg Kroah-Hartman 	if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
235496fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
235596fd7ce5SGreg Kroah-Hartman 
235696fd7ce5SGreg Kroah-Hartman 	if (tty->ops->resize)
235796fd7ce5SGreg Kroah-Hartman 		return tty->ops->resize(tty, &tmp_ws);
235896fd7ce5SGreg Kroah-Hartman 	else
235996fd7ce5SGreg Kroah-Hartman 		return tty_do_resize(tty, &tmp_ws);
236096fd7ce5SGreg Kroah-Hartman }
236196fd7ce5SGreg Kroah-Hartman 
236296fd7ce5SGreg Kroah-Hartman /**
236396fd7ce5SGreg Kroah-Hartman  *	tioccons	-	allow admin to move logical console
236496fd7ce5SGreg Kroah-Hartman  *	@file: the file to become console
236596fd7ce5SGreg Kroah-Hartman  *
236625985edcSLucas De Marchi  *	Allow the administrator to move the redirected console device
236796fd7ce5SGreg Kroah-Hartman  *
236896fd7ce5SGreg Kroah-Hartman  *	Locking: uses redirect_lock to guard the redirect information
236996fd7ce5SGreg Kroah-Hartman  */
237096fd7ce5SGreg Kroah-Hartman 
237196fd7ce5SGreg Kroah-Hartman static int tioccons(struct file *file)
237296fd7ce5SGreg Kroah-Hartman {
237396fd7ce5SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN))
237496fd7ce5SGreg Kroah-Hartman 		return -EPERM;
237596fd7ce5SGreg Kroah-Hartman 	if (file->f_op->write == redirected_tty_write) {
237696fd7ce5SGreg Kroah-Hartman 		struct file *f;
237796fd7ce5SGreg Kroah-Hartman 		spin_lock(&redirect_lock);
237896fd7ce5SGreg Kroah-Hartman 		f = redirect;
237996fd7ce5SGreg Kroah-Hartman 		redirect = NULL;
238096fd7ce5SGreg Kroah-Hartman 		spin_unlock(&redirect_lock);
238196fd7ce5SGreg Kroah-Hartman 		if (f)
238296fd7ce5SGreg Kroah-Hartman 			fput(f);
238396fd7ce5SGreg Kroah-Hartman 		return 0;
238496fd7ce5SGreg Kroah-Hartman 	}
238596fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
238696fd7ce5SGreg Kroah-Hartman 	if (redirect) {
238796fd7ce5SGreg Kroah-Hartman 		spin_unlock(&redirect_lock);
238896fd7ce5SGreg Kroah-Hartman 		return -EBUSY;
238996fd7ce5SGreg Kroah-Hartman 	}
2390cb0942b8SAl Viro 	redirect = get_file(file);
239196fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
239296fd7ce5SGreg Kroah-Hartman 	return 0;
239396fd7ce5SGreg Kroah-Hartman }
239496fd7ce5SGreg Kroah-Hartman 
239596fd7ce5SGreg Kroah-Hartman /**
239696fd7ce5SGreg Kroah-Hartman  *	fionbio		-	non blocking ioctl
239796fd7ce5SGreg Kroah-Hartman  *	@file: file to set blocking value
239896fd7ce5SGreg Kroah-Hartman  *	@p: user parameter
239996fd7ce5SGreg Kroah-Hartman  *
240096fd7ce5SGreg Kroah-Hartman  *	Historical tty interfaces had a blocking control ioctl before
240196fd7ce5SGreg Kroah-Hartman  *	the generic functionality existed. This piece of history is preserved
240296fd7ce5SGreg Kroah-Hartman  *	in the expected tty API of posix OS's.
240396fd7ce5SGreg Kroah-Hartman  *
240496fd7ce5SGreg Kroah-Hartman  *	Locking: none, the open file handle ensures it won't go away.
240596fd7ce5SGreg Kroah-Hartman  */
240696fd7ce5SGreg Kroah-Hartman 
240796fd7ce5SGreg Kroah-Hartman static int fionbio(struct file *file, int __user *p)
240896fd7ce5SGreg Kroah-Hartman {
240996fd7ce5SGreg Kroah-Hartman 	int nonblock;
241096fd7ce5SGreg Kroah-Hartman 
241196fd7ce5SGreg Kroah-Hartman 	if (get_user(nonblock, p))
241296fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
241396fd7ce5SGreg Kroah-Hartman 
241496fd7ce5SGreg Kroah-Hartman 	spin_lock(&file->f_lock);
241596fd7ce5SGreg Kroah-Hartman 	if (nonblock)
241696fd7ce5SGreg Kroah-Hartman 		file->f_flags |= O_NONBLOCK;
241796fd7ce5SGreg Kroah-Hartman 	else
241896fd7ce5SGreg Kroah-Hartman 		file->f_flags &= ~O_NONBLOCK;
241996fd7ce5SGreg Kroah-Hartman 	spin_unlock(&file->f_lock);
242096fd7ce5SGreg Kroah-Hartman 	return 0;
242196fd7ce5SGreg Kroah-Hartman }
242296fd7ce5SGreg Kroah-Hartman 
242396fd7ce5SGreg Kroah-Hartman /**
242496fd7ce5SGreg Kroah-Hartman  *	tiocsctty	-	set controlling tty
242596fd7ce5SGreg Kroah-Hartman  *	@tty: tty structure
242696fd7ce5SGreg Kroah-Hartman  *	@arg: user argument
242796fd7ce5SGreg Kroah-Hartman  *
242896fd7ce5SGreg Kroah-Hartman  *	This ioctl is used to manage job control. It permits a session
242996fd7ce5SGreg Kroah-Hartman  *	leader to set this tty as the controlling tty for the session.
243096fd7ce5SGreg Kroah-Hartman  *
243196fd7ce5SGreg Kroah-Hartman  *	Locking:
2432e218eb32SPeter Hurley  *		Takes tty_lock() to serialize proc_set_tty() for this tty
243396fd7ce5SGreg Kroah-Hartman  *		Takes tasklist_lock internally to walk sessions
243496fd7ce5SGreg Kroah-Hartman  *		Takes ->siglock() when updating signal->tty
243596fd7ce5SGreg Kroah-Hartman  */
243696fd7ce5SGreg Kroah-Hartman 
24370c556271SJann Horn static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
243896fd7ce5SGreg Kroah-Hartman {
243996fd7ce5SGreg Kroah-Hartman 	int ret = 0;
244096fd7ce5SGreg Kroah-Hartman 
2441e218eb32SPeter Hurley 	tty_lock(tty);
24422c411c11SPeter Hurley 	read_lock(&tasklist_lock);
24432c411c11SPeter Hurley 
24442c411c11SPeter Hurley 	if (current->signal->leader && (task_session(current) == tty->session))
24452c411c11SPeter Hurley 		goto unlock;
24462c411c11SPeter Hurley 
244796fd7ce5SGreg Kroah-Hartman 	/*
244896fd7ce5SGreg Kroah-Hartman 	 * The process must be a session leader and
244996fd7ce5SGreg Kroah-Hartman 	 * not have a controlling tty already.
245096fd7ce5SGreg Kroah-Hartman 	 */
245196fd7ce5SGreg Kroah-Hartman 	if (!current->signal->leader || current->signal->tty) {
245296fd7ce5SGreg Kroah-Hartman 		ret = -EPERM;
245396fd7ce5SGreg Kroah-Hartman 		goto unlock;
245496fd7ce5SGreg Kroah-Hartman 	}
245596fd7ce5SGreg Kroah-Hartman 
245696fd7ce5SGreg Kroah-Hartman 	if (tty->session) {
245796fd7ce5SGreg Kroah-Hartman 		/*
245896fd7ce5SGreg Kroah-Hartman 		 * This tty is already the controlling
245996fd7ce5SGreg Kroah-Hartman 		 * tty for another session group!
246096fd7ce5SGreg Kroah-Hartman 		 */
246196fd7ce5SGreg Kroah-Hartman 		if (arg == 1 && capable(CAP_SYS_ADMIN)) {
246296fd7ce5SGreg Kroah-Hartman 			/*
246396fd7ce5SGreg Kroah-Hartman 			 * Steal it away
246496fd7ce5SGreg Kroah-Hartman 			 */
246596fd7ce5SGreg Kroah-Hartman 			session_clear_tty(tty->session);
246696fd7ce5SGreg Kroah-Hartman 		} else {
246796fd7ce5SGreg Kroah-Hartman 			ret = -EPERM;
246896fd7ce5SGreg Kroah-Hartman 			goto unlock;
246996fd7ce5SGreg Kroah-Hartman 		}
247096fd7ce5SGreg Kroah-Hartman 	}
24710c556271SJann Horn 
24720c556271SJann Horn 	/* See the comment in tty_open(). */
24730c556271SJann Horn 	if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
24740c556271SJann Horn 		ret = -EPERM;
24750c556271SJann Horn 		goto unlock;
24760c556271SJann Horn 	}
24770c556271SJann Horn 
2478bce65f18SPeter Hurley 	proc_set_tty(tty);
247996fd7ce5SGreg Kroah-Hartman unlock:
24802c411c11SPeter Hurley 	read_unlock(&tasklist_lock);
2481e218eb32SPeter Hurley 	tty_unlock(tty);
248296fd7ce5SGreg Kroah-Hartman 	return ret;
248396fd7ce5SGreg Kroah-Hartman }
248496fd7ce5SGreg Kroah-Hartman 
248596fd7ce5SGreg Kroah-Hartman /**
248696fd7ce5SGreg Kroah-Hartman  *	tty_get_pgrp	-	return a ref counted pgrp pid
248796fd7ce5SGreg Kroah-Hartman  *	@tty: tty to read
248896fd7ce5SGreg Kroah-Hartman  *
248996fd7ce5SGreg Kroah-Hartman  *	Returns a refcounted instance of the pid struct for the process
249096fd7ce5SGreg Kroah-Hartman  *	group controlling the tty.
249196fd7ce5SGreg Kroah-Hartman  */
249296fd7ce5SGreg Kroah-Hartman 
249396fd7ce5SGreg Kroah-Hartman struct pid *tty_get_pgrp(struct tty_struct *tty)
249496fd7ce5SGreg Kroah-Hartman {
249596fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
249696fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
249796fd7ce5SGreg Kroah-Hartman 
249896fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
249996fd7ce5SGreg Kroah-Hartman 	pgrp = get_pid(tty->pgrp);
250096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
250196fd7ce5SGreg Kroah-Hartman 
250296fd7ce5SGreg Kroah-Hartman 	return pgrp;
250396fd7ce5SGreg Kroah-Hartman }
250496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_get_pgrp);
250596fd7ce5SGreg Kroah-Hartman 
2506e1c2296cSPeter Hurley /*
2507e1c2296cSPeter Hurley  * This checks not only the pgrp, but falls back on the pid if no
2508e1c2296cSPeter Hurley  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
2509e1c2296cSPeter Hurley  * without this...
2510e1c2296cSPeter Hurley  *
2511e1c2296cSPeter Hurley  * The caller must hold rcu lock or the tasklist lock.
2512e1c2296cSPeter Hurley  */
2513e1c2296cSPeter Hurley static struct pid *session_of_pgrp(struct pid *pgrp)
2514e1c2296cSPeter Hurley {
2515e1c2296cSPeter Hurley 	struct task_struct *p;
2516e1c2296cSPeter Hurley 	struct pid *sid = NULL;
2517e1c2296cSPeter Hurley 
2518e1c2296cSPeter Hurley 	p = pid_task(pgrp, PIDTYPE_PGID);
2519e1c2296cSPeter Hurley 	if (p == NULL)
2520e1c2296cSPeter Hurley 		p = pid_task(pgrp, PIDTYPE_PID);
2521e1c2296cSPeter Hurley 	if (p != NULL)
2522e1c2296cSPeter Hurley 		sid = task_session(p);
2523e1c2296cSPeter Hurley 
2524e1c2296cSPeter Hurley 	return sid;
2525e1c2296cSPeter Hurley }
2526e1c2296cSPeter Hurley 
252796fd7ce5SGreg Kroah-Hartman /**
252896fd7ce5SGreg Kroah-Hartman  *	tiocgpgrp		-	get process group
252996fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
253025985edcSLucas De Marchi  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
253196fd7ce5SGreg Kroah-Hartman  *	@p: returned pid
253296fd7ce5SGreg Kroah-Hartman  *
253396fd7ce5SGreg Kroah-Hartman  *	Obtain the process group of the tty. If there is no process group
253496fd7ce5SGreg Kroah-Hartman  *	return an error.
253596fd7ce5SGreg Kroah-Hartman  *
253696fd7ce5SGreg Kroah-Hartman  *	Locking: none. Reference to current->signal->tty is safe.
253796fd7ce5SGreg Kroah-Hartman  */
253896fd7ce5SGreg Kroah-Hartman 
253996fd7ce5SGreg Kroah-Hartman static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
254096fd7ce5SGreg Kroah-Hartman {
254196fd7ce5SGreg Kroah-Hartman 	struct pid *pid;
254296fd7ce5SGreg Kroah-Hartman 	int ret;
254396fd7ce5SGreg Kroah-Hartman 	/*
254496fd7ce5SGreg Kroah-Hartman 	 * (tty == real_tty) is a cheap way of
254596fd7ce5SGreg Kroah-Hartman 	 * testing if the tty is NOT a master pty.
254696fd7ce5SGreg Kroah-Hartman 	 */
254796fd7ce5SGreg Kroah-Hartman 	if (tty == real_tty && current->signal->tty != real_tty)
254896fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
254996fd7ce5SGreg Kroah-Hartman 	pid = tty_get_pgrp(real_tty);
255096fd7ce5SGreg Kroah-Hartman 	ret =  put_user(pid_vnr(pid), p);
255196fd7ce5SGreg Kroah-Hartman 	put_pid(pid);
255296fd7ce5SGreg Kroah-Hartman 	return ret;
255396fd7ce5SGreg Kroah-Hartman }
255496fd7ce5SGreg Kroah-Hartman 
255596fd7ce5SGreg Kroah-Hartman /**
255696fd7ce5SGreg Kroah-Hartman  *	tiocspgrp		-	attempt to set process group
255796fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
255896fd7ce5SGreg Kroah-Hartman  *	@real_tty: tty side device matching tty passed by user
255996fd7ce5SGreg Kroah-Hartman  *	@p: pid pointer
256096fd7ce5SGreg Kroah-Hartman  *
256196fd7ce5SGreg Kroah-Hartman  *	Set the process group of the tty to the session passed. Only
256296fd7ce5SGreg Kroah-Hartman  *	permitted where the tty session is our session.
256396fd7ce5SGreg Kroah-Hartman  *
256496fd7ce5SGreg Kroah-Hartman  *	Locking: RCU, ctrl lock
256596fd7ce5SGreg Kroah-Hartman  */
256696fd7ce5SGreg Kroah-Hartman 
256796fd7ce5SGreg Kroah-Hartman static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
256896fd7ce5SGreg Kroah-Hartman {
256996fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
257096fd7ce5SGreg Kroah-Hartman 	pid_t pgrp_nr;
257196fd7ce5SGreg Kroah-Hartman 	int retval = tty_check_change(real_tty);
257296fd7ce5SGreg Kroah-Hartman 
257396fd7ce5SGreg Kroah-Hartman 	if (retval == -EIO)
257496fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
257596fd7ce5SGreg Kroah-Hartman 	if (retval)
257696fd7ce5SGreg Kroah-Hartman 		return retval;
257796fd7ce5SGreg Kroah-Hartman 	if (!current->signal->tty ||
257896fd7ce5SGreg Kroah-Hartman 	    (current->signal->tty != real_tty) ||
257996fd7ce5SGreg Kroah-Hartman 	    (real_tty->session != task_session(current)))
258096fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
258196fd7ce5SGreg Kroah-Hartman 	if (get_user(pgrp_nr, p))
258296fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
258396fd7ce5SGreg Kroah-Hartman 	if (pgrp_nr < 0)
258496fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
258596fd7ce5SGreg Kroah-Hartman 	rcu_read_lock();
258696fd7ce5SGreg Kroah-Hartman 	pgrp = find_vpid(pgrp_nr);
258796fd7ce5SGreg Kroah-Hartman 	retval = -ESRCH;
258896fd7ce5SGreg Kroah-Hartman 	if (!pgrp)
258996fd7ce5SGreg Kroah-Hartman 		goto out_unlock;
259096fd7ce5SGreg Kroah-Hartman 	retval = -EPERM;
259196fd7ce5SGreg Kroah-Hartman 	if (session_of_pgrp(pgrp) != task_session(current))
259296fd7ce5SGreg Kroah-Hartman 		goto out_unlock;
259396fd7ce5SGreg Kroah-Hartman 	retval = 0;
25941e86b5bfSPeter Hurley 	spin_lock_irq(&tty->ctrl_lock);
259596fd7ce5SGreg Kroah-Hartman 	put_pid(real_tty->pgrp);
259696fd7ce5SGreg Kroah-Hartman 	real_tty->pgrp = get_pid(pgrp);
25971e86b5bfSPeter Hurley 	spin_unlock_irq(&tty->ctrl_lock);
259896fd7ce5SGreg Kroah-Hartman out_unlock:
259996fd7ce5SGreg Kroah-Hartman 	rcu_read_unlock();
260096fd7ce5SGreg Kroah-Hartman 	return retval;
260196fd7ce5SGreg Kroah-Hartman }
260296fd7ce5SGreg Kroah-Hartman 
260396fd7ce5SGreg Kroah-Hartman /**
260496fd7ce5SGreg Kroah-Hartman  *	tiocgsid		-	get session id
260596fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
260625985edcSLucas De Marchi  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
260796fd7ce5SGreg Kroah-Hartman  *	@p: pointer to returned session id
260896fd7ce5SGreg Kroah-Hartman  *
260996fd7ce5SGreg Kroah-Hartman  *	Obtain the session id of the tty. If there is no session
261096fd7ce5SGreg Kroah-Hartman  *	return an error.
261196fd7ce5SGreg Kroah-Hartman  *
261296fd7ce5SGreg Kroah-Hartman  *	Locking: none. Reference to current->signal->tty is safe.
261396fd7ce5SGreg Kroah-Hartman  */
261496fd7ce5SGreg Kroah-Hartman 
261596fd7ce5SGreg Kroah-Hartman static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
261696fd7ce5SGreg Kroah-Hartman {
261796fd7ce5SGreg Kroah-Hartman 	/*
261896fd7ce5SGreg Kroah-Hartman 	 * (tty == real_tty) is a cheap way of
261996fd7ce5SGreg Kroah-Hartman 	 * testing if the tty is NOT a master pty.
262096fd7ce5SGreg Kroah-Hartman 	*/
262196fd7ce5SGreg Kroah-Hartman 	if (tty == real_tty && current->signal->tty != real_tty)
262296fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
262396fd7ce5SGreg Kroah-Hartman 	if (!real_tty->session)
262496fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
262596fd7ce5SGreg Kroah-Hartman 	return put_user(pid_vnr(real_tty->session), p);
262696fd7ce5SGreg Kroah-Hartman }
262796fd7ce5SGreg Kroah-Hartman 
262896fd7ce5SGreg Kroah-Hartman /**
262996fd7ce5SGreg Kroah-Hartman  *	tiocsetd	-	set line discipline
263096fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
263196fd7ce5SGreg Kroah-Hartman  *	@p: pointer to user data
263296fd7ce5SGreg Kroah-Hartman  *
263396fd7ce5SGreg Kroah-Hartman  *	Set the line discipline according to user request.
263496fd7ce5SGreg Kroah-Hartman  *
263596fd7ce5SGreg Kroah-Hartman  *	Locking: see tty_set_ldisc, this function is just a helper
263696fd7ce5SGreg Kroah-Hartman  */
263796fd7ce5SGreg Kroah-Hartman 
263896fd7ce5SGreg Kroah-Hartman static int tiocsetd(struct tty_struct *tty, int __user *p)
263996fd7ce5SGreg Kroah-Hartman {
264096fd7ce5SGreg Kroah-Hartman 	int ldisc;
264196fd7ce5SGreg Kroah-Hartman 	int ret;
264296fd7ce5SGreg Kroah-Hartman 
264396fd7ce5SGreg Kroah-Hartman 	if (get_user(ldisc, p))
264496fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
264596fd7ce5SGreg Kroah-Hartman 
264696fd7ce5SGreg Kroah-Hartman 	ret = tty_set_ldisc(tty, ldisc);
264796fd7ce5SGreg Kroah-Hartman 
264896fd7ce5SGreg Kroah-Hartman 	return ret;
264996fd7ce5SGreg Kroah-Hartman }
265096fd7ce5SGreg Kroah-Hartman 
265196fd7ce5SGreg Kroah-Hartman /**
265296fd7ce5SGreg Kroah-Hartman  *	send_break	-	performed time break
265396fd7ce5SGreg Kroah-Hartman  *	@tty: device to break on
265496fd7ce5SGreg Kroah-Hartman  *	@duration: timeout in mS
265596fd7ce5SGreg Kroah-Hartman  *
265696fd7ce5SGreg Kroah-Hartman  *	Perform a timed break on hardware that lacks its own driver level
265796fd7ce5SGreg Kroah-Hartman  *	timed break functionality.
265896fd7ce5SGreg Kroah-Hartman  *
265996fd7ce5SGreg Kroah-Hartman  *	Locking:
266096fd7ce5SGreg Kroah-Hartman  *		atomic_write_lock serializes
266196fd7ce5SGreg Kroah-Hartman  *
266296fd7ce5SGreg Kroah-Hartman  */
266396fd7ce5SGreg Kroah-Hartman 
266496fd7ce5SGreg Kroah-Hartman static int send_break(struct tty_struct *tty, unsigned int duration)
266596fd7ce5SGreg Kroah-Hartman {
266696fd7ce5SGreg Kroah-Hartman 	int retval;
266796fd7ce5SGreg Kroah-Hartman 
266896fd7ce5SGreg Kroah-Hartman 	if (tty->ops->break_ctl == NULL)
266996fd7ce5SGreg Kroah-Hartman 		return 0;
267096fd7ce5SGreg Kroah-Hartman 
267196fd7ce5SGreg Kroah-Hartman 	if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
267296fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, duration);
267396fd7ce5SGreg Kroah-Hartman 	else {
267496fd7ce5SGreg Kroah-Hartman 		/* Do the work ourselves */
267596fd7ce5SGreg Kroah-Hartman 		if (tty_write_lock(tty, 0) < 0)
267696fd7ce5SGreg Kroah-Hartman 			return -EINTR;
267796fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, -1);
267896fd7ce5SGreg Kroah-Hartman 		if (retval)
267996fd7ce5SGreg Kroah-Hartman 			goto out;
268096fd7ce5SGreg Kroah-Hartman 		if (!signal_pending(current))
268196fd7ce5SGreg Kroah-Hartman 			msleep_interruptible(duration);
268296fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, 0);
268396fd7ce5SGreg Kroah-Hartman out:
268496fd7ce5SGreg Kroah-Hartman 		tty_write_unlock(tty);
268596fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
268696fd7ce5SGreg Kroah-Hartman 			retval = -EINTR;
268796fd7ce5SGreg Kroah-Hartman 	}
268896fd7ce5SGreg Kroah-Hartman 	return retval;
268996fd7ce5SGreg Kroah-Hartman }
269096fd7ce5SGreg Kroah-Hartman 
269196fd7ce5SGreg Kroah-Hartman /**
269296fd7ce5SGreg Kroah-Hartman  *	tty_tiocmget		-	get modem status
269396fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
269496fd7ce5SGreg Kroah-Hartman  *	@file: user file pointer
269596fd7ce5SGreg Kroah-Hartman  *	@p: pointer to result
269696fd7ce5SGreg Kroah-Hartman  *
269796fd7ce5SGreg Kroah-Hartman  *	Obtain the modem status bits from the tty driver if the feature
269896fd7ce5SGreg Kroah-Hartman  *	is supported. Return -EINVAL if it is not available.
269996fd7ce5SGreg Kroah-Hartman  *
270096fd7ce5SGreg Kroah-Hartman  *	Locking: none (up to the driver)
270196fd7ce5SGreg Kroah-Hartman  */
270296fd7ce5SGreg Kroah-Hartman 
270360b33c13SAlan Cox static int tty_tiocmget(struct tty_struct *tty, int __user *p)
270496fd7ce5SGreg Kroah-Hartman {
270596fd7ce5SGreg Kroah-Hartman 	int retval = -EINVAL;
270696fd7ce5SGreg Kroah-Hartman 
270796fd7ce5SGreg Kroah-Hartman 	if (tty->ops->tiocmget) {
270860b33c13SAlan Cox 		retval = tty->ops->tiocmget(tty);
270996fd7ce5SGreg Kroah-Hartman 
271096fd7ce5SGreg Kroah-Hartman 		if (retval >= 0)
271196fd7ce5SGreg Kroah-Hartman 			retval = put_user(retval, p);
271296fd7ce5SGreg Kroah-Hartman 	}
271396fd7ce5SGreg Kroah-Hartman 	return retval;
271496fd7ce5SGreg Kroah-Hartman }
271596fd7ce5SGreg Kroah-Hartman 
271696fd7ce5SGreg Kroah-Hartman /**
271796fd7ce5SGreg Kroah-Hartman  *	tty_tiocmset		-	set modem status
271896fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
271996fd7ce5SGreg Kroah-Hartman  *	@cmd: command - clear bits, set bits or set all
272096fd7ce5SGreg Kroah-Hartman  *	@p: pointer to desired bits
272196fd7ce5SGreg Kroah-Hartman  *
272296fd7ce5SGreg Kroah-Hartman  *	Set the modem status bits from the tty driver if the feature
272396fd7ce5SGreg Kroah-Hartman  *	is supported. Return -EINVAL if it is not available.
272496fd7ce5SGreg Kroah-Hartman  *
272596fd7ce5SGreg Kroah-Hartman  *	Locking: none (up to the driver)
272696fd7ce5SGreg Kroah-Hartman  */
272796fd7ce5SGreg Kroah-Hartman 
272820b9d177SAlan Cox static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
272996fd7ce5SGreg Kroah-Hartman 	     unsigned __user *p)
273096fd7ce5SGreg Kroah-Hartman {
273196fd7ce5SGreg Kroah-Hartman 	int retval;
273296fd7ce5SGreg Kroah-Hartman 	unsigned int set, clear, val;
273396fd7ce5SGreg Kroah-Hartman 
273496fd7ce5SGreg Kroah-Hartman 	if (tty->ops->tiocmset == NULL)
273596fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
273696fd7ce5SGreg Kroah-Hartman 
273796fd7ce5SGreg Kroah-Hartman 	retval = get_user(val, p);
273896fd7ce5SGreg Kroah-Hartman 	if (retval)
273996fd7ce5SGreg Kroah-Hartman 		return retval;
274096fd7ce5SGreg Kroah-Hartman 	set = clear = 0;
274196fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
274296fd7ce5SGreg Kroah-Hartman 	case TIOCMBIS:
274396fd7ce5SGreg Kroah-Hartman 		set = val;
274496fd7ce5SGreg Kroah-Hartman 		break;
274596fd7ce5SGreg Kroah-Hartman 	case TIOCMBIC:
274696fd7ce5SGreg Kroah-Hartman 		clear = val;
274796fd7ce5SGreg Kroah-Hartman 		break;
274896fd7ce5SGreg Kroah-Hartman 	case TIOCMSET:
274996fd7ce5SGreg Kroah-Hartman 		set = val;
275096fd7ce5SGreg Kroah-Hartman 		clear = ~val;
275196fd7ce5SGreg Kroah-Hartman 		break;
275296fd7ce5SGreg Kroah-Hartman 	}
275396fd7ce5SGreg Kroah-Hartman 	set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
275496fd7ce5SGreg Kroah-Hartman 	clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
275520b9d177SAlan Cox 	return tty->ops->tiocmset(tty, set, clear);
275696fd7ce5SGreg Kroah-Hartman }
275796fd7ce5SGreg Kroah-Hartman 
275896fd7ce5SGreg Kroah-Hartman static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
275996fd7ce5SGreg Kroah-Hartman {
276096fd7ce5SGreg Kroah-Hartman 	int retval = -EINVAL;
276196fd7ce5SGreg Kroah-Hartman 	struct serial_icounter_struct icount;
276296fd7ce5SGreg Kroah-Hartman 	memset(&icount, 0, sizeof(icount));
276396fd7ce5SGreg Kroah-Hartman 	if (tty->ops->get_icount)
276496fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->get_icount(tty, &icount);
276596fd7ce5SGreg Kroah-Hartman 	if (retval != 0)
276696fd7ce5SGreg Kroah-Hartman 		return retval;
276796fd7ce5SGreg Kroah-Hartman 	if (copy_to_user(arg, &icount, sizeof(icount)))
276896fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
276996fd7ce5SGreg Kroah-Hartman 	return 0;
277096fd7ce5SGreg Kroah-Hartman }
277196fd7ce5SGreg Kroah-Hartman 
27728a8ae62fSJiri Slaby static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
27738a8ae62fSJiri Slaby {
27748a8ae62fSJiri Slaby 	static DEFINE_RATELIMIT_STATE(depr_flags,
27758a8ae62fSJiri Slaby 			DEFAULT_RATELIMIT_INTERVAL,
27768a8ae62fSJiri Slaby 			DEFAULT_RATELIMIT_BURST);
27778a8ae62fSJiri Slaby 	char comm[TASK_COMM_LEN];
27788a8ae62fSJiri Slaby 	int flags;
27798a8ae62fSJiri Slaby 
27808a8ae62fSJiri Slaby 	if (get_user(flags, &ss->flags))
27818a8ae62fSJiri Slaby 		return;
27828a8ae62fSJiri Slaby 
27838a8ae62fSJiri Slaby 	flags &= ASYNC_DEPRECATED;
27848a8ae62fSJiri Slaby 
27858a8ae62fSJiri Slaby 	if (flags && __ratelimit(&depr_flags))
27868a8ae62fSJiri Slaby 		pr_warning("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
27878a8ae62fSJiri Slaby 				__func__, get_task_comm(comm, current), flags);
27888a8ae62fSJiri Slaby }
27898a8ae62fSJiri Slaby 
27908f166e00SPeter Hurley /*
27918f166e00SPeter Hurley  * if pty, return the slave side (real_tty)
27928f166e00SPeter Hurley  * otherwise, return self
27938f166e00SPeter Hurley  */
27948f166e00SPeter Hurley static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
279596fd7ce5SGreg Kroah-Hartman {
279696fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
279796fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_MASTER)
279896fd7ce5SGreg Kroah-Hartman 		tty = tty->link;
279996fd7ce5SGreg Kroah-Hartman 	return tty;
280096fd7ce5SGreg Kroah-Hartman }
280196fd7ce5SGreg Kroah-Hartman 
280296fd7ce5SGreg Kroah-Hartman /*
280396fd7ce5SGreg Kroah-Hartman  * Split this up, as gcc can choke on it otherwise..
280496fd7ce5SGreg Kroah-Hartman  */
280596fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
280696fd7ce5SGreg Kroah-Hartman {
280796fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
280896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *real_tty;
280996fd7ce5SGreg Kroah-Hartman 	void __user *p = (void __user *)arg;
281096fd7ce5SGreg Kroah-Hartman 	int retval;
281196fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
281296fd7ce5SGreg Kroah-Hartman 
28136131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
281496fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
281596fd7ce5SGreg Kroah-Hartman 
281696fd7ce5SGreg Kroah-Hartman 	real_tty = tty_pair_get_tty(tty);
281796fd7ce5SGreg Kroah-Hartman 
281896fd7ce5SGreg Kroah-Hartman 	/*
281996fd7ce5SGreg Kroah-Hartman 	 * Factor out some common prep work
282096fd7ce5SGreg Kroah-Hartman 	 */
282196fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
282296fd7ce5SGreg Kroah-Hartman 	case TIOCSETD:
282396fd7ce5SGreg Kroah-Hartman 	case TIOCSBRK:
282496fd7ce5SGreg Kroah-Hartman 	case TIOCCBRK:
282596fd7ce5SGreg Kroah-Hartman 	case TCSBRK:
282696fd7ce5SGreg Kroah-Hartman 	case TCSBRKP:
282796fd7ce5SGreg Kroah-Hartman 		retval = tty_check_change(tty);
282896fd7ce5SGreg Kroah-Hartman 		if (retval)
282996fd7ce5SGreg Kroah-Hartman 			return retval;
283096fd7ce5SGreg Kroah-Hartman 		if (cmd != TIOCCBRK) {
283196fd7ce5SGreg Kroah-Hartman 			tty_wait_until_sent(tty, 0);
283296fd7ce5SGreg Kroah-Hartman 			if (signal_pending(current))
283396fd7ce5SGreg Kroah-Hartman 				return -EINTR;
283496fd7ce5SGreg Kroah-Hartman 		}
283596fd7ce5SGreg Kroah-Hartman 		break;
283696fd7ce5SGreg Kroah-Hartman 	}
283796fd7ce5SGreg Kroah-Hartman 
283896fd7ce5SGreg Kroah-Hartman 	/*
283996fd7ce5SGreg Kroah-Hartman 	 *	Now do the stuff.
284096fd7ce5SGreg Kroah-Hartman 	 */
284196fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
284296fd7ce5SGreg Kroah-Hartman 	case TIOCSTI:
284396fd7ce5SGreg Kroah-Hartman 		return tiocsti(tty, p);
284496fd7ce5SGreg Kroah-Hartman 	case TIOCGWINSZ:
284596fd7ce5SGreg Kroah-Hartman 		return tiocgwinsz(real_tty, p);
284696fd7ce5SGreg Kroah-Hartman 	case TIOCSWINSZ:
284796fd7ce5SGreg Kroah-Hartman 		return tiocswinsz(real_tty, p);
284896fd7ce5SGreg Kroah-Hartman 	case TIOCCONS:
284996fd7ce5SGreg Kroah-Hartman 		return real_tty != tty ? -EINVAL : tioccons(file);
285096fd7ce5SGreg Kroah-Hartman 	case FIONBIO:
285196fd7ce5SGreg Kroah-Hartman 		return fionbio(file, p);
285296fd7ce5SGreg Kroah-Hartman 	case TIOCEXCL:
285396fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_EXCLUSIVE, &tty->flags);
285496fd7ce5SGreg Kroah-Hartman 		return 0;
285596fd7ce5SGreg Kroah-Hartman 	case TIOCNXCL:
285696fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_EXCLUSIVE, &tty->flags);
285796fd7ce5SGreg Kroah-Hartman 		return 0;
285884fd7bdfSCyrill Gorcunov 	case TIOCGEXCL:
285984fd7bdfSCyrill Gorcunov 	{
286084fd7bdfSCyrill Gorcunov 		int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
286184fd7bdfSCyrill Gorcunov 		return put_user(excl, (int __user *)p);
286284fd7bdfSCyrill Gorcunov 	}
286396fd7ce5SGreg Kroah-Hartman 	case TIOCNOTTY:
286496fd7ce5SGreg Kroah-Hartman 		if (current->signal->tty != tty)
286596fd7ce5SGreg Kroah-Hartman 			return -ENOTTY;
286696fd7ce5SGreg Kroah-Hartman 		no_tty();
286796fd7ce5SGreg Kroah-Hartman 		return 0;
286896fd7ce5SGreg Kroah-Hartman 	case TIOCSCTTY:
28690c556271SJann Horn 		return tiocsctty(tty, file, arg);
287096fd7ce5SGreg Kroah-Hartman 	case TIOCGPGRP:
287196fd7ce5SGreg Kroah-Hartman 		return tiocgpgrp(tty, real_tty, p);
287296fd7ce5SGreg Kroah-Hartman 	case TIOCSPGRP:
287396fd7ce5SGreg Kroah-Hartman 		return tiocspgrp(tty, real_tty, p);
287496fd7ce5SGreg Kroah-Hartman 	case TIOCGSID:
287596fd7ce5SGreg Kroah-Hartman 		return tiocgsid(tty, real_tty, p);
287696fd7ce5SGreg Kroah-Hartman 	case TIOCGETD:
287796fd7ce5SGreg Kroah-Hartman 		return put_user(tty->ldisc->ops->num, (int __user *)p);
287896fd7ce5SGreg Kroah-Hartman 	case TIOCSETD:
287996fd7ce5SGreg Kroah-Hartman 		return tiocsetd(tty, p);
28803c95c985SKay Sievers 	case TIOCVHANGUP:
28813c95c985SKay Sievers 		if (!capable(CAP_SYS_ADMIN))
28823c95c985SKay Sievers 			return -EPERM;
28833c95c985SKay Sievers 		tty_vhangup(tty);
28843c95c985SKay Sievers 		return 0;
2885b7b8de08SWerner Fink 	case TIOCGDEV:
2886b7b8de08SWerner Fink 	{
2887b7b8de08SWerner Fink 		unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2888b7b8de08SWerner Fink 		return put_user(ret, (unsigned int __user *)p);
2889b7b8de08SWerner Fink 	}
289096fd7ce5SGreg Kroah-Hartman 	/*
289196fd7ce5SGreg Kroah-Hartman 	 * Break handling
289296fd7ce5SGreg Kroah-Hartman 	 */
289396fd7ce5SGreg Kroah-Hartman 	case TIOCSBRK:	/* Turn break on, unconditionally */
289496fd7ce5SGreg Kroah-Hartman 		if (tty->ops->break_ctl)
289596fd7ce5SGreg Kroah-Hartman 			return tty->ops->break_ctl(tty, -1);
289696fd7ce5SGreg Kroah-Hartman 		return 0;
289796fd7ce5SGreg Kroah-Hartman 	case TIOCCBRK:	/* Turn break off, unconditionally */
289896fd7ce5SGreg Kroah-Hartman 		if (tty->ops->break_ctl)
289996fd7ce5SGreg Kroah-Hartman 			return tty->ops->break_ctl(tty, 0);
290096fd7ce5SGreg Kroah-Hartman 		return 0;
290196fd7ce5SGreg Kroah-Hartman 	case TCSBRK:   /* SVID version: non-zero arg --> no break */
290296fd7ce5SGreg Kroah-Hartman 		/* non-zero arg means wait for all output data
290396fd7ce5SGreg Kroah-Hartman 		 * to be sent (performed above) but don't send break.
290496fd7ce5SGreg Kroah-Hartman 		 * This is used by the tcdrain() termios function.
290596fd7ce5SGreg Kroah-Hartman 		 */
290696fd7ce5SGreg Kroah-Hartman 		if (!arg)
290796fd7ce5SGreg Kroah-Hartman 			return send_break(tty, 250);
290896fd7ce5SGreg Kroah-Hartman 		return 0;
290996fd7ce5SGreg Kroah-Hartman 	case TCSBRKP:	/* support for POSIX tcsendbreak() */
291096fd7ce5SGreg Kroah-Hartman 		return send_break(tty, arg ? arg*100 : 250);
291196fd7ce5SGreg Kroah-Hartman 
291296fd7ce5SGreg Kroah-Hartman 	case TIOCMGET:
291360b33c13SAlan Cox 		return tty_tiocmget(tty, p);
291496fd7ce5SGreg Kroah-Hartman 	case TIOCMSET:
291596fd7ce5SGreg Kroah-Hartman 	case TIOCMBIC:
291696fd7ce5SGreg Kroah-Hartman 	case TIOCMBIS:
291720b9d177SAlan Cox 		return tty_tiocmset(tty, cmd, p);
291896fd7ce5SGreg Kroah-Hartman 	case TIOCGICOUNT:
291996fd7ce5SGreg Kroah-Hartman 		retval = tty_tiocgicount(tty, p);
292096fd7ce5SGreg Kroah-Hartman 		/* For the moment allow fall through to the old method */
292196fd7ce5SGreg Kroah-Hartman         	if (retval != -EINVAL)
292296fd7ce5SGreg Kroah-Hartman 			return retval;
292396fd7ce5SGreg Kroah-Hartman 		break;
292496fd7ce5SGreg Kroah-Hartman 	case TCFLSH:
292596fd7ce5SGreg Kroah-Hartman 		switch (arg) {
292696fd7ce5SGreg Kroah-Hartman 		case TCIFLUSH:
292796fd7ce5SGreg Kroah-Hartman 		case TCIOFLUSH:
292896fd7ce5SGreg Kroah-Hartman 		/* flush tty buffer and allow ldisc to process ioctl */
292986c80a8eSPeter Hurley 			tty_buffer_flush(tty, NULL);
293096fd7ce5SGreg Kroah-Hartman 			break;
293196fd7ce5SGreg Kroah-Hartman 		}
293296fd7ce5SGreg Kroah-Hartman 		break;
29338a8ae62fSJiri Slaby 	case TIOCSSERIAL:
29348a8ae62fSJiri Slaby 		tty_warn_deprecated_flags(p);
29358a8ae62fSJiri Slaby 		break;
293696fd7ce5SGreg Kroah-Hartman 	}
293796fd7ce5SGreg Kroah-Hartman 	if (tty->ops->ioctl) {
2938c961bfb1SPeter Hurley 		retval = tty->ops->ioctl(tty, cmd, arg);
293996fd7ce5SGreg Kroah-Hartman 		if (retval != -ENOIOCTLCMD)
294096fd7ce5SGreg Kroah-Hartman 			return retval;
294196fd7ce5SGreg Kroah-Hartman 	}
294296fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
294396fd7ce5SGreg Kroah-Hartman 	retval = -EINVAL;
294496fd7ce5SGreg Kroah-Hartman 	if (ld->ops->ioctl) {
294596fd7ce5SGreg Kroah-Hartman 		retval = ld->ops->ioctl(tty, file, cmd, arg);
294696fd7ce5SGreg Kroah-Hartman 		if (retval == -ENOIOCTLCMD)
2947bbb63c51SWanlong Gao 			retval = -ENOTTY;
294896fd7ce5SGreg Kroah-Hartman 	}
294996fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
295096fd7ce5SGreg Kroah-Hartman 	return retval;
295196fd7ce5SGreg Kroah-Hartman }
295296fd7ce5SGreg Kroah-Hartman 
295396fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT
295496fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd,
295596fd7ce5SGreg Kroah-Hartman 				unsigned long arg)
295696fd7ce5SGreg Kroah-Hartman {
295796fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
295896fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
295996fd7ce5SGreg Kroah-Hartman 	int retval = -ENOIOCTLCMD;
296096fd7ce5SGreg Kroah-Hartman 
29616131ffaaSAl Viro 	if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
296296fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
296396fd7ce5SGreg Kroah-Hartman 
296496fd7ce5SGreg Kroah-Hartman 	if (tty->ops->compat_ioctl) {
2965c961bfb1SPeter Hurley 		retval = tty->ops->compat_ioctl(tty, cmd, arg);
296696fd7ce5SGreg Kroah-Hartman 		if (retval != -ENOIOCTLCMD)
296796fd7ce5SGreg Kroah-Hartman 			return retval;
296896fd7ce5SGreg Kroah-Hartman 	}
296996fd7ce5SGreg Kroah-Hartman 
297096fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
297196fd7ce5SGreg Kroah-Hartman 	if (ld->ops->compat_ioctl)
297296fd7ce5SGreg Kroah-Hartman 		retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
29738193c429SThomas Meyer 	else
29748193c429SThomas Meyer 		retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
297596fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
297696fd7ce5SGreg Kroah-Hartman 
297796fd7ce5SGreg Kroah-Hartman 	return retval;
297896fd7ce5SGreg Kroah-Hartman }
297996fd7ce5SGreg Kroah-Hartman #endif
298096fd7ce5SGreg Kroah-Hartman 
2981c3c073f8SAl Viro static int this_tty(const void *t, struct file *file, unsigned fd)
2982c3c073f8SAl Viro {
2983c3c073f8SAl Viro 	if (likely(file->f_op->read != tty_read))
2984c3c073f8SAl Viro 		return 0;
2985c3c073f8SAl Viro 	return file_tty(file) != t ? 0 : fd + 1;
2986c3c073f8SAl Viro }
2987c3c073f8SAl Viro 
298896fd7ce5SGreg Kroah-Hartman /*
298996fd7ce5SGreg Kroah-Hartman  * This implements the "Secure Attention Key" ---  the idea is to
299096fd7ce5SGreg Kroah-Hartman  * prevent trojan horses by killing all processes associated with this
299196fd7ce5SGreg Kroah-Hartman  * tty when the user hits the "Secure Attention Key".  Required for
299296fd7ce5SGreg Kroah-Hartman  * super-paranoid applications --- see the Orange Book for more details.
299396fd7ce5SGreg Kroah-Hartman  *
299496fd7ce5SGreg Kroah-Hartman  * This code could be nicer; ideally it should send a HUP, wait a few
299596fd7ce5SGreg Kroah-Hartman  * seconds, then send a INT, and then a KILL signal.  But you then
299696fd7ce5SGreg Kroah-Hartman  * have to coordinate with the init process, since all processes associated
299796fd7ce5SGreg Kroah-Hartman  * with the current tty must be dead before the new getty is allowed
299896fd7ce5SGreg Kroah-Hartman  * to spawn.
299996fd7ce5SGreg Kroah-Hartman  *
300096fd7ce5SGreg Kroah-Hartman  * Now, if it would be correct ;-/ The current code has a nasty hole -
300196fd7ce5SGreg Kroah-Hartman  * it doesn't catch files in flight. We may send the descriptor to ourselves
300296fd7ce5SGreg Kroah-Hartman  * via AF_UNIX socket, close it and later fetch from socket. FIXME.
300396fd7ce5SGreg Kroah-Hartman  *
300496fd7ce5SGreg Kroah-Hartman  * Nasty bug: do_SAK is being called in interrupt context.  This can
300596fd7ce5SGreg Kroah-Hartman  * deadlock.  We punt it up to process context.  AKPM - 16Mar2001
300696fd7ce5SGreg Kroah-Hartman  */
300796fd7ce5SGreg Kroah-Hartman void __do_SAK(struct tty_struct *tty)
300896fd7ce5SGreg Kroah-Hartman {
300996fd7ce5SGreg Kroah-Hartman #ifdef TTY_SOFT_SAK
301096fd7ce5SGreg Kroah-Hartman 	tty_hangup(tty);
301196fd7ce5SGreg Kroah-Hartman #else
301296fd7ce5SGreg Kroah-Hartman 	struct task_struct *g, *p;
301396fd7ce5SGreg Kroah-Hartman 	struct pid *session;
301496fd7ce5SGreg Kroah-Hartman 	int		i;
301596fd7ce5SGreg Kroah-Hartman 
301696fd7ce5SGreg Kroah-Hartman 	if (!tty)
301796fd7ce5SGreg Kroah-Hartman 		return;
301896fd7ce5SGreg Kroah-Hartman 	session = tty->session;
301996fd7ce5SGreg Kroah-Hartman 
302096fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
302196fd7ce5SGreg Kroah-Hartman 
302296fd7ce5SGreg Kroah-Hartman 	tty_driver_flush_buffer(tty);
302396fd7ce5SGreg Kroah-Hartman 
302496fd7ce5SGreg Kroah-Hartman 	read_lock(&tasklist_lock);
302596fd7ce5SGreg Kroah-Hartman 	/* Kill the entire session */
302696fd7ce5SGreg Kroah-Hartman 	do_each_pid_task(session, PIDTYPE_SID, p) {
30279b42bb75SPeter Hurley 		tty_notice(tty, "SAK: killed process %d (%s): by session\n",
302896fd7ce5SGreg Kroah-Hartman 			   task_pid_nr(p), p->comm);
302996fd7ce5SGreg Kroah-Hartman 		send_sig(SIGKILL, p, 1);
303096fd7ce5SGreg Kroah-Hartman 	} while_each_pid_task(session, PIDTYPE_SID, p);
30319b42bb75SPeter Hurley 
30329b42bb75SPeter Hurley 	/* Now kill any processes that happen to have the tty open */
303396fd7ce5SGreg Kroah-Hartman 	do_each_thread(g, p) {
303496fd7ce5SGreg Kroah-Hartman 		if (p->signal->tty == tty) {
30359b42bb75SPeter Hurley 			tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
303696fd7ce5SGreg Kroah-Hartman 				   task_pid_nr(p), p->comm);
303796fd7ce5SGreg Kroah-Hartman 			send_sig(SIGKILL, p, 1);
303896fd7ce5SGreg Kroah-Hartman 			continue;
303996fd7ce5SGreg Kroah-Hartman 		}
304096fd7ce5SGreg Kroah-Hartman 		task_lock(p);
3041c3c073f8SAl Viro 		i = iterate_fd(p->files, 0, this_tty, tty);
3042c3c073f8SAl Viro 		if (i != 0) {
30439b42bb75SPeter Hurley 			tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
3044c3c073f8SAl Viro 				   task_pid_nr(p), p->comm, i - 1);
304596fd7ce5SGreg Kroah-Hartman 			force_sig(SIGKILL, p);
304696fd7ce5SGreg Kroah-Hartman 		}
304796fd7ce5SGreg Kroah-Hartman 		task_unlock(p);
304896fd7ce5SGreg Kroah-Hartman 	} while_each_thread(g, p);
304996fd7ce5SGreg Kroah-Hartman 	read_unlock(&tasklist_lock);
305096fd7ce5SGreg Kroah-Hartman #endif
305196fd7ce5SGreg Kroah-Hartman }
305296fd7ce5SGreg Kroah-Hartman 
305396fd7ce5SGreg Kroah-Hartman static void do_SAK_work(struct work_struct *work)
305496fd7ce5SGreg Kroah-Hartman {
305596fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
305696fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, SAK_work);
305796fd7ce5SGreg Kroah-Hartman 	__do_SAK(tty);
305896fd7ce5SGreg Kroah-Hartman }
305996fd7ce5SGreg Kroah-Hartman 
306096fd7ce5SGreg Kroah-Hartman /*
306196fd7ce5SGreg Kroah-Hartman  * The tq handling here is a little racy - tty->SAK_work may already be queued.
306296fd7ce5SGreg Kroah-Hartman  * Fortunately we don't need to worry, because if ->SAK_work is already queued,
306396fd7ce5SGreg Kroah-Hartman  * the values which we write to it will be identical to the values which it
306496fd7ce5SGreg Kroah-Hartman  * already has. --akpm
306596fd7ce5SGreg Kroah-Hartman  */
306696fd7ce5SGreg Kroah-Hartman void do_SAK(struct tty_struct *tty)
306796fd7ce5SGreg Kroah-Hartman {
306896fd7ce5SGreg Kroah-Hartman 	if (!tty)
306996fd7ce5SGreg Kroah-Hartman 		return;
307096fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->SAK_work);
307196fd7ce5SGreg Kroah-Hartman }
307296fd7ce5SGreg Kroah-Hartman 
307396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(do_SAK);
307496fd7ce5SGreg Kroah-Hartman 
30756e9430acSGreg Kroah-Hartman static int dev_match_devt(struct device *dev, const void *data)
307696fd7ce5SGreg Kroah-Hartman {
30776e9430acSGreg Kroah-Hartman 	const dev_t *devt = data;
307896fd7ce5SGreg Kroah-Hartman 	return dev->devt == *devt;
307996fd7ce5SGreg Kroah-Hartman }
308096fd7ce5SGreg Kroah-Hartman 
308196fd7ce5SGreg Kroah-Hartman /* Must put_device() after it's unused! */
308296fd7ce5SGreg Kroah-Hartman static struct device *tty_get_device(struct tty_struct *tty)
308396fd7ce5SGreg Kroah-Hartman {
308496fd7ce5SGreg Kroah-Hartman 	dev_t devt = tty_devnum(tty);
308596fd7ce5SGreg Kroah-Hartman 	return class_find_device(tty_class, NULL, &devt, dev_match_devt);
308696fd7ce5SGreg Kroah-Hartman }
308796fd7ce5SGreg Kroah-Hartman 
308896fd7ce5SGreg Kroah-Hartman 
308996fd7ce5SGreg Kroah-Hartman /**
30902c964a2fSRasmus Villemoes  *	alloc_tty_struct
309196fd7ce5SGreg Kroah-Hartman  *
30922c964a2fSRasmus Villemoes  *	This subroutine allocates and initializes a tty structure.
309396fd7ce5SGreg Kroah-Hartman  *
30942c964a2fSRasmus Villemoes  *	Locking: none - tty in question is not exposed at this point
309596fd7ce5SGreg Kroah-Hartman  */
309696fd7ce5SGreg Kroah-Hartman 
30972c964a2fSRasmus Villemoes struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
309896fd7ce5SGreg Kroah-Hartman {
30992c964a2fSRasmus Villemoes 	struct tty_struct *tty;
31002c964a2fSRasmus Villemoes 
31012c964a2fSRasmus Villemoes 	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
31022c964a2fSRasmus Villemoes 	if (!tty)
31032c964a2fSRasmus Villemoes 		return NULL;
31042c964a2fSRasmus Villemoes 
310596fd7ce5SGreg Kroah-Hartman 	kref_init(&tty->kref);
310696fd7ce5SGreg Kroah-Hartman 	tty->magic = TTY_MAGIC;
310796fd7ce5SGreg Kroah-Hartman 	tty_ldisc_init(tty);
310896fd7ce5SGreg Kroah-Hartman 	tty->session = NULL;
310996fd7ce5SGreg Kroah-Hartman 	tty->pgrp = NULL;
311089c8d91eSAlan Cox 	mutex_init(&tty->legacy_mutex);
3111d8c1f929SPeter Hurley 	mutex_init(&tty->throttle_mutex);
31126a1c0680SPeter Hurley 	init_rwsem(&tty->termios_rwsem);
3113dee4a0beSPeter Hurley 	mutex_init(&tty->winsize_mutex);
311436697529SPeter Hurley 	init_ldsem(&tty->ldisc_sem);
311596fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&tty->write_wait);
311696fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&tty->read_wait);
311796fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->hangup_work, do_tty_hangup);
311896fd7ce5SGreg Kroah-Hartman 	mutex_init(&tty->atomic_write_lock);
311996fd7ce5SGreg Kroah-Hartman 	spin_lock_init(&tty->ctrl_lock);
3120f9e053dcSPeter Hurley 	spin_lock_init(&tty->flow_lock);
312196fd7ce5SGreg Kroah-Hartman 	INIT_LIST_HEAD(&tty->tty_files);
312296fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->SAK_work, do_SAK_work);
312396fd7ce5SGreg Kroah-Hartman 
312496fd7ce5SGreg Kroah-Hartman 	tty->driver = driver;
312596fd7ce5SGreg Kroah-Hartman 	tty->ops = driver->ops;
312696fd7ce5SGreg Kroah-Hartman 	tty->index = idx;
312796fd7ce5SGreg Kroah-Hartman 	tty_line_name(driver, idx, tty->name);
312896fd7ce5SGreg Kroah-Hartman 	tty->dev = tty_get_device(tty);
31292c964a2fSRasmus Villemoes 
31302c964a2fSRasmus Villemoes 	return tty;
313196fd7ce5SGreg Kroah-Hartman }
313296fd7ce5SGreg Kroah-Hartman 
313396fd7ce5SGreg Kroah-Hartman /**
31346716671dSJiri Slaby  *	deinitialize_tty_struct
31356716671dSJiri Slaby  *	@tty: tty to deinitialize
31366716671dSJiri Slaby  *
31376716671dSJiri Slaby  *	This subroutine deinitializes a tty structure that has been newly
31386716671dSJiri Slaby  *	allocated but tty_release cannot be called on that yet.
31396716671dSJiri Slaby  *
31406716671dSJiri Slaby  *	Locking: none - tty in question must not be exposed at this point
31416716671dSJiri Slaby  */
31426716671dSJiri Slaby void deinitialize_tty_struct(struct tty_struct *tty)
31436716671dSJiri Slaby {
31446716671dSJiri Slaby 	tty_ldisc_deinit(tty);
31456716671dSJiri Slaby }
31466716671dSJiri Slaby 
31476716671dSJiri Slaby /**
314896fd7ce5SGreg Kroah-Hartman  *	tty_put_char	-	write one character to a tty
314996fd7ce5SGreg Kroah-Hartman  *	@tty: tty
315096fd7ce5SGreg Kroah-Hartman  *	@ch: character
315196fd7ce5SGreg Kroah-Hartman  *
315296fd7ce5SGreg Kroah-Hartman  *	Write one byte to the tty using the provided put_char method
315396fd7ce5SGreg Kroah-Hartman  *	if present. Returns the number of characters successfully output.
315496fd7ce5SGreg Kroah-Hartman  *
315596fd7ce5SGreg Kroah-Hartman  *	Note: the specific put_char operation in the driver layer may go
315696fd7ce5SGreg Kroah-Hartman  *	away soon. Don't call it directly, use this method
315796fd7ce5SGreg Kroah-Hartman  */
315896fd7ce5SGreg Kroah-Hartman 
315996fd7ce5SGreg Kroah-Hartman int tty_put_char(struct tty_struct *tty, unsigned char ch)
316096fd7ce5SGreg Kroah-Hartman {
316196fd7ce5SGreg Kroah-Hartman 	if (tty->ops->put_char)
316296fd7ce5SGreg Kroah-Hartman 		return tty->ops->put_char(tty, ch);
316396fd7ce5SGreg Kroah-Hartman 	return tty->ops->write(tty, &ch, 1);
316496fd7ce5SGreg Kroah-Hartman }
316596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_put_char);
316696fd7ce5SGreg Kroah-Hartman 
316796fd7ce5SGreg Kroah-Hartman struct class *tty_class;
316896fd7ce5SGreg Kroah-Hartman 
31697e73eca6SJiri Slaby static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
31707e73eca6SJiri Slaby 		unsigned int index, unsigned int count)
31717e73eca6SJiri Slaby {
3172c1a752baSLeon Yu 	int err;
3173c1a752baSLeon Yu 
31747e73eca6SJiri Slaby 	/* init here, since reused cdevs cause crashes */
3175a3a10ce3SRichard Watts 	driver->cdevs[index] = cdev_alloc();
3176a3a10ce3SRichard Watts 	if (!driver->cdevs[index])
3177a3a10ce3SRichard Watts 		return -ENOMEM;
3178c1a752baSLeon Yu 	driver->cdevs[index]->ops = &tty_fops;
3179a3a10ce3SRichard Watts 	driver->cdevs[index]->owner = driver->owner;
3180c1a752baSLeon Yu 	err = cdev_add(driver->cdevs[index], dev, count);
3181c1a752baSLeon Yu 	if (err)
3182c1a752baSLeon Yu 		kobject_put(&driver->cdevs[index]->kobj);
3183c1a752baSLeon Yu 	return err;
31847e73eca6SJiri Slaby }
31857e73eca6SJiri Slaby 
318696fd7ce5SGreg Kroah-Hartman /**
318796fd7ce5SGreg Kroah-Hartman  *	tty_register_device - register a tty device
318896fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver that describes the tty device
318996fd7ce5SGreg Kroah-Hartman  *	@index: the index in the tty driver for this tty device
319096fd7ce5SGreg Kroah-Hartman  *	@device: a struct device that is associated with this tty device.
319196fd7ce5SGreg Kroah-Hartman  *		This field is optional, if there is no known struct device
319296fd7ce5SGreg Kroah-Hartman  *		for this tty device it can be set to NULL safely.
319396fd7ce5SGreg Kroah-Hartman  *
319496fd7ce5SGreg Kroah-Hartman  *	Returns a pointer to the struct device for this tty device
319596fd7ce5SGreg Kroah-Hartman  *	(or ERR_PTR(-EFOO) on error).
319696fd7ce5SGreg Kroah-Hartman  *
319796fd7ce5SGreg Kroah-Hartman  *	This call is required to be made to register an individual tty device
319896fd7ce5SGreg Kroah-Hartman  *	if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set.  If
319996fd7ce5SGreg Kroah-Hartman  *	that bit is not set, this function should not be called by a tty
320096fd7ce5SGreg Kroah-Hartman  *	driver.
320196fd7ce5SGreg Kroah-Hartman  *
320296fd7ce5SGreg Kroah-Hartman  *	Locking: ??
320396fd7ce5SGreg Kroah-Hartman  */
320496fd7ce5SGreg Kroah-Hartman 
320596fd7ce5SGreg Kroah-Hartman struct device *tty_register_device(struct tty_driver *driver, unsigned index,
320696fd7ce5SGreg Kroah-Hartman 				   struct device *device)
320796fd7ce5SGreg Kroah-Hartman {
32086915c0e4STomas Hlavacek 	return tty_register_device_attr(driver, index, device, NULL, NULL);
32096915c0e4STomas Hlavacek }
32106915c0e4STomas Hlavacek EXPORT_SYMBOL(tty_register_device);
32116915c0e4STomas Hlavacek 
3212b1b79916STomas Hlavacek static void tty_device_create_release(struct device *dev)
3213b1b79916STomas Hlavacek {
321483db1df4SPeter Hurley 	dev_dbg(dev, "releasing...\n");
3215b1b79916STomas Hlavacek 	kfree(dev);
3216b1b79916STomas Hlavacek }
3217b1b79916STomas Hlavacek 
32186915c0e4STomas Hlavacek /**
32196915c0e4STomas Hlavacek  *	tty_register_device_attr - register a tty device
32206915c0e4STomas Hlavacek  *	@driver: the tty driver that describes the tty device
32216915c0e4STomas Hlavacek  *	@index: the index in the tty driver for this tty device
32226915c0e4STomas Hlavacek  *	@device: a struct device that is associated with this tty device.
32236915c0e4STomas Hlavacek  *		This field is optional, if there is no known struct device
32246915c0e4STomas Hlavacek  *		for this tty device it can be set to NULL safely.
32256915c0e4STomas Hlavacek  *	@drvdata: Driver data to be set to device.
32266915c0e4STomas Hlavacek  *	@attr_grp: Attribute group to be set on device.
32276915c0e4STomas Hlavacek  *
32286915c0e4STomas Hlavacek  *	Returns a pointer to the struct device for this tty device
32296915c0e4STomas Hlavacek  *	(or ERR_PTR(-EFOO) on error).
32306915c0e4STomas Hlavacek  *
32316915c0e4STomas Hlavacek  *	This call is required to be made to register an individual tty device
32326915c0e4STomas Hlavacek  *	if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set.  If
32336915c0e4STomas Hlavacek  *	that bit is not set, this function should not be called by a tty
32346915c0e4STomas Hlavacek  *	driver.
32356915c0e4STomas Hlavacek  *
32366915c0e4STomas Hlavacek  *	Locking: ??
32376915c0e4STomas Hlavacek  */
32386915c0e4STomas Hlavacek struct device *tty_register_device_attr(struct tty_driver *driver,
32396915c0e4STomas Hlavacek 				   unsigned index, struct device *device,
32406915c0e4STomas Hlavacek 				   void *drvdata,
32416915c0e4STomas Hlavacek 				   const struct attribute_group **attr_grp)
32426915c0e4STomas Hlavacek {
324396fd7ce5SGreg Kroah-Hartman 	char name[64];
32446915c0e4STomas Hlavacek 	dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
32456915c0e4STomas Hlavacek 	struct device *dev = NULL;
32466915c0e4STomas Hlavacek 	int retval = -ENODEV;
32477e73eca6SJiri Slaby 	bool cdev = false;
324896fd7ce5SGreg Kroah-Hartman 
324996fd7ce5SGreg Kroah-Hartman 	if (index >= driver->num) {
3250656fb867SPeter Hurley 		pr_err("%s: Attempt to register invalid tty line number (%d)\n",
3251656fb867SPeter Hurley 		       driver->name, index);
325296fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-EINVAL);
325396fd7ce5SGreg Kroah-Hartman 	}
325496fd7ce5SGreg Kroah-Hartman 
325596fd7ce5SGreg Kroah-Hartman 	if (driver->type == TTY_DRIVER_TYPE_PTY)
325696fd7ce5SGreg Kroah-Hartman 		pty_line_name(driver, index, name);
325796fd7ce5SGreg Kroah-Hartman 	else
325896fd7ce5SGreg Kroah-Hartman 		tty_line_name(driver, index, name);
325996fd7ce5SGreg Kroah-Hartman 
32607e73eca6SJiri Slaby 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
32616915c0e4STomas Hlavacek 		retval = tty_cdev_add(driver, devt, index, 1);
32626915c0e4STomas Hlavacek 		if (retval)
32636915c0e4STomas Hlavacek 			goto error;
32647e73eca6SJiri Slaby 		cdev = true;
32657e73eca6SJiri Slaby 	}
32667e73eca6SJiri Slaby 
32676915c0e4STomas Hlavacek 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
32686915c0e4STomas Hlavacek 	if (!dev) {
32696915c0e4STomas Hlavacek 		retval = -ENOMEM;
32706915c0e4STomas Hlavacek 		goto error;
327196fd7ce5SGreg Kroah-Hartman 	}
32726915c0e4STomas Hlavacek 
32736915c0e4STomas Hlavacek 	dev->devt = devt;
32746915c0e4STomas Hlavacek 	dev->class = tty_class;
32756915c0e4STomas Hlavacek 	dev->parent = device;
3276b1b79916STomas Hlavacek 	dev->release = tty_device_create_release;
32776915c0e4STomas Hlavacek 	dev_set_name(dev, "%s", name);
32786915c0e4STomas Hlavacek 	dev->groups = attr_grp;
32796915c0e4STomas Hlavacek 	dev_set_drvdata(dev, drvdata);
32806915c0e4STomas Hlavacek 
32816915c0e4STomas Hlavacek 	retval = device_register(dev);
32826915c0e4STomas Hlavacek 	if (retval)
32836915c0e4STomas Hlavacek 		goto error;
32846915c0e4STomas Hlavacek 
32856915c0e4STomas Hlavacek 	return dev;
32866915c0e4STomas Hlavacek 
32876915c0e4STomas Hlavacek error:
32886915c0e4STomas Hlavacek 	put_device(dev);
3289a3a10ce3SRichard Watts 	if (cdev) {
3290a3a10ce3SRichard Watts 		cdev_del(driver->cdevs[index]);
3291a3a10ce3SRichard Watts 		driver->cdevs[index] = NULL;
3292a3a10ce3SRichard Watts 	}
32936915c0e4STomas Hlavacek 	return ERR_PTR(retval);
32946915c0e4STomas Hlavacek }
32956915c0e4STomas Hlavacek EXPORT_SYMBOL_GPL(tty_register_device_attr);
329696fd7ce5SGreg Kroah-Hartman 
329796fd7ce5SGreg Kroah-Hartman /**
329896fd7ce5SGreg Kroah-Hartman  * 	tty_unregister_device - unregister a tty device
329996fd7ce5SGreg Kroah-Hartman  * 	@driver: the tty driver that describes the tty device
330096fd7ce5SGreg Kroah-Hartman  * 	@index: the index in the tty driver for this tty device
330196fd7ce5SGreg Kroah-Hartman  *
330296fd7ce5SGreg Kroah-Hartman  * 	If a tty device is registered with a call to tty_register_device() then
330396fd7ce5SGreg Kroah-Hartman  *	this function must be called when the tty device is gone.
330496fd7ce5SGreg Kroah-Hartman  *
330596fd7ce5SGreg Kroah-Hartman  *	Locking: ??
330696fd7ce5SGreg Kroah-Hartman  */
330796fd7ce5SGreg Kroah-Hartman 
330896fd7ce5SGreg Kroah-Hartman void tty_unregister_device(struct tty_driver *driver, unsigned index)
330996fd7ce5SGreg Kroah-Hartman {
331096fd7ce5SGreg Kroah-Hartman 	device_destroy(tty_class,
331196fd7ce5SGreg Kroah-Hartman 		MKDEV(driver->major, driver->minor_start) + index);
3312a3a10ce3SRichard Watts 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3313a3a10ce3SRichard Watts 		cdev_del(driver->cdevs[index]);
3314a3a10ce3SRichard Watts 		driver->cdevs[index] = NULL;
3315a3a10ce3SRichard Watts 	}
331696fd7ce5SGreg Kroah-Hartman }
331796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_device);
331896fd7ce5SGreg Kroah-Hartman 
33197f0bc6a6SJiri Slaby /**
33207f0bc6a6SJiri Slaby  * __tty_alloc_driver -- allocate tty driver
33217f0bc6a6SJiri Slaby  * @lines: count of lines this driver can handle at most
33227f0bc6a6SJiri Slaby  * @owner: module which is repsonsible for this driver
33237f0bc6a6SJiri Slaby  * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
33247f0bc6a6SJiri Slaby  *
33257f0bc6a6SJiri Slaby  * This should not be called directly, some of the provided macros should be
33267f0bc6a6SJiri Slaby  * used instead. Use IS_ERR and friends on @retval.
33277f0bc6a6SJiri Slaby  */
33287f0bc6a6SJiri Slaby struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
33297f0bc6a6SJiri Slaby 		unsigned long flags)
333096fd7ce5SGreg Kroah-Hartman {
333196fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver;
33327e73eca6SJiri Slaby 	unsigned int cdevs = 1;
333316a02081SJiri Slaby 	int err;
333496fd7ce5SGreg Kroah-Hartman 
33350019b408SJiri Slaby 	if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
33367f0bc6a6SJiri Slaby 		return ERR_PTR(-EINVAL);
33377f0bc6a6SJiri Slaby 
333896fd7ce5SGreg Kroah-Hartman 	driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
33397f0bc6a6SJiri Slaby 	if (!driver)
33407f0bc6a6SJiri Slaby 		return ERR_PTR(-ENOMEM);
33417f0bc6a6SJiri Slaby 
334296fd7ce5SGreg Kroah-Hartman 	kref_init(&driver->kref);
334396fd7ce5SGreg Kroah-Hartman 	driver->magic = TTY_DRIVER_MAGIC;
334496fd7ce5SGreg Kroah-Hartman 	driver->num = lines;
33451a54a76dSJiri Slaby 	driver->owner = owner;
33467f0bc6a6SJiri Slaby 	driver->flags = flags;
334716a02081SJiri Slaby 
334816a02081SJiri Slaby 	if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
334916a02081SJiri Slaby 		driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
335016a02081SJiri Slaby 				GFP_KERNEL);
335116a02081SJiri Slaby 		driver->termios = kcalloc(lines, sizeof(*driver->termios),
335216a02081SJiri Slaby 				GFP_KERNEL);
335316a02081SJiri Slaby 		if (!driver->ttys || !driver->termios) {
335416a02081SJiri Slaby 			err = -ENOMEM;
335516a02081SJiri Slaby 			goto err_free_all;
335616a02081SJiri Slaby 		}
335716a02081SJiri Slaby 	}
335816a02081SJiri Slaby 
335916a02081SJiri Slaby 	if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
336016a02081SJiri Slaby 		driver->ports = kcalloc(lines, sizeof(*driver->ports),
336116a02081SJiri Slaby 				GFP_KERNEL);
336216a02081SJiri Slaby 		if (!driver->ports) {
336316a02081SJiri Slaby 			err = -ENOMEM;
336416a02081SJiri Slaby 			goto err_free_all;
336516a02081SJiri Slaby 		}
33667e73eca6SJiri Slaby 		cdevs = lines;
33677e73eca6SJiri Slaby 	}
33687e73eca6SJiri Slaby 
33697e73eca6SJiri Slaby 	driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
33707e73eca6SJiri Slaby 	if (!driver->cdevs) {
33717e73eca6SJiri Slaby 		err = -ENOMEM;
33727e73eca6SJiri Slaby 		goto err_free_all;
337316a02081SJiri Slaby 	}
33747f0bc6a6SJiri Slaby 
337596fd7ce5SGreg Kroah-Hartman 	return driver;
337616a02081SJiri Slaby err_free_all:
337716a02081SJiri Slaby 	kfree(driver->ports);
337816a02081SJiri Slaby 	kfree(driver->ttys);
337916a02081SJiri Slaby 	kfree(driver->termios);
3380a3a10ce3SRichard Watts 	kfree(driver->cdevs);
338116a02081SJiri Slaby 	kfree(driver);
338216a02081SJiri Slaby 	return ERR_PTR(err);
338396fd7ce5SGreg Kroah-Hartman }
33847f0bc6a6SJiri Slaby EXPORT_SYMBOL(__tty_alloc_driver);
338596fd7ce5SGreg Kroah-Hartman 
338696fd7ce5SGreg Kroah-Hartman static void destruct_tty_driver(struct kref *kref)
338796fd7ce5SGreg Kroah-Hartman {
338896fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
338996fd7ce5SGreg Kroah-Hartman 	int i;
339096fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
339196fd7ce5SGreg Kroah-Hartman 
339296fd7ce5SGreg Kroah-Hartman 	if (driver->flags & TTY_DRIVER_INSTALLED) {
339396fd7ce5SGreg Kroah-Hartman 		/*
339496fd7ce5SGreg Kroah-Hartman 		 * Free the termios and termios_locked structures because
339596fd7ce5SGreg Kroah-Hartman 		 * we don't want to get memory leaks when modular tty
339696fd7ce5SGreg Kroah-Hartman 		 * drivers are removed from the kernel.
339796fd7ce5SGreg Kroah-Hartman 		 */
339896fd7ce5SGreg Kroah-Hartman 		for (i = 0; i < driver->num; i++) {
339996fd7ce5SGreg Kroah-Hartman 			tp = driver->termios[i];
340096fd7ce5SGreg Kroah-Hartman 			if (tp) {
340196fd7ce5SGreg Kroah-Hartman 				driver->termios[i] = NULL;
340296fd7ce5SGreg Kroah-Hartman 				kfree(tp);
340396fd7ce5SGreg Kroah-Hartman 			}
340496fd7ce5SGreg Kroah-Hartman 			if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
340596fd7ce5SGreg Kroah-Hartman 				tty_unregister_device(driver, i);
340696fd7ce5SGreg Kroah-Hartman 		}
340796fd7ce5SGreg Kroah-Hartman 		proc_tty_unregister_driver(driver);
34087e73eca6SJiri Slaby 		if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
3409a3a10ce3SRichard Watts 			cdev_del(driver->cdevs[0]);
341096fd7ce5SGreg Kroah-Hartman 	}
34117e73eca6SJiri Slaby 	kfree(driver->cdevs);
341204831dc1SJiri Slaby 	kfree(driver->ports);
341316a02081SJiri Slaby 	kfree(driver->termios);
341416a02081SJiri Slaby 	kfree(driver->ttys);
341596fd7ce5SGreg Kroah-Hartman 	kfree(driver);
341696fd7ce5SGreg Kroah-Hartman }
341796fd7ce5SGreg Kroah-Hartman 
341896fd7ce5SGreg Kroah-Hartman void tty_driver_kref_put(struct tty_driver *driver)
341996fd7ce5SGreg Kroah-Hartman {
342096fd7ce5SGreg Kroah-Hartman 	kref_put(&driver->kref, destruct_tty_driver);
342196fd7ce5SGreg Kroah-Hartman }
342296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_driver_kref_put);
342396fd7ce5SGreg Kroah-Hartman 
342496fd7ce5SGreg Kroah-Hartman void tty_set_operations(struct tty_driver *driver,
342596fd7ce5SGreg Kroah-Hartman 			const struct tty_operations *op)
342696fd7ce5SGreg Kroah-Hartman {
342796fd7ce5SGreg Kroah-Hartman 	driver->ops = op;
342896fd7ce5SGreg Kroah-Hartman };
342996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_set_operations);
343096fd7ce5SGreg Kroah-Hartman 
343196fd7ce5SGreg Kroah-Hartman void put_tty_driver(struct tty_driver *d)
343296fd7ce5SGreg Kroah-Hartman {
343396fd7ce5SGreg Kroah-Hartman 	tty_driver_kref_put(d);
343496fd7ce5SGreg Kroah-Hartman }
343596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(put_tty_driver);
343696fd7ce5SGreg Kroah-Hartman 
343796fd7ce5SGreg Kroah-Hartman /*
343896fd7ce5SGreg Kroah-Hartman  * Called by a tty driver to register itself.
343996fd7ce5SGreg Kroah-Hartman  */
344096fd7ce5SGreg Kroah-Hartman int tty_register_driver(struct tty_driver *driver)
344196fd7ce5SGreg Kroah-Hartman {
344296fd7ce5SGreg Kroah-Hartman 	int error;
344396fd7ce5SGreg Kroah-Hartman 	int i;
344496fd7ce5SGreg Kroah-Hartman 	dev_t dev;
344596fd7ce5SGreg Kroah-Hartman 	struct device *d;
344696fd7ce5SGreg Kroah-Hartman 
344796fd7ce5SGreg Kroah-Hartman 	if (!driver->major) {
344896fd7ce5SGreg Kroah-Hartman 		error = alloc_chrdev_region(&dev, driver->minor_start,
344996fd7ce5SGreg Kroah-Hartman 						driver->num, driver->name);
345096fd7ce5SGreg Kroah-Hartman 		if (!error) {
345196fd7ce5SGreg Kroah-Hartman 			driver->major = MAJOR(dev);
345296fd7ce5SGreg Kroah-Hartman 			driver->minor_start = MINOR(dev);
345396fd7ce5SGreg Kroah-Hartman 		}
345496fd7ce5SGreg Kroah-Hartman 	} else {
345596fd7ce5SGreg Kroah-Hartman 		dev = MKDEV(driver->major, driver->minor_start);
345696fd7ce5SGreg Kroah-Hartman 		error = register_chrdev_region(dev, driver->num, driver->name);
345796fd7ce5SGreg Kroah-Hartman 	}
34589bb8a3d4SJiri Slaby 	if (error < 0)
345916a02081SJiri Slaby 		goto err;
346096fd7ce5SGreg Kroah-Hartman 
34617e73eca6SJiri Slaby 	if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
34627e73eca6SJiri Slaby 		error = tty_cdev_add(driver, dev, 0, driver->num);
34639bb8a3d4SJiri Slaby 		if (error)
34649bb8a3d4SJiri Slaby 			goto err_unreg_char;
34657e73eca6SJiri Slaby 	}
346696fd7ce5SGreg Kroah-Hartman 
346796fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
346896fd7ce5SGreg Kroah-Hartman 	list_add(&driver->tty_drivers, &tty_drivers);
346996fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
347096fd7ce5SGreg Kroah-Hartman 
347196fd7ce5SGreg Kroah-Hartman 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
347296fd7ce5SGreg Kroah-Hartman 		for (i = 0; i < driver->num; i++) {
347396fd7ce5SGreg Kroah-Hartman 			d = tty_register_device(driver, i, NULL);
347496fd7ce5SGreg Kroah-Hartman 			if (IS_ERR(d)) {
347596fd7ce5SGreg Kroah-Hartman 				error = PTR_ERR(d);
347616a02081SJiri Slaby 				goto err_unreg_devs;
347796fd7ce5SGreg Kroah-Hartman 			}
347896fd7ce5SGreg Kroah-Hartman 		}
347996fd7ce5SGreg Kroah-Hartman 	}
348096fd7ce5SGreg Kroah-Hartman 	proc_tty_register_driver(driver);
348196fd7ce5SGreg Kroah-Hartman 	driver->flags |= TTY_DRIVER_INSTALLED;
348296fd7ce5SGreg Kroah-Hartman 	return 0;
348396fd7ce5SGreg Kroah-Hartman 
348416a02081SJiri Slaby err_unreg_devs:
348596fd7ce5SGreg Kroah-Hartman 	for (i--; i >= 0; i--)
348696fd7ce5SGreg Kroah-Hartman 		tty_unregister_device(driver, i);
348796fd7ce5SGreg Kroah-Hartman 
348896fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
348996fd7ce5SGreg Kroah-Hartman 	list_del(&driver->tty_drivers);
349096fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
349196fd7ce5SGreg Kroah-Hartman 
34929bb8a3d4SJiri Slaby err_unreg_char:
349396fd7ce5SGreg Kroah-Hartman 	unregister_chrdev_region(dev, driver->num);
349416a02081SJiri Slaby err:
349596fd7ce5SGreg Kroah-Hartman 	return error;
349696fd7ce5SGreg Kroah-Hartman }
349796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_driver);
349896fd7ce5SGreg Kroah-Hartman 
349996fd7ce5SGreg Kroah-Hartman /*
350096fd7ce5SGreg Kroah-Hartman  * Called by a tty driver to unregister itself.
350196fd7ce5SGreg Kroah-Hartman  */
350296fd7ce5SGreg Kroah-Hartman int tty_unregister_driver(struct tty_driver *driver)
350396fd7ce5SGreg Kroah-Hartman {
350496fd7ce5SGreg Kroah-Hartman #if 0
350596fd7ce5SGreg Kroah-Hartman 	/* FIXME */
350696fd7ce5SGreg Kroah-Hartman 	if (driver->refcount)
350796fd7ce5SGreg Kroah-Hartman 		return -EBUSY;
350896fd7ce5SGreg Kroah-Hartman #endif
350996fd7ce5SGreg Kroah-Hartman 	unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
351096fd7ce5SGreg Kroah-Hartman 				driver->num);
351196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
351296fd7ce5SGreg Kroah-Hartman 	list_del(&driver->tty_drivers);
351396fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
351496fd7ce5SGreg Kroah-Hartman 	return 0;
351596fd7ce5SGreg Kroah-Hartman }
351696fd7ce5SGreg Kroah-Hartman 
351796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_driver);
351896fd7ce5SGreg Kroah-Hartman 
351996fd7ce5SGreg Kroah-Hartman dev_t tty_devnum(struct tty_struct *tty)
352096fd7ce5SGreg Kroah-Hartman {
352196fd7ce5SGreg Kroah-Hartman 	return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
352296fd7ce5SGreg Kroah-Hartman }
352396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_devnum);
352496fd7ce5SGreg Kroah-Hartman 
352596fd7ce5SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops)
352696fd7ce5SGreg Kroah-Hartman {
352796fd7ce5SGreg Kroah-Hartman 	*fops = tty_fops;
352896fd7ce5SGreg Kroah-Hartman }
352996fd7ce5SGreg Kroah-Hartman 
353096fd7ce5SGreg Kroah-Hartman /*
353196fd7ce5SGreg Kroah-Hartman  * Initialize the console device. This is called *early*, so
353296fd7ce5SGreg Kroah-Hartman  * we can't necessarily depend on lots of kernel help here.
353396fd7ce5SGreg Kroah-Hartman  * Just do some early initializations, and do the complex setup
353496fd7ce5SGreg Kroah-Hartman  * later.
353596fd7ce5SGreg Kroah-Hartman  */
353696fd7ce5SGreg Kroah-Hartman void __init console_init(void)
353796fd7ce5SGreg Kroah-Hartman {
353896fd7ce5SGreg Kroah-Hartman 	initcall_t *call;
353996fd7ce5SGreg Kroah-Hartman 
354096fd7ce5SGreg Kroah-Hartman 	/* Setup the default TTY line discipline. */
354196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_begin();
354296fd7ce5SGreg Kroah-Hartman 
354396fd7ce5SGreg Kroah-Hartman 	/*
354496fd7ce5SGreg Kroah-Hartman 	 * set up the console device so that later boot sequences can
354596fd7ce5SGreg Kroah-Hartman 	 * inform about problems etc..
354696fd7ce5SGreg Kroah-Hartman 	 */
354796fd7ce5SGreg Kroah-Hartman 	call = __con_initcall_start;
354896fd7ce5SGreg Kroah-Hartman 	while (call < __con_initcall_end) {
354996fd7ce5SGreg Kroah-Hartman 		(*call)();
355096fd7ce5SGreg Kroah-Hartman 		call++;
355196fd7ce5SGreg Kroah-Hartman 	}
355296fd7ce5SGreg Kroah-Hartman }
355396fd7ce5SGreg Kroah-Hartman 
35542c9ede55SAl Viro static char *tty_devnode(struct device *dev, umode_t *mode)
355596fd7ce5SGreg Kroah-Hartman {
355696fd7ce5SGreg Kroah-Hartman 	if (!mode)
355796fd7ce5SGreg Kroah-Hartman 		return NULL;
355896fd7ce5SGreg Kroah-Hartman 	if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
355996fd7ce5SGreg Kroah-Hartman 	    dev->devt == MKDEV(TTYAUX_MAJOR, 2))
356096fd7ce5SGreg Kroah-Hartman 		*mode = 0666;
356196fd7ce5SGreg Kroah-Hartman 	return NULL;
356296fd7ce5SGreg Kroah-Hartman }
356396fd7ce5SGreg Kroah-Hartman 
356496fd7ce5SGreg Kroah-Hartman static int __init tty_class_init(void)
356596fd7ce5SGreg Kroah-Hartman {
356696fd7ce5SGreg Kroah-Hartman 	tty_class = class_create(THIS_MODULE, "tty");
356796fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(tty_class))
356896fd7ce5SGreg Kroah-Hartman 		return PTR_ERR(tty_class);
356996fd7ce5SGreg Kroah-Hartman 	tty_class->devnode = tty_devnode;
357096fd7ce5SGreg Kroah-Hartman 	return 0;
357196fd7ce5SGreg Kroah-Hartman }
357296fd7ce5SGreg Kroah-Hartman 
357396fd7ce5SGreg Kroah-Hartman postcore_initcall(tty_class_init);
357496fd7ce5SGreg Kroah-Hartman 
357596fd7ce5SGreg Kroah-Hartman /* 3/2004 jmc: why do these devices exist? */
357696fd7ce5SGreg Kroah-Hartman static struct cdev tty_cdev, console_cdev;
357796fd7ce5SGreg Kroah-Hartman 
3578fbc92a34SKay Sievers static ssize_t show_cons_active(struct device *dev,
3579fbc92a34SKay Sievers 				struct device_attribute *attr, char *buf)
3580fbc92a34SKay Sievers {
3581fbc92a34SKay Sievers 	struct console *cs[16];
3582fbc92a34SKay Sievers 	int i = 0;
3583fbc92a34SKay Sievers 	struct console *c;
3584fbc92a34SKay Sievers 	ssize_t count = 0;
3585fbc92a34SKay Sievers 
3586ac751efaSTorben Hohn 	console_lock();
3587a2a6a822SKay Sievers 	for_each_console(c) {
3588fbc92a34SKay Sievers 		if (!c->device)
3589fbc92a34SKay Sievers 			continue;
3590fbc92a34SKay Sievers 		if (!c->write)
3591fbc92a34SKay Sievers 			continue;
3592fbc92a34SKay Sievers 		if ((c->flags & CON_ENABLED) == 0)
3593fbc92a34SKay Sievers 			continue;
3594fbc92a34SKay Sievers 		cs[i++] = c;
3595fbc92a34SKay Sievers 		if (i >= ARRAY_SIZE(cs))
3596fbc92a34SKay Sievers 			break;
3597fbc92a34SKay Sievers 	}
3598723abd87SHannes Reinecke 	while (i--) {
3599723abd87SHannes Reinecke 		int index = cs[i]->index;
3600723abd87SHannes Reinecke 		struct tty_driver *drv = cs[i]->device(cs[i], &index);
3601723abd87SHannes Reinecke 
3602723abd87SHannes Reinecke 		/* don't resolve tty0 as some programs depend on it */
3603723abd87SHannes Reinecke 		if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3604723abd87SHannes Reinecke 			count += tty_line_name(drv, index, buf + count);
3605723abd87SHannes Reinecke 		else
3606723abd87SHannes Reinecke 			count += sprintf(buf + count, "%s%d",
3607723abd87SHannes Reinecke 					 cs[i]->name, cs[i]->index);
3608723abd87SHannes Reinecke 
3609723abd87SHannes Reinecke 		count += sprintf(buf + count, "%c", i ? ' ':'\n');
3610723abd87SHannes Reinecke 	}
3611ac751efaSTorben Hohn 	console_unlock();
3612fbc92a34SKay Sievers 
3613fbc92a34SKay Sievers 	return count;
3614fbc92a34SKay Sievers }
3615fbc92a34SKay Sievers static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3616fbc92a34SKay Sievers 
36171083a7beSTakashi Iwai static struct attribute *cons_dev_attrs[] = {
36181083a7beSTakashi Iwai 	&dev_attr_active.attr,
36191083a7beSTakashi Iwai 	NULL
36201083a7beSTakashi Iwai };
36211083a7beSTakashi Iwai 
36221083a7beSTakashi Iwai ATTRIBUTE_GROUPS(cons_dev);
36231083a7beSTakashi Iwai 
3624fbc92a34SKay Sievers static struct device *consdev;
3625fbc92a34SKay Sievers 
3626fbc92a34SKay Sievers void console_sysfs_notify(void)
3627fbc92a34SKay Sievers {
3628fbc92a34SKay Sievers 	if (consdev)
3629fbc92a34SKay Sievers 		sysfs_notify(&consdev->kobj, NULL, "active");
3630fbc92a34SKay Sievers }
3631fbc92a34SKay Sievers 
363296fd7ce5SGreg Kroah-Hartman /*
363396fd7ce5SGreg Kroah-Hartman  * Ok, now we can initialize the rest of the tty devices and can count
363496fd7ce5SGreg Kroah-Hartman  * on memory allocations, interrupts etc..
363596fd7ce5SGreg Kroah-Hartman  */
363696fd7ce5SGreg Kroah-Hartman int __init tty_init(void)
363796fd7ce5SGreg Kroah-Hartman {
363896fd7ce5SGreg Kroah-Hartman 	cdev_init(&tty_cdev, &tty_fops);
363996fd7ce5SGreg Kroah-Hartman 	if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
364096fd7ce5SGreg Kroah-Hartman 	    register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
364196fd7ce5SGreg Kroah-Hartman 		panic("Couldn't register /dev/tty driver\n");
3642fbc92a34SKay Sievers 	device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
364396fd7ce5SGreg Kroah-Hartman 
364496fd7ce5SGreg Kroah-Hartman 	cdev_init(&console_cdev, &console_fops);
364596fd7ce5SGreg Kroah-Hartman 	if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
364696fd7ce5SGreg Kroah-Hartman 	    register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
364796fd7ce5SGreg Kroah-Hartman 		panic("Couldn't register /dev/console driver\n");
36481083a7beSTakashi Iwai 	consdev = device_create_with_groups(tty_class, NULL,
36491083a7beSTakashi Iwai 					    MKDEV(TTYAUX_MAJOR, 1), NULL,
36501083a7beSTakashi Iwai 					    cons_dev_groups, "console");
3651fbc92a34SKay Sievers 	if (IS_ERR(consdev))
3652fbc92a34SKay Sievers 		consdev = NULL;
365396fd7ce5SGreg Kroah-Hartman 
365496fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT
365596fd7ce5SGreg Kroah-Hartman 	vty_init(&console_fops);
365696fd7ce5SGreg Kroah-Hartman #endif
365796fd7ce5SGreg Kroah-Hartman 	return 0;
365896fd7ce5SGreg Kroah-Hartman }
365996fd7ce5SGreg Kroah-Hartman 
3660