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 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.
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
108 static int pty_write(struct tty_struct *tty, const unsigned char *buf,
111 struct tty_struct *to = tty->link;
112 struct tty_ldisc *ld;
115 if (!to || tty->stopped)
117 ld = tty_ldisc_ref(to);
120 c = to->receive_room;
123 ld->ops->receive_buf(to, buf, NULL, c);
129 static int pty_write_room(struct tty_struct *tty)
131 struct tty_struct *to = tty->link;
133 if (!to || tty->stopped)
136 return to->receive_room;
140 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
141 * The chars_in_buffer() value is used by the ldisc select() function
142 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
143 * The pty driver chars_in_buffer() Master/Slave must behave differently:
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 >
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
150 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
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.
156 static int pty_chars_in_buffer(struct tty_struct *tty)
158 struct tty_struct *to = tty->link;
159 struct tty_ldisc *ld;
162 /* We should get the line discipline lock for "tty->link" */
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);
171 /* The ldisc must report 0 if no characters available to be read */
172 if (ld->ops->chars_in_buffer)
173 count = ld->ops->chars_in_buffer(to);
177 if (tty->driver->subtype == PTY_TYPE_SLAVE)
180 /* Master side driver ... if the other side's read buffer is less than
181 * half full, return 0 to allow writers to proceed; otherwise return
182 * the count. This leaves a comfortable margin to avoid overflow,
183 * and still allows half a buffer's worth of typed-ahead commands.
185 return (count < N_TTY_BUF_SIZE/2) ? 0 : count;
188 /* Set the lock flag on a pty */
189 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
192 if (get_user(val, arg))
195 set_bit(TTY_PTY_LOCK, &tty->flags);
197 clear_bit(TTY_PTY_LOCK, &tty->flags);
201 static void pty_flush_buffer(struct tty_struct *tty)
203 struct tty_struct *to = tty->link;
205 struct tty_ldisc *ld;
209 ld = tty_ldisc_ref(to);
211 /* The other end is changing discipline */
215 if (ld->ops->flush_buffer)
216 to->ldisc->ops->flush_buffer(to);
220 spin_lock_irqsave(&tty->ctrl_lock, flags);
221 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
222 wake_up_interruptible(&to->read_wait);
223 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
227 static int pty_open(struct tty_struct *tty, struct file *filp)
229 int retval = -ENODEV;
231 if (!tty || !tty->link)
235 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
237 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
239 if (tty->link->count != 1)
242 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
243 set_bit(TTY_THROTTLED, &tty->flags);
249 static void pty_set_termios(struct tty_struct *tty,
250 struct ktermios *old_termios)
252 tty->termios->c_cflag &= ~(CSIZE | PARENB);
253 tty->termios->c_cflag |= (CS8 | CREAD);
257 * pty_do_resize - resize event
258 * @tty: tty being resized
259 * @ws: window size being set.
261 * Update the termios variables and send the neccessary signals to
262 * peform a terminal resize correctly
265 int pty_resize(struct tty_struct *tty, struct winsize *ws)
267 struct pid *pgrp, *rpgrp;
269 struct tty_struct *pty = tty->link;
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)))
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. */
280 spin_lock_irqsave(&tty->ctrl_lock, flags);
281 pgrp = get_pid(tty->pgrp);
282 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
284 spin_lock_irqsave(&pty->ctrl_lock, flags);
285 rpgrp = get_pid(pty->pgrp);
286 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
289 kill_pgrp(pgrp, SIGWINCH, 1);
290 if (rpgrp != pgrp && rpgrp)
291 kill_pgrp(rpgrp, SIGWINCH, 1);
297 pty->winsize = *ws; /* Never used so will go away soon */
299 mutex_unlock(&tty->termios_mutex);
303 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
305 struct tty_struct *o_tty;
306 int idx = tty->index;
309 o_tty = alloc_tty_struct();
312 if (!try_module_get(driver->other->owner)) {
313 /* This cannot in fact currently happen */
314 free_tty_struct(o_tty);
317 initialize_tty_struct(o_tty, driver->other, idx);
319 /* We always use new tty termios data so we can do this
321 retval = tty_init_termios(tty);
325 retval = tty_init_termios(o_tty);
327 tty_free_termios(tty);
332 * Everything allocated ... set up the o_tty structure.
334 driver->other->ttys[idx] = o_tty;
335 tty_driver_kref_get(driver->other);
336 if (driver->subtype == PTY_TYPE_MASTER)
338 /* Establish the links in both directions */
342 tty_driver_kref_get(driver);
344 driver->ttys[idx] = tty;
347 module_put(o_tty->driver->owner);
348 free_tty_struct(o_tty);
353 static const struct tty_operations pty_ops = {
354 .install = pty_install,
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,
366 /* Traditional BSD devices */
367 #ifdef CONFIG_LEGACY_PTYS
368 static struct tty_driver *pty_driver, *pty_slave_driver;
370 static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
371 unsigned int cmd, unsigned long arg)
374 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
375 return pty_set_lock(tty, (int __user *) arg);
380 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
381 module_param(legacy_count, int, 0);
383 static const struct tty_operations pty_ops_bsd = {
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,
396 static void __init legacy_pty_init(void)
398 if (legacy_count <= 0)
401 pty_driver = alloc_tty_driver(legacy_count);
403 panic("Couldn't allocate pty driver");
405 pty_slave_driver = alloc_tty_driver(legacy_count);
406 if (!pty_slave_driver)
407 panic("Couldn't allocate pty slave driver");
409 pty_driver->owner = THIS_MODULE;
410 pty_driver->driver_name = "pty_master";
411 pty_driver->name = "pty";
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;
421 pty_driver->init_termios.c_ispeed = 38400;
422 pty_driver->init_termios.c_ospeed = 38400;
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);
427 pty_slave_driver->owner = THIS_MODULE;
428 pty_slave_driver->driver_name = "pty_slave";
429 pty_slave_driver->name = "ttyp";
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;
436 pty_slave_driver->init_termios.c_ispeed = 38400;
437 pty_slave_driver->init_termios.c_ospeed = 38400;
438 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
440 pty_slave_driver->other = pty_driver;
441 tty_set_operations(pty_slave_driver, &pty_ops);
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");
449 static inline void legacy_pty_init(void) { }
453 #ifdef CONFIG_UNIX98_PTYS
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.
458 int pty_limit = NR_UNIX98_PTY_DEFAULT;
459 static int pty_limit_min;
460 static int pty_limit_max = NR_UNIX98_PTY_MAX;
461 static int pty_count;
463 static struct cdev ptmx_cdev;
465 static struct ctl_table pty_table[] = {
469 .maxlen = sizeof(int),
472 .proc_handler = &proc_dointvec_minmax,
473 .strategy = &sysctl_intvec,
474 .extra1 = &pty_limit_min,
475 .extra2 = &pty_limit_max,
479 .maxlen = sizeof(int),
482 .proc_handler = &proc_dointvec,
488 static struct ctl_table pty_kern_table[] = {
490 .ctl_name = KERN_PTY,
498 static struct ctl_table pty_root_table[] = {
500 .ctl_name = CTL_KERN,
501 .procname = "kernel",
503 .child = pty_kern_table,
509 static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
510 unsigned int cmd, unsigned long arg)
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);
523 * ptm_unix98_lookup - find a pty master
524 * @driver: ptm driver
527 * Look up a pty master device. Called under the tty_mutex for now.
528 * This provides our locking.
531 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
532 struct inode *ptm_inode, int idx)
534 struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
541 * pts_unix98_lookup - find a pty slave
542 * @driver: pts driver
545 * Look up a pty master device. Called under the tty_mutex for now.
546 * This provides our locking.
549 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
550 struct inode *pts_inode, int idx)
552 struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
553 /* Master must be open before slave */
555 return ERR_PTR(-EIO);
559 static void pty_unix98_shutdown(struct tty_struct *tty)
561 /* We have our own method as we don't use the tty index */
565 /* We have no need to install and remove our tty objects as devpts does all
568 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
570 struct tty_struct *o_tty;
571 int idx = tty->index;
573 o_tty = alloc_tty_struct();
576 if (!try_module_get(driver->other->owner)) {
577 /* This cannot in fact currently happen */
578 free_tty_struct(o_tty);
581 initialize_tty_struct(o_tty, driver->other, idx);
583 tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
584 if (tty->termios == NULL)
586 *tty->termios = driver->init_termios;
587 tty->termios_locked = tty->termios + 1;
589 o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
590 if (o_tty->termios == NULL)
592 *o_tty->termios = driver->other->init_termios;
593 o_tty->termios_locked = o_tty->termios + 1;
595 tty_driver_kref_get(driver->other);
596 if (driver->subtype == PTY_TYPE_MASTER)
598 /* Establish the links in both directions */
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.
606 tty_driver_kref_get(driver);
611 kfree(o_tty->termios);
612 module_put(o_tty->driver->owner);
613 free_tty_struct(o_tty);
618 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
623 static const struct tty_operations ptm_unix98_ops = {
624 .lookup = ptm_unix98_lookup,
625 .install = pty_unix98_install,
626 .remove = pty_unix98_remove,
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,
635 .ioctl = pty_unix98_ioctl,
636 .shutdown = pty_unix98_shutdown,
640 static const struct tty_operations pty_unix98_ops = {
641 .lookup = pts_unix98_lookup,
642 .install = pty_unix98_install,
643 .remove = pty_unix98_remove,
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,
652 .shutdown = pty_unix98_shutdown
656 * ptmx_open - open a unix 98 pty master
657 * @inode: inode of device file
658 * @filp: file pointer to tty
660 * Allocate a unix98 pty master device from the ptmx driver.
662 * Locking: tty_mutex protects the init_dev work. tty->count should
664 * allocated_ptys_lock handles the list of free pty numbers
667 static int __ptmx_open(struct inode *inode, struct file *filp)
669 struct tty_struct *tty;
673 nonseekable_open(inode, filp);
675 /* find a device that is not in use. */
676 index = devpts_new_index(inode);
680 mutex_lock(&tty_mutex);
681 tty = tty_init_dev(ptm_driver, index, 1);
682 mutex_unlock(&tty_mutex);
685 retval = PTR_ERR(tty);
689 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
690 filp->private_data = tty;
691 file_move(filp, &tty->tty_files);
693 retval = devpts_pty_new(inode, tty->link);
697 retval = ptm_driver->ops->open(tty, filp);
701 tty_release_dev(filp);
704 devpts_kill_index(inode, index);
708 static int ptmx_open(struct inode *inode, struct file *filp)
713 ret = __ptmx_open(inode, filp);
718 static struct file_operations ptmx_fops;
720 static void __init unix98_pty_init(void)
722 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
724 panic("Couldn't allocate Unix98 ptm driver");
725 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
727 panic("Couldn't allocate Unix98 pts driver");
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;
741 ptm_driver->init_termios.c_ispeed = 38400;
742 ptm_driver->init_termios.c_ospeed = 38400;
743 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
744 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
745 ptm_driver->other = pts_driver;
746 tty_set_operations(ptm_driver, &ptm_unix98_ops);
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;
757 pts_driver->init_termios.c_ispeed = 38400;
758 pts_driver->init_termios.c_ospeed = 38400;
759 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
760 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
761 pts_driver->other = ptm_driver;
762 tty_set_operations(pts_driver, &pty_unix98_ops);
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");
769 register_sysctl_table(pty_root_table);
771 /* Now create the /dev/ptmx special device */
772 tty_default_fops(&ptmx_fops);
773 ptmx_fops.open = ptmx_open;
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");
783 static inline void unix98_pty_init(void) { }
786 static int __init pty_init(void)
792 module_init(pty_init);