2 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Added support for a Unix98-style ptmx device.
5 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
7 * When reading this code see also fs/devpts. In particular note that the
8 * driver_data field is used by the devpts side as a binding to the devpts
12 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/fcntl.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/major.h>
23 #include <linux/init.h>
24 #include <linux/sysctl.h>
25 #include <linux/device.h>
26 #include <linux/uaccess.h>
27 #include <linux/bitops.h>
28 #include <linux/devpts_fs.h>
29 #include <linux/slab.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 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
53 wake_up_interruptible(&tty->link->read_wait);
54 wake_up_interruptible(&tty->link->write_wait);
55 if (tty->driver->subtype == PTY_TYPE_MASTER) {
56 set_bit(TTY_OTHER_CLOSED, &tty->flags);
57 #ifdef CONFIG_UNIX98_PTYS
58 if (tty->driver == ptm_driver)
59 devpts_pty_kill(tty->link);
62 tty_vhangup(tty->link);
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
71 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
72 * 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.
77 static void pty_unthrottle(struct tty_struct *tty)
79 tty_wakeup(tty->link);
80 set_bit(TTY_THROTTLED, &tty->flags);
84 * pty_space - report space left for writing
85 * @to: tty we are writing into
87 * The tty buffers allow 64K but we sneak a peak and clip at 8K this
88 * allows a lot of overspill room for echo and other fun messes to
92 static int pty_space(struct tty_struct *to)
94 int n = 8192 - to->buf.memory_used;
101 * pty_write - write to a pty
102 * @tty: the tty we write from
103 * @buf: kernel buffer of data
104 * @count: bytes to write
106 * Our "hardware" write method. Data is coming from the ldisc which
107 * may be in a non sleeping state. We simply throw this at the other
108 * end of the link as if we were an IRQ handler receiving stuff for
109 * the other side of the pty/tty pair.
112 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
114 struct tty_struct *to = tty->link;
120 /* Stuff the data into the input queue of the other end */
121 c = tty_insert_flip_string(to, buf, c);
124 tty_flip_buffer_push(to);
132 * pty_write_room - write space
133 * @tty: tty we are writing from
135 * Report how many bytes the ldisc can send into the queue for
139 static int pty_write_room(struct tty_struct *tty)
143 return pty_space(tty->link);
147 * pty_chars_in_buffer - characters currently in our tx queue
150 * Report how much we have in the transmit queue. As everything is
151 * instantly at the other end this is easy to implement.
154 static int pty_chars_in_buffer(struct tty_struct *tty)
159 /* Set the lock flag on a pty */
160 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
163 if (get_user(val, arg))
166 set_bit(TTY_PTY_LOCK, &tty->flags);
168 clear_bit(TTY_PTY_LOCK, &tty->flags);
172 /* Send a signal to the slave */
173 static int pty_signal(struct tty_struct *tty, int sig)
178 if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
182 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
183 pgrp = get_pid(tty->link->pgrp);
184 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
186 kill_pgrp(pgrp, sig, 1);
192 static void pty_flush_buffer(struct tty_struct *tty)
194 struct tty_struct *to = tty->link;
199 /* tty_buffer_flush(to); FIXME */
201 spin_lock_irqsave(&tty->ctrl_lock, flags);
202 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
203 wake_up_interruptible(&to->read_wait);
204 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
208 static int pty_open(struct tty_struct *tty, struct file *filp)
210 int retval = -ENODEV;
212 if (!tty || !tty->link)
216 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
218 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
220 if (tty->link->count != 1)
223 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
224 set_bit(TTY_THROTTLED, &tty->flags);
230 static void pty_set_termios(struct tty_struct *tty,
231 struct ktermios *old_termios)
233 tty->termios->c_cflag &= ~(CSIZE | PARENB);
234 tty->termios->c_cflag |= (CS8 | CREAD);
238 * pty_do_resize - resize event
239 * @tty: tty being resized
240 * @ws: window size being set.
242 * Update the termios variables and send the necessary signals to
243 * peform a terminal resize correctly
246 int pty_resize(struct tty_struct *tty, struct winsize *ws)
248 struct pid *pgrp, *rpgrp;
250 struct tty_struct *pty = tty->link;
252 /* For a PTY we need to lock the tty side */
253 mutex_lock(&tty->termios_mutex);
254 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
257 /* Get the PID values and reference them so we can
258 avoid holding the tty ctrl lock while sending signals.
259 We need to lock these individually however. */
261 spin_lock_irqsave(&tty->ctrl_lock, flags);
262 pgrp = get_pid(tty->pgrp);
263 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
265 spin_lock_irqsave(&pty->ctrl_lock, flags);
266 rpgrp = get_pid(pty->pgrp);
267 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
270 kill_pgrp(pgrp, SIGWINCH, 1);
271 if (rpgrp != pgrp && rpgrp)
272 kill_pgrp(rpgrp, SIGWINCH, 1);
278 pty->winsize = *ws; /* Never used so will go away soon */
280 mutex_unlock(&tty->termios_mutex);
284 /* Traditional BSD devices */
285 #ifdef CONFIG_LEGACY_PTYS
287 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
289 struct tty_struct *o_tty;
290 int idx = tty->index;
293 o_tty = alloc_tty_struct();
296 if (!try_module_get(driver->other->owner)) {
297 /* This cannot in fact currently happen */
301 initialize_tty_struct(o_tty, driver->other, idx);
303 /* We always use new tty termios data so we can do this
305 retval = tty_init_termios(tty);
309 retval = tty_init_termios(o_tty);
311 goto err_free_termios;
314 * Everything allocated ... set up the o_tty structure.
316 driver->other->ttys[idx] = o_tty;
317 tty_driver_kref_get(driver->other);
318 if (driver->subtype == PTY_TYPE_MASTER)
320 /* Establish the links in both directions */
324 tty_driver_kref_get(driver);
326 driver->ttys[idx] = tty;
329 tty_free_termios(tty);
331 deinitialize_tty_struct(o_tty);
332 module_put(o_tty->driver->owner);
334 free_tty_struct(o_tty);
338 static int pty_bsd_ioctl(struct tty_struct *tty,
339 unsigned int cmd, unsigned long arg)
342 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
343 return pty_set_lock(tty, (int __user *) arg);
344 case TIOCSIG: /* Send signal to other side of pty */
345 return pty_signal(tty, (int) arg);
350 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
351 module_param(legacy_count, int, 0);
354 * The master side of a pty can do TIOCSPTLCK and thus
357 static const struct tty_operations master_pty_ops_bsd = {
358 .install = pty_install,
362 .write_room = pty_write_room,
363 .flush_buffer = pty_flush_buffer,
364 .chars_in_buffer = pty_chars_in_buffer,
365 .unthrottle = pty_unthrottle,
366 .set_termios = pty_set_termios,
367 .ioctl = pty_bsd_ioctl,
371 static const struct tty_operations slave_pty_ops_bsd = {
372 .install = pty_install,
376 .write_room = pty_write_room,
377 .flush_buffer = pty_flush_buffer,
378 .chars_in_buffer = pty_chars_in_buffer,
379 .unthrottle = pty_unthrottle,
380 .set_termios = pty_set_termios,
384 static void __init legacy_pty_init(void)
386 struct tty_driver *pty_driver, *pty_slave_driver;
388 if (legacy_count <= 0)
391 pty_driver = alloc_tty_driver(legacy_count);
393 panic("Couldn't allocate pty driver");
395 pty_slave_driver = alloc_tty_driver(legacy_count);
396 if (!pty_slave_driver)
397 panic("Couldn't allocate pty slave driver");
399 pty_driver->owner = THIS_MODULE;
400 pty_driver->driver_name = "pty_master";
401 pty_driver->name = "pty";
402 pty_driver->major = PTY_MASTER_MAJOR;
403 pty_driver->minor_start = 0;
404 pty_driver->type = TTY_DRIVER_TYPE_PTY;
405 pty_driver->subtype = PTY_TYPE_MASTER;
406 pty_driver->init_termios = tty_std_termios;
407 pty_driver->init_termios.c_iflag = 0;
408 pty_driver->init_termios.c_oflag = 0;
409 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
410 pty_driver->init_termios.c_lflag = 0;
411 pty_driver->init_termios.c_ispeed = 38400;
412 pty_driver->init_termios.c_ospeed = 38400;
413 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
414 pty_driver->other = pty_slave_driver;
415 tty_set_operations(pty_driver, &master_pty_ops_bsd);
417 pty_slave_driver->owner = THIS_MODULE;
418 pty_slave_driver->driver_name = "pty_slave";
419 pty_slave_driver->name = "ttyp";
420 pty_slave_driver->major = PTY_SLAVE_MAJOR;
421 pty_slave_driver->minor_start = 0;
422 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
423 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
424 pty_slave_driver->init_termios = tty_std_termios;
425 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
426 pty_slave_driver->init_termios.c_ispeed = 38400;
427 pty_slave_driver->init_termios.c_ospeed = 38400;
428 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
430 pty_slave_driver->other = pty_driver;
431 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
433 if (tty_register_driver(pty_driver))
434 panic("Couldn't register pty driver");
435 if (tty_register_driver(pty_slave_driver))
436 panic("Couldn't register pty slave driver");
439 static inline void legacy_pty_init(void) { }
443 #ifdef CONFIG_UNIX98_PTYS
445 * sysctl support for setting limits on the number of Unix98 ptys allocated.
446 * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
448 int pty_limit = NR_UNIX98_PTY_DEFAULT;
449 static int pty_limit_min;
450 static int pty_limit_max = NR_UNIX98_PTY_MAX;
451 static int tty_count;
452 static int pty_count;
454 static inline void pty_inc_count(void)
456 pty_count = (++tty_count) / 2;
459 static inline void pty_dec_count(void)
461 pty_count = (--tty_count) / 2;
464 static struct cdev ptmx_cdev;
466 static struct ctl_table pty_table[] = {
469 .maxlen = sizeof(int),
472 .proc_handler = proc_dointvec_minmax,
473 .extra1 = &pty_limit_min,
474 .extra2 = &pty_limit_max,
477 .maxlen = sizeof(int),
480 .proc_handler = proc_dointvec,
485 static struct ctl_table pty_kern_table[] = {
494 static struct ctl_table pty_root_table[] = {
496 .procname = "kernel",
498 .child = pty_kern_table,
504 static int pty_unix98_ioctl(struct tty_struct *tty,
505 unsigned int cmd, unsigned long arg)
508 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
509 return pty_set_lock(tty, (int __user *)arg);
510 case TIOCGPTN: /* Get PT Number */
511 return put_user(tty->index, (unsigned int __user *)arg);
512 case TIOCSIG: /* Send signal to other side of pty */
513 return pty_signal(tty, (int) arg);
520 * ptm_unix98_lookup - find a pty master
521 * @driver: ptm driver
524 * Look up a pty master device. Called under the tty_mutex for now.
525 * This provides our locking.
528 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
529 struct inode *ptm_inode, int idx)
531 struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
538 * pts_unix98_lookup - find a pty slave
539 * @driver: pts driver
542 * Look up a pty master device. Called under the tty_mutex for now.
543 * This provides our locking.
546 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
547 struct inode *pts_inode, int idx)
549 struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
550 /* Master must be open before slave */
552 return ERR_PTR(-EIO);
556 static void pty_unix98_shutdown(struct tty_struct *tty)
558 tty_driver_remove_tty(tty->driver, tty);
559 /* We have our own method as we don't use the tty index */
563 /* We have no need to install and remove our tty objects as devpts does all
566 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
568 struct tty_struct *o_tty;
569 int idx = tty->index;
571 o_tty = alloc_tty_struct();
574 if (!try_module_get(driver->other->owner)) {
575 /* This cannot in fact currently happen */
578 initialize_tty_struct(o_tty, driver->other, idx);
580 tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
581 if (tty->termios == NULL)
583 *tty->termios = driver->init_termios;
584 tty->termios_locked = tty->termios + 1;
586 o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
587 if (o_tty->termios == NULL)
589 *o_tty->termios = driver->other->init_termios;
590 o_tty->termios_locked = o_tty->termios + 1;
592 tty_driver_kref_get(driver->other);
593 if (driver->subtype == PTY_TYPE_MASTER)
595 /* Establish the links in both directions */
599 * All structures have been allocated, so now we install them.
600 * Failures after this point use release_tty to clean up, so
601 * there's no need to null out the local pointers.
603 tty_driver_kref_get(driver);
605 pty_inc_count(); /* tty */
606 pty_inc_count(); /* tty->link */
609 deinitialize_tty_struct(o_tty);
610 kfree(o_tty->termios);
612 module_put(o_tty->driver->owner);
614 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 /* We refuse fsnotify events on ptmx, since it's a shared resource */
676 filp->f_mode |= FMODE_NONOTIFY;
678 retval = tty_alloc_file(filp);
682 /* find a device that is not in use. */
684 index = devpts_new_index(inode);
691 mutex_lock(&tty_mutex);
693 tty = tty_init_dev(ptm_driver, index, 1);
694 mutex_unlock(&tty_mutex);
697 retval = PTR_ERR(tty);
701 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
703 tty_add_file(tty, filp);
705 retval = devpts_pty_new(inode, tty->link);
709 retval = ptm_driver->ops->open(tty, filp);
717 tty_release(inode, filp);
720 devpts_kill_index(inode, index);
727 static struct file_operations ptmx_fops;
729 static void __init unix98_pty_init(void)
731 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
733 panic("Couldn't allocate Unix98 ptm driver");
734 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
736 panic("Couldn't allocate Unix98 pts driver");
738 ptm_driver->owner = THIS_MODULE;
739 ptm_driver->driver_name = "pty_master";
740 ptm_driver->name = "ptm";
741 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
742 ptm_driver->minor_start = 0;
743 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
744 ptm_driver->subtype = PTY_TYPE_MASTER;
745 ptm_driver->init_termios = tty_std_termios;
746 ptm_driver->init_termios.c_iflag = 0;
747 ptm_driver->init_termios.c_oflag = 0;
748 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
749 ptm_driver->init_termios.c_lflag = 0;
750 ptm_driver->init_termios.c_ispeed = 38400;
751 ptm_driver->init_termios.c_ospeed = 38400;
752 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
753 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
754 ptm_driver->other = pts_driver;
755 tty_set_operations(ptm_driver, &ptm_unix98_ops);
757 pts_driver->owner = THIS_MODULE;
758 pts_driver->driver_name = "pty_slave";
759 pts_driver->name = "pts";
760 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
761 pts_driver->minor_start = 0;
762 pts_driver->type = TTY_DRIVER_TYPE_PTY;
763 pts_driver->subtype = PTY_TYPE_SLAVE;
764 pts_driver->init_termios = tty_std_termios;
765 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
766 pts_driver->init_termios.c_ispeed = 38400;
767 pts_driver->init_termios.c_ospeed = 38400;
768 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
769 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
770 pts_driver->other = ptm_driver;
771 tty_set_operations(pts_driver, &pty_unix98_ops);
773 if (tty_register_driver(ptm_driver))
774 panic("Couldn't register Unix98 ptm driver");
775 if (tty_register_driver(pts_driver))
776 panic("Couldn't register Unix98 pts driver");
778 register_sysctl_table(pty_root_table);
780 /* Now create the /dev/ptmx special device */
781 tty_default_fops(&ptmx_fops);
782 ptmx_fops.open = ptmx_open;
784 cdev_init(&ptmx_cdev, &ptmx_fops);
785 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
786 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
787 panic("Couldn't register /dev/ptmx driver\n");
788 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
792 static inline void unix98_pty_init(void) { }
795 static int __init pty_init(void)
801 module_init(pty_init);