2 * linux/drivers/char/pty.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Added support for a Unix98-style ptmx device.
7 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
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
14 #include <linux/module.h>
16 #include <linux/errno.h>
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>
24 #include <linux/init.h>
25 #include <linux/sysctl.h>
26 #include <linux/device.h>
27 #include <linux/uaccess.h>
28 #include <linux/bitops.h>
29 #include <linux/devpts_fs.h>
31 #include <asm/system.h>
33 #ifdef CONFIG_UNIX98_PTYS
34 static struct tty_driver *ptm_driver;
35 static struct tty_driver *pts_driver;
38 static void pty_close(struct tty_struct *tty, struct file *filp)
41 if (tty->driver->subtype == PTY_TYPE_MASTER)
42 WARN_ON(tty->count > 1);
47 wake_up_interruptible(&tty->read_wait);
48 wake_up_interruptible(&tty->write_wait);
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)
60 devpts_pty_kill(tty->link);
62 tty_vhangup(tty->link);
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
70 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
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.
76 static void pty_unthrottle(struct tty_struct *tty)
78 struct tty_struct *o_tty = tty->link;
84 set_bit(TTY_THROTTLED, &tty->flags);
88 * WSH 05/24/97: modified to
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
97 * FIXME: Our pty_write method is called with our ldisc lock held but
98 * not our partners. We can't just take the other one blindly without
101 static int pty_write(struct tty_struct *tty, const unsigned char *buf,
104 struct tty_struct *to = tty->link;
107 if (!to || tty->stopped)
110 c = to->receive_room;
113 to->ldisc->ops->receive_buf(to, buf, NULL, c);
118 static int pty_write_room(struct tty_struct *tty)
120 struct tty_struct *to = tty->link;
122 if (!to || tty->stopped)
125 return to->receive_room;
129 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
130 * The chars_in_buffer() value is used by the ldisc select() function
131 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
132 * The pty driver chars_in_buffer() Master/Slave must behave differently:
134 * The Master side needs to allow typed-ahead commands to accumulate
135 * while being canonicalized, so we report "our buffer" as empty until
136 * some threshold is reached, and then report the count. (Any count >
137 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
138 * the count returned must be 0 if no canonical data is available to be
139 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
141 * The Slave side passes all characters in raw mode to the Master side's
142 * buffer where they can be read immediately, so in this case we can
143 * return the true count in the buffer.
145 static int pty_chars_in_buffer(struct tty_struct *tty)
147 struct tty_struct *to = tty->link;
150 /* We should get the line discipline lock for "tty->link" */
151 if (!to || !to->ldisc->ops->chars_in_buffer)
154 /* The ldisc must report 0 if no characters available to be read */
155 count = to->ldisc->ops->chars_in_buffer(to);
157 if (tty->driver->subtype == PTY_TYPE_SLAVE)
160 /* Master side driver ... if the other side's read buffer is less than
161 * half full, return 0 to allow writers to proceed; otherwise return
162 * the count. This leaves a comfortable margin to avoid overflow,
163 * and still allows half a buffer's worth of typed-ahead commands.
165 return (count < N_TTY_BUF_SIZE/2) ? 0 : count;
168 /* Set the lock flag on a pty */
169 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
172 if (get_user(val, arg))
175 set_bit(TTY_PTY_LOCK, &tty->flags);
177 clear_bit(TTY_PTY_LOCK, &tty->flags);
181 static void pty_flush_buffer(struct tty_struct *tty)
183 struct tty_struct *to = tty->link;
189 if (to->ldisc->ops->flush_buffer)
190 to->ldisc->ops->flush_buffer(to);
193 spin_lock_irqsave(&tty->ctrl_lock, flags);
194 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
195 wake_up_interruptible(&to->read_wait);
196 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
200 static int pty_open(struct tty_struct *tty, struct file *filp)
202 int retval = -ENODEV;
204 if (!tty || !tty->link)
208 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
210 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
212 if (tty->link->count != 1)
215 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
216 set_bit(TTY_THROTTLED, &tty->flags);
222 static void pty_set_termios(struct tty_struct *tty,
223 struct ktermios *old_termios)
225 tty->termios->c_cflag &= ~(CSIZE | PARENB);
226 tty->termios->c_cflag |= (CS8 | CREAD);
230 * pty_do_resize - resize event
231 * @tty: tty being resized
232 * @ws: window size being set.
234 * Update the termios variables and send the neccessary signals to
235 * peform a terminal resize correctly
238 int pty_resize(struct tty_struct *tty, struct winsize *ws)
240 struct pid *pgrp, *rpgrp;
242 struct tty_struct *pty = tty->link;
244 /* For a PTY we need to lock the tty side */
245 mutex_lock(&tty->termios_mutex);
246 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
249 /* Get the PID values and reference them so we can
250 avoid holding the tty ctrl lock while sending signals.
251 We need to lock these individually however. */
253 spin_lock_irqsave(&tty->ctrl_lock, flags);
254 pgrp = get_pid(tty->pgrp);
255 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
257 spin_lock_irqsave(&pty->ctrl_lock, flags);
258 rpgrp = get_pid(pty->pgrp);
259 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
262 kill_pgrp(pgrp, SIGWINCH, 1);
263 if (rpgrp != pgrp && rpgrp)
264 kill_pgrp(rpgrp, SIGWINCH, 1);
270 pty->winsize = *ws; /* Never used so will go away soon */
272 mutex_unlock(&tty->termios_mutex);
276 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
278 struct tty_struct *o_tty;
279 int idx = tty->index;
282 o_tty = alloc_tty_struct();
285 if (!try_module_get(driver->other->owner)) {
286 /* This cannot in fact currently happen */
287 free_tty_struct(o_tty);
290 initialize_tty_struct(o_tty, driver->other, idx);
292 /* We always use new tty termios data so we can do this
294 retval = tty_init_termios(tty);
298 retval = tty_init_termios(o_tty);
300 tty_free_termios(tty);
305 * Everything allocated ... set up the o_tty structure.
307 driver->other->ttys[idx] = o_tty;
308 tty_driver_kref_get(driver->other);
309 if (driver->subtype == PTY_TYPE_MASTER)
311 /* Establish the links in both directions */
315 tty_driver_kref_get(driver);
317 driver->ttys[idx] = tty;
320 module_put(o_tty->driver->owner);
321 free_tty_struct(o_tty);
326 static const struct tty_operations pty_ops = {
327 .install = pty_install,
331 .write_room = pty_write_room,
332 .flush_buffer = pty_flush_buffer,
333 .chars_in_buffer = pty_chars_in_buffer,
334 .unthrottle = pty_unthrottle,
335 .set_termios = pty_set_termios,
339 /* Traditional BSD devices */
340 #ifdef CONFIG_LEGACY_PTYS
341 static struct tty_driver *pty_driver, *pty_slave_driver;
343 static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
344 unsigned int cmd, unsigned long arg)
347 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
348 return pty_set_lock(tty, (int __user *) arg);
353 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
354 module_param(legacy_count, int, 0);
356 static const struct tty_operations pty_ops_bsd = {
360 .write_room = pty_write_room,
361 .flush_buffer = pty_flush_buffer,
362 .chars_in_buffer = pty_chars_in_buffer,
363 .unthrottle = pty_unthrottle,
364 .set_termios = pty_set_termios,
365 .ioctl = pty_bsd_ioctl,
369 static void __init legacy_pty_init(void)
371 if (legacy_count <= 0)
374 pty_driver = alloc_tty_driver(legacy_count);
376 panic("Couldn't allocate pty driver");
378 pty_slave_driver = alloc_tty_driver(legacy_count);
379 if (!pty_slave_driver)
380 panic("Couldn't allocate pty slave driver");
382 pty_driver->owner = THIS_MODULE;
383 pty_driver->driver_name = "pty_master";
384 pty_driver->name = "pty";
385 pty_driver->major = PTY_MASTER_MAJOR;
386 pty_driver->minor_start = 0;
387 pty_driver->type = TTY_DRIVER_TYPE_PTY;
388 pty_driver->subtype = PTY_TYPE_MASTER;
389 pty_driver->init_termios = tty_std_termios;
390 pty_driver->init_termios.c_iflag = 0;
391 pty_driver->init_termios.c_oflag = 0;
392 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
393 pty_driver->init_termios.c_lflag = 0;
394 pty_driver->init_termios.c_ispeed = 38400;
395 pty_driver->init_termios.c_ospeed = 38400;
396 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
397 pty_driver->other = pty_slave_driver;
398 tty_set_operations(pty_driver, &pty_ops);
400 pty_slave_driver->owner = THIS_MODULE;
401 pty_slave_driver->driver_name = "pty_slave";
402 pty_slave_driver->name = "ttyp";
403 pty_slave_driver->major = PTY_SLAVE_MAJOR;
404 pty_slave_driver->minor_start = 0;
405 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
406 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
407 pty_slave_driver->init_termios = tty_std_termios;
408 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
409 pty_slave_driver->init_termios.c_ispeed = 38400;
410 pty_slave_driver->init_termios.c_ospeed = 38400;
411 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
413 pty_slave_driver->other = pty_driver;
414 tty_set_operations(pty_slave_driver, &pty_ops);
416 if (tty_register_driver(pty_driver))
417 panic("Couldn't register pty driver");
418 if (tty_register_driver(pty_slave_driver))
419 panic("Couldn't register pty slave driver");
422 static inline void legacy_pty_init(void) { }
426 #ifdef CONFIG_UNIX98_PTYS
428 * sysctl support for setting limits on the number of Unix98 ptys allocated.
429 * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
431 int pty_limit = NR_UNIX98_PTY_DEFAULT;
432 static int pty_limit_min;
433 static int pty_limit_max = NR_UNIX98_PTY_MAX;
434 static int pty_count;
436 static struct cdev ptmx_cdev;
438 static struct ctl_table pty_table[] = {
442 .maxlen = sizeof(int),
445 .proc_handler = &proc_dointvec_minmax,
446 .strategy = &sysctl_intvec,
447 .extra1 = &pty_limit_min,
448 .extra2 = &pty_limit_max,
452 .maxlen = sizeof(int),
455 .proc_handler = &proc_dointvec,
461 static struct ctl_table pty_kern_table[] = {
463 .ctl_name = KERN_PTY,
471 static struct ctl_table pty_root_table[] = {
473 .ctl_name = CTL_KERN,
474 .procname = "kernel",
476 .child = pty_kern_table,
482 static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
483 unsigned int cmd, unsigned long arg)
486 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
487 return pty_set_lock(tty, (int __user *)arg);
488 case TIOCGPTN: /* Get PT Number */
489 return put_user(tty->index, (unsigned int __user *)arg);
496 * ptm_unix98_lookup - find a pty master
497 * @driver: ptm driver
500 * Look up a pty master device. Called under the tty_mutex for now.
501 * This provides our locking.
504 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
505 struct inode *ptm_inode, int idx)
507 struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
514 * pts_unix98_lookup - find a pty slave
515 * @driver: pts driver
518 * Look up a pty master device. Called under the tty_mutex for now.
519 * This provides our locking.
522 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
523 struct inode *pts_inode, int idx)
525 struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
526 /* Master must be open before slave */
528 return ERR_PTR(-EIO);
532 static void pty_unix98_shutdown(struct tty_struct *tty)
534 /* We have our own method as we don't use the tty index */
538 /* We have no need to install and remove our tty objects as devpts does all
541 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
543 struct tty_struct *o_tty;
544 int idx = tty->index;
546 o_tty = alloc_tty_struct();
549 if (!try_module_get(driver->other->owner)) {
550 /* This cannot in fact currently happen */
551 free_tty_struct(o_tty);
554 initialize_tty_struct(o_tty, driver->other, idx);
556 tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
557 if (tty->termios == NULL)
559 *tty->termios = driver->init_termios;
560 tty->termios_locked = tty->termios + 1;
562 o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
563 if (o_tty->termios == NULL)
565 *o_tty->termios = driver->other->init_termios;
566 o_tty->termios_locked = o_tty->termios + 1;
568 tty_driver_kref_get(driver->other);
569 if (driver->subtype == PTY_TYPE_MASTER)
571 /* Establish the links in both directions */
575 * All structures have been allocated, so now we install them.
576 * Failures after this point use release_tty to clean up, so
577 * there's no need to null out the local pointers.
579 tty_driver_kref_get(driver);
584 kfree(o_tty->termios);
585 module_put(o_tty->driver->owner);
586 free_tty_struct(o_tty);
591 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
596 static const struct tty_operations ptm_unix98_ops = {
597 .lookup = ptm_unix98_lookup,
598 .install = pty_unix98_install,
599 .remove = pty_unix98_remove,
603 .write_room = pty_write_room,
604 .flush_buffer = pty_flush_buffer,
605 .chars_in_buffer = pty_chars_in_buffer,
606 .unthrottle = pty_unthrottle,
607 .set_termios = pty_set_termios,
608 .ioctl = pty_unix98_ioctl,
609 .shutdown = pty_unix98_shutdown,
613 static const struct tty_operations pty_unix98_ops = {
614 .lookup = pts_unix98_lookup,
615 .install = pty_unix98_install,
616 .remove = pty_unix98_remove,
620 .write_room = pty_write_room,
621 .flush_buffer = pty_flush_buffer,
622 .chars_in_buffer = pty_chars_in_buffer,
623 .unthrottle = pty_unthrottle,
624 .set_termios = pty_set_termios,
625 .shutdown = pty_unix98_shutdown
629 * ptmx_open - open a unix 98 pty master
630 * @inode: inode of device file
631 * @filp: file pointer to tty
633 * Allocate a unix98 pty master device from the ptmx driver.
635 * Locking: tty_mutex protects the init_dev work. tty->count should
637 * allocated_ptys_lock handles the list of free pty numbers
640 static int __ptmx_open(struct inode *inode, struct file *filp)
642 struct tty_struct *tty;
646 nonseekable_open(inode, filp);
648 /* find a device that is not in use. */
649 index = devpts_new_index(inode);
653 mutex_lock(&tty_mutex);
654 tty = tty_init_dev(ptm_driver, index, 1);
655 mutex_unlock(&tty_mutex);
658 retval = PTR_ERR(tty);
662 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
663 filp->private_data = tty;
664 file_move(filp, &tty->tty_files);
666 retval = devpts_pty_new(inode, tty->link);
670 retval = ptm_driver->ops->open(tty, filp);
674 tty_release_dev(filp);
677 devpts_kill_index(inode, index);
681 static int ptmx_open(struct inode *inode, struct file *filp)
686 ret = __ptmx_open(inode, filp);
691 static struct file_operations ptmx_fops;
693 static void __init unix98_pty_init(void)
695 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
697 panic("Couldn't allocate Unix98 ptm driver");
698 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
700 panic("Couldn't allocate Unix98 pts driver");
702 ptm_driver->owner = THIS_MODULE;
703 ptm_driver->driver_name = "pty_master";
704 ptm_driver->name = "ptm";
705 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
706 ptm_driver->minor_start = 0;
707 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
708 ptm_driver->subtype = PTY_TYPE_MASTER;
709 ptm_driver->init_termios = tty_std_termios;
710 ptm_driver->init_termios.c_iflag = 0;
711 ptm_driver->init_termios.c_oflag = 0;
712 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
713 ptm_driver->init_termios.c_lflag = 0;
714 ptm_driver->init_termios.c_ispeed = 38400;
715 ptm_driver->init_termios.c_ospeed = 38400;
716 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
717 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
718 ptm_driver->other = pts_driver;
719 tty_set_operations(ptm_driver, &ptm_unix98_ops);
721 pts_driver->owner = THIS_MODULE;
722 pts_driver->driver_name = "pty_slave";
723 pts_driver->name = "pts";
724 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
725 pts_driver->minor_start = 0;
726 pts_driver->type = TTY_DRIVER_TYPE_PTY;
727 pts_driver->subtype = PTY_TYPE_SLAVE;
728 pts_driver->init_termios = tty_std_termios;
729 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
730 pts_driver->init_termios.c_ispeed = 38400;
731 pts_driver->init_termios.c_ospeed = 38400;
732 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
733 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
734 pts_driver->other = ptm_driver;
735 tty_set_operations(pts_driver, &pty_unix98_ops);
737 if (tty_register_driver(ptm_driver))
738 panic("Couldn't register Unix98 ptm driver");
739 if (tty_register_driver(pts_driver))
740 panic("Couldn't register Unix98 pts driver");
742 register_sysctl_table(pty_root_table);
744 /* Now create the /dev/ptmx special device */
745 tty_default_fops(&ptmx_fops);
746 ptmx_fops.open = ptmx_open;
748 cdev_init(&ptmx_cdev, &ptmx_fops);
749 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
750 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
751 panic("Couldn't register /dev/ptmx driver\n");
752 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
756 static inline void unix98_pty_init(void) { }
759 static int __init pty_init(void)
765 module_init(pty_init);