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