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 #include <asm/system.h> 10196fd7ce5SGreg Kroah-Hartman 10296fd7ce5SGreg Kroah-Hartman #include <linux/kbd_kern.h> 10396fd7ce5SGreg Kroah-Hartman #include <linux/vt_kern.h> 10496fd7ce5SGreg Kroah-Hartman #include <linux/selection.h> 10596fd7ce5SGreg Kroah-Hartman 10696fd7ce5SGreg Kroah-Hartman #include <linux/kmod.h> 10796fd7ce5SGreg Kroah-Hartman #include <linux/nsproxy.h> 10896fd7ce5SGreg Kroah-Hartman 10996fd7ce5SGreg Kroah-Hartman #undef TTY_DEBUG_HANGUP 11096fd7ce5SGreg Kroah-Hartman 11196fd7ce5SGreg Kroah-Hartman #define TTY_PARANOIA_CHECK 1 11296fd7ce5SGreg Kroah-Hartman #define CHECK_TTY_COUNT 1 11396fd7ce5SGreg Kroah-Hartman 11496fd7ce5SGreg Kroah-Hartman struct ktermios tty_std_termios = { /* for the benefit of tty drivers */ 11596fd7ce5SGreg Kroah-Hartman .c_iflag = ICRNL | IXON, 11696fd7ce5SGreg Kroah-Hartman .c_oflag = OPOST | ONLCR, 11796fd7ce5SGreg Kroah-Hartman .c_cflag = B38400 | CS8 | CREAD | HUPCL, 11896fd7ce5SGreg Kroah-Hartman .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | 11996fd7ce5SGreg Kroah-Hartman ECHOCTL | ECHOKE | IEXTEN, 12096fd7ce5SGreg Kroah-Hartman .c_cc = INIT_C_CC, 12196fd7ce5SGreg Kroah-Hartman .c_ispeed = 38400, 12296fd7ce5SGreg Kroah-Hartman .c_ospeed = 38400 12396fd7ce5SGreg Kroah-Hartman }; 12496fd7ce5SGreg Kroah-Hartman 12596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_std_termios); 12696fd7ce5SGreg Kroah-Hartman 12796fd7ce5SGreg Kroah-Hartman /* This list gets poked at by procfs and various bits of boot up code. This 12896fd7ce5SGreg Kroah-Hartman could do with some rationalisation such as pulling the tty proc function 12996fd7ce5SGreg Kroah-Hartman into this file */ 13096fd7ce5SGreg Kroah-Hartman 13196fd7ce5SGreg Kroah-Hartman LIST_HEAD(tty_drivers); /* linked list of tty drivers */ 13296fd7ce5SGreg Kroah-Hartman 13396fd7ce5SGreg Kroah-Hartman /* Mutex to protect creating and releasing a tty. This is shared with 13496fd7ce5SGreg Kroah-Hartman vt.c for deeply disgusting hack reasons */ 13596fd7ce5SGreg Kroah-Hartman DEFINE_MUTEX(tty_mutex); 13696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_mutex); 13796fd7ce5SGreg Kroah-Hartman 13896fd7ce5SGreg Kroah-Hartman /* Spinlock to protect the tty->tty_files list */ 13996fd7ce5SGreg Kroah-Hartman DEFINE_SPINLOCK(tty_files_lock); 14096fd7ce5SGreg Kroah-Hartman 14196fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); 14296fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); 14396fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *, const char __user *, 14496fd7ce5SGreg Kroah-Hartman size_t, loff_t *); 14596fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *, poll_table *); 14696fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *, struct file *); 14796fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 14896fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 14996fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 15096fd7ce5SGreg Kroah-Hartman unsigned long arg); 15196fd7ce5SGreg Kroah-Hartman #else 15296fd7ce5SGreg Kroah-Hartman #define tty_compat_ioctl NULL 15396fd7ce5SGreg Kroah-Hartman #endif 15496fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on); 15596fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on); 15696fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx); 15796fd7ce5SGreg Kroah-Hartman static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); 15896fd7ce5SGreg Kroah-Hartman static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); 15996fd7ce5SGreg Kroah-Hartman 16096fd7ce5SGreg Kroah-Hartman /** 16196fd7ce5SGreg Kroah-Hartman * alloc_tty_struct - allocate a tty object 16296fd7ce5SGreg Kroah-Hartman * 16396fd7ce5SGreg Kroah-Hartman * Return a new empty tty structure. The data fields have not 16496fd7ce5SGreg Kroah-Hartman * been initialized in any way but has been zeroed 16596fd7ce5SGreg Kroah-Hartman * 16696fd7ce5SGreg Kroah-Hartman * Locking: none 16796fd7ce5SGreg Kroah-Hartman */ 16896fd7ce5SGreg Kroah-Hartman 16996fd7ce5SGreg Kroah-Hartman struct tty_struct *alloc_tty_struct(void) 17096fd7ce5SGreg Kroah-Hartman { 17196fd7ce5SGreg Kroah-Hartman return kzalloc(sizeof(struct tty_struct), GFP_KERNEL); 17296fd7ce5SGreg Kroah-Hartman } 17396fd7ce5SGreg Kroah-Hartman 17496fd7ce5SGreg Kroah-Hartman /** 17596fd7ce5SGreg Kroah-Hartman * free_tty_struct - free a disused tty 17696fd7ce5SGreg Kroah-Hartman * @tty: tty struct to free 17796fd7ce5SGreg Kroah-Hartman * 17896fd7ce5SGreg Kroah-Hartman * Free the write buffers, tty queue and tty memory itself. 17996fd7ce5SGreg Kroah-Hartman * 18096fd7ce5SGreg Kroah-Hartman * Locking: none. Must be called after tty is definitely unused 18196fd7ce5SGreg Kroah-Hartman */ 18296fd7ce5SGreg Kroah-Hartman 18396fd7ce5SGreg Kroah-Hartman void free_tty_struct(struct tty_struct *tty) 18496fd7ce5SGreg Kroah-Hartman { 18596fd7ce5SGreg Kroah-Hartman if (tty->dev) 18696fd7ce5SGreg Kroah-Hartman put_device(tty->dev); 18796fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 18896fd7ce5SGreg Kroah-Hartman tty_buffer_free_all(tty); 18996fd7ce5SGreg Kroah-Hartman kfree(tty); 19096fd7ce5SGreg Kroah-Hartman } 19196fd7ce5SGreg Kroah-Hartman 19296fd7ce5SGreg Kroah-Hartman static inline struct tty_struct *file_tty(struct file *file) 19396fd7ce5SGreg Kroah-Hartman { 19496fd7ce5SGreg Kroah-Hartman return ((struct tty_file_private *)file->private_data)->tty; 19596fd7ce5SGreg Kroah-Hartman } 19696fd7ce5SGreg Kroah-Hartman 19796fd7ce5SGreg Kroah-Hartman /* Associate a new file with the tty structure */ 19896fd7ce5SGreg Kroah-Hartman int tty_add_file(struct tty_struct *tty, struct file *file) 19996fd7ce5SGreg Kroah-Hartman { 20096fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 20196fd7ce5SGreg Kroah-Hartman 20296fd7ce5SGreg Kroah-Hartman priv = kmalloc(sizeof(*priv), GFP_KERNEL); 20396fd7ce5SGreg Kroah-Hartman if (!priv) 20496fd7ce5SGreg Kroah-Hartman return -ENOMEM; 20596fd7ce5SGreg Kroah-Hartman 20696fd7ce5SGreg Kroah-Hartman priv->tty = tty; 20796fd7ce5SGreg Kroah-Hartman priv->file = file; 20896fd7ce5SGreg Kroah-Hartman file->private_data = priv; 20996fd7ce5SGreg Kroah-Hartman 21096fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 21196fd7ce5SGreg Kroah-Hartman list_add(&priv->list, &tty->tty_files); 21296fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 21396fd7ce5SGreg Kroah-Hartman 21496fd7ce5SGreg Kroah-Hartman return 0; 21596fd7ce5SGreg Kroah-Hartman } 21696fd7ce5SGreg Kroah-Hartman 21796fd7ce5SGreg Kroah-Hartman /* Delete file from its tty */ 21896fd7ce5SGreg Kroah-Hartman void tty_del_file(struct file *file) 21996fd7ce5SGreg Kroah-Hartman { 22096fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv = file->private_data; 22196fd7ce5SGreg Kroah-Hartman 22296fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 22396fd7ce5SGreg Kroah-Hartman list_del(&priv->list); 22496fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 22596fd7ce5SGreg Kroah-Hartman file->private_data = NULL; 22696fd7ce5SGreg Kroah-Hartman kfree(priv); 22796fd7ce5SGreg Kroah-Hartman } 22896fd7ce5SGreg Kroah-Hartman 22996fd7ce5SGreg Kroah-Hartman 23096fd7ce5SGreg Kroah-Hartman #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) 23196fd7ce5SGreg Kroah-Hartman 23296fd7ce5SGreg Kroah-Hartman /** 23396fd7ce5SGreg Kroah-Hartman * tty_name - return tty naming 23496fd7ce5SGreg Kroah-Hartman * @tty: tty structure 23596fd7ce5SGreg Kroah-Hartman * @buf: buffer for output 23696fd7ce5SGreg Kroah-Hartman * 23796fd7ce5SGreg Kroah-Hartman * Convert a tty structure into a name. The name reflects the kernel 23896fd7ce5SGreg Kroah-Hartman * naming policy and if udev is in use may not reflect user space 23996fd7ce5SGreg Kroah-Hartman * 24096fd7ce5SGreg Kroah-Hartman * Locking: none 24196fd7ce5SGreg Kroah-Hartman */ 24296fd7ce5SGreg Kroah-Hartman 24396fd7ce5SGreg Kroah-Hartman char *tty_name(struct tty_struct *tty, char *buf) 24496fd7ce5SGreg Kroah-Hartman { 24596fd7ce5SGreg Kroah-Hartman if (!tty) /* Hmm. NULL pointer. That's fun. */ 24696fd7ce5SGreg Kroah-Hartman strcpy(buf, "NULL tty"); 24796fd7ce5SGreg Kroah-Hartman else 24896fd7ce5SGreg Kroah-Hartman strcpy(buf, tty->name); 24996fd7ce5SGreg Kroah-Hartman return buf; 25096fd7ce5SGreg Kroah-Hartman } 25196fd7ce5SGreg Kroah-Hartman 25296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_name); 25396fd7ce5SGreg Kroah-Hartman 25496fd7ce5SGreg Kroah-Hartman int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, 25596fd7ce5SGreg Kroah-Hartman const char *routine) 25696fd7ce5SGreg Kroah-Hartman { 25796fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK 25896fd7ce5SGreg Kroah-Hartman if (!tty) { 25996fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING 26096fd7ce5SGreg Kroah-Hartman "null TTY for (%d:%d) in %s\n", 26196fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 26296fd7ce5SGreg Kroah-Hartman return 1; 26396fd7ce5SGreg Kroah-Hartman } 26496fd7ce5SGreg Kroah-Hartman if (tty->magic != TTY_MAGIC) { 26596fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING 26696fd7ce5SGreg Kroah-Hartman "bad magic number for tty struct (%d:%d) in %s\n", 26796fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 26896fd7ce5SGreg Kroah-Hartman return 1; 26996fd7ce5SGreg Kroah-Hartman } 27096fd7ce5SGreg Kroah-Hartman #endif 27196fd7ce5SGreg Kroah-Hartman return 0; 27296fd7ce5SGreg Kroah-Hartman } 27396fd7ce5SGreg Kroah-Hartman 27496fd7ce5SGreg Kroah-Hartman static int check_tty_count(struct tty_struct *tty, const char *routine) 27596fd7ce5SGreg Kroah-Hartman { 27696fd7ce5SGreg Kroah-Hartman #ifdef CHECK_TTY_COUNT 27796fd7ce5SGreg Kroah-Hartman struct list_head *p; 27896fd7ce5SGreg Kroah-Hartman int count = 0; 27996fd7ce5SGreg Kroah-Hartman 28096fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 28196fd7ce5SGreg Kroah-Hartman list_for_each(p, &tty->tty_files) { 28296fd7ce5SGreg Kroah-Hartman count++; 28396fd7ce5SGreg Kroah-Hartman } 28496fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 28596fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 28696fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_SLAVE && 28796fd7ce5SGreg Kroah-Hartman tty->link && tty->link->count) 28896fd7ce5SGreg Kroah-Hartman count++; 28996fd7ce5SGreg Kroah-Hartman if (tty->count != count) { 29096fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) " 29196fd7ce5SGreg Kroah-Hartman "!= #fd's(%d) in %s\n", 29296fd7ce5SGreg Kroah-Hartman tty->name, tty->count, count, routine); 29396fd7ce5SGreg Kroah-Hartman return count; 29496fd7ce5SGreg Kroah-Hartman } 29596fd7ce5SGreg Kroah-Hartman #endif 29696fd7ce5SGreg Kroah-Hartman return 0; 29796fd7ce5SGreg Kroah-Hartman } 29896fd7ce5SGreg Kroah-Hartman 29996fd7ce5SGreg Kroah-Hartman /** 30096fd7ce5SGreg Kroah-Hartman * get_tty_driver - find device of a tty 30196fd7ce5SGreg Kroah-Hartman * @dev_t: device identifier 30296fd7ce5SGreg Kroah-Hartman * @index: returns the index of the tty 30396fd7ce5SGreg Kroah-Hartman * 30496fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a device number 30596fd7ce5SGreg Kroah-Hartman * and also passes back the index number. 30696fd7ce5SGreg Kroah-Hartman * 30796fd7ce5SGreg Kroah-Hartman * Locking: caller must hold tty_mutex 30896fd7ce5SGreg Kroah-Hartman */ 30996fd7ce5SGreg Kroah-Hartman 31096fd7ce5SGreg Kroah-Hartman static struct tty_driver *get_tty_driver(dev_t device, int *index) 31196fd7ce5SGreg Kroah-Hartman { 31296fd7ce5SGreg Kroah-Hartman struct tty_driver *p; 31396fd7ce5SGreg Kroah-Hartman 31496fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 31596fd7ce5SGreg Kroah-Hartman dev_t base = MKDEV(p->major, p->minor_start); 31696fd7ce5SGreg Kroah-Hartman if (device < base || device >= base + p->num) 31796fd7ce5SGreg Kroah-Hartman continue; 31896fd7ce5SGreg Kroah-Hartman *index = device - base; 31996fd7ce5SGreg Kroah-Hartman return tty_driver_kref_get(p); 32096fd7ce5SGreg Kroah-Hartman } 32196fd7ce5SGreg Kroah-Hartman return NULL; 32296fd7ce5SGreg Kroah-Hartman } 32396fd7ce5SGreg Kroah-Hartman 32496fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 32596fd7ce5SGreg Kroah-Hartman 32696fd7ce5SGreg Kroah-Hartman /** 32796fd7ce5SGreg Kroah-Hartman * tty_find_polling_driver - find device of a polled tty 32896fd7ce5SGreg Kroah-Hartman * @name: name string to match 32996fd7ce5SGreg Kroah-Hartman * @line: pointer to resulting tty line nr 33096fd7ce5SGreg Kroah-Hartman * 33196fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a name 33296fd7ce5SGreg Kroah-Hartman * and the condition that the tty driver is capable of polled 33396fd7ce5SGreg Kroah-Hartman * operation. 33496fd7ce5SGreg Kroah-Hartman */ 33596fd7ce5SGreg Kroah-Hartman struct tty_driver *tty_find_polling_driver(char *name, int *line) 33696fd7ce5SGreg Kroah-Hartman { 33796fd7ce5SGreg Kroah-Hartman struct tty_driver *p, *res = NULL; 33896fd7ce5SGreg Kroah-Hartman int tty_line = 0; 33996fd7ce5SGreg Kroah-Hartman int len; 34096fd7ce5SGreg Kroah-Hartman char *str, *stp; 34196fd7ce5SGreg Kroah-Hartman 34296fd7ce5SGreg Kroah-Hartman for (str = name; *str; str++) 34396fd7ce5SGreg Kroah-Hartman if ((*str >= '0' && *str <= '9') || *str == ',') 34496fd7ce5SGreg Kroah-Hartman break; 34596fd7ce5SGreg Kroah-Hartman if (!*str) 34696fd7ce5SGreg Kroah-Hartman return NULL; 34796fd7ce5SGreg Kroah-Hartman 34896fd7ce5SGreg Kroah-Hartman len = str - name; 34996fd7ce5SGreg Kroah-Hartman tty_line = simple_strtoul(str, &str, 10); 35096fd7ce5SGreg Kroah-Hartman 35196fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 35296fd7ce5SGreg Kroah-Hartman /* Search through the tty devices to look for a match */ 35396fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 35496fd7ce5SGreg Kroah-Hartman if (strncmp(name, p->name, len) != 0) 35596fd7ce5SGreg Kroah-Hartman continue; 35696fd7ce5SGreg Kroah-Hartman stp = str; 35796fd7ce5SGreg Kroah-Hartman if (*stp == ',') 35896fd7ce5SGreg Kroah-Hartman stp++; 35996fd7ce5SGreg Kroah-Hartman if (*stp == '\0') 36096fd7ce5SGreg Kroah-Hartman stp = NULL; 36196fd7ce5SGreg Kroah-Hartman 36296fd7ce5SGreg Kroah-Hartman if (tty_line >= 0 && tty_line < p->num && p->ops && 36396fd7ce5SGreg Kroah-Hartman p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { 36496fd7ce5SGreg Kroah-Hartman res = tty_driver_kref_get(p); 36596fd7ce5SGreg Kroah-Hartman *line = tty_line; 36696fd7ce5SGreg Kroah-Hartman break; 36796fd7ce5SGreg Kroah-Hartman } 36896fd7ce5SGreg Kroah-Hartman } 36996fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 37096fd7ce5SGreg Kroah-Hartman 37196fd7ce5SGreg Kroah-Hartman return res; 37296fd7ce5SGreg Kroah-Hartman } 37396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_find_polling_driver); 37496fd7ce5SGreg Kroah-Hartman #endif 37596fd7ce5SGreg Kroah-Hartman 37696fd7ce5SGreg Kroah-Hartman /** 37796fd7ce5SGreg Kroah-Hartman * tty_check_change - check for POSIX terminal changes 37896fd7ce5SGreg Kroah-Hartman * @tty: tty to check 37996fd7ce5SGreg Kroah-Hartman * 38096fd7ce5SGreg Kroah-Hartman * If we try to write to, or set the state of, a terminal and we're 38196fd7ce5SGreg Kroah-Hartman * not in the foreground, send a SIGTTOU. If the signal is blocked or 38296fd7ce5SGreg Kroah-Hartman * ignored, go ahead and perform the operation. (POSIX 7.2) 38396fd7ce5SGreg Kroah-Hartman * 38496fd7ce5SGreg Kroah-Hartman * Locking: ctrl_lock 38596fd7ce5SGreg Kroah-Hartman */ 38696fd7ce5SGreg Kroah-Hartman 38796fd7ce5SGreg Kroah-Hartman int tty_check_change(struct tty_struct *tty) 38896fd7ce5SGreg Kroah-Hartman { 38996fd7ce5SGreg Kroah-Hartman unsigned long flags; 39096fd7ce5SGreg Kroah-Hartman int ret = 0; 39196fd7ce5SGreg Kroah-Hartman 39296fd7ce5SGreg Kroah-Hartman if (current->signal->tty != tty) 39396fd7ce5SGreg Kroah-Hartman return 0; 39496fd7ce5SGreg Kroah-Hartman 39596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 39696fd7ce5SGreg Kroah-Hartman 39796fd7ce5SGreg Kroah-Hartman if (!tty->pgrp) { 39896fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n"); 39996fd7ce5SGreg Kroah-Hartman goto out_unlock; 40096fd7ce5SGreg Kroah-Hartman } 40196fd7ce5SGreg Kroah-Hartman if (task_pgrp(current) == tty->pgrp) 40296fd7ce5SGreg Kroah-Hartman goto out_unlock; 40396fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 40496fd7ce5SGreg Kroah-Hartman if (is_ignored(SIGTTOU)) 40596fd7ce5SGreg Kroah-Hartman goto out; 40696fd7ce5SGreg Kroah-Hartman if (is_current_pgrp_orphaned()) { 40796fd7ce5SGreg Kroah-Hartman ret = -EIO; 40896fd7ce5SGreg Kroah-Hartman goto out; 40996fd7ce5SGreg Kroah-Hartman } 41096fd7ce5SGreg Kroah-Hartman kill_pgrp(task_pgrp(current), SIGTTOU, 1); 41196fd7ce5SGreg Kroah-Hartman set_thread_flag(TIF_SIGPENDING); 41296fd7ce5SGreg Kroah-Hartman ret = -ERESTARTSYS; 41396fd7ce5SGreg Kroah-Hartman out: 41496fd7ce5SGreg Kroah-Hartman return ret; 41596fd7ce5SGreg Kroah-Hartman out_unlock: 41696fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 41796fd7ce5SGreg Kroah-Hartman return ret; 41896fd7ce5SGreg Kroah-Hartman } 41996fd7ce5SGreg Kroah-Hartman 42096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_check_change); 42196fd7ce5SGreg Kroah-Hartman 42296fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_read(struct file *file, char __user *buf, 42396fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 42496fd7ce5SGreg Kroah-Hartman { 42596fd7ce5SGreg Kroah-Hartman return 0; 42696fd7ce5SGreg Kroah-Hartman } 42796fd7ce5SGreg Kroah-Hartman 42896fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_write(struct file *file, const char __user *buf, 42996fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 43096fd7ce5SGreg Kroah-Hartman { 43196fd7ce5SGreg Kroah-Hartman return -EIO; 43296fd7ce5SGreg Kroah-Hartman } 43396fd7ce5SGreg Kroah-Hartman 43496fd7ce5SGreg Kroah-Hartman /* No kernel lock held - none needed ;) */ 43596fd7ce5SGreg Kroah-Hartman static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait) 43696fd7ce5SGreg Kroah-Hartman { 43796fd7ce5SGreg Kroah-Hartman return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM; 43896fd7ce5SGreg Kroah-Hartman } 43996fd7ce5SGreg Kroah-Hartman 44096fd7ce5SGreg Kroah-Hartman static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, 44196fd7ce5SGreg Kroah-Hartman unsigned long arg) 44296fd7ce5SGreg Kroah-Hartman { 44396fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 44496fd7ce5SGreg Kroah-Hartman } 44596fd7ce5SGreg Kroah-Hartman 44696fd7ce5SGreg Kroah-Hartman static long hung_up_tty_compat_ioctl(struct file *file, 44796fd7ce5SGreg Kroah-Hartman unsigned int cmd, unsigned long arg) 44896fd7ce5SGreg Kroah-Hartman { 44996fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 45096fd7ce5SGreg Kroah-Hartman } 45196fd7ce5SGreg Kroah-Hartman 45296fd7ce5SGreg Kroah-Hartman static const struct file_operations tty_fops = { 45396fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 45496fd7ce5SGreg Kroah-Hartman .read = tty_read, 45596fd7ce5SGreg Kroah-Hartman .write = tty_write, 45696fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 45796fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 45896fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 45996fd7ce5SGreg Kroah-Hartman .open = tty_open, 46096fd7ce5SGreg Kroah-Hartman .release = tty_release, 46196fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 46296fd7ce5SGreg Kroah-Hartman }; 46396fd7ce5SGreg Kroah-Hartman 46496fd7ce5SGreg Kroah-Hartman static const struct file_operations console_fops = { 46596fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 46696fd7ce5SGreg Kroah-Hartman .read = tty_read, 46796fd7ce5SGreg Kroah-Hartman .write = redirected_tty_write, 46896fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 46996fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 47096fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 47196fd7ce5SGreg Kroah-Hartman .open = tty_open, 47296fd7ce5SGreg Kroah-Hartman .release = tty_release, 47396fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 47496fd7ce5SGreg Kroah-Hartman }; 47596fd7ce5SGreg Kroah-Hartman 47696fd7ce5SGreg Kroah-Hartman static const struct file_operations hung_up_tty_fops = { 47796fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 47896fd7ce5SGreg Kroah-Hartman .read = hung_up_tty_read, 47996fd7ce5SGreg Kroah-Hartman .write = hung_up_tty_write, 48096fd7ce5SGreg Kroah-Hartman .poll = hung_up_tty_poll, 48196fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = hung_up_tty_ioctl, 48296fd7ce5SGreg Kroah-Hartman .compat_ioctl = hung_up_tty_compat_ioctl, 48396fd7ce5SGreg Kroah-Hartman .release = tty_release, 48496fd7ce5SGreg Kroah-Hartman }; 48596fd7ce5SGreg Kroah-Hartman 48696fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(redirect_lock); 48796fd7ce5SGreg Kroah-Hartman static struct file *redirect; 48896fd7ce5SGreg Kroah-Hartman 48996fd7ce5SGreg Kroah-Hartman /** 49096fd7ce5SGreg Kroah-Hartman * tty_wakeup - request more data 49196fd7ce5SGreg Kroah-Hartman * @tty: terminal 49296fd7ce5SGreg Kroah-Hartman * 49396fd7ce5SGreg Kroah-Hartman * Internal and external helper for wakeups of tty. This function 49496fd7ce5SGreg Kroah-Hartman * informs the line discipline if present that the driver is ready 49596fd7ce5SGreg Kroah-Hartman * to receive more output data. 49696fd7ce5SGreg Kroah-Hartman */ 49796fd7ce5SGreg Kroah-Hartman 49896fd7ce5SGreg Kroah-Hartman void tty_wakeup(struct tty_struct *tty) 49996fd7ce5SGreg Kroah-Hartman { 50096fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 50196fd7ce5SGreg Kroah-Hartman 50296fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { 50396fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref(tty); 50496fd7ce5SGreg Kroah-Hartman if (ld) { 50596fd7ce5SGreg Kroah-Hartman if (ld->ops->write_wakeup) 50696fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup(tty); 50796fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 50896fd7ce5SGreg Kroah-Hartman } 50996fd7ce5SGreg Kroah-Hartman } 51096fd7ce5SGreg Kroah-Hartman wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 51196fd7ce5SGreg Kroah-Hartman } 51296fd7ce5SGreg Kroah-Hartman 51396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_wakeup); 51496fd7ce5SGreg Kroah-Hartman 51596fd7ce5SGreg Kroah-Hartman /** 51696fd7ce5SGreg Kroah-Hartman * __tty_hangup - actual handler for hangup events 51796fd7ce5SGreg Kroah-Hartman * @work: tty device 51896fd7ce5SGreg Kroah-Hartman * 51996fd7ce5SGreg Kroah-Hartman * This can be called by the "eventd" kernel thread. That is process 52096fd7ce5SGreg Kroah-Hartman * synchronous but doesn't hold any locks, so we need to make sure we 52196fd7ce5SGreg Kroah-Hartman * have the appropriate locks for what we're doing. 52296fd7ce5SGreg Kroah-Hartman * 52396fd7ce5SGreg Kroah-Hartman * The hangup event clears any pending redirections onto the hung up 52496fd7ce5SGreg Kroah-Hartman * device. It ensures future writes will error and it does the needed 52596fd7ce5SGreg Kroah-Hartman * line discipline hangup and signal delivery. The tty object itself 52696fd7ce5SGreg Kroah-Hartman * remains intact. 52796fd7ce5SGreg Kroah-Hartman * 52896fd7ce5SGreg Kroah-Hartman * Locking: 52996fd7ce5SGreg Kroah-Hartman * BTM 53096fd7ce5SGreg Kroah-Hartman * redirect lock for undoing redirection 53196fd7ce5SGreg Kroah-Hartman * file list lock for manipulating list of ttys 53296fd7ce5SGreg Kroah-Hartman * tty_ldisc_lock from called functions 53396fd7ce5SGreg Kroah-Hartman * termios_mutex resetting termios data 53496fd7ce5SGreg Kroah-Hartman * tasklist_lock to walk task list for hangup event 53596fd7ce5SGreg Kroah-Hartman * ->siglock to protect ->signal/->sighand 53696fd7ce5SGreg Kroah-Hartman */ 53796fd7ce5SGreg Kroah-Hartman void __tty_hangup(struct tty_struct *tty) 53896fd7ce5SGreg Kroah-Hartman { 53996fd7ce5SGreg Kroah-Hartman struct file *cons_filp = NULL; 54096fd7ce5SGreg Kroah-Hartman struct file *filp, *f = NULL; 54196fd7ce5SGreg Kroah-Hartman struct task_struct *p; 54296fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 54396fd7ce5SGreg Kroah-Hartman int closecount = 0, n; 54496fd7ce5SGreg Kroah-Hartman unsigned long flags; 54596fd7ce5SGreg Kroah-Hartman int refs = 0; 54696fd7ce5SGreg Kroah-Hartman 54796fd7ce5SGreg Kroah-Hartman if (!tty) 54896fd7ce5SGreg Kroah-Hartman return; 54996fd7ce5SGreg Kroah-Hartman 55096fd7ce5SGreg Kroah-Hartman 55196fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 55296fd7ce5SGreg Kroah-Hartman if (redirect && file_tty(redirect) == tty) { 55396fd7ce5SGreg Kroah-Hartman f = redirect; 55496fd7ce5SGreg Kroah-Hartman redirect = NULL; 55596fd7ce5SGreg Kroah-Hartman } 55696fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 55796fd7ce5SGreg Kroah-Hartman 55896fd7ce5SGreg Kroah-Hartman tty_lock(); 55996fd7ce5SGreg Kroah-Hartman 560acfa747bSJiri Slaby /* some functions below drop BTM, so we need this bit */ 561acfa747bSJiri Slaby set_bit(TTY_HUPPING, &tty->flags); 562acfa747bSJiri Slaby 56396fd7ce5SGreg Kroah-Hartman /* inuse_filps is protected by the single tty lock, 56496fd7ce5SGreg Kroah-Hartman this really needs to change if we want to flush the 56596fd7ce5SGreg Kroah-Hartman workqueue with the lock held */ 56696fd7ce5SGreg Kroah-Hartman check_tty_count(tty, "tty_hangup"); 56796fd7ce5SGreg Kroah-Hartman 56896fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 56996fd7ce5SGreg Kroah-Hartman /* This breaks for file handles being sent over AF_UNIX sockets ? */ 57096fd7ce5SGreg Kroah-Hartman list_for_each_entry(priv, &tty->tty_files, list) { 57196fd7ce5SGreg Kroah-Hartman filp = priv->file; 57296fd7ce5SGreg Kroah-Hartman if (filp->f_op->write == redirected_tty_write) 57396fd7ce5SGreg Kroah-Hartman cons_filp = filp; 57496fd7ce5SGreg Kroah-Hartman if (filp->f_op->write != tty_write) 57596fd7ce5SGreg Kroah-Hartman continue; 57696fd7ce5SGreg Kroah-Hartman closecount++; 57796fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); /* can't block */ 57896fd7ce5SGreg Kroah-Hartman filp->f_op = &hung_up_tty_fops; 57996fd7ce5SGreg Kroah-Hartman } 58096fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 58196fd7ce5SGreg Kroah-Hartman 582acfa747bSJiri Slaby /* 583acfa747bSJiri Slaby * it drops BTM and thus races with reopen 584acfa747bSJiri Slaby * we protect the race by TTY_HUPPING 585acfa747bSJiri Slaby */ 58696fd7ce5SGreg Kroah-Hartman tty_ldisc_hangup(tty); 58796fd7ce5SGreg Kroah-Hartman 58896fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 58996fd7ce5SGreg Kroah-Hartman if (tty->session) { 59096fd7ce5SGreg Kroah-Hartman do_each_pid_task(tty->session, PIDTYPE_SID, p) { 59196fd7ce5SGreg Kroah-Hartman spin_lock_irq(&p->sighand->siglock); 59296fd7ce5SGreg Kroah-Hartman if (p->signal->tty == tty) { 59396fd7ce5SGreg Kroah-Hartman p->signal->tty = NULL; 59496fd7ce5SGreg Kroah-Hartman /* We defer the dereferences outside fo 59596fd7ce5SGreg Kroah-Hartman the tasklist lock */ 59696fd7ce5SGreg Kroah-Hartman refs++; 59796fd7ce5SGreg Kroah-Hartman } 59896fd7ce5SGreg Kroah-Hartman if (!p->signal->leader) { 59996fd7ce5SGreg Kroah-Hartman spin_unlock_irq(&p->sighand->siglock); 60096fd7ce5SGreg Kroah-Hartman continue; 60196fd7ce5SGreg Kroah-Hartman } 60296fd7ce5SGreg Kroah-Hartman __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p); 60396fd7ce5SGreg Kroah-Hartman __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p); 60496fd7ce5SGreg Kroah-Hartman put_pid(p->signal->tty_old_pgrp); /* A noop */ 60596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 60696fd7ce5SGreg Kroah-Hartman if (tty->pgrp) 60796fd7ce5SGreg Kroah-Hartman p->signal->tty_old_pgrp = get_pid(tty->pgrp); 60896fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 60996fd7ce5SGreg Kroah-Hartman spin_unlock_irq(&p->sighand->siglock); 61096fd7ce5SGreg Kroah-Hartman } while_each_pid_task(tty->session, PIDTYPE_SID, p); 61196fd7ce5SGreg Kroah-Hartman } 61296fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 61396fd7ce5SGreg Kroah-Hartman 61496fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 61596fd7ce5SGreg Kroah-Hartman clear_bit(TTY_THROTTLED, &tty->flags); 61696fd7ce5SGreg Kroah-Hartman clear_bit(TTY_PUSH, &tty->flags); 61796fd7ce5SGreg Kroah-Hartman clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 61896fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 61996fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 62096fd7ce5SGreg Kroah-Hartman tty->session = NULL; 62196fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 62296fd7ce5SGreg Kroah-Hartman tty->ctrl_status = 0; 62396fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 62496fd7ce5SGreg Kroah-Hartman 62596fd7ce5SGreg Kroah-Hartman /* Account for the p->signal references we killed */ 62696fd7ce5SGreg Kroah-Hartman while (refs--) 62796fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 62896fd7ce5SGreg Kroah-Hartman 62996fd7ce5SGreg Kroah-Hartman /* 63096fd7ce5SGreg Kroah-Hartman * If one of the devices matches a console pointer, we 63196fd7ce5SGreg Kroah-Hartman * cannot just call hangup() because that will cause 63296fd7ce5SGreg Kroah-Hartman * tty->count and state->count to go out of sync. 63396fd7ce5SGreg Kroah-Hartman * So we just call close() the right number of times. 63496fd7ce5SGreg Kroah-Hartman */ 63596fd7ce5SGreg Kroah-Hartman if (cons_filp) { 63696fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 63796fd7ce5SGreg Kroah-Hartman for (n = 0; n < closecount; n++) 63896fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, cons_filp); 63996fd7ce5SGreg Kroah-Hartman } else if (tty->ops->hangup) 64096fd7ce5SGreg Kroah-Hartman (tty->ops->hangup)(tty); 64196fd7ce5SGreg Kroah-Hartman /* 64296fd7ce5SGreg Kroah-Hartman * We don't want to have driver/ldisc interactions beyond 64396fd7ce5SGreg Kroah-Hartman * the ones we did here. The driver layer expects no 64496fd7ce5SGreg Kroah-Hartman * calls after ->hangup() from the ldisc side. However we 64596fd7ce5SGreg Kroah-Hartman * can't yet guarantee all that. 64696fd7ce5SGreg Kroah-Hartman */ 64796fd7ce5SGreg Kroah-Hartman set_bit(TTY_HUPPED, &tty->flags); 648acfa747bSJiri Slaby clear_bit(TTY_HUPPING, &tty->flags); 64996fd7ce5SGreg Kroah-Hartman tty_ldisc_enable(tty); 65096fd7ce5SGreg Kroah-Hartman 65196fd7ce5SGreg Kroah-Hartman tty_unlock(); 65296fd7ce5SGreg Kroah-Hartman 65396fd7ce5SGreg Kroah-Hartman if (f) 65496fd7ce5SGreg Kroah-Hartman fput(f); 65596fd7ce5SGreg Kroah-Hartman } 65696fd7ce5SGreg Kroah-Hartman 65796fd7ce5SGreg Kroah-Hartman static void do_tty_hangup(struct work_struct *work) 65896fd7ce5SGreg Kroah-Hartman { 65996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 66096fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 66196fd7ce5SGreg Kroah-Hartman 66296fd7ce5SGreg Kroah-Hartman __tty_hangup(tty); 66396fd7ce5SGreg Kroah-Hartman } 66496fd7ce5SGreg Kroah-Hartman 66596fd7ce5SGreg Kroah-Hartman /** 66696fd7ce5SGreg Kroah-Hartman * tty_hangup - trigger a hangup event 66796fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 66896fd7ce5SGreg Kroah-Hartman * 66996fd7ce5SGreg Kroah-Hartman * A carrier loss (virtual or otherwise) has occurred on this like 67096fd7ce5SGreg Kroah-Hartman * schedule a hangup sequence to run after this event. 67196fd7ce5SGreg Kroah-Hartman */ 67296fd7ce5SGreg Kroah-Hartman 67396fd7ce5SGreg Kroah-Hartman void tty_hangup(struct tty_struct *tty) 67496fd7ce5SGreg Kroah-Hartman { 67596fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 67696fd7ce5SGreg Kroah-Hartman char buf[64]; 67796fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf)); 67896fd7ce5SGreg Kroah-Hartman #endif 67996fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 68096fd7ce5SGreg Kroah-Hartman } 68196fd7ce5SGreg Kroah-Hartman 68296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hangup); 68396fd7ce5SGreg Kroah-Hartman 68496fd7ce5SGreg Kroah-Hartman /** 68596fd7ce5SGreg Kroah-Hartman * tty_vhangup - process vhangup 68696fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 68796fd7ce5SGreg Kroah-Hartman * 68896fd7ce5SGreg Kroah-Hartman * The user has asked via system call for the terminal to be hung up. 68996fd7ce5SGreg Kroah-Hartman * We do this synchronously so that when the syscall returns the process 69096fd7ce5SGreg Kroah-Hartman * is complete. That guarantee is necessary for security reasons. 69196fd7ce5SGreg Kroah-Hartman */ 69296fd7ce5SGreg Kroah-Hartman 69396fd7ce5SGreg Kroah-Hartman void tty_vhangup(struct tty_struct *tty) 69496fd7ce5SGreg Kroah-Hartman { 69596fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 69696fd7ce5SGreg Kroah-Hartman char buf[64]; 69796fd7ce5SGreg Kroah-Hartman 69896fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); 69996fd7ce5SGreg Kroah-Hartman #endif 70096fd7ce5SGreg Kroah-Hartman __tty_hangup(tty); 70196fd7ce5SGreg Kroah-Hartman } 70296fd7ce5SGreg Kroah-Hartman 70396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_vhangup); 70496fd7ce5SGreg Kroah-Hartman 70596fd7ce5SGreg Kroah-Hartman 70696fd7ce5SGreg Kroah-Hartman /** 70796fd7ce5SGreg Kroah-Hartman * tty_vhangup_self - process vhangup for own ctty 70896fd7ce5SGreg Kroah-Hartman * 70996fd7ce5SGreg Kroah-Hartman * Perform a vhangup on the current controlling tty 71096fd7ce5SGreg Kroah-Hartman */ 71196fd7ce5SGreg Kroah-Hartman 71296fd7ce5SGreg Kroah-Hartman void tty_vhangup_self(void) 71396fd7ce5SGreg Kroah-Hartman { 71496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 71596fd7ce5SGreg Kroah-Hartman 71696fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 71796fd7ce5SGreg Kroah-Hartman if (tty) { 71896fd7ce5SGreg Kroah-Hartman tty_vhangup(tty); 71996fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 72096fd7ce5SGreg Kroah-Hartman } 72196fd7ce5SGreg Kroah-Hartman } 72296fd7ce5SGreg Kroah-Hartman 72396fd7ce5SGreg Kroah-Hartman /** 72496fd7ce5SGreg Kroah-Hartman * tty_hung_up_p - was tty hung up 72596fd7ce5SGreg Kroah-Hartman * @filp: file pointer of tty 72696fd7ce5SGreg Kroah-Hartman * 72796fd7ce5SGreg Kroah-Hartman * Return true if the tty has been subject to a vhangup or a carrier 72896fd7ce5SGreg Kroah-Hartman * loss 72996fd7ce5SGreg Kroah-Hartman */ 73096fd7ce5SGreg Kroah-Hartman 73196fd7ce5SGreg Kroah-Hartman int tty_hung_up_p(struct file *filp) 73296fd7ce5SGreg Kroah-Hartman { 73396fd7ce5SGreg Kroah-Hartman return (filp->f_op == &hung_up_tty_fops); 73496fd7ce5SGreg Kroah-Hartman } 73596fd7ce5SGreg Kroah-Hartman 73696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hung_up_p); 73796fd7ce5SGreg Kroah-Hartman 73896fd7ce5SGreg Kroah-Hartman static void session_clear_tty(struct pid *session) 73996fd7ce5SGreg Kroah-Hartman { 74096fd7ce5SGreg Kroah-Hartman struct task_struct *p; 74196fd7ce5SGreg Kroah-Hartman do_each_pid_task(session, PIDTYPE_SID, p) { 74296fd7ce5SGreg Kroah-Hartman proc_clear_tty(p); 74396fd7ce5SGreg Kroah-Hartman } while_each_pid_task(session, PIDTYPE_SID, p); 74496fd7ce5SGreg Kroah-Hartman } 74596fd7ce5SGreg Kroah-Hartman 74696fd7ce5SGreg Kroah-Hartman /** 74796fd7ce5SGreg Kroah-Hartman * disassociate_ctty - disconnect controlling tty 74896fd7ce5SGreg Kroah-Hartman * @on_exit: true if exiting so need to "hang up" the session 74996fd7ce5SGreg Kroah-Hartman * 75096fd7ce5SGreg Kroah-Hartman * This function is typically called only by the session leader, when 75196fd7ce5SGreg Kroah-Hartman * it wants to disassociate itself from its controlling tty. 75296fd7ce5SGreg Kroah-Hartman * 75396fd7ce5SGreg Kroah-Hartman * It performs the following functions: 75496fd7ce5SGreg Kroah-Hartman * (1) Sends a SIGHUP and SIGCONT to the foreground process group 75596fd7ce5SGreg Kroah-Hartman * (2) Clears the tty from being controlling the session 75696fd7ce5SGreg Kroah-Hartman * (3) Clears the controlling tty for all processes in the 75796fd7ce5SGreg Kroah-Hartman * session group. 75896fd7ce5SGreg Kroah-Hartman * 75996fd7ce5SGreg Kroah-Hartman * The argument on_exit is set to 1 if called when a process is 76096fd7ce5SGreg Kroah-Hartman * exiting; it is 0 if called by the ioctl TIOCNOTTY. 76196fd7ce5SGreg Kroah-Hartman * 76296fd7ce5SGreg Kroah-Hartman * Locking: 76396fd7ce5SGreg Kroah-Hartman * BTM is taken for hysterical raisins, and held when 76496fd7ce5SGreg Kroah-Hartman * called from no_tty(). 76596fd7ce5SGreg Kroah-Hartman * tty_mutex is taken to protect tty 76696fd7ce5SGreg Kroah-Hartman * ->siglock is taken to protect ->signal/->sighand 76796fd7ce5SGreg Kroah-Hartman * tasklist_lock is taken to walk process list for sessions 76896fd7ce5SGreg Kroah-Hartman * ->siglock is taken to protect ->signal/->sighand 76996fd7ce5SGreg Kroah-Hartman */ 77096fd7ce5SGreg Kroah-Hartman 77196fd7ce5SGreg Kroah-Hartman void disassociate_ctty(int on_exit) 77296fd7ce5SGreg Kroah-Hartman { 77396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 77496fd7ce5SGreg Kroah-Hartman struct pid *tty_pgrp = NULL; 77596fd7ce5SGreg Kroah-Hartman 77696fd7ce5SGreg Kroah-Hartman if (!current->signal->leader) 77796fd7ce5SGreg Kroah-Hartman return; 77896fd7ce5SGreg Kroah-Hartman 77996fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 78096fd7ce5SGreg Kroah-Hartman if (tty) { 78196fd7ce5SGreg Kroah-Hartman tty_pgrp = get_pid(tty->pgrp); 78296fd7ce5SGreg Kroah-Hartman if (on_exit) { 78396fd7ce5SGreg Kroah-Hartman if (tty->driver->type != TTY_DRIVER_TYPE_PTY) 78496fd7ce5SGreg Kroah-Hartman tty_vhangup(tty); 78596fd7ce5SGreg Kroah-Hartman } 78696fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 78796fd7ce5SGreg Kroah-Hartman } else if (on_exit) { 78896fd7ce5SGreg Kroah-Hartman struct pid *old_pgrp; 78996fd7ce5SGreg Kroah-Hartman spin_lock_irq(¤t->sighand->siglock); 79096fd7ce5SGreg Kroah-Hartman old_pgrp = current->signal->tty_old_pgrp; 79196fd7ce5SGreg Kroah-Hartman current->signal->tty_old_pgrp = NULL; 79296fd7ce5SGreg Kroah-Hartman spin_unlock_irq(¤t->sighand->siglock); 79396fd7ce5SGreg Kroah-Hartman if (old_pgrp) { 79496fd7ce5SGreg Kroah-Hartman kill_pgrp(old_pgrp, SIGHUP, on_exit); 79596fd7ce5SGreg Kroah-Hartman kill_pgrp(old_pgrp, SIGCONT, on_exit); 79696fd7ce5SGreg Kroah-Hartman put_pid(old_pgrp); 79796fd7ce5SGreg Kroah-Hartman } 79896fd7ce5SGreg Kroah-Hartman return; 79996fd7ce5SGreg Kroah-Hartman } 80096fd7ce5SGreg Kroah-Hartman if (tty_pgrp) { 80196fd7ce5SGreg Kroah-Hartman kill_pgrp(tty_pgrp, SIGHUP, on_exit); 80296fd7ce5SGreg Kroah-Hartman if (!on_exit) 80396fd7ce5SGreg Kroah-Hartman kill_pgrp(tty_pgrp, SIGCONT, on_exit); 80496fd7ce5SGreg Kroah-Hartman put_pid(tty_pgrp); 80596fd7ce5SGreg Kroah-Hartman } 80696fd7ce5SGreg Kroah-Hartman 80796fd7ce5SGreg Kroah-Hartman spin_lock_irq(¤t->sighand->siglock); 80896fd7ce5SGreg Kroah-Hartman put_pid(current->signal->tty_old_pgrp); 80996fd7ce5SGreg Kroah-Hartman current->signal->tty_old_pgrp = NULL; 81096fd7ce5SGreg Kroah-Hartman spin_unlock_irq(¤t->sighand->siglock); 81196fd7ce5SGreg Kroah-Hartman 81296fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 81396fd7ce5SGreg Kroah-Hartman if (tty) { 81496fd7ce5SGreg Kroah-Hartman unsigned long flags; 81596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 81696fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 81796fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 81896fd7ce5SGreg Kroah-Hartman tty->session = NULL; 81996fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 82096fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 82196fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 82296fd7ce5SGreg Kroah-Hartman } else { 82396fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 82496fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "error attempted to write to tty [0x%p]" 82596fd7ce5SGreg Kroah-Hartman " = NULL", tty); 82696fd7ce5SGreg Kroah-Hartman #endif 82796fd7ce5SGreg Kroah-Hartman } 82896fd7ce5SGreg Kroah-Hartman 82996fd7ce5SGreg Kroah-Hartman /* Now clear signal->tty under the lock */ 83096fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 83196fd7ce5SGreg Kroah-Hartman session_clear_tty(task_session(current)); 83296fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 83396fd7ce5SGreg Kroah-Hartman } 83496fd7ce5SGreg Kroah-Hartman 83596fd7ce5SGreg Kroah-Hartman /** 83696fd7ce5SGreg Kroah-Hartman * 83796fd7ce5SGreg Kroah-Hartman * no_tty - Ensure the current process does not have a controlling tty 83896fd7ce5SGreg Kroah-Hartman */ 83996fd7ce5SGreg Kroah-Hartman void no_tty(void) 84096fd7ce5SGreg Kroah-Hartman { 84196fd7ce5SGreg Kroah-Hartman struct task_struct *tsk = current; 84296fd7ce5SGreg Kroah-Hartman tty_lock(); 84396fd7ce5SGreg Kroah-Hartman disassociate_ctty(0); 84496fd7ce5SGreg Kroah-Hartman tty_unlock(); 84596fd7ce5SGreg Kroah-Hartman proc_clear_tty(tsk); 84696fd7ce5SGreg Kroah-Hartman } 84796fd7ce5SGreg Kroah-Hartman 84896fd7ce5SGreg Kroah-Hartman 84996fd7ce5SGreg Kroah-Hartman /** 85096fd7ce5SGreg Kroah-Hartman * stop_tty - propagate flow control 85196fd7ce5SGreg Kroah-Hartman * @tty: tty to stop 85296fd7ce5SGreg Kroah-Hartman * 85396fd7ce5SGreg Kroah-Hartman * Perform flow control to the driver. For PTY/TTY pairs we 85496fd7ce5SGreg Kroah-Hartman * must also propagate the TIOCKPKT status. May be called 85596fd7ce5SGreg Kroah-Hartman * on an already stopped device and will not re-call the driver 85696fd7ce5SGreg Kroah-Hartman * method. 85796fd7ce5SGreg Kroah-Hartman * 85896fd7ce5SGreg Kroah-Hartman * This functionality is used by both the line disciplines for 85996fd7ce5SGreg Kroah-Hartman * halting incoming flow and by the driver. It may therefore be 86096fd7ce5SGreg Kroah-Hartman * called from any context, may be under the tty atomic_write_lock 86196fd7ce5SGreg Kroah-Hartman * but not always. 86296fd7ce5SGreg Kroah-Hartman * 86396fd7ce5SGreg Kroah-Hartman * Locking: 86496fd7ce5SGreg Kroah-Hartman * Uses the tty control lock internally 86596fd7ce5SGreg Kroah-Hartman */ 86696fd7ce5SGreg Kroah-Hartman 86796fd7ce5SGreg Kroah-Hartman void stop_tty(struct tty_struct *tty) 86896fd7ce5SGreg Kroah-Hartman { 86996fd7ce5SGreg Kroah-Hartman unsigned long flags; 87096fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 87196fd7ce5SGreg Kroah-Hartman if (tty->stopped) { 87296fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 87396fd7ce5SGreg Kroah-Hartman return; 87496fd7ce5SGreg Kroah-Hartman } 87596fd7ce5SGreg Kroah-Hartman tty->stopped = 1; 87696fd7ce5SGreg Kroah-Hartman if (tty->link && tty->link->packet) { 87796fd7ce5SGreg Kroah-Hartman tty->ctrl_status &= ~TIOCPKT_START; 87896fd7ce5SGreg Kroah-Hartman tty->ctrl_status |= TIOCPKT_STOP; 87996fd7ce5SGreg Kroah-Hartman wake_up_interruptible_poll(&tty->link->read_wait, POLLIN); 88096fd7ce5SGreg Kroah-Hartman } 88196fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 88296fd7ce5SGreg Kroah-Hartman if (tty->ops->stop) 88396fd7ce5SGreg Kroah-Hartman (tty->ops->stop)(tty); 88496fd7ce5SGreg Kroah-Hartman } 88596fd7ce5SGreg Kroah-Hartman 88696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(stop_tty); 88796fd7ce5SGreg Kroah-Hartman 88896fd7ce5SGreg Kroah-Hartman /** 88996fd7ce5SGreg Kroah-Hartman * start_tty - propagate flow control 89096fd7ce5SGreg Kroah-Hartman * @tty: tty to start 89196fd7ce5SGreg Kroah-Hartman * 89296fd7ce5SGreg Kroah-Hartman * Start a tty that has been stopped if at all possible. Perform 89396fd7ce5SGreg Kroah-Hartman * any necessary wakeups and propagate the TIOCPKT status. If this 89496fd7ce5SGreg Kroah-Hartman * is the tty was previous stopped and is being started then the 89596fd7ce5SGreg Kroah-Hartman * driver start method is invoked and the line discipline woken. 89696fd7ce5SGreg Kroah-Hartman * 89796fd7ce5SGreg Kroah-Hartman * Locking: 89896fd7ce5SGreg Kroah-Hartman * ctrl_lock 89996fd7ce5SGreg Kroah-Hartman */ 90096fd7ce5SGreg Kroah-Hartman 90196fd7ce5SGreg Kroah-Hartman void start_tty(struct tty_struct *tty) 90296fd7ce5SGreg Kroah-Hartman { 90396fd7ce5SGreg Kroah-Hartman unsigned long flags; 90496fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 90596fd7ce5SGreg Kroah-Hartman if (!tty->stopped || tty->flow_stopped) { 90696fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 90796fd7ce5SGreg Kroah-Hartman return; 90896fd7ce5SGreg Kroah-Hartman } 90996fd7ce5SGreg Kroah-Hartman tty->stopped = 0; 91096fd7ce5SGreg Kroah-Hartman if (tty->link && tty->link->packet) { 91196fd7ce5SGreg Kroah-Hartman tty->ctrl_status &= ~TIOCPKT_STOP; 91296fd7ce5SGreg Kroah-Hartman tty->ctrl_status |= TIOCPKT_START; 91396fd7ce5SGreg Kroah-Hartman wake_up_interruptible_poll(&tty->link->read_wait, POLLIN); 91496fd7ce5SGreg Kroah-Hartman } 91596fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 91696fd7ce5SGreg Kroah-Hartman if (tty->ops->start) 91796fd7ce5SGreg Kroah-Hartman (tty->ops->start)(tty); 91896fd7ce5SGreg Kroah-Hartman /* If we have a running line discipline it may need kicking */ 91996fd7ce5SGreg Kroah-Hartman tty_wakeup(tty); 92096fd7ce5SGreg Kroah-Hartman } 92196fd7ce5SGreg Kroah-Hartman 92296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(start_tty); 92396fd7ce5SGreg Kroah-Hartman 92496fd7ce5SGreg Kroah-Hartman /** 92596fd7ce5SGreg Kroah-Hartman * tty_read - read method for tty device files 92696fd7ce5SGreg Kroah-Hartman * @file: pointer to tty file 92796fd7ce5SGreg Kroah-Hartman * @buf: user buffer 92896fd7ce5SGreg Kroah-Hartman * @count: size of user buffer 92996fd7ce5SGreg Kroah-Hartman * @ppos: unused 93096fd7ce5SGreg Kroah-Hartman * 93196fd7ce5SGreg Kroah-Hartman * Perform the read system call function on this terminal device. Checks 93296fd7ce5SGreg Kroah-Hartman * for hung up devices before calling the line discipline method. 93396fd7ce5SGreg Kroah-Hartman * 93496fd7ce5SGreg Kroah-Hartman * Locking: 93596fd7ce5SGreg Kroah-Hartman * Locks the line discipline internally while needed. Multiple 93696fd7ce5SGreg Kroah-Hartman * read calls may be outstanding in parallel. 93796fd7ce5SGreg Kroah-Hartman */ 93896fd7ce5SGreg Kroah-Hartman 93996fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *file, char __user *buf, size_t count, 94096fd7ce5SGreg Kroah-Hartman loff_t *ppos) 94196fd7ce5SGreg Kroah-Hartman { 94296fd7ce5SGreg Kroah-Hartman int i; 94396fd7ce5SGreg Kroah-Hartman struct inode *inode = file->f_path.dentry->d_inode; 94496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 94596fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 94696fd7ce5SGreg Kroah-Hartman 94796fd7ce5SGreg Kroah-Hartman if (tty_paranoia_check(tty, inode, "tty_read")) 94896fd7ce5SGreg Kroah-Hartman return -EIO; 94996fd7ce5SGreg Kroah-Hartman if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) 95096fd7ce5SGreg Kroah-Hartman return -EIO; 95196fd7ce5SGreg Kroah-Hartman 95296fd7ce5SGreg Kroah-Hartman /* We want to wait for the line discipline to sort out in this 95396fd7ce5SGreg Kroah-Hartman situation */ 95496fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 95596fd7ce5SGreg Kroah-Hartman if (ld->ops->read) 95696fd7ce5SGreg Kroah-Hartman i = (ld->ops->read)(tty, file, buf, count); 95796fd7ce5SGreg Kroah-Hartman else 95896fd7ce5SGreg Kroah-Hartman i = -EIO; 95996fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 96096fd7ce5SGreg Kroah-Hartman if (i > 0) 96196fd7ce5SGreg Kroah-Hartman inode->i_atime = current_fs_time(inode->i_sb); 96296fd7ce5SGreg Kroah-Hartman return i; 96396fd7ce5SGreg Kroah-Hartman } 96496fd7ce5SGreg Kroah-Hartman 96596fd7ce5SGreg Kroah-Hartman void tty_write_unlock(struct tty_struct *tty) 96683c67571SJiri Slaby __releases(&tty->atomic_write_lock) 96796fd7ce5SGreg Kroah-Hartman { 96896fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->atomic_write_lock); 96996fd7ce5SGreg Kroah-Hartman wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 97096fd7ce5SGreg Kroah-Hartman } 97196fd7ce5SGreg Kroah-Hartman 97296fd7ce5SGreg Kroah-Hartman int tty_write_lock(struct tty_struct *tty, int ndelay) 97383c67571SJiri Slaby __acquires(&tty->atomic_write_lock) 97496fd7ce5SGreg Kroah-Hartman { 97596fd7ce5SGreg Kroah-Hartman if (!mutex_trylock(&tty->atomic_write_lock)) { 97696fd7ce5SGreg Kroah-Hartman if (ndelay) 97796fd7ce5SGreg Kroah-Hartman return -EAGAIN; 97896fd7ce5SGreg Kroah-Hartman if (mutex_lock_interruptible(&tty->atomic_write_lock)) 97996fd7ce5SGreg Kroah-Hartman return -ERESTARTSYS; 98096fd7ce5SGreg Kroah-Hartman } 98196fd7ce5SGreg Kroah-Hartman return 0; 98296fd7ce5SGreg Kroah-Hartman } 98396fd7ce5SGreg Kroah-Hartman 98496fd7ce5SGreg Kroah-Hartman /* 98596fd7ce5SGreg Kroah-Hartman * Split writes up in sane blocksizes to avoid 98696fd7ce5SGreg Kroah-Hartman * denial-of-service type attacks 98796fd7ce5SGreg Kroah-Hartman */ 98896fd7ce5SGreg Kroah-Hartman static inline ssize_t do_tty_write( 98996fd7ce5SGreg Kroah-Hartman ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), 99096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, 99196fd7ce5SGreg Kroah-Hartman struct file *file, 99296fd7ce5SGreg Kroah-Hartman const char __user *buf, 99396fd7ce5SGreg Kroah-Hartman size_t count) 99496fd7ce5SGreg Kroah-Hartman { 99596fd7ce5SGreg Kroah-Hartman ssize_t ret, written = 0; 99696fd7ce5SGreg Kroah-Hartman unsigned int chunk; 99796fd7ce5SGreg Kroah-Hartman 99896fd7ce5SGreg Kroah-Hartman ret = tty_write_lock(tty, file->f_flags & O_NDELAY); 99996fd7ce5SGreg Kroah-Hartman if (ret < 0) 100096fd7ce5SGreg Kroah-Hartman return ret; 100196fd7ce5SGreg Kroah-Hartman 100296fd7ce5SGreg Kroah-Hartman /* 100396fd7ce5SGreg Kroah-Hartman * We chunk up writes into a temporary buffer. This 100496fd7ce5SGreg Kroah-Hartman * simplifies low-level drivers immensely, since they 100596fd7ce5SGreg Kroah-Hartman * don't have locking issues and user mode accesses. 100696fd7ce5SGreg Kroah-Hartman * 100796fd7ce5SGreg Kroah-Hartman * But if TTY_NO_WRITE_SPLIT is set, we should use a 100896fd7ce5SGreg Kroah-Hartman * big chunk-size.. 100996fd7ce5SGreg Kroah-Hartman * 101096fd7ce5SGreg Kroah-Hartman * The default chunk-size is 2kB, because the NTTY 101196fd7ce5SGreg Kroah-Hartman * layer has problems with bigger chunks. It will 101296fd7ce5SGreg Kroah-Hartman * claim to be able to handle more characters than 101396fd7ce5SGreg Kroah-Hartman * it actually does. 101496fd7ce5SGreg Kroah-Hartman * 101596fd7ce5SGreg Kroah-Hartman * FIXME: This can probably go away now except that 64K chunks 101696fd7ce5SGreg Kroah-Hartman * are too likely to fail unless switched to vmalloc... 101796fd7ce5SGreg Kroah-Hartman */ 101896fd7ce5SGreg Kroah-Hartman chunk = 2048; 101996fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) 102096fd7ce5SGreg Kroah-Hartman chunk = 65536; 102196fd7ce5SGreg Kroah-Hartman if (count < chunk) 102296fd7ce5SGreg Kroah-Hartman chunk = count; 102396fd7ce5SGreg Kroah-Hartman 102496fd7ce5SGreg Kroah-Hartman /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ 102596fd7ce5SGreg Kroah-Hartman if (tty->write_cnt < chunk) { 102696fd7ce5SGreg Kroah-Hartman unsigned char *buf_chunk; 102796fd7ce5SGreg Kroah-Hartman 102896fd7ce5SGreg Kroah-Hartman if (chunk < 1024) 102996fd7ce5SGreg Kroah-Hartman chunk = 1024; 103096fd7ce5SGreg Kroah-Hartman 103196fd7ce5SGreg Kroah-Hartman buf_chunk = kmalloc(chunk, GFP_KERNEL); 103296fd7ce5SGreg Kroah-Hartman if (!buf_chunk) { 103396fd7ce5SGreg Kroah-Hartman ret = -ENOMEM; 103496fd7ce5SGreg Kroah-Hartman goto out; 103596fd7ce5SGreg Kroah-Hartman } 103696fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 103796fd7ce5SGreg Kroah-Hartman tty->write_cnt = chunk; 103896fd7ce5SGreg Kroah-Hartman tty->write_buf = buf_chunk; 103996fd7ce5SGreg Kroah-Hartman } 104096fd7ce5SGreg Kroah-Hartman 104196fd7ce5SGreg Kroah-Hartman /* Do the write .. */ 104296fd7ce5SGreg Kroah-Hartman for (;;) { 104396fd7ce5SGreg Kroah-Hartman size_t size = count; 104496fd7ce5SGreg Kroah-Hartman if (size > chunk) 104596fd7ce5SGreg Kroah-Hartman size = chunk; 104696fd7ce5SGreg Kroah-Hartman ret = -EFAULT; 104796fd7ce5SGreg Kroah-Hartman if (copy_from_user(tty->write_buf, buf, size)) 104896fd7ce5SGreg Kroah-Hartman break; 104996fd7ce5SGreg Kroah-Hartman ret = write(tty, file, tty->write_buf, size); 105096fd7ce5SGreg Kroah-Hartman if (ret <= 0) 105196fd7ce5SGreg Kroah-Hartman break; 105296fd7ce5SGreg Kroah-Hartman written += ret; 105396fd7ce5SGreg Kroah-Hartman buf += ret; 105496fd7ce5SGreg Kroah-Hartman count -= ret; 105596fd7ce5SGreg Kroah-Hartman if (!count) 105696fd7ce5SGreg Kroah-Hartman break; 105796fd7ce5SGreg Kroah-Hartman ret = -ERESTARTSYS; 105896fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 105996fd7ce5SGreg Kroah-Hartman break; 106096fd7ce5SGreg Kroah-Hartman cond_resched(); 106196fd7ce5SGreg Kroah-Hartman } 106296fd7ce5SGreg Kroah-Hartman if (written) { 106396fd7ce5SGreg Kroah-Hartman struct inode *inode = file->f_path.dentry->d_inode; 106496fd7ce5SGreg Kroah-Hartman inode->i_mtime = current_fs_time(inode->i_sb); 106596fd7ce5SGreg Kroah-Hartman ret = written; 106696fd7ce5SGreg Kroah-Hartman } 106796fd7ce5SGreg Kroah-Hartman out: 106896fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 106996fd7ce5SGreg Kroah-Hartman return ret; 107096fd7ce5SGreg Kroah-Hartman } 107196fd7ce5SGreg Kroah-Hartman 107296fd7ce5SGreg Kroah-Hartman /** 107396fd7ce5SGreg Kroah-Hartman * tty_write_message - write a message to a certain tty, not just the console. 107496fd7ce5SGreg Kroah-Hartman * @tty: the destination tty_struct 107596fd7ce5SGreg Kroah-Hartman * @msg: the message to write 107696fd7ce5SGreg Kroah-Hartman * 107796fd7ce5SGreg Kroah-Hartman * This is used for messages that need to be redirected to a specific tty. 107896fd7ce5SGreg Kroah-Hartman * We don't put it into the syslog queue right now maybe in the future if 107996fd7ce5SGreg Kroah-Hartman * really needed. 108096fd7ce5SGreg Kroah-Hartman * 108196fd7ce5SGreg Kroah-Hartman * We must still hold the BTM and test the CLOSING flag for the moment. 108296fd7ce5SGreg Kroah-Hartman */ 108396fd7ce5SGreg Kroah-Hartman 108496fd7ce5SGreg Kroah-Hartman void tty_write_message(struct tty_struct *tty, char *msg) 108596fd7ce5SGreg Kroah-Hartman { 108696fd7ce5SGreg Kroah-Hartman if (tty) { 108796fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->atomic_write_lock); 108896fd7ce5SGreg Kroah-Hartman tty_lock(); 108996fd7ce5SGreg Kroah-Hartman if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { 109096fd7ce5SGreg Kroah-Hartman tty_unlock(); 109196fd7ce5SGreg Kroah-Hartman tty->ops->write(tty, msg, strlen(msg)); 109296fd7ce5SGreg Kroah-Hartman } else 109396fd7ce5SGreg Kroah-Hartman tty_unlock(); 109496fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 109596fd7ce5SGreg Kroah-Hartman } 109696fd7ce5SGreg Kroah-Hartman return; 109796fd7ce5SGreg Kroah-Hartman } 109896fd7ce5SGreg Kroah-Hartman 109996fd7ce5SGreg Kroah-Hartman 110096fd7ce5SGreg Kroah-Hartman /** 110196fd7ce5SGreg Kroah-Hartman * tty_write - write method for tty device file 110296fd7ce5SGreg Kroah-Hartman * @file: tty file pointer 110396fd7ce5SGreg Kroah-Hartman * @buf: user data to write 110496fd7ce5SGreg Kroah-Hartman * @count: bytes to write 110596fd7ce5SGreg Kroah-Hartman * @ppos: unused 110696fd7ce5SGreg Kroah-Hartman * 110796fd7ce5SGreg Kroah-Hartman * Write data to a tty device via the line discipline. 110896fd7ce5SGreg Kroah-Hartman * 110996fd7ce5SGreg Kroah-Hartman * Locking: 111096fd7ce5SGreg Kroah-Hartman * Locks the line discipline as required 111196fd7ce5SGreg Kroah-Hartman * Writes to the tty driver are serialized by the atomic_write_lock 111296fd7ce5SGreg Kroah-Hartman * and are then processed in chunks to the device. The line discipline 111396fd7ce5SGreg Kroah-Hartman * write method will not be invoked in parallel for each device. 111496fd7ce5SGreg Kroah-Hartman */ 111596fd7ce5SGreg Kroah-Hartman 111696fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *file, const char __user *buf, 111796fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 111896fd7ce5SGreg Kroah-Hartman { 111996fd7ce5SGreg Kroah-Hartman struct inode *inode = file->f_path.dentry->d_inode; 112096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 112196fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 112296fd7ce5SGreg Kroah-Hartman ssize_t ret; 112396fd7ce5SGreg Kroah-Hartman 112496fd7ce5SGreg Kroah-Hartman if (tty_paranoia_check(tty, inode, "tty_write")) 112596fd7ce5SGreg Kroah-Hartman return -EIO; 112696fd7ce5SGreg Kroah-Hartman if (!tty || !tty->ops->write || 112796fd7ce5SGreg Kroah-Hartman (test_bit(TTY_IO_ERROR, &tty->flags))) 112896fd7ce5SGreg Kroah-Hartman return -EIO; 112996fd7ce5SGreg Kroah-Hartman /* Short term debug to catch buggy drivers */ 113096fd7ce5SGreg Kroah-Hartman if (tty->ops->write_room == NULL) 113196fd7ce5SGreg Kroah-Hartman printk(KERN_ERR "tty driver %s lacks a write_room method.\n", 113296fd7ce5SGreg Kroah-Hartman tty->driver->name); 113396fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 113496fd7ce5SGreg Kroah-Hartman if (!ld->ops->write) 113596fd7ce5SGreg Kroah-Hartman ret = -EIO; 113696fd7ce5SGreg Kroah-Hartman else 113796fd7ce5SGreg Kroah-Hartman ret = do_tty_write(ld->ops->write, tty, file, buf, count); 113896fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 113996fd7ce5SGreg Kroah-Hartman return ret; 114096fd7ce5SGreg Kroah-Hartman } 114196fd7ce5SGreg Kroah-Hartman 114296fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *file, const char __user *buf, 114396fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 114496fd7ce5SGreg Kroah-Hartman { 114596fd7ce5SGreg Kroah-Hartman struct file *p = NULL; 114696fd7ce5SGreg Kroah-Hartman 114796fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 114896fd7ce5SGreg Kroah-Hartman if (redirect) { 114996fd7ce5SGreg Kroah-Hartman get_file(redirect); 115096fd7ce5SGreg Kroah-Hartman p = redirect; 115196fd7ce5SGreg Kroah-Hartman } 115296fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 115396fd7ce5SGreg Kroah-Hartman 115496fd7ce5SGreg Kroah-Hartman if (p) { 115596fd7ce5SGreg Kroah-Hartman ssize_t res; 115696fd7ce5SGreg Kroah-Hartman res = vfs_write(p, buf, count, &p->f_pos); 115796fd7ce5SGreg Kroah-Hartman fput(p); 115896fd7ce5SGreg Kroah-Hartman return res; 115996fd7ce5SGreg Kroah-Hartman } 116096fd7ce5SGreg Kroah-Hartman return tty_write(file, buf, count, ppos); 116196fd7ce5SGreg Kroah-Hartman } 116296fd7ce5SGreg Kroah-Hartman 116396fd7ce5SGreg Kroah-Hartman static char ptychar[] = "pqrstuvwxyzabcde"; 116496fd7ce5SGreg Kroah-Hartman 116596fd7ce5SGreg Kroah-Hartman /** 116696fd7ce5SGreg Kroah-Hartman * pty_line_name - generate name for a pty 116796fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 116896fd7ce5SGreg Kroah-Hartman * @index: the minor number 116996fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 6 bytes 117096fd7ce5SGreg Kroah-Hartman * 117196fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 117296fd7ce5SGreg Kroah-Hartman * buffer. 117396fd7ce5SGreg Kroah-Hartman * 117496fd7ce5SGreg Kroah-Hartman * Locking: None 117596fd7ce5SGreg Kroah-Hartman */ 117696fd7ce5SGreg Kroah-Hartman static void pty_line_name(struct tty_driver *driver, int index, char *p) 117796fd7ce5SGreg Kroah-Hartman { 117896fd7ce5SGreg Kroah-Hartman int i = index + driver->name_base; 117996fd7ce5SGreg Kroah-Hartman /* ->name is initialized to "ttyp", but "tty" is expected */ 118096fd7ce5SGreg Kroah-Hartman sprintf(p, "%s%c%x", 118196fd7ce5SGreg Kroah-Hartman driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, 118296fd7ce5SGreg Kroah-Hartman ptychar[i >> 4 & 0xf], i & 0xf); 118396fd7ce5SGreg Kroah-Hartman } 118496fd7ce5SGreg Kroah-Hartman 118596fd7ce5SGreg Kroah-Hartman /** 118696fd7ce5SGreg Kroah-Hartman * tty_line_name - generate name for a tty 118796fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 118896fd7ce5SGreg Kroah-Hartman * @index: the minor number 118996fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 7 bytes 119096fd7ce5SGreg Kroah-Hartman * 119196fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 119296fd7ce5SGreg Kroah-Hartman * buffer. 119396fd7ce5SGreg Kroah-Hartman * 119496fd7ce5SGreg Kroah-Hartman * Locking: None 119596fd7ce5SGreg Kroah-Hartman */ 119696fd7ce5SGreg Kroah-Hartman static void tty_line_name(struct tty_driver *driver, int index, char *p) 119796fd7ce5SGreg Kroah-Hartman { 119896fd7ce5SGreg Kroah-Hartman sprintf(p, "%s%d", driver->name, index + driver->name_base); 119996fd7ce5SGreg Kroah-Hartman } 120096fd7ce5SGreg Kroah-Hartman 120196fd7ce5SGreg Kroah-Hartman /** 120296fd7ce5SGreg Kroah-Hartman * tty_driver_lookup_tty() - find an existing tty, if any 120396fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 120496fd7ce5SGreg Kroah-Hartman * @idx: the minor number 120596fd7ce5SGreg Kroah-Hartman * 120696fd7ce5SGreg Kroah-Hartman * Return the tty, if found or ERR_PTR() otherwise. 120796fd7ce5SGreg Kroah-Hartman * 120896fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex must be held. If tty is found, the mutex must 120996fd7ce5SGreg Kroah-Hartman * be held until the 'fast-open' is also done. Will change once we 121096fd7ce5SGreg Kroah-Hartman * have refcounting in the driver and per driver locking 121196fd7ce5SGreg Kroah-Hartman */ 121296fd7ce5SGreg Kroah-Hartman static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, 121396fd7ce5SGreg Kroah-Hartman struct inode *inode, int idx) 121496fd7ce5SGreg Kroah-Hartman { 121596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 121696fd7ce5SGreg Kroah-Hartman 121796fd7ce5SGreg Kroah-Hartman if (driver->ops->lookup) 121896fd7ce5SGreg Kroah-Hartman return driver->ops->lookup(driver, inode, idx); 121996fd7ce5SGreg Kroah-Hartman 122096fd7ce5SGreg Kroah-Hartman tty = driver->ttys[idx]; 122196fd7ce5SGreg Kroah-Hartman return tty; 122296fd7ce5SGreg Kroah-Hartman } 122396fd7ce5SGreg Kroah-Hartman 122496fd7ce5SGreg Kroah-Hartman /** 122596fd7ce5SGreg Kroah-Hartman * tty_init_termios - helper for termios setup 122696fd7ce5SGreg Kroah-Hartman * @tty: the tty to set up 122796fd7ce5SGreg Kroah-Hartman * 122896fd7ce5SGreg Kroah-Hartman * Initialise the termios structures for this tty. Thus runs under 122996fd7ce5SGreg Kroah-Hartman * the tty_mutex currently so we can be relaxed about ordering. 123096fd7ce5SGreg Kroah-Hartman */ 123196fd7ce5SGreg Kroah-Hartman 123296fd7ce5SGreg Kroah-Hartman int tty_init_termios(struct tty_struct *tty) 123396fd7ce5SGreg Kroah-Hartman { 123496fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 123596fd7ce5SGreg Kroah-Hartman int idx = tty->index; 123696fd7ce5SGreg Kroah-Hartman 123796fd7ce5SGreg Kroah-Hartman tp = tty->driver->termios[idx]; 123896fd7ce5SGreg Kroah-Hartman if (tp == NULL) { 123996fd7ce5SGreg Kroah-Hartman tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); 124096fd7ce5SGreg Kroah-Hartman if (tp == NULL) 124196fd7ce5SGreg Kroah-Hartman return -ENOMEM; 124296fd7ce5SGreg Kroah-Hartman memcpy(tp, &tty->driver->init_termios, 124396fd7ce5SGreg Kroah-Hartman sizeof(struct ktermios)); 124496fd7ce5SGreg Kroah-Hartman tty->driver->termios[idx] = tp; 124596fd7ce5SGreg Kroah-Hartman } 124696fd7ce5SGreg Kroah-Hartman tty->termios = tp; 124796fd7ce5SGreg Kroah-Hartman tty->termios_locked = tp + 1; 124896fd7ce5SGreg Kroah-Hartman 124996fd7ce5SGreg Kroah-Hartman /* Compatibility until drivers always set this */ 125096fd7ce5SGreg Kroah-Hartman tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios); 125196fd7ce5SGreg Kroah-Hartman tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios); 125296fd7ce5SGreg Kroah-Hartman return 0; 125396fd7ce5SGreg Kroah-Hartman } 125496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_init_termios); 125596fd7ce5SGreg Kroah-Hartman 125696fd7ce5SGreg Kroah-Hartman /** 125796fd7ce5SGreg Kroah-Hartman * tty_driver_install_tty() - install a tty entry in the driver 125896fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 125996fd7ce5SGreg Kroah-Hartman * @tty: the tty 126096fd7ce5SGreg Kroah-Hartman * 126196fd7ce5SGreg Kroah-Hartman * Install a tty object into the driver tables. The tty->index field 126296fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. This method is responsible 126396fd7ce5SGreg Kroah-Hartman * for ensuring any need additional structures are allocated and 126496fd7ce5SGreg Kroah-Hartman * configured. 126596fd7ce5SGreg Kroah-Hartman * 126696fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 126796fd7ce5SGreg Kroah-Hartman */ 126896fd7ce5SGreg Kroah-Hartman static int tty_driver_install_tty(struct tty_driver *driver, 126996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty) 127096fd7ce5SGreg Kroah-Hartman { 127196fd7ce5SGreg Kroah-Hartman int idx = tty->index; 127296fd7ce5SGreg Kroah-Hartman int ret; 127396fd7ce5SGreg Kroah-Hartman 127496fd7ce5SGreg Kroah-Hartman if (driver->ops->install) { 127596fd7ce5SGreg Kroah-Hartman ret = driver->ops->install(driver, tty); 127696fd7ce5SGreg Kroah-Hartman return ret; 127796fd7ce5SGreg Kroah-Hartman } 127896fd7ce5SGreg Kroah-Hartman 127996fd7ce5SGreg Kroah-Hartman if (tty_init_termios(tty) == 0) { 128096fd7ce5SGreg Kroah-Hartman tty_driver_kref_get(driver); 128196fd7ce5SGreg Kroah-Hartman tty->count++; 128296fd7ce5SGreg Kroah-Hartman driver->ttys[idx] = tty; 128396fd7ce5SGreg Kroah-Hartman return 0; 128496fd7ce5SGreg Kroah-Hartman } 128596fd7ce5SGreg Kroah-Hartman return -ENOMEM; 128696fd7ce5SGreg Kroah-Hartman } 128796fd7ce5SGreg Kroah-Hartman 128896fd7ce5SGreg Kroah-Hartman /** 128996fd7ce5SGreg Kroah-Hartman * tty_driver_remove_tty() - remove a tty from the driver tables 129096fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 129196fd7ce5SGreg Kroah-Hartman * @idx: the minor number 129296fd7ce5SGreg Kroah-Hartman * 129396fd7ce5SGreg Kroah-Hartman * Remvoe a tty object from the driver tables. The tty->index field 129496fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. 129596fd7ce5SGreg Kroah-Hartman * 129696fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 129796fd7ce5SGreg Kroah-Hartman */ 129824d406a6SJiri Slaby void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) 129996fd7ce5SGreg Kroah-Hartman { 130096fd7ce5SGreg Kroah-Hartman if (driver->ops->remove) 130196fd7ce5SGreg Kroah-Hartman driver->ops->remove(driver, tty); 130296fd7ce5SGreg Kroah-Hartman else 130396fd7ce5SGreg Kroah-Hartman driver->ttys[tty->index] = NULL; 130496fd7ce5SGreg Kroah-Hartman } 130596fd7ce5SGreg Kroah-Hartman 130696fd7ce5SGreg Kroah-Hartman /* 130796fd7ce5SGreg Kroah-Hartman * tty_reopen() - fast re-open of an open tty 130896fd7ce5SGreg Kroah-Hartman * @tty - the tty to open 130996fd7ce5SGreg Kroah-Hartman * 131096fd7ce5SGreg Kroah-Hartman * Return 0 on success, -errno on error. 131196fd7ce5SGreg Kroah-Hartman * 131296fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex must be held from the time the tty was found 131396fd7ce5SGreg Kroah-Hartman * till this open completes. 131496fd7ce5SGreg Kroah-Hartman */ 131596fd7ce5SGreg Kroah-Hartman static int tty_reopen(struct tty_struct *tty) 131696fd7ce5SGreg Kroah-Hartman { 131796fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 131896fd7ce5SGreg Kroah-Hartman 1319e2efafbfSJiri Slaby if (test_bit(TTY_CLOSING, &tty->flags) || 1320acfa747bSJiri Slaby test_bit(TTY_HUPPING, &tty->flags) || 1321e2efafbfSJiri Slaby test_bit(TTY_LDISC_CHANGING, &tty->flags)) 132296fd7ce5SGreg Kroah-Hartman return -EIO; 132396fd7ce5SGreg Kroah-Hartman 132496fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY && 132596fd7ce5SGreg Kroah-Hartman driver->subtype == PTY_TYPE_MASTER) { 132696fd7ce5SGreg Kroah-Hartman /* 132796fd7ce5SGreg Kroah-Hartman * special case for PTY masters: only one open permitted, 132896fd7ce5SGreg Kroah-Hartman * and the slave side open count is incremented as well. 132996fd7ce5SGreg Kroah-Hartman */ 133096fd7ce5SGreg Kroah-Hartman if (tty->count) 133196fd7ce5SGreg Kroah-Hartman return -EIO; 133296fd7ce5SGreg Kroah-Hartman 133396fd7ce5SGreg Kroah-Hartman tty->link->count++; 133496fd7ce5SGreg Kroah-Hartman } 133596fd7ce5SGreg Kroah-Hartman tty->count++; 133696fd7ce5SGreg Kroah-Hartman tty->driver = driver; /* N.B. why do this every time?? */ 133796fd7ce5SGreg Kroah-Hartman 133896fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->ldisc_mutex); 133996fd7ce5SGreg Kroah-Hartman WARN_ON(!test_bit(TTY_LDISC, &tty->flags)); 134096fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->ldisc_mutex); 134196fd7ce5SGreg Kroah-Hartman 134296fd7ce5SGreg Kroah-Hartman return 0; 134396fd7ce5SGreg Kroah-Hartman } 134496fd7ce5SGreg Kroah-Hartman 134596fd7ce5SGreg Kroah-Hartman /** 134696fd7ce5SGreg Kroah-Hartman * tty_init_dev - initialise a tty device 134796fd7ce5SGreg Kroah-Hartman * @driver: tty driver we are opening a device on 134896fd7ce5SGreg Kroah-Hartman * @idx: device index 134996fd7ce5SGreg Kroah-Hartman * @ret_tty: returned tty structure 135096fd7ce5SGreg Kroah-Hartman * @first_ok: ok to open a new device (used by ptmx) 135196fd7ce5SGreg Kroah-Hartman * 135296fd7ce5SGreg Kroah-Hartman * Prepare a tty device. This may not be a "new" clean device but 135396fd7ce5SGreg Kroah-Hartman * could also be an active device. The pty drivers require special 135496fd7ce5SGreg Kroah-Hartman * handling because of this. 135596fd7ce5SGreg Kroah-Hartman * 135696fd7ce5SGreg Kroah-Hartman * Locking: 135796fd7ce5SGreg Kroah-Hartman * The function is called under the tty_mutex, which 135896fd7ce5SGreg Kroah-Hartman * protects us from the tty struct or driver itself going away. 135996fd7ce5SGreg Kroah-Hartman * 136096fd7ce5SGreg Kroah-Hartman * On exit the tty device has the line discipline attached and 136196fd7ce5SGreg Kroah-Hartman * a reference count of 1. If a pair was created for pty/tty use 136296fd7ce5SGreg Kroah-Hartman * and the other was a pty master then it too has a reference count of 1. 136396fd7ce5SGreg Kroah-Hartman * 136496fd7ce5SGreg Kroah-Hartman * WSH 06/09/97: Rewritten to remove races and properly clean up after a 136596fd7ce5SGreg Kroah-Hartman * failed open. The new code protects the open with a mutex, so it's 136696fd7ce5SGreg Kroah-Hartman * really quite straightforward. The mutex locking can probably be 136796fd7ce5SGreg Kroah-Hartman * relaxed for the (most common) case of reopening a tty. 136896fd7ce5SGreg Kroah-Hartman */ 136996fd7ce5SGreg Kroah-Hartman 137096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx, 137196fd7ce5SGreg Kroah-Hartman int first_ok) 137296fd7ce5SGreg Kroah-Hartman { 137396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 137496fd7ce5SGreg Kroah-Hartman int retval; 137596fd7ce5SGreg Kroah-Hartman 137696fd7ce5SGreg Kroah-Hartman /* Check if pty master is being opened multiple times */ 137796fd7ce5SGreg Kroah-Hartman if (driver->subtype == PTY_TYPE_MASTER && 137896fd7ce5SGreg Kroah-Hartman (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) { 137996fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EIO); 138096fd7ce5SGreg Kroah-Hartman } 138196fd7ce5SGreg Kroah-Hartman 138296fd7ce5SGreg Kroah-Hartman /* 138396fd7ce5SGreg Kroah-Hartman * First time open is complex, especially for PTY devices. 138496fd7ce5SGreg Kroah-Hartman * This code guarantees that either everything succeeds and the 138596fd7ce5SGreg Kroah-Hartman * TTY is ready for operation, or else the table slots are vacated 138696fd7ce5SGreg Kroah-Hartman * and the allocated memory released. (Except that the termios 138796fd7ce5SGreg Kroah-Hartman * and locked termios may be retained.) 138896fd7ce5SGreg Kroah-Hartman */ 138996fd7ce5SGreg Kroah-Hartman 139096fd7ce5SGreg Kroah-Hartman if (!try_module_get(driver->owner)) 139196fd7ce5SGreg Kroah-Hartman return ERR_PTR(-ENODEV); 139296fd7ce5SGreg Kroah-Hartman 139396fd7ce5SGreg Kroah-Hartman tty = alloc_tty_struct(); 1394d5543503SJiri Slaby if (!tty) { 1395d5543503SJiri Slaby retval = -ENOMEM; 1396d5543503SJiri Slaby goto err_module_put; 1397d5543503SJiri Slaby } 139896fd7ce5SGreg Kroah-Hartman initialize_tty_struct(tty, driver, idx); 139996fd7ce5SGreg Kroah-Hartman 140096fd7ce5SGreg Kroah-Hartman retval = tty_driver_install_tty(driver, tty); 1401d5543503SJiri Slaby if (retval < 0) 1402a9dccddbSJiri Slaby goto err_deinit_tty; 140396fd7ce5SGreg Kroah-Hartman 140496fd7ce5SGreg Kroah-Hartman /* 140596fd7ce5SGreg Kroah-Hartman * Structures all installed ... call the ldisc open routines. 140696fd7ce5SGreg Kroah-Hartman * If we fail here just call release_tty to clean up. No need 140796fd7ce5SGreg Kroah-Hartman * to decrement the use counts, as release_tty doesn't care. 140896fd7ce5SGreg Kroah-Hartman */ 140996fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_setup(tty, tty->link); 141096fd7ce5SGreg Kroah-Hartman if (retval) 1411d5543503SJiri Slaby goto err_release_tty; 141296fd7ce5SGreg Kroah-Hartman return tty; 141396fd7ce5SGreg Kroah-Hartman 1414a9dccddbSJiri Slaby err_deinit_tty: 1415a9dccddbSJiri Slaby deinitialize_tty_struct(tty); 1416d5543503SJiri Slaby free_tty_struct(tty); 1417d5543503SJiri Slaby err_module_put: 141896fd7ce5SGreg Kroah-Hartman module_put(driver->owner); 1419d5543503SJiri Slaby return ERR_PTR(retval); 142096fd7ce5SGreg Kroah-Hartman 142196fd7ce5SGreg Kroah-Hartman /* call the tty release_tty routine to clean out this slot */ 1422d5543503SJiri Slaby err_release_tty: 14235a3c6b25SManuel Zerpies printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, " 142496fd7ce5SGreg Kroah-Hartman "clearing slot %d\n", idx); 142596fd7ce5SGreg Kroah-Hartman release_tty(tty, idx); 142696fd7ce5SGreg Kroah-Hartman return ERR_PTR(retval); 142796fd7ce5SGreg Kroah-Hartman } 142896fd7ce5SGreg Kroah-Hartman 142996fd7ce5SGreg Kroah-Hartman void tty_free_termios(struct tty_struct *tty) 143096fd7ce5SGreg Kroah-Hartman { 143196fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 143296fd7ce5SGreg Kroah-Hartman int idx = tty->index; 143396fd7ce5SGreg Kroah-Hartman /* Kill this flag and push into drivers for locking etc */ 143496fd7ce5SGreg Kroah-Hartman if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) { 143596fd7ce5SGreg Kroah-Hartman /* FIXME: Locking on ->termios array */ 143696fd7ce5SGreg Kroah-Hartman tp = tty->termios; 143796fd7ce5SGreg Kroah-Hartman tty->driver->termios[idx] = NULL; 143896fd7ce5SGreg Kroah-Hartman kfree(tp); 143996fd7ce5SGreg Kroah-Hartman } 144096fd7ce5SGreg Kroah-Hartman } 144196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_free_termios); 144296fd7ce5SGreg Kroah-Hartman 144396fd7ce5SGreg Kroah-Hartman void tty_shutdown(struct tty_struct *tty) 144496fd7ce5SGreg Kroah-Hartman { 144596fd7ce5SGreg Kroah-Hartman tty_driver_remove_tty(tty->driver, tty); 144696fd7ce5SGreg Kroah-Hartman tty_free_termios(tty); 144796fd7ce5SGreg Kroah-Hartman } 144896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_shutdown); 144996fd7ce5SGreg Kroah-Hartman 145096fd7ce5SGreg Kroah-Hartman /** 145196fd7ce5SGreg Kroah-Hartman * release_one_tty - release tty structure memory 145296fd7ce5SGreg Kroah-Hartman * @kref: kref of tty we are obliterating 145396fd7ce5SGreg Kroah-Hartman * 145496fd7ce5SGreg Kroah-Hartman * Releases memory associated with a tty structure, and clears out the 145596fd7ce5SGreg Kroah-Hartman * driver table slots. This function is called when a device is no longer 145696fd7ce5SGreg Kroah-Hartman * in use. It also gets called when setup of a device fails. 145796fd7ce5SGreg Kroah-Hartman * 145896fd7ce5SGreg Kroah-Hartman * Locking: 145996fd7ce5SGreg Kroah-Hartman * tty_mutex - sometimes only 146096fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 146196fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 146296fd7ce5SGreg Kroah-Hartman * 146396fd7ce5SGreg Kroah-Hartman * This method gets called from a work queue so that the driver private 146496fd7ce5SGreg Kroah-Hartman * cleanup ops can sleep (needed for USB at least) 146596fd7ce5SGreg Kroah-Hartman */ 146696fd7ce5SGreg Kroah-Hartman static void release_one_tty(struct work_struct *work) 146796fd7ce5SGreg Kroah-Hartman { 146896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 146996fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 147096fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 147196fd7ce5SGreg Kroah-Hartman 147296fd7ce5SGreg Kroah-Hartman if (tty->ops->cleanup) 147396fd7ce5SGreg Kroah-Hartman tty->ops->cleanup(tty); 147496fd7ce5SGreg Kroah-Hartman 147596fd7ce5SGreg Kroah-Hartman tty->magic = 0; 147696fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(driver); 147796fd7ce5SGreg Kroah-Hartman module_put(driver->owner); 147896fd7ce5SGreg Kroah-Hartman 147996fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 148096fd7ce5SGreg Kroah-Hartman list_del_init(&tty->tty_files); 148196fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 148296fd7ce5SGreg Kroah-Hartman 148396fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 148496fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 148596fd7ce5SGreg Kroah-Hartman free_tty_struct(tty); 148696fd7ce5SGreg Kroah-Hartman } 148796fd7ce5SGreg Kroah-Hartman 148896fd7ce5SGreg Kroah-Hartman static void queue_release_one_tty(struct kref *kref) 148996fd7ce5SGreg Kroah-Hartman { 149096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = container_of(kref, struct tty_struct, kref); 149196fd7ce5SGreg Kroah-Hartman 149296fd7ce5SGreg Kroah-Hartman if (tty->ops->shutdown) 149396fd7ce5SGreg Kroah-Hartman tty->ops->shutdown(tty); 149496fd7ce5SGreg Kroah-Hartman else 149596fd7ce5SGreg Kroah-Hartman tty_shutdown(tty); 149696fd7ce5SGreg Kroah-Hartman 149796fd7ce5SGreg Kroah-Hartman /* The hangup queue is now free so we can reuse it rather than 149896fd7ce5SGreg Kroah-Hartman waste a chunk of memory for each port */ 149996fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, release_one_tty); 150096fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 150196fd7ce5SGreg Kroah-Hartman } 150296fd7ce5SGreg Kroah-Hartman 150396fd7ce5SGreg Kroah-Hartman /** 150496fd7ce5SGreg Kroah-Hartman * tty_kref_put - release a tty kref 150596fd7ce5SGreg Kroah-Hartman * @tty: tty device 150696fd7ce5SGreg Kroah-Hartman * 150796fd7ce5SGreg Kroah-Hartman * Release a reference to a tty device and if need be let the kref 150896fd7ce5SGreg Kroah-Hartman * layer destruct the object for us 150996fd7ce5SGreg Kroah-Hartman */ 151096fd7ce5SGreg Kroah-Hartman 151196fd7ce5SGreg Kroah-Hartman void tty_kref_put(struct tty_struct *tty) 151296fd7ce5SGreg Kroah-Hartman { 151396fd7ce5SGreg Kroah-Hartman if (tty) 151496fd7ce5SGreg Kroah-Hartman kref_put(&tty->kref, queue_release_one_tty); 151596fd7ce5SGreg Kroah-Hartman } 151696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_kref_put); 151796fd7ce5SGreg Kroah-Hartman 151896fd7ce5SGreg Kroah-Hartman /** 151996fd7ce5SGreg Kroah-Hartman * release_tty - release tty structure memory 152096fd7ce5SGreg Kroah-Hartman * 152196fd7ce5SGreg Kroah-Hartman * Release both @tty and a possible linked partner (think pty pair), 152296fd7ce5SGreg Kroah-Hartman * and decrement the refcount of the backing module. 152396fd7ce5SGreg Kroah-Hartman * 152496fd7ce5SGreg Kroah-Hartman * Locking: 152596fd7ce5SGreg Kroah-Hartman * tty_mutex - sometimes only 152696fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 152796fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 152896fd7ce5SGreg Kroah-Hartman * FIXME: should we require tty_mutex is held here ?? 152996fd7ce5SGreg Kroah-Hartman * 153096fd7ce5SGreg Kroah-Hartman */ 153196fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx) 153296fd7ce5SGreg Kroah-Hartman { 153396fd7ce5SGreg Kroah-Hartman /* This should always be true but check for the moment */ 153496fd7ce5SGreg Kroah-Hartman WARN_ON(tty->index != idx); 153596fd7ce5SGreg Kroah-Hartman 153696fd7ce5SGreg Kroah-Hartman if (tty->link) 153796fd7ce5SGreg Kroah-Hartman tty_kref_put(tty->link); 153896fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 153996fd7ce5SGreg Kroah-Hartman } 154096fd7ce5SGreg Kroah-Hartman 154196fd7ce5SGreg Kroah-Hartman /** 154296fd7ce5SGreg Kroah-Hartman * tty_release - vfs callback for close 154396fd7ce5SGreg Kroah-Hartman * @inode: inode of tty 154496fd7ce5SGreg Kroah-Hartman * @filp: file pointer for handle to tty 154596fd7ce5SGreg Kroah-Hartman * 154696fd7ce5SGreg Kroah-Hartman * Called the last time each file handle is closed that references 154796fd7ce5SGreg Kroah-Hartman * this tty. There may however be several such references. 154896fd7ce5SGreg Kroah-Hartman * 154996fd7ce5SGreg Kroah-Hartman * Locking: 155096fd7ce5SGreg Kroah-Hartman * Takes bkl. See tty_release_dev 155196fd7ce5SGreg Kroah-Hartman * 155296fd7ce5SGreg Kroah-Hartman * Even releasing the tty structures is a tricky business.. We have 155396fd7ce5SGreg Kroah-Hartman * to be very careful that the structures are all released at the 155496fd7ce5SGreg Kroah-Hartman * same time, as interrupts might otherwise get the wrong pointers. 155596fd7ce5SGreg Kroah-Hartman * 155696fd7ce5SGreg Kroah-Hartman * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 155796fd7ce5SGreg Kroah-Hartman * lead to double frees or releasing memory still in use. 155896fd7ce5SGreg Kroah-Hartman */ 155996fd7ce5SGreg Kroah-Hartman 156096fd7ce5SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp) 156196fd7ce5SGreg Kroah-Hartman { 156296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 156396fd7ce5SGreg Kroah-Hartman struct tty_struct *o_tty; 156496fd7ce5SGreg Kroah-Hartman int pty_master, tty_closing, o_tty_closing, do_sleep; 156596fd7ce5SGreg Kroah-Hartman int devpts; 156696fd7ce5SGreg Kroah-Hartman int idx; 156796fd7ce5SGreg Kroah-Hartman char buf[64]; 156896fd7ce5SGreg Kroah-Hartman 156996fd7ce5SGreg Kroah-Hartman if (tty_paranoia_check(tty, inode, "tty_release_dev")) 157096fd7ce5SGreg Kroah-Hartman return 0; 157196fd7ce5SGreg Kroah-Hartman 157296fd7ce5SGreg Kroah-Hartman tty_lock(); 157396fd7ce5SGreg Kroah-Hartman check_tty_count(tty, "tty_release_dev"); 157496fd7ce5SGreg Kroah-Hartman 157596fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); 157696fd7ce5SGreg Kroah-Hartman 157796fd7ce5SGreg Kroah-Hartman idx = tty->index; 157896fd7ce5SGreg Kroah-Hartman pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && 157996fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER); 158096fd7ce5SGreg Kroah-Hartman devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0; 158196fd7ce5SGreg Kroah-Hartman o_tty = tty->link; 158296fd7ce5SGreg Kroah-Hartman 158396fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK 158496fd7ce5SGreg Kroah-Hartman if (idx < 0 || idx >= tty->driver->num) { 158596fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "tty_release_dev: bad idx when trying to " 158696fd7ce5SGreg Kroah-Hartman "free (%s)\n", tty->name); 158796fd7ce5SGreg Kroah-Hartman tty_unlock(); 158896fd7ce5SGreg Kroah-Hartman return 0; 158996fd7ce5SGreg Kroah-Hartman } 159096fd7ce5SGreg Kroah-Hartman if (!devpts) { 159196fd7ce5SGreg Kroah-Hartman if (tty != tty->driver->ttys[idx]) { 159296fd7ce5SGreg Kroah-Hartman tty_unlock(); 159396fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty " 159496fd7ce5SGreg Kroah-Hartman "for (%s)\n", idx, tty->name); 159596fd7ce5SGreg Kroah-Hartman return 0; 159696fd7ce5SGreg Kroah-Hartman } 159796fd7ce5SGreg Kroah-Hartman if (tty->termios != tty->driver->termios[idx]) { 159896fd7ce5SGreg Kroah-Hartman tty_unlock(); 159996fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios " 160096fd7ce5SGreg Kroah-Hartman "for (%s)\n", 160196fd7ce5SGreg Kroah-Hartman idx, tty->name); 160296fd7ce5SGreg Kroah-Hartman return 0; 160396fd7ce5SGreg Kroah-Hartman } 160496fd7ce5SGreg Kroah-Hartman } 160596fd7ce5SGreg Kroah-Hartman #endif 160696fd7ce5SGreg Kroah-Hartman 160796fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 160896fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...", 160996fd7ce5SGreg Kroah-Hartman tty_name(tty, buf), tty->count); 161096fd7ce5SGreg Kroah-Hartman #endif 161196fd7ce5SGreg Kroah-Hartman 161296fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK 161396fd7ce5SGreg Kroah-Hartman if (tty->driver->other && 161496fd7ce5SGreg Kroah-Hartman !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { 161596fd7ce5SGreg Kroah-Hartman if (o_tty != tty->driver->other->ttys[idx]) { 161696fd7ce5SGreg Kroah-Hartman tty_unlock(); 161796fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "tty_release_dev: other->table[%d] " 161896fd7ce5SGreg Kroah-Hartman "not o_tty for (%s)\n", 161996fd7ce5SGreg Kroah-Hartman idx, tty->name); 162096fd7ce5SGreg Kroah-Hartman return 0 ; 162196fd7ce5SGreg Kroah-Hartman } 162296fd7ce5SGreg Kroah-Hartman if (o_tty->termios != tty->driver->other->termios[idx]) { 162396fd7ce5SGreg Kroah-Hartman tty_unlock(); 162496fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "tty_release_dev: other->termios[%d] " 162596fd7ce5SGreg Kroah-Hartman "not o_termios for (%s)\n", 162696fd7ce5SGreg Kroah-Hartman idx, tty->name); 162796fd7ce5SGreg Kroah-Hartman return 0; 162896fd7ce5SGreg Kroah-Hartman } 162996fd7ce5SGreg Kroah-Hartman if (o_tty->link != tty) { 163096fd7ce5SGreg Kroah-Hartman tty_unlock(); 163196fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n"); 163296fd7ce5SGreg Kroah-Hartman return 0; 163396fd7ce5SGreg Kroah-Hartman } 163496fd7ce5SGreg Kroah-Hartman } 163596fd7ce5SGreg Kroah-Hartman #endif 163696fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 163796fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, filp); 163896fd7ce5SGreg Kroah-Hartman 163996fd7ce5SGreg Kroah-Hartman tty_unlock(); 164096fd7ce5SGreg Kroah-Hartman /* 164196fd7ce5SGreg Kroah-Hartman * Sanity check: if tty->count is going to zero, there shouldn't be 164296fd7ce5SGreg Kroah-Hartman * any waiters on tty->read_wait or tty->write_wait. We test the 164396fd7ce5SGreg Kroah-Hartman * wait queues and kick everyone out _before_ actually starting to 164496fd7ce5SGreg Kroah-Hartman * close. This ensures that we won't block while releasing the tty 164596fd7ce5SGreg Kroah-Hartman * structure. 164696fd7ce5SGreg Kroah-Hartman * 164796fd7ce5SGreg Kroah-Hartman * The test for the o_tty closing is necessary, since the master and 164896fd7ce5SGreg Kroah-Hartman * slave sides may close in any order. If the slave side closes out 164996fd7ce5SGreg Kroah-Hartman * first, its count will be one, since the master side holds an open. 165096fd7ce5SGreg Kroah-Hartman * Thus this test wouldn't be triggered at the time the slave closes, 165196fd7ce5SGreg Kroah-Hartman * so we do it now. 165296fd7ce5SGreg Kroah-Hartman * 165396fd7ce5SGreg Kroah-Hartman * Note that it's possible for the tty to be opened again while we're 165496fd7ce5SGreg Kroah-Hartman * flushing out waiters. By recalculating the closing flags before 165596fd7ce5SGreg Kroah-Hartman * each iteration we avoid any problems. 165696fd7ce5SGreg Kroah-Hartman */ 165796fd7ce5SGreg Kroah-Hartman while (1) { 165896fd7ce5SGreg Kroah-Hartman /* Guard against races with tty->count changes elsewhere and 165996fd7ce5SGreg Kroah-Hartman opens on /dev/tty */ 166096fd7ce5SGreg Kroah-Hartman 166196fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 166296fd7ce5SGreg Kroah-Hartman tty_lock(); 166396fd7ce5SGreg Kroah-Hartman tty_closing = tty->count <= 1; 166496fd7ce5SGreg Kroah-Hartman o_tty_closing = o_tty && 166596fd7ce5SGreg Kroah-Hartman (o_tty->count <= (pty_master ? 1 : 0)); 166696fd7ce5SGreg Kroah-Hartman do_sleep = 0; 166796fd7ce5SGreg Kroah-Hartman 166896fd7ce5SGreg Kroah-Hartman if (tty_closing) { 166996fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->read_wait)) { 167096fd7ce5SGreg Kroah-Hartman wake_up_poll(&tty->read_wait, POLLIN); 167196fd7ce5SGreg Kroah-Hartman do_sleep++; 167296fd7ce5SGreg Kroah-Hartman } 167396fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->write_wait)) { 167496fd7ce5SGreg Kroah-Hartman wake_up_poll(&tty->write_wait, POLLOUT); 167596fd7ce5SGreg Kroah-Hartman do_sleep++; 167696fd7ce5SGreg Kroah-Hartman } 167796fd7ce5SGreg Kroah-Hartman } 167896fd7ce5SGreg Kroah-Hartman if (o_tty_closing) { 167996fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->read_wait)) { 168096fd7ce5SGreg Kroah-Hartman wake_up_poll(&o_tty->read_wait, POLLIN); 168196fd7ce5SGreg Kroah-Hartman do_sleep++; 168296fd7ce5SGreg Kroah-Hartman } 168396fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->write_wait)) { 168496fd7ce5SGreg Kroah-Hartman wake_up_poll(&o_tty->write_wait, POLLOUT); 168596fd7ce5SGreg Kroah-Hartman do_sleep++; 168696fd7ce5SGreg Kroah-Hartman } 168796fd7ce5SGreg Kroah-Hartman } 168896fd7ce5SGreg Kroah-Hartman if (!do_sleep) 168996fd7ce5SGreg Kroah-Hartman break; 169096fd7ce5SGreg Kroah-Hartman 169196fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue " 169296fd7ce5SGreg Kroah-Hartman "active!\n", tty_name(tty, buf)); 169396fd7ce5SGreg Kroah-Hartman tty_unlock(); 169496fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 169596fd7ce5SGreg Kroah-Hartman schedule(); 169696fd7ce5SGreg Kroah-Hartman } 169796fd7ce5SGreg Kroah-Hartman 169896fd7ce5SGreg Kroah-Hartman /* 169996fd7ce5SGreg Kroah-Hartman * The closing flags are now consistent with the open counts on 170096fd7ce5SGreg Kroah-Hartman * both sides, and we've completed the last operation that could 170196fd7ce5SGreg Kroah-Hartman * block, so it's safe to proceed with closing. 170296fd7ce5SGreg Kroah-Hartman */ 170396fd7ce5SGreg Kroah-Hartman if (pty_master) { 170496fd7ce5SGreg Kroah-Hartman if (--o_tty->count < 0) { 170596fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "tty_release_dev: bad pty slave count " 170696fd7ce5SGreg Kroah-Hartman "(%d) for %s\n", 170796fd7ce5SGreg Kroah-Hartman o_tty->count, tty_name(o_tty, buf)); 170896fd7ce5SGreg Kroah-Hartman o_tty->count = 0; 170996fd7ce5SGreg Kroah-Hartman } 171096fd7ce5SGreg Kroah-Hartman } 171196fd7ce5SGreg Kroah-Hartman if (--tty->count < 0) { 171296fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n", 171396fd7ce5SGreg Kroah-Hartman tty->count, tty_name(tty, buf)); 171496fd7ce5SGreg Kroah-Hartman tty->count = 0; 171596fd7ce5SGreg Kroah-Hartman } 171696fd7ce5SGreg Kroah-Hartman 171796fd7ce5SGreg Kroah-Hartman /* 171896fd7ce5SGreg Kroah-Hartman * We've decremented tty->count, so we need to remove this file 171996fd7ce5SGreg Kroah-Hartman * descriptor off the tty->tty_files list; this serves two 172096fd7ce5SGreg Kroah-Hartman * purposes: 172196fd7ce5SGreg Kroah-Hartman * - check_tty_count sees the correct number of file descriptors 172296fd7ce5SGreg Kroah-Hartman * associated with this tty. 172396fd7ce5SGreg Kroah-Hartman * - do_tty_hangup no longer sees this file descriptor as 172496fd7ce5SGreg Kroah-Hartman * something that needs to be handled for hangups. 172596fd7ce5SGreg Kroah-Hartman */ 172696fd7ce5SGreg Kroah-Hartman tty_del_file(filp); 172796fd7ce5SGreg Kroah-Hartman 172896fd7ce5SGreg Kroah-Hartman /* 172996fd7ce5SGreg Kroah-Hartman * Perform some housekeeping before deciding whether to return. 173096fd7ce5SGreg Kroah-Hartman * 173196fd7ce5SGreg Kroah-Hartman * Set the TTY_CLOSING flag if this was the last open. In the 173296fd7ce5SGreg Kroah-Hartman * case of a pty we may have to wait around for the other side 173396fd7ce5SGreg Kroah-Hartman * to close, and TTY_CLOSING makes sure we can't be reopened. 173496fd7ce5SGreg Kroah-Hartman */ 173596fd7ce5SGreg Kroah-Hartman if (tty_closing) 173696fd7ce5SGreg Kroah-Hartman set_bit(TTY_CLOSING, &tty->flags); 173796fd7ce5SGreg Kroah-Hartman if (o_tty_closing) 173896fd7ce5SGreg Kroah-Hartman set_bit(TTY_CLOSING, &o_tty->flags); 173996fd7ce5SGreg Kroah-Hartman 174096fd7ce5SGreg Kroah-Hartman /* 174196fd7ce5SGreg Kroah-Hartman * If _either_ side is closing, make sure there aren't any 174296fd7ce5SGreg Kroah-Hartman * processes that still think tty or o_tty is their controlling 174396fd7ce5SGreg Kroah-Hartman * tty. 174496fd7ce5SGreg Kroah-Hartman */ 174596fd7ce5SGreg Kroah-Hartman if (tty_closing || o_tty_closing) { 174696fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 174796fd7ce5SGreg Kroah-Hartman session_clear_tty(tty->session); 174896fd7ce5SGreg Kroah-Hartman if (o_tty) 174996fd7ce5SGreg Kroah-Hartman session_clear_tty(o_tty->session); 175096fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 175196fd7ce5SGreg Kroah-Hartman } 175296fd7ce5SGreg Kroah-Hartman 175396fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 175496fd7ce5SGreg Kroah-Hartman 175596fd7ce5SGreg Kroah-Hartman /* check whether both sides are closing ... */ 175696fd7ce5SGreg Kroah-Hartman if (!tty_closing || (o_tty && !o_tty_closing)) { 175796fd7ce5SGreg Kroah-Hartman tty_unlock(); 175896fd7ce5SGreg Kroah-Hartman return 0; 175996fd7ce5SGreg Kroah-Hartman } 176096fd7ce5SGreg Kroah-Hartman 176196fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 176296fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "freeing tty structure..."); 176396fd7ce5SGreg Kroah-Hartman #endif 176496fd7ce5SGreg Kroah-Hartman /* 176596fd7ce5SGreg Kroah-Hartman * Ask the line discipline code to release its structures 176696fd7ce5SGreg Kroah-Hartman */ 176796fd7ce5SGreg Kroah-Hartman tty_ldisc_release(tty, o_tty); 176896fd7ce5SGreg Kroah-Hartman /* 176996fd7ce5SGreg Kroah-Hartman * The release_tty function takes care of the details of clearing 177096fd7ce5SGreg Kroah-Hartman * the slots and preserving the termios structure. 177196fd7ce5SGreg Kroah-Hartman */ 177296fd7ce5SGreg Kroah-Hartman release_tty(tty, idx); 177396fd7ce5SGreg Kroah-Hartman 177496fd7ce5SGreg Kroah-Hartman /* Make this pty number available for reallocation */ 177596fd7ce5SGreg Kroah-Hartman if (devpts) 177696fd7ce5SGreg Kroah-Hartman devpts_kill_index(inode, idx); 177796fd7ce5SGreg Kroah-Hartman tty_unlock(); 177896fd7ce5SGreg Kroah-Hartman return 0; 177996fd7ce5SGreg Kroah-Hartman } 178096fd7ce5SGreg Kroah-Hartman 178196fd7ce5SGreg Kroah-Hartman /** 178296fd7ce5SGreg Kroah-Hartman * tty_open - open a tty device 178396fd7ce5SGreg Kroah-Hartman * @inode: inode of device file 178496fd7ce5SGreg Kroah-Hartman * @filp: file pointer to tty 178596fd7ce5SGreg Kroah-Hartman * 178696fd7ce5SGreg Kroah-Hartman * tty_open and tty_release keep up the tty count that contains the 178796fd7ce5SGreg Kroah-Hartman * number of opens done on a tty. We cannot use the inode-count, as 178896fd7ce5SGreg Kroah-Hartman * different inodes might point to the same tty. 178996fd7ce5SGreg Kroah-Hartman * 179096fd7ce5SGreg Kroah-Hartman * Open-counting is needed for pty masters, as well as for keeping 179196fd7ce5SGreg Kroah-Hartman * track of serial lines: DTR is dropped when the last close happens. 179296fd7ce5SGreg Kroah-Hartman * (This is not done solely through tty->count, now. - Ted 1/27/92) 179396fd7ce5SGreg Kroah-Hartman * 179496fd7ce5SGreg Kroah-Hartman * The termios state of a pty is reset on first open so that 179596fd7ce5SGreg Kroah-Hartman * settings don't persist across reuse. 179696fd7ce5SGreg Kroah-Hartman * 179796fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work. 179896fd7ce5SGreg Kroah-Hartman * tty->count should protect the rest. 179996fd7ce5SGreg Kroah-Hartman * ->siglock protects ->signal/->sighand 180096fd7ce5SGreg Kroah-Hartman */ 180196fd7ce5SGreg Kroah-Hartman 180296fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *inode, struct file *filp) 180396fd7ce5SGreg Kroah-Hartman { 180496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = NULL; 180596fd7ce5SGreg Kroah-Hartman int noctty, retval; 180696fd7ce5SGreg Kroah-Hartman struct tty_driver *driver; 180796fd7ce5SGreg Kroah-Hartman int index; 180896fd7ce5SGreg Kroah-Hartman dev_t device = inode->i_rdev; 180996fd7ce5SGreg Kroah-Hartman unsigned saved_flags = filp->f_flags; 181096fd7ce5SGreg Kroah-Hartman 181196fd7ce5SGreg Kroah-Hartman nonseekable_open(inode, filp); 181296fd7ce5SGreg Kroah-Hartman 181396fd7ce5SGreg Kroah-Hartman retry_open: 181496fd7ce5SGreg Kroah-Hartman noctty = filp->f_flags & O_NOCTTY; 181596fd7ce5SGreg Kroah-Hartman index = -1; 181696fd7ce5SGreg Kroah-Hartman retval = 0; 181796fd7ce5SGreg Kroah-Hartman 181896fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 181996fd7ce5SGreg Kroah-Hartman tty_lock(); 182096fd7ce5SGreg Kroah-Hartman 182196fd7ce5SGreg Kroah-Hartman if (device == MKDEV(TTYAUX_MAJOR, 0)) { 182296fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 182396fd7ce5SGreg Kroah-Hartman if (!tty) { 182496fd7ce5SGreg Kroah-Hartman tty_unlock(); 182596fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 182696fd7ce5SGreg Kroah-Hartman return -ENXIO; 182796fd7ce5SGreg Kroah-Hartman } 182896fd7ce5SGreg Kroah-Hartman driver = tty_driver_kref_get(tty->driver); 182996fd7ce5SGreg Kroah-Hartman index = tty->index; 183096fd7ce5SGreg Kroah-Hartman filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ 183196fd7ce5SGreg Kroah-Hartman /* noctty = 1; */ 183296fd7ce5SGreg Kroah-Hartman /* FIXME: Should we take a driver reference ? */ 183396fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 183496fd7ce5SGreg Kroah-Hartman goto got_driver; 183596fd7ce5SGreg Kroah-Hartman } 183696fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT 183796fd7ce5SGreg Kroah-Hartman if (device == MKDEV(TTY_MAJOR, 0)) { 183896fd7ce5SGreg Kroah-Hartman extern struct tty_driver *console_driver; 183996fd7ce5SGreg Kroah-Hartman driver = tty_driver_kref_get(console_driver); 184096fd7ce5SGreg Kroah-Hartman index = fg_console; 184196fd7ce5SGreg Kroah-Hartman noctty = 1; 184296fd7ce5SGreg Kroah-Hartman goto got_driver; 184396fd7ce5SGreg Kroah-Hartman } 184496fd7ce5SGreg Kroah-Hartman #endif 184596fd7ce5SGreg Kroah-Hartman if (device == MKDEV(TTYAUX_MAJOR, 1)) { 184696fd7ce5SGreg Kroah-Hartman struct tty_driver *console_driver = console_device(&index); 184796fd7ce5SGreg Kroah-Hartman if (console_driver) { 184896fd7ce5SGreg Kroah-Hartman driver = tty_driver_kref_get(console_driver); 184996fd7ce5SGreg Kroah-Hartman if (driver) { 185096fd7ce5SGreg Kroah-Hartman /* Don't let /dev/console block */ 185196fd7ce5SGreg Kroah-Hartman filp->f_flags |= O_NONBLOCK; 185296fd7ce5SGreg Kroah-Hartman noctty = 1; 185396fd7ce5SGreg Kroah-Hartman goto got_driver; 185496fd7ce5SGreg Kroah-Hartman } 185596fd7ce5SGreg Kroah-Hartman } 185696fd7ce5SGreg Kroah-Hartman tty_unlock(); 185796fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 185896fd7ce5SGreg Kroah-Hartman return -ENODEV; 185996fd7ce5SGreg Kroah-Hartman } 186096fd7ce5SGreg Kroah-Hartman 186196fd7ce5SGreg Kroah-Hartman driver = get_tty_driver(device, &index); 186296fd7ce5SGreg Kroah-Hartman if (!driver) { 186396fd7ce5SGreg Kroah-Hartman tty_unlock(); 186496fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 186596fd7ce5SGreg Kroah-Hartman return -ENODEV; 186696fd7ce5SGreg Kroah-Hartman } 186796fd7ce5SGreg Kroah-Hartman got_driver: 186896fd7ce5SGreg Kroah-Hartman if (!tty) { 186996fd7ce5SGreg Kroah-Hartman /* check whether we're reopening an existing tty */ 187096fd7ce5SGreg Kroah-Hartman tty = tty_driver_lookup_tty(driver, inode, index); 187196fd7ce5SGreg Kroah-Hartman 187296fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty)) { 187396fd7ce5SGreg Kroah-Hartman tty_unlock(); 187496fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 187596fd7ce5SGreg Kroah-Hartman return PTR_ERR(tty); 187696fd7ce5SGreg Kroah-Hartman } 187796fd7ce5SGreg Kroah-Hartman } 187896fd7ce5SGreg Kroah-Hartman 187996fd7ce5SGreg Kroah-Hartman if (tty) { 188096fd7ce5SGreg Kroah-Hartman retval = tty_reopen(tty); 188196fd7ce5SGreg Kroah-Hartman if (retval) 188296fd7ce5SGreg Kroah-Hartman tty = ERR_PTR(retval); 188396fd7ce5SGreg Kroah-Hartman } else 188496fd7ce5SGreg Kroah-Hartman tty = tty_init_dev(driver, index, 0); 188596fd7ce5SGreg Kroah-Hartman 188696fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 188796fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(driver); 188896fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty)) { 188996fd7ce5SGreg Kroah-Hartman tty_unlock(); 189096fd7ce5SGreg Kroah-Hartman return PTR_ERR(tty); 189196fd7ce5SGreg Kroah-Hartman } 189296fd7ce5SGreg Kroah-Hartman 189396fd7ce5SGreg Kroah-Hartman retval = tty_add_file(tty, filp); 189496fd7ce5SGreg Kroah-Hartman if (retval) { 189596fd7ce5SGreg Kroah-Hartman tty_unlock(); 18960259894cSJiri Slaby tty_release(inode, filp); 189796fd7ce5SGreg Kroah-Hartman return retval; 189896fd7ce5SGreg Kroah-Hartman } 189996fd7ce5SGreg Kroah-Hartman 190096fd7ce5SGreg Kroah-Hartman check_tty_count(tty, "tty_open"); 190196fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 190296fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER) 190396fd7ce5SGreg Kroah-Hartman noctty = 1; 190496fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 190596fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "opening %s...", tty->name); 190696fd7ce5SGreg Kroah-Hartman #endif 190796fd7ce5SGreg Kroah-Hartman if (tty->ops->open) 190896fd7ce5SGreg Kroah-Hartman retval = tty->ops->open(tty, filp); 190996fd7ce5SGreg Kroah-Hartman else 191096fd7ce5SGreg Kroah-Hartman retval = -ENODEV; 191196fd7ce5SGreg Kroah-Hartman filp->f_flags = saved_flags; 191296fd7ce5SGreg Kroah-Hartman 191396fd7ce5SGreg Kroah-Hartman if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && 191496fd7ce5SGreg Kroah-Hartman !capable(CAP_SYS_ADMIN)) 191596fd7ce5SGreg Kroah-Hartman retval = -EBUSY; 191696fd7ce5SGreg Kroah-Hartman 191796fd7ce5SGreg Kroah-Hartman if (retval) { 191896fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 191996fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "error %d in opening %s...", retval, 192096fd7ce5SGreg Kroah-Hartman tty->name); 192196fd7ce5SGreg Kroah-Hartman #endif 192296fd7ce5SGreg Kroah-Hartman tty_unlock(); /* need to call tty_release without BTM */ 192396fd7ce5SGreg Kroah-Hartman tty_release(inode, filp); 192496fd7ce5SGreg Kroah-Hartman if (retval != -ERESTARTSYS) 192596fd7ce5SGreg Kroah-Hartman return retval; 192696fd7ce5SGreg Kroah-Hartman 192796fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 192896fd7ce5SGreg Kroah-Hartman return retval; 192996fd7ce5SGreg Kroah-Hartman 193096fd7ce5SGreg Kroah-Hartman schedule(); 193196fd7ce5SGreg Kroah-Hartman /* 193296fd7ce5SGreg Kroah-Hartman * Need to reset f_op in case a hangup happened. 193396fd7ce5SGreg Kroah-Hartman */ 193496fd7ce5SGreg Kroah-Hartman tty_lock(); 193596fd7ce5SGreg Kroah-Hartman if (filp->f_op == &hung_up_tty_fops) 193696fd7ce5SGreg Kroah-Hartman filp->f_op = &tty_fops; 193796fd7ce5SGreg Kroah-Hartman tty_unlock(); 193896fd7ce5SGreg Kroah-Hartman goto retry_open; 193996fd7ce5SGreg Kroah-Hartman } 194096fd7ce5SGreg Kroah-Hartman tty_unlock(); 194196fd7ce5SGreg Kroah-Hartman 194296fd7ce5SGreg Kroah-Hartman 194396fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 194496fd7ce5SGreg Kroah-Hartman tty_lock(); 194596fd7ce5SGreg Kroah-Hartman spin_lock_irq(¤t->sighand->siglock); 194696fd7ce5SGreg Kroah-Hartman if (!noctty && 194796fd7ce5SGreg Kroah-Hartman current->signal->leader && 194896fd7ce5SGreg Kroah-Hartman !current->signal->tty && 194996fd7ce5SGreg Kroah-Hartman tty->session == NULL) 195096fd7ce5SGreg Kroah-Hartman __proc_set_tty(current, tty); 195196fd7ce5SGreg Kroah-Hartman spin_unlock_irq(¤t->sighand->siglock); 195296fd7ce5SGreg Kroah-Hartman tty_unlock(); 195396fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 195496fd7ce5SGreg Kroah-Hartman return 0; 195596fd7ce5SGreg Kroah-Hartman } 195696fd7ce5SGreg Kroah-Hartman 195796fd7ce5SGreg Kroah-Hartman 195896fd7ce5SGreg Kroah-Hartman 195996fd7ce5SGreg Kroah-Hartman /** 196096fd7ce5SGreg Kroah-Hartman * tty_poll - check tty status 196196fd7ce5SGreg Kroah-Hartman * @filp: file being polled 196296fd7ce5SGreg Kroah-Hartman * @wait: poll wait structures to update 196396fd7ce5SGreg Kroah-Hartman * 196496fd7ce5SGreg Kroah-Hartman * Call the line discipline polling method to obtain the poll 196596fd7ce5SGreg Kroah-Hartman * status of the device. 196696fd7ce5SGreg Kroah-Hartman * 196796fd7ce5SGreg Kroah-Hartman * Locking: locks called line discipline but ldisc poll method 196896fd7ce5SGreg Kroah-Hartman * may be re-entered freely by other callers. 196996fd7ce5SGreg Kroah-Hartman */ 197096fd7ce5SGreg Kroah-Hartman 197196fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *filp, poll_table *wait) 197296fd7ce5SGreg Kroah-Hartman { 197396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 197496fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 197596fd7ce5SGreg Kroah-Hartman int ret = 0; 197696fd7ce5SGreg Kroah-Hartman 197796fd7ce5SGreg Kroah-Hartman if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll")) 197896fd7ce5SGreg Kroah-Hartman return 0; 197996fd7ce5SGreg Kroah-Hartman 198096fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 198196fd7ce5SGreg Kroah-Hartman if (ld->ops->poll) 198296fd7ce5SGreg Kroah-Hartman ret = (ld->ops->poll)(tty, filp, wait); 198396fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 198496fd7ce5SGreg Kroah-Hartman return ret; 198596fd7ce5SGreg Kroah-Hartman } 198696fd7ce5SGreg Kroah-Hartman 198796fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on) 198896fd7ce5SGreg Kroah-Hartman { 198996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 199096fd7ce5SGreg Kroah-Hartman unsigned long flags; 199196fd7ce5SGreg Kroah-Hartman int retval = 0; 199296fd7ce5SGreg Kroah-Hartman 199396fd7ce5SGreg Kroah-Hartman if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync")) 199496fd7ce5SGreg Kroah-Hartman goto out; 199596fd7ce5SGreg Kroah-Hartman 199696fd7ce5SGreg Kroah-Hartman retval = fasync_helper(fd, filp, on, &tty->fasync); 199796fd7ce5SGreg Kroah-Hartman if (retval <= 0) 199896fd7ce5SGreg Kroah-Hartman goto out; 199996fd7ce5SGreg Kroah-Hartman 200096fd7ce5SGreg Kroah-Hartman if (on) { 200196fd7ce5SGreg Kroah-Hartman enum pid_type type; 200296fd7ce5SGreg Kroah-Hartman struct pid *pid; 200396fd7ce5SGreg Kroah-Hartman if (!waitqueue_active(&tty->read_wait)) 200496fd7ce5SGreg Kroah-Hartman tty->minimum_to_wake = 1; 200596fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 200696fd7ce5SGreg Kroah-Hartman if (tty->pgrp) { 200796fd7ce5SGreg Kroah-Hartman pid = tty->pgrp; 200896fd7ce5SGreg Kroah-Hartman type = PIDTYPE_PGID; 200996fd7ce5SGreg Kroah-Hartman } else { 201096fd7ce5SGreg Kroah-Hartman pid = task_pid(current); 201196fd7ce5SGreg Kroah-Hartman type = PIDTYPE_PID; 201296fd7ce5SGreg Kroah-Hartman } 201396fd7ce5SGreg Kroah-Hartman get_pid(pid); 201496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 201596fd7ce5SGreg Kroah-Hartman retval = __f_setown(filp, pid, type, 0); 201696fd7ce5SGreg Kroah-Hartman put_pid(pid); 201796fd7ce5SGreg Kroah-Hartman if (retval) 201896fd7ce5SGreg Kroah-Hartman goto out; 201996fd7ce5SGreg Kroah-Hartman } else { 202096fd7ce5SGreg Kroah-Hartman if (!tty->fasync && !waitqueue_active(&tty->read_wait)) 202196fd7ce5SGreg Kroah-Hartman tty->minimum_to_wake = N_TTY_BUF_SIZE; 202296fd7ce5SGreg Kroah-Hartman } 202396fd7ce5SGreg Kroah-Hartman retval = 0; 202496fd7ce5SGreg Kroah-Hartman out: 202596fd7ce5SGreg Kroah-Hartman return retval; 202696fd7ce5SGreg Kroah-Hartman } 202796fd7ce5SGreg Kroah-Hartman 202896fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on) 202996fd7ce5SGreg Kroah-Hartman { 203096fd7ce5SGreg Kroah-Hartman int retval; 203196fd7ce5SGreg Kroah-Hartman tty_lock(); 203296fd7ce5SGreg Kroah-Hartman retval = __tty_fasync(fd, filp, on); 203396fd7ce5SGreg Kroah-Hartman tty_unlock(); 203496fd7ce5SGreg Kroah-Hartman return retval; 203596fd7ce5SGreg Kroah-Hartman } 203696fd7ce5SGreg Kroah-Hartman 203796fd7ce5SGreg Kroah-Hartman /** 203896fd7ce5SGreg Kroah-Hartman * tiocsti - fake input character 203996fd7ce5SGreg Kroah-Hartman * @tty: tty to fake input into 204096fd7ce5SGreg Kroah-Hartman * @p: pointer to character 204196fd7ce5SGreg Kroah-Hartman * 204296fd7ce5SGreg Kroah-Hartman * Fake input to a tty device. Does the necessary locking and 204396fd7ce5SGreg Kroah-Hartman * input management. 204496fd7ce5SGreg Kroah-Hartman * 204596fd7ce5SGreg Kroah-Hartman * FIXME: does not honour flow control ?? 204696fd7ce5SGreg Kroah-Hartman * 204796fd7ce5SGreg Kroah-Hartman * Locking: 204896fd7ce5SGreg Kroah-Hartman * Called functions take tty_ldisc_lock 204996fd7ce5SGreg Kroah-Hartman * current->signal->tty check is safe without locks 205096fd7ce5SGreg Kroah-Hartman * 205196fd7ce5SGreg Kroah-Hartman * FIXME: may race normal receive processing 205296fd7ce5SGreg Kroah-Hartman */ 205396fd7ce5SGreg Kroah-Hartman 205496fd7ce5SGreg Kroah-Hartman static int tiocsti(struct tty_struct *tty, char __user *p) 205596fd7ce5SGreg Kroah-Hartman { 205696fd7ce5SGreg Kroah-Hartman char ch, mbz = 0; 205796fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 205896fd7ce5SGreg Kroah-Hartman 205996fd7ce5SGreg Kroah-Hartman if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) 206096fd7ce5SGreg Kroah-Hartman return -EPERM; 206196fd7ce5SGreg Kroah-Hartman if (get_user(ch, p)) 206296fd7ce5SGreg Kroah-Hartman return -EFAULT; 206396fd7ce5SGreg Kroah-Hartman tty_audit_tiocsti(tty, ch); 206496fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 206596fd7ce5SGreg Kroah-Hartman ld->ops->receive_buf(tty, &ch, &mbz, 1); 206696fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 206796fd7ce5SGreg Kroah-Hartman return 0; 206896fd7ce5SGreg Kroah-Hartman } 206996fd7ce5SGreg Kroah-Hartman 207096fd7ce5SGreg Kroah-Hartman /** 207196fd7ce5SGreg Kroah-Hartman * tiocgwinsz - implement window query ioctl 207296fd7ce5SGreg Kroah-Hartman * @tty; tty 207396fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 207496fd7ce5SGreg Kroah-Hartman * 207596fd7ce5SGreg Kroah-Hartman * Copies the kernel idea of the window size into the user buffer. 207696fd7ce5SGreg Kroah-Hartman * 207796fd7ce5SGreg Kroah-Hartman * Locking: tty->termios_mutex is taken to ensure the winsize data 207896fd7ce5SGreg Kroah-Hartman * is consistent. 207996fd7ce5SGreg Kroah-Hartman */ 208096fd7ce5SGreg Kroah-Hartman 208196fd7ce5SGreg Kroah-Hartman static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) 208296fd7ce5SGreg Kroah-Hartman { 208396fd7ce5SGreg Kroah-Hartman int err; 208496fd7ce5SGreg Kroah-Hartman 208596fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->termios_mutex); 208696fd7ce5SGreg Kroah-Hartman err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); 208796fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->termios_mutex); 208896fd7ce5SGreg Kroah-Hartman 208996fd7ce5SGreg Kroah-Hartman return err ? -EFAULT: 0; 209096fd7ce5SGreg Kroah-Hartman } 209196fd7ce5SGreg Kroah-Hartman 209296fd7ce5SGreg Kroah-Hartman /** 209396fd7ce5SGreg Kroah-Hartman * tty_do_resize - resize event 209496fd7ce5SGreg Kroah-Hartman * @tty: tty being resized 209596fd7ce5SGreg Kroah-Hartman * @rows: rows (character) 209696fd7ce5SGreg Kroah-Hartman * @cols: cols (character) 209796fd7ce5SGreg Kroah-Hartman * 209896fd7ce5SGreg Kroah-Hartman * Update the termios variables and send the necessary signals to 209996fd7ce5SGreg Kroah-Hartman * peform a terminal resize correctly 210096fd7ce5SGreg Kroah-Hartman */ 210196fd7ce5SGreg Kroah-Hartman 210296fd7ce5SGreg Kroah-Hartman int tty_do_resize(struct tty_struct *tty, struct winsize *ws) 210396fd7ce5SGreg Kroah-Hartman { 210496fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 210596fd7ce5SGreg Kroah-Hartman unsigned long flags; 210696fd7ce5SGreg Kroah-Hartman 210796fd7ce5SGreg Kroah-Hartman /* Lock the tty */ 210896fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->termios_mutex); 210996fd7ce5SGreg Kroah-Hartman if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 211096fd7ce5SGreg Kroah-Hartman goto done; 211196fd7ce5SGreg Kroah-Hartman /* Get the PID values and reference them so we can 211296fd7ce5SGreg Kroah-Hartman avoid holding the tty ctrl lock while sending signals */ 211396fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 211496fd7ce5SGreg Kroah-Hartman pgrp = get_pid(tty->pgrp); 211596fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 211696fd7ce5SGreg Kroah-Hartman 211796fd7ce5SGreg Kroah-Hartman if (pgrp) 211896fd7ce5SGreg Kroah-Hartman kill_pgrp(pgrp, SIGWINCH, 1); 211996fd7ce5SGreg Kroah-Hartman put_pid(pgrp); 212096fd7ce5SGreg Kroah-Hartman 212196fd7ce5SGreg Kroah-Hartman tty->winsize = *ws; 212296fd7ce5SGreg Kroah-Hartman done: 212396fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->termios_mutex); 212496fd7ce5SGreg Kroah-Hartman return 0; 212596fd7ce5SGreg Kroah-Hartman } 212696fd7ce5SGreg Kroah-Hartman 212796fd7ce5SGreg Kroah-Hartman /** 212896fd7ce5SGreg Kroah-Hartman * tiocswinsz - implement window size set ioctl 212996fd7ce5SGreg Kroah-Hartman * @tty; tty side of tty 213096fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 213196fd7ce5SGreg Kroah-Hartman * 213296fd7ce5SGreg Kroah-Hartman * Copies the user idea of the window size to the kernel. Traditionally 213396fd7ce5SGreg Kroah-Hartman * this is just advisory information but for the Linux console it 213496fd7ce5SGreg Kroah-Hartman * actually has driver level meaning and triggers a VC resize. 213596fd7ce5SGreg Kroah-Hartman * 213696fd7ce5SGreg Kroah-Hartman * Locking: 213725985edcSLucas De Marchi * Driver dependent. The default do_resize method takes the 213896fd7ce5SGreg Kroah-Hartman * tty termios mutex and ctrl_lock. The console takes its own lock 213996fd7ce5SGreg Kroah-Hartman * then calls into the default method. 214096fd7ce5SGreg Kroah-Hartman */ 214196fd7ce5SGreg Kroah-Hartman 214296fd7ce5SGreg Kroah-Hartman static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) 214396fd7ce5SGreg Kroah-Hartman { 214496fd7ce5SGreg Kroah-Hartman struct winsize tmp_ws; 214596fd7ce5SGreg Kroah-Hartman if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) 214696fd7ce5SGreg Kroah-Hartman return -EFAULT; 214796fd7ce5SGreg Kroah-Hartman 214896fd7ce5SGreg Kroah-Hartman if (tty->ops->resize) 214996fd7ce5SGreg Kroah-Hartman return tty->ops->resize(tty, &tmp_ws); 215096fd7ce5SGreg Kroah-Hartman else 215196fd7ce5SGreg Kroah-Hartman return tty_do_resize(tty, &tmp_ws); 215296fd7ce5SGreg Kroah-Hartman } 215396fd7ce5SGreg Kroah-Hartman 215496fd7ce5SGreg Kroah-Hartman /** 215596fd7ce5SGreg Kroah-Hartman * tioccons - allow admin to move logical console 215696fd7ce5SGreg Kroah-Hartman * @file: the file to become console 215796fd7ce5SGreg Kroah-Hartman * 215825985edcSLucas De Marchi * Allow the administrator to move the redirected console device 215996fd7ce5SGreg Kroah-Hartman * 216096fd7ce5SGreg Kroah-Hartman * Locking: uses redirect_lock to guard the redirect information 216196fd7ce5SGreg Kroah-Hartman */ 216296fd7ce5SGreg Kroah-Hartman 216396fd7ce5SGreg Kroah-Hartman static int tioccons(struct file *file) 216496fd7ce5SGreg Kroah-Hartman { 216596fd7ce5SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) 216696fd7ce5SGreg Kroah-Hartman return -EPERM; 216796fd7ce5SGreg Kroah-Hartman if (file->f_op->write == redirected_tty_write) { 216896fd7ce5SGreg Kroah-Hartman struct file *f; 216996fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 217096fd7ce5SGreg Kroah-Hartman f = redirect; 217196fd7ce5SGreg Kroah-Hartman redirect = NULL; 217296fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 217396fd7ce5SGreg Kroah-Hartman if (f) 217496fd7ce5SGreg Kroah-Hartman fput(f); 217596fd7ce5SGreg Kroah-Hartman return 0; 217696fd7ce5SGreg Kroah-Hartman } 217796fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 217896fd7ce5SGreg Kroah-Hartman if (redirect) { 217996fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 218096fd7ce5SGreg Kroah-Hartman return -EBUSY; 218196fd7ce5SGreg Kroah-Hartman } 218296fd7ce5SGreg Kroah-Hartman get_file(file); 218396fd7ce5SGreg Kroah-Hartman redirect = file; 218496fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 218596fd7ce5SGreg Kroah-Hartman return 0; 218696fd7ce5SGreg Kroah-Hartman } 218796fd7ce5SGreg Kroah-Hartman 218896fd7ce5SGreg Kroah-Hartman /** 218996fd7ce5SGreg Kroah-Hartman * fionbio - non blocking ioctl 219096fd7ce5SGreg Kroah-Hartman * @file: file to set blocking value 219196fd7ce5SGreg Kroah-Hartman * @p: user parameter 219296fd7ce5SGreg Kroah-Hartman * 219396fd7ce5SGreg Kroah-Hartman * Historical tty interfaces had a blocking control ioctl before 219496fd7ce5SGreg Kroah-Hartman * the generic functionality existed. This piece of history is preserved 219596fd7ce5SGreg Kroah-Hartman * in the expected tty API of posix OS's. 219696fd7ce5SGreg Kroah-Hartman * 219796fd7ce5SGreg Kroah-Hartman * Locking: none, the open file handle ensures it won't go away. 219896fd7ce5SGreg Kroah-Hartman */ 219996fd7ce5SGreg Kroah-Hartman 220096fd7ce5SGreg Kroah-Hartman static int fionbio(struct file *file, int __user *p) 220196fd7ce5SGreg Kroah-Hartman { 220296fd7ce5SGreg Kroah-Hartman int nonblock; 220396fd7ce5SGreg Kroah-Hartman 220496fd7ce5SGreg Kroah-Hartman if (get_user(nonblock, p)) 220596fd7ce5SGreg Kroah-Hartman return -EFAULT; 220696fd7ce5SGreg Kroah-Hartman 220796fd7ce5SGreg Kroah-Hartman spin_lock(&file->f_lock); 220896fd7ce5SGreg Kroah-Hartman if (nonblock) 220996fd7ce5SGreg Kroah-Hartman file->f_flags |= O_NONBLOCK; 221096fd7ce5SGreg Kroah-Hartman else 221196fd7ce5SGreg Kroah-Hartman file->f_flags &= ~O_NONBLOCK; 221296fd7ce5SGreg Kroah-Hartman spin_unlock(&file->f_lock); 221396fd7ce5SGreg Kroah-Hartman return 0; 221496fd7ce5SGreg Kroah-Hartman } 221596fd7ce5SGreg Kroah-Hartman 221696fd7ce5SGreg Kroah-Hartman /** 221796fd7ce5SGreg Kroah-Hartman * tiocsctty - set controlling tty 221896fd7ce5SGreg Kroah-Hartman * @tty: tty structure 221996fd7ce5SGreg Kroah-Hartman * @arg: user argument 222096fd7ce5SGreg Kroah-Hartman * 222196fd7ce5SGreg Kroah-Hartman * This ioctl is used to manage job control. It permits a session 222296fd7ce5SGreg Kroah-Hartman * leader to set this tty as the controlling tty for the session. 222396fd7ce5SGreg Kroah-Hartman * 222496fd7ce5SGreg Kroah-Hartman * Locking: 222596fd7ce5SGreg Kroah-Hartman * Takes tty_mutex() to protect tty instance 222696fd7ce5SGreg Kroah-Hartman * Takes tasklist_lock internally to walk sessions 222796fd7ce5SGreg Kroah-Hartman * Takes ->siglock() when updating signal->tty 222896fd7ce5SGreg Kroah-Hartman */ 222996fd7ce5SGreg Kroah-Hartman 223096fd7ce5SGreg Kroah-Hartman static int tiocsctty(struct tty_struct *tty, int arg) 223196fd7ce5SGreg Kroah-Hartman { 223296fd7ce5SGreg Kroah-Hartman int ret = 0; 223396fd7ce5SGreg Kroah-Hartman if (current->signal->leader && (task_session(current) == tty->session)) 223496fd7ce5SGreg Kroah-Hartman return ret; 223596fd7ce5SGreg Kroah-Hartman 223696fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 223796fd7ce5SGreg Kroah-Hartman /* 223896fd7ce5SGreg Kroah-Hartman * The process must be a session leader and 223996fd7ce5SGreg Kroah-Hartman * not have a controlling tty already. 224096fd7ce5SGreg Kroah-Hartman */ 224196fd7ce5SGreg Kroah-Hartman if (!current->signal->leader || current->signal->tty) { 224296fd7ce5SGreg Kroah-Hartman ret = -EPERM; 224396fd7ce5SGreg Kroah-Hartman goto unlock; 224496fd7ce5SGreg Kroah-Hartman } 224596fd7ce5SGreg Kroah-Hartman 224696fd7ce5SGreg Kroah-Hartman if (tty->session) { 224796fd7ce5SGreg Kroah-Hartman /* 224896fd7ce5SGreg Kroah-Hartman * This tty is already the controlling 224996fd7ce5SGreg Kroah-Hartman * tty for another session group! 225096fd7ce5SGreg Kroah-Hartman */ 225196fd7ce5SGreg Kroah-Hartman if (arg == 1 && capable(CAP_SYS_ADMIN)) { 225296fd7ce5SGreg Kroah-Hartman /* 225396fd7ce5SGreg Kroah-Hartman * Steal it away 225496fd7ce5SGreg Kroah-Hartman */ 225596fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 225696fd7ce5SGreg Kroah-Hartman session_clear_tty(tty->session); 225796fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 225896fd7ce5SGreg Kroah-Hartman } else { 225996fd7ce5SGreg Kroah-Hartman ret = -EPERM; 226096fd7ce5SGreg Kroah-Hartman goto unlock; 226196fd7ce5SGreg Kroah-Hartman } 226296fd7ce5SGreg Kroah-Hartman } 226396fd7ce5SGreg Kroah-Hartman proc_set_tty(current, tty); 226496fd7ce5SGreg Kroah-Hartman unlock: 226596fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 226696fd7ce5SGreg Kroah-Hartman return ret; 226796fd7ce5SGreg Kroah-Hartman } 226896fd7ce5SGreg Kroah-Hartman 226996fd7ce5SGreg Kroah-Hartman /** 227096fd7ce5SGreg Kroah-Hartman * tty_get_pgrp - return a ref counted pgrp pid 227196fd7ce5SGreg Kroah-Hartman * @tty: tty to read 227296fd7ce5SGreg Kroah-Hartman * 227396fd7ce5SGreg Kroah-Hartman * Returns a refcounted instance of the pid struct for the process 227496fd7ce5SGreg Kroah-Hartman * group controlling the tty. 227596fd7ce5SGreg Kroah-Hartman */ 227696fd7ce5SGreg Kroah-Hartman 227796fd7ce5SGreg Kroah-Hartman struct pid *tty_get_pgrp(struct tty_struct *tty) 227896fd7ce5SGreg Kroah-Hartman { 227996fd7ce5SGreg Kroah-Hartman unsigned long flags; 228096fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 228196fd7ce5SGreg Kroah-Hartman 228296fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 228396fd7ce5SGreg Kroah-Hartman pgrp = get_pid(tty->pgrp); 228496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 228596fd7ce5SGreg Kroah-Hartman 228696fd7ce5SGreg Kroah-Hartman return pgrp; 228796fd7ce5SGreg Kroah-Hartman } 228896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_get_pgrp); 228996fd7ce5SGreg Kroah-Hartman 229096fd7ce5SGreg Kroah-Hartman /** 229196fd7ce5SGreg Kroah-Hartman * tiocgpgrp - get process group 229296fd7ce5SGreg Kroah-Hartman * @tty: tty passed by user 229325985edcSLucas De Marchi * @real_tty: tty side of the tty passed by the user if a pty else the tty 229496fd7ce5SGreg Kroah-Hartman * @p: returned pid 229596fd7ce5SGreg Kroah-Hartman * 229696fd7ce5SGreg Kroah-Hartman * Obtain the process group of the tty. If there is no process group 229796fd7ce5SGreg Kroah-Hartman * return an error. 229896fd7ce5SGreg Kroah-Hartman * 229996fd7ce5SGreg Kroah-Hartman * Locking: none. Reference to current->signal->tty is safe. 230096fd7ce5SGreg Kroah-Hartman */ 230196fd7ce5SGreg Kroah-Hartman 230296fd7ce5SGreg Kroah-Hartman static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 230396fd7ce5SGreg Kroah-Hartman { 230496fd7ce5SGreg Kroah-Hartman struct pid *pid; 230596fd7ce5SGreg Kroah-Hartman int ret; 230696fd7ce5SGreg Kroah-Hartman /* 230796fd7ce5SGreg Kroah-Hartman * (tty == real_tty) is a cheap way of 230896fd7ce5SGreg Kroah-Hartman * testing if the tty is NOT a master pty. 230996fd7ce5SGreg Kroah-Hartman */ 231096fd7ce5SGreg Kroah-Hartman if (tty == real_tty && current->signal->tty != real_tty) 231196fd7ce5SGreg Kroah-Hartman return -ENOTTY; 231296fd7ce5SGreg Kroah-Hartman pid = tty_get_pgrp(real_tty); 231396fd7ce5SGreg Kroah-Hartman ret = put_user(pid_vnr(pid), p); 231496fd7ce5SGreg Kroah-Hartman put_pid(pid); 231596fd7ce5SGreg Kroah-Hartman return ret; 231696fd7ce5SGreg Kroah-Hartman } 231796fd7ce5SGreg Kroah-Hartman 231896fd7ce5SGreg Kroah-Hartman /** 231996fd7ce5SGreg Kroah-Hartman * tiocspgrp - attempt to set process group 232096fd7ce5SGreg Kroah-Hartman * @tty: tty passed by user 232196fd7ce5SGreg Kroah-Hartman * @real_tty: tty side device matching tty passed by user 232296fd7ce5SGreg Kroah-Hartman * @p: pid pointer 232396fd7ce5SGreg Kroah-Hartman * 232496fd7ce5SGreg Kroah-Hartman * Set the process group of the tty to the session passed. Only 232596fd7ce5SGreg Kroah-Hartman * permitted where the tty session is our session. 232696fd7ce5SGreg Kroah-Hartman * 232796fd7ce5SGreg Kroah-Hartman * Locking: RCU, ctrl lock 232896fd7ce5SGreg Kroah-Hartman */ 232996fd7ce5SGreg Kroah-Hartman 233096fd7ce5SGreg Kroah-Hartman static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 233196fd7ce5SGreg Kroah-Hartman { 233296fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 233396fd7ce5SGreg Kroah-Hartman pid_t pgrp_nr; 233496fd7ce5SGreg Kroah-Hartman int retval = tty_check_change(real_tty); 233596fd7ce5SGreg Kroah-Hartman unsigned long flags; 233696fd7ce5SGreg Kroah-Hartman 233796fd7ce5SGreg Kroah-Hartman if (retval == -EIO) 233896fd7ce5SGreg Kroah-Hartman return -ENOTTY; 233996fd7ce5SGreg Kroah-Hartman if (retval) 234096fd7ce5SGreg Kroah-Hartman return retval; 234196fd7ce5SGreg Kroah-Hartman if (!current->signal->tty || 234296fd7ce5SGreg Kroah-Hartman (current->signal->tty != real_tty) || 234396fd7ce5SGreg Kroah-Hartman (real_tty->session != task_session(current))) 234496fd7ce5SGreg Kroah-Hartman return -ENOTTY; 234596fd7ce5SGreg Kroah-Hartman if (get_user(pgrp_nr, p)) 234696fd7ce5SGreg Kroah-Hartman return -EFAULT; 234796fd7ce5SGreg Kroah-Hartman if (pgrp_nr < 0) 234896fd7ce5SGreg Kroah-Hartman return -EINVAL; 234996fd7ce5SGreg Kroah-Hartman rcu_read_lock(); 235096fd7ce5SGreg Kroah-Hartman pgrp = find_vpid(pgrp_nr); 235196fd7ce5SGreg Kroah-Hartman retval = -ESRCH; 235296fd7ce5SGreg Kroah-Hartman if (!pgrp) 235396fd7ce5SGreg Kroah-Hartman goto out_unlock; 235496fd7ce5SGreg Kroah-Hartman retval = -EPERM; 235596fd7ce5SGreg Kroah-Hartman if (session_of_pgrp(pgrp) != task_session(current)) 235696fd7ce5SGreg Kroah-Hartman goto out_unlock; 235796fd7ce5SGreg Kroah-Hartman retval = 0; 235896fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 235996fd7ce5SGreg Kroah-Hartman put_pid(real_tty->pgrp); 236096fd7ce5SGreg Kroah-Hartman real_tty->pgrp = get_pid(pgrp); 236196fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 236296fd7ce5SGreg Kroah-Hartman out_unlock: 236396fd7ce5SGreg Kroah-Hartman rcu_read_unlock(); 236496fd7ce5SGreg Kroah-Hartman return retval; 236596fd7ce5SGreg Kroah-Hartman } 236696fd7ce5SGreg Kroah-Hartman 236796fd7ce5SGreg Kroah-Hartman /** 236896fd7ce5SGreg Kroah-Hartman * tiocgsid - get session id 236996fd7ce5SGreg Kroah-Hartman * @tty: tty passed by user 237025985edcSLucas De Marchi * @real_tty: tty side of the tty passed by the user if a pty else the tty 237196fd7ce5SGreg Kroah-Hartman * @p: pointer to returned session id 237296fd7ce5SGreg Kroah-Hartman * 237396fd7ce5SGreg Kroah-Hartman * Obtain the session id of the tty. If there is no session 237496fd7ce5SGreg Kroah-Hartman * return an error. 237596fd7ce5SGreg Kroah-Hartman * 237696fd7ce5SGreg Kroah-Hartman * Locking: none. Reference to current->signal->tty is safe. 237796fd7ce5SGreg Kroah-Hartman */ 237896fd7ce5SGreg Kroah-Hartman 237996fd7ce5SGreg Kroah-Hartman static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 238096fd7ce5SGreg Kroah-Hartman { 238196fd7ce5SGreg Kroah-Hartman /* 238296fd7ce5SGreg Kroah-Hartman * (tty == real_tty) is a cheap way of 238396fd7ce5SGreg Kroah-Hartman * testing if the tty is NOT a master pty. 238496fd7ce5SGreg Kroah-Hartman */ 238596fd7ce5SGreg Kroah-Hartman if (tty == real_tty && current->signal->tty != real_tty) 238696fd7ce5SGreg Kroah-Hartman return -ENOTTY; 238796fd7ce5SGreg Kroah-Hartman if (!real_tty->session) 238896fd7ce5SGreg Kroah-Hartman return -ENOTTY; 238996fd7ce5SGreg Kroah-Hartman return put_user(pid_vnr(real_tty->session), p); 239096fd7ce5SGreg Kroah-Hartman } 239196fd7ce5SGreg Kroah-Hartman 239296fd7ce5SGreg Kroah-Hartman /** 239396fd7ce5SGreg Kroah-Hartman * tiocsetd - set line discipline 239496fd7ce5SGreg Kroah-Hartman * @tty: tty device 239596fd7ce5SGreg Kroah-Hartman * @p: pointer to user data 239696fd7ce5SGreg Kroah-Hartman * 239796fd7ce5SGreg Kroah-Hartman * Set the line discipline according to user request. 239896fd7ce5SGreg Kroah-Hartman * 239996fd7ce5SGreg Kroah-Hartman * Locking: see tty_set_ldisc, this function is just a helper 240096fd7ce5SGreg Kroah-Hartman */ 240196fd7ce5SGreg Kroah-Hartman 240296fd7ce5SGreg Kroah-Hartman static int tiocsetd(struct tty_struct *tty, int __user *p) 240396fd7ce5SGreg Kroah-Hartman { 240496fd7ce5SGreg Kroah-Hartman int ldisc; 240596fd7ce5SGreg Kroah-Hartman int ret; 240696fd7ce5SGreg Kroah-Hartman 240796fd7ce5SGreg Kroah-Hartman if (get_user(ldisc, p)) 240896fd7ce5SGreg Kroah-Hartman return -EFAULT; 240996fd7ce5SGreg Kroah-Hartman 241096fd7ce5SGreg Kroah-Hartman ret = tty_set_ldisc(tty, ldisc); 241196fd7ce5SGreg Kroah-Hartman 241296fd7ce5SGreg Kroah-Hartman return ret; 241396fd7ce5SGreg Kroah-Hartman } 241496fd7ce5SGreg Kroah-Hartman 241596fd7ce5SGreg Kroah-Hartman /** 241696fd7ce5SGreg Kroah-Hartman * send_break - performed time break 241796fd7ce5SGreg Kroah-Hartman * @tty: device to break on 241896fd7ce5SGreg Kroah-Hartman * @duration: timeout in mS 241996fd7ce5SGreg Kroah-Hartman * 242096fd7ce5SGreg Kroah-Hartman * Perform a timed break on hardware that lacks its own driver level 242196fd7ce5SGreg Kroah-Hartman * timed break functionality. 242296fd7ce5SGreg Kroah-Hartman * 242396fd7ce5SGreg Kroah-Hartman * Locking: 242496fd7ce5SGreg Kroah-Hartman * atomic_write_lock serializes 242596fd7ce5SGreg Kroah-Hartman * 242696fd7ce5SGreg Kroah-Hartman */ 242796fd7ce5SGreg Kroah-Hartman 242896fd7ce5SGreg Kroah-Hartman static int send_break(struct tty_struct *tty, unsigned int duration) 242996fd7ce5SGreg Kroah-Hartman { 243096fd7ce5SGreg Kroah-Hartman int retval; 243196fd7ce5SGreg Kroah-Hartman 243296fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl == NULL) 243396fd7ce5SGreg Kroah-Hartman return 0; 243496fd7ce5SGreg Kroah-Hartman 243596fd7ce5SGreg Kroah-Hartman if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) 243696fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, duration); 243796fd7ce5SGreg Kroah-Hartman else { 243896fd7ce5SGreg Kroah-Hartman /* Do the work ourselves */ 243996fd7ce5SGreg Kroah-Hartman if (tty_write_lock(tty, 0) < 0) 244096fd7ce5SGreg Kroah-Hartman return -EINTR; 244196fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, -1); 244296fd7ce5SGreg Kroah-Hartman if (retval) 244396fd7ce5SGreg Kroah-Hartman goto out; 244496fd7ce5SGreg Kroah-Hartman if (!signal_pending(current)) 244596fd7ce5SGreg Kroah-Hartman msleep_interruptible(duration); 244696fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, 0); 244796fd7ce5SGreg Kroah-Hartman out: 244896fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 244996fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 245096fd7ce5SGreg Kroah-Hartman retval = -EINTR; 245196fd7ce5SGreg Kroah-Hartman } 245296fd7ce5SGreg Kroah-Hartman return retval; 245396fd7ce5SGreg Kroah-Hartman } 245496fd7ce5SGreg Kroah-Hartman 245596fd7ce5SGreg Kroah-Hartman /** 245696fd7ce5SGreg Kroah-Hartman * tty_tiocmget - get modem status 245796fd7ce5SGreg Kroah-Hartman * @tty: tty device 245896fd7ce5SGreg Kroah-Hartman * @file: user file pointer 245996fd7ce5SGreg Kroah-Hartman * @p: pointer to result 246096fd7ce5SGreg Kroah-Hartman * 246196fd7ce5SGreg Kroah-Hartman * Obtain the modem status bits from the tty driver if the feature 246296fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 246396fd7ce5SGreg Kroah-Hartman * 246496fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 246596fd7ce5SGreg Kroah-Hartman */ 246696fd7ce5SGreg Kroah-Hartman 246760b33c13SAlan Cox static int tty_tiocmget(struct tty_struct *tty, int __user *p) 246896fd7ce5SGreg Kroah-Hartman { 246996fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 247096fd7ce5SGreg Kroah-Hartman 247196fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmget) { 247260b33c13SAlan Cox retval = tty->ops->tiocmget(tty); 247396fd7ce5SGreg Kroah-Hartman 247496fd7ce5SGreg Kroah-Hartman if (retval >= 0) 247596fd7ce5SGreg Kroah-Hartman retval = put_user(retval, p); 247696fd7ce5SGreg Kroah-Hartman } 247796fd7ce5SGreg Kroah-Hartman return retval; 247896fd7ce5SGreg Kroah-Hartman } 247996fd7ce5SGreg Kroah-Hartman 248096fd7ce5SGreg Kroah-Hartman /** 248196fd7ce5SGreg Kroah-Hartman * tty_tiocmset - set modem status 248296fd7ce5SGreg Kroah-Hartman * @tty: tty device 248396fd7ce5SGreg Kroah-Hartman * @cmd: command - clear bits, set bits or set all 248496fd7ce5SGreg Kroah-Hartman * @p: pointer to desired bits 248596fd7ce5SGreg Kroah-Hartman * 248696fd7ce5SGreg Kroah-Hartman * Set the modem status bits from the tty driver if the feature 248796fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 248896fd7ce5SGreg Kroah-Hartman * 248996fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 249096fd7ce5SGreg Kroah-Hartman */ 249196fd7ce5SGreg Kroah-Hartman 249220b9d177SAlan Cox static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, 249396fd7ce5SGreg Kroah-Hartman unsigned __user *p) 249496fd7ce5SGreg Kroah-Hartman { 249596fd7ce5SGreg Kroah-Hartman int retval; 249696fd7ce5SGreg Kroah-Hartman unsigned int set, clear, val; 249796fd7ce5SGreg Kroah-Hartman 249896fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmset == NULL) 249996fd7ce5SGreg Kroah-Hartman return -EINVAL; 250096fd7ce5SGreg Kroah-Hartman 250196fd7ce5SGreg Kroah-Hartman retval = get_user(val, p); 250296fd7ce5SGreg Kroah-Hartman if (retval) 250396fd7ce5SGreg Kroah-Hartman return retval; 250496fd7ce5SGreg Kroah-Hartman set = clear = 0; 250596fd7ce5SGreg Kroah-Hartman switch (cmd) { 250696fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 250796fd7ce5SGreg Kroah-Hartman set = val; 250896fd7ce5SGreg Kroah-Hartman break; 250996fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 251096fd7ce5SGreg Kroah-Hartman clear = val; 251196fd7ce5SGreg Kroah-Hartman break; 251296fd7ce5SGreg Kroah-Hartman case TIOCMSET: 251396fd7ce5SGreg Kroah-Hartman set = val; 251496fd7ce5SGreg Kroah-Hartman clear = ~val; 251596fd7ce5SGreg Kroah-Hartman break; 251696fd7ce5SGreg Kroah-Hartman } 251796fd7ce5SGreg Kroah-Hartman set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 251896fd7ce5SGreg Kroah-Hartman clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 251920b9d177SAlan Cox return tty->ops->tiocmset(tty, set, clear); 252096fd7ce5SGreg Kroah-Hartman } 252196fd7ce5SGreg Kroah-Hartman 252296fd7ce5SGreg Kroah-Hartman static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) 252396fd7ce5SGreg Kroah-Hartman { 252496fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 252596fd7ce5SGreg Kroah-Hartman struct serial_icounter_struct icount; 252696fd7ce5SGreg Kroah-Hartman memset(&icount, 0, sizeof(icount)); 252796fd7ce5SGreg Kroah-Hartman if (tty->ops->get_icount) 252896fd7ce5SGreg Kroah-Hartman retval = tty->ops->get_icount(tty, &icount); 252996fd7ce5SGreg Kroah-Hartman if (retval != 0) 253096fd7ce5SGreg Kroah-Hartman return retval; 253196fd7ce5SGreg Kroah-Hartman if (copy_to_user(arg, &icount, sizeof(icount))) 253296fd7ce5SGreg Kroah-Hartman return -EFAULT; 253396fd7ce5SGreg Kroah-Hartman return 0; 253496fd7ce5SGreg Kroah-Hartman } 253596fd7ce5SGreg Kroah-Hartman 253696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) 253796fd7ce5SGreg Kroah-Hartman { 253896fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 253996fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER) 254096fd7ce5SGreg Kroah-Hartman tty = tty->link; 254196fd7ce5SGreg Kroah-Hartman return tty; 254296fd7ce5SGreg Kroah-Hartman } 254396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_pair_get_tty); 254496fd7ce5SGreg Kroah-Hartman 254596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_pair_get_pty(struct tty_struct *tty) 254696fd7ce5SGreg Kroah-Hartman { 254796fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 254896fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER) 254996fd7ce5SGreg Kroah-Hartman return tty; 255096fd7ce5SGreg Kroah-Hartman return tty->link; 255196fd7ce5SGreg Kroah-Hartman } 255296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_pair_get_pty); 255396fd7ce5SGreg Kroah-Hartman 255496fd7ce5SGreg Kroah-Hartman /* 255596fd7ce5SGreg Kroah-Hartman * Split this up, as gcc can choke on it otherwise.. 255696fd7ce5SGreg Kroah-Hartman */ 255796fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 255896fd7ce5SGreg Kroah-Hartman { 255996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 256096fd7ce5SGreg Kroah-Hartman struct tty_struct *real_tty; 256196fd7ce5SGreg Kroah-Hartman void __user *p = (void __user *)arg; 256296fd7ce5SGreg Kroah-Hartman int retval; 256396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 256496fd7ce5SGreg Kroah-Hartman struct inode *inode = file->f_dentry->d_inode; 256596fd7ce5SGreg Kroah-Hartman 256696fd7ce5SGreg Kroah-Hartman if (tty_paranoia_check(tty, inode, "tty_ioctl")) 256796fd7ce5SGreg Kroah-Hartman return -EINVAL; 256896fd7ce5SGreg Kroah-Hartman 256996fd7ce5SGreg Kroah-Hartman real_tty = tty_pair_get_tty(tty); 257096fd7ce5SGreg Kroah-Hartman 257196fd7ce5SGreg Kroah-Hartman /* 257296fd7ce5SGreg Kroah-Hartman * Factor out some common prep work 257396fd7ce5SGreg Kroah-Hartman */ 257496fd7ce5SGreg Kroah-Hartman switch (cmd) { 257596fd7ce5SGreg Kroah-Hartman case TIOCSETD: 257696fd7ce5SGreg Kroah-Hartman case TIOCSBRK: 257796fd7ce5SGreg Kroah-Hartman case TIOCCBRK: 257896fd7ce5SGreg Kroah-Hartman case TCSBRK: 257996fd7ce5SGreg Kroah-Hartman case TCSBRKP: 258096fd7ce5SGreg Kroah-Hartman retval = tty_check_change(tty); 258196fd7ce5SGreg Kroah-Hartman if (retval) 258296fd7ce5SGreg Kroah-Hartman return retval; 258396fd7ce5SGreg Kroah-Hartman if (cmd != TIOCCBRK) { 258496fd7ce5SGreg Kroah-Hartman tty_wait_until_sent(tty, 0); 258596fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 258696fd7ce5SGreg Kroah-Hartman return -EINTR; 258796fd7ce5SGreg Kroah-Hartman } 258896fd7ce5SGreg Kroah-Hartman break; 258996fd7ce5SGreg Kroah-Hartman } 259096fd7ce5SGreg Kroah-Hartman 259196fd7ce5SGreg Kroah-Hartman /* 259296fd7ce5SGreg Kroah-Hartman * Now do the stuff. 259396fd7ce5SGreg Kroah-Hartman */ 259496fd7ce5SGreg Kroah-Hartman switch (cmd) { 259596fd7ce5SGreg Kroah-Hartman case TIOCSTI: 259696fd7ce5SGreg Kroah-Hartman return tiocsti(tty, p); 259796fd7ce5SGreg Kroah-Hartman case TIOCGWINSZ: 259896fd7ce5SGreg Kroah-Hartman return tiocgwinsz(real_tty, p); 259996fd7ce5SGreg Kroah-Hartman case TIOCSWINSZ: 260096fd7ce5SGreg Kroah-Hartman return tiocswinsz(real_tty, p); 260196fd7ce5SGreg Kroah-Hartman case TIOCCONS: 260296fd7ce5SGreg Kroah-Hartman return real_tty != tty ? -EINVAL : tioccons(file); 260396fd7ce5SGreg Kroah-Hartman case FIONBIO: 260496fd7ce5SGreg Kroah-Hartman return fionbio(file, p); 260596fd7ce5SGreg Kroah-Hartman case TIOCEXCL: 260696fd7ce5SGreg Kroah-Hartman set_bit(TTY_EXCLUSIVE, &tty->flags); 260796fd7ce5SGreg Kroah-Hartman return 0; 260896fd7ce5SGreg Kroah-Hartman case TIOCNXCL: 260996fd7ce5SGreg Kroah-Hartman clear_bit(TTY_EXCLUSIVE, &tty->flags); 261096fd7ce5SGreg Kroah-Hartman return 0; 261196fd7ce5SGreg Kroah-Hartman case TIOCNOTTY: 261296fd7ce5SGreg Kroah-Hartman if (current->signal->tty != tty) 261396fd7ce5SGreg Kroah-Hartman return -ENOTTY; 261496fd7ce5SGreg Kroah-Hartman no_tty(); 261596fd7ce5SGreg Kroah-Hartman return 0; 261696fd7ce5SGreg Kroah-Hartman case TIOCSCTTY: 261796fd7ce5SGreg Kroah-Hartman return tiocsctty(tty, arg); 261896fd7ce5SGreg Kroah-Hartman case TIOCGPGRP: 261996fd7ce5SGreg Kroah-Hartman return tiocgpgrp(tty, real_tty, p); 262096fd7ce5SGreg Kroah-Hartman case TIOCSPGRP: 262196fd7ce5SGreg Kroah-Hartman return tiocspgrp(tty, real_tty, p); 262296fd7ce5SGreg Kroah-Hartman case TIOCGSID: 262396fd7ce5SGreg Kroah-Hartman return tiocgsid(tty, real_tty, p); 262496fd7ce5SGreg Kroah-Hartman case TIOCGETD: 262596fd7ce5SGreg Kroah-Hartman return put_user(tty->ldisc->ops->num, (int __user *)p); 262696fd7ce5SGreg Kroah-Hartman case TIOCSETD: 262796fd7ce5SGreg Kroah-Hartman return tiocsetd(tty, p); 26283c95c985SKay Sievers case TIOCVHANGUP: 26293c95c985SKay Sievers if (!capable(CAP_SYS_ADMIN)) 26303c95c985SKay Sievers return -EPERM; 26313c95c985SKay Sievers tty_vhangup(tty); 26323c95c985SKay Sievers return 0; 2633b7b8de08SWerner Fink case TIOCGDEV: 2634b7b8de08SWerner Fink { 2635b7b8de08SWerner Fink unsigned int ret = new_encode_dev(tty_devnum(real_tty)); 2636b7b8de08SWerner Fink return put_user(ret, (unsigned int __user *)p); 2637b7b8de08SWerner Fink } 263896fd7ce5SGreg Kroah-Hartman /* 263996fd7ce5SGreg Kroah-Hartman * Break handling 264096fd7ce5SGreg Kroah-Hartman */ 264196fd7ce5SGreg Kroah-Hartman case TIOCSBRK: /* Turn break on, unconditionally */ 264296fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 264396fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, -1); 264496fd7ce5SGreg Kroah-Hartman return 0; 264596fd7ce5SGreg Kroah-Hartman case TIOCCBRK: /* Turn break off, unconditionally */ 264696fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 264796fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, 0); 264896fd7ce5SGreg Kroah-Hartman return 0; 264996fd7ce5SGreg Kroah-Hartman case TCSBRK: /* SVID version: non-zero arg --> no break */ 265096fd7ce5SGreg Kroah-Hartman /* non-zero arg means wait for all output data 265196fd7ce5SGreg Kroah-Hartman * to be sent (performed above) but don't send break. 265296fd7ce5SGreg Kroah-Hartman * This is used by the tcdrain() termios function. 265396fd7ce5SGreg Kroah-Hartman */ 265496fd7ce5SGreg Kroah-Hartman if (!arg) 265596fd7ce5SGreg Kroah-Hartman return send_break(tty, 250); 265696fd7ce5SGreg Kroah-Hartman return 0; 265796fd7ce5SGreg Kroah-Hartman case TCSBRKP: /* support for POSIX tcsendbreak() */ 265896fd7ce5SGreg Kroah-Hartman return send_break(tty, arg ? arg*100 : 250); 265996fd7ce5SGreg Kroah-Hartman 266096fd7ce5SGreg Kroah-Hartman case TIOCMGET: 266160b33c13SAlan Cox return tty_tiocmget(tty, p); 266296fd7ce5SGreg Kroah-Hartman case TIOCMSET: 266396fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 266496fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 266520b9d177SAlan Cox return tty_tiocmset(tty, cmd, p); 266696fd7ce5SGreg Kroah-Hartman case TIOCGICOUNT: 266796fd7ce5SGreg Kroah-Hartman retval = tty_tiocgicount(tty, p); 266896fd7ce5SGreg Kroah-Hartman /* For the moment allow fall through to the old method */ 266996fd7ce5SGreg Kroah-Hartman if (retval != -EINVAL) 267096fd7ce5SGreg Kroah-Hartman return retval; 267196fd7ce5SGreg Kroah-Hartman break; 267296fd7ce5SGreg Kroah-Hartman case TCFLSH: 267396fd7ce5SGreg Kroah-Hartman switch (arg) { 267496fd7ce5SGreg Kroah-Hartman case TCIFLUSH: 267596fd7ce5SGreg Kroah-Hartman case TCIOFLUSH: 267696fd7ce5SGreg Kroah-Hartman /* flush tty buffer and allow ldisc to process ioctl */ 267796fd7ce5SGreg Kroah-Hartman tty_buffer_flush(tty); 267896fd7ce5SGreg Kroah-Hartman break; 267996fd7ce5SGreg Kroah-Hartman } 268096fd7ce5SGreg Kroah-Hartman break; 268196fd7ce5SGreg Kroah-Hartman } 268296fd7ce5SGreg Kroah-Hartman if (tty->ops->ioctl) { 26836caa76b7SAlan Cox retval = (tty->ops->ioctl)(tty, cmd, arg); 268496fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 268596fd7ce5SGreg Kroah-Hartman return retval; 268696fd7ce5SGreg Kroah-Hartman } 268796fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 268896fd7ce5SGreg Kroah-Hartman retval = -EINVAL; 268996fd7ce5SGreg Kroah-Hartman if (ld->ops->ioctl) { 269096fd7ce5SGreg Kroah-Hartman retval = ld->ops->ioctl(tty, file, cmd, arg); 269196fd7ce5SGreg Kroah-Hartman if (retval == -ENOIOCTLCMD) 269296fd7ce5SGreg Kroah-Hartman retval = -EINVAL; 269396fd7ce5SGreg Kroah-Hartman } 269496fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 269596fd7ce5SGreg Kroah-Hartman return retval; 269696fd7ce5SGreg Kroah-Hartman } 269796fd7ce5SGreg Kroah-Hartman 269896fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 269996fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 270096fd7ce5SGreg Kroah-Hartman unsigned long arg) 270196fd7ce5SGreg Kroah-Hartman { 270296fd7ce5SGreg Kroah-Hartman struct inode *inode = file->f_dentry->d_inode; 270396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 270496fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 270596fd7ce5SGreg Kroah-Hartman int retval = -ENOIOCTLCMD; 270696fd7ce5SGreg Kroah-Hartman 270796fd7ce5SGreg Kroah-Hartman if (tty_paranoia_check(tty, inode, "tty_ioctl")) 270896fd7ce5SGreg Kroah-Hartman return -EINVAL; 270996fd7ce5SGreg Kroah-Hartman 271096fd7ce5SGreg Kroah-Hartman if (tty->ops->compat_ioctl) { 27116caa76b7SAlan Cox retval = (tty->ops->compat_ioctl)(tty, cmd, arg); 271296fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 271396fd7ce5SGreg Kroah-Hartman return retval; 271496fd7ce5SGreg Kroah-Hartman } 271596fd7ce5SGreg Kroah-Hartman 271696fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 271796fd7ce5SGreg Kroah-Hartman if (ld->ops->compat_ioctl) 271896fd7ce5SGreg Kroah-Hartman retval = ld->ops->compat_ioctl(tty, file, cmd, arg); 271996fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 272096fd7ce5SGreg Kroah-Hartman 272196fd7ce5SGreg Kroah-Hartman return retval; 272296fd7ce5SGreg Kroah-Hartman } 272396fd7ce5SGreg Kroah-Hartman #endif 272496fd7ce5SGreg Kroah-Hartman 272596fd7ce5SGreg Kroah-Hartman /* 272696fd7ce5SGreg Kroah-Hartman * This implements the "Secure Attention Key" --- the idea is to 272796fd7ce5SGreg Kroah-Hartman * prevent trojan horses by killing all processes associated with this 272896fd7ce5SGreg Kroah-Hartman * tty when the user hits the "Secure Attention Key". Required for 272996fd7ce5SGreg Kroah-Hartman * super-paranoid applications --- see the Orange Book for more details. 273096fd7ce5SGreg Kroah-Hartman * 273196fd7ce5SGreg Kroah-Hartman * This code could be nicer; ideally it should send a HUP, wait a few 273296fd7ce5SGreg Kroah-Hartman * seconds, then send a INT, and then a KILL signal. But you then 273396fd7ce5SGreg Kroah-Hartman * have to coordinate with the init process, since all processes associated 273496fd7ce5SGreg Kroah-Hartman * with the current tty must be dead before the new getty is allowed 273596fd7ce5SGreg Kroah-Hartman * to spawn. 273696fd7ce5SGreg Kroah-Hartman * 273796fd7ce5SGreg Kroah-Hartman * Now, if it would be correct ;-/ The current code has a nasty hole - 273896fd7ce5SGreg Kroah-Hartman * it doesn't catch files in flight. We may send the descriptor to ourselves 273996fd7ce5SGreg Kroah-Hartman * via AF_UNIX socket, close it and later fetch from socket. FIXME. 274096fd7ce5SGreg Kroah-Hartman * 274196fd7ce5SGreg Kroah-Hartman * Nasty bug: do_SAK is being called in interrupt context. This can 274296fd7ce5SGreg Kroah-Hartman * deadlock. We punt it up to process context. AKPM - 16Mar2001 274396fd7ce5SGreg Kroah-Hartman */ 274496fd7ce5SGreg Kroah-Hartman void __do_SAK(struct tty_struct *tty) 274596fd7ce5SGreg Kroah-Hartman { 274696fd7ce5SGreg Kroah-Hartman #ifdef TTY_SOFT_SAK 274796fd7ce5SGreg Kroah-Hartman tty_hangup(tty); 274896fd7ce5SGreg Kroah-Hartman #else 274996fd7ce5SGreg Kroah-Hartman struct task_struct *g, *p; 275096fd7ce5SGreg Kroah-Hartman struct pid *session; 275196fd7ce5SGreg Kroah-Hartman int i; 275296fd7ce5SGreg Kroah-Hartman struct file *filp; 275396fd7ce5SGreg Kroah-Hartman struct fdtable *fdt; 275496fd7ce5SGreg Kroah-Hartman 275596fd7ce5SGreg Kroah-Hartman if (!tty) 275696fd7ce5SGreg Kroah-Hartman return; 275796fd7ce5SGreg Kroah-Hartman session = tty->session; 275896fd7ce5SGreg Kroah-Hartman 275996fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 276096fd7ce5SGreg Kroah-Hartman 276196fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 276296fd7ce5SGreg Kroah-Hartman 276396fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 276496fd7ce5SGreg Kroah-Hartman /* Kill the entire session */ 276596fd7ce5SGreg Kroah-Hartman do_each_pid_task(session, PIDTYPE_SID, p) { 276696fd7ce5SGreg Kroah-Hartman printk(KERN_NOTICE "SAK: killed process %d" 276796fd7ce5SGreg Kroah-Hartman " (%s): task_session(p)==tty->session\n", 276896fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 276996fd7ce5SGreg Kroah-Hartman send_sig(SIGKILL, p, 1); 277096fd7ce5SGreg Kroah-Hartman } while_each_pid_task(session, PIDTYPE_SID, p); 277196fd7ce5SGreg Kroah-Hartman /* Now kill any processes that happen to have the 277296fd7ce5SGreg Kroah-Hartman * tty open. 277396fd7ce5SGreg Kroah-Hartman */ 277496fd7ce5SGreg Kroah-Hartman do_each_thread(g, p) { 277596fd7ce5SGreg Kroah-Hartman if (p->signal->tty == tty) { 277696fd7ce5SGreg Kroah-Hartman printk(KERN_NOTICE "SAK: killed process %d" 277796fd7ce5SGreg Kroah-Hartman " (%s): task_session(p)==tty->session\n", 277896fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 277996fd7ce5SGreg Kroah-Hartman send_sig(SIGKILL, p, 1); 278096fd7ce5SGreg Kroah-Hartman continue; 278196fd7ce5SGreg Kroah-Hartman } 278296fd7ce5SGreg Kroah-Hartman task_lock(p); 278396fd7ce5SGreg Kroah-Hartman if (p->files) { 278496fd7ce5SGreg Kroah-Hartman /* 278596fd7ce5SGreg Kroah-Hartman * We don't take a ref to the file, so we must 278696fd7ce5SGreg Kroah-Hartman * hold ->file_lock instead. 278796fd7ce5SGreg Kroah-Hartman */ 278896fd7ce5SGreg Kroah-Hartman spin_lock(&p->files->file_lock); 278996fd7ce5SGreg Kroah-Hartman fdt = files_fdtable(p->files); 279096fd7ce5SGreg Kroah-Hartman for (i = 0; i < fdt->max_fds; i++) { 279196fd7ce5SGreg Kroah-Hartman filp = fcheck_files(p->files, i); 279296fd7ce5SGreg Kroah-Hartman if (!filp) 279396fd7ce5SGreg Kroah-Hartman continue; 279496fd7ce5SGreg Kroah-Hartman if (filp->f_op->read == tty_read && 279596fd7ce5SGreg Kroah-Hartman file_tty(filp) == tty) { 279696fd7ce5SGreg Kroah-Hartman printk(KERN_NOTICE "SAK: killed process %d" 279796fd7ce5SGreg Kroah-Hartman " (%s): fd#%d opened to the tty\n", 279896fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm, i); 279996fd7ce5SGreg Kroah-Hartman force_sig(SIGKILL, p); 280096fd7ce5SGreg Kroah-Hartman break; 280196fd7ce5SGreg Kroah-Hartman } 280296fd7ce5SGreg Kroah-Hartman } 280396fd7ce5SGreg Kroah-Hartman spin_unlock(&p->files->file_lock); 280496fd7ce5SGreg Kroah-Hartman } 280596fd7ce5SGreg Kroah-Hartman task_unlock(p); 280696fd7ce5SGreg Kroah-Hartman } while_each_thread(g, p); 280796fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 280896fd7ce5SGreg Kroah-Hartman #endif 280996fd7ce5SGreg Kroah-Hartman } 281096fd7ce5SGreg Kroah-Hartman 281196fd7ce5SGreg Kroah-Hartman static void do_SAK_work(struct work_struct *work) 281296fd7ce5SGreg Kroah-Hartman { 281396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 281496fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, SAK_work); 281596fd7ce5SGreg Kroah-Hartman __do_SAK(tty); 281696fd7ce5SGreg Kroah-Hartman } 281796fd7ce5SGreg Kroah-Hartman 281896fd7ce5SGreg Kroah-Hartman /* 281996fd7ce5SGreg Kroah-Hartman * The tq handling here is a little racy - tty->SAK_work may already be queued. 282096fd7ce5SGreg Kroah-Hartman * Fortunately we don't need to worry, because if ->SAK_work is already queued, 282196fd7ce5SGreg Kroah-Hartman * the values which we write to it will be identical to the values which it 282296fd7ce5SGreg Kroah-Hartman * already has. --akpm 282396fd7ce5SGreg Kroah-Hartman */ 282496fd7ce5SGreg Kroah-Hartman void do_SAK(struct tty_struct *tty) 282596fd7ce5SGreg Kroah-Hartman { 282696fd7ce5SGreg Kroah-Hartman if (!tty) 282796fd7ce5SGreg Kroah-Hartman return; 282896fd7ce5SGreg Kroah-Hartman schedule_work(&tty->SAK_work); 282996fd7ce5SGreg Kroah-Hartman } 283096fd7ce5SGreg Kroah-Hartman 283196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(do_SAK); 283296fd7ce5SGreg Kroah-Hartman 283396fd7ce5SGreg Kroah-Hartman static int dev_match_devt(struct device *dev, void *data) 283496fd7ce5SGreg Kroah-Hartman { 283596fd7ce5SGreg Kroah-Hartman dev_t *devt = data; 283696fd7ce5SGreg Kroah-Hartman return dev->devt == *devt; 283796fd7ce5SGreg Kroah-Hartman } 283896fd7ce5SGreg Kroah-Hartman 283996fd7ce5SGreg Kroah-Hartman /* Must put_device() after it's unused! */ 284096fd7ce5SGreg Kroah-Hartman static struct device *tty_get_device(struct tty_struct *tty) 284196fd7ce5SGreg Kroah-Hartman { 284296fd7ce5SGreg Kroah-Hartman dev_t devt = tty_devnum(tty); 284396fd7ce5SGreg Kroah-Hartman return class_find_device(tty_class, NULL, &devt, dev_match_devt); 284496fd7ce5SGreg Kroah-Hartman } 284596fd7ce5SGreg Kroah-Hartman 284696fd7ce5SGreg Kroah-Hartman 284796fd7ce5SGreg Kroah-Hartman /** 284896fd7ce5SGreg Kroah-Hartman * initialize_tty_struct 284996fd7ce5SGreg Kroah-Hartman * @tty: tty to initialize 285096fd7ce5SGreg Kroah-Hartman * 285196fd7ce5SGreg Kroah-Hartman * This subroutine initializes a tty structure that has been newly 285296fd7ce5SGreg Kroah-Hartman * allocated. 285396fd7ce5SGreg Kroah-Hartman * 285496fd7ce5SGreg Kroah-Hartman * Locking: none - tty in question must not be exposed at this point 285596fd7ce5SGreg Kroah-Hartman */ 285696fd7ce5SGreg Kroah-Hartman 285796fd7ce5SGreg Kroah-Hartman void initialize_tty_struct(struct tty_struct *tty, 285896fd7ce5SGreg Kroah-Hartman struct tty_driver *driver, int idx) 285996fd7ce5SGreg Kroah-Hartman { 286096fd7ce5SGreg Kroah-Hartman memset(tty, 0, sizeof(struct tty_struct)); 286196fd7ce5SGreg Kroah-Hartman kref_init(&tty->kref); 286296fd7ce5SGreg Kroah-Hartman tty->magic = TTY_MAGIC; 286396fd7ce5SGreg Kroah-Hartman tty_ldisc_init(tty); 286496fd7ce5SGreg Kroah-Hartman tty->session = NULL; 286596fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 286696fd7ce5SGreg Kroah-Hartman tty->overrun_time = jiffies; 286796fd7ce5SGreg Kroah-Hartman tty->buf.head = tty->buf.tail = NULL; 286896fd7ce5SGreg Kroah-Hartman tty_buffer_init(tty); 286996fd7ce5SGreg Kroah-Hartman mutex_init(&tty->termios_mutex); 287096fd7ce5SGreg Kroah-Hartman mutex_init(&tty->ldisc_mutex); 287196fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->write_wait); 287296fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->read_wait); 287396fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, do_tty_hangup); 287496fd7ce5SGreg Kroah-Hartman mutex_init(&tty->atomic_read_lock); 287596fd7ce5SGreg Kroah-Hartman mutex_init(&tty->atomic_write_lock); 287696fd7ce5SGreg Kroah-Hartman mutex_init(&tty->output_lock); 287796fd7ce5SGreg Kroah-Hartman mutex_init(&tty->echo_lock); 287896fd7ce5SGreg Kroah-Hartman spin_lock_init(&tty->read_lock); 287996fd7ce5SGreg Kroah-Hartman spin_lock_init(&tty->ctrl_lock); 288096fd7ce5SGreg Kroah-Hartman INIT_LIST_HEAD(&tty->tty_files); 288196fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->SAK_work, do_SAK_work); 288296fd7ce5SGreg Kroah-Hartman 288396fd7ce5SGreg Kroah-Hartman tty->driver = driver; 288496fd7ce5SGreg Kroah-Hartman tty->ops = driver->ops; 288596fd7ce5SGreg Kroah-Hartman tty->index = idx; 288696fd7ce5SGreg Kroah-Hartman tty_line_name(driver, idx, tty->name); 288796fd7ce5SGreg Kroah-Hartman tty->dev = tty_get_device(tty); 288896fd7ce5SGreg Kroah-Hartman } 288996fd7ce5SGreg Kroah-Hartman 289096fd7ce5SGreg Kroah-Hartman /** 28916716671dSJiri Slaby * deinitialize_tty_struct 28926716671dSJiri Slaby * @tty: tty to deinitialize 28936716671dSJiri Slaby * 28946716671dSJiri Slaby * This subroutine deinitializes a tty structure that has been newly 28956716671dSJiri Slaby * allocated but tty_release cannot be called on that yet. 28966716671dSJiri Slaby * 28976716671dSJiri Slaby * Locking: none - tty in question must not be exposed at this point 28986716671dSJiri Slaby */ 28996716671dSJiri Slaby void deinitialize_tty_struct(struct tty_struct *tty) 29006716671dSJiri Slaby { 29016716671dSJiri Slaby tty_ldisc_deinit(tty); 29026716671dSJiri Slaby } 29036716671dSJiri Slaby 29046716671dSJiri Slaby /** 290596fd7ce5SGreg Kroah-Hartman * tty_put_char - write one character to a tty 290696fd7ce5SGreg Kroah-Hartman * @tty: tty 290796fd7ce5SGreg Kroah-Hartman * @ch: character 290896fd7ce5SGreg Kroah-Hartman * 290996fd7ce5SGreg Kroah-Hartman * Write one byte to the tty using the provided put_char method 291096fd7ce5SGreg Kroah-Hartman * if present. Returns the number of characters successfully output. 291196fd7ce5SGreg Kroah-Hartman * 291296fd7ce5SGreg Kroah-Hartman * Note: the specific put_char operation in the driver layer may go 291396fd7ce5SGreg Kroah-Hartman * away soon. Don't call it directly, use this method 291496fd7ce5SGreg Kroah-Hartman */ 291596fd7ce5SGreg Kroah-Hartman 291696fd7ce5SGreg Kroah-Hartman int tty_put_char(struct tty_struct *tty, unsigned char ch) 291796fd7ce5SGreg Kroah-Hartman { 291896fd7ce5SGreg Kroah-Hartman if (tty->ops->put_char) 291996fd7ce5SGreg Kroah-Hartman return tty->ops->put_char(tty, ch); 292096fd7ce5SGreg Kroah-Hartman return tty->ops->write(tty, &ch, 1); 292196fd7ce5SGreg Kroah-Hartman } 292296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_put_char); 292396fd7ce5SGreg Kroah-Hartman 292496fd7ce5SGreg Kroah-Hartman struct class *tty_class; 292596fd7ce5SGreg Kroah-Hartman 292696fd7ce5SGreg Kroah-Hartman /** 292796fd7ce5SGreg Kroah-Hartman * tty_register_device - register a tty device 292896fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 292996fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 293096fd7ce5SGreg Kroah-Hartman * @device: a struct device that is associated with this tty device. 293196fd7ce5SGreg Kroah-Hartman * This field is optional, if there is no known struct device 293296fd7ce5SGreg Kroah-Hartman * for this tty device it can be set to NULL safely. 293396fd7ce5SGreg Kroah-Hartman * 293496fd7ce5SGreg Kroah-Hartman * Returns a pointer to the struct device for this tty device 293596fd7ce5SGreg Kroah-Hartman * (or ERR_PTR(-EFOO) on error). 293696fd7ce5SGreg Kroah-Hartman * 293796fd7ce5SGreg Kroah-Hartman * This call is required to be made to register an individual tty device 293896fd7ce5SGreg Kroah-Hartman * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 293996fd7ce5SGreg Kroah-Hartman * that bit is not set, this function should not be called by a tty 294096fd7ce5SGreg Kroah-Hartman * driver. 294196fd7ce5SGreg Kroah-Hartman * 294296fd7ce5SGreg Kroah-Hartman * Locking: ?? 294396fd7ce5SGreg Kroah-Hartman */ 294496fd7ce5SGreg Kroah-Hartman 294596fd7ce5SGreg Kroah-Hartman struct device *tty_register_device(struct tty_driver *driver, unsigned index, 294696fd7ce5SGreg Kroah-Hartman struct device *device) 294796fd7ce5SGreg Kroah-Hartman { 294896fd7ce5SGreg Kroah-Hartman char name[64]; 294996fd7ce5SGreg Kroah-Hartman dev_t dev = MKDEV(driver->major, driver->minor_start) + index; 295096fd7ce5SGreg Kroah-Hartman 295196fd7ce5SGreg Kroah-Hartman if (index >= driver->num) { 295296fd7ce5SGreg Kroah-Hartman printk(KERN_ERR "Attempt to register invalid tty line number " 295396fd7ce5SGreg Kroah-Hartman " (%d).\n", index); 295496fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EINVAL); 295596fd7ce5SGreg Kroah-Hartman } 295696fd7ce5SGreg Kroah-Hartman 295796fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY) 295896fd7ce5SGreg Kroah-Hartman pty_line_name(driver, index, name); 295996fd7ce5SGreg Kroah-Hartman else 296096fd7ce5SGreg Kroah-Hartman tty_line_name(driver, index, name); 296196fd7ce5SGreg Kroah-Hartman 296296fd7ce5SGreg Kroah-Hartman return device_create(tty_class, device, dev, NULL, name); 296396fd7ce5SGreg Kroah-Hartman } 296496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_device); 296596fd7ce5SGreg Kroah-Hartman 296696fd7ce5SGreg Kroah-Hartman /** 296796fd7ce5SGreg Kroah-Hartman * tty_unregister_device - unregister a tty device 296896fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 296996fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 297096fd7ce5SGreg Kroah-Hartman * 297196fd7ce5SGreg Kroah-Hartman * If a tty device is registered with a call to tty_register_device() then 297296fd7ce5SGreg Kroah-Hartman * this function must be called when the tty device is gone. 297396fd7ce5SGreg Kroah-Hartman * 297496fd7ce5SGreg Kroah-Hartman * Locking: ?? 297596fd7ce5SGreg Kroah-Hartman */ 297696fd7ce5SGreg Kroah-Hartman 297796fd7ce5SGreg Kroah-Hartman void tty_unregister_device(struct tty_driver *driver, unsigned index) 297896fd7ce5SGreg Kroah-Hartman { 297996fd7ce5SGreg Kroah-Hartman device_destroy(tty_class, 298096fd7ce5SGreg Kroah-Hartman MKDEV(driver->major, driver->minor_start) + index); 298196fd7ce5SGreg Kroah-Hartman } 298296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_device); 298396fd7ce5SGreg Kroah-Hartman 298496fd7ce5SGreg Kroah-Hartman struct tty_driver *alloc_tty_driver(int lines) 298596fd7ce5SGreg Kroah-Hartman { 298696fd7ce5SGreg Kroah-Hartman struct tty_driver *driver; 298796fd7ce5SGreg Kroah-Hartman 298896fd7ce5SGreg Kroah-Hartman driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); 298996fd7ce5SGreg Kroah-Hartman if (driver) { 299096fd7ce5SGreg Kroah-Hartman kref_init(&driver->kref); 299196fd7ce5SGreg Kroah-Hartman driver->magic = TTY_DRIVER_MAGIC; 299296fd7ce5SGreg Kroah-Hartman driver->num = lines; 299396fd7ce5SGreg Kroah-Hartman /* later we'll move allocation of tables here */ 299496fd7ce5SGreg Kroah-Hartman } 299596fd7ce5SGreg Kroah-Hartman return driver; 299696fd7ce5SGreg Kroah-Hartman } 299796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(alloc_tty_driver); 299896fd7ce5SGreg Kroah-Hartman 299996fd7ce5SGreg Kroah-Hartman static void destruct_tty_driver(struct kref *kref) 300096fd7ce5SGreg Kroah-Hartman { 300196fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = container_of(kref, struct tty_driver, kref); 300296fd7ce5SGreg Kroah-Hartman int i; 300396fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 300496fd7ce5SGreg Kroah-Hartman void *p; 300596fd7ce5SGreg Kroah-Hartman 300696fd7ce5SGreg Kroah-Hartman if (driver->flags & TTY_DRIVER_INSTALLED) { 300796fd7ce5SGreg Kroah-Hartman /* 300896fd7ce5SGreg Kroah-Hartman * Free the termios and termios_locked structures because 300996fd7ce5SGreg Kroah-Hartman * we don't want to get memory leaks when modular tty 301096fd7ce5SGreg Kroah-Hartman * drivers are removed from the kernel. 301196fd7ce5SGreg Kroah-Hartman */ 301296fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 301396fd7ce5SGreg Kroah-Hartman tp = driver->termios[i]; 301496fd7ce5SGreg Kroah-Hartman if (tp) { 301596fd7ce5SGreg Kroah-Hartman driver->termios[i] = NULL; 301696fd7ce5SGreg Kroah-Hartman kfree(tp); 301796fd7ce5SGreg Kroah-Hartman } 301896fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) 301996fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 302096fd7ce5SGreg Kroah-Hartman } 302196fd7ce5SGreg Kroah-Hartman p = driver->ttys; 302296fd7ce5SGreg Kroah-Hartman proc_tty_unregister_driver(driver); 302396fd7ce5SGreg Kroah-Hartman driver->ttys = NULL; 302496fd7ce5SGreg Kroah-Hartman driver->termios = NULL; 302596fd7ce5SGreg Kroah-Hartman kfree(p); 302696fd7ce5SGreg Kroah-Hartman cdev_del(&driver->cdev); 302796fd7ce5SGreg Kroah-Hartman } 302896fd7ce5SGreg Kroah-Hartman kfree(driver); 302996fd7ce5SGreg Kroah-Hartman } 303096fd7ce5SGreg Kroah-Hartman 303196fd7ce5SGreg Kroah-Hartman void tty_driver_kref_put(struct tty_driver *driver) 303296fd7ce5SGreg Kroah-Hartman { 303396fd7ce5SGreg Kroah-Hartman kref_put(&driver->kref, destruct_tty_driver); 303496fd7ce5SGreg Kroah-Hartman } 303596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_driver_kref_put); 303696fd7ce5SGreg Kroah-Hartman 303796fd7ce5SGreg Kroah-Hartman void tty_set_operations(struct tty_driver *driver, 303896fd7ce5SGreg Kroah-Hartman const struct tty_operations *op) 303996fd7ce5SGreg Kroah-Hartman { 304096fd7ce5SGreg Kroah-Hartman driver->ops = op; 304196fd7ce5SGreg Kroah-Hartman }; 304296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_set_operations); 304396fd7ce5SGreg Kroah-Hartman 304496fd7ce5SGreg Kroah-Hartman void put_tty_driver(struct tty_driver *d) 304596fd7ce5SGreg Kroah-Hartman { 304696fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(d); 304796fd7ce5SGreg Kroah-Hartman } 304896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(put_tty_driver); 304996fd7ce5SGreg Kroah-Hartman 305096fd7ce5SGreg Kroah-Hartman /* 305196fd7ce5SGreg Kroah-Hartman * Called by a tty driver to register itself. 305296fd7ce5SGreg Kroah-Hartman */ 305396fd7ce5SGreg Kroah-Hartman int tty_register_driver(struct tty_driver *driver) 305496fd7ce5SGreg Kroah-Hartman { 305596fd7ce5SGreg Kroah-Hartman int error; 305696fd7ce5SGreg Kroah-Hartman int i; 305796fd7ce5SGreg Kroah-Hartman dev_t dev; 305896fd7ce5SGreg Kroah-Hartman void **p = NULL; 305996fd7ce5SGreg Kroah-Hartman struct device *d; 306096fd7ce5SGreg Kroah-Hartman 306196fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) { 306296fd7ce5SGreg Kroah-Hartman p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL); 306396fd7ce5SGreg Kroah-Hartman if (!p) 306496fd7ce5SGreg Kroah-Hartman return -ENOMEM; 306596fd7ce5SGreg Kroah-Hartman } 306696fd7ce5SGreg Kroah-Hartman 306796fd7ce5SGreg Kroah-Hartman if (!driver->major) { 306896fd7ce5SGreg Kroah-Hartman error = alloc_chrdev_region(&dev, driver->minor_start, 306996fd7ce5SGreg Kroah-Hartman driver->num, driver->name); 307096fd7ce5SGreg Kroah-Hartman if (!error) { 307196fd7ce5SGreg Kroah-Hartman driver->major = MAJOR(dev); 307296fd7ce5SGreg Kroah-Hartman driver->minor_start = MINOR(dev); 307396fd7ce5SGreg Kroah-Hartman } 307496fd7ce5SGreg Kroah-Hartman } else { 307596fd7ce5SGreg Kroah-Hartman dev = MKDEV(driver->major, driver->minor_start); 307696fd7ce5SGreg Kroah-Hartman error = register_chrdev_region(dev, driver->num, driver->name); 307796fd7ce5SGreg Kroah-Hartman } 307896fd7ce5SGreg Kroah-Hartman if (error < 0) { 307996fd7ce5SGreg Kroah-Hartman kfree(p); 308096fd7ce5SGreg Kroah-Hartman return error; 308196fd7ce5SGreg Kroah-Hartman } 308296fd7ce5SGreg Kroah-Hartman 308396fd7ce5SGreg Kroah-Hartman if (p) { 308496fd7ce5SGreg Kroah-Hartman driver->ttys = (struct tty_struct **)p; 308596fd7ce5SGreg Kroah-Hartman driver->termios = (struct ktermios **)(p + driver->num); 308696fd7ce5SGreg Kroah-Hartman } else { 308796fd7ce5SGreg Kroah-Hartman driver->ttys = NULL; 308896fd7ce5SGreg Kroah-Hartman driver->termios = NULL; 308996fd7ce5SGreg Kroah-Hartman } 309096fd7ce5SGreg Kroah-Hartman 309196fd7ce5SGreg Kroah-Hartman cdev_init(&driver->cdev, &tty_fops); 309296fd7ce5SGreg Kroah-Hartman driver->cdev.owner = driver->owner; 309396fd7ce5SGreg Kroah-Hartman error = cdev_add(&driver->cdev, dev, driver->num); 309496fd7ce5SGreg Kroah-Hartman if (error) { 309596fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(dev, driver->num); 309696fd7ce5SGreg Kroah-Hartman driver->ttys = NULL; 309796fd7ce5SGreg Kroah-Hartman driver->termios = NULL; 309896fd7ce5SGreg Kroah-Hartman kfree(p); 309996fd7ce5SGreg Kroah-Hartman return error; 310096fd7ce5SGreg Kroah-Hartman } 310196fd7ce5SGreg Kroah-Hartman 310296fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 310396fd7ce5SGreg Kroah-Hartman list_add(&driver->tty_drivers, &tty_drivers); 310496fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 310596fd7ce5SGreg Kroah-Hartman 310696fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { 310796fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 310896fd7ce5SGreg Kroah-Hartman d = tty_register_device(driver, i, NULL); 310996fd7ce5SGreg Kroah-Hartman if (IS_ERR(d)) { 311096fd7ce5SGreg Kroah-Hartman error = PTR_ERR(d); 311196fd7ce5SGreg Kroah-Hartman goto err; 311296fd7ce5SGreg Kroah-Hartman } 311396fd7ce5SGreg Kroah-Hartman } 311496fd7ce5SGreg Kroah-Hartman } 311596fd7ce5SGreg Kroah-Hartman proc_tty_register_driver(driver); 311696fd7ce5SGreg Kroah-Hartman driver->flags |= TTY_DRIVER_INSTALLED; 311796fd7ce5SGreg Kroah-Hartman return 0; 311896fd7ce5SGreg Kroah-Hartman 311996fd7ce5SGreg Kroah-Hartman err: 312096fd7ce5SGreg Kroah-Hartman for (i--; i >= 0; i--) 312196fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 312296fd7ce5SGreg Kroah-Hartman 312396fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 312496fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 312596fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 312696fd7ce5SGreg Kroah-Hartman 312796fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(dev, driver->num); 312896fd7ce5SGreg Kroah-Hartman driver->ttys = NULL; 312996fd7ce5SGreg Kroah-Hartman driver->termios = NULL; 313096fd7ce5SGreg Kroah-Hartman kfree(p); 313196fd7ce5SGreg Kroah-Hartman return error; 313296fd7ce5SGreg Kroah-Hartman } 313396fd7ce5SGreg Kroah-Hartman 313496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_driver); 313596fd7ce5SGreg Kroah-Hartman 313696fd7ce5SGreg Kroah-Hartman /* 313796fd7ce5SGreg Kroah-Hartman * Called by a tty driver to unregister itself. 313896fd7ce5SGreg Kroah-Hartman */ 313996fd7ce5SGreg Kroah-Hartman int tty_unregister_driver(struct tty_driver *driver) 314096fd7ce5SGreg Kroah-Hartman { 314196fd7ce5SGreg Kroah-Hartman #if 0 314296fd7ce5SGreg Kroah-Hartman /* FIXME */ 314396fd7ce5SGreg Kroah-Hartman if (driver->refcount) 314496fd7ce5SGreg Kroah-Hartman return -EBUSY; 314596fd7ce5SGreg Kroah-Hartman #endif 314696fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), 314796fd7ce5SGreg Kroah-Hartman driver->num); 314896fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 314996fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 315096fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 315196fd7ce5SGreg Kroah-Hartman return 0; 315296fd7ce5SGreg Kroah-Hartman } 315396fd7ce5SGreg Kroah-Hartman 315496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_driver); 315596fd7ce5SGreg Kroah-Hartman 315696fd7ce5SGreg Kroah-Hartman dev_t tty_devnum(struct tty_struct *tty) 315796fd7ce5SGreg Kroah-Hartman { 315896fd7ce5SGreg Kroah-Hartman return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; 315996fd7ce5SGreg Kroah-Hartman } 316096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_devnum); 316196fd7ce5SGreg Kroah-Hartman 316296fd7ce5SGreg Kroah-Hartman void proc_clear_tty(struct task_struct *p) 316396fd7ce5SGreg Kroah-Hartman { 316496fd7ce5SGreg Kroah-Hartman unsigned long flags; 316596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 316696fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&p->sighand->siglock, flags); 316796fd7ce5SGreg Kroah-Hartman tty = p->signal->tty; 316896fd7ce5SGreg Kroah-Hartman p->signal->tty = NULL; 316996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&p->sighand->siglock, flags); 317096fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 317196fd7ce5SGreg Kroah-Hartman } 317296fd7ce5SGreg Kroah-Hartman 317396fd7ce5SGreg Kroah-Hartman /* Called under the sighand lock */ 317496fd7ce5SGreg Kroah-Hartman 317596fd7ce5SGreg Kroah-Hartman static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty) 317696fd7ce5SGreg Kroah-Hartman { 317796fd7ce5SGreg Kroah-Hartman if (tty) { 317896fd7ce5SGreg Kroah-Hartman unsigned long flags; 317996fd7ce5SGreg Kroah-Hartman /* We should not have a session or pgrp to put here but.... */ 318096fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 318196fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 318296fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 318396fd7ce5SGreg Kroah-Hartman tty->pgrp = get_pid(task_pgrp(tsk)); 318496fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 318596fd7ce5SGreg Kroah-Hartman tty->session = get_pid(task_session(tsk)); 318696fd7ce5SGreg Kroah-Hartman if (tsk->signal->tty) { 318796fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "tty not NULL!!\n"); 318896fd7ce5SGreg Kroah-Hartman tty_kref_put(tsk->signal->tty); 318996fd7ce5SGreg Kroah-Hartman } 319096fd7ce5SGreg Kroah-Hartman } 319196fd7ce5SGreg Kroah-Hartman put_pid(tsk->signal->tty_old_pgrp); 319296fd7ce5SGreg Kroah-Hartman tsk->signal->tty = tty_kref_get(tty); 319396fd7ce5SGreg Kroah-Hartman tsk->signal->tty_old_pgrp = NULL; 319496fd7ce5SGreg Kroah-Hartman } 319596fd7ce5SGreg Kroah-Hartman 319696fd7ce5SGreg Kroah-Hartman static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty) 319796fd7ce5SGreg Kroah-Hartman { 319896fd7ce5SGreg Kroah-Hartman spin_lock_irq(&tsk->sighand->siglock); 319996fd7ce5SGreg Kroah-Hartman __proc_set_tty(tsk, tty); 320096fd7ce5SGreg Kroah-Hartman spin_unlock_irq(&tsk->sighand->siglock); 320196fd7ce5SGreg Kroah-Hartman } 320296fd7ce5SGreg Kroah-Hartman 320396fd7ce5SGreg Kroah-Hartman struct tty_struct *get_current_tty(void) 320496fd7ce5SGreg Kroah-Hartman { 320596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 320696fd7ce5SGreg Kroah-Hartman unsigned long flags; 320796fd7ce5SGreg Kroah-Hartman 320896fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(¤t->sighand->siglock, flags); 320996fd7ce5SGreg Kroah-Hartman tty = tty_kref_get(current->signal->tty); 321096fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(¤t->sighand->siglock, flags); 321196fd7ce5SGreg Kroah-Hartman return tty; 321296fd7ce5SGreg Kroah-Hartman } 321396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(get_current_tty); 321496fd7ce5SGreg Kroah-Hartman 321596fd7ce5SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops) 321696fd7ce5SGreg Kroah-Hartman { 321796fd7ce5SGreg Kroah-Hartman *fops = tty_fops; 321896fd7ce5SGreg Kroah-Hartman } 321996fd7ce5SGreg Kroah-Hartman 322096fd7ce5SGreg Kroah-Hartman /* 322196fd7ce5SGreg Kroah-Hartman * Initialize the console device. This is called *early*, so 322296fd7ce5SGreg Kroah-Hartman * we can't necessarily depend on lots of kernel help here. 322396fd7ce5SGreg Kroah-Hartman * Just do some early initializations, and do the complex setup 322496fd7ce5SGreg Kroah-Hartman * later. 322596fd7ce5SGreg Kroah-Hartman */ 322696fd7ce5SGreg Kroah-Hartman void __init console_init(void) 322796fd7ce5SGreg Kroah-Hartman { 322896fd7ce5SGreg Kroah-Hartman initcall_t *call; 322996fd7ce5SGreg Kroah-Hartman 323096fd7ce5SGreg Kroah-Hartman /* Setup the default TTY line discipline. */ 323196fd7ce5SGreg Kroah-Hartman tty_ldisc_begin(); 323296fd7ce5SGreg Kroah-Hartman 323396fd7ce5SGreg Kroah-Hartman /* 323496fd7ce5SGreg Kroah-Hartman * set up the console device so that later boot sequences can 323596fd7ce5SGreg Kroah-Hartman * inform about problems etc.. 323696fd7ce5SGreg Kroah-Hartman */ 323796fd7ce5SGreg Kroah-Hartman call = __con_initcall_start; 323896fd7ce5SGreg Kroah-Hartman while (call < __con_initcall_end) { 323996fd7ce5SGreg Kroah-Hartman (*call)(); 324096fd7ce5SGreg Kroah-Hartman call++; 324196fd7ce5SGreg Kroah-Hartman } 324296fd7ce5SGreg Kroah-Hartman } 324396fd7ce5SGreg Kroah-Hartman 324496fd7ce5SGreg Kroah-Hartman static char *tty_devnode(struct device *dev, mode_t *mode) 324596fd7ce5SGreg Kroah-Hartman { 324696fd7ce5SGreg Kroah-Hartman if (!mode) 324796fd7ce5SGreg Kroah-Hartman return NULL; 324896fd7ce5SGreg Kroah-Hartman if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || 324996fd7ce5SGreg Kroah-Hartman dev->devt == MKDEV(TTYAUX_MAJOR, 2)) 325096fd7ce5SGreg Kroah-Hartman *mode = 0666; 325196fd7ce5SGreg Kroah-Hartman return NULL; 325296fd7ce5SGreg Kroah-Hartman } 325396fd7ce5SGreg Kroah-Hartman 325496fd7ce5SGreg Kroah-Hartman static int __init tty_class_init(void) 325596fd7ce5SGreg Kroah-Hartman { 325696fd7ce5SGreg Kroah-Hartman tty_class = class_create(THIS_MODULE, "tty"); 325796fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty_class)) 325896fd7ce5SGreg Kroah-Hartman return PTR_ERR(tty_class); 325996fd7ce5SGreg Kroah-Hartman tty_class->devnode = tty_devnode; 326096fd7ce5SGreg Kroah-Hartman return 0; 326196fd7ce5SGreg Kroah-Hartman } 326296fd7ce5SGreg Kroah-Hartman 326396fd7ce5SGreg Kroah-Hartman postcore_initcall(tty_class_init); 326496fd7ce5SGreg Kroah-Hartman 326596fd7ce5SGreg Kroah-Hartman /* 3/2004 jmc: why do these devices exist? */ 326696fd7ce5SGreg Kroah-Hartman static struct cdev tty_cdev, console_cdev; 326796fd7ce5SGreg Kroah-Hartman 3268fbc92a34SKay Sievers static ssize_t show_cons_active(struct device *dev, 3269fbc92a34SKay Sievers struct device_attribute *attr, char *buf) 3270fbc92a34SKay Sievers { 3271fbc92a34SKay Sievers struct console *cs[16]; 3272fbc92a34SKay Sievers int i = 0; 3273fbc92a34SKay Sievers struct console *c; 3274fbc92a34SKay Sievers ssize_t count = 0; 3275fbc92a34SKay Sievers 3276ac751efaSTorben Hohn console_lock(); 3277a2a6a822SKay Sievers for_each_console(c) { 3278fbc92a34SKay Sievers if (!c->device) 3279fbc92a34SKay Sievers continue; 3280fbc92a34SKay Sievers if (!c->write) 3281fbc92a34SKay Sievers continue; 3282fbc92a34SKay Sievers if ((c->flags & CON_ENABLED) == 0) 3283fbc92a34SKay Sievers continue; 3284fbc92a34SKay Sievers cs[i++] = c; 3285fbc92a34SKay Sievers if (i >= ARRAY_SIZE(cs)) 3286fbc92a34SKay Sievers break; 3287fbc92a34SKay Sievers } 3288fbc92a34SKay Sievers while (i--) 3289fbc92a34SKay Sievers count += sprintf(buf + count, "%s%d%c", 3290fbc92a34SKay Sievers cs[i]->name, cs[i]->index, i ? ' ':'\n'); 3291ac751efaSTorben Hohn console_unlock(); 3292fbc92a34SKay Sievers 3293fbc92a34SKay Sievers return count; 3294fbc92a34SKay Sievers } 3295fbc92a34SKay Sievers static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL); 3296fbc92a34SKay Sievers 3297fbc92a34SKay Sievers static struct device *consdev; 3298fbc92a34SKay Sievers 3299fbc92a34SKay Sievers void console_sysfs_notify(void) 3300fbc92a34SKay Sievers { 3301fbc92a34SKay Sievers if (consdev) 3302fbc92a34SKay Sievers sysfs_notify(&consdev->kobj, NULL, "active"); 3303fbc92a34SKay Sievers } 3304fbc92a34SKay Sievers 330596fd7ce5SGreg Kroah-Hartman /* 330696fd7ce5SGreg Kroah-Hartman * Ok, now we can initialize the rest of the tty devices and can count 330796fd7ce5SGreg Kroah-Hartman * on memory allocations, interrupts etc.. 330896fd7ce5SGreg Kroah-Hartman */ 330996fd7ce5SGreg Kroah-Hartman int __init tty_init(void) 331096fd7ce5SGreg Kroah-Hartman { 331196fd7ce5SGreg Kroah-Hartman cdev_init(&tty_cdev, &tty_fops); 331296fd7ce5SGreg Kroah-Hartman if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || 331396fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) 331496fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/tty driver\n"); 3315fbc92a34SKay Sievers device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); 331696fd7ce5SGreg Kroah-Hartman 331796fd7ce5SGreg Kroah-Hartman cdev_init(&console_cdev, &console_fops); 331896fd7ce5SGreg Kroah-Hartman if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || 331996fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) 332096fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/console driver\n"); 3321fbc92a34SKay Sievers consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, 332296fd7ce5SGreg Kroah-Hartman "console"); 3323fbc92a34SKay Sievers if (IS_ERR(consdev)) 3324fbc92a34SKay Sievers consdev = NULL; 3325fbc92a34SKay Sievers else 3326a2a6a822SKay Sievers WARN_ON(device_create_file(consdev, &dev_attr_active) < 0); 332796fd7ce5SGreg Kroah-Hartman 332896fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT 332996fd7ce5SGreg Kroah-Hartman vty_init(&console_fops); 333096fd7ce5SGreg Kroah-Hartman #endif 333196fd7ce5SGreg Kroah-Hartman return 0; 333296fd7ce5SGreg Kroah-Hartman } 333396fd7ce5SGreg Kroah-Hartman 3334