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