Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/pty.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * Added support for a Unix98-style ptmx device. |
| 7 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 9 | * When reading this code see also fs/devpts. In particular note that the |
| 10 | * driver_data field is used by the devpts side as a binding to the devpts |
| 11 | * inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/tty.h> |
| 19 | #include <linux/tty_flip.h> |
| 20 | #include <linux/fcntl.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/major.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/sysctl.h> |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 26 | #include <linux/device.h> |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/bitops.h> |
| 29 | #include <linux/devpts_fs.h> |
| 30 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 31 | #include <asm/system.h> |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #ifdef CONFIG_UNIX98_PTYS |
Roel Kluin | da2bdf9 | 2009-01-07 18:09:15 -0800 | [diff] [blame] | 34 | static struct tty_driver *ptm_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static struct tty_driver *pts_driver; |
| 36 | #endif |
| 37 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 38 | static void pty_close(struct tty_struct *tty, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 40 | BUG_ON(!tty); |
| 41 | if (tty->driver->subtype == PTY_TYPE_MASTER) |
| 42 | WARN_ON(tty->count > 1); |
| 43 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | if (tty->count > 2) |
| 45 | return; |
| 46 | } |
| 47 | wake_up_interruptible(&tty->read_wait); |
| 48 | wake_up_interruptible(&tty->write_wait); |
| 49 | tty->packet = 0; |
| 50 | if (!tty->link) |
| 51 | return; |
| 52 | tty->link->packet = 0; |
| 53 | set_bit(TTY_OTHER_CLOSED, &tty->link->flags); |
| 54 | wake_up_interruptible(&tty->link->read_wait); |
| 55 | wake_up_interruptible(&tty->link->write_wait); |
| 56 | if (tty->driver->subtype == PTY_TYPE_MASTER) { |
| 57 | set_bit(TTY_OTHER_CLOSED, &tty->flags); |
| 58 | #ifdef CONFIG_UNIX98_PTYS |
| 59 | if (tty->driver == ptm_driver) |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 60 | devpts_pty_kill(tty->link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |
| 62 | tty_vhangup(tty->link); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * The unthrottle routine is called by the line discipline to signal |
| 68 | * that it can receive more characters. For PTY's, the TTY_THROTTLED |
| 69 | * flag is always set, to force the line discipline to always call the |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 70 | * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | * characters in the queue. This is necessary since each time this |
| 72 | * happens, we need to wake up any sleeping processes that could be |
| 73 | * (1) trying to send data to the pty, or (2) waiting in wait_until_sent() |
| 74 | * for the pty buffer to be drained. |
| 75 | */ |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 76 | static void pty_unthrottle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | struct tty_struct *o_tty = tty->link; |
| 79 | |
| 80 | if (!o_tty) |
| 81 | return; |
| 82 | |
| 83 | tty_wakeup(o_tty); |
| 84 | set_bit(TTY_THROTTLED, &tty->flags); |
| 85 | } |
| 86 | |
| 87 | /* |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 88 | * WSH 05/24/97: modified to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * (1) use space in tty->flip instead of a shared temp buffer |
| 90 | * The flip buffers aren't being used for a pty, so there's lots |
| 91 | * of space available. The buffer is protected by a per-pty |
| 92 | * semaphore that should almost never come under contention. |
| 93 | * (2) avoid redundant copying for cases where count >> receive_room |
| 94 | * N.B. Calls from user space may now return an error code instead of |
| 95 | * a count. |
| 96 | * |
| 97 | * FIXME: Our pty_write method is called with our ldisc lock held but |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 98 | * not our partners. We can't just wait on the other one blindly without |
| 99 | * risking deadlocks. At some point when everything has settled down we need |
| 100 | * to look into making pty_write at least able to sleep over an ldisc change. |
| 101 | * |
| 102 | * The return on no ldisc is a bit counter intuitive but the logic works |
| 103 | * like this. During an ldisc change the other end will flush its buffers. We |
| 104 | * thus return the full length which is identical to the case where we had |
| 105 | * proper locking and happened to queue the bytes just before the flush during |
| 106 | * the ldisc change. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | */ |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 108 | static int pty_write(struct tty_struct *tty, const unsigned char *buf, |
| 109 | int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct tty_struct *to = tty->link; |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 112 | struct tty_ldisc *ld; |
| 113 | int c = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | if (!to || tty->stopped) |
| 116 | return 0; |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 117 | ld = tty_ldisc_ref(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 119 | if (ld) { |
| 120 | c = to->receive_room; |
| 121 | if (c > count) |
| 122 | c = count; |
| 123 | ld->ops->receive_buf(to, buf, NULL, c); |
| 124 | tty_ldisc_deref(ld); |
| 125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return c; |
| 127 | } |
| 128 | |
| 129 | static int pty_write_room(struct tty_struct *tty) |
| 130 | { |
| 131 | struct tty_struct *to = tty->link; |
| 132 | |
| 133 | if (!to || tty->stopped) |
| 134 | return 0; |
| 135 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 136 | return to->receive_room; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* |
| 140 | * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 141 | * The chars_in_buffer() value is used by the ldisc select() function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256). |
| 143 | * The pty driver chars_in_buffer() Master/Slave must behave differently: |
| 144 | * |
| 145 | * The Master side needs to allow typed-ahead commands to accumulate |
| 146 | * while being canonicalized, so we report "our buffer" as empty until |
| 147 | * some threshold is reached, and then report the count. (Any count > |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 148 | * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock |
| 149 | * the count returned must be 0 if no canonical data is available to be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | * read. (The N_TTY ldisc.chars_in_buffer now knows this.) |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 151 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | * The Slave side passes all characters in raw mode to the Master side's |
| 153 | * buffer where they can be read immediately, so in this case we can |
| 154 | * return the true count in the buffer. |
| 155 | */ |
| 156 | static int pty_chars_in_buffer(struct tty_struct *tty) |
| 157 | { |
| 158 | struct tty_struct *to = tty->link; |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 159 | struct tty_ldisc *ld; |
| 160 | int count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | /* We should get the line discipline lock for "tty->link" */ |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 163 | if (!to) |
| 164 | return 0; |
| 165 | /* We cannot take a sleeping reference here without deadlocking with |
| 166 | an ldisc change - but it doesn't really matter */ |
| 167 | ld = tty_ldisc_ref(to); |
| 168 | if (ld == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return 0; |
| 170 | |
| 171 | /* The ldisc must report 0 if no characters available to be read */ |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 172 | if (ld->ops->chars_in_buffer) |
| 173 | count = ld->ops->chars_in_buffer(to); |
| 174 | |
| 175 | tty_ldisc_deref(ld); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 177 | if (tty->driver->subtype == PTY_TYPE_SLAVE) |
| 178 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 180 | /* Master side driver ... if the other side's read buffer is less than |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | * half full, return 0 to allow writers to proceed; otherwise return |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 182 | * the count. This leaves a comfortable margin to avoid overflow, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * and still allows half a buffer's worth of typed-ahead commands. |
| 184 | */ |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 185 | return (count < N_TTY_BUF_SIZE/2) ? 0 : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* Set the lock flag on a pty */ |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 189 | static int pty_set_lock(struct tty_struct *tty, int __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | int val; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 192 | if (get_user(val, arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return -EFAULT; |
| 194 | if (val) |
| 195 | set_bit(TTY_PTY_LOCK, &tty->flags); |
| 196 | else |
| 197 | clear_bit(TTY_PTY_LOCK, &tty->flags); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static void pty_flush_buffer(struct tty_struct *tty) |
| 202 | { |
| 203 | struct tty_struct *to = tty->link; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 204 | unsigned long flags; |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 205 | struct tty_ldisc *ld; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (!to) |
| 208 | return; |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 209 | ld = tty_ldisc_ref(to); |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 210 | |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 211 | /* The other end is changing discipline */ |
| 212 | if (!ld) |
| 213 | return; |
| 214 | |
| 215 | if (ld->ops->flush_buffer) |
Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 216 | to->ldisc->ops->flush_buffer(to); |
Alan Cox | 762faae | 2009-06-16 17:01:13 +0100 | [diff] [blame^] | 217 | tty_ldisc_deref(ld); |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (to->packet) { |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 220 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | tty->ctrl_status |= TIOCPKT_FLUSHWRITE; |
| 222 | wake_up_interruptible(&to->read_wait); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 223 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 227 | static int pty_open(struct tty_struct *tty, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 229 | int retval = -ENODEV; |
| 230 | |
| 231 | if (!tty || !tty->link) |
| 232 | goto out; |
| 233 | |
| 234 | retval = -EIO; |
| 235 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) |
| 236 | goto out; |
| 237 | if (test_bit(TTY_PTY_LOCK, &tty->link->flags)) |
| 238 | goto out; |
| 239 | if (tty->link->count != 1) |
| 240 | goto out; |
| 241 | |
| 242 | clear_bit(TTY_OTHER_CLOSED, &tty->link->flags); |
| 243 | set_bit(TTY_THROTTLED, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | retval = 0; |
| 245 | out: |
| 246 | return retval; |
| 247 | } |
| 248 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 249 | static void pty_set_termios(struct tty_struct *tty, |
| 250 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 252 | tty->termios->c_cflag &= ~(CSIZE | PARENB); |
| 253 | tty->termios->c_cflag |= (CS8 | CREAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 256 | /** |
| 257 | * pty_do_resize - resize event |
| 258 | * @tty: tty being resized |
Alan Cox | c774bda | 2009-01-11 19:46:49 +0000 | [diff] [blame] | 259 | * @ws: window size being set. |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 260 | * |
| 261 | * Update the termios variables and send the neccessary signals to |
| 262 | * peform a terminal resize correctly |
| 263 | */ |
| 264 | |
| 265 | int pty_resize(struct tty_struct *tty, struct winsize *ws) |
| 266 | { |
| 267 | struct pid *pgrp, *rpgrp; |
| 268 | unsigned long flags; |
| 269 | struct tty_struct *pty = tty->link; |
| 270 | |
| 271 | /* For a PTY we need to lock the tty side */ |
| 272 | mutex_lock(&tty->termios_mutex); |
| 273 | if (!memcmp(ws, &tty->winsize, sizeof(*ws))) |
| 274 | goto done; |
| 275 | |
| 276 | /* Get the PID values and reference them so we can |
| 277 | avoid holding the tty ctrl lock while sending signals. |
| 278 | We need to lock these individually however. */ |
| 279 | |
| 280 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
| 281 | pgrp = get_pid(tty->pgrp); |
| 282 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
| 283 | |
| 284 | spin_lock_irqsave(&pty->ctrl_lock, flags); |
| 285 | rpgrp = get_pid(pty->pgrp); |
| 286 | spin_unlock_irqrestore(&pty->ctrl_lock, flags); |
| 287 | |
| 288 | if (pgrp) |
| 289 | kill_pgrp(pgrp, SIGWINCH, 1); |
| 290 | if (rpgrp != pgrp && rpgrp) |
| 291 | kill_pgrp(rpgrp, SIGWINCH, 1); |
| 292 | |
| 293 | put_pid(pgrp); |
| 294 | put_pid(rpgrp); |
| 295 | |
| 296 | tty->winsize = *ws; |
| 297 | pty->winsize = *ws; /* Never used so will go away soon */ |
| 298 | done: |
| 299 | mutex_unlock(&tty->termios_mutex); |
| 300 | return 0; |
| 301 | } |
| 302 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 303 | static int pty_install(struct tty_driver *driver, struct tty_struct *tty) |
| 304 | { |
| 305 | struct tty_struct *o_tty; |
| 306 | int idx = tty->index; |
| 307 | int retval; |
| 308 | |
| 309 | o_tty = alloc_tty_struct(); |
| 310 | if (!o_tty) |
| 311 | return -ENOMEM; |
| 312 | if (!try_module_get(driver->other->owner)) { |
| 313 | /* This cannot in fact currently happen */ |
| 314 | free_tty_struct(o_tty); |
| 315 | return -ENOMEM; |
| 316 | } |
| 317 | initialize_tty_struct(o_tty, driver->other, idx); |
| 318 | |
| 319 | /* We always use new tty termios data so we can do this |
| 320 | the easy way .. */ |
| 321 | retval = tty_init_termios(tty); |
| 322 | if (retval) |
| 323 | goto free_mem_out; |
| 324 | |
| 325 | retval = tty_init_termios(o_tty); |
| 326 | if (retval) { |
| 327 | tty_free_termios(tty); |
| 328 | goto free_mem_out; |
| 329 | } |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 330 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 331 | /* |
| 332 | * Everything allocated ... set up the o_tty structure. |
| 333 | */ |
| 334 | driver->other->ttys[idx] = o_tty; |
| 335 | tty_driver_kref_get(driver->other); |
| 336 | if (driver->subtype == PTY_TYPE_MASTER) |
| 337 | o_tty->count++; |
| 338 | /* Establish the links in both directions */ |
| 339 | tty->link = o_tty; |
| 340 | o_tty->link = tty; |
| 341 | |
| 342 | tty_driver_kref_get(driver); |
| 343 | tty->count++; |
| 344 | driver->ttys[idx] = tty; |
| 345 | return 0; |
| 346 | free_mem_out: |
| 347 | module_put(o_tty->driver->owner); |
| 348 | free_tty_struct(o_tty); |
| 349 | return -ENOMEM; |
| 350 | } |
| 351 | |
| 352 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 353 | static const struct tty_operations pty_ops = { |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 354 | .install = pty_install, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | .open = pty_open, |
| 356 | .close = pty_close, |
| 357 | .write = pty_write, |
| 358 | .write_room = pty_write_room, |
| 359 | .flush_buffer = pty_flush_buffer, |
| 360 | .chars_in_buffer = pty_chars_in_buffer, |
| 361 | .unthrottle = pty_unthrottle, |
| 362 | .set_termios = pty_set_termios, |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 363 | .resize = pty_resize |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | /* Traditional BSD devices */ |
| 367 | #ifdef CONFIG_LEGACY_PTYS |
| 368 | static struct tty_driver *pty_driver, *pty_slave_driver; |
| 369 | |
| 370 | static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file, |
| 371 | unsigned int cmd, unsigned long arg) |
| 372 | { |
| 373 | switch (cmd) { |
| 374 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ |
| 375 | return pty_set_lock(tty, (int __user *) arg); |
| 376 | } |
| 377 | return -ENOIOCTLCMD; |
| 378 | } |
| 379 | |
Kay Sievers | dc8c858 | 2007-08-15 12:25:38 +0200 | [diff] [blame] | 380 | static int legacy_count = CONFIG_LEGACY_PTY_COUNT; |
| 381 | module_param(legacy_count, int, 0); |
| 382 | |
Alan Cox | 3e8e88c | 2008-04-30 00:54:10 -0700 | [diff] [blame] | 383 | static const struct tty_operations pty_ops_bsd = { |
| 384 | .open = pty_open, |
| 385 | .close = pty_close, |
| 386 | .write = pty_write, |
| 387 | .write_room = pty_write_room, |
| 388 | .flush_buffer = pty_flush_buffer, |
| 389 | .chars_in_buffer = pty_chars_in_buffer, |
| 390 | .unthrottle = pty_unthrottle, |
| 391 | .set_termios = pty_set_termios, |
| 392 | .ioctl = pty_bsd_ioctl, |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 393 | .resize = pty_resize |
Alan Cox | 3e8e88c | 2008-04-30 00:54:10 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | static void __init legacy_pty_init(void) |
| 397 | { |
Kay Sievers | dc8c858 | 2007-08-15 12:25:38 +0200 | [diff] [blame] | 398 | if (legacy_count <= 0) |
| 399 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
Kay Sievers | dc8c858 | 2007-08-15 12:25:38 +0200 | [diff] [blame] | 401 | pty_driver = alloc_tty_driver(legacy_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (!pty_driver) |
| 403 | panic("Couldn't allocate pty driver"); |
| 404 | |
Kay Sievers | dc8c858 | 2007-08-15 12:25:38 +0200 | [diff] [blame] | 405 | pty_slave_driver = alloc_tty_driver(legacy_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if (!pty_slave_driver) |
| 407 | panic("Couldn't allocate pty slave driver"); |
| 408 | |
| 409 | pty_driver->owner = THIS_MODULE; |
| 410 | pty_driver->driver_name = "pty_master"; |
| 411 | pty_driver->name = "pty"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | pty_driver->major = PTY_MASTER_MAJOR; |
| 413 | pty_driver->minor_start = 0; |
| 414 | pty_driver->type = TTY_DRIVER_TYPE_PTY; |
| 415 | pty_driver->subtype = PTY_TYPE_MASTER; |
| 416 | pty_driver->init_termios = tty_std_termios; |
| 417 | pty_driver->init_termios.c_iflag = 0; |
| 418 | pty_driver->init_termios.c_oflag = 0; |
| 419 | pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
| 420 | pty_driver->init_termios.c_lflag = 0; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 421 | pty_driver->init_termios.c_ispeed = 38400; |
| 422 | pty_driver->init_termios.c_ospeed = 38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW; |
| 424 | pty_driver->other = pty_slave_driver; |
| 425 | tty_set_operations(pty_driver, &pty_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | pty_slave_driver->owner = THIS_MODULE; |
| 428 | pty_slave_driver->driver_name = "pty_slave"; |
| 429 | pty_slave_driver->name = "ttyp"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | pty_slave_driver->major = PTY_SLAVE_MAJOR; |
| 431 | pty_slave_driver->minor_start = 0; |
| 432 | pty_slave_driver->type = TTY_DRIVER_TYPE_PTY; |
| 433 | pty_slave_driver->subtype = PTY_TYPE_SLAVE; |
| 434 | pty_slave_driver->init_termios = tty_std_termios; |
| 435 | pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 436 | pty_slave_driver->init_termios.c_ispeed = 38400; |
| 437 | pty_slave_driver->init_termios.c_ospeed = 38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS | |
| 439 | TTY_DRIVER_REAL_RAW; |
| 440 | pty_slave_driver->other = pty_driver; |
| 441 | tty_set_operations(pty_slave_driver, &pty_ops); |
| 442 | |
| 443 | if (tty_register_driver(pty_driver)) |
| 444 | panic("Couldn't register pty driver"); |
| 445 | if (tty_register_driver(pty_slave_driver)) |
| 446 | panic("Couldn't register pty slave driver"); |
| 447 | } |
| 448 | #else |
| 449 | static inline void legacy_pty_init(void) { } |
| 450 | #endif |
| 451 | |
| 452 | /* Unix98 devices */ |
| 453 | #ifdef CONFIG_UNIX98_PTYS |
| 454 | /* |
| 455 | * sysctl support for setting limits on the number of Unix98 ptys allocated. |
| 456 | * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly. |
| 457 | */ |
| 458 | int pty_limit = NR_UNIX98_PTY_DEFAULT; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 459 | static int pty_limit_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | static int pty_limit_max = NR_UNIX98_PTY_MAX; |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 461 | static int pty_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 463 | static struct cdev ptmx_cdev; |
| 464 | |
Eric W. Biederman | 35834ca | 2007-10-18 03:05:31 -0700 | [diff] [blame] | 465 | static struct ctl_table pty_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
| 467 | .ctl_name = PTY_MAX, |
| 468 | .procname = "max", |
| 469 | .maxlen = sizeof(int), |
| 470 | .mode = 0644, |
| 471 | .data = &pty_limit, |
| 472 | .proc_handler = &proc_dointvec_minmax, |
| 473 | .strategy = &sysctl_intvec, |
| 474 | .extra1 = &pty_limit_min, |
| 475 | .extra2 = &pty_limit_max, |
| 476 | }, { |
| 477 | .ctl_name = PTY_NR, |
| 478 | .procname = "nr", |
| 479 | .maxlen = sizeof(int), |
| 480 | .mode = 0444, |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 481 | .data = &pty_count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | .proc_handler = &proc_dointvec, |
| 483 | }, { |
| 484 | .ctl_name = 0 |
| 485 | } |
| 486 | }; |
| 487 | |
Eric W. Biederman | 35834ca | 2007-10-18 03:05:31 -0700 | [diff] [blame] | 488 | static struct ctl_table pty_kern_table[] = { |
| 489 | { |
| 490 | .ctl_name = KERN_PTY, |
| 491 | .procname = "pty", |
| 492 | .mode = 0555, |
| 493 | .child = pty_table, |
| 494 | }, |
| 495 | {} |
| 496 | }; |
| 497 | |
| 498 | static struct ctl_table pty_root_table[] = { |
| 499 | { |
| 500 | .ctl_name = CTL_KERN, |
| 501 | .procname = "kernel", |
| 502 | .mode = 0555, |
| 503 | .child = pty_kern_table, |
| 504 | }, |
| 505 | {} |
| 506 | }; |
| 507 | |
| 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file, |
| 510 | unsigned int cmd, unsigned long arg) |
| 511 | { |
| 512 | switch (cmd) { |
| 513 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ |
| 514 | return pty_set_lock(tty, (int __user *)arg); |
| 515 | case TIOCGPTN: /* Get PT Number */ |
| 516 | return put_user(tty->index, (unsigned int __user *)arg); |
| 517 | } |
| 518 | |
| 519 | return -ENOIOCTLCMD; |
| 520 | } |
| 521 | |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 522 | /** |
| 523 | * ptm_unix98_lookup - find a pty master |
| 524 | * @driver: ptm driver |
| 525 | * @idx: tty index |
| 526 | * |
| 527 | * Look up a pty master device. Called under the tty_mutex for now. |
| 528 | * This provides our locking. |
| 529 | */ |
| 530 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 531 | static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver, |
| 532 | struct inode *ptm_inode, int idx) |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 533 | { |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 534 | struct tty_struct *tty = devpts_get_tty(ptm_inode, idx); |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 535 | if (tty) |
| 536 | tty = tty->link; |
| 537 | return tty; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * pts_unix98_lookup - find a pty slave |
| 542 | * @driver: pts driver |
| 543 | * @idx: tty index |
| 544 | * |
| 545 | * Look up a pty master device. Called under the tty_mutex for now. |
| 546 | * This provides our locking. |
| 547 | */ |
| 548 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 549 | static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, |
| 550 | struct inode *pts_inode, int idx) |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 551 | { |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 552 | struct tty_struct *tty = devpts_get_tty(pts_inode, idx); |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 553 | /* Master must be open before slave */ |
| 554 | if (!tty) |
| 555 | return ERR_PTR(-EIO); |
| 556 | return tty; |
| 557 | } |
| 558 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 559 | static void pty_unix98_shutdown(struct tty_struct *tty) |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 560 | { |
| 561 | /* We have our own method as we don't use the tty index */ |
| 562 | kfree(tty->termios); |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 563 | } |
| 564 | |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 565 | /* We have no need to install and remove our tty objects as devpts does all |
| 566 | the work for us */ |
| 567 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 568 | static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 569 | { |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 570 | struct tty_struct *o_tty; |
| 571 | int idx = tty->index; |
| 572 | |
| 573 | o_tty = alloc_tty_struct(); |
| 574 | if (!o_tty) |
| 575 | return -ENOMEM; |
| 576 | if (!try_module_get(driver->other->owner)) { |
| 577 | /* This cannot in fact currently happen */ |
| 578 | free_tty_struct(o_tty); |
| 579 | return -ENOMEM; |
| 580 | } |
| 581 | initialize_tty_struct(o_tty, driver->other, idx); |
| 582 | |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 583 | tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 584 | if (tty->termios == NULL) |
| 585 | goto free_mem_out; |
| 586 | *tty->termios = driver->init_termios; |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 587 | tty->termios_locked = tty->termios + 1; |
| 588 | |
| 589 | o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 590 | if (o_tty->termios == NULL) |
| 591 | goto free_mem_out; |
| 592 | *o_tty->termios = driver->other->init_termios; |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 593 | o_tty->termios_locked = o_tty->termios + 1; |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 594 | |
| 595 | tty_driver_kref_get(driver->other); |
| 596 | if (driver->subtype == PTY_TYPE_MASTER) |
| 597 | o_tty->count++; |
| 598 | /* Establish the links in both directions */ |
| 599 | tty->link = o_tty; |
| 600 | o_tty->link = tty; |
| 601 | /* |
| 602 | * All structures have been allocated, so now we install them. |
| 603 | * Failures after this point use release_tty to clean up, so |
| 604 | * there's no need to null out the local pointers. |
| 605 | */ |
| 606 | tty_driver_kref_get(driver); |
| 607 | tty->count++; |
| 608 | pty_count++; |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 609 | return 0; |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 610 | free_mem_out: |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 611 | kfree(o_tty->termios); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 612 | module_put(o_tty->driver->owner); |
| 613 | free_tty_struct(o_tty); |
Alan Cox | 8dff04e | 2008-10-13 10:43:58 +0100 | [diff] [blame] | 614 | kfree(tty->termios); |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 615 | return -ENOMEM; |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 618 | static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty) |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 619 | { |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 620 | pty_count--; |
Alan Cox | 8b0a88d | 2008-10-13 10:42:19 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 623 | static const struct tty_operations ptm_unix98_ops = { |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 624 | .lookup = ptm_unix98_lookup, |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 625 | .install = pty_unix98_install, |
| 626 | .remove = pty_unix98_remove, |
Alan Cox | 3e8e88c | 2008-04-30 00:54:10 -0700 | [diff] [blame] | 627 | .open = pty_open, |
| 628 | .close = pty_close, |
| 629 | .write = pty_write, |
| 630 | .write_room = pty_write_room, |
| 631 | .flush_buffer = pty_flush_buffer, |
| 632 | .chars_in_buffer = pty_chars_in_buffer, |
| 633 | .unthrottle = pty_unthrottle, |
| 634 | .set_termios = pty_set_termios, |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 635 | .ioctl = pty_unix98_ioctl, |
Alan Cox | fc6f623 | 2009-01-02 13:43:17 +0000 | [diff] [blame] | 636 | .shutdown = pty_unix98_shutdown, |
| 637 | .resize = pty_resize |
Alan Cox | 3e8e88c | 2008-04-30 00:54:10 -0700 | [diff] [blame] | 638 | }; |
| 639 | |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 640 | static const struct tty_operations pty_unix98_ops = { |
| 641 | .lookup = pts_unix98_lookup, |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 642 | .install = pty_unix98_install, |
| 643 | .remove = pty_unix98_remove, |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 644 | .open = pty_open, |
| 645 | .close = pty_close, |
| 646 | .write = pty_write, |
| 647 | .write_room = pty_write_room, |
| 648 | .flush_buffer = pty_flush_buffer, |
| 649 | .chars_in_buffer = pty_chars_in_buffer, |
| 650 | .unthrottle = pty_unthrottle, |
| 651 | .set_termios = pty_set_termios, |
Alan Cox | bf970ee | 2008-10-13 10:42:39 +0100 | [diff] [blame] | 652 | .shutdown = pty_unix98_shutdown |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 653 | }; |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 654 | |
| 655 | /** |
| 656 | * ptmx_open - open a unix 98 pty master |
| 657 | * @inode: inode of device file |
| 658 | * @filp: file pointer to tty |
| 659 | * |
| 660 | * Allocate a unix98 pty master device from the ptmx driver. |
| 661 | * |
| 662 | * Locking: tty_mutex protects the init_dev work. tty->count should |
| 663 | * protect the rest. |
| 664 | * allocated_ptys_lock handles the list of free pty numbers |
| 665 | */ |
| 666 | |
| 667 | static int __ptmx_open(struct inode *inode, struct file *filp) |
| 668 | { |
| 669 | struct tty_struct *tty; |
| 670 | int retval; |
| 671 | int index; |
| 672 | |
| 673 | nonseekable_open(inode, filp); |
| 674 | |
| 675 | /* find a device that is not in use. */ |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 676 | index = devpts_new_index(inode); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 677 | if (index < 0) |
| 678 | return index; |
| 679 | |
| 680 | mutex_lock(&tty_mutex); |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 681 | tty = tty_init_dev(ptm_driver, index, 1); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 682 | mutex_unlock(&tty_mutex); |
| 683 | |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 684 | if (IS_ERR(tty)) { |
| 685 | retval = PTR_ERR(tty); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 686 | goto out; |
Alan Cox | 73ec06f | 2008-10-13 10:42:29 +0100 | [diff] [blame] | 687 | } |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 688 | |
| 689 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ |
| 690 | filp->private_data = tty; |
| 691 | file_move(filp, &tty->tty_files); |
| 692 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 693 | retval = devpts_pty_new(inode, tty->link); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 694 | if (retval) |
| 695 | goto out1; |
| 696 | |
| 697 | retval = ptm_driver->ops->open(tty, filp); |
| 698 | if (!retval) |
| 699 | return 0; |
| 700 | out1: |
| 701 | tty_release_dev(filp); |
| 702 | return retval; |
| 703 | out: |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 704 | devpts_kill_index(inode, index); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 705 | return retval; |
| 706 | } |
| 707 | |
| 708 | static int ptmx_open(struct inode *inode, struct file *filp) |
| 709 | { |
| 710 | int ret; |
| 711 | |
| 712 | lock_kernel(); |
| 713 | ret = __ptmx_open(inode, filp); |
| 714 | unlock_kernel(); |
| 715 | return ret; |
| 716 | } |
| 717 | |
| 718 | static struct file_operations ptmx_fops; |
| 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | static void __init unix98_pty_init(void) |
| 721 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); |
| 723 | if (!ptm_driver) |
| 724 | panic("Couldn't allocate Unix98 ptm driver"); |
| 725 | pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); |
| 726 | if (!pts_driver) |
| 727 | panic("Couldn't allocate Unix98 pts driver"); |
| 728 | |
| 729 | ptm_driver->owner = THIS_MODULE; |
| 730 | ptm_driver->driver_name = "pty_master"; |
| 731 | ptm_driver->name = "ptm"; |
| 732 | ptm_driver->major = UNIX98_PTY_MASTER_MAJOR; |
| 733 | ptm_driver->minor_start = 0; |
| 734 | ptm_driver->type = TTY_DRIVER_TYPE_PTY; |
| 735 | ptm_driver->subtype = PTY_TYPE_MASTER; |
| 736 | ptm_driver->init_termios = tty_std_termios; |
| 737 | ptm_driver->init_termios.c_iflag = 0; |
| 738 | ptm_driver->init_termios.c_oflag = 0; |
| 739 | ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
| 740 | ptm_driver->init_termios.c_lflag = 0; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 741 | ptm_driver->init_termios.c_ispeed = 38400; |
| 742 | ptm_driver->init_termios.c_ospeed = 38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 744 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | ptm_driver->other = pts_driver; |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 746 | tty_set_operations(ptm_driver, &ptm_unix98_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | pts_driver->owner = THIS_MODULE; |
| 749 | pts_driver->driver_name = "pty_slave"; |
| 750 | pts_driver->name = "pts"; |
| 751 | pts_driver->major = UNIX98_PTY_SLAVE_MAJOR; |
| 752 | pts_driver->minor_start = 0; |
| 753 | pts_driver->type = TTY_DRIVER_TYPE_PTY; |
| 754 | pts_driver->subtype = PTY_TYPE_SLAVE; |
| 755 | pts_driver->init_termios = tty_std_termios; |
| 756 | pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 757 | pts_driver->init_termios.c_ispeed = 38400; |
| 758 | pts_driver->init_termios.c_ospeed = 38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | |
Greg Kroah-Hartman | 331b831 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 760 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | pts_driver->other = ptm_driver; |
Alan Cox | 99f1fe1 | 2008-10-13 10:42:00 +0100 | [diff] [blame] | 762 | tty_set_operations(pts_driver, &pty_unix98_ops); |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | if (tty_register_driver(ptm_driver)) |
| 765 | panic("Couldn't register Unix98 ptm driver"); |
| 766 | if (tty_register_driver(pts_driver)) |
| 767 | panic("Couldn't register Unix98 pts driver"); |
| 768 | |
Alan Cox | fe9cd96 | 2008-10-13 10:43:38 +0100 | [diff] [blame] | 769 | register_sysctl_table(pty_root_table); |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 770 | |
| 771 | /* Now create the /dev/ptmx special device */ |
| 772 | tty_default_fops(&ptmx_fops); |
| 773 | ptmx_fops.open = ptmx_open; |
| 774 | |
| 775 | cdev_init(&ptmx_cdev, &ptmx_fops); |
| 776 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || |
| 777 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) |
| 778 | panic("Couldn't register /dev/ptmx driver\n"); |
| 779 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | } |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | #else |
| 783 | static inline void unix98_pty_init(void) { } |
| 784 | #endif |
| 785 | |
| 786 | static int __init pty_init(void) |
| 787 | { |
| 788 | legacy_pty_init(); |
| 789 | unix98_pty_init(); |
| 790 | return 0; |
| 791 | } |
| 792 | module_init(pty_init); |