196fd7ce5SGreg Kroah-Hartman /* 296fd7ce5SGreg Kroah-Hartman * Copyright (C) 1991, 1992 Linus Torvalds 396fd7ce5SGreg Kroah-Hartman */ 496fd7ce5SGreg Kroah-Hartman 596fd7ce5SGreg Kroah-Hartman /* 696fd7ce5SGreg Kroah-Hartman * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles 796fd7ce5SGreg Kroah-Hartman * or rs-channels. It also implements echoing, cooked mode etc. 896fd7ce5SGreg Kroah-Hartman * 996fd7ce5SGreg Kroah-Hartman * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0. 1096fd7ce5SGreg Kroah-Hartman * 1196fd7ce5SGreg Kroah-Hartman * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the 1296fd7ce5SGreg Kroah-Hartman * tty_struct and tty_queue structures. Previously there was an array 1396fd7ce5SGreg Kroah-Hartman * of 256 tty_struct's which was statically allocated, and the 1496fd7ce5SGreg Kroah-Hartman * tty_queue structures were allocated at boot time. Both are now 1596fd7ce5SGreg Kroah-Hartman * dynamically allocated only when the tty is open. 1696fd7ce5SGreg Kroah-Hartman * 1796fd7ce5SGreg Kroah-Hartman * Also restructured routines so that there is more of a separation 1896fd7ce5SGreg Kroah-Hartman * between the high-level tty routines (tty_io.c and tty_ioctl.c) and 1996fd7ce5SGreg Kroah-Hartman * the low-level tty routines (serial.c, pty.c, console.c). This 2096fd7ce5SGreg Kroah-Hartman * makes for cleaner and more compact code. -TYT, 9/17/92 2196fd7ce5SGreg Kroah-Hartman * 2296fd7ce5SGreg Kroah-Hartman * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines 2396fd7ce5SGreg Kroah-Hartman * which can be dynamically activated and de-activated by the line 2496fd7ce5SGreg Kroah-Hartman * discipline handling modules (like SLIP). 2596fd7ce5SGreg Kroah-Hartman * 2696fd7ce5SGreg Kroah-Hartman * NOTE: pay no attention to the line discipline code (yet); its 2796fd7ce5SGreg Kroah-Hartman * interface is still subject to change in this version... 2896fd7ce5SGreg Kroah-Hartman * -- TYT, 1/31/92 2996fd7ce5SGreg Kroah-Hartman * 3096fd7ce5SGreg Kroah-Hartman * Added functionality to the OPOST tty handling. No delays, but all 3196fd7ce5SGreg Kroah-Hartman * other bits should be there. 3296fd7ce5SGreg Kroah-Hartman * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993. 3396fd7ce5SGreg Kroah-Hartman * 3496fd7ce5SGreg Kroah-Hartman * Rewrote canonical mode and added more termios flags. 3596fd7ce5SGreg Kroah-Hartman * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94 3696fd7ce5SGreg Kroah-Hartman * 3796fd7ce5SGreg Kroah-Hartman * Reorganized FASYNC support so mouse code can share it. 3896fd7ce5SGreg Kroah-Hartman * -- ctm@ardi.com, 9Sep95 3996fd7ce5SGreg Kroah-Hartman * 4096fd7ce5SGreg Kroah-Hartman * New TIOCLINUX variants added. 4196fd7ce5SGreg Kroah-Hartman * -- mj@k332.feld.cvut.cz, 19-Nov-95 4296fd7ce5SGreg Kroah-Hartman * 4396fd7ce5SGreg Kroah-Hartman * Restrict vt switching via ioctl() 4496fd7ce5SGreg Kroah-Hartman * -- grif@cs.ucr.edu, 5-Dec-95 4596fd7ce5SGreg Kroah-Hartman * 4696fd7ce5SGreg Kroah-Hartman * Move console and virtual terminal code to more appropriate files, 4796fd7ce5SGreg Kroah-Hartman * implement CONFIG_VT and generalize console device interface. 4896fd7ce5SGreg Kroah-Hartman * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 4996fd7ce5SGreg Kroah-Hartman * 5096fd7ce5SGreg Kroah-Hartman * Rewrote tty_init_dev and tty_release_dev to eliminate races. 5196fd7ce5SGreg Kroah-Hartman * -- Bill Hawes <whawes@star.net>, June 97 5296fd7ce5SGreg Kroah-Hartman * 5396fd7ce5SGreg Kroah-Hartman * Added devfs support. 5496fd7ce5SGreg Kroah-Hartman * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998 5596fd7ce5SGreg Kroah-Hartman * 5696fd7ce5SGreg Kroah-Hartman * Added support for a Unix98-style ptmx device. 5796fd7ce5SGreg Kroah-Hartman * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 5896fd7ce5SGreg Kroah-Hartman * 5996fd7ce5SGreg Kroah-Hartman * Reduced memory usage for older ARM systems 6096fd7ce5SGreg Kroah-Hartman * -- Russell King <rmk@arm.linux.org.uk> 6196fd7ce5SGreg Kroah-Hartman * 6296fd7ce5SGreg Kroah-Hartman * Move do_SAK() into process context. Less stack use in devfs functions. 6396fd7ce5SGreg Kroah-Hartman * alloc_tty_struct() always uses kmalloc() 6496fd7ce5SGreg Kroah-Hartman * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01 6596fd7ce5SGreg Kroah-Hartman */ 6696fd7ce5SGreg Kroah-Hartman 6796fd7ce5SGreg Kroah-Hartman #include <linux/types.h> 6896fd7ce5SGreg Kroah-Hartman #include <linux/major.h> 6996fd7ce5SGreg Kroah-Hartman #include <linux/errno.h> 7096fd7ce5SGreg Kroah-Hartman #include <linux/signal.h> 7196fd7ce5SGreg Kroah-Hartman #include <linux/fcntl.h> 7296fd7ce5SGreg Kroah-Hartman #include <linux/sched.h> 7396fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h> 7496fd7ce5SGreg Kroah-Hartman #include <linux/tty.h> 7596fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h> 7696fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h> 7796fd7ce5SGreg Kroah-Hartman #include <linux/devpts_fs.h> 7896fd7ce5SGreg Kroah-Hartman #include <linux/file.h> 7996fd7ce5SGreg Kroah-Hartman #include <linux/fdtable.h> 8096fd7ce5SGreg Kroah-Hartman #include <linux/console.h> 8196fd7ce5SGreg Kroah-Hartman #include <linux/timer.h> 8296fd7ce5SGreg Kroah-Hartman #include <linux/ctype.h> 8396fd7ce5SGreg Kroah-Hartman #include <linux/kd.h> 8496fd7ce5SGreg Kroah-Hartman #include <linux/mm.h> 8596fd7ce5SGreg Kroah-Hartman #include <linux/string.h> 8696fd7ce5SGreg Kroah-Hartman #include <linux/slab.h> 8796fd7ce5SGreg Kroah-Hartman #include <linux/poll.h> 8896fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h> 8996fd7ce5SGreg Kroah-Hartman #include <linux/init.h> 9096fd7ce5SGreg Kroah-Hartman #include <linux/module.h> 9196fd7ce5SGreg Kroah-Hartman #include <linux/device.h> 9296fd7ce5SGreg Kroah-Hartman #include <linux/wait.h> 9396fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h> 9496fd7ce5SGreg Kroah-Hartman #include <linux/delay.h> 9596fd7ce5SGreg Kroah-Hartman #include <linux/seq_file.h> 9696fd7ce5SGreg Kroah-Hartman #include <linux/serial.h> 975a3c6b25SManuel Zerpies #include <linux/ratelimit.h> 9896fd7ce5SGreg Kroah-Hartman 9996fd7ce5SGreg Kroah-Hartman #include <linux/uaccess.h> 10096fd7ce5SGreg Kroah-Hartman 10196fd7ce5SGreg Kroah-Hartman #include <linux/kbd_kern.h> 10296fd7ce5SGreg Kroah-Hartman #include <linux/vt_kern.h> 10396fd7ce5SGreg Kroah-Hartman #include <linux/selection.h> 10496fd7ce5SGreg Kroah-Hartman 10596fd7ce5SGreg Kroah-Hartman #include <linux/kmod.h> 10696fd7ce5SGreg Kroah-Hartman #include <linux/nsproxy.h> 10796fd7ce5SGreg Kroah-Hartman 10896fd7ce5SGreg Kroah-Hartman #undef TTY_DEBUG_HANGUP 10996fd7ce5SGreg Kroah-Hartman 11096fd7ce5SGreg Kroah-Hartman #define TTY_PARANOIA_CHECK 1 11196fd7ce5SGreg Kroah-Hartman #define CHECK_TTY_COUNT 1 11296fd7ce5SGreg Kroah-Hartman 11396fd7ce5SGreg Kroah-Hartman struct ktermios tty_std_termios = { /* for the benefit of tty drivers */ 11496fd7ce5SGreg Kroah-Hartman .c_iflag = ICRNL | IXON, 11596fd7ce5SGreg Kroah-Hartman .c_oflag = OPOST | ONLCR, 11696fd7ce5SGreg Kroah-Hartman .c_cflag = B38400 | CS8 | CREAD | HUPCL, 11796fd7ce5SGreg Kroah-Hartman .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | 11896fd7ce5SGreg Kroah-Hartman ECHOCTL | ECHOKE | IEXTEN, 11996fd7ce5SGreg Kroah-Hartman .c_cc = INIT_C_CC, 12096fd7ce5SGreg Kroah-Hartman .c_ispeed = 38400, 12196fd7ce5SGreg Kroah-Hartman .c_ospeed = 38400 12296fd7ce5SGreg Kroah-Hartman }; 12396fd7ce5SGreg Kroah-Hartman 12496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_std_termios); 12596fd7ce5SGreg Kroah-Hartman 12696fd7ce5SGreg Kroah-Hartman /* This list gets poked at by procfs and various bits of boot up code. This 12796fd7ce5SGreg Kroah-Hartman could do with some rationalisation such as pulling the tty proc function 12896fd7ce5SGreg Kroah-Hartman into this file */ 12996fd7ce5SGreg Kroah-Hartman 13096fd7ce5SGreg Kroah-Hartman LIST_HEAD(tty_drivers); /* linked list of tty drivers */ 13196fd7ce5SGreg Kroah-Hartman 13296fd7ce5SGreg Kroah-Hartman /* Mutex to protect creating and releasing a tty. This is shared with 13396fd7ce5SGreg Kroah-Hartman vt.c for deeply disgusting hack reasons */ 13496fd7ce5SGreg Kroah-Hartman DEFINE_MUTEX(tty_mutex); 13596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_mutex); 13696fd7ce5SGreg Kroah-Hartman 13796fd7ce5SGreg Kroah-Hartman /* Spinlock to protect the tty->tty_files list */ 13896fd7ce5SGreg Kroah-Hartman DEFINE_SPINLOCK(tty_files_lock); 13996fd7ce5SGreg Kroah-Hartman 14096fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); 14196fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); 14296fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *, const char __user *, 14396fd7ce5SGreg Kroah-Hartman size_t, loff_t *); 14496fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *, poll_table *); 14596fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *, struct file *); 14696fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 14796fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 14896fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 14996fd7ce5SGreg Kroah-Hartman unsigned long arg); 15096fd7ce5SGreg Kroah-Hartman #else 15196fd7ce5SGreg Kroah-Hartman #define tty_compat_ioctl NULL 15296fd7ce5SGreg Kroah-Hartman #endif 15396fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on); 15496fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on); 15596fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx); 15696fd7ce5SGreg Kroah-Hartman 15796fd7ce5SGreg Kroah-Hartman /** 15896fd7ce5SGreg Kroah-Hartman * free_tty_struct - free a disused tty 15996fd7ce5SGreg Kroah-Hartman * @tty: tty struct to free 16096fd7ce5SGreg Kroah-Hartman * 16196fd7ce5SGreg Kroah-Hartman * Free the write buffers, tty queue and tty memory itself. 16296fd7ce5SGreg Kroah-Hartman * 16396fd7ce5SGreg Kroah-Hartman * Locking: none. Must be called after tty is definitely unused 16496fd7ce5SGreg Kroah-Hartman */ 16596fd7ce5SGreg Kroah-Hartman 16696fd7ce5SGreg Kroah-Hartman void free_tty_struct(struct tty_struct *tty) 16796fd7ce5SGreg Kroah-Hartman { 168dc6802a7SDan Carpenter if (!tty) 169dc6802a7SDan Carpenter return; 17096fd7ce5SGreg Kroah-Hartman if (tty->dev) 17196fd7ce5SGreg Kroah-Hartman put_device(tty->dev); 17296fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 17389c8d91eSAlan Cox tty->magic = 0xDEADDEAD; 17496fd7ce5SGreg Kroah-Hartman kfree(tty); 17596fd7ce5SGreg Kroah-Hartman } 17696fd7ce5SGreg Kroah-Hartman 17796fd7ce5SGreg Kroah-Hartman static inline struct tty_struct *file_tty(struct file *file) 17896fd7ce5SGreg Kroah-Hartman { 17996fd7ce5SGreg Kroah-Hartman return ((struct tty_file_private *)file->private_data)->tty; 18096fd7ce5SGreg Kroah-Hartman } 18196fd7ce5SGreg Kroah-Hartman 182fa90e1c9SJiri Slaby int tty_alloc_file(struct file *file) 18396fd7ce5SGreg Kroah-Hartman { 18496fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 18596fd7ce5SGreg Kroah-Hartman 18696fd7ce5SGreg Kroah-Hartman priv = kmalloc(sizeof(*priv), GFP_KERNEL); 18796fd7ce5SGreg Kroah-Hartman if (!priv) 18896fd7ce5SGreg Kroah-Hartman return -ENOMEM; 18996fd7ce5SGreg Kroah-Hartman 190fa90e1c9SJiri Slaby file->private_data = priv; 191fa90e1c9SJiri Slaby 192fa90e1c9SJiri Slaby return 0; 193fa90e1c9SJiri Slaby } 194fa90e1c9SJiri Slaby 195fa90e1c9SJiri Slaby /* Associate a new file with the tty structure */ 196fa90e1c9SJiri Slaby void tty_add_file(struct tty_struct *tty, struct file *file) 197fa90e1c9SJiri Slaby { 198fa90e1c9SJiri Slaby struct tty_file_private *priv = file->private_data; 199fa90e1c9SJiri Slaby 20096fd7ce5SGreg Kroah-Hartman priv->tty = tty; 20196fd7ce5SGreg Kroah-Hartman priv->file = file; 20296fd7ce5SGreg Kroah-Hartman 20396fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 20496fd7ce5SGreg Kroah-Hartman list_add(&priv->list, &tty->tty_files); 20596fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 206fa90e1c9SJiri Slaby } 20796fd7ce5SGreg Kroah-Hartman 208fa90e1c9SJiri Slaby /** 209fa90e1c9SJiri Slaby * tty_free_file - free file->private_data 210fa90e1c9SJiri Slaby * 211fa90e1c9SJiri Slaby * This shall be used only for fail path handling when tty_add_file was not 212fa90e1c9SJiri Slaby * called yet. 213fa90e1c9SJiri Slaby */ 214fa90e1c9SJiri Slaby void tty_free_file(struct file *file) 215fa90e1c9SJiri Slaby { 216fa90e1c9SJiri Slaby struct tty_file_private *priv = file->private_data; 217fa90e1c9SJiri Slaby 218fa90e1c9SJiri Slaby file->private_data = NULL; 219fa90e1c9SJiri Slaby kfree(priv); 22096fd7ce5SGreg Kroah-Hartman } 22196fd7ce5SGreg Kroah-Hartman 22296fd7ce5SGreg Kroah-Hartman /* Delete file from its tty */ 2232520e274SJosh Triplett static void tty_del_file(struct file *file) 22496fd7ce5SGreg Kroah-Hartman { 22596fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv = file->private_data; 22696fd7ce5SGreg Kroah-Hartman 22796fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 22896fd7ce5SGreg Kroah-Hartman list_del(&priv->list); 22996fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 230fa90e1c9SJiri Slaby tty_free_file(file); 23196fd7ce5SGreg Kroah-Hartman } 23296fd7ce5SGreg Kroah-Hartman 23396fd7ce5SGreg Kroah-Hartman 23496fd7ce5SGreg Kroah-Hartman #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) 23596fd7ce5SGreg Kroah-Hartman 23696fd7ce5SGreg Kroah-Hartman /** 23796fd7ce5SGreg Kroah-Hartman * tty_name - return tty naming 23896fd7ce5SGreg Kroah-Hartman * @tty: tty structure 23996fd7ce5SGreg Kroah-Hartman * @buf: buffer for output 24096fd7ce5SGreg Kroah-Hartman * 24196fd7ce5SGreg Kroah-Hartman * Convert a tty structure into a name. The name reflects the kernel 24296fd7ce5SGreg Kroah-Hartman * naming policy and if udev is in use may not reflect user space 24396fd7ce5SGreg Kroah-Hartman * 24496fd7ce5SGreg Kroah-Hartman * Locking: none 24596fd7ce5SGreg Kroah-Hartman */ 24696fd7ce5SGreg Kroah-Hartman 24796fd7ce5SGreg Kroah-Hartman char *tty_name(struct tty_struct *tty, char *buf) 24896fd7ce5SGreg Kroah-Hartman { 24996fd7ce5SGreg Kroah-Hartman if (!tty) /* Hmm. NULL pointer. That's fun. */ 25096fd7ce5SGreg Kroah-Hartman strcpy(buf, "NULL tty"); 25196fd7ce5SGreg Kroah-Hartman else 25296fd7ce5SGreg Kroah-Hartman strcpy(buf, tty->name); 25396fd7ce5SGreg Kroah-Hartman return buf; 25496fd7ce5SGreg Kroah-Hartman } 25596fd7ce5SGreg Kroah-Hartman 25696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_name); 25796fd7ce5SGreg Kroah-Hartman 25896fd7ce5SGreg Kroah-Hartman int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, 25996fd7ce5SGreg Kroah-Hartman const char *routine) 26096fd7ce5SGreg Kroah-Hartman { 26196fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK 26296fd7ce5SGreg Kroah-Hartman if (!tty) { 26396fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING 26496fd7ce5SGreg Kroah-Hartman "null TTY for (%d:%d) in %s\n", 26596fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 26696fd7ce5SGreg Kroah-Hartman return 1; 26796fd7ce5SGreg Kroah-Hartman } 26896fd7ce5SGreg Kroah-Hartman if (tty->magic != TTY_MAGIC) { 26996fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING 27096fd7ce5SGreg Kroah-Hartman "bad magic number for tty struct (%d:%d) in %s\n", 27196fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 27296fd7ce5SGreg Kroah-Hartman return 1; 27396fd7ce5SGreg Kroah-Hartman } 27496fd7ce5SGreg Kroah-Hartman #endif 27596fd7ce5SGreg Kroah-Hartman return 0; 27696fd7ce5SGreg Kroah-Hartman } 27796fd7ce5SGreg Kroah-Hartman 27896fd7ce5SGreg Kroah-Hartman static int check_tty_count(struct tty_struct *tty, const char *routine) 27996fd7ce5SGreg Kroah-Hartman { 28096fd7ce5SGreg Kroah-Hartman #ifdef CHECK_TTY_COUNT 28196fd7ce5SGreg Kroah-Hartman struct list_head *p; 28296fd7ce5SGreg Kroah-Hartman int count = 0; 28396fd7ce5SGreg Kroah-Hartman 28496fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 28596fd7ce5SGreg Kroah-Hartman list_for_each(p, &tty->tty_files) { 28696fd7ce5SGreg Kroah-Hartman count++; 28796fd7ce5SGreg Kroah-Hartman } 28896fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 28996fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 29096fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_SLAVE && 29196fd7ce5SGreg Kroah-Hartman tty->link && tty->link->count) 29296fd7ce5SGreg Kroah-Hartman count++; 29396fd7ce5SGreg Kroah-Hartman if (tty->count != count) { 29496fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) " 29596fd7ce5SGreg Kroah-Hartman "!= #fd's(%d) in %s\n", 29696fd7ce5SGreg Kroah-Hartman tty->name, tty->count, count, routine); 29796fd7ce5SGreg Kroah-Hartman return count; 29896fd7ce5SGreg Kroah-Hartman } 29996fd7ce5SGreg Kroah-Hartman #endif 30096fd7ce5SGreg Kroah-Hartman return 0; 30196fd7ce5SGreg Kroah-Hartman } 30296fd7ce5SGreg Kroah-Hartman 30396fd7ce5SGreg Kroah-Hartman /** 30496fd7ce5SGreg Kroah-Hartman * get_tty_driver - find device of a tty 30596fd7ce5SGreg Kroah-Hartman * @dev_t: device identifier 30696fd7ce5SGreg Kroah-Hartman * @index: returns the index of the tty 30796fd7ce5SGreg Kroah-Hartman * 30896fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a device number 30996fd7ce5SGreg Kroah-Hartman * and also passes back the index number. 31096fd7ce5SGreg Kroah-Hartman * 31196fd7ce5SGreg Kroah-Hartman * Locking: caller must hold tty_mutex 31296fd7ce5SGreg Kroah-Hartman */ 31396fd7ce5SGreg Kroah-Hartman 31496fd7ce5SGreg Kroah-Hartman static struct tty_driver *get_tty_driver(dev_t device, int *index) 31596fd7ce5SGreg Kroah-Hartman { 31696fd7ce5SGreg Kroah-Hartman struct tty_driver *p; 31796fd7ce5SGreg Kroah-Hartman 31896fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 31996fd7ce5SGreg Kroah-Hartman dev_t base = MKDEV(p->major, p->minor_start); 32096fd7ce5SGreg Kroah-Hartman if (device < base || device >= base + p->num) 32196fd7ce5SGreg Kroah-Hartman continue; 32296fd7ce5SGreg Kroah-Hartman *index = device - base; 32396fd7ce5SGreg Kroah-Hartman return tty_driver_kref_get(p); 32496fd7ce5SGreg Kroah-Hartman } 32596fd7ce5SGreg Kroah-Hartman return NULL; 32696fd7ce5SGreg Kroah-Hartman } 32796fd7ce5SGreg Kroah-Hartman 32896fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 32996fd7ce5SGreg Kroah-Hartman 33096fd7ce5SGreg Kroah-Hartman /** 33196fd7ce5SGreg Kroah-Hartman * tty_find_polling_driver - find device of a polled tty 33296fd7ce5SGreg Kroah-Hartman * @name: name string to match 33396fd7ce5SGreg Kroah-Hartman * @line: pointer to resulting tty line nr 33496fd7ce5SGreg Kroah-Hartman * 33596fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a name 33696fd7ce5SGreg Kroah-Hartman * and the condition that the tty driver is capable of polled 33796fd7ce5SGreg Kroah-Hartman * operation. 33896fd7ce5SGreg Kroah-Hartman */ 33996fd7ce5SGreg Kroah-Hartman struct tty_driver *tty_find_polling_driver(char *name, int *line) 34096fd7ce5SGreg Kroah-Hartman { 34196fd7ce5SGreg Kroah-Hartman struct tty_driver *p, *res = NULL; 34296fd7ce5SGreg Kroah-Hartman int tty_line = 0; 34396fd7ce5SGreg Kroah-Hartman int len; 34496fd7ce5SGreg Kroah-Hartman char *str, *stp; 34596fd7ce5SGreg Kroah-Hartman 34696fd7ce5SGreg Kroah-Hartman for (str = name; *str; str++) 34796fd7ce5SGreg Kroah-Hartman if ((*str >= '0' && *str <= '9') || *str == ',') 34896fd7ce5SGreg Kroah-Hartman break; 34996fd7ce5SGreg Kroah-Hartman if (!*str) 35096fd7ce5SGreg Kroah-Hartman return NULL; 35196fd7ce5SGreg Kroah-Hartman 35296fd7ce5SGreg Kroah-Hartman len = str - name; 35396fd7ce5SGreg Kroah-Hartman tty_line = simple_strtoul(str, &str, 10); 35496fd7ce5SGreg Kroah-Hartman 35596fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 35696fd7ce5SGreg Kroah-Hartman /* Search through the tty devices to look for a match */ 35796fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 35896fd7ce5SGreg Kroah-Hartman if (strncmp(name, p->name, len) != 0) 35996fd7ce5SGreg Kroah-Hartman continue; 36096fd7ce5SGreg Kroah-Hartman stp = str; 36196fd7ce5SGreg Kroah-Hartman if (*stp == ',') 36296fd7ce5SGreg Kroah-Hartman stp++; 36396fd7ce5SGreg Kroah-Hartman if (*stp == '\0') 36496fd7ce5SGreg Kroah-Hartman stp = NULL; 36596fd7ce5SGreg Kroah-Hartman 36696fd7ce5SGreg Kroah-Hartman if (tty_line >= 0 && tty_line < p->num && p->ops && 36796fd7ce5SGreg Kroah-Hartman p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { 36896fd7ce5SGreg Kroah-Hartman res = tty_driver_kref_get(p); 36996fd7ce5SGreg Kroah-Hartman *line = tty_line; 37096fd7ce5SGreg Kroah-Hartman break; 37196fd7ce5SGreg Kroah-Hartman } 37296fd7ce5SGreg Kroah-Hartman } 37396fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 37496fd7ce5SGreg Kroah-Hartman 37596fd7ce5SGreg Kroah-Hartman return res; 37696fd7ce5SGreg Kroah-Hartman } 37796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_find_polling_driver); 37896fd7ce5SGreg Kroah-Hartman #endif 37996fd7ce5SGreg Kroah-Hartman 38096fd7ce5SGreg Kroah-Hartman /** 38196fd7ce5SGreg Kroah-Hartman * tty_check_change - check for POSIX terminal changes 38296fd7ce5SGreg Kroah-Hartman * @tty: tty to check 38396fd7ce5SGreg Kroah-Hartman * 38496fd7ce5SGreg Kroah-Hartman * If we try to write to, or set the state of, a terminal and we're 38596fd7ce5SGreg Kroah-Hartman * not in the foreground, send a SIGTTOU. If the signal is blocked or 38696fd7ce5SGreg Kroah-Hartman * ignored, go ahead and perform the operation. (POSIX 7.2) 38796fd7ce5SGreg Kroah-Hartman * 38896fd7ce5SGreg Kroah-Hartman * Locking: ctrl_lock 38996fd7ce5SGreg Kroah-Hartman */ 39096fd7ce5SGreg Kroah-Hartman 39196fd7ce5SGreg Kroah-Hartman int tty_check_change(struct tty_struct *tty) 39296fd7ce5SGreg Kroah-Hartman { 39396fd7ce5SGreg Kroah-Hartman unsigned long flags; 39496fd7ce5SGreg Kroah-Hartman int ret = 0; 39596fd7ce5SGreg Kroah-Hartman 39696fd7ce5SGreg Kroah-Hartman if (current->signal->tty != tty) 39796fd7ce5SGreg Kroah-Hartman return 0; 39896fd7ce5SGreg Kroah-Hartman 39996fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 40096fd7ce5SGreg Kroah-Hartman 40196fd7ce5SGreg Kroah-Hartman if (!tty->pgrp) { 40296fd7ce5SGreg Kroah-Hartman printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n"); 40396fd7ce5SGreg Kroah-Hartman goto out_unlock; 40496fd7ce5SGreg Kroah-Hartman } 40596fd7ce5SGreg Kroah-Hartman if (task_pgrp(current) == tty->pgrp) 40696fd7ce5SGreg Kroah-Hartman goto out_unlock; 40796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 40896fd7ce5SGreg Kroah-Hartman if (is_ignored(SIGTTOU)) 40996fd7ce5SGreg Kroah-Hartman goto out; 41096fd7ce5SGreg Kroah-Hartman if (is_current_pgrp_orphaned()) { 41196fd7ce5SGreg Kroah-Hartman ret = -EIO; 41296fd7ce5SGreg Kroah-Hartman goto out; 41396fd7ce5SGreg Kroah-Hartman } 41496fd7ce5SGreg Kroah-Hartman kill_pgrp(task_pgrp(current), SIGTTOU, 1); 41596fd7ce5SGreg Kroah-Hartman set_thread_flag(TIF_SIGPENDING); 41696fd7ce5SGreg Kroah-Hartman ret = -ERESTARTSYS; 41796fd7ce5SGreg Kroah-Hartman out: 41896fd7ce5SGreg Kroah-Hartman return ret; 41996fd7ce5SGreg Kroah-Hartman out_unlock: 42096fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 42196fd7ce5SGreg Kroah-Hartman return ret; 42296fd7ce5SGreg Kroah-Hartman } 42396fd7ce5SGreg Kroah-Hartman 42496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_check_change); 42596fd7ce5SGreg Kroah-Hartman 42696fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_read(struct file *file, char __user *buf, 42796fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 42896fd7ce5SGreg Kroah-Hartman { 42996fd7ce5SGreg Kroah-Hartman return 0; 43096fd7ce5SGreg Kroah-Hartman } 43196fd7ce5SGreg Kroah-Hartman 43296fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_write(struct file *file, const char __user *buf, 43396fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 43496fd7ce5SGreg Kroah-Hartman { 43596fd7ce5SGreg Kroah-Hartman return -EIO; 43696fd7ce5SGreg Kroah-Hartman } 43796fd7ce5SGreg Kroah-Hartman 43896fd7ce5SGreg Kroah-Hartman /* No kernel lock held - none needed ;) */ 43996fd7ce5SGreg Kroah-Hartman static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait) 44096fd7ce5SGreg Kroah-Hartman { 44196fd7ce5SGreg Kroah-Hartman return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM; 44296fd7ce5SGreg Kroah-Hartman } 44396fd7ce5SGreg Kroah-Hartman 44496fd7ce5SGreg Kroah-Hartman static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, 44596fd7ce5SGreg Kroah-Hartman unsigned long arg) 44696fd7ce5SGreg Kroah-Hartman { 44796fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 44896fd7ce5SGreg Kroah-Hartman } 44996fd7ce5SGreg Kroah-Hartman 45096fd7ce5SGreg Kroah-Hartman static long hung_up_tty_compat_ioctl(struct file *file, 45196fd7ce5SGreg Kroah-Hartman unsigned int cmd, unsigned long arg) 45296fd7ce5SGreg Kroah-Hartman { 45396fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 45496fd7ce5SGreg Kroah-Hartman } 45596fd7ce5SGreg Kroah-Hartman 45696fd7ce5SGreg Kroah-Hartman static const struct file_operations tty_fops = { 45796fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 45896fd7ce5SGreg Kroah-Hartman .read = tty_read, 45996fd7ce5SGreg Kroah-Hartman .write = tty_write, 46096fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 46196fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 46296fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 46396fd7ce5SGreg Kroah-Hartman .open = tty_open, 46496fd7ce5SGreg Kroah-Hartman .release = tty_release, 46596fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 46696fd7ce5SGreg Kroah-Hartman }; 46796fd7ce5SGreg Kroah-Hartman 46896fd7ce5SGreg Kroah-Hartman static const struct file_operations console_fops = { 46996fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 47096fd7ce5SGreg Kroah-Hartman .read = tty_read, 47196fd7ce5SGreg Kroah-Hartman .write = redirected_tty_write, 47296fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 47396fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 47496fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 47596fd7ce5SGreg Kroah-Hartman .open = tty_open, 47696fd7ce5SGreg Kroah-Hartman .release = tty_release, 47796fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 47896fd7ce5SGreg Kroah-Hartman }; 47996fd7ce5SGreg Kroah-Hartman 48096fd7ce5SGreg Kroah-Hartman static const struct file_operations hung_up_tty_fops = { 48196fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 48296fd7ce5SGreg Kroah-Hartman .read = hung_up_tty_read, 48396fd7ce5SGreg Kroah-Hartman .write = hung_up_tty_write, 48496fd7ce5SGreg Kroah-Hartman .poll = hung_up_tty_poll, 48596fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = hung_up_tty_ioctl, 48696fd7ce5SGreg Kroah-Hartman .compat_ioctl = hung_up_tty_compat_ioctl, 48796fd7ce5SGreg Kroah-Hartman .release = tty_release, 48896fd7ce5SGreg Kroah-Hartman }; 48996fd7ce5SGreg Kroah-Hartman 49096fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(redirect_lock); 49196fd7ce5SGreg Kroah-Hartman static struct file *redirect; 49296fd7ce5SGreg Kroah-Hartman 49311d9befdSPeter Hurley 49411d9befdSPeter Hurley void proc_clear_tty(struct task_struct *p) 49511d9befdSPeter Hurley { 49611d9befdSPeter Hurley unsigned long flags; 49711d9befdSPeter Hurley struct tty_struct *tty; 49811d9befdSPeter Hurley spin_lock_irqsave(&p->sighand->siglock, flags); 49911d9befdSPeter Hurley tty = p->signal->tty; 50011d9befdSPeter Hurley p->signal->tty = NULL; 50111d9befdSPeter Hurley spin_unlock_irqrestore(&p->sighand->siglock, flags); 50211d9befdSPeter Hurley tty_kref_put(tty); 50311d9befdSPeter Hurley } 50411d9befdSPeter Hurley 5052c411c11SPeter Hurley /** 5062c411c11SPeter Hurley * proc_set_tty - set the controlling terminal 5072c411c11SPeter Hurley * 5082c411c11SPeter Hurley * Only callable by the session leader and only if it does not already have 5092c411c11SPeter Hurley * a controlling terminal. 5102c411c11SPeter Hurley * 511e218eb32SPeter Hurley * Caller must hold: tty_lock() 512e218eb32SPeter Hurley * a readlock on tasklist_lock 5132c411c11SPeter Hurley * sighand lock 5142c411c11SPeter Hurley */ 515bce65f18SPeter Hurley static void __proc_set_tty(struct tty_struct *tty) 51611d9befdSPeter Hurley { 51711d9befdSPeter Hurley unsigned long flags; 518ae28fa72SPeter Hurley 51911d9befdSPeter Hurley /* We should not have a session or pgrp to put here but.... */ 52011d9befdSPeter Hurley spin_lock_irqsave(&tty->ctrl_lock, flags); 52111d9befdSPeter Hurley put_pid(tty->session); 52211d9befdSPeter Hurley put_pid(tty->pgrp); 523bce65f18SPeter Hurley tty->pgrp = get_pid(task_pgrp(current)); 52411d9befdSPeter Hurley spin_unlock_irqrestore(&tty->ctrl_lock, flags); 525bce65f18SPeter Hurley tty->session = get_pid(task_session(current)); 526bce65f18SPeter Hurley if (current->signal->tty) { 52711d9befdSPeter Hurley printk(KERN_DEBUG "tty not NULL!!\n"); 528bce65f18SPeter Hurley tty_kref_put(current->signal->tty); 52911d9befdSPeter Hurley } 530bce65f18SPeter Hurley put_pid(current->signal->tty_old_pgrp); 531bce65f18SPeter Hurley current->signal->tty = tty_kref_get(tty); 532bce65f18SPeter Hurley current->signal->tty_old_pgrp = NULL; 53311d9befdSPeter Hurley } 53411d9befdSPeter Hurley 535bce65f18SPeter Hurley static void proc_set_tty(struct tty_struct *tty) 53611d9befdSPeter Hurley { 537bce65f18SPeter Hurley spin_lock_irq(¤t->sighand->siglock); 538bce65f18SPeter Hurley __proc_set_tty(tty); 539bce65f18SPeter Hurley spin_unlock_irq(¤t->sighand->siglock); 54011d9befdSPeter Hurley } 54111d9befdSPeter Hurley 54211d9befdSPeter Hurley struct tty_struct *get_current_tty(void) 54311d9befdSPeter Hurley { 54411d9befdSPeter Hurley struct tty_struct *tty; 54511d9befdSPeter Hurley unsigned long flags; 54611d9befdSPeter Hurley 54711d9befdSPeter Hurley spin_lock_irqsave(¤t->sighand->siglock, flags); 54811d9befdSPeter Hurley tty = tty_kref_get(current->signal->tty); 54911d9befdSPeter Hurley spin_unlock_irqrestore(¤t->sighand->siglock, flags); 55011d9befdSPeter Hurley return tty; 55111d9befdSPeter Hurley } 55211d9befdSPeter Hurley EXPORT_SYMBOL_GPL(get_current_tty); 55311d9befdSPeter Hurley 55411d9befdSPeter Hurley static void session_clear_tty(struct pid *session) 55511d9befdSPeter Hurley { 55611d9befdSPeter Hurley struct task_struct *p; 55711d9befdSPeter Hurley do_each_pid_task(session, PIDTYPE_SID, p) { 55811d9befdSPeter Hurley proc_clear_tty(p); 55911d9befdSPeter Hurley } while_each_pid_task(session, PIDTYPE_SID, p); 56011d9befdSPeter Hurley } 56111d9befdSPeter Hurley 56296fd7ce5SGreg Kroah-Hartman /** 56396fd7ce5SGreg Kroah-Hartman * tty_wakeup - request more data 56496fd7ce5SGreg Kroah-Hartman * @tty: terminal 56596fd7ce5SGreg Kroah-Hartman * 56696fd7ce5SGreg Kroah-Hartman * Internal and external helper for wakeups of tty. This function 56796fd7ce5SGreg Kroah-Hartman * informs the line discipline if present that the driver is ready 56896fd7ce5SGreg Kroah-Hartman * to receive more output data. 56996fd7ce5SGreg Kroah-Hartman */ 57096fd7ce5SGreg Kroah-Hartman 57196fd7ce5SGreg Kroah-Hartman void tty_wakeup(struct tty_struct *tty) 57296fd7ce5SGreg Kroah-Hartman { 57396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 57496fd7ce5SGreg Kroah-Hartman 57596fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { 57696fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref(tty); 57796fd7ce5SGreg Kroah-Hartman if (ld) { 57896fd7ce5SGreg Kroah-Hartman if (ld->ops->write_wakeup) 57996fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup(tty); 58096fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 58196fd7ce5SGreg Kroah-Hartman } 58296fd7ce5SGreg Kroah-Hartman } 58396fd7ce5SGreg Kroah-Hartman wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 58496fd7ce5SGreg Kroah-Hartman } 58596fd7ce5SGreg Kroah-Hartman 58696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_wakeup); 58796fd7ce5SGreg Kroah-Hartman 58896fd7ce5SGreg Kroah-Hartman /** 589ea648a47SPeter Hurley * tty_signal_session_leader - sends SIGHUP to session leader 590f91e2590SPeter Hurley * @tty controlling tty 591f91e2590SPeter Hurley * @exit_session if non-zero, signal all foreground group processes 592ea648a47SPeter Hurley * 593f91e2590SPeter Hurley * Send SIGHUP and SIGCONT to the session leader and its process group. 594f91e2590SPeter Hurley * Optionally, signal all processes in the foreground process group. 595ea648a47SPeter Hurley * 596ea648a47SPeter Hurley * Returns the number of processes in the session with this tty 597ea648a47SPeter Hurley * as their controlling terminal. This value is used to drop 598ea648a47SPeter Hurley * tty references for those processes. 599ea648a47SPeter Hurley */ 600f91e2590SPeter Hurley static int tty_signal_session_leader(struct tty_struct *tty, int exit_session) 601ea648a47SPeter Hurley { 602ea648a47SPeter Hurley struct task_struct *p; 603ea648a47SPeter Hurley int refs = 0; 604f91e2590SPeter Hurley struct pid *tty_pgrp = NULL; 605ea648a47SPeter Hurley 606ea648a47SPeter Hurley read_lock(&tasklist_lock); 607ea648a47SPeter Hurley if (tty->session) { 608ea648a47SPeter Hurley do_each_pid_task(tty->session, PIDTYPE_SID, p) { 609ea648a47SPeter Hurley spin_lock_irq(&p->sighand->siglock); 610ea648a47SPeter Hurley if (p->signal->tty == tty) { 611ea648a47SPeter Hurley p->signal->tty = NULL; 612ea648a47SPeter Hurley /* We defer the dereferences outside fo 613ea648a47SPeter Hurley the tasklist lock */ 614ea648a47SPeter Hurley refs++; 615ea648a47SPeter Hurley } 616ea648a47SPeter Hurley if (!p->signal->leader) { 617ea648a47SPeter Hurley spin_unlock_irq(&p->sighand->siglock); 618ea648a47SPeter Hurley continue; 619ea648a47SPeter Hurley } 620ea648a47SPeter Hurley __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p); 621ea648a47SPeter Hurley __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p); 622ea648a47SPeter Hurley put_pid(p->signal->tty_old_pgrp); /* A noop */ 623bc30c3b2SPeter Hurley spin_lock(&tty->ctrl_lock); 624f91e2590SPeter Hurley tty_pgrp = get_pid(tty->pgrp); 625ea648a47SPeter Hurley if (tty->pgrp) 626ea648a47SPeter Hurley p->signal->tty_old_pgrp = get_pid(tty->pgrp); 627bc30c3b2SPeter Hurley spin_unlock(&tty->ctrl_lock); 628ea648a47SPeter Hurley spin_unlock_irq(&p->sighand->siglock); 629ea648a47SPeter Hurley } while_each_pid_task(tty->session, PIDTYPE_SID, p); 630ea648a47SPeter Hurley } 631ea648a47SPeter Hurley read_unlock(&tasklist_lock); 632ea648a47SPeter Hurley 633f91e2590SPeter Hurley if (tty_pgrp) { 634f91e2590SPeter Hurley if (exit_session) 635f91e2590SPeter Hurley kill_pgrp(tty_pgrp, SIGHUP, exit_session); 636f91e2590SPeter Hurley put_pid(tty_pgrp); 637f91e2590SPeter Hurley } 638f91e2590SPeter Hurley 639ea648a47SPeter Hurley return refs; 640ea648a47SPeter Hurley } 641ea648a47SPeter Hurley 642ea648a47SPeter Hurley /** 64396fd7ce5SGreg Kroah-Hartman * __tty_hangup - actual handler for hangup events 64496fd7ce5SGreg Kroah-Hartman * @work: tty device 64596fd7ce5SGreg Kroah-Hartman * 646ef4f527cSKevin Cernekee * This can be called by a "kworker" kernel thread. That is process 64796fd7ce5SGreg Kroah-Hartman * synchronous but doesn't hold any locks, so we need to make sure we 64896fd7ce5SGreg Kroah-Hartman * have the appropriate locks for what we're doing. 64996fd7ce5SGreg Kroah-Hartman * 65096fd7ce5SGreg Kroah-Hartman * The hangup event clears any pending redirections onto the hung up 65196fd7ce5SGreg Kroah-Hartman * device. It ensures future writes will error and it does the needed 65296fd7ce5SGreg Kroah-Hartman * line discipline hangup and signal delivery. The tty object itself 65396fd7ce5SGreg Kroah-Hartman * remains intact. 65496fd7ce5SGreg Kroah-Hartman * 65596fd7ce5SGreg Kroah-Hartman * Locking: 65696fd7ce5SGreg Kroah-Hartman * BTM 65796fd7ce5SGreg Kroah-Hartman * redirect lock for undoing redirection 65896fd7ce5SGreg Kroah-Hartman * file list lock for manipulating list of ttys 659137084bbSPeter Hurley * tty_ldiscs_lock from called functions 6606a1c0680SPeter Hurley * termios_rwsem resetting termios data 66196fd7ce5SGreg Kroah-Hartman * tasklist_lock to walk task list for hangup event 66296fd7ce5SGreg Kroah-Hartman * ->siglock to protect ->signal/->sighand 66396fd7ce5SGreg Kroah-Hartman */ 664f91e2590SPeter Hurley static void __tty_hangup(struct tty_struct *tty, int exit_session) 66596fd7ce5SGreg Kroah-Hartman { 66696fd7ce5SGreg Kroah-Hartman struct file *cons_filp = NULL; 66796fd7ce5SGreg Kroah-Hartman struct file *filp, *f = NULL; 66896fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 66996fd7ce5SGreg Kroah-Hartman int closecount = 0, n; 670ea648a47SPeter Hurley int refs; 67196fd7ce5SGreg Kroah-Hartman 67296fd7ce5SGreg Kroah-Hartman if (!tty) 67396fd7ce5SGreg Kroah-Hartman return; 67496fd7ce5SGreg Kroah-Hartman 67596fd7ce5SGreg Kroah-Hartman 67696fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 67796fd7ce5SGreg Kroah-Hartman if (redirect && file_tty(redirect) == tty) { 67896fd7ce5SGreg Kroah-Hartman f = redirect; 67996fd7ce5SGreg Kroah-Hartman redirect = NULL; 68096fd7ce5SGreg Kroah-Hartman } 68196fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 68296fd7ce5SGreg Kroah-Hartman 68389c8d91eSAlan Cox tty_lock(tty); 68496fd7ce5SGreg Kroah-Hartman 685cb50e523SPeter Hurley if (test_bit(TTY_HUPPED, &tty->flags)) { 686cb50e523SPeter Hurley tty_unlock(tty); 687cb50e523SPeter Hurley return; 688cb50e523SPeter Hurley } 689cb50e523SPeter Hurley 690acfa747bSJiri Slaby /* some functions below drop BTM, so we need this bit */ 691acfa747bSJiri Slaby set_bit(TTY_HUPPING, &tty->flags); 692acfa747bSJiri Slaby 69396fd7ce5SGreg Kroah-Hartman /* inuse_filps is protected by the single tty lock, 69496fd7ce5SGreg Kroah-Hartman this really needs to change if we want to flush the 69596fd7ce5SGreg Kroah-Hartman workqueue with the lock held */ 69696fd7ce5SGreg Kroah-Hartman check_tty_count(tty, "tty_hangup"); 69796fd7ce5SGreg Kroah-Hartman 69896fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 69996fd7ce5SGreg Kroah-Hartman /* This breaks for file handles being sent over AF_UNIX sockets ? */ 70096fd7ce5SGreg Kroah-Hartman list_for_each_entry(priv, &tty->tty_files, list) { 70196fd7ce5SGreg Kroah-Hartman filp = priv->file; 70296fd7ce5SGreg Kroah-Hartman if (filp->f_op->write == redirected_tty_write) 70396fd7ce5SGreg Kroah-Hartman cons_filp = filp; 70496fd7ce5SGreg Kroah-Hartman if (filp->f_op->write != tty_write) 70596fd7ce5SGreg Kroah-Hartman continue; 70696fd7ce5SGreg Kroah-Hartman closecount++; 70796fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); /* can't block */ 70896fd7ce5SGreg Kroah-Hartman filp->f_op = &hung_up_tty_fops; 70996fd7ce5SGreg Kroah-Hartman } 71096fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 71196fd7ce5SGreg Kroah-Hartman 71225fdf243SPeter Hurley refs = tty_signal_session_leader(tty, exit_session); 71325fdf243SPeter Hurley /* Account for the p->signal references we killed */ 71425fdf243SPeter Hurley while (refs--) 71525fdf243SPeter Hurley tty_kref_put(tty); 71625fdf243SPeter Hurley 717acfa747bSJiri Slaby /* 718acfa747bSJiri Slaby * it drops BTM and thus races with reopen 719acfa747bSJiri Slaby * we protect the race by TTY_HUPPING 720acfa747bSJiri Slaby */ 72196fd7ce5SGreg Kroah-Hartman tty_ldisc_hangup(tty); 72296fd7ce5SGreg Kroah-Hartman 72320cc225bSPeter Hurley spin_lock_irq(&tty->ctrl_lock); 72496fd7ce5SGreg Kroah-Hartman clear_bit(TTY_THROTTLED, &tty->flags); 72596fd7ce5SGreg Kroah-Hartman clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 72696fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 72796fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 72896fd7ce5SGreg Kroah-Hartman tty->session = NULL; 72996fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 73096fd7ce5SGreg Kroah-Hartman tty->ctrl_status = 0; 73120cc225bSPeter Hurley spin_unlock_irq(&tty->ctrl_lock); 73296fd7ce5SGreg Kroah-Hartman 73396fd7ce5SGreg Kroah-Hartman /* 73496fd7ce5SGreg Kroah-Hartman * If one of the devices matches a console pointer, we 73596fd7ce5SGreg Kroah-Hartman * cannot just call hangup() because that will cause 73696fd7ce5SGreg Kroah-Hartman * tty->count and state->count to go out of sync. 73796fd7ce5SGreg Kroah-Hartman * So we just call close() the right number of times. 73896fd7ce5SGreg Kroah-Hartman */ 73996fd7ce5SGreg Kroah-Hartman if (cons_filp) { 74096fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 74196fd7ce5SGreg Kroah-Hartman for (n = 0; n < closecount; n++) 74296fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, cons_filp); 74396fd7ce5SGreg Kroah-Hartman } else if (tty->ops->hangup) 7447c6d340fSPeter Hurley tty->ops->hangup(tty); 74596fd7ce5SGreg Kroah-Hartman /* 74696fd7ce5SGreg Kroah-Hartman * We don't want to have driver/ldisc interactions beyond 74796fd7ce5SGreg Kroah-Hartman * the ones we did here. The driver layer expects no 74896fd7ce5SGreg Kroah-Hartman * calls after ->hangup() from the ldisc side. However we 74996fd7ce5SGreg Kroah-Hartman * can't yet guarantee all that. 75096fd7ce5SGreg Kroah-Hartman */ 75196fd7ce5SGreg Kroah-Hartman set_bit(TTY_HUPPED, &tty->flags); 752acfa747bSJiri Slaby clear_bit(TTY_HUPPING, &tty->flags); 75396fd7ce5SGreg Kroah-Hartman 75489c8d91eSAlan Cox tty_unlock(tty); 75596fd7ce5SGreg Kroah-Hartman 75696fd7ce5SGreg Kroah-Hartman if (f) 75796fd7ce5SGreg Kroah-Hartman fput(f); 75896fd7ce5SGreg Kroah-Hartman } 75996fd7ce5SGreg Kroah-Hartman 76096fd7ce5SGreg Kroah-Hartman static void do_tty_hangup(struct work_struct *work) 76196fd7ce5SGreg Kroah-Hartman { 76296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 76396fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 76496fd7ce5SGreg Kroah-Hartman 765f91e2590SPeter Hurley __tty_hangup(tty, 0); 76696fd7ce5SGreg Kroah-Hartman } 76796fd7ce5SGreg Kroah-Hartman 76896fd7ce5SGreg Kroah-Hartman /** 76996fd7ce5SGreg Kroah-Hartman * tty_hangup - trigger a hangup event 77096fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 77196fd7ce5SGreg Kroah-Hartman * 77296fd7ce5SGreg Kroah-Hartman * A carrier loss (virtual or otherwise) has occurred on this like 77396fd7ce5SGreg Kroah-Hartman * schedule a hangup sequence to run after this event. 77496fd7ce5SGreg Kroah-Hartman */ 77596fd7ce5SGreg Kroah-Hartman 77696fd7ce5SGreg Kroah-Hartman void tty_hangup(struct tty_struct *tty) 77796fd7ce5SGreg Kroah-Hartman { 77896fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 77996fd7ce5SGreg Kroah-Hartman char buf[64]; 78096fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf)); 78196fd7ce5SGreg Kroah-Hartman #endif 78296fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 78396fd7ce5SGreg Kroah-Hartman } 78496fd7ce5SGreg Kroah-Hartman 78596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hangup); 78696fd7ce5SGreg Kroah-Hartman 78796fd7ce5SGreg Kroah-Hartman /** 78896fd7ce5SGreg Kroah-Hartman * tty_vhangup - process vhangup 78996fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 79096fd7ce5SGreg Kroah-Hartman * 79196fd7ce5SGreg Kroah-Hartman * The user has asked via system call for the terminal to be hung up. 79296fd7ce5SGreg Kroah-Hartman * We do this synchronously so that when the syscall returns the process 79396fd7ce5SGreg Kroah-Hartman * is complete. That guarantee is necessary for security reasons. 79496fd7ce5SGreg Kroah-Hartman */ 79596fd7ce5SGreg Kroah-Hartman 79696fd7ce5SGreg Kroah-Hartman void tty_vhangup(struct tty_struct *tty) 79796fd7ce5SGreg Kroah-Hartman { 79896fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 79996fd7ce5SGreg Kroah-Hartman char buf[64]; 80096fd7ce5SGreg Kroah-Hartman 80196fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); 80296fd7ce5SGreg Kroah-Hartman #endif 803f91e2590SPeter Hurley __tty_hangup(tty, 0); 80496fd7ce5SGreg Kroah-Hartman } 80596fd7ce5SGreg Kroah-Hartman 80696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_vhangup); 80796fd7ce5SGreg Kroah-Hartman 80896fd7ce5SGreg Kroah-Hartman 80996fd7ce5SGreg Kroah-Hartman /** 81096fd7ce5SGreg Kroah-Hartman * tty_vhangup_self - process vhangup for own ctty 81196fd7ce5SGreg Kroah-Hartman * 81296fd7ce5SGreg Kroah-Hartman * Perform a vhangup on the current controlling tty 81396fd7ce5SGreg Kroah-Hartman */ 81496fd7ce5SGreg Kroah-Hartman 81596fd7ce5SGreg Kroah-Hartman void tty_vhangup_self(void) 81696fd7ce5SGreg Kroah-Hartman { 81796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 81896fd7ce5SGreg Kroah-Hartman 81996fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 82096fd7ce5SGreg Kroah-Hartman if (tty) { 82196fd7ce5SGreg Kroah-Hartman tty_vhangup(tty); 82296fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 82396fd7ce5SGreg Kroah-Hartman } 82496fd7ce5SGreg Kroah-Hartman } 82596fd7ce5SGreg Kroah-Hartman 82696fd7ce5SGreg Kroah-Hartman /** 827f91e2590SPeter Hurley * tty_vhangup_session - hangup session leader exit 828f91e2590SPeter Hurley * @tty: tty to hangup 829f91e2590SPeter Hurley * 830f91e2590SPeter Hurley * The session leader is exiting and hanging up its controlling terminal. 831f91e2590SPeter Hurley * Every process in the foreground process group is signalled SIGHUP. 832f91e2590SPeter Hurley * 833f91e2590SPeter Hurley * We do this synchronously so that when the syscall returns the process 834f91e2590SPeter Hurley * is complete. That guarantee is necessary for security reasons. 835f91e2590SPeter Hurley */ 836f91e2590SPeter Hurley 83744a459fdSChanglong Xie static void tty_vhangup_session(struct tty_struct *tty) 838f91e2590SPeter Hurley { 839f91e2590SPeter Hurley #ifdef TTY_DEBUG_HANGUP 840f91e2590SPeter Hurley char buf[64]; 841f91e2590SPeter Hurley 842f91e2590SPeter Hurley printk(KERN_DEBUG "%s vhangup session...\n", tty_name(tty, buf)); 843f91e2590SPeter Hurley #endif 844f91e2590SPeter Hurley __tty_hangup(tty, 1); 845f91e2590SPeter Hurley } 846f91e2590SPeter Hurley 847f91e2590SPeter Hurley /** 84896fd7ce5SGreg Kroah-Hartman * tty_hung_up_p - was tty hung up 84996fd7ce5SGreg Kroah-Hartman * @filp: file pointer of tty 85096fd7ce5SGreg Kroah-Hartman * 85196fd7ce5SGreg Kroah-Hartman * Return true if the tty has been subject to a vhangup or a carrier 85296fd7ce5SGreg Kroah-Hartman * loss 85396fd7ce5SGreg Kroah-Hartman */ 85496fd7ce5SGreg Kroah-Hartman 85596fd7ce5SGreg Kroah-Hartman int tty_hung_up_p(struct file *filp) 85696fd7ce5SGreg Kroah-Hartman { 85796fd7ce5SGreg Kroah-Hartman return (filp->f_op == &hung_up_tty_fops); 85896fd7ce5SGreg Kroah-Hartman } 85996fd7ce5SGreg Kroah-Hartman 86096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hung_up_p); 86196fd7ce5SGreg Kroah-Hartman 86296fd7ce5SGreg Kroah-Hartman /** 86396fd7ce5SGreg Kroah-Hartman * disassociate_ctty - disconnect controlling tty 86496fd7ce5SGreg Kroah-Hartman * @on_exit: true if exiting so need to "hang up" the session 86596fd7ce5SGreg Kroah-Hartman * 86696fd7ce5SGreg Kroah-Hartman * This function is typically called only by the session leader, when 86796fd7ce5SGreg Kroah-Hartman * it wants to disassociate itself from its controlling tty. 86896fd7ce5SGreg Kroah-Hartman * 86996fd7ce5SGreg Kroah-Hartman * It performs the following functions: 87096fd7ce5SGreg Kroah-Hartman * (1) Sends a SIGHUP and SIGCONT to the foreground process group 87196fd7ce5SGreg Kroah-Hartman * (2) Clears the tty from being controlling the session 87296fd7ce5SGreg Kroah-Hartman * (3) Clears the controlling tty for all processes in the 87396fd7ce5SGreg Kroah-Hartman * session group. 87496fd7ce5SGreg Kroah-Hartman * 87596fd7ce5SGreg Kroah-Hartman * The argument on_exit is set to 1 if called when a process is 87696fd7ce5SGreg Kroah-Hartman * exiting; it is 0 if called by the ioctl TIOCNOTTY. 87796fd7ce5SGreg Kroah-Hartman * 87896fd7ce5SGreg Kroah-Hartman * Locking: 87996fd7ce5SGreg Kroah-Hartman * BTM is taken for hysterical raisins, and held when 88096fd7ce5SGreg Kroah-Hartman * called from no_tty(). 88196fd7ce5SGreg Kroah-Hartman * tty_mutex is taken to protect tty 88296fd7ce5SGreg Kroah-Hartman * ->siglock is taken to protect ->signal/->sighand 88396fd7ce5SGreg Kroah-Hartman * tasklist_lock is taken to walk process list for sessions 88496fd7ce5SGreg Kroah-Hartman * ->siglock is taken to protect ->signal/->sighand 88596fd7ce5SGreg Kroah-Hartman */ 88696fd7ce5SGreg Kroah-Hartman 88796fd7ce5SGreg Kroah-Hartman void disassociate_ctty(int on_exit) 88896fd7ce5SGreg Kroah-Hartman { 88996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 89096fd7ce5SGreg Kroah-Hartman 89196fd7ce5SGreg Kroah-Hartman if (!current->signal->leader) 89296fd7ce5SGreg Kroah-Hartman return; 89396fd7ce5SGreg Kroah-Hartman 89496fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 89596fd7ce5SGreg Kroah-Hartman if (tty) { 896f91e2590SPeter Hurley if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) { 897f91e2590SPeter Hurley tty_vhangup_session(tty); 898f91e2590SPeter Hurley } else { 899f91e2590SPeter Hurley struct pid *tty_pgrp = tty_get_pgrp(tty); 9001411dc4aSJiri Slaby if (tty_pgrp) { 9011411dc4aSJiri Slaby kill_pgrp(tty_pgrp, SIGHUP, on_exit); 90203e12617SOleg Nesterov if (!on_exit) 9031411dc4aSJiri Slaby kill_pgrp(tty_pgrp, SIGCONT, on_exit); 9041411dc4aSJiri Slaby put_pid(tty_pgrp); 9051411dc4aSJiri Slaby } 906f91e2590SPeter Hurley } 907f91e2590SPeter Hurley tty_kref_put(tty); 908f91e2590SPeter Hurley 90996fd7ce5SGreg Kroah-Hartman } else if (on_exit) { 91096fd7ce5SGreg Kroah-Hartman struct pid *old_pgrp; 91196fd7ce5SGreg Kroah-Hartman spin_lock_irq(¤t->sighand->siglock); 91296fd7ce5SGreg Kroah-Hartman old_pgrp = current->signal->tty_old_pgrp; 91396fd7ce5SGreg Kroah-Hartman current->signal->tty_old_pgrp = NULL; 91496fd7ce5SGreg Kroah-Hartman spin_unlock_irq(¤t->sighand->siglock); 91596fd7ce5SGreg Kroah-Hartman if (old_pgrp) { 91696fd7ce5SGreg Kroah-Hartman kill_pgrp(old_pgrp, SIGHUP, on_exit); 91796fd7ce5SGreg Kroah-Hartman kill_pgrp(old_pgrp, SIGCONT, on_exit); 91896fd7ce5SGreg Kroah-Hartman put_pid(old_pgrp); 91996fd7ce5SGreg Kroah-Hartman } 92096fd7ce5SGreg Kroah-Hartman return; 92196fd7ce5SGreg Kroah-Hartman } 92296fd7ce5SGreg Kroah-Hartman 92396fd7ce5SGreg Kroah-Hartman spin_lock_irq(¤t->sighand->siglock); 92496fd7ce5SGreg Kroah-Hartman put_pid(current->signal->tty_old_pgrp); 92596fd7ce5SGreg Kroah-Hartman current->signal->tty_old_pgrp = NULL; 92696fd7ce5SGreg Kroah-Hartman 927c70dbb1eSChen Tingjie tty = tty_kref_get(current->signal->tty); 92896fd7ce5SGreg Kroah-Hartman if (tty) { 92996fd7ce5SGreg Kroah-Hartman unsigned long flags; 93096fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 93196fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 93296fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 93396fd7ce5SGreg Kroah-Hartman tty->session = NULL; 93496fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 93596fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 93696fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 93796fd7ce5SGreg Kroah-Hartman } else { 93896fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 93996fd7ce5SGreg Kroah-Hartman printk(KERN_DEBUG "error attempted to write to tty [0x%p]" 94096fd7ce5SGreg Kroah-Hartman " = NULL", tty); 94196fd7ce5SGreg Kroah-Hartman #endif 94296fd7ce5SGreg Kroah-Hartman } 94396fd7ce5SGreg Kroah-Hartman 944c70dbb1eSChen Tingjie spin_unlock_irq(¤t->sighand->siglock); 94596fd7ce5SGreg Kroah-Hartman /* Now clear signal->tty under the lock */ 94696fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 94796fd7ce5SGreg Kroah-Hartman session_clear_tty(task_session(current)); 94896fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 94996fd7ce5SGreg Kroah-Hartman } 95096fd7ce5SGreg Kroah-Hartman 95196fd7ce5SGreg Kroah-Hartman /** 95296fd7ce5SGreg Kroah-Hartman * 95396fd7ce5SGreg Kroah-Hartman * no_tty - Ensure the current process does not have a controlling tty 95496fd7ce5SGreg Kroah-Hartman */ 95596fd7ce5SGreg Kroah-Hartman void no_tty(void) 95696fd7ce5SGreg Kroah-Hartman { 9573af502b9SAlan Cox /* FIXME: Review locking here. The tty_lock never covered any race 9583af502b9SAlan Cox between a new association and proc_clear_tty but possible we need 9593af502b9SAlan Cox to protect against this anyway */ 96096fd7ce5SGreg Kroah-Hartman struct task_struct *tsk = current; 96196fd7ce5SGreg Kroah-Hartman disassociate_ctty(0); 96296fd7ce5SGreg Kroah-Hartman proc_clear_tty(tsk); 96396fd7ce5SGreg Kroah-Hartman } 96496fd7ce5SGreg Kroah-Hartman 96596fd7ce5SGreg Kroah-Hartman 96696fd7ce5SGreg Kroah-Hartman /** 96796fd7ce5SGreg Kroah-Hartman * stop_tty - propagate flow control 96896fd7ce5SGreg Kroah-Hartman * @tty: tty to stop 96996fd7ce5SGreg Kroah-Hartman * 97001adc807SPeter Hurley * Perform flow control to the driver. May be called 97196fd7ce5SGreg Kroah-Hartman * on an already stopped device and will not re-call the driver 97296fd7ce5SGreg Kroah-Hartman * method. 97396fd7ce5SGreg Kroah-Hartman * 97496fd7ce5SGreg Kroah-Hartman * This functionality is used by both the line disciplines for 97596fd7ce5SGreg Kroah-Hartman * halting incoming flow and by the driver. It may therefore be 97696fd7ce5SGreg Kroah-Hartman * called from any context, may be under the tty atomic_write_lock 97796fd7ce5SGreg Kroah-Hartman * but not always. 97896fd7ce5SGreg Kroah-Hartman * 97996fd7ce5SGreg Kroah-Hartman * Locking: 980f9e053dcSPeter Hurley * flow_lock 98196fd7ce5SGreg Kroah-Hartman */ 98296fd7ce5SGreg Kroah-Hartman 983f9e053dcSPeter Hurley void __stop_tty(struct tty_struct *tty) 98496fd7ce5SGreg Kroah-Hartman { 985f9e053dcSPeter Hurley if (tty->stopped) 98696fd7ce5SGreg Kroah-Hartman return; 98796fd7ce5SGreg Kroah-Hartman tty->stopped = 1; 98896fd7ce5SGreg Kroah-Hartman if (tty->ops->stop) 98996fd7ce5SGreg Kroah-Hartman (tty->ops->stop)(tty); 99096fd7ce5SGreg Kroah-Hartman } 99196fd7ce5SGreg Kroah-Hartman 992f9e053dcSPeter Hurley void stop_tty(struct tty_struct *tty) 993f9e053dcSPeter Hurley { 994f9e053dcSPeter Hurley unsigned long flags; 995f9e053dcSPeter Hurley 996f9e053dcSPeter Hurley spin_lock_irqsave(&tty->flow_lock, flags); 997f9e053dcSPeter Hurley __stop_tty(tty); 998f9e053dcSPeter Hurley spin_unlock_irqrestore(&tty->flow_lock, flags); 999f9e053dcSPeter Hurley } 100096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(stop_tty); 100196fd7ce5SGreg Kroah-Hartman 100296fd7ce5SGreg Kroah-Hartman /** 100396fd7ce5SGreg Kroah-Hartman * start_tty - propagate flow control 100496fd7ce5SGreg Kroah-Hartman * @tty: tty to start 100596fd7ce5SGreg Kroah-Hartman * 100601adc807SPeter Hurley * Start a tty that has been stopped if at all possible. If this 100701adc807SPeter Hurley * tty was previous stopped and is now being started, the driver 100801adc807SPeter Hurley * start method is invoked and the line discipline woken. 100996fd7ce5SGreg Kroah-Hartman * 101096fd7ce5SGreg Kroah-Hartman * Locking: 1011f9e053dcSPeter Hurley * flow_lock 101296fd7ce5SGreg Kroah-Hartman */ 101396fd7ce5SGreg Kroah-Hartman 1014f9e053dcSPeter Hurley void __start_tty(struct tty_struct *tty) 101596fd7ce5SGreg Kroah-Hartman { 1016f9e053dcSPeter Hurley if (!tty->stopped || tty->flow_stopped) 101796fd7ce5SGreg Kroah-Hartman return; 101896fd7ce5SGreg Kroah-Hartman tty->stopped = 0; 101996fd7ce5SGreg Kroah-Hartman if (tty->ops->start) 102096fd7ce5SGreg Kroah-Hartman (tty->ops->start)(tty); 102196fd7ce5SGreg Kroah-Hartman tty_wakeup(tty); 102296fd7ce5SGreg Kroah-Hartman } 102396fd7ce5SGreg Kroah-Hartman 1024f9e053dcSPeter Hurley void start_tty(struct tty_struct *tty) 1025f9e053dcSPeter Hurley { 1026f9e053dcSPeter Hurley unsigned long flags; 1027f9e053dcSPeter Hurley 1028f9e053dcSPeter Hurley spin_lock_irqsave(&tty->flow_lock, flags); 1029f9e053dcSPeter Hurley __start_tty(tty); 1030f9e053dcSPeter Hurley spin_unlock_irqrestore(&tty->flow_lock, flags); 1031f9e053dcSPeter Hurley } 103296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(start_tty); 103396fd7ce5SGreg Kroah-Hartman 1034b0b88565SLinus Torvalds /* We limit tty time update visibility to every 8 seconds or so. */ 103537b7f3c7SJiri Slaby static void tty_update_time(struct timespec *time) 103637b7f3c7SJiri Slaby { 1037b0b88565SLinus Torvalds unsigned long sec = get_seconds() & ~7; 103837b7f3c7SJiri Slaby if ((long)(sec - time->tv_sec) > 0) 103937b7f3c7SJiri Slaby time->tv_sec = sec; 104037b7f3c7SJiri Slaby } 104137b7f3c7SJiri Slaby 104296fd7ce5SGreg Kroah-Hartman /** 104396fd7ce5SGreg Kroah-Hartman * tty_read - read method for tty device files 104496fd7ce5SGreg Kroah-Hartman * @file: pointer to tty file 104596fd7ce5SGreg Kroah-Hartman * @buf: user buffer 104696fd7ce5SGreg Kroah-Hartman * @count: size of user buffer 104796fd7ce5SGreg Kroah-Hartman * @ppos: unused 104896fd7ce5SGreg Kroah-Hartman * 104996fd7ce5SGreg Kroah-Hartman * Perform the read system call function on this terminal device. Checks 105096fd7ce5SGreg Kroah-Hartman * for hung up devices before calling the line discipline method. 105196fd7ce5SGreg Kroah-Hartman * 105296fd7ce5SGreg Kroah-Hartman * Locking: 105396fd7ce5SGreg Kroah-Hartman * Locks the line discipline internally while needed. Multiple 105496fd7ce5SGreg Kroah-Hartman * read calls may be outstanding in parallel. 105596fd7ce5SGreg Kroah-Hartman */ 105696fd7ce5SGreg Kroah-Hartman 105796fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *file, char __user *buf, size_t count, 105896fd7ce5SGreg Kroah-Hartman loff_t *ppos) 105996fd7ce5SGreg Kroah-Hartman { 106096fd7ce5SGreg Kroah-Hartman int i; 106137b7f3c7SJiri Slaby struct inode *inode = file_inode(file); 106296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 106396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 106496fd7ce5SGreg Kroah-Hartman 106537b7f3c7SJiri Slaby if (tty_paranoia_check(tty, inode, "tty_read")) 106696fd7ce5SGreg Kroah-Hartman return -EIO; 106796fd7ce5SGreg Kroah-Hartman if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) 106896fd7ce5SGreg Kroah-Hartman return -EIO; 106996fd7ce5SGreg Kroah-Hartman 107096fd7ce5SGreg Kroah-Hartman /* We want to wait for the line discipline to sort out in this 107196fd7ce5SGreg Kroah-Hartman situation */ 107296fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 107396fd7ce5SGreg Kroah-Hartman if (ld->ops->read) 107496fd7ce5SGreg Kroah-Hartman i = (ld->ops->read)(tty, file, buf, count); 107596fd7ce5SGreg Kroah-Hartman else 107696fd7ce5SGreg Kroah-Hartman i = -EIO; 107796fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 1078b0de59b5SJiri Slaby 107937b7f3c7SJiri Slaby if (i > 0) 108037b7f3c7SJiri Slaby tty_update_time(&inode->i_atime); 108137b7f3c7SJiri Slaby 108296fd7ce5SGreg Kroah-Hartman return i; 108396fd7ce5SGreg Kroah-Hartman } 108496fd7ce5SGreg Kroah-Hartman 1085136d5258SPeter Hurley static void tty_write_unlock(struct tty_struct *tty) 108696fd7ce5SGreg Kroah-Hartman { 108796fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->atomic_write_lock); 108896fd7ce5SGreg Kroah-Hartman wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 108996fd7ce5SGreg Kroah-Hartman } 109096fd7ce5SGreg Kroah-Hartman 1091136d5258SPeter Hurley static int tty_write_lock(struct tty_struct *tty, int ndelay) 109296fd7ce5SGreg Kroah-Hartman { 109396fd7ce5SGreg Kroah-Hartman if (!mutex_trylock(&tty->atomic_write_lock)) { 109496fd7ce5SGreg Kroah-Hartman if (ndelay) 109596fd7ce5SGreg Kroah-Hartman return -EAGAIN; 109696fd7ce5SGreg Kroah-Hartman if (mutex_lock_interruptible(&tty->atomic_write_lock)) 109796fd7ce5SGreg Kroah-Hartman return -ERESTARTSYS; 109896fd7ce5SGreg Kroah-Hartman } 109996fd7ce5SGreg Kroah-Hartman return 0; 110096fd7ce5SGreg Kroah-Hartman } 110196fd7ce5SGreg Kroah-Hartman 110296fd7ce5SGreg Kroah-Hartman /* 110396fd7ce5SGreg Kroah-Hartman * Split writes up in sane blocksizes to avoid 110496fd7ce5SGreg Kroah-Hartman * denial-of-service type attacks 110596fd7ce5SGreg Kroah-Hartman */ 110696fd7ce5SGreg Kroah-Hartman static inline ssize_t do_tty_write( 110796fd7ce5SGreg Kroah-Hartman ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), 110896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, 110996fd7ce5SGreg Kroah-Hartman struct file *file, 111096fd7ce5SGreg Kroah-Hartman const char __user *buf, 111196fd7ce5SGreg Kroah-Hartman size_t count) 111296fd7ce5SGreg Kroah-Hartman { 111396fd7ce5SGreg Kroah-Hartman ssize_t ret, written = 0; 111496fd7ce5SGreg Kroah-Hartman unsigned int chunk; 111596fd7ce5SGreg Kroah-Hartman 111696fd7ce5SGreg Kroah-Hartman ret = tty_write_lock(tty, file->f_flags & O_NDELAY); 111796fd7ce5SGreg Kroah-Hartman if (ret < 0) 111896fd7ce5SGreg Kroah-Hartman return ret; 111996fd7ce5SGreg Kroah-Hartman 112096fd7ce5SGreg Kroah-Hartman /* 112196fd7ce5SGreg Kroah-Hartman * We chunk up writes into a temporary buffer. This 112296fd7ce5SGreg Kroah-Hartman * simplifies low-level drivers immensely, since they 112396fd7ce5SGreg Kroah-Hartman * don't have locking issues and user mode accesses. 112496fd7ce5SGreg Kroah-Hartman * 112596fd7ce5SGreg Kroah-Hartman * But if TTY_NO_WRITE_SPLIT is set, we should use a 112696fd7ce5SGreg Kroah-Hartman * big chunk-size.. 112796fd7ce5SGreg Kroah-Hartman * 112896fd7ce5SGreg Kroah-Hartman * The default chunk-size is 2kB, because the NTTY 112996fd7ce5SGreg Kroah-Hartman * layer has problems with bigger chunks. It will 113096fd7ce5SGreg Kroah-Hartman * claim to be able to handle more characters than 113196fd7ce5SGreg Kroah-Hartman * it actually does. 113296fd7ce5SGreg Kroah-Hartman * 113396fd7ce5SGreg Kroah-Hartman * FIXME: This can probably go away now except that 64K chunks 113496fd7ce5SGreg Kroah-Hartman * are too likely to fail unless switched to vmalloc... 113596fd7ce5SGreg Kroah-Hartman */ 113696fd7ce5SGreg Kroah-Hartman chunk = 2048; 113796fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) 113896fd7ce5SGreg Kroah-Hartman chunk = 65536; 113996fd7ce5SGreg Kroah-Hartman if (count < chunk) 114096fd7ce5SGreg Kroah-Hartman chunk = count; 114196fd7ce5SGreg Kroah-Hartman 114296fd7ce5SGreg Kroah-Hartman /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ 114396fd7ce5SGreg Kroah-Hartman if (tty->write_cnt < chunk) { 114496fd7ce5SGreg Kroah-Hartman unsigned char *buf_chunk; 114596fd7ce5SGreg Kroah-Hartman 114696fd7ce5SGreg Kroah-Hartman if (chunk < 1024) 114796fd7ce5SGreg Kroah-Hartman chunk = 1024; 114896fd7ce5SGreg Kroah-Hartman 114996fd7ce5SGreg Kroah-Hartman buf_chunk = kmalloc(chunk, GFP_KERNEL); 115096fd7ce5SGreg Kroah-Hartman if (!buf_chunk) { 115196fd7ce5SGreg Kroah-Hartman ret = -ENOMEM; 115296fd7ce5SGreg Kroah-Hartman goto out; 115396fd7ce5SGreg Kroah-Hartman } 115496fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 115596fd7ce5SGreg Kroah-Hartman tty->write_cnt = chunk; 115696fd7ce5SGreg Kroah-Hartman tty->write_buf = buf_chunk; 115796fd7ce5SGreg Kroah-Hartman } 115896fd7ce5SGreg Kroah-Hartman 115996fd7ce5SGreg Kroah-Hartman /* Do the write .. */ 116096fd7ce5SGreg Kroah-Hartman for (;;) { 116196fd7ce5SGreg Kroah-Hartman size_t size = count; 116296fd7ce5SGreg Kroah-Hartman if (size > chunk) 116396fd7ce5SGreg Kroah-Hartman size = chunk; 116496fd7ce5SGreg Kroah-Hartman ret = -EFAULT; 116596fd7ce5SGreg Kroah-Hartman if (copy_from_user(tty->write_buf, buf, size)) 116696fd7ce5SGreg Kroah-Hartman break; 116796fd7ce5SGreg Kroah-Hartman ret = write(tty, file, tty->write_buf, size); 116896fd7ce5SGreg Kroah-Hartman if (ret <= 0) 116996fd7ce5SGreg Kroah-Hartman break; 117096fd7ce5SGreg Kroah-Hartman written += ret; 117196fd7ce5SGreg Kroah-Hartman buf += ret; 117296fd7ce5SGreg Kroah-Hartman count -= ret; 117396fd7ce5SGreg Kroah-Hartman if (!count) 117496fd7ce5SGreg Kroah-Hartman break; 117596fd7ce5SGreg Kroah-Hartman ret = -ERESTARTSYS; 117696fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 117796fd7ce5SGreg Kroah-Hartman break; 117896fd7ce5SGreg Kroah-Hartman cond_resched(); 117996fd7ce5SGreg Kroah-Hartman } 118037b7f3c7SJiri Slaby if (written) { 118137b7f3c7SJiri Slaby tty_update_time(&file_inode(file)->i_mtime); 118296fd7ce5SGreg Kroah-Hartman ret = written; 118337b7f3c7SJiri Slaby } 118496fd7ce5SGreg Kroah-Hartman out: 118596fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 118696fd7ce5SGreg Kroah-Hartman return ret; 118796fd7ce5SGreg Kroah-Hartman } 118896fd7ce5SGreg Kroah-Hartman 118996fd7ce5SGreg Kroah-Hartman /** 119096fd7ce5SGreg Kroah-Hartman * tty_write_message - write a message to a certain tty, not just the console. 119196fd7ce5SGreg Kroah-Hartman * @tty: the destination tty_struct 119296fd7ce5SGreg Kroah-Hartman * @msg: the message to write 119396fd7ce5SGreg Kroah-Hartman * 119496fd7ce5SGreg Kroah-Hartman * This is used for messages that need to be redirected to a specific tty. 119596fd7ce5SGreg Kroah-Hartman * We don't put it into the syslog queue right now maybe in the future if 119696fd7ce5SGreg Kroah-Hartman * really needed. 119796fd7ce5SGreg Kroah-Hartman * 119896fd7ce5SGreg Kroah-Hartman * We must still hold the BTM and test the CLOSING flag for the moment. 119996fd7ce5SGreg Kroah-Hartman */ 120096fd7ce5SGreg Kroah-Hartman 120196fd7ce5SGreg Kroah-Hartman void tty_write_message(struct tty_struct *tty, char *msg) 120296fd7ce5SGreg Kroah-Hartman { 120396fd7ce5SGreg Kroah-Hartman if (tty) { 120496fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->atomic_write_lock); 120589c8d91eSAlan Cox tty_lock(tty); 120696fd7ce5SGreg Kroah-Hartman if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { 120789c8d91eSAlan Cox tty_unlock(tty); 120896fd7ce5SGreg Kroah-Hartman tty->ops->write(tty, msg, strlen(msg)); 120996fd7ce5SGreg Kroah-Hartman } else 121089c8d91eSAlan Cox tty_unlock(tty); 121196fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 121296fd7ce5SGreg Kroah-Hartman } 121396fd7ce5SGreg Kroah-Hartman return; 121496fd7ce5SGreg Kroah-Hartman } 121596fd7ce5SGreg Kroah-Hartman 121696fd7ce5SGreg Kroah-Hartman 121796fd7ce5SGreg Kroah-Hartman /** 121896fd7ce5SGreg Kroah-Hartman * tty_write - write method for tty device file 121996fd7ce5SGreg Kroah-Hartman * @file: tty file pointer 122096fd7ce5SGreg Kroah-Hartman * @buf: user data to write 122196fd7ce5SGreg Kroah-Hartman * @count: bytes to write 122296fd7ce5SGreg Kroah-Hartman * @ppos: unused 122396fd7ce5SGreg Kroah-Hartman * 122496fd7ce5SGreg Kroah-Hartman * Write data to a tty device via the line discipline. 122596fd7ce5SGreg Kroah-Hartman * 122696fd7ce5SGreg Kroah-Hartman * Locking: 122796fd7ce5SGreg Kroah-Hartman * Locks the line discipline as required 122896fd7ce5SGreg Kroah-Hartman * Writes to the tty driver are serialized by the atomic_write_lock 122996fd7ce5SGreg Kroah-Hartman * and are then processed in chunks to the device. The line discipline 123096fd7ce5SGreg Kroah-Hartman * write method will not be invoked in parallel for each device. 123196fd7ce5SGreg Kroah-Hartman */ 123296fd7ce5SGreg Kroah-Hartman 123396fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *file, const char __user *buf, 123496fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 123596fd7ce5SGreg Kroah-Hartman { 123696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 123796fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 123896fd7ce5SGreg Kroah-Hartman ssize_t ret; 123996fd7ce5SGreg Kroah-Hartman 12406131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_write")) 124196fd7ce5SGreg Kroah-Hartman return -EIO; 124296fd7ce5SGreg Kroah-Hartman if (!tty || !tty->ops->write || 124396fd7ce5SGreg Kroah-Hartman (test_bit(TTY_IO_ERROR, &tty->flags))) 124496fd7ce5SGreg Kroah-Hartman return -EIO; 124596fd7ce5SGreg Kroah-Hartman /* Short term debug to catch buggy drivers */ 124696fd7ce5SGreg Kroah-Hartman if (tty->ops->write_room == NULL) 124796fd7ce5SGreg Kroah-Hartman printk(KERN_ERR "tty driver %s lacks a write_room method.\n", 124896fd7ce5SGreg Kroah-Hartman tty->driver->name); 124996fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 125096fd7ce5SGreg Kroah-Hartman if (!ld->ops->write) 125196fd7ce5SGreg Kroah-Hartman ret = -EIO; 125296fd7ce5SGreg Kroah-Hartman else 125396fd7ce5SGreg Kroah-Hartman ret = do_tty_write(ld->ops->write, tty, file, buf, count); 125496fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 125596fd7ce5SGreg Kroah-Hartman return ret; 125696fd7ce5SGreg Kroah-Hartman } 125796fd7ce5SGreg Kroah-Hartman 125896fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *file, const char __user *buf, 125996fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 126096fd7ce5SGreg Kroah-Hartman { 126196fd7ce5SGreg Kroah-Hartman struct file *p = NULL; 126296fd7ce5SGreg Kroah-Hartman 126396fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 1264cb0942b8SAl Viro if (redirect) 1265cb0942b8SAl Viro p = get_file(redirect); 126696fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 126796fd7ce5SGreg Kroah-Hartman 126896fd7ce5SGreg Kroah-Hartman if (p) { 126996fd7ce5SGreg Kroah-Hartman ssize_t res; 127096fd7ce5SGreg Kroah-Hartman res = vfs_write(p, buf, count, &p->f_pos); 127196fd7ce5SGreg Kroah-Hartman fput(p); 127296fd7ce5SGreg Kroah-Hartman return res; 127396fd7ce5SGreg Kroah-Hartman } 127496fd7ce5SGreg Kroah-Hartman return tty_write(file, buf, count, ppos); 127596fd7ce5SGreg Kroah-Hartman } 127696fd7ce5SGreg Kroah-Hartman 1277136d5258SPeter Hurley /** 1278136d5258SPeter Hurley * tty_send_xchar - send priority character 1279136d5258SPeter Hurley * 1280136d5258SPeter Hurley * Send a high priority character to the tty even if stopped 1281136d5258SPeter Hurley * 1282136d5258SPeter Hurley * Locking: none for xchar method, write ordering for write method. 1283136d5258SPeter Hurley */ 1284136d5258SPeter Hurley 1285136d5258SPeter Hurley int tty_send_xchar(struct tty_struct *tty, char ch) 1286136d5258SPeter Hurley { 1287136d5258SPeter Hurley int was_stopped = tty->stopped; 1288136d5258SPeter Hurley 1289136d5258SPeter Hurley if (tty->ops->send_xchar) { 1290136d5258SPeter Hurley tty->ops->send_xchar(tty, ch); 1291136d5258SPeter Hurley return 0; 1292136d5258SPeter Hurley } 1293136d5258SPeter Hurley 1294136d5258SPeter Hurley if (tty_write_lock(tty, 0) < 0) 1295136d5258SPeter Hurley return -ERESTARTSYS; 1296136d5258SPeter Hurley 1297136d5258SPeter Hurley if (was_stopped) 1298136d5258SPeter Hurley start_tty(tty); 1299136d5258SPeter Hurley tty->ops->write(tty, &ch, 1); 1300136d5258SPeter Hurley if (was_stopped) 1301136d5258SPeter Hurley stop_tty(tty); 1302136d5258SPeter Hurley tty_write_unlock(tty); 1303136d5258SPeter Hurley return 0; 1304136d5258SPeter Hurley } 1305136d5258SPeter Hurley 130696fd7ce5SGreg Kroah-Hartman static char ptychar[] = "pqrstuvwxyzabcde"; 130796fd7ce5SGreg Kroah-Hartman 130896fd7ce5SGreg Kroah-Hartman /** 130996fd7ce5SGreg Kroah-Hartman * pty_line_name - generate name for a pty 131096fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 131196fd7ce5SGreg Kroah-Hartman * @index: the minor number 131296fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 6 bytes 131396fd7ce5SGreg Kroah-Hartman * 131496fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 131596fd7ce5SGreg Kroah-Hartman * buffer. 131696fd7ce5SGreg Kroah-Hartman * 131796fd7ce5SGreg Kroah-Hartman * Locking: None 131896fd7ce5SGreg Kroah-Hartman */ 131996fd7ce5SGreg Kroah-Hartman static void pty_line_name(struct tty_driver *driver, int index, char *p) 132096fd7ce5SGreg Kroah-Hartman { 132196fd7ce5SGreg Kroah-Hartman int i = index + driver->name_base; 132296fd7ce5SGreg Kroah-Hartman /* ->name is initialized to "ttyp", but "tty" is expected */ 132396fd7ce5SGreg Kroah-Hartman sprintf(p, "%s%c%x", 132496fd7ce5SGreg Kroah-Hartman driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, 132596fd7ce5SGreg Kroah-Hartman ptychar[i >> 4 & 0xf], i & 0xf); 132696fd7ce5SGreg Kroah-Hartman } 132796fd7ce5SGreg Kroah-Hartman 132896fd7ce5SGreg Kroah-Hartman /** 132996fd7ce5SGreg Kroah-Hartman * tty_line_name - generate name for a tty 133096fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 133196fd7ce5SGreg Kroah-Hartman * @index: the minor number 133296fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 7 bytes 133396fd7ce5SGreg Kroah-Hartman * 133496fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 13355c0a2450SGreg Kroah-Hartman * buffer. 133696fd7ce5SGreg Kroah-Hartman * 133796fd7ce5SGreg Kroah-Hartman * Locking: None 133896fd7ce5SGreg Kroah-Hartman */ 1339723abd87SHannes Reinecke static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p) 134096fd7ce5SGreg Kroah-Hartman { 13410019b408SJiri Slaby if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE) 1342723abd87SHannes Reinecke return sprintf(p, "%s", driver->name); 13430019b408SJiri Slaby else 1344723abd87SHannes Reinecke return sprintf(p, "%s%d", driver->name, 1345723abd87SHannes Reinecke index + driver->name_base); 134696fd7ce5SGreg Kroah-Hartman } 134796fd7ce5SGreg Kroah-Hartman 134896fd7ce5SGreg Kroah-Hartman /** 134996fd7ce5SGreg Kroah-Hartman * tty_driver_lookup_tty() - find an existing tty, if any 135096fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 135196fd7ce5SGreg Kroah-Hartman * @idx: the minor number 135296fd7ce5SGreg Kroah-Hartman * 135396fd7ce5SGreg Kroah-Hartman * Return the tty, if found or ERR_PTR() otherwise. 135496fd7ce5SGreg Kroah-Hartman * 135596fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex must be held. If tty is found, the mutex must 135696fd7ce5SGreg Kroah-Hartman * be held until the 'fast-open' is also done. Will change once we 135796fd7ce5SGreg Kroah-Hartman * have refcounting in the driver and per driver locking 135896fd7ce5SGreg Kroah-Hartman */ 135996fd7ce5SGreg Kroah-Hartman static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, 136096fd7ce5SGreg Kroah-Hartman struct inode *inode, int idx) 136196fd7ce5SGreg Kroah-Hartman { 136296fd7ce5SGreg Kroah-Hartman if (driver->ops->lookup) 136396fd7ce5SGreg Kroah-Hartman return driver->ops->lookup(driver, inode, idx); 136496fd7ce5SGreg Kroah-Hartman 1365d4834267SJiri Slaby return driver->ttys[idx]; 136696fd7ce5SGreg Kroah-Hartman } 136796fd7ce5SGreg Kroah-Hartman 136896fd7ce5SGreg Kroah-Hartman /** 136996fd7ce5SGreg Kroah-Hartman * tty_init_termios - helper for termios setup 137096fd7ce5SGreg Kroah-Hartman * @tty: the tty to set up 137196fd7ce5SGreg Kroah-Hartman * 137296fd7ce5SGreg Kroah-Hartman * Initialise the termios structures for this tty. Thus runs under 137396fd7ce5SGreg Kroah-Hartman * the tty_mutex currently so we can be relaxed about ordering. 137496fd7ce5SGreg Kroah-Hartman */ 137596fd7ce5SGreg Kroah-Hartman 137696fd7ce5SGreg Kroah-Hartman int tty_init_termios(struct tty_struct *tty) 137796fd7ce5SGreg Kroah-Hartman { 137896fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 137996fd7ce5SGreg Kroah-Hartman int idx = tty->index; 138096fd7ce5SGreg Kroah-Hartman 138136b3c070SAlan Cox if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 138236b3c070SAlan Cox tty->termios = tty->driver->init_termios; 138336b3c070SAlan Cox else { 138436b3c070SAlan Cox /* Check for lazy saved data */ 138596fd7ce5SGreg Kroah-Hartman tp = tty->driver->termios[idx]; 138636b3c070SAlan Cox if (tp != NULL) 1387adc8d746SAlan Cox tty->termios = *tp; 138836b3c070SAlan Cox else 138936b3c070SAlan Cox tty->termios = tty->driver->init_termios; 139036b3c070SAlan Cox } 139196fd7ce5SGreg Kroah-Hartman /* Compatibility until drivers always set this */ 1392adc8d746SAlan Cox tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 1393adc8d746SAlan Cox tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 139496fd7ce5SGreg Kroah-Hartman return 0; 139596fd7ce5SGreg Kroah-Hartman } 139696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_init_termios); 139796fd7ce5SGreg Kroah-Hartman 139866d450e8SJiri Slaby int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) 139966d450e8SJiri Slaby { 140066d450e8SJiri Slaby int ret = tty_init_termios(tty); 140166d450e8SJiri Slaby if (ret) 140266d450e8SJiri Slaby return ret; 140366d450e8SJiri Slaby 140466d450e8SJiri Slaby tty_driver_kref_get(driver); 140566d450e8SJiri Slaby tty->count++; 140666d450e8SJiri Slaby driver->ttys[tty->index] = tty; 140766d450e8SJiri Slaby return 0; 140866d450e8SJiri Slaby } 140966d450e8SJiri Slaby EXPORT_SYMBOL_GPL(tty_standard_install); 141066d450e8SJiri Slaby 141196fd7ce5SGreg Kroah-Hartman /** 141296fd7ce5SGreg Kroah-Hartman * tty_driver_install_tty() - install a tty entry in the driver 141396fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 141496fd7ce5SGreg Kroah-Hartman * @tty: the tty 141596fd7ce5SGreg Kroah-Hartman * 141696fd7ce5SGreg Kroah-Hartman * Install a tty object into the driver tables. The tty->index field 141796fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. This method is responsible 141896fd7ce5SGreg Kroah-Hartman * for ensuring any need additional structures are allocated and 141996fd7ce5SGreg Kroah-Hartman * configured. 142096fd7ce5SGreg Kroah-Hartman * 142196fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 142296fd7ce5SGreg Kroah-Hartman */ 142396fd7ce5SGreg Kroah-Hartman static int tty_driver_install_tty(struct tty_driver *driver, 142496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty) 142596fd7ce5SGreg Kroah-Hartman { 142666d450e8SJiri Slaby return driver->ops->install ? driver->ops->install(driver, tty) : 142766d450e8SJiri Slaby tty_standard_install(driver, tty); 142896fd7ce5SGreg Kroah-Hartman } 142996fd7ce5SGreg Kroah-Hartman 143096fd7ce5SGreg Kroah-Hartman /** 143196fd7ce5SGreg Kroah-Hartman * tty_driver_remove_tty() - remove a tty from the driver tables 143296fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 143396fd7ce5SGreg Kroah-Hartman * @idx: the minor number 143496fd7ce5SGreg Kroah-Hartman * 143596fd7ce5SGreg Kroah-Hartman * Remvoe a tty object from the driver tables. The tty->index field 143696fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. 143796fd7ce5SGreg Kroah-Hartman * 143896fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 143996fd7ce5SGreg Kroah-Hartman */ 144024d406a6SJiri Slaby void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) 144196fd7ce5SGreg Kroah-Hartman { 144296fd7ce5SGreg Kroah-Hartman if (driver->ops->remove) 144396fd7ce5SGreg Kroah-Hartman driver->ops->remove(driver, tty); 144496fd7ce5SGreg Kroah-Hartman else 144596fd7ce5SGreg Kroah-Hartman driver->ttys[tty->index] = NULL; 144696fd7ce5SGreg Kroah-Hartman } 144796fd7ce5SGreg Kroah-Hartman 144896fd7ce5SGreg Kroah-Hartman /* 144996fd7ce5SGreg Kroah-Hartman * tty_reopen() - fast re-open of an open tty 145096fd7ce5SGreg Kroah-Hartman * @tty - the tty to open 145196fd7ce5SGreg Kroah-Hartman * 145296fd7ce5SGreg Kroah-Hartman * Return 0 on success, -errno on error. 145396fd7ce5SGreg Kroah-Hartman * 145496fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex must be held from the time the tty was found 145596fd7ce5SGreg Kroah-Hartman * till this open completes. 145696fd7ce5SGreg Kroah-Hartman */ 145796fd7ce5SGreg Kroah-Hartman static int tty_reopen(struct tty_struct *tty) 145896fd7ce5SGreg Kroah-Hartman { 145996fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 146096fd7ce5SGreg Kroah-Hartman 1461e2efafbfSJiri Slaby if (test_bit(TTY_CLOSING, &tty->flags) || 146236697529SPeter Hurley test_bit(TTY_HUPPING, &tty->flags)) 146396fd7ce5SGreg Kroah-Hartman return -EIO; 146496fd7ce5SGreg Kroah-Hartman 146596fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY && 146696fd7ce5SGreg Kroah-Hartman driver->subtype == PTY_TYPE_MASTER) { 146796fd7ce5SGreg Kroah-Hartman /* 146896fd7ce5SGreg Kroah-Hartman * special case for PTY masters: only one open permitted, 146996fd7ce5SGreg Kroah-Hartman * and the slave side open count is incremented as well. 147096fd7ce5SGreg Kroah-Hartman */ 147196fd7ce5SGreg Kroah-Hartman if (tty->count) 147296fd7ce5SGreg Kroah-Hartman return -EIO; 147396fd7ce5SGreg Kroah-Hartman 147496fd7ce5SGreg Kroah-Hartman tty->link->count++; 147596fd7ce5SGreg Kroah-Hartman } 147696fd7ce5SGreg Kroah-Hartman tty->count++; 147796fd7ce5SGreg Kroah-Hartman 147836697529SPeter Hurley WARN_ON(!tty->ldisc); 147996fd7ce5SGreg Kroah-Hartman 148096fd7ce5SGreg Kroah-Hartman return 0; 148196fd7ce5SGreg Kroah-Hartman } 148296fd7ce5SGreg Kroah-Hartman 148396fd7ce5SGreg Kroah-Hartman /** 148496fd7ce5SGreg Kroah-Hartman * tty_init_dev - initialise a tty device 148596fd7ce5SGreg Kroah-Hartman * @driver: tty driver we are opening a device on 148696fd7ce5SGreg Kroah-Hartman * @idx: device index 148796fd7ce5SGreg Kroah-Hartman * @ret_tty: returned tty structure 148896fd7ce5SGreg Kroah-Hartman * 148996fd7ce5SGreg Kroah-Hartman * Prepare a tty device. This may not be a "new" clean device but 149096fd7ce5SGreg Kroah-Hartman * could also be an active device. The pty drivers require special 149196fd7ce5SGreg Kroah-Hartman * handling because of this. 149296fd7ce5SGreg Kroah-Hartman * 149396fd7ce5SGreg Kroah-Hartman * Locking: 149496fd7ce5SGreg Kroah-Hartman * The function is called under the tty_mutex, which 149596fd7ce5SGreg Kroah-Hartman * protects us from the tty struct or driver itself going away. 149696fd7ce5SGreg Kroah-Hartman * 149796fd7ce5SGreg Kroah-Hartman * On exit the tty device has the line discipline attached and 149896fd7ce5SGreg Kroah-Hartman * a reference count of 1. If a pair was created for pty/tty use 149996fd7ce5SGreg Kroah-Hartman * and the other was a pty master then it too has a reference count of 1. 150096fd7ce5SGreg Kroah-Hartman * 150196fd7ce5SGreg Kroah-Hartman * WSH 06/09/97: Rewritten to remove races and properly clean up after a 150296fd7ce5SGreg Kroah-Hartman * failed open. The new code protects the open with a mutex, so it's 150396fd7ce5SGreg Kroah-Hartman * really quite straightforward. The mutex locking can probably be 150496fd7ce5SGreg Kroah-Hartman * relaxed for the (most common) case of reopening a tty. 150596fd7ce5SGreg Kroah-Hartman */ 150696fd7ce5SGreg Kroah-Hartman 1507593a27c4SKonstantin Khlebnikov struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx) 150896fd7ce5SGreg Kroah-Hartman { 150996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 151096fd7ce5SGreg Kroah-Hartman int retval; 151196fd7ce5SGreg Kroah-Hartman 151296fd7ce5SGreg Kroah-Hartman /* 151396fd7ce5SGreg Kroah-Hartman * First time open is complex, especially for PTY devices. 151496fd7ce5SGreg Kroah-Hartman * This code guarantees that either everything succeeds and the 151596fd7ce5SGreg Kroah-Hartman * TTY is ready for operation, or else the table slots are vacated 151696fd7ce5SGreg Kroah-Hartman * and the allocated memory released. (Except that the termios 151796fd7ce5SGreg Kroah-Hartman * and locked termios may be retained.) 151896fd7ce5SGreg Kroah-Hartman */ 151996fd7ce5SGreg Kroah-Hartman 152096fd7ce5SGreg Kroah-Hartman if (!try_module_get(driver->owner)) 152196fd7ce5SGreg Kroah-Hartman return ERR_PTR(-ENODEV); 152296fd7ce5SGreg Kroah-Hartman 15232c964a2fSRasmus Villemoes tty = alloc_tty_struct(driver, idx); 1524d5543503SJiri Slaby if (!tty) { 1525d5543503SJiri Slaby retval = -ENOMEM; 1526d5543503SJiri Slaby goto err_module_put; 1527d5543503SJiri Slaby } 152896fd7ce5SGreg Kroah-Hartman 152989c8d91eSAlan Cox tty_lock(tty); 153096fd7ce5SGreg Kroah-Hartman retval = tty_driver_install_tty(driver, tty); 1531d5543503SJiri Slaby if (retval < 0) 1532a9dccddbSJiri Slaby goto err_deinit_tty; 153396fd7ce5SGreg Kroah-Hartman 153404831dc1SJiri Slaby if (!tty->port) 153504831dc1SJiri Slaby tty->port = driver->ports[idx]; 153604831dc1SJiri Slaby 15375d4121c0SJiri Slaby WARN_RATELIMIT(!tty->port, 15385d4121c0SJiri Slaby "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n", 15395d4121c0SJiri Slaby __func__, tty->driver->name); 15405d4121c0SJiri Slaby 1541967fab69SJiri Slaby tty->port->itty = tty; 1542967fab69SJiri Slaby 154396fd7ce5SGreg Kroah-Hartman /* 154496fd7ce5SGreg Kroah-Hartman * Structures all installed ... call the ldisc open routines. 154596fd7ce5SGreg Kroah-Hartman * If we fail here just call release_tty to clean up. No need 154696fd7ce5SGreg Kroah-Hartman * to decrement the use counts, as release_tty doesn't care. 154796fd7ce5SGreg Kroah-Hartman */ 154896fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_setup(tty, tty->link); 154996fd7ce5SGreg Kroah-Hartman if (retval) 1550d5543503SJiri Slaby goto err_release_tty; 155189c8d91eSAlan Cox /* Return the tty locked so that it cannot vanish under the caller */ 155296fd7ce5SGreg Kroah-Hartman return tty; 155396fd7ce5SGreg Kroah-Hartman 1554a9dccddbSJiri Slaby err_deinit_tty: 155589c8d91eSAlan Cox tty_unlock(tty); 1556a9dccddbSJiri Slaby deinitialize_tty_struct(tty); 1557d5543503SJiri Slaby free_tty_struct(tty); 1558d5543503SJiri Slaby err_module_put: 155996fd7ce5SGreg Kroah-Hartman module_put(driver->owner); 1560d5543503SJiri Slaby return ERR_PTR(retval); 156196fd7ce5SGreg Kroah-Hartman 156296fd7ce5SGreg Kroah-Hartman /* call the tty release_tty routine to clean out this slot */ 1563d5543503SJiri Slaby err_release_tty: 156489c8d91eSAlan Cox tty_unlock(tty); 15655a3c6b25SManuel Zerpies printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, " 156696fd7ce5SGreg Kroah-Hartman "clearing slot %d\n", idx); 156796fd7ce5SGreg Kroah-Hartman release_tty(tty, idx); 156896fd7ce5SGreg Kroah-Hartman return ERR_PTR(retval); 156996fd7ce5SGreg Kroah-Hartman } 157096fd7ce5SGreg Kroah-Hartman 157196fd7ce5SGreg Kroah-Hartman void tty_free_termios(struct tty_struct *tty) 157296fd7ce5SGreg Kroah-Hartman { 157396fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 157496fd7ce5SGreg Kroah-Hartman int idx = tty->index; 157536b3c070SAlan Cox 157636b3c070SAlan Cox /* If the port is going to reset then it has no termios to save */ 157736b3c070SAlan Cox if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 157836b3c070SAlan Cox return; 157936b3c070SAlan Cox 158036b3c070SAlan Cox /* Stash the termios data */ 1581adc8d746SAlan Cox tp = tty->driver->termios[idx]; 158236b3c070SAlan Cox if (tp == NULL) { 158336b3c070SAlan Cox tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL); 158436b3c070SAlan Cox if (tp == NULL) { 158536b3c070SAlan Cox pr_warn("tty: no memory to save termios state.\n"); 158636b3c070SAlan Cox return; 158796fd7ce5SGreg Kroah-Hartman } 15884ac5d705SDan Carpenter tty->driver->termios[idx] = tp; 158936b3c070SAlan Cox } 159036b3c070SAlan Cox *tp = tty->termios; 159196fd7ce5SGreg Kroah-Hartman } 159296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_free_termios); 159396fd7ce5SGreg Kroah-Hartman 1594a2965b7bSPeter Hurley /** 1595a2965b7bSPeter Hurley * tty_flush_works - flush all works of a tty 1596a2965b7bSPeter Hurley * @tty: tty device to flush works for 1597a2965b7bSPeter Hurley * 1598a2965b7bSPeter Hurley * Sync flush all works belonging to @tty. 1599a2965b7bSPeter Hurley */ 1600a2965b7bSPeter Hurley static void tty_flush_works(struct tty_struct *tty) 1601a2965b7bSPeter Hurley { 1602a2965b7bSPeter Hurley flush_work(&tty->SAK_work); 1603a2965b7bSPeter Hurley flush_work(&tty->hangup_work); 1604a2965b7bSPeter Hurley } 160596fd7ce5SGreg Kroah-Hartman 160696fd7ce5SGreg Kroah-Hartman /** 160796fd7ce5SGreg Kroah-Hartman * release_one_tty - release tty structure memory 160896fd7ce5SGreg Kroah-Hartman * @kref: kref of tty we are obliterating 160996fd7ce5SGreg Kroah-Hartman * 161096fd7ce5SGreg Kroah-Hartman * Releases memory associated with a tty structure, and clears out the 161196fd7ce5SGreg Kroah-Hartman * driver table slots. This function is called when a device is no longer 161296fd7ce5SGreg Kroah-Hartman * in use. It also gets called when setup of a device fails. 161396fd7ce5SGreg Kroah-Hartman * 161496fd7ce5SGreg Kroah-Hartman * Locking: 161596fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 161696fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 161796fd7ce5SGreg Kroah-Hartman * 161896fd7ce5SGreg Kroah-Hartman * This method gets called from a work queue so that the driver private 161996fd7ce5SGreg Kroah-Hartman * cleanup ops can sleep (needed for USB at least) 162096fd7ce5SGreg Kroah-Hartman */ 162196fd7ce5SGreg Kroah-Hartman static void release_one_tty(struct work_struct *work) 162296fd7ce5SGreg Kroah-Hartman { 162396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 162496fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 162596fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 1626b216df53SCyrill Gorcunov struct module *owner = driver->owner; 162796fd7ce5SGreg Kroah-Hartman 162896fd7ce5SGreg Kroah-Hartman if (tty->ops->cleanup) 162996fd7ce5SGreg Kroah-Hartman tty->ops->cleanup(tty); 163096fd7ce5SGreg Kroah-Hartman 163196fd7ce5SGreg Kroah-Hartman tty->magic = 0; 163296fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(driver); 1633b216df53SCyrill Gorcunov module_put(owner); 163496fd7ce5SGreg Kroah-Hartman 163596fd7ce5SGreg Kroah-Hartman spin_lock(&tty_files_lock); 163696fd7ce5SGreg Kroah-Hartman list_del_init(&tty->tty_files); 163796fd7ce5SGreg Kroah-Hartman spin_unlock(&tty_files_lock); 163896fd7ce5SGreg Kroah-Hartman 163996fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 164096fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 164196fd7ce5SGreg Kroah-Hartman free_tty_struct(tty); 164296fd7ce5SGreg Kroah-Hartman } 164396fd7ce5SGreg Kroah-Hartman 164496fd7ce5SGreg Kroah-Hartman static void queue_release_one_tty(struct kref *kref) 164596fd7ce5SGreg Kroah-Hartman { 164696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = container_of(kref, struct tty_struct, kref); 164796fd7ce5SGreg Kroah-Hartman 164896fd7ce5SGreg Kroah-Hartman /* The hangup queue is now free so we can reuse it rather than 164996fd7ce5SGreg Kroah-Hartman waste a chunk of memory for each port */ 165096fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, release_one_tty); 165196fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 165296fd7ce5SGreg Kroah-Hartman } 165396fd7ce5SGreg Kroah-Hartman 165496fd7ce5SGreg Kroah-Hartman /** 165596fd7ce5SGreg Kroah-Hartman * tty_kref_put - release a tty kref 165696fd7ce5SGreg Kroah-Hartman * @tty: tty device 165796fd7ce5SGreg Kroah-Hartman * 165896fd7ce5SGreg Kroah-Hartman * Release a reference to a tty device and if need be let the kref 165996fd7ce5SGreg Kroah-Hartman * layer destruct the object for us 166096fd7ce5SGreg Kroah-Hartman */ 166196fd7ce5SGreg Kroah-Hartman 166296fd7ce5SGreg Kroah-Hartman void tty_kref_put(struct tty_struct *tty) 166396fd7ce5SGreg Kroah-Hartman { 166496fd7ce5SGreg Kroah-Hartman if (tty) 166596fd7ce5SGreg Kroah-Hartman kref_put(&tty->kref, queue_release_one_tty); 166696fd7ce5SGreg Kroah-Hartman } 166796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_kref_put); 166896fd7ce5SGreg Kroah-Hartman 166996fd7ce5SGreg Kroah-Hartman /** 167096fd7ce5SGreg Kroah-Hartman * release_tty - release tty structure memory 167196fd7ce5SGreg Kroah-Hartman * 167296fd7ce5SGreg Kroah-Hartman * Release both @tty and a possible linked partner (think pty pair), 167396fd7ce5SGreg Kroah-Hartman * and decrement the refcount of the backing module. 167496fd7ce5SGreg Kroah-Hartman * 167596fd7ce5SGreg Kroah-Hartman * Locking: 1676d155255aSAlan Cox * tty_mutex 167796fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 167896fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 167996fd7ce5SGreg Kroah-Hartman * 168096fd7ce5SGreg Kroah-Hartman */ 168196fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx) 168296fd7ce5SGreg Kroah-Hartman { 168396fd7ce5SGreg Kroah-Hartman /* This should always be true but check for the moment */ 168496fd7ce5SGreg Kroah-Hartman WARN_ON(tty->index != idx); 1685d155255aSAlan Cox WARN_ON(!mutex_is_locked(&tty_mutex)); 168636b3c070SAlan Cox if (tty->ops->shutdown) 168736b3c070SAlan Cox tty->ops->shutdown(tty); 168836b3c070SAlan Cox tty_free_termios(tty); 168936b3c070SAlan Cox tty_driver_remove_tty(tty->driver, tty); 1690967fab69SJiri Slaby tty->port->itty = NULL; 169164e377dcSPeter Hurley if (tty->link) 169264e377dcSPeter Hurley tty->link->port->itty = NULL; 16934f98d467SPeter Hurley cancel_work_sync(&tty->port->buf.work); 169436b3c070SAlan Cox 169596fd7ce5SGreg Kroah-Hartman if (tty->link) 169696fd7ce5SGreg Kroah-Hartman tty_kref_put(tty->link); 169796fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 169896fd7ce5SGreg Kroah-Hartman } 169996fd7ce5SGreg Kroah-Hartman 170096fd7ce5SGreg Kroah-Hartman /** 1701955787caSJiri Slaby * tty_release_checks - check a tty before real release 1702955787caSJiri Slaby * @tty: tty to check 1703955787caSJiri Slaby * @o_tty: link of @tty (if any) 1704955787caSJiri Slaby * @idx: index of the tty 1705955787caSJiri Slaby * 1706955787caSJiri Slaby * Performs some paranoid checking before true release of the @tty. 1707955787caSJiri Slaby * This is a no-op unless TTY_PARANOIA_CHECK is defined. 1708955787caSJiri Slaby */ 1709955787caSJiri Slaby static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty, 1710955787caSJiri Slaby int idx) 1711955787caSJiri Slaby { 1712955787caSJiri Slaby #ifdef TTY_PARANOIA_CHECK 1713955787caSJiri Slaby if (idx < 0 || idx >= tty->driver->num) { 17149de44bd6SJiri Slaby printk(KERN_DEBUG "%s: bad idx when trying to free (%s)\n", 17159de44bd6SJiri Slaby __func__, tty->name); 1716955787caSJiri Slaby return -1; 1717955787caSJiri Slaby } 1718955787caSJiri Slaby 1719955787caSJiri Slaby /* not much to check for devpts */ 1720955787caSJiri Slaby if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) 1721955787caSJiri Slaby return 0; 1722955787caSJiri Slaby 1723955787caSJiri Slaby if (tty != tty->driver->ttys[idx]) { 17249de44bd6SJiri Slaby printk(KERN_DEBUG "%s: driver.table[%d] not tty for (%s)\n", 17259de44bd6SJiri Slaby __func__, idx, tty->name); 1726955787caSJiri Slaby return -1; 1727955787caSJiri Slaby } 1728955787caSJiri Slaby if (tty->driver->other) { 1729955787caSJiri Slaby if (o_tty != tty->driver->other->ttys[idx]) { 17309de44bd6SJiri Slaby printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n", 17319de44bd6SJiri Slaby __func__, idx, tty->name); 1732955787caSJiri Slaby return -1; 1733955787caSJiri Slaby } 1734955787caSJiri Slaby if (o_tty->link != tty) { 17359de44bd6SJiri Slaby printk(KERN_DEBUG "%s: bad pty pointers\n", __func__); 1736955787caSJiri Slaby return -1; 1737955787caSJiri Slaby } 1738955787caSJiri Slaby } 1739955787caSJiri Slaby #endif 1740955787caSJiri Slaby return 0; 1741955787caSJiri Slaby } 1742955787caSJiri Slaby 1743955787caSJiri Slaby /** 174496fd7ce5SGreg Kroah-Hartman * tty_release - vfs callback for close 174596fd7ce5SGreg Kroah-Hartman * @inode: inode of tty 174696fd7ce5SGreg Kroah-Hartman * @filp: file pointer for handle to tty 174796fd7ce5SGreg Kroah-Hartman * 174896fd7ce5SGreg Kroah-Hartman * Called the last time each file handle is closed that references 174996fd7ce5SGreg Kroah-Hartman * this tty. There may however be several such references. 175096fd7ce5SGreg Kroah-Hartman * 175196fd7ce5SGreg Kroah-Hartman * Locking: 175296fd7ce5SGreg Kroah-Hartman * Takes bkl. See tty_release_dev 175396fd7ce5SGreg Kroah-Hartman * 175496fd7ce5SGreg Kroah-Hartman * Even releasing the tty structures is a tricky business.. We have 175596fd7ce5SGreg Kroah-Hartman * to be very careful that the structures are all released at the 175696fd7ce5SGreg Kroah-Hartman * same time, as interrupts might otherwise get the wrong pointers. 175796fd7ce5SGreg Kroah-Hartman * 175896fd7ce5SGreg Kroah-Hartman * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 175996fd7ce5SGreg Kroah-Hartman * lead to double frees or releasing memory still in use. 176096fd7ce5SGreg Kroah-Hartman */ 176196fd7ce5SGreg Kroah-Hartman 176296fd7ce5SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp) 176396fd7ce5SGreg Kroah-Hartman { 176496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 176596fd7ce5SGreg Kroah-Hartman struct tty_struct *o_tty; 176696fd7ce5SGreg Kroah-Hartman int pty_master, tty_closing, o_tty_closing, do_sleep; 176796fd7ce5SGreg Kroah-Hartman int idx; 176896fd7ce5SGreg Kroah-Hartman char buf[64]; 176996fd7ce5SGreg Kroah-Hartman 17709de44bd6SJiri Slaby if (tty_paranoia_check(tty, inode, __func__)) 177196fd7ce5SGreg Kroah-Hartman return 0; 177296fd7ce5SGreg Kroah-Hartman 177389c8d91eSAlan Cox tty_lock(tty); 17749de44bd6SJiri Slaby check_tty_count(tty, __func__); 177596fd7ce5SGreg Kroah-Hartman 177696fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); 177796fd7ce5SGreg Kroah-Hartman 177896fd7ce5SGreg Kroah-Hartman idx = tty->index; 177996fd7ce5SGreg Kroah-Hartman pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && 178096fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER); 178189c8d91eSAlan Cox /* Review: parallel close */ 178296fd7ce5SGreg Kroah-Hartman o_tty = tty->link; 178396fd7ce5SGreg Kroah-Hartman 1784955787caSJiri Slaby if (tty_release_checks(tty, o_tty, idx)) { 178589c8d91eSAlan Cox tty_unlock(tty); 178696fd7ce5SGreg Kroah-Hartman return 0; 178796fd7ce5SGreg Kroah-Hartman } 178896fd7ce5SGreg Kroah-Hartman 178996fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 17909de44bd6SJiri Slaby printk(KERN_DEBUG "%s: %s (tty count=%d)...\n", __func__, 179196fd7ce5SGreg Kroah-Hartman tty_name(tty, buf), tty->count); 179296fd7ce5SGreg Kroah-Hartman #endif 179396fd7ce5SGreg Kroah-Hartman 179496fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 179596fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, filp); 179696fd7ce5SGreg Kroah-Hartman 179789c8d91eSAlan Cox tty_unlock(tty); 179896fd7ce5SGreg Kroah-Hartman /* 179996fd7ce5SGreg Kroah-Hartman * Sanity check: if tty->count is going to zero, there shouldn't be 180096fd7ce5SGreg Kroah-Hartman * any waiters on tty->read_wait or tty->write_wait. We test the 180196fd7ce5SGreg Kroah-Hartman * wait queues and kick everyone out _before_ actually starting to 180296fd7ce5SGreg Kroah-Hartman * close. This ensures that we won't block while releasing the tty 180396fd7ce5SGreg Kroah-Hartman * structure. 180496fd7ce5SGreg Kroah-Hartman * 180596fd7ce5SGreg Kroah-Hartman * The test for the o_tty closing is necessary, since the master and 180696fd7ce5SGreg Kroah-Hartman * slave sides may close in any order. If the slave side closes out 180796fd7ce5SGreg Kroah-Hartman * first, its count will be one, since the master side holds an open. 180896fd7ce5SGreg Kroah-Hartman * Thus this test wouldn't be triggered at the time the slave closes, 180996fd7ce5SGreg Kroah-Hartman * so we do it now. 181096fd7ce5SGreg Kroah-Hartman * 181196fd7ce5SGreg Kroah-Hartman * Note that it's possible for the tty to be opened again while we're 181296fd7ce5SGreg Kroah-Hartman * flushing out waiters. By recalculating the closing flags before 181396fd7ce5SGreg Kroah-Hartman * each iteration we avoid any problems. 181496fd7ce5SGreg Kroah-Hartman */ 181596fd7ce5SGreg Kroah-Hartman while (1) { 181696fd7ce5SGreg Kroah-Hartman /* Guard against races with tty->count changes elsewhere and 181796fd7ce5SGreg Kroah-Hartman opens on /dev/tty */ 181896fd7ce5SGreg Kroah-Hartman 181996fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 182089c8d91eSAlan Cox tty_lock_pair(tty, o_tty); 182196fd7ce5SGreg Kroah-Hartman tty_closing = tty->count <= 1; 182296fd7ce5SGreg Kroah-Hartman o_tty_closing = o_tty && 182396fd7ce5SGreg Kroah-Hartman (o_tty->count <= (pty_master ? 1 : 0)); 182496fd7ce5SGreg Kroah-Hartman do_sleep = 0; 182596fd7ce5SGreg Kroah-Hartman 182696fd7ce5SGreg Kroah-Hartman if (tty_closing) { 182796fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->read_wait)) { 182896fd7ce5SGreg Kroah-Hartman wake_up_poll(&tty->read_wait, POLLIN); 182996fd7ce5SGreg Kroah-Hartman do_sleep++; 183096fd7ce5SGreg Kroah-Hartman } 183196fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->write_wait)) { 183296fd7ce5SGreg Kroah-Hartman wake_up_poll(&tty->write_wait, POLLOUT); 183396fd7ce5SGreg Kroah-Hartman do_sleep++; 183496fd7ce5SGreg Kroah-Hartman } 183596fd7ce5SGreg Kroah-Hartman } 183696fd7ce5SGreg Kroah-Hartman if (o_tty_closing) { 183796fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->read_wait)) { 183896fd7ce5SGreg Kroah-Hartman wake_up_poll(&o_tty->read_wait, POLLIN); 183996fd7ce5SGreg Kroah-Hartman do_sleep++; 184096fd7ce5SGreg Kroah-Hartman } 184196fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->write_wait)) { 184296fd7ce5SGreg Kroah-Hartman wake_up_poll(&o_tty->write_wait, POLLOUT); 184396fd7ce5SGreg Kroah-Hartman do_sleep++; 184496fd7ce5SGreg Kroah-Hartman } 184596fd7ce5SGreg Kroah-Hartman } 184696fd7ce5SGreg Kroah-Hartman if (!do_sleep) 184796fd7ce5SGreg Kroah-Hartman break; 184896fd7ce5SGreg Kroah-Hartman 18499de44bd6SJiri Slaby printk(KERN_WARNING "%s: %s: read/write wait queue active!\n", 18509de44bd6SJiri Slaby __func__, tty_name(tty, buf)); 185189c8d91eSAlan Cox tty_unlock_pair(tty, o_tty); 185296fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 185396fd7ce5SGreg Kroah-Hartman schedule(); 185496fd7ce5SGreg Kroah-Hartman } 185596fd7ce5SGreg Kroah-Hartman 185696fd7ce5SGreg Kroah-Hartman /* 185796fd7ce5SGreg Kroah-Hartman * The closing flags are now consistent with the open counts on 185896fd7ce5SGreg Kroah-Hartman * both sides, and we've completed the last operation that could 185996fd7ce5SGreg Kroah-Hartman * block, so it's safe to proceed with closing. 1860d155255aSAlan Cox * 1861d155255aSAlan Cox * We must *not* drop the tty_mutex until we ensure that a further 1862d155255aSAlan Cox * entry into tty_open can not pick up this tty. 186396fd7ce5SGreg Kroah-Hartman */ 186496fd7ce5SGreg Kroah-Hartman if (pty_master) { 186596fd7ce5SGreg Kroah-Hartman if (--o_tty->count < 0) { 18669de44bd6SJiri Slaby printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n", 18679de44bd6SJiri Slaby __func__, o_tty->count, tty_name(o_tty, buf)); 186896fd7ce5SGreg Kroah-Hartman o_tty->count = 0; 186996fd7ce5SGreg Kroah-Hartman } 187096fd7ce5SGreg Kroah-Hartman } 187196fd7ce5SGreg Kroah-Hartman if (--tty->count < 0) { 18729de44bd6SJiri Slaby printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n", 18739de44bd6SJiri Slaby __func__, tty->count, tty_name(tty, buf)); 187496fd7ce5SGreg Kroah-Hartman tty->count = 0; 187596fd7ce5SGreg Kroah-Hartman } 187696fd7ce5SGreg Kroah-Hartman 187796fd7ce5SGreg Kroah-Hartman /* 187896fd7ce5SGreg Kroah-Hartman * We've decremented tty->count, so we need to remove this file 187996fd7ce5SGreg Kroah-Hartman * descriptor off the tty->tty_files list; this serves two 188096fd7ce5SGreg Kroah-Hartman * purposes: 188196fd7ce5SGreg Kroah-Hartman * - check_tty_count sees the correct number of file descriptors 188296fd7ce5SGreg Kroah-Hartman * associated with this tty. 188396fd7ce5SGreg Kroah-Hartman * - do_tty_hangup no longer sees this file descriptor as 188496fd7ce5SGreg Kroah-Hartman * something that needs to be handled for hangups. 188596fd7ce5SGreg Kroah-Hartman */ 188696fd7ce5SGreg Kroah-Hartman tty_del_file(filp); 188796fd7ce5SGreg Kroah-Hartman 188896fd7ce5SGreg Kroah-Hartman /* 188996fd7ce5SGreg Kroah-Hartman * Perform some housekeeping before deciding whether to return. 189096fd7ce5SGreg Kroah-Hartman * 189196fd7ce5SGreg Kroah-Hartman * Set the TTY_CLOSING flag if this was the last open. In the 189296fd7ce5SGreg Kroah-Hartman * case of a pty we may have to wait around for the other side 189396fd7ce5SGreg Kroah-Hartman * to close, and TTY_CLOSING makes sure we can't be reopened. 189496fd7ce5SGreg Kroah-Hartman */ 189596fd7ce5SGreg Kroah-Hartman if (tty_closing) 189696fd7ce5SGreg Kroah-Hartman set_bit(TTY_CLOSING, &tty->flags); 189796fd7ce5SGreg Kroah-Hartman if (o_tty_closing) 189896fd7ce5SGreg Kroah-Hartman set_bit(TTY_CLOSING, &o_tty->flags); 189996fd7ce5SGreg Kroah-Hartman 190096fd7ce5SGreg Kroah-Hartman /* 190196fd7ce5SGreg Kroah-Hartman * If _either_ side is closing, make sure there aren't any 190296fd7ce5SGreg Kroah-Hartman * processes that still think tty or o_tty is their controlling 190396fd7ce5SGreg Kroah-Hartman * tty. 190496fd7ce5SGreg Kroah-Hartman */ 190596fd7ce5SGreg Kroah-Hartman if (tty_closing || o_tty_closing) { 190696fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 190796fd7ce5SGreg Kroah-Hartman session_clear_tty(tty->session); 190896fd7ce5SGreg Kroah-Hartman if (o_tty) 190996fd7ce5SGreg Kroah-Hartman session_clear_tty(o_tty->session); 191096fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 191196fd7ce5SGreg Kroah-Hartman } 191296fd7ce5SGreg Kroah-Hartman 191396fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 191489c8d91eSAlan Cox tty_unlock_pair(tty, o_tty); 1915d155255aSAlan Cox /* At this point the TTY_CLOSING flag should ensure a dead tty 1916d155255aSAlan Cox cannot be re-opened by a racing opener */ 191796fd7ce5SGreg Kroah-Hartman 191896fd7ce5SGreg Kroah-Hartman /* check whether both sides are closing ... */ 1919d155255aSAlan Cox if (!tty_closing || (o_tty && !o_tty_closing)) 192096fd7ce5SGreg Kroah-Hartman return 0; 192196fd7ce5SGreg Kroah-Hartman 192296fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 192396433d10SPeter Hurley printk(KERN_DEBUG "%s: %s: final close\n", __func__, tty_name(tty, buf)); 192496fd7ce5SGreg Kroah-Hartman #endif 192596fd7ce5SGreg Kroah-Hartman /* 192696fd7ce5SGreg Kroah-Hartman * Ask the line discipline code to release its structures 192796fd7ce5SGreg Kroah-Hartman */ 192896fd7ce5SGreg Kroah-Hartman tty_ldisc_release(tty, o_tty); 1929a2965b7bSPeter Hurley 1930a2965b7bSPeter Hurley /* Wait for pending work before tty destruction commmences */ 1931a2965b7bSPeter Hurley tty_flush_works(tty); 1932a2965b7bSPeter Hurley if (o_tty) 1933a2965b7bSPeter Hurley tty_flush_works(o_tty); 1934a2965b7bSPeter Hurley 193596433d10SPeter Hurley #ifdef TTY_DEBUG_HANGUP 193696433d10SPeter Hurley printk(KERN_DEBUG "%s: %s: freeing structure...\n", __func__, tty_name(tty, buf)); 193796433d10SPeter Hurley #endif 193896fd7ce5SGreg Kroah-Hartman /* 193996fd7ce5SGreg Kroah-Hartman * The release_tty function takes care of the details of clearing 194089c8d91eSAlan Cox * the slots and preserving the termios structure. The tty_unlock_pair 194189c8d91eSAlan Cox * should be safe as we keep a kref while the tty is locked (so the 194289c8d91eSAlan Cox * unlock never unlocks a freed tty). 194396fd7ce5SGreg Kroah-Hartman */ 1944d155255aSAlan Cox mutex_lock(&tty_mutex); 194596fd7ce5SGreg Kroah-Hartman release_tty(tty, idx); 1946d155255aSAlan Cox mutex_unlock(&tty_mutex); 194796fd7ce5SGreg Kroah-Hartman 194896fd7ce5SGreg Kroah-Hartman return 0; 194996fd7ce5SGreg Kroah-Hartman } 195096fd7ce5SGreg Kroah-Hartman 195196fd7ce5SGreg Kroah-Hartman /** 1952b82154acSJiri Slaby * tty_open_current_tty - get tty of current task for open 1953b82154acSJiri Slaby * @device: device number 1954b82154acSJiri Slaby * @filp: file pointer to tty 1955b82154acSJiri Slaby * @return: tty of the current task iff @device is /dev/tty 1956b82154acSJiri Slaby * 1957b82154acSJiri Slaby * We cannot return driver and index like for the other nodes because 1958b82154acSJiri Slaby * devpts will not work then. It expects inodes to be from devpts FS. 19593af502b9SAlan Cox * 19603af502b9SAlan Cox * We need to move to returning a refcounted object from all the lookup 19613af502b9SAlan Cox * paths including this one. 1962b82154acSJiri Slaby */ 1963b82154acSJiri Slaby static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp) 1964b82154acSJiri Slaby { 1965b82154acSJiri Slaby struct tty_struct *tty; 1966b82154acSJiri Slaby 1967b82154acSJiri Slaby if (device != MKDEV(TTYAUX_MAJOR, 0)) 1968b82154acSJiri Slaby return NULL; 1969b82154acSJiri Slaby 1970b82154acSJiri Slaby tty = get_current_tty(); 1971b82154acSJiri Slaby if (!tty) 1972b82154acSJiri Slaby return ERR_PTR(-ENXIO); 1973b82154acSJiri Slaby 1974b82154acSJiri Slaby filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ 1975b82154acSJiri Slaby /* noctty = 1; */ 1976b82154acSJiri Slaby tty_kref_put(tty); 1977b82154acSJiri Slaby /* FIXME: we put a reference and return a TTY! */ 19783af502b9SAlan Cox /* This is only safe because the caller holds tty_mutex */ 1979b82154acSJiri Slaby return tty; 1980b82154acSJiri Slaby } 1981b82154acSJiri Slaby 1982b82154acSJiri Slaby /** 19835b5e7040SJiri Slaby * tty_lookup_driver - lookup a tty driver for a given device file 19845b5e7040SJiri Slaby * @device: device number 19855b5e7040SJiri Slaby * @filp: file pointer to tty 19865b5e7040SJiri Slaby * @noctty: set if the device should not become a controlling tty 19875b5e7040SJiri Slaby * @index: index for the device in the @return driver 19885b5e7040SJiri Slaby * @return: driver for this inode (with increased refcount) 19895b5e7040SJiri Slaby * 19905b5e7040SJiri Slaby * If @return is not erroneous, the caller is responsible to decrement the 19915b5e7040SJiri Slaby * refcount by tty_driver_kref_put. 19925b5e7040SJiri Slaby * 19935b5e7040SJiri Slaby * Locking: tty_mutex protects get_tty_driver 19945b5e7040SJiri Slaby */ 19955b5e7040SJiri Slaby static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp, 19965b5e7040SJiri Slaby int *noctty, int *index) 19975b5e7040SJiri Slaby { 19985b5e7040SJiri Slaby struct tty_driver *driver; 19995b5e7040SJiri Slaby 20002cd0050cSJiri Slaby switch (device) { 20015b5e7040SJiri Slaby #ifdef CONFIG_VT 20022cd0050cSJiri Slaby case MKDEV(TTY_MAJOR, 0): { 20035b5e7040SJiri Slaby extern struct tty_driver *console_driver; 20045b5e7040SJiri Slaby driver = tty_driver_kref_get(console_driver); 20055b5e7040SJiri Slaby *index = fg_console; 20065b5e7040SJiri Slaby *noctty = 1; 20072cd0050cSJiri Slaby break; 20085b5e7040SJiri Slaby } 20095b5e7040SJiri Slaby #endif 20102cd0050cSJiri Slaby case MKDEV(TTYAUX_MAJOR, 1): { 20115b5e7040SJiri Slaby struct tty_driver *console_driver = console_device(index); 20125b5e7040SJiri Slaby if (console_driver) { 20135b5e7040SJiri Slaby driver = tty_driver_kref_get(console_driver); 20145b5e7040SJiri Slaby if (driver) { 20155b5e7040SJiri Slaby /* Don't let /dev/console block */ 20165b5e7040SJiri Slaby filp->f_flags |= O_NONBLOCK; 20175b5e7040SJiri Slaby *noctty = 1; 20182cd0050cSJiri Slaby break; 20195b5e7040SJiri Slaby } 20205b5e7040SJiri Slaby } 20215b5e7040SJiri Slaby return ERR_PTR(-ENODEV); 20225b5e7040SJiri Slaby } 20232cd0050cSJiri Slaby default: 20245b5e7040SJiri Slaby driver = get_tty_driver(device, index); 20255b5e7040SJiri Slaby if (!driver) 20265b5e7040SJiri Slaby return ERR_PTR(-ENODEV); 20272cd0050cSJiri Slaby break; 20282cd0050cSJiri Slaby } 20295b5e7040SJiri Slaby return driver; 20305b5e7040SJiri Slaby } 20315b5e7040SJiri Slaby 20325b5e7040SJiri Slaby /** 203396fd7ce5SGreg Kroah-Hartman * tty_open - open a tty device 203496fd7ce5SGreg Kroah-Hartman * @inode: inode of device file 203596fd7ce5SGreg Kroah-Hartman * @filp: file pointer to tty 203696fd7ce5SGreg Kroah-Hartman * 203796fd7ce5SGreg Kroah-Hartman * tty_open and tty_release keep up the tty count that contains the 203896fd7ce5SGreg Kroah-Hartman * number of opens done on a tty. We cannot use the inode-count, as 203996fd7ce5SGreg Kroah-Hartman * different inodes might point to the same tty. 204096fd7ce5SGreg Kroah-Hartman * 204196fd7ce5SGreg Kroah-Hartman * Open-counting is needed for pty masters, as well as for keeping 204296fd7ce5SGreg Kroah-Hartman * track of serial lines: DTR is dropped when the last close happens. 204396fd7ce5SGreg Kroah-Hartman * (This is not done solely through tty->count, now. - Ted 1/27/92) 204496fd7ce5SGreg Kroah-Hartman * 204596fd7ce5SGreg Kroah-Hartman * The termios state of a pty is reset on first open so that 204696fd7ce5SGreg Kroah-Hartman * settings don't persist across reuse. 204796fd7ce5SGreg Kroah-Hartman * 20485b5e7040SJiri Slaby * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. 204996fd7ce5SGreg Kroah-Hartman * tty->count should protect the rest. 205096fd7ce5SGreg Kroah-Hartman * ->siglock protects ->signal/->sighand 205189c8d91eSAlan Cox * 205289c8d91eSAlan Cox * Note: the tty_unlock/lock cases without a ref are only safe due to 205389c8d91eSAlan Cox * tty_mutex 205496fd7ce5SGreg Kroah-Hartman */ 205596fd7ce5SGreg Kroah-Hartman 205696fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *inode, struct file *filp) 205796fd7ce5SGreg Kroah-Hartman { 2058b82154acSJiri Slaby struct tty_struct *tty; 205996fd7ce5SGreg Kroah-Hartman int noctty, retval; 2060b82154acSJiri Slaby struct tty_driver *driver = NULL; 206196fd7ce5SGreg Kroah-Hartman int index; 206296fd7ce5SGreg Kroah-Hartman dev_t device = inode->i_rdev; 206396fd7ce5SGreg Kroah-Hartman unsigned saved_flags = filp->f_flags; 206496fd7ce5SGreg Kroah-Hartman 206596fd7ce5SGreg Kroah-Hartman nonseekable_open(inode, filp); 206696fd7ce5SGreg Kroah-Hartman 206796fd7ce5SGreg Kroah-Hartman retry_open: 2068fa90e1c9SJiri Slaby retval = tty_alloc_file(filp); 2069fa90e1c9SJiri Slaby if (retval) 2070fa90e1c9SJiri Slaby return -ENOMEM; 2071fa90e1c9SJiri Slaby 207296fd7ce5SGreg Kroah-Hartman noctty = filp->f_flags & O_NOCTTY; 207396fd7ce5SGreg Kroah-Hartman index = -1; 207496fd7ce5SGreg Kroah-Hartman retval = 0; 207596fd7ce5SGreg Kroah-Hartman 207696fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 207789c8d91eSAlan Cox /* This is protected by the tty_mutex */ 2078b82154acSJiri Slaby tty = tty_open_current_tty(device, filp); 2079b82154acSJiri Slaby if (IS_ERR(tty)) { 2080ba5db448SJiri Slaby retval = PTR_ERR(tty); 2081ba5db448SJiri Slaby goto err_unlock; 20825b5e7040SJiri Slaby } else if (!tty) { 20835b5e7040SJiri Slaby driver = tty_lookup_driver(device, filp, &noctty, &index); 20845b5e7040SJiri Slaby if (IS_ERR(driver)) { 2085ba5db448SJiri Slaby retval = PTR_ERR(driver); 2086ba5db448SJiri Slaby goto err_unlock; 208796fd7ce5SGreg Kroah-Hartman } 208896fd7ce5SGreg Kroah-Hartman 208996fd7ce5SGreg Kroah-Hartman /* check whether we're reopening an existing tty */ 209096fd7ce5SGreg Kroah-Hartman tty = tty_driver_lookup_tty(driver, inode, index); 209196fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty)) { 2092ba5db448SJiri Slaby retval = PTR_ERR(tty); 2093ba5db448SJiri Slaby goto err_unlock; 209496fd7ce5SGreg Kroah-Hartman } 209596fd7ce5SGreg Kroah-Hartman } 209696fd7ce5SGreg Kroah-Hartman 209796fd7ce5SGreg Kroah-Hartman if (tty) { 209889c8d91eSAlan Cox tty_lock(tty); 209996fd7ce5SGreg Kroah-Hartman retval = tty_reopen(tty); 210089c8d91eSAlan Cox if (retval < 0) { 210189c8d91eSAlan Cox tty_unlock(tty); 210296fd7ce5SGreg Kroah-Hartman tty = ERR_PTR(retval); 210389c8d91eSAlan Cox } 210489c8d91eSAlan Cox } else /* Returns with the tty_lock held for now */ 2105593a27c4SKonstantin Khlebnikov tty = tty_init_dev(driver, index); 210696fd7ce5SGreg Kroah-Hartman 210796fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 2108b82154acSJiri Slaby if (driver) 210996fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(driver); 211096fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty)) { 2111ba5db448SJiri Slaby retval = PTR_ERR(tty); 2112ba5db448SJiri Slaby goto err_file; 211396fd7ce5SGreg Kroah-Hartman } 211496fd7ce5SGreg Kroah-Hartman 2115fa90e1c9SJiri Slaby tty_add_file(tty, filp); 211696fd7ce5SGreg Kroah-Hartman 21179de44bd6SJiri Slaby check_tty_count(tty, __func__); 211896fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 211996fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER) 212096fd7ce5SGreg Kroah-Hartman noctty = 1; 212196fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 21229de44bd6SJiri Slaby printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name); 212396fd7ce5SGreg Kroah-Hartman #endif 212496fd7ce5SGreg Kroah-Hartman if (tty->ops->open) 212596fd7ce5SGreg Kroah-Hartman retval = tty->ops->open(tty, filp); 212696fd7ce5SGreg Kroah-Hartman else 212796fd7ce5SGreg Kroah-Hartman retval = -ENODEV; 212896fd7ce5SGreg Kroah-Hartman filp->f_flags = saved_flags; 212996fd7ce5SGreg Kroah-Hartman 213096fd7ce5SGreg Kroah-Hartman if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && 213196fd7ce5SGreg Kroah-Hartman !capable(CAP_SYS_ADMIN)) 213296fd7ce5SGreg Kroah-Hartman retval = -EBUSY; 213396fd7ce5SGreg Kroah-Hartman 213496fd7ce5SGreg Kroah-Hartman if (retval) { 213596fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP 21369de44bd6SJiri Slaby printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__, 21379de44bd6SJiri Slaby retval, tty->name); 213896fd7ce5SGreg Kroah-Hartman #endif 213989c8d91eSAlan Cox tty_unlock(tty); /* need to call tty_release without BTM */ 214096fd7ce5SGreg Kroah-Hartman tty_release(inode, filp); 214196fd7ce5SGreg Kroah-Hartman if (retval != -ERESTARTSYS) 214296fd7ce5SGreg Kroah-Hartman return retval; 214396fd7ce5SGreg Kroah-Hartman 214496fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 214596fd7ce5SGreg Kroah-Hartman return retval; 214696fd7ce5SGreg Kroah-Hartman 214796fd7ce5SGreg Kroah-Hartman schedule(); 214896fd7ce5SGreg Kroah-Hartman /* 214996fd7ce5SGreg Kroah-Hartman * Need to reset f_op in case a hangup happened. 215096fd7ce5SGreg Kroah-Hartman */ 215196fd7ce5SGreg Kroah-Hartman if (filp->f_op == &hung_up_tty_fops) 215296fd7ce5SGreg Kroah-Hartman filp->f_op = &tty_fops; 215396fd7ce5SGreg Kroah-Hartman goto retry_open; 215496fd7ce5SGreg Kroah-Hartman } 2155d4855e1fSPeter Hurley clear_bit(TTY_HUPPED, &tty->flags); 215696fd7ce5SGreg Kroah-Hartman 215796fd7ce5SGreg Kroah-Hartman 21582c411c11SPeter Hurley read_lock(&tasklist_lock); 215996fd7ce5SGreg Kroah-Hartman spin_lock_irq(¤t->sighand->siglock); 216096fd7ce5SGreg Kroah-Hartman if (!noctty && 216196fd7ce5SGreg Kroah-Hartman current->signal->leader && 216296fd7ce5SGreg Kroah-Hartman !current->signal->tty && 216396fd7ce5SGreg Kroah-Hartman tty->session == NULL) 2164bce65f18SPeter Hurley __proc_set_tty(tty); 216596fd7ce5SGreg Kroah-Hartman spin_unlock_irq(¤t->sighand->siglock); 21662c411c11SPeter Hurley read_unlock(&tasklist_lock); 216789c8d91eSAlan Cox tty_unlock(tty); 216896fd7ce5SGreg Kroah-Hartman return 0; 2169ba5db448SJiri Slaby err_unlock: 2170ba5db448SJiri Slaby mutex_unlock(&tty_mutex); 2171ba5db448SJiri Slaby /* after locks to avoid deadlock */ 2172ba5db448SJiri Slaby if (!IS_ERR_OR_NULL(driver)) 2173ba5db448SJiri Slaby tty_driver_kref_put(driver); 2174ba5db448SJiri Slaby err_file: 2175ba5db448SJiri Slaby tty_free_file(filp); 2176ba5db448SJiri Slaby return retval; 217796fd7ce5SGreg Kroah-Hartman } 217896fd7ce5SGreg Kroah-Hartman 217996fd7ce5SGreg Kroah-Hartman 218096fd7ce5SGreg Kroah-Hartman 218196fd7ce5SGreg Kroah-Hartman /** 218296fd7ce5SGreg Kroah-Hartman * tty_poll - check tty status 218396fd7ce5SGreg Kroah-Hartman * @filp: file being polled 218496fd7ce5SGreg Kroah-Hartman * @wait: poll wait structures to update 218596fd7ce5SGreg Kroah-Hartman * 218696fd7ce5SGreg Kroah-Hartman * Call the line discipline polling method to obtain the poll 218796fd7ce5SGreg Kroah-Hartman * status of the device. 218896fd7ce5SGreg Kroah-Hartman * 218996fd7ce5SGreg Kroah-Hartman * Locking: locks called line discipline but ldisc poll method 219096fd7ce5SGreg Kroah-Hartman * may be re-entered freely by other callers. 219196fd7ce5SGreg Kroah-Hartman */ 219296fd7ce5SGreg Kroah-Hartman 219396fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *filp, poll_table *wait) 219496fd7ce5SGreg Kroah-Hartman { 219596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 219696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 219796fd7ce5SGreg Kroah-Hartman int ret = 0; 219896fd7ce5SGreg Kroah-Hartman 21996131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) 220096fd7ce5SGreg Kroah-Hartman return 0; 220196fd7ce5SGreg Kroah-Hartman 220296fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 220396fd7ce5SGreg Kroah-Hartman if (ld->ops->poll) 220496fd7ce5SGreg Kroah-Hartman ret = (ld->ops->poll)(tty, filp, wait); 220596fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 220696fd7ce5SGreg Kroah-Hartman return ret; 220796fd7ce5SGreg Kroah-Hartman } 220896fd7ce5SGreg Kroah-Hartman 220996fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on) 221096fd7ce5SGreg Kroah-Hartman { 221196fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 2212f6c8dbe6SPeter Hurley struct tty_ldisc *ldisc; 221396fd7ce5SGreg Kroah-Hartman unsigned long flags; 221496fd7ce5SGreg Kroah-Hartman int retval = 0; 221596fd7ce5SGreg Kroah-Hartman 22166131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) 221796fd7ce5SGreg Kroah-Hartman goto out; 221896fd7ce5SGreg Kroah-Hartman 221996fd7ce5SGreg Kroah-Hartman retval = fasync_helper(fd, filp, on, &tty->fasync); 222096fd7ce5SGreg Kroah-Hartman if (retval <= 0) 222196fd7ce5SGreg Kroah-Hartman goto out; 222296fd7ce5SGreg Kroah-Hartman 2223f6c8dbe6SPeter Hurley ldisc = tty_ldisc_ref(tty); 2224f6c8dbe6SPeter Hurley if (ldisc) { 2225f6c8dbe6SPeter Hurley if (ldisc->ops->fasync) 2226f6c8dbe6SPeter Hurley ldisc->ops->fasync(tty, on); 2227f6c8dbe6SPeter Hurley tty_ldisc_deref(ldisc); 2228f6c8dbe6SPeter Hurley } 2229f6c8dbe6SPeter Hurley 223096fd7ce5SGreg Kroah-Hartman if (on) { 223196fd7ce5SGreg Kroah-Hartman enum pid_type type; 223296fd7ce5SGreg Kroah-Hartman struct pid *pid; 2233f6c8dbe6SPeter Hurley 223496fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 223596fd7ce5SGreg Kroah-Hartman if (tty->pgrp) { 223696fd7ce5SGreg Kroah-Hartman pid = tty->pgrp; 223796fd7ce5SGreg Kroah-Hartman type = PIDTYPE_PGID; 223896fd7ce5SGreg Kroah-Hartman } else { 223996fd7ce5SGreg Kroah-Hartman pid = task_pid(current); 224096fd7ce5SGreg Kroah-Hartman type = PIDTYPE_PID; 224196fd7ce5SGreg Kroah-Hartman } 224296fd7ce5SGreg Kroah-Hartman get_pid(pid); 224396fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 2244e0b93eddSJeff Layton __f_setown(filp, pid, type, 0); 224596fd7ce5SGreg Kroah-Hartman put_pid(pid); 2246e0b93eddSJeff Layton retval = 0; 224796fd7ce5SGreg Kroah-Hartman } 224896fd7ce5SGreg Kroah-Hartman out: 224996fd7ce5SGreg Kroah-Hartman return retval; 225096fd7ce5SGreg Kroah-Hartman } 225196fd7ce5SGreg Kroah-Hartman 225296fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on) 225396fd7ce5SGreg Kroah-Hartman { 225489c8d91eSAlan Cox struct tty_struct *tty = file_tty(filp); 225596fd7ce5SGreg Kroah-Hartman int retval; 225689c8d91eSAlan Cox 225789c8d91eSAlan Cox tty_lock(tty); 225896fd7ce5SGreg Kroah-Hartman retval = __tty_fasync(fd, filp, on); 225989c8d91eSAlan Cox tty_unlock(tty); 226089c8d91eSAlan Cox 226196fd7ce5SGreg Kroah-Hartman return retval; 226296fd7ce5SGreg Kroah-Hartman } 226396fd7ce5SGreg Kroah-Hartman 226496fd7ce5SGreg Kroah-Hartman /** 226596fd7ce5SGreg Kroah-Hartman * tiocsti - fake input character 226696fd7ce5SGreg Kroah-Hartman * @tty: tty to fake input into 226796fd7ce5SGreg Kroah-Hartman * @p: pointer to character 226896fd7ce5SGreg Kroah-Hartman * 226996fd7ce5SGreg Kroah-Hartman * Fake input to a tty device. Does the necessary locking and 227096fd7ce5SGreg Kroah-Hartman * input management. 227196fd7ce5SGreg Kroah-Hartman * 227296fd7ce5SGreg Kroah-Hartman * FIXME: does not honour flow control ?? 227396fd7ce5SGreg Kroah-Hartman * 227496fd7ce5SGreg Kroah-Hartman * Locking: 2275137084bbSPeter Hurley * Called functions take tty_ldiscs_lock 227696fd7ce5SGreg Kroah-Hartman * current->signal->tty check is safe without locks 227796fd7ce5SGreg Kroah-Hartman * 227896fd7ce5SGreg Kroah-Hartman * FIXME: may race normal receive processing 227996fd7ce5SGreg Kroah-Hartman */ 228096fd7ce5SGreg Kroah-Hartman 228196fd7ce5SGreg Kroah-Hartman static int tiocsti(struct tty_struct *tty, char __user *p) 228296fd7ce5SGreg Kroah-Hartman { 228396fd7ce5SGreg Kroah-Hartman char ch, mbz = 0; 228496fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 228596fd7ce5SGreg Kroah-Hartman 228696fd7ce5SGreg Kroah-Hartman if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) 228796fd7ce5SGreg Kroah-Hartman return -EPERM; 228896fd7ce5SGreg Kroah-Hartman if (get_user(ch, p)) 228996fd7ce5SGreg Kroah-Hartman return -EFAULT; 229096fd7ce5SGreg Kroah-Hartman tty_audit_tiocsti(tty, ch); 229196fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 229296fd7ce5SGreg Kroah-Hartman ld->ops->receive_buf(tty, &ch, &mbz, 1); 229396fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 229496fd7ce5SGreg Kroah-Hartman return 0; 229596fd7ce5SGreg Kroah-Hartman } 229696fd7ce5SGreg Kroah-Hartman 229796fd7ce5SGreg Kroah-Hartman /** 229896fd7ce5SGreg Kroah-Hartman * tiocgwinsz - implement window query ioctl 229996fd7ce5SGreg Kroah-Hartman * @tty; tty 230096fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 230196fd7ce5SGreg Kroah-Hartman * 230296fd7ce5SGreg Kroah-Hartman * Copies the kernel idea of the window size into the user buffer. 230396fd7ce5SGreg Kroah-Hartman * 2304dee4a0beSPeter Hurley * Locking: tty->winsize_mutex is taken to ensure the winsize data 230596fd7ce5SGreg Kroah-Hartman * is consistent. 230696fd7ce5SGreg Kroah-Hartman */ 230796fd7ce5SGreg Kroah-Hartman 230896fd7ce5SGreg Kroah-Hartman static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) 230996fd7ce5SGreg Kroah-Hartman { 231096fd7ce5SGreg Kroah-Hartman int err; 231196fd7ce5SGreg Kroah-Hartman 2312dee4a0beSPeter Hurley mutex_lock(&tty->winsize_mutex); 231396fd7ce5SGreg Kroah-Hartman err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); 2314dee4a0beSPeter Hurley mutex_unlock(&tty->winsize_mutex); 231596fd7ce5SGreg Kroah-Hartman 231696fd7ce5SGreg Kroah-Hartman return err ? -EFAULT: 0; 231796fd7ce5SGreg Kroah-Hartman } 231896fd7ce5SGreg Kroah-Hartman 231996fd7ce5SGreg Kroah-Hartman /** 232096fd7ce5SGreg Kroah-Hartman * tty_do_resize - resize event 232196fd7ce5SGreg Kroah-Hartman * @tty: tty being resized 232296fd7ce5SGreg Kroah-Hartman * @rows: rows (character) 232396fd7ce5SGreg Kroah-Hartman * @cols: cols (character) 232496fd7ce5SGreg Kroah-Hartman * 232596fd7ce5SGreg Kroah-Hartman * Update the termios variables and send the necessary signals to 232696fd7ce5SGreg Kroah-Hartman * peform a terminal resize correctly 232796fd7ce5SGreg Kroah-Hartman */ 232896fd7ce5SGreg Kroah-Hartman 232996fd7ce5SGreg Kroah-Hartman int tty_do_resize(struct tty_struct *tty, struct winsize *ws) 233096fd7ce5SGreg Kroah-Hartman { 233196fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 233296fd7ce5SGreg Kroah-Hartman 233396fd7ce5SGreg Kroah-Hartman /* Lock the tty */ 2334dee4a0beSPeter Hurley mutex_lock(&tty->winsize_mutex); 233596fd7ce5SGreg Kroah-Hartman if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 233696fd7ce5SGreg Kroah-Hartman goto done; 233796fd7ce5SGreg Kroah-Hartman 23385b239542SPeter Hurley /* Signal the foreground process group */ 23395b239542SPeter Hurley pgrp = tty_get_pgrp(tty); 234096fd7ce5SGreg Kroah-Hartman if (pgrp) 234196fd7ce5SGreg Kroah-Hartman kill_pgrp(pgrp, SIGWINCH, 1); 234296fd7ce5SGreg Kroah-Hartman put_pid(pgrp); 234396fd7ce5SGreg Kroah-Hartman 234496fd7ce5SGreg Kroah-Hartman tty->winsize = *ws; 234596fd7ce5SGreg Kroah-Hartman done: 2346dee4a0beSPeter Hurley mutex_unlock(&tty->winsize_mutex); 234796fd7ce5SGreg Kroah-Hartman return 0; 234896fd7ce5SGreg Kroah-Hartman } 23494d334fd1SMartin Schwidefsky EXPORT_SYMBOL(tty_do_resize); 235096fd7ce5SGreg Kroah-Hartman 235196fd7ce5SGreg Kroah-Hartman /** 235296fd7ce5SGreg Kroah-Hartman * tiocswinsz - implement window size set ioctl 235396fd7ce5SGreg Kroah-Hartman * @tty; tty side of tty 235496fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 235596fd7ce5SGreg Kroah-Hartman * 235696fd7ce5SGreg Kroah-Hartman * Copies the user idea of the window size to the kernel. Traditionally 235796fd7ce5SGreg Kroah-Hartman * this is just advisory information but for the Linux console it 235896fd7ce5SGreg Kroah-Hartman * actually has driver level meaning and triggers a VC resize. 235996fd7ce5SGreg Kroah-Hartman * 236096fd7ce5SGreg Kroah-Hartman * Locking: 236125985edcSLucas De Marchi * Driver dependent. The default do_resize method takes the 236296fd7ce5SGreg Kroah-Hartman * tty termios mutex and ctrl_lock. The console takes its own lock 236396fd7ce5SGreg Kroah-Hartman * then calls into the default method. 236496fd7ce5SGreg Kroah-Hartman */ 236596fd7ce5SGreg Kroah-Hartman 236696fd7ce5SGreg Kroah-Hartman static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) 236796fd7ce5SGreg Kroah-Hartman { 236896fd7ce5SGreg Kroah-Hartman struct winsize tmp_ws; 236996fd7ce5SGreg Kroah-Hartman if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) 237096fd7ce5SGreg Kroah-Hartman return -EFAULT; 237196fd7ce5SGreg Kroah-Hartman 237296fd7ce5SGreg Kroah-Hartman if (tty->ops->resize) 237396fd7ce5SGreg Kroah-Hartman return tty->ops->resize(tty, &tmp_ws); 237496fd7ce5SGreg Kroah-Hartman else 237596fd7ce5SGreg Kroah-Hartman return tty_do_resize(tty, &tmp_ws); 237696fd7ce5SGreg Kroah-Hartman } 237796fd7ce5SGreg Kroah-Hartman 237896fd7ce5SGreg Kroah-Hartman /** 237996fd7ce5SGreg Kroah-Hartman * tioccons - allow admin to move logical console 238096fd7ce5SGreg Kroah-Hartman * @file: the file to become console 238196fd7ce5SGreg Kroah-Hartman * 238225985edcSLucas De Marchi * Allow the administrator to move the redirected console device 238396fd7ce5SGreg Kroah-Hartman * 238496fd7ce5SGreg Kroah-Hartman * Locking: uses redirect_lock to guard the redirect information 238596fd7ce5SGreg Kroah-Hartman */ 238696fd7ce5SGreg Kroah-Hartman 238796fd7ce5SGreg Kroah-Hartman static int tioccons(struct file *file) 238896fd7ce5SGreg Kroah-Hartman { 238996fd7ce5SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) 239096fd7ce5SGreg Kroah-Hartman return -EPERM; 239196fd7ce5SGreg Kroah-Hartman if (file->f_op->write == redirected_tty_write) { 239296fd7ce5SGreg Kroah-Hartman struct file *f; 239396fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 239496fd7ce5SGreg Kroah-Hartman f = redirect; 239596fd7ce5SGreg Kroah-Hartman redirect = NULL; 239696fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 239796fd7ce5SGreg Kroah-Hartman if (f) 239896fd7ce5SGreg Kroah-Hartman fput(f); 239996fd7ce5SGreg Kroah-Hartman return 0; 240096fd7ce5SGreg Kroah-Hartman } 240196fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 240296fd7ce5SGreg Kroah-Hartman if (redirect) { 240396fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 240496fd7ce5SGreg Kroah-Hartman return -EBUSY; 240596fd7ce5SGreg Kroah-Hartman } 2406cb0942b8SAl Viro redirect = get_file(file); 240796fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 240896fd7ce5SGreg Kroah-Hartman return 0; 240996fd7ce5SGreg Kroah-Hartman } 241096fd7ce5SGreg Kroah-Hartman 241196fd7ce5SGreg Kroah-Hartman /** 241296fd7ce5SGreg Kroah-Hartman * fionbio - non blocking ioctl 241396fd7ce5SGreg Kroah-Hartman * @file: file to set blocking value 241496fd7ce5SGreg Kroah-Hartman * @p: user parameter 241596fd7ce5SGreg Kroah-Hartman * 241696fd7ce5SGreg Kroah-Hartman * Historical tty interfaces had a blocking control ioctl before 241796fd7ce5SGreg Kroah-Hartman * the generic functionality existed. This piece of history is preserved 241896fd7ce5SGreg Kroah-Hartman * in the expected tty API of posix OS's. 241996fd7ce5SGreg Kroah-Hartman * 242096fd7ce5SGreg Kroah-Hartman * Locking: none, the open file handle ensures it won't go away. 242196fd7ce5SGreg Kroah-Hartman */ 242296fd7ce5SGreg Kroah-Hartman 242396fd7ce5SGreg Kroah-Hartman static int fionbio(struct file *file, int __user *p) 242496fd7ce5SGreg Kroah-Hartman { 242596fd7ce5SGreg Kroah-Hartman int nonblock; 242696fd7ce5SGreg Kroah-Hartman 242796fd7ce5SGreg Kroah-Hartman if (get_user(nonblock, p)) 242896fd7ce5SGreg Kroah-Hartman return -EFAULT; 242996fd7ce5SGreg Kroah-Hartman 243096fd7ce5SGreg Kroah-Hartman spin_lock(&file->f_lock); 243196fd7ce5SGreg Kroah-Hartman if (nonblock) 243296fd7ce5SGreg Kroah-Hartman file->f_flags |= O_NONBLOCK; 243396fd7ce5SGreg Kroah-Hartman else 243496fd7ce5SGreg Kroah-Hartman file->f_flags &= ~O_NONBLOCK; 243596fd7ce5SGreg Kroah-Hartman spin_unlock(&file->f_lock); 243696fd7ce5SGreg Kroah-Hartman return 0; 243796fd7ce5SGreg Kroah-Hartman } 243896fd7ce5SGreg Kroah-Hartman 243996fd7ce5SGreg Kroah-Hartman /** 244096fd7ce5SGreg Kroah-Hartman * tiocsctty - set controlling tty 244196fd7ce5SGreg Kroah-Hartman * @tty: tty structure 244296fd7ce5SGreg Kroah-Hartman * @arg: user argument 244396fd7ce5SGreg Kroah-Hartman * 244496fd7ce5SGreg Kroah-Hartman * This ioctl is used to manage job control. It permits a session 244596fd7ce5SGreg Kroah-Hartman * leader to set this tty as the controlling tty for the session. 244696fd7ce5SGreg Kroah-Hartman * 244796fd7ce5SGreg Kroah-Hartman * Locking: 2448e218eb32SPeter Hurley * Takes tty_lock() to serialize proc_set_tty() for this tty 244996fd7ce5SGreg Kroah-Hartman * Takes tasklist_lock internally to walk sessions 245096fd7ce5SGreg Kroah-Hartman * Takes ->siglock() when updating signal->tty 245196fd7ce5SGreg Kroah-Hartman */ 245296fd7ce5SGreg Kroah-Hartman 245396fd7ce5SGreg Kroah-Hartman static int tiocsctty(struct tty_struct *tty, int arg) 245496fd7ce5SGreg Kroah-Hartman { 245596fd7ce5SGreg Kroah-Hartman int ret = 0; 245696fd7ce5SGreg Kroah-Hartman 2457e218eb32SPeter Hurley tty_lock(tty); 24582c411c11SPeter Hurley read_lock(&tasklist_lock); 24592c411c11SPeter Hurley 24602c411c11SPeter Hurley if (current->signal->leader && (task_session(current) == tty->session)) 24612c411c11SPeter Hurley goto unlock; 24622c411c11SPeter Hurley 246396fd7ce5SGreg Kroah-Hartman /* 246496fd7ce5SGreg Kroah-Hartman * The process must be a session leader and 246596fd7ce5SGreg Kroah-Hartman * not have a controlling tty already. 246696fd7ce5SGreg Kroah-Hartman */ 246796fd7ce5SGreg Kroah-Hartman if (!current->signal->leader || current->signal->tty) { 246896fd7ce5SGreg Kroah-Hartman ret = -EPERM; 246996fd7ce5SGreg Kroah-Hartman goto unlock; 247096fd7ce5SGreg Kroah-Hartman } 247196fd7ce5SGreg Kroah-Hartman 247296fd7ce5SGreg Kroah-Hartman if (tty->session) { 247396fd7ce5SGreg Kroah-Hartman /* 247496fd7ce5SGreg Kroah-Hartman * This tty is already the controlling 247596fd7ce5SGreg Kroah-Hartman * tty for another session group! 247696fd7ce5SGreg Kroah-Hartman */ 247796fd7ce5SGreg Kroah-Hartman if (arg == 1 && capable(CAP_SYS_ADMIN)) { 247896fd7ce5SGreg Kroah-Hartman /* 247996fd7ce5SGreg Kroah-Hartman * Steal it away 248096fd7ce5SGreg Kroah-Hartman */ 248196fd7ce5SGreg Kroah-Hartman session_clear_tty(tty->session); 248296fd7ce5SGreg Kroah-Hartman } else { 248396fd7ce5SGreg Kroah-Hartman ret = -EPERM; 248496fd7ce5SGreg Kroah-Hartman goto unlock; 248596fd7ce5SGreg Kroah-Hartman } 248696fd7ce5SGreg Kroah-Hartman } 2487bce65f18SPeter Hurley proc_set_tty(tty); 248896fd7ce5SGreg Kroah-Hartman unlock: 24892c411c11SPeter Hurley read_unlock(&tasklist_lock); 2490e218eb32SPeter Hurley tty_unlock(tty); 249196fd7ce5SGreg Kroah-Hartman return ret; 249296fd7ce5SGreg Kroah-Hartman } 249396fd7ce5SGreg Kroah-Hartman 249496fd7ce5SGreg Kroah-Hartman /** 249596fd7ce5SGreg Kroah-Hartman * tty_get_pgrp - return a ref counted pgrp pid 249696fd7ce5SGreg Kroah-Hartman * @tty: tty to read 249796fd7ce5SGreg Kroah-Hartman * 249896fd7ce5SGreg Kroah-Hartman * Returns a refcounted instance of the pid struct for the process 249996fd7ce5SGreg Kroah-Hartman * group controlling the tty. 250096fd7ce5SGreg Kroah-Hartman */ 250196fd7ce5SGreg Kroah-Hartman 250296fd7ce5SGreg Kroah-Hartman struct pid *tty_get_pgrp(struct tty_struct *tty) 250396fd7ce5SGreg Kroah-Hartman { 250496fd7ce5SGreg Kroah-Hartman unsigned long flags; 250596fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 250696fd7ce5SGreg Kroah-Hartman 250796fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 250896fd7ce5SGreg Kroah-Hartman pgrp = get_pid(tty->pgrp); 250996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 251096fd7ce5SGreg Kroah-Hartman 251196fd7ce5SGreg Kroah-Hartman return pgrp; 251296fd7ce5SGreg Kroah-Hartman } 251396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_get_pgrp); 251496fd7ce5SGreg Kroah-Hartman 2515e1c2296cSPeter Hurley /* 2516e1c2296cSPeter Hurley * This checks not only the pgrp, but falls back on the pid if no 2517e1c2296cSPeter Hurley * satisfactory pgrp is found. I dunno - gdb doesn't work correctly 2518e1c2296cSPeter Hurley * without this... 2519e1c2296cSPeter Hurley * 2520e1c2296cSPeter Hurley * The caller must hold rcu lock or the tasklist lock. 2521e1c2296cSPeter Hurley */ 2522e1c2296cSPeter Hurley static struct pid *session_of_pgrp(struct pid *pgrp) 2523e1c2296cSPeter Hurley { 2524e1c2296cSPeter Hurley struct task_struct *p; 2525e1c2296cSPeter Hurley struct pid *sid = NULL; 2526e1c2296cSPeter Hurley 2527e1c2296cSPeter Hurley p = pid_task(pgrp, PIDTYPE_PGID); 2528e1c2296cSPeter Hurley if (p == NULL) 2529e1c2296cSPeter Hurley p = pid_task(pgrp, PIDTYPE_PID); 2530e1c2296cSPeter Hurley if (p != NULL) 2531e1c2296cSPeter Hurley sid = task_session(p); 2532e1c2296cSPeter Hurley 2533e1c2296cSPeter Hurley return sid; 2534e1c2296cSPeter Hurley } 2535e1c2296cSPeter Hurley 253696fd7ce5SGreg Kroah-Hartman /** 253796fd7ce5SGreg Kroah-Hartman * tiocgpgrp - get process group 253896fd7ce5SGreg Kroah-Hartman * @tty: tty passed by user 253925985edcSLucas De Marchi * @real_tty: tty side of the tty passed by the user if a pty else the tty 254096fd7ce5SGreg Kroah-Hartman * @p: returned pid 254196fd7ce5SGreg Kroah-Hartman * 254296fd7ce5SGreg Kroah-Hartman * Obtain the process group of the tty. If there is no process group 254396fd7ce5SGreg Kroah-Hartman * return an error. 254496fd7ce5SGreg Kroah-Hartman * 254596fd7ce5SGreg Kroah-Hartman * Locking: none. Reference to current->signal->tty is safe. 254696fd7ce5SGreg Kroah-Hartman */ 254796fd7ce5SGreg Kroah-Hartman 254896fd7ce5SGreg Kroah-Hartman static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 254996fd7ce5SGreg Kroah-Hartman { 255096fd7ce5SGreg Kroah-Hartman struct pid *pid; 255196fd7ce5SGreg Kroah-Hartman int ret; 255296fd7ce5SGreg Kroah-Hartman /* 255396fd7ce5SGreg Kroah-Hartman * (tty == real_tty) is a cheap way of 255496fd7ce5SGreg Kroah-Hartman * testing if the tty is NOT a master pty. 255596fd7ce5SGreg Kroah-Hartman */ 255696fd7ce5SGreg Kroah-Hartman if (tty == real_tty && current->signal->tty != real_tty) 255796fd7ce5SGreg Kroah-Hartman return -ENOTTY; 255896fd7ce5SGreg Kroah-Hartman pid = tty_get_pgrp(real_tty); 255996fd7ce5SGreg Kroah-Hartman ret = put_user(pid_vnr(pid), p); 256096fd7ce5SGreg Kroah-Hartman put_pid(pid); 256196fd7ce5SGreg Kroah-Hartman return ret; 256296fd7ce5SGreg Kroah-Hartman } 256396fd7ce5SGreg Kroah-Hartman 256496fd7ce5SGreg Kroah-Hartman /** 256596fd7ce5SGreg Kroah-Hartman * tiocspgrp - attempt to set process group 256696fd7ce5SGreg Kroah-Hartman * @tty: tty passed by user 256796fd7ce5SGreg Kroah-Hartman * @real_tty: tty side device matching tty passed by user 256896fd7ce5SGreg Kroah-Hartman * @p: pid pointer 256996fd7ce5SGreg Kroah-Hartman * 257096fd7ce5SGreg Kroah-Hartman * Set the process group of the tty to the session passed. Only 257196fd7ce5SGreg Kroah-Hartman * permitted where the tty session is our session. 257296fd7ce5SGreg Kroah-Hartman * 257396fd7ce5SGreg Kroah-Hartman * Locking: RCU, ctrl lock 257496fd7ce5SGreg Kroah-Hartman */ 257596fd7ce5SGreg Kroah-Hartman 257696fd7ce5SGreg Kroah-Hartman static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 257796fd7ce5SGreg Kroah-Hartman { 257896fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 257996fd7ce5SGreg Kroah-Hartman pid_t pgrp_nr; 258096fd7ce5SGreg Kroah-Hartman int retval = tty_check_change(real_tty); 258196fd7ce5SGreg Kroah-Hartman unsigned long flags; 258296fd7ce5SGreg Kroah-Hartman 258396fd7ce5SGreg Kroah-Hartman if (retval == -EIO) 258496fd7ce5SGreg Kroah-Hartman return -ENOTTY; 258596fd7ce5SGreg Kroah-Hartman if (retval) 258696fd7ce5SGreg Kroah-Hartman return retval; 258796fd7ce5SGreg Kroah-Hartman if (!current->signal->tty || 258896fd7ce5SGreg Kroah-Hartman (current->signal->tty != real_tty) || 258996fd7ce5SGreg Kroah-Hartman (real_tty->session != task_session(current))) 259096fd7ce5SGreg Kroah-Hartman return -ENOTTY; 259196fd7ce5SGreg Kroah-Hartman if (get_user(pgrp_nr, p)) 259296fd7ce5SGreg Kroah-Hartman return -EFAULT; 259396fd7ce5SGreg Kroah-Hartman if (pgrp_nr < 0) 259496fd7ce5SGreg Kroah-Hartman return -EINVAL; 259596fd7ce5SGreg Kroah-Hartman rcu_read_lock(); 259696fd7ce5SGreg Kroah-Hartman pgrp = find_vpid(pgrp_nr); 259796fd7ce5SGreg Kroah-Hartman retval = -ESRCH; 259896fd7ce5SGreg Kroah-Hartman if (!pgrp) 259996fd7ce5SGreg Kroah-Hartman goto out_unlock; 260096fd7ce5SGreg Kroah-Hartman retval = -EPERM; 260196fd7ce5SGreg Kroah-Hartman if (session_of_pgrp(pgrp) != task_session(current)) 260296fd7ce5SGreg Kroah-Hartman goto out_unlock; 260396fd7ce5SGreg Kroah-Hartman retval = 0; 260496fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 260596fd7ce5SGreg Kroah-Hartman put_pid(real_tty->pgrp); 260696fd7ce5SGreg Kroah-Hartman real_tty->pgrp = get_pid(pgrp); 260796fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 260896fd7ce5SGreg Kroah-Hartman out_unlock: 260996fd7ce5SGreg Kroah-Hartman rcu_read_unlock(); 261096fd7ce5SGreg Kroah-Hartman return retval; 261196fd7ce5SGreg Kroah-Hartman } 261296fd7ce5SGreg Kroah-Hartman 261396fd7ce5SGreg Kroah-Hartman /** 261496fd7ce5SGreg Kroah-Hartman * tiocgsid - get session id 261596fd7ce5SGreg Kroah-Hartman * @tty: tty passed by user 261625985edcSLucas De Marchi * @real_tty: tty side of the tty passed by the user if a pty else the tty 261796fd7ce5SGreg Kroah-Hartman * @p: pointer to returned session id 261896fd7ce5SGreg Kroah-Hartman * 261996fd7ce5SGreg Kroah-Hartman * Obtain the session id of the tty. If there is no session 262096fd7ce5SGreg Kroah-Hartman * return an error. 262196fd7ce5SGreg Kroah-Hartman * 262296fd7ce5SGreg Kroah-Hartman * Locking: none. Reference to current->signal->tty is safe. 262396fd7ce5SGreg Kroah-Hartman */ 262496fd7ce5SGreg Kroah-Hartman 262596fd7ce5SGreg Kroah-Hartman static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 262696fd7ce5SGreg Kroah-Hartman { 262796fd7ce5SGreg Kroah-Hartman /* 262896fd7ce5SGreg Kroah-Hartman * (tty == real_tty) is a cheap way of 262996fd7ce5SGreg Kroah-Hartman * testing if the tty is NOT a master pty. 263096fd7ce5SGreg Kroah-Hartman */ 263196fd7ce5SGreg Kroah-Hartman if (tty == real_tty && current->signal->tty != real_tty) 263296fd7ce5SGreg Kroah-Hartman return -ENOTTY; 263396fd7ce5SGreg Kroah-Hartman if (!real_tty->session) 263496fd7ce5SGreg Kroah-Hartman return -ENOTTY; 263596fd7ce5SGreg Kroah-Hartman return put_user(pid_vnr(real_tty->session), p); 263696fd7ce5SGreg Kroah-Hartman } 263796fd7ce5SGreg Kroah-Hartman 263896fd7ce5SGreg Kroah-Hartman /** 263996fd7ce5SGreg Kroah-Hartman * tiocsetd - set line discipline 264096fd7ce5SGreg Kroah-Hartman * @tty: tty device 264196fd7ce5SGreg Kroah-Hartman * @p: pointer to user data 264296fd7ce5SGreg Kroah-Hartman * 264396fd7ce5SGreg Kroah-Hartman * Set the line discipline according to user request. 264496fd7ce5SGreg Kroah-Hartman * 264596fd7ce5SGreg Kroah-Hartman * Locking: see tty_set_ldisc, this function is just a helper 264696fd7ce5SGreg Kroah-Hartman */ 264796fd7ce5SGreg Kroah-Hartman 264896fd7ce5SGreg Kroah-Hartman static int tiocsetd(struct tty_struct *tty, int __user *p) 264996fd7ce5SGreg Kroah-Hartman { 265096fd7ce5SGreg Kroah-Hartman int ldisc; 265196fd7ce5SGreg Kroah-Hartman int ret; 265296fd7ce5SGreg Kroah-Hartman 265396fd7ce5SGreg Kroah-Hartman if (get_user(ldisc, p)) 265496fd7ce5SGreg Kroah-Hartman return -EFAULT; 265596fd7ce5SGreg Kroah-Hartman 265696fd7ce5SGreg Kroah-Hartman ret = tty_set_ldisc(tty, ldisc); 265796fd7ce5SGreg Kroah-Hartman 265896fd7ce5SGreg Kroah-Hartman return ret; 265996fd7ce5SGreg Kroah-Hartman } 266096fd7ce5SGreg Kroah-Hartman 266196fd7ce5SGreg Kroah-Hartman /** 266296fd7ce5SGreg Kroah-Hartman * send_break - performed time break 266396fd7ce5SGreg Kroah-Hartman * @tty: device to break on 266496fd7ce5SGreg Kroah-Hartman * @duration: timeout in mS 266596fd7ce5SGreg Kroah-Hartman * 266696fd7ce5SGreg Kroah-Hartman * Perform a timed break on hardware that lacks its own driver level 266796fd7ce5SGreg Kroah-Hartman * timed break functionality. 266896fd7ce5SGreg Kroah-Hartman * 266996fd7ce5SGreg Kroah-Hartman * Locking: 267096fd7ce5SGreg Kroah-Hartman * atomic_write_lock serializes 267196fd7ce5SGreg Kroah-Hartman * 267296fd7ce5SGreg Kroah-Hartman */ 267396fd7ce5SGreg Kroah-Hartman 267496fd7ce5SGreg Kroah-Hartman static int send_break(struct tty_struct *tty, unsigned int duration) 267596fd7ce5SGreg Kroah-Hartman { 267696fd7ce5SGreg Kroah-Hartman int retval; 267796fd7ce5SGreg Kroah-Hartman 267896fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl == NULL) 267996fd7ce5SGreg Kroah-Hartman return 0; 268096fd7ce5SGreg Kroah-Hartman 268196fd7ce5SGreg Kroah-Hartman if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) 268296fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, duration); 268396fd7ce5SGreg Kroah-Hartman else { 268496fd7ce5SGreg Kroah-Hartman /* Do the work ourselves */ 268596fd7ce5SGreg Kroah-Hartman if (tty_write_lock(tty, 0) < 0) 268696fd7ce5SGreg Kroah-Hartman return -EINTR; 268796fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, -1); 268896fd7ce5SGreg Kroah-Hartman if (retval) 268996fd7ce5SGreg Kroah-Hartman goto out; 269096fd7ce5SGreg Kroah-Hartman if (!signal_pending(current)) 269196fd7ce5SGreg Kroah-Hartman msleep_interruptible(duration); 269296fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, 0); 269396fd7ce5SGreg Kroah-Hartman out: 269496fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 269596fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 269696fd7ce5SGreg Kroah-Hartman retval = -EINTR; 269796fd7ce5SGreg Kroah-Hartman } 269896fd7ce5SGreg Kroah-Hartman return retval; 269996fd7ce5SGreg Kroah-Hartman } 270096fd7ce5SGreg Kroah-Hartman 270196fd7ce5SGreg Kroah-Hartman /** 270296fd7ce5SGreg Kroah-Hartman * tty_tiocmget - get modem status 270396fd7ce5SGreg Kroah-Hartman * @tty: tty device 270496fd7ce5SGreg Kroah-Hartman * @file: user file pointer 270596fd7ce5SGreg Kroah-Hartman * @p: pointer to result 270696fd7ce5SGreg Kroah-Hartman * 270796fd7ce5SGreg Kroah-Hartman * Obtain the modem status bits from the tty driver if the feature 270896fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 270996fd7ce5SGreg Kroah-Hartman * 271096fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 271196fd7ce5SGreg Kroah-Hartman */ 271296fd7ce5SGreg Kroah-Hartman 271360b33c13SAlan Cox static int tty_tiocmget(struct tty_struct *tty, int __user *p) 271496fd7ce5SGreg Kroah-Hartman { 271596fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 271696fd7ce5SGreg Kroah-Hartman 271796fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmget) { 271860b33c13SAlan Cox retval = tty->ops->tiocmget(tty); 271996fd7ce5SGreg Kroah-Hartman 272096fd7ce5SGreg Kroah-Hartman if (retval >= 0) 272196fd7ce5SGreg Kroah-Hartman retval = put_user(retval, p); 272296fd7ce5SGreg Kroah-Hartman } 272396fd7ce5SGreg Kroah-Hartman return retval; 272496fd7ce5SGreg Kroah-Hartman } 272596fd7ce5SGreg Kroah-Hartman 272696fd7ce5SGreg Kroah-Hartman /** 272796fd7ce5SGreg Kroah-Hartman * tty_tiocmset - set modem status 272896fd7ce5SGreg Kroah-Hartman * @tty: tty device 272996fd7ce5SGreg Kroah-Hartman * @cmd: command - clear bits, set bits or set all 273096fd7ce5SGreg Kroah-Hartman * @p: pointer to desired bits 273196fd7ce5SGreg Kroah-Hartman * 273296fd7ce5SGreg Kroah-Hartman * Set the modem status bits from the tty driver if the feature 273396fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 273496fd7ce5SGreg Kroah-Hartman * 273596fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 273696fd7ce5SGreg Kroah-Hartman */ 273796fd7ce5SGreg Kroah-Hartman 273820b9d177SAlan Cox static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, 273996fd7ce5SGreg Kroah-Hartman unsigned __user *p) 274096fd7ce5SGreg Kroah-Hartman { 274196fd7ce5SGreg Kroah-Hartman int retval; 274296fd7ce5SGreg Kroah-Hartman unsigned int set, clear, val; 274396fd7ce5SGreg Kroah-Hartman 274496fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmset == NULL) 274596fd7ce5SGreg Kroah-Hartman return -EINVAL; 274696fd7ce5SGreg Kroah-Hartman 274796fd7ce5SGreg Kroah-Hartman retval = get_user(val, p); 274896fd7ce5SGreg Kroah-Hartman if (retval) 274996fd7ce5SGreg Kroah-Hartman return retval; 275096fd7ce5SGreg Kroah-Hartman set = clear = 0; 275196fd7ce5SGreg Kroah-Hartman switch (cmd) { 275296fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 275396fd7ce5SGreg Kroah-Hartman set = val; 275496fd7ce5SGreg Kroah-Hartman break; 275596fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 275696fd7ce5SGreg Kroah-Hartman clear = val; 275796fd7ce5SGreg Kroah-Hartman break; 275896fd7ce5SGreg Kroah-Hartman case TIOCMSET: 275996fd7ce5SGreg Kroah-Hartman set = val; 276096fd7ce5SGreg Kroah-Hartman clear = ~val; 276196fd7ce5SGreg Kroah-Hartman break; 276296fd7ce5SGreg Kroah-Hartman } 276396fd7ce5SGreg Kroah-Hartman set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 276496fd7ce5SGreg Kroah-Hartman clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 276520b9d177SAlan Cox return tty->ops->tiocmset(tty, set, clear); 276696fd7ce5SGreg Kroah-Hartman } 276796fd7ce5SGreg Kroah-Hartman 276896fd7ce5SGreg Kroah-Hartman static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) 276996fd7ce5SGreg Kroah-Hartman { 277096fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 277196fd7ce5SGreg Kroah-Hartman struct serial_icounter_struct icount; 277296fd7ce5SGreg Kroah-Hartman memset(&icount, 0, sizeof(icount)); 277396fd7ce5SGreg Kroah-Hartman if (tty->ops->get_icount) 277496fd7ce5SGreg Kroah-Hartman retval = tty->ops->get_icount(tty, &icount); 277596fd7ce5SGreg Kroah-Hartman if (retval != 0) 277696fd7ce5SGreg Kroah-Hartman return retval; 277796fd7ce5SGreg Kroah-Hartman if (copy_to_user(arg, &icount, sizeof(icount))) 277896fd7ce5SGreg Kroah-Hartman return -EFAULT; 277996fd7ce5SGreg Kroah-Hartman return 0; 278096fd7ce5SGreg Kroah-Hartman } 278196fd7ce5SGreg Kroah-Hartman 27828f166e00SPeter Hurley /* 27838f166e00SPeter Hurley * if pty, return the slave side (real_tty) 27848f166e00SPeter Hurley * otherwise, return self 27858f166e00SPeter Hurley */ 27868f166e00SPeter Hurley static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) 278796fd7ce5SGreg Kroah-Hartman { 278896fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 278996fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER) 279096fd7ce5SGreg Kroah-Hartman tty = tty->link; 279196fd7ce5SGreg Kroah-Hartman return tty; 279296fd7ce5SGreg Kroah-Hartman } 279396fd7ce5SGreg Kroah-Hartman 279496fd7ce5SGreg Kroah-Hartman /* 279596fd7ce5SGreg Kroah-Hartman * Split this up, as gcc can choke on it otherwise.. 279696fd7ce5SGreg Kroah-Hartman */ 279796fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 279896fd7ce5SGreg Kroah-Hartman { 279996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 280096fd7ce5SGreg Kroah-Hartman struct tty_struct *real_tty; 280196fd7ce5SGreg Kroah-Hartman void __user *p = (void __user *)arg; 280296fd7ce5SGreg Kroah-Hartman int retval; 280396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 280496fd7ce5SGreg Kroah-Hartman 28056131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) 280696fd7ce5SGreg Kroah-Hartman return -EINVAL; 280796fd7ce5SGreg Kroah-Hartman 280896fd7ce5SGreg Kroah-Hartman real_tty = tty_pair_get_tty(tty); 280996fd7ce5SGreg Kroah-Hartman 281096fd7ce5SGreg Kroah-Hartman /* 281196fd7ce5SGreg Kroah-Hartman * Factor out some common prep work 281296fd7ce5SGreg Kroah-Hartman */ 281396fd7ce5SGreg Kroah-Hartman switch (cmd) { 281496fd7ce5SGreg Kroah-Hartman case TIOCSETD: 281596fd7ce5SGreg Kroah-Hartman case TIOCSBRK: 281696fd7ce5SGreg Kroah-Hartman case TIOCCBRK: 281796fd7ce5SGreg Kroah-Hartman case TCSBRK: 281896fd7ce5SGreg Kroah-Hartman case TCSBRKP: 281996fd7ce5SGreg Kroah-Hartman retval = tty_check_change(tty); 282096fd7ce5SGreg Kroah-Hartman if (retval) 282196fd7ce5SGreg Kroah-Hartman return retval; 282296fd7ce5SGreg Kroah-Hartman if (cmd != TIOCCBRK) { 282396fd7ce5SGreg Kroah-Hartman tty_wait_until_sent(tty, 0); 282496fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 282596fd7ce5SGreg Kroah-Hartman return -EINTR; 282696fd7ce5SGreg Kroah-Hartman } 282796fd7ce5SGreg Kroah-Hartman break; 282896fd7ce5SGreg Kroah-Hartman } 282996fd7ce5SGreg Kroah-Hartman 283096fd7ce5SGreg Kroah-Hartman /* 283196fd7ce5SGreg Kroah-Hartman * Now do the stuff. 283296fd7ce5SGreg Kroah-Hartman */ 283396fd7ce5SGreg Kroah-Hartman switch (cmd) { 283496fd7ce5SGreg Kroah-Hartman case TIOCSTI: 283596fd7ce5SGreg Kroah-Hartman return tiocsti(tty, p); 283696fd7ce5SGreg Kroah-Hartman case TIOCGWINSZ: 283796fd7ce5SGreg Kroah-Hartman return tiocgwinsz(real_tty, p); 283896fd7ce5SGreg Kroah-Hartman case TIOCSWINSZ: 283996fd7ce5SGreg Kroah-Hartman return tiocswinsz(real_tty, p); 284096fd7ce5SGreg Kroah-Hartman case TIOCCONS: 284196fd7ce5SGreg Kroah-Hartman return real_tty != tty ? -EINVAL : tioccons(file); 284296fd7ce5SGreg Kroah-Hartman case FIONBIO: 284396fd7ce5SGreg Kroah-Hartman return fionbio(file, p); 284496fd7ce5SGreg Kroah-Hartman case TIOCEXCL: 284596fd7ce5SGreg Kroah-Hartman set_bit(TTY_EXCLUSIVE, &tty->flags); 284696fd7ce5SGreg Kroah-Hartman return 0; 284796fd7ce5SGreg Kroah-Hartman case TIOCNXCL: 284896fd7ce5SGreg Kroah-Hartman clear_bit(TTY_EXCLUSIVE, &tty->flags); 284996fd7ce5SGreg Kroah-Hartman return 0; 285084fd7bdfSCyrill Gorcunov case TIOCGEXCL: 285184fd7bdfSCyrill Gorcunov { 285284fd7bdfSCyrill Gorcunov int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); 285384fd7bdfSCyrill Gorcunov return put_user(excl, (int __user *)p); 285484fd7bdfSCyrill Gorcunov } 285596fd7ce5SGreg Kroah-Hartman case TIOCNOTTY: 285696fd7ce5SGreg Kroah-Hartman if (current->signal->tty != tty) 285796fd7ce5SGreg Kroah-Hartman return -ENOTTY; 285896fd7ce5SGreg Kroah-Hartman no_tty(); 285996fd7ce5SGreg Kroah-Hartman return 0; 286096fd7ce5SGreg Kroah-Hartman case TIOCSCTTY: 286196fd7ce5SGreg Kroah-Hartman return tiocsctty(tty, arg); 286296fd7ce5SGreg Kroah-Hartman case TIOCGPGRP: 286396fd7ce5SGreg Kroah-Hartman return tiocgpgrp(tty, real_tty, p); 286496fd7ce5SGreg Kroah-Hartman case TIOCSPGRP: 286596fd7ce5SGreg Kroah-Hartman return tiocspgrp(tty, real_tty, p); 286696fd7ce5SGreg Kroah-Hartman case TIOCGSID: 286796fd7ce5SGreg Kroah-Hartman return tiocgsid(tty, real_tty, p); 286896fd7ce5SGreg Kroah-Hartman case TIOCGETD: 286996fd7ce5SGreg Kroah-Hartman return put_user(tty->ldisc->ops->num, (int __user *)p); 287096fd7ce5SGreg Kroah-Hartman case TIOCSETD: 287196fd7ce5SGreg Kroah-Hartman return tiocsetd(tty, p); 28723c95c985SKay Sievers case TIOCVHANGUP: 28733c95c985SKay Sievers if (!capable(CAP_SYS_ADMIN)) 28743c95c985SKay Sievers return -EPERM; 28753c95c985SKay Sievers tty_vhangup(tty); 28763c95c985SKay Sievers return 0; 2877b7b8de08SWerner Fink case TIOCGDEV: 2878b7b8de08SWerner Fink { 2879b7b8de08SWerner Fink unsigned int ret = new_encode_dev(tty_devnum(real_tty)); 2880b7b8de08SWerner Fink return put_user(ret, (unsigned int __user *)p); 2881b7b8de08SWerner Fink } 288296fd7ce5SGreg Kroah-Hartman /* 288396fd7ce5SGreg Kroah-Hartman * Break handling 288496fd7ce5SGreg Kroah-Hartman */ 288596fd7ce5SGreg Kroah-Hartman case TIOCSBRK: /* Turn break on, unconditionally */ 288696fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 288796fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, -1); 288896fd7ce5SGreg Kroah-Hartman return 0; 288996fd7ce5SGreg Kroah-Hartman case TIOCCBRK: /* Turn break off, unconditionally */ 289096fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 289196fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, 0); 289296fd7ce5SGreg Kroah-Hartman return 0; 289396fd7ce5SGreg Kroah-Hartman case TCSBRK: /* SVID version: non-zero arg --> no break */ 289496fd7ce5SGreg Kroah-Hartman /* non-zero arg means wait for all output data 289596fd7ce5SGreg Kroah-Hartman * to be sent (performed above) but don't send break. 289696fd7ce5SGreg Kroah-Hartman * This is used by the tcdrain() termios function. 289796fd7ce5SGreg Kroah-Hartman */ 289896fd7ce5SGreg Kroah-Hartman if (!arg) 289996fd7ce5SGreg Kroah-Hartman return send_break(tty, 250); 290096fd7ce5SGreg Kroah-Hartman return 0; 290196fd7ce5SGreg Kroah-Hartman case TCSBRKP: /* support for POSIX tcsendbreak() */ 290296fd7ce5SGreg Kroah-Hartman return send_break(tty, arg ? arg*100 : 250); 290396fd7ce5SGreg Kroah-Hartman 290496fd7ce5SGreg Kroah-Hartman case TIOCMGET: 290560b33c13SAlan Cox return tty_tiocmget(tty, p); 290696fd7ce5SGreg Kroah-Hartman case TIOCMSET: 290796fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 290896fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 290920b9d177SAlan Cox return tty_tiocmset(tty, cmd, p); 291096fd7ce5SGreg Kroah-Hartman case TIOCGICOUNT: 291196fd7ce5SGreg Kroah-Hartman retval = tty_tiocgicount(tty, p); 291296fd7ce5SGreg Kroah-Hartman /* For the moment allow fall through to the old method */ 291396fd7ce5SGreg Kroah-Hartman if (retval != -EINVAL) 291496fd7ce5SGreg Kroah-Hartman return retval; 291596fd7ce5SGreg Kroah-Hartman break; 291696fd7ce5SGreg Kroah-Hartman case TCFLSH: 291796fd7ce5SGreg Kroah-Hartman switch (arg) { 291896fd7ce5SGreg Kroah-Hartman case TCIFLUSH: 291996fd7ce5SGreg Kroah-Hartman case TCIOFLUSH: 292096fd7ce5SGreg Kroah-Hartman /* flush tty buffer and allow ldisc to process ioctl */ 292196fd7ce5SGreg Kroah-Hartman tty_buffer_flush(tty); 292296fd7ce5SGreg Kroah-Hartman break; 292396fd7ce5SGreg Kroah-Hartman } 292496fd7ce5SGreg Kroah-Hartman break; 292596fd7ce5SGreg Kroah-Hartman } 292696fd7ce5SGreg Kroah-Hartman if (tty->ops->ioctl) { 29276caa76b7SAlan Cox retval = (tty->ops->ioctl)(tty, cmd, arg); 292896fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 292996fd7ce5SGreg Kroah-Hartman return retval; 293096fd7ce5SGreg Kroah-Hartman } 293196fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 293296fd7ce5SGreg Kroah-Hartman retval = -EINVAL; 293396fd7ce5SGreg Kroah-Hartman if (ld->ops->ioctl) { 293496fd7ce5SGreg Kroah-Hartman retval = ld->ops->ioctl(tty, file, cmd, arg); 293596fd7ce5SGreg Kroah-Hartman if (retval == -ENOIOCTLCMD) 2936bbb63c51SWanlong Gao retval = -ENOTTY; 293796fd7ce5SGreg Kroah-Hartman } 293896fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 293996fd7ce5SGreg Kroah-Hartman return retval; 294096fd7ce5SGreg Kroah-Hartman } 294196fd7ce5SGreg Kroah-Hartman 294296fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 294396fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 294496fd7ce5SGreg Kroah-Hartman unsigned long arg) 294596fd7ce5SGreg Kroah-Hartman { 294696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 294796fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 294896fd7ce5SGreg Kroah-Hartman int retval = -ENOIOCTLCMD; 294996fd7ce5SGreg Kroah-Hartman 29506131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) 295196fd7ce5SGreg Kroah-Hartman return -EINVAL; 295296fd7ce5SGreg Kroah-Hartman 295396fd7ce5SGreg Kroah-Hartman if (tty->ops->compat_ioctl) { 29546caa76b7SAlan Cox retval = (tty->ops->compat_ioctl)(tty, cmd, arg); 295596fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 295696fd7ce5SGreg Kroah-Hartman return retval; 295796fd7ce5SGreg Kroah-Hartman } 295896fd7ce5SGreg Kroah-Hartman 295996fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 296096fd7ce5SGreg Kroah-Hartman if (ld->ops->compat_ioctl) 296196fd7ce5SGreg Kroah-Hartman retval = ld->ops->compat_ioctl(tty, file, cmd, arg); 29628193c429SThomas Meyer else 29638193c429SThomas Meyer retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg); 296496fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 296596fd7ce5SGreg Kroah-Hartman 296696fd7ce5SGreg Kroah-Hartman return retval; 296796fd7ce5SGreg Kroah-Hartman } 296896fd7ce5SGreg Kroah-Hartman #endif 296996fd7ce5SGreg Kroah-Hartman 2970c3c073f8SAl Viro static int this_tty(const void *t, struct file *file, unsigned fd) 2971c3c073f8SAl Viro { 2972c3c073f8SAl Viro if (likely(file->f_op->read != tty_read)) 2973c3c073f8SAl Viro return 0; 2974c3c073f8SAl Viro return file_tty(file) != t ? 0 : fd + 1; 2975c3c073f8SAl Viro } 2976c3c073f8SAl Viro 297796fd7ce5SGreg Kroah-Hartman /* 297896fd7ce5SGreg Kroah-Hartman * This implements the "Secure Attention Key" --- the idea is to 297996fd7ce5SGreg Kroah-Hartman * prevent trojan horses by killing all processes associated with this 298096fd7ce5SGreg Kroah-Hartman * tty when the user hits the "Secure Attention Key". Required for 298196fd7ce5SGreg Kroah-Hartman * super-paranoid applications --- see the Orange Book for more details. 298296fd7ce5SGreg Kroah-Hartman * 298396fd7ce5SGreg Kroah-Hartman * This code could be nicer; ideally it should send a HUP, wait a few 298496fd7ce5SGreg Kroah-Hartman * seconds, then send a INT, and then a KILL signal. But you then 298596fd7ce5SGreg Kroah-Hartman * have to coordinate with the init process, since all processes associated 298696fd7ce5SGreg Kroah-Hartman * with the current tty must be dead before the new getty is allowed 298796fd7ce5SGreg Kroah-Hartman * to spawn. 298896fd7ce5SGreg Kroah-Hartman * 298996fd7ce5SGreg Kroah-Hartman * Now, if it would be correct ;-/ The current code has a nasty hole - 299096fd7ce5SGreg Kroah-Hartman * it doesn't catch files in flight. We may send the descriptor to ourselves 299196fd7ce5SGreg Kroah-Hartman * via AF_UNIX socket, close it and later fetch from socket. FIXME. 299296fd7ce5SGreg Kroah-Hartman * 299396fd7ce5SGreg Kroah-Hartman * Nasty bug: do_SAK is being called in interrupt context. This can 299496fd7ce5SGreg Kroah-Hartman * deadlock. We punt it up to process context. AKPM - 16Mar2001 299596fd7ce5SGreg Kroah-Hartman */ 299696fd7ce5SGreg Kroah-Hartman void __do_SAK(struct tty_struct *tty) 299796fd7ce5SGreg Kroah-Hartman { 299896fd7ce5SGreg Kroah-Hartman #ifdef TTY_SOFT_SAK 299996fd7ce5SGreg Kroah-Hartman tty_hangup(tty); 300096fd7ce5SGreg Kroah-Hartman #else 300196fd7ce5SGreg Kroah-Hartman struct task_struct *g, *p; 300296fd7ce5SGreg Kroah-Hartman struct pid *session; 300396fd7ce5SGreg Kroah-Hartman int i; 300496fd7ce5SGreg Kroah-Hartman 300596fd7ce5SGreg Kroah-Hartman if (!tty) 300696fd7ce5SGreg Kroah-Hartman return; 300796fd7ce5SGreg Kroah-Hartman session = tty->session; 300896fd7ce5SGreg Kroah-Hartman 300996fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 301096fd7ce5SGreg Kroah-Hartman 301196fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 301296fd7ce5SGreg Kroah-Hartman 301396fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 301496fd7ce5SGreg Kroah-Hartman /* Kill the entire session */ 301596fd7ce5SGreg Kroah-Hartman do_each_pid_task(session, PIDTYPE_SID, p) { 301696fd7ce5SGreg Kroah-Hartman printk(KERN_NOTICE "SAK: killed process %d" 301796fd7ce5SGreg Kroah-Hartman " (%s): task_session(p)==tty->session\n", 301896fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 301996fd7ce5SGreg Kroah-Hartman send_sig(SIGKILL, p, 1); 302096fd7ce5SGreg Kroah-Hartman } while_each_pid_task(session, PIDTYPE_SID, p); 302196fd7ce5SGreg Kroah-Hartman /* Now kill any processes that happen to have the 302296fd7ce5SGreg Kroah-Hartman * tty open. 302396fd7ce5SGreg Kroah-Hartman */ 302496fd7ce5SGreg Kroah-Hartman do_each_thread(g, p) { 302596fd7ce5SGreg Kroah-Hartman if (p->signal->tty == tty) { 302696fd7ce5SGreg Kroah-Hartman printk(KERN_NOTICE "SAK: killed process %d" 302796fd7ce5SGreg Kroah-Hartman " (%s): task_session(p)==tty->session\n", 302896fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 302996fd7ce5SGreg Kroah-Hartman send_sig(SIGKILL, p, 1); 303096fd7ce5SGreg Kroah-Hartman continue; 303196fd7ce5SGreg Kroah-Hartman } 303296fd7ce5SGreg Kroah-Hartman task_lock(p); 3033c3c073f8SAl Viro i = iterate_fd(p->files, 0, this_tty, tty); 3034c3c073f8SAl Viro if (i != 0) { 303596fd7ce5SGreg Kroah-Hartman printk(KERN_NOTICE "SAK: killed process %d" 303696fd7ce5SGreg Kroah-Hartman " (%s): fd#%d opened to the tty\n", 3037c3c073f8SAl Viro task_pid_nr(p), p->comm, i - 1); 303896fd7ce5SGreg Kroah-Hartman force_sig(SIGKILL, p); 303996fd7ce5SGreg Kroah-Hartman } 304096fd7ce5SGreg Kroah-Hartman task_unlock(p); 304196fd7ce5SGreg Kroah-Hartman } while_each_thread(g, p); 304296fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 304396fd7ce5SGreg Kroah-Hartman #endif 304496fd7ce5SGreg Kroah-Hartman } 304596fd7ce5SGreg Kroah-Hartman 304696fd7ce5SGreg Kroah-Hartman static void do_SAK_work(struct work_struct *work) 304796fd7ce5SGreg Kroah-Hartman { 304896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 304996fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, SAK_work); 305096fd7ce5SGreg Kroah-Hartman __do_SAK(tty); 305196fd7ce5SGreg Kroah-Hartman } 305296fd7ce5SGreg Kroah-Hartman 305396fd7ce5SGreg Kroah-Hartman /* 305496fd7ce5SGreg Kroah-Hartman * The tq handling here is a little racy - tty->SAK_work may already be queued. 305596fd7ce5SGreg Kroah-Hartman * Fortunately we don't need to worry, because if ->SAK_work is already queued, 305696fd7ce5SGreg Kroah-Hartman * the values which we write to it will be identical to the values which it 305796fd7ce5SGreg Kroah-Hartman * already has. --akpm 305896fd7ce5SGreg Kroah-Hartman */ 305996fd7ce5SGreg Kroah-Hartman void do_SAK(struct tty_struct *tty) 306096fd7ce5SGreg Kroah-Hartman { 306196fd7ce5SGreg Kroah-Hartman if (!tty) 306296fd7ce5SGreg Kroah-Hartman return; 306396fd7ce5SGreg Kroah-Hartman schedule_work(&tty->SAK_work); 306496fd7ce5SGreg Kroah-Hartman } 306596fd7ce5SGreg Kroah-Hartman 306696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(do_SAK); 306796fd7ce5SGreg Kroah-Hartman 30686e9430acSGreg Kroah-Hartman static int dev_match_devt(struct device *dev, const void *data) 306996fd7ce5SGreg Kroah-Hartman { 30706e9430acSGreg Kroah-Hartman const dev_t *devt = data; 307196fd7ce5SGreg Kroah-Hartman return dev->devt == *devt; 307296fd7ce5SGreg Kroah-Hartman } 307396fd7ce5SGreg Kroah-Hartman 307496fd7ce5SGreg Kroah-Hartman /* Must put_device() after it's unused! */ 307596fd7ce5SGreg Kroah-Hartman static struct device *tty_get_device(struct tty_struct *tty) 307696fd7ce5SGreg Kroah-Hartman { 307796fd7ce5SGreg Kroah-Hartman dev_t devt = tty_devnum(tty); 307896fd7ce5SGreg Kroah-Hartman return class_find_device(tty_class, NULL, &devt, dev_match_devt); 307996fd7ce5SGreg Kroah-Hartman } 308096fd7ce5SGreg Kroah-Hartman 308196fd7ce5SGreg Kroah-Hartman 308296fd7ce5SGreg Kroah-Hartman /** 30832c964a2fSRasmus Villemoes * alloc_tty_struct 308496fd7ce5SGreg Kroah-Hartman * 30852c964a2fSRasmus Villemoes * This subroutine allocates and initializes a tty structure. 308696fd7ce5SGreg Kroah-Hartman * 30872c964a2fSRasmus Villemoes * Locking: none - tty in question is not exposed at this point 308896fd7ce5SGreg Kroah-Hartman */ 308996fd7ce5SGreg Kroah-Hartman 30902c964a2fSRasmus Villemoes struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) 309196fd7ce5SGreg Kroah-Hartman { 30922c964a2fSRasmus Villemoes struct tty_struct *tty; 30932c964a2fSRasmus Villemoes 30942c964a2fSRasmus Villemoes tty = kzalloc(sizeof(*tty), GFP_KERNEL); 30952c964a2fSRasmus Villemoes if (!tty) 30962c964a2fSRasmus Villemoes return NULL; 30972c964a2fSRasmus Villemoes 309896fd7ce5SGreg Kroah-Hartman kref_init(&tty->kref); 309996fd7ce5SGreg Kroah-Hartman tty->magic = TTY_MAGIC; 310096fd7ce5SGreg Kroah-Hartman tty_ldisc_init(tty); 310196fd7ce5SGreg Kroah-Hartman tty->session = NULL; 310296fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 310389c8d91eSAlan Cox mutex_init(&tty->legacy_mutex); 3104d8c1f929SPeter Hurley mutex_init(&tty->throttle_mutex); 31056a1c0680SPeter Hurley init_rwsem(&tty->termios_rwsem); 3106dee4a0beSPeter Hurley mutex_init(&tty->winsize_mutex); 310736697529SPeter Hurley init_ldsem(&tty->ldisc_sem); 310896fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->write_wait); 310996fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->read_wait); 311096fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, do_tty_hangup); 311196fd7ce5SGreg Kroah-Hartman mutex_init(&tty->atomic_write_lock); 311296fd7ce5SGreg Kroah-Hartman spin_lock_init(&tty->ctrl_lock); 3113f9e053dcSPeter Hurley spin_lock_init(&tty->flow_lock); 311496fd7ce5SGreg Kroah-Hartman INIT_LIST_HEAD(&tty->tty_files); 311596fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->SAK_work, do_SAK_work); 311696fd7ce5SGreg Kroah-Hartman 311796fd7ce5SGreg Kroah-Hartman tty->driver = driver; 311896fd7ce5SGreg Kroah-Hartman tty->ops = driver->ops; 311996fd7ce5SGreg Kroah-Hartman tty->index = idx; 312096fd7ce5SGreg Kroah-Hartman tty_line_name(driver, idx, tty->name); 312196fd7ce5SGreg Kroah-Hartman tty->dev = tty_get_device(tty); 31222c964a2fSRasmus Villemoes 31232c964a2fSRasmus Villemoes return tty; 312496fd7ce5SGreg Kroah-Hartman } 312596fd7ce5SGreg Kroah-Hartman 312696fd7ce5SGreg Kroah-Hartman /** 31276716671dSJiri Slaby * deinitialize_tty_struct 31286716671dSJiri Slaby * @tty: tty to deinitialize 31296716671dSJiri Slaby * 31306716671dSJiri Slaby * This subroutine deinitializes a tty structure that has been newly 31316716671dSJiri Slaby * allocated but tty_release cannot be called on that yet. 31326716671dSJiri Slaby * 31336716671dSJiri Slaby * Locking: none - tty in question must not be exposed at this point 31346716671dSJiri Slaby */ 31356716671dSJiri Slaby void deinitialize_tty_struct(struct tty_struct *tty) 31366716671dSJiri Slaby { 31376716671dSJiri Slaby tty_ldisc_deinit(tty); 31386716671dSJiri Slaby } 31396716671dSJiri Slaby 31406716671dSJiri Slaby /** 314196fd7ce5SGreg Kroah-Hartman * tty_put_char - write one character to a tty 314296fd7ce5SGreg Kroah-Hartman * @tty: tty 314396fd7ce5SGreg Kroah-Hartman * @ch: character 314496fd7ce5SGreg Kroah-Hartman * 314596fd7ce5SGreg Kroah-Hartman * Write one byte to the tty using the provided put_char method 314696fd7ce5SGreg Kroah-Hartman * if present. Returns the number of characters successfully output. 314796fd7ce5SGreg Kroah-Hartman * 314896fd7ce5SGreg Kroah-Hartman * Note: the specific put_char operation in the driver layer may go 314996fd7ce5SGreg Kroah-Hartman * away soon. Don't call it directly, use this method 315096fd7ce5SGreg Kroah-Hartman */ 315196fd7ce5SGreg Kroah-Hartman 315296fd7ce5SGreg Kroah-Hartman int tty_put_char(struct tty_struct *tty, unsigned char ch) 315396fd7ce5SGreg Kroah-Hartman { 315496fd7ce5SGreg Kroah-Hartman if (tty->ops->put_char) 315596fd7ce5SGreg Kroah-Hartman return tty->ops->put_char(tty, ch); 315696fd7ce5SGreg Kroah-Hartman return tty->ops->write(tty, &ch, 1); 315796fd7ce5SGreg Kroah-Hartman } 315896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_put_char); 315996fd7ce5SGreg Kroah-Hartman 316096fd7ce5SGreg Kroah-Hartman struct class *tty_class; 316196fd7ce5SGreg Kroah-Hartman 31627e73eca6SJiri Slaby static int tty_cdev_add(struct tty_driver *driver, dev_t dev, 31637e73eca6SJiri Slaby unsigned int index, unsigned int count) 31647e73eca6SJiri Slaby { 31657e73eca6SJiri Slaby /* init here, since reused cdevs cause crashes */ 31667e73eca6SJiri Slaby cdev_init(&driver->cdevs[index], &tty_fops); 31677e73eca6SJiri Slaby driver->cdevs[index].owner = driver->owner; 31687e73eca6SJiri Slaby return cdev_add(&driver->cdevs[index], dev, count); 31697e73eca6SJiri Slaby } 31707e73eca6SJiri Slaby 317196fd7ce5SGreg Kroah-Hartman /** 317296fd7ce5SGreg Kroah-Hartman * tty_register_device - register a tty device 317396fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 317496fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 317596fd7ce5SGreg Kroah-Hartman * @device: a struct device that is associated with this tty device. 317696fd7ce5SGreg Kroah-Hartman * This field is optional, if there is no known struct device 317796fd7ce5SGreg Kroah-Hartman * for this tty device it can be set to NULL safely. 317896fd7ce5SGreg Kroah-Hartman * 317996fd7ce5SGreg Kroah-Hartman * Returns a pointer to the struct device for this tty device 318096fd7ce5SGreg Kroah-Hartman * (or ERR_PTR(-EFOO) on error). 318196fd7ce5SGreg Kroah-Hartman * 318296fd7ce5SGreg Kroah-Hartman * This call is required to be made to register an individual tty device 318396fd7ce5SGreg Kroah-Hartman * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 318496fd7ce5SGreg Kroah-Hartman * that bit is not set, this function should not be called by a tty 318596fd7ce5SGreg Kroah-Hartman * driver. 318696fd7ce5SGreg Kroah-Hartman * 318796fd7ce5SGreg Kroah-Hartman * Locking: ?? 318896fd7ce5SGreg Kroah-Hartman */ 318996fd7ce5SGreg Kroah-Hartman 319096fd7ce5SGreg Kroah-Hartman struct device *tty_register_device(struct tty_driver *driver, unsigned index, 319196fd7ce5SGreg Kroah-Hartman struct device *device) 319296fd7ce5SGreg Kroah-Hartman { 31936915c0e4STomas Hlavacek return tty_register_device_attr(driver, index, device, NULL, NULL); 31946915c0e4STomas Hlavacek } 31956915c0e4STomas Hlavacek EXPORT_SYMBOL(tty_register_device); 31966915c0e4STomas Hlavacek 3197b1b79916STomas Hlavacek static void tty_device_create_release(struct device *dev) 3198b1b79916STomas Hlavacek { 3199b1b79916STomas Hlavacek pr_debug("device: '%s': %s\n", dev_name(dev), __func__); 3200b1b79916STomas Hlavacek kfree(dev); 3201b1b79916STomas Hlavacek } 3202b1b79916STomas Hlavacek 32036915c0e4STomas Hlavacek /** 32046915c0e4STomas Hlavacek * tty_register_device_attr - register a tty device 32056915c0e4STomas Hlavacek * @driver: the tty driver that describes the tty device 32066915c0e4STomas Hlavacek * @index: the index in the tty driver for this tty device 32076915c0e4STomas Hlavacek * @device: a struct device that is associated with this tty device. 32086915c0e4STomas Hlavacek * This field is optional, if there is no known struct device 32096915c0e4STomas Hlavacek * for this tty device it can be set to NULL safely. 32106915c0e4STomas Hlavacek * @drvdata: Driver data to be set to device. 32116915c0e4STomas Hlavacek * @attr_grp: Attribute group to be set on device. 32126915c0e4STomas Hlavacek * 32136915c0e4STomas Hlavacek * Returns a pointer to the struct device for this tty device 32146915c0e4STomas Hlavacek * (or ERR_PTR(-EFOO) on error). 32156915c0e4STomas Hlavacek * 32166915c0e4STomas Hlavacek * This call is required to be made to register an individual tty device 32176915c0e4STomas Hlavacek * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 32186915c0e4STomas Hlavacek * that bit is not set, this function should not be called by a tty 32196915c0e4STomas Hlavacek * driver. 32206915c0e4STomas Hlavacek * 32216915c0e4STomas Hlavacek * Locking: ?? 32226915c0e4STomas Hlavacek */ 32236915c0e4STomas Hlavacek struct device *tty_register_device_attr(struct tty_driver *driver, 32246915c0e4STomas Hlavacek unsigned index, struct device *device, 32256915c0e4STomas Hlavacek void *drvdata, 32266915c0e4STomas Hlavacek const struct attribute_group **attr_grp) 32276915c0e4STomas Hlavacek { 322896fd7ce5SGreg Kroah-Hartman char name[64]; 32296915c0e4STomas Hlavacek dev_t devt = MKDEV(driver->major, driver->minor_start) + index; 32306915c0e4STomas Hlavacek struct device *dev = NULL; 32316915c0e4STomas Hlavacek int retval = -ENODEV; 32327e73eca6SJiri Slaby bool cdev = false; 323396fd7ce5SGreg Kroah-Hartman 323496fd7ce5SGreg Kroah-Hartman if (index >= driver->num) { 323596fd7ce5SGreg Kroah-Hartman printk(KERN_ERR "Attempt to register invalid tty line number " 323696fd7ce5SGreg Kroah-Hartman " (%d).\n", index); 323796fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EINVAL); 323896fd7ce5SGreg Kroah-Hartman } 323996fd7ce5SGreg Kroah-Hartman 324096fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY) 324196fd7ce5SGreg Kroah-Hartman pty_line_name(driver, index, name); 324296fd7ce5SGreg Kroah-Hartman else 324396fd7ce5SGreg Kroah-Hartman tty_line_name(driver, index, name); 324496fd7ce5SGreg Kroah-Hartman 32457e73eca6SJiri Slaby if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 32466915c0e4STomas Hlavacek retval = tty_cdev_add(driver, devt, index, 1); 32476915c0e4STomas Hlavacek if (retval) 32486915c0e4STomas Hlavacek goto error; 32497e73eca6SJiri Slaby cdev = true; 32507e73eca6SJiri Slaby } 32517e73eca6SJiri Slaby 32526915c0e4STomas Hlavacek dev = kzalloc(sizeof(*dev), GFP_KERNEL); 32536915c0e4STomas Hlavacek if (!dev) { 32546915c0e4STomas Hlavacek retval = -ENOMEM; 32556915c0e4STomas Hlavacek goto error; 325696fd7ce5SGreg Kroah-Hartman } 32576915c0e4STomas Hlavacek 32586915c0e4STomas Hlavacek dev->devt = devt; 32596915c0e4STomas Hlavacek dev->class = tty_class; 32606915c0e4STomas Hlavacek dev->parent = device; 3261b1b79916STomas Hlavacek dev->release = tty_device_create_release; 32626915c0e4STomas Hlavacek dev_set_name(dev, "%s", name); 32636915c0e4STomas Hlavacek dev->groups = attr_grp; 32646915c0e4STomas Hlavacek dev_set_drvdata(dev, drvdata); 32656915c0e4STomas Hlavacek 32666915c0e4STomas Hlavacek retval = device_register(dev); 32676915c0e4STomas Hlavacek if (retval) 32686915c0e4STomas Hlavacek goto error; 32696915c0e4STomas Hlavacek 32706915c0e4STomas Hlavacek return dev; 32716915c0e4STomas Hlavacek 32726915c0e4STomas Hlavacek error: 32736915c0e4STomas Hlavacek put_device(dev); 32746915c0e4STomas Hlavacek if (cdev) 32756915c0e4STomas Hlavacek cdev_del(&driver->cdevs[index]); 32766915c0e4STomas Hlavacek return ERR_PTR(retval); 32776915c0e4STomas Hlavacek } 32786915c0e4STomas Hlavacek EXPORT_SYMBOL_GPL(tty_register_device_attr); 327996fd7ce5SGreg Kroah-Hartman 328096fd7ce5SGreg Kroah-Hartman /** 328196fd7ce5SGreg Kroah-Hartman * tty_unregister_device - unregister a tty device 328296fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 328396fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 328496fd7ce5SGreg Kroah-Hartman * 328596fd7ce5SGreg Kroah-Hartman * If a tty device is registered with a call to tty_register_device() then 328696fd7ce5SGreg Kroah-Hartman * this function must be called when the tty device is gone. 328796fd7ce5SGreg Kroah-Hartman * 328896fd7ce5SGreg Kroah-Hartman * Locking: ?? 328996fd7ce5SGreg Kroah-Hartman */ 329096fd7ce5SGreg Kroah-Hartman 329196fd7ce5SGreg Kroah-Hartman void tty_unregister_device(struct tty_driver *driver, unsigned index) 329296fd7ce5SGreg Kroah-Hartman { 329396fd7ce5SGreg Kroah-Hartman device_destroy(tty_class, 329496fd7ce5SGreg Kroah-Hartman MKDEV(driver->major, driver->minor_start) + index); 32957e73eca6SJiri Slaby if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) 32967e73eca6SJiri Slaby cdev_del(&driver->cdevs[index]); 329796fd7ce5SGreg Kroah-Hartman } 329896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_device); 329996fd7ce5SGreg Kroah-Hartman 33007f0bc6a6SJiri Slaby /** 33017f0bc6a6SJiri Slaby * __tty_alloc_driver -- allocate tty driver 33027f0bc6a6SJiri Slaby * @lines: count of lines this driver can handle at most 33037f0bc6a6SJiri Slaby * @owner: module which is repsonsible for this driver 33047f0bc6a6SJiri Slaby * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags 33057f0bc6a6SJiri Slaby * 33067f0bc6a6SJiri Slaby * This should not be called directly, some of the provided macros should be 33077f0bc6a6SJiri Slaby * used instead. Use IS_ERR and friends on @retval. 33087f0bc6a6SJiri Slaby */ 33097f0bc6a6SJiri Slaby struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner, 33107f0bc6a6SJiri Slaby unsigned long flags) 331196fd7ce5SGreg Kroah-Hartman { 331296fd7ce5SGreg Kroah-Hartman struct tty_driver *driver; 33137e73eca6SJiri Slaby unsigned int cdevs = 1; 331416a02081SJiri Slaby int err; 331596fd7ce5SGreg Kroah-Hartman 33160019b408SJiri Slaby if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1)) 33177f0bc6a6SJiri Slaby return ERR_PTR(-EINVAL); 33187f0bc6a6SJiri Slaby 331996fd7ce5SGreg Kroah-Hartman driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); 33207f0bc6a6SJiri Slaby if (!driver) 33217f0bc6a6SJiri Slaby return ERR_PTR(-ENOMEM); 33227f0bc6a6SJiri Slaby 332396fd7ce5SGreg Kroah-Hartman kref_init(&driver->kref); 332496fd7ce5SGreg Kroah-Hartman driver->magic = TTY_DRIVER_MAGIC; 332596fd7ce5SGreg Kroah-Hartman driver->num = lines; 33261a54a76dSJiri Slaby driver->owner = owner; 33277f0bc6a6SJiri Slaby driver->flags = flags; 332816a02081SJiri Slaby 332916a02081SJiri Slaby if (!(flags & TTY_DRIVER_DEVPTS_MEM)) { 333016a02081SJiri Slaby driver->ttys = kcalloc(lines, sizeof(*driver->ttys), 333116a02081SJiri Slaby GFP_KERNEL); 333216a02081SJiri Slaby driver->termios = kcalloc(lines, sizeof(*driver->termios), 333316a02081SJiri Slaby GFP_KERNEL); 333416a02081SJiri Slaby if (!driver->ttys || !driver->termios) { 333516a02081SJiri Slaby err = -ENOMEM; 333616a02081SJiri Slaby goto err_free_all; 333716a02081SJiri Slaby } 333816a02081SJiri Slaby } 333916a02081SJiri Slaby 334016a02081SJiri Slaby if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 334116a02081SJiri Slaby driver->ports = kcalloc(lines, sizeof(*driver->ports), 334216a02081SJiri Slaby GFP_KERNEL); 334316a02081SJiri Slaby if (!driver->ports) { 334416a02081SJiri Slaby err = -ENOMEM; 334516a02081SJiri Slaby goto err_free_all; 334616a02081SJiri Slaby } 33477e73eca6SJiri Slaby cdevs = lines; 33487e73eca6SJiri Slaby } 33497e73eca6SJiri Slaby 33507e73eca6SJiri Slaby driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL); 33517e73eca6SJiri Slaby if (!driver->cdevs) { 33527e73eca6SJiri Slaby err = -ENOMEM; 33537e73eca6SJiri Slaby goto err_free_all; 335416a02081SJiri Slaby } 33557f0bc6a6SJiri Slaby 335696fd7ce5SGreg Kroah-Hartman return driver; 335716a02081SJiri Slaby err_free_all: 335816a02081SJiri Slaby kfree(driver->ports); 335916a02081SJiri Slaby kfree(driver->ttys); 336016a02081SJiri Slaby kfree(driver->termios); 336116a02081SJiri Slaby kfree(driver); 336216a02081SJiri Slaby return ERR_PTR(err); 336396fd7ce5SGreg Kroah-Hartman } 33647f0bc6a6SJiri Slaby EXPORT_SYMBOL(__tty_alloc_driver); 336596fd7ce5SGreg Kroah-Hartman 336696fd7ce5SGreg Kroah-Hartman static void destruct_tty_driver(struct kref *kref) 336796fd7ce5SGreg Kroah-Hartman { 336896fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = container_of(kref, struct tty_driver, kref); 336996fd7ce5SGreg Kroah-Hartman int i; 337096fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 337196fd7ce5SGreg Kroah-Hartman 337296fd7ce5SGreg Kroah-Hartman if (driver->flags & TTY_DRIVER_INSTALLED) { 337396fd7ce5SGreg Kroah-Hartman /* 337496fd7ce5SGreg Kroah-Hartman * Free the termios and termios_locked structures because 337596fd7ce5SGreg Kroah-Hartman * we don't want to get memory leaks when modular tty 337696fd7ce5SGreg Kroah-Hartman * drivers are removed from the kernel. 337796fd7ce5SGreg Kroah-Hartman */ 337896fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 337996fd7ce5SGreg Kroah-Hartman tp = driver->termios[i]; 338096fd7ce5SGreg Kroah-Hartman if (tp) { 338196fd7ce5SGreg Kroah-Hartman driver->termios[i] = NULL; 338296fd7ce5SGreg Kroah-Hartman kfree(tp); 338396fd7ce5SGreg Kroah-Hartman } 338496fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) 338596fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 338696fd7ce5SGreg Kroah-Hartman } 338796fd7ce5SGreg Kroah-Hartman proc_tty_unregister_driver(driver); 33887e73eca6SJiri Slaby if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) 33897e73eca6SJiri Slaby cdev_del(&driver->cdevs[0]); 339096fd7ce5SGreg Kroah-Hartman } 33917e73eca6SJiri Slaby kfree(driver->cdevs); 339204831dc1SJiri Slaby kfree(driver->ports); 339316a02081SJiri Slaby kfree(driver->termios); 339416a02081SJiri Slaby kfree(driver->ttys); 339596fd7ce5SGreg Kroah-Hartman kfree(driver); 339696fd7ce5SGreg Kroah-Hartman } 339796fd7ce5SGreg Kroah-Hartman 339896fd7ce5SGreg Kroah-Hartman void tty_driver_kref_put(struct tty_driver *driver) 339996fd7ce5SGreg Kroah-Hartman { 340096fd7ce5SGreg Kroah-Hartman kref_put(&driver->kref, destruct_tty_driver); 340196fd7ce5SGreg Kroah-Hartman } 340296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_driver_kref_put); 340396fd7ce5SGreg Kroah-Hartman 340496fd7ce5SGreg Kroah-Hartman void tty_set_operations(struct tty_driver *driver, 340596fd7ce5SGreg Kroah-Hartman const struct tty_operations *op) 340696fd7ce5SGreg Kroah-Hartman { 340796fd7ce5SGreg Kroah-Hartman driver->ops = op; 340896fd7ce5SGreg Kroah-Hartman }; 340996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_set_operations); 341096fd7ce5SGreg Kroah-Hartman 341196fd7ce5SGreg Kroah-Hartman void put_tty_driver(struct tty_driver *d) 341296fd7ce5SGreg Kroah-Hartman { 341396fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(d); 341496fd7ce5SGreg Kroah-Hartman } 341596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(put_tty_driver); 341696fd7ce5SGreg Kroah-Hartman 341796fd7ce5SGreg Kroah-Hartman /* 341896fd7ce5SGreg Kroah-Hartman * Called by a tty driver to register itself. 341996fd7ce5SGreg Kroah-Hartman */ 342096fd7ce5SGreg Kroah-Hartman int tty_register_driver(struct tty_driver *driver) 342196fd7ce5SGreg Kroah-Hartman { 342296fd7ce5SGreg Kroah-Hartman int error; 342396fd7ce5SGreg Kroah-Hartman int i; 342496fd7ce5SGreg Kroah-Hartman dev_t dev; 342596fd7ce5SGreg Kroah-Hartman struct device *d; 342696fd7ce5SGreg Kroah-Hartman 342796fd7ce5SGreg Kroah-Hartman if (!driver->major) { 342896fd7ce5SGreg Kroah-Hartman error = alloc_chrdev_region(&dev, driver->minor_start, 342996fd7ce5SGreg Kroah-Hartman driver->num, driver->name); 343096fd7ce5SGreg Kroah-Hartman if (!error) { 343196fd7ce5SGreg Kroah-Hartman driver->major = MAJOR(dev); 343296fd7ce5SGreg Kroah-Hartman driver->minor_start = MINOR(dev); 343396fd7ce5SGreg Kroah-Hartman } 343496fd7ce5SGreg Kroah-Hartman } else { 343596fd7ce5SGreg Kroah-Hartman dev = MKDEV(driver->major, driver->minor_start); 343696fd7ce5SGreg Kroah-Hartman error = register_chrdev_region(dev, driver->num, driver->name); 343796fd7ce5SGreg Kroah-Hartman } 34389bb8a3d4SJiri Slaby if (error < 0) 343916a02081SJiri Slaby goto err; 344096fd7ce5SGreg Kroah-Hartman 34417e73eca6SJiri Slaby if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) { 34427e73eca6SJiri Slaby error = tty_cdev_add(driver, dev, 0, driver->num); 34439bb8a3d4SJiri Slaby if (error) 34449bb8a3d4SJiri Slaby goto err_unreg_char; 34457e73eca6SJiri Slaby } 344696fd7ce5SGreg Kroah-Hartman 344796fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 344896fd7ce5SGreg Kroah-Hartman list_add(&driver->tty_drivers, &tty_drivers); 344996fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 345096fd7ce5SGreg Kroah-Hartman 345196fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { 345296fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 345396fd7ce5SGreg Kroah-Hartman d = tty_register_device(driver, i, NULL); 345496fd7ce5SGreg Kroah-Hartman if (IS_ERR(d)) { 345596fd7ce5SGreg Kroah-Hartman error = PTR_ERR(d); 345616a02081SJiri Slaby goto err_unreg_devs; 345796fd7ce5SGreg Kroah-Hartman } 345896fd7ce5SGreg Kroah-Hartman } 345996fd7ce5SGreg Kroah-Hartman } 346096fd7ce5SGreg Kroah-Hartman proc_tty_register_driver(driver); 346196fd7ce5SGreg Kroah-Hartman driver->flags |= TTY_DRIVER_INSTALLED; 346296fd7ce5SGreg Kroah-Hartman return 0; 346396fd7ce5SGreg Kroah-Hartman 346416a02081SJiri Slaby err_unreg_devs: 346596fd7ce5SGreg Kroah-Hartman for (i--; i >= 0; i--) 346696fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 346796fd7ce5SGreg Kroah-Hartman 346896fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 346996fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 347096fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 347196fd7ce5SGreg Kroah-Hartman 34729bb8a3d4SJiri Slaby err_unreg_char: 347396fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(dev, driver->num); 347416a02081SJiri Slaby err: 347596fd7ce5SGreg Kroah-Hartman return error; 347696fd7ce5SGreg Kroah-Hartman } 347796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_driver); 347896fd7ce5SGreg Kroah-Hartman 347996fd7ce5SGreg Kroah-Hartman /* 348096fd7ce5SGreg Kroah-Hartman * Called by a tty driver to unregister itself. 348196fd7ce5SGreg Kroah-Hartman */ 348296fd7ce5SGreg Kroah-Hartman int tty_unregister_driver(struct tty_driver *driver) 348396fd7ce5SGreg Kroah-Hartman { 348496fd7ce5SGreg Kroah-Hartman #if 0 348596fd7ce5SGreg Kroah-Hartman /* FIXME */ 348696fd7ce5SGreg Kroah-Hartman if (driver->refcount) 348796fd7ce5SGreg Kroah-Hartman return -EBUSY; 348896fd7ce5SGreg Kroah-Hartman #endif 348996fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), 349096fd7ce5SGreg Kroah-Hartman driver->num); 349196fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 349296fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 349396fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 349496fd7ce5SGreg Kroah-Hartman return 0; 349596fd7ce5SGreg Kroah-Hartman } 349696fd7ce5SGreg Kroah-Hartman 349796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_driver); 349896fd7ce5SGreg Kroah-Hartman 349996fd7ce5SGreg Kroah-Hartman dev_t tty_devnum(struct tty_struct *tty) 350096fd7ce5SGreg Kroah-Hartman { 350196fd7ce5SGreg Kroah-Hartman return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; 350296fd7ce5SGreg Kroah-Hartman } 350396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_devnum); 350496fd7ce5SGreg Kroah-Hartman 350596fd7ce5SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops) 350696fd7ce5SGreg Kroah-Hartman { 350796fd7ce5SGreg Kroah-Hartman *fops = tty_fops; 350896fd7ce5SGreg Kroah-Hartman } 350996fd7ce5SGreg Kroah-Hartman 351096fd7ce5SGreg Kroah-Hartman /* 351196fd7ce5SGreg Kroah-Hartman * Initialize the console device. This is called *early*, so 351296fd7ce5SGreg Kroah-Hartman * we can't necessarily depend on lots of kernel help here. 351396fd7ce5SGreg Kroah-Hartman * Just do some early initializations, and do the complex setup 351496fd7ce5SGreg Kroah-Hartman * later. 351596fd7ce5SGreg Kroah-Hartman */ 351696fd7ce5SGreg Kroah-Hartman void __init console_init(void) 351796fd7ce5SGreg Kroah-Hartman { 351896fd7ce5SGreg Kroah-Hartman initcall_t *call; 351996fd7ce5SGreg Kroah-Hartman 352096fd7ce5SGreg Kroah-Hartman /* Setup the default TTY line discipline. */ 352196fd7ce5SGreg Kroah-Hartman tty_ldisc_begin(); 352296fd7ce5SGreg Kroah-Hartman 352396fd7ce5SGreg Kroah-Hartman /* 352496fd7ce5SGreg Kroah-Hartman * set up the console device so that later boot sequences can 352596fd7ce5SGreg Kroah-Hartman * inform about problems etc.. 352696fd7ce5SGreg Kroah-Hartman */ 352796fd7ce5SGreg Kroah-Hartman call = __con_initcall_start; 352896fd7ce5SGreg Kroah-Hartman while (call < __con_initcall_end) { 352996fd7ce5SGreg Kroah-Hartman (*call)(); 353096fd7ce5SGreg Kroah-Hartman call++; 353196fd7ce5SGreg Kroah-Hartman } 353296fd7ce5SGreg Kroah-Hartman } 353396fd7ce5SGreg Kroah-Hartman 35342c9ede55SAl Viro static char *tty_devnode(struct device *dev, umode_t *mode) 353596fd7ce5SGreg Kroah-Hartman { 353696fd7ce5SGreg Kroah-Hartman if (!mode) 353796fd7ce5SGreg Kroah-Hartman return NULL; 353896fd7ce5SGreg Kroah-Hartman if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || 353996fd7ce5SGreg Kroah-Hartman dev->devt == MKDEV(TTYAUX_MAJOR, 2)) 354096fd7ce5SGreg Kroah-Hartman *mode = 0666; 354196fd7ce5SGreg Kroah-Hartman return NULL; 354296fd7ce5SGreg Kroah-Hartman } 354396fd7ce5SGreg Kroah-Hartman 354496fd7ce5SGreg Kroah-Hartman static int __init tty_class_init(void) 354596fd7ce5SGreg Kroah-Hartman { 354696fd7ce5SGreg Kroah-Hartman tty_class = class_create(THIS_MODULE, "tty"); 354796fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty_class)) 354896fd7ce5SGreg Kroah-Hartman return PTR_ERR(tty_class); 354996fd7ce5SGreg Kroah-Hartman tty_class->devnode = tty_devnode; 355096fd7ce5SGreg Kroah-Hartman return 0; 355196fd7ce5SGreg Kroah-Hartman } 355296fd7ce5SGreg Kroah-Hartman 355396fd7ce5SGreg Kroah-Hartman postcore_initcall(tty_class_init); 355496fd7ce5SGreg Kroah-Hartman 355596fd7ce5SGreg Kroah-Hartman /* 3/2004 jmc: why do these devices exist? */ 355696fd7ce5SGreg Kroah-Hartman static struct cdev tty_cdev, console_cdev; 355796fd7ce5SGreg Kroah-Hartman 3558fbc92a34SKay Sievers static ssize_t show_cons_active(struct device *dev, 3559fbc92a34SKay Sievers struct device_attribute *attr, char *buf) 3560fbc92a34SKay Sievers { 3561fbc92a34SKay Sievers struct console *cs[16]; 3562fbc92a34SKay Sievers int i = 0; 3563fbc92a34SKay Sievers struct console *c; 3564fbc92a34SKay Sievers ssize_t count = 0; 3565fbc92a34SKay Sievers 3566ac751efaSTorben Hohn console_lock(); 3567a2a6a822SKay Sievers for_each_console(c) { 3568fbc92a34SKay Sievers if (!c->device) 3569fbc92a34SKay Sievers continue; 3570fbc92a34SKay Sievers if (!c->write) 3571fbc92a34SKay Sievers continue; 3572fbc92a34SKay Sievers if ((c->flags & CON_ENABLED) == 0) 3573fbc92a34SKay Sievers continue; 3574fbc92a34SKay Sievers cs[i++] = c; 3575fbc92a34SKay Sievers if (i >= ARRAY_SIZE(cs)) 3576fbc92a34SKay Sievers break; 3577fbc92a34SKay Sievers } 3578723abd87SHannes Reinecke while (i--) { 3579723abd87SHannes Reinecke int index = cs[i]->index; 3580723abd87SHannes Reinecke struct tty_driver *drv = cs[i]->device(cs[i], &index); 3581723abd87SHannes Reinecke 3582723abd87SHannes Reinecke /* don't resolve tty0 as some programs depend on it */ 3583723abd87SHannes Reinecke if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR)) 3584723abd87SHannes Reinecke count += tty_line_name(drv, index, buf + count); 3585723abd87SHannes Reinecke else 3586723abd87SHannes Reinecke count += sprintf(buf + count, "%s%d", 3587723abd87SHannes Reinecke cs[i]->name, cs[i]->index); 3588723abd87SHannes Reinecke 3589723abd87SHannes Reinecke count += sprintf(buf + count, "%c", i ? ' ':'\n'); 3590723abd87SHannes Reinecke } 3591ac751efaSTorben Hohn console_unlock(); 3592fbc92a34SKay Sievers 3593fbc92a34SKay Sievers return count; 3594fbc92a34SKay Sievers } 3595fbc92a34SKay Sievers static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL); 3596fbc92a34SKay Sievers 3597fbc92a34SKay Sievers static struct device *consdev; 3598fbc92a34SKay Sievers 3599fbc92a34SKay Sievers void console_sysfs_notify(void) 3600fbc92a34SKay Sievers { 3601fbc92a34SKay Sievers if (consdev) 3602fbc92a34SKay Sievers sysfs_notify(&consdev->kobj, NULL, "active"); 3603fbc92a34SKay Sievers } 3604fbc92a34SKay Sievers 360596fd7ce5SGreg Kroah-Hartman /* 360696fd7ce5SGreg Kroah-Hartman * Ok, now we can initialize the rest of the tty devices and can count 360796fd7ce5SGreg Kroah-Hartman * on memory allocations, interrupts etc.. 360896fd7ce5SGreg Kroah-Hartman */ 360996fd7ce5SGreg Kroah-Hartman int __init tty_init(void) 361096fd7ce5SGreg Kroah-Hartman { 361196fd7ce5SGreg Kroah-Hartman cdev_init(&tty_cdev, &tty_fops); 361296fd7ce5SGreg Kroah-Hartman if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || 361396fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) 361496fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/tty driver\n"); 3615fbc92a34SKay Sievers device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); 361696fd7ce5SGreg Kroah-Hartman 361796fd7ce5SGreg Kroah-Hartman cdev_init(&console_cdev, &console_fops); 361896fd7ce5SGreg Kroah-Hartman if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || 361996fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) 362096fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/console driver\n"); 3621fbc92a34SKay Sievers consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, 362296fd7ce5SGreg Kroah-Hartman "console"); 3623fbc92a34SKay Sievers if (IS_ERR(consdev)) 3624fbc92a34SKay Sievers consdev = NULL; 3625fbc92a34SKay Sievers else 3626a2a6a822SKay Sievers WARN_ON(device_create_file(consdev, &dev_attr_active) < 0); 362796fd7ce5SGreg Kroah-Hartman 362896fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT 362996fd7ce5SGreg Kroah-Hartman vty_init(&console_fops); 363096fd7ce5SGreg Kroah-Hartman #endif 363196fd7ce5SGreg Kroah-Hartman return 0; 363296fd7ce5SGreg Kroah-Hartman } 363396fd7ce5SGreg Kroah-Hartman 3634