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 | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 179 | /** |
| 180 | * free_tty_struct - free a disused tty |
| 181 | * @tty: tty struct to free |
| 182 | * |
| 183 | * Free the write buffers, tty queue and tty memory itself. |
| 184 | * |
| 185 | * Locking: none. Must be called after tty is definitely unused |
| 186 | */ |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | static inline void free_tty_struct(struct tty_struct *tty) |
| 189 | { |
| 190 | kfree(tty->write_buf); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 191 | tty_buffer_free_all(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | kfree(tty); |
| 193 | } |
| 194 | |
| 195 | #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) |
| 196 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 197 | /** |
| 198 | * tty_name - return tty naming |
| 199 | * @tty: tty structure |
| 200 | * @buf: buffer for output |
| 201 | * |
| 202 | * Convert a tty structure into a name. The name reflects the kernel |
| 203 | * naming policy and if udev is in use may not reflect user space |
| 204 | * |
| 205 | * Locking: none |
| 206 | */ |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | char *tty_name(struct tty_struct *tty, char *buf) |
| 209 | { |
| 210 | if (!tty) /* Hmm. NULL pointer. That's fun. */ |
| 211 | strcpy(buf, "NULL tty"); |
| 212 | else |
| 213 | strcpy(buf, tty->name); |
| 214 | return buf; |
| 215 | } |
| 216 | |
| 217 | EXPORT_SYMBOL(tty_name); |
| 218 | |
Andrew Morton | d769a66 | 2005-05-05 16:15:50 -0700 | [diff] [blame] | 219 | int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | const char *routine) |
| 221 | { |
| 222 | #ifdef TTY_PARANOIA_CHECK |
| 223 | if (!tty) { |
| 224 | printk(KERN_WARNING |
| 225 | "null TTY for (%d:%d) in %s\n", |
| 226 | imajor(inode), iminor(inode), routine); |
| 227 | return 1; |
| 228 | } |
| 229 | if (tty->magic != TTY_MAGIC) { |
| 230 | printk(KERN_WARNING |
| 231 | "bad magic number for tty struct (%d:%d) in %s\n", |
| 232 | imajor(inode), iminor(inode), routine); |
| 233 | return 1; |
| 234 | } |
| 235 | #endif |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int check_tty_count(struct tty_struct *tty, const char *routine) |
| 240 | { |
| 241 | #ifdef CHECK_TTY_COUNT |
| 242 | struct list_head *p; |
| 243 | int count = 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | file_list_lock(); |
| 246 | list_for_each(p, &tty->tty_files) { |
| 247 | count++; |
| 248 | } |
| 249 | file_list_unlock(); |
| 250 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 251 | tty->driver->subtype == PTY_TYPE_SLAVE && |
| 252 | tty->link && tty->link->count) |
| 253 | count++; |
| 254 | if (tty->count != count) { |
| 255 | printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) " |
| 256 | "!= #fd's(%d) in %s\n", |
| 257 | tty->name, tty->count, count, routine); |
| 258 | return count; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | #endif |
| 261 | return 0; |
| 262 | } |
| 263 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 264 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 265 | * get_tty_driver - find device of a tty |
| 266 | * @dev_t: device identifier |
| 267 | * @index: returns the index of the tty |
| 268 | * |
| 269 | * This routine returns a tty driver structure, given a device number |
| 270 | * and also passes back the index number. |
| 271 | * |
| 272 | * Locking: caller must hold tty_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | static struct tty_driver *get_tty_driver(dev_t device, int *index) |
| 276 | { |
| 277 | struct tty_driver *p; |
| 278 | |
| 279 | list_for_each_entry(p, &tty_drivers, tty_drivers) { |
| 280 | dev_t base = MKDEV(p->major, p->minor_start); |
| 281 | if (device < base || device >= base + p->num) |
| 282 | continue; |
| 283 | *index = device - base; |
| 284 | return p; |
| 285 | } |
| 286 | return NULL; |
| 287 | } |
| 288 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 289 | #ifdef CONFIG_CONSOLE_POLL |
| 290 | |
| 291 | /** |
| 292 | * tty_find_polling_driver - find device of a polled tty |
| 293 | * @name: name string to match |
| 294 | * @line: pointer to resulting tty line nr |
| 295 | * |
| 296 | * This routine returns a tty driver structure, given a name |
| 297 | * and the condition that the tty driver is capable of polled |
| 298 | * operation. |
| 299 | */ |
| 300 | struct tty_driver *tty_find_polling_driver(char *name, int *line) |
| 301 | { |
| 302 | struct tty_driver *p, *res = NULL; |
| 303 | int tty_line = 0; |
Jason Wessel | 0dca0fd | 2008-09-26 10:36:42 -0500 | [diff] [blame] | 304 | int len; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 305 | char *str; |
| 306 | |
Jason Wessel | 0dca0fd | 2008-09-26 10:36:42 -0500 | [diff] [blame] | 307 | for (str = name; *str; str++) |
| 308 | if ((*str >= '0' && *str <= '9') || *str == ',') |
| 309 | break; |
| 310 | if (!*str) |
| 311 | return NULL; |
| 312 | |
| 313 | len = str - name; |
| 314 | tty_line = simple_strtoul(str, &str, 10); |
| 315 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 316 | mutex_lock(&tty_mutex); |
| 317 | /* Search through the tty devices to look for a match */ |
| 318 | list_for_each_entry(p, &tty_drivers, tty_drivers) { |
Jason Wessel | 0dca0fd | 2008-09-26 10:36:42 -0500 | [diff] [blame] | 319 | if (strncmp(name, p->name, len) != 0) |
| 320 | continue; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 321 | if (*str == ',') |
| 322 | str++; |
| 323 | if (*str == '\0') |
Harvey Harrison | 8da5630 | 2008-04-28 14:13:20 -0700 | [diff] [blame] | 324 | str = NULL; |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 325 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 326 | if (tty_line >= 0 && tty_line <= p->num && p->ops && |
| 327 | p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) { |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 328 | res = p; |
| 329 | *line = tty_line; |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | mutex_unlock(&tty_mutex); |
| 334 | |
| 335 | return res; |
| 336 | } |
| 337 | EXPORT_SYMBOL_GPL(tty_find_polling_driver); |
| 338 | #endif |
| 339 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 340 | /** |
| 341 | * tty_check_change - check for POSIX terminal changes |
| 342 | * @tty: tty to check |
| 343 | * |
| 344 | * If we try to write to, or set the state of, a terminal and we're |
| 345 | * not in the foreground, send a SIGTTOU. If the signal is blocked or |
| 346 | * ignored, go ahead and perform the operation. (POSIX 7.2) |
| 347 | * |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 348 | * Locking: ctrl_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 350 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 351 | int tty_check_change(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 353 | unsigned long flags; |
| 354 | int ret = 0; |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (current->signal->tty != tty) |
| 357 | return 0; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 358 | |
| 359 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 360 | |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 361 | if (!tty->pgrp) { |
| 362 | printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n"); |
Andrew Morton | 9ffee4c | 2008-05-14 16:05:58 -0700 | [diff] [blame] | 363 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 365 | if (task_pgrp(current) == tty->pgrp) |
Andrew Morton | 9ffee4c | 2008-05-14 16:05:58 -0700 | [diff] [blame] | 366 | goto out_unlock; |
| 367 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | if (is_ignored(SIGTTOU)) |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 369 | goto out; |
| 370 | if (is_current_pgrp_orphaned()) { |
| 371 | ret = -EIO; |
| 372 | goto out; |
| 373 | } |
Oleg Nesterov | 040b636 | 2007-06-01 00:46:53 -0700 | [diff] [blame] | 374 | kill_pgrp(task_pgrp(current), SIGTTOU, 1); |
| 375 | set_thread_flag(TIF_SIGPENDING); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 376 | ret = -ERESTARTSYS; |
| 377 | out: |
Andrew Morton | 9ffee4c | 2008-05-14 16:05:58 -0700 | [diff] [blame] | 378 | return ret; |
| 379 | out_unlock: |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 380 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 381 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | EXPORT_SYMBOL(tty_check_change); |
| 385 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 386 | 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] | 387 | size_t count, loff_t *ppos) |
| 388 | { |
| 389 | return 0; |
| 390 | } |
| 391 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 392 | 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] | 393 | size_t count, loff_t *ppos) |
| 394 | { |
| 395 | return -EIO; |
| 396 | } |
| 397 | |
| 398 | /* No kernel lock held - none needed ;) */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 399 | 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] | 400 | { |
| 401 | return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM; |
| 402 | } |
| 403 | |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 404 | static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, |
| 405 | unsigned long arg) |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 406 | { |
| 407 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; |
| 408 | } |
| 409 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 410 | static long hung_up_tty_compat_ioctl(struct file *file, |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 411 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; |
| 414 | } |
| 415 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 416 | static const struct file_operations tty_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | .llseek = no_llseek, |
| 418 | .read = tty_read, |
| 419 | .write = tty_write, |
| 420 | .poll = tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 421 | .unlocked_ioctl = tty_ioctl, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 422 | .compat_ioctl = tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | .open = tty_open, |
| 424 | .release = tty_release, |
| 425 | .fasync = tty_fasync, |
| 426 | }; |
| 427 | |
| 428 | #ifdef CONFIG_UNIX98_PTYS |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 429 | static const struct file_operations ptmx_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | .llseek = no_llseek, |
| 431 | .read = tty_read, |
| 432 | .write = tty_write, |
| 433 | .poll = tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 434 | .unlocked_ioctl = tty_ioctl, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 435 | .compat_ioctl = tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | .open = ptmx_open, |
| 437 | .release = tty_release, |
| 438 | .fasync = tty_fasync, |
| 439 | }; |
| 440 | #endif |
| 441 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 442 | static const struct file_operations console_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | .llseek = no_llseek, |
| 444 | .read = tty_read, |
| 445 | .write = redirected_tty_write, |
| 446 | .poll = tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 447 | .unlocked_ioctl = tty_ioctl, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 448 | .compat_ioctl = tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | .open = tty_open, |
| 450 | .release = tty_release, |
| 451 | .fasync = tty_fasync, |
| 452 | }; |
| 453 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 454 | static const struct file_operations hung_up_tty_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | .llseek = no_llseek, |
| 456 | .read = hung_up_tty_read, |
| 457 | .write = hung_up_tty_write, |
| 458 | .poll = hung_up_tty_poll, |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 459 | .unlocked_ioctl = hung_up_tty_ioctl, |
Paul Fulghum | 38ad2ed | 2007-06-16 10:15:55 -0700 | [diff] [blame] | 460 | .compat_ioctl = hung_up_tty_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | .release = tty_release, |
| 462 | }; |
| 463 | |
| 464 | static DEFINE_SPINLOCK(redirect_lock); |
| 465 | static struct file *redirect; |
| 466 | |
| 467 | /** |
| 468 | * tty_wakeup - request more data |
| 469 | * @tty: terminal |
| 470 | * |
| 471 | * Internal and external helper for wakeups of tty. This function |
| 472 | * informs the line discipline if present that the driver is ready |
| 473 | * to receive more output data. |
| 474 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | void tty_wakeup(struct tty_struct *tty) |
| 477 | { |
| 478 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { |
| 481 | ld = tty_ldisc_ref(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 482 | if (ld) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 483 | if (ld->ops->write_wakeup) |
| 484 | ld->ops->write_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | tty_ldisc_deref(ld); |
| 486 | } |
| 487 | } |
| 488 | wake_up_interruptible(&tty->write_wait); |
| 489 | } |
| 490 | |
| 491 | EXPORT_SYMBOL_GPL(tty_wakeup); |
| 492 | |
| 493 | /** |
| 494 | * tty_ldisc_flush - flush line discipline queue |
| 495 | * @tty: tty |
| 496 | * |
| 497 | * Flush the line discipline queue (if any) for this tty. If there |
| 498 | * is no line discipline active this is a no-op. |
| 499 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | void tty_ldisc_flush(struct tty_struct *tty) |
| 502 | { |
| 503 | struct tty_ldisc *ld = tty_ldisc_ref(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 504 | if (ld) { |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 505 | if (ld->ops->flush_buffer) |
| 506 | ld->ops->flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | tty_ldisc_deref(ld); |
| 508 | } |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 509 | tty_buffer_flush(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * tty_reset_termios - reset terminal state |
| 516 | * @tty: tty to reset |
| 517 | * |
| 518 | * Restore a terminal to the driver default state |
| 519 | */ |
| 520 | |
| 521 | static void tty_reset_termios(struct tty_struct *tty) |
| 522 | { |
| 523 | mutex_lock(&tty->termios_mutex); |
| 524 | *tty->termios = tty->driver->init_termios; |
| 525 | tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios); |
| 526 | tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios); |
| 527 | mutex_unlock(&tty->termios_mutex); |
| 528 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 529 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 530 | /** |
| 531 | * do_tty_hangup - actual handler for hangup events |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 532 | * @work: tty device |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 533 | * |
Alan Cox | 1bad879 | 2008-07-22 23:38:04 +0100 | [diff] [blame] | 534 | * This can be called by the "eventd" kernel thread. That is process |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 535 | * synchronous but doesn't hold any locks, so we need to make sure we |
| 536 | * have the appropriate locks for what we're doing. |
| 537 | * |
| 538 | * The hangup event clears any pending redirections onto the hung up |
| 539 | * device. It ensures future writes will error and it does the needed |
| 540 | * line discipline hangup and signal delivery. The tty object itself |
| 541 | * remains intact. |
| 542 | * |
| 543 | * Locking: |
| 544 | * BKL |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 545 | * redirect lock for undoing redirection |
| 546 | * file list lock for manipulating list of ttys |
| 547 | * tty_ldisc_lock from called functions |
| 548 | * termios_mutex resetting termios data |
| 549 | * tasklist_lock to walk task list for hangup event |
| 550 | * ->siglock to protect ->signal/->sighand |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 552 | static void do_tty_hangup(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 554 | struct tty_struct *tty = |
| 555 | container_of(work, struct tty_struct, hangup_work); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 556 | struct file *cons_filp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | struct file *filp, *f = NULL; |
| 558 | struct task_struct *p; |
| 559 | struct tty_ldisc *ld; |
| 560 | int closecount = 0, n; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 561 | unsigned long flags; |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 562 | int refs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | if (!tty) |
| 565 | return; |
| 566 | |
| 567 | /* inuse_filps is protected by the single kernel lock */ |
| 568 | lock_kernel(); |
| 569 | |
| 570 | spin_lock(&redirect_lock); |
| 571 | if (redirect && redirect->private_data == tty) { |
| 572 | f = redirect; |
| 573 | redirect = NULL; |
| 574 | } |
| 575 | spin_unlock(&redirect_lock); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | check_tty_count(tty, "do_tty_hangup"); |
| 578 | file_list_lock(); |
| 579 | /* This breaks for file handles being sent over AF_UNIX sockets ? */ |
Eric Dumazet | 2f51201 | 2005-10-30 15:02:16 -0800 | [diff] [blame] | 580 | list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | if (filp->f_op->write == redirected_tty_write) |
| 582 | cons_filp = filp; |
| 583 | if (filp->f_op->write != tty_write) |
| 584 | continue; |
| 585 | closecount++; |
| 586 | tty_fasync(-1, filp, 0); /* can't block */ |
| 587 | filp->f_op = &hung_up_tty_fops; |
| 588 | } |
| 589 | file_list_unlock(); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 590 | /* |
| 591 | * FIXME! What are the locking issues here? This may me overdoing |
| 592 | * things... This question is especially important now that we've |
| 593 | * removed the irqlock. |
| 594 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | ld = tty_ldisc_ref(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 596 | if (ld != NULL) { |
| 597 | /* We may have no line discipline at this point */ |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 598 | if (ld->ops->flush_buffer) |
| 599 | ld->ops->flush_buffer(tty); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 600 | tty_driver_flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 602 | ld->ops->write_wakeup) |
| 603 | ld->ops->write_wakeup(tty); |
| 604 | if (ld->ops->hangup) |
| 605 | ld->ops->hangup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 607 | /* |
| 608 | * FIXME: Once we trust the LDISC code better we can wait here for |
| 609 | * ldisc completion and fix the driver call race |
| 610 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | wake_up_interruptible(&tty->write_wait); |
| 612 | wake_up_interruptible(&tty->read_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /* |
| 614 | * Shutdown the current line discipline, and reset it to |
| 615 | * N_TTY. |
| 616 | */ |
| 617 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 618 | tty_reset_termios(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | /* Defer ldisc switch */ |
| 620 | /* tty_deferred_ldisc_switch(N_TTY); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | This should get done automatically when the port closes and |
| 623 | tty_release is called */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | read_lock(&tasklist_lock); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 626 | if (tty->session) { |
| 627 | do_each_pid_task(tty->session, PIDTYPE_SID, p) { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 628 | spin_lock_irq(&p->sighand->siglock); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 629 | if (p->signal->tty == tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | p->signal->tty = NULL; |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 631 | /* We defer the dereferences outside fo |
| 632 | the tasklist lock */ |
| 633 | refs++; |
| 634 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 635 | if (!p->signal->leader) { |
| 636 | spin_unlock_irq(&p->sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | continue; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 638 | } |
| 639 | __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p); |
| 640 | __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 641 | put_pid(p->signal->tty_old_pgrp); /* A noop */ |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 642 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 643 | if (tty->pgrp) |
| 644 | p->signal->tty_old_pgrp = get_pid(tty->pgrp); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 645 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 646 | spin_unlock_irq(&p->sighand->siglock); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 647 | } while_each_pid_task(tty->session, PIDTYPE_SID, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | read_unlock(&tasklist_lock); |
| 650 | |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 651 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | tty->flags = 0; |
Eric W. Biederman | d9c1e9a | 2007-03-18 12:45:44 -0600 | [diff] [blame] | 653 | put_pid(tty->session); |
| 654 | put_pid(tty->pgrp); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 655 | tty->session = NULL; |
| 656 | tty->pgrp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | tty->ctrl_status = 0; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 658 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 659 | |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 660 | /* Account for the p->signal references we killed */ |
| 661 | while (refs--) |
| 662 | tty_kref_put(tty); |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 665 | * If one of the devices matches a console pointer, we |
| 666 | * cannot just call hangup() because that will cause |
| 667 | * tty->count and state->count to go out of sync. |
| 668 | * So we just call close() the right number of times. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | */ |
| 670 | if (cons_filp) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 671 | if (tty->ops->close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | for (n = 0; n < closecount; n++) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 673 | tty->ops->close(tty, cons_filp); |
| 674 | } else if (tty->ops->hangup) |
| 675 | (tty->ops->hangup)(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 676 | /* |
| 677 | * We don't want to have driver/ldisc interactions beyond |
| 678 | * the ones we did here. The driver layer expects no |
| 679 | * calls after ->hangup() from the ldisc side. However we |
| 680 | * can't yet guarantee all that. |
| 681 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | set_bit(TTY_HUPPED, &tty->flags); |
| 683 | if (ld) { |
| 684 | tty_ldisc_enable(tty); |
| 685 | tty_ldisc_deref(ld); |
| 686 | } |
| 687 | unlock_kernel(); |
| 688 | if (f) |
| 689 | fput(f); |
| 690 | } |
| 691 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 692 | /** |
| 693 | * tty_hangup - trigger a hangup event |
| 694 | * @tty: tty to hangup |
| 695 | * |
| 696 | * A carrier loss (virtual or otherwise) has occurred on this like |
| 697 | * schedule a hangup sequence to run after this event. |
| 698 | */ |
| 699 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 700 | void tty_hangup(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
| 702 | #ifdef TTY_DEBUG_HANGUP |
| 703 | char buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf)); |
| 705 | #endif |
| 706 | schedule_work(&tty->hangup_work); |
| 707 | } |
| 708 | |
| 709 | EXPORT_SYMBOL(tty_hangup); |
| 710 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 711 | /** |
| 712 | * tty_vhangup - process vhangup |
| 713 | * @tty: tty to hangup |
| 714 | * |
| 715 | * The user has asked via system call for the terminal to be hung up. |
| 716 | * 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] | 717 | * is complete. That guarantee is necessary for security reasons. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 718 | */ |
| 719 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 720 | void tty_vhangup(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
| 722 | #ifdef TTY_DEBUG_HANGUP |
| 723 | char buf[64]; |
| 724 | |
| 725 | printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); |
| 726 | #endif |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 727 | do_tty_hangup(&tty->hangup_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | EXPORT_SYMBOL(tty_vhangup); |
| 731 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 732 | /** |
| 733 | * tty_hung_up_p - was tty hung up |
| 734 | * @filp: file pointer of tty |
| 735 | * |
| 736 | * Return true if the tty has been subject to a vhangup or a carrier |
| 737 | * loss |
| 738 | */ |
| 739 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 740 | int tty_hung_up_p(struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | { |
| 742 | return (filp->f_op == &hung_up_tty_fops); |
| 743 | } |
| 744 | |
| 745 | EXPORT_SYMBOL(tty_hung_up_p); |
| 746 | |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 747 | static void session_clear_tty(struct pid *session) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 748 | { |
| 749 | struct task_struct *p; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 750 | do_each_pid_task(session, PIDTYPE_SID, p) { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 751 | proc_clear_tty(p); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 752 | } while_each_pid_task(session, PIDTYPE_SID, p); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 755 | /** |
| 756 | * disassociate_ctty - disconnect controlling tty |
| 757 | * @on_exit: true if exiting so need to "hang up" the session |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 759 | * This function is typically called only by the session leader, when |
| 760 | * it wants to disassociate itself from its controlling tty. |
| 761 | * |
| 762 | * It performs the following functions: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | * (1) Sends a SIGHUP and SIGCONT to the foreground process group |
| 764 | * (2) Clears the tty from being controlling the session |
| 765 | * (3) Clears the controlling tty for all processes in the |
| 766 | * session group. |
| 767 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 768 | * The argument on_exit is set to 1 if called when a process is |
| 769 | * exiting; it is 0 if called by the ioctl TIOCNOTTY. |
| 770 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 771 | * Locking: |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 772 | * BKL is taken for hysterical raisins |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 773 | * tty_mutex is taken to protect tty |
| 774 | * ->siglock is taken to protect ->signal/->sighand |
| 775 | * tasklist_lock is taken to walk process list for sessions |
| 776 | * ->siglock is taken to protect ->signal/->sighand |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | void disassociate_ctty(int on_exit) |
| 780 | { |
| 781 | struct tty_struct *tty; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 782 | struct pid *tty_pgrp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 785 | mutex_lock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 786 | tty = get_current_tty(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | if (tty) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 788 | tty_pgrp = get_pid(tty->pgrp); |
Christian Borntraeger | 000b915 | 2008-08-11 09:02:49 +0100 | [diff] [blame] | 789 | mutex_unlock(&tty_mutex); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 790 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) |
| 792 | tty_vhangup(tty); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 793 | unlock_kernel(); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 794 | tty_kref_put(tty); |
Eric W. Biederman | 680a967 | 2007-02-12 00:52:52 -0800 | [diff] [blame] | 795 | } else if (on_exit) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 796 | struct pid *old_pgrp; |
Eric W. Biederman | 680a967 | 2007-02-12 00:52:52 -0800 | [diff] [blame] | 797 | spin_lock_irq(¤t->sighand->siglock); |
| 798 | old_pgrp = current->signal->tty_old_pgrp; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 799 | current->signal->tty_old_pgrp = NULL; |
Eric W. Biederman | 680a967 | 2007-02-12 00:52:52 -0800 | [diff] [blame] | 800 | spin_unlock_irq(¤t->sighand->siglock); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 801 | if (old_pgrp) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 802 | kill_pgrp(old_pgrp, SIGHUP, on_exit); |
| 803 | kill_pgrp(old_pgrp, SIGCONT, on_exit); |
| 804 | put_pid(old_pgrp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 806 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | return; |
| 808 | } |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 809 | if (tty_pgrp) { |
| 810 | kill_pgrp(tty_pgrp, SIGHUP, on_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | if (!on_exit) |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 812 | kill_pgrp(tty_pgrp, SIGCONT, on_exit); |
| 813 | put_pid(tty_pgrp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 816 | spin_lock_irq(¤t->sighand->siglock); |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 817 | put_pid(current->signal->tty_old_pgrp); |
Randy Dunlap | 23cac8d | 2007-02-20 13:58:05 -0800 | [diff] [blame] | 818 | current->signal->tty_old_pgrp = NULL; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 819 | spin_unlock_irq(¤t->sighand->siglock); |
| 820 | |
| 821 | mutex_lock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 822 | tty = get_current_tty(); |
| 823 | if (tty) { |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 824 | unsigned long flags; |
| 825 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 826 | put_pid(tty->session); |
| 827 | put_pid(tty->pgrp); |
| 828 | tty->session = NULL; |
| 829 | tty->pgrp = NULL; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 830 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 831 | tty_kref_put(tty); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 832 | } else { |
| 833 | #ifdef TTY_DEBUG_HANGUP |
| 834 | printk(KERN_DEBUG "error attempted to write to tty [0x%p]" |
| 835 | " = NULL", tty); |
| 836 | #endif |
| 837 | } |
| 838 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
| 840 | /* Now clear signal->tty under the lock */ |
| 841 | read_lock(&tasklist_lock); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 842 | session_clear_tty(task_session(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 846 | /** |
| 847 | * |
| 848 | * no_tty - Ensure the current process does not have a controlling tty |
| 849 | */ |
| 850 | void no_tty(void) |
| 851 | { |
| 852 | struct task_struct *tsk = current; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 853 | lock_kernel(); |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 854 | if (tsk->signal->leader) |
| 855 | disassociate_ctty(0); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 856 | unlock_kernel(); |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 857 | proc_clear_tty(tsk); |
| 858 | } |
| 859 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 860 | |
| 861 | /** |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 862 | * stop_tty - propagate flow control |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 863 | * @tty: tty to stop |
| 864 | * |
| 865 | * 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] | 866 | * must also propagate the TIOCKPKT status. May be called |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 867 | * on an already stopped device and will not re-call the driver |
| 868 | * method. |
| 869 | * |
| 870 | * This functionality is used by both the line disciplines for |
| 871 | * halting incoming flow and by the driver. It may therefore be |
| 872 | * called from any context, may be under the tty atomic_write_lock |
| 873 | * but not always. |
| 874 | * |
| 875 | * Locking: |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 876 | * Uses the tty control lock internally |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 877 | */ |
| 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | void stop_tty(struct tty_struct *tty) |
| 880 | { |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 881 | unsigned long flags; |
| 882 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 883 | if (tty->stopped) { |
| 884 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | return; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 886 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | tty->stopped = 1; |
| 888 | if (tty->link && tty->link->packet) { |
| 889 | tty->ctrl_status &= ~TIOCPKT_START; |
| 890 | tty->ctrl_status |= TIOCPKT_STOP; |
| 891 | wake_up_interruptible(&tty->link->read_wait); |
| 892 | } |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 893 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 894 | if (tty->ops->stop) |
| 895 | (tty->ops->stop)(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | EXPORT_SYMBOL(stop_tty); |
| 899 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 900 | /** |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 901 | * start_tty - propagate flow control |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 902 | * @tty: tty to start |
| 903 | * |
| 904 | * 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] | 905 | * any necessary wakeups and propagate the TIOCPKT status. If this |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 906 | * is the tty was previous stopped and is being started then the |
| 907 | * driver start method is invoked and the line discipline woken. |
| 908 | * |
| 909 | * Locking: |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 910 | * ctrl_lock |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 911 | */ |
| 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | void start_tty(struct tty_struct *tty) |
| 914 | { |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 915 | unsigned long flags; |
| 916 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 917 | if (!tty->stopped || tty->flow_stopped) { |
| 918 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 920 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | tty->stopped = 0; |
| 922 | if (tty->link && tty->link->packet) { |
| 923 | tty->ctrl_status &= ~TIOCPKT_STOP; |
| 924 | tty->ctrl_status |= TIOCPKT_START; |
| 925 | wake_up_interruptible(&tty->link->read_wait); |
| 926 | } |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 927 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 928 | if (tty->ops->start) |
| 929 | (tty->ops->start)(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | /* If we have a running line discipline it may need kicking */ |
| 931 | tty_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | EXPORT_SYMBOL(start_tty); |
| 935 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 936 | /** |
| 937 | * tty_read - read method for tty device files |
| 938 | * @file: pointer to tty file |
| 939 | * @buf: user buffer |
| 940 | * @count: size of user buffer |
| 941 | * @ppos: unused |
| 942 | * |
| 943 | * Perform the read system call function on this terminal device. Checks |
| 944 | * for hung up devices before calling the line discipline method. |
| 945 | * |
| 946 | * Locking: |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 947 | * Locks the line discipline internally while needed. Multiple |
| 948 | * read calls may be outstanding in parallel. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 949 | */ |
| 950 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 951 | 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] | 952 | loff_t *ppos) |
| 953 | { |
| 954 | int i; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 955 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | struct inode *inode; |
| 957 | struct tty_ldisc *ld; |
| 958 | |
| 959 | tty = (struct tty_struct *)file->private_data; |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 960 | inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | if (tty_paranoia_check(tty, inode, "tty_read")) |
| 962 | return -EIO; |
| 963 | if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) |
| 964 | return -EIO; |
| 965 | |
| 966 | /* We want to wait for the line discipline to sort out in this |
| 967 | situation */ |
| 968 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 969 | if (ld->ops->read) |
| 970 | i = (ld->ops->read)(tty, file, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | else |
| 972 | i = -EIO; |
| 973 | tty_ldisc_deref(ld); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | if (i > 0) |
| 975 | inode->i_atime = current_fs_time(inode->i_sb); |
| 976 | return i; |
| 977 | } |
| 978 | |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 979 | void tty_write_unlock(struct tty_struct *tty) |
| 980 | { |
| 981 | mutex_unlock(&tty->atomic_write_lock); |
| 982 | wake_up_interruptible(&tty->write_wait); |
| 983 | } |
| 984 | |
| 985 | int tty_write_lock(struct tty_struct *tty, int ndelay) |
| 986 | { |
| 987 | if (!mutex_trylock(&tty->atomic_write_lock)) { |
| 988 | if (ndelay) |
| 989 | return -EAGAIN; |
| 990 | if (mutex_lock_interruptible(&tty->atomic_write_lock)) |
| 991 | return -ERESTARTSYS; |
| 992 | } |
| 993 | return 0; |
| 994 | } |
| 995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | /* |
| 997 | * Split writes up in sane blocksizes to avoid |
| 998 | * denial-of-service type attacks |
| 999 | */ |
| 1000 | static inline ssize_t do_tty_write( |
| 1001 | ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), |
| 1002 | struct tty_struct *tty, |
| 1003 | struct file *file, |
| 1004 | const char __user *buf, |
| 1005 | size_t count) |
| 1006 | { |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1007 | ssize_t ret, written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | unsigned int chunk; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1009 | |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1010 | ret = tty_write_lock(tty, file->f_flags & O_NDELAY); |
| 1011 | if (ret < 0) |
| 1012 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
| 1014 | /* |
| 1015 | * We chunk up writes into a temporary buffer. This |
| 1016 | * simplifies low-level drivers immensely, since they |
| 1017 | * don't have locking issues and user mode accesses. |
| 1018 | * |
| 1019 | * But if TTY_NO_WRITE_SPLIT is set, we should use a |
| 1020 | * big chunk-size.. |
| 1021 | * |
| 1022 | * The default chunk-size is 2kB, because the NTTY |
| 1023 | * layer has problems with bigger chunks. It will |
| 1024 | * claim to be able to handle more characters than |
| 1025 | * it actually does. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1026 | * |
| 1027 | * FIXME: This can probably go away now except that 64K chunks |
| 1028 | * are too likely to fail unless switched to vmalloc... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | */ |
| 1030 | chunk = 2048; |
| 1031 | if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) |
| 1032 | chunk = 65536; |
| 1033 | if (count < chunk) |
| 1034 | chunk = count; |
| 1035 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1036 | /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | if (tty->write_cnt < chunk) { |
| 1038 | unsigned char *buf; |
| 1039 | |
| 1040 | if (chunk < 1024) |
| 1041 | chunk = 1024; |
| 1042 | |
| 1043 | buf = kmalloc(chunk, GFP_KERNEL); |
| 1044 | if (!buf) { |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1045 | ret = -ENOMEM; |
| 1046 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | } |
| 1048 | kfree(tty->write_buf); |
| 1049 | tty->write_cnt = chunk; |
| 1050 | tty->write_buf = buf; |
| 1051 | } |
| 1052 | |
| 1053 | /* Do the write .. */ |
| 1054 | for (;;) { |
| 1055 | size_t size = count; |
| 1056 | if (size > chunk) |
| 1057 | size = chunk; |
| 1058 | ret = -EFAULT; |
| 1059 | if (copy_from_user(tty->write_buf, buf, size)) |
| 1060 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | ret = write(tty, file, tty->write_buf, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (ret <= 0) |
| 1063 | break; |
| 1064 | written += ret; |
| 1065 | buf += ret; |
| 1066 | count -= ret; |
| 1067 | if (!count) |
| 1068 | break; |
| 1069 | ret = -ERESTARTSYS; |
| 1070 | if (signal_pending(current)) |
| 1071 | break; |
| 1072 | cond_resched(); |
| 1073 | } |
| 1074 | if (written) { |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 1075 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | inode->i_mtime = current_fs_time(inode->i_sb); |
| 1077 | ret = written; |
| 1078 | } |
Alan Cox | 9c1729d | 2007-07-15 23:39:43 -0700 | [diff] [blame] | 1079 | out: |
| 1080 | tty_write_unlock(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | return ret; |
| 1082 | } |
| 1083 | |
Alan Cox | 95f9bfc | 2008-10-13 10:39:23 +0100 | [diff] [blame^] | 1084 | /** |
| 1085 | * tty_write_message - write a message to a certain tty, not just the console. |
| 1086 | * @tty: the destination tty_struct |
| 1087 | * @msg: the message to write |
| 1088 | * |
| 1089 | * This is used for messages that need to be redirected to a specific tty. |
| 1090 | * We don't put it into the syslog queue right now maybe in the future if |
| 1091 | * really needed. |
| 1092 | * |
| 1093 | * We must still hold the BKL and test the CLOSING flag for the moment. |
| 1094 | */ |
| 1095 | |
| 1096 | void tty_write_message(struct tty_struct *tty, char *msg) |
| 1097 | { |
| 1098 | lock_kernel(); |
| 1099 | if (tty) { |
| 1100 | mutex_lock(&tty->atomic_write_lock); |
| 1101 | if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) |
| 1102 | tty->ops->write(tty, msg, strlen(msg)); |
| 1103 | tty_write_unlock(tty); |
| 1104 | } |
| 1105 | unlock_kernel(); |
| 1106 | return; |
| 1107 | } |
| 1108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1110 | /** |
| 1111 | * tty_write - write method for tty device file |
| 1112 | * @file: tty file pointer |
| 1113 | * @buf: user data to write |
| 1114 | * @count: bytes to write |
| 1115 | * @ppos: unused |
| 1116 | * |
| 1117 | * Write data to a tty device via the line discipline. |
| 1118 | * |
| 1119 | * Locking: |
| 1120 | * Locks the line discipline as required |
| 1121 | * Writes to the tty driver are serialized by the atomic_write_lock |
| 1122 | * and are then processed in chunks to the device. The line discipline |
| 1123 | * write method will not be involked in parallel for each device |
| 1124 | * The line discipline write method is called under the big |
| 1125 | * kernel lock for historical reasons. New code should not rely on this. |
| 1126 | */ |
| 1127 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1128 | static ssize_t tty_write(struct file *file, const char __user *buf, |
| 1129 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1131 | struct tty_struct *tty; |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 1132 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | ssize_t ret; |
| 1134 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | tty = (struct tty_struct *)file->private_data; |
| 1137 | if (tty_paranoia_check(tty, inode, "tty_write")) |
| 1138 | return -EIO; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1139 | if (!tty || !tty->ops->write || |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1140 | (test_bit(TTY_IO_ERROR, &tty->flags))) |
| 1141 | return -EIO; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1142 | /* Short term debug to catch buggy drivers */ |
| 1143 | if (tty->ops->write_room == NULL) |
| 1144 | printk(KERN_ERR "tty driver %s lacks a write_room method.\n", |
| 1145 | tty->driver->name); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1146 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1147 | if (!ld->ops->write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | ret = -EIO; |
| 1149 | else |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 1150 | ret = do_tty_write(ld->ops->write, tty, file, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | tty_ldisc_deref(ld); |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1155 | ssize_t redirected_tty_write(struct file *file, const char __user *buf, |
| 1156 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | { |
| 1158 | struct file *p = NULL; |
| 1159 | |
| 1160 | spin_lock(&redirect_lock); |
| 1161 | if (redirect) { |
| 1162 | get_file(redirect); |
| 1163 | p = redirect; |
| 1164 | } |
| 1165 | spin_unlock(&redirect_lock); |
| 1166 | |
| 1167 | if (p) { |
| 1168 | ssize_t res; |
| 1169 | res = vfs_write(p, buf, count, &p->f_pos); |
| 1170 | fput(p); |
| 1171 | return res; |
| 1172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | return tty_write(file, buf, count, ppos); |
| 1174 | } |
| 1175 | |
| 1176 | static char ptychar[] = "pqrstuvwxyzabcde"; |
| 1177 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1178 | /** |
| 1179 | * pty_line_name - generate name for a pty |
| 1180 | * @driver: the tty driver in use |
| 1181 | * @index: the minor number |
| 1182 | * @p: output buffer of at least 6 bytes |
| 1183 | * |
| 1184 | * Generate a name from a driver reference and write it to the output |
| 1185 | * buffer. |
| 1186 | * |
| 1187 | * Locking: None |
| 1188 | */ |
| 1189 | 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] | 1190 | { |
| 1191 | int i = index + driver->name_base; |
| 1192 | /* ->name is initialized to "ttyp", but "tty" is expected */ |
| 1193 | sprintf(p, "%s%c%x", |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1194 | driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, |
| 1195 | ptychar[i >> 4 & 0xf], i & 0xf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1198 | /** |
| 1199 | * pty_line_name - generate name for a tty |
| 1200 | * @driver: the tty driver in use |
| 1201 | * @index: the minor number |
| 1202 | * @p: output buffer of at least 7 bytes |
| 1203 | * |
| 1204 | * Generate a name from a driver reference and write it to the output |
| 1205 | * buffer. |
| 1206 | * |
| 1207 | * Locking: None |
| 1208 | */ |
| 1209 | 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] | 1210 | { |
| 1211 | sprintf(p, "%s%d", driver->name, index + driver->name_base); |
| 1212 | } |
| 1213 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1214 | /** |
| 1215 | * init_dev - initialise a tty device |
| 1216 | * @driver: tty driver we are opening a device on |
| 1217 | * @idx: device index |
| 1218 | * @tty: returned tty structure |
| 1219 | * |
| 1220 | * Prepare a tty device. This may not be a "new" clean device but |
| 1221 | * could also be an active device. The pty drivers require special |
| 1222 | * handling because of this. |
| 1223 | * |
| 1224 | * Locking: |
| 1225 | * The function is called under the tty_mutex, which |
| 1226 | * protects us from the tty struct or driver itself going away. |
| 1227 | * |
| 1228 | * On exit the tty device has the line discipline attached and |
| 1229 | * a reference count of 1. If a pair was created for pty/tty use |
| 1230 | * and the other was a pty master then it too has a reference count of 1. |
| 1231 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | * 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] | 1233 | * failed open. The new code protects the open with a mutex, so it's |
| 1234 | * really quite straightforward. The mutex locking can probably be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | * relaxed for the (most common) case of reopening a tty. |
| 1236 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | static int init_dev(struct tty_driver *driver, int idx, |
| 1239 | struct tty_struct **ret_tty) |
| 1240 | { |
| 1241 | struct tty_struct *tty, *o_tty; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 1242 | struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc; |
| 1243 | struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc; |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1244 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
| 1246 | /* check whether we're reopening an existing tty */ |
| 1247 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { |
| 1248 | tty = devpts_get_tty(idx); |
Aristeu Sergio Rozanski Filho | 5a39e8c | 2007-02-28 20:13:53 -0800 | [diff] [blame] | 1249 | /* |
| 1250 | * If we don't have a tty here on a slave open, it's because |
| 1251 | * the master already started the close process and there's |
| 1252 | * no relation between devpts file and tty anymore. |
| 1253 | */ |
| 1254 | if (!tty && driver->subtype == PTY_TYPE_SLAVE) { |
| 1255 | retval = -EIO; |
| 1256 | goto end_init; |
| 1257 | } |
| 1258 | /* |
| 1259 | * It's safe from now on because init_dev() is called with |
| 1260 | * tty_mutex held and release_dev() won't change tty->count |
| 1261 | * or tty->flags without having to grab tty_mutex |
| 1262 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | if (tty && driver->subtype == PTY_TYPE_MASTER) |
| 1264 | tty = tty->link; |
| 1265 | } else { |
| 1266 | tty = driver->ttys[idx]; |
| 1267 | } |
| 1268 | if (tty) goto fast_track; |
| 1269 | |
| 1270 | /* |
| 1271 | * First time open is complex, especially for PTY devices. |
| 1272 | * This code guarantees that either everything succeeds and the |
| 1273 | * TTY is ready for operation, or else the table slots are vacated |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1274 | * and the allocated memory released. (Except that the termios |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | * and locked termios may be retained.) |
| 1276 | */ |
| 1277 | |
| 1278 | if (!try_module_get(driver->owner)) { |
| 1279 | retval = -ENODEV; |
| 1280 | goto end_init; |
| 1281 | } |
| 1282 | |
| 1283 | o_tty = NULL; |
| 1284 | tp = o_tp = NULL; |
| 1285 | ltp = o_ltp = NULL; |
| 1286 | |
| 1287 | tty = alloc_tty_struct(); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1288 | if (!tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | goto fail_no_mem; |
| 1290 | initialize_tty_struct(tty); |
| 1291 | tty->driver = driver; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1292 | tty->ops = driver->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | tty->index = idx; |
| 1294 | tty_line_name(driver, idx, tty->name); |
| 1295 | |
| 1296 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { |
| 1297 | tp_loc = &tty->termios; |
| 1298 | ltp_loc = &tty->termios_locked; |
| 1299 | } else { |
| 1300 | tp_loc = &driver->termios[idx]; |
| 1301 | ltp_loc = &driver->termios_locked[idx]; |
| 1302 | } |
| 1303 | |
| 1304 | if (!*tp_loc) { |
Jesper Juhl | abcb1ff3 | 2007-08-24 02:28:42 +0200 | [diff] [blame] | 1305 | tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | if (!tp) |
| 1307 | goto free_mem_out; |
| 1308 | *tp = driver->init_termios; |
| 1309 | } |
| 1310 | |
| 1311 | if (!*ltp_loc) { |
Jean Delvare | 506eb99 | 2007-07-15 23:40:14 -0700 | [diff] [blame] | 1312 | ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if (!ltp) |
| 1314 | goto free_mem_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | if (driver->type == TTY_DRIVER_TYPE_PTY) { |
| 1318 | o_tty = alloc_tty_struct(); |
| 1319 | if (!o_tty) |
| 1320 | goto free_mem_out; |
Alan Cox | 6f967f7 | 2008-10-13 10:37:36 +0100 | [diff] [blame] | 1321 | if (!try_module_get(driver->other->owner)) { |
| 1322 | /* This cannot in fact currently happen */ |
| 1323 | free_tty_struct(o_tty); |
| 1324 | o_tty = NULL; |
| 1325 | goto free_mem_out; |
| 1326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | initialize_tty_struct(o_tty); |
| 1328 | o_tty->driver = driver->other; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1329 | o_tty->ops = driver->ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | o_tty->index = idx; |
| 1331 | tty_line_name(driver->other, idx, o_tty->name); |
| 1332 | |
| 1333 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { |
| 1334 | o_tp_loc = &o_tty->termios; |
| 1335 | o_ltp_loc = &o_tty->termios_locked; |
| 1336 | } else { |
| 1337 | o_tp_loc = &driver->other->termios[idx]; |
| 1338 | o_ltp_loc = &driver->other->termios_locked[idx]; |
| 1339 | } |
| 1340 | |
| 1341 | if (!*o_tp_loc) { |
Jesper Juhl | abcb1ff3 | 2007-08-24 02:28:42 +0200 | [diff] [blame] | 1342 | o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | if (!o_tp) |
| 1344 | goto free_mem_out; |
| 1345 | *o_tp = driver->other->init_termios; |
| 1346 | } |
| 1347 | |
| 1348 | if (!*o_ltp_loc) { |
Jean Delvare | 506eb99 | 2007-07-15 23:40:14 -0700 | [diff] [blame] | 1349 | o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | if (!o_ltp) |
| 1351 | goto free_mem_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Everything allocated ... set up the o_tty structure. |
| 1356 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1357 | if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | driver->other->ttys[idx] = o_tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | if (!*o_tp_loc) |
| 1360 | *o_tp_loc = o_tp; |
| 1361 | if (!*o_ltp_loc) |
| 1362 | *o_ltp_loc = o_ltp; |
| 1363 | o_tty->termios = *o_tp_loc; |
| 1364 | o_tty->termios_locked = *o_ltp_loc; |
| 1365 | driver->other->refcount++; |
| 1366 | if (driver->subtype == PTY_TYPE_MASTER) |
| 1367 | o_tty->count++; |
| 1368 | |
| 1369 | /* Establish the links in both directions */ |
| 1370 | tty->link = o_tty; |
| 1371 | o_tty->link = tty; |
| 1372 | } |
| 1373 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1374 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | * All structures have been allocated, so now we install them. |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1376 | * Failures after this point use release_tty to clean up, so |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | * there's no need to null out the local pointers. |
| 1378 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1379 | if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | driver->ttys[idx] = tty; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | if (!*tp_loc) |
| 1383 | *tp_loc = tp; |
| 1384 | if (!*ltp_loc) |
| 1385 | *ltp_loc = ltp; |
| 1386 | tty->termios = *tp_loc; |
| 1387 | tty->termios_locked = *ltp_loc; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 1388 | /* Compatibility until drivers always set this */ |
| 1389 | tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios); |
| 1390 | tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | driver->refcount++; |
| 1392 | tty->count++; |
| 1393 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1394 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | * Structures all installed ... call the ldisc open routines. |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1396 | * If we fail here just call release_tty to clean up. No need |
| 1397 | * to decrement the use counts, as release_tty doesn't care. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | */ |
| 1399 | |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 1400 | retval = tty_ldisc_setup(tty, o_tty); |
| 1401 | |
| 1402 | if (retval) |
| 1403 | goto release_mem_out; |
| 1404 | goto success; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
| 1406 | /* |
| 1407 | * This fast open can be used if the tty is already open. |
| 1408 | * No memory is allocated, and the only failures are from |
| 1409 | * attempting to open a closing tty or attempting multiple |
| 1410 | * opens on a pty master. |
| 1411 | */ |
| 1412 | fast_track: |
| 1413 | if (test_bit(TTY_CLOSING, &tty->flags)) { |
| 1414 | retval = -EIO; |
| 1415 | goto end_init; |
| 1416 | } |
| 1417 | if (driver->type == TTY_DRIVER_TYPE_PTY && |
| 1418 | driver->subtype == PTY_TYPE_MASTER) { |
| 1419 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1420 | * special case for PTY masters: only one open permitted, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | * and the slave side open count is incremented as well. |
| 1422 | */ |
| 1423 | if (tty->count) { |
| 1424 | retval = -EIO; |
| 1425 | goto end_init; |
| 1426 | } |
| 1427 | tty->link->count++; |
| 1428 | } |
| 1429 | tty->count++; |
| 1430 | tty->driver = driver; /* N.B. why do this every time?? */ |
| 1431 | |
| 1432 | /* FIXME */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1433 | if (!test_bit(TTY_LDISC, &tty->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | printk(KERN_ERR "init_dev but no ldisc\n"); |
| 1435 | success: |
| 1436 | *ret_tty = tty; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1437 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1438 | /* All paths come through here to release the mutex */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | end_init: |
| 1440 | return retval; |
| 1441 | |
| 1442 | /* Release locally allocated memory ... nothing placed in slots */ |
| 1443 | free_mem_out: |
Jesper Juhl | 735d566 | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 1444 | kfree(o_tp); |
Alan Cox | 6f967f7 | 2008-10-13 10:37:36 +0100 | [diff] [blame] | 1445 | if (o_tty) { |
| 1446 | module_put(o_tty->driver->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | free_tty_struct(o_tty); |
Alan Cox | 6f967f7 | 2008-10-13 10:37:36 +0100 | [diff] [blame] | 1448 | } |
Jesper Juhl | 735d566 | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 1449 | kfree(ltp); |
| 1450 | kfree(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | free_tty_struct(tty); |
| 1452 | |
| 1453 | fail_no_mem: |
| 1454 | module_put(driver->owner); |
| 1455 | retval = -ENOMEM; |
| 1456 | goto end_init; |
| 1457 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1458 | /* call the tty release_tty routine to clean out this slot */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | release_mem_out: |
Akinobu Mita | 4050914 | 2006-09-29 02:01:27 -0700 | [diff] [blame] | 1460 | if (printk_ratelimit()) |
| 1461 | printk(KERN_INFO "init_dev: ldisc open failed, " |
| 1462 | "clearing slot %d\n", idx); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1463 | release_tty(tty, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | goto end_init; |
| 1465 | } |
| 1466 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1467 | /** |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1468 | * release_one_tty - release tty structure memory |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1469 | * @kref: kref of tty we are obliterating |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1470 | * |
| 1471 | * Releases memory associated with a tty structure, and clears out the |
| 1472 | * driver table slots. This function is called when a device is no longer |
| 1473 | * in use. It also gets called when setup of a device fails. |
| 1474 | * |
| 1475 | * Locking: |
| 1476 | * tty_mutex - sometimes only |
| 1477 | * takes the file list lock internally when working on the list |
| 1478 | * of ttys that the driver keeps. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | */ |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1480 | static void release_one_tty(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | { |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1482 | struct tty_struct *tty = container_of(kref, struct tty_struct, kref); |
Alan Cox | 6f967f7 | 2008-10-13 10:37:36 +0100 | [diff] [blame] | 1483 | struct tty_driver *driver = tty->driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM; |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1485 | struct ktermios *tp; |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1486 | int idx = tty->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
| 1488 | if (!devpts) |
| 1489 | tty->driver->ttys[idx] = NULL; |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) { |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1492 | /* FIXME: Locking on ->termios array */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | tp = tty->termios; |
| 1494 | if (!devpts) |
| 1495 | tty->driver->termios[idx] = NULL; |
| 1496 | kfree(tp); |
| 1497 | |
| 1498 | tp = tty->termios_locked; |
| 1499 | if (!devpts) |
| 1500 | tty->driver->termios_locked[idx] = NULL; |
| 1501 | kfree(tp); |
| 1502 | } |
| 1503 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | tty->magic = 0; |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1506 | /* FIXME: locking on tty->driver->refcount */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | tty->driver->refcount--; |
Alan Cox | 6f967f7 | 2008-10-13 10:37:36 +0100 | [diff] [blame] | 1508 | module_put(driver->owner); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | file_list_lock(); |
| 1511 | list_del_init(&tty->tty_files); |
| 1512 | file_list_unlock(); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | free_tty_struct(tty); |
| 1515 | } |
| 1516 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1517 | /** |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1518 | * tty_kref_put - release a tty kref |
| 1519 | * @tty: tty device |
| 1520 | * |
| 1521 | * Release a reference to a tty device and if need be let the kref |
| 1522 | * layer destruct the object for us |
| 1523 | */ |
| 1524 | |
| 1525 | void tty_kref_put(struct tty_struct *tty) |
| 1526 | { |
| 1527 | if (tty) |
| 1528 | kref_put(&tty->kref, release_one_tty); |
| 1529 | } |
| 1530 | EXPORT_SYMBOL(tty_kref_put); |
| 1531 | |
| 1532 | /** |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1533 | * release_tty - release tty structure memory |
| 1534 | * |
| 1535 | * Release both @tty and a possible linked partner (think pty pair), |
| 1536 | * and decrement the refcount of the backing module. |
| 1537 | * |
| 1538 | * Locking: |
| 1539 | * tty_mutex - sometimes only |
| 1540 | * takes the file list lock internally when working on the list |
| 1541 | * of ttys that the driver keeps. |
| 1542 | * FIXME: should we require tty_mutex is held here ?? |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1543 | * |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1544 | */ |
| 1545 | static void release_tty(struct tty_struct *tty, int idx) |
| 1546 | { |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1547 | /* This should always be true but check for the moment */ |
| 1548 | WARN_ON(tty->index != idx); |
| 1549 | |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1550 | if (tty->link) |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1551 | tty_kref_put(tty->link); |
| 1552 | tty_kref_put(tty); |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1553 | } |
| 1554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | /* |
| 1556 | * Even releasing the tty structures is a tricky business.. We have |
| 1557 | * to be very careful that the structures are all released at the |
| 1558 | * same time, as interrupts might otherwise get the wrong pointers. |
| 1559 | * |
| 1560 | * WSH 09/09/97: rewritten to avoid some nasty race conditions that could |
| 1561 | * lead to double frees or releasing memory still in use. |
| 1562 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1563 | static void release_dev(struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | { |
| 1565 | struct tty_struct *tty, *o_tty; |
| 1566 | int pty_master, tty_closing, o_tty_closing, do_sleep; |
Paul Fulghum | 14a6283 | 2006-04-10 22:54:19 -0700 | [diff] [blame] | 1567 | int devpts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | int idx; |
| 1569 | char buf[64]; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | tty = (struct tty_struct *)filp->private_data; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1572 | if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, |
| 1573 | "release_dev")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | return; |
| 1575 | |
| 1576 | check_tty_count(tty, "release_dev"); |
| 1577 | |
| 1578 | tty_fasync(-1, filp, 0); |
| 1579 | |
| 1580 | idx = tty->index; |
| 1581 | pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 1582 | tty->driver->subtype == PTY_TYPE_MASTER); |
| 1583 | devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | o_tty = tty->link; |
| 1585 | |
| 1586 | #ifdef TTY_PARANOIA_CHECK |
| 1587 | if (idx < 0 || idx >= tty->driver->num) { |
| 1588 | printk(KERN_DEBUG "release_dev: bad idx when trying to " |
| 1589 | "free (%s)\n", tty->name); |
| 1590 | return; |
| 1591 | } |
| 1592 | if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { |
| 1593 | if (tty != tty->driver->ttys[idx]) { |
| 1594 | printk(KERN_DEBUG "release_dev: driver.table[%d] not tty " |
| 1595 | "for (%s)\n", idx, tty->name); |
| 1596 | return; |
| 1597 | } |
| 1598 | if (tty->termios != tty->driver->termios[idx]) { |
| 1599 | printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios " |
| 1600 | "for (%s)\n", |
| 1601 | idx, tty->name); |
| 1602 | return; |
| 1603 | } |
| 1604 | if (tty->termios_locked != tty->driver->termios_locked[idx]) { |
| 1605 | printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not " |
| 1606 | "termios_locked for (%s)\n", |
| 1607 | idx, tty->name); |
| 1608 | return; |
| 1609 | } |
| 1610 | } |
| 1611 | #endif |
| 1612 | |
| 1613 | #ifdef TTY_DEBUG_HANGUP |
| 1614 | printk(KERN_DEBUG "release_dev of %s (tty count=%d)...", |
| 1615 | tty_name(tty, buf), tty->count); |
| 1616 | #endif |
| 1617 | |
| 1618 | #ifdef TTY_PARANOIA_CHECK |
| 1619 | if (tty->driver->other && |
| 1620 | !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { |
| 1621 | if (o_tty != tty->driver->other->ttys[idx]) { |
| 1622 | printk(KERN_DEBUG "release_dev: other->table[%d] " |
| 1623 | "not o_tty for (%s)\n", |
| 1624 | idx, tty->name); |
| 1625 | return; |
| 1626 | } |
| 1627 | if (o_tty->termios != tty->driver->other->termios[idx]) { |
| 1628 | printk(KERN_DEBUG "release_dev: other->termios[%d] " |
| 1629 | "not o_termios for (%s)\n", |
| 1630 | idx, tty->name); |
| 1631 | return; |
| 1632 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1633 | if (o_tty->termios_locked != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | tty->driver->other->termios_locked[idx]) { |
| 1635 | printk(KERN_DEBUG "release_dev: other->termios_locked[" |
| 1636 | "%d] not o_termios_locked for (%s)\n", |
| 1637 | idx, tty->name); |
| 1638 | return; |
| 1639 | } |
| 1640 | if (o_tty->link != tty) { |
| 1641 | printk(KERN_DEBUG "release_dev: bad pty pointers\n"); |
| 1642 | return; |
| 1643 | } |
| 1644 | } |
| 1645 | #endif |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1646 | if (tty->ops->close) |
| 1647 | tty->ops->close(tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | |
| 1649 | /* |
| 1650 | * Sanity check: if tty->count is going to zero, there shouldn't be |
| 1651 | * any waiters on tty->read_wait or tty->write_wait. We test the |
| 1652 | * wait queues and kick everyone out _before_ actually starting to |
| 1653 | * close. This ensures that we won't block while releasing the tty |
| 1654 | * structure. |
| 1655 | * |
| 1656 | * The test for the o_tty closing is necessary, since the master and |
| 1657 | * slave sides may close in any order. If the slave side closes out |
| 1658 | * first, its count will be one, since the master side holds an open. |
| 1659 | * Thus this test wouldn't be triggered at the time the slave closes, |
| 1660 | * so we do it now. |
| 1661 | * |
| 1662 | * Note that it's possible for the tty to be opened again while we're |
| 1663 | * flushing out waiters. By recalculating the closing flags before |
| 1664 | * each iteration we avoid any problems. |
| 1665 | */ |
| 1666 | while (1) { |
| 1667 | /* Guard against races with tty->count changes elsewhere and |
| 1668 | opens on /dev/tty */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1669 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1670 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | tty_closing = tty->count <= 1; |
| 1672 | o_tty_closing = o_tty && |
| 1673 | (o_tty->count <= (pty_master ? 1 : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | do_sleep = 0; |
| 1675 | |
| 1676 | if (tty_closing) { |
| 1677 | if (waitqueue_active(&tty->read_wait)) { |
| 1678 | wake_up(&tty->read_wait); |
| 1679 | do_sleep++; |
| 1680 | } |
| 1681 | if (waitqueue_active(&tty->write_wait)) { |
| 1682 | wake_up(&tty->write_wait); |
| 1683 | do_sleep++; |
| 1684 | } |
| 1685 | } |
| 1686 | if (o_tty_closing) { |
| 1687 | if (waitqueue_active(&o_tty->read_wait)) { |
| 1688 | wake_up(&o_tty->read_wait); |
| 1689 | do_sleep++; |
| 1690 | } |
| 1691 | if (waitqueue_active(&o_tty->write_wait)) { |
| 1692 | wake_up(&o_tty->write_wait); |
| 1693 | do_sleep++; |
| 1694 | } |
| 1695 | } |
| 1696 | if (!do_sleep) |
| 1697 | break; |
| 1698 | |
| 1699 | printk(KERN_WARNING "release_dev: %s: read/write wait queue " |
| 1700 | "active!\n", tty_name(tty, buf)); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1701 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | schedule(); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | |
| 1705 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1706 | * The closing flags are now consistent with the open counts on |
| 1707 | * both sides, and we've completed the last operation that could |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | * block, so it's safe to proceed with closing. |
| 1709 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | if (pty_master) { |
| 1711 | if (--o_tty->count < 0) { |
| 1712 | printk(KERN_WARNING "release_dev: bad pty slave count " |
| 1713 | "(%d) for %s\n", |
| 1714 | o_tty->count, tty_name(o_tty, buf)); |
| 1715 | o_tty->count = 0; |
| 1716 | } |
| 1717 | } |
| 1718 | if (--tty->count < 0) { |
| 1719 | printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n", |
| 1720 | tty->count, tty_name(tty, buf)); |
| 1721 | tty->count = 0; |
| 1722 | } |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | /* |
| 1725 | * We've decremented tty->count, so we need to remove this file |
| 1726 | * descriptor off the tty->tty_files list; this serves two |
| 1727 | * purposes: |
| 1728 | * - check_tty_count sees the correct number of file descriptors |
| 1729 | * associated with this tty. |
| 1730 | * - do_tty_hangup no longer sees this file descriptor as |
| 1731 | * something that needs to be handled for hangups. |
| 1732 | */ |
| 1733 | file_kill(filp); |
| 1734 | filp->private_data = NULL; |
| 1735 | |
| 1736 | /* |
| 1737 | * Perform some housekeeping before deciding whether to return. |
| 1738 | * |
| 1739 | * Set the TTY_CLOSING flag if this was the last open. In the |
| 1740 | * case of a pty we may have to wait around for the other side |
| 1741 | * to close, and TTY_CLOSING makes sure we can't be reopened. |
| 1742 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1743 | if (tty_closing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | set_bit(TTY_CLOSING, &tty->flags); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1745 | if (o_tty_closing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | set_bit(TTY_CLOSING, &o_tty->flags); |
| 1747 | |
| 1748 | /* |
| 1749 | * If _either_ side is closing, make sure there aren't any |
| 1750 | * processes that still think tty or o_tty is their controlling |
| 1751 | * tty. |
| 1752 | */ |
| 1753 | if (tty_closing || o_tty_closing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | read_lock(&tasklist_lock); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1755 | session_clear_tty(tty->session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | if (o_tty) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1757 | session_clear_tty(o_tty->session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | read_unlock(&tasklist_lock); |
| 1759 | } |
| 1760 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1761 | mutex_unlock(&tty_mutex); |
Paul Fulghum | da96582 | 2006-02-14 13:53:00 -0800 | [diff] [blame] | 1762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | /* check whether both sides are closing ... */ |
| 1764 | if (!tty_closing || (o_tty && !o_tty_closing)) |
| 1765 | return; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | #ifdef TTY_DEBUG_HANGUP |
| 1768 | printk(KERN_DEBUG "freeing tty structure..."); |
| 1769 | #endif |
| 1770 | /* |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 1771 | * Ask the line discipline code to release its structures |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | */ |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 1773 | tty_ldisc_release(tty, o_tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | /* |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1775 | * The release_tty function takes care of the details of clearing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | * the slots and preserving the termios structure. |
| 1777 | */ |
Christoph Hellwig | d5698c2 | 2007-02-10 01:46:46 -0800 | [diff] [blame] | 1778 | release_tty(tty, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | /* Make this pty number available for reallocation */ |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 1781 | if (devpts) |
| 1782 | devpts_kill_index(idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1785 | /** |
| 1786 | * tty_open - open a tty device |
| 1787 | * @inode: inode of device file |
| 1788 | * @filp: file pointer to tty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1790 | * tty_open and tty_release keep up the tty count that contains the |
| 1791 | * number of opens done on a tty. We cannot use the inode-count, as |
| 1792 | * different inodes might point to the same tty. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1794 | * Open-counting is needed for pty masters, as well as for keeping |
| 1795 | * track of serial lines: DTR is dropped when the last close happens. |
| 1796 | * (This is not done solely through tty->count, now. - Ted 1/27/92) |
| 1797 | * |
| 1798 | * The termios state of a pty is reset on first open so that |
| 1799 | * settings don't persist across reuse. |
| 1800 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1801 | * Locking: tty_mutex protects tty, get_tty_driver and init_dev work. |
| 1802 | * tty->count should protect the rest. |
| 1803 | * ->siglock protects ->signal/->sighand |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1805 | |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 1806 | static int __tty_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | { |
| 1808 | struct tty_struct *tty; |
| 1809 | int noctty, retval; |
| 1810 | struct tty_driver *driver; |
| 1811 | int index; |
| 1812 | dev_t device = inode->i_rdev; |
| 1813 | unsigned short saved_flags = filp->f_flags; |
| 1814 | |
| 1815 | nonseekable_open(inode, filp); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | retry_open: |
| 1818 | noctty = filp->f_flags & O_NOCTTY; |
| 1819 | index = -1; |
| 1820 | retval = 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1821 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1822 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1824 | if (device == MKDEV(TTYAUX_MAJOR, 0)) { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1825 | tty = get_current_tty(); |
| 1826 | if (!tty) { |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1827 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | return -ENXIO; |
| 1829 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1830 | driver = tty->driver; |
| 1831 | index = tty->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ |
| 1833 | /* noctty = 1; */ |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 1834 | /* FIXME: Should we take a driver reference ? */ |
| 1835 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | goto got_driver; |
| 1837 | } |
| 1838 | #ifdef CONFIG_VT |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1839 | if (device == MKDEV(TTY_MAJOR, 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | extern struct tty_driver *console_driver; |
| 1841 | driver = console_driver; |
| 1842 | index = fg_console; |
| 1843 | noctty = 1; |
| 1844 | goto got_driver; |
| 1845 | } |
| 1846 | #endif |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1847 | if (device == MKDEV(TTYAUX_MAJOR, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | driver = console_device(&index); |
| 1849 | if (driver) { |
| 1850 | /* Don't let /dev/console block */ |
| 1851 | filp->f_flags |= O_NONBLOCK; |
| 1852 | noctty = 1; |
| 1853 | goto got_driver; |
| 1854 | } |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1855 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | return -ENODEV; |
| 1857 | } |
| 1858 | |
| 1859 | driver = get_tty_driver(device, &index); |
| 1860 | if (!driver) { |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1861 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | return -ENODEV; |
| 1863 | } |
| 1864 | got_driver: |
| 1865 | retval = init_dev(driver, index, &tty); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1866 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | if (retval) |
| 1868 | return retval; |
| 1869 | |
| 1870 | filp->private_data = tty; |
| 1871 | file_move(filp, &tty->tty_files); |
| 1872 | check_tty_count(tty, "tty_open"); |
| 1873 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 1874 | tty->driver->subtype == PTY_TYPE_MASTER) |
| 1875 | noctty = 1; |
| 1876 | #ifdef TTY_DEBUG_HANGUP |
| 1877 | printk(KERN_DEBUG "opening %s...", tty->name); |
| 1878 | #endif |
| 1879 | if (!retval) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1880 | if (tty->ops->open) |
| 1881 | retval = tty->ops->open(tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | else |
| 1883 | retval = -ENODEV; |
| 1884 | } |
| 1885 | filp->f_flags = saved_flags; |
| 1886 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1887 | if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && |
| 1888 | !capable(CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | retval = -EBUSY; |
| 1890 | |
| 1891 | if (retval) { |
| 1892 | #ifdef TTY_DEBUG_HANGUP |
| 1893 | printk(KERN_DEBUG "error %d in opening %s...", retval, |
| 1894 | tty->name); |
| 1895 | #endif |
| 1896 | release_dev(filp); |
| 1897 | if (retval != -ERESTARTSYS) |
| 1898 | return retval; |
| 1899 | if (signal_pending(current)) |
| 1900 | return retval; |
| 1901 | schedule(); |
| 1902 | /* |
| 1903 | * Need to reset f_op in case a hangup happened. |
| 1904 | */ |
| 1905 | if (filp->f_op == &hung_up_tty_fops) |
| 1906 | filp->f_op = &tty_fops; |
| 1907 | goto retry_open; |
| 1908 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1909 | |
| 1910 | mutex_lock(&tty_mutex); |
| 1911 | spin_lock_irq(¤t->sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | if (!noctty && |
| 1913 | current->signal->leader && |
| 1914 | !current->signal->tty && |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1915 | tty->session == NULL) |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 1916 | __proc_set_tty(current, tty); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1917 | spin_unlock_irq(¤t->sighand->siglock); |
| 1918 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | return 0; |
| 1920 | } |
| 1921 | |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 1922 | /* BKL pushdown: scary code avoidance wrapper */ |
| 1923 | static int tty_open(struct inode *inode, struct file *filp) |
| 1924 | { |
| 1925 | int ret; |
| 1926 | |
| 1927 | lock_kernel(); |
| 1928 | ret = __tty_open(inode, filp); |
| 1929 | unlock_kernel(); |
| 1930 | return ret; |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | #ifdef CONFIG_UNIX98_PTYS |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1936 | /** |
| 1937 | * ptmx_open - open a unix 98 pty master |
| 1938 | * @inode: inode of device file |
| 1939 | * @filp: file pointer to tty |
| 1940 | * |
| 1941 | * Allocate a unix98 pty master device from the ptmx driver. |
| 1942 | * |
| 1943 | * Locking: tty_mutex protects theinit_dev work. tty->count should |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1944 | * protect the rest. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1945 | * allocated_ptys_lock handles the list of free pty numbers |
| 1946 | */ |
| 1947 | |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 1948 | static int __ptmx_open(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | { |
| 1950 | struct tty_struct *tty; |
| 1951 | int retval; |
| 1952 | int index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | |
| 1954 | nonseekable_open(inode, filp); |
| 1955 | |
| 1956 | /* find a device that is not in use. */ |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 1957 | index = devpts_new_index(); |
| 1958 | if (index < 0) |
| 1959 | return index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1961 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | retval = init_dev(ptm_driver, index, &tty); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1963 | mutex_unlock(&tty_mutex); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 1964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | if (retval) |
| 1966 | goto out; |
| 1967 | |
| 1968 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ |
| 1969 | filp->private_data = tty; |
| 1970 | file_move(filp, &tty->tty_files); |
| 1971 | |
Sukadev Bhattiprolu | 4f8f9d6 | 2008-04-30 00:54:20 -0700 | [diff] [blame] | 1972 | retval = devpts_pty_new(tty->link); |
| 1973 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | goto out1; |
| 1975 | |
Hiroshi Shimamoto | 86a9653 | 2008-04-30 00:54:20 -0700 | [diff] [blame] | 1976 | check_tty_count(tty, "ptmx_open"); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1977 | retval = ptm_driver->ops->open(tty, filp); |
Miloslav Trmac | 4112622 | 2008-04-18 13:30:14 -0700 | [diff] [blame] | 1978 | if (!retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | return 0; |
| 1980 | out1: |
| 1981 | release_dev(filp); |
Paul Fulghum | 9453a5a | 2006-04-10 22:54:18 -0700 | [diff] [blame] | 1982 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | out: |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 1984 | devpts_kill_index(index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | return retval; |
| 1986 | } |
Jonathan Corbet | 39d95b9 | 2008-05-16 09:10:50 -0600 | [diff] [blame] | 1987 | |
| 1988 | static int ptmx_open(struct inode *inode, struct file *filp) |
| 1989 | { |
| 1990 | int ret; |
| 1991 | |
| 1992 | lock_kernel(); |
| 1993 | ret = __ptmx_open(inode, filp); |
| 1994 | unlock_kernel(); |
| 1995 | return ret; |
| 1996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | #endif |
| 1998 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 1999 | /** |
| 2000 | * tty_release - vfs callback for close |
| 2001 | * @inode: inode of tty |
| 2002 | * @filp: file pointer for handle to tty |
| 2003 | * |
| 2004 | * Called the last time each file handle is closed that references |
| 2005 | * this tty. There may however be several such references. |
| 2006 | * |
| 2007 | * Locking: |
| 2008 | * Takes bkl. See release_dev |
| 2009 | */ |
| 2010 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2011 | static int tty_release(struct inode *inode, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | { |
| 2013 | lock_kernel(); |
| 2014 | release_dev(filp); |
| 2015 | unlock_kernel(); |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2019 | /** |
| 2020 | * tty_poll - check tty status |
| 2021 | * @filp: file being polled |
| 2022 | * @wait: poll wait structures to update |
| 2023 | * |
| 2024 | * Call the line discipline polling method to obtain the poll |
| 2025 | * status of the device. |
| 2026 | * |
| 2027 | * Locking: locks called line discipline but ldisc poll method |
| 2028 | * may be re-entered freely by other callers. |
| 2029 | */ |
| 2030 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2031 | static unsigned int tty_poll(struct file *filp, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2033 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | struct tty_ldisc *ld; |
| 2035 | int ret = 0; |
| 2036 | |
| 2037 | tty = (struct tty_struct *)filp->private_data; |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 2038 | 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] | 2039 | return 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2042 | if (ld->ops->poll) |
| 2043 | ret = (ld->ops->poll)(tty, filp, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | tty_ldisc_deref(ld); |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2048 | static int tty_fasync(int fd, struct file *filp, int on) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2050 | struct tty_struct *tty; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2051 | unsigned long flags; |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2052 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2054 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | tty = (struct tty_struct *)filp->private_data; |
Josef Sipek | a7113a9 | 2006-12-08 02:36:55 -0800 | [diff] [blame] | 2056 | 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] | 2057 | goto out; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | retval = fasync_helper(fd, filp, on, &tty->fasync); |
| 2060 | if (retval <= 0) |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2061 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | |
| 2063 | if (on) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2064 | enum pid_type type; |
| 2065 | struct pid *pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | if (!waitqueue_active(&tty->read_wait)) |
| 2067 | tty->minimum_to_wake = 1; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2068 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2069 | if (tty->pgrp) { |
| 2070 | pid = tty->pgrp; |
| 2071 | type = PIDTYPE_PGID; |
| 2072 | } else { |
| 2073 | pid = task_pid(current); |
| 2074 | type = PIDTYPE_PID; |
| 2075 | } |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2076 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2077 | retval = __f_setown(filp, pid, type, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | if (retval) |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2079 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | } else { |
| 2081 | if (!tty->fasync && !waitqueue_active(&tty->read_wait)) |
| 2082 | tty->minimum_to_wake = N_TTY_BUF_SIZE; |
| 2083 | } |
Jonathan Corbet | 5d1e323 | 2008-06-19 16:04:53 -0600 | [diff] [blame] | 2084 | retval = 0; |
| 2085 | out: |
| 2086 | unlock_kernel(); |
| 2087 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2090 | /** |
| 2091 | * tiocsti - fake input character |
| 2092 | * @tty: tty to fake input into |
| 2093 | * @p: pointer to character |
| 2094 | * |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 2095 | * Fake input to a tty device. Does the necessary locking and |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2096 | * input management. |
| 2097 | * |
| 2098 | * FIXME: does not honour flow control ?? |
| 2099 | * |
| 2100 | * Locking: |
| 2101 | * Called functions take tty_ldisc_lock |
| 2102 | * current->signal->tty check is safe without locks |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 2103 | * |
| 2104 | * FIXME: may race normal receive processing |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2105 | */ |
| 2106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | static int tiocsti(struct tty_struct *tty, char __user *p) |
| 2108 | { |
| 2109 | char ch, mbz = 0; |
| 2110 | struct tty_ldisc *ld; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) |
| 2113 | return -EPERM; |
| 2114 | if (get_user(ch, p)) |
| 2115 | return -EFAULT; |
| 2116 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2117 | ld->ops->receive_buf(tty, &ch, &mbz, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | tty_ldisc_deref(ld); |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2122 | /** |
| 2123 | * tiocgwinsz - implement window query ioctl |
| 2124 | * @tty; tty |
| 2125 | * @arg: user buffer for result |
| 2126 | * |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2127 | * Copies the kernel idea of the window size into the user buffer. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2128 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2129 | * Locking: tty->termios_mutex is taken to ensure the winsize data |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2130 | * is consistent. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2131 | */ |
| 2132 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2133 | static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | { |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2135 | int err; |
| 2136 | |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 2137 | mutex_lock(&tty->termios_mutex); |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2138 | err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 2139 | mutex_unlock(&tty->termios_mutex); |
Alan Cox | 808a0d3 | 2006-09-29 02:00:40 -0700 | [diff] [blame] | 2140 | |
| 2141 | return err ? -EFAULT: 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2144 | /** |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2145 | * tty_do_resize - resize event |
| 2146 | * @tty: tty being resized |
Alan Cox | a152db7 | 2008-08-26 19:55:58 +0100 | [diff] [blame] | 2147 | * @real_tty: real tty (not the same as tty if using a pty/tty pair) |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2148 | * @rows: rows (character) |
| 2149 | * @cols: cols (character) |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2150 | * |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2151 | * Update the termios variables and send the neccessary signals to |
| 2152 | * peform a terminal resize correctly |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2153 | */ |
| 2154 | |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2155 | int tty_do_resize(struct tty_struct *tty, struct tty_struct *real_tty, |
| 2156 | struct winsize *ws) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | { |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2158 | struct pid *pgrp, *rpgrp; |
| 2159 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | |
Alan Cox | a152db7 | 2008-08-26 19:55:58 +0100 | [diff] [blame] | 2161 | /* For a PTY we need to lock the tty side */ |
| 2162 | mutex_lock(&real_tty->termios_mutex); |
Alan Cox | f4d2a6c | 2008-10-13 10:39:01 +0100 | [diff] [blame] | 2163 | if (!memcmp(ws, &real_tty->winsize, sizeof(*ws))) |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 2164 | goto done; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2165 | /* Get the PID values and reference them so we can |
| 2166 | avoid holding the tty ctrl lock while sending signals */ |
| 2167 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 2168 | pgrp = get_pid(tty->pgrp); |
| 2169 | rpgrp = get_pid(real_tty->pgrp); |
| 2170 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 2171 | |
| 2172 | if (pgrp) |
| 2173 | kill_pgrp(pgrp, SIGWINCH, 1); |
| 2174 | if (rpgrp != pgrp && rpgrp) |
| 2175 | kill_pgrp(rpgrp, SIGWINCH, 1); |
| 2176 | |
| 2177 | put_pid(pgrp); |
| 2178 | put_pid(rpgrp); |
| 2179 | |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2180 | tty->winsize = *ws; |
| 2181 | real_tty->winsize = *ws; |
Alan Cox | ca9bda0 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 2182 | done: |
Alan Cox | a152db7 | 2008-08-26 19:55:58 +0100 | [diff] [blame] | 2183 | mutex_unlock(&real_tty->termios_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | return 0; |
| 2185 | } |
| 2186 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2187 | /** |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 2188 | * tiocswinsz - implement window size set ioctl |
| 2189 | * @tty; tty |
| 2190 | * @arg: user buffer for result |
| 2191 | * |
| 2192 | * Copies the user idea of the window size to the kernel. Traditionally |
| 2193 | * this is just advisory information but for the Linux console it |
| 2194 | * actually has driver level meaning and triggers a VC resize. |
| 2195 | * |
| 2196 | * Locking: |
| 2197 | * Driver dependant. The default do_resize method takes the |
| 2198 | * tty termios mutex and ctrl_lock. The console takes its own lock |
| 2199 | * then calls into the default method. |
| 2200 | */ |
| 2201 | |
| 2202 | static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty, |
| 2203 | struct winsize __user *arg) |
| 2204 | { |
| 2205 | struct winsize tmp_ws; |
| 2206 | if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) |
| 2207 | return -EFAULT; |
| 2208 | |
| 2209 | if (tty->ops->resize) |
| 2210 | return tty->ops->resize(tty, real_tty, &tmp_ws); |
| 2211 | else |
| 2212 | return tty_do_resize(tty, real_tty, &tmp_ws); |
| 2213 | } |
| 2214 | |
| 2215 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2216 | * tioccons - allow admin to move logical console |
| 2217 | * @file: the file to become console |
| 2218 | * |
| 2219 | * Allow the adminstrator to move the redirected console device |
| 2220 | * |
| 2221 | * Locking: uses redirect_lock to guard the redirect information |
| 2222 | */ |
| 2223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | static int tioccons(struct file *file) |
| 2225 | { |
| 2226 | if (!capable(CAP_SYS_ADMIN)) |
| 2227 | return -EPERM; |
| 2228 | if (file->f_op->write == redirected_tty_write) { |
| 2229 | struct file *f; |
| 2230 | spin_lock(&redirect_lock); |
| 2231 | f = redirect; |
| 2232 | redirect = NULL; |
| 2233 | spin_unlock(&redirect_lock); |
| 2234 | if (f) |
| 2235 | fput(f); |
| 2236 | return 0; |
| 2237 | } |
| 2238 | spin_lock(&redirect_lock); |
| 2239 | if (redirect) { |
| 2240 | spin_unlock(&redirect_lock); |
| 2241 | return -EBUSY; |
| 2242 | } |
| 2243 | get_file(file); |
| 2244 | redirect = file; |
| 2245 | spin_unlock(&redirect_lock); |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2249 | /** |
| 2250 | * fionbio - non blocking ioctl |
| 2251 | * @file: file to set blocking value |
| 2252 | * @p: user parameter |
| 2253 | * |
| 2254 | * Historical tty interfaces had a blocking control ioctl before |
| 2255 | * the generic functionality existed. This piece of history is preserved |
| 2256 | * in the expected tty API of posix OS's. |
| 2257 | * |
| 2258 | * Locking: none, the open fle handle ensures it won't go away. |
| 2259 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | |
| 2261 | static int fionbio(struct file *file, int __user *p) |
| 2262 | { |
| 2263 | int nonblock; |
| 2264 | |
| 2265 | if (get_user(nonblock, p)) |
| 2266 | return -EFAULT; |
| 2267 | |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2268 | /* file->f_flags is still BKL protected in the fs layer - vomit */ |
| 2269 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | if (nonblock) |
| 2271 | file->f_flags |= O_NONBLOCK; |
| 2272 | else |
| 2273 | file->f_flags &= ~O_NONBLOCK; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2274 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | return 0; |
| 2276 | } |
| 2277 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2278 | /** |
| 2279 | * tiocsctty - set controlling tty |
| 2280 | * @tty: tty structure |
| 2281 | * @arg: user argument |
| 2282 | * |
| 2283 | * This ioctl is used to manage job control. It permits a session |
| 2284 | * leader to set this tty as the controlling tty for the session. |
| 2285 | * |
| 2286 | * Locking: |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 2287 | * Takes tty_mutex() to protect tty instance |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2288 | * Takes tasklist_lock internally to walk sessions |
| 2289 | * Takes ->siglock() when updating signal->tty |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2290 | */ |
| 2291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | static int tiocsctty(struct tty_struct *tty, int arg) |
| 2293 | { |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2294 | int ret = 0; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2295 | if (current->signal->leader && (task_session(current) == tty->session)) |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2296 | return ret; |
| 2297 | |
| 2298 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | /* |
| 2300 | * The process must be a session leader and |
| 2301 | * not have a controlling tty already. |
| 2302 | */ |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2303 | if (!current->signal->leader || current->signal->tty) { |
| 2304 | ret = -EPERM; |
| 2305 | goto unlock; |
| 2306 | } |
| 2307 | |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2308 | if (tty->session) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | /* |
| 2310 | * This tty is already the controlling |
| 2311 | * tty for another session group! |
| 2312 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2313 | if (arg == 1 && capable(CAP_SYS_ADMIN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | /* |
| 2315 | * Steal it away |
| 2316 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | read_lock(&tasklist_lock); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2318 | session_clear_tty(tty->session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | read_unlock(&tasklist_lock); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2320 | } else { |
| 2321 | ret = -EPERM; |
| 2322 | goto unlock; |
| 2323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2325 | proc_set_tty(current, tty); |
| 2326 | unlock: |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 2327 | mutex_unlock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2328 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2331 | /** |
Alan Cox | 5d0fdf1 | 2008-04-30 00:53:31 -0700 | [diff] [blame] | 2332 | * tty_get_pgrp - return a ref counted pgrp pid |
| 2333 | * @tty: tty to read |
| 2334 | * |
| 2335 | * Returns a refcounted instance of the pid struct for the process |
| 2336 | * group controlling the tty. |
| 2337 | */ |
| 2338 | |
| 2339 | struct pid *tty_get_pgrp(struct tty_struct *tty) |
| 2340 | { |
| 2341 | unsigned long flags; |
| 2342 | struct pid *pgrp; |
| 2343 | |
| 2344 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 2345 | pgrp = get_pid(tty->pgrp); |
| 2346 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 2347 | |
| 2348 | return pgrp; |
| 2349 | } |
| 2350 | EXPORT_SYMBOL_GPL(tty_get_pgrp); |
| 2351 | |
| 2352 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2353 | * tiocgpgrp - get process group |
| 2354 | * @tty: tty passed by user |
| 2355 | * @real_tty: tty side of the tty pased by the user if a pty else the tty |
| 2356 | * @p: returned pid |
| 2357 | * |
| 2358 | * Obtain the process group of the tty. If there is no process group |
| 2359 | * return an error. |
| 2360 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2361 | * Locking: none. Reference to current->signal->tty is safe. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2362 | */ |
| 2363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
| 2365 | { |
Alan Cox | 5d0fdf1 | 2008-04-30 00:53:31 -0700 | [diff] [blame] | 2366 | struct pid *pid; |
| 2367 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | /* |
| 2369 | * (tty == real_tty) is a cheap way of |
| 2370 | * testing if the tty is NOT a master pty. |
| 2371 | */ |
| 2372 | if (tty == real_tty && current->signal->tty != real_tty) |
| 2373 | return -ENOTTY; |
Alan Cox | 5d0fdf1 | 2008-04-30 00:53:31 -0700 | [diff] [blame] | 2374 | pid = tty_get_pgrp(real_tty); |
| 2375 | ret = put_user(pid_vnr(pid), p); |
| 2376 | put_pid(pid); |
| 2377 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2380 | /** |
| 2381 | * tiocspgrp - attempt to set process group |
| 2382 | * @tty: tty passed by user |
| 2383 | * @real_tty: tty side device matching tty passed by user |
| 2384 | * @p: pid pointer |
| 2385 | * |
| 2386 | * Set the process group of the tty to the session passed. Only |
| 2387 | * permitted where the tty session is our session. |
| 2388 | * |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2389 | * Locking: RCU, ctrl lock |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2390 | */ |
| 2391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
| 2393 | { |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 2394 | struct pid *pgrp; |
| 2395 | pid_t pgrp_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | int retval = tty_check_change(real_tty); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2397 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | |
| 2399 | if (retval == -EIO) |
| 2400 | return -ENOTTY; |
| 2401 | if (retval) |
| 2402 | return retval; |
| 2403 | if (!current->signal->tty || |
| 2404 | (current->signal->tty != real_tty) || |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2405 | (real_tty->session != task_session(current))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | return -ENOTTY; |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 2407 | if (get_user(pgrp_nr, p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | return -EFAULT; |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 2409 | if (pgrp_nr < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | return -EINVAL; |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 2411 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2412 | pgrp = find_vpid(pgrp_nr); |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 2413 | retval = -ESRCH; |
| 2414 | if (!pgrp) |
| 2415 | goto out_unlock; |
| 2416 | retval = -EPERM; |
| 2417 | if (session_of_pgrp(pgrp) != task_session(current)) |
| 2418 | goto out_unlock; |
| 2419 | retval = 0; |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2420 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2421 | put_pid(real_tty->pgrp); |
| 2422 | real_tty->pgrp = get_pid(pgrp); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 2423 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Eric W. Biederman | 04a2e6a | 2007-02-12 00:52:56 -0800 | [diff] [blame] | 2424 | out_unlock: |
| 2425 | rcu_read_unlock(); |
| 2426 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2429 | /** |
| 2430 | * tiocgsid - get session id |
| 2431 | * @tty: tty passed by user |
| 2432 | * @real_tty: tty side of the tty pased by the user if a pty else the tty |
| 2433 | * @p: pointer to returned session id |
| 2434 | * |
| 2435 | * Obtain the session id of the tty. If there is no session |
| 2436 | * return an error. |
| 2437 | * |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2438 | * Locking: none. Reference to current->signal->tty is safe. |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2439 | */ |
| 2440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
| 2442 | { |
| 2443 | /* |
| 2444 | * (tty == real_tty) is a cheap way of |
| 2445 | * testing if the tty is NOT a master pty. |
| 2446 | */ |
| 2447 | if (tty == real_tty && current->signal->tty != real_tty) |
| 2448 | return -ENOTTY; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2449 | if (!real_tty->session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | return -ENOTTY; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2451 | return put_user(pid_vnr(real_tty->session), p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | } |
| 2453 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2454 | /** |
| 2455 | * tiocsetd - set line discipline |
| 2456 | * @tty: tty device |
| 2457 | * @p: pointer to user data |
| 2458 | * |
| 2459 | * Set the line discipline according to user request. |
| 2460 | * |
| 2461 | * Locking: see tty_set_ldisc, this function is just a helper |
| 2462 | */ |
| 2463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | static int tiocsetd(struct tty_struct *tty, int __user *p) |
| 2465 | { |
| 2466 | int ldisc; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2467 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | |
| 2469 | if (get_user(ldisc, p)) |
| 2470 | return -EFAULT; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2471 | |
| 2472 | lock_kernel(); |
| 2473 | ret = tty_set_ldisc(tty, ldisc); |
| 2474 | unlock_kernel(); |
| 2475 | |
| 2476 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | } |
| 2478 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2479 | /** |
| 2480 | * send_break - performed time break |
| 2481 | * @tty: device to break on |
| 2482 | * @duration: timeout in mS |
| 2483 | * |
| 2484 | * Perform a timed break on hardware that lacks its own driver level |
| 2485 | * timed break functionality. |
| 2486 | * |
| 2487 | * Locking: |
Alan Cox | 2829823 | 2006-09-29 02:00:58 -0700 | [diff] [blame] | 2488 | * atomic_write_lock serializes |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2489 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2490 | */ |
| 2491 | |
Domen Puncer | b20f3ae | 2005-06-25 14:58:42 -0700 | [diff] [blame] | 2492 | static int send_break(struct tty_struct *tty, unsigned int duration) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | { |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 2494 | int retval; |
| 2495 | |
| 2496 | if (tty->ops->break_ctl == NULL) |
| 2497 | return 0; |
| 2498 | |
| 2499 | if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) |
| 2500 | retval = tty->ops->break_ctl(tty, duration); |
| 2501 | else { |
| 2502 | /* Do the work ourselves */ |
| 2503 | if (tty_write_lock(tty, 0) < 0) |
| 2504 | return -EINTR; |
| 2505 | retval = tty->ops->break_ctl(tty, -1); |
| 2506 | if (retval) |
| 2507 | goto out; |
| 2508 | if (!signal_pending(current)) |
| 2509 | msleep_interruptible(duration); |
| 2510 | retval = tty->ops->break_ctl(tty, 0); |
| 2511 | out: |
| 2512 | tty_write_unlock(tty); |
| 2513 | if (signal_pending(current)) |
| 2514 | retval = -EINTR; |
| 2515 | } |
| 2516 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2519 | /** |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2520 | * tty_tiocmget - get modem status |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2521 | * @tty: tty device |
| 2522 | * @file: user file pointer |
| 2523 | * @p: pointer to result |
| 2524 | * |
| 2525 | * Obtain the modem status bits from the tty driver if the feature |
| 2526 | * is supported. Return -EINVAL if it is not available. |
| 2527 | * |
| 2528 | * Locking: none (up to the driver) |
| 2529 | */ |
| 2530 | |
| 2531 | 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] | 2532 | { |
| 2533 | int retval = -EINVAL; |
| 2534 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2535 | if (tty->ops->tiocmget) { |
| 2536 | retval = tty->ops->tiocmget(tty, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
| 2538 | if (retval >= 0) |
| 2539 | retval = put_user(retval, p); |
| 2540 | } |
| 2541 | return retval; |
| 2542 | } |
| 2543 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2544 | /** |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2545 | * tty_tiocmset - set modem status |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2546 | * @tty: tty device |
| 2547 | * @file: user file pointer |
| 2548 | * @cmd: command - clear bits, set bits or set all |
| 2549 | * @p: pointer to desired bits |
| 2550 | * |
| 2551 | * Set the modem status bits from the tty driver if the feature |
| 2552 | * is supported. Return -EINVAL if it is not available. |
| 2553 | * |
| 2554 | * Locking: none (up to the driver) |
| 2555 | */ |
| 2556 | |
| 2557 | 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] | 2558 | unsigned __user *p) |
| 2559 | { |
Alan Cox | ae67751 | 2008-07-16 21:56:54 +0100 | [diff] [blame] | 2560 | int retval; |
| 2561 | unsigned int set, clear, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | |
Alan Cox | ae67751 | 2008-07-16 21:56:54 +0100 | [diff] [blame] | 2563 | if (tty->ops->tiocmset == NULL) |
| 2564 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | |
Alan Cox | ae67751 | 2008-07-16 21:56:54 +0100 | [diff] [blame] | 2566 | retval = get_user(val, p); |
| 2567 | if (retval) |
| 2568 | return retval; |
| 2569 | set = clear = 0; |
| 2570 | switch (cmd) { |
| 2571 | case TIOCMBIS: |
| 2572 | set = val; |
| 2573 | break; |
| 2574 | case TIOCMBIC: |
| 2575 | clear = val; |
| 2576 | break; |
| 2577 | case TIOCMSET: |
| 2578 | set = val; |
| 2579 | clear = ~val; |
| 2580 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | } |
Alan Cox | ae67751 | 2008-07-16 21:56:54 +0100 | [diff] [blame] | 2582 | set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; |
| 2583 | clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; |
| 2584 | return tty->ops->tiocmset(tty, file, set, clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | /* |
| 2588 | * Split this up, as gcc can choke on it otherwise.. |
| 2589 | */ |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2590 | long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | { |
| 2592 | struct tty_struct *tty, *real_tty; |
| 2593 | void __user *p = (void __user *)arg; |
| 2594 | int retval; |
| 2595 | struct tty_ldisc *ld; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2596 | struct inode *inode = file->f_dentry->d_inode; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | tty = (struct tty_struct *)file->private_data; |
| 2599 | if (tty_paranoia_check(tty, inode, "tty_ioctl")) |
| 2600 | return -EINVAL; |
| 2601 | |
| 2602 | real_tty = tty; |
| 2603 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
| 2604 | tty->driver->subtype == PTY_TYPE_MASTER) |
| 2605 | real_tty = tty->link; |
| 2606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | |
| 2608 | /* |
| 2609 | * Factor out some common prep work |
| 2610 | */ |
| 2611 | switch (cmd) { |
| 2612 | case TIOCSETD: |
| 2613 | case TIOCSBRK: |
| 2614 | case TIOCCBRK: |
| 2615 | case TCSBRK: |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2616 | case TCSBRKP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | retval = tty_check_change(tty); |
| 2618 | if (retval) |
| 2619 | return retval; |
| 2620 | if (cmd != TIOCCBRK) { |
| 2621 | tty_wait_until_sent(tty, 0); |
| 2622 | if (signal_pending(current)) |
| 2623 | return -EINTR; |
| 2624 | } |
| 2625 | break; |
| 2626 | } |
| 2627 | |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 2628 | /* |
| 2629 | * Now do the stuff. |
| 2630 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | switch (cmd) { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2632 | case TIOCSTI: |
| 2633 | return tiocsti(tty, p); |
| 2634 | case TIOCGWINSZ: |
Alan Cox | 8f52002 | 2008-10-13 10:38:46 +0100 | [diff] [blame] | 2635 | return tiocgwinsz(real_tty, p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2636 | case TIOCSWINSZ: |
| 2637 | return tiocswinsz(tty, real_tty, p); |
| 2638 | case TIOCCONS: |
| 2639 | return real_tty != tty ? -EINVAL : tioccons(file); |
| 2640 | case FIONBIO: |
| 2641 | return fionbio(file, p); |
| 2642 | case TIOCEXCL: |
| 2643 | set_bit(TTY_EXCLUSIVE, &tty->flags); |
| 2644 | return 0; |
| 2645 | case TIOCNXCL: |
| 2646 | clear_bit(TTY_EXCLUSIVE, &tty->flags); |
| 2647 | return 0; |
| 2648 | case TIOCNOTTY: |
| 2649 | if (current->signal->tty != tty) |
| 2650 | return -ENOTTY; |
| 2651 | no_tty(); |
| 2652 | return 0; |
| 2653 | case TIOCSCTTY: |
| 2654 | return tiocsctty(tty, arg); |
| 2655 | case TIOCGPGRP: |
| 2656 | return tiocgpgrp(tty, real_tty, p); |
| 2657 | case TIOCSPGRP: |
| 2658 | return tiocspgrp(tty, real_tty, p); |
| 2659 | case TIOCGSID: |
| 2660 | return tiocgsid(tty, real_tty, p); |
| 2661 | case TIOCGETD: |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2662 | return put_user(tty->ldisc.ops->num, (int __user *)p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2663 | case TIOCSETD: |
| 2664 | return tiocsetd(tty, p); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2665 | /* |
| 2666 | * Break handling |
| 2667 | */ |
| 2668 | case TIOCSBRK: /* Turn break on, unconditionally */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2669 | if (tty->ops->break_ctl) |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 2670 | return tty->ops->break_ctl(tty, -1); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2671 | return 0; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2672 | case TIOCCBRK: /* Turn break off, unconditionally */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2673 | if (tty->ops->break_ctl) |
Alan Cox | 9e98966 | 2008-07-22 11:18:03 +0100 | [diff] [blame] | 2674 | return tty->ops->break_ctl(tty, 0); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2675 | return 0; |
| 2676 | case TCSBRK: /* SVID version: non-zero arg --> no break */ |
| 2677 | /* non-zero arg means wait for all output data |
| 2678 | * to be sent (performed above) but don't send break. |
| 2679 | * This is used by the tcdrain() termios function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | */ |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2681 | if (!arg) |
| 2682 | return send_break(tty, 250); |
| 2683 | return 0; |
| 2684 | case TCSBRKP: /* support for POSIX tcsendbreak() */ |
| 2685 | return send_break(tty, arg ? arg*100 : 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2687 | case TIOCMGET: |
| 2688 | return tty_tiocmget(tty, file, p); |
| 2689 | case TIOCMSET: |
| 2690 | case TIOCMBIC: |
| 2691 | case TIOCMBIS: |
| 2692 | return tty_tiocmset(tty, file, cmd, p); |
| 2693 | case TCFLSH: |
| 2694 | switch (arg) { |
| 2695 | case TCIFLUSH: |
| 2696 | case TCIOFLUSH: |
| 2697 | /* flush tty buffer and allow ldisc to process ioctl */ |
| 2698 | tty_buffer_flush(tty); |
Paul Fulghum | c5c34d4 | 2007-05-12 10:36:55 -0700 | [diff] [blame] | 2699 | break; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2700 | } |
| 2701 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2703 | if (tty->ops->ioctl) { |
| 2704 | retval = (tty->ops->ioctl)(tty, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | if (retval != -ENOIOCTLCMD) |
| 2706 | return retval; |
| 2707 | } |
| 2708 | ld = tty_ldisc_ref_wait(tty); |
| 2709 | retval = -EINVAL; |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2710 | if (ld->ops->ioctl) { |
| 2711 | retval = ld->ops->ioctl(tty, file, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | if (retval == -ENOIOCTLCMD) |
| 2713 | retval = -EINVAL; |
| 2714 | } |
| 2715 | tty_ldisc_deref(ld); |
| 2716 | return retval; |
| 2717 | } |
| 2718 | |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2719 | #ifdef CONFIG_COMPAT |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2720 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2721 | unsigned long arg) |
| 2722 | { |
| 2723 | struct inode *inode = file->f_dentry->d_inode; |
| 2724 | struct tty_struct *tty = file->private_data; |
| 2725 | struct tty_ldisc *ld; |
| 2726 | int retval = -ENOIOCTLCMD; |
| 2727 | |
| 2728 | if (tty_paranoia_check(tty, inode, "tty_ioctl")) |
| 2729 | return -EINVAL; |
| 2730 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2731 | if (tty->ops->compat_ioctl) { |
| 2732 | retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2733 | if (retval != -ENOIOCTLCMD) |
| 2734 | return retval; |
| 2735 | } |
| 2736 | |
| 2737 | ld = tty_ldisc_ref_wait(tty); |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 2738 | if (ld->ops->compat_ioctl) |
| 2739 | retval = ld->ops->compat_ioctl(tty, file, cmd, arg); |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 2740 | tty_ldisc_deref(ld); |
| 2741 | |
| 2742 | return retval; |
| 2743 | } |
| 2744 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | |
| 2746 | /* |
| 2747 | * This implements the "Secure Attention Key" --- the idea is to |
| 2748 | * prevent trojan horses by killing all processes associated with this |
| 2749 | * tty when the user hits the "Secure Attention Key". Required for |
| 2750 | * super-paranoid applications --- see the Orange Book for more details. |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2751 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | * This code could be nicer; ideally it should send a HUP, wait a few |
| 2753 | * seconds, then send a INT, and then a KILL signal. But you then |
| 2754 | * have to coordinate with the init process, since all processes associated |
| 2755 | * with the current tty must be dead before the new getty is allowed |
| 2756 | * to spawn. |
| 2757 | * |
| 2758 | * Now, if it would be correct ;-/ The current code has a nasty hole - |
| 2759 | * it doesn't catch files in flight. We may send the descriptor to ourselves |
| 2760 | * via AF_UNIX socket, close it and later fetch from socket. FIXME. |
| 2761 | * |
| 2762 | * Nasty bug: do_SAK is being called in interrupt context. This can |
| 2763 | * deadlock. We punt it up to process context. AKPM - 16Mar2001 |
| 2764 | */ |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 2765 | void __do_SAK(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | { |
| 2767 | #ifdef TTY_SOFT_SAK |
| 2768 | tty_hangup(tty); |
| 2769 | #else |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 2770 | struct task_struct *g, *p; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2771 | struct pid *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | int i; |
| 2773 | struct file *filp; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 2774 | struct fdtable *fdt; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | if (!tty) |
| 2777 | return; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 2778 | session = tty->session; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2779 | |
Dan Carpenter | b3f13de | 2006-12-13 00:35:09 -0800 | [diff] [blame] | 2780 | tty_ldisc_flush(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2782 | tty_driver_flush_buffer(tty); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | read_lock(&tasklist_lock); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 2785 | /* Kill the entire session */ |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2786 | do_each_pid_task(session, PIDTYPE_SID, p) { |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 2787 | printk(KERN_NOTICE "SAK: killed process %d" |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 2788 | " (%s): task_session_nr(p)==tty->session\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 2789 | task_pid_nr(p), p->comm); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 2790 | send_sig(SIGKILL, p, 1); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2791 | } while_each_pid_task(session, PIDTYPE_SID, p); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 2792 | /* Now kill any processes that happen to have the |
| 2793 | * tty open. |
| 2794 | */ |
| 2795 | do_each_thread(g, p) { |
| 2796 | if (p->signal->tty == tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | printk(KERN_NOTICE "SAK: killed process %d" |
Pavel Emelianov | a47afb0 | 2007-10-18 23:39:46 -0700 | [diff] [blame] | 2798 | " (%s): task_session_nr(p)==tty->session\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 2799 | task_pid_nr(p), p->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | send_sig(SIGKILL, p, 1); |
| 2801 | continue; |
| 2802 | } |
| 2803 | task_lock(p); |
| 2804 | if (p->files) { |
Dipankar Sarma | ca99c1d | 2006-04-18 22:21:46 -0700 | [diff] [blame] | 2805 | /* |
| 2806 | * We don't take a ref to the file, so we must |
| 2807 | * hold ->file_lock instead. |
| 2808 | */ |
| 2809 | spin_lock(&p->files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 2810 | fdt = files_fdtable(p->files); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2811 | for (i = 0; i < fdt->max_fds; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | filp = fcheck_files(p->files, i); |
| 2813 | if (!filp) |
| 2814 | continue; |
| 2815 | if (filp->f_op->read == tty_read && |
| 2816 | filp->private_data == tty) { |
| 2817 | printk(KERN_NOTICE "SAK: killed process %d" |
| 2818 | " (%s): fd#%d opened to the tty\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 2819 | task_pid_nr(p), p->comm, i); |
Eric W. Biederman | 20ac943 | 2006-04-13 04:49:07 -0600 | [diff] [blame] | 2820 | force_sig(SIGKILL, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | break; |
| 2822 | } |
| 2823 | } |
Dipankar Sarma | ca99c1d | 2006-04-18 22:21:46 -0700 | [diff] [blame] | 2824 | spin_unlock(&p->files->file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | } |
| 2826 | task_unlock(p); |
Eric W. Biederman | 652486f | 2006-03-28 16:11:02 -0800 | [diff] [blame] | 2827 | } while_each_thread(g, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | read_unlock(&tasklist_lock); |
| 2829 | #endif |
| 2830 | } |
| 2831 | |
Eric W. Biederman | 8b6312f | 2007-02-10 01:44:34 -0800 | [diff] [blame] | 2832 | static void do_SAK_work(struct work_struct *work) |
| 2833 | { |
| 2834 | struct tty_struct *tty = |
| 2835 | container_of(work, struct tty_struct, SAK_work); |
| 2836 | __do_SAK(tty); |
| 2837 | } |
| 2838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | /* |
| 2840 | * The tq handling here is a little racy - tty->SAK_work may already be queued. |
| 2841 | * Fortunately we don't need to worry, because if ->SAK_work is already queued, |
| 2842 | * the values which we write to it will be identical to the values which it |
| 2843 | * already has. --akpm |
| 2844 | */ |
| 2845 | void do_SAK(struct tty_struct *tty) |
| 2846 | { |
| 2847 | if (!tty) |
| 2848 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | schedule_work(&tty->SAK_work); |
| 2850 | } |
| 2851 | |
| 2852 | EXPORT_SYMBOL(do_SAK); |
| 2853 | |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2854 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2855 | * initialize_tty_struct |
| 2856 | * @tty: tty to initialize |
| 2857 | * |
| 2858 | * This subroutine initializes a tty structure that has been newly |
| 2859 | * allocated. |
| 2860 | * |
| 2861 | * Locking: none - tty in question must not be exposed at this point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | static void initialize_tty_struct(struct tty_struct *tty) |
| 2865 | { |
| 2866 | memset(tty, 0, sizeof(struct tty_struct)); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 2867 | kref_init(&tty->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | tty->magic = TTY_MAGIC; |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 2869 | tty_ldisc_init(tty); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 2870 | tty->session = NULL; |
| 2871 | tty->pgrp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | tty->overrun_time = jiffies; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 2873 | tty->buf.head = tty->buf.tail = NULL; |
| 2874 | tty_buffer_init(tty); |
Arjan van de Ven | 5785c95 | 2006-09-29 02:00:43 -0700 | [diff] [blame] | 2875 | mutex_init(&tty->termios_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | init_waitqueue_head(&tty->write_wait); |
| 2877 | init_waitqueue_head(&tty->read_wait); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 2878 | INIT_WORK(&tty->hangup_work, do_tty_hangup); |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 2879 | mutex_init(&tty->atomic_read_lock); |
| 2880 | mutex_init(&tty->atomic_write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | spin_lock_init(&tty->read_lock); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 2882 | spin_lock_init(&tty->ctrl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | INIT_LIST_HEAD(&tty->tty_files); |
Eric W. Biederman | 7f1f86a | 2007-02-13 14:38:58 -0700 | [diff] [blame] | 2884 | INIT_WORK(&tty->SAK_work, do_SAK_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2887 | /** |
| 2888 | * tty_put_char - write one character to a tty |
| 2889 | * @tty: tty |
| 2890 | * @ch: character |
| 2891 | * |
| 2892 | * Write one byte to the tty using the provided put_char method |
| 2893 | * if present. Returns the number of characters successfully output. |
| 2894 | * |
| 2895 | * Note: the specific put_char operation in the driver layer may go |
| 2896 | * away soon. Don't call it directly, use this method |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2898 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2899 | int tty_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2901 | if (tty->ops->put_char) |
| 2902 | return tty->ops->put_char(tty, ch); |
| 2903 | return tty->ops->write(tty, &ch, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | } |
| 2905 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2906 | EXPORT_SYMBOL_GPL(tty_put_char); |
| 2907 | |
gregkh@suse.de | 7fe845d | 2005-03-15 14:23:15 -0800 | [diff] [blame] | 2908 | static struct class *tty_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | |
| 2910 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2911 | * tty_register_device - register a tty device |
| 2912 | * @driver: the tty driver that describes the tty device |
| 2913 | * @index: the index in the tty driver for this tty device |
| 2914 | * @device: a struct device that is associated with this tty device. |
| 2915 | * This field is optional, if there is no known struct device |
| 2916 | * for this tty device it can be set to NULL safely. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | * |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 2918 | * Returns a pointer to the struct device for this tty device |
| 2919 | * (or ERR_PTR(-EFOO) on error). |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 2920 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2921 | * This call is required to be made to register an individual tty device |
| 2922 | * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If |
| 2923 | * that bit is not set, this function should not be called by a tty |
| 2924 | * driver. |
| 2925 | * |
| 2926 | * Locking: ?? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2928 | |
Greg Kroah-Hartman | 01107d3 | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 2929 | struct device *tty_register_device(struct tty_driver *driver, unsigned index, |
| 2930 | struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | { |
| 2932 | char name[64]; |
| 2933 | dev_t dev = MKDEV(driver->major, driver->minor_start) + index; |
| 2934 | |
| 2935 | if (index >= driver->num) { |
| 2936 | printk(KERN_ERR "Attempt to register invalid tty line number " |
| 2937 | " (%d).\n", index); |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 2938 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | if (driver->type == TTY_DRIVER_TYPE_PTY) |
| 2942 | pty_line_name(driver, index, name); |
| 2943 | else |
| 2944 | tty_line_name(driver, index, name); |
Hansjoerg Lipp | 1cdcb6b | 2006-04-22 18:36:53 +0200 | [diff] [blame] | 2945 | |
Greg Kroah-Hartman | 47aa579 | 2008-05-21 12:52:33 -0700 | [diff] [blame] | 2946 | return device_create_drvdata(tty_class, device, dev, NULL, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | /** |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2950 | * tty_unregister_device - unregister a tty device |
| 2951 | * @driver: the tty driver that describes the tty device |
| 2952 | * @index: the index in the tty driver for this tty device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | * |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2954 | * If a tty device is registered with a call to tty_register_device() then |
| 2955 | * this function must be called when the tty device is gone. |
| 2956 | * |
| 2957 | * Locking: ?? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | */ |
Alan Cox | af9b897 | 2006-08-27 01:24:01 -0700 | [diff] [blame] | 2959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | void tty_unregister_device(struct tty_driver *driver, unsigned index) |
| 2961 | { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 2962 | device_destroy(tty_class, |
| 2963 | MKDEV(driver->major, driver->minor_start) + index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | EXPORT_SYMBOL(tty_register_device); |
| 2967 | EXPORT_SYMBOL(tty_unregister_device); |
| 2968 | |
| 2969 | struct tty_driver *alloc_tty_driver(int lines) |
| 2970 | { |
| 2971 | struct tty_driver *driver; |
| 2972 | |
Jean Delvare | 506eb99 | 2007-07-15 23:40:14 -0700 | [diff] [blame] | 2973 | driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | if (driver) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | driver->magic = TTY_DRIVER_MAGIC; |
| 2976 | driver->num = lines; |
| 2977 | /* later we'll move allocation of tables here */ |
| 2978 | } |
| 2979 | return driver; |
| 2980 | } |
| 2981 | |
| 2982 | void put_tty_driver(struct tty_driver *driver) |
| 2983 | { |
| 2984 | kfree(driver); |
| 2985 | } |
| 2986 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 2987 | void tty_set_operations(struct tty_driver *driver, |
| 2988 | const struct tty_operations *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 2990 | driver->ops = op; |
| 2991 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | |
| 2993 | EXPORT_SYMBOL(alloc_tty_driver); |
| 2994 | EXPORT_SYMBOL(put_tty_driver); |
| 2995 | EXPORT_SYMBOL(tty_set_operations); |
| 2996 | |
| 2997 | /* |
| 2998 | * Called by a tty driver to register itself. |
| 2999 | */ |
| 3000 | int tty_register_driver(struct tty_driver *driver) |
| 3001 | { |
| 3002 | int error; |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3003 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3004 | dev_t dev; |
| 3005 | void **p = NULL; |
| 3006 | |
| 3007 | if (driver->flags & TTY_DRIVER_INSTALLED) |
| 3008 | return 0; |
| 3009 | |
Andy Whitcroft | 543691a | 2007-05-06 14:49:33 -0700 | [diff] [blame] | 3010 | if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) { |
| 3011 | p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | if (!p) |
| 3013 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | if (!driver->major) { |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3017 | error = alloc_chrdev_region(&dev, driver->minor_start, |
| 3018 | driver->num, driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | if (!error) { |
| 3020 | driver->major = MAJOR(dev); |
| 3021 | driver->minor_start = MINOR(dev); |
| 3022 | } |
| 3023 | } else { |
| 3024 | dev = MKDEV(driver->major, driver->minor_start); |
Geert Uytterhoeven | e5717c4 | 2007-02-20 15:45:21 +0100 | [diff] [blame] | 3025 | error = register_chrdev_region(dev, driver->num, driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | } |
| 3027 | if (error < 0) { |
| 3028 | kfree(p); |
| 3029 | return error; |
| 3030 | } |
| 3031 | |
| 3032 | if (p) { |
| 3033 | driver->ttys = (struct tty_struct **)p; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 3034 | driver->termios = (struct ktermios **)(p + driver->num); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3035 | driver->termios_locked = (struct ktermios **) |
| 3036 | (p + driver->num * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | } else { |
| 3038 | driver->ttys = NULL; |
| 3039 | driver->termios = NULL; |
| 3040 | driver->termios_locked = NULL; |
| 3041 | } |
| 3042 | |
| 3043 | cdev_init(&driver->cdev, &tty_fops); |
| 3044 | driver->cdev.owner = driver->owner; |
| 3045 | error = cdev_add(&driver->cdev, dev, driver->num); |
| 3046 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | unregister_chrdev_region(dev, driver->num); |
| 3048 | driver->ttys = NULL; |
| 3049 | driver->termios = driver->termios_locked = NULL; |
| 3050 | kfree(p); |
| 3051 | return error; |
| 3052 | } |
| 3053 | |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 3054 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | list_add(&driver->tty_drivers, &tty_drivers); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 3056 | mutex_unlock(&tty_mutex); |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3057 | |
| 3058 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { |
| 3059 | for (i = 0; i < driver->num; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | tty_register_device(driver, i, NULL); |
| 3061 | } |
| 3062 | proc_tty_register_driver(driver); |
| 3063 | return 0; |
| 3064 | } |
| 3065 | |
| 3066 | EXPORT_SYMBOL(tty_register_driver); |
| 3067 | |
| 3068 | /* |
| 3069 | * Called by a tty driver to unregister itself. |
| 3070 | */ |
| 3071 | int tty_unregister_driver(struct tty_driver *driver) |
| 3072 | { |
| 3073 | int i; |
Alan Cox | edc6afc | 2006-12-08 02:38:44 -0800 | [diff] [blame] | 3074 | struct ktermios *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | void *p; |
| 3076 | |
| 3077 | if (driver->refcount) |
| 3078 | return -EBUSY; |
| 3079 | |
| 3080 | unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), |
| 3081 | driver->num); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 3082 | mutex_lock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | list_del(&driver->tty_drivers); |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 3084 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | |
| 3086 | /* |
| 3087 | * Free the termios and termios_locked structures because |
| 3088 | * we don't want to get memory leaks when modular tty |
| 3089 | * drivers are removed from the kernel. |
| 3090 | */ |
| 3091 | for (i = 0; i < driver->num; i++) { |
| 3092 | tp = driver->termios[i]; |
| 3093 | if (tp) { |
| 3094 | driver->termios[i] = NULL; |
| 3095 | kfree(tp); |
| 3096 | } |
| 3097 | tp = driver->termios_locked[i]; |
| 3098 | if (tp) { |
| 3099 | driver->termios_locked[i] = NULL; |
| 3100 | kfree(tp); |
| 3101 | } |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3102 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | tty_unregister_device(driver, i); |
| 3104 | } |
| 3105 | p = driver->ttys; |
| 3106 | proc_tty_unregister_driver(driver); |
| 3107 | driver->ttys = NULL; |
| 3108 | driver->termios = driver->termios_locked = NULL; |
| 3109 | kfree(p); |
| 3110 | cdev_del(&driver->cdev); |
| 3111 | return 0; |
| 3112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | EXPORT_SYMBOL(tty_unregister_driver); |
| 3114 | |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3115 | dev_t tty_devnum(struct tty_struct *tty) |
| 3116 | { |
| 3117 | return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; |
| 3118 | } |
| 3119 | EXPORT_SYMBOL(tty_devnum); |
| 3120 | |
| 3121 | void proc_clear_tty(struct task_struct *p) |
| 3122 | { |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 3123 | struct tty_struct *tty; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3124 | spin_lock_irq(&p->sighand->siglock); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 3125 | tty = p->signal->tty; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3126 | p->signal->tty = NULL; |
| 3127 | spin_unlock_irq(&p->sighand->siglock); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 3128 | tty_kref_put(tty); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3129 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3130 | |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3131 | /* Called under the sighand lock */ |
| 3132 | |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 3133 | 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] | 3134 | { |
| 3135 | if (tty) { |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3136 | unsigned long flags; |
| 3137 | /* We should not have a session or pgrp to put here but.... */ |
| 3138 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Eric W. Biederman | d9c1e9a | 2007-03-18 12:45:44 -0600 | [diff] [blame] | 3139 | put_pid(tty->session); |
| 3140 | put_pid(tty->pgrp); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3141 | tty->pgrp = get_pid(task_pgrp(tsk)); |
Alan Cox | 47f8683 | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 3142 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 3143 | tty->session = get_pid(task_session(tsk)); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 3144 | if (tsk->signal->tty) { |
| 3145 | printk(KERN_DEBUG "tty not NULL!!\n"); |
| 3146 | tty_kref_put(tsk->signal->tty); |
| 3147 | } |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3148 | } |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 3149 | put_pid(tsk->signal->tty_old_pgrp); |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 3150 | tsk->signal->tty = tty_kref_get(tty); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 3151 | tsk->signal->tty_old_pgrp = NULL; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3152 | } |
| 3153 | |
Eric W. Biederman | 98a27ba | 2007-05-08 00:26:56 -0700 | [diff] [blame] | 3154 | 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] | 3155 | { |
| 3156 | spin_lock_irq(&tsk->sighand->siglock); |
Eric W. Biederman | 2a65f1d | 2007-05-08 00:26:53 -0700 | [diff] [blame] | 3157 | __proc_set_tty(tsk, tty); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3158 | spin_unlock_irq(&tsk->sighand->siglock); |
| 3159 | } |
| 3160 | |
| 3161 | struct tty_struct *get_current_tty(void) |
| 3162 | { |
| 3163 | struct tty_struct *tty; |
| 3164 | WARN_ON_ONCE(!mutex_is_locked(&tty_mutex)); |
Alan Cox | 452a00d | 2008-10-13 10:39:13 +0100 | [diff] [blame] | 3165 | tty = tty_kref_get(current->signal->tty); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 3166 | /* |
| 3167 | * session->tty can be changed/cleared from under us, make sure we |
| 3168 | * issue the load. The obtained pointer, when not NULL, is valid as |
| 3169 | * long as we hold tty_mutex. |
| 3170 | */ |
| 3171 | barrier(); |
| 3172 | return tty; |
| 3173 | } |
Heiko Carstens | a311f74 | 2006-12-13 00:33:41 -0800 | [diff] [blame] | 3174 | EXPORT_SYMBOL_GPL(get_current_tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | |
| 3176 | /* |
| 3177 | * Initialize the console device. This is called *early*, so |
| 3178 | * we can't necessarily depend on lots of kernel help here. |
| 3179 | * Just do some early initializations, and do the complex setup |
| 3180 | * later. |
| 3181 | */ |
| 3182 | void __init console_init(void) |
| 3183 | { |
| 3184 | initcall_t *call; |
| 3185 | |
| 3186 | /* Setup the default TTY line discipline. */ |
Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 3187 | tty_ldisc_begin(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | |
| 3189 | /* |
Alan Cox | 37bdfb0 | 2008-02-08 04:18:47 -0800 | [diff] [blame] | 3190 | * set up the console device so that later boot sequences can |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | * inform about problems etc.. |
| 3192 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | call = __con_initcall_start; |
| 3194 | while (call < __con_initcall_end) { |
| 3195 | (*call)(); |
| 3196 | call++; |
| 3197 | } |
| 3198 | } |
| 3199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | static int __init tty_class_init(void) |
| 3201 | { |
gregkh@suse.de | 7fe845d | 2005-03-15 14:23:15 -0800 | [diff] [blame] | 3202 | tty_class = class_create(THIS_MODULE, "tty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | if (IS_ERR(tty_class)) |
| 3204 | return PTR_ERR(tty_class); |
| 3205 | return 0; |
| 3206 | } |
| 3207 | |
| 3208 | postcore_initcall(tty_class_init); |
| 3209 | |
| 3210 | /* 3/2004 jmc: why do these devices exist? */ |
| 3211 | |
| 3212 | static struct cdev tty_cdev, console_cdev; |
| 3213 | #ifdef CONFIG_UNIX98_PTYS |
| 3214 | static struct cdev ptmx_cdev; |
| 3215 | #endif |
| 3216 | #ifdef CONFIG_VT |
| 3217 | static struct cdev vc0_cdev; |
| 3218 | #endif |
| 3219 | |
| 3220 | /* |
| 3221 | * Ok, now we can initialize the rest of the tty devices and can count |
| 3222 | * on memory allocations, interrupts etc.. |
| 3223 | */ |
| 3224 | static int __init tty_init(void) |
| 3225 | { |
| 3226 | cdev_init(&tty_cdev, &tty_fops); |
| 3227 | if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || |
| 3228 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) |
| 3229 | panic("Couldn't register /dev/tty driver\n"); |
Greg Kroah-Hartman | 47aa579 | 2008-05-21 12:52:33 -0700 | [diff] [blame] | 3230 | device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, |
| 3231 | "tty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | |
| 3233 | cdev_init(&console_cdev, &console_fops); |
| 3234 | if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || |
| 3235 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) |
| 3236 | panic("Couldn't register /dev/console driver\n"); |
Greg Kroah-Hartman | 47aa579 | 2008-05-21 12:52:33 -0700 | [diff] [blame] | 3237 | device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, |
| 3238 | "console"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | |
| 3240 | #ifdef CONFIG_UNIX98_PTYS |
| 3241 | cdev_init(&ptmx_cdev, &ptmx_fops); |
| 3242 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || |
| 3243 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) |
| 3244 | panic("Couldn't register /dev/ptmx driver\n"); |
Greg Kroah-Hartman | 47aa579 | 2008-05-21 12:52:33 -0700 | [diff] [blame] | 3245 | device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | #endif |
| 3247 | |
| 3248 | #ifdef CONFIG_VT |
| 3249 | cdev_init(&vc0_cdev, &console_fops); |
| 3250 | if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || |
| 3251 | register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) |
| 3252 | panic("Couldn't register /dev/tty0 driver\n"); |
Greg Kroah-Hartman | 47aa579 | 2008-05-21 12:52:33 -0700 | [diff] [blame] | 3253 | device_create_drvdata(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | |
| 3255 | vty_init(); |
| 3256 | #endif |
| 3257 | return 0; |
| 3258 | } |
| 3259 | module_init(tty_init); |