Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/tty_io.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles |
| 9 | * or rs-channels. It also implements echoing, cooked mode etc. |
| 10 | * |
| 11 | * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0. |
| 12 | * |
| 13 | * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the |
| 14 | * tty_struct and tty_queue structures. Previously there was an array |
| 15 | * of 256 tty_struct's which was statically allocated, and the |
| 16 | * tty_queue structures were allocated at boot time. Both are now |
| 17 | * dynamically allocated only when the tty is open. |
| 18 | * |
| 19 | * Also restructured routines so that there is more of a separation |
| 20 | * between the high-level tty routines (tty_io.c and tty_ioctl.c) and |
| 21 | * the low-level tty routines (serial.c, pty.c, console.c). This |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 22 | * makes for cleaner and more compact code. -TYT, 9/17/92 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
| 24 | * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines |
| 25 | * which can be dynamically activated and de-activated by the line |
| 26 | * discipline handling modules (like SLIP). |
| 27 | * |
| 28 | * NOTE: pay no attention to the line discipline code (yet); its |
| 29 | * interface is still subject to change in this version... |
| 30 | * -- TYT, 1/31/92 |
| 31 | * |
| 32 | * Added functionality to the OPOST tty handling. No delays, but all |
| 33 | * other bits should be there. |
| 34 | * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993. |
| 35 | * |
| 36 | * Rewrote canonical mode and added more termios flags. |
| 37 | * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94 |
| 38 | * |
| 39 | * Reorganized FASYNC support so mouse code can share it. |
| 40 | * -- ctm@ardi.com, 9Sep95 |
| 41 | * |
| 42 | * New TIOCLINUX variants added. |
| 43 | * -- mj@k332.feld.cvut.cz, 19-Nov-95 |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 44 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * Restrict vt switching via ioctl() |
| 46 | * -- grif@cs.ucr.edu, 5-Dec-95 |
| 47 | * |
| 48 | * Move console and virtual terminal code to more appropriate files, |
| 49 | * implement CONFIG_VT and generalize console device interface. |
| 50 | * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 |
| 51 | * |
| 52 | * Rewrote init_dev and release_dev to eliminate races. |
| 53 | * -- Bill Hawes <whawes@star.net>, June 97 |
| 54 | * |
| 55 | * Added devfs support. |
| 56 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998 |
| 57 | * |
| 58 | * Added support for a Unix98-style ptmx device. |
| 59 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 |
| 60 | * |
| 61 | * Reduced memory usage for older ARM systems |
| 62 | * -- Russell King <rmk@arm.linux.org.uk> |
| 63 | * |
| 64 | * Move do_SAK() into process context. Less stack use in devfs functions. |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 65 | * alloc_tty_struct() always uses kmalloc() |
| 66 | * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | */ |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #include <linux/types.h> |
| 70 | #include <linux/major.h> |
| 71 | #include <linux/errno.h> |
| 72 | #include <linux/signal.h> |
| 73 | #include <linux/fcntl.h> |
| 74 | #include <linux/sched.h> |
| 75 | #include <linux/interrupt.h> |
| 76 | #include <linux/tty.h> |
| 77 | #include <linux/tty_driver.h> |
| 78 | #include <linux/tty_flip.h> |
| 79 | #include <linux/devpts_fs.h> |
| 80 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 81 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #include <linux/console.h> |
| 83 | #include <linux/timer.h> |
| 84 | #include <linux/ctype.h> |
| 85 | #include <linux/kd.h> |
| 86 | #include <linux/mm.h> |
| 87 | #include <linux/string.h> |
| 88 | #include <linux/slab.h> |
| 89 | #include <linux/poll.h> |
| 90 | #include <linux/proc_fs.h> |
| 91 | #include <linux/init.h> |
| 92 | #include <linux/module.h> |
| 93 | #include <linux/smp_lock.h> |
| 94 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | #include <linux/wait.h> |
| 96 | #include <linux/bitops.h> |
Domen Puncer | b20f3ae | 2005-06-25 14:58:42 -0700 | [diff] [blame] | 97 | #include <linux/delay.h> |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 98 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 100 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | #include <asm/system.h> |
| 102 | |
| 103 | #include <linux/kbd_kern.h> |
| 104 | #include <linux/vt_kern.h> |
| 105 | #include <linux/selection.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | #include <linux/kmod.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 108 | #include <linux/nsproxy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | #undef TTY_DEBUG_HANGUP |
| 111 | |
| 112 | #define TTY_PARANOIA_CHECK 1 |
| 113 | #define CHECK_TTY_COUNT 1 |
| 114 | |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 115 | struct ktermios tty_std_termios = { /* for the benefit of tty drivers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | .c_iflag = ICRNL | IXON, |
| 117 | .c_oflag = OPOST | ONLCR, |
| 118 | .c_cflag = B38400 | CS8 | CREAD | HUPCL, |
| 119 | .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | |
| 120 | ECHOCTL | ECHOKE | IEXTEN, |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 121 | .c_cc = INIT_C_CC, |
| 122 | .c_ispeed = 38400, |
| 123 | .c_ospeed = 38400 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | EXPORT_SYMBOL(tty_std_termios); |
| 127 | |
| 128 | /* This list gets poked at by procfs and various bits of boot up code. This |
| 129 | could do with some rationalisation such as pulling the tty proc function |
| 130 | into this file */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | LIST_HEAD(tty_drivers); /* linked list of tty drivers */ |
| 133 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 134 | /* Mutex to protect creating and releasing a tty. This is shared with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | vt.c for deeply disgusting hack reasons */ |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 136 | DEFINE_MUTEX(tty_mutex); |
Alan Cox | de2a84f | 2006-09-29 02:00:57 -0700 | [diff] [blame] | 137 | EXPORT_SYMBOL(tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | #ifdef CONFIG_UNIX98_PTYS |
| 140 | extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | static int ptmx_open(struct inode *, struct file *); |
| 142 | #endif |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | static void initialize_tty_struct(struct tty_struct *tty); |
| 145 | |
| 146 | static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); |
| 147 | static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 148 | ssize_t redirected_tty_write(struct file *, const char __user *, |
| 149 | size_t, loff_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | static unsigned int tty_poll(struct file *, poll_table *); |
| 151 | static int tty_open(struct inode *, struct file *); |
| 152 | static int tty_release(struct inode *, struct file *); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 153 | long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 154 | #ifdef CONFIG_COMPAT |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 155 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 156 | unsigned long arg); |
| 157 | #else |
| 158 | #define tty_compat_ioctl NULL |
| 159 | #endif |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 160 | static int tty_fasync(int fd, struct file *filp, int on); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 161 | static void release_tty(struct tty_struct *tty, int idx); |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 162 | static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 163 | static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 165 | /** |
| 166 | * alloc_tty_struct - allocate a tty object |
| 167 | * |
| 168 | * Return a new empty tty structure. The data fields have not |
| 169 | * been initialized in any way but has been zeroed |
| 170 | * |
| 171 | * Locking: none |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 172 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | static struct tty_struct *alloc_tty_struct(void) |
| 175 | { |
Alan Cox | 1266b1e | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 176 | return kzalloc(sizeof(struct tty_struct), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 179 | static void tty_buffer_free_all(struct tty_struct *); |
| 180 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 181 | /** |
| 182 | * free_tty_struct - free a disused tty |
| 183 | * @tty: tty struct to free |
| 184 | * |
| 185 | * Free the write buffers, tty queue and tty memory itself. |
| 186 | * |
| 187 | * Locking: none. Must be called after tty is definitely unused |
| 188 | */ |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | static inline void free_tty_struct(struct tty_struct *tty) |
| 191 | { |
| 192 | kfree(tty->write_buf); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 193 | tty_buffer_free_all(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | kfree(tty); |
| 195 | } |
| 196 | |
| 197 | #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) |
| 198 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 199 | /** |
| 200 | * tty_name - return tty naming |
| 201 | * @tty: tty structure |
| 202 | * @buf: buffer for output |
| 203 | * |
| 204 | * Convert a tty structure into a name. The name reflects the kernel |
| 205 | * naming policy and if udev is in use may not reflect user space |
| 206 | * |
| 207 | * Locking: none |
| 208 | */ |
| 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | char *tty_name(struct tty_struct *tty, char *buf) |
| 211 | { |
| 212 | if (!tty) /* Hmm. NULL pointer. That's fun. */ |
| 213 | strcpy(buf, "NULL tty"); |
| 214 | else |
| 215 | strcpy(buf, tty->name); |
| 216 | return buf; |
| 217 | } |
| 218 | |
| 219 | EXPORT_SYMBOL(tty_name); |
| 220 | |
Andrew Morton | d769a66 | 2005-05-05 16:15:50 -0700 | [diff] [blame] | 221 | int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | const char *routine) |
| 223 | { |
| 224 | #ifdef TTY_PARANOIA_CHECK |
| 225 | if (!tty) { |
| 226 | printk(KERN_WARNING |
| 227 | "null TTY for (%d:%d) in %s\n", |
| 228 | imajor(inode), iminor(inode), routine); |
| 229 | return 1; |
| 230 | } |
| 231 | if (tty->magic != TTY_MAGIC) { |
| 232 | printk(KERN_WARNING |
| 233 | "bad magic number for tty struct (%d:%d) in %s\n", |
| 234 | imajor(inode), iminor(inode), routine); |
| 235 | return 1; |
| 236 | } |
| 237 | #endif |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int check_tty_count(struct tty_struct *tty, const char *routine) |
| 242 | { |
| 243 | #ifdef CHECK_TTY_COUNT |
| 244 | struct list_head *p; |
| 245 | int count = 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | file_list_lock(); |
| 248 | list_for_each(p, &tty->tty_files) { |
| 249 | count++; |
| 250 | } |
| 251 | file_list_unlock(); |
| 252 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 253 | tty->driver->subtype == PTY_TYPE_SLAVE && |
| 254 | tty->link && tty->link->count) |
| 255 | count++; |
| 256 | if (tty->count != count) { |
| 257 | printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) " |
| 258 | "!= #fd's(%d) in %s\n", |
| 259 | tty->name, tty->count, count, routine); |
| 260 | return count; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | #endif |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /* |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 267 | * Tty buffer allocation management |
| 268 | */ |
| 269 | |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 270 | /** |
| 271 | * tty_buffer_free_all - free buffers used by a tty |
| 272 | * @tty: tty to free from |
| 273 | * |
| 274 | * Remove all the buffers pending on a tty whether queued with data |
| 275 | * or in the free ring. Must be called when the tty is no longer in use |
| 276 | * |
| 277 | * Locking: none |
| 278 | */ |
| 279 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 280 | static void tty_buffer_free_all(struct tty_struct *tty) |
| 281 | { |
| 282 | struct tty_buffer *thead; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 283 | while ((thead = tty->buf.head) != NULL) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 284 | tty->buf.head = thead->next; |
| 285 | kfree(thead); |
| 286 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 287 | while ((thead = tty->buf.free) != NULL) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 288 | tty->buf.free = thead->next; |
| 289 | kfree(thead); |
| 290 | } |
| 291 | tty->buf.tail = NULL; |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 292 | tty->buf.memory_used = 0; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 295 | /** |
| 296 | * tty_buffer_init - prepare a tty buffer structure |
| 297 | * @tty: tty to initialise |
| 298 | * |
| 299 | * Set up the initial state of the buffer management for a tty device. |
| 300 | * Must be called before the other tty buffer functions are used. |
| 301 | * |
| 302 | * Locking: none |
| 303 | */ |
| 304 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 305 | static void tty_buffer_init(struct tty_struct *tty) |
| 306 | { |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 307 | spin_lock_init(&tty->buf.lock); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 308 | tty->buf.head = NULL; |
| 309 | tty->buf.tail = NULL; |
| 310 | tty->buf.free = NULL; |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 311 | tty->buf.memory_used = 0; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 314 | /** |
| 315 | * tty_buffer_alloc - allocate a tty buffer |
| 316 | * @tty: tty device |
| 317 | * @size: desired size (characters) |
| 318 | * |
| 319 | * Allocate a new tty buffer to hold the desired number of characters. |
| 320 | * Return NULL if out of memory or the allocation would exceed the |
| 321 | * per device queue |
| 322 | * |
| 323 | * Locking: Caller must hold tty->buf.lock |
| 324 | */ |
| 325 | |
| 326 | static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 327 | { |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 328 | struct tty_buffer *p; |
| 329 | |
| 330 | if (tty->buf.memory_used + size > 65536) |
| 331 | return NULL; |
| 332 | p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 333 | if (p == NULL) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 334 | return NULL; |
| 335 | p->used = 0; |
| 336 | p->size = size; |
| 337 | p->next = NULL; |
Paul Fulghum | 8977d92 | 2006-02-10 01:51:14 -0800 | [diff] [blame] | 338 | p->commit = 0; |
| 339 | p->read = 0; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 340 | p->char_buf_ptr = (char *)(p->data); |
| 341 | p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size; |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 342 | tty->buf.memory_used += size; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 343 | return p; |
| 344 | } |
| 345 | |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 346 | /** |
| 347 | * tty_buffer_free - free a tty buffer |
| 348 | * @tty: tty owning the buffer |
| 349 | * @b: the buffer to free |
| 350 | * |
| 351 | * Free a tty buffer, or add it to the free list according to our |
| 352 | * internal strategy |
| 353 | * |
| 354 | * Locking: Caller must hold tty->buf.lock |
| 355 | */ |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 356 | |
| 357 | static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b) |
| 358 | { |
| 359 | /* Dumb strategy for now - should keep some stats */ |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 360 | tty->buf.memory_used -= b->size; |
| 361 | WARN_ON(tty->buf.memory_used < 0); |
| 362 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 363 | if (b->size >= 512) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 364 | kfree(b); |
| 365 | else { |
| 366 | b->next = tty->buf.free; |
| 367 | tty->buf.free = b; |
| 368 | } |
| 369 | } |
| 370 | |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 371 | /** |
Alan Cox | 42fd552 | 2007-08-10 13:01:05 -0700 | [diff] [blame] | 372 | * __tty_buffer_flush - flush full tty buffers |
| 373 | * @tty: tty to flush |
| 374 | * |
| 375 | * flush all the buffers containing receive data. Caller must |
| 376 | * hold the buffer lock and must have ensured no parallel flush to |
| 377 | * ldisc is running. |
| 378 | * |
| 379 | * Locking: Caller must hold tty->buf.lock |
| 380 | */ |
| 381 | |
| 382 | static void __tty_buffer_flush(struct tty_struct *tty) |
| 383 | { |
| 384 | struct tty_buffer *thead; |
| 385 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 386 | while ((thead = tty->buf.head) != NULL) { |
Alan Cox | 42fd552 | 2007-08-10 13:01:05 -0700 | [diff] [blame] | 387 | tty->buf.head = thead->next; |
| 388 | tty_buffer_free(tty, thead); |
| 389 | } |
| 390 | tty->buf.tail = NULL; |
| 391 | } |
| 392 | |
| 393 | /** |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 394 | * tty_buffer_flush - flush full tty buffers |
| 395 | * @tty: tty to flush |
| 396 | * |
Alan Cox | 42fd552 | 2007-08-10 13:01:05 -0700 | [diff] [blame] | 397 | * flush all the buffers containing receive data. If the buffer is |
| 398 | * being processed by flush_to_ldisc then we defer the processing |
| 399 | * to that function |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 400 | * |
| 401 | * Locking: none |
| 402 | */ |
| 403 | |
| 404 | static void tty_buffer_flush(struct tty_struct *tty) |
| 405 | { |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 406 | unsigned long flags; |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 407 | spin_lock_irqsave(&tty->buf.lock, flags); |
Alan Cox | 42fd552 | 2007-08-10 13:01:05 -0700 | [diff] [blame] | 408 | |
| 409 | /* If the data is being pushed to the tty layer then we can't |
| 410 | process it here. Instead set a flag and the flush_to_ldisc |
| 411 | path will process the flush request before it exits */ |
| 412 | if (test_bit(TTY_FLUSHING, &tty->flags)) { |
| 413 | set_bit(TTY_FLUSHPENDING, &tty->flags); |
| 414 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 415 | wait_event(tty->read_wait, |
| 416 | test_bit(TTY_FLUSHPENDING, &tty->flags) == 0); |
| 417 | return; |
| 418 | } else |
| 419 | __tty_buffer_flush(tty); |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 420 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 421 | } |
| 422 | |
| 423 | /** |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 424 | * tty_buffer_find - find a free tty buffer |
| 425 | * @tty: tty owning the buffer |
| 426 | * @size: characters wanted |
| 427 | * |
| 428 | * Locate an existing suitable tty buffer or if we are lacking one then |
| 429 | * allocate a new one. We round our buffers off in 256 character chunks |
| 430 | * to get better allocation behaviour. |
| 431 | * |
| 432 | * Locking: Caller must hold tty->buf.lock |
| 433 | */ |
| 434 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 435 | static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size) |
| 436 | { |
| 437 | struct tty_buffer **tbh = &tty->buf.free; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 438 | while ((*tbh) != NULL) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 439 | struct tty_buffer *t = *tbh; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 440 | if (t->size >= size) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 441 | *tbh = t->next; |
| 442 | t->next = NULL; |
| 443 | t->used = 0; |
Paul Fulghum | 8977d92 | 2006-02-10 01:51:14 -0800 | [diff] [blame] | 444 | t->commit = 0; |
| 445 | t->read = 0; |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 446 | tty->buf.memory_used += t->size; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 447 | return t; |
| 448 | } |
| 449 | tbh = &((*tbh)->next); |
| 450 | } |
| 451 | /* Round the buffer size out */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 452 | size = (size + 0xFF) & ~0xFF; |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 453 | return tty_buffer_alloc(tty, size); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 454 | /* Should possibly check if this fails for the largest buffer we |
| 455 | have queued and recycle that ? */ |
| 456 | } |
| 457 | |
Alan Cox | 01da5fd | 2006-08-27 01:24:02 -0700 | [diff] [blame] | 458 | /** |
| 459 | * tty_buffer_request_room - grow tty buffer if needed |
| 460 | * @tty: tty structure |
| 461 | * @size: size desired |
| 462 | * |
| 463 | * Make at least size bytes of linear space available for the tty |
| 464 | * buffer. If we fail return the size we managed to find. |
| 465 | * |
| 466 | * Locking: Takes tty->buf.lock |
| 467 | */ |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 468 | int tty_buffer_request_room(struct tty_struct *tty, size_t size) |
| 469 | { |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 470 | struct tty_buffer *b, *n; |
| 471 | int left; |
| 472 | unsigned long flags; |
| 473 | |
| 474 | spin_lock_irqsave(&tty->buf.lock, flags); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 475 | |
| 476 | /* OPTIMISATION: We could keep a per tty "zero" sized buffer to |
| 477 | remove this conditional if its worth it. This would be invisible |
| 478 | to the callers */ |
Paul Fulghum | 33b37a3 | 2006-06-28 04:26:49 -0700 | [diff] [blame] | 479 | if ((b = tty->buf.tail) != NULL) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 480 | left = b->size - b->used; |
Paul Fulghum | 33b37a3 | 2006-06-28 04:26:49 -0700 | [diff] [blame] | 481 | else |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 482 | left = 0; |
| 483 | |
| 484 | if (left < size) { |
| 485 | /* This is the slow path - looking for new buffers to use */ |
| 486 | if ((n = tty_buffer_find(tty, size)) != NULL) { |
| 487 | if (b != NULL) { |
| 488 | b->next = n; |
Paul Fulghum | 8977d92 | 2006-02-10 01:51:14 -0800 | [diff] [blame] | 489 | b->commit = b->used; |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 490 | } else |
| 491 | tty->buf.head = n; |
| 492 | tty->buf.tail = n; |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 493 | } else |
| 494 | size = left; |
| 495 | } |
| 496 | |
| 497 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 498 | return size; |
| 499 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 500 | EXPORT_SYMBOL_GPL(tty_buffer_request_room); |
| 501 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 502 | /** |
| 503 | * tty_insert_flip_string - Add characters to the tty buffer |
| 504 | * @tty: tty structure |
| 505 | * @chars: characters |
| 506 | * @size: size |
| 507 | * |
| 508 | * Queue a series of bytes to the tty buffering. All the characters |
| 509 | * passed are marked as without error. Returns the number added. |
| 510 | * |
| 511 | * Locking: Called functions may take tty->buf.lock |
| 512 | */ |
| 513 | |
Andrew Morton | e1a2509 | 2006-04-10 22:54:05 -0700 | [diff] [blame] | 514 | int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars, |
| 515 | size_t size) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 516 | { |
| 517 | int copied = 0; |
| 518 | do { |
| 519 | int space = tty_buffer_request_room(tty, size - copied); |
| 520 | struct tty_buffer *tb = tty->buf.tail; |
| 521 | /* If there is no space then tb may be NULL */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 522 | if (unlikely(space == 0)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 523 | break; |
| 524 | memcpy(tb->char_buf_ptr + tb->used, chars, space); |
| 525 | memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); |
| 526 | tb->used += space; |
| 527 | copied += space; |
| 528 | chars += space; |
Alexey Dobriyan | 527063b | 2006-09-29 01:59:50 -0700 | [diff] [blame] | 529 | /* There is a small chance that we need to split the data over |
| 530 | several buffers. If this is the case we must loop */ |
| 531 | } while (unlikely(size > copied)); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 532 | return copied; |
| 533 | } |
Andrew Morton | ee37df7 | 2006-03-31 02:30:35 -0800 | [diff] [blame] | 534 | EXPORT_SYMBOL(tty_insert_flip_string); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 535 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 536 | /** |
| 537 | * tty_insert_flip_string_flags - Add characters to the tty buffer |
| 538 | * @tty: tty structure |
| 539 | * @chars: characters |
| 540 | * @flags: flag bytes |
| 541 | * @size: size |
| 542 | * |
| 543 | * Queue a series of bytes to the tty buffering. For each character |
| 544 | * the flags array indicates the status of the character. Returns the |
| 545 | * number added. |
| 546 | * |
| 547 | * Locking: Called functions may take tty->buf.lock |
| 548 | */ |
| 549 | |
Andrew Morton | e1a2509 | 2006-04-10 22:54:05 -0700 | [diff] [blame] | 550 | int tty_insert_flip_string_flags(struct tty_struct *tty, |
| 551 | const unsigned char *chars, const char *flags, size_t size) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 552 | { |
| 553 | int copied = 0; |
| 554 | do { |
| 555 | int space = tty_buffer_request_room(tty, size - copied); |
| 556 | struct tty_buffer *tb = tty->buf.tail; |
| 557 | /* If there is no space then tb may be NULL */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 558 | if (unlikely(space == 0)) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 559 | break; |
| 560 | memcpy(tb->char_buf_ptr + tb->used, chars, space); |
| 561 | memcpy(tb->flag_buf_ptr + tb->used, flags, space); |
| 562 | tb->used += space; |
| 563 | copied += space; |
| 564 | chars += space; |
| 565 | flags += space; |
Alexey Dobriyan | 527063b | 2006-09-29 01:59:50 -0700 | [diff] [blame] | 566 | /* There is a small chance that we need to split the data over |
| 567 | several buffers. If this is the case we must loop */ |
| 568 | } while (unlikely(size > copied)); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 569 | return copied; |
| 570 | } |
Tobias Powalowski | ff4547f | 2006-05-22 22:35:28 -0700 | [diff] [blame] | 571 | EXPORT_SYMBOL(tty_insert_flip_string_flags); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 572 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 573 | /** |
| 574 | * tty_schedule_flip - push characters to ldisc |
| 575 | * @tty: tty to push from |
| 576 | * |
| 577 | * Takes any pending buffers and transfers their ownership to the |
| 578 | * ldisc side of the queue. It then schedules those characters for |
| 579 | * processing by the line discipline. |
| 580 | * |
| 581 | * Locking: Takes tty->buf.lock |
| 582 | */ |
| 583 | |
Andrew Morton | e1a2509 | 2006-04-10 22:54:05 -0700 | [diff] [blame] | 584 | void tty_schedule_flip(struct tty_struct *tty) |
| 585 | { |
| 586 | unsigned long flags; |
| 587 | spin_lock_irqsave(&tty->buf.lock, flags); |
Paul Fulghum | 33b37a3 | 2006-06-28 04:26:49 -0700 | [diff] [blame] | 588 | if (tty->buf.tail != NULL) |
Andrew Morton | e1a2509 | 2006-04-10 22:54:05 -0700 | [diff] [blame] | 589 | tty->buf.tail->commit = tty->buf.tail->used; |
Andrew Morton | e1a2509 | 2006-04-10 22:54:05 -0700 | [diff] [blame] | 590 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 591 | schedule_delayed_work(&tty->buf.work, 1); |
| 592 | } |
| 593 | EXPORT_SYMBOL(tty_schedule_flip); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 594 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 595 | /** |
| 596 | * tty_prepare_flip_string - make room for characters |
| 597 | * @tty: tty |
| 598 | * @chars: return pointer for character write area |
| 599 | * @size: desired size |
| 600 | * |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 601 | * Prepare a block of space in the buffer for data. Returns the length |
| 602 | * available and buffer pointer to the space which is now allocated and |
| 603 | * accounted for as ready for normal characters. This is used for drivers |
| 604 | * that need their own block copy routines into the buffer. There is no |
| 605 | * guarantee the buffer is a DMA target! |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 606 | * |
| 607 | * Locking: May call functions taking tty->buf.lock |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 608 | */ |
| 609 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 610 | int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, |
| 611 | size_t size) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 612 | { |
| 613 | int space = tty_buffer_request_room(tty, size); |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 614 | if (likely(space)) { |
| 615 | struct tty_buffer *tb = tty->buf.tail; |
| 616 | *chars = tb->char_buf_ptr + tb->used; |
| 617 | memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); |
| 618 | tb->used += space; |
| 619 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 620 | return space; |
| 621 | } |
| 622 | |
| 623 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string); |
| 624 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 625 | /** |
| 626 | * tty_prepare_flip_string_flags - make room for characters |
| 627 | * @tty: tty |
| 628 | * @chars: return pointer for character write area |
| 629 | * @flags: return pointer for status flag write area |
| 630 | * @size: desired size |
| 631 | * |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 632 | * Prepare a block of space in the buffer for data. Returns the length |
| 633 | * available and buffer pointer to the space which is now allocated and |
| 634 | * accounted for as ready for characters. This is used for drivers |
| 635 | * that need their own block copy routines into the buffer. There is no |
| 636 | * guarantee the buffer is a DMA target! |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 637 | * |
| 638 | * Locking: May call functions taking tty->buf.lock |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 639 | */ |
| 640 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 641 | int tty_prepare_flip_string_flags(struct tty_struct *tty, |
| 642 | unsigned char **chars, char **flags, size_t size) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 643 | { |
| 644 | int space = tty_buffer_request_room(tty, size); |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 645 | if (likely(space)) { |
| 646 | struct tty_buffer *tb = tty->buf.tail; |
| 647 | *chars = tb->char_buf_ptr + tb->used; |
| 648 | *flags = tb->flag_buf_ptr + tb->used; |
| 649 | tb->used += space; |
| 650 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 651 | return space; |
| 652 | } |
| 653 | |
| 654 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); |
| 655 | |
| 656 | |
| 657 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 658 | /** |
| 659 | * tty_set_termios_ldisc - set ldisc field |
| 660 | * @tty: tty structure |
| 661 | * @num: line discipline number |
| 662 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | * This is probably overkill for real world processors but |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 664 | * they are not on hot paths so a little discipline won't do |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | * any harm. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 666 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 667 | * Locking: takes termios_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) |
| 671 | { |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 672 | mutex_lock(&tty->termios_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | tty->termios->c_line = num; |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 674 | mutex_unlock(&tty->termios_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
| 678 | * This guards the refcounted line discipline lists. The lock |
| 679 | * must be taken with irqs off because there are hangup path |
| 680 | * callers who will do ldisc lookups and cannot sleep. |
| 681 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | static DEFINE_SPINLOCK(tty_ldisc_lock); |
| 684 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 685 | /* Line disc dispatch table */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 686 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 688 | /** |
| 689 | * tty_register_ldisc - install a line discipline |
| 690 | * @disc: ldisc number |
| 691 | * @new_ldisc: pointer to the ldisc object |
| 692 | * |
| 693 | * Installs a new line discipline into the kernel. The discipline |
| 694 | * is set up as unreferenced and then made available to the kernel |
| 695 | * from this point onwards. |
| 696 | * |
| 697 | * Locking: |
| 698 | * takes tty_ldisc_lock to guard against ldisc races |
| 699 | */ |
| 700 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 701 | int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
| 703 | unsigned long flags; |
| 704 | int ret = 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | if (disc < N_TTY || disc >= NR_LDISCS) |
| 707 | return -EINVAL; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 710 | tty_ldiscs[disc] = new_ldisc; |
| 711 | new_ldisc->num = disc; |
| 712 | new_ldisc->refcount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | return ret; |
| 716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | EXPORT_SYMBOL(tty_register_ldisc); |
| 718 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 719 | /** |
| 720 | * tty_unregister_ldisc - unload a line discipline |
| 721 | * @disc: ldisc number |
| 722 | * @new_ldisc: pointer to the ldisc object |
| 723 | * |
| 724 | * Remove a line discipline from the kernel providing it is not |
| 725 | * currently in use. |
| 726 | * |
| 727 | * Locking: |
| 728 | * takes tty_ldisc_lock to guard against ldisc races |
| 729 | */ |
| 730 | |
Alexey Dobriyan | bfb0759 | 2005-06-23 00:10:32 -0700 | [diff] [blame] | 731 | int tty_unregister_ldisc(int disc) |
| 732 | { |
| 733 | unsigned long flags; |
| 734 | int ret = 0; |
| 735 | |
| 736 | if (disc < N_TTY || disc >= NR_LDISCS) |
| 737 | return -EINVAL; |
| 738 | |
| 739 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 740 | if (tty_ldiscs[disc]->refcount) |
Alexey Dobriyan | bfb0759 | 2005-06-23 00:10:32 -0700 | [diff] [blame] | 741 | ret = -EBUSY; |
| 742 | else |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 743 | tty_ldiscs[disc] = NULL; |
Alexey Dobriyan | bfb0759 | 2005-06-23 00:10:32 -0700 | [diff] [blame] | 744 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
| 745 | |
| 746 | return ret; |
| 747 | } |
| 748 | EXPORT_SYMBOL(tty_unregister_ldisc); |
| 749 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 750 | |
| 751 | /** |
| 752 | * tty_ldisc_try_get - try and reference an ldisc |
| 753 | * @disc: ldisc number |
| 754 | * @ld: tty ldisc structure to complete |
| 755 | * |
| 756 | * Attempt to open and lock a line discipline into place. Return |
| 757 | * the line discipline refcounted and assigned in ld. On an error |
| 758 | * report the error code back |
| 759 | */ |
| 760 | |
| 761 | static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld) |
| 762 | { |
| 763 | unsigned long flags; |
| 764 | struct tty_ldisc_ops *ldops; |
| 765 | int err = -EINVAL; |
| 766 | |
| 767 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
| 768 | ld->ops = NULL; |
| 769 | ldops = tty_ldiscs[disc]; |
| 770 | /* Check the entry is defined */ |
| 771 | if (ldops) { |
| 772 | /* If the module is being unloaded we can't use it */ |
| 773 | if (!try_module_get(ldops->owner)) |
| 774 | err = -EAGAIN; |
| 775 | else { |
| 776 | /* lock it */ |
| 777 | ldops->refcount++; |
| 778 | ld->ops = ldops; |
| 779 | err = 0; |
| 780 | } |
| 781 | } |
| 782 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
| 783 | return err; |
| 784 | } |
| 785 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 786 | /** |
| 787 | * tty_ldisc_get - take a reference to an ldisc |
| 788 | * @disc: ldisc number |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 789 | * @ld: tty line discipline structure to use |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 790 | * |
| 791 | * Takes a reference to a line discipline. Deals with refcounts and |
| 792 | * module locking counts. Returns NULL if the discipline is not available. |
| 793 | * Returns a pointer to the discipline and bumps the ref count if it is |
| 794 | * available |
| 795 | * |
| 796 | * Locking: |
| 797 | * takes tty_ldisc_lock to guard against ldisc races |
| 798 | */ |
| 799 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 800 | static int tty_ldisc_get(int disc, struct tty_ldisc *ld) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 802 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | if (disc < N_TTY || disc >= NR_LDISCS) |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 805 | return -EINVAL; |
| 806 | err = tty_ldisc_try_get(disc, ld); |
| 807 | if (err == -EAGAIN) { |
| 808 | request_module("tty-ldisc-%d", disc); |
| 809 | err = tty_ldisc_try_get(disc, ld); |
| 810 | } |
| 811 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 814 | /** |
| 815 | * tty_ldisc_put - drop ldisc reference |
| 816 | * @disc: ldisc number |
| 817 | * |
| 818 | * Drop a reference to a line discipline. Manage refcounts and |
| 819 | * module usage counts |
| 820 | * |
| 821 | * Locking: |
| 822 | * takes tty_ldisc_lock to guard against ldisc races |
| 823 | */ |
| 824 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 825 | static void tty_ldisc_put(struct tty_ldisc_ops *ld) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | unsigned long flags; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 828 | int disc = ld->num; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 829 | |
Eric Sesterhenn | 56ee482 | 2006-03-26 18:17:21 +0200 | [diff] [blame] | 830 | BUG_ON(disc < N_TTY || disc >= NR_LDISCS); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 833 | ld = tty_ldiscs[disc]; |
Eric Sesterhenn | 56ee482 | 2006-03-26 18:17:21 +0200 | [diff] [blame] | 834 | BUG_ON(ld->refcount == 0); |
| 835 | ld->refcount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | module_put(ld->owner); |
| 837 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
| 838 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 839 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 840 | static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) |
| 841 | { |
| 842 | return (*pos < NR_LDISCS) ? pos : NULL; |
| 843 | } |
| 844 | |
| 845 | static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 846 | { |
| 847 | (*pos)++; |
| 848 | return (*pos < NR_LDISCS) ? pos : NULL; |
| 849 | } |
| 850 | |
| 851 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) |
| 852 | { |
| 853 | } |
| 854 | |
| 855 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) |
| 856 | { |
| 857 | int i = *(loff_t *)v; |
| 858 | struct tty_ldisc ld; |
| 859 | |
| 860 | if (tty_ldisc_get(i, &ld) < 0) |
| 861 | return 0; |
| 862 | seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i); |
| 863 | tty_ldisc_put(ld.ops); |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | static const struct seq_operations tty_ldiscs_seq_ops = { |
| 868 | .start = tty_ldiscs_seq_start, |
| 869 | .next = tty_ldiscs_seq_next, |
| 870 | .stop = tty_ldiscs_seq_stop, |
| 871 | .show = tty_ldiscs_seq_show, |
| 872 | }; |
| 873 | |
| 874 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) |
| 875 | { |
| 876 | return seq_open(file, &tty_ldiscs_seq_ops); |
| 877 | } |
| 878 | |
| 879 | const struct file_operations tty_ldiscs_proc_fops = { |
| 880 | .owner = THIS_MODULE, |
| 881 | .open = proc_tty_ldiscs_open, |
| 882 | .read = seq_read, |
| 883 | .llseek = seq_lseek, |
| 884 | .release = seq_release, |
| 885 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 887 | /** |
| 888 | * tty_ldisc_assign - set ldisc on a tty |
| 889 | * @tty: tty to assign |
| 890 | * @ld: line discipline |
| 891 | * |
| 892 | * Install an instance of a line discipline into a tty structure. The |
| 893 | * ldisc must have a reference count above zero to ensure it remains/ |
| 894 | * The tty instance refcount starts at zero. |
| 895 | * |
| 896 | * Locking: |
| 897 | * Caller must hold references |
| 898 | */ |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) |
| 901 | { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 902 | ld->refcount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | tty->ldisc = *ld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /** |
| 907 | * tty_ldisc_try - internal helper |
| 908 | * @tty: the tty |
| 909 | * |
| 910 | * Make a single attempt to grab and bump the refcount on |
| 911 | * the tty ldisc. Return 0 on failure or 1 on success. This is |
| 912 | * used to implement both the waiting and non waiting versions |
| 913 | * of tty_ldisc_ref |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 914 | * |
| 915 | * Locking: takes tty_ldisc_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | */ |
| 917 | |
| 918 | static int tty_ldisc_try(struct tty_struct *tty) |
| 919 | { |
| 920 | unsigned long flags; |
| 921 | struct tty_ldisc *ld; |
| 922 | int ret = 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
| 925 | ld = &tty->ldisc; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 926 | if (test_bit(TTY_LDISC, &tty->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | ld->refcount++; |
| 928 | ret = 1; |
| 929 | } |
| 930 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
| 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * tty_ldisc_ref_wait - wait for the tty ldisc |
| 936 | * @tty: tty device |
| 937 | * |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 938 | * Dereference the line discipline for the terminal and take a |
| 939 | * reference to it. If the line discipline is in flux then |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | * wait patiently until it changes. |
| 941 | * |
| 942 | * Note: Must not be called from an IRQ/timer context. The caller |
| 943 | * must also be careful not to hold other locks that will deadlock |
| 944 | * against a discipline change, such as an existing ldisc reference |
| 945 | * (which we check for) |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 946 | * |
| 947 | * Locking: call functions take tty_ldisc_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) |
| 951 | { |
| 952 | /* wait_event is a macro */ |
| 953 | wait_event(tty_ldisc_wait, tty_ldisc_try(tty)); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 954 | if (tty->ldisc.refcount == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | printk(KERN_ERR "tty_ldisc_ref_wait\n"); |
| 956 | return &tty->ldisc; |
| 957 | } |
| 958 | |
| 959 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); |
| 960 | |
| 961 | /** |
| 962 | * tty_ldisc_ref - get the tty ldisc |
| 963 | * @tty: tty device |
| 964 | * |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 965 | * Dereference the line discipline for the terminal and take a |
| 966 | * reference to it. If the line discipline is in flux then |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | * return NULL. Can be called from IRQ and timer functions. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 968 | * |
| 969 | * Locking: called functions take tty_ldisc_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) |
| 973 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 974 | if (tty_ldisc_try(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | return &tty->ldisc; |
| 976 | return NULL; |
| 977 | } |
| 978 | |
| 979 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); |
| 980 | |
| 981 | /** |
| 982 | * tty_ldisc_deref - free a tty ldisc reference |
| 983 | * @ld: reference to free up |
| 984 | * |
| 985 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May |
| 986 | * be called in IRQ context. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 987 | * |
| 988 | * Locking: takes tty_ldisc_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | void tty_ldisc_deref(struct tty_ldisc *ld) |
| 992 | { |
| 993 | unsigned long flags; |
| 994 | |
Eric Sesterhenn | 56ee482 | 2006-03-26 18:17:21 +0200 | [diff] [blame] | 995 | BUG_ON(ld == NULL); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 998 | if (ld->refcount == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | printk(KERN_ERR "tty_ldisc_deref: no references.\n"); |
| 1000 | else |
| 1001 | ld->refcount--; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1002 | if (ld->refcount == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | wake_up(&tty_ldisc_wait); |
| 1004 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
| 1005 | } |
| 1006 | |
| 1007 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); |
| 1008 | |
| 1009 | /** |
| 1010 | * tty_ldisc_enable - allow ldisc use |
| 1011 | * @tty: terminal to activate ldisc on |
| 1012 | * |
| 1013 | * Set the TTY_LDISC flag when the line discipline can be called |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 1014 | * again. Do necessary wakeups for existing sleepers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | * |
| 1016 | * Note: nobody should set this bit except via this function. Clearing |
| 1017 | * directly is allowed. |
| 1018 | */ |
| 1019 | |
| 1020 | static void tty_ldisc_enable(struct tty_struct *tty) |
| 1021 | { |
| 1022 | set_bit(TTY_LDISC, &tty->flags); |
| 1023 | wake_up(&tty_ldisc_wait); |
| 1024 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | /** |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1027 | * tty_ldisc_restore - helper for tty ldisc change |
| 1028 | * @tty: tty to recover |
| 1029 | * @old: previous ldisc |
| 1030 | * |
| 1031 | * Restore the previous line discipline or N_TTY when a line discipline |
| 1032 | * change fails due to an open error |
| 1033 | */ |
| 1034 | |
| 1035 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) |
| 1036 | { |
| 1037 | char buf[64]; |
| 1038 | struct tty_ldisc new_ldisc; |
| 1039 | |
| 1040 | /* There is an outstanding reference here so this is safe */ |
| 1041 | tty_ldisc_get(old->ops->num, old); |
| 1042 | tty_ldisc_assign(tty, old); |
| 1043 | tty_set_termios_ldisc(tty, old->ops->num); |
| 1044 | if (old->ops->open && (old->ops->open(tty) < 0)) { |
| 1045 | tty_ldisc_put(old->ops); |
| 1046 | /* This driver is always present */ |
| 1047 | if (tty_ldisc_get(N_TTY, &new_ldisc) < 0) |
| 1048 | panic("n_tty: get"); |
| 1049 | tty_ldisc_assign(tty, &new_ldisc); |
| 1050 | tty_set_termios_ldisc(tty, N_TTY); |
| 1051 | if (new_ldisc.ops->open) { |
| 1052 | int r = new_ldisc.ops->open(tty); |
| 1053 | if (r < 0) |
| 1054 | panic("Couldn't open N_TTY ldisc for " |
| 1055 | "%s --- error %d.", |
| 1056 | tty_name(tty, buf), r); |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | * tty_set_ldisc - set line discipline |
| 1063 | * @tty: the terminal to set |
| 1064 | * @ldisc: the line discipline |
| 1065 | * |
| 1066 | * Set the discipline of a tty line. Must be called from a process |
| 1067 | * context. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1068 | * |
| 1069 | * Locking: takes tty_ldisc_lock. |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1070 | * called functions take termios_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | static int tty_set_ldisc(struct tty_struct *tty, int ldisc) |
| 1074 | { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1075 | int retval; |
| 1076 | struct tty_ldisc o_ldisc, new_ldisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | int work; |
| 1078 | unsigned long flags; |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1079 | struct tty_struct *o_tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | restart: |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1082 | /* This is a bit ugly for now but means we can break the 'ldisc |
| 1083 | is part of the tty struct' assumption later */ |
| 1084 | retval = tty_ldisc_get(ldisc, &new_ldisc); |
| 1085 | if (retval) |
| 1086 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1088 | /* |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1089 | * Problem: What do we do if this blocks ? |
| 1090 | */ |
| 1091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | tty_wait_until_sent(tty, 0); |
| 1093 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1094 | if (tty->ldisc.ops->num == ldisc) { |
| 1095 | tty_ldisc_put(new_ldisc.ops); |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
Paul Fulghum | ae030e4 | 2007-05-09 02:33:38 -0700 | [diff] [blame] | 1099 | /* |
| 1100 | * No more input please, we are switching. The new ldisc |
| 1101 | * will update this value in the ldisc open function |
| 1102 | */ |
| 1103 | |
| 1104 | tty->receive_room = 0; |
| 1105 | |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1106 | o_ldisc = tty->ldisc; |
| 1107 | o_tty = tty->link; |
| 1108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | /* |
| 1110 | * Make sure we don't change while someone holds a |
| 1111 | * reference to the line discipline. The TTY_LDISC bit |
| 1112 | * prevents anyone taking a reference once it is clear. |
| 1113 | * We need the lock to avoid racing reference takers. |
| 1114 | */ |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1117 | if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1118 | if (tty->ldisc.refcount) { |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1119 | /* Free the new ldisc we grabbed. Must drop the lock |
| 1120 | first. */ |
| 1121 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1122 | tty_ldisc_put(o_ldisc.ops); |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1123 | /* |
| 1124 | * There are several reasons we may be busy, including |
| 1125 | * random momentary I/O traffic. We must therefore |
| 1126 | * retry. We could distinguish between blocking ops |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1127 | * and retries if we made tty_ldisc_wait() smarter. |
| 1128 | * That is up for discussion. |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1129 | */ |
| 1130 | if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0) |
| 1131 | return -ERESTARTSYS; |
| 1132 | goto restart; |
| 1133 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1134 | if (o_tty && o_tty->ldisc.refcount) { |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1135 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1136 | tty_ldisc_put(o_tty->ldisc.ops); |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1137 | if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0) |
| 1138 | return -ERESTARTSYS; |
| 1139 | goto restart; |
| 1140 | } |
| 1141 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1142 | /* |
| 1143 | * If the TTY_LDISC bit is set, then we are racing against |
| 1144 | * another ldisc change |
| 1145 | */ |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1146 | if (!test_bit(TTY_LDISC, &tty->flags)) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1147 | struct tty_ldisc *ld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1149 | tty_ldisc_put(new_ldisc.ops); |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1150 | ld = tty_ldisc_ref_wait(tty); |
| 1151 | tty_ldisc_deref(ld); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | goto restart; |
| 1153 | } |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1154 | |
| 1155 | clear_bit(TTY_LDISC, &tty->flags); |
Paul Fulghum | 817d6d3 | 2006-06-28 04:26:47 -0700 | [diff] [blame] | 1156 | if (o_tty) |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1157 | clear_bit(TTY_LDISC, &o_tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | /* |
| 1161 | * From this point on we know nobody has an ldisc |
| 1162 | * usage reference, nor can they obtain one until |
| 1163 | * we say so later on. |
| 1164 | */ |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1165 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1166 | work = cancel_delayed_work(&tty->buf.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | /* |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1168 | * Wait for ->hangup_work and ->buf.work handlers to terminate |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1169 | * MUST NOT hold locks here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | flush_scheduled_work(); |
| 1172 | /* Shutdown the current discipline. */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1173 | if (o_ldisc.ops->close) |
| 1174 | (o_ldisc.ops->close)(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
| 1176 | /* Now set up the new line discipline. */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1177 | tty_ldisc_assign(tty, &new_ldisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | tty_set_termios_ldisc(tty, ldisc); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1179 | if (new_ldisc.ops->open) |
| 1180 | retval = (new_ldisc.ops->open)(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | if (retval < 0) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1182 | tty_ldisc_put(new_ldisc.ops); |
| 1183 | tty_ldisc_restore(tty, &o_ldisc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | } |
| 1185 | /* At this point we hold a reference to the new ldisc and a |
| 1186 | a reference to the old ldisc. If we ended up flipping back |
| 1187 | to the existing ldisc we have two references to it */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1188 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1189 | if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1190 | tty->ops->set_ldisc(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1191 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1192 | tty_ldisc_put(o_ldisc.ops); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | /* |
| 1195 | * Allow ldisc referencing to occur as soon as the driver |
| 1196 | * ldisc callback completes. |
| 1197 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | tty_ldisc_enable(tty); |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1200 | if (o_tty) |
| 1201 | tty_ldisc_enable(o_tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | /* Restart it in case no characters kick it off. Safe if |
| 1204 | already running */ |
Jason Baron | ff55fe2 | 2005-09-09 13:01:57 -0700 | [diff] [blame] | 1205 | if (work) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1206 | schedule_delayed_work(&tty->buf.work, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | return retval; |
| 1208 | } |
| 1209 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1210 | /** |
| 1211 | * get_tty_driver - find device of a tty |
| 1212 | * @dev_t: device identifier |
| 1213 | * @index: returns the index of the tty |
| 1214 | * |
| 1215 | * This routine returns a tty driver structure, given a device number |
| 1216 | * and also passes back the index number. |
| 1217 | * |
| 1218 | * Locking: caller must hold tty_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | static struct tty_driver *get_tty_driver(dev_t device, int *index) |
| 1222 | { |
| 1223 | struct tty_driver *p; |
| 1224 | |
| 1225 | list_for_each_entry(p, &tty_drivers, tty_drivers) { |
| 1226 | dev_t base = MKDEV(p->major, p->minor_start); |
| 1227 | if (device < base || device >= base + p->num) |
| 1228 | continue; |
| 1229 | *index = device - base; |
| 1230 | return p; |
| 1231 | } |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1235 | #ifdef CONFIG_CONSOLE_POLL |
| 1236 | |
| 1237 | /** |
| 1238 | * tty_find_polling_driver - find device of a polled tty |
| 1239 | * @name: name string to match |
| 1240 | * @line: pointer to resulting tty line nr |
| 1241 | * |
| 1242 | * This routine returns a tty driver structure, given a name |
| 1243 | * and the condition that the tty driver is capable of polled |
| 1244 | * operation. |
| 1245 | */ |
| 1246 | struct tty_driver *tty_find_polling_driver(char *name, int *line) |
| 1247 | { |
| 1248 | struct tty_driver *p, *res = NULL; |
| 1249 | int tty_line = 0; |
| 1250 | char *str; |
| 1251 | |
| 1252 | mutex_lock(&tty_mutex); |
| 1253 | /* Search through the tty devices to look for a match */ |
| 1254 | list_for_each_entry(p, &tty_drivers, tty_drivers) { |
| 1255 | str = name + strlen(p->name); |
| 1256 | tty_line = simple_strtoul(str, &str, 10); |
| 1257 | if (*str == ',') |
| 1258 | str++; |
| 1259 | if (*str == '\0') |
Harvey Harrison | 8da5630 | 2008-04-28 14:13:20 -0700 | [diff] [blame] | 1260 | str = NULL; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1261 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1262 | if (tty_line >= 0 && tty_line <= p->num && p->ops && |
| 1263 | p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) { |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1264 | res = p; |
| 1265 | *line = tty_line; |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | mutex_unlock(&tty_mutex); |
| 1270 | |
| 1271 | return res; |
| 1272 | } |
| 1273 | EXPORT_SYMBOL_GPL(tty_find_polling_driver); |
| 1274 | #endif |
| 1275 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1276 | /** |
| 1277 | * tty_check_change - check for POSIX terminal changes |
| 1278 | * @tty: tty to check |
| 1279 | * |
| 1280 | * If we try to write to, or set the state of, a terminal and we're |
| 1281 | * not in the foreground, send a SIGTTOU. If the signal is blocked or |
| 1282 | * ignored, go ahead and perform the operation. (POSIX 7.2) |
| 1283 | * |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 1284 | * Locking: ctrl_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1286 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1287 | int tty_check_change(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | { |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1289 | unsigned long flags; |
| 1290 | int ret = 0; |
| 1291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | if (current->signal->tty != tty) |
| 1293 | return 0; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1294 | |
| 1295 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 1296 | |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1297 | if (!tty->pgrp) { |
| 1298 | printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n"); |
Andrew Morton | 9ffee4c | 2008-05-14 16:05:58 -0700 | [diff] [blame] | 1299 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1301 | if (task_pgrp(current) == tty->pgrp) |
Andrew Morton | 9ffee4c | 2008-05-14 16:05:58 -0700 | [diff] [blame] | 1302 | goto out_unlock; |
| 1303 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | if (is_ignored(SIGTTOU)) |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1305 | goto out; |
| 1306 | if (is_current_pgrp_orphaned()) { |
| 1307 | ret = -EIO; |
| 1308 | goto out; |
| 1309 | } |
Oleg Nesterov | 040b636 | 2007-06-01 00:46:53 -0700 | [diff] [blame] | 1310 | kill_pgrp(task_pgrp(current), SIGTTOU, 1); |
| 1311 | set_thread_flag(TIF_SIGPENDING); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1312 | ret = -ERESTARTSYS; |
| 1313 | out: |
Andrew Morton | 9ffee4c | 2008-05-14 16:05:58 -0700 | [diff] [blame] | 1314 | return ret; |
| 1315 | out_unlock: |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1316 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 1317 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | EXPORT_SYMBOL(tty_check_change); |
| 1321 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1322 | static ssize_t hung_up_tty_read(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | size_t count, loff_t *ppos) |
| 1324 | { |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1328 | static ssize_t hung_up_tty_write(struct file *file, const char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | size_t count, loff_t *ppos) |
| 1330 | { |
| 1331 | return -EIO; |
| 1332 | } |
| 1333 | |
| 1334 | /* No kernel lock held - none needed ;) */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1335 | static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | { |
| 1337 | return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM; |
| 1338 | } |
| 1339 | |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1340 | static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, |
| 1341 | unsigned long arg) |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 1342 | { |
| 1343 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; |
| 1344 | } |
| 1345 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1346 | static long hung_up_tty_compat_ioctl(struct file *file, |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 1347 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | { |
| 1349 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; |
| 1350 | } |
| 1351 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 1352 | static const struct file_operations tty_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | .llseek = no_llseek, |
| 1354 | .read = tty_read, |
| 1355 | .write = tty_write, |
| 1356 | .poll = tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1357 | .unlocked_ioctl = tty_ioctl, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 1358 | .compat_ioctl = tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | .open = tty_open, |
| 1360 | .release = tty_release, |
| 1361 | .fasync = tty_fasync, |
| 1362 | }; |
| 1363 | |
| 1364 | #ifdef CONFIG_UNIX98_PTYS |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 1365 | static const struct file_operations ptmx_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | .llseek = no_llseek, |
| 1367 | .read = tty_read, |
| 1368 | .write = tty_write, |
| 1369 | .poll = tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1370 | .unlocked_ioctl = tty_ioctl, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 1371 | .compat_ioctl = tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | .open = ptmx_open, |
| 1373 | .release = tty_release, |
| 1374 | .fasync = tty_fasync, |
| 1375 | }; |
| 1376 | #endif |
| 1377 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 1378 | static const struct file_operations console_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | .llseek = no_llseek, |
| 1380 | .read = tty_read, |
| 1381 | .write = redirected_tty_write, |
| 1382 | .poll = tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1383 | .unlocked_ioctl = tty_ioctl, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 1384 | .compat_ioctl = tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | .open = tty_open, |
| 1386 | .release = tty_release, |
| 1387 | .fasync = tty_fasync, |
| 1388 | }; |
| 1389 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 1390 | static const struct file_operations hung_up_tty_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | .llseek = no_llseek, |
| 1392 | .read = hung_up_tty_read, |
| 1393 | .write = hung_up_tty_write, |
| 1394 | .poll = hung_up_tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1395 | .unlocked_ioctl = hung_up_tty_ioctl, |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 1396 | .compat_ioctl = hung_up_tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | .release = tty_release, |
| 1398 | }; |
| 1399 | |
| 1400 | static DEFINE_SPINLOCK(redirect_lock); |
| 1401 | static struct file *redirect; |
| 1402 | |
| 1403 | /** |
| 1404 | * tty_wakeup - request more data |
| 1405 | * @tty: terminal |
| 1406 | * |
| 1407 | * Internal and external helper for wakeups of tty. This function |
| 1408 | * informs the line discipline if present that the driver is ready |
| 1409 | * to receive more output data. |
| 1410 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | void tty_wakeup(struct tty_struct *tty) |
| 1413 | { |
| 1414 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { |
| 1417 | ld = tty_ldisc_ref(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1418 | if (ld) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1419 | if (ld->ops->write_wakeup) |
| 1420 | ld->ops->write_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | tty_ldisc_deref(ld); |
| 1422 | } |
| 1423 | } |
| 1424 | wake_up_interruptible(&tty->write_wait); |
| 1425 | } |
| 1426 | |
| 1427 | EXPORT_SYMBOL_GPL(tty_wakeup); |
| 1428 | |
| 1429 | /** |
| 1430 | * tty_ldisc_flush - flush line discipline queue |
| 1431 | * @tty: tty |
| 1432 | * |
| 1433 | * Flush the line discipline queue (if any) for this tty. If there |
| 1434 | * is no line discipline active this is a no-op. |
| 1435 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | void tty_ldisc_flush(struct tty_struct *tty) |
| 1438 | { |
| 1439 | struct tty_ldisc *ld = tty_ldisc_ref(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1440 | if (ld) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1441 | if (ld->ops->flush_buffer) |
| 1442 | ld->ops->flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | tty_ldisc_deref(ld); |
| 1444 | } |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 1445 | tty_buffer_flush(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 1449 | |
| 1450 | /** |
| 1451 | * tty_reset_termios - reset terminal state |
| 1452 | * @tty: tty to reset |
| 1453 | * |
| 1454 | * Restore a terminal to the driver default state |
| 1455 | */ |
| 1456 | |
| 1457 | static void tty_reset_termios(struct tty_struct *tty) |
| 1458 | { |
| 1459 | mutex_lock(&tty->termios_mutex); |
| 1460 | *tty->termios = tty->driver->init_termios; |
| 1461 | tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios); |
| 1462 | tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios); |
| 1463 | mutex_unlock(&tty->termios_mutex); |
| 1464 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1465 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1466 | /** |
| 1467 | * do_tty_hangup - actual handler for hangup events |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1468 | * @work: tty device |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1469 | * |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1470 | k * This can be called by the "eventd" kernel thread. That is process |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1471 | * synchronous but doesn't hold any locks, so we need to make sure we |
| 1472 | * have the appropriate locks for what we're doing. |
| 1473 | * |
| 1474 | * The hangup event clears any pending redirections onto the hung up |
| 1475 | * device. It ensures future writes will error and it does the needed |
| 1476 | * line discipline hangup and signal delivery. The tty object itself |
| 1477 | * remains intact. |
| 1478 | * |
| 1479 | * Locking: |
| 1480 | * BKL |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1481 | * redirect lock for undoing redirection |
| 1482 | * file list lock for manipulating list of ttys |
| 1483 | * tty_ldisc_lock from called functions |
| 1484 | * termios_mutex resetting termios data |
| 1485 | * tasklist_lock to walk task list for hangup event |
| 1486 | * ->siglock to protect ->signal/->sighand |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1488 | static void do_tty_hangup(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1490 | struct tty_struct *tty = |
| 1491 | container_of(work, struct tty_struct, hangup_work); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1492 | struct file *cons_filp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | struct file *filp, *f = NULL; |
| 1494 | struct task_struct *p; |
| 1495 | struct tty_ldisc *ld; |
| 1496 | int closecount = 0, n; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1497 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
| 1499 | if (!tty) |
| 1500 | return; |
| 1501 | |
| 1502 | /* inuse_filps is protected by the single kernel lock */ |
| 1503 | lock_kernel(); |
| 1504 | |
| 1505 | spin_lock(&redirect_lock); |
| 1506 | if (redirect && redirect->private_data == tty) { |
| 1507 | f = redirect; |
| 1508 | redirect = NULL; |
| 1509 | } |
| 1510 | spin_unlock(&redirect_lock); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | check_tty_count(tty, "do_tty_hangup"); |
| 1513 | file_list_lock(); |
| 1514 | /* This breaks for file handles being sent over AF_UNIX sockets ? */ |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 1515 | list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | if (filp->f_op->write == redirected_tty_write) |
| 1517 | cons_filp = filp; |
| 1518 | if (filp->f_op->write != tty_write) |
| 1519 | continue; |
| 1520 | closecount++; |
| 1521 | tty_fasync(-1, filp, 0); /* can't block */ |
| 1522 | filp->f_op = &hung_up_tty_fops; |
| 1523 | } |
| 1524 | file_list_unlock(); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1525 | /* |
| 1526 | * FIXME! What are the locking issues here? This may me overdoing |
| 1527 | * things... This question is especially important now that we've |
| 1528 | * removed the irqlock. |
| 1529 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | ld = tty_ldisc_ref(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1531 | if (ld != NULL) { |
| 1532 | /* We may have no line discipline at this point */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1533 | if (ld->ops->flush_buffer) |
| 1534 | ld->ops->flush_buffer(tty); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1535 | tty_driver_flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1537 | ld->ops->write_wakeup) |
| 1538 | ld->ops->write_wakeup(tty); |
| 1539 | if (ld->ops->hangup) |
| 1540 | ld->ops->hangup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1542 | /* |
| 1543 | * FIXME: Once we trust the LDISC code better we can wait here for |
| 1544 | * ldisc completion and fix the driver call race |
| 1545 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | wake_up_interruptible(&tty->write_wait); |
| 1547 | wake_up_interruptible(&tty->read_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | /* |
| 1549 | * Shutdown the current line discipline, and reset it to |
| 1550 | * N_TTY. |
| 1551 | */ |
| 1552 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 1553 | tty_reset_termios(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | /* Defer ldisc switch */ |
| 1555 | /* tty_deferred_ldisc_switch(N_TTY); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | This should get done automatically when the port closes and |
| 1558 | tty_release is called */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | read_lock(&tasklist_lock); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1561 | if (tty->session) { |
| 1562 | do_each_pid_task(tty->session, PIDTYPE_SID, p) { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1563 | spin_lock_irq(&p->sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | if (p->signal->tty == tty) |
| 1565 | p->signal->tty = NULL; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1566 | if (!p->signal->leader) { |
| 1567 | spin_unlock_irq(&p->sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | continue; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1569 | } |
| 1570 | __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p); |
| 1571 | __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1572 | put_pid(p->signal->tty_old_pgrp); /* A noop */ |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1573 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1574 | if (tty->pgrp) |
| 1575 | p->signal->tty_old_pgrp = get_pid(tty->pgrp); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1576 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1577 | spin_unlock_irq(&p->sighand->siglock); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1578 | } while_each_pid_task(tty->session, PIDTYPE_SID, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | } |
| 1580 | read_unlock(&tasklist_lock); |
| 1581 | |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1582 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | tty->flags = 0; |
Eric W. Biederman | d9c1e9a | 2007-03-18 12:45:44 -0600 | [diff] [blame] | 1584 | put_pid(tty->session); |
| 1585 | put_pid(tty->pgrp); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1586 | tty->session = NULL; |
| 1587 | tty->pgrp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | tty->ctrl_status = 0; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1589 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 1590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1592 | * If one of the devices matches a console pointer, we |
| 1593 | * cannot just call hangup() because that will cause |
| 1594 | * tty->count and state->count to go out of sync. |
| 1595 | * So we just call close() the right number of times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | */ |
| 1597 | if (cons_filp) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1598 | if (tty->ops->close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | for (n = 0; n < closecount; n++) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1600 | tty->ops->close(tty, cons_filp); |
| 1601 | } else if (tty->ops->hangup) |
| 1602 | (tty->ops->hangup)(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1603 | /* |
| 1604 | * We don't want to have driver/ldisc interactions beyond |
| 1605 | * the ones we did here. The driver layer expects no |
| 1606 | * calls after ->hangup() from the ldisc side. However we |
| 1607 | * can't yet guarantee all that. |
| 1608 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | set_bit(TTY_HUPPED, &tty->flags); |
| 1610 | if (ld) { |
| 1611 | tty_ldisc_enable(tty); |
| 1612 | tty_ldisc_deref(ld); |
| 1613 | } |
| 1614 | unlock_kernel(); |
| 1615 | if (f) |
| 1616 | fput(f); |
| 1617 | } |
| 1618 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1619 | /** |
| 1620 | * tty_hangup - trigger a hangup event |
| 1621 | * @tty: tty to hangup |
| 1622 | * |
| 1623 | * A carrier loss (virtual or otherwise) has occurred on this like |
| 1624 | * schedule a hangup sequence to run after this event. |
| 1625 | */ |
| 1626 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1627 | void tty_hangup(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
| 1629 | #ifdef TTY_DEBUG_HANGUP |
| 1630 | char buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf)); |
| 1632 | #endif |
| 1633 | schedule_work(&tty->hangup_work); |
| 1634 | } |
| 1635 | |
| 1636 | EXPORT_SYMBOL(tty_hangup); |
| 1637 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1638 | /** |
| 1639 | * tty_vhangup - process vhangup |
| 1640 | * @tty: tty to hangup |
| 1641 | * |
| 1642 | * The user has asked via system call for the terminal to be hung up. |
| 1643 | * We do this synchronously so that when the syscall returns the process |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 1644 | * is complete. That guarantee is necessary for security reasons. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1645 | */ |
| 1646 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1647 | void tty_vhangup(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | { |
| 1649 | #ifdef TTY_DEBUG_HANGUP |
| 1650 | char buf[64]; |
| 1651 | |
| 1652 | printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); |
| 1653 | #endif |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1654 | do_tty_hangup(&tty->hangup_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | EXPORT_SYMBOL(tty_vhangup); |
| 1658 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1659 | /** |
| 1660 | * tty_hung_up_p - was tty hung up |
| 1661 | * @filp: file pointer of tty |
| 1662 | * |
| 1663 | * Return true if the tty has been subject to a vhangup or a carrier |
| 1664 | * loss |
| 1665 | */ |
| 1666 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1667 | int tty_hung_up_p(struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | { |
| 1669 | return (filp->f_op == &hung_up_tty_fops); |
| 1670 | } |
| 1671 | |
| 1672 | EXPORT_SYMBOL(tty_hung_up_p); |
| 1673 | |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1674 | /** |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1675 | * is_tty - checker whether file is a TTY |
| 1676 | * @filp: file handle that may be a tty |
| 1677 | * |
| 1678 | * Check if the file handle is a tty handle. |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1679 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1680 | |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1681 | int is_tty(struct file *filp) |
| 1682 | { |
| 1683 | return filp->f_op->read == tty_read |
| 1684 | || filp->f_op->read == hung_up_tty_read; |
| 1685 | } |
| 1686 | |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1687 | static void session_clear_tty(struct pid *session) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1688 | { |
| 1689 | struct task_struct *p; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1690 | do_each_pid_task(session, PIDTYPE_SID, p) { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1691 | proc_clear_tty(p); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1692 | } while_each_pid_task(session, PIDTYPE_SID, p); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1693 | } |
| 1694 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1695 | /** |
| 1696 | * disassociate_ctty - disconnect controlling tty |
| 1697 | * @on_exit: true if exiting so need to "hang up" the session |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1699 | * This function is typically called only by the session leader, when |
| 1700 | * it wants to disassociate itself from its controlling tty. |
| 1701 | * |
| 1702 | * It performs the following functions: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | * (1) Sends a SIGHUP and SIGCONT to the foreground process group |
| 1704 | * (2) Clears the tty from being controlling the session |
| 1705 | * (3) Clears the controlling tty for all processes in the |
| 1706 | * session group. |
| 1707 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1708 | * The argument on_exit is set to 1 if called when a process is |
| 1709 | * exiting; it is 0 if called by the ioctl TIOCNOTTY. |
| 1710 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1711 | * Locking: |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1712 | * BKL is taken for hysterical raisins |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1713 | * tty_mutex is taken to protect tty |
| 1714 | * ->siglock is taken to protect ->signal/->sighand |
| 1715 | * tasklist_lock is taken to walk process list for sessions |
| 1716 | * ->siglock is taken to protect ->signal/->sighand |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | void disassociate_ctty(int on_exit) |
| 1720 | { |
| 1721 | struct tty_struct *tty; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1722 | struct pid *tty_pgrp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1725 | mutex_lock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1726 | tty = get_current_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | if (tty) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1728 | tty_pgrp = get_pid(tty->pgrp); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1729 | mutex_unlock(&tty_mutex); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1730 | lock_kernel(); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1731 | /* XXX: here we race, there is nothing protecting tty */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) |
| 1733 | tty_vhangup(tty); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1734 | unlock_kernel(); |
Eric W. Biederman | 680a967 | 2007-02-12 00:52:52 -0800 | [diff] [blame] | 1735 | } else if (on_exit) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1736 | struct pid *old_pgrp; |
Eric W. Biederman | 680a967 | 2007-02-12 00:52:52 -0800 | [diff] [blame] | 1737 | spin_lock_irq(¤t->sighand->siglock); |
| 1738 | old_pgrp = current->signal->tty_old_pgrp; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1739 | current->signal->tty_old_pgrp = NULL; |
Eric W. Biederman | 680a967 | 2007-02-12 00:52:52 -0800 | [diff] [blame] | 1740 | spin_unlock_irq(¤t->sighand->siglock); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1741 | if (old_pgrp) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1742 | kill_pgrp(old_pgrp, SIGHUP, on_exit); |
| 1743 | kill_pgrp(old_pgrp, SIGCONT, on_exit); |
| 1744 | put_pid(old_pgrp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | } |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1746 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | return; |
| 1748 | } |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1749 | if (tty_pgrp) { |
| 1750 | kill_pgrp(tty_pgrp, SIGHUP, on_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | if (!on_exit) |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1752 | kill_pgrp(tty_pgrp, SIGCONT, on_exit); |
| 1753 | put_pid(tty_pgrp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1756 | spin_lock_irq(¤t->sighand->siglock); |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 1757 | put_pid(current->signal->tty_old_pgrp); |
Randy Dunlap | 23cac8d | 2007-02-20 13:58:05 -0800 | [diff] [blame] | 1758 | current->signal->tty_old_pgrp = NULL; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1759 | spin_unlock_irq(¤t->sighand->siglock); |
| 1760 | |
| 1761 | mutex_lock(&tty_mutex); |
| 1762 | /* It is possible that do_tty_hangup has free'd this tty */ |
| 1763 | tty = get_current_tty(); |
| 1764 | if (tty) { |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1765 | unsigned long flags; |
| 1766 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1767 | put_pid(tty->session); |
| 1768 | put_pid(tty->pgrp); |
| 1769 | tty->session = NULL; |
| 1770 | tty->pgrp = NULL; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1771 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1772 | } else { |
| 1773 | #ifdef TTY_DEBUG_HANGUP |
| 1774 | printk(KERN_DEBUG "error attempted to write to tty [0x%p]" |
| 1775 | " = NULL", tty); |
| 1776 | #endif |
| 1777 | } |
| 1778 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | |
| 1780 | /* Now clear signal->tty under the lock */ |
| 1781 | read_lock(&tasklist_lock); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1782 | session_clear_tty(task_session(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 1786 | /** |
| 1787 | * |
| 1788 | * no_tty - Ensure the current process does not have a controlling tty |
| 1789 | */ |
| 1790 | void no_tty(void) |
| 1791 | { |
| 1792 | struct task_struct *tsk = current; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1793 | lock_kernel(); |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 1794 | if (tsk->signal->leader) |
| 1795 | disassociate_ctty(0); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1796 | unlock_kernel(); |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 1797 | proc_clear_tty(tsk); |
| 1798 | } |
| 1799 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1800 | |
| 1801 | /** |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 1802 | * stop_tty - propagate flow control |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1803 | * @tty: tty to stop |
| 1804 | * |
| 1805 | * Perform flow control to the driver. For PTY/TTY pairs we |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 1806 | * must also propagate the TIOCKPKT status. May be called |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1807 | * on an already stopped device and will not re-call the driver |
| 1808 | * method. |
| 1809 | * |
| 1810 | * This functionality is used by both the line disciplines for |
| 1811 | * halting incoming flow and by the driver. It may therefore be |
| 1812 | * called from any context, may be under the tty atomic_write_lock |
| 1813 | * but not always. |
| 1814 | * |
| 1815 | * Locking: |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1816 | * Uses the tty control lock internally |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1817 | */ |
| 1818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | void stop_tty(struct tty_struct *tty) |
| 1820 | { |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1821 | unsigned long flags; |
| 1822 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 1823 | if (tty->stopped) { |
| 1824 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | return; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | tty->stopped = 1; |
| 1828 | if (tty->link && tty->link->packet) { |
| 1829 | tty->ctrl_status &= ~TIOCPKT_START; |
| 1830 | tty->ctrl_status |= TIOCPKT_STOP; |
| 1831 | wake_up_interruptible(&tty->link->read_wait); |
| 1832 | } |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1833 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1834 | if (tty->ops->stop) |
| 1835 | (tty->ops->stop)(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | EXPORT_SYMBOL(stop_tty); |
| 1839 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1840 | /** |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 1841 | * start_tty - propagate flow control |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1842 | * @tty: tty to start |
| 1843 | * |
| 1844 | * Start a tty that has been stopped if at all possible. Perform |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 1845 | * any necessary wakeups and propagate the TIOCPKT status. If this |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1846 | * is the tty was previous stopped and is being started then the |
| 1847 | * driver start method is invoked and the line discipline woken. |
| 1848 | * |
| 1849 | * Locking: |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1850 | * ctrl_lock |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1851 | */ |
| 1852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | void start_tty(struct tty_struct *tty) |
| 1854 | { |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1855 | unsigned long flags; |
| 1856 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 1857 | if (!tty->stopped || tty->flow_stopped) { |
| 1858 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | return; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | tty->stopped = 0; |
| 1862 | if (tty->link && tty->link->packet) { |
| 1863 | tty->ctrl_status &= ~TIOCPKT_STOP; |
| 1864 | tty->ctrl_status |= TIOCPKT_START; |
| 1865 | wake_up_interruptible(&tty->link->read_wait); |
| 1866 | } |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1867 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1868 | if (tty->ops->start) |
| 1869 | (tty->ops->start)(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | /* If we have a running line discipline it may need kicking */ |
| 1871 | tty_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | EXPORT_SYMBOL(start_tty); |
| 1875 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1876 | /** |
| 1877 | * tty_read - read method for tty device files |
| 1878 | * @file: pointer to tty file |
| 1879 | * @buf: user buffer |
| 1880 | * @count: size of user buffer |
| 1881 | * @ppos: unused |
| 1882 | * |
| 1883 | * Perform the read system call function on this terminal device. Checks |
| 1884 | * for hung up devices before calling the line discipline method. |
| 1885 | * |
| 1886 | * Locking: |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 1887 | * Locks the line discipline internally while needed. Multiple |
| 1888 | * read calls may be outstanding in parallel. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1889 | */ |
| 1890 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1891 | static ssize_t tty_read(struct file *file, char __user *buf, size_t count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | loff_t *ppos) |
| 1893 | { |
| 1894 | int i; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1895 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | struct inode *inode; |
| 1897 | struct tty_ldisc *ld; |
| 1898 | |
| 1899 | tty = (struct tty_struct *)file->private_data; |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 1900 | inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | if (tty_paranoia_check(tty, inode, "tty_read")) |
| 1902 | return -EIO; |
| 1903 | if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) |
| 1904 | return -EIO; |
| 1905 | |
| 1906 | /* We want to wait for the line discipline to sort out in this |
| 1907 | situation */ |
| 1908 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1909 | if (ld->ops->read) |
| 1910 | i = (ld->ops->read)(tty, file, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | else |
| 1912 | i = -EIO; |
| 1913 | tty_ldisc_deref(ld); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | if (i > 0) |
| 1915 | inode->i_atime = current_fs_time(inode->i_sb); |
| 1916 | return i; |
| 1917 | } |
| 1918 | |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1919 | void tty_write_unlock(struct tty_struct *tty) |
| 1920 | { |
| 1921 | mutex_unlock(&tty->atomic_write_lock); |
| 1922 | wake_up_interruptible(&tty->write_wait); |
| 1923 | } |
| 1924 | |
| 1925 | int tty_write_lock(struct tty_struct *tty, int ndelay) |
| 1926 | { |
| 1927 | if (!mutex_trylock(&tty->atomic_write_lock)) { |
| 1928 | if (ndelay) |
| 1929 | return -EAGAIN; |
| 1930 | if (mutex_lock_interruptible(&tty->atomic_write_lock)) |
| 1931 | return -ERESTARTSYS; |
| 1932 | } |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | /* |
| 1937 | * Split writes up in sane blocksizes to avoid |
| 1938 | * denial-of-service type attacks |
| 1939 | */ |
| 1940 | static inline ssize_t do_tty_write( |
| 1941 | ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), |
| 1942 | struct tty_struct *tty, |
| 1943 | struct file *file, |
| 1944 | const char __user *buf, |
| 1945 | size_t count) |
| 1946 | { |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1947 | ssize_t ret, written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | unsigned int chunk; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1949 | |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1950 | ret = tty_write_lock(tty, file->f_flags & O_NDELAY); |
| 1951 | if (ret < 0) |
| 1952 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | |
| 1954 | /* |
| 1955 | * We chunk up writes into a temporary buffer. This |
| 1956 | * simplifies low-level drivers immensely, since they |
| 1957 | * don't have locking issues and user mode accesses. |
| 1958 | * |
| 1959 | * But if TTY_NO_WRITE_SPLIT is set, we should use a |
| 1960 | * big chunk-size.. |
| 1961 | * |
| 1962 | * The default chunk-size is 2kB, because the NTTY |
| 1963 | * layer has problems with bigger chunks. It will |
| 1964 | * claim to be able to handle more characters than |
| 1965 | * it actually does. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1966 | * |
| 1967 | * FIXME: This can probably go away now except that 64K chunks |
| 1968 | * are too likely to fail unless switched to vmalloc... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | */ |
| 1970 | chunk = 2048; |
| 1971 | if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) |
| 1972 | chunk = 65536; |
| 1973 | if (count < chunk) |
| 1974 | chunk = count; |
| 1975 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1976 | /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | if (tty->write_cnt < chunk) { |
| 1978 | unsigned char *buf; |
| 1979 | |
| 1980 | if (chunk < 1024) |
| 1981 | chunk = 1024; |
| 1982 | |
| 1983 | buf = kmalloc(chunk, GFP_KERNEL); |
| 1984 | if (!buf) { |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1985 | ret = -ENOMEM; |
| 1986 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | } |
| 1988 | kfree(tty->write_buf); |
| 1989 | tty->write_cnt = chunk; |
| 1990 | tty->write_buf = buf; |
| 1991 | } |
| 1992 | |
| 1993 | /* Do the write .. */ |
| 1994 | for (;;) { |
| 1995 | size_t size = count; |
| 1996 | if (size > chunk) |
| 1997 | size = chunk; |
| 1998 | ret = -EFAULT; |
| 1999 | if (copy_from_user(tty->write_buf, buf, size)) |
| 2000 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | ret = write(tty, file, tty->write_buf, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | if (ret <= 0) |
| 2003 | break; |
| 2004 | written += ret; |
| 2005 | buf += ret; |
| 2006 | count -= ret; |
| 2007 | if (!count) |
| 2008 | break; |
| 2009 | ret = -ERESTARTSYS; |
| 2010 | if (signal_pending(current)) |
| 2011 | break; |
| 2012 | cond_resched(); |
| 2013 | } |
| 2014 | if (written) { |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 2015 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | inode->i_mtime = current_fs_time(inode->i_sb); |
| 2017 | ret = written; |
| 2018 | } |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 2019 | out: |
| 2020 | tty_write_unlock(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | return ret; |
| 2022 | } |
| 2023 | |
| 2024 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2025 | /** |
| 2026 | * tty_write - write method for tty device file |
| 2027 | * @file: tty file pointer |
| 2028 | * @buf: user data to write |
| 2029 | * @count: bytes to write |
| 2030 | * @ppos: unused |
| 2031 | * |
| 2032 | * Write data to a tty device via the line discipline. |
| 2033 | * |
| 2034 | * Locking: |
| 2035 | * Locks the line discipline as required |
| 2036 | * Writes to the tty driver are serialized by the atomic_write_lock |
| 2037 | * and are then processed in chunks to the device. The line discipline |
| 2038 | * write method will not be involked in parallel for each device |
| 2039 | * The line discipline write method is called under the big |
| 2040 | * kernel lock for historical reasons. New code should not rely on this. |
| 2041 | */ |
| 2042 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2043 | static ssize_t tty_write(struct file *file, const char __user *buf, |
| 2044 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2046 | struct tty_struct *tty; |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 2047 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | ssize_t ret; |
| 2049 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | tty = (struct tty_struct *)file->private_data; |
| 2052 | if (tty_paranoia_check(tty, inode, "tty_write")) |
| 2053 | return -EIO; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2054 | if (!tty || !tty->ops->write || |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2055 | (test_bit(TTY_IO_ERROR, &tty->flags))) |
| 2056 | return -EIO; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2057 | /* Short term debug to catch buggy drivers */ |
| 2058 | if (tty->ops->write_room == NULL) |
| 2059 | printk(KERN_ERR "tty driver %s lacks a write_room method.\n", |
| 2060 | tty->driver->name); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2061 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2062 | if (!ld->ops->write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | ret = -EIO; |
| 2064 | else |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2065 | ret = do_tty_write(ld->ops->write, tty, file, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | tty_ldisc_deref(ld); |
| 2067 | return ret; |
| 2068 | } |
| 2069 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2070 | ssize_t redirected_tty_write(struct file *file, const char __user *buf, |
| 2071 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | { |
| 2073 | struct file *p = NULL; |
| 2074 | |
| 2075 | spin_lock(&redirect_lock); |
| 2076 | if (redirect) { |
| 2077 | get_file(redirect); |
| 2078 | p = redirect; |
| 2079 | } |
| 2080 | spin_unlock(&redirect_lock); |
| 2081 | |
| 2082 | if (p) { |
| 2083 | ssize_t res; |
| 2084 | res = vfs_write(p, buf, count, &p->f_pos); |
| 2085 | fput(p); |
| 2086 | return res; |
| 2087 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | return tty_write(file, buf, count, ppos); |
| 2089 | } |
| 2090 | |
Alan Cox | 6f67048 | 2008-07-16 21:53:41 +0100 | [diff] [blame^] | 2091 | void tty_port_init(struct tty_port *port) |
| 2092 | { |
| 2093 | memset(port, 0, sizeof(*port)); |
| 2094 | init_waitqueue_head(&port->open_wait); |
| 2095 | init_waitqueue_head(&port->close_wait); |
| 2096 | mutex_init(&port->mutex); |
| 2097 | } |
| 2098 | EXPORT_SYMBOL(tty_port_init); |
| 2099 | |
| 2100 | int tty_port_alloc_xmit_buf(struct tty_port *port) |
| 2101 | { |
| 2102 | /* We may sleep in get_zeroed_page() */ |
| 2103 | mutex_lock(&port->mutex); |
| 2104 | if (port->xmit_buf == NULL) |
| 2105 | port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); |
| 2106 | mutex_unlock(&port->mutex); |
| 2107 | if (port->xmit_buf == NULL) |
| 2108 | return -ENOMEM; |
| 2109 | return 0; |
| 2110 | } |
| 2111 | EXPORT_SYMBOL(tty_port_alloc_xmit_buf); |
| 2112 | |
| 2113 | void tty_port_free_xmit_buf(struct tty_port *port) |
| 2114 | { |
| 2115 | mutex_lock(&port->mutex); |
| 2116 | if (port->xmit_buf != NULL) { |
| 2117 | free_page((unsigned long)port->xmit_buf); |
| 2118 | port->xmit_buf = NULL; |
| 2119 | } |
| 2120 | mutex_unlock(&port->mutex); |
| 2121 | } |
| 2122 | EXPORT_SYMBOL(tty_port_free_xmit_buf); |
| 2123 | |
| 2124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | static char ptychar[] = "pqrstuvwxyzabcde"; |
| 2126 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2127 | /** |
| 2128 | * pty_line_name - generate name for a pty |
| 2129 | * @driver: the tty driver in use |
| 2130 | * @index: the minor number |
| 2131 | * @p: output buffer of at least 6 bytes |
| 2132 | * |
| 2133 | * Generate a name from a driver reference and write it to the output |
| 2134 | * buffer. |
| 2135 | * |
| 2136 | * Locking: None |
| 2137 | */ |
| 2138 | static void pty_line_name(struct tty_driver *driver, int index, char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | { |
| 2140 | int i = index + driver->name_base; |
| 2141 | /* ->name is initialized to "ttyp", but "tty" is expected */ |
| 2142 | sprintf(p, "%s%c%x", |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2143 | driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, |
| 2144 | ptychar[i >> 4 & 0xf], i & 0xf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2147 | /** |
| 2148 | * pty_line_name - generate name for a tty |
| 2149 | * @driver: the tty driver in use |
| 2150 | * @index: the minor number |
| 2151 | * @p: output buffer of at least 7 bytes |
| 2152 | * |
| 2153 | * Generate a name from a driver reference and write it to the output |
| 2154 | * buffer. |
| 2155 | * |
| 2156 | * Locking: None |
| 2157 | */ |
| 2158 | static void tty_line_name(struct tty_driver *driver, int index, char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | { |
| 2160 | sprintf(p, "%s%d", driver->name, index + driver->name_base); |
| 2161 | } |
| 2162 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2163 | /** |
| 2164 | * init_dev - initialise a tty device |
| 2165 | * @driver: tty driver we are opening a device on |
| 2166 | * @idx: device index |
| 2167 | * @tty: returned tty structure |
| 2168 | * |
| 2169 | * Prepare a tty device. This may not be a "new" clean device but |
| 2170 | * could also be an active device. The pty drivers require special |
| 2171 | * handling because of this. |
| 2172 | * |
| 2173 | * Locking: |
| 2174 | * The function is called under the tty_mutex, which |
| 2175 | * protects us from the tty struct or driver itself going away. |
| 2176 | * |
| 2177 | * On exit the tty device has the line discipline attached and |
| 2178 | * a reference count of 1. If a pair was created for pty/tty use |
| 2179 | * and the other was a pty master then it too has a reference count of 1. |
| 2180 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | * WSH 06/09/97: Rewritten to remove races and properly clean up after a |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2182 | * failed open. The new code protects the open with a mutex, so it's |
| 2183 | * really quite straightforward. The mutex locking can probably be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | * relaxed for the (most common) case of reopening a tty. |
| 2185 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | static int init_dev(struct tty_driver *driver, int idx, |
| 2188 | struct tty_struct **ret_tty) |
| 2189 | { |
| 2190 | struct tty_struct *tty, *o_tty; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 2191 | struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc; |
| 2192 | struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc; |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2193 | int retval = 0; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2194 | struct tty_ldisc *ld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | |
| 2196 | /* check whether we're reopening an existing tty */ |
| 2197 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { |
| 2198 | tty = devpts_get_tty(idx); |
Aristeu Sergio Rozanski Filho | 5a39e8c | 2007-02-28 20:13:53 -0800 | [diff] [blame] | 2199 | /* |
| 2200 | * If we don't have a tty here on a slave open, it's because |
| 2201 | * the master already started the close process and there's |
| 2202 | * no relation between devpts file and tty anymore. |
| 2203 | */ |
| 2204 | if (!tty && driver->subtype == PTY_TYPE_SLAVE) { |
| 2205 | retval = -EIO; |
| 2206 | goto end_init; |
| 2207 | } |
| 2208 | /* |
| 2209 | * It's safe from now on because init_dev() is called with |
| 2210 | * tty_mutex held and release_dev() won't change tty->count |
| 2211 | * or tty->flags without having to grab tty_mutex |
| 2212 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | if (tty && driver->subtype == PTY_TYPE_MASTER) |
| 2214 | tty = tty->link; |
| 2215 | } else { |
| 2216 | tty = driver->ttys[idx]; |
| 2217 | } |
| 2218 | if (tty) goto fast_track; |
| 2219 | |
| 2220 | /* |
| 2221 | * First time open is complex, especially for PTY devices. |
| 2222 | * This code guarantees that either everything succeeds and the |
| 2223 | * TTY is ready for operation, or else the table slots are vacated |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2224 | * and the allocated memory released. (Except that the termios |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | * and locked termios may be retained.) |
| 2226 | */ |
| 2227 | |
| 2228 | if (!try_module_get(driver->owner)) { |
| 2229 | retval = -ENODEV; |
| 2230 | goto end_init; |
| 2231 | } |
| 2232 | |
| 2233 | o_tty = NULL; |
| 2234 | tp = o_tp = NULL; |
| 2235 | ltp = o_ltp = NULL; |
| 2236 | |
| 2237 | tty = alloc_tty_struct(); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2238 | if (!tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | goto fail_no_mem; |
| 2240 | initialize_tty_struct(tty); |
| 2241 | tty->driver = driver; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2242 | tty->ops = driver->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | tty->index = idx; |
| 2244 | tty_line_name(driver, idx, tty->name); |
| 2245 | |
| 2246 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { |
| 2247 | tp_loc = &tty->termios; |
| 2248 | ltp_loc = &tty->termios_locked; |
| 2249 | } else { |
| 2250 | tp_loc = &driver->termios[idx]; |
| 2251 | ltp_loc = &driver->termios_locked[idx]; |
| 2252 | } |
| 2253 | |
| 2254 | if (!*tp_loc) { |
Jesper Juhl | abcb1ff3 | 2007-08-24 02:28:42 +0200 | [diff] [blame] | 2255 | tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | if (!tp) |
| 2257 | goto free_mem_out; |
| 2258 | *tp = driver->init_termios; |
| 2259 | } |
| 2260 | |
| 2261 | if (!*ltp_loc) { |
Jean Delvare | 506eb99 | 2007-07-15 23:40:14 -0700 | [diff] [blame] | 2262 | ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | if (!ltp) |
| 2264 | goto free_mem_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | if (driver->type == TTY_DRIVER_TYPE_PTY) { |
| 2268 | o_tty = alloc_tty_struct(); |
| 2269 | if (!o_tty) |
| 2270 | goto free_mem_out; |
| 2271 | initialize_tty_struct(o_tty); |
| 2272 | o_tty->driver = driver->other; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2273 | o_tty->ops = driver->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | o_tty->index = idx; |
| 2275 | tty_line_name(driver->other, idx, o_tty->name); |
| 2276 | |
| 2277 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { |
| 2278 | o_tp_loc = &o_tty->termios; |
| 2279 | o_ltp_loc = &o_tty->termios_locked; |
| 2280 | } else { |
| 2281 | o_tp_loc = &driver->other->termios[idx]; |
| 2282 | o_ltp_loc = &driver->other->termios_locked[idx]; |
| 2283 | } |
| 2284 | |
| 2285 | if (!*o_tp_loc) { |
Jesper Juhl | abcb1ff3 | 2007-08-24 02:28:42 +0200 | [diff] [blame] | 2286 | o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | if (!o_tp) |
| 2288 | goto free_mem_out; |
| 2289 | *o_tp = driver->other->init_termios; |
| 2290 | } |
| 2291 | |
| 2292 | if (!*o_ltp_loc) { |
Jean Delvare | 506eb99 | 2007-07-15 23:40:14 -0700 | [diff] [blame] | 2293 | o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | if (!o_ltp) |
| 2295 | goto free_mem_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * Everything allocated ... set up the o_tty structure. |
| 2300 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2301 | if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | driver->other->ttys[idx] = o_tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | if (!*o_tp_loc) |
| 2304 | *o_tp_loc = o_tp; |
| 2305 | if (!*o_ltp_loc) |
| 2306 | *o_ltp_loc = o_ltp; |
| 2307 | o_tty->termios = *o_tp_loc; |
| 2308 | o_tty->termios_locked = *o_ltp_loc; |
| 2309 | driver->other->refcount++; |
| 2310 | if (driver->subtype == PTY_TYPE_MASTER) |
| 2311 | o_tty->count++; |
| 2312 | |
| 2313 | /* Establish the links in both directions */ |
| 2314 | tty->link = o_tty; |
| 2315 | o_tty->link = tty; |
| 2316 | } |
| 2317 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2318 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | * All structures have been allocated, so now we install them. |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2320 | * Failures after this point use release_tty to clean up, so |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | * there's no need to null out the local pointers. |
| 2322 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2323 | if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | driver->ttys[idx] = tty; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | if (!*tp_loc) |
| 2327 | *tp_loc = tp; |
| 2328 | if (!*ltp_loc) |
| 2329 | *ltp_loc = ltp; |
| 2330 | tty->termios = *tp_loc; |
| 2331 | tty->termios_locked = *ltp_loc; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 2332 | /* Compatibility until drivers always set this */ |
| 2333 | tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios); |
| 2334 | tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | driver->refcount++; |
| 2336 | tty->count++; |
| 2337 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2338 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | * Structures all installed ... call the ldisc open routines. |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2340 | * If we fail here just call release_tty to clean up. No need |
| 2341 | * to decrement the use counts, as release_tty doesn't care. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2343 | |
| 2344 | ld = &tty->ldisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2346 | if (ld->ops->open) { |
| 2347 | retval = (ld->ops->open)(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | if (retval) |
| 2349 | goto release_mem_out; |
| 2350 | } |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2351 | if (o_tty && o_tty->ldisc.ops->open) { |
| 2352 | retval = (o_tty->ldisc.ops->open)(o_tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | if (retval) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2354 | if (ld->ops->close) |
| 2355 | (ld->ops->close)(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | goto release_mem_out; |
| 2357 | } |
| 2358 | tty_ldisc_enable(o_tty); |
| 2359 | } |
| 2360 | tty_ldisc_enable(tty); |
| 2361 | goto success; |
| 2362 | |
| 2363 | /* |
| 2364 | * This fast open can be used if the tty is already open. |
| 2365 | * No memory is allocated, and the only failures are from |
| 2366 | * attempting to open a closing tty or attempting multiple |
| 2367 | * opens on a pty master. |
| 2368 | */ |
| 2369 | fast_track: |
| 2370 | if (test_bit(TTY_CLOSING, &tty->flags)) { |
| 2371 | retval = -EIO; |
| 2372 | goto end_init; |
| 2373 | } |
| 2374 | if (driver->type == TTY_DRIVER_TYPE_PTY && |
| 2375 | driver->subtype == PTY_TYPE_MASTER) { |
| 2376 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2377 | * special case for PTY masters: only one open permitted, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | * and the slave side open count is incremented as well. |
| 2379 | */ |
| 2380 | if (tty->count) { |
| 2381 | retval = -EIO; |
| 2382 | goto end_init; |
| 2383 | } |
| 2384 | tty->link->count++; |
| 2385 | } |
| 2386 | tty->count++; |
| 2387 | tty->driver = driver; /* N.B. why do this every time?? */ |
| 2388 | |
| 2389 | /* FIXME */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2390 | if (!test_bit(TTY_LDISC, &tty->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | printk(KERN_ERR "init_dev but no ldisc\n"); |
| 2392 | success: |
| 2393 | *ret_tty = tty; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2394 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2395 | /* All paths come through here to release the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | end_init: |
| 2397 | return retval; |
| 2398 | |
| 2399 | /* Release locally allocated memory ... nothing placed in slots */ |
| 2400 | free_mem_out: |
Jesper Juhl | 735d566 | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 2401 | kfree(o_tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | if (o_tty) |
| 2403 | free_tty_struct(o_tty); |
Jesper Juhl | 735d566 | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 2404 | kfree(ltp); |
| 2405 | kfree(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | free_tty_struct(tty); |
| 2407 | |
| 2408 | fail_no_mem: |
| 2409 | module_put(driver->owner); |
| 2410 | retval = -ENOMEM; |
| 2411 | goto end_init; |
| 2412 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2413 | /* call the tty release_tty routine to clean out this slot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | release_mem_out: |
Akinobu Mita | 4050914 | 2006-09-29 02:01:27 -0700 | [diff] [blame] | 2415 | if (printk_ratelimit()) |
| 2416 | printk(KERN_INFO "init_dev: ldisc open failed, " |
| 2417 | "clearing slot %d\n", idx); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2418 | release_tty(tty, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | goto end_init; |
| 2420 | } |
| 2421 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2422 | /** |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2423 | * release_one_tty - release tty structure memory |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2424 | * |
| 2425 | * Releases memory associated with a tty structure, and clears out the |
| 2426 | * driver table slots. This function is called when a device is no longer |
| 2427 | * in use. It also gets called when setup of a device fails. |
| 2428 | * |
| 2429 | * Locking: |
| 2430 | * tty_mutex - sometimes only |
| 2431 | * takes the file list lock internally when working on the list |
| 2432 | * of ttys that the driver keeps. |
| 2433 | * FIXME: should we require tty_mutex is held here ?? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | */ |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2435 | static void release_one_tty(struct tty_struct *tty, int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM; |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2438 | struct ktermios *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | |
| 2440 | if (!devpts) |
| 2441 | tty->driver->ttys[idx] = NULL; |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) { |
| 2444 | tp = tty->termios; |
| 2445 | if (!devpts) |
| 2446 | tty->driver->termios[idx] = NULL; |
| 2447 | kfree(tp); |
| 2448 | |
| 2449 | tp = tty->termios_locked; |
| 2450 | if (!devpts) |
| 2451 | tty->driver->termios_locked[idx] = NULL; |
| 2452 | kfree(tp); |
| 2453 | } |
| 2454 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | tty->magic = 0; |
| 2457 | tty->driver->refcount--; |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | file_list_lock(); |
| 2460 | list_del_init(&tty->tty_files); |
| 2461 | file_list_unlock(); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | free_tty_struct(tty); |
| 2464 | } |
| 2465 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2466 | /** |
| 2467 | * release_tty - release tty structure memory |
| 2468 | * |
| 2469 | * Release both @tty and a possible linked partner (think pty pair), |
| 2470 | * and decrement the refcount of the backing module. |
| 2471 | * |
| 2472 | * Locking: |
| 2473 | * tty_mutex - sometimes only |
| 2474 | * takes the file list lock internally when working on the list |
| 2475 | * of ttys that the driver keeps. |
| 2476 | * FIXME: should we require tty_mutex is held here ?? |
| 2477 | */ |
| 2478 | static void release_tty(struct tty_struct *tty, int idx) |
| 2479 | { |
| 2480 | struct tty_driver *driver = tty->driver; |
| 2481 | |
| 2482 | if (tty->link) |
| 2483 | release_one_tty(tty->link, idx); |
| 2484 | release_one_tty(tty, idx); |
| 2485 | module_put(driver->owner); |
| 2486 | } |
| 2487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | /* |
| 2489 | * Even releasing the tty structures is a tricky business.. We have |
| 2490 | * to be very careful that the structures are all released at the |
| 2491 | * same time, as interrupts might otherwise get the wrong pointers. |
| 2492 | * |
| 2493 | * WSH 09/09/97: rewritten to avoid some nasty race conditions that could |
| 2494 | * lead to double frees or releasing memory still in use. |
| 2495 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2496 | static void release_dev(struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | { |
| 2498 | struct tty_struct *tty, *o_tty; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2499 | struct tty_ldisc ld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | int pty_master, tty_closing, o_tty_closing, do_sleep; |
Paul Fulghum | 14a6283 | 2006-04-10 22:54:19 -0700 | [diff] [blame] | 2501 | int devpts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | int idx; |
| 2503 | char buf[64]; |
| 2504 | unsigned long flags; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | tty = (struct tty_struct *)filp->private_data; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2507 | if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, |
| 2508 | "release_dev")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | return; |
| 2510 | |
| 2511 | check_tty_count(tty, "release_dev"); |
| 2512 | |
| 2513 | tty_fasync(-1, filp, 0); |
| 2514 | |
| 2515 | idx = tty->index; |
| 2516 | pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 2517 | tty->driver->subtype == PTY_TYPE_MASTER); |
| 2518 | devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | o_tty = tty->link; |
| 2520 | |
| 2521 | #ifdef TTY_PARANOIA_CHECK |
| 2522 | if (idx < 0 || idx >= tty->driver->num) { |
| 2523 | printk(KERN_DEBUG "release_dev: bad idx when trying to " |
| 2524 | "free (%s)\n", tty->name); |
| 2525 | return; |
| 2526 | } |
| 2527 | if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { |
| 2528 | if (tty != tty->driver->ttys[idx]) { |
| 2529 | printk(KERN_DEBUG "release_dev: driver.table[%d] not tty " |
| 2530 | "for (%s)\n", idx, tty->name); |
| 2531 | return; |
| 2532 | } |
| 2533 | if (tty->termios != tty->driver->termios[idx]) { |
| 2534 | printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios " |
| 2535 | "for (%s)\n", |
| 2536 | idx, tty->name); |
| 2537 | return; |
| 2538 | } |
| 2539 | if (tty->termios_locked != tty->driver->termios_locked[idx]) { |
| 2540 | printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not " |
| 2541 | "termios_locked for (%s)\n", |
| 2542 | idx, tty->name); |
| 2543 | return; |
| 2544 | } |
| 2545 | } |
| 2546 | #endif |
| 2547 | |
| 2548 | #ifdef TTY_DEBUG_HANGUP |
| 2549 | printk(KERN_DEBUG "release_dev of %s (tty count=%d)...", |
| 2550 | tty_name(tty, buf), tty->count); |
| 2551 | #endif |
| 2552 | |
| 2553 | #ifdef TTY_PARANOIA_CHECK |
| 2554 | if (tty->driver->other && |
| 2555 | !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { |
| 2556 | if (o_tty != tty->driver->other->ttys[idx]) { |
| 2557 | printk(KERN_DEBUG "release_dev: other->table[%d] " |
| 2558 | "not o_tty for (%s)\n", |
| 2559 | idx, tty->name); |
| 2560 | return; |
| 2561 | } |
| 2562 | if (o_tty->termios != tty->driver->other->termios[idx]) { |
| 2563 | printk(KERN_DEBUG "release_dev: other->termios[%d] " |
| 2564 | "not o_termios for (%s)\n", |
| 2565 | idx, tty->name); |
| 2566 | return; |
| 2567 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2568 | if (o_tty->termios_locked != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | tty->driver->other->termios_locked[idx]) { |
| 2570 | printk(KERN_DEBUG "release_dev: other->termios_locked[" |
| 2571 | "%d] not o_termios_locked for (%s)\n", |
| 2572 | idx, tty->name); |
| 2573 | return; |
| 2574 | } |
| 2575 | if (o_tty->link != tty) { |
| 2576 | printk(KERN_DEBUG "release_dev: bad pty pointers\n"); |
| 2577 | return; |
| 2578 | } |
| 2579 | } |
| 2580 | #endif |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2581 | if (tty->ops->close) |
| 2582 | tty->ops->close(tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | |
| 2584 | /* |
| 2585 | * Sanity check: if tty->count is going to zero, there shouldn't be |
| 2586 | * any waiters on tty->read_wait or tty->write_wait. We test the |
| 2587 | * wait queues and kick everyone out _before_ actually starting to |
| 2588 | * close. This ensures that we won't block while releasing the tty |
| 2589 | * structure. |
| 2590 | * |
| 2591 | * The test for the o_tty closing is necessary, since the master and |
| 2592 | * slave sides may close in any order. If the slave side closes out |
| 2593 | * first, its count will be one, since the master side holds an open. |
| 2594 | * Thus this test wouldn't be triggered at the time the slave closes, |
| 2595 | * so we do it now. |
| 2596 | * |
| 2597 | * Note that it's possible for the tty to be opened again while we're |
| 2598 | * flushing out waiters. By recalculating the closing flags before |
| 2599 | * each iteration we avoid any problems. |
| 2600 | */ |
| 2601 | while (1) { |
| 2602 | /* Guard against races with tty->count changes elsewhere and |
| 2603 | opens on /dev/tty */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2604 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2605 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | tty_closing = tty->count <= 1; |
| 2607 | o_tty_closing = o_tty && |
| 2608 | (o_tty->count <= (pty_master ? 1 : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | do_sleep = 0; |
| 2610 | |
| 2611 | if (tty_closing) { |
| 2612 | if (waitqueue_active(&tty->read_wait)) { |
| 2613 | wake_up(&tty->read_wait); |
| 2614 | do_sleep++; |
| 2615 | } |
| 2616 | if (waitqueue_active(&tty->write_wait)) { |
| 2617 | wake_up(&tty->write_wait); |
| 2618 | do_sleep++; |
| 2619 | } |
| 2620 | } |
| 2621 | if (o_tty_closing) { |
| 2622 | if (waitqueue_active(&o_tty->read_wait)) { |
| 2623 | wake_up(&o_tty->read_wait); |
| 2624 | do_sleep++; |
| 2625 | } |
| 2626 | if (waitqueue_active(&o_tty->write_wait)) { |
| 2627 | wake_up(&o_tty->write_wait); |
| 2628 | do_sleep++; |
| 2629 | } |
| 2630 | } |
| 2631 | if (!do_sleep) |
| 2632 | break; |
| 2633 | |
| 2634 | printk(KERN_WARNING "release_dev: %s: read/write wait queue " |
| 2635 | "active!\n", tty_name(tty, buf)); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2636 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | schedule(); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | |
| 2640 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2641 | * The closing flags are now consistent with the open counts on |
| 2642 | * both sides, and we've completed the last operation that could |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | * block, so it's safe to proceed with closing. |
| 2644 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | if (pty_master) { |
| 2646 | if (--o_tty->count < 0) { |
| 2647 | printk(KERN_WARNING "release_dev: bad pty slave count " |
| 2648 | "(%d) for %s\n", |
| 2649 | o_tty->count, tty_name(o_tty, buf)); |
| 2650 | o_tty->count = 0; |
| 2651 | } |
| 2652 | } |
| 2653 | if (--tty->count < 0) { |
| 2654 | printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n", |
| 2655 | tty->count, tty_name(tty, buf)); |
| 2656 | tty->count = 0; |
| 2657 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | /* |
| 2660 | * We've decremented tty->count, so we need to remove this file |
| 2661 | * descriptor off the tty->tty_files list; this serves two |
| 2662 | * purposes: |
| 2663 | * - check_tty_count sees the correct number of file descriptors |
| 2664 | * associated with this tty. |
| 2665 | * - do_tty_hangup no longer sees this file descriptor as |
| 2666 | * something that needs to be handled for hangups. |
| 2667 | */ |
| 2668 | file_kill(filp); |
| 2669 | filp->private_data = NULL; |
| 2670 | |
| 2671 | /* |
| 2672 | * Perform some housekeeping before deciding whether to return. |
| 2673 | * |
| 2674 | * Set the TTY_CLOSING flag if this was the last open. In the |
| 2675 | * case of a pty we may have to wait around for the other side |
| 2676 | * to close, and TTY_CLOSING makes sure we can't be reopened. |
| 2677 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2678 | if (tty_closing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | set_bit(TTY_CLOSING, &tty->flags); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2680 | if (o_tty_closing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | set_bit(TTY_CLOSING, &o_tty->flags); |
| 2682 | |
| 2683 | /* |
| 2684 | * If _either_ side is closing, make sure there aren't any |
| 2685 | * processes that still think tty or o_tty is their controlling |
| 2686 | * tty. |
| 2687 | */ |
| 2688 | if (tty_closing || o_tty_closing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | read_lock(&tasklist_lock); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2690 | session_clear_tty(tty->session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | if (o_tty) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2692 | session_clear_tty(o_tty->session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | read_unlock(&tasklist_lock); |
| 2694 | } |
| 2695 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2696 | mutex_unlock(&tty_mutex); |
Paul Fulghum | da96582 | 2006-02-14 13:53:00 -0800 | [diff] [blame] | 2697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | /* check whether both sides are closing ... */ |
| 2699 | if (!tty_closing || (o_tty && !o_tty_closing)) |
| 2700 | return; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | #ifdef TTY_DEBUG_HANGUP |
| 2703 | printk(KERN_DEBUG "freeing tty structure..."); |
| 2704 | #endif |
| 2705 | /* |
| 2706 | * Prevent flush_to_ldisc() from rescheduling the work for later. Then |
| 2707 | * kill any delayed work. As this is the final close it does not |
| 2708 | * race with the set_ldisc code path. |
| 2709 | */ |
| 2710 | clear_bit(TTY_LDISC, &tty->flags); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 2711 | cancel_delayed_work(&tty->buf.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | |
| 2713 | /* |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 2714 | * Wait for ->hangup_work and ->buf.work handlers to terminate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | flush_scheduled_work(); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | /* |
| 2720 | * Wait for any short term users (we know they are just driver |
| 2721 | * side waiters as the file is closing so user count on the file |
| 2722 | * side is zero. |
| 2723 | */ |
| 2724 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2725 | while (tty->ldisc.refcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
| 2727 | wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0); |
| 2728 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
| 2729 | } |
| 2730 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
| 2731 | /* |
| 2732 | * Shutdown the current line discipline, and reset it to N_TTY. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | * |
| 2734 | * FIXME: this MUST get fixed for the new reflocking |
| 2735 | */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2736 | if (tty->ldisc.ops->close) |
| 2737 | (tty->ldisc.ops->close)(tty); |
| 2738 | tty_ldisc_put(tty->ldisc.ops); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | /* |
| 2741 | * Switch the line discipline back |
| 2742 | */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2743 | WARN_ON(tty_ldisc_get(N_TTY, &ld)); |
| 2744 | tty_ldisc_assign(tty, &ld); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2745 | tty_set_termios_ldisc(tty, N_TTY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | if (o_tty) { |
| 2747 | /* FIXME: could o_tty be in setldisc here ? */ |
| 2748 | clear_bit(TTY_LDISC, &o_tty->flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2749 | if (o_tty->ldisc.ops->close) |
| 2750 | (o_tty->ldisc.ops->close)(o_tty); |
| 2751 | tty_ldisc_put(o_tty->ldisc.ops); |
| 2752 | WARN_ON(tty_ldisc_get(N_TTY, &ld)); |
| 2753 | tty_ldisc_assign(o_tty, &ld); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2754 | tty_set_termios_ldisc(o_tty, N_TTY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | } |
| 2756 | /* |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2757 | * The release_tty function takes care of the details of clearing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | * the slots and preserving the termios structure. |
| 2759 | */ |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 2760 | release_tty(tty, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | /* Make this pty number available for reallocation */ |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 2763 | if (devpts) |
| 2764 | devpts_kill_index(idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | } |
| 2766 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2767 | /** |
| 2768 | * tty_open - open a tty device |
| 2769 | * @inode: inode of device file |
| 2770 | * @filp: file pointer to tty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2772 | * tty_open and tty_release keep up the tty count that contains the |
| 2773 | * number of opens done on a tty. We cannot use the inode-count, as |
| 2774 | * different inodes might point to the same tty. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2776 | * Open-counting is needed for pty masters, as well as for keeping |
| 2777 | * track of serial lines: DTR is dropped when the last close happens. |
| 2778 | * (This is not done solely through tty->count, now. - Ted 1/27/92) |
| 2779 | * |
| 2780 | * The termios state of a pty is reset on first open so that |
| 2781 | * settings don't persist across reuse. |
| 2782 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2783 | * Locking: tty_mutex protects tty, get_tty_driver and init_dev work. |
| 2784 | * tty->count should protect the rest. |
| 2785 | * ->siglock protects ->signal/->sighand |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2787 | |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 2788 | static int __tty_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | { |
| 2790 | struct tty_struct *tty; |
| 2791 | int noctty, retval; |
| 2792 | struct tty_driver *driver; |
| 2793 | int index; |
| 2794 | dev_t device = inode->i_rdev; |
| 2795 | unsigned short saved_flags = filp->f_flags; |
| 2796 | |
| 2797 | nonseekable_open(inode, filp); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | retry_open: |
| 2800 | noctty = filp->f_flags & O_NOCTTY; |
| 2801 | index = -1; |
| 2802 | retval = 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2803 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2804 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2806 | if (device == MKDEV(TTYAUX_MAJOR, 0)) { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2807 | tty = get_current_tty(); |
| 2808 | if (!tty) { |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2809 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | return -ENXIO; |
| 2811 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2812 | driver = tty->driver; |
| 2813 | index = tty->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ |
| 2815 | /* noctty = 1; */ |
| 2816 | goto got_driver; |
| 2817 | } |
| 2818 | #ifdef CONFIG_VT |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2819 | if (device == MKDEV(TTY_MAJOR, 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | extern struct tty_driver *console_driver; |
| 2821 | driver = console_driver; |
| 2822 | index = fg_console; |
| 2823 | noctty = 1; |
| 2824 | goto got_driver; |
| 2825 | } |
| 2826 | #endif |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2827 | if (device == MKDEV(TTYAUX_MAJOR, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | driver = console_device(&index); |
| 2829 | if (driver) { |
| 2830 | /* Don't let /dev/console block */ |
| 2831 | filp->f_flags |= O_NONBLOCK; |
| 2832 | noctty = 1; |
| 2833 | goto got_driver; |
| 2834 | } |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2835 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | return -ENODEV; |
| 2837 | } |
| 2838 | |
| 2839 | driver = get_tty_driver(device, &index); |
| 2840 | if (!driver) { |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2841 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | return -ENODEV; |
| 2843 | } |
| 2844 | got_driver: |
| 2845 | retval = init_dev(driver, index, &tty); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2846 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | if (retval) |
| 2848 | return retval; |
| 2849 | |
| 2850 | filp->private_data = tty; |
| 2851 | file_move(filp, &tty->tty_files); |
| 2852 | check_tty_count(tty, "tty_open"); |
| 2853 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 2854 | tty->driver->subtype == PTY_TYPE_MASTER) |
| 2855 | noctty = 1; |
| 2856 | #ifdef TTY_DEBUG_HANGUP |
| 2857 | printk(KERN_DEBUG "opening %s...", tty->name); |
| 2858 | #endif |
| 2859 | if (!retval) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2860 | if (tty->ops->open) |
| 2861 | retval = tty->ops->open(tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | else |
| 2863 | retval = -ENODEV; |
| 2864 | } |
| 2865 | filp->f_flags = saved_flags; |
| 2866 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2867 | if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && |
| 2868 | !capable(CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | retval = -EBUSY; |
| 2870 | |
| 2871 | if (retval) { |
| 2872 | #ifdef TTY_DEBUG_HANGUP |
| 2873 | printk(KERN_DEBUG "error %d in opening %s...", retval, |
| 2874 | tty->name); |
| 2875 | #endif |
| 2876 | release_dev(filp); |
| 2877 | if (retval != -ERESTARTSYS) |
| 2878 | return retval; |
| 2879 | if (signal_pending(current)) |
| 2880 | return retval; |
| 2881 | schedule(); |
| 2882 | /* |
| 2883 | * Need to reset f_op in case a hangup happened. |
| 2884 | */ |
| 2885 | if (filp->f_op == &hung_up_tty_fops) |
| 2886 | filp->f_op = &tty_fops; |
| 2887 | goto retry_open; |
| 2888 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2889 | |
| 2890 | mutex_lock(&tty_mutex); |
| 2891 | spin_lock_irq(¤t->sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | if (!noctty && |
| 2893 | current->signal->leader && |
| 2894 | !current->signal->tty && |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2895 | tty->session == NULL) |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 2896 | __proc_set_tty(current, tty); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2897 | spin_unlock_irq(¤t->sighand->siglock); |
| 2898 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | return 0; |
| 2900 | } |
| 2901 | |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 2902 | /* BKL pushdown: scary code avoidance wrapper */ |
| 2903 | static int tty_open(struct inode *inode, struct file *filp) |
| 2904 | { |
| 2905 | int ret; |
| 2906 | |
| 2907 | lock_kernel(); |
| 2908 | ret = __tty_open(inode, filp); |
| 2909 | unlock_kernel(); |
| 2910 | return ret; |
| 2911 | } |
| 2912 | |
| 2913 | |
| 2914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | #ifdef CONFIG_UNIX98_PTYS |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2916 | /** |
| 2917 | * ptmx_open - open a unix 98 pty master |
| 2918 | * @inode: inode of device file |
| 2919 | * @filp: file pointer to tty |
| 2920 | * |
| 2921 | * Allocate a unix98 pty master device from the ptmx driver. |
| 2922 | * |
| 2923 | * Locking: tty_mutex protects theinit_dev work. tty->count should |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2924 | * protect the rest. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2925 | * allocated_ptys_lock handles the list of free pty numbers |
| 2926 | */ |
| 2927 | |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 2928 | static int __ptmx_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | { |
| 2930 | struct tty_struct *tty; |
| 2931 | int retval; |
| 2932 | int index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | |
| 2934 | nonseekable_open(inode, filp); |
| 2935 | |
| 2936 | /* find a device that is not in use. */ |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 2937 | index = devpts_new_index(); |
| 2938 | if (index < 0) |
| 2939 | return index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2941 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | retval = init_dev(ptm_driver, index, &tty); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2943 | mutex_unlock(&tty_mutex); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | if (retval) |
| 2946 | goto out; |
| 2947 | |
| 2948 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ |
| 2949 | filp->private_data = tty; |
| 2950 | file_move(filp, &tty->tty_files); |
| 2951 | |
Sukadev Bhattiprolu | 4f8f9d6 | 2008-04-30 00:54:20 -0700 | [diff] [blame] | 2952 | retval = devpts_pty_new(tty->link); |
| 2953 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | goto out1; |
| 2955 | |
Hiroshi Shimamoto | 86a9653 | 2008-04-30 00:54:20 -0700 | [diff] [blame] | 2956 | check_tty_count(tty, "ptmx_open"); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2957 | retval = ptm_driver->ops->open(tty, filp); |
Miloslav Trmac | 4112622 | 2008-04-18 13:30:14 -0700 | [diff] [blame] | 2958 | if (!retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | return 0; |
| 2960 | out1: |
| 2961 | release_dev(filp); |
Paul Fulghum | 9453a5a | 2006-04-10 22:54:18 -0700 | [diff] [blame] | 2962 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | out: |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 2964 | devpts_kill_index(index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | return retval; |
| 2966 | } |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 2967 | |
| 2968 | static int ptmx_open(struct inode *inode, struct file *filp) |
| 2969 | { |
| 2970 | int ret; |
| 2971 | |
| 2972 | lock_kernel(); |
| 2973 | ret = __ptmx_open(inode, filp); |
| 2974 | unlock_kernel(); |
| 2975 | return ret; |
| 2976 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | #endif |
| 2978 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2979 | /** |
| 2980 | * tty_release - vfs callback for close |
| 2981 | * @inode: inode of tty |
| 2982 | * @filp: file pointer for handle to tty |
| 2983 | * |
| 2984 | * Called the last time each file handle is closed that references |
| 2985 | * this tty. There may however be several such references. |
| 2986 | * |
| 2987 | * Locking: |
| 2988 | * Takes bkl. See release_dev |
| 2989 | */ |
| 2990 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2991 | static int tty_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | { |
| 2993 | lock_kernel(); |
| 2994 | release_dev(filp); |
| 2995 | unlock_kernel(); |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2999 | /** |
| 3000 | * tty_poll - check tty status |
| 3001 | * @filp: file being polled |
| 3002 | * @wait: poll wait structures to update |
| 3003 | * |
| 3004 | * Call the line discipline polling method to obtain the poll |
| 3005 | * status of the device. |
| 3006 | * |
| 3007 | * Locking: locks called line discipline but ldisc poll method |
| 3008 | * may be re-entered freely by other callers. |
| 3009 | */ |
| 3010 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3011 | static unsigned int tty_poll(struct file *filp, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3013 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | struct tty_ldisc *ld; |
| 3015 | int ret = 0; |
| 3016 | |
| 3017 | tty = (struct tty_struct *)filp->private_data; |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 3018 | if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | return 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3022 | if (ld->ops->poll) |
| 3023 | ret = (ld->ops->poll)(tty, filp, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | tty_ldisc_deref(ld); |
| 3025 | return ret; |
| 3026 | } |
| 3027 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3028 | static int tty_fasync(int fd, struct file *filp, int on) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3030 | struct tty_struct *tty; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3031 | unsigned long flags; |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 3032 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 3034 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | tty = (struct tty_struct *)filp->private_data; |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 3036 | if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync")) |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 3037 | goto out; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | retval = fasync_helper(fd, filp, on, &tty->fasync); |
| 3040 | if (retval <= 0) |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 3041 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | |
| 3043 | if (on) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3044 | enum pid_type type; |
| 3045 | struct pid *pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | if (!waitqueue_active(&tty->read_wait)) |
| 3047 | tty->minimum_to_wake = 1; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3048 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3049 | if (tty->pgrp) { |
| 3050 | pid = tty->pgrp; |
| 3051 | type = PIDTYPE_PGID; |
| 3052 | } else { |
| 3053 | pid = task_pid(current); |
| 3054 | type = PIDTYPE_PID; |
| 3055 | } |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3056 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3057 | retval = __f_setown(filp, pid, type, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | if (retval) |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 3059 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | } else { |
| 3061 | if (!tty->fasync && !waitqueue_active(&tty->read_wait)) |
| 3062 | tty->minimum_to_wake = N_TTY_BUF_SIZE; |
| 3063 | } |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 3064 | retval = 0; |
| 3065 | out: |
| 3066 | unlock_kernel(); |
| 3067 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | } |
| 3069 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3070 | /** |
| 3071 | * tiocsti - fake input character |
| 3072 | * @tty: tty to fake input into |
| 3073 | * @p: pointer to character |
| 3074 | * |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 3075 | * Fake input to a tty device. Does the necessary locking and |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3076 | * input management. |
| 3077 | * |
| 3078 | * FIXME: does not honour flow control ?? |
| 3079 | * |
| 3080 | * Locking: |
| 3081 | * Called functions take tty_ldisc_lock |
| 3082 | * current->signal->tty check is safe without locks |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 3083 | * |
| 3084 | * FIXME: may race normal receive processing |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3085 | */ |
| 3086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | static int tiocsti(struct tty_struct *tty, char __user *p) |
| 3088 | { |
| 3089 | char ch, mbz = 0; |
| 3090 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) |
| 3093 | return -EPERM; |
| 3094 | if (get_user(ch, p)) |
| 3095 | return -EFAULT; |
| 3096 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3097 | ld->ops->receive_buf(tty, &ch, &mbz, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | tty_ldisc_deref(ld); |
| 3099 | return 0; |
| 3100 | } |
| 3101 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3102 | /** |
| 3103 | * tiocgwinsz - implement window query ioctl |
| 3104 | * @tty; tty |
| 3105 | * @arg: user buffer for result |
| 3106 | * |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 3107 | * Copies the kernel idea of the window size into the user buffer. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3108 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3109 | * Locking: tty->termios_mutex is taken to ensure the winsize data |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 3110 | * is consistent. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3111 | */ |
| 3112 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3113 | static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | { |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 3115 | int err; |
| 3116 | |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 3117 | mutex_lock(&tty->termios_mutex); |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 3118 | err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 3119 | mutex_unlock(&tty->termios_mutex); |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 3120 | |
| 3121 | return err ? -EFAULT: 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3124 | /** |
| 3125 | * tiocswinsz - implement window size set ioctl |
| 3126 | * @tty; tty |
| 3127 | * @arg: user buffer for result |
| 3128 | * |
| 3129 | * Copies the user idea of the window size to the kernel. Traditionally |
| 3130 | * this is just advisory information but for the Linux console it |
| 3131 | * actually has driver level meaning and triggers a VC resize. |
| 3132 | * |
| 3133 | * Locking: |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 3134 | * Called function use the console_sem is used to ensure we do |
| 3135 | * not try and resize the console twice at once. |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3136 | * The tty->termios_mutex is used to ensure we don't double |
| 3137 | * resize and get confused. Lock order - tty->termios_mutex before |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 3138 | * console sem |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3139 | */ |
| 3140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty, |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3142 | struct winsize __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | { |
| 3144 | struct winsize tmp_ws; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3145 | struct pid *pgrp, *rpgrp; |
| 3146 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | |
| 3148 | if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) |
| 3149 | return -EFAULT; |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 3150 | |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 3151 | mutex_lock(&tty->termios_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg))) |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 3153 | goto done; |
| 3154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | #ifdef CONFIG_VT |
| 3156 | if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) { |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 3157 | if (vc_lock_resize(tty->driver_data, tmp_ws.ws_col, |
| 3158 | tmp_ws.ws_row)) { |
| 3159 | mutex_unlock(&tty->termios_mutex); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3160 | return -ENXIO; |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 3161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | } |
| 3163 | #endif |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3164 | /* Get the PID values and reference them so we can |
| 3165 | avoid holding the tty ctrl lock while sending signals */ |
| 3166 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 3167 | pgrp = get_pid(tty->pgrp); |
| 3168 | rpgrp = get_pid(real_tty->pgrp); |
| 3169 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 3170 | |
| 3171 | if (pgrp) |
| 3172 | kill_pgrp(pgrp, SIGWINCH, 1); |
| 3173 | if (rpgrp != pgrp && rpgrp) |
| 3174 | kill_pgrp(rpgrp, SIGWINCH, 1); |
| 3175 | |
| 3176 | put_pid(pgrp); |
| 3177 | put_pid(rpgrp); |
| 3178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | tty->winsize = tmp_ws; |
| 3180 | real_tty->winsize = tmp_ws; |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 3181 | done: |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 3182 | mutex_unlock(&tty->termios_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | return 0; |
| 3184 | } |
| 3185 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3186 | /** |
| 3187 | * tioccons - allow admin to move logical console |
| 3188 | * @file: the file to become console |
| 3189 | * |
| 3190 | * Allow the adminstrator to move the redirected console device |
| 3191 | * |
| 3192 | * Locking: uses redirect_lock to guard the redirect information |
| 3193 | */ |
| 3194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | static int tioccons(struct file *file) |
| 3196 | { |
| 3197 | if (!capable(CAP_SYS_ADMIN)) |
| 3198 | return -EPERM; |
| 3199 | if (file->f_op->write == redirected_tty_write) { |
| 3200 | struct file *f; |
| 3201 | spin_lock(&redirect_lock); |
| 3202 | f = redirect; |
| 3203 | redirect = NULL; |
| 3204 | spin_unlock(&redirect_lock); |
| 3205 | if (f) |
| 3206 | fput(f); |
| 3207 | return 0; |
| 3208 | } |
| 3209 | spin_lock(&redirect_lock); |
| 3210 | if (redirect) { |
| 3211 | spin_unlock(&redirect_lock); |
| 3212 | return -EBUSY; |
| 3213 | } |
| 3214 | get_file(file); |
| 3215 | redirect = file; |
| 3216 | spin_unlock(&redirect_lock); |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3220 | /** |
| 3221 | * fionbio - non blocking ioctl |
| 3222 | * @file: file to set blocking value |
| 3223 | * @p: user parameter |
| 3224 | * |
| 3225 | * Historical tty interfaces had a blocking control ioctl before |
| 3226 | * the generic functionality existed. This piece of history is preserved |
| 3227 | * in the expected tty API of posix OS's. |
| 3228 | * |
| 3229 | * Locking: none, the open fle handle ensures it won't go away. |
| 3230 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | |
| 3232 | static int fionbio(struct file *file, int __user *p) |
| 3233 | { |
| 3234 | int nonblock; |
| 3235 | |
| 3236 | if (get_user(nonblock, p)) |
| 3237 | return -EFAULT; |
| 3238 | |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3239 | /* file->f_flags is still BKL protected in the fs layer - vomit */ |
| 3240 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | if (nonblock) |
| 3242 | file->f_flags |= O_NONBLOCK; |
| 3243 | else |
| 3244 | file->f_flags &= ~O_NONBLOCK; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3245 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | return 0; |
| 3247 | } |
| 3248 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3249 | /** |
| 3250 | * tiocsctty - set controlling tty |
| 3251 | * @tty: tty structure |
| 3252 | * @arg: user argument |
| 3253 | * |
| 3254 | * This ioctl is used to manage job control. It permits a session |
| 3255 | * leader to set this tty as the controlling tty for the session. |
| 3256 | * |
| 3257 | * Locking: |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 3258 | * Takes tty_mutex() to protect tty instance |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3259 | * Takes tasklist_lock internally to walk sessions |
| 3260 | * Takes ->siglock() when updating signal->tty |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3261 | */ |
| 3262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | static int tiocsctty(struct tty_struct *tty, int arg) |
| 3264 | { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3265 | int ret = 0; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3266 | if (current->signal->leader && (task_session(current) == tty->session)) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3267 | return ret; |
| 3268 | |
| 3269 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | /* |
| 3271 | * The process must be a session leader and |
| 3272 | * not have a controlling tty already. |
| 3273 | */ |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3274 | if (!current->signal->leader || current->signal->tty) { |
| 3275 | ret = -EPERM; |
| 3276 | goto unlock; |
| 3277 | } |
| 3278 | |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3279 | if (tty->session) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | /* |
| 3281 | * This tty is already the controlling |
| 3282 | * tty for another session group! |
| 3283 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3284 | if (arg == 1 && capable(CAP_SYS_ADMIN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | /* |
| 3286 | * Steal it away |
| 3287 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | read_lock(&tasklist_lock); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3289 | session_clear_tty(tty->session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | read_unlock(&tasklist_lock); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3291 | } else { |
| 3292 | ret = -EPERM; |
| 3293 | goto unlock; |
| 3294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3296 | proc_set_tty(current, tty); |
| 3297 | unlock: |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 3298 | mutex_unlock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3299 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3302 | /** |
Alan Cox | 5d0fdf1 | 2008-04-30 00:53:31 -0700 | [diff] [blame] | 3303 | * tty_get_pgrp - return a ref counted pgrp pid |
| 3304 | * @tty: tty to read |
| 3305 | * |
| 3306 | * Returns a refcounted instance of the pid struct for the process |
| 3307 | * group controlling the tty. |
| 3308 | */ |
| 3309 | |
| 3310 | struct pid *tty_get_pgrp(struct tty_struct *tty) |
| 3311 | { |
| 3312 | unsigned long flags; |
| 3313 | struct pid *pgrp; |
| 3314 | |
| 3315 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 3316 | pgrp = get_pid(tty->pgrp); |
| 3317 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 3318 | |
| 3319 | return pgrp; |
| 3320 | } |
| 3321 | EXPORT_SYMBOL_GPL(tty_get_pgrp); |
| 3322 | |
| 3323 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3324 | * tiocgpgrp - get process group |
| 3325 | * @tty: tty passed by user |
| 3326 | * @real_tty: tty side of the tty pased by the user if a pty else the tty |
| 3327 | * @p: returned pid |
| 3328 | * |
| 3329 | * Obtain the process group of the tty. If there is no process group |
| 3330 | * return an error. |
| 3331 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3332 | * Locking: none. Reference to current->signal->tty is safe. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3333 | */ |
| 3334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
| 3336 | { |
Alan Cox | 5d0fdf1 | 2008-04-30 00:53:31 -0700 | [diff] [blame] | 3337 | struct pid *pid; |
| 3338 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | /* |
| 3340 | * (tty == real_tty) is a cheap way of |
| 3341 | * testing if the tty is NOT a master pty. |
| 3342 | */ |
| 3343 | if (tty == real_tty && current->signal->tty != real_tty) |
| 3344 | return -ENOTTY; |
Alan Cox | 5d0fdf1 | 2008-04-30 00:53:31 -0700 | [diff] [blame] | 3345 | pid = tty_get_pgrp(real_tty); |
| 3346 | ret = put_user(pid_vnr(pid), p); |
| 3347 | put_pid(pid); |
| 3348 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3351 | /** |
| 3352 | * tiocspgrp - attempt to set process group |
| 3353 | * @tty: tty passed by user |
| 3354 | * @real_tty: tty side device matching tty passed by user |
| 3355 | * @p: pid pointer |
| 3356 | * |
| 3357 | * Set the process group of the tty to the session passed. Only |
| 3358 | * permitted where the tty session is our session. |
| 3359 | * |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3360 | * Locking: RCU, ctrl lock |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3361 | */ |
| 3362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
| 3364 | { |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 3365 | struct pid *pgrp; |
| 3366 | pid_t pgrp_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | int retval = tty_check_change(real_tty); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3368 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | |
| 3370 | if (retval == -EIO) |
| 3371 | return -ENOTTY; |
| 3372 | if (retval) |
| 3373 | return retval; |
| 3374 | if (!current->signal->tty || |
| 3375 | (current->signal->tty != real_tty) || |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3376 | (real_tty->session != task_session(current))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | return -ENOTTY; |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 3378 | if (get_user(pgrp_nr, p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | return -EFAULT; |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 3380 | if (pgrp_nr < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | return -EINVAL; |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 3382 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3383 | pgrp = find_vpid(pgrp_nr); |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 3384 | retval = -ESRCH; |
| 3385 | if (!pgrp) |
| 3386 | goto out_unlock; |
| 3387 | retval = -EPERM; |
| 3388 | if (session_of_pgrp(pgrp) != task_session(current)) |
| 3389 | goto out_unlock; |
| 3390 | retval = 0; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3391 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3392 | put_pid(real_tty->pgrp); |
| 3393 | real_tty->pgrp = get_pid(pgrp); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3394 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 3395 | out_unlock: |
| 3396 | rcu_read_unlock(); |
| 3397 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | } |
| 3399 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3400 | /** |
| 3401 | * tiocgsid - get session id |
| 3402 | * @tty: tty passed by user |
| 3403 | * @real_tty: tty side of the tty pased by the user if a pty else the tty |
| 3404 | * @p: pointer to returned session id |
| 3405 | * |
| 3406 | * Obtain the session id of the tty. If there is no session |
| 3407 | * return an error. |
| 3408 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3409 | * Locking: none. Reference to current->signal->tty is safe. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3410 | */ |
| 3411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
| 3413 | { |
| 3414 | /* |
| 3415 | * (tty == real_tty) is a cheap way of |
| 3416 | * testing if the tty is NOT a master pty. |
| 3417 | */ |
| 3418 | if (tty == real_tty && current->signal->tty != real_tty) |
| 3419 | return -ENOTTY; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3420 | if (!real_tty->session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3421 | return -ENOTTY; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3422 | return put_user(pid_vnr(real_tty->session), p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | } |
| 3424 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3425 | /** |
| 3426 | * tiocsetd - set line discipline |
| 3427 | * @tty: tty device |
| 3428 | * @p: pointer to user data |
| 3429 | * |
| 3430 | * Set the line discipline according to user request. |
| 3431 | * |
| 3432 | * Locking: see tty_set_ldisc, this function is just a helper |
| 3433 | */ |
| 3434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | static int tiocsetd(struct tty_struct *tty, int __user *p) |
| 3436 | { |
| 3437 | int ldisc; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3438 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | |
| 3440 | if (get_user(ldisc, p)) |
| 3441 | return -EFAULT; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3442 | |
| 3443 | lock_kernel(); |
| 3444 | ret = tty_set_ldisc(tty, ldisc); |
| 3445 | unlock_kernel(); |
| 3446 | |
| 3447 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | } |
| 3449 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3450 | /** |
| 3451 | * send_break - performed time break |
| 3452 | * @tty: device to break on |
| 3453 | * @duration: timeout in mS |
| 3454 | * |
| 3455 | * Perform a timed break on hardware that lacks its own driver level |
| 3456 | * timed break functionality. |
| 3457 | * |
| 3458 | * Locking: |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 3459 | * atomic_write_lock serializes |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3460 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3461 | */ |
| 3462 | |
Domen Puncer | b20f3ae | 2005-06-25 14:58:42 -0700 | [diff] [blame] | 3463 | static int send_break(struct tty_struct *tty, unsigned int duration) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | { |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 3465 | if (tty_write_lock(tty, 0) < 0) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3466 | return -EINTR; |
| 3467 | tty->ops->break_ctl(tty, -1); |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 3468 | if (!signal_pending(current)) |
Domen Puncer | b20f3ae | 2005-06-25 14:58:42 -0700 | [diff] [blame] | 3469 | msleep_interruptible(duration); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3470 | tty->ops->break_ctl(tty, 0); |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 3471 | tty_write_unlock(tty); |
Alan Cox | 3e2a078 | 2008-06-30 17:40:08 +0100 | [diff] [blame] | 3472 | if (signal_pending(current)) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3473 | return -EINTR; |
| 3474 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | } |
| 3476 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3477 | /** |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3478 | * tty_tiocmget - get modem status |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3479 | * @tty: tty device |
| 3480 | * @file: user file pointer |
| 3481 | * @p: pointer to result |
| 3482 | * |
| 3483 | * Obtain the modem status bits from the tty driver if the feature |
| 3484 | * is supported. Return -EINVAL if it is not available. |
| 3485 | * |
| 3486 | * Locking: none (up to the driver) |
| 3487 | */ |
| 3488 | |
| 3489 | static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | { |
| 3491 | int retval = -EINVAL; |
| 3492 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3493 | if (tty->ops->tiocmget) { |
| 3494 | retval = tty->ops->tiocmget(tty, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | |
| 3496 | if (retval >= 0) |
| 3497 | retval = put_user(retval, p); |
| 3498 | } |
| 3499 | return retval; |
| 3500 | } |
| 3501 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3502 | /** |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3503 | * tty_tiocmset - set modem status |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3504 | * @tty: tty device |
| 3505 | * @file: user file pointer |
| 3506 | * @cmd: command - clear bits, set bits or set all |
| 3507 | * @p: pointer to desired bits |
| 3508 | * |
| 3509 | * Set the modem status bits from the tty driver if the feature |
| 3510 | * is supported. Return -EINVAL if it is not available. |
| 3511 | * |
| 3512 | * Locking: none (up to the driver) |
| 3513 | */ |
| 3514 | |
| 3515 | static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | unsigned __user *p) |
| 3517 | { |
| 3518 | int retval = -EINVAL; |
| 3519 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3520 | if (tty->ops->tiocmset) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | unsigned int set, clear, val; |
| 3522 | |
| 3523 | retval = get_user(val, p); |
| 3524 | if (retval) |
| 3525 | return retval; |
| 3526 | |
| 3527 | set = clear = 0; |
| 3528 | switch (cmd) { |
| 3529 | case TIOCMBIS: |
| 3530 | set = val; |
| 3531 | break; |
| 3532 | case TIOCMBIC: |
| 3533 | clear = val; |
| 3534 | break; |
| 3535 | case TIOCMSET: |
| 3536 | set = val; |
| 3537 | clear = ~val; |
| 3538 | break; |
| 3539 | } |
| 3540 | |
| 3541 | set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; |
| 3542 | clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; |
| 3543 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3544 | retval = tty->ops->tiocmset(tty, file, set, clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | } |
| 3546 | return retval; |
| 3547 | } |
| 3548 | |
| 3549 | /* |
| 3550 | * Split this up, as gcc can choke on it otherwise.. |
| 3551 | */ |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3552 | long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | { |
| 3554 | struct tty_struct *tty, *real_tty; |
| 3555 | void __user *p = (void __user *)arg; |
| 3556 | int retval; |
| 3557 | struct tty_ldisc *ld; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3558 | struct inode *inode = file->f_dentry->d_inode; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | tty = (struct tty_struct *)file->private_data; |
| 3561 | if (tty_paranoia_check(tty, inode, "tty_ioctl")) |
| 3562 | return -EINVAL; |
| 3563 | |
| 3564 | real_tty = tty; |
| 3565 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 3566 | tty->driver->subtype == PTY_TYPE_MASTER) |
| 3567 | real_tty = tty->link; |
| 3568 | |
| 3569 | /* |
| 3570 | * Break handling by driver |
| 3571 | */ |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3572 | |
| 3573 | retval = -EINVAL; |
| 3574 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3575 | if (!tty->ops->break_ctl) { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3576 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | case TIOCSBRK: |
| 3578 | case TIOCCBRK: |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3579 | if (tty->ops->ioctl) |
| 3580 | retval = tty->ops->ioctl(tty, file, cmd, arg); |
| 3581 | if (retval != -EINVAL && retval != -ENOIOCTLCMD) |
| 3582 | printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3583 | return retval; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | /* These two ioctl's always return success; even if */ |
| 3586 | /* the driver doesn't support them. */ |
| 3587 | case TCSBRK: |
| 3588 | case TCSBRKP: |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3589 | if (!tty->ops->ioctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | return 0; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3591 | retval = tty->ops->ioctl(tty, file, cmd, arg); |
| 3592 | if (retval != -EINVAL && retval != -ENOIOCTLCMD) |
| 3593 | printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | if (retval == -ENOIOCTLCMD) |
| 3595 | retval = 0; |
| 3596 | return retval; |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | /* |
| 3601 | * Factor out some common prep work |
| 3602 | */ |
| 3603 | switch (cmd) { |
| 3604 | case TIOCSETD: |
| 3605 | case TIOCSBRK: |
| 3606 | case TIOCCBRK: |
| 3607 | case TCSBRK: |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3608 | case TCSBRKP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | retval = tty_check_change(tty); |
| 3610 | if (retval) |
| 3611 | return retval; |
| 3612 | if (cmd != TIOCCBRK) { |
| 3613 | tty_wait_until_sent(tty, 0); |
| 3614 | if (signal_pending(current)) |
| 3615 | return -EINTR; |
| 3616 | } |
| 3617 | break; |
| 3618 | } |
| 3619 | |
| 3620 | switch (cmd) { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3621 | case TIOCSTI: |
| 3622 | return tiocsti(tty, p); |
| 3623 | case TIOCGWINSZ: |
| 3624 | return tiocgwinsz(tty, p); |
| 3625 | case TIOCSWINSZ: |
| 3626 | return tiocswinsz(tty, real_tty, p); |
| 3627 | case TIOCCONS: |
| 3628 | return real_tty != tty ? -EINVAL : tioccons(file); |
| 3629 | case FIONBIO: |
| 3630 | return fionbio(file, p); |
| 3631 | case TIOCEXCL: |
| 3632 | set_bit(TTY_EXCLUSIVE, &tty->flags); |
| 3633 | return 0; |
| 3634 | case TIOCNXCL: |
| 3635 | clear_bit(TTY_EXCLUSIVE, &tty->flags); |
| 3636 | return 0; |
| 3637 | case TIOCNOTTY: |
| 3638 | if (current->signal->tty != tty) |
| 3639 | return -ENOTTY; |
| 3640 | no_tty(); |
| 3641 | return 0; |
| 3642 | case TIOCSCTTY: |
| 3643 | return tiocsctty(tty, arg); |
| 3644 | case TIOCGPGRP: |
| 3645 | return tiocgpgrp(tty, real_tty, p); |
| 3646 | case TIOCSPGRP: |
| 3647 | return tiocspgrp(tty, real_tty, p); |
| 3648 | case TIOCGSID: |
| 3649 | return tiocgsid(tty, real_tty, p); |
| 3650 | case TIOCGETD: |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3651 | return put_user(tty->ldisc.ops->num, (int __user *)p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3652 | case TIOCSETD: |
| 3653 | return tiocsetd(tty, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | #ifdef CONFIG_VT |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3655 | case TIOCLINUX: |
| 3656 | return tioclinux(tty, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | #endif |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3658 | /* |
| 3659 | * Break handling |
| 3660 | */ |
| 3661 | case TIOCSBRK: /* Turn break on, unconditionally */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3662 | if (tty->ops->break_ctl) |
| 3663 | tty->ops->break_ctl(tty, -1); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3664 | return 0; |
| 3665 | |
| 3666 | case TIOCCBRK: /* Turn break off, unconditionally */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3667 | if (tty->ops->break_ctl) |
| 3668 | tty->ops->break_ctl(tty, 0); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3669 | return 0; |
| 3670 | case TCSBRK: /* SVID version: non-zero arg --> no break */ |
| 3671 | /* non-zero arg means wait for all output data |
| 3672 | * to be sent (performed above) but don't send break. |
| 3673 | * This is used by the tcdrain() termios function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3675 | if (!arg) |
| 3676 | return send_break(tty, 250); |
| 3677 | return 0; |
| 3678 | case TCSBRKP: /* support for POSIX tcsendbreak() */ |
| 3679 | return send_break(tty, arg ? arg*100 : 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3681 | case TIOCMGET: |
| 3682 | return tty_tiocmget(tty, file, p); |
| 3683 | case TIOCMSET: |
| 3684 | case TIOCMBIC: |
| 3685 | case TIOCMBIS: |
| 3686 | return tty_tiocmset(tty, file, cmd, p); |
| 3687 | case TCFLSH: |
| 3688 | switch (arg) { |
| 3689 | case TCIFLUSH: |
| 3690 | case TCIOFLUSH: |
| 3691 | /* flush tty buffer and allow ldisc to process ioctl */ |
| 3692 | tty_buffer_flush(tty); |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 3693 | break; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3694 | } |
| 3695 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3697 | if (tty->ops->ioctl) { |
| 3698 | retval = (tty->ops->ioctl)(tty, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | if (retval != -ENOIOCTLCMD) |
| 3700 | return retval; |
| 3701 | } |
| 3702 | ld = tty_ldisc_ref_wait(tty); |
| 3703 | retval = -EINVAL; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3704 | if (ld->ops->ioctl) { |
| 3705 | retval = ld->ops->ioctl(tty, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | if (retval == -ENOIOCTLCMD) |
| 3707 | retval = -EINVAL; |
| 3708 | } |
| 3709 | tty_ldisc_deref(ld); |
| 3710 | return retval; |
| 3711 | } |
| 3712 | |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 3713 | #ifdef CONFIG_COMPAT |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3714 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 3715 | unsigned long arg) |
| 3716 | { |
| 3717 | struct inode *inode = file->f_dentry->d_inode; |
| 3718 | struct tty_struct *tty = file->private_data; |
| 3719 | struct tty_ldisc *ld; |
| 3720 | int retval = -ENOIOCTLCMD; |
| 3721 | |
| 3722 | if (tty_paranoia_check(tty, inode, "tty_ioctl")) |
| 3723 | return -EINVAL; |
| 3724 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3725 | if (tty->ops->compat_ioctl) { |
| 3726 | retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 3727 | if (retval != -ENOIOCTLCMD) |
| 3728 | return retval; |
| 3729 | } |
| 3730 | |
| 3731 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3732 | if (ld->ops->compat_ioctl) |
| 3733 | retval = ld->ops->compat_ioctl(tty, file, cmd, arg); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 3734 | tty_ldisc_deref(ld); |
| 3735 | |
| 3736 | return retval; |
| 3737 | } |
| 3738 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | |
| 3740 | /* |
| 3741 | * This implements the "Secure Attention Key" --- the idea is to |
| 3742 | * prevent trojan horses by killing all processes associated with this |
| 3743 | * tty when the user hits the "Secure Attention Key". Required for |
| 3744 | * super-paranoid applications --- see the Orange Book for more details. |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3745 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | * This code could be nicer; ideally it should send a HUP, wait a few |
| 3747 | * seconds, then send a INT, and then a KILL signal. But you then |
| 3748 | * have to coordinate with the init process, since all processes associated |
| 3749 | * with the current tty must be dead before the new getty is allowed |
| 3750 | * to spawn. |
| 3751 | * |
| 3752 | * Now, if it would be correct ;-/ The current code has a nasty hole - |
| 3753 | * it doesn't catch files in flight. We may send the descriptor to ourselves |
| 3754 | * via AF_UNIX socket, close it and later fetch from socket. FIXME. |
| 3755 | * |
| 3756 | * Nasty bug: do_SAK is being called in interrupt context. This can |
| 3757 | * deadlock. We punt it up to process context. AKPM - 16Mar2001 |
| 3758 | */ |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 3759 | void __do_SAK(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | { |
| 3761 | #ifdef TTY_SOFT_SAK |
| 3762 | tty_hangup(tty); |
| 3763 | #else |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3764 | struct task_struct *g, *p; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3765 | struct pid *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3766 | int i; |
| 3767 | struct file *filp; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 3768 | struct fdtable *fdt; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | if (!tty) |
| 3771 | return; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3772 | session = tty->session; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3773 | |
Dan Carpenter | b3f13de | 2006-12-13 00:35:09 -0800 | [diff] [blame] | 3774 | tty_ldisc_flush(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3776 | tty_driver_flush_buffer(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | read_lock(&tasklist_lock); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3779 | /* Kill the entire session */ |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3780 | do_each_pid_task(session, PIDTYPE_SID, p) { |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3781 | printk(KERN_NOTICE "SAK: killed process %d" |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 3782 | " (%s): task_session_nr(p)==tty->session\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3783 | task_pid_nr(p), p->comm); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3784 | send_sig(SIGKILL, p, 1); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3785 | } while_each_pid_task(session, PIDTYPE_SID, p); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3786 | /* Now kill any processes that happen to have the |
| 3787 | * tty open. |
| 3788 | */ |
| 3789 | do_each_thread(g, p) { |
| 3790 | if (p->signal->tty == tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3791 | printk(KERN_NOTICE "SAK: killed process %d" |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 3792 | " (%s): task_session_nr(p)==tty->session\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3793 | task_pid_nr(p), p->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3794 | send_sig(SIGKILL, p, 1); |
| 3795 | continue; |
| 3796 | } |
| 3797 | task_lock(p); |
| 3798 | if (p->files) { |
Dipankar Sarma | ca99c1d | 2006-04-18 22:21:46 -0700 | [diff] [blame] | 3799 | /* |
| 3800 | * We don't take a ref to the file, so we must |
| 3801 | * hold ->file_lock instead. |
| 3802 | */ |
| 3803 | spin_lock(&p->files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 3804 | fdt = files_fdtable(p->files); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3805 | for (i = 0; i < fdt->max_fds; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3806 | filp = fcheck_files(p->files, i); |
| 3807 | if (!filp) |
| 3808 | continue; |
| 3809 | if (filp->f_op->read == tty_read && |
| 3810 | filp->private_data == tty) { |
| 3811 | printk(KERN_NOTICE "SAK: killed process %d" |
| 3812 | " (%s): fd#%d opened to the tty\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3813 | task_pid_nr(p), p->comm, i); |
Eric W. Biederman | 20ac943 | 2006-04-13 04:49:07 -0600 | [diff] [blame] | 3814 | force_sig(SIGKILL, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | break; |
| 3816 | } |
| 3817 | } |
Dipankar Sarma | ca99c1d | 2006-04-18 22:21:46 -0700 | [diff] [blame] | 3818 | spin_unlock(&p->files->file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | } |
| 3820 | task_unlock(p); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 3821 | } while_each_thread(g, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3822 | read_unlock(&tasklist_lock); |
| 3823 | #endif |
| 3824 | } |
| 3825 | |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 3826 | static void do_SAK_work(struct work_struct *work) |
| 3827 | { |
| 3828 | struct tty_struct *tty = |
| 3829 | container_of(work, struct tty_struct, SAK_work); |
| 3830 | __do_SAK(tty); |
| 3831 | } |
| 3832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3833 | /* |
| 3834 | * The tq handling here is a little racy - tty->SAK_work may already be queued. |
| 3835 | * Fortunately we don't need to worry, because if ->SAK_work is already queued, |
| 3836 | * the values which we write to it will be identical to the values which it |
| 3837 | * already has. --akpm |
| 3838 | */ |
| 3839 | void do_SAK(struct tty_struct *tty) |
| 3840 | { |
| 3841 | if (!tty) |
| 3842 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | schedule_work(&tty->SAK_work); |
| 3844 | } |
| 3845 | |
| 3846 | EXPORT_SYMBOL(do_SAK); |
| 3847 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3848 | /** |
| 3849 | * flush_to_ldisc |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3850 | * @work: tty structure passed from work queue. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3851 | * |
| 3852 | * This routine is called out of the software interrupt to flush data |
| 3853 | * from the buffer chain to the line discipline. |
| 3854 | * |
| 3855 | * Locking: holds tty->buf.lock to guard buffer list. Drops the lock |
| 3856 | * while invoking the line discipline receive_buf method. The |
| 3857 | * receive_buf method is single threaded for each tty instance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3859 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3860 | static void flush_to_ldisc(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3862 | struct tty_struct *tty = |
| 3863 | container_of(work, struct tty_struct, buf.work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | unsigned long flags; |
| 3865 | struct tty_ldisc *disc; |
Paul Fulghum | 2c3bb20 | 2006-06-28 04:26:48 -0700 | [diff] [blame] | 3866 | struct tty_buffer *tbuf, *head; |
Paul Fulghum | 8977d92 | 2006-02-10 01:51:14 -0800 | [diff] [blame] | 3867 | char *char_buf; |
| 3868 | unsigned char *flag_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | |
| 3870 | disc = tty_ldisc_ref(tty); |
| 3871 | if (disc == NULL) /* !TTY_LDISC */ |
| 3872 | return; |
| 3873 | |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 3874 | spin_lock_irqsave(&tty->buf.lock, flags); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3875 | /* So we know a flush is running */ |
| 3876 | set_bit(TTY_FLUSHING, &tty->flags); |
Paul Fulghum | 2c3bb20 | 2006-06-28 04:26:48 -0700 | [diff] [blame] | 3877 | head = tty->buf.head; |
| 3878 | if (head != NULL) { |
| 3879 | tty->buf.head = NULL; |
| 3880 | for (;;) { |
| 3881 | int count = head->commit - head->read; |
| 3882 | if (!count) { |
| 3883 | if (head->next == NULL) |
| 3884 | break; |
| 3885 | tbuf = head; |
| 3886 | head = head->next; |
| 3887 | tty_buffer_free(tty, tbuf); |
| 3888 | continue; |
| 3889 | } |
Alan Cox | 42fd552 | 2007-08-10 13:01:05 -0700 | [diff] [blame] | 3890 | /* Ldisc or user is trying to flush the buffers |
| 3891 | we are feeding to the ldisc, stop feeding the |
| 3892 | line discipline as we want to empty the queue */ |
| 3893 | if (test_bit(TTY_FLUSHPENDING, &tty->flags)) |
| 3894 | break; |
Paul Fulghum | 2c3bb20 | 2006-06-28 04:26:48 -0700 | [diff] [blame] | 3895 | if (!tty->receive_room) { |
| 3896 | schedule_delayed_work(&tty->buf.work, 1); |
| 3897 | break; |
| 3898 | } |
| 3899 | if (count > tty->receive_room) |
| 3900 | count = tty->receive_room; |
| 3901 | char_buf = head->char_buf_ptr + head->read; |
| 3902 | flag_buf = head->flag_buf_ptr + head->read; |
| 3903 | head->read += count; |
Paul Fulghum | 8977d92 | 2006-02-10 01:51:14 -0800 | [diff] [blame] | 3904 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3905 | disc->ops->receive_buf(tty, char_buf, |
| 3906 | flag_buf, count); |
Paul Fulghum | 8977d92 | 2006-02-10 01:51:14 -0800 | [diff] [blame] | 3907 | spin_lock_irqsave(&tty->buf.lock, flags); |
| 3908 | } |
Alan Cox | 42fd552 | 2007-08-10 13:01:05 -0700 | [diff] [blame] | 3909 | /* Restore the queue head */ |
Paul Fulghum | 2c3bb20 | 2006-06-28 04:26:48 -0700 | [diff] [blame] | 3910 | tty->buf.head = head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | } |
Alan Cox | 42fd552 | 2007-08-10 13:01:05 -0700 | [diff] [blame] | 3912 | /* We may have a deferred request to flush the input buffer, |
| 3913 | if so pull the chain under the lock and empty the queue */ |
| 3914 | if (test_bit(TTY_FLUSHPENDING, &tty->flags)) { |
| 3915 | __tty_buffer_flush(tty); |
| 3916 | clear_bit(TTY_FLUSHPENDING, &tty->flags); |
| 3917 | wake_up(&tty->read_wait); |
| 3918 | } |
| 3919 | clear_bit(TTY_FLUSHING, &tty->flags); |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 3920 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
Paul Fulghum | 817d6d3 | 2006-06-28 04:26:47 -0700 | [diff] [blame] | 3921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3922 | tty_ldisc_deref(disc); |
| 3923 | } |
| 3924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | /** |
| 3926 | * tty_flip_buffer_push - terminal |
| 3927 | * @tty: tty to push |
| 3928 | * |
| 3929 | * Queue a push of the terminal flip buffers to the line discipline. This |
| 3930 | * function must not be called from IRQ context if tty->low_latency is set. |
| 3931 | * |
| 3932 | * In the event of the queue being busy for flipping the work will be |
| 3933 | * held off and retried later. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3934 | * |
| 3935 | * Locking: tty buffer lock. Driver locks in low latency mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | */ |
| 3937 | |
| 3938 | void tty_flip_buffer_push(struct tty_struct *tty) |
| 3939 | { |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 3940 | unsigned long flags; |
| 3941 | spin_lock_irqsave(&tty->buf.lock, flags); |
Paul Fulghum | 33b37a3 | 2006-06-28 04:26:49 -0700 | [diff] [blame] | 3942 | if (tty->buf.tail != NULL) |
Paul Fulghum | 8977d92 | 2006-02-10 01:51:14 -0800 | [diff] [blame] | 3943 | tty->buf.tail->commit = tty->buf.tail->used; |
Paul Fulghum | 808249c | 2006-02-03 03:04:41 -0800 | [diff] [blame] | 3944 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 3945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | if (tty->low_latency) |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3947 | flush_to_ldisc(&tty->buf.work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | else |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 3949 | schedule_delayed_work(&tty->buf.work, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | EXPORT_SYMBOL(tty_flip_buffer_push); |
| 3953 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 3954 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3955 | /** |
| 3956 | * initialize_tty_struct |
| 3957 | * @tty: tty to initialize |
| 3958 | * |
| 3959 | * This subroutine initializes a tty structure that has been newly |
| 3960 | * allocated. |
| 3961 | * |
| 3962 | * Locking: none - tty in question must not be exposed at this point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 3964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | static void initialize_tty_struct(struct tty_struct *tty) |
| 3966 | { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3967 | struct tty_ldisc ld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | memset(tty, 0, sizeof(struct tty_struct)); |
| 3969 | tty->magic = TTY_MAGIC; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 3970 | if (tty_ldisc_get(N_TTY, &ld) < 0) |
| 3971 | panic("n_tty: init_tty"); |
| 3972 | tty_ldisc_assign(tty, &ld); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3973 | tty->session = NULL; |
| 3974 | tty->pgrp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | tty->overrun_time = jiffies; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 3976 | tty->buf.head = tty->buf.tail = NULL; |
| 3977 | tty_buffer_init(tty); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3978 | INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc); |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 3979 | mutex_init(&tty->termios_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3980 | init_waitqueue_head(&tty->write_wait); |
| 3981 | init_waitqueue_head(&tty->read_wait); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 3982 | INIT_WORK(&tty->hangup_work, do_tty_hangup); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 3983 | mutex_init(&tty->atomic_read_lock); |
| 3984 | mutex_init(&tty->atomic_write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | spin_lock_init(&tty->read_lock); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3986 | spin_lock_init(&tty->ctrl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | INIT_LIST_HEAD(&tty->tty_files); |
Eric W. Biederman | 7f1f86a | 2007-02-13 14:38:58 -0700 | [diff] [blame] | 3988 | INIT_WORK(&tty->SAK_work, do_SAK_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | } |
| 3990 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 3991 | /** |
| 3992 | * tty_put_char - write one character to a tty |
| 3993 | * @tty: tty |
| 3994 | * @ch: character |
| 3995 | * |
| 3996 | * Write one byte to the tty using the provided put_char method |
| 3997 | * if present. Returns the number of characters successfully output. |
| 3998 | * |
| 3999 | * Note: the specific put_char operation in the driver layer may go |
| 4000 | * away soon. Don't call it directly, use this method |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 4002 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 4003 | int tty_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 4005 | if (tty->ops->put_char) |
| 4006 | return tty->ops->put_char(tty, ch); |
| 4007 | return tty->ops->write(tty, &ch, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | } |
| 4009 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 4010 | EXPORT_SYMBOL_GPL(tty_put_char); |
| 4011 | |
gregkh@suse.de | 7fe845d | 2005-03-15 14:23:15 -0800 | [diff] [blame] | 4012 | static struct class *tty_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4013 | |
| 4014 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 4015 | * tty_register_device - register a tty device |
| 4016 | * @driver: the tty driver that describes the tty device |
| 4017 | * @index: the index in the tty driver for this tty device |
| 4018 | * @device: a struct device that is associated with this tty device. |
| 4019 | * This field is optional, if there is no known struct device |
| 4020 | * for this tty device it can be set to NULL safely. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | * |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4022 | * Returns a pointer to the struct device for this tty device |
| 4023 | * (or ERR_PTR(-EFOO) on error). |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 4024 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 4025 | * This call is required to be made to register an individual tty device |
| 4026 | * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If |
| 4027 | * that bit is not set, this function should not be called by a tty |
| 4028 | * driver. |
| 4029 | * |
| 4030 | * Locking: ?? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 4032 | |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4033 | struct device *tty_register_device(struct tty_driver *driver, unsigned index, |
| 4034 | struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4035 | { |
| 4036 | char name[64]; |
| 4037 | dev_t dev = MKDEV(driver->major, driver->minor_start) + index; |
| 4038 | |
| 4039 | if (index >= driver->num) { |
| 4040 | printk(KERN_ERR "Attempt to register invalid tty line number " |
| 4041 | " (%d).\n", index); |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 4042 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | } |
| 4044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | if (driver->type == TTY_DRIVER_TYPE_PTY) |
| 4046 | pty_line_name(driver, index, name); |
| 4047 | else |
| 4048 | tty_line_name(driver, index, name); |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 4049 | |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4050 | return device_create(tty_class, device, dev, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 4054 | * tty_unregister_device - unregister a tty device |
| 4055 | * @driver: the tty driver that describes the tty device |
| 4056 | * @index: the index in the tty driver for this tty device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 4058 | * If a tty device is registered with a call to tty_register_device() then |
| 4059 | * this function must be called when the tty device is gone. |
| 4060 | * |
| 4061 | * Locking: ?? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 4063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | void tty_unregister_device(struct tty_driver *driver, unsigned index) |
| 4065 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 4066 | device_destroy(tty_class, |
| 4067 | MKDEV(driver->major, driver->minor_start) + index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | EXPORT_SYMBOL(tty_register_device); |
| 4071 | EXPORT_SYMBOL(tty_unregister_device); |
| 4072 | |
| 4073 | struct tty_driver *alloc_tty_driver(int lines) |
| 4074 | { |
| 4075 | struct tty_driver *driver; |
| 4076 | |
Jean Delvare | 506eb99 | 2007-07-15 23:40:14 -0700 | [diff] [blame] | 4077 | driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | if (driver) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | driver->magic = TTY_DRIVER_MAGIC; |
| 4080 | driver->num = lines; |
| 4081 | /* later we'll move allocation of tables here */ |
| 4082 | } |
| 4083 | return driver; |
| 4084 | } |
| 4085 | |
| 4086 | void put_tty_driver(struct tty_driver *driver) |
| 4087 | { |
| 4088 | kfree(driver); |
| 4089 | } |
| 4090 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 4091 | void tty_set_operations(struct tty_driver *driver, |
| 4092 | const struct tty_operations *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 4094 | driver->ops = op; |
| 4095 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | |
| 4097 | EXPORT_SYMBOL(alloc_tty_driver); |
| 4098 | EXPORT_SYMBOL(put_tty_driver); |
| 4099 | EXPORT_SYMBOL(tty_set_operations); |
| 4100 | |
| 4101 | /* |
| 4102 | * Called by a tty driver to register itself. |
| 4103 | */ |
| 4104 | int tty_register_driver(struct tty_driver *driver) |
| 4105 | { |
| 4106 | int error; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 4107 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | dev_t dev; |
| 4109 | void **p = NULL; |
| 4110 | |
| 4111 | if (driver->flags & TTY_DRIVER_INSTALLED) |
| 4112 | return 0; |
| 4113 | |
Andy Whitcroft | 543691a | 2007-05-06 14:49:33 -0700 | [diff] [blame] | 4114 | if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) { |
| 4115 | p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | if (!p) |
| 4117 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | if (!driver->major) { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 4121 | error = alloc_chrdev_region(&dev, driver->minor_start, |
| 4122 | driver->num, driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | if (!error) { |
| 4124 | driver->major = MAJOR(dev); |
| 4125 | driver->minor_start = MINOR(dev); |
| 4126 | } |
| 4127 | } else { |
| 4128 | dev = MKDEV(driver->major, driver->minor_start); |
Geert Uytterhoeven | e5717c4 | 2007-02-20 15:45:21 +0100 | [diff] [blame] | 4129 | error = register_chrdev_region(dev, driver->num, driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | } |
| 4131 | if (error < 0) { |
| 4132 | kfree(p); |
| 4133 | return error; |
| 4134 | } |
| 4135 | |
| 4136 | if (p) { |
| 4137 | driver->ttys = (struct tty_struct **)p; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 4138 | driver->termios = (struct ktermios **)(p + driver->num); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 4139 | driver->termios_locked = (struct ktermios **) |
| 4140 | (p + driver->num * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4141 | } else { |
| 4142 | driver->ttys = NULL; |
| 4143 | driver->termios = NULL; |
| 4144 | driver->termios_locked = NULL; |
| 4145 | } |
| 4146 | |
| 4147 | cdev_init(&driver->cdev, &tty_fops); |
| 4148 | driver->cdev.owner = driver->owner; |
| 4149 | error = cdev_add(&driver->cdev, dev, driver->num); |
| 4150 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | unregister_chrdev_region(dev, driver->num); |
| 4152 | driver->ttys = NULL; |
| 4153 | driver->termios = driver->termios_locked = NULL; |
| 4154 | kfree(p); |
| 4155 | return error; |
| 4156 | } |
| 4157 | |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 4158 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | list_add(&driver->tty_drivers, &tty_drivers); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 4160 | mutex_unlock(&tty_mutex); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 4161 | |
| 4162 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { |
| 4163 | for (i = 0; i < driver->num; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | tty_register_device(driver, i, NULL); |
| 4165 | } |
| 4166 | proc_tty_register_driver(driver); |
| 4167 | return 0; |
| 4168 | } |
| 4169 | |
| 4170 | EXPORT_SYMBOL(tty_register_driver); |
| 4171 | |
| 4172 | /* |
| 4173 | * Called by a tty driver to unregister itself. |
| 4174 | */ |
| 4175 | int tty_unregister_driver(struct tty_driver *driver) |
| 4176 | { |
| 4177 | int i; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 4178 | struct ktermios *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | void *p; |
| 4180 | |
| 4181 | if (driver->refcount) |
| 4182 | return -EBUSY; |
| 4183 | |
| 4184 | unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), |
| 4185 | driver->num); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 4186 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | list_del(&driver->tty_drivers); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 4188 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | |
| 4190 | /* |
| 4191 | * Free the termios and termios_locked structures because |
| 4192 | * we don't want to get memory leaks when modular tty |
| 4193 | * drivers are removed from the kernel. |
| 4194 | */ |
| 4195 | for (i = 0; i < driver->num; i++) { |
| 4196 | tp = driver->termios[i]; |
| 4197 | if (tp) { |
| 4198 | driver->termios[i] = NULL; |
| 4199 | kfree(tp); |
| 4200 | } |
| 4201 | tp = driver->termios_locked[i]; |
| 4202 | if (tp) { |
| 4203 | driver->termios_locked[i] = NULL; |
| 4204 | kfree(tp); |
| 4205 | } |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4206 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | tty_unregister_device(driver, i); |
| 4208 | } |
| 4209 | p = driver->ttys; |
| 4210 | proc_tty_unregister_driver(driver); |
| 4211 | driver->ttys = NULL; |
| 4212 | driver->termios = driver->termios_locked = NULL; |
| 4213 | kfree(p); |
| 4214 | cdev_del(&driver->cdev); |
| 4215 | return 0; |
| 4216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | EXPORT_SYMBOL(tty_unregister_driver); |
| 4218 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 4219 | dev_t tty_devnum(struct tty_struct *tty) |
| 4220 | { |
| 4221 | return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; |
| 4222 | } |
| 4223 | EXPORT_SYMBOL(tty_devnum); |
| 4224 | |
| 4225 | void proc_clear_tty(struct task_struct *p) |
| 4226 | { |
| 4227 | spin_lock_irq(&p->sighand->siglock); |
| 4228 | p->signal->tty = NULL; |
| 4229 | spin_unlock_irq(&p->sighand->siglock); |
| 4230 | } |
David S. Miller | 7cac4ce | 2007-05-11 14:30:57 -0700 | [diff] [blame] | 4231 | EXPORT_SYMBOL(proc_clear_tty); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 4232 | |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 4233 | /* Called under the sighand lock */ |
| 4234 | |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 4235 | static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 4236 | { |
| 4237 | if (tty) { |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 4238 | unsigned long flags; |
| 4239 | /* We should not have a session or pgrp to put here but.... */ |
| 4240 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | d9c1e9a | 2007-03-18 12:45:44 -0600 | [diff] [blame] | 4241 | put_pid(tty->session); |
| 4242 | put_pid(tty->pgrp); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 4243 | tty->pgrp = get_pid(task_pgrp(tsk)); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 4244 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 4245 | tty->session = get_pid(task_session(tsk)); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 4246 | } |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 4247 | put_pid(tsk->signal->tty_old_pgrp); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 4248 | tsk->signal->tty = tty; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 4249 | tsk->signal->tty_old_pgrp = NULL; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 4250 | } |
| 4251 | |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 4252 | static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 4253 | { |
| 4254 | spin_lock_irq(&tsk->sighand->siglock); |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 4255 | __proc_set_tty(tsk, tty); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 4256 | spin_unlock_irq(&tsk->sighand->siglock); |
| 4257 | } |
| 4258 | |
| 4259 | struct tty_struct *get_current_tty(void) |
| 4260 | { |
| 4261 | struct tty_struct *tty; |
| 4262 | WARN_ON_ONCE(!mutex_is_locked(&tty_mutex)); |
| 4263 | tty = current->signal->tty; |
| 4264 | /* |
| 4265 | * session->tty can be changed/cleared from under us, make sure we |
| 4266 | * issue the load. The obtained pointer, when not NULL, is valid as |
| 4267 | * long as we hold tty_mutex. |
| 4268 | */ |
| 4269 | barrier(); |
| 4270 | return tty; |
| 4271 | } |
Heiko Carstens | a311f74 | 2006-12-13 00:33:41 -0800 | [diff] [blame] | 4272 | EXPORT_SYMBOL_GPL(get_current_tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | |
| 4274 | /* |
| 4275 | * Initialize the console device. This is called *early*, so |
| 4276 | * we can't necessarily depend on lots of kernel help here. |
| 4277 | * Just do some early initializations, and do the complex setup |
| 4278 | * later. |
| 4279 | */ |
| 4280 | void __init console_init(void) |
| 4281 | { |
| 4282 | initcall_t *call; |
| 4283 | |
| 4284 | /* Setup the default TTY line discipline. */ |
| 4285 | (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); |
| 4286 | |
| 4287 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 4288 | * set up the console device so that later boot sequences can |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | * inform about problems etc.. |
| 4290 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | call = __con_initcall_start; |
| 4292 | while (call < __con_initcall_end) { |
| 4293 | (*call)(); |
| 4294 | call++; |
| 4295 | } |
| 4296 | } |
| 4297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | static int __init tty_class_init(void) |
| 4299 | { |
gregkh@suse.de | 7fe845d | 2005-03-15 14:23:15 -0800 | [diff] [blame] | 4300 | tty_class = class_create(THIS_MODULE, "tty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | if (IS_ERR(tty_class)) |
| 4302 | return PTR_ERR(tty_class); |
| 4303 | return 0; |
| 4304 | } |
| 4305 | |
| 4306 | postcore_initcall(tty_class_init); |
| 4307 | |
| 4308 | /* 3/2004 jmc: why do these devices exist? */ |
| 4309 | |
| 4310 | static struct cdev tty_cdev, console_cdev; |
| 4311 | #ifdef CONFIG_UNIX98_PTYS |
| 4312 | static struct cdev ptmx_cdev; |
| 4313 | #endif |
| 4314 | #ifdef CONFIG_VT |
| 4315 | static struct cdev vc0_cdev; |
| 4316 | #endif |
| 4317 | |
| 4318 | /* |
| 4319 | * Ok, now we can initialize the rest of the tty devices and can count |
| 4320 | * on memory allocations, interrupts etc.. |
| 4321 | */ |
| 4322 | static int __init tty_init(void) |
| 4323 | { |
| 4324 | cdev_init(&tty_cdev, &tty_fops); |
| 4325 | if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || |
| 4326 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) |
| 4327 | panic("Couldn't register /dev/tty driver\n"); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4328 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), "tty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | |
| 4330 | cdev_init(&console_cdev, &console_fops); |
| 4331 | if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || |
| 4332 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) |
| 4333 | panic("Couldn't register /dev/console driver\n"); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4334 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), "console"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | |
| 4336 | #ifdef CONFIG_UNIX98_PTYS |
| 4337 | cdev_init(&ptmx_cdev, &ptmx_fops); |
| 4338 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || |
| 4339 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) |
| 4340 | panic("Couldn't register /dev/ptmx driver\n"); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4341 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), "ptmx"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | #endif |
| 4343 | |
| 4344 | #ifdef CONFIG_VT |
| 4345 | cdev_init(&vc0_cdev, &console_fops); |
| 4346 | if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || |
| 4347 | register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) |
| 4348 | panic("Couldn't register /dev/tty0 driver\n"); |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4349 | device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), "tty0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | |
| 4351 | vty_init(); |
| 4352 | #endif |
| 4353 | return 0; |
| 4354 | } |
| 4355 | module_init(tty_init); |