1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 296fd7ce5SGreg Kroah-Hartman /* 396fd7ce5SGreg Kroah-Hartman * Copyright (C) 1991, 1992 Linus Torvalds 496fd7ce5SGreg Kroah-Hartman */ 596fd7ce5SGreg Kroah-Hartman 696fd7ce5SGreg Kroah-Hartman /* 796fd7ce5SGreg Kroah-Hartman * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles 896fd7ce5SGreg Kroah-Hartman * or rs-channels. It also implements echoing, cooked mode etc. 996fd7ce5SGreg Kroah-Hartman * 1096fd7ce5SGreg Kroah-Hartman * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0. 1196fd7ce5SGreg Kroah-Hartman * 1296fd7ce5SGreg Kroah-Hartman * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the 1396fd7ce5SGreg Kroah-Hartman * tty_struct and tty_queue structures. Previously there was an array 1496fd7ce5SGreg Kroah-Hartman * of 256 tty_struct's which was statically allocated, and the 1596fd7ce5SGreg Kroah-Hartman * tty_queue structures were allocated at boot time. Both are now 1696fd7ce5SGreg Kroah-Hartman * dynamically allocated only when the tty is open. 1796fd7ce5SGreg Kroah-Hartman * 1896fd7ce5SGreg Kroah-Hartman * Also restructured routines so that there is more of a separation 1996fd7ce5SGreg Kroah-Hartman * between the high-level tty routines (tty_io.c and tty_ioctl.c) and 2096fd7ce5SGreg Kroah-Hartman * the low-level tty routines (serial.c, pty.c, console.c). This 2196fd7ce5SGreg Kroah-Hartman * makes for cleaner and more compact code. -TYT, 9/17/92 2296fd7ce5SGreg Kroah-Hartman * 2396fd7ce5SGreg Kroah-Hartman * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines 2496fd7ce5SGreg Kroah-Hartman * which can be dynamically activated and de-activated by the line 2596fd7ce5SGreg Kroah-Hartman * discipline handling modules (like SLIP). 2696fd7ce5SGreg Kroah-Hartman * 2796fd7ce5SGreg Kroah-Hartman * NOTE: pay no attention to the line discipline code (yet); its 2896fd7ce5SGreg Kroah-Hartman * interface is still subject to change in this version... 2996fd7ce5SGreg Kroah-Hartman * -- TYT, 1/31/92 3096fd7ce5SGreg Kroah-Hartman * 3196fd7ce5SGreg Kroah-Hartman * Added functionality to the OPOST tty handling. No delays, but all 3296fd7ce5SGreg Kroah-Hartman * other bits should be there. 3396fd7ce5SGreg Kroah-Hartman * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993. 3496fd7ce5SGreg Kroah-Hartman * 3596fd7ce5SGreg Kroah-Hartman * Rewrote canonical mode and added more termios flags. 3696fd7ce5SGreg Kroah-Hartman * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94 3796fd7ce5SGreg Kroah-Hartman * 3896fd7ce5SGreg Kroah-Hartman * Reorganized FASYNC support so mouse code can share it. 3996fd7ce5SGreg Kroah-Hartman * -- ctm@ardi.com, 9Sep95 4096fd7ce5SGreg Kroah-Hartman * 4196fd7ce5SGreg Kroah-Hartman * New TIOCLINUX variants added. 4296fd7ce5SGreg Kroah-Hartman * -- mj@k332.feld.cvut.cz, 19-Nov-95 4396fd7ce5SGreg Kroah-Hartman * 4496fd7ce5SGreg Kroah-Hartman * Restrict vt switching via ioctl() 4596fd7ce5SGreg Kroah-Hartman * -- grif@cs.ucr.edu, 5-Dec-95 4696fd7ce5SGreg Kroah-Hartman * 4796fd7ce5SGreg Kroah-Hartman * Move console and virtual terminal code to more appropriate files, 4896fd7ce5SGreg Kroah-Hartman * implement CONFIG_VT and generalize console device interface. 4996fd7ce5SGreg Kroah-Hartman * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 5096fd7ce5SGreg Kroah-Hartman * 5196fd7ce5SGreg Kroah-Hartman * Rewrote tty_init_dev and tty_release_dev to eliminate races. 5296fd7ce5SGreg Kroah-Hartman * -- Bill Hawes <whawes@star.net>, June 97 5396fd7ce5SGreg Kroah-Hartman * 5496fd7ce5SGreg Kroah-Hartman * Added devfs support. 5596fd7ce5SGreg Kroah-Hartman * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998 5696fd7ce5SGreg Kroah-Hartman * 5796fd7ce5SGreg Kroah-Hartman * Added support for a Unix98-style ptmx device. 5896fd7ce5SGreg Kroah-Hartman * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 5996fd7ce5SGreg Kroah-Hartman * 6096fd7ce5SGreg Kroah-Hartman * Reduced memory usage for older ARM systems 6196fd7ce5SGreg Kroah-Hartman * -- Russell King <rmk@arm.linux.org.uk> 6296fd7ce5SGreg Kroah-Hartman * 6396fd7ce5SGreg Kroah-Hartman * Move do_SAK() into process context. Less stack use in devfs functions. 6496fd7ce5SGreg Kroah-Hartman * alloc_tty_struct() always uses kmalloc() 6596fd7ce5SGreg Kroah-Hartman * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01 6696fd7ce5SGreg Kroah-Hartman */ 6796fd7ce5SGreg Kroah-Hartman 6896fd7ce5SGreg Kroah-Hartman #include <linux/types.h> 6996fd7ce5SGreg Kroah-Hartman #include <linux/major.h> 7096fd7ce5SGreg Kroah-Hartman #include <linux/errno.h> 7196fd7ce5SGreg Kroah-Hartman #include <linux/signal.h> 7296fd7ce5SGreg Kroah-Hartman #include <linux/fcntl.h> 733f07c014SIngo Molnar #include <linux/sched/signal.h> 7429930025SIngo Molnar #include <linux/sched/task.h> 7596fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h> 7696fd7ce5SGreg Kroah-Hartman #include <linux/tty.h> 7796fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h> 7896fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h> 7996fd7ce5SGreg Kroah-Hartman #include <linux/devpts_fs.h> 8096fd7ce5SGreg Kroah-Hartman #include <linux/file.h> 8196fd7ce5SGreg Kroah-Hartman #include <linux/fdtable.h> 8296fd7ce5SGreg Kroah-Hartman #include <linux/console.h> 8396fd7ce5SGreg Kroah-Hartman #include <linux/timer.h> 8496fd7ce5SGreg Kroah-Hartman #include <linux/ctype.h> 8596fd7ce5SGreg Kroah-Hartman #include <linux/kd.h> 8696fd7ce5SGreg Kroah-Hartman #include <linux/mm.h> 8796fd7ce5SGreg Kroah-Hartman #include <linux/string.h> 8896fd7ce5SGreg Kroah-Hartman #include <linux/slab.h> 8996fd7ce5SGreg Kroah-Hartman #include <linux/poll.h> 9096fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h> 9196fd7ce5SGreg Kroah-Hartman #include <linux/init.h> 9296fd7ce5SGreg Kroah-Hartman #include <linux/module.h> 9396fd7ce5SGreg Kroah-Hartman #include <linux/device.h> 9496fd7ce5SGreg Kroah-Hartman #include <linux/wait.h> 9596fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h> 9696fd7ce5SGreg Kroah-Hartman #include <linux/delay.h> 9796fd7ce5SGreg Kroah-Hartman #include <linux/seq_file.h> 9896fd7ce5SGreg Kroah-Hartman #include <linux/serial.h> 995a3c6b25SManuel Zerpies #include <linux/ratelimit.h> 10096fd7ce5SGreg Kroah-Hartman 10196fd7ce5SGreg Kroah-Hartman #include <linux/uaccess.h> 10296fd7ce5SGreg Kroah-Hartman 10396fd7ce5SGreg Kroah-Hartman #include <linux/kbd_kern.h> 10496fd7ce5SGreg Kroah-Hartman #include <linux/vt_kern.h> 10596fd7ce5SGreg Kroah-Hartman #include <linux/selection.h> 10696fd7ce5SGreg Kroah-Hartman 10796fd7ce5SGreg Kroah-Hartman #include <linux/kmod.h> 10896fd7ce5SGreg Kroah-Hartman #include <linux/nsproxy.h> 10996fd7ce5SGreg Kroah-Hartman 11096fd7ce5SGreg Kroah-Hartman #undef TTY_DEBUG_HANGUP 111accff793SPeter Hurley #ifdef TTY_DEBUG_HANGUP 112accff793SPeter Hurley # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args) 113accff793SPeter Hurley #else 114accff793SPeter Hurley # define tty_debug_hangup(tty, f, args...) do { } while (0) 115accff793SPeter Hurley #endif 11696fd7ce5SGreg Kroah-Hartman 11796fd7ce5SGreg Kroah-Hartman #define TTY_PARANOIA_CHECK 1 11896fd7ce5SGreg Kroah-Hartman #define CHECK_TTY_COUNT 1 11996fd7ce5SGreg Kroah-Hartman 12096fd7ce5SGreg Kroah-Hartman struct ktermios tty_std_termios = { /* for the benefit of tty drivers */ 12196fd7ce5SGreg Kroah-Hartman .c_iflag = ICRNL | IXON, 12296fd7ce5SGreg Kroah-Hartman .c_oflag = OPOST | ONLCR, 12396fd7ce5SGreg Kroah-Hartman .c_cflag = B38400 | CS8 | CREAD | HUPCL, 12496fd7ce5SGreg Kroah-Hartman .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | 12596fd7ce5SGreg Kroah-Hartman ECHOCTL | ECHOKE | IEXTEN, 12696fd7ce5SGreg Kroah-Hartman .c_cc = INIT_C_CC, 12796fd7ce5SGreg Kroah-Hartman .c_ispeed = 38400, 128133b1306SPeter Hurley .c_ospeed = 38400, 129133b1306SPeter Hurley /* .c_line = N_TTY, */ 13096fd7ce5SGreg Kroah-Hartman }; 13196fd7ce5SGreg Kroah-Hartman 13296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_std_termios); 13396fd7ce5SGreg Kroah-Hartman 13496fd7ce5SGreg Kroah-Hartman /* This list gets poked at by procfs and various bits of boot up code. This 13596fd7ce5SGreg Kroah-Hartman could do with some rationalisation such as pulling the tty proc function 13696fd7ce5SGreg Kroah-Hartman into this file */ 13796fd7ce5SGreg Kroah-Hartman 13896fd7ce5SGreg Kroah-Hartman LIST_HEAD(tty_drivers); /* linked list of tty drivers */ 13996fd7ce5SGreg Kroah-Hartman 140d1d027efSPeter Hurley /* Mutex to protect creating and releasing a tty */ 14196fd7ce5SGreg Kroah-Hartman DEFINE_MUTEX(tty_mutex); 14296fd7ce5SGreg Kroah-Hartman 14396fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); 14496fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); 14596fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *, const char __user *, 14696fd7ce5SGreg Kroah-Hartman size_t, loff_t *); 147afc9a42bSAl Viro static __poll_t tty_poll(struct file *, poll_table *); 14896fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *, struct file *); 14996fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 15096fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 15196fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 15296fd7ce5SGreg Kroah-Hartman unsigned long arg); 15396fd7ce5SGreg Kroah-Hartman #else 15496fd7ce5SGreg Kroah-Hartman #define tty_compat_ioctl NULL 15596fd7ce5SGreg Kroah-Hartman #endif 15696fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on); 15796fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on); 15896fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx); 15996fd7ce5SGreg Kroah-Hartman 16096fd7ce5SGreg Kroah-Hartman /** 16196fd7ce5SGreg Kroah-Hartman * free_tty_struct - free a disused tty 16296fd7ce5SGreg Kroah-Hartman * @tty: tty struct to free 16396fd7ce5SGreg Kroah-Hartman * 16496fd7ce5SGreg Kroah-Hartman * Free the write buffers, tty queue and tty memory itself. 16596fd7ce5SGreg Kroah-Hartman * 16696fd7ce5SGreg Kroah-Hartman * Locking: none. Must be called after tty is definitely unused 16796fd7ce5SGreg Kroah-Hartman */ 16896fd7ce5SGreg Kroah-Hartman 169a3123fd0SPeter Hurley static void free_tty_struct(struct tty_struct *tty) 17096fd7ce5SGreg Kroah-Hartman { 171c8b710b3SPeter Hurley tty_ldisc_deinit(tty); 17296fd7ce5SGreg Kroah-Hartman put_device(tty->dev); 17396fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 17489c8d91eSAlan Cox tty->magic = 0xDEADDEAD; 17596fd7ce5SGreg Kroah-Hartman kfree(tty); 17696fd7ce5SGreg Kroah-Hartman } 17796fd7ce5SGreg Kroah-Hartman 17896fd7ce5SGreg Kroah-Hartman static inline struct tty_struct *file_tty(struct file *file) 17996fd7ce5SGreg Kroah-Hartman { 18096fd7ce5SGreg Kroah-Hartman return ((struct tty_file_private *)file->private_data)->tty; 18196fd7ce5SGreg Kroah-Hartman } 18296fd7ce5SGreg Kroah-Hartman 183fa90e1c9SJiri Slaby int tty_alloc_file(struct file *file) 18496fd7ce5SGreg Kroah-Hartman { 18596fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 18696fd7ce5SGreg Kroah-Hartman 18796fd7ce5SGreg Kroah-Hartman priv = kmalloc(sizeof(*priv), GFP_KERNEL); 18896fd7ce5SGreg Kroah-Hartman if (!priv) 18996fd7ce5SGreg Kroah-Hartman return -ENOMEM; 19096fd7ce5SGreg Kroah-Hartman 191fa90e1c9SJiri Slaby file->private_data = priv; 192fa90e1c9SJiri Slaby 193fa90e1c9SJiri Slaby return 0; 194fa90e1c9SJiri Slaby } 195fa90e1c9SJiri Slaby 196fa90e1c9SJiri Slaby /* Associate a new file with the tty structure */ 197fa90e1c9SJiri Slaby void tty_add_file(struct tty_struct *tty, struct file *file) 198fa90e1c9SJiri Slaby { 199fa90e1c9SJiri Slaby struct tty_file_private *priv = file->private_data; 200fa90e1c9SJiri Slaby 20196fd7ce5SGreg Kroah-Hartman priv->tty = tty; 20296fd7ce5SGreg Kroah-Hartman priv->file = file; 20396fd7ce5SGreg Kroah-Hartman 2044a510969SPeter Hurley spin_lock(&tty->files_lock); 20596fd7ce5SGreg Kroah-Hartman list_add(&priv->list, &tty->tty_files); 2064a510969SPeter Hurley spin_unlock(&tty->files_lock); 207fa90e1c9SJiri Slaby } 20896fd7ce5SGreg Kroah-Hartman 209fa90e1c9SJiri Slaby /** 210fa90e1c9SJiri Slaby * tty_free_file - free file->private_data 211fa90e1c9SJiri Slaby * 212fa90e1c9SJiri Slaby * This shall be used only for fail path handling when tty_add_file was not 213fa90e1c9SJiri Slaby * called yet. 214fa90e1c9SJiri Slaby */ 215fa90e1c9SJiri Slaby void tty_free_file(struct file *file) 216fa90e1c9SJiri Slaby { 217fa90e1c9SJiri Slaby struct tty_file_private *priv = file->private_data; 218fa90e1c9SJiri Slaby 219fa90e1c9SJiri Slaby file->private_data = NULL; 220fa90e1c9SJiri Slaby kfree(priv); 22196fd7ce5SGreg Kroah-Hartman } 22296fd7ce5SGreg Kroah-Hartman 22396fd7ce5SGreg Kroah-Hartman /* Delete file from its tty */ 2242520e274SJosh Triplett static void tty_del_file(struct file *file) 22596fd7ce5SGreg Kroah-Hartman { 22696fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv = file->private_data; 2274a510969SPeter Hurley struct tty_struct *tty = priv->tty; 22896fd7ce5SGreg Kroah-Hartman 2294a510969SPeter Hurley spin_lock(&tty->files_lock); 23096fd7ce5SGreg Kroah-Hartman list_del(&priv->list); 2314a510969SPeter Hurley spin_unlock(&tty->files_lock); 232fa90e1c9SJiri Slaby tty_free_file(file); 23396fd7ce5SGreg Kroah-Hartman } 23496fd7ce5SGreg Kroah-Hartman 23596fd7ce5SGreg Kroah-Hartman /** 23696fd7ce5SGreg Kroah-Hartman * tty_name - return tty naming 23796fd7ce5SGreg Kroah-Hartman * @tty: tty structure 23896fd7ce5SGreg Kroah-Hartman * 23996fd7ce5SGreg Kroah-Hartman * Convert a tty structure into a name. The name reflects the kernel 24096fd7ce5SGreg Kroah-Hartman * naming policy and if udev is in use may not reflect user space 24196fd7ce5SGreg Kroah-Hartman * 24296fd7ce5SGreg Kroah-Hartman * Locking: none 24396fd7ce5SGreg Kroah-Hartman */ 24496fd7ce5SGreg Kroah-Hartman 245429b4749SRasmus Villemoes const char *tty_name(const struct tty_struct *tty) 24696fd7ce5SGreg Kroah-Hartman { 24796fd7ce5SGreg Kroah-Hartman if (!tty) /* Hmm. NULL pointer. That's fun. */ 248917162c9SRasmus Villemoes return "NULL tty"; 249917162c9SRasmus Villemoes return tty->name; 25096fd7ce5SGreg Kroah-Hartman } 25196fd7ce5SGreg Kroah-Hartman 25296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_name); 25396fd7ce5SGreg Kroah-Hartman 2540a083eddSPeter Hurley const char *tty_driver_name(const struct tty_struct *tty) 2550a083eddSPeter Hurley { 2560a083eddSPeter Hurley if (!tty || !tty->driver) 2570a083eddSPeter Hurley return ""; 2580a083eddSPeter Hurley return tty->driver->name; 2590a083eddSPeter Hurley } 2600a083eddSPeter Hurley 26182b8f888SPeter Hurley static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, 26296fd7ce5SGreg Kroah-Hartman const char *routine) 26396fd7ce5SGreg Kroah-Hartman { 26496fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK 26596fd7ce5SGreg Kroah-Hartman if (!tty) { 26689222e62SPeter Hurley pr_warn("(%d:%d): %s: NULL tty\n", 26796fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 26896fd7ce5SGreg Kroah-Hartman return 1; 26996fd7ce5SGreg Kroah-Hartman } 27096fd7ce5SGreg Kroah-Hartman if (tty->magic != TTY_MAGIC) { 27189222e62SPeter Hurley pr_warn("(%d:%d): %s: bad magic number\n", 27296fd7ce5SGreg Kroah-Hartman imajor(inode), iminor(inode), routine); 27396fd7ce5SGreg Kroah-Hartman return 1; 27496fd7ce5SGreg Kroah-Hartman } 27596fd7ce5SGreg Kroah-Hartman #endif 27696fd7ce5SGreg Kroah-Hartman return 0; 27796fd7ce5SGreg Kroah-Hartman } 27896fd7ce5SGreg Kroah-Hartman 279deb287e7SPeter Hurley /* Caller must hold tty_lock */ 28096fd7ce5SGreg Kroah-Hartman static int check_tty_count(struct tty_struct *tty, const char *routine) 28196fd7ce5SGreg Kroah-Hartman { 28296fd7ce5SGreg Kroah-Hartman #ifdef CHECK_TTY_COUNT 28396fd7ce5SGreg Kroah-Hartman struct list_head *p; 284a09ac397SOkash Khawaja int count = 0, kopen_count = 0; 28596fd7ce5SGreg Kroah-Hartman 2864a510969SPeter Hurley spin_lock(&tty->files_lock); 28796fd7ce5SGreg Kroah-Hartman list_for_each(p, &tty->tty_files) { 28896fd7ce5SGreg Kroah-Hartman count++; 28996fd7ce5SGreg Kroah-Hartman } 2904a510969SPeter Hurley spin_unlock(&tty->files_lock); 29196fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 29296fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_SLAVE && 29396fd7ce5SGreg Kroah-Hartman tty->link && tty->link->count) 29496fd7ce5SGreg Kroah-Hartman count++; 295a09ac397SOkash Khawaja if (tty_port_kopened(tty->port)) 296a09ac397SOkash Khawaja kopen_count++; 297a09ac397SOkash Khawaja if (tty->count != (count + kopen_count)) { 298a09ac397SOkash Khawaja tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", 299a09ac397SOkash Khawaja routine, tty->count, count, kopen_count); 300a09ac397SOkash Khawaja return (count + kopen_count); 30196fd7ce5SGreg Kroah-Hartman } 30296fd7ce5SGreg Kroah-Hartman #endif 30396fd7ce5SGreg Kroah-Hartman return 0; 30496fd7ce5SGreg Kroah-Hartman } 30596fd7ce5SGreg Kroah-Hartman 30696fd7ce5SGreg Kroah-Hartman /** 30796fd7ce5SGreg Kroah-Hartman * get_tty_driver - find device of a tty 30896fd7ce5SGreg Kroah-Hartman * @dev_t: device identifier 30996fd7ce5SGreg Kroah-Hartman * @index: returns the index of the tty 31096fd7ce5SGreg Kroah-Hartman * 31196fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a device number 31296fd7ce5SGreg Kroah-Hartman * and also passes back the index number. 31396fd7ce5SGreg Kroah-Hartman * 31496fd7ce5SGreg Kroah-Hartman * Locking: caller must hold tty_mutex 31596fd7ce5SGreg Kroah-Hartman */ 31696fd7ce5SGreg Kroah-Hartman 31796fd7ce5SGreg Kroah-Hartman static struct tty_driver *get_tty_driver(dev_t device, int *index) 31896fd7ce5SGreg Kroah-Hartman { 31996fd7ce5SGreg Kroah-Hartman struct tty_driver *p; 32096fd7ce5SGreg Kroah-Hartman 32196fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 32296fd7ce5SGreg Kroah-Hartman dev_t base = MKDEV(p->major, p->minor_start); 32396fd7ce5SGreg Kroah-Hartman if (device < base || device >= base + p->num) 32496fd7ce5SGreg Kroah-Hartman continue; 32596fd7ce5SGreg Kroah-Hartman *index = device - base; 32696fd7ce5SGreg Kroah-Hartman return tty_driver_kref_get(p); 32796fd7ce5SGreg Kroah-Hartman } 32896fd7ce5SGreg Kroah-Hartman return NULL; 32996fd7ce5SGreg Kroah-Hartman } 33096fd7ce5SGreg Kroah-Hartman 331fc61ed51SOkash Khawaja /** 332fc61ed51SOkash Khawaja * tty_dev_name_to_number - return dev_t for device name 333fc61ed51SOkash Khawaja * @name: user space name of device under /dev 334fc61ed51SOkash Khawaja * @number: pointer to dev_t that this function will populate 335fc61ed51SOkash Khawaja * 336fc61ed51SOkash Khawaja * This function converts device names like ttyS0 or ttyUSB1 into dev_t 337fc61ed51SOkash Khawaja * like (4, 64) or (188, 1). If no corresponding driver is registered then 338fc61ed51SOkash Khawaja * the function returns -ENODEV. 339fc61ed51SOkash Khawaja * 340fc61ed51SOkash Khawaja * Locking: this acquires tty_mutex to protect the tty_drivers list from 341fc61ed51SOkash Khawaja * being modified while we are traversing it, and makes sure to 342fc61ed51SOkash Khawaja * release it before exiting. 343fc61ed51SOkash Khawaja */ 344fc61ed51SOkash Khawaja int tty_dev_name_to_number(const char *name, dev_t *number) 345fc61ed51SOkash Khawaja { 346fc61ed51SOkash Khawaja struct tty_driver *p; 347fc61ed51SOkash Khawaja int ret; 348fc61ed51SOkash Khawaja int index, prefix_length = 0; 349fc61ed51SOkash Khawaja const char *str; 350fc61ed51SOkash Khawaja 351fc61ed51SOkash Khawaja for (str = name; *str && !isdigit(*str); str++) 352fc61ed51SOkash Khawaja ; 353fc61ed51SOkash Khawaja 354fc61ed51SOkash Khawaja if (!*str) 355fc61ed51SOkash Khawaja return -EINVAL; 356fc61ed51SOkash Khawaja 357fc61ed51SOkash Khawaja ret = kstrtoint(str, 10, &index); 358fc61ed51SOkash Khawaja if (ret) 359fc61ed51SOkash Khawaja return ret; 360fc61ed51SOkash Khawaja 361fc61ed51SOkash Khawaja prefix_length = str - name; 362fc61ed51SOkash Khawaja mutex_lock(&tty_mutex); 363fc61ed51SOkash Khawaja 364fc61ed51SOkash Khawaja list_for_each_entry(p, &tty_drivers, tty_drivers) 365fc61ed51SOkash Khawaja if (prefix_length == strlen(p->name) && strncmp(name, 366fc61ed51SOkash Khawaja p->name, prefix_length) == 0) { 367fc61ed51SOkash Khawaja if (index < p->num) { 368fc61ed51SOkash Khawaja *number = MKDEV(p->major, p->minor_start + index); 369fc61ed51SOkash Khawaja goto out; 370fc61ed51SOkash Khawaja } 371fc61ed51SOkash Khawaja } 372fc61ed51SOkash Khawaja 373fc61ed51SOkash Khawaja /* if here then driver wasn't found */ 374fc61ed51SOkash Khawaja ret = -ENODEV; 375fc61ed51SOkash Khawaja out: 376fc61ed51SOkash Khawaja mutex_unlock(&tty_mutex); 377fc61ed51SOkash Khawaja return ret; 378fc61ed51SOkash Khawaja } 379fc61ed51SOkash Khawaja EXPORT_SYMBOL_GPL(tty_dev_name_to_number); 380fc61ed51SOkash Khawaja 38196fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 38296fd7ce5SGreg Kroah-Hartman 38396fd7ce5SGreg Kroah-Hartman /** 38496fd7ce5SGreg Kroah-Hartman * tty_find_polling_driver - find device of a polled tty 38596fd7ce5SGreg Kroah-Hartman * @name: name string to match 38696fd7ce5SGreg Kroah-Hartman * @line: pointer to resulting tty line nr 38796fd7ce5SGreg Kroah-Hartman * 38896fd7ce5SGreg Kroah-Hartman * This routine returns a tty driver structure, given a name 38996fd7ce5SGreg Kroah-Hartman * and the condition that the tty driver is capable of polled 39096fd7ce5SGreg Kroah-Hartman * operation. 39196fd7ce5SGreg Kroah-Hartman */ 39296fd7ce5SGreg Kroah-Hartman struct tty_driver *tty_find_polling_driver(char *name, int *line) 39396fd7ce5SGreg Kroah-Hartman { 39496fd7ce5SGreg Kroah-Hartman struct tty_driver *p, *res = NULL; 39596fd7ce5SGreg Kroah-Hartman int tty_line = 0; 39696fd7ce5SGreg Kroah-Hartman int len; 39796fd7ce5SGreg Kroah-Hartman char *str, *stp; 39896fd7ce5SGreg Kroah-Hartman 39996fd7ce5SGreg Kroah-Hartman for (str = name; *str; str++) 40096fd7ce5SGreg Kroah-Hartman if ((*str >= '0' && *str <= '9') || *str == ',') 40196fd7ce5SGreg Kroah-Hartman break; 40296fd7ce5SGreg Kroah-Hartman if (!*str) 40396fd7ce5SGreg Kroah-Hartman return NULL; 40496fd7ce5SGreg Kroah-Hartman 40596fd7ce5SGreg Kroah-Hartman len = str - name; 40696fd7ce5SGreg Kroah-Hartman tty_line = simple_strtoul(str, &str, 10); 40796fd7ce5SGreg Kroah-Hartman 40896fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 40996fd7ce5SGreg Kroah-Hartman /* Search through the tty devices to look for a match */ 41096fd7ce5SGreg Kroah-Hartman list_for_each_entry(p, &tty_drivers, tty_drivers) { 41196fd7ce5SGreg Kroah-Hartman if (strncmp(name, p->name, len) != 0) 41296fd7ce5SGreg Kroah-Hartman continue; 41396fd7ce5SGreg Kroah-Hartman stp = str; 41496fd7ce5SGreg Kroah-Hartman if (*stp == ',') 41596fd7ce5SGreg Kroah-Hartman stp++; 41696fd7ce5SGreg Kroah-Hartman if (*stp == '\0') 41796fd7ce5SGreg Kroah-Hartman stp = NULL; 41896fd7ce5SGreg Kroah-Hartman 41996fd7ce5SGreg Kroah-Hartman if (tty_line >= 0 && tty_line < p->num && p->ops && 42096fd7ce5SGreg Kroah-Hartman p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { 42196fd7ce5SGreg Kroah-Hartman res = tty_driver_kref_get(p); 42296fd7ce5SGreg Kroah-Hartman *line = tty_line; 42396fd7ce5SGreg Kroah-Hartman break; 42496fd7ce5SGreg Kroah-Hartman } 42596fd7ce5SGreg Kroah-Hartman } 42696fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 42796fd7ce5SGreg Kroah-Hartman 42896fd7ce5SGreg Kroah-Hartman return res; 42996fd7ce5SGreg Kroah-Hartman } 43096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_find_polling_driver); 43196fd7ce5SGreg Kroah-Hartman #endif 43296fd7ce5SGreg Kroah-Hartman 43396fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_read(struct file *file, char __user *buf, 43496fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 43596fd7ce5SGreg Kroah-Hartman { 43696fd7ce5SGreg Kroah-Hartman return 0; 43796fd7ce5SGreg Kroah-Hartman } 43896fd7ce5SGreg Kroah-Hartman 43996fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_write(struct file *file, const char __user *buf, 44096fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 44196fd7ce5SGreg Kroah-Hartman { 44296fd7ce5SGreg Kroah-Hartman return -EIO; 44396fd7ce5SGreg Kroah-Hartman } 44496fd7ce5SGreg Kroah-Hartman 44596fd7ce5SGreg Kroah-Hartman /* No kernel lock held - none needed ;) */ 446afc9a42bSAl Viro static __poll_t hung_up_tty_poll(struct file *filp, poll_table *wait) 44796fd7ce5SGreg Kroah-Hartman { 448a9a08845SLinus Torvalds return EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP | EPOLLRDNORM | EPOLLWRNORM; 44996fd7ce5SGreg Kroah-Hartman } 45096fd7ce5SGreg Kroah-Hartman 45196fd7ce5SGreg Kroah-Hartman static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, 45296fd7ce5SGreg Kroah-Hartman unsigned long arg) 45396fd7ce5SGreg Kroah-Hartman { 45496fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 45596fd7ce5SGreg Kroah-Hartman } 45696fd7ce5SGreg Kroah-Hartman 45796fd7ce5SGreg Kroah-Hartman static long hung_up_tty_compat_ioctl(struct file *file, 45896fd7ce5SGreg Kroah-Hartman unsigned int cmd, unsigned long arg) 45996fd7ce5SGreg Kroah-Hartman { 46096fd7ce5SGreg Kroah-Hartman return cmd == TIOCSPGRP ? -ENOTTY : -EIO; 46196fd7ce5SGreg Kroah-Hartman } 46296fd7ce5SGreg Kroah-Hartman 463f557474cSPeter Hurley static int hung_up_tty_fasync(int fd, struct file *file, int on) 464f557474cSPeter Hurley { 465f557474cSPeter Hurley return -ENOTTY; 466f557474cSPeter Hurley } 467f557474cSPeter Hurley 468d01c3289SMasatake YAMATO static void tty_show_fdinfo(struct seq_file *m, struct file *file) 469d01c3289SMasatake YAMATO { 470d01c3289SMasatake YAMATO struct tty_struct *tty = file_tty(file); 471d01c3289SMasatake YAMATO 472d01c3289SMasatake YAMATO if (tty && tty->ops && tty->ops->show_fdinfo) 473d01c3289SMasatake YAMATO tty->ops->show_fdinfo(tty, m); 474d01c3289SMasatake YAMATO } 475d01c3289SMasatake YAMATO 47696fd7ce5SGreg Kroah-Hartman static const struct file_operations tty_fops = { 47796fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 47896fd7ce5SGreg Kroah-Hartman .read = tty_read, 47996fd7ce5SGreg Kroah-Hartman .write = tty_write, 48096fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 48196fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 48296fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 48396fd7ce5SGreg Kroah-Hartman .open = tty_open, 48496fd7ce5SGreg Kroah-Hartman .release = tty_release, 48596fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 486d01c3289SMasatake YAMATO .show_fdinfo = tty_show_fdinfo, 48796fd7ce5SGreg Kroah-Hartman }; 48896fd7ce5SGreg Kroah-Hartman 48996fd7ce5SGreg Kroah-Hartman static const struct file_operations console_fops = { 49096fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 49196fd7ce5SGreg Kroah-Hartman .read = tty_read, 49296fd7ce5SGreg Kroah-Hartman .write = redirected_tty_write, 49396fd7ce5SGreg Kroah-Hartman .poll = tty_poll, 49496fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = tty_ioctl, 49596fd7ce5SGreg Kroah-Hartman .compat_ioctl = tty_compat_ioctl, 49696fd7ce5SGreg Kroah-Hartman .open = tty_open, 49796fd7ce5SGreg Kroah-Hartman .release = tty_release, 49896fd7ce5SGreg Kroah-Hartman .fasync = tty_fasync, 49996fd7ce5SGreg Kroah-Hartman }; 50096fd7ce5SGreg Kroah-Hartman 50196fd7ce5SGreg Kroah-Hartman static const struct file_operations hung_up_tty_fops = { 50296fd7ce5SGreg Kroah-Hartman .llseek = no_llseek, 50396fd7ce5SGreg Kroah-Hartman .read = hung_up_tty_read, 50496fd7ce5SGreg Kroah-Hartman .write = hung_up_tty_write, 50596fd7ce5SGreg Kroah-Hartman .poll = hung_up_tty_poll, 50696fd7ce5SGreg Kroah-Hartman .unlocked_ioctl = hung_up_tty_ioctl, 50796fd7ce5SGreg Kroah-Hartman .compat_ioctl = hung_up_tty_compat_ioctl, 50896fd7ce5SGreg Kroah-Hartman .release = tty_release, 509f557474cSPeter Hurley .fasync = hung_up_tty_fasync, 51096fd7ce5SGreg Kroah-Hartman }; 51196fd7ce5SGreg Kroah-Hartman 51296fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(redirect_lock); 51396fd7ce5SGreg Kroah-Hartman static struct file *redirect; 51496fd7ce5SGreg Kroah-Hartman 51596fd7ce5SGreg Kroah-Hartman /** 51696fd7ce5SGreg Kroah-Hartman * tty_wakeup - request more data 51796fd7ce5SGreg Kroah-Hartman * @tty: terminal 51896fd7ce5SGreg Kroah-Hartman * 51996fd7ce5SGreg Kroah-Hartman * Internal and external helper for wakeups of tty. This function 52096fd7ce5SGreg Kroah-Hartman * informs the line discipline if present that the driver is ready 52196fd7ce5SGreg Kroah-Hartman * to receive more output data. 52296fd7ce5SGreg Kroah-Hartman */ 52396fd7ce5SGreg Kroah-Hartman 52496fd7ce5SGreg Kroah-Hartman void tty_wakeup(struct tty_struct *tty) 52596fd7ce5SGreg Kroah-Hartman { 52696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 52796fd7ce5SGreg Kroah-Hartman 52896fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { 52996fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref(tty); 53096fd7ce5SGreg Kroah-Hartman if (ld) { 53196fd7ce5SGreg Kroah-Hartman if (ld->ops->write_wakeup) 53296fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup(tty); 53396fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 53496fd7ce5SGreg Kroah-Hartman } 53596fd7ce5SGreg Kroah-Hartman } 536a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); 53796fd7ce5SGreg Kroah-Hartman } 53896fd7ce5SGreg Kroah-Hartman 53996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_wakeup); 54096fd7ce5SGreg Kroah-Hartman 54196fd7ce5SGreg Kroah-Hartman /** 54296fd7ce5SGreg Kroah-Hartman * __tty_hangup - actual handler for hangup events 54396fd7ce5SGreg Kroah-Hartman * @work: tty device 54496fd7ce5SGreg Kroah-Hartman * 545ef4f527cSKevin Cernekee * This can be called by a "kworker" kernel thread. That is process 54696fd7ce5SGreg Kroah-Hartman * synchronous but doesn't hold any locks, so we need to make sure we 54796fd7ce5SGreg Kroah-Hartman * have the appropriate locks for what we're doing. 54896fd7ce5SGreg Kroah-Hartman * 54996fd7ce5SGreg Kroah-Hartman * The hangup event clears any pending redirections onto the hung up 55096fd7ce5SGreg Kroah-Hartman * device. It ensures future writes will error and it does the needed 55196fd7ce5SGreg Kroah-Hartman * line discipline hangup and signal delivery. The tty object itself 55296fd7ce5SGreg Kroah-Hartman * remains intact. 55396fd7ce5SGreg Kroah-Hartman * 55496fd7ce5SGreg Kroah-Hartman * Locking: 55596fd7ce5SGreg Kroah-Hartman * BTM 55696fd7ce5SGreg Kroah-Hartman * redirect lock for undoing redirection 55796fd7ce5SGreg Kroah-Hartman * file list lock for manipulating list of ttys 558137084bbSPeter Hurley * tty_ldiscs_lock from called functions 5596a1c0680SPeter Hurley * termios_rwsem resetting termios data 56096fd7ce5SGreg Kroah-Hartman * tasklist_lock to walk task list for hangup event 56196fd7ce5SGreg Kroah-Hartman * ->siglock to protect ->signal/->sighand 56296fd7ce5SGreg Kroah-Hartman */ 563f91e2590SPeter Hurley static void __tty_hangup(struct tty_struct *tty, int exit_session) 56496fd7ce5SGreg Kroah-Hartman { 56596fd7ce5SGreg Kroah-Hartman struct file *cons_filp = NULL; 56696fd7ce5SGreg Kroah-Hartman struct file *filp, *f = NULL; 56796fd7ce5SGreg Kroah-Hartman struct tty_file_private *priv; 56896fd7ce5SGreg Kroah-Hartman int closecount = 0, n; 569ea648a47SPeter Hurley int refs; 57096fd7ce5SGreg Kroah-Hartman 57196fd7ce5SGreg Kroah-Hartman if (!tty) 57296fd7ce5SGreg Kroah-Hartman return; 57396fd7ce5SGreg Kroah-Hartman 57496fd7ce5SGreg Kroah-Hartman 57596fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 57696fd7ce5SGreg Kroah-Hartman if (redirect && file_tty(redirect) == tty) { 57796fd7ce5SGreg Kroah-Hartman f = redirect; 57896fd7ce5SGreg Kroah-Hartman redirect = NULL; 57996fd7ce5SGreg Kroah-Hartman } 58096fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 58196fd7ce5SGreg Kroah-Hartman 58289c8d91eSAlan Cox tty_lock(tty); 58396fd7ce5SGreg Kroah-Hartman 584cb50e523SPeter Hurley if (test_bit(TTY_HUPPED, &tty->flags)) { 585cb50e523SPeter Hurley tty_unlock(tty); 586cb50e523SPeter Hurley return; 587cb50e523SPeter Hurley } 588cb50e523SPeter Hurley 58928b0f8a6STejun Heo /* 59028b0f8a6STejun Heo * Some console devices aren't actually hung up for technical and 59128b0f8a6STejun Heo * historical reasons, which can lead to indefinite interruptible 59228b0f8a6STejun Heo * sleep in n_tty_read(). The following explicitly tells 59328b0f8a6STejun Heo * n_tty_read() to abort readers. 59428b0f8a6STejun Heo */ 59528b0f8a6STejun Heo set_bit(TTY_HUPPING, &tty->flags); 59628b0f8a6STejun Heo 59796fd7ce5SGreg Kroah-Hartman /* inuse_filps is protected by the single tty lock, 59896fd7ce5SGreg Kroah-Hartman this really needs to change if we want to flush the 59996fd7ce5SGreg Kroah-Hartman workqueue with the lock held */ 60096fd7ce5SGreg Kroah-Hartman check_tty_count(tty, "tty_hangup"); 60196fd7ce5SGreg Kroah-Hartman 6024a510969SPeter Hurley spin_lock(&tty->files_lock); 60396fd7ce5SGreg Kroah-Hartman /* This breaks for file handles being sent over AF_UNIX sockets ? */ 60496fd7ce5SGreg Kroah-Hartman list_for_each_entry(priv, &tty->tty_files, list) { 60596fd7ce5SGreg Kroah-Hartman filp = priv->file; 60696fd7ce5SGreg Kroah-Hartman if (filp->f_op->write == redirected_tty_write) 60796fd7ce5SGreg Kroah-Hartman cons_filp = filp; 60896fd7ce5SGreg Kroah-Hartman if (filp->f_op->write != tty_write) 60996fd7ce5SGreg Kroah-Hartman continue; 61096fd7ce5SGreg Kroah-Hartman closecount++; 61196fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); /* can't block */ 61296fd7ce5SGreg Kroah-Hartman filp->f_op = &hung_up_tty_fops; 61396fd7ce5SGreg Kroah-Hartman } 6144a510969SPeter Hurley spin_unlock(&tty->files_lock); 61596fd7ce5SGreg Kroah-Hartman 61625fdf243SPeter Hurley refs = tty_signal_session_leader(tty, exit_session); 61725fdf243SPeter Hurley /* Account for the p->signal references we killed */ 61825fdf243SPeter Hurley while (refs--) 61925fdf243SPeter Hurley tty_kref_put(tty); 62025fdf243SPeter Hurley 621892d1fa7SPeter Hurley tty_ldisc_hangup(tty, cons_filp != NULL); 62296fd7ce5SGreg Kroah-Hartman 62320cc225bSPeter Hurley spin_lock_irq(&tty->ctrl_lock); 62496fd7ce5SGreg Kroah-Hartman clear_bit(TTY_THROTTLED, &tty->flags); 62596fd7ce5SGreg Kroah-Hartman clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 62696fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 62796fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 62896fd7ce5SGreg Kroah-Hartman tty->session = NULL; 62996fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 63096fd7ce5SGreg Kroah-Hartman tty->ctrl_status = 0; 63120cc225bSPeter Hurley spin_unlock_irq(&tty->ctrl_lock); 63296fd7ce5SGreg Kroah-Hartman 63396fd7ce5SGreg Kroah-Hartman /* 63496fd7ce5SGreg Kroah-Hartman * If one of the devices matches a console pointer, we 63596fd7ce5SGreg Kroah-Hartman * cannot just call hangup() because that will cause 63696fd7ce5SGreg Kroah-Hartman * tty->count and state->count to go out of sync. 63796fd7ce5SGreg Kroah-Hartman * So we just call close() the right number of times. 63896fd7ce5SGreg Kroah-Hartman */ 63996fd7ce5SGreg Kroah-Hartman if (cons_filp) { 64096fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 64196fd7ce5SGreg Kroah-Hartman for (n = 0; n < closecount; n++) 64296fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, cons_filp); 64396fd7ce5SGreg Kroah-Hartman } else if (tty->ops->hangup) 6447c6d340fSPeter Hurley tty->ops->hangup(tty); 64596fd7ce5SGreg Kroah-Hartman /* 646892d1fa7SPeter Hurley * We don't want to have driver/ldisc interactions beyond the ones 647892d1fa7SPeter Hurley * we did here. The driver layer expects no calls after ->hangup() 648892d1fa7SPeter Hurley * from the ldisc side, which is now guaranteed. 64996fd7ce5SGreg Kroah-Hartman */ 65096fd7ce5SGreg Kroah-Hartman set_bit(TTY_HUPPED, &tty->flags); 65128b0f8a6STejun Heo clear_bit(TTY_HUPPING, &tty->flags); 65289c8d91eSAlan Cox tty_unlock(tty); 65396fd7ce5SGreg Kroah-Hartman 65496fd7ce5SGreg Kroah-Hartman if (f) 65596fd7ce5SGreg Kroah-Hartman fput(f); 65696fd7ce5SGreg Kroah-Hartman } 65796fd7ce5SGreg Kroah-Hartman 65896fd7ce5SGreg Kroah-Hartman static void do_tty_hangup(struct work_struct *work) 65996fd7ce5SGreg Kroah-Hartman { 66096fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 66196fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 66296fd7ce5SGreg Kroah-Hartman 663f91e2590SPeter Hurley __tty_hangup(tty, 0); 66496fd7ce5SGreg Kroah-Hartman } 66596fd7ce5SGreg Kroah-Hartman 66696fd7ce5SGreg Kroah-Hartman /** 66796fd7ce5SGreg Kroah-Hartman * tty_hangup - trigger a hangup event 66896fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 66996fd7ce5SGreg Kroah-Hartman * 67096fd7ce5SGreg Kroah-Hartman * A carrier loss (virtual or otherwise) has occurred on this like 67196fd7ce5SGreg Kroah-Hartman * schedule a hangup sequence to run after this event. 67296fd7ce5SGreg Kroah-Hartman */ 67396fd7ce5SGreg Kroah-Hartman 67496fd7ce5SGreg Kroah-Hartman void tty_hangup(struct tty_struct *tty) 67596fd7ce5SGreg Kroah-Hartman { 676d435cefeSPeter Hurley tty_debug_hangup(tty, "hangup\n"); 67796fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 67896fd7ce5SGreg Kroah-Hartman } 67996fd7ce5SGreg Kroah-Hartman 68096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hangup); 68196fd7ce5SGreg Kroah-Hartman 68296fd7ce5SGreg Kroah-Hartman /** 68396fd7ce5SGreg Kroah-Hartman * tty_vhangup - process vhangup 68496fd7ce5SGreg Kroah-Hartman * @tty: tty to hangup 68596fd7ce5SGreg Kroah-Hartman * 68696fd7ce5SGreg Kroah-Hartman * The user has asked via system call for the terminal to be hung up. 68796fd7ce5SGreg Kroah-Hartman * We do this synchronously so that when the syscall returns the process 68896fd7ce5SGreg Kroah-Hartman * is complete. That guarantee is necessary for security reasons. 68996fd7ce5SGreg Kroah-Hartman */ 69096fd7ce5SGreg Kroah-Hartman 69196fd7ce5SGreg Kroah-Hartman void tty_vhangup(struct tty_struct *tty) 69296fd7ce5SGreg Kroah-Hartman { 693d435cefeSPeter Hurley tty_debug_hangup(tty, "vhangup\n"); 694f91e2590SPeter Hurley __tty_hangup(tty, 0); 69596fd7ce5SGreg Kroah-Hartman } 69696fd7ce5SGreg Kroah-Hartman 69796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_vhangup); 69896fd7ce5SGreg Kroah-Hartman 69996fd7ce5SGreg Kroah-Hartman 70096fd7ce5SGreg Kroah-Hartman /** 70196fd7ce5SGreg Kroah-Hartman * tty_vhangup_self - process vhangup for own ctty 70296fd7ce5SGreg Kroah-Hartman * 70396fd7ce5SGreg Kroah-Hartman * Perform a vhangup on the current controlling tty 70496fd7ce5SGreg Kroah-Hartman */ 70596fd7ce5SGreg Kroah-Hartman 70696fd7ce5SGreg Kroah-Hartman void tty_vhangup_self(void) 70796fd7ce5SGreg Kroah-Hartman { 70896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 70996fd7ce5SGreg Kroah-Hartman 71096fd7ce5SGreg Kroah-Hartman tty = get_current_tty(); 71196fd7ce5SGreg Kroah-Hartman if (tty) { 71296fd7ce5SGreg Kroah-Hartman tty_vhangup(tty); 71396fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 71496fd7ce5SGreg Kroah-Hartman } 71596fd7ce5SGreg Kroah-Hartman } 71696fd7ce5SGreg Kroah-Hartman 71796fd7ce5SGreg Kroah-Hartman /** 718f91e2590SPeter Hurley * tty_vhangup_session - hangup session leader exit 719f91e2590SPeter Hurley * @tty: tty to hangup 720f91e2590SPeter Hurley * 721f91e2590SPeter Hurley * The session leader is exiting and hanging up its controlling terminal. 722f91e2590SPeter Hurley * Every process in the foreground process group is signalled SIGHUP. 723f91e2590SPeter Hurley * 724f91e2590SPeter Hurley * We do this synchronously so that when the syscall returns the process 725f91e2590SPeter Hurley * is complete. That guarantee is necessary for security reasons. 726f91e2590SPeter Hurley */ 727f91e2590SPeter Hurley 728a1235b3eSNicolas Pitre void tty_vhangup_session(struct tty_struct *tty) 729f91e2590SPeter Hurley { 730d435cefeSPeter Hurley tty_debug_hangup(tty, "session hangup\n"); 731f91e2590SPeter Hurley __tty_hangup(tty, 1); 732f91e2590SPeter Hurley } 733f91e2590SPeter Hurley 734f91e2590SPeter Hurley /** 73596fd7ce5SGreg Kroah-Hartman * tty_hung_up_p - was tty hung up 73696fd7ce5SGreg Kroah-Hartman * @filp: file pointer of tty 73796fd7ce5SGreg Kroah-Hartman * 73896fd7ce5SGreg Kroah-Hartman * Return true if the tty has been subject to a vhangup or a carrier 73996fd7ce5SGreg Kroah-Hartman * loss 74096fd7ce5SGreg Kroah-Hartman */ 74196fd7ce5SGreg Kroah-Hartman 74296fd7ce5SGreg Kroah-Hartman int tty_hung_up_p(struct file *filp) 74396fd7ce5SGreg Kroah-Hartman { 744ed3f0af8SAlan Cox return (filp && filp->f_op == &hung_up_tty_fops); 74596fd7ce5SGreg Kroah-Hartman } 74696fd7ce5SGreg Kroah-Hartman 74796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hung_up_p); 74896fd7ce5SGreg Kroah-Hartman 74996fd7ce5SGreg Kroah-Hartman /** 75096fd7ce5SGreg Kroah-Hartman * stop_tty - propagate flow control 75196fd7ce5SGreg Kroah-Hartman * @tty: tty to stop 75296fd7ce5SGreg Kroah-Hartman * 75301adc807SPeter Hurley * Perform flow control to the driver. May be called 75496fd7ce5SGreg Kroah-Hartman * on an already stopped device and will not re-call the driver 75596fd7ce5SGreg Kroah-Hartman * method. 75696fd7ce5SGreg Kroah-Hartman * 75796fd7ce5SGreg Kroah-Hartman * This functionality is used by both the line disciplines for 75896fd7ce5SGreg Kroah-Hartman * halting incoming flow and by the driver. It may therefore be 75996fd7ce5SGreg Kroah-Hartman * called from any context, may be under the tty atomic_write_lock 76096fd7ce5SGreg Kroah-Hartman * but not always. 76196fd7ce5SGreg Kroah-Hartman * 76296fd7ce5SGreg Kroah-Hartman * Locking: 763f9e053dcSPeter Hurley * flow_lock 76496fd7ce5SGreg Kroah-Hartman */ 76596fd7ce5SGreg Kroah-Hartman 766f9e053dcSPeter Hurley void __stop_tty(struct tty_struct *tty) 76796fd7ce5SGreg Kroah-Hartman { 768f9e053dcSPeter Hurley if (tty->stopped) 76996fd7ce5SGreg Kroah-Hartman return; 77096fd7ce5SGreg Kroah-Hartman tty->stopped = 1; 77196fd7ce5SGreg Kroah-Hartman if (tty->ops->stop) 772c961bfb1SPeter Hurley tty->ops->stop(tty); 77396fd7ce5SGreg Kroah-Hartman } 77496fd7ce5SGreg Kroah-Hartman 775f9e053dcSPeter Hurley void stop_tty(struct tty_struct *tty) 776f9e053dcSPeter Hurley { 777f9e053dcSPeter Hurley unsigned long flags; 778f9e053dcSPeter Hurley 779f9e053dcSPeter Hurley spin_lock_irqsave(&tty->flow_lock, flags); 780f9e053dcSPeter Hurley __stop_tty(tty); 781f9e053dcSPeter Hurley spin_unlock_irqrestore(&tty->flow_lock, flags); 782f9e053dcSPeter Hurley } 78396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(stop_tty); 78496fd7ce5SGreg Kroah-Hartman 78596fd7ce5SGreg Kroah-Hartman /** 78696fd7ce5SGreg Kroah-Hartman * start_tty - propagate flow control 78796fd7ce5SGreg Kroah-Hartman * @tty: tty to start 78896fd7ce5SGreg Kroah-Hartman * 78901adc807SPeter Hurley * Start a tty that has been stopped if at all possible. If this 79001adc807SPeter Hurley * tty was previous stopped and is now being started, the driver 79101adc807SPeter Hurley * start method is invoked and the line discipline woken. 79296fd7ce5SGreg Kroah-Hartman * 79396fd7ce5SGreg Kroah-Hartman * Locking: 794f9e053dcSPeter Hurley * flow_lock 79596fd7ce5SGreg Kroah-Hartman */ 79696fd7ce5SGreg Kroah-Hartman 797f9e053dcSPeter Hurley void __start_tty(struct tty_struct *tty) 79896fd7ce5SGreg Kroah-Hartman { 799f9e053dcSPeter Hurley if (!tty->stopped || tty->flow_stopped) 80096fd7ce5SGreg Kroah-Hartman return; 80196fd7ce5SGreg Kroah-Hartman tty->stopped = 0; 80296fd7ce5SGreg Kroah-Hartman if (tty->ops->start) 803c961bfb1SPeter Hurley tty->ops->start(tty); 80496fd7ce5SGreg Kroah-Hartman tty_wakeup(tty); 80596fd7ce5SGreg Kroah-Hartman } 80696fd7ce5SGreg Kroah-Hartman 807f9e053dcSPeter Hurley void start_tty(struct tty_struct *tty) 808f9e053dcSPeter Hurley { 809f9e053dcSPeter Hurley unsigned long flags; 810f9e053dcSPeter Hurley 811f9e053dcSPeter Hurley spin_lock_irqsave(&tty->flow_lock, flags); 812f9e053dcSPeter Hurley __start_tty(tty); 813f9e053dcSPeter Hurley spin_unlock_irqrestore(&tty->flow_lock, flags); 814f9e053dcSPeter Hurley } 81596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(start_tty); 81696fd7ce5SGreg Kroah-Hartman 81737b7f3c7SJiri Slaby static void tty_update_time(struct timespec *time) 81837b7f3c7SJiri Slaby { 819f0bf0bd0SJiri Slaby unsigned long sec = get_seconds(); 820fbf47635SGreg Kroah-Hartman 821fbf47635SGreg Kroah-Hartman /* 822fbf47635SGreg Kroah-Hartman * We only care if the two values differ in anything other than the 823fbf47635SGreg Kroah-Hartman * lower three bits (i.e every 8 seconds). If so, then we can update 824fbf47635SGreg Kroah-Hartman * the time of the tty device, otherwise it could be construded as a 825fbf47635SGreg Kroah-Hartman * security leak to let userspace know the exact timing of the tty. 826fbf47635SGreg Kroah-Hartman */ 827fbf47635SGreg Kroah-Hartman if ((sec ^ time->tv_sec) & ~7) 82837b7f3c7SJiri Slaby time->tv_sec = sec; 82937b7f3c7SJiri Slaby } 83037b7f3c7SJiri Slaby 83196fd7ce5SGreg Kroah-Hartman /** 83296fd7ce5SGreg Kroah-Hartman * tty_read - read method for tty device files 83396fd7ce5SGreg Kroah-Hartman * @file: pointer to tty file 83496fd7ce5SGreg Kroah-Hartman * @buf: user buffer 83596fd7ce5SGreg Kroah-Hartman * @count: size of user buffer 83696fd7ce5SGreg Kroah-Hartman * @ppos: unused 83796fd7ce5SGreg Kroah-Hartman * 83896fd7ce5SGreg Kroah-Hartman * Perform the read system call function on this terminal device. Checks 83996fd7ce5SGreg Kroah-Hartman * for hung up devices before calling the line discipline method. 84096fd7ce5SGreg Kroah-Hartman * 84196fd7ce5SGreg Kroah-Hartman * Locking: 84296fd7ce5SGreg Kroah-Hartman * Locks the line discipline internally while needed. Multiple 84396fd7ce5SGreg Kroah-Hartman * read calls may be outstanding in parallel. 84496fd7ce5SGreg Kroah-Hartman */ 84596fd7ce5SGreg Kroah-Hartman 84696fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *file, char __user *buf, size_t count, 84796fd7ce5SGreg Kroah-Hartman loff_t *ppos) 84896fd7ce5SGreg Kroah-Hartman { 84996fd7ce5SGreg Kroah-Hartman int i; 85037b7f3c7SJiri Slaby struct inode *inode = file_inode(file); 85196fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 85296fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 85396fd7ce5SGreg Kroah-Hartman 85437b7f3c7SJiri Slaby if (tty_paranoia_check(tty, inode, "tty_read")) 85596fd7ce5SGreg Kroah-Hartman return -EIO; 85618900ca6SPeter Hurley if (!tty || tty_io_error(tty)) 85796fd7ce5SGreg Kroah-Hartman return -EIO; 85896fd7ce5SGreg Kroah-Hartman 85996fd7ce5SGreg Kroah-Hartman /* We want to wait for the line discipline to sort out in this 86096fd7ce5SGreg Kroah-Hartman situation */ 86196fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 862e55afd11SPeter Hurley if (!ld) 863e55afd11SPeter Hurley return hung_up_tty_read(file, buf, count, ppos); 86496fd7ce5SGreg Kroah-Hartman if (ld->ops->read) 865c961bfb1SPeter Hurley i = ld->ops->read(tty, file, buf, count); 86696fd7ce5SGreg Kroah-Hartman else 86796fd7ce5SGreg Kroah-Hartman i = -EIO; 86896fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 869b0de59b5SJiri Slaby 87037b7f3c7SJiri Slaby if (i > 0) 87137b7f3c7SJiri Slaby tty_update_time(&inode->i_atime); 87237b7f3c7SJiri Slaby 87396fd7ce5SGreg Kroah-Hartman return i; 87496fd7ce5SGreg Kroah-Hartman } 87596fd7ce5SGreg Kroah-Hartman 876136d5258SPeter Hurley static void tty_write_unlock(struct tty_struct *tty) 87796fd7ce5SGreg Kroah-Hartman { 87896fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->atomic_write_lock); 879a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); 88096fd7ce5SGreg Kroah-Hartman } 88196fd7ce5SGreg Kroah-Hartman 882136d5258SPeter Hurley static int tty_write_lock(struct tty_struct *tty, int ndelay) 88396fd7ce5SGreg Kroah-Hartman { 88496fd7ce5SGreg Kroah-Hartman if (!mutex_trylock(&tty->atomic_write_lock)) { 88596fd7ce5SGreg Kroah-Hartman if (ndelay) 88696fd7ce5SGreg Kroah-Hartman return -EAGAIN; 88796fd7ce5SGreg Kroah-Hartman if (mutex_lock_interruptible(&tty->atomic_write_lock)) 88896fd7ce5SGreg Kroah-Hartman return -ERESTARTSYS; 88996fd7ce5SGreg Kroah-Hartman } 89096fd7ce5SGreg Kroah-Hartman return 0; 89196fd7ce5SGreg Kroah-Hartman } 89296fd7ce5SGreg Kroah-Hartman 89396fd7ce5SGreg Kroah-Hartman /* 89496fd7ce5SGreg Kroah-Hartman * Split writes up in sane blocksizes to avoid 89596fd7ce5SGreg Kroah-Hartman * denial-of-service type attacks 89696fd7ce5SGreg Kroah-Hartman */ 89796fd7ce5SGreg Kroah-Hartman static inline ssize_t do_tty_write( 89896fd7ce5SGreg Kroah-Hartman ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), 89996fd7ce5SGreg Kroah-Hartman struct tty_struct *tty, 90096fd7ce5SGreg Kroah-Hartman struct file *file, 90196fd7ce5SGreg Kroah-Hartman const char __user *buf, 90296fd7ce5SGreg Kroah-Hartman size_t count) 90396fd7ce5SGreg Kroah-Hartman { 90496fd7ce5SGreg Kroah-Hartman ssize_t ret, written = 0; 90596fd7ce5SGreg Kroah-Hartman unsigned int chunk; 90696fd7ce5SGreg Kroah-Hartman 90796fd7ce5SGreg Kroah-Hartman ret = tty_write_lock(tty, file->f_flags & O_NDELAY); 90896fd7ce5SGreg Kroah-Hartman if (ret < 0) 90996fd7ce5SGreg Kroah-Hartman return ret; 91096fd7ce5SGreg Kroah-Hartman 91196fd7ce5SGreg Kroah-Hartman /* 91296fd7ce5SGreg Kroah-Hartman * We chunk up writes into a temporary buffer. This 91396fd7ce5SGreg Kroah-Hartman * simplifies low-level drivers immensely, since they 91496fd7ce5SGreg Kroah-Hartman * don't have locking issues and user mode accesses. 91596fd7ce5SGreg Kroah-Hartman * 91696fd7ce5SGreg Kroah-Hartman * But if TTY_NO_WRITE_SPLIT is set, we should use a 91796fd7ce5SGreg Kroah-Hartman * big chunk-size.. 91896fd7ce5SGreg Kroah-Hartman * 91996fd7ce5SGreg Kroah-Hartman * The default chunk-size is 2kB, because the NTTY 92096fd7ce5SGreg Kroah-Hartman * layer has problems with bigger chunks. It will 92196fd7ce5SGreg Kroah-Hartman * claim to be able to handle more characters than 92296fd7ce5SGreg Kroah-Hartman * it actually does. 92396fd7ce5SGreg Kroah-Hartman * 92496fd7ce5SGreg Kroah-Hartman * FIXME: This can probably go away now except that 64K chunks 92596fd7ce5SGreg Kroah-Hartman * are too likely to fail unless switched to vmalloc... 92696fd7ce5SGreg Kroah-Hartman */ 92796fd7ce5SGreg Kroah-Hartman chunk = 2048; 92896fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) 92996fd7ce5SGreg Kroah-Hartman chunk = 65536; 93096fd7ce5SGreg Kroah-Hartman if (count < chunk) 93196fd7ce5SGreg Kroah-Hartman chunk = count; 93296fd7ce5SGreg Kroah-Hartman 93396fd7ce5SGreg Kroah-Hartman /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ 93496fd7ce5SGreg Kroah-Hartman if (tty->write_cnt < chunk) { 93596fd7ce5SGreg Kroah-Hartman unsigned char *buf_chunk; 93696fd7ce5SGreg Kroah-Hartman 93796fd7ce5SGreg Kroah-Hartman if (chunk < 1024) 93896fd7ce5SGreg Kroah-Hartman chunk = 1024; 93996fd7ce5SGreg Kroah-Hartman 94096fd7ce5SGreg Kroah-Hartman buf_chunk = kmalloc(chunk, GFP_KERNEL); 94196fd7ce5SGreg Kroah-Hartman if (!buf_chunk) { 94296fd7ce5SGreg Kroah-Hartman ret = -ENOMEM; 94396fd7ce5SGreg Kroah-Hartman goto out; 94496fd7ce5SGreg Kroah-Hartman } 94596fd7ce5SGreg Kroah-Hartman kfree(tty->write_buf); 94696fd7ce5SGreg Kroah-Hartman tty->write_cnt = chunk; 94796fd7ce5SGreg Kroah-Hartman tty->write_buf = buf_chunk; 94896fd7ce5SGreg Kroah-Hartman } 94996fd7ce5SGreg Kroah-Hartman 95096fd7ce5SGreg Kroah-Hartman /* Do the write .. */ 95196fd7ce5SGreg Kroah-Hartman for (;;) { 95296fd7ce5SGreg Kroah-Hartman size_t size = count; 95396fd7ce5SGreg Kroah-Hartman if (size > chunk) 95496fd7ce5SGreg Kroah-Hartman size = chunk; 95596fd7ce5SGreg Kroah-Hartman ret = -EFAULT; 95696fd7ce5SGreg Kroah-Hartman if (copy_from_user(tty->write_buf, buf, size)) 95796fd7ce5SGreg Kroah-Hartman break; 95896fd7ce5SGreg Kroah-Hartman ret = write(tty, file, tty->write_buf, size); 95996fd7ce5SGreg Kroah-Hartman if (ret <= 0) 96096fd7ce5SGreg Kroah-Hartman break; 96196fd7ce5SGreg Kroah-Hartman written += ret; 96296fd7ce5SGreg Kroah-Hartman buf += ret; 96396fd7ce5SGreg Kroah-Hartman count -= ret; 96496fd7ce5SGreg Kroah-Hartman if (!count) 96596fd7ce5SGreg Kroah-Hartman break; 96696fd7ce5SGreg Kroah-Hartman ret = -ERESTARTSYS; 96796fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 96896fd7ce5SGreg Kroah-Hartman break; 96996fd7ce5SGreg Kroah-Hartman cond_resched(); 97096fd7ce5SGreg Kroah-Hartman } 97137b7f3c7SJiri Slaby if (written) { 97237b7f3c7SJiri Slaby tty_update_time(&file_inode(file)->i_mtime); 97396fd7ce5SGreg Kroah-Hartman ret = written; 97437b7f3c7SJiri Slaby } 97596fd7ce5SGreg Kroah-Hartman out: 97696fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 97796fd7ce5SGreg Kroah-Hartman return ret; 97896fd7ce5SGreg Kroah-Hartman } 97996fd7ce5SGreg Kroah-Hartman 98096fd7ce5SGreg Kroah-Hartman /** 98196fd7ce5SGreg Kroah-Hartman * tty_write_message - write a message to a certain tty, not just the console. 98296fd7ce5SGreg Kroah-Hartman * @tty: the destination tty_struct 98396fd7ce5SGreg Kroah-Hartman * @msg: the message to write 98496fd7ce5SGreg Kroah-Hartman * 98596fd7ce5SGreg Kroah-Hartman * This is used for messages that need to be redirected to a specific tty. 98696fd7ce5SGreg Kroah-Hartman * We don't put it into the syslog queue right now maybe in the future if 98796fd7ce5SGreg Kroah-Hartman * really needed. 98896fd7ce5SGreg Kroah-Hartman * 98996fd7ce5SGreg Kroah-Hartman * We must still hold the BTM and test the CLOSING flag for the moment. 99096fd7ce5SGreg Kroah-Hartman */ 99196fd7ce5SGreg Kroah-Hartman 99296fd7ce5SGreg Kroah-Hartman void tty_write_message(struct tty_struct *tty, char *msg) 99396fd7ce5SGreg Kroah-Hartman { 99496fd7ce5SGreg Kroah-Hartman if (tty) { 99596fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->atomic_write_lock); 99689c8d91eSAlan Cox tty_lock(tty); 9974b41b953SPeter Hurley if (tty->ops->write && tty->count > 0) 99896fd7ce5SGreg Kroah-Hartman tty->ops->write(tty, msg, strlen(msg)); 99989c8d91eSAlan Cox tty_unlock(tty); 100096fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 100196fd7ce5SGreg Kroah-Hartman } 100296fd7ce5SGreg Kroah-Hartman return; 100396fd7ce5SGreg Kroah-Hartman } 100496fd7ce5SGreg Kroah-Hartman 100596fd7ce5SGreg Kroah-Hartman 100696fd7ce5SGreg Kroah-Hartman /** 100796fd7ce5SGreg Kroah-Hartman * tty_write - write method for tty device file 100896fd7ce5SGreg Kroah-Hartman * @file: tty file pointer 100996fd7ce5SGreg Kroah-Hartman * @buf: user data to write 101096fd7ce5SGreg Kroah-Hartman * @count: bytes to write 101196fd7ce5SGreg Kroah-Hartman * @ppos: unused 101296fd7ce5SGreg Kroah-Hartman * 101396fd7ce5SGreg Kroah-Hartman * Write data to a tty device via the line discipline. 101496fd7ce5SGreg Kroah-Hartman * 101596fd7ce5SGreg Kroah-Hartman * Locking: 101696fd7ce5SGreg Kroah-Hartman * Locks the line discipline as required 101796fd7ce5SGreg Kroah-Hartman * Writes to the tty driver are serialized by the atomic_write_lock 101896fd7ce5SGreg Kroah-Hartman * and are then processed in chunks to the device. The line discipline 101996fd7ce5SGreg Kroah-Hartman * write method will not be invoked in parallel for each device. 102096fd7ce5SGreg Kroah-Hartman */ 102196fd7ce5SGreg Kroah-Hartman 102296fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *file, const char __user *buf, 102396fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 102496fd7ce5SGreg Kroah-Hartman { 102596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 102696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 102796fd7ce5SGreg Kroah-Hartman ssize_t ret; 102896fd7ce5SGreg Kroah-Hartman 10296131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_write")) 103096fd7ce5SGreg Kroah-Hartman return -EIO; 103118900ca6SPeter Hurley if (!tty || !tty->ops->write || tty_io_error(tty)) 103296fd7ce5SGreg Kroah-Hartman return -EIO; 103396fd7ce5SGreg Kroah-Hartman /* Short term debug to catch buggy drivers */ 103496fd7ce5SGreg Kroah-Hartman if (tty->ops->write_room == NULL) 1035339f36baSPeter Hurley tty_err(tty, "missing write_room method\n"); 103696fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 1037e55afd11SPeter Hurley if (!ld) 1038e55afd11SPeter Hurley return hung_up_tty_write(file, buf, count, ppos); 103996fd7ce5SGreg Kroah-Hartman if (!ld->ops->write) 104096fd7ce5SGreg Kroah-Hartman ret = -EIO; 104196fd7ce5SGreg Kroah-Hartman else 104296fd7ce5SGreg Kroah-Hartman ret = do_tty_write(ld->ops->write, tty, file, buf, count); 104396fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 104496fd7ce5SGreg Kroah-Hartman return ret; 104596fd7ce5SGreg Kroah-Hartman } 104696fd7ce5SGreg Kroah-Hartman 104796fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *file, const char __user *buf, 104896fd7ce5SGreg Kroah-Hartman size_t count, loff_t *ppos) 104996fd7ce5SGreg Kroah-Hartman { 105096fd7ce5SGreg Kroah-Hartman struct file *p = NULL; 105196fd7ce5SGreg Kroah-Hartman 105296fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 1053cb0942b8SAl Viro if (redirect) 1054cb0942b8SAl Viro p = get_file(redirect); 105596fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 105696fd7ce5SGreg Kroah-Hartman 105796fd7ce5SGreg Kroah-Hartman if (p) { 105896fd7ce5SGreg Kroah-Hartman ssize_t res; 105996fd7ce5SGreg Kroah-Hartman res = vfs_write(p, buf, count, &p->f_pos); 106096fd7ce5SGreg Kroah-Hartman fput(p); 106196fd7ce5SGreg Kroah-Hartman return res; 106296fd7ce5SGreg Kroah-Hartman } 106396fd7ce5SGreg Kroah-Hartman return tty_write(file, buf, count, ppos); 106496fd7ce5SGreg Kroah-Hartman } 106596fd7ce5SGreg Kroah-Hartman 1066136d5258SPeter Hurley /** 1067136d5258SPeter Hurley * tty_send_xchar - send priority character 1068136d5258SPeter Hurley * 1069136d5258SPeter Hurley * Send a high priority character to the tty even if stopped 1070136d5258SPeter Hurley * 1071136d5258SPeter Hurley * Locking: none for xchar method, write ordering for write method. 1072136d5258SPeter Hurley */ 1073136d5258SPeter Hurley 1074136d5258SPeter Hurley int tty_send_xchar(struct tty_struct *tty, char ch) 1075136d5258SPeter Hurley { 1076136d5258SPeter Hurley int was_stopped = tty->stopped; 1077136d5258SPeter Hurley 1078136d5258SPeter Hurley if (tty->ops->send_xchar) { 1079ee0c1a65SPeter Hurley down_read(&tty->termios_rwsem); 1080136d5258SPeter Hurley tty->ops->send_xchar(tty, ch); 1081ee0c1a65SPeter Hurley up_read(&tty->termios_rwsem); 1082136d5258SPeter Hurley return 0; 1083136d5258SPeter Hurley } 1084136d5258SPeter Hurley 1085136d5258SPeter Hurley if (tty_write_lock(tty, 0) < 0) 1086136d5258SPeter Hurley return -ERESTARTSYS; 1087136d5258SPeter Hurley 1088ee0c1a65SPeter Hurley down_read(&tty->termios_rwsem); 1089136d5258SPeter Hurley if (was_stopped) 1090136d5258SPeter Hurley start_tty(tty); 1091136d5258SPeter Hurley tty->ops->write(tty, &ch, 1); 1092136d5258SPeter Hurley if (was_stopped) 1093136d5258SPeter Hurley stop_tty(tty); 1094ee0c1a65SPeter Hurley up_read(&tty->termios_rwsem); 1095136d5258SPeter Hurley tty_write_unlock(tty); 1096136d5258SPeter Hurley return 0; 1097136d5258SPeter Hurley } 1098136d5258SPeter Hurley 109996fd7ce5SGreg Kroah-Hartman static char ptychar[] = "pqrstuvwxyzabcde"; 110096fd7ce5SGreg Kroah-Hartman 110196fd7ce5SGreg Kroah-Hartman /** 110296fd7ce5SGreg Kroah-Hartman * pty_line_name - generate name for a pty 110396fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 110496fd7ce5SGreg Kroah-Hartman * @index: the minor number 110596fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 6 bytes 110696fd7ce5SGreg Kroah-Hartman * 110796fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 110896fd7ce5SGreg Kroah-Hartman * buffer. 110996fd7ce5SGreg Kroah-Hartman * 111096fd7ce5SGreg Kroah-Hartman * Locking: None 111196fd7ce5SGreg Kroah-Hartman */ 111296fd7ce5SGreg Kroah-Hartman static void pty_line_name(struct tty_driver *driver, int index, char *p) 111396fd7ce5SGreg Kroah-Hartman { 111496fd7ce5SGreg Kroah-Hartman int i = index + driver->name_base; 111596fd7ce5SGreg Kroah-Hartman /* ->name is initialized to "ttyp", but "tty" is expected */ 111696fd7ce5SGreg Kroah-Hartman sprintf(p, "%s%c%x", 111796fd7ce5SGreg Kroah-Hartman driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, 111896fd7ce5SGreg Kroah-Hartman ptychar[i >> 4 & 0xf], i & 0xf); 111996fd7ce5SGreg Kroah-Hartman } 112096fd7ce5SGreg Kroah-Hartman 112196fd7ce5SGreg Kroah-Hartman /** 112296fd7ce5SGreg Kroah-Hartman * tty_line_name - generate name for a tty 112396fd7ce5SGreg Kroah-Hartman * @driver: the tty driver in use 112496fd7ce5SGreg Kroah-Hartman * @index: the minor number 112596fd7ce5SGreg Kroah-Hartman * @p: output buffer of at least 7 bytes 112696fd7ce5SGreg Kroah-Hartman * 112796fd7ce5SGreg Kroah-Hartman * Generate a name from a driver reference and write it to the output 11285c0a2450SGreg Kroah-Hartman * buffer. 112996fd7ce5SGreg Kroah-Hartman * 113096fd7ce5SGreg Kroah-Hartman * Locking: None 113196fd7ce5SGreg Kroah-Hartman */ 1132723abd87SHannes Reinecke static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p) 113396fd7ce5SGreg Kroah-Hartman { 11340019b408SJiri Slaby if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE) 1135723abd87SHannes Reinecke return sprintf(p, "%s", driver->name); 11360019b408SJiri Slaby else 1137723abd87SHannes Reinecke return sprintf(p, "%s%d", driver->name, 1138723abd87SHannes Reinecke index + driver->name_base); 113996fd7ce5SGreg Kroah-Hartman } 114096fd7ce5SGreg Kroah-Hartman 114196fd7ce5SGreg Kroah-Hartman /** 114296fd7ce5SGreg Kroah-Hartman * tty_driver_lookup_tty() - find an existing tty, if any 114396fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 114496fd7ce5SGreg Kroah-Hartman * @idx: the minor number 114596fd7ce5SGreg Kroah-Hartman * 1146aa3cb814SPeter Hurley * Return the tty, if found. If not found, return NULL or ERR_PTR() if the 1147aa3cb814SPeter Hurley * driver lookup() method returns an error. 114896fd7ce5SGreg Kroah-Hartman * 1149aa3cb814SPeter Hurley * Locking: tty_mutex must be held. If the tty is found, bump the tty kref. 115096fd7ce5SGreg Kroah-Hartman */ 115196fd7ce5SGreg Kroah-Hartman static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, 11528ead9dd5SLinus Torvalds struct file *file, int idx) 115396fd7ce5SGreg Kroah-Hartman { 1154aa3cb814SPeter Hurley struct tty_struct *tty; 115596fd7ce5SGreg Kroah-Hartman 1156aa3cb814SPeter Hurley if (driver->ops->lookup) 115712e84c71SOkash Khawaja if (!file) 115812e84c71SOkash Khawaja tty = ERR_PTR(-EIO); 115912e84c71SOkash Khawaja else 11608ead9dd5SLinus Torvalds tty = driver->ops->lookup(driver, file, idx); 1161aa3cb814SPeter Hurley else 1162aa3cb814SPeter Hurley tty = driver->ttys[idx]; 1163aa3cb814SPeter Hurley 1164aa3cb814SPeter Hurley if (!IS_ERR(tty)) 1165aa3cb814SPeter Hurley tty_kref_get(tty); 1166aa3cb814SPeter Hurley return tty; 116796fd7ce5SGreg Kroah-Hartman } 116896fd7ce5SGreg Kroah-Hartman 116996fd7ce5SGreg Kroah-Hartman /** 117096fd7ce5SGreg Kroah-Hartman * tty_init_termios - helper for termios setup 117196fd7ce5SGreg Kroah-Hartman * @tty: the tty to set up 117296fd7ce5SGreg Kroah-Hartman * 117396fd7ce5SGreg Kroah-Hartman * Initialise the termios structures for this tty. Thus runs under 117496fd7ce5SGreg Kroah-Hartman * the tty_mutex currently so we can be relaxed about ordering. 117596fd7ce5SGreg Kroah-Hartman */ 117696fd7ce5SGreg Kroah-Hartman 1177a3123fd0SPeter Hurley void tty_init_termios(struct tty_struct *tty) 117896fd7ce5SGreg Kroah-Hartman { 117996fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 118096fd7ce5SGreg Kroah-Hartman int idx = tty->index; 118196fd7ce5SGreg Kroah-Hartman 118236b3c070SAlan Cox if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 118336b3c070SAlan Cox tty->termios = tty->driver->init_termios; 118436b3c070SAlan Cox else { 118536b3c070SAlan Cox /* Check for lazy saved data */ 118696fd7ce5SGreg Kroah-Hartman tp = tty->driver->termios[idx]; 1187ece53405SPeter Hurley if (tp != NULL) { 1188adc8d746SAlan Cox tty->termios = *tp; 1189ece53405SPeter Hurley tty->termios.c_line = tty->driver->init_termios.c_line; 1190ece53405SPeter Hurley } else 119136b3c070SAlan Cox tty->termios = tty->driver->init_termios; 119236b3c070SAlan Cox } 119396fd7ce5SGreg Kroah-Hartman /* Compatibility until drivers always set this */ 1194adc8d746SAlan Cox tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 1195adc8d746SAlan Cox tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 119696fd7ce5SGreg Kroah-Hartman } 119796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_init_termios); 119896fd7ce5SGreg Kroah-Hartman 119966d450e8SJiri Slaby int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) 120066d450e8SJiri Slaby { 1201a3123fd0SPeter Hurley tty_init_termios(tty); 120266d450e8SJiri Slaby tty_driver_kref_get(driver); 120366d450e8SJiri Slaby tty->count++; 120466d450e8SJiri Slaby driver->ttys[tty->index] = tty; 120566d450e8SJiri Slaby return 0; 120666d450e8SJiri Slaby } 120766d450e8SJiri Slaby EXPORT_SYMBOL_GPL(tty_standard_install); 120866d450e8SJiri Slaby 120996fd7ce5SGreg Kroah-Hartman /** 121096fd7ce5SGreg Kroah-Hartman * tty_driver_install_tty() - install a tty entry in the driver 121196fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 121296fd7ce5SGreg Kroah-Hartman * @tty: the tty 121396fd7ce5SGreg Kroah-Hartman * 121496fd7ce5SGreg Kroah-Hartman * Install a tty object into the driver tables. The tty->index field 121596fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. This method is responsible 121696fd7ce5SGreg Kroah-Hartman * for ensuring any need additional structures are allocated and 121796fd7ce5SGreg Kroah-Hartman * configured. 121896fd7ce5SGreg Kroah-Hartman * 121996fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 122096fd7ce5SGreg Kroah-Hartman */ 122196fd7ce5SGreg Kroah-Hartman static int tty_driver_install_tty(struct tty_driver *driver, 122296fd7ce5SGreg Kroah-Hartman struct tty_struct *tty) 122396fd7ce5SGreg Kroah-Hartman { 122466d450e8SJiri Slaby return driver->ops->install ? driver->ops->install(driver, tty) : 122566d450e8SJiri Slaby tty_standard_install(driver, tty); 122696fd7ce5SGreg Kroah-Hartman } 122796fd7ce5SGreg Kroah-Hartman 122896fd7ce5SGreg Kroah-Hartman /** 122996fd7ce5SGreg Kroah-Hartman * tty_driver_remove_tty() - remove a tty from the driver tables 123096fd7ce5SGreg Kroah-Hartman * @driver: the driver for the tty 123196fd7ce5SGreg Kroah-Hartman * @idx: the minor number 123296fd7ce5SGreg Kroah-Hartman * 123396fd7ce5SGreg Kroah-Hartman * Remvoe a tty object from the driver tables. The tty->index field 123496fd7ce5SGreg Kroah-Hartman * will be set by the time this is called. 123596fd7ce5SGreg Kroah-Hartman * 123696fd7ce5SGreg Kroah-Hartman * Locking: tty_mutex for now 123796fd7ce5SGreg Kroah-Hartman */ 123805de87edSPeter Hurley static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) 123996fd7ce5SGreg Kroah-Hartman { 124096fd7ce5SGreg Kroah-Hartman if (driver->ops->remove) 124196fd7ce5SGreg Kroah-Hartman driver->ops->remove(driver, tty); 124296fd7ce5SGreg Kroah-Hartman else 124396fd7ce5SGreg Kroah-Hartman driver->ttys[tty->index] = NULL; 124496fd7ce5SGreg Kroah-Hartman } 124596fd7ce5SGreg Kroah-Hartman 124696fd7ce5SGreg Kroah-Hartman /* 124796fd7ce5SGreg Kroah-Hartman * tty_reopen() - fast re-open of an open tty 124896fd7ce5SGreg Kroah-Hartman * @tty - the tty to open 124996fd7ce5SGreg Kroah-Hartman * 125096fd7ce5SGreg Kroah-Hartman * Return 0 on success, -errno on error. 12515d93e748SPeter Hurley * Re-opens on master ptys are not allowed and return -EIO. 125296fd7ce5SGreg Kroah-Hartman * 12535d93e748SPeter Hurley * Locking: Caller must hold tty_lock 125496fd7ce5SGreg Kroah-Hartman */ 125596fd7ce5SGreg Kroah-Hartman static int tty_reopen(struct tty_struct *tty) 125696fd7ce5SGreg Kroah-Hartman { 125796fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 125896fd7ce5SGreg Kroah-Hartman 125996fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY && 12605d93e748SPeter Hurley driver->subtype == PTY_TYPE_MASTER) 126196fd7ce5SGreg Kroah-Hartman return -EIO; 126296fd7ce5SGreg Kroah-Hartman 12637f22f6c9SPeter Hurley if (!tty->count) 12647f22f6c9SPeter Hurley return -EAGAIN; 12657f22f6c9SPeter Hurley 126686f2c00fSPeter Hurley if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) 126786f2c00fSPeter Hurley return -EBUSY; 126886f2c00fSPeter Hurley 126996fd7ce5SGreg Kroah-Hartman tty->count++; 127096fd7ce5SGreg Kroah-Hartman 1271892d1fa7SPeter Hurley if (!tty->ldisc) 1272892d1fa7SPeter Hurley return tty_ldisc_reinit(tty, tty->termios.c_line); 127396fd7ce5SGreg Kroah-Hartman 127496fd7ce5SGreg Kroah-Hartman return 0; 127596fd7ce5SGreg Kroah-Hartman } 127696fd7ce5SGreg Kroah-Hartman 127796fd7ce5SGreg Kroah-Hartman /** 127896fd7ce5SGreg Kroah-Hartman * tty_init_dev - initialise a tty device 127996fd7ce5SGreg Kroah-Hartman * @driver: tty driver we are opening a device on 128096fd7ce5SGreg Kroah-Hartman * @idx: device index 128196fd7ce5SGreg Kroah-Hartman * @ret_tty: returned tty structure 128296fd7ce5SGreg Kroah-Hartman * 128396fd7ce5SGreg Kroah-Hartman * Prepare a tty device. This may not be a "new" clean device but 128496fd7ce5SGreg Kroah-Hartman * could also be an active device. The pty drivers require special 128596fd7ce5SGreg Kroah-Hartman * handling because of this. 128696fd7ce5SGreg Kroah-Hartman * 128796fd7ce5SGreg Kroah-Hartman * Locking: 128896fd7ce5SGreg Kroah-Hartman * The function is called under the tty_mutex, which 128996fd7ce5SGreg Kroah-Hartman * protects us from the tty struct or driver itself going away. 129096fd7ce5SGreg Kroah-Hartman * 129196fd7ce5SGreg Kroah-Hartman * On exit the tty device has the line discipline attached and 129296fd7ce5SGreg Kroah-Hartman * a reference count of 1. If a pair was created for pty/tty use 129396fd7ce5SGreg Kroah-Hartman * and the other was a pty master then it too has a reference count of 1. 129496fd7ce5SGreg Kroah-Hartman * 129596fd7ce5SGreg Kroah-Hartman * WSH 06/09/97: Rewritten to remove races and properly clean up after a 129696fd7ce5SGreg Kroah-Hartman * failed open. The new code protects the open with a mutex, so it's 129796fd7ce5SGreg Kroah-Hartman * really quite straightforward. The mutex locking can probably be 129896fd7ce5SGreg Kroah-Hartman * relaxed for the (most common) case of reopening a tty. 129996fd7ce5SGreg Kroah-Hartman */ 130096fd7ce5SGreg Kroah-Hartman 1301593a27c4SKonstantin Khlebnikov struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx) 130296fd7ce5SGreg Kroah-Hartman { 130396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty; 130496fd7ce5SGreg Kroah-Hartman int retval; 130596fd7ce5SGreg Kroah-Hartman 130696fd7ce5SGreg Kroah-Hartman /* 130796fd7ce5SGreg Kroah-Hartman * First time open is complex, especially for PTY devices. 130896fd7ce5SGreg Kroah-Hartman * This code guarantees that either everything succeeds and the 130996fd7ce5SGreg Kroah-Hartman * TTY is ready for operation, or else the table slots are vacated 131096fd7ce5SGreg Kroah-Hartman * and the allocated memory released. (Except that the termios 131116b00ae8SJohan Hovold * may be retained.) 131296fd7ce5SGreg Kroah-Hartman */ 131396fd7ce5SGreg Kroah-Hartman 131496fd7ce5SGreg Kroah-Hartman if (!try_module_get(driver->owner)) 131596fd7ce5SGreg Kroah-Hartman return ERR_PTR(-ENODEV); 131696fd7ce5SGreg Kroah-Hartman 13172c964a2fSRasmus Villemoes tty = alloc_tty_struct(driver, idx); 1318d5543503SJiri Slaby if (!tty) { 1319d5543503SJiri Slaby retval = -ENOMEM; 1320d5543503SJiri Slaby goto err_module_put; 1321d5543503SJiri Slaby } 132296fd7ce5SGreg Kroah-Hartman 132389c8d91eSAlan Cox tty_lock(tty); 132496fd7ce5SGreg Kroah-Hartman retval = tty_driver_install_tty(driver, tty); 1325d5543503SJiri Slaby if (retval < 0) 1326c8b710b3SPeter Hurley goto err_free_tty; 132796fd7ce5SGreg Kroah-Hartman 132804831dc1SJiri Slaby if (!tty->port) 132904831dc1SJiri Slaby tty->port = driver->ports[idx]; 133004831dc1SJiri Slaby 13315d4121c0SJiri Slaby WARN_RATELIMIT(!tty->port, 13325d4121c0SJiri Slaby "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n", 13335d4121c0SJiri Slaby __func__, tty->driver->name); 13345d4121c0SJiri Slaby 1335b027e229SGaurav Kohli retval = tty_ldisc_lock(tty, 5 * HZ); 1336b027e229SGaurav Kohli if (retval) 1337b027e229SGaurav Kohli goto err_release_lock; 1338967fab69SJiri Slaby tty->port->itty = tty; 1339967fab69SJiri Slaby 134096fd7ce5SGreg Kroah-Hartman /* 134196fd7ce5SGreg Kroah-Hartman * Structures all installed ... call the ldisc open routines. 134296fd7ce5SGreg Kroah-Hartman * If we fail here just call release_tty to clean up. No need 134396fd7ce5SGreg Kroah-Hartman * to decrement the use counts, as release_tty doesn't care. 134496fd7ce5SGreg Kroah-Hartman */ 134596fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_setup(tty, tty->link); 134696fd7ce5SGreg Kroah-Hartman if (retval) 1347d5543503SJiri Slaby goto err_release_tty; 1348b027e229SGaurav Kohli tty_ldisc_unlock(tty); 134989c8d91eSAlan Cox /* Return the tty locked so that it cannot vanish under the caller */ 135096fd7ce5SGreg Kroah-Hartman return tty; 135196fd7ce5SGreg Kroah-Hartman 1352c8b710b3SPeter Hurley err_free_tty: 135389c8d91eSAlan Cox tty_unlock(tty); 1354d5543503SJiri Slaby free_tty_struct(tty); 1355d5543503SJiri Slaby err_module_put: 135696fd7ce5SGreg Kroah-Hartman module_put(driver->owner); 1357d5543503SJiri Slaby return ERR_PTR(retval); 135896fd7ce5SGreg Kroah-Hartman 135996fd7ce5SGreg Kroah-Hartman /* call the tty release_tty routine to clean out this slot */ 1360d5543503SJiri Slaby err_release_tty: 1361b027e229SGaurav Kohli tty_ldisc_unlock(tty); 1362339f36baSPeter Hurley tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", 1363339f36baSPeter Hurley retval, idx); 1364b027e229SGaurav Kohli err_release_lock: 1365b027e229SGaurav Kohli tty_unlock(tty); 136696fd7ce5SGreg Kroah-Hartman release_tty(tty, idx); 136796fd7ce5SGreg Kroah-Hartman return ERR_PTR(retval); 136896fd7ce5SGreg Kroah-Hartman } 136996fd7ce5SGreg Kroah-Hartman 1370a3123fd0SPeter Hurley static void tty_free_termios(struct tty_struct *tty) 137196fd7ce5SGreg Kroah-Hartman { 137296fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 137396fd7ce5SGreg Kroah-Hartman int idx = tty->index; 137436b3c070SAlan Cox 137536b3c070SAlan Cox /* If the port is going to reset then it has no termios to save */ 137636b3c070SAlan Cox if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 137736b3c070SAlan Cox return; 137836b3c070SAlan Cox 137936b3c070SAlan Cox /* Stash the termios data */ 1380adc8d746SAlan Cox tp = tty->driver->termios[idx]; 138136b3c070SAlan Cox if (tp == NULL) { 138236b3c070SAlan Cox tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL); 138325080652SPeter Hurley if (tp == NULL) 138436b3c070SAlan Cox return; 13854ac5d705SDan Carpenter tty->driver->termios[idx] = tp; 138636b3c070SAlan Cox } 138736b3c070SAlan Cox *tp = tty->termios; 138896fd7ce5SGreg Kroah-Hartman } 138996fd7ce5SGreg Kroah-Hartman 1390a2965b7bSPeter Hurley /** 1391949aa64fSPeter Hurley * tty_flush_works - flush all works of a tty/pty pair 1392949aa64fSPeter Hurley * @tty: tty device to flush works for (or either end of a pty pair) 1393a2965b7bSPeter Hurley * 1394949aa64fSPeter Hurley * Sync flush all works belonging to @tty (and the 'other' tty). 1395a2965b7bSPeter Hurley */ 1396a2965b7bSPeter Hurley static void tty_flush_works(struct tty_struct *tty) 1397a2965b7bSPeter Hurley { 1398a2965b7bSPeter Hurley flush_work(&tty->SAK_work); 1399a2965b7bSPeter Hurley flush_work(&tty->hangup_work); 1400949aa64fSPeter Hurley if (tty->link) { 1401949aa64fSPeter Hurley flush_work(&tty->link->SAK_work); 1402949aa64fSPeter Hurley flush_work(&tty->link->hangup_work); 1403949aa64fSPeter Hurley } 1404a2965b7bSPeter Hurley } 140596fd7ce5SGreg Kroah-Hartman 140696fd7ce5SGreg Kroah-Hartman /** 140796fd7ce5SGreg Kroah-Hartman * release_one_tty - release tty structure memory 140896fd7ce5SGreg Kroah-Hartman * @kref: kref of tty we are obliterating 140996fd7ce5SGreg Kroah-Hartman * 141096fd7ce5SGreg Kroah-Hartman * Releases memory associated with a tty structure, and clears out the 141196fd7ce5SGreg Kroah-Hartman * driver table slots. This function is called when a device is no longer 141296fd7ce5SGreg Kroah-Hartman * in use. It also gets called when setup of a device fails. 141396fd7ce5SGreg Kroah-Hartman * 141496fd7ce5SGreg Kroah-Hartman * Locking: 141596fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 141696fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 141796fd7ce5SGreg Kroah-Hartman * 141896fd7ce5SGreg Kroah-Hartman * This method gets called from a work queue so that the driver private 141996fd7ce5SGreg Kroah-Hartman * cleanup ops can sleep (needed for USB at least) 142096fd7ce5SGreg Kroah-Hartman */ 142196fd7ce5SGreg Kroah-Hartman static void release_one_tty(struct work_struct *work) 142296fd7ce5SGreg Kroah-Hartman { 142396fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 142496fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, hangup_work); 142596fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = tty->driver; 1426b216df53SCyrill Gorcunov struct module *owner = driver->owner; 142796fd7ce5SGreg Kroah-Hartman 142896fd7ce5SGreg Kroah-Hartman if (tty->ops->cleanup) 142996fd7ce5SGreg Kroah-Hartman tty->ops->cleanup(tty); 143096fd7ce5SGreg Kroah-Hartman 143196fd7ce5SGreg Kroah-Hartman tty->magic = 0; 143296fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(driver); 1433b216df53SCyrill Gorcunov module_put(owner); 143496fd7ce5SGreg Kroah-Hartman 14354a510969SPeter Hurley spin_lock(&tty->files_lock); 143696fd7ce5SGreg Kroah-Hartman list_del_init(&tty->tty_files); 14374a510969SPeter Hurley spin_unlock(&tty->files_lock); 143896fd7ce5SGreg Kroah-Hartman 143996fd7ce5SGreg Kroah-Hartman put_pid(tty->pgrp); 144096fd7ce5SGreg Kroah-Hartman put_pid(tty->session); 144196fd7ce5SGreg Kroah-Hartman free_tty_struct(tty); 144296fd7ce5SGreg Kroah-Hartman } 144396fd7ce5SGreg Kroah-Hartman 144496fd7ce5SGreg Kroah-Hartman static void queue_release_one_tty(struct kref *kref) 144596fd7ce5SGreg Kroah-Hartman { 144696fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = container_of(kref, struct tty_struct, kref); 144796fd7ce5SGreg Kroah-Hartman 144896fd7ce5SGreg Kroah-Hartman /* The hangup queue is now free so we can reuse it rather than 144996fd7ce5SGreg Kroah-Hartman waste a chunk of memory for each port */ 145096fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, release_one_tty); 145196fd7ce5SGreg Kroah-Hartman schedule_work(&tty->hangup_work); 145296fd7ce5SGreg Kroah-Hartman } 145396fd7ce5SGreg Kroah-Hartman 145496fd7ce5SGreg Kroah-Hartman /** 145596fd7ce5SGreg Kroah-Hartman * tty_kref_put - release a tty kref 145696fd7ce5SGreg Kroah-Hartman * @tty: tty device 145796fd7ce5SGreg Kroah-Hartman * 145896fd7ce5SGreg Kroah-Hartman * Release a reference to a tty device and if need be let the kref 145996fd7ce5SGreg Kroah-Hartman * layer destruct the object for us 146096fd7ce5SGreg Kroah-Hartman */ 146196fd7ce5SGreg Kroah-Hartman 146296fd7ce5SGreg Kroah-Hartman void tty_kref_put(struct tty_struct *tty) 146396fd7ce5SGreg Kroah-Hartman { 146496fd7ce5SGreg Kroah-Hartman if (tty) 146596fd7ce5SGreg Kroah-Hartman kref_put(&tty->kref, queue_release_one_tty); 146696fd7ce5SGreg Kroah-Hartman } 146796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_kref_put); 146896fd7ce5SGreg Kroah-Hartman 146996fd7ce5SGreg Kroah-Hartman /** 147096fd7ce5SGreg Kroah-Hartman * release_tty - release tty structure memory 147196fd7ce5SGreg Kroah-Hartman * 147296fd7ce5SGreg Kroah-Hartman * Release both @tty and a possible linked partner (think pty pair), 147396fd7ce5SGreg Kroah-Hartman * and decrement the refcount of the backing module. 147496fd7ce5SGreg Kroah-Hartman * 147596fd7ce5SGreg Kroah-Hartman * Locking: 1476d155255aSAlan Cox * tty_mutex 147796fd7ce5SGreg Kroah-Hartman * takes the file list lock internally when working on the list 147896fd7ce5SGreg Kroah-Hartman * of ttys that the driver keeps. 147996fd7ce5SGreg Kroah-Hartman * 148096fd7ce5SGreg Kroah-Hartman */ 148196fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx) 148296fd7ce5SGreg Kroah-Hartman { 148396fd7ce5SGreg Kroah-Hartman /* This should always be true but check for the moment */ 148496fd7ce5SGreg Kroah-Hartman WARN_ON(tty->index != idx); 1485d155255aSAlan Cox WARN_ON(!mutex_is_locked(&tty_mutex)); 148636b3c070SAlan Cox if (tty->ops->shutdown) 148736b3c070SAlan Cox tty->ops->shutdown(tty); 148836b3c070SAlan Cox tty_free_termios(tty); 148936b3c070SAlan Cox tty_driver_remove_tty(tty->driver, tty); 1490967fab69SJiri Slaby tty->port->itty = NULL; 149164e377dcSPeter Hurley if (tty->link) 149264e377dcSPeter Hurley tty->link->port->itty = NULL; 1493e176058fSPeter Hurley tty_buffer_cancel_work(tty->port); 14942b022ab7SSahara if (tty->link) 14952b022ab7SSahara tty_buffer_cancel_work(tty->link->port); 149636b3c070SAlan Cox 149796fd7ce5SGreg Kroah-Hartman tty_kref_put(tty->link); 149896fd7ce5SGreg Kroah-Hartman tty_kref_put(tty); 149996fd7ce5SGreg Kroah-Hartman } 150096fd7ce5SGreg Kroah-Hartman 150196fd7ce5SGreg Kroah-Hartman /** 1502955787caSJiri Slaby * tty_release_checks - check a tty before real release 1503955787caSJiri Slaby * @tty: tty to check 1504955787caSJiri Slaby * @o_tty: link of @tty (if any) 1505955787caSJiri Slaby * @idx: index of the tty 1506955787caSJiri Slaby * 1507955787caSJiri Slaby * Performs some paranoid checking before true release of the @tty. 1508955787caSJiri Slaby * This is a no-op unless TTY_PARANOIA_CHECK is defined. 1509955787caSJiri Slaby */ 1510359b9fb5SPeter Hurley static int tty_release_checks(struct tty_struct *tty, int idx) 1511955787caSJiri Slaby { 1512955787caSJiri Slaby #ifdef TTY_PARANOIA_CHECK 1513955787caSJiri Slaby if (idx < 0 || idx >= tty->driver->num) { 1514e2dfa3d3SPeter Hurley tty_debug(tty, "bad idx %d\n", idx); 1515955787caSJiri Slaby return -1; 1516955787caSJiri Slaby } 1517955787caSJiri Slaby 1518955787caSJiri Slaby /* not much to check for devpts */ 1519955787caSJiri Slaby if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) 1520955787caSJiri Slaby return 0; 1521955787caSJiri Slaby 1522955787caSJiri Slaby if (tty != tty->driver->ttys[idx]) { 1523e2dfa3d3SPeter Hurley tty_debug(tty, "bad driver table[%d] = %p\n", 1524e2dfa3d3SPeter Hurley idx, tty->driver->ttys[idx]); 1525955787caSJiri Slaby return -1; 1526955787caSJiri Slaby } 1527955787caSJiri Slaby if (tty->driver->other) { 1528359b9fb5SPeter Hurley struct tty_struct *o_tty = tty->link; 1529359b9fb5SPeter Hurley 1530955787caSJiri Slaby if (o_tty != tty->driver->other->ttys[idx]) { 1531e2dfa3d3SPeter Hurley tty_debug(tty, "bad other table[%d] = %p\n", 1532e2dfa3d3SPeter Hurley idx, tty->driver->other->ttys[idx]); 1533955787caSJiri Slaby return -1; 1534955787caSJiri Slaby } 1535955787caSJiri Slaby if (o_tty->link != tty) { 1536e2dfa3d3SPeter Hurley tty_debug(tty, "bad link = %p\n", o_tty->link); 1537955787caSJiri Slaby return -1; 1538955787caSJiri Slaby } 1539955787caSJiri Slaby } 1540955787caSJiri Slaby #endif 1541955787caSJiri Slaby return 0; 1542955787caSJiri Slaby } 1543955787caSJiri Slaby 1544955787caSJiri Slaby /** 1545a09ac397SOkash Khawaja * tty_kclose - closes tty opened by tty_kopen 1546a09ac397SOkash Khawaja * @tty: tty device 1547a09ac397SOkash Khawaja * 1548a09ac397SOkash Khawaja * Performs the final steps to release and free a tty device. It is the 1549a09ac397SOkash Khawaja * same as tty_release_struct except that it also resets TTY_PORT_KOPENED 1550a09ac397SOkash Khawaja * flag on tty->port. 1551a09ac397SOkash Khawaja */ 1552a09ac397SOkash Khawaja void tty_kclose(struct tty_struct *tty) 1553a09ac397SOkash Khawaja { 1554a09ac397SOkash Khawaja /* 1555a09ac397SOkash Khawaja * Ask the line discipline code to release its structures 1556a09ac397SOkash Khawaja */ 1557a09ac397SOkash Khawaja tty_ldisc_release(tty); 1558a09ac397SOkash Khawaja 1559a09ac397SOkash Khawaja /* Wait for pending work before tty destruction commmences */ 1560a09ac397SOkash Khawaja tty_flush_works(tty); 1561a09ac397SOkash Khawaja 1562a09ac397SOkash Khawaja tty_debug_hangup(tty, "freeing structure\n"); 1563a09ac397SOkash Khawaja /* 1564a09ac397SOkash Khawaja * The release_tty function takes care of the details of clearing 1565a09ac397SOkash Khawaja * the slots and preserving the termios structure. The tty_unlock_pair 1566a09ac397SOkash Khawaja * should be safe as we keep a kref while the tty is locked (so the 1567a09ac397SOkash Khawaja * unlock never unlocks a freed tty). 1568a09ac397SOkash Khawaja */ 1569a09ac397SOkash Khawaja mutex_lock(&tty_mutex); 1570a09ac397SOkash Khawaja tty_port_set_kopened(tty->port, 0); 1571a09ac397SOkash Khawaja release_tty(tty, tty->index); 1572a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1573a09ac397SOkash Khawaja } 1574a09ac397SOkash Khawaja EXPORT_SYMBOL_GPL(tty_kclose); 1575a09ac397SOkash Khawaja 1576a09ac397SOkash Khawaja /** 15779ed90d20SRob Herring * tty_release_struct - release a tty struct 15789ed90d20SRob Herring * @tty: tty device 15799ed90d20SRob Herring * @idx: index of the tty 15809ed90d20SRob Herring * 15819ed90d20SRob Herring * Performs the final steps to release and free a tty device. It is 15829ed90d20SRob Herring * roughly the reverse of tty_init_dev. 15839ed90d20SRob Herring */ 15849ed90d20SRob Herring void tty_release_struct(struct tty_struct *tty, int idx) 15859ed90d20SRob Herring { 15869ed90d20SRob Herring /* 15879ed90d20SRob Herring * Ask the line discipline code to release its structures 15889ed90d20SRob Herring */ 15899ed90d20SRob Herring tty_ldisc_release(tty); 15909ed90d20SRob Herring 15919ed90d20SRob Herring /* Wait for pending work before tty destruction commmences */ 15929ed90d20SRob Herring tty_flush_works(tty); 15939ed90d20SRob Herring 15949ed90d20SRob Herring tty_debug_hangup(tty, "freeing structure\n"); 15959ed90d20SRob Herring /* 15969ed90d20SRob Herring * The release_tty function takes care of the details of clearing 15979ed90d20SRob Herring * the slots and preserving the termios structure. The tty_unlock_pair 15989ed90d20SRob Herring * should be safe as we keep a kref while the tty is locked (so the 15999ed90d20SRob Herring * unlock never unlocks a freed tty). 16009ed90d20SRob Herring */ 16019ed90d20SRob Herring mutex_lock(&tty_mutex); 16029ed90d20SRob Herring release_tty(tty, idx); 16039ed90d20SRob Herring mutex_unlock(&tty_mutex); 16049ed90d20SRob Herring } 16059ed90d20SRob Herring EXPORT_SYMBOL_GPL(tty_release_struct); 16069ed90d20SRob Herring 16079ed90d20SRob Herring /** 160896fd7ce5SGreg Kroah-Hartman * tty_release - vfs callback for close 160996fd7ce5SGreg Kroah-Hartman * @inode: inode of tty 161096fd7ce5SGreg Kroah-Hartman * @filp: file pointer for handle to tty 161196fd7ce5SGreg Kroah-Hartman * 161296fd7ce5SGreg Kroah-Hartman * Called the last time each file handle is closed that references 161396fd7ce5SGreg Kroah-Hartman * this tty. There may however be several such references. 161496fd7ce5SGreg Kroah-Hartman * 161596fd7ce5SGreg Kroah-Hartman * Locking: 161696fd7ce5SGreg Kroah-Hartman * Takes bkl. See tty_release_dev 161796fd7ce5SGreg Kroah-Hartman * 161896fd7ce5SGreg Kroah-Hartman * Even releasing the tty structures is a tricky business.. We have 161996fd7ce5SGreg Kroah-Hartman * to be very careful that the structures are all released at the 162096fd7ce5SGreg Kroah-Hartman * same time, as interrupts might otherwise get the wrong pointers. 162196fd7ce5SGreg Kroah-Hartman * 162296fd7ce5SGreg Kroah-Hartman * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 162396fd7ce5SGreg Kroah-Hartman * lead to double frees or releasing memory still in use. 162496fd7ce5SGreg Kroah-Hartman */ 162596fd7ce5SGreg Kroah-Hartman 162696fd7ce5SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp) 162796fd7ce5SGreg Kroah-Hartman { 162896fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 16297ffb6da9SPeter Hurley struct tty_struct *o_tty = NULL; 16307ffb6da9SPeter Hurley int do_sleep, final; 163196fd7ce5SGreg Kroah-Hartman int idx; 163237b16457SPeter Hurley long timeout = 0; 1633494c1eacSPeter Hurley int once = 1; 163496fd7ce5SGreg Kroah-Hartman 16359de44bd6SJiri Slaby if (tty_paranoia_check(tty, inode, __func__)) 163696fd7ce5SGreg Kroah-Hartman return 0; 163796fd7ce5SGreg Kroah-Hartman 163889c8d91eSAlan Cox tty_lock(tty); 16399de44bd6SJiri Slaby check_tty_count(tty, __func__); 164096fd7ce5SGreg Kroah-Hartman 164196fd7ce5SGreg Kroah-Hartman __tty_fasync(-1, filp, 0); 164296fd7ce5SGreg Kroah-Hartman 164396fd7ce5SGreg Kroah-Hartman idx = tty->index; 16447ffb6da9SPeter Hurley if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 16457ffb6da9SPeter Hurley tty->driver->subtype == PTY_TYPE_MASTER) 164696fd7ce5SGreg Kroah-Hartman o_tty = tty->link; 164796fd7ce5SGreg Kroah-Hartman 1648359b9fb5SPeter Hurley if (tty_release_checks(tty, idx)) { 164989c8d91eSAlan Cox tty_unlock(tty); 165096fd7ce5SGreg Kroah-Hartman return 0; 165196fd7ce5SGreg Kroah-Hartman } 165296fd7ce5SGreg Kroah-Hartman 1653d435cefeSPeter Hurley tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); 165496fd7ce5SGreg Kroah-Hartman 165596fd7ce5SGreg Kroah-Hartman if (tty->ops->close) 165696fd7ce5SGreg Kroah-Hartman tty->ops->close(tty, filp); 165796fd7ce5SGreg Kroah-Hartman 16582aff5e2bSPeter Hurley /* If tty is pty master, lock the slave pty (stable lock order) */ 16592aff5e2bSPeter Hurley tty_lock_slave(o_tty); 16602aff5e2bSPeter Hurley 166196fd7ce5SGreg Kroah-Hartman /* 166296fd7ce5SGreg Kroah-Hartman * Sanity check: if tty->count is going to zero, there shouldn't be 166396fd7ce5SGreg Kroah-Hartman * any waiters on tty->read_wait or tty->write_wait. We test the 166496fd7ce5SGreg Kroah-Hartman * wait queues and kick everyone out _before_ actually starting to 166596fd7ce5SGreg Kroah-Hartman * close. This ensures that we won't block while releasing the tty 166696fd7ce5SGreg Kroah-Hartman * structure. 166796fd7ce5SGreg Kroah-Hartman * 166896fd7ce5SGreg Kroah-Hartman * The test for the o_tty closing is necessary, since the master and 166996fd7ce5SGreg Kroah-Hartman * slave sides may close in any order. If the slave side closes out 167096fd7ce5SGreg Kroah-Hartman * first, its count will be one, since the master side holds an open. 1671324c1650SPeter Hurley * Thus this test wouldn't be triggered at the time the slave closed, 167296fd7ce5SGreg Kroah-Hartman * so we do it now. 167396fd7ce5SGreg Kroah-Hartman */ 1674d5e370a4SPeter Hurley while (1) { 167596fd7ce5SGreg Kroah-Hartman do_sleep = 0; 167696fd7ce5SGreg Kroah-Hartman 1677324c1650SPeter Hurley if (tty->count <= 1) { 167896fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->read_wait)) { 1679a9a08845SLinus Torvalds wake_up_poll(&tty->read_wait, EPOLLIN); 168096fd7ce5SGreg Kroah-Hartman do_sleep++; 168196fd7ce5SGreg Kroah-Hartman } 168296fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&tty->write_wait)) { 1683a9a08845SLinus Torvalds wake_up_poll(&tty->write_wait, EPOLLOUT); 168496fd7ce5SGreg Kroah-Hartman do_sleep++; 168596fd7ce5SGreg Kroah-Hartman } 168696fd7ce5SGreg Kroah-Hartman } 16877ffb6da9SPeter Hurley if (o_tty && o_tty->count <= 1) { 168896fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->read_wait)) { 1689a9a08845SLinus Torvalds wake_up_poll(&o_tty->read_wait, EPOLLIN); 169096fd7ce5SGreg Kroah-Hartman do_sleep++; 169196fd7ce5SGreg Kroah-Hartman } 169296fd7ce5SGreg Kroah-Hartman if (waitqueue_active(&o_tty->write_wait)) { 1693a9a08845SLinus Torvalds wake_up_poll(&o_tty->write_wait, EPOLLOUT); 169496fd7ce5SGreg Kroah-Hartman do_sleep++; 169596fd7ce5SGreg Kroah-Hartman } 169696fd7ce5SGreg Kroah-Hartman } 169796fd7ce5SGreg Kroah-Hartman if (!do_sleep) 169896fd7ce5SGreg Kroah-Hartman break; 169996fd7ce5SGreg Kroah-Hartman 1700494c1eacSPeter Hurley if (once) { 1701494c1eacSPeter Hurley once = 0; 1702339f36baSPeter Hurley tty_warn(tty, "read/write wait queue active!\n"); 1703494c1eacSPeter Hurley } 170437b16457SPeter Hurley schedule_timeout_killable(timeout); 170537b16457SPeter Hurley if (timeout < 120 * HZ) 170637b16457SPeter Hurley timeout = 2 * timeout + 1; 170737b16457SPeter Hurley else 170837b16457SPeter Hurley timeout = MAX_SCHEDULE_TIMEOUT; 170996fd7ce5SGreg Kroah-Hartman } 171096fd7ce5SGreg Kroah-Hartman 17117ffb6da9SPeter Hurley if (o_tty) { 171296fd7ce5SGreg Kroah-Hartman if (--o_tty->count < 0) { 1713339f36baSPeter Hurley tty_warn(tty, "bad slave count (%d)\n", o_tty->count); 171496fd7ce5SGreg Kroah-Hartman o_tty->count = 0; 171596fd7ce5SGreg Kroah-Hartman } 171696fd7ce5SGreg Kroah-Hartman } 171796fd7ce5SGreg Kroah-Hartman if (--tty->count < 0) { 1718339f36baSPeter Hurley tty_warn(tty, "bad tty->count (%d)\n", tty->count); 171996fd7ce5SGreg Kroah-Hartman tty->count = 0; 172096fd7ce5SGreg Kroah-Hartman } 172196fd7ce5SGreg Kroah-Hartman 172296fd7ce5SGreg Kroah-Hartman /* 172396fd7ce5SGreg Kroah-Hartman * We've decremented tty->count, so we need to remove this file 172496fd7ce5SGreg Kroah-Hartman * descriptor off the tty->tty_files list; this serves two 172596fd7ce5SGreg Kroah-Hartman * purposes: 172696fd7ce5SGreg Kroah-Hartman * - check_tty_count sees the correct number of file descriptors 172796fd7ce5SGreg Kroah-Hartman * associated with this tty. 172896fd7ce5SGreg Kroah-Hartman * - do_tty_hangup no longer sees this file descriptor as 172996fd7ce5SGreg Kroah-Hartman * something that needs to be handled for hangups. 173096fd7ce5SGreg Kroah-Hartman */ 173196fd7ce5SGreg Kroah-Hartman tty_del_file(filp); 173296fd7ce5SGreg Kroah-Hartman 173396fd7ce5SGreg Kroah-Hartman /* 173496fd7ce5SGreg Kroah-Hartman * Perform some housekeeping before deciding whether to return. 173596fd7ce5SGreg Kroah-Hartman * 173696fd7ce5SGreg Kroah-Hartman * If _either_ side is closing, make sure there aren't any 173796fd7ce5SGreg Kroah-Hartman * processes that still think tty or o_tty is their controlling 173896fd7ce5SGreg Kroah-Hartman * tty. 173996fd7ce5SGreg Kroah-Hartman */ 1740324c1650SPeter Hurley if (!tty->count) { 174196fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 174296fd7ce5SGreg Kroah-Hartman session_clear_tty(tty->session); 17437ffb6da9SPeter Hurley if (o_tty) 174496fd7ce5SGreg Kroah-Hartman session_clear_tty(o_tty->session); 174596fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 174696fd7ce5SGreg Kroah-Hartman } 174796fd7ce5SGreg Kroah-Hartman 1748324c1650SPeter Hurley /* check whether both sides are closing ... */ 17497ffb6da9SPeter Hurley final = !tty->count && !(o_tty && o_tty->count); 1750324c1650SPeter Hurley 17512aff5e2bSPeter Hurley tty_unlock_slave(o_tty); 17522aff5e2bSPeter Hurley tty_unlock(tty); 17532aff5e2bSPeter Hurley 175404980706SPeter Hurley /* At this point, the tty->count == 0 should ensure a dead tty 1755d155255aSAlan Cox cannot be re-opened by a racing opener */ 175696fd7ce5SGreg Kroah-Hartman 1757324c1650SPeter Hurley if (!final) 175896fd7ce5SGreg Kroah-Hartman return 0; 175996fd7ce5SGreg Kroah-Hartman 1760accff793SPeter Hurley tty_debug_hangup(tty, "final close\n"); 1761a2965b7bSPeter Hurley 17629ed90d20SRob Herring tty_release_struct(tty, idx); 176396fd7ce5SGreg Kroah-Hartman return 0; 176496fd7ce5SGreg Kroah-Hartman } 176596fd7ce5SGreg Kroah-Hartman 176696fd7ce5SGreg Kroah-Hartman /** 176752494eebSPeter Hurley * tty_open_current_tty - get locked tty of current task 1768b82154acSJiri Slaby * @device: device number 1769b82154acSJiri Slaby * @filp: file pointer to tty 177052494eebSPeter Hurley * @return: locked tty of the current task iff @device is /dev/tty 177152494eebSPeter Hurley * 177252494eebSPeter Hurley * Performs a re-open of the current task's controlling tty. 1773b82154acSJiri Slaby * 1774b82154acSJiri Slaby * We cannot return driver and index like for the other nodes because 1775b82154acSJiri Slaby * devpts will not work then. It expects inodes to be from devpts FS. 1776b82154acSJiri Slaby */ 1777b82154acSJiri Slaby static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp) 1778b82154acSJiri Slaby { 1779b82154acSJiri Slaby struct tty_struct *tty; 178052494eebSPeter Hurley int retval; 1781b82154acSJiri Slaby 1782b82154acSJiri Slaby if (device != MKDEV(TTYAUX_MAJOR, 0)) 1783b82154acSJiri Slaby return NULL; 1784b82154acSJiri Slaby 1785b82154acSJiri Slaby tty = get_current_tty(); 1786b82154acSJiri Slaby if (!tty) 1787b82154acSJiri Slaby return ERR_PTR(-ENXIO); 1788b82154acSJiri Slaby 1789b82154acSJiri Slaby filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ 1790b82154acSJiri Slaby /* noctty = 1; */ 179152494eebSPeter Hurley tty_lock(tty); 179252494eebSPeter Hurley tty_kref_put(tty); /* safe to drop the kref now */ 179352494eebSPeter Hurley 179452494eebSPeter Hurley retval = tty_reopen(tty); 179552494eebSPeter Hurley if (retval < 0) { 179652494eebSPeter Hurley tty_unlock(tty); 179752494eebSPeter Hurley tty = ERR_PTR(retval); 179852494eebSPeter Hurley } 1799b82154acSJiri Slaby return tty; 1800b82154acSJiri Slaby } 1801b82154acSJiri Slaby 1802b82154acSJiri Slaby /** 18035b5e7040SJiri Slaby * tty_lookup_driver - lookup a tty driver for a given device file 18045b5e7040SJiri Slaby * @device: device number 18055b5e7040SJiri Slaby * @filp: file pointer to tty 18065b5e7040SJiri Slaby * @index: index for the device in the @return driver 18075b5e7040SJiri Slaby * @return: driver for this inode (with increased refcount) 18085b5e7040SJiri Slaby * 18095b5e7040SJiri Slaby * If @return is not erroneous, the caller is responsible to decrement the 18105b5e7040SJiri Slaby * refcount by tty_driver_kref_put. 18115b5e7040SJiri Slaby * 18125b5e7040SJiri Slaby * Locking: tty_mutex protects get_tty_driver 18135b5e7040SJiri Slaby */ 18145b5e7040SJiri Slaby static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp, 181511e1d4aaSPeter Hurley int *index) 18165b5e7040SJiri Slaby { 18175b5e7040SJiri Slaby struct tty_driver *driver; 18185b5e7040SJiri Slaby 18192cd0050cSJiri Slaby switch (device) { 18205b5e7040SJiri Slaby #ifdef CONFIG_VT 18212cd0050cSJiri Slaby case MKDEV(TTY_MAJOR, 0): { 18225b5e7040SJiri Slaby extern struct tty_driver *console_driver; 18235b5e7040SJiri Slaby driver = tty_driver_kref_get(console_driver); 18245b5e7040SJiri Slaby *index = fg_console; 18252cd0050cSJiri Slaby break; 18265b5e7040SJiri Slaby } 18275b5e7040SJiri Slaby #endif 18282cd0050cSJiri Slaby case MKDEV(TTYAUX_MAJOR, 1): { 18295b5e7040SJiri Slaby struct tty_driver *console_driver = console_device(index); 18305b5e7040SJiri Slaby if (console_driver) { 18315b5e7040SJiri Slaby driver = tty_driver_kref_get(console_driver); 183212e84c71SOkash Khawaja if (driver && filp) { 18335b5e7040SJiri Slaby /* Don't let /dev/console block */ 18345b5e7040SJiri Slaby filp->f_flags |= O_NONBLOCK; 18352cd0050cSJiri Slaby break; 18365b5e7040SJiri Slaby } 18375b5e7040SJiri Slaby } 18385b5e7040SJiri Slaby return ERR_PTR(-ENODEV); 18395b5e7040SJiri Slaby } 18402cd0050cSJiri Slaby default: 18415b5e7040SJiri Slaby driver = get_tty_driver(device, index); 18425b5e7040SJiri Slaby if (!driver) 18435b5e7040SJiri Slaby return ERR_PTR(-ENODEV); 18442cd0050cSJiri Slaby break; 18452cd0050cSJiri Slaby } 18465b5e7040SJiri Slaby return driver; 18475b5e7040SJiri Slaby } 18485b5e7040SJiri Slaby 18495b5e7040SJiri Slaby /** 1850a09ac397SOkash Khawaja * tty_kopen - open a tty device for kernel 1851a09ac397SOkash Khawaja * @device: dev_t of device to open 1852a09ac397SOkash Khawaja * 1853a09ac397SOkash Khawaja * Opens tty exclusively for kernel. Performs the driver lookup, 1854a09ac397SOkash Khawaja * makes sure it's not already opened and performs the first-time 1855a09ac397SOkash Khawaja * tty initialization. 1856a09ac397SOkash Khawaja * 1857a09ac397SOkash Khawaja * Returns the locked initialized &tty_struct 1858a09ac397SOkash Khawaja * 1859a09ac397SOkash Khawaja * Claims the global tty_mutex to serialize: 1860a09ac397SOkash Khawaja * - concurrent first-time tty initialization 1861a09ac397SOkash Khawaja * - concurrent tty driver removal w/ lookup 1862a09ac397SOkash Khawaja * - concurrent tty removal from driver table 1863a09ac397SOkash Khawaja */ 1864a09ac397SOkash Khawaja struct tty_struct *tty_kopen(dev_t device) 1865a09ac397SOkash Khawaja { 1866a09ac397SOkash Khawaja struct tty_struct *tty; 1867a09ac397SOkash Khawaja struct tty_driver *driver = NULL; 1868a09ac397SOkash Khawaja int index = -1; 1869a09ac397SOkash Khawaja 1870a09ac397SOkash Khawaja mutex_lock(&tty_mutex); 1871a09ac397SOkash Khawaja driver = tty_lookup_driver(device, NULL, &index); 1872a09ac397SOkash Khawaja if (IS_ERR(driver)) { 1873a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1874a09ac397SOkash Khawaja return ERR_CAST(driver); 1875a09ac397SOkash Khawaja } 1876a09ac397SOkash Khawaja 1877a09ac397SOkash Khawaja /* check whether we're reopening an existing tty */ 1878a09ac397SOkash Khawaja tty = tty_driver_lookup_tty(driver, NULL, index); 1879a09ac397SOkash Khawaja if (IS_ERR(tty)) 1880a09ac397SOkash Khawaja goto out; 1881a09ac397SOkash Khawaja 1882a09ac397SOkash Khawaja if (tty) { 1883a09ac397SOkash Khawaja /* drop kref from tty_driver_lookup_tty() */ 1884a09ac397SOkash Khawaja tty_kref_put(tty); 1885a09ac397SOkash Khawaja tty = ERR_PTR(-EBUSY); 1886a09ac397SOkash Khawaja } else { /* tty_init_dev returns tty with the tty_lock held */ 1887a09ac397SOkash Khawaja tty = tty_init_dev(driver, index); 1888a09ac397SOkash Khawaja if (IS_ERR(tty)) 1889a09ac397SOkash Khawaja goto out; 1890a09ac397SOkash Khawaja tty_port_set_kopened(tty->port, 1); 1891a09ac397SOkash Khawaja } 1892a09ac397SOkash Khawaja out: 1893a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1894a09ac397SOkash Khawaja tty_driver_kref_put(driver); 1895a09ac397SOkash Khawaja return tty; 1896a09ac397SOkash Khawaja } 1897a09ac397SOkash Khawaja EXPORT_SYMBOL_GPL(tty_kopen); 1898a09ac397SOkash Khawaja 1899a09ac397SOkash Khawaja /** 1900d6203d0cSPeter Hurley * tty_open_by_driver - open a tty device 1901d6203d0cSPeter Hurley * @device: dev_t of device to open 1902d6203d0cSPeter Hurley * @inode: inode of device file 1903d6203d0cSPeter Hurley * @filp: file pointer to tty 1904d6203d0cSPeter Hurley * 1905d6203d0cSPeter Hurley * Performs the driver lookup, checks for a reopen, or otherwise 1906d6203d0cSPeter Hurley * performs the first-time tty initialization. 1907d6203d0cSPeter Hurley * 1908d6203d0cSPeter Hurley * Returns the locked initialized or re-opened &tty_struct 1909d6203d0cSPeter Hurley * 1910d6203d0cSPeter Hurley * Claims the global tty_mutex to serialize: 1911d6203d0cSPeter Hurley * - concurrent first-time tty initialization 1912d6203d0cSPeter Hurley * - concurrent tty driver removal w/ lookup 1913d6203d0cSPeter Hurley * - concurrent tty removal from driver table 1914d6203d0cSPeter Hurley */ 1915a033c3b1SOkash Khawaja static struct tty_struct *tty_open_by_driver(dev_t device, struct inode *inode, 1916d6203d0cSPeter Hurley struct file *filp) 1917d6203d0cSPeter Hurley { 1918d6203d0cSPeter Hurley struct tty_struct *tty; 1919d6203d0cSPeter Hurley struct tty_driver *driver = NULL; 1920d6203d0cSPeter Hurley int index = -1; 1921d6203d0cSPeter Hurley int retval; 1922d6203d0cSPeter Hurley 1923d6203d0cSPeter Hurley mutex_lock(&tty_mutex); 1924d6203d0cSPeter Hurley driver = tty_lookup_driver(device, filp, &index); 1925d6203d0cSPeter Hurley if (IS_ERR(driver)) { 1926d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 1927d6203d0cSPeter Hurley return ERR_CAST(driver); 1928d6203d0cSPeter Hurley } 1929d6203d0cSPeter Hurley 1930d6203d0cSPeter Hurley /* check whether we're reopening an existing tty */ 19318ead9dd5SLinus Torvalds tty = tty_driver_lookup_tty(driver, filp, index); 1932d6203d0cSPeter Hurley if (IS_ERR(tty)) { 1933d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 1934d6203d0cSPeter Hurley goto out; 1935d6203d0cSPeter Hurley } 1936d6203d0cSPeter Hurley 1937d6203d0cSPeter Hurley if (tty) { 1938a09ac397SOkash Khawaja if (tty_port_kopened(tty->port)) { 1939a09ac397SOkash Khawaja tty_kref_put(tty); 1940a09ac397SOkash Khawaja mutex_unlock(&tty_mutex); 1941a09ac397SOkash Khawaja tty = ERR_PTR(-EBUSY); 1942a09ac397SOkash Khawaja goto out; 1943a09ac397SOkash Khawaja } 1944d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 1945d6203d0cSPeter Hurley retval = tty_lock_interruptible(tty); 19465e00bbfbSPeter Hurley tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ 1947d6203d0cSPeter Hurley if (retval) { 1948d6203d0cSPeter Hurley if (retval == -EINTR) 1949d6203d0cSPeter Hurley retval = -ERESTARTSYS; 1950d6203d0cSPeter Hurley tty = ERR_PTR(retval); 1951d6203d0cSPeter Hurley goto out; 1952d6203d0cSPeter Hurley } 1953d6203d0cSPeter Hurley retval = tty_reopen(tty); 1954d6203d0cSPeter Hurley if (retval < 0) { 1955d6203d0cSPeter Hurley tty_unlock(tty); 1956d6203d0cSPeter Hurley tty = ERR_PTR(retval); 1957d6203d0cSPeter Hurley } 1958d6203d0cSPeter Hurley } else { /* Returns with the tty_lock held for now */ 1959d6203d0cSPeter Hurley tty = tty_init_dev(driver, index); 1960d6203d0cSPeter Hurley mutex_unlock(&tty_mutex); 1961d6203d0cSPeter Hurley } 1962d6203d0cSPeter Hurley out: 1963d6203d0cSPeter Hurley tty_driver_kref_put(driver); 1964d6203d0cSPeter Hurley return tty; 1965d6203d0cSPeter Hurley } 1966d6203d0cSPeter Hurley 1967d6203d0cSPeter Hurley /** 196896fd7ce5SGreg Kroah-Hartman * tty_open - open a tty device 196996fd7ce5SGreg Kroah-Hartman * @inode: inode of device file 197096fd7ce5SGreg Kroah-Hartman * @filp: file pointer to tty 197196fd7ce5SGreg Kroah-Hartman * 197296fd7ce5SGreg Kroah-Hartman * tty_open and tty_release keep up the tty count that contains the 197396fd7ce5SGreg Kroah-Hartman * number of opens done on a tty. We cannot use the inode-count, as 197496fd7ce5SGreg Kroah-Hartman * different inodes might point to the same tty. 197596fd7ce5SGreg Kroah-Hartman * 197696fd7ce5SGreg Kroah-Hartman * Open-counting is needed for pty masters, as well as for keeping 197796fd7ce5SGreg Kroah-Hartman * track of serial lines: DTR is dropped when the last close happens. 197896fd7ce5SGreg Kroah-Hartman * (This is not done solely through tty->count, now. - Ted 1/27/92) 197996fd7ce5SGreg Kroah-Hartman * 198096fd7ce5SGreg Kroah-Hartman * The termios state of a pty is reset on first open so that 198196fd7ce5SGreg Kroah-Hartman * settings don't persist across reuse. 198296fd7ce5SGreg Kroah-Hartman * 19835b5e7040SJiri Slaby * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. 198496fd7ce5SGreg Kroah-Hartman * tty->count should protect the rest. 198596fd7ce5SGreg Kroah-Hartman * ->siglock protects ->signal/->sighand 198689c8d91eSAlan Cox * 198789c8d91eSAlan Cox * Note: the tty_unlock/lock cases without a ref are only safe due to 198889c8d91eSAlan Cox * tty_mutex 198996fd7ce5SGreg Kroah-Hartman */ 199096fd7ce5SGreg Kroah-Hartman 199196fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *inode, struct file *filp) 199296fd7ce5SGreg Kroah-Hartman { 1993b82154acSJiri Slaby struct tty_struct *tty; 199496fd7ce5SGreg Kroah-Hartman int noctty, retval; 199596fd7ce5SGreg Kroah-Hartman dev_t device = inode->i_rdev; 199696fd7ce5SGreg Kroah-Hartman unsigned saved_flags = filp->f_flags; 199796fd7ce5SGreg Kroah-Hartman 199896fd7ce5SGreg Kroah-Hartman nonseekable_open(inode, filp); 199996fd7ce5SGreg Kroah-Hartman 200096fd7ce5SGreg Kroah-Hartman retry_open: 2001fa90e1c9SJiri Slaby retval = tty_alloc_file(filp); 2002fa90e1c9SJiri Slaby if (retval) 2003fa90e1c9SJiri Slaby return -ENOMEM; 2004fa90e1c9SJiri Slaby 2005b82154acSJiri Slaby tty = tty_open_current_tty(device, filp); 2006d6203d0cSPeter Hurley if (!tty) 2007d6203d0cSPeter Hurley tty = tty_open_by_driver(device, inode, filp); 200852494eebSPeter Hurley 200996fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty)) { 2010d6203d0cSPeter Hurley tty_free_file(filp); 2011ba5db448SJiri Slaby retval = PTR_ERR(tty); 20127f22f6c9SPeter Hurley if (retval != -EAGAIN || signal_pending(current)) 2013d6203d0cSPeter Hurley return retval; 20147f22f6c9SPeter Hurley schedule(); 20157f22f6c9SPeter Hurley goto retry_open; 201696fd7ce5SGreg Kroah-Hartman } 201796fd7ce5SGreg Kroah-Hartman 2018fa90e1c9SJiri Slaby tty_add_file(tty, filp); 201996fd7ce5SGreg Kroah-Hartman 20209de44bd6SJiri Slaby check_tty_count(tty, __func__); 2021d435cefeSPeter Hurley tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); 2022accff793SPeter Hurley 202396fd7ce5SGreg Kroah-Hartman if (tty->ops->open) 202496fd7ce5SGreg Kroah-Hartman retval = tty->ops->open(tty, filp); 202596fd7ce5SGreg Kroah-Hartman else 202696fd7ce5SGreg Kroah-Hartman retval = -ENODEV; 202796fd7ce5SGreg Kroah-Hartman filp->f_flags = saved_flags; 202896fd7ce5SGreg Kroah-Hartman 202996fd7ce5SGreg Kroah-Hartman if (retval) { 2030d435cefeSPeter Hurley tty_debug_hangup(tty, "open error %d, releasing\n", retval); 2031accff793SPeter Hurley 203289c8d91eSAlan Cox tty_unlock(tty); /* need to call tty_release without BTM */ 203396fd7ce5SGreg Kroah-Hartman tty_release(inode, filp); 203496fd7ce5SGreg Kroah-Hartman if (retval != -ERESTARTSYS) 203596fd7ce5SGreg Kroah-Hartman return retval; 203696fd7ce5SGreg Kroah-Hartman 203796fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 203896fd7ce5SGreg Kroah-Hartman return retval; 203996fd7ce5SGreg Kroah-Hartman 204096fd7ce5SGreg Kroah-Hartman schedule(); 204196fd7ce5SGreg Kroah-Hartman /* 204296fd7ce5SGreg Kroah-Hartman * Need to reset f_op in case a hangup happened. 204396fd7ce5SGreg Kroah-Hartman */ 20441256937fSPeter Hurley if (tty_hung_up_p(filp)) 204596fd7ce5SGreg Kroah-Hartman filp->f_op = &tty_fops; 204696fd7ce5SGreg Kroah-Hartman goto retry_open; 204796fd7ce5SGreg Kroah-Hartman } 2048d4855e1fSPeter Hurley clear_bit(TTY_HUPPED, &tty->flags); 204996fd7ce5SGreg Kroah-Hartman 205011e1d4aaSPeter Hurley noctty = (filp->f_flags & O_NOCTTY) || 2051da5a0fc6SPeter Hurley (IS_ENABLED(CONFIG_VT) && device == MKDEV(TTY_MAJOR, 0)) || 205211e1d4aaSPeter Hurley device == MKDEV(TTYAUX_MAJOR, 1) || 205311e1d4aaSPeter Hurley (tty->driver->type == TTY_DRIVER_TYPE_PTY && 205411e1d4aaSPeter Hurley tty->driver->subtype == PTY_TYPE_MASTER); 2055a1235b3eSNicolas Pitre if (!noctty) 2056a1235b3eSNicolas Pitre tty_open_proc_set_tty(filp, tty); 205789c8d91eSAlan Cox tty_unlock(tty); 205896fd7ce5SGreg Kroah-Hartman return 0; 205996fd7ce5SGreg Kroah-Hartman } 206096fd7ce5SGreg Kroah-Hartman 206196fd7ce5SGreg Kroah-Hartman 206296fd7ce5SGreg Kroah-Hartman 206396fd7ce5SGreg Kroah-Hartman /** 206496fd7ce5SGreg Kroah-Hartman * tty_poll - check tty status 206596fd7ce5SGreg Kroah-Hartman * @filp: file being polled 206696fd7ce5SGreg Kroah-Hartman * @wait: poll wait structures to update 206796fd7ce5SGreg Kroah-Hartman * 206896fd7ce5SGreg Kroah-Hartman * Call the line discipline polling method to obtain the poll 206996fd7ce5SGreg Kroah-Hartman * status of the device. 207096fd7ce5SGreg Kroah-Hartman * 207196fd7ce5SGreg Kroah-Hartman * Locking: locks called line discipline but ldisc poll method 207296fd7ce5SGreg Kroah-Hartman * may be re-entered freely by other callers. 207396fd7ce5SGreg Kroah-Hartman */ 207496fd7ce5SGreg Kroah-Hartman 2075afc9a42bSAl Viro static __poll_t tty_poll(struct file *filp, poll_table *wait) 207696fd7ce5SGreg Kroah-Hartman { 207796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 207896fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 2079e6c8adcaSAl Viro __poll_t ret = 0; 208096fd7ce5SGreg Kroah-Hartman 20816131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) 208296fd7ce5SGreg Kroah-Hartman return 0; 208396fd7ce5SGreg Kroah-Hartman 208496fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2085e55afd11SPeter Hurley if (!ld) 2086e55afd11SPeter Hurley return hung_up_tty_poll(filp, wait); 208796fd7ce5SGreg Kroah-Hartman if (ld->ops->poll) 2088c961bfb1SPeter Hurley ret = ld->ops->poll(tty, filp, wait); 208996fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 209096fd7ce5SGreg Kroah-Hartman return ret; 209196fd7ce5SGreg Kroah-Hartman } 209296fd7ce5SGreg Kroah-Hartman 209396fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on) 209496fd7ce5SGreg Kroah-Hartman { 209596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(filp); 209696fd7ce5SGreg Kroah-Hartman unsigned long flags; 209796fd7ce5SGreg Kroah-Hartman int retval = 0; 209896fd7ce5SGreg Kroah-Hartman 20996131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) 210096fd7ce5SGreg Kroah-Hartman goto out; 210196fd7ce5SGreg Kroah-Hartman 210296fd7ce5SGreg Kroah-Hartman retval = fasync_helper(fd, filp, on, &tty->fasync); 210396fd7ce5SGreg Kroah-Hartman if (retval <= 0) 210496fd7ce5SGreg Kroah-Hartman goto out; 210596fd7ce5SGreg Kroah-Hartman 210696fd7ce5SGreg Kroah-Hartman if (on) { 210796fd7ce5SGreg Kroah-Hartman enum pid_type type; 210896fd7ce5SGreg Kroah-Hartman struct pid *pid; 2109f6c8dbe6SPeter Hurley 211096fd7ce5SGreg Kroah-Hartman spin_lock_irqsave(&tty->ctrl_lock, flags); 211196fd7ce5SGreg Kroah-Hartman if (tty->pgrp) { 211296fd7ce5SGreg Kroah-Hartman pid = tty->pgrp; 211396fd7ce5SGreg Kroah-Hartman type = PIDTYPE_PGID; 211496fd7ce5SGreg Kroah-Hartman } else { 211596fd7ce5SGreg Kroah-Hartman pid = task_pid(current); 211696fd7ce5SGreg Kroah-Hartman type = PIDTYPE_PID; 211796fd7ce5SGreg Kroah-Hartman } 211896fd7ce5SGreg Kroah-Hartman get_pid(pid); 211996fd7ce5SGreg Kroah-Hartman spin_unlock_irqrestore(&tty->ctrl_lock, flags); 2120e0b93eddSJeff Layton __f_setown(filp, pid, type, 0); 212196fd7ce5SGreg Kroah-Hartman put_pid(pid); 2122e0b93eddSJeff Layton retval = 0; 212396fd7ce5SGreg Kroah-Hartman } 212496fd7ce5SGreg Kroah-Hartman out: 212596fd7ce5SGreg Kroah-Hartman return retval; 212696fd7ce5SGreg Kroah-Hartman } 212796fd7ce5SGreg Kroah-Hartman 212896fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on) 212996fd7ce5SGreg Kroah-Hartman { 213089c8d91eSAlan Cox struct tty_struct *tty = file_tty(filp); 2131a8f3a297SPeter Hurley int retval = -ENOTTY; 213289c8d91eSAlan Cox 213389c8d91eSAlan Cox tty_lock(tty); 2134a8f3a297SPeter Hurley if (!tty_hung_up_p(filp)) 213596fd7ce5SGreg Kroah-Hartman retval = __tty_fasync(fd, filp, on); 213689c8d91eSAlan Cox tty_unlock(tty); 213789c8d91eSAlan Cox 213896fd7ce5SGreg Kroah-Hartman return retval; 213996fd7ce5SGreg Kroah-Hartman } 214096fd7ce5SGreg Kroah-Hartman 214196fd7ce5SGreg Kroah-Hartman /** 214296fd7ce5SGreg Kroah-Hartman * tiocsti - fake input character 214396fd7ce5SGreg Kroah-Hartman * @tty: tty to fake input into 214496fd7ce5SGreg Kroah-Hartman * @p: pointer to character 214596fd7ce5SGreg Kroah-Hartman * 214696fd7ce5SGreg Kroah-Hartman * Fake input to a tty device. Does the necessary locking and 214796fd7ce5SGreg Kroah-Hartman * input management. 214896fd7ce5SGreg Kroah-Hartman * 214996fd7ce5SGreg Kroah-Hartman * FIXME: does not honour flow control ?? 215096fd7ce5SGreg Kroah-Hartman * 215196fd7ce5SGreg Kroah-Hartman * Locking: 2152137084bbSPeter Hurley * Called functions take tty_ldiscs_lock 215396fd7ce5SGreg Kroah-Hartman * current->signal->tty check is safe without locks 215496fd7ce5SGreg Kroah-Hartman * 215596fd7ce5SGreg Kroah-Hartman * FIXME: may race normal receive processing 215696fd7ce5SGreg Kroah-Hartman */ 215796fd7ce5SGreg Kroah-Hartman 215896fd7ce5SGreg Kroah-Hartman static int tiocsti(struct tty_struct *tty, char __user *p) 215996fd7ce5SGreg Kroah-Hartman { 216096fd7ce5SGreg Kroah-Hartman char ch, mbz = 0; 216196fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 216296fd7ce5SGreg Kroah-Hartman 216396fd7ce5SGreg Kroah-Hartman if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) 216496fd7ce5SGreg Kroah-Hartman return -EPERM; 216596fd7ce5SGreg Kroah-Hartman if (get_user(ch, p)) 216696fd7ce5SGreg Kroah-Hartman return -EFAULT; 216796fd7ce5SGreg Kroah-Hartman tty_audit_tiocsti(tty, ch); 216896fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2169e55afd11SPeter Hurley if (!ld) 2170e55afd11SPeter Hurley return -EIO; 217196fd7ce5SGreg Kroah-Hartman ld->ops->receive_buf(tty, &ch, &mbz, 1); 217296fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 217396fd7ce5SGreg Kroah-Hartman return 0; 217496fd7ce5SGreg Kroah-Hartman } 217596fd7ce5SGreg Kroah-Hartman 217696fd7ce5SGreg Kroah-Hartman /** 217796fd7ce5SGreg Kroah-Hartman * tiocgwinsz - implement window query ioctl 217896fd7ce5SGreg Kroah-Hartman * @tty; tty 217996fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 218096fd7ce5SGreg Kroah-Hartman * 218196fd7ce5SGreg Kroah-Hartman * Copies the kernel idea of the window size into the user buffer. 218296fd7ce5SGreg Kroah-Hartman * 2183dee4a0beSPeter Hurley * Locking: tty->winsize_mutex is taken to ensure the winsize data 218496fd7ce5SGreg Kroah-Hartman * is consistent. 218596fd7ce5SGreg Kroah-Hartman */ 218696fd7ce5SGreg Kroah-Hartman 218796fd7ce5SGreg Kroah-Hartman static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) 218896fd7ce5SGreg Kroah-Hartman { 218996fd7ce5SGreg Kroah-Hartman int err; 219096fd7ce5SGreg Kroah-Hartman 2191dee4a0beSPeter Hurley mutex_lock(&tty->winsize_mutex); 219296fd7ce5SGreg Kroah-Hartman err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); 2193dee4a0beSPeter Hurley mutex_unlock(&tty->winsize_mutex); 219496fd7ce5SGreg Kroah-Hartman 219596fd7ce5SGreg Kroah-Hartman return err ? -EFAULT: 0; 219696fd7ce5SGreg Kroah-Hartman } 219796fd7ce5SGreg Kroah-Hartman 219896fd7ce5SGreg Kroah-Hartman /** 219996fd7ce5SGreg Kroah-Hartman * tty_do_resize - resize event 220096fd7ce5SGreg Kroah-Hartman * @tty: tty being resized 220196fd7ce5SGreg Kroah-Hartman * @rows: rows (character) 220296fd7ce5SGreg Kroah-Hartman * @cols: cols (character) 220396fd7ce5SGreg Kroah-Hartman * 220496fd7ce5SGreg Kroah-Hartman * Update the termios variables and send the necessary signals to 220596fd7ce5SGreg Kroah-Hartman * peform a terminal resize correctly 220696fd7ce5SGreg Kroah-Hartman */ 220796fd7ce5SGreg Kroah-Hartman 220896fd7ce5SGreg Kroah-Hartman int tty_do_resize(struct tty_struct *tty, struct winsize *ws) 220996fd7ce5SGreg Kroah-Hartman { 221096fd7ce5SGreg Kroah-Hartman struct pid *pgrp; 221196fd7ce5SGreg Kroah-Hartman 221296fd7ce5SGreg Kroah-Hartman /* Lock the tty */ 2213dee4a0beSPeter Hurley mutex_lock(&tty->winsize_mutex); 221496fd7ce5SGreg Kroah-Hartman if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 221596fd7ce5SGreg Kroah-Hartman goto done; 221696fd7ce5SGreg Kroah-Hartman 22175b239542SPeter Hurley /* Signal the foreground process group */ 22185b239542SPeter Hurley pgrp = tty_get_pgrp(tty); 221996fd7ce5SGreg Kroah-Hartman if (pgrp) 222096fd7ce5SGreg Kroah-Hartman kill_pgrp(pgrp, SIGWINCH, 1); 222196fd7ce5SGreg Kroah-Hartman put_pid(pgrp); 222296fd7ce5SGreg Kroah-Hartman 222396fd7ce5SGreg Kroah-Hartman tty->winsize = *ws; 222496fd7ce5SGreg Kroah-Hartman done: 2225dee4a0beSPeter Hurley mutex_unlock(&tty->winsize_mutex); 222696fd7ce5SGreg Kroah-Hartman return 0; 222796fd7ce5SGreg Kroah-Hartman } 22284d334fd1SMartin Schwidefsky EXPORT_SYMBOL(tty_do_resize); 222996fd7ce5SGreg Kroah-Hartman 223096fd7ce5SGreg Kroah-Hartman /** 223196fd7ce5SGreg Kroah-Hartman * tiocswinsz - implement window size set ioctl 223296fd7ce5SGreg Kroah-Hartman * @tty; tty side of tty 223396fd7ce5SGreg Kroah-Hartman * @arg: user buffer for result 223496fd7ce5SGreg Kroah-Hartman * 223596fd7ce5SGreg Kroah-Hartman * Copies the user idea of the window size to the kernel. Traditionally 223696fd7ce5SGreg Kroah-Hartman * this is just advisory information but for the Linux console it 223796fd7ce5SGreg Kroah-Hartman * actually has driver level meaning and triggers a VC resize. 223896fd7ce5SGreg Kroah-Hartman * 223996fd7ce5SGreg Kroah-Hartman * Locking: 224025985edcSLucas De Marchi * Driver dependent. The default do_resize method takes the 224196fd7ce5SGreg Kroah-Hartman * tty termios mutex and ctrl_lock. The console takes its own lock 224296fd7ce5SGreg Kroah-Hartman * then calls into the default method. 224396fd7ce5SGreg Kroah-Hartman */ 224496fd7ce5SGreg Kroah-Hartman 224596fd7ce5SGreg Kroah-Hartman static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) 224696fd7ce5SGreg Kroah-Hartman { 224796fd7ce5SGreg Kroah-Hartman struct winsize tmp_ws; 224896fd7ce5SGreg Kroah-Hartman if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) 224996fd7ce5SGreg Kroah-Hartman return -EFAULT; 225096fd7ce5SGreg Kroah-Hartman 225196fd7ce5SGreg Kroah-Hartman if (tty->ops->resize) 225296fd7ce5SGreg Kroah-Hartman return tty->ops->resize(tty, &tmp_ws); 225396fd7ce5SGreg Kroah-Hartman else 225496fd7ce5SGreg Kroah-Hartman return tty_do_resize(tty, &tmp_ws); 225596fd7ce5SGreg Kroah-Hartman } 225696fd7ce5SGreg Kroah-Hartman 225796fd7ce5SGreg Kroah-Hartman /** 225896fd7ce5SGreg Kroah-Hartman * tioccons - allow admin to move logical console 225996fd7ce5SGreg Kroah-Hartman * @file: the file to become console 226096fd7ce5SGreg Kroah-Hartman * 226125985edcSLucas De Marchi * Allow the administrator to move the redirected console device 226296fd7ce5SGreg Kroah-Hartman * 226396fd7ce5SGreg Kroah-Hartman * Locking: uses redirect_lock to guard the redirect information 226496fd7ce5SGreg Kroah-Hartman */ 226596fd7ce5SGreg Kroah-Hartman 226696fd7ce5SGreg Kroah-Hartman static int tioccons(struct file *file) 226796fd7ce5SGreg Kroah-Hartman { 226896fd7ce5SGreg Kroah-Hartman if (!capable(CAP_SYS_ADMIN)) 226996fd7ce5SGreg Kroah-Hartman return -EPERM; 227096fd7ce5SGreg Kroah-Hartman if (file->f_op->write == redirected_tty_write) { 227196fd7ce5SGreg Kroah-Hartman struct file *f; 227296fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 227396fd7ce5SGreg Kroah-Hartman f = redirect; 227496fd7ce5SGreg Kroah-Hartman redirect = NULL; 227596fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 227696fd7ce5SGreg Kroah-Hartman if (f) 227796fd7ce5SGreg Kroah-Hartman fput(f); 227896fd7ce5SGreg Kroah-Hartman return 0; 227996fd7ce5SGreg Kroah-Hartman } 228096fd7ce5SGreg Kroah-Hartman spin_lock(&redirect_lock); 228196fd7ce5SGreg Kroah-Hartman if (redirect) { 228296fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 228396fd7ce5SGreg Kroah-Hartman return -EBUSY; 228496fd7ce5SGreg Kroah-Hartman } 2285cb0942b8SAl Viro redirect = get_file(file); 228696fd7ce5SGreg Kroah-Hartman spin_unlock(&redirect_lock); 228796fd7ce5SGreg Kroah-Hartman return 0; 228896fd7ce5SGreg Kroah-Hartman } 228996fd7ce5SGreg Kroah-Hartman 229096fd7ce5SGreg Kroah-Hartman /** 229196fd7ce5SGreg Kroah-Hartman * fionbio - non blocking ioctl 229296fd7ce5SGreg Kroah-Hartman * @file: file to set blocking value 229396fd7ce5SGreg Kroah-Hartman * @p: user parameter 229496fd7ce5SGreg Kroah-Hartman * 229596fd7ce5SGreg Kroah-Hartman * Historical tty interfaces had a blocking control ioctl before 229696fd7ce5SGreg Kroah-Hartman * the generic functionality existed. This piece of history is preserved 229796fd7ce5SGreg Kroah-Hartman * in the expected tty API of posix OS's. 229896fd7ce5SGreg Kroah-Hartman * 229996fd7ce5SGreg Kroah-Hartman * Locking: none, the open file handle ensures it won't go away. 230096fd7ce5SGreg Kroah-Hartman */ 230196fd7ce5SGreg Kroah-Hartman 230296fd7ce5SGreg Kroah-Hartman static int fionbio(struct file *file, int __user *p) 230396fd7ce5SGreg Kroah-Hartman { 230496fd7ce5SGreg Kroah-Hartman int nonblock; 230596fd7ce5SGreg Kroah-Hartman 230696fd7ce5SGreg Kroah-Hartman if (get_user(nonblock, p)) 230796fd7ce5SGreg Kroah-Hartman return -EFAULT; 230896fd7ce5SGreg Kroah-Hartman 230996fd7ce5SGreg Kroah-Hartman spin_lock(&file->f_lock); 231096fd7ce5SGreg Kroah-Hartman if (nonblock) 231196fd7ce5SGreg Kroah-Hartman file->f_flags |= O_NONBLOCK; 231296fd7ce5SGreg Kroah-Hartman else 231396fd7ce5SGreg Kroah-Hartman file->f_flags &= ~O_NONBLOCK; 231496fd7ce5SGreg Kroah-Hartman spin_unlock(&file->f_lock); 231596fd7ce5SGreg Kroah-Hartman return 0; 231696fd7ce5SGreg Kroah-Hartman } 231796fd7ce5SGreg Kroah-Hartman 231896fd7ce5SGreg Kroah-Hartman /** 231996fd7ce5SGreg Kroah-Hartman * tiocsetd - set line discipline 232096fd7ce5SGreg Kroah-Hartman * @tty: tty device 232196fd7ce5SGreg Kroah-Hartman * @p: pointer to user data 232296fd7ce5SGreg Kroah-Hartman * 232396fd7ce5SGreg Kroah-Hartman * Set the line discipline according to user request. 232496fd7ce5SGreg Kroah-Hartman * 232596fd7ce5SGreg Kroah-Hartman * Locking: see tty_set_ldisc, this function is just a helper 232696fd7ce5SGreg Kroah-Hartman */ 232796fd7ce5SGreg Kroah-Hartman 232896fd7ce5SGreg Kroah-Hartman static int tiocsetd(struct tty_struct *tty, int __user *p) 232996fd7ce5SGreg Kroah-Hartman { 2330c12da96fSPeter Hurley int disc; 233196fd7ce5SGreg Kroah-Hartman int ret; 233296fd7ce5SGreg Kroah-Hartman 2333c12da96fSPeter Hurley if (get_user(disc, p)) 233496fd7ce5SGreg Kroah-Hartman return -EFAULT; 233596fd7ce5SGreg Kroah-Hartman 2336c12da96fSPeter Hurley ret = tty_set_ldisc(tty, disc); 233796fd7ce5SGreg Kroah-Hartman 233896fd7ce5SGreg Kroah-Hartman return ret; 233996fd7ce5SGreg Kroah-Hartman } 234096fd7ce5SGreg Kroah-Hartman 234196fd7ce5SGreg Kroah-Hartman /** 23425c17c861SPeter Hurley * tiocgetd - get line discipline 23435c17c861SPeter Hurley * @tty: tty device 23445c17c861SPeter Hurley * @p: pointer to user data 23455c17c861SPeter Hurley * 23465c17c861SPeter Hurley * Retrieves the line discipline id directly from the ldisc. 23475c17c861SPeter Hurley * 23485c17c861SPeter Hurley * Locking: waits for ldisc reference (in case the line discipline 23495c17c861SPeter Hurley * is changing or the tty is being hungup) 23505c17c861SPeter Hurley */ 23515c17c861SPeter Hurley 23525c17c861SPeter Hurley static int tiocgetd(struct tty_struct *tty, int __user *p) 23535c17c861SPeter Hurley { 23545c17c861SPeter Hurley struct tty_ldisc *ld; 23555c17c861SPeter Hurley int ret; 23565c17c861SPeter Hurley 23575c17c861SPeter Hurley ld = tty_ldisc_ref_wait(tty); 2358e55afd11SPeter Hurley if (!ld) 2359e55afd11SPeter Hurley return -EIO; 23605c17c861SPeter Hurley ret = put_user(ld->ops->num, p); 23615c17c861SPeter Hurley tty_ldisc_deref(ld); 23625c17c861SPeter Hurley return ret; 23635c17c861SPeter Hurley } 23645c17c861SPeter Hurley 23655c17c861SPeter Hurley /** 236696fd7ce5SGreg Kroah-Hartman * send_break - performed time break 236796fd7ce5SGreg Kroah-Hartman * @tty: device to break on 236896fd7ce5SGreg Kroah-Hartman * @duration: timeout in mS 236996fd7ce5SGreg Kroah-Hartman * 237096fd7ce5SGreg Kroah-Hartman * Perform a timed break on hardware that lacks its own driver level 237196fd7ce5SGreg Kroah-Hartman * timed break functionality. 237296fd7ce5SGreg Kroah-Hartman * 237396fd7ce5SGreg Kroah-Hartman * Locking: 237496fd7ce5SGreg Kroah-Hartman * atomic_write_lock serializes 237596fd7ce5SGreg Kroah-Hartman * 237696fd7ce5SGreg Kroah-Hartman */ 237796fd7ce5SGreg Kroah-Hartman 237896fd7ce5SGreg Kroah-Hartman static int send_break(struct tty_struct *tty, unsigned int duration) 237996fd7ce5SGreg Kroah-Hartman { 238096fd7ce5SGreg Kroah-Hartman int retval; 238196fd7ce5SGreg Kroah-Hartman 238296fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl == NULL) 238396fd7ce5SGreg Kroah-Hartman return 0; 238496fd7ce5SGreg Kroah-Hartman 238596fd7ce5SGreg Kroah-Hartman if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) 238696fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, duration); 238796fd7ce5SGreg Kroah-Hartman else { 238896fd7ce5SGreg Kroah-Hartman /* Do the work ourselves */ 238996fd7ce5SGreg Kroah-Hartman if (tty_write_lock(tty, 0) < 0) 239096fd7ce5SGreg Kroah-Hartman return -EINTR; 239196fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, -1); 239296fd7ce5SGreg Kroah-Hartman if (retval) 239396fd7ce5SGreg Kroah-Hartman goto out; 239496fd7ce5SGreg Kroah-Hartman if (!signal_pending(current)) 239596fd7ce5SGreg Kroah-Hartman msleep_interruptible(duration); 239696fd7ce5SGreg Kroah-Hartman retval = tty->ops->break_ctl(tty, 0); 239796fd7ce5SGreg Kroah-Hartman out: 239896fd7ce5SGreg Kroah-Hartman tty_write_unlock(tty); 239996fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 240096fd7ce5SGreg Kroah-Hartman retval = -EINTR; 240196fd7ce5SGreg Kroah-Hartman } 240296fd7ce5SGreg Kroah-Hartman return retval; 240396fd7ce5SGreg Kroah-Hartman } 240496fd7ce5SGreg Kroah-Hartman 240596fd7ce5SGreg Kroah-Hartman /** 240696fd7ce5SGreg Kroah-Hartman * tty_tiocmget - get modem status 240796fd7ce5SGreg Kroah-Hartman * @tty: tty device 240896fd7ce5SGreg Kroah-Hartman * @file: user file pointer 240996fd7ce5SGreg Kroah-Hartman * @p: pointer to result 241096fd7ce5SGreg Kroah-Hartman * 241196fd7ce5SGreg Kroah-Hartman * Obtain the modem status bits from the tty driver if the feature 241296fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 241396fd7ce5SGreg Kroah-Hartman * 241496fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 241596fd7ce5SGreg Kroah-Hartman */ 241696fd7ce5SGreg Kroah-Hartman 241760b33c13SAlan Cox static int tty_tiocmget(struct tty_struct *tty, int __user *p) 241896fd7ce5SGreg Kroah-Hartman { 241996fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 242096fd7ce5SGreg Kroah-Hartman 242196fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmget) { 242260b33c13SAlan Cox retval = tty->ops->tiocmget(tty); 242396fd7ce5SGreg Kroah-Hartman 242496fd7ce5SGreg Kroah-Hartman if (retval >= 0) 242596fd7ce5SGreg Kroah-Hartman retval = put_user(retval, p); 242696fd7ce5SGreg Kroah-Hartman } 242796fd7ce5SGreg Kroah-Hartman return retval; 242896fd7ce5SGreg Kroah-Hartman } 242996fd7ce5SGreg Kroah-Hartman 243096fd7ce5SGreg Kroah-Hartman /** 243196fd7ce5SGreg Kroah-Hartman * tty_tiocmset - set modem status 243296fd7ce5SGreg Kroah-Hartman * @tty: tty device 243396fd7ce5SGreg Kroah-Hartman * @cmd: command - clear bits, set bits or set all 243496fd7ce5SGreg Kroah-Hartman * @p: pointer to desired bits 243596fd7ce5SGreg Kroah-Hartman * 243696fd7ce5SGreg Kroah-Hartman * Set the modem status bits from the tty driver if the feature 243796fd7ce5SGreg Kroah-Hartman * is supported. Return -EINVAL if it is not available. 243896fd7ce5SGreg Kroah-Hartman * 243996fd7ce5SGreg Kroah-Hartman * Locking: none (up to the driver) 244096fd7ce5SGreg Kroah-Hartman */ 244196fd7ce5SGreg Kroah-Hartman 244220b9d177SAlan Cox static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, 244396fd7ce5SGreg Kroah-Hartman unsigned __user *p) 244496fd7ce5SGreg Kroah-Hartman { 244596fd7ce5SGreg Kroah-Hartman int retval; 244696fd7ce5SGreg Kroah-Hartman unsigned int set, clear, val; 244796fd7ce5SGreg Kroah-Hartman 244896fd7ce5SGreg Kroah-Hartman if (tty->ops->tiocmset == NULL) 244996fd7ce5SGreg Kroah-Hartman return -EINVAL; 245096fd7ce5SGreg Kroah-Hartman 245196fd7ce5SGreg Kroah-Hartman retval = get_user(val, p); 245296fd7ce5SGreg Kroah-Hartman if (retval) 245396fd7ce5SGreg Kroah-Hartman return retval; 245496fd7ce5SGreg Kroah-Hartman set = clear = 0; 245596fd7ce5SGreg Kroah-Hartman switch (cmd) { 245696fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 245796fd7ce5SGreg Kroah-Hartman set = val; 245896fd7ce5SGreg Kroah-Hartman break; 245996fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 246096fd7ce5SGreg Kroah-Hartman clear = val; 246196fd7ce5SGreg Kroah-Hartman break; 246296fd7ce5SGreg Kroah-Hartman case TIOCMSET: 246396fd7ce5SGreg Kroah-Hartman set = val; 246496fd7ce5SGreg Kroah-Hartman clear = ~val; 246596fd7ce5SGreg Kroah-Hartman break; 246696fd7ce5SGreg Kroah-Hartman } 246796fd7ce5SGreg Kroah-Hartman set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 246896fd7ce5SGreg Kroah-Hartman clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; 246920b9d177SAlan Cox return tty->ops->tiocmset(tty, set, clear); 247096fd7ce5SGreg Kroah-Hartman } 247196fd7ce5SGreg Kroah-Hartman 247296fd7ce5SGreg Kroah-Hartman static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) 247396fd7ce5SGreg Kroah-Hartman { 247496fd7ce5SGreg Kroah-Hartman int retval = -EINVAL; 247596fd7ce5SGreg Kroah-Hartman struct serial_icounter_struct icount; 247696fd7ce5SGreg Kroah-Hartman memset(&icount, 0, sizeof(icount)); 247796fd7ce5SGreg Kroah-Hartman if (tty->ops->get_icount) 247896fd7ce5SGreg Kroah-Hartman retval = tty->ops->get_icount(tty, &icount); 247996fd7ce5SGreg Kroah-Hartman if (retval != 0) 248096fd7ce5SGreg Kroah-Hartman return retval; 248196fd7ce5SGreg Kroah-Hartman if (copy_to_user(arg, &icount, sizeof(icount))) 248296fd7ce5SGreg Kroah-Hartman return -EFAULT; 248396fd7ce5SGreg Kroah-Hartman return 0; 248496fd7ce5SGreg Kroah-Hartman } 248596fd7ce5SGreg Kroah-Hartman 24868a8ae62fSJiri Slaby static void tty_warn_deprecated_flags(struct serial_struct __user *ss) 24878a8ae62fSJiri Slaby { 24888a8ae62fSJiri Slaby static DEFINE_RATELIMIT_STATE(depr_flags, 24898a8ae62fSJiri Slaby DEFAULT_RATELIMIT_INTERVAL, 24908a8ae62fSJiri Slaby DEFAULT_RATELIMIT_BURST); 24918a8ae62fSJiri Slaby char comm[TASK_COMM_LEN]; 24928a8ae62fSJiri Slaby int flags; 24938a8ae62fSJiri Slaby 24948a8ae62fSJiri Slaby if (get_user(flags, &ss->flags)) 24958a8ae62fSJiri Slaby return; 24968a8ae62fSJiri Slaby 24978a8ae62fSJiri Slaby flags &= ASYNC_DEPRECATED; 24988a8ae62fSJiri Slaby 24998a8ae62fSJiri Slaby if (flags && __ratelimit(&depr_flags)) 2500d437fa91SJoe Perches pr_warn("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n", 25018a8ae62fSJiri Slaby __func__, get_task_comm(comm, current), flags); 25028a8ae62fSJiri Slaby } 25038a8ae62fSJiri Slaby 25048f166e00SPeter Hurley /* 25058f166e00SPeter Hurley * if pty, return the slave side (real_tty) 25068f166e00SPeter Hurley * otherwise, return self 25078f166e00SPeter Hurley */ 25088f166e00SPeter Hurley static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) 250996fd7ce5SGreg Kroah-Hartman { 251096fd7ce5SGreg Kroah-Hartman if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 251196fd7ce5SGreg Kroah-Hartman tty->driver->subtype == PTY_TYPE_MASTER) 251296fd7ce5SGreg Kroah-Hartman tty = tty->link; 251396fd7ce5SGreg Kroah-Hartman return tty; 251496fd7ce5SGreg Kroah-Hartman } 251596fd7ce5SGreg Kroah-Hartman 251696fd7ce5SGreg Kroah-Hartman /* 251796fd7ce5SGreg Kroah-Hartman * Split this up, as gcc can choke on it otherwise.. 251896fd7ce5SGreg Kroah-Hartman */ 251996fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 252096fd7ce5SGreg Kroah-Hartman { 252196fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 252296fd7ce5SGreg Kroah-Hartman struct tty_struct *real_tty; 252396fd7ce5SGreg Kroah-Hartman void __user *p = (void __user *)arg; 252496fd7ce5SGreg Kroah-Hartman int retval; 252596fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 252696fd7ce5SGreg Kroah-Hartman 25276131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) 252896fd7ce5SGreg Kroah-Hartman return -EINVAL; 252996fd7ce5SGreg Kroah-Hartman 253096fd7ce5SGreg Kroah-Hartman real_tty = tty_pair_get_tty(tty); 253196fd7ce5SGreg Kroah-Hartman 253296fd7ce5SGreg Kroah-Hartman /* 253396fd7ce5SGreg Kroah-Hartman * Factor out some common prep work 253496fd7ce5SGreg Kroah-Hartman */ 253596fd7ce5SGreg Kroah-Hartman switch (cmd) { 253696fd7ce5SGreg Kroah-Hartman case TIOCSETD: 253796fd7ce5SGreg Kroah-Hartman case TIOCSBRK: 253896fd7ce5SGreg Kroah-Hartman case TIOCCBRK: 253996fd7ce5SGreg Kroah-Hartman case TCSBRK: 254096fd7ce5SGreg Kroah-Hartman case TCSBRKP: 254196fd7ce5SGreg Kroah-Hartman retval = tty_check_change(tty); 254296fd7ce5SGreg Kroah-Hartman if (retval) 254396fd7ce5SGreg Kroah-Hartman return retval; 254496fd7ce5SGreg Kroah-Hartman if (cmd != TIOCCBRK) { 254596fd7ce5SGreg Kroah-Hartman tty_wait_until_sent(tty, 0); 254696fd7ce5SGreg Kroah-Hartman if (signal_pending(current)) 254796fd7ce5SGreg Kroah-Hartman return -EINTR; 254896fd7ce5SGreg Kroah-Hartman } 254996fd7ce5SGreg Kroah-Hartman break; 255096fd7ce5SGreg Kroah-Hartman } 255196fd7ce5SGreg Kroah-Hartman 255296fd7ce5SGreg Kroah-Hartman /* 255396fd7ce5SGreg Kroah-Hartman * Now do the stuff. 255496fd7ce5SGreg Kroah-Hartman */ 255596fd7ce5SGreg Kroah-Hartman switch (cmd) { 255696fd7ce5SGreg Kroah-Hartman case TIOCSTI: 255796fd7ce5SGreg Kroah-Hartman return tiocsti(tty, p); 255896fd7ce5SGreg Kroah-Hartman case TIOCGWINSZ: 255996fd7ce5SGreg Kroah-Hartman return tiocgwinsz(real_tty, p); 256096fd7ce5SGreg Kroah-Hartman case TIOCSWINSZ: 256196fd7ce5SGreg Kroah-Hartman return tiocswinsz(real_tty, p); 256296fd7ce5SGreg Kroah-Hartman case TIOCCONS: 256396fd7ce5SGreg Kroah-Hartman return real_tty != tty ? -EINVAL : tioccons(file); 256496fd7ce5SGreg Kroah-Hartman case FIONBIO: 256596fd7ce5SGreg Kroah-Hartman return fionbio(file, p); 256696fd7ce5SGreg Kroah-Hartman case TIOCEXCL: 256796fd7ce5SGreg Kroah-Hartman set_bit(TTY_EXCLUSIVE, &tty->flags); 256896fd7ce5SGreg Kroah-Hartman return 0; 256996fd7ce5SGreg Kroah-Hartman case TIOCNXCL: 257096fd7ce5SGreg Kroah-Hartman clear_bit(TTY_EXCLUSIVE, &tty->flags); 257196fd7ce5SGreg Kroah-Hartman return 0; 257284fd7bdfSCyrill Gorcunov case TIOCGEXCL: 257384fd7bdfSCyrill Gorcunov { 257484fd7bdfSCyrill Gorcunov int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); 257584fd7bdfSCyrill Gorcunov return put_user(excl, (int __user *)p); 257684fd7bdfSCyrill Gorcunov } 257796fd7ce5SGreg Kroah-Hartman case TIOCGETD: 25785c17c861SPeter Hurley return tiocgetd(tty, p); 257996fd7ce5SGreg Kroah-Hartman case TIOCSETD: 258096fd7ce5SGreg Kroah-Hartman return tiocsetd(tty, p); 25813c95c985SKay Sievers case TIOCVHANGUP: 25823c95c985SKay Sievers if (!capable(CAP_SYS_ADMIN)) 25833c95c985SKay Sievers return -EPERM; 25843c95c985SKay Sievers tty_vhangup(tty); 25853c95c985SKay Sievers return 0; 2586b7b8de08SWerner Fink case TIOCGDEV: 2587b7b8de08SWerner Fink { 2588b7b8de08SWerner Fink unsigned int ret = new_encode_dev(tty_devnum(real_tty)); 2589b7b8de08SWerner Fink return put_user(ret, (unsigned int __user *)p); 2590b7b8de08SWerner Fink } 259196fd7ce5SGreg Kroah-Hartman /* 259296fd7ce5SGreg Kroah-Hartman * Break handling 259396fd7ce5SGreg Kroah-Hartman */ 259496fd7ce5SGreg Kroah-Hartman case TIOCSBRK: /* Turn break on, unconditionally */ 259596fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 259696fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, -1); 259796fd7ce5SGreg Kroah-Hartman return 0; 259896fd7ce5SGreg Kroah-Hartman case TIOCCBRK: /* Turn break off, unconditionally */ 259996fd7ce5SGreg Kroah-Hartman if (tty->ops->break_ctl) 260096fd7ce5SGreg Kroah-Hartman return tty->ops->break_ctl(tty, 0); 260196fd7ce5SGreg Kroah-Hartman return 0; 260296fd7ce5SGreg Kroah-Hartman case TCSBRK: /* SVID version: non-zero arg --> no break */ 260396fd7ce5SGreg Kroah-Hartman /* non-zero arg means wait for all output data 260496fd7ce5SGreg Kroah-Hartman * to be sent (performed above) but don't send break. 260596fd7ce5SGreg Kroah-Hartman * This is used by the tcdrain() termios function. 260696fd7ce5SGreg Kroah-Hartman */ 260796fd7ce5SGreg Kroah-Hartman if (!arg) 260896fd7ce5SGreg Kroah-Hartman return send_break(tty, 250); 260996fd7ce5SGreg Kroah-Hartman return 0; 261096fd7ce5SGreg Kroah-Hartman case TCSBRKP: /* support for POSIX tcsendbreak() */ 261196fd7ce5SGreg Kroah-Hartman return send_break(tty, arg ? arg*100 : 250); 261296fd7ce5SGreg Kroah-Hartman 261396fd7ce5SGreg Kroah-Hartman case TIOCMGET: 261460b33c13SAlan Cox return tty_tiocmget(tty, p); 261596fd7ce5SGreg Kroah-Hartman case TIOCMSET: 261696fd7ce5SGreg Kroah-Hartman case TIOCMBIC: 261796fd7ce5SGreg Kroah-Hartman case TIOCMBIS: 261820b9d177SAlan Cox return tty_tiocmset(tty, cmd, p); 261996fd7ce5SGreg Kroah-Hartman case TIOCGICOUNT: 262096fd7ce5SGreg Kroah-Hartman retval = tty_tiocgicount(tty, p); 262196fd7ce5SGreg Kroah-Hartman /* For the moment allow fall through to the old method */ 262296fd7ce5SGreg Kroah-Hartman if (retval != -EINVAL) 262396fd7ce5SGreg Kroah-Hartman return retval; 262496fd7ce5SGreg Kroah-Hartman break; 262596fd7ce5SGreg Kroah-Hartman case TCFLSH: 262696fd7ce5SGreg Kroah-Hartman switch (arg) { 262796fd7ce5SGreg Kroah-Hartman case TCIFLUSH: 262896fd7ce5SGreg Kroah-Hartman case TCIOFLUSH: 262996fd7ce5SGreg Kroah-Hartman /* flush tty buffer and allow ldisc to process ioctl */ 263086c80a8eSPeter Hurley tty_buffer_flush(tty, NULL); 263196fd7ce5SGreg Kroah-Hartman break; 263296fd7ce5SGreg Kroah-Hartman } 263396fd7ce5SGreg Kroah-Hartman break; 26348a8ae62fSJiri Slaby case TIOCSSERIAL: 26358a8ae62fSJiri Slaby tty_warn_deprecated_flags(p); 26368a8ae62fSJiri Slaby break; 2637311fc65cSEric W. Biederman case TIOCGPTPEER: 2638311fc65cSEric W. Biederman /* Special because the struct file is needed */ 2639311fc65cSEric W. Biederman return ptm_open_peer(file, tty, (int)arg); 2640a1235b3eSNicolas Pitre default: 2641a1235b3eSNicolas Pitre retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); 2642a1235b3eSNicolas Pitre if (retval != -ENOIOCTLCMD) 2643a1235b3eSNicolas Pitre return retval; 264496fd7ce5SGreg Kroah-Hartman } 264596fd7ce5SGreg Kroah-Hartman if (tty->ops->ioctl) { 2646c961bfb1SPeter Hurley retval = tty->ops->ioctl(tty, cmd, arg); 264796fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 264896fd7ce5SGreg Kroah-Hartman return retval; 264996fd7ce5SGreg Kroah-Hartman } 265096fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2651e55afd11SPeter Hurley if (!ld) 2652e55afd11SPeter Hurley return hung_up_tty_ioctl(file, cmd, arg); 265396fd7ce5SGreg Kroah-Hartman retval = -EINVAL; 265496fd7ce5SGreg Kroah-Hartman if (ld->ops->ioctl) { 265596fd7ce5SGreg Kroah-Hartman retval = ld->ops->ioctl(tty, file, cmd, arg); 265696fd7ce5SGreg Kroah-Hartman if (retval == -ENOIOCTLCMD) 2657bbb63c51SWanlong Gao retval = -ENOTTY; 265896fd7ce5SGreg Kroah-Hartman } 265996fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 266096fd7ce5SGreg Kroah-Hartman return retval; 266196fd7ce5SGreg Kroah-Hartman } 266296fd7ce5SGreg Kroah-Hartman 266396fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT 266496fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd, 266596fd7ce5SGreg Kroah-Hartman unsigned long arg) 266696fd7ce5SGreg Kroah-Hartman { 266796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = file_tty(file); 266896fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 266996fd7ce5SGreg Kroah-Hartman int retval = -ENOIOCTLCMD; 267096fd7ce5SGreg Kroah-Hartman 26716131ffaaSAl Viro if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) 267296fd7ce5SGreg Kroah-Hartman return -EINVAL; 267396fd7ce5SGreg Kroah-Hartman 267496fd7ce5SGreg Kroah-Hartman if (tty->ops->compat_ioctl) { 2675c961bfb1SPeter Hurley retval = tty->ops->compat_ioctl(tty, cmd, arg); 267696fd7ce5SGreg Kroah-Hartman if (retval != -ENOIOCTLCMD) 267796fd7ce5SGreg Kroah-Hartman return retval; 267896fd7ce5SGreg Kroah-Hartman } 267996fd7ce5SGreg Kroah-Hartman 268096fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref_wait(tty); 2681e55afd11SPeter Hurley if (!ld) 2682e55afd11SPeter Hurley return hung_up_tty_compat_ioctl(file, cmd, arg); 268396fd7ce5SGreg Kroah-Hartman if (ld->ops->compat_ioctl) 268496fd7ce5SGreg Kroah-Hartman retval = ld->ops->compat_ioctl(tty, file, cmd, arg); 26858193c429SThomas Meyer else 26868193c429SThomas Meyer retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg); 268796fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 268896fd7ce5SGreg Kroah-Hartman 268996fd7ce5SGreg Kroah-Hartman return retval; 269096fd7ce5SGreg Kroah-Hartman } 269196fd7ce5SGreg Kroah-Hartman #endif 269296fd7ce5SGreg Kroah-Hartman 2693c3c073f8SAl Viro static int this_tty(const void *t, struct file *file, unsigned fd) 2694c3c073f8SAl Viro { 2695c3c073f8SAl Viro if (likely(file->f_op->read != tty_read)) 2696c3c073f8SAl Viro return 0; 2697c3c073f8SAl Viro return file_tty(file) != t ? 0 : fd + 1; 2698c3c073f8SAl Viro } 2699c3c073f8SAl Viro 270096fd7ce5SGreg Kroah-Hartman /* 270196fd7ce5SGreg Kroah-Hartman * This implements the "Secure Attention Key" --- the idea is to 270296fd7ce5SGreg Kroah-Hartman * prevent trojan horses by killing all processes associated with this 270396fd7ce5SGreg Kroah-Hartman * tty when the user hits the "Secure Attention Key". Required for 270496fd7ce5SGreg Kroah-Hartman * super-paranoid applications --- see the Orange Book for more details. 270596fd7ce5SGreg Kroah-Hartman * 270696fd7ce5SGreg Kroah-Hartman * This code could be nicer; ideally it should send a HUP, wait a few 270796fd7ce5SGreg Kroah-Hartman * seconds, then send a INT, and then a KILL signal. But you then 270896fd7ce5SGreg Kroah-Hartman * have to coordinate with the init process, since all processes associated 270996fd7ce5SGreg Kroah-Hartman * with the current tty must be dead before the new getty is allowed 271096fd7ce5SGreg Kroah-Hartman * to spawn. 271196fd7ce5SGreg Kroah-Hartman * 271296fd7ce5SGreg Kroah-Hartman * Now, if it would be correct ;-/ The current code has a nasty hole - 271396fd7ce5SGreg Kroah-Hartman * it doesn't catch files in flight. We may send the descriptor to ourselves 271496fd7ce5SGreg Kroah-Hartman * via AF_UNIX socket, close it and later fetch from socket. FIXME. 271596fd7ce5SGreg Kroah-Hartman * 271696fd7ce5SGreg Kroah-Hartman * Nasty bug: do_SAK is being called in interrupt context. This can 271796fd7ce5SGreg Kroah-Hartman * deadlock. We punt it up to process context. AKPM - 16Mar2001 271896fd7ce5SGreg Kroah-Hartman */ 271996fd7ce5SGreg Kroah-Hartman void __do_SAK(struct tty_struct *tty) 272096fd7ce5SGreg Kroah-Hartman { 272196fd7ce5SGreg Kroah-Hartman #ifdef TTY_SOFT_SAK 272296fd7ce5SGreg Kroah-Hartman tty_hangup(tty); 272396fd7ce5SGreg Kroah-Hartman #else 272496fd7ce5SGreg Kroah-Hartman struct task_struct *g, *p; 272596fd7ce5SGreg Kroah-Hartman struct pid *session; 272696fd7ce5SGreg Kroah-Hartman int i; 272796fd7ce5SGreg Kroah-Hartman 272896fd7ce5SGreg Kroah-Hartman if (!tty) 272996fd7ce5SGreg Kroah-Hartman return; 273096fd7ce5SGreg Kroah-Hartman session = tty->session; 273196fd7ce5SGreg Kroah-Hartman 273296fd7ce5SGreg Kroah-Hartman tty_ldisc_flush(tty); 273396fd7ce5SGreg Kroah-Hartman 273496fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 273596fd7ce5SGreg Kroah-Hartman 273696fd7ce5SGreg Kroah-Hartman read_lock(&tasklist_lock); 273796fd7ce5SGreg Kroah-Hartman /* Kill the entire session */ 273896fd7ce5SGreg Kroah-Hartman do_each_pid_task(session, PIDTYPE_SID, p) { 27399b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by session\n", 274096fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 274196fd7ce5SGreg Kroah-Hartman send_sig(SIGKILL, p, 1); 274296fd7ce5SGreg Kroah-Hartman } while_each_pid_task(session, PIDTYPE_SID, p); 27439b42bb75SPeter Hurley 27449b42bb75SPeter Hurley /* Now kill any processes that happen to have the tty open */ 274596fd7ce5SGreg Kroah-Hartman do_each_thread(g, p) { 274696fd7ce5SGreg Kroah-Hartman if (p->signal->tty == tty) { 27479b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", 274896fd7ce5SGreg Kroah-Hartman task_pid_nr(p), p->comm); 274996fd7ce5SGreg Kroah-Hartman send_sig(SIGKILL, p, 1); 275096fd7ce5SGreg Kroah-Hartman continue; 275196fd7ce5SGreg Kroah-Hartman } 275296fd7ce5SGreg Kroah-Hartman task_lock(p); 2753c3c073f8SAl Viro i = iterate_fd(p->files, 0, this_tty, tty); 2754c3c073f8SAl Viro if (i != 0) { 27559b42bb75SPeter Hurley tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", 2756c3c073f8SAl Viro task_pid_nr(p), p->comm, i - 1); 275796fd7ce5SGreg Kroah-Hartman force_sig(SIGKILL, p); 275896fd7ce5SGreg Kroah-Hartman } 275996fd7ce5SGreg Kroah-Hartman task_unlock(p); 276096fd7ce5SGreg Kroah-Hartman } while_each_thread(g, p); 276196fd7ce5SGreg Kroah-Hartman read_unlock(&tasklist_lock); 276296fd7ce5SGreg Kroah-Hartman #endif 276396fd7ce5SGreg Kroah-Hartman } 276496fd7ce5SGreg Kroah-Hartman 276596fd7ce5SGreg Kroah-Hartman static void do_SAK_work(struct work_struct *work) 276696fd7ce5SGreg Kroah-Hartman { 276796fd7ce5SGreg Kroah-Hartman struct tty_struct *tty = 276896fd7ce5SGreg Kroah-Hartman container_of(work, struct tty_struct, SAK_work); 276996fd7ce5SGreg Kroah-Hartman __do_SAK(tty); 277096fd7ce5SGreg Kroah-Hartman } 277196fd7ce5SGreg Kroah-Hartman 277296fd7ce5SGreg Kroah-Hartman /* 277396fd7ce5SGreg Kroah-Hartman * The tq handling here is a little racy - tty->SAK_work may already be queued. 277496fd7ce5SGreg Kroah-Hartman * Fortunately we don't need to worry, because if ->SAK_work is already queued, 277596fd7ce5SGreg Kroah-Hartman * the values which we write to it will be identical to the values which it 277696fd7ce5SGreg Kroah-Hartman * already has. --akpm 277796fd7ce5SGreg Kroah-Hartman */ 277896fd7ce5SGreg Kroah-Hartman void do_SAK(struct tty_struct *tty) 277996fd7ce5SGreg Kroah-Hartman { 278096fd7ce5SGreg Kroah-Hartman if (!tty) 278196fd7ce5SGreg Kroah-Hartman return; 278296fd7ce5SGreg Kroah-Hartman schedule_work(&tty->SAK_work); 278396fd7ce5SGreg Kroah-Hartman } 278496fd7ce5SGreg Kroah-Hartman 278596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(do_SAK); 278696fd7ce5SGreg Kroah-Hartman 27876e9430acSGreg Kroah-Hartman static int dev_match_devt(struct device *dev, const void *data) 278896fd7ce5SGreg Kroah-Hartman { 27896e9430acSGreg Kroah-Hartman const dev_t *devt = data; 279096fd7ce5SGreg Kroah-Hartman return dev->devt == *devt; 279196fd7ce5SGreg Kroah-Hartman } 279296fd7ce5SGreg Kroah-Hartman 279396fd7ce5SGreg Kroah-Hartman /* Must put_device() after it's unused! */ 279496fd7ce5SGreg Kroah-Hartman static struct device *tty_get_device(struct tty_struct *tty) 279596fd7ce5SGreg Kroah-Hartman { 279696fd7ce5SGreg Kroah-Hartman dev_t devt = tty_devnum(tty); 279796fd7ce5SGreg Kroah-Hartman return class_find_device(tty_class, NULL, &devt, dev_match_devt); 279896fd7ce5SGreg Kroah-Hartman } 279996fd7ce5SGreg Kroah-Hartman 280096fd7ce5SGreg Kroah-Hartman 280196fd7ce5SGreg Kroah-Hartman /** 28022c964a2fSRasmus Villemoes * alloc_tty_struct 280396fd7ce5SGreg Kroah-Hartman * 28042c964a2fSRasmus Villemoes * This subroutine allocates and initializes a tty structure. 280596fd7ce5SGreg Kroah-Hartman * 28062c964a2fSRasmus Villemoes * Locking: none - tty in question is not exposed at this point 280796fd7ce5SGreg Kroah-Hartman */ 280896fd7ce5SGreg Kroah-Hartman 28092c964a2fSRasmus Villemoes struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) 281096fd7ce5SGreg Kroah-Hartman { 28112c964a2fSRasmus Villemoes struct tty_struct *tty; 28122c964a2fSRasmus Villemoes 28132c964a2fSRasmus Villemoes tty = kzalloc(sizeof(*tty), GFP_KERNEL); 28142c964a2fSRasmus Villemoes if (!tty) 28152c964a2fSRasmus Villemoes return NULL; 28162c964a2fSRasmus Villemoes 281796fd7ce5SGreg Kroah-Hartman kref_init(&tty->kref); 281896fd7ce5SGreg Kroah-Hartman tty->magic = TTY_MAGIC; 281996fd7ce5SGreg Kroah-Hartman tty_ldisc_init(tty); 282096fd7ce5SGreg Kroah-Hartman tty->session = NULL; 282196fd7ce5SGreg Kroah-Hartman tty->pgrp = NULL; 282289c8d91eSAlan Cox mutex_init(&tty->legacy_mutex); 2823d8c1f929SPeter Hurley mutex_init(&tty->throttle_mutex); 28246a1c0680SPeter Hurley init_rwsem(&tty->termios_rwsem); 2825dee4a0beSPeter Hurley mutex_init(&tty->winsize_mutex); 282636697529SPeter Hurley init_ldsem(&tty->ldisc_sem); 282796fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->write_wait); 282896fd7ce5SGreg Kroah-Hartman init_waitqueue_head(&tty->read_wait); 282996fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->hangup_work, do_tty_hangup); 283096fd7ce5SGreg Kroah-Hartman mutex_init(&tty->atomic_write_lock); 283196fd7ce5SGreg Kroah-Hartman spin_lock_init(&tty->ctrl_lock); 2832f9e053dcSPeter Hurley spin_lock_init(&tty->flow_lock); 28334a510969SPeter Hurley spin_lock_init(&tty->files_lock); 283496fd7ce5SGreg Kroah-Hartman INIT_LIST_HEAD(&tty->tty_files); 283596fd7ce5SGreg Kroah-Hartman INIT_WORK(&tty->SAK_work, do_SAK_work); 283696fd7ce5SGreg Kroah-Hartman 283796fd7ce5SGreg Kroah-Hartman tty->driver = driver; 283896fd7ce5SGreg Kroah-Hartman tty->ops = driver->ops; 283996fd7ce5SGreg Kroah-Hartman tty->index = idx; 284096fd7ce5SGreg Kroah-Hartman tty_line_name(driver, idx, tty->name); 284196fd7ce5SGreg Kroah-Hartman tty->dev = tty_get_device(tty); 28422c964a2fSRasmus Villemoes 28432c964a2fSRasmus Villemoes return tty; 284496fd7ce5SGreg Kroah-Hartman } 284596fd7ce5SGreg Kroah-Hartman 284696fd7ce5SGreg Kroah-Hartman /** 284796fd7ce5SGreg Kroah-Hartman * tty_put_char - write one character to a tty 284896fd7ce5SGreg Kroah-Hartman * @tty: tty 284996fd7ce5SGreg Kroah-Hartman * @ch: character 285096fd7ce5SGreg Kroah-Hartman * 285196fd7ce5SGreg Kroah-Hartman * Write one byte to the tty using the provided put_char method 285296fd7ce5SGreg Kroah-Hartman * if present. Returns the number of characters successfully output. 285396fd7ce5SGreg Kroah-Hartman * 285496fd7ce5SGreg Kroah-Hartman * Note: the specific put_char operation in the driver layer may go 285596fd7ce5SGreg Kroah-Hartman * away soon. Don't call it directly, use this method 285696fd7ce5SGreg Kroah-Hartman */ 285796fd7ce5SGreg Kroah-Hartman 285896fd7ce5SGreg Kroah-Hartman int tty_put_char(struct tty_struct *tty, unsigned char ch) 285996fd7ce5SGreg Kroah-Hartman { 286096fd7ce5SGreg Kroah-Hartman if (tty->ops->put_char) 286196fd7ce5SGreg Kroah-Hartman return tty->ops->put_char(tty, ch); 286296fd7ce5SGreg Kroah-Hartman return tty->ops->write(tty, &ch, 1); 286396fd7ce5SGreg Kroah-Hartman } 286496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_put_char); 286596fd7ce5SGreg Kroah-Hartman 286696fd7ce5SGreg Kroah-Hartman struct class *tty_class; 286796fd7ce5SGreg Kroah-Hartman 28687e73eca6SJiri Slaby static int tty_cdev_add(struct tty_driver *driver, dev_t dev, 28697e73eca6SJiri Slaby unsigned int index, unsigned int count) 28707e73eca6SJiri Slaby { 2871c1a752baSLeon Yu int err; 2872c1a752baSLeon Yu 28737e73eca6SJiri Slaby /* init here, since reused cdevs cause crashes */ 2874a3a10ce3SRichard Watts driver->cdevs[index] = cdev_alloc(); 2875a3a10ce3SRichard Watts if (!driver->cdevs[index]) 2876a3a10ce3SRichard Watts return -ENOMEM; 2877c1a752baSLeon Yu driver->cdevs[index]->ops = &tty_fops; 2878a3a10ce3SRichard Watts driver->cdevs[index]->owner = driver->owner; 2879c1a752baSLeon Yu err = cdev_add(driver->cdevs[index], dev, count); 2880c1a752baSLeon Yu if (err) 2881c1a752baSLeon Yu kobject_put(&driver->cdevs[index]->kobj); 2882c1a752baSLeon Yu return err; 28837e73eca6SJiri Slaby } 28847e73eca6SJiri Slaby 288596fd7ce5SGreg Kroah-Hartman /** 288696fd7ce5SGreg Kroah-Hartman * tty_register_device - register a tty device 288796fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 288896fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 288996fd7ce5SGreg Kroah-Hartman * @device: a struct device that is associated with this tty device. 289096fd7ce5SGreg Kroah-Hartman * This field is optional, if there is no known struct device 289196fd7ce5SGreg Kroah-Hartman * for this tty device it can be set to NULL safely. 289296fd7ce5SGreg Kroah-Hartman * 289396fd7ce5SGreg Kroah-Hartman * Returns a pointer to the struct device for this tty device 289496fd7ce5SGreg Kroah-Hartman * (or ERR_PTR(-EFOO) on error). 289596fd7ce5SGreg Kroah-Hartman * 289696fd7ce5SGreg Kroah-Hartman * This call is required to be made to register an individual tty device 289796fd7ce5SGreg Kroah-Hartman * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 289896fd7ce5SGreg Kroah-Hartman * that bit is not set, this function should not be called by a tty 289996fd7ce5SGreg Kroah-Hartman * driver. 290096fd7ce5SGreg Kroah-Hartman * 290196fd7ce5SGreg Kroah-Hartman * Locking: ?? 290296fd7ce5SGreg Kroah-Hartman */ 290396fd7ce5SGreg Kroah-Hartman 290496fd7ce5SGreg Kroah-Hartman struct device *tty_register_device(struct tty_driver *driver, unsigned index, 290596fd7ce5SGreg Kroah-Hartman struct device *device) 290696fd7ce5SGreg Kroah-Hartman { 29076915c0e4STomas Hlavacek return tty_register_device_attr(driver, index, device, NULL, NULL); 29086915c0e4STomas Hlavacek } 29096915c0e4STomas Hlavacek EXPORT_SYMBOL(tty_register_device); 29106915c0e4STomas Hlavacek 2911b1b79916STomas Hlavacek static void tty_device_create_release(struct device *dev) 2912b1b79916STomas Hlavacek { 291383db1df4SPeter Hurley dev_dbg(dev, "releasing...\n"); 2914b1b79916STomas Hlavacek kfree(dev); 2915b1b79916STomas Hlavacek } 2916b1b79916STomas Hlavacek 29176915c0e4STomas Hlavacek /** 29186915c0e4STomas Hlavacek * tty_register_device_attr - register a tty device 29196915c0e4STomas Hlavacek * @driver: the tty driver that describes the tty device 29206915c0e4STomas Hlavacek * @index: the index in the tty driver for this tty device 29216915c0e4STomas Hlavacek * @device: a struct device that is associated with this tty device. 29226915c0e4STomas Hlavacek * This field is optional, if there is no known struct device 29236915c0e4STomas Hlavacek * for this tty device it can be set to NULL safely. 29246915c0e4STomas Hlavacek * @drvdata: Driver data to be set to device. 29256915c0e4STomas Hlavacek * @attr_grp: Attribute group to be set on device. 29266915c0e4STomas Hlavacek * 29276915c0e4STomas Hlavacek * Returns a pointer to the struct device for this tty device 29286915c0e4STomas Hlavacek * (or ERR_PTR(-EFOO) on error). 29296915c0e4STomas Hlavacek * 29306915c0e4STomas Hlavacek * This call is required to be made to register an individual tty device 29316915c0e4STomas Hlavacek * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 29326915c0e4STomas Hlavacek * that bit is not set, this function should not be called by a tty 29336915c0e4STomas Hlavacek * driver. 29346915c0e4STomas Hlavacek * 29356915c0e4STomas Hlavacek * Locking: ?? 29366915c0e4STomas Hlavacek */ 29376915c0e4STomas Hlavacek struct device *tty_register_device_attr(struct tty_driver *driver, 29386915c0e4STomas Hlavacek unsigned index, struct device *device, 29396915c0e4STomas Hlavacek void *drvdata, 29406915c0e4STomas Hlavacek const struct attribute_group **attr_grp) 29416915c0e4STomas Hlavacek { 294296fd7ce5SGreg Kroah-Hartman char name[64]; 29436915c0e4STomas Hlavacek dev_t devt = MKDEV(driver->major, driver->minor_start) + index; 294493857eddSJohan Hovold struct ktermios *tp; 29456a7e6f78SJohan Hovold struct device *dev; 29466a7e6f78SJohan Hovold int retval; 294796fd7ce5SGreg Kroah-Hartman 294896fd7ce5SGreg Kroah-Hartman if (index >= driver->num) { 2949656fb867SPeter Hurley pr_err("%s: Attempt to register invalid tty line number (%d)\n", 2950656fb867SPeter Hurley driver->name, index); 295196fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EINVAL); 295296fd7ce5SGreg Kroah-Hartman } 295396fd7ce5SGreg Kroah-Hartman 295496fd7ce5SGreg Kroah-Hartman if (driver->type == TTY_DRIVER_TYPE_PTY) 295596fd7ce5SGreg Kroah-Hartman pty_line_name(driver, index, name); 295696fd7ce5SGreg Kroah-Hartman else 295796fd7ce5SGreg Kroah-Hartman tty_line_name(driver, index, name); 295896fd7ce5SGreg Kroah-Hartman 29596915c0e4STomas Hlavacek dev = kzalloc(sizeof(*dev), GFP_KERNEL); 29606a7e6f78SJohan Hovold if (!dev) 29616a7e6f78SJohan Hovold return ERR_PTR(-ENOMEM); 29626915c0e4STomas Hlavacek 29636915c0e4STomas Hlavacek dev->devt = devt; 29646915c0e4STomas Hlavacek dev->class = tty_class; 29656915c0e4STomas Hlavacek dev->parent = device; 2966b1b79916STomas Hlavacek dev->release = tty_device_create_release; 29676915c0e4STomas Hlavacek dev_set_name(dev, "%s", name); 29686915c0e4STomas Hlavacek dev->groups = attr_grp; 29696915c0e4STomas Hlavacek dev_set_drvdata(dev, drvdata); 29706915c0e4STomas Hlavacek 29716a7e6f78SJohan Hovold dev_set_uevent_suppress(dev, 1); 29726a7e6f78SJohan Hovold 29736915c0e4STomas Hlavacek retval = device_register(dev); 29746915c0e4STomas Hlavacek if (retval) 29756a7e6f78SJohan Hovold goto err_put; 29766a7e6f78SJohan Hovold 29776a7e6f78SJohan Hovold if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 297893857eddSJohan Hovold /* 297993857eddSJohan Hovold * Free any saved termios data so that the termios state is 298093857eddSJohan Hovold * reset when reusing a minor number. 298193857eddSJohan Hovold */ 298293857eddSJohan Hovold tp = driver->termios[index]; 298393857eddSJohan Hovold if (tp) { 298493857eddSJohan Hovold driver->termios[index] = NULL; 298593857eddSJohan Hovold kfree(tp); 298693857eddSJohan Hovold } 298793857eddSJohan Hovold 29886a7e6f78SJohan Hovold retval = tty_cdev_add(driver, devt, index, 1); 29896a7e6f78SJohan Hovold if (retval) 29906a7e6f78SJohan Hovold goto err_del; 29916a7e6f78SJohan Hovold } 29926a7e6f78SJohan Hovold 29936a7e6f78SJohan Hovold dev_set_uevent_suppress(dev, 0); 29946a7e6f78SJohan Hovold kobject_uevent(&dev->kobj, KOBJ_ADD); 29956915c0e4STomas Hlavacek 29966915c0e4STomas Hlavacek return dev; 29976915c0e4STomas Hlavacek 29986a7e6f78SJohan Hovold err_del: 29996a7e6f78SJohan Hovold device_del(dev); 30006a7e6f78SJohan Hovold err_put: 30016915c0e4STomas Hlavacek put_device(dev); 30026a7e6f78SJohan Hovold 30036915c0e4STomas Hlavacek return ERR_PTR(retval); 30046915c0e4STomas Hlavacek } 30056915c0e4STomas Hlavacek EXPORT_SYMBOL_GPL(tty_register_device_attr); 300696fd7ce5SGreg Kroah-Hartman 300796fd7ce5SGreg Kroah-Hartman /** 300896fd7ce5SGreg Kroah-Hartman * tty_unregister_device - unregister a tty device 300996fd7ce5SGreg Kroah-Hartman * @driver: the tty driver that describes the tty device 301096fd7ce5SGreg Kroah-Hartman * @index: the index in the tty driver for this tty device 301196fd7ce5SGreg Kroah-Hartman * 301296fd7ce5SGreg Kroah-Hartman * If a tty device is registered with a call to tty_register_device() then 301396fd7ce5SGreg Kroah-Hartman * this function must be called when the tty device is gone. 301496fd7ce5SGreg Kroah-Hartman * 301596fd7ce5SGreg Kroah-Hartman * Locking: ?? 301696fd7ce5SGreg Kroah-Hartman */ 301796fd7ce5SGreg Kroah-Hartman 301896fd7ce5SGreg Kroah-Hartman void tty_unregister_device(struct tty_driver *driver, unsigned index) 301996fd7ce5SGreg Kroah-Hartman { 302096fd7ce5SGreg Kroah-Hartman device_destroy(tty_class, 302196fd7ce5SGreg Kroah-Hartman MKDEV(driver->major, driver->minor_start) + index); 3022a3a10ce3SRichard Watts if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 3023a3a10ce3SRichard Watts cdev_del(driver->cdevs[index]); 3024a3a10ce3SRichard Watts driver->cdevs[index] = NULL; 3025a3a10ce3SRichard Watts } 302696fd7ce5SGreg Kroah-Hartman } 302796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_device); 302896fd7ce5SGreg Kroah-Hartman 30297f0bc6a6SJiri Slaby /** 30307f0bc6a6SJiri Slaby * __tty_alloc_driver -- allocate tty driver 30317f0bc6a6SJiri Slaby * @lines: count of lines this driver can handle at most 303287838ae3SThadeu Lima de Souza Cascardo * @owner: module which is responsible for this driver 30337f0bc6a6SJiri Slaby * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags 30347f0bc6a6SJiri Slaby * 30357f0bc6a6SJiri Slaby * This should not be called directly, some of the provided macros should be 30367f0bc6a6SJiri Slaby * used instead. Use IS_ERR and friends on @retval. 30377f0bc6a6SJiri Slaby */ 30387f0bc6a6SJiri Slaby struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner, 30397f0bc6a6SJiri Slaby unsigned long flags) 304096fd7ce5SGreg Kroah-Hartman { 304196fd7ce5SGreg Kroah-Hartman struct tty_driver *driver; 30427e73eca6SJiri Slaby unsigned int cdevs = 1; 304316a02081SJiri Slaby int err; 304496fd7ce5SGreg Kroah-Hartman 30450019b408SJiri Slaby if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1)) 30467f0bc6a6SJiri Slaby return ERR_PTR(-EINVAL); 30477f0bc6a6SJiri Slaby 304896fd7ce5SGreg Kroah-Hartman driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); 30497f0bc6a6SJiri Slaby if (!driver) 30507f0bc6a6SJiri Slaby return ERR_PTR(-ENOMEM); 30517f0bc6a6SJiri Slaby 305296fd7ce5SGreg Kroah-Hartman kref_init(&driver->kref); 305396fd7ce5SGreg Kroah-Hartman driver->magic = TTY_DRIVER_MAGIC; 305496fd7ce5SGreg Kroah-Hartman driver->num = lines; 30551a54a76dSJiri Slaby driver->owner = owner; 30567f0bc6a6SJiri Slaby driver->flags = flags; 305716a02081SJiri Slaby 305816a02081SJiri Slaby if (!(flags & TTY_DRIVER_DEVPTS_MEM)) { 305916a02081SJiri Slaby driver->ttys = kcalloc(lines, sizeof(*driver->ttys), 306016a02081SJiri Slaby GFP_KERNEL); 306116a02081SJiri Slaby driver->termios = kcalloc(lines, sizeof(*driver->termios), 306216a02081SJiri Slaby GFP_KERNEL); 306316a02081SJiri Slaby if (!driver->ttys || !driver->termios) { 306416a02081SJiri Slaby err = -ENOMEM; 306516a02081SJiri Slaby goto err_free_all; 306616a02081SJiri Slaby } 306716a02081SJiri Slaby } 306816a02081SJiri Slaby 306916a02081SJiri Slaby if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) { 307016a02081SJiri Slaby driver->ports = kcalloc(lines, sizeof(*driver->ports), 307116a02081SJiri Slaby GFP_KERNEL); 307216a02081SJiri Slaby if (!driver->ports) { 307316a02081SJiri Slaby err = -ENOMEM; 307416a02081SJiri Slaby goto err_free_all; 307516a02081SJiri Slaby } 30767e73eca6SJiri Slaby cdevs = lines; 30777e73eca6SJiri Slaby } 30787e73eca6SJiri Slaby 30797e73eca6SJiri Slaby driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL); 30807e73eca6SJiri Slaby if (!driver->cdevs) { 30817e73eca6SJiri Slaby err = -ENOMEM; 30827e73eca6SJiri Slaby goto err_free_all; 308316a02081SJiri Slaby } 30847f0bc6a6SJiri Slaby 308596fd7ce5SGreg Kroah-Hartman return driver; 308616a02081SJiri Slaby err_free_all: 308716a02081SJiri Slaby kfree(driver->ports); 308816a02081SJiri Slaby kfree(driver->ttys); 308916a02081SJiri Slaby kfree(driver->termios); 3090a3a10ce3SRichard Watts kfree(driver->cdevs); 309116a02081SJiri Slaby kfree(driver); 309216a02081SJiri Slaby return ERR_PTR(err); 309396fd7ce5SGreg Kroah-Hartman } 30947f0bc6a6SJiri Slaby EXPORT_SYMBOL(__tty_alloc_driver); 309596fd7ce5SGreg Kroah-Hartman 309696fd7ce5SGreg Kroah-Hartman static void destruct_tty_driver(struct kref *kref) 309796fd7ce5SGreg Kroah-Hartman { 309896fd7ce5SGreg Kroah-Hartman struct tty_driver *driver = container_of(kref, struct tty_driver, kref); 309996fd7ce5SGreg Kroah-Hartman int i; 310096fd7ce5SGreg Kroah-Hartman struct ktermios *tp; 310196fd7ce5SGreg Kroah-Hartman 310296fd7ce5SGreg Kroah-Hartman if (driver->flags & TTY_DRIVER_INSTALLED) { 310396fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 310496fd7ce5SGreg Kroah-Hartman tp = driver->termios[i]; 310596fd7ce5SGreg Kroah-Hartman if (tp) { 310696fd7ce5SGreg Kroah-Hartman driver->termios[i] = NULL; 310796fd7ce5SGreg Kroah-Hartman kfree(tp); 310896fd7ce5SGreg Kroah-Hartman } 310996fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) 311096fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 311196fd7ce5SGreg Kroah-Hartman } 311296fd7ce5SGreg Kroah-Hartman proc_tty_unregister_driver(driver); 31137e73eca6SJiri Slaby if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) 3114a3a10ce3SRichard Watts cdev_del(driver->cdevs[0]); 311596fd7ce5SGreg Kroah-Hartman } 31167e73eca6SJiri Slaby kfree(driver->cdevs); 311704831dc1SJiri Slaby kfree(driver->ports); 311816a02081SJiri Slaby kfree(driver->termios); 311916a02081SJiri Slaby kfree(driver->ttys); 312096fd7ce5SGreg Kroah-Hartman kfree(driver); 312196fd7ce5SGreg Kroah-Hartman } 312296fd7ce5SGreg Kroah-Hartman 312396fd7ce5SGreg Kroah-Hartman void tty_driver_kref_put(struct tty_driver *driver) 312496fd7ce5SGreg Kroah-Hartman { 312596fd7ce5SGreg Kroah-Hartman kref_put(&driver->kref, destruct_tty_driver); 312696fd7ce5SGreg Kroah-Hartman } 312796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_driver_kref_put); 312896fd7ce5SGreg Kroah-Hartman 312996fd7ce5SGreg Kroah-Hartman void tty_set_operations(struct tty_driver *driver, 313096fd7ce5SGreg Kroah-Hartman const struct tty_operations *op) 313196fd7ce5SGreg Kroah-Hartman { 313296fd7ce5SGreg Kroah-Hartman driver->ops = op; 313396fd7ce5SGreg Kroah-Hartman }; 313496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_set_operations); 313596fd7ce5SGreg Kroah-Hartman 313696fd7ce5SGreg Kroah-Hartman void put_tty_driver(struct tty_driver *d) 313796fd7ce5SGreg Kroah-Hartman { 313896fd7ce5SGreg Kroah-Hartman tty_driver_kref_put(d); 313996fd7ce5SGreg Kroah-Hartman } 314096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(put_tty_driver); 314196fd7ce5SGreg Kroah-Hartman 314296fd7ce5SGreg Kroah-Hartman /* 314396fd7ce5SGreg Kroah-Hartman * Called by a tty driver to register itself. 314496fd7ce5SGreg Kroah-Hartman */ 314596fd7ce5SGreg Kroah-Hartman int tty_register_driver(struct tty_driver *driver) 314696fd7ce5SGreg Kroah-Hartman { 314796fd7ce5SGreg Kroah-Hartman int error; 314896fd7ce5SGreg Kroah-Hartman int i; 314996fd7ce5SGreg Kroah-Hartman dev_t dev; 315096fd7ce5SGreg Kroah-Hartman struct device *d; 315196fd7ce5SGreg Kroah-Hartman 315296fd7ce5SGreg Kroah-Hartman if (!driver->major) { 315396fd7ce5SGreg Kroah-Hartman error = alloc_chrdev_region(&dev, driver->minor_start, 315496fd7ce5SGreg Kroah-Hartman driver->num, driver->name); 315596fd7ce5SGreg Kroah-Hartman if (!error) { 315696fd7ce5SGreg Kroah-Hartman driver->major = MAJOR(dev); 315796fd7ce5SGreg Kroah-Hartman driver->minor_start = MINOR(dev); 315896fd7ce5SGreg Kroah-Hartman } 315996fd7ce5SGreg Kroah-Hartman } else { 316096fd7ce5SGreg Kroah-Hartman dev = MKDEV(driver->major, driver->minor_start); 316196fd7ce5SGreg Kroah-Hartman error = register_chrdev_region(dev, driver->num, driver->name); 316296fd7ce5SGreg Kroah-Hartman } 31639bb8a3d4SJiri Slaby if (error < 0) 316416a02081SJiri Slaby goto err; 316596fd7ce5SGreg Kroah-Hartman 31667e73eca6SJiri Slaby if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) { 31677e73eca6SJiri Slaby error = tty_cdev_add(driver, dev, 0, driver->num); 31689bb8a3d4SJiri Slaby if (error) 31699bb8a3d4SJiri Slaby goto err_unreg_char; 31707e73eca6SJiri Slaby } 317196fd7ce5SGreg Kroah-Hartman 317296fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 317396fd7ce5SGreg Kroah-Hartman list_add(&driver->tty_drivers, &tty_drivers); 317496fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 317596fd7ce5SGreg Kroah-Hartman 317696fd7ce5SGreg Kroah-Hartman if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { 317796fd7ce5SGreg Kroah-Hartman for (i = 0; i < driver->num; i++) { 317896fd7ce5SGreg Kroah-Hartman d = tty_register_device(driver, i, NULL); 317996fd7ce5SGreg Kroah-Hartman if (IS_ERR(d)) { 318096fd7ce5SGreg Kroah-Hartman error = PTR_ERR(d); 318116a02081SJiri Slaby goto err_unreg_devs; 318296fd7ce5SGreg Kroah-Hartman } 318396fd7ce5SGreg Kroah-Hartman } 318496fd7ce5SGreg Kroah-Hartman } 318596fd7ce5SGreg Kroah-Hartman proc_tty_register_driver(driver); 318696fd7ce5SGreg Kroah-Hartman driver->flags |= TTY_DRIVER_INSTALLED; 318796fd7ce5SGreg Kroah-Hartman return 0; 318896fd7ce5SGreg Kroah-Hartman 318916a02081SJiri Slaby err_unreg_devs: 319096fd7ce5SGreg Kroah-Hartman for (i--; i >= 0; i--) 319196fd7ce5SGreg Kroah-Hartman tty_unregister_device(driver, i); 319296fd7ce5SGreg Kroah-Hartman 319396fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 319496fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 319596fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 319696fd7ce5SGreg Kroah-Hartman 31979bb8a3d4SJiri Slaby err_unreg_char: 319896fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(dev, driver->num); 319916a02081SJiri Slaby err: 320096fd7ce5SGreg Kroah-Hartman return error; 320196fd7ce5SGreg Kroah-Hartman } 320296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_driver); 320396fd7ce5SGreg Kroah-Hartman 320496fd7ce5SGreg Kroah-Hartman /* 320596fd7ce5SGreg Kroah-Hartman * Called by a tty driver to unregister itself. 320696fd7ce5SGreg Kroah-Hartman */ 320796fd7ce5SGreg Kroah-Hartman int tty_unregister_driver(struct tty_driver *driver) 320896fd7ce5SGreg Kroah-Hartman { 320996fd7ce5SGreg Kroah-Hartman #if 0 321096fd7ce5SGreg Kroah-Hartman /* FIXME */ 321196fd7ce5SGreg Kroah-Hartman if (driver->refcount) 321296fd7ce5SGreg Kroah-Hartman return -EBUSY; 321396fd7ce5SGreg Kroah-Hartman #endif 321496fd7ce5SGreg Kroah-Hartman unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), 321596fd7ce5SGreg Kroah-Hartman driver->num); 321696fd7ce5SGreg Kroah-Hartman mutex_lock(&tty_mutex); 321796fd7ce5SGreg Kroah-Hartman list_del(&driver->tty_drivers); 321896fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty_mutex); 321996fd7ce5SGreg Kroah-Hartman return 0; 322096fd7ce5SGreg Kroah-Hartman } 322196fd7ce5SGreg Kroah-Hartman 322296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_driver); 322396fd7ce5SGreg Kroah-Hartman 322496fd7ce5SGreg Kroah-Hartman dev_t tty_devnum(struct tty_struct *tty) 322596fd7ce5SGreg Kroah-Hartman { 322696fd7ce5SGreg Kroah-Hartman return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; 322796fd7ce5SGreg Kroah-Hartman } 322896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_devnum); 322996fd7ce5SGreg Kroah-Hartman 323096fd7ce5SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops) 323196fd7ce5SGreg Kroah-Hartman { 323296fd7ce5SGreg Kroah-Hartman *fops = tty_fops; 323396fd7ce5SGreg Kroah-Hartman } 323496fd7ce5SGreg Kroah-Hartman 32352c9ede55SAl Viro static char *tty_devnode(struct device *dev, umode_t *mode) 323696fd7ce5SGreg Kroah-Hartman { 323796fd7ce5SGreg Kroah-Hartman if (!mode) 323896fd7ce5SGreg Kroah-Hartman return NULL; 323996fd7ce5SGreg Kroah-Hartman if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || 324096fd7ce5SGreg Kroah-Hartman dev->devt == MKDEV(TTYAUX_MAJOR, 2)) 324196fd7ce5SGreg Kroah-Hartman *mode = 0666; 324296fd7ce5SGreg Kroah-Hartman return NULL; 324396fd7ce5SGreg Kroah-Hartman } 324496fd7ce5SGreg Kroah-Hartman 324596fd7ce5SGreg Kroah-Hartman static int __init tty_class_init(void) 324696fd7ce5SGreg Kroah-Hartman { 324796fd7ce5SGreg Kroah-Hartman tty_class = class_create(THIS_MODULE, "tty"); 324896fd7ce5SGreg Kroah-Hartman if (IS_ERR(tty_class)) 324996fd7ce5SGreg Kroah-Hartman return PTR_ERR(tty_class); 325096fd7ce5SGreg Kroah-Hartman tty_class->devnode = tty_devnode; 325196fd7ce5SGreg Kroah-Hartman return 0; 325296fd7ce5SGreg Kroah-Hartman } 325396fd7ce5SGreg Kroah-Hartman 325496fd7ce5SGreg Kroah-Hartman postcore_initcall(tty_class_init); 325596fd7ce5SGreg Kroah-Hartman 325696fd7ce5SGreg Kroah-Hartman /* 3/2004 jmc: why do these devices exist? */ 325796fd7ce5SGreg Kroah-Hartman static struct cdev tty_cdev, console_cdev; 325896fd7ce5SGreg Kroah-Hartman 3259fbc92a34SKay Sievers static ssize_t show_cons_active(struct device *dev, 3260fbc92a34SKay Sievers struct device_attribute *attr, char *buf) 3261fbc92a34SKay Sievers { 3262fbc92a34SKay Sievers struct console *cs[16]; 3263fbc92a34SKay Sievers int i = 0; 3264fbc92a34SKay Sievers struct console *c; 3265fbc92a34SKay Sievers ssize_t count = 0; 3266fbc92a34SKay Sievers 3267ac751efaSTorben Hohn console_lock(); 3268a2a6a822SKay Sievers for_each_console(c) { 3269fbc92a34SKay Sievers if (!c->device) 3270fbc92a34SKay Sievers continue; 3271fbc92a34SKay Sievers if (!c->write) 3272fbc92a34SKay Sievers continue; 3273fbc92a34SKay Sievers if ((c->flags & CON_ENABLED) == 0) 3274fbc92a34SKay Sievers continue; 3275fbc92a34SKay Sievers cs[i++] = c; 3276fbc92a34SKay Sievers if (i >= ARRAY_SIZE(cs)) 3277fbc92a34SKay Sievers break; 3278fbc92a34SKay Sievers } 3279723abd87SHannes Reinecke while (i--) { 3280723abd87SHannes Reinecke int index = cs[i]->index; 3281723abd87SHannes Reinecke struct tty_driver *drv = cs[i]->device(cs[i], &index); 3282723abd87SHannes Reinecke 3283723abd87SHannes Reinecke /* don't resolve tty0 as some programs depend on it */ 3284723abd87SHannes Reinecke if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR)) 3285723abd87SHannes Reinecke count += tty_line_name(drv, index, buf + count); 3286723abd87SHannes Reinecke else 3287723abd87SHannes Reinecke count += sprintf(buf + count, "%s%d", 3288723abd87SHannes Reinecke cs[i]->name, cs[i]->index); 3289723abd87SHannes Reinecke 3290723abd87SHannes Reinecke count += sprintf(buf + count, "%c", i ? ' ':'\n'); 3291723abd87SHannes Reinecke } 3292ac751efaSTorben Hohn console_unlock(); 3293fbc92a34SKay Sievers 3294fbc92a34SKay Sievers return count; 3295fbc92a34SKay Sievers } 3296fbc92a34SKay Sievers static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL); 3297fbc92a34SKay Sievers 32981083a7beSTakashi Iwai static struct attribute *cons_dev_attrs[] = { 32991083a7beSTakashi Iwai &dev_attr_active.attr, 33001083a7beSTakashi Iwai NULL 33011083a7beSTakashi Iwai }; 33021083a7beSTakashi Iwai 33031083a7beSTakashi Iwai ATTRIBUTE_GROUPS(cons_dev); 33041083a7beSTakashi Iwai 3305fbc92a34SKay Sievers static struct device *consdev; 3306fbc92a34SKay Sievers 3307fbc92a34SKay Sievers void console_sysfs_notify(void) 3308fbc92a34SKay Sievers { 3309fbc92a34SKay Sievers if (consdev) 3310fbc92a34SKay Sievers sysfs_notify(&consdev->kobj, NULL, "active"); 3311fbc92a34SKay Sievers } 3312fbc92a34SKay Sievers 331396fd7ce5SGreg Kroah-Hartman /* 331496fd7ce5SGreg Kroah-Hartman * Ok, now we can initialize the rest of the tty devices and can count 331596fd7ce5SGreg Kroah-Hartman * on memory allocations, interrupts etc.. 331696fd7ce5SGreg Kroah-Hartman */ 331796fd7ce5SGreg Kroah-Hartman int __init tty_init(void) 331896fd7ce5SGreg Kroah-Hartman { 331996fd7ce5SGreg Kroah-Hartman cdev_init(&tty_cdev, &tty_fops); 332096fd7ce5SGreg Kroah-Hartman if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || 332196fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) 332296fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/tty driver\n"); 3323fbc92a34SKay Sievers device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); 332496fd7ce5SGreg Kroah-Hartman 332596fd7ce5SGreg Kroah-Hartman cdev_init(&console_cdev, &console_fops); 332696fd7ce5SGreg Kroah-Hartman if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || 332796fd7ce5SGreg Kroah-Hartman register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) 332896fd7ce5SGreg Kroah-Hartman panic("Couldn't register /dev/console driver\n"); 33291083a7beSTakashi Iwai consdev = device_create_with_groups(tty_class, NULL, 33301083a7beSTakashi Iwai MKDEV(TTYAUX_MAJOR, 1), NULL, 33311083a7beSTakashi Iwai cons_dev_groups, "console"); 3332fbc92a34SKay Sievers if (IS_ERR(consdev)) 3333fbc92a34SKay Sievers consdev = NULL; 333496fd7ce5SGreg Kroah-Hartman 333596fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT 333696fd7ce5SGreg Kroah-Hartman vty_init(&console_fops); 333796fd7ce5SGreg Kroah-Hartman #endif 333896fd7ce5SGreg Kroah-Hartman return 0; 333996fd7ce5SGreg Kroah-Hartman } 334096fd7ce5SGreg Kroah-Hartman 3341