blob: daebe1ba43d42ad0f3a4053cea80e6c411e8968a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -07008 *
Alan Coxfe9cd962008-10-13 10:43:38 +01009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Alan Coxfe9cd962008-10-13 10:43:38 +010014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/sysctl.h>
Alan Coxd81ed102008-10-13 10:41:42 +010026#include <linux/device.h>
Alan Coxfe9cd962008-10-13 10:43:38 +010027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/bitops.h>
29#include <linux/devpts_fs.h>
30
Alan Coxfe9cd962008-10-13 10:43:38 +010031#include <asm/system.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#ifdef CONFIG_UNIX98_PTYS
Roel Kluinda2bdf92009-01-07 18:09:15 -080034static struct tty_driver *ptm_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static struct tty_driver *pts_driver;
36#endif
37
Alan Coxfe9cd962008-10-13 10:43:38 +010038static void pty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Alan Coxfe9cd962008-10-13 10:43:38 +010040 BUG_ON(!tty);
41 if (tty->driver->subtype == PTY_TYPE_MASTER)
42 WARN_ON(tty->count > 1);
43 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 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 Bhattiprolu15f1a632008-10-13 10:42:59 +010060 devpts_pty_kill(tty->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#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 Coxfe9cd962008-10-13 10:43:38 +010070 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * 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 Coxfe9cd962008-10-13 10:43:38 +010076static void pty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
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 Coxfe9cd962008-10-13 10:43:38 +010088 * WSH 05/24/97: modified to
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * (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 Cox762faae2009-06-16 17:01:13 +010098 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700107 */
Alan Coxfe9cd962008-10-13 10:43:38 +0100108static int pty_write(struct tty_struct *tty, const unsigned char *buf,
109 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct tty_struct *to = tty->link;
Alan Cox762faae2009-06-16 17:01:13 +0100112 struct tty_ldisc *ld;
113 int c = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (!to || tty->stopped)
116 return 0;
Alan Cox762faae2009-06-16 17:01:13 +0100117 ld = tty_ldisc_ref(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Alan Cox762faae2009-06-16 17:01:13 +0100119 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 Torvalds1da177e2005-04-16 15:20:36 -0700126 return c;
127}
128
129static 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 Cox33f0f882006-01-09 20:54:13 -0800136 return to->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/*
140 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
Alan Coxfe9cd962008-10-13 10:43:38 +0100141 * The chars_in_buffer() value is used by the ldisc select() function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * 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 Coxfe9cd962008-10-13 10:43:38 +0100148 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700150 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
Alan Coxfe9cd962008-10-13 10:43:38 +0100151 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * 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 */
156static int pty_chars_in_buffer(struct tty_struct *tty)
157{
158 struct tty_struct *to = tty->link;
Alan Cox762faae2009-06-16 17:01:13 +0100159 struct tty_ldisc *ld;
160 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* We should get the line discipline lock for "tty->link" */
Alan Cox762faae2009-06-16 17:01:13 +0100163 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 Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170
171 /* The ldisc must report 0 if no characters available to be read */
Alan Cox762faae2009-06-16 17:01:13 +0100172 if (ld->ops->chars_in_buffer)
173 count = ld->ops->chars_in_buffer(to);
174
175 tty_ldisc_deref(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Alan Coxfe9cd962008-10-13 10:43:38 +0100177 if (tty->driver->subtype == PTY_TYPE_SLAVE)
178 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Alan Coxfe9cd962008-10-13 10:43:38 +0100180 /* Master side driver ... if the other side's read buffer is less than
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * half full, return 0 to allow writers to proceed; otherwise return
Alan Coxfe9cd962008-10-13 10:43:38 +0100182 * the count. This leaves a comfortable margin to avoid overflow,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * and still allows half a buffer's worth of typed-ahead commands.
184 */
Alan Coxfe9cd962008-10-13 10:43:38 +0100185 return (count < N_TTY_BUF_SIZE/2) ? 0 : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/* Set the lock flag on a pty */
Alan Coxfe9cd962008-10-13 10:43:38 +0100189static int pty_set_lock(struct tty_struct *tty, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int val;
Alan Coxfe9cd962008-10-13 10:43:38 +0100192 if (get_user(val, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 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
201static void pty_flush_buffer(struct tty_struct *tty)
202{
203 struct tty_struct *to = tty->link;
Alan Cox04f378b2008-04-30 00:53:29 -0700204 unsigned long flags;
Alan Cox762faae2009-06-16 17:01:13 +0100205 struct tty_ldisc *ld;
Alan Coxfe9cd962008-10-13 10:43:38 +0100206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!to)
208 return;
Alan Cox762faae2009-06-16 17:01:13 +0100209 ld = tty_ldisc_ref(to);
Alan Coxfe9cd962008-10-13 10:43:38 +0100210
Alan Cox762faae2009-06-16 17:01:13 +0100211 /* The other end is changing discipline */
212 if (!ld)
213 return;
214
215 if (ld->ops->flush_buffer)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100216 to->ldisc->ops->flush_buffer(to);
Alan Cox762faae2009-06-16 17:01:13 +0100217 tty_ldisc_deref(ld);
Alan Coxfe9cd962008-10-13 10:43:38 +0100218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (to->packet) {
Alan Cox04f378b2008-04-30 00:53:29 -0700220 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
222 wake_up_interruptible(&to->read_wait);
Alan Cox04f378b2008-04-30 00:53:29 -0700223 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225}
226
Alan Coxfe9cd962008-10-13 10:43:38 +0100227static int pty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
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 Torvalds1da177e2005-04-16 15:20:36 -0700244 retval = 0;
245out:
246 return retval;
247}
248
Alan Coxfe9cd962008-10-13 10:43:38 +0100249static void pty_set_termios(struct tty_struct *tty,
250 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Alan Coxfe9cd962008-10-13 10:43:38 +0100252 tty->termios->c_cflag &= ~(CSIZE | PARENB);
253 tty->termios->c_cflag |= (CS8 | CREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Alan Coxfc6f6232009-01-02 13:43:17 +0000256/**
257 * pty_do_resize - resize event
258 * @tty: tty being resized
Alan Coxc774bda2009-01-11 19:46:49 +0000259 * @ws: window size being set.
Alan Coxfc6f6232009-01-02 13:43:17 +0000260 *
261 * Update the termios variables and send the neccessary signals to
262 * peform a terminal resize correctly
263 */
264
265int 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 */
298done:
299 mutex_unlock(&tty->termios_mutex);
300 return 0;
301}
302
Alan Coxbf970ee2008-10-13 10:42:39 +0100303static 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 Coxfe9cd962008-10-13 10:43:38 +0100330
Alan Coxbf970ee2008-10-13 10:42:39 +0100331 /*
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;
346free_mem_out:
347 module_put(o_tty->driver->owner);
348 free_tty_struct(o_tty);
349 return -ENOMEM;
350}
351
352
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700353static const struct tty_operations pty_ops = {
Alan Coxbf970ee2008-10-13 10:42:39 +0100354 .install = pty_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .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 Coxfc6f6232009-01-02 13:43:17 +0000363 .resize = pty_resize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364};
365
366/* Traditional BSD devices */
367#ifdef CONFIG_LEGACY_PTYS
368static struct tty_driver *pty_driver, *pty_slave_driver;
369
370static 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 Sieversdc8c8582007-08-15 12:25:38 +0200380static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
381module_param(legacy_count, int, 0);
382
Alan Cox3e8e88c2008-04-30 00:54:10 -0700383static 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 Coxfc6f6232009-01-02 13:43:17 +0000393 .resize = pty_resize
Alan Cox3e8e88c2008-04-30 00:54:10 -0700394};
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static void __init legacy_pty_init(void)
397{
Kay Sieversdc8c8582007-08-15 12:25:38 +0200398 if (legacy_count <= 0)
399 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Kay Sieversdc8c8582007-08-15 12:25:38 +0200401 pty_driver = alloc_tty_driver(legacy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!pty_driver)
403 panic("Couldn't allocate pty driver");
404
Kay Sieversdc8c8582007-08-15 12:25:38 +0200405 pty_slave_driver = alloc_tty_driver(legacy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 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 Torvalds1da177e2005-04-16 15:20:36 -0700412 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 Cox606d0992006-12-08 02:38:45 -0800421 pty_driver->init_termios.c_ispeed = 38400;
422 pty_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 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 Torvalds1da177e2005-04-16 15:20:36 -0700426
427 pty_slave_driver->owner = THIS_MODULE;
428 pty_slave_driver->driver_name = "pty_slave";
429 pty_slave_driver->name = "ttyp";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 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 Cox606d0992006-12-08 02:38:45 -0800436 pty_slave_driver->init_termios.c_ispeed = 38400;
437 pty_slave_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 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
449static 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 */
458int pty_limit = NR_UNIX98_PTY_DEFAULT;
Alan Coxfe9cd962008-10-13 10:43:38 +0100459static int pty_limit_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static int pty_limit_max = NR_UNIX98_PTY_MAX;
Alan Coxfe9cd962008-10-13 10:43:38 +0100461static int pty_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Alan Coxd81ed102008-10-13 10:41:42 +0100463static struct cdev ptmx_cdev;
464
Eric W. Biederman35834ca2007-10-18 03:05:31 -0700465static struct ctl_table pty_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 {
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 Coxbf970ee2008-10-13 10:42:39 +0100481 .data = &pty_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 .proc_handler = &proc_dointvec,
483 }, {
484 .ctl_name = 0
485 }
486};
487
Eric W. Biederman35834ca2007-10-18 03:05:31 -0700488static 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
498static 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 Torvalds1da177e2005-04-16 15:20:36 -0700509static 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 Cox99f1fe12008-10-13 10:42:00 +0100522/**
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 Bhattiprolu15f1a632008-10-13 10:42:59 +0100531static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
532 struct inode *ptm_inode, int idx)
Alan Cox99f1fe12008-10-13 10:42:00 +0100533{
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100534 struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
Alan Cox99f1fe12008-10-13 10:42:00 +0100535 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 Bhattiprolu15f1a632008-10-13 10:42:59 +0100549static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
550 struct inode *pts_inode, int idx)
Alan Cox99f1fe12008-10-13 10:42:00 +0100551{
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100552 struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
Alan Cox99f1fe12008-10-13 10:42:00 +0100553 /* Master must be open before slave */
554 if (!tty)
555 return ERR_PTR(-EIO);
556 return tty;
557}
558
Alan Coxbf970ee2008-10-13 10:42:39 +0100559static void pty_unix98_shutdown(struct tty_struct *tty)
Alan Coxfeebed62008-10-13 10:41:30 +0100560{
561 /* We have our own method as we don't use the tty index */
562 kfree(tty->termios);
Alan Coxfeebed62008-10-13 10:41:30 +0100563}
564
Alan Cox8b0a88d2008-10-13 10:42:19 +0100565/* We have no need to install and remove our tty objects as devpts does all
566 the work for us */
567
Alan Coxbf970ee2008-10-13 10:42:39 +0100568static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +0100569{
Alan Coxbf970ee2008-10-13 10:42:39 +0100570 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 Cox8dff04e2008-10-13 10:43:58 +0100583 tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
Alan Coxbf970ee2008-10-13 10:42:39 +0100584 if (tty->termios == NULL)
585 goto free_mem_out;
586 *tty->termios = driver->init_termios;
Alan Cox8dff04e2008-10-13 10:43:58 +0100587 tty->termios_locked = tty->termios + 1;
588
589 o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
Alan Coxbf970ee2008-10-13 10:42:39 +0100590 if (o_tty->termios == NULL)
591 goto free_mem_out;
592 *o_tty->termios = driver->other->init_termios;
Alan Cox8dff04e2008-10-13 10:43:58 +0100593 o_tty->termios_locked = o_tty->termios + 1;
Alan Coxbf970ee2008-10-13 10:42:39 +0100594
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 Cox8b0a88d2008-10-13 10:42:19 +0100609 return 0;
Alan Coxbf970ee2008-10-13 10:42:39 +0100610free_mem_out:
Alan Cox8dff04e2008-10-13 10:43:58 +0100611 kfree(o_tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +0100612 module_put(o_tty->driver->owner);
613 free_tty_struct(o_tty);
Alan Cox8dff04e2008-10-13 10:43:58 +0100614 kfree(tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +0100615 return -ENOMEM;
Alan Cox8b0a88d2008-10-13 10:42:19 +0100616}
617
Alan Coxbf970ee2008-10-13 10:42:39 +0100618static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +0100619{
Alan Coxbf970ee2008-10-13 10:42:39 +0100620 pty_count--;
Alan Cox8b0a88d2008-10-13 10:42:19 +0100621}
622
Alan Coxfeebed62008-10-13 10:41:30 +0100623static const struct tty_operations ptm_unix98_ops = {
Alan Cox99f1fe12008-10-13 10:42:00 +0100624 .lookup = ptm_unix98_lookup,
Alan Coxbf970ee2008-10-13 10:42:39 +0100625 .install = pty_unix98_install,
626 .remove = pty_unix98_remove,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700627 .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 Coxfeebed62008-10-13 10:41:30 +0100635 .ioctl = pty_unix98_ioctl,
Alan Coxfc6f6232009-01-02 13:43:17 +0000636 .shutdown = pty_unix98_shutdown,
637 .resize = pty_resize
Alan Cox3e8e88c2008-04-30 00:54:10 -0700638};
639
Alan Cox99f1fe12008-10-13 10:42:00 +0100640static const struct tty_operations pty_unix98_ops = {
641 .lookup = pts_unix98_lookup,
Alan Coxbf970ee2008-10-13 10:42:39 +0100642 .install = pty_unix98_install,
643 .remove = pty_unix98_remove,
Alan Cox99f1fe12008-10-13 10:42:00 +0100644 .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 Coxbf970ee2008-10-13 10:42:39 +0100652 .shutdown = pty_unix98_shutdown
Alan Cox99f1fe12008-10-13 10:42:00 +0100653};
Alan Coxd81ed102008-10-13 10:41:42 +0100654
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
667static 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 Bhattiprolu15f1a632008-10-13 10:42:59 +0100676 index = devpts_new_index(inode);
Alan Coxd81ed102008-10-13 10:41:42 +0100677 if (index < 0)
678 return index;
679
680 mutex_lock(&tty_mutex);
Alan Cox73ec06f2008-10-13 10:42:29 +0100681 tty = tty_init_dev(ptm_driver, index, 1);
Alan Coxd81ed102008-10-13 10:41:42 +0100682 mutex_unlock(&tty_mutex);
683
Alan Cox73ec06f2008-10-13 10:42:29 +0100684 if (IS_ERR(tty)) {
685 retval = PTR_ERR(tty);
Alan Coxd81ed102008-10-13 10:41:42 +0100686 goto out;
Alan Cox73ec06f2008-10-13 10:42:29 +0100687 }
Alan Coxd81ed102008-10-13 10:41:42 +0100688
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 Bhattiprolu15f1a632008-10-13 10:42:59 +0100693 retval = devpts_pty_new(inode, tty->link);
Alan Coxd81ed102008-10-13 10:41:42 +0100694 if (retval)
695 goto out1;
696
697 retval = ptm_driver->ops->open(tty, filp);
698 if (!retval)
699 return 0;
700out1:
701 tty_release_dev(filp);
702 return retval;
703out:
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100704 devpts_kill_index(inode, index);
Alan Coxd81ed102008-10-13 10:41:42 +0100705 return retval;
706}
707
708static 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
718static struct file_operations ptmx_fops;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static void __init unix98_pty_init(void)
721{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 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 Cox606d0992006-12-08 02:38:45 -0800741 ptm_driver->init_termios.c_ispeed = 38400;
742 ptm_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -0700744 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 ptm_driver->other = pts_driver;
Alan Coxfeebed62008-10-13 10:41:30 +0100746 tty_set_operations(ptm_driver, &ptm_unix98_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
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 Cox606d0992006-12-08 02:38:45 -0800757 pts_driver->init_termios.c_ispeed = 38400;
758 pts_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -0700760 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 pts_driver->other = ptm_driver;
Alan Cox99f1fe12008-10-13 10:42:00 +0100762 tty_set_operations(pts_driver, &pty_unix98_ops);
Alan Coxfe9cd962008-10-13 10:43:38 +0100763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 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 Coxfe9cd962008-10-13 10:43:38 +0100769 register_sysctl_table(pty_root_table);
Alan Coxd81ed102008-10-13 10:41:42 +0100770
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 Torvalds1da177e2005-04-16 15:20:36 -0700780}
Alan Coxd81ed102008-10-13 10:41:42 +0100781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782#else
783static inline void unix98_pty_init(void) { }
784#endif
785
786static int __init pty_init(void)
787{
788 legacy_pty_init();
789 unix98_pty_init();
790 return 0;
791}
792module_init(pty_init);