blob: 146c97613da0c2719f8084228d9cbca4a742814d [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/* These are global because they are accessed in tty_io.c */
34#ifdef CONFIG_UNIX98_PTYS
Roel Kluinda2bdf92009-01-07 18:09:15 -080035static struct tty_driver *ptm_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static struct tty_driver *pts_driver;
37#endif
38
Alan Coxfe9cd962008-10-13 10:43:38 +010039static void pty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Alan Coxfe9cd962008-10-13 10:43:38 +010041 BUG_ON(!tty);
42 if (tty->driver->subtype == PTY_TYPE_MASTER)
43 WARN_ON(tty->count > 1);
44 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (tty->count > 2)
46 return;
47 }
48 wake_up_interruptible(&tty->read_wait);
49 wake_up_interruptible(&tty->write_wait);
50 tty->packet = 0;
51 if (!tty->link)
52 return;
53 tty->link->packet = 0;
54 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
55 wake_up_interruptible(&tty->link->read_wait);
56 wake_up_interruptible(&tty->link->write_wait);
57 if (tty->driver->subtype == PTY_TYPE_MASTER) {
58 set_bit(TTY_OTHER_CLOSED, &tty->flags);
59#ifdef CONFIG_UNIX98_PTYS
60 if (tty->driver == ptm_driver)
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +010061 devpts_pty_kill(tty->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63 tty_vhangup(tty->link);
64 }
65}
66
67/*
68 * The unthrottle routine is called by the line discipline to signal
69 * that it can receive more characters. For PTY's, the TTY_THROTTLED
70 * flag is always set, to force the line discipline to always call the
Alan Coxfe9cd962008-10-13 10:43:38 +010071 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * characters in the queue. This is necessary since each time this
73 * happens, we need to wake up any sleeping processes that could be
74 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
75 * for the pty buffer to be drained.
76 */
Alan Coxfe9cd962008-10-13 10:43:38 +010077static void pty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct tty_struct *o_tty = tty->link;
80
81 if (!o_tty)
82 return;
83
84 tty_wakeup(o_tty);
85 set_bit(TTY_THROTTLED, &tty->flags);
86}
87
88/*
Alan Coxfe9cd962008-10-13 10:43:38 +010089 * WSH 05/24/97: modified to
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * (1) use space in tty->flip instead of a shared temp buffer
91 * The flip buffers aren't being used for a pty, so there's lots
92 * of space available. The buffer is protected by a per-pty
93 * semaphore that should almost never come under contention.
94 * (2) avoid redundant copying for cases where count >> receive_room
95 * N.B. Calls from user space may now return an error code instead of
96 * a count.
97 *
98 * FIXME: Our pty_write method is called with our ldisc lock held but
99 * not our partners. We can't just take the other one blindly without
Paul Fulghum817d6d32006-06-28 04:26:47 -0700100 * risking deadlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Alan Coxfe9cd962008-10-13 10:43:38 +0100102static int pty_write(struct tty_struct *tty, const unsigned char *buf,
103 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct tty_struct *to = tty->link;
106 int c;
107
108 if (!to || tty->stopped)
109 return 0;
110
Alan Cox33f0f882006-01-09 20:54:13 -0800111 c = to->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (c > count)
113 c = count;
Alan Coxa352def2008-07-16 21:53:12 +0100114 to->ldisc.ops->receive_buf(to, buf, NULL, c);
Alan Coxfe9cd962008-10-13 10:43:38 +0100115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return c;
117}
118
119static int pty_write_room(struct tty_struct *tty)
120{
121 struct tty_struct *to = tty->link;
122
123 if (!to || tty->stopped)
124 return 0;
125
Alan Cox33f0f882006-01-09 20:54:13 -0800126 return to->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129/*
130 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
Alan Coxfe9cd962008-10-13 10:43:38 +0100131 * The chars_in_buffer() value is used by the ldisc select() function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
133 * The pty driver chars_in_buffer() Master/Slave must behave differently:
134 *
135 * The Master side needs to allow typed-ahead commands to accumulate
136 * while being canonicalized, so we report "our buffer" as empty until
137 * some threshold is reached, and then report the count. (Any count >
Alan Coxfe9cd962008-10-13 10:43:38 +0100138 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
139 * the count returned must be 0 if no canonical data is available to be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
Alan Coxfe9cd962008-10-13 10:43:38 +0100141 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * The Slave side passes all characters in raw mode to the Master side's
143 * buffer where they can be read immediately, so in this case we can
144 * return the true count in the buffer.
145 */
146static int pty_chars_in_buffer(struct tty_struct *tty)
147{
148 struct tty_struct *to = tty->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int count;
150
151 /* We should get the line discipline lock for "tty->link" */
Alan Coxa352def2008-07-16 21:53:12 +0100152 if (!to || !to->ldisc.ops->chars_in_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
154
155 /* The ldisc must report 0 if no characters available to be read */
Alan Coxa352def2008-07-16 21:53:12 +0100156 count = to->ldisc.ops->chars_in_buffer(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Alan Coxfe9cd962008-10-13 10:43:38 +0100158 if (tty->driver->subtype == PTY_TYPE_SLAVE)
159 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Alan Coxfe9cd962008-10-13 10:43:38 +0100161 /* Master side driver ... if the other side's read buffer is less than
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * half full, return 0 to allow writers to proceed; otherwise return
Alan Coxfe9cd962008-10-13 10:43:38 +0100163 * the count. This leaves a comfortable margin to avoid overflow,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * and still allows half a buffer's worth of typed-ahead commands.
165 */
Alan Coxfe9cd962008-10-13 10:43:38 +0100166 return (count < N_TTY_BUF_SIZE/2) ? 0 : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/* Set the lock flag on a pty */
Alan Coxfe9cd962008-10-13 10:43:38 +0100170static int pty_set_lock(struct tty_struct *tty, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 int val;
Alan Coxfe9cd962008-10-13 10:43:38 +0100173 if (get_user(val, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -EFAULT;
175 if (val)
176 set_bit(TTY_PTY_LOCK, &tty->flags);
177 else
178 clear_bit(TTY_PTY_LOCK, &tty->flags);
179 return 0;
180}
181
182static void pty_flush_buffer(struct tty_struct *tty)
183{
184 struct tty_struct *to = tty->link;
Alan Cox04f378b2008-04-30 00:53:29 -0700185 unsigned long flags;
Alan Coxfe9cd962008-10-13 10:43:38 +0100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (!to)
188 return;
Alan Coxfe9cd962008-10-13 10:43:38 +0100189
Alan Coxa352def2008-07-16 21:53:12 +0100190 if (to->ldisc.ops->flush_buffer)
191 to->ldisc.ops->flush_buffer(to);
Alan Coxfe9cd962008-10-13 10:43:38 +0100192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (to->packet) {
Alan Cox04f378b2008-04-30 00:53:29 -0700194 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
196 wake_up_interruptible(&to->read_wait);
Alan Cox04f378b2008-04-30 00:53:29 -0700197 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199}
200
Alan Coxfe9cd962008-10-13 10:43:38 +0100201static int pty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int retval = -ENODEV;
204
205 if (!tty || !tty->link)
206 goto out;
207
208 retval = -EIO;
209 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
210 goto out;
211 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
212 goto out;
213 if (tty->link->count != 1)
214 goto out;
215
216 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
217 set_bit(TTY_THROTTLED, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 retval = 0;
219out:
220 return retval;
221}
222
Alan Coxfe9cd962008-10-13 10:43:38 +0100223static void pty_set_termios(struct tty_struct *tty,
224 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Alan Coxfe9cd962008-10-13 10:43:38 +0100226 tty->termios->c_cflag &= ~(CSIZE | PARENB);
227 tty->termios->c_cflag |= (CS8 | CREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Alan Coxfc6f6232009-01-02 13:43:17 +0000230/**
231 * pty_do_resize - resize event
232 * @tty: tty being resized
233 * @real_tty: real tty (not the same as tty if using a pty/tty pair)
234 * @rows: rows (character)
235 * @cols: cols (character)
236 *
237 * Update the termios variables and send the neccessary signals to
238 * peform a terminal resize correctly
239 */
240
241int pty_resize(struct tty_struct *tty, struct winsize *ws)
242{
243 struct pid *pgrp, *rpgrp;
244 unsigned long flags;
245 struct tty_struct *pty = tty->link;
246
247 /* For a PTY we need to lock the tty side */
248 mutex_lock(&tty->termios_mutex);
249 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
250 goto done;
251
252 /* Get the PID values and reference them so we can
253 avoid holding the tty ctrl lock while sending signals.
254 We need to lock these individually however. */
255
256 spin_lock_irqsave(&tty->ctrl_lock, flags);
257 pgrp = get_pid(tty->pgrp);
258 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
259
260 spin_lock_irqsave(&pty->ctrl_lock, flags);
261 rpgrp = get_pid(pty->pgrp);
262 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
263
264 if (pgrp)
265 kill_pgrp(pgrp, SIGWINCH, 1);
266 if (rpgrp != pgrp && rpgrp)
267 kill_pgrp(rpgrp, SIGWINCH, 1);
268
269 put_pid(pgrp);
270 put_pid(rpgrp);
271
272 tty->winsize = *ws;
273 pty->winsize = *ws; /* Never used so will go away soon */
274done:
275 mutex_unlock(&tty->termios_mutex);
276 return 0;
277}
278
Alan Coxbf970ee2008-10-13 10:42:39 +0100279static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
280{
281 struct tty_struct *o_tty;
282 int idx = tty->index;
283 int retval;
284
285 o_tty = alloc_tty_struct();
286 if (!o_tty)
287 return -ENOMEM;
288 if (!try_module_get(driver->other->owner)) {
289 /* This cannot in fact currently happen */
290 free_tty_struct(o_tty);
291 return -ENOMEM;
292 }
293 initialize_tty_struct(o_tty, driver->other, idx);
294
295 /* We always use new tty termios data so we can do this
296 the easy way .. */
297 retval = tty_init_termios(tty);
298 if (retval)
299 goto free_mem_out;
300
301 retval = tty_init_termios(o_tty);
302 if (retval) {
303 tty_free_termios(tty);
304 goto free_mem_out;
305 }
Alan Coxfe9cd962008-10-13 10:43:38 +0100306
Alan Coxbf970ee2008-10-13 10:42:39 +0100307 /*
308 * Everything allocated ... set up the o_tty structure.
309 */
310 driver->other->ttys[idx] = o_tty;
311 tty_driver_kref_get(driver->other);
312 if (driver->subtype == PTY_TYPE_MASTER)
313 o_tty->count++;
314 /* Establish the links in both directions */
315 tty->link = o_tty;
316 o_tty->link = tty;
317
318 tty_driver_kref_get(driver);
319 tty->count++;
320 driver->ttys[idx] = tty;
321 return 0;
322free_mem_out:
323 module_put(o_tty->driver->owner);
324 free_tty_struct(o_tty);
325 return -ENOMEM;
326}
327
328
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700329static const struct tty_operations pty_ops = {
Alan Coxbf970ee2008-10-13 10:42:39 +0100330 .install = pty_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 .open = pty_open,
332 .close = pty_close,
333 .write = pty_write,
334 .write_room = pty_write_room,
335 .flush_buffer = pty_flush_buffer,
336 .chars_in_buffer = pty_chars_in_buffer,
337 .unthrottle = pty_unthrottle,
338 .set_termios = pty_set_termios,
Alan Coxfc6f6232009-01-02 13:43:17 +0000339 .resize = pty_resize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
341
342/* Traditional BSD devices */
343#ifdef CONFIG_LEGACY_PTYS
344static struct tty_driver *pty_driver, *pty_slave_driver;
345
346static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
347 unsigned int cmd, unsigned long arg)
348{
349 switch (cmd) {
350 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
351 return pty_set_lock(tty, (int __user *) arg);
352 }
353 return -ENOIOCTLCMD;
354}
355
Kay Sieversdc8c8582007-08-15 12:25:38 +0200356static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
357module_param(legacy_count, int, 0);
358
Alan Cox3e8e88c2008-04-30 00:54:10 -0700359static const struct tty_operations pty_ops_bsd = {
360 .open = pty_open,
361 .close = pty_close,
362 .write = pty_write,
363 .write_room = pty_write_room,
364 .flush_buffer = pty_flush_buffer,
365 .chars_in_buffer = pty_chars_in_buffer,
366 .unthrottle = pty_unthrottle,
367 .set_termios = pty_set_termios,
368 .ioctl = pty_bsd_ioctl,
Alan Coxfc6f6232009-01-02 13:43:17 +0000369 .resize = pty_resize
Alan Cox3e8e88c2008-04-30 00:54:10 -0700370};
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static void __init legacy_pty_init(void)
373{
Kay Sieversdc8c8582007-08-15 12:25:38 +0200374 if (legacy_count <= 0)
375 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Kay Sieversdc8c8582007-08-15 12:25:38 +0200377 pty_driver = alloc_tty_driver(legacy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!pty_driver)
379 panic("Couldn't allocate pty driver");
380
Kay Sieversdc8c8582007-08-15 12:25:38 +0200381 pty_slave_driver = alloc_tty_driver(legacy_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!pty_slave_driver)
383 panic("Couldn't allocate pty slave driver");
384
385 pty_driver->owner = THIS_MODULE;
386 pty_driver->driver_name = "pty_master";
387 pty_driver->name = "pty";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 pty_driver->major = PTY_MASTER_MAJOR;
389 pty_driver->minor_start = 0;
390 pty_driver->type = TTY_DRIVER_TYPE_PTY;
391 pty_driver->subtype = PTY_TYPE_MASTER;
392 pty_driver->init_termios = tty_std_termios;
393 pty_driver->init_termios.c_iflag = 0;
394 pty_driver->init_termios.c_oflag = 0;
395 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
396 pty_driver->init_termios.c_lflag = 0;
Alan Cox606d0992006-12-08 02:38:45 -0800397 pty_driver->init_termios.c_ispeed = 38400;
398 pty_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
400 pty_driver->other = pty_slave_driver;
401 tty_set_operations(pty_driver, &pty_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 pty_slave_driver->owner = THIS_MODULE;
404 pty_slave_driver->driver_name = "pty_slave";
405 pty_slave_driver->name = "ttyp";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 pty_slave_driver->major = PTY_SLAVE_MAJOR;
407 pty_slave_driver->minor_start = 0;
408 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
409 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
410 pty_slave_driver->init_termios = tty_std_termios;
411 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
Alan Cox606d0992006-12-08 02:38:45 -0800412 pty_slave_driver->init_termios.c_ispeed = 38400;
413 pty_slave_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
415 TTY_DRIVER_REAL_RAW;
416 pty_slave_driver->other = pty_driver;
417 tty_set_operations(pty_slave_driver, &pty_ops);
418
419 if (tty_register_driver(pty_driver))
420 panic("Couldn't register pty driver");
421 if (tty_register_driver(pty_slave_driver))
422 panic("Couldn't register pty slave driver");
423}
424#else
425static inline void legacy_pty_init(void) { }
426#endif
427
428/* Unix98 devices */
429#ifdef CONFIG_UNIX98_PTYS
430/*
431 * sysctl support for setting limits on the number of Unix98 ptys allocated.
432 * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
433 */
434int pty_limit = NR_UNIX98_PTY_DEFAULT;
Alan Coxfe9cd962008-10-13 10:43:38 +0100435static int pty_limit_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static int pty_limit_max = NR_UNIX98_PTY_MAX;
Alan Coxfe9cd962008-10-13 10:43:38 +0100437static int pty_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Alan Coxd81ed102008-10-13 10:41:42 +0100439static struct cdev ptmx_cdev;
440
Eric W. Biederman35834ca2007-10-18 03:05:31 -0700441static struct ctl_table pty_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 {
443 .ctl_name = PTY_MAX,
444 .procname = "max",
445 .maxlen = sizeof(int),
446 .mode = 0644,
447 .data = &pty_limit,
448 .proc_handler = &proc_dointvec_minmax,
449 .strategy = &sysctl_intvec,
450 .extra1 = &pty_limit_min,
451 .extra2 = &pty_limit_max,
452 }, {
453 .ctl_name = PTY_NR,
454 .procname = "nr",
455 .maxlen = sizeof(int),
456 .mode = 0444,
Alan Coxbf970ee2008-10-13 10:42:39 +0100457 .data = &pty_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 .proc_handler = &proc_dointvec,
459 }, {
460 .ctl_name = 0
461 }
462};
463
Eric W. Biederman35834ca2007-10-18 03:05:31 -0700464static struct ctl_table pty_kern_table[] = {
465 {
466 .ctl_name = KERN_PTY,
467 .procname = "pty",
468 .mode = 0555,
469 .child = pty_table,
470 },
471 {}
472};
473
474static struct ctl_table pty_root_table[] = {
475 {
476 .ctl_name = CTL_KERN,
477 .procname = "kernel",
478 .mode = 0555,
479 .child = pty_kern_table,
480 },
481 {}
482};
483
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
486 unsigned int cmd, unsigned long arg)
487{
488 switch (cmd) {
489 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
490 return pty_set_lock(tty, (int __user *)arg);
491 case TIOCGPTN: /* Get PT Number */
492 return put_user(tty->index, (unsigned int __user *)arg);
493 }
494
495 return -ENOIOCTLCMD;
496}
497
Alan Cox99f1fe12008-10-13 10:42:00 +0100498/**
499 * ptm_unix98_lookup - find a pty master
500 * @driver: ptm driver
501 * @idx: tty index
502 *
503 * Look up a pty master device. Called under the tty_mutex for now.
504 * This provides our locking.
505 */
506
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100507static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
508 struct inode *ptm_inode, int idx)
Alan Cox99f1fe12008-10-13 10:42:00 +0100509{
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100510 struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
Alan Cox99f1fe12008-10-13 10:42:00 +0100511 if (tty)
512 tty = tty->link;
513 return tty;
514}
515
516/**
517 * pts_unix98_lookup - find a pty slave
518 * @driver: pts driver
519 * @idx: tty index
520 *
521 * Look up a pty master device. Called under the tty_mutex for now.
522 * This provides our locking.
523 */
524
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100525static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
526 struct inode *pts_inode, int idx)
Alan Cox99f1fe12008-10-13 10:42:00 +0100527{
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100528 struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
Alan Cox99f1fe12008-10-13 10:42:00 +0100529 /* Master must be open before slave */
530 if (!tty)
531 return ERR_PTR(-EIO);
532 return tty;
533}
534
Alan Coxbf970ee2008-10-13 10:42:39 +0100535static void pty_unix98_shutdown(struct tty_struct *tty)
Alan Coxfeebed62008-10-13 10:41:30 +0100536{
537 /* We have our own method as we don't use the tty index */
538 kfree(tty->termios);
Alan Coxfeebed62008-10-13 10:41:30 +0100539}
540
Alan Cox8b0a88d2008-10-13 10:42:19 +0100541/* We have no need to install and remove our tty objects as devpts does all
542 the work for us */
543
Alan Coxbf970ee2008-10-13 10:42:39 +0100544static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +0100545{
Alan Coxbf970ee2008-10-13 10:42:39 +0100546 struct tty_struct *o_tty;
547 int idx = tty->index;
548
549 o_tty = alloc_tty_struct();
550 if (!o_tty)
551 return -ENOMEM;
552 if (!try_module_get(driver->other->owner)) {
553 /* This cannot in fact currently happen */
554 free_tty_struct(o_tty);
555 return -ENOMEM;
556 }
557 initialize_tty_struct(o_tty, driver->other, idx);
558
Alan Cox8dff04e2008-10-13 10:43:58 +0100559 tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
Alan Coxbf970ee2008-10-13 10:42:39 +0100560 if (tty->termios == NULL)
561 goto free_mem_out;
562 *tty->termios = driver->init_termios;
Alan Cox8dff04e2008-10-13 10:43:58 +0100563 tty->termios_locked = tty->termios + 1;
564
565 o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
Alan Coxbf970ee2008-10-13 10:42:39 +0100566 if (o_tty->termios == NULL)
567 goto free_mem_out;
568 *o_tty->termios = driver->other->init_termios;
Alan Cox8dff04e2008-10-13 10:43:58 +0100569 o_tty->termios_locked = o_tty->termios + 1;
Alan Coxbf970ee2008-10-13 10:42:39 +0100570
571 tty_driver_kref_get(driver->other);
572 if (driver->subtype == PTY_TYPE_MASTER)
573 o_tty->count++;
574 /* Establish the links in both directions */
575 tty->link = o_tty;
576 o_tty->link = tty;
577 /*
578 * All structures have been allocated, so now we install them.
579 * Failures after this point use release_tty to clean up, so
580 * there's no need to null out the local pointers.
581 */
582 tty_driver_kref_get(driver);
583 tty->count++;
584 pty_count++;
Alan Cox8b0a88d2008-10-13 10:42:19 +0100585 return 0;
Alan Coxbf970ee2008-10-13 10:42:39 +0100586free_mem_out:
Alan Cox8dff04e2008-10-13 10:43:58 +0100587 kfree(o_tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +0100588 module_put(o_tty->driver->owner);
589 free_tty_struct(o_tty);
Alan Cox8dff04e2008-10-13 10:43:58 +0100590 kfree(tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +0100591 return -ENOMEM;
Alan Cox8b0a88d2008-10-13 10:42:19 +0100592}
593
Alan Coxbf970ee2008-10-13 10:42:39 +0100594static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +0100595{
Alan Coxbf970ee2008-10-13 10:42:39 +0100596 pty_count--;
Alan Cox8b0a88d2008-10-13 10:42:19 +0100597}
598
Alan Coxfeebed62008-10-13 10:41:30 +0100599static const struct tty_operations ptm_unix98_ops = {
Alan Cox99f1fe12008-10-13 10:42:00 +0100600 .lookup = ptm_unix98_lookup,
Alan Coxbf970ee2008-10-13 10:42:39 +0100601 .install = pty_unix98_install,
602 .remove = pty_unix98_remove,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700603 .open = pty_open,
604 .close = pty_close,
605 .write = pty_write,
606 .write_room = pty_write_room,
607 .flush_buffer = pty_flush_buffer,
608 .chars_in_buffer = pty_chars_in_buffer,
609 .unthrottle = pty_unthrottle,
610 .set_termios = pty_set_termios,
Alan Coxfeebed62008-10-13 10:41:30 +0100611 .ioctl = pty_unix98_ioctl,
Alan Coxfc6f6232009-01-02 13:43:17 +0000612 .shutdown = pty_unix98_shutdown,
613 .resize = pty_resize
Alan Cox3e8e88c2008-04-30 00:54:10 -0700614};
615
Alan Cox99f1fe12008-10-13 10:42:00 +0100616static const struct tty_operations pty_unix98_ops = {
617 .lookup = pts_unix98_lookup,
Alan Coxbf970ee2008-10-13 10:42:39 +0100618 .install = pty_unix98_install,
619 .remove = pty_unix98_remove,
Alan Cox99f1fe12008-10-13 10:42:00 +0100620 .open = pty_open,
621 .close = pty_close,
622 .write = pty_write,
623 .write_room = pty_write_room,
624 .flush_buffer = pty_flush_buffer,
625 .chars_in_buffer = pty_chars_in_buffer,
626 .unthrottle = pty_unthrottle,
627 .set_termios = pty_set_termios,
Alan Coxbf970ee2008-10-13 10:42:39 +0100628 .shutdown = pty_unix98_shutdown
Alan Cox99f1fe12008-10-13 10:42:00 +0100629};
Alan Coxd81ed102008-10-13 10:41:42 +0100630
631/**
632 * ptmx_open - open a unix 98 pty master
633 * @inode: inode of device file
634 * @filp: file pointer to tty
635 *
636 * Allocate a unix98 pty master device from the ptmx driver.
637 *
638 * Locking: tty_mutex protects the init_dev work. tty->count should
639 * protect the rest.
640 * allocated_ptys_lock handles the list of free pty numbers
641 */
642
643static int __ptmx_open(struct inode *inode, struct file *filp)
644{
645 struct tty_struct *tty;
646 int retval;
647 int index;
648
649 nonseekable_open(inode, filp);
650
651 /* find a device that is not in use. */
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100652 index = devpts_new_index(inode);
Alan Coxd81ed102008-10-13 10:41:42 +0100653 if (index < 0)
654 return index;
655
656 mutex_lock(&tty_mutex);
Alan Cox73ec06f2008-10-13 10:42:29 +0100657 tty = tty_init_dev(ptm_driver, index, 1);
Alan Coxd81ed102008-10-13 10:41:42 +0100658 mutex_unlock(&tty_mutex);
659
Alan Cox73ec06f2008-10-13 10:42:29 +0100660 if (IS_ERR(tty)) {
661 retval = PTR_ERR(tty);
Alan Coxd81ed102008-10-13 10:41:42 +0100662 goto out;
Alan Cox73ec06f2008-10-13 10:42:29 +0100663 }
Alan Coxd81ed102008-10-13 10:41:42 +0100664
665 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
666 filp->private_data = tty;
667 file_move(filp, &tty->tty_files);
668
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100669 retval = devpts_pty_new(inode, tty->link);
Alan Coxd81ed102008-10-13 10:41:42 +0100670 if (retval)
671 goto out1;
672
673 retval = ptm_driver->ops->open(tty, filp);
674 if (!retval)
675 return 0;
676out1:
677 tty_release_dev(filp);
678 return retval;
679out:
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100680 devpts_kill_index(inode, index);
Alan Coxd81ed102008-10-13 10:41:42 +0100681 return retval;
682}
683
684static int ptmx_open(struct inode *inode, struct file *filp)
685{
686 int ret;
687
688 lock_kernel();
689 ret = __ptmx_open(inode, filp);
690 unlock_kernel();
691 return ret;
692}
693
694static struct file_operations ptmx_fops;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static void __init unix98_pty_init(void)
697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
699 if (!ptm_driver)
700 panic("Couldn't allocate Unix98 ptm driver");
701 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
702 if (!pts_driver)
703 panic("Couldn't allocate Unix98 pts driver");
704
705 ptm_driver->owner = THIS_MODULE;
706 ptm_driver->driver_name = "pty_master";
707 ptm_driver->name = "ptm";
708 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
709 ptm_driver->minor_start = 0;
710 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
711 ptm_driver->subtype = PTY_TYPE_MASTER;
712 ptm_driver->init_termios = tty_std_termios;
713 ptm_driver->init_termios.c_iflag = 0;
714 ptm_driver->init_termios.c_oflag = 0;
715 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
716 ptm_driver->init_termios.c_lflag = 0;
Alan Cox606d0992006-12-08 02:38:45 -0800717 ptm_driver->init_termios.c_ispeed = 38400;
718 ptm_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -0700720 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ptm_driver->other = pts_driver;
Alan Coxfeebed62008-10-13 10:41:30 +0100722 tty_set_operations(ptm_driver, &ptm_unix98_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 pts_driver->owner = THIS_MODULE;
725 pts_driver->driver_name = "pty_slave";
726 pts_driver->name = "pts";
727 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
728 pts_driver->minor_start = 0;
729 pts_driver->type = TTY_DRIVER_TYPE_PTY;
730 pts_driver->subtype = PTY_TYPE_SLAVE;
731 pts_driver->init_termios = tty_std_termios;
732 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
Alan Cox606d0992006-12-08 02:38:45 -0800733 pts_driver->init_termios.c_ispeed = 38400;
734 pts_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -0700736 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 pts_driver->other = ptm_driver;
Alan Cox99f1fe12008-10-13 10:42:00 +0100738 tty_set_operations(pts_driver, &pty_unix98_ops);
Alan Coxfe9cd962008-10-13 10:43:38 +0100739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (tty_register_driver(ptm_driver))
741 panic("Couldn't register Unix98 ptm driver");
742 if (tty_register_driver(pts_driver))
743 panic("Couldn't register Unix98 pts driver");
744
Alan Coxfe9cd962008-10-13 10:43:38 +0100745 register_sysctl_table(pty_root_table);
Alan Coxd81ed102008-10-13 10:41:42 +0100746
747 /* Now create the /dev/ptmx special device */
748 tty_default_fops(&ptmx_fops);
749 ptmx_fops.open = ptmx_open;
750
751 cdev_init(&ptmx_cdev, &ptmx_fops);
752 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
753 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
754 panic("Couldn't register /dev/ptmx driver\n");
755 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
Alan Coxd81ed102008-10-13 10:41:42 +0100757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#else
759static inline void unix98_pty_init(void) { }
760#endif
761
762static int __init pty_init(void)
763{
764 legacy_pty_init();
765 unix98_pty_init();
766 return 0;
767}
768module_init(pty_init);