| 1 | |
| 2 | |
| 3 | * Copyright (C) 1991, 1992 Linus Torvalds
|
| 4 | *
|
| 5 | * Added support for a Unix98-style ptmx device.
|
| 6 | * -- C. Scott Ananian <[email protected]>, 14-Jan-1998
|
| 7 | *
|
| 8 |
|
| 9 |
|
| 10 | #include <linux/module.h>
|
| 11 | #include <linux/errno.h>
|
| 12 | #include <linux/interrupt.h>
|
| 13 | #include <linux/tty.h>
|
| 14 | #include <linux/tty_flip.h>
|
| 15 | #include <linux/fcntl.h>
|
| 16 | #include <linux/sched/signal.h>
|
| 17 | #include <linux/string.h>
|
| 18 | #include <linux/major.h>
|
| 19 | #include <linux/mm.h>
|
| 20 | #include <linux/init.h>
|
| 21 | #include <linux/device.h>
|
| 22 | #include <linux/uaccess.h>
|
| 23 | #include <linux/bitops.h>
|
| 24 | #include <linux/devpts_fs.h>
|
| 25 | #include <linux/slab.h>
|
| 26 | #include <linux/mutex.h>
|
| 27 | #include <linux/poll.h>
|
| 28 | #include <linux/mount.h>
|
| 29 | #include <linux/file.h>
|
| 30 | #include <linux/ioctl.h>
|
| 31 | #include <linux/compat.h>
|
| 32 | #include "tty.h"
|
| 33 |
|
| 34 | #undef TTY_DEBUG_HANGUP
|
| 35 | #ifdef TTY_DEBUG_HANGUP
|
| 36 | # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
|
| 37 | #else
|
| 38 | # define tty_debug_hangup(tty, f, args...) do {} while (0)
|
| 39 | #endif
|
| 40 |
|
| 41 | #ifdef CONFIG_UNIX98_PTYS
|
| 42 | static struct tty_driver *ptm_driver;
|
| 43 | static struct tty_driver *pts_driver;
|
| 44 | static DEFINE_MUTEX(devpts_mutex);
|
| 45 | #endif
|
| 46 |
|
| 47 | static void pty_close(struct tty_struct *tty, struct file *filp)
|
| 48 | {
|
| 49 | if (tty->driver->subtype == PTY_TYPE_MASTER)
|
| 50 | WARN_ON(tty->count > 1);
|
| 51 | else {
|
| 52 | if (tty_io_error(tty))
|
| 53 | return;
|
| 54 | if (tty->count > 2)
|
| 55 | return;
|
| 56 | }
|
| 57 | set_bit(TTY_IO_ERROR, &tty->flags);
|
| 58 | wake_up_interruptible(&tty->read_wait);
|
| 59 | wake_up_interruptible(&tty->write_wait);
|
| 60 | scoped_guard(spinlock_irq, &tty->ctrl.lock)
|
| 61 | tty->ctrl.packet = false;
|
| 62 |
|
| 63 | if (!tty->link)
|
| 64 | return;
|
| 65 | set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
|
| 66 | wake_up_interruptible(&tty->link->read_wait);
|
| 67 | wake_up_interruptible(&tty->link->write_wait);
|
| 68 | if (tty->driver->subtype == PTY_TYPE_MASTER) {
|
| 69 | set_bit(TTY_OTHER_CLOSED, &tty->flags);
|
| 70 | #ifdef CONFIG_UNIX98_PTYS
|
| 71 | if (tty->driver == ptm_driver) {
|
| 72 | guard(mutex)(&devpts_mutex);
|
| 73 | if (tty->link->driver_data)
|
| 74 | devpts_pty_kill(tty->link->driver_data);
|
| 75 | }
|
| 76 | #endif
|
| 77 | tty_vhangup(tty->link);
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | |
| 82 | * The unthrottle routine is called by the line discipline to signal
|
| 83 | * that it can receive more characters. For PTY's, the TTY_THROTTLED
|
| 84 | * flag is always set, to force the line discipline to always call the
|
| 85 | * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
|
| 86 | * characters in the queue. This is necessary since each time this
|
| 87 | * happens, we need to wake up any sleeping processes that could be
|
| 88 | * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
|
| 89 | * for the pty buffer to be drained.
|
| 90 |
|
| 91 | static void pty_unthrottle(struct tty_struct *tty)
|
| 92 | {
|
| 93 | tty_wakeup(tty->link);
|
| 94 | set_bit(TTY_THROTTLED, &tty->flags);
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | * pty_write - write to a pty
|
| 99 | * @tty: the tty we write from
|
| 100 | * @buf: kernel buffer of data
|
| 101 | * @c: bytes to write
|
| 102 | *
|
| 103 | * Our "hardware" write method. Data is coming from the ldisc which
|
| 104 | * may be in a non sleeping state. We simply throw this at the other
|
| 105 | * end of the link as if we were an IRQ handler receiving stuff for
|
| 106 | * the other side of the pty/tty pair.
|
| 107 |
|
| 108 |
|
| 109 | static ssize_t pty_write(struct tty_struct *tty, const u8 *buf, size_t c)
|
| 110 | {
|
| 111 | struct tty_struct *to = tty->link;
|
| 112 |
|
| 113 | if (tty->flow.stopped || !c)
|
| 114 | return 0;
|
| 115 |
|
| 116 | return tty_insert_flip_string_and_push_buffer(to->port, buf, c);
|
| 117 | }
|
| 118 |
|
| 119 | |
| 120 | * pty_write_room - write space
|
| 121 | * @tty: tty we are writing from
|
| 122 | *
|
| 123 | * Report how many bytes the ldisc can send into the queue for
|
| 124 | * the other device.
|
| 125 |
|
| 126 |
|
| 127 | static unsigned int pty_write_room(struct tty_struct *tty)
|
| 128 | {
|
| 129 | if (tty->flow.stopped)
|
| 130 | return 0;
|
| 131 | return tty_buffer_space_avail(tty->link->port);
|
| 132 | }
|
| 133 |
|
| 134 |
|
| 135 | static int pty_set_lock(struct tty_struct *tty, int __user *arg)
|
| 136 | {
|
| 137 | int val;
|
| 138 |
|
| 139 | if (get_user(val, arg))
|
| 140 | return -EFAULT;
|
| 141 | if (val)
|
| 142 | set_bit(TTY_PTY_LOCK, &tty->flags);
|
| 143 | else
|
| 144 | clear_bit(TTY_PTY_LOCK, &tty->flags);
|
| 145 | return 0;
|
| 146 | }
|
| 147 |
|
| 148 | static int pty_get_lock(struct tty_struct *tty, int __user *arg)
|
| 149 | {
|
| 150 | int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
|
| 151 |
|
| 152 | return put_user(locked, arg);
|
| 153 | }
|
| 154 |
|
| 155 |
|
| 156 | static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
|
| 157 | {
|
| 158 | int want_pktmode;
|
| 159 |
|
| 160 | if (get_user(want_pktmode, arg))
|
| 161 | return -EFAULT;
|
| 162 |
|
| 163 | guard(spinlock_irq)(&tty->ctrl.lock);
|
| 164 | if (!want_pktmode) {
|
| 165 | tty->ctrl.packet = false;
|
| 166 | return 0;
|
| 167 | }
|
| 168 |
|
| 169 | if (tty->ctrl.packet)
|
| 170 | return 0;
|
| 171 |
|
| 172 | tty->link->ctrl.pktstatus = 0;
|
| 173 | smp_mb();
|
| 174 | tty->ctrl.packet = true;
|
| 175 |
|
| 176 | return 0;
|
| 177 | }
|
| 178 |
|
| 179 |
|
| 180 | static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
|
| 181 | {
|
| 182 | int pktmode = tty->ctrl.packet;
|
| 183 |
|
| 184 | return put_user(pktmode, arg);
|
| 185 | }
|
| 186 |
|
| 187 |
|
| 188 | static int pty_signal(struct tty_struct *tty, int sig)
|
| 189 | {
|
| 190 | struct pid *pgrp;
|
| 191 |
|
| 192 | if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
|
| 193 | return -EINVAL;
|
| 194 |
|
| 195 | if (tty->link) {
|
| 196 | pgrp = tty_get_pgrp(tty->link);
|
| 197 | if (pgrp)
|
| 198 | kill_pgrp(pgrp, sig, 1);
|
| 199 | put_pid(pgrp);
|
| 200 | }
|
| 201 | return 0;
|
| 202 | }
|
| 203 |
|
| 204 | static void pty_flush_buffer(struct tty_struct *tty)
|
| 205 | {
|
| 206 | struct tty_struct *to = tty->link;
|
| 207 |
|
| 208 | if (!to)
|
| 209 | return;
|
| 210 |
|
| 211 | tty_buffer_flush(to, NULL);
|
| 212 | if (to->ctrl.packet) {
|
| 213 | guard(spinlock_irq)(&tty->ctrl.lock);
|
| 214 | tty->ctrl.pktstatus |= TIOCPKT_FLUSHWRITE;
|
| 215 | wake_up_interruptible(&to->read_wait);
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | static int pty_open(struct tty_struct *tty, struct file *filp)
|
| 220 | {
|
| 221 | if (!tty || !tty->link)
|
| 222 | return -ENODEV;
|
| 223 |
|
| 224 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
|
| 225 | goto out;
|
| 226 | if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
|
| 227 | goto out;
|
| 228 | if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
|
| 229 | goto out;
|
| 230 |
|
| 231 | clear_bit(TTY_IO_ERROR, &tty->flags);
|
| 232 | clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
|
| 233 | set_bit(TTY_THROTTLED, &tty->flags);
|
| 234 | return 0;
|
| 235 |
|
| 236 | out:
|
| 237 | set_bit(TTY_IO_ERROR, &tty->flags);
|
| 238 | return -EIO;
|
| 239 | }
|
| 240 |
|
| 241 | static void pty_set_termios(struct tty_struct *tty,
|
| 242 | const struct ktermios *old_termios)
|
| 243 | {
|
| 244 |
|
| 245 | if (tty->link && tty->link->ctrl.packet) {
|
| 246 | int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
|
| 247 | int old_flow = ((old_termios->c_iflag & IXON) &&
|
| 248 | (old_termios->c_cc[VSTOP] == '\023') &&
|
| 249 | (old_termios->c_cc[VSTART] == '\021'));
|
| 250 | int new_flow = (I_IXON(tty) &&
|
| 251 | STOP_CHAR(tty) == '\023' &&
|
| 252 | START_CHAR(tty) == '\021');
|
| 253 | if ((old_flow != new_flow) || extproc) {
|
| 254 | scoped_guard(spinlock_irq, &tty->ctrl.lock) {
|
| 255 | if (old_flow != new_flow) {
|
| 256 | tty->ctrl.pktstatus &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
|
| 257 | if (new_flow)
|
| 258 | tty->ctrl.pktstatus |= TIOCPKT_DOSTOP;
|
| 259 | else
|
| 260 | tty->ctrl.pktstatus |= TIOCPKT_NOSTOP;
|
| 261 | }
|
| 262 | if (extproc)
|
| 263 | tty->ctrl.pktstatus |= TIOCPKT_IOCTL;
|
| 264 | }
|
| 265 | wake_up_interruptible(&tty->link->read_wait);
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | tty->termios.c_cflag &= ~(CSIZE | PARENB);
|
| 270 | tty->termios.c_cflag |= (CS8 | CREAD);
|
| 271 | }
|
| 272 |
|
| 273 | |
| 274 | * pty_resize - resize event
|
| 275 | * @tty: tty being resized
|
| 276 | * @ws: window size being set.
|
| 277 | *
|
| 278 | * Update the termios variables and send the necessary signals to
|
| 279 | * peform a terminal resize correctly
|
| 280 |
|
| 281 |
|
| 282 | static int pty_resize(struct tty_struct *tty, struct winsize *ws)
|
| 283 | {
|
| 284 | struct pid *pgrp, *rpgrp;
|
| 285 | struct tty_struct *pty = tty->link;
|
| 286 |
|
| 287 |
|
| 288 | guard(mutex)(&tty->winsize_mutex);
|
| 289 | if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
|
| 290 | return 0;
|
| 291 |
|
| 292 |
|
| 293 | pgrp = tty_get_pgrp(tty);
|
| 294 | rpgrp = tty_get_pgrp(pty);
|
| 295 |
|
| 296 | if (pgrp)
|
| 297 | kill_pgrp(pgrp, SIGWINCH, 1);
|
| 298 | if (rpgrp != pgrp && rpgrp)
|
| 299 | kill_pgrp(rpgrp, SIGWINCH, 1);
|
| 300 |
|
| 301 | put_pid(pgrp);
|
| 302 | put_pid(rpgrp);
|
| 303 |
|
| 304 | tty->winsize = *ws;
|
| 305 | pty->winsize = *ws;
|
| 306 |
|
| 307 | return 0;
|
| 308 | }
|
| 309 |
|
| 310 | |
| 311 | * pty_start - start() handler
|
| 312 | * pty_stop - stop() handler
|
| 313 | * @tty: tty being flow-controlled
|
| 314 | *
|
| 315 | * Propagates the TIOCPKT status to the master pty.
|
| 316 | *
|
| 317 | * NB: only the master pty can be in packet mode so only the slave
|
| 318 | * needs start()/stop() handlers
|
| 319 |
|
| 320 | static void pty_start(struct tty_struct *tty)
|
| 321 | {
|
| 322 | if (!tty->link || !tty->link->ctrl.packet)
|
| 323 | return;
|
| 324 |
|
| 325 | scoped_guard(spinlock_irqsave, &tty->ctrl.lock) {
|
| 326 | tty->ctrl.pktstatus &= ~TIOCPKT_STOP;
|
| 327 | tty->ctrl.pktstatus |= TIOCPKT_START;
|
| 328 | }
|
| 329 | wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
|
| 330 | }
|
| 331 |
|
| 332 | static void pty_stop(struct tty_struct *tty)
|
| 333 | {
|
| 334 | if (!tty->link || !tty->link->ctrl.packet)
|
| 335 | return;
|
| 336 |
|
| 337 | scoped_guard(spinlock_irqsave, &tty->ctrl.lock) {
|
| 338 | tty->ctrl.pktstatus &= ~TIOCPKT_START;
|
| 339 | tty->ctrl.pktstatus |= TIOCPKT_STOP;
|
| 340 | }
|
| 341 | wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
|
| 342 | }
|
| 343 |
|
| 344 | |
| 345 | * pty_common_install - set up the pty pair
|
| 346 | * @driver: the pty driver
|
| 347 | * @tty: the tty being instantiated
|
| 348 | * @legacy: true if this is BSD style
|
| 349 | *
|
| 350 | * Perform the initial set up for the tty/pty pair. Called from the
|
| 351 | * tty layer when the port is first opened.
|
| 352 | *
|
| 353 | * Locking: the caller must hold the tty_mutex
|
| 354 |
|
| 355 | static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
|
| 356 | bool legacy)
|
| 357 | {
|
| 358 | struct tty_struct *o_tty;
|
| 359 | struct tty_port *ports[2];
|
| 360 | int idx = tty->index;
|
| 361 | int retval = -ENOMEM;
|
| 362 |
|
| 363 |
|
| 364 | if (driver->subtype != PTY_TYPE_MASTER)
|
| 365 | return -EIO;
|
| 366 |
|
| 367 | ports[0] = kmalloc_obj(**ports);
|
| 368 | ports[1] = kmalloc_obj(**ports);
|
| 369 | if (!ports[0] || !ports[1])
|
| 370 | goto err;
|
| 371 | if (!try_module_get(driver->other->owner)) {
|
| 372 |
|
| 373 | goto err;
|
| 374 | }
|
| 375 | o_tty = alloc_tty_struct(driver->other, idx);
|
| 376 | if (!o_tty)
|
| 377 | goto err_put_module;
|
| 378 |
|
| 379 | tty_set_lock_subclass(o_tty);
|
| 380 | lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
|
| 381 |
|
| 382 | if (legacy) {
|
| 383 | |
| 384 | the easy way ..
|
| 385 | tty_init_termios(tty);
|
| 386 | tty_init_termios(o_tty);
|
| 387 |
|
| 388 | driver->other->ttys[idx] = o_tty;
|
| 389 | driver->ttys[idx] = tty;
|
| 390 | } else {
|
| 391 | memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
|
| 392 | tty->termios = driver->init_termios;
|
| 393 | memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
|
| 394 | o_tty->termios = driver->other->init_termios;
|
| 395 | }
|
| 396 |
|
| 397 | |
| 398 | * Everything allocated ... set up the o_tty structure.
|
| 399 |
|
| 400 | tty_driver_kref_get(driver->other);
|
| 401 |
|
| 402 | tty->link = o_tty;
|
| 403 | o_tty->link = tty;
|
| 404 | tty_port_init(ports[0]);
|
| 405 | tty_port_init(ports[1]);
|
| 406 | tty_buffer_set_limit(ports[0], 8192);
|
| 407 | tty_buffer_set_limit(ports[1], 8192);
|
| 408 | o_tty->port = ports[0];
|
| 409 | tty->port = ports[1];
|
| 410 | o_tty->port->itty = o_tty;
|
| 411 |
|
| 412 | tty_buffer_set_lock_subclass(o_tty->port);
|
| 413 |
|
| 414 | tty_driver_kref_get(driver);
|
| 415 | tty->count++;
|
| 416 | o_tty->count++;
|
| 417 | return 0;
|
| 418 |
|
| 419 | err_put_module:
|
| 420 | module_put(driver->other->owner);
|
| 421 | err:
|
| 422 | kfree(ports[0]);
|
| 423 | kfree(ports[1]);
|
| 424 | return retval;
|
| 425 | }
|
| 426 |
|
| 427 | static void pty_cleanup(struct tty_struct *tty)
|
| 428 | {
|
| 429 | tty_port_put(tty->port);
|
| 430 | }
|
| 431 |
|
| 432 |
|
| 433 | #ifdef CONFIG_LEGACY_PTYS
|
| 434 |
|
| 435 | static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
|
| 436 | {
|
| 437 | return pty_common_install(driver, tty, true);
|
| 438 | }
|
| 439 |
|
| 440 | static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
|
| 441 | {
|
| 442 | struct tty_struct *pair = tty->link;
|
| 443 |
|
| 444 | driver->ttys[tty->index] = NULL;
|
| 445 | if (pair)
|
| 446 | pair->driver->ttys[pair->index] = NULL;
|
| 447 | }
|
| 448 |
|
| 449 | static int pty_bsd_ioctl(struct tty_struct *tty,
|
| 450 | unsigned int cmd, unsigned long arg)
|
| 451 | {
|
| 452 | switch (cmd) {
|
| 453 | case TIOCSPTLCK:
|
| 454 | return pty_set_lock(tty, (int __user *) arg);
|
| 455 | case TIOCGPTLCK:
|
| 456 | return pty_get_lock(tty, (int __user *)arg);
|
| 457 | case TIOCPKT:
|
| 458 | return pty_set_pktmode(tty, (int __user *)arg);
|
| 459 | case TIOCGPKT:
|
| 460 | return pty_get_pktmode(tty, (int __user *)arg);
|
| 461 | case TIOCSIG:
|
| 462 | return pty_signal(tty, (int) arg);
|
| 463 | case TIOCGPTN:
|
| 464 | return -EINVAL;
|
| 465 | }
|
| 466 | return -ENOIOCTLCMD;
|
| 467 | }
|
| 468 |
|
| 469 | #ifdef CONFIG_COMPAT
|
| 470 | static long pty_bsd_compat_ioctl(struct tty_struct *tty,
|
| 471 | unsigned int cmd, unsigned long arg)
|
| 472 | {
|
| 473 | |
| 474 | * PTY ioctls don't require any special translation between 32-bit and
|
| 475 | * 64-bit userspace, they are already compatible.
|
| 476 |
|
| 477 | return pty_bsd_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
|
| 478 | }
|
| 479 | #else
|
| 480 | #define pty_bsd_compat_ioctl NULL
|
| 481 | #endif
|
| 482 |
|
| 483 | static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
|
| 484 | |
| 485 | * not really modular, but the easiest way to keep compat with existing
|
| 486 | * bootargs behaviour is to continue using module_param here.
|
| 487 |
|
| 488 | module_param(legacy_count, int, 0);
|
| 489 |
|
| 490 | |
| 491 | * The master side of a pty can do TIOCSPTLCK and thus
|
| 492 | * has pty_bsd_ioctl.
|
| 493 |
|
| 494 | static const struct tty_operations master_pty_ops_bsd = {
|
| 495 | .install = pty_install,
|
| 496 | .open = pty_open,
|
| 497 | .close = pty_close,
|
| 498 | .write = pty_write,
|
| 499 | .write_room = pty_write_room,
|
| 500 | .flush_buffer = pty_flush_buffer,
|
| 501 | .unthrottle = pty_unthrottle,
|
| 502 | .ioctl = pty_bsd_ioctl,
|
| 503 | .compat_ioctl = pty_bsd_compat_ioctl,
|
| 504 | .cleanup = pty_cleanup,
|
| 505 | .resize = pty_resize,
|
| 506 | .remove = pty_remove
|
| 507 | };
|
| 508 |
|
| 509 | static const struct tty_operations slave_pty_ops_bsd = {
|
| 510 | .install = pty_install,
|
| 511 | .open = pty_open,
|
| 512 | .close = pty_close,
|
| 513 | .write = pty_write,
|
| 514 | .write_room = pty_write_room,
|
| 515 | .flush_buffer = pty_flush_buffer,
|
| 516 | .unthrottle = pty_unthrottle,
|
| 517 | .set_termios = pty_set_termios,
|
| 518 | .cleanup = pty_cleanup,
|
| 519 | .resize = pty_resize,
|
| 520 | .start = pty_start,
|
| 521 | .stop = pty_stop,
|
| 522 | .remove = pty_remove
|
| 523 | };
|
| 524 |
|
| 525 | static void __init legacy_pty_init(void)
|
| 526 | {
|
| 527 | struct tty_driver *pty_driver, *pty_slave_driver;
|
| 528 |
|
| 529 | if (legacy_count <= 0)
|
| 530 | return;
|
| 531 |
|
| 532 | pty_driver = tty_alloc_driver(legacy_count,
|
| 533 | TTY_DRIVER_RESET_TERMIOS |
|
| 534 | TTY_DRIVER_REAL_RAW |
|
| 535 | TTY_DRIVER_DYNAMIC_ALLOC |
|
| 536 | TTY_DRIVER_NO_WORKQUEUE);
|
| 537 | if (IS_ERR(pty_driver))
|
| 538 | panic("Couldn't allocate pty driver");
|
| 539 |
|
| 540 | pty_slave_driver = tty_alloc_driver(legacy_count,
|
| 541 | TTY_DRIVER_RESET_TERMIOS |
|
| 542 | TTY_DRIVER_REAL_RAW |
|
| 543 | TTY_DRIVER_DYNAMIC_ALLOC |
|
| 544 | TTY_DRIVER_NO_WORKQUEUE);
|
| 545 | if (IS_ERR(pty_slave_driver))
|
| 546 | panic("Couldn't allocate pty slave driver");
|
| 547 |
|
| 548 | pty_driver->driver_name = "pty_master";
|
| 549 | pty_driver->name = "pty";
|
| 550 | pty_driver->major = PTY_MASTER_MAJOR;
|
| 551 | pty_driver->minor_start = 0;
|
| 552 | pty_driver->type = TTY_DRIVER_TYPE_PTY;
|
| 553 | pty_driver->subtype = PTY_TYPE_MASTER;
|
| 554 | pty_driver->init_termios = tty_std_termios;
|
| 555 | pty_driver->init_termios.c_iflag = 0;
|
| 556 | pty_driver->init_termios.c_oflag = 0;
|
| 557 | pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
|
| 558 | pty_driver->init_termios.c_lflag = 0;
|
| 559 | pty_driver->init_termios.c_ispeed = 38400;
|
| 560 | pty_driver->init_termios.c_ospeed = 38400;
|
| 561 | pty_driver->other = pty_slave_driver;
|
| 562 | tty_set_operations(pty_driver, &master_pty_ops_bsd);
|
| 563 |
|
| 564 | pty_slave_driver->driver_name = "pty_slave";
|
| 565 | pty_slave_driver->name = "ttyp";
|
| 566 | pty_slave_driver->major = PTY_SLAVE_MAJOR;
|
| 567 | pty_slave_driver->minor_start = 0;
|
| 568 | pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
|
| 569 | pty_slave_driver->subtype = PTY_TYPE_SLAVE;
|
| 570 | pty_slave_driver->init_termios = tty_std_termios;
|
| 571 | pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
|
| 572 | pty_slave_driver->init_termios.c_ispeed = 38400;
|
| 573 | pty_slave_driver->init_termios.c_ospeed = 38400;
|
| 574 | pty_slave_driver->other = pty_driver;
|
| 575 | tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
|
| 576 |
|
| 577 | if (tty_register_driver(pty_driver))
|
| 578 | panic("Couldn't register pty driver");
|
| 579 | if (tty_register_driver(pty_slave_driver))
|
| 580 | panic("Couldn't register pty slave driver");
|
| 581 | }
|
| 582 | #else
|
| 583 | static inline void legacy_pty_init(void) { }
|
| 584 | #endif
|
| 585 |
|
| 586 |
|
| 587 | #ifdef CONFIG_UNIX98_PTYS
|
| 588 | static struct cdev ptmx_cdev;
|
| 589 |
|
| 590 | static struct file *ptm_open_peer_file(struct file *master,
|
| 591 | struct tty_struct *tty, int flags)
|
| 592 | {
|
| 593 | struct path path;
|
| 594 | struct file *file;
|
| 595 |
|
| 596 |
|
| 597 | path.mnt = devpts_mntget(master, tty->driver_data);
|
| 598 | if (IS_ERR(path.mnt))
|
| 599 | return ERR_CAST(path.mnt);
|
| 600 | path.dentry = tty->link->driver_data;
|
| 601 |
|
| 602 | file = dentry_open(&path, flags, current_cred());
|
| 603 | mntput(path.mnt);
|
| 604 | return file;
|
| 605 | }
|
| 606 |
|
| 607 | |
| 608 | * ptm_open_peer - open the peer of a pty
|
| 609 | * @master: the open struct file of the ptmx device node
|
| 610 | * @tty: the master of the pty being opened
|
| 611 | * @flags: the flags for open
|
| 612 | *
|
| 613 | * Provide a race free way for userspace to open the slave end of a pty
|
| 614 | * (where they have the master fd and cannot access or trust the mount
|
| 615 | * namespace /dev/pts was mounted inside).
|
| 616 |
|
| 617 | int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
|
| 618 | {
|
| 619 | if (tty->driver != ptm_driver)
|
| 620 | return -EIO;
|
| 621 |
|
| 622 | return FD_ADD(flags, ptm_open_peer_file(master, tty, flags));
|
| 623 | }
|
| 624 |
|
| 625 | static int pty_unix98_ioctl(struct tty_struct *tty,
|
| 626 | unsigned int cmd, unsigned long arg)
|
| 627 | {
|
| 628 | switch (cmd) {
|
| 629 | case TIOCSPTLCK:
|
| 630 | return pty_set_lock(tty, (int __user *)arg);
|
| 631 | case TIOCGPTLCK:
|
| 632 | return pty_get_lock(tty, (int __user *)arg);
|
| 633 | case TIOCPKT:
|
| 634 | return pty_set_pktmode(tty, (int __user *)arg);
|
| 635 | case TIOCGPKT:
|
| 636 | return pty_get_pktmode(tty, (int __user *)arg);
|
| 637 | case TIOCGPTN:
|
| 638 | return put_user(tty->index, (unsigned int __user *)arg);
|
| 639 | case TIOCSIG:
|
| 640 | return pty_signal(tty, (int) arg);
|
| 641 | }
|
| 642 |
|
| 643 | return -ENOIOCTLCMD;
|
| 644 | }
|
| 645 |
|
| 646 | #ifdef CONFIG_COMPAT
|
| 647 | static long pty_unix98_compat_ioctl(struct tty_struct *tty,
|
| 648 | unsigned int cmd, unsigned long arg)
|
| 649 | {
|
| 650 | |
| 651 | * PTY ioctls don't require any special translation between 32-bit and
|
| 652 | * 64-bit userspace, they are already compatible.
|
| 653 |
|
| 654 | return pty_unix98_ioctl(tty, cmd,
|
| 655 | cmd == TIOCSIG ? arg : (unsigned long)compat_ptr(arg));
|
| 656 | }
|
| 657 | #else
|
| 658 | #define pty_unix98_compat_ioctl NULL
|
| 659 | #endif
|
| 660 |
|
| 661 | |
| 662 | * ptm_unix98_lookup - find a pty master
|
| 663 | * @driver: ptm driver
|
| 664 | * @file: unused
|
| 665 | * @idx: tty index
|
| 666 | *
|
| 667 | * Look up a pty master device. Called under the tty_mutex for now.
|
| 668 | * This provides our locking.
|
| 669 |
|
| 670 |
|
| 671 | static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
|
| 672 | struct file *file, int idx)
|
| 673 | {
|
| 674 |
|
| 675 | return ERR_PTR(-EIO);
|
| 676 | }
|
| 677 |
|
| 678 | |
| 679 | * pts_unix98_lookup - find a pty slave
|
| 680 | * @driver: pts driver
|
| 681 | * @file: file pointer to tty
|
| 682 | * @idx: tty index
|
| 683 | *
|
| 684 | * Look up a pty master device. Called under the tty_mutex for now.
|
| 685 | * This provides our locking for the tty pointer.
|
| 686 |
|
| 687 |
|
| 688 | static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
|
| 689 | struct file *file, int idx)
|
| 690 | {
|
| 691 | guard(mutex)(&devpts_mutex);
|
| 692 |
|
| 693 | return devpts_get_priv(file->f_path.dentry) ? : ERR_PTR(-EIO);
|
| 694 | }
|
| 695 |
|
| 696 | static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
|
| 697 | {
|
| 698 | return pty_common_install(driver, tty, false);
|
| 699 | }
|
| 700 |
|
| 701 |
|
| 702 | static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
|
| 703 | {
|
| 704 | struct pts_fs_info *fsi;
|
| 705 |
|
| 706 | if (tty->driver->subtype == PTY_TYPE_MASTER)
|
| 707 | fsi = tty->driver_data;
|
| 708 | else
|
| 709 | fsi = tty->link->driver_data;
|
| 710 |
|
| 711 | if (fsi) {
|
| 712 | devpts_kill_index(fsi, tty->index);
|
| 713 | devpts_release(fsi);
|
| 714 | }
|
| 715 | }
|
| 716 |
|
| 717 | static void pty_show_fdinfo(struct tty_struct *tty, struct seq_file *m)
|
| 718 | {
|
| 719 | seq_printf(m, "tty-index:\t%d\n", tty->index);
|
| 720 | }
|
| 721 |
|
| 722 | static const struct tty_operations ptm_unix98_ops = {
|
| 723 | .lookup = ptm_unix98_lookup,
|
| 724 | .install = pty_unix98_install,
|
| 725 | .remove = pty_unix98_remove,
|
| 726 | .open = pty_open,
|
| 727 | .close = pty_close,
|
| 728 | .write = pty_write,
|
| 729 | .write_room = pty_write_room,
|
| 730 | .flush_buffer = pty_flush_buffer,
|
| 731 | .unthrottle = pty_unthrottle,
|
| 732 | .ioctl = pty_unix98_ioctl,
|
| 733 | .compat_ioctl = pty_unix98_compat_ioctl,
|
| 734 | .resize = pty_resize,
|
| 735 | .cleanup = pty_cleanup,
|
| 736 | .show_fdinfo = pty_show_fdinfo,
|
| 737 | };
|
| 738 |
|
| 739 | static const struct tty_operations pty_unix98_ops = {
|
| 740 | .lookup = pts_unix98_lookup,
|
| 741 | .install = pty_unix98_install,
|
| 742 | .remove = pty_unix98_remove,
|
| 743 | .open = pty_open,
|
| 744 | .close = pty_close,
|
| 745 | .write = pty_write,
|
| 746 | .write_room = pty_write_room,
|
| 747 | .flush_buffer = pty_flush_buffer,
|
| 748 | .unthrottle = pty_unthrottle,
|
| 749 | .set_termios = pty_set_termios,
|
| 750 | .start = pty_start,
|
| 751 | .stop = pty_stop,
|
| 752 | .cleanup = pty_cleanup,
|
| 753 | };
|
| 754 |
|
| 755 | |
| 756 | * ptmx_open - open a unix 98 pty master
|
| 757 | * @inode: inode of device file
|
| 758 | * @filp: file pointer to tty
|
| 759 | *
|
| 760 | * Allocate a unix98 pty master device from the ptmx driver.
|
| 761 | *
|
| 762 | * Locking: tty_mutex protects the init_dev work. tty->count should
|
| 763 | * protect the rest.
|
| 764 | * allocated_ptys_lock handles the list of free pty numbers
|
| 765 |
|
| 766 |
|
| 767 | static int ptmx_open(struct inode *inode, struct file *filp)
|
| 768 | {
|
| 769 | struct pts_fs_info *fsi;
|
| 770 | struct tty_struct *tty;
|
| 771 | struct dentry *dentry;
|
| 772 | int retval;
|
| 773 | int index;
|
| 774 |
|
| 775 | nonseekable_open(inode, filp);
|
| 776 |
|
| 777 |
|
| 778 | file_set_fsnotify_mode(filp, FMODE_NONOTIFY);
|
| 779 |
|
| 780 | retval = tty_alloc_file(filp);
|
| 781 | if (retval)
|
| 782 | return retval;
|
| 783 |
|
| 784 | fsi = devpts_acquire(filp);
|
| 785 | if (IS_ERR(fsi)) {
|
| 786 | retval = PTR_ERR(fsi);
|
| 787 | goto out_free_file;
|
| 788 | }
|
| 789 |
|
| 790 |
|
| 791 | scoped_guard(mutex, &devpts_mutex)
|
| 792 | index = devpts_new_index(fsi);
|
| 793 |
|
| 794 | retval = index;
|
| 795 | if (index < 0)
|
| 796 | goto out_put_fsi;
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 | scoped_guard(mutex, &tty_mutex)
|
| 801 | tty = tty_init_dev(ptm_driver, index);
|
| 802 |
|
| 803 | retval = PTR_ERR(tty);
|
| 804 | if (IS_ERR(tty))
|
| 805 | goto out;
|
| 806 |
|
| 807 | |
| 808 | * From here on out, the tty is "live", and the index and
|
| 809 | * fsi will be killed/put by the tty_release()
|
| 810 |
|
| 811 | set_bit(TTY_PTY_LOCK, &tty->flags);
|
| 812 | tty->driver_data = fsi;
|
| 813 |
|
| 814 | tty_add_file(tty, filp);
|
| 815 |
|
| 816 | dentry = devpts_pty_new(fsi, index, tty->link);
|
| 817 | if (IS_ERR(dentry)) {
|
| 818 | retval = PTR_ERR(dentry);
|
| 819 | goto err_release;
|
| 820 | }
|
| 821 | tty->link->driver_data = dentry;
|
| 822 |
|
| 823 | retval = ptm_driver->ops->open(tty, filp);
|
| 824 | if (retval)
|
| 825 | goto err_release;
|
| 826 |
|
| 827 | tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
|
| 828 |
|
| 829 | tty_unlock(tty);
|
| 830 | return 0;
|
| 831 | err_release:
|
| 832 | tty_unlock(tty);
|
| 833 | |
| 834 | tty_release(inode, filp);
|
| 835 | return retval;
|
| 836 | out:
|
| 837 | devpts_kill_index(fsi, index);
|
| 838 | out_put_fsi:
|
| 839 | devpts_release(fsi);
|
| 840 | out_free_file:
|
| 841 | tty_free_file(filp);
|
| 842 | return retval;
|
| 843 | }
|
| 844 |
|
| 845 | static struct file_operations ptmx_fops __ro_after_init;
|
| 846 |
|
| 847 | static void __init unix98_pty_init(void)
|
| 848 | {
|
| 849 | ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
|
| 850 | TTY_DRIVER_RESET_TERMIOS |
|
| 851 | TTY_DRIVER_REAL_RAW |
|
| 852 | TTY_DRIVER_DYNAMIC_DEV |
|
| 853 | TTY_DRIVER_DEVPTS_MEM |
|
| 854 | TTY_DRIVER_DYNAMIC_ALLOC |
|
| 855 | TTY_DRIVER_NO_WORKQUEUE);
|
| 856 | if (IS_ERR(ptm_driver))
|
| 857 | panic("Couldn't allocate Unix98 ptm driver");
|
| 858 | pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
|
| 859 | TTY_DRIVER_RESET_TERMIOS |
|
| 860 | TTY_DRIVER_REAL_RAW |
|
| 861 | TTY_DRIVER_DYNAMIC_DEV |
|
| 862 | TTY_DRIVER_DEVPTS_MEM |
|
| 863 | TTY_DRIVER_DYNAMIC_ALLOC |
|
| 864 | TTY_DRIVER_NO_WORKQUEUE);
|
| 865 | if (IS_ERR(pts_driver))
|
| 866 | panic("Couldn't allocate Unix98 pts driver");
|
| 867 |
|
| 868 | ptm_driver->driver_name = "pty_master";
|
| 869 | ptm_driver->name = "ptm";
|
| 870 | ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
|
| 871 | ptm_driver->minor_start = 0;
|
| 872 | ptm_driver->type = TTY_DRIVER_TYPE_PTY;
|
| 873 | ptm_driver->subtype = PTY_TYPE_MASTER;
|
| 874 | ptm_driver->init_termios = tty_std_termios;
|
| 875 | ptm_driver->init_termios.c_iflag = 0;
|
| 876 | ptm_driver->init_termios.c_oflag = 0;
|
| 877 | ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
|
| 878 | ptm_driver->init_termios.c_lflag = 0;
|
| 879 | ptm_driver->init_termios.c_ispeed = 38400;
|
| 880 | ptm_driver->init_termios.c_ospeed = 38400;
|
| 881 | ptm_driver->other = pts_driver;
|
| 882 | tty_set_operations(ptm_driver, &ptm_unix98_ops);
|
| 883 |
|
| 884 | pts_driver->driver_name = "pty_slave";
|
| 885 | pts_driver->name = "pts";
|
| 886 | pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
|
| 887 | pts_driver->minor_start = 0;
|
| 888 | pts_driver->type = TTY_DRIVER_TYPE_PTY;
|
| 889 | pts_driver->subtype = PTY_TYPE_SLAVE;
|
| 890 | pts_driver->init_termios = tty_std_termios;
|
| 891 | pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
|
| 892 | pts_driver->init_termios.c_ispeed = 38400;
|
| 893 | pts_driver->init_termios.c_ospeed = 38400;
|
| 894 | pts_driver->other = ptm_driver;
|
| 895 | tty_set_operations(pts_driver, &pty_unix98_ops);
|
| 896 |
|
| 897 | if (tty_register_driver(ptm_driver))
|
| 898 | panic("Couldn't register Unix98 ptm driver");
|
| 899 | if (tty_register_driver(pts_driver))
|
| 900 | panic("Couldn't register Unix98 pts driver");
|
| 901 |
|
| 902 |
|
| 903 | tty_default_fops(&ptmx_fops);
|
| 904 | ptmx_fops.open = ptmx_open;
|
| 905 |
|
| 906 | cdev_init(&ptmx_cdev, &ptmx_fops);
|
| 907 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
|
| 908 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
|
| 909 | panic("Couldn't register /dev/ptmx driver");
|
| 910 | device_create(&tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
|
| 911 | }
|
| 912 |
|
| 913 | #else
|
| 914 | static inline void unix98_pty_init(void) { }
|
| 915 | #endif
|
| 916 |
|
| 917 | static int __init pty_init(void)
|
| 918 | {
|
| 919 | legacy_pty_init();
|
| 920 | unix98_pty_init();
|
| 921 | return 0;
|
| 922 | }
|
| 923 | device_initcall(pty_init);
|