0001
0002
0003
0004
0005
0006
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
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
0085
0086
0087
0088
0089
0090
0091
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
0101
0102
0103
0104
0105
0106
0107
0108
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
0123
0124
0125
0126
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
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
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
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
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
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
0276
0277
0278
0279
0280
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
0289 mutex_lock(&tty->winsize_mutex);
0290 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
0291 goto done;
0292
0293
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;
0307 done:
0308 mutex_unlock(&tty->winsize_mutex);
0309 return 0;
0310 }
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
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
0350
0351
0352
0353
0354
0355
0356
0357
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
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
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
0388
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
0403
0404 tty_driver_kref_get(driver->other);
0405
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
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:
0458 return pty_set_lock(tty, (int __user *) arg);
0459 case TIOCGPTLCK:
0460 return pty_get_lock(tty, (int __user *)arg);
0461 case TIOCPKT:
0462 return pty_set_pktmode(tty, (int __user *)arg);
0463 case TIOCGPKT:
0464 return pty_get_pktmode(tty, (int __user *)arg);
0465 case TIOCSIG:
0466 return pty_signal(tty, (int) arg);
0467 case TIOCGPTN:
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
0479
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
0490
0491
0492 module_param(legacy_count, int, 0);
0493
0494
0495
0496
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
0589 #ifdef CONFIG_UNIX98_PTYS
0590 static struct cdev ptmx_cdev;
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
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
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:
0647 return pty_set_lock(tty, (int __user *)arg);
0648 case TIOCGPTLCK:
0649 return pty_get_lock(tty, (int __user *)arg);
0650 case TIOCPKT:
0651 return pty_set_pktmode(tty, (int __user *)arg);
0652 case TIOCGPKT:
0653 return pty_get_pktmode(tty, (int __user *)arg);
0654 case TIOCGPTN:
0655 return put_user(tty->index, (unsigned int __user *)arg);
0656 case TIOCSIG:
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
0669
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
0680
0681
0682
0683
0684
0685
0686
0687
0688 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
0689 struct file *file, int idx)
0690 {
0691
0692 return ERR_PTR(-EIO);
0693 }
0694
0695
0696
0697
0698
0699
0700
0701
0702
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
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
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
0780
0781
0782
0783
0784
0785
0786
0787
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
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
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
0826
0827 mutex_unlock(&tty_mutex);
0828
0829 retval = PTR_ERR(tty);
0830 if (IS_ERR(tty))
0831 goto out;
0832
0833
0834
0835
0836
0837 set_bit(TTY_PTY_LOCK, &tty->flags);
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
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
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);