Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  Copyright (C) 1991, 1992  Linus Torvalds
0004  *
0005  *  Added support for a Unix98-style ptmx device.
0006  *    -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
0007  *
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/errno.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/tty.h>
0014 #include <linux/tty_flip.h>
0015 #include <linux/fcntl.h>
0016 #include <linux/sched/signal.h>
0017 #include <linux/string.h>
0018 #include <linux/major.h>
0019 #include <linux/mm.h>
0020 #include <linux/init.h>
0021 #include <linux/device.h>
0022 #include <linux/uaccess.h>
0023 #include <linux/bitops.h>
0024 #include <linux/devpts_fs.h>
0025 #include <linux/slab.h>
0026 #include <linux/mutex.h>
0027 #include <linux/poll.h>
0028 #include <linux/mount.h>
0029 #include <linux/file.h>
0030 #include <linux/ioctl.h>
0031 #include <linux/compat.h>
0032 #include "tty.h"
0033 
0034 #undef TTY_DEBUG_HANGUP
0035 #ifdef TTY_DEBUG_HANGUP
0036 # define tty_debug_hangup(tty, f, args...)  tty_debug(tty, f, ##args)
0037 #else
0038 # define tty_debug_hangup(tty, f, args...)  do {} while (0)
0039 #endif
0040 
0041 #ifdef CONFIG_UNIX98_PTYS
0042 static struct tty_driver *ptm_driver;
0043 static struct tty_driver *pts_driver;
0044 static DEFINE_MUTEX(devpts_mutex);
0045 #endif
0046 
0047 static void pty_close(struct tty_struct *tty, struct file *filp)
0048 {
0049     if (tty->driver->subtype == PTY_TYPE_MASTER)
0050         WARN_ON(tty->count > 1);
0051     else {
0052         if (tty_io_error(tty))
0053             return;
0054         if (tty->count > 2)
0055             return;
0056     }
0057     set_bit(TTY_IO_ERROR, &tty->flags);
0058     wake_up_interruptible(&tty->read_wait);
0059     wake_up_interruptible(&tty->write_wait);
0060     spin_lock_irq(&tty->ctrl.lock);
0061     tty->ctrl.packet = false;
0062     spin_unlock_irq(&tty->ctrl.lock);
0063     /* Review - krefs on tty_link ?? */
0064     if (!tty->link)
0065         return;
0066     set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
0067     wake_up_interruptible(&tty->link->read_wait);
0068     wake_up_interruptible(&tty->link->write_wait);
0069     if (tty->driver->subtype == PTY_TYPE_MASTER) {
0070         set_bit(TTY_OTHER_CLOSED, &tty->flags);
0071 #ifdef CONFIG_UNIX98_PTYS
0072         if (tty->driver == ptm_driver) {
0073             mutex_lock(&devpts_mutex);
0074             if (tty->link->driver_data)
0075                 devpts_pty_kill(tty->link->driver_data);
0076             mutex_unlock(&devpts_mutex);
0077         }
0078 #endif
0079         tty_vhangup(tty->link);
0080     }
0081 }
0082 
0083 /*
0084  * The unthrottle routine is called by the line discipline to signal
0085  * that it can receive more characters.  For PTY's, the TTY_THROTTLED
0086  * flag is always set, to force the line discipline to always call the
0087  * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
0088  * characters in the queue.  This is necessary since each time this
0089  * happens, we need to wake up any sleeping processes that could be
0090  * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
0091  * for the pty buffer to be drained.
0092  */
0093 static void pty_unthrottle(struct tty_struct *tty)
0094 {
0095     tty_wakeup(tty->link);
0096     set_bit(TTY_THROTTLED, &tty->flags);
0097 }
0098 
0099 /**
0100  *  pty_write       -   write to a pty
0101  *  @tty: the tty we write from
0102  *  @buf: kernel buffer of data
0103  *  @c: bytes to write
0104  *
0105  *  Our "hardware" write method. Data is coming from the ldisc which
0106  *  may be in a non sleeping state. We simply throw this at the other
0107  *  end of the link as if we were an IRQ handler receiving stuff for
0108  *  the other side of the pty/tty pair.
0109  */
0110 
0111 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
0112 {
0113     struct tty_struct *to = tty->link;
0114 
0115     if (tty->flow.stopped || !c)
0116         return 0;
0117 
0118     return tty_insert_flip_string_and_push_buffer(to->port, buf, c);
0119 }
0120 
0121 /**
0122  *  pty_write_room  -   write space
0123  *  @tty: tty we are writing from
0124  *
0125  *  Report how many bytes the ldisc can send into the queue for
0126  *  the other device.
0127  */
0128 
0129 static unsigned int pty_write_room(struct tty_struct *tty)
0130 {
0131     if (tty->flow.stopped)
0132         return 0;
0133     return tty_buffer_space_avail(tty->link->port);
0134 }
0135 
0136 /* Set the lock flag on a pty */
0137 static int pty_set_lock(struct tty_struct *tty, int __user *arg)
0138 {
0139     int val;
0140 
0141     if (get_user(val, arg))
0142         return -EFAULT;
0143     if (val)
0144         set_bit(TTY_PTY_LOCK, &tty->flags);
0145     else
0146         clear_bit(TTY_PTY_LOCK, &tty->flags);
0147     return 0;
0148 }
0149 
0150 static int pty_get_lock(struct tty_struct *tty, int __user *arg)
0151 {
0152     int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
0153 
0154     return put_user(locked, arg);
0155 }
0156 
0157 /* Set the packet mode on a pty */
0158 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
0159 {
0160     int pktmode;
0161 
0162     if (get_user(pktmode, arg))
0163         return -EFAULT;
0164 
0165     spin_lock_irq(&tty->ctrl.lock);
0166     if (pktmode) {
0167         if (!tty->ctrl.packet) {
0168             tty->link->ctrl.pktstatus = 0;
0169             smp_mb();
0170             tty->ctrl.packet = true;
0171         }
0172     } else
0173         tty->ctrl.packet = false;
0174     spin_unlock_irq(&tty->ctrl.lock);
0175 
0176     return 0;
0177 }
0178 
0179 /* Get the packet mode of a pty */
0180 static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
0181 {
0182     int pktmode = tty->ctrl.packet;
0183 
0184     return put_user(pktmode, arg);
0185 }
0186 
0187 /* Send a signal to the slave */
0188 static int pty_signal(struct tty_struct *tty, int sig)
0189 {
0190     struct pid *pgrp;
0191 
0192     if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
0193         return -EINVAL;
0194 
0195     if (tty->link) {
0196         pgrp = tty_get_pgrp(tty->link);
0197         if (pgrp)
0198             kill_pgrp(pgrp, sig, 1);
0199         put_pid(pgrp);
0200     }
0201     return 0;
0202 }
0203 
0204 static void pty_flush_buffer(struct tty_struct *tty)
0205 {
0206     struct tty_struct *to = tty->link;
0207 
0208     if (!to)
0209         return;
0210 
0211     tty_buffer_flush(to, NULL);
0212     if (to->ctrl.packet) {
0213         spin_lock_irq(&tty->ctrl.lock);
0214         tty->ctrl.pktstatus |= TIOCPKT_FLUSHWRITE;
0215         wake_up_interruptible(&to->read_wait);
0216         spin_unlock_irq(&tty->ctrl.lock);
0217     }
0218 }
0219 
0220 static int pty_open(struct tty_struct *tty, struct file *filp)
0221 {
0222     if (!tty || !tty->link)
0223         return -ENODEV;
0224 
0225     if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
0226         goto out;
0227     if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
0228         goto out;
0229     if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
0230         goto out;
0231 
0232     clear_bit(TTY_IO_ERROR, &tty->flags);
0233     clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
0234     set_bit(TTY_THROTTLED, &tty->flags);
0235     return 0;
0236 
0237 out:
0238     set_bit(TTY_IO_ERROR, &tty->flags);
0239     return -EIO;
0240 }
0241 
0242 static void pty_set_termios(struct tty_struct *tty,
0243                     struct ktermios *old_termios)
0244 {
0245     /* See if packet mode change of state. */
0246     if (tty->link && tty->link->ctrl.packet) {
0247         int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
0248         int old_flow = ((old_termios->c_iflag & IXON) &&
0249                 (old_termios->c_cc[VSTOP] == '\023') &&
0250                 (old_termios->c_cc[VSTART] == '\021'));
0251         int new_flow = (I_IXON(tty) &&
0252                 STOP_CHAR(tty) == '\023' &&
0253                 START_CHAR(tty) == '\021');
0254         if ((old_flow != new_flow) || extproc) {
0255             spin_lock_irq(&tty->ctrl.lock);
0256             if (old_flow != new_flow) {
0257                 tty->ctrl.pktstatus &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
0258                 if (new_flow)
0259                     tty->ctrl.pktstatus |= TIOCPKT_DOSTOP;
0260                 else
0261                     tty->ctrl.pktstatus |= TIOCPKT_NOSTOP;
0262             }
0263             if (extproc)
0264                 tty->ctrl.pktstatus |= TIOCPKT_IOCTL;
0265             spin_unlock_irq(&tty->ctrl.lock);
0266             wake_up_interruptible(&tty->link->read_wait);
0267         }
0268     }
0269 
0270     tty->termios.c_cflag &= ~(CSIZE | PARENB);
0271     tty->termios.c_cflag |= (CS8 | CREAD);
0272 }
0273 
0274 /**
0275  *  pty_resize      -   resize event
0276  *  @tty: tty being resized
0277  *  @ws: window size being set.
0278  *
0279  *  Update the termios variables and send the necessary signals to
0280  *  peform a terminal resize correctly
0281  */
0282 
0283 static int pty_resize(struct tty_struct *tty,  struct winsize *ws)
0284 {
0285     struct pid *pgrp, *rpgrp;
0286     struct tty_struct *pty = tty->link;
0287 
0288     /* For a PTY we need to lock the tty side */
0289     mutex_lock(&tty->winsize_mutex);
0290     if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
0291         goto done;
0292 
0293     /* Signal the foreground process group of both ptys */
0294     pgrp = tty_get_pgrp(tty);
0295     rpgrp = tty_get_pgrp(pty);
0296 
0297     if (pgrp)
0298         kill_pgrp(pgrp, SIGWINCH, 1);
0299     if (rpgrp != pgrp && rpgrp)
0300         kill_pgrp(rpgrp, SIGWINCH, 1);
0301 
0302     put_pid(pgrp);
0303     put_pid(rpgrp);
0304 
0305     tty->winsize = *ws;
0306     pty->winsize = *ws; /* Never used so will go away soon */
0307 done:
0308     mutex_unlock(&tty->winsize_mutex);
0309     return 0;
0310 }
0311 
0312 /**
0313  *  pty_start - start() handler
0314  *  pty_stop  - stop() handler
0315  *  @tty: tty being flow-controlled
0316  *
0317  *  Propagates the TIOCPKT status to the master pty.
0318  *
0319  *  NB: only the master pty can be in packet mode so only the slave
0320  *      needs start()/stop() handlers
0321  */
0322 static void pty_start(struct tty_struct *tty)
0323 {
0324     unsigned long flags;
0325 
0326     if (tty->link && tty->link->ctrl.packet) {
0327         spin_lock_irqsave(&tty->ctrl.lock, flags);
0328         tty->ctrl.pktstatus &= ~TIOCPKT_STOP;
0329         tty->ctrl.pktstatus |= TIOCPKT_START;
0330         spin_unlock_irqrestore(&tty->ctrl.lock, flags);
0331         wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
0332     }
0333 }
0334 
0335 static void pty_stop(struct tty_struct *tty)
0336 {
0337     unsigned long flags;
0338 
0339     if (tty->link && tty->link->ctrl.packet) {
0340         spin_lock_irqsave(&tty->ctrl.lock, flags);
0341         tty->ctrl.pktstatus &= ~TIOCPKT_START;
0342         tty->ctrl.pktstatus |= TIOCPKT_STOP;
0343         spin_unlock_irqrestore(&tty->ctrl.lock, flags);
0344         wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
0345     }
0346 }
0347 
0348 /**
0349  *  pty_common_install      -   set up the pty pair
0350  *  @driver: the pty driver
0351  *  @tty: the tty being instantiated
0352  *  @legacy: true if this is BSD style
0353  *
0354  *  Perform the initial set up for the tty/pty pair. Called from the
0355  *  tty layer when the port is first opened.
0356  *
0357  *  Locking: the caller must hold the tty_mutex
0358  */
0359 static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
0360         bool legacy)
0361 {
0362     struct tty_struct *o_tty;
0363     struct tty_port *ports[2];
0364     int idx = tty->index;
0365     int retval = -ENOMEM;
0366 
0367     /* Opening the slave first has always returned -EIO */
0368     if (driver->subtype != PTY_TYPE_MASTER)
0369         return -EIO;
0370 
0371     ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
0372     ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
0373     if (!ports[0] || !ports[1])
0374         goto err;
0375     if (!try_module_get(driver->other->owner)) {
0376         /* This cannot in fact currently happen */
0377         goto err;
0378     }
0379     o_tty = alloc_tty_struct(driver->other, idx);
0380     if (!o_tty)
0381         goto err_put_module;
0382 
0383     tty_set_lock_subclass(o_tty);
0384     lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
0385 
0386     if (legacy) {
0387         /* We always use new tty termios data so we can do this
0388            the easy way .. */
0389         tty_init_termios(tty);
0390         tty_init_termios(o_tty);
0391 
0392         driver->other->ttys[idx] = o_tty;
0393         driver->ttys[idx] = tty;
0394     } else {
0395         memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
0396         tty->termios = driver->init_termios;
0397         memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
0398         o_tty->termios = driver->other->init_termios;
0399     }
0400 
0401     /*
0402      * Everything allocated ... set up the o_tty structure.
0403      */
0404     tty_driver_kref_get(driver->other);
0405     /* Establish the links in both directions */
0406     tty->link   = o_tty;
0407     o_tty->link = tty;
0408     tty_port_init(ports[0]);
0409     tty_port_init(ports[1]);
0410     tty_buffer_set_limit(ports[0], 8192);
0411     tty_buffer_set_limit(ports[1], 8192);
0412     o_tty->port = ports[0];
0413     tty->port = ports[1];
0414     o_tty->port->itty = o_tty;
0415 
0416     tty_buffer_set_lock_subclass(o_tty->port);
0417 
0418     tty_driver_kref_get(driver);
0419     tty->count++;
0420     o_tty->count++;
0421     return 0;
0422 
0423 err_put_module:
0424     module_put(driver->other->owner);
0425 err:
0426     kfree(ports[0]);
0427     kfree(ports[1]);
0428     return retval;
0429 }
0430 
0431 static void pty_cleanup(struct tty_struct *tty)
0432 {
0433     tty_port_put(tty->port);
0434 }
0435 
0436 /* Traditional BSD devices */
0437 #ifdef CONFIG_LEGACY_PTYS
0438 
0439 static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
0440 {
0441     return pty_common_install(driver, tty, true);
0442 }
0443 
0444 static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
0445 {
0446     struct tty_struct *pair = tty->link;
0447 
0448     driver->ttys[tty->index] = NULL;
0449     if (pair)
0450         pair->driver->ttys[pair->index] = NULL;
0451 }
0452 
0453 static int pty_bsd_ioctl(struct tty_struct *tty,
0454              unsigned int cmd, unsigned long arg)
0455 {
0456     switch (cmd) {
0457     case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
0458         return pty_set_lock(tty, (int __user *) arg);
0459     case TIOCGPTLCK: /* Get PT Lock status */
0460         return pty_get_lock(tty, (int __user *)arg);
0461     case TIOCPKT: /* Set PT packet mode */
0462         return pty_set_pktmode(tty, (int __user *)arg);
0463     case TIOCGPKT: /* Get PT packet mode */
0464         return pty_get_pktmode(tty, (int __user *)arg);
0465     case TIOCSIG:    /* Send signal to other side of pty */
0466         return pty_signal(tty, (int) arg);
0467     case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
0468         return -EINVAL;
0469     }
0470     return -ENOIOCTLCMD;
0471 }
0472 
0473 #ifdef CONFIG_COMPAT
0474 static long pty_bsd_compat_ioctl(struct tty_struct *tty,
0475                  unsigned int cmd, unsigned long arg)
0476 {
0477     /*
0478      * PTY ioctls don't require any special translation between 32-bit and
0479      * 64-bit userspace, they are already compatible.
0480      */
0481     return pty_bsd_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
0482 }
0483 #else
0484 #define pty_bsd_compat_ioctl NULL
0485 #endif
0486 
0487 static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
0488 /*
0489  * not really modular, but the easiest way to keep compat with existing
0490  * bootargs behaviour is to continue using module_param here.
0491  */
0492 module_param(legacy_count, int, 0);
0493 
0494 /*
0495  * The master side of a pty can do TIOCSPTLCK and thus
0496  * has pty_bsd_ioctl.
0497  */
0498 static const struct tty_operations master_pty_ops_bsd = {
0499     .install = pty_install,
0500     .open = pty_open,
0501     .close = pty_close,
0502     .write = pty_write,
0503     .write_room = pty_write_room,
0504     .flush_buffer = pty_flush_buffer,
0505     .unthrottle = pty_unthrottle,
0506     .ioctl = pty_bsd_ioctl,
0507     .compat_ioctl = pty_bsd_compat_ioctl,
0508     .cleanup = pty_cleanup,
0509     .resize = pty_resize,
0510     .remove = pty_remove
0511 };
0512 
0513 static const struct tty_operations slave_pty_ops_bsd = {
0514     .install = pty_install,
0515     .open = pty_open,
0516     .close = pty_close,
0517     .write = pty_write,
0518     .write_room = pty_write_room,
0519     .flush_buffer = pty_flush_buffer,
0520     .unthrottle = pty_unthrottle,
0521     .set_termios = pty_set_termios,
0522     .cleanup = pty_cleanup,
0523     .resize = pty_resize,
0524     .start = pty_start,
0525     .stop = pty_stop,
0526     .remove = pty_remove
0527 };
0528 
0529 static void __init legacy_pty_init(void)
0530 {
0531     struct tty_driver *pty_driver, *pty_slave_driver;
0532 
0533     if (legacy_count <= 0)
0534         return;
0535 
0536     pty_driver = tty_alloc_driver(legacy_count,
0537             TTY_DRIVER_RESET_TERMIOS |
0538             TTY_DRIVER_REAL_RAW |
0539             TTY_DRIVER_DYNAMIC_ALLOC);
0540     if (IS_ERR(pty_driver))
0541         panic("Couldn't allocate pty driver");
0542 
0543     pty_slave_driver = tty_alloc_driver(legacy_count,
0544             TTY_DRIVER_RESET_TERMIOS |
0545             TTY_DRIVER_REAL_RAW |
0546             TTY_DRIVER_DYNAMIC_ALLOC);
0547     if (IS_ERR(pty_slave_driver))
0548         panic("Couldn't allocate pty slave driver");
0549 
0550     pty_driver->driver_name = "pty_master";
0551     pty_driver->name = "pty";
0552     pty_driver->major = PTY_MASTER_MAJOR;
0553     pty_driver->minor_start = 0;
0554     pty_driver->type = TTY_DRIVER_TYPE_PTY;
0555     pty_driver->subtype = PTY_TYPE_MASTER;
0556     pty_driver->init_termios = tty_std_termios;
0557     pty_driver->init_termios.c_iflag = 0;
0558     pty_driver->init_termios.c_oflag = 0;
0559     pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
0560     pty_driver->init_termios.c_lflag = 0;
0561     pty_driver->init_termios.c_ispeed = 38400;
0562     pty_driver->init_termios.c_ospeed = 38400;
0563     pty_driver->other = pty_slave_driver;
0564     tty_set_operations(pty_driver, &master_pty_ops_bsd);
0565 
0566     pty_slave_driver->driver_name = "pty_slave";
0567     pty_slave_driver->name = "ttyp";
0568     pty_slave_driver->major = PTY_SLAVE_MAJOR;
0569     pty_slave_driver->minor_start = 0;
0570     pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
0571     pty_slave_driver->subtype = PTY_TYPE_SLAVE;
0572     pty_slave_driver->init_termios = tty_std_termios;
0573     pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
0574     pty_slave_driver->init_termios.c_ispeed = 38400;
0575     pty_slave_driver->init_termios.c_ospeed = 38400;
0576     pty_slave_driver->other = pty_driver;
0577     tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
0578 
0579     if (tty_register_driver(pty_driver))
0580         panic("Couldn't register pty driver");
0581     if (tty_register_driver(pty_slave_driver))
0582         panic("Couldn't register pty slave driver");
0583 }
0584 #else
0585 static inline void legacy_pty_init(void) { }
0586 #endif
0587 
0588 /* Unix98 devices */
0589 #ifdef CONFIG_UNIX98_PTYS
0590 static struct cdev ptmx_cdev;
0591 
0592 /**
0593  *  ptm_open_peer - open the peer of a pty
0594  *  @master: the open struct file of the ptmx device node
0595  *  @tty: the master of the pty being opened
0596  *  @flags: the flags for open
0597  *
0598  *  Provide a race free way for userspace to open the slave end of a pty
0599  *  (where they have the master fd and cannot access or trust the mount
0600  *  namespace /dev/pts was mounted inside).
0601  */
0602 int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
0603 {
0604     int fd;
0605     struct file *filp;
0606     int retval = -EINVAL;
0607     struct path path;
0608 
0609     if (tty->driver != ptm_driver)
0610         return -EIO;
0611 
0612     fd = get_unused_fd_flags(flags);
0613     if (fd < 0) {
0614         retval = fd;
0615         goto err;
0616     }
0617 
0618     /* Compute the slave's path */
0619     path.mnt = devpts_mntget(master, tty->driver_data);
0620     if (IS_ERR(path.mnt)) {
0621         retval = PTR_ERR(path.mnt);
0622         goto err_put;
0623     }
0624     path.dentry = tty->link->driver_data;
0625 
0626     filp = dentry_open(&path, flags, current_cred());
0627     mntput(path.mnt);
0628     if (IS_ERR(filp)) {
0629         retval = PTR_ERR(filp);
0630         goto err_put;
0631     }
0632 
0633     fd_install(fd, filp);
0634     return fd;
0635 
0636 err_put:
0637     put_unused_fd(fd);
0638 err:
0639     return retval;
0640 }
0641 
0642 static int pty_unix98_ioctl(struct tty_struct *tty,
0643                 unsigned int cmd, unsigned long arg)
0644 {
0645     switch (cmd) {
0646     case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
0647         return pty_set_lock(tty, (int __user *)arg);
0648     case TIOCGPTLCK: /* Get PT Lock status */
0649         return pty_get_lock(tty, (int __user *)arg);
0650     case TIOCPKT: /* Set PT packet mode */
0651         return pty_set_pktmode(tty, (int __user *)arg);
0652     case TIOCGPKT: /* Get PT packet mode */
0653         return pty_get_pktmode(tty, (int __user *)arg);
0654     case TIOCGPTN: /* Get PT Number */
0655         return put_user(tty->index, (unsigned int __user *)arg);
0656     case TIOCSIG:    /* Send signal to other side of pty */
0657         return pty_signal(tty, (int) arg);
0658     }
0659 
0660     return -ENOIOCTLCMD;
0661 }
0662 
0663 #ifdef CONFIG_COMPAT
0664 static long pty_unix98_compat_ioctl(struct tty_struct *tty,
0665                  unsigned int cmd, unsigned long arg)
0666 {
0667     /*
0668      * PTY ioctls don't require any special translation between 32-bit and
0669      * 64-bit userspace, they are already compatible.
0670      */
0671     return pty_unix98_ioctl(tty, cmd,
0672         cmd == TIOCSIG ? arg : (unsigned long)compat_ptr(arg));
0673 }
0674 #else
0675 #define pty_unix98_compat_ioctl NULL
0676 #endif
0677 
0678 /**
0679  *  ptm_unix98_lookup   -   find a pty master
0680  *  @driver: ptm driver
0681  *  @file: unused
0682  *  @idx: tty index
0683  *
0684  *  Look up a pty master device. Called under the tty_mutex for now.
0685  *  This provides our locking.
0686  */
0687 
0688 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
0689         struct file *file, int idx)
0690 {
0691     /* Master must be open via /dev/ptmx */
0692     return ERR_PTR(-EIO);
0693 }
0694 
0695 /**
0696  *  pts_unix98_lookup   -   find a pty slave
0697  *  @driver: pts driver
0698  *  @file: file pointer to tty
0699  *  @idx: tty index
0700  *
0701  *  Look up a pty master device. Called under the tty_mutex for now.
0702  *  This provides our locking for the tty pointer.
0703  */
0704 
0705 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
0706         struct file *file, int idx)
0707 {
0708     struct tty_struct *tty;
0709 
0710     mutex_lock(&devpts_mutex);
0711     tty = devpts_get_priv(file->f_path.dentry);
0712     mutex_unlock(&devpts_mutex);
0713     /* Master must be open before slave */
0714     if (!tty)
0715         return ERR_PTR(-EIO);
0716     return tty;
0717 }
0718 
0719 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
0720 {
0721     return pty_common_install(driver, tty, false);
0722 }
0723 
0724 /* this is called once with whichever end is closed last */
0725 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
0726 {
0727     struct pts_fs_info *fsi;
0728 
0729     if (tty->driver->subtype == PTY_TYPE_MASTER)
0730         fsi = tty->driver_data;
0731     else
0732         fsi = tty->link->driver_data;
0733 
0734     if (fsi) {
0735         devpts_kill_index(fsi, tty->index);
0736         devpts_release(fsi);
0737     }
0738 }
0739 
0740 static void pty_show_fdinfo(struct tty_struct *tty, struct seq_file *m)
0741 {
0742     seq_printf(m, "tty-index:\t%d\n", tty->index);
0743 }
0744 
0745 static const struct tty_operations ptm_unix98_ops = {
0746     .lookup = ptm_unix98_lookup,
0747     .install = pty_unix98_install,
0748     .remove = pty_unix98_remove,
0749     .open = pty_open,
0750     .close = pty_close,
0751     .write = pty_write,
0752     .write_room = pty_write_room,
0753     .flush_buffer = pty_flush_buffer,
0754     .unthrottle = pty_unthrottle,
0755     .ioctl = pty_unix98_ioctl,
0756     .compat_ioctl = pty_unix98_compat_ioctl,
0757     .resize = pty_resize,
0758     .cleanup = pty_cleanup,
0759     .show_fdinfo = pty_show_fdinfo,
0760 };
0761 
0762 static const struct tty_operations pty_unix98_ops = {
0763     .lookup = pts_unix98_lookup,
0764     .install = pty_unix98_install,
0765     .remove = pty_unix98_remove,
0766     .open = pty_open,
0767     .close = pty_close,
0768     .write = pty_write,
0769     .write_room = pty_write_room,
0770     .flush_buffer = pty_flush_buffer,
0771     .unthrottle = pty_unthrottle,
0772     .set_termios = pty_set_termios,
0773     .start = pty_start,
0774     .stop = pty_stop,
0775     .cleanup = pty_cleanup,
0776 };
0777 
0778 /**
0779  *  ptmx_open       -   open a unix 98 pty master
0780  *  @inode: inode of device file
0781  *  @filp: file pointer to tty
0782  *
0783  *  Allocate a unix98 pty master device from the ptmx driver.
0784  *
0785  *  Locking: tty_mutex protects the init_dev work. tty->count should
0786  *      protect the rest.
0787  *      allocated_ptys_lock handles the list of free pty numbers
0788  */
0789 
0790 static int ptmx_open(struct inode *inode, struct file *filp)
0791 {
0792     struct pts_fs_info *fsi;
0793     struct tty_struct *tty;
0794     struct dentry *dentry;
0795     int retval;
0796     int index;
0797 
0798     nonseekable_open(inode, filp);
0799 
0800     /* We refuse fsnotify events on ptmx, since it's a shared resource */
0801     filp->f_mode |= FMODE_NONOTIFY;
0802 
0803     retval = tty_alloc_file(filp);
0804     if (retval)
0805         return retval;
0806 
0807     fsi = devpts_acquire(filp);
0808     if (IS_ERR(fsi)) {
0809         retval = PTR_ERR(fsi);
0810         goto out_free_file;
0811     }
0812 
0813     /* find a device that is not in use. */
0814     mutex_lock(&devpts_mutex);
0815     index = devpts_new_index(fsi);
0816     mutex_unlock(&devpts_mutex);
0817 
0818     retval = index;
0819     if (index < 0)
0820         goto out_put_fsi;
0821 
0822 
0823     mutex_lock(&tty_mutex);
0824     tty = tty_init_dev(ptm_driver, index);
0825     /* The tty returned here is locked so we can safely
0826        drop the mutex */
0827     mutex_unlock(&tty_mutex);
0828 
0829     retval = PTR_ERR(tty);
0830     if (IS_ERR(tty))
0831         goto out;
0832 
0833     /*
0834      * From here on out, the tty is "live", and the index and
0835      * fsi will be killed/put by the tty_release()
0836      */
0837     set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
0838     tty->driver_data = fsi;
0839 
0840     tty_add_file(tty, filp);
0841 
0842     dentry = devpts_pty_new(fsi, index, tty->link);
0843     if (IS_ERR(dentry)) {
0844         retval = PTR_ERR(dentry);
0845         goto err_release;
0846     }
0847     tty->link->driver_data = dentry;
0848 
0849     retval = ptm_driver->ops->open(tty, filp);
0850     if (retval)
0851         goto err_release;
0852 
0853     tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
0854 
0855     tty_unlock(tty);
0856     return 0;
0857 err_release:
0858     tty_unlock(tty);
0859     // This will also put-ref the fsi
0860     tty_release(inode, filp);
0861     return retval;
0862 out:
0863     devpts_kill_index(fsi, index);
0864 out_put_fsi:
0865     devpts_release(fsi);
0866 out_free_file:
0867     tty_free_file(filp);
0868     return retval;
0869 }
0870 
0871 static struct file_operations ptmx_fops __ro_after_init;
0872 
0873 static void __init unix98_pty_init(void)
0874 {
0875     ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
0876             TTY_DRIVER_RESET_TERMIOS |
0877             TTY_DRIVER_REAL_RAW |
0878             TTY_DRIVER_DYNAMIC_DEV |
0879             TTY_DRIVER_DEVPTS_MEM |
0880             TTY_DRIVER_DYNAMIC_ALLOC);
0881     if (IS_ERR(ptm_driver))
0882         panic("Couldn't allocate Unix98 ptm driver");
0883     pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
0884             TTY_DRIVER_RESET_TERMIOS |
0885             TTY_DRIVER_REAL_RAW |
0886             TTY_DRIVER_DYNAMIC_DEV |
0887             TTY_DRIVER_DEVPTS_MEM |
0888             TTY_DRIVER_DYNAMIC_ALLOC);
0889     if (IS_ERR(pts_driver))
0890         panic("Couldn't allocate Unix98 pts driver");
0891 
0892     ptm_driver->driver_name = "pty_master";
0893     ptm_driver->name = "ptm";
0894     ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
0895     ptm_driver->minor_start = 0;
0896     ptm_driver->type = TTY_DRIVER_TYPE_PTY;
0897     ptm_driver->subtype = PTY_TYPE_MASTER;
0898     ptm_driver->init_termios = tty_std_termios;
0899     ptm_driver->init_termios.c_iflag = 0;
0900     ptm_driver->init_termios.c_oflag = 0;
0901     ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
0902     ptm_driver->init_termios.c_lflag = 0;
0903     ptm_driver->init_termios.c_ispeed = 38400;
0904     ptm_driver->init_termios.c_ospeed = 38400;
0905     ptm_driver->other = pts_driver;
0906     tty_set_operations(ptm_driver, &ptm_unix98_ops);
0907 
0908     pts_driver->driver_name = "pty_slave";
0909     pts_driver->name = "pts";
0910     pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
0911     pts_driver->minor_start = 0;
0912     pts_driver->type = TTY_DRIVER_TYPE_PTY;
0913     pts_driver->subtype = PTY_TYPE_SLAVE;
0914     pts_driver->init_termios = tty_std_termios;
0915     pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
0916     pts_driver->init_termios.c_ispeed = 38400;
0917     pts_driver->init_termios.c_ospeed = 38400;
0918     pts_driver->other = ptm_driver;
0919     tty_set_operations(pts_driver, &pty_unix98_ops);
0920 
0921     if (tty_register_driver(ptm_driver))
0922         panic("Couldn't register Unix98 ptm driver");
0923     if (tty_register_driver(pts_driver))
0924         panic("Couldn't register Unix98 pts driver");
0925 
0926     /* Now create the /dev/ptmx special device */
0927     tty_default_fops(&ptmx_fops);
0928     ptmx_fops.open = ptmx_open;
0929 
0930     cdev_init(&ptmx_cdev, &ptmx_fops);
0931     if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
0932         register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
0933         panic("Couldn't register /dev/ptmx driver");
0934     device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
0935 }
0936 
0937 #else
0938 static inline void unix98_pty_init(void) { }
0939 #endif
0940 
0941 static int __init pty_init(void)
0942 {
0943     legacy_pty_init();
0944     unix98_pty_init();
0945     return 0;
0946 }
0947 device_initcall(pty_init);