xref: /linux/drivers/tty/pty.c (revision 13532b5186a7aa4dfd9885355c6af7562b75dd7f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
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 	/* Review - krefs on tty_link ?? */
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 /* Set the lock flag on a pty */
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 /* Set the packet mode on a pty */
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 /* Get the packet mode of a pty */
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 /* Send a signal to the slave */
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 	/* See if packet mode change of state. */
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 	/* For a PTY we need to lock the tty side */
288 	guard(mutex)(&tty->winsize_mutex);
289 	if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
290 		return 0;
291 
292 	/* Signal the foreground process group of both ptys */
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;	/* Never used so will go away soon */
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 	/* Opening the slave first has always returned -EIO */
364 	if (driver->subtype != PTY_TYPE_MASTER)
365 		return -EIO;
366 
367 	ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
368 	ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
369 	if (!ports[0] || !ports[1])
370 		goto err;
371 	if (!try_module_get(driver->other->owner)) {
372 		/* This cannot in fact currently happen */
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 		/* We always use new tty termios data so we can do this
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 	/* Establish the links in both directions */
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 /* Traditional BSD devices */
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: /* Set PT Lock (disallow slave open) */
454 		return pty_set_lock(tty, (int __user *) arg);
455 	case TIOCGPTLCK: /* Get PT Lock status */
456 		return pty_get_lock(tty, (int __user *)arg);
457 	case TIOCPKT: /* Set PT packet mode */
458 		return pty_set_pktmode(tty, (int __user *)arg);
459 	case TIOCGPKT: /* Get PT packet mode */
460 		return pty_get_pktmode(tty, (int __user *)arg);
461 	case TIOCSIG:    /* Send signal to other side of pty */
462 		return pty_signal(tty, (int) arg);
463 	case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
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 	if (IS_ERR(pty_driver))
537 		panic("Couldn't allocate pty driver");
538 
539 	pty_slave_driver = tty_alloc_driver(legacy_count,
540 			TTY_DRIVER_RESET_TERMIOS |
541 			TTY_DRIVER_REAL_RAW |
542 			TTY_DRIVER_DYNAMIC_ALLOC);
543 	if (IS_ERR(pty_slave_driver))
544 		panic("Couldn't allocate pty slave driver");
545 
546 	pty_driver->driver_name = "pty_master";
547 	pty_driver->name = "pty";
548 	pty_driver->major = PTY_MASTER_MAJOR;
549 	pty_driver->minor_start = 0;
550 	pty_driver->type = TTY_DRIVER_TYPE_PTY;
551 	pty_driver->subtype = PTY_TYPE_MASTER;
552 	pty_driver->init_termios = tty_std_termios;
553 	pty_driver->init_termios.c_iflag = 0;
554 	pty_driver->init_termios.c_oflag = 0;
555 	pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
556 	pty_driver->init_termios.c_lflag = 0;
557 	pty_driver->init_termios.c_ispeed = 38400;
558 	pty_driver->init_termios.c_ospeed = 38400;
559 	pty_driver->other = pty_slave_driver;
560 	tty_set_operations(pty_driver, &master_pty_ops_bsd);
561 
562 	pty_slave_driver->driver_name = "pty_slave";
563 	pty_slave_driver->name = "ttyp";
564 	pty_slave_driver->major = PTY_SLAVE_MAJOR;
565 	pty_slave_driver->minor_start = 0;
566 	pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
567 	pty_slave_driver->subtype = PTY_TYPE_SLAVE;
568 	pty_slave_driver->init_termios = tty_std_termios;
569 	pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
570 	pty_slave_driver->init_termios.c_ispeed = 38400;
571 	pty_slave_driver->init_termios.c_ospeed = 38400;
572 	pty_slave_driver->other = pty_driver;
573 	tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
574 
575 	if (tty_register_driver(pty_driver))
576 		panic("Couldn't register pty driver");
577 	if (tty_register_driver(pty_slave_driver))
578 		panic("Couldn't register pty slave driver");
579 }
580 #else
581 static inline void legacy_pty_init(void) { }
582 #endif
583 
584 /* Unix98 devices */
585 #ifdef CONFIG_UNIX98_PTYS
586 static struct cdev ptmx_cdev;
587 
588 /**
589  *	ptm_open_peer - open the peer of a pty
590  *	@master: the open struct file of the ptmx device node
591  *	@tty: the master of the pty being opened
592  *	@flags: the flags for open
593  *
594  *	Provide a race free way for userspace to open the slave end of a pty
595  *	(where they have the master fd and cannot access or trust the mount
596  *	namespace /dev/pts was mounted inside).
597  */
598 int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
599 {
600 	int fd;
601 	struct file *filp;
602 	int retval = -EINVAL;
603 	struct path path;
604 
605 	if (tty->driver != ptm_driver)
606 		return -EIO;
607 
608 	fd = get_unused_fd_flags(flags);
609 	if (fd < 0) {
610 		retval = fd;
611 		goto err;
612 	}
613 
614 	/* Compute the slave's path */
615 	path.mnt = devpts_mntget(master, tty->driver_data);
616 	if (IS_ERR(path.mnt)) {
617 		retval = PTR_ERR(path.mnt);
618 		goto err_put;
619 	}
620 	path.dentry = tty->link->driver_data;
621 
622 	filp = dentry_open(&path, flags, current_cred());
623 	mntput(path.mnt);
624 	if (IS_ERR(filp)) {
625 		retval = PTR_ERR(filp);
626 		goto err_put;
627 	}
628 
629 	fd_install(fd, filp);
630 	return fd;
631 
632 err_put:
633 	put_unused_fd(fd);
634 err:
635 	return retval;
636 }
637 
638 static int pty_unix98_ioctl(struct tty_struct *tty,
639 			    unsigned int cmd, unsigned long arg)
640 {
641 	switch (cmd) {
642 	case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
643 		return pty_set_lock(tty, (int __user *)arg);
644 	case TIOCGPTLCK: /* Get PT Lock status */
645 		return pty_get_lock(tty, (int __user *)arg);
646 	case TIOCPKT: /* Set PT packet mode */
647 		return pty_set_pktmode(tty, (int __user *)arg);
648 	case TIOCGPKT: /* Get PT packet mode */
649 		return pty_get_pktmode(tty, (int __user *)arg);
650 	case TIOCGPTN: /* Get PT Number */
651 		return put_user(tty->index, (unsigned int __user *)arg);
652 	case TIOCSIG:    /* Send signal to other side of pty */
653 		return pty_signal(tty, (int) arg);
654 	}
655 
656 	return -ENOIOCTLCMD;
657 }
658 
659 #ifdef CONFIG_COMPAT
660 static long pty_unix98_compat_ioctl(struct tty_struct *tty,
661 				 unsigned int cmd, unsigned long arg)
662 {
663 	/*
664 	 * PTY ioctls don't require any special translation between 32-bit and
665 	 * 64-bit userspace, they are already compatible.
666 	 */
667 	return pty_unix98_ioctl(tty, cmd,
668 		cmd == TIOCSIG ? arg : (unsigned long)compat_ptr(arg));
669 }
670 #else
671 #define pty_unix98_compat_ioctl NULL
672 #endif
673 
674 /**
675  *	ptm_unix98_lookup	-	find a pty master
676  *	@driver: ptm driver
677  *	@file: unused
678  *	@idx: tty index
679  *
680  *	Look up a pty master device. Called under the tty_mutex for now.
681  *	This provides our locking.
682  */
683 
684 static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
685 		struct file *file, int idx)
686 {
687 	/* Master must be open via /dev/ptmx */
688 	return ERR_PTR(-EIO);
689 }
690 
691 /**
692  *	pts_unix98_lookup	-	find a pty slave
693  *	@driver: pts driver
694  *	@file: file pointer to tty
695  *	@idx: tty index
696  *
697  *	Look up a pty master device. Called under the tty_mutex for now.
698  *	This provides our locking for the tty pointer.
699  */
700 
701 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
702 		struct file *file, int idx)
703 {
704 	guard(mutex)(&devpts_mutex);
705 	/* Master must be open before slave */
706 	return devpts_get_priv(file->f_path.dentry) ? : ERR_PTR(-EIO);
707 }
708 
709 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
710 {
711 	return pty_common_install(driver, tty, false);
712 }
713 
714 /* this is called once with whichever end is closed last */
715 static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
716 {
717 	struct pts_fs_info *fsi;
718 
719 	if (tty->driver->subtype == PTY_TYPE_MASTER)
720 		fsi = tty->driver_data;
721 	else
722 		fsi = tty->link->driver_data;
723 
724 	if (fsi) {
725 		devpts_kill_index(fsi, tty->index);
726 		devpts_release(fsi);
727 	}
728 }
729 
730 static void pty_show_fdinfo(struct tty_struct *tty, struct seq_file *m)
731 {
732 	seq_printf(m, "tty-index:\t%d\n", tty->index);
733 }
734 
735 static const struct tty_operations ptm_unix98_ops = {
736 	.lookup = ptm_unix98_lookup,
737 	.install = pty_unix98_install,
738 	.remove = pty_unix98_remove,
739 	.open = pty_open,
740 	.close = pty_close,
741 	.write = pty_write,
742 	.write_room = pty_write_room,
743 	.flush_buffer = pty_flush_buffer,
744 	.unthrottle = pty_unthrottle,
745 	.ioctl = pty_unix98_ioctl,
746 	.compat_ioctl = pty_unix98_compat_ioctl,
747 	.resize = pty_resize,
748 	.cleanup = pty_cleanup,
749 	.show_fdinfo = pty_show_fdinfo,
750 };
751 
752 static const struct tty_operations pty_unix98_ops = {
753 	.lookup = pts_unix98_lookup,
754 	.install = pty_unix98_install,
755 	.remove = pty_unix98_remove,
756 	.open = pty_open,
757 	.close = pty_close,
758 	.write = pty_write,
759 	.write_room = pty_write_room,
760 	.flush_buffer = pty_flush_buffer,
761 	.unthrottle = pty_unthrottle,
762 	.set_termios = pty_set_termios,
763 	.start = pty_start,
764 	.stop = pty_stop,
765 	.cleanup = pty_cleanup,
766 };
767 
768 /**
769  *	ptmx_open		-	open a unix 98 pty master
770  *	@inode: inode of device file
771  *	@filp: file pointer to tty
772  *
773  *	Allocate a unix98 pty master device from the ptmx driver.
774  *
775  *	Locking: tty_mutex protects the init_dev work. tty->count should
776  *		protect the rest.
777  *		allocated_ptys_lock handles the list of free pty numbers
778  */
779 
780 static int ptmx_open(struct inode *inode, struct file *filp)
781 {
782 	struct pts_fs_info *fsi;
783 	struct tty_struct *tty;
784 	struct dentry *dentry;
785 	int retval;
786 	int index;
787 
788 	nonseekable_open(inode, filp);
789 
790 	/* We refuse fsnotify events on ptmx, since it's a shared resource */
791 	file_set_fsnotify_mode(filp, FMODE_NONOTIFY);
792 
793 	retval = tty_alloc_file(filp);
794 	if (retval)
795 		return retval;
796 
797 	fsi = devpts_acquire(filp);
798 	if (IS_ERR(fsi)) {
799 		retval = PTR_ERR(fsi);
800 		goto out_free_file;
801 	}
802 
803 	/* find a device that is not in use. */
804 	scoped_guard(mutex, &devpts_mutex)
805 		index = devpts_new_index(fsi);
806 
807 	retval = index;
808 	if (index < 0)
809 		goto out_put_fsi;
810 
811 
812 	/* The tty returned here is locked so we can safely drop the mutex */
813 	scoped_guard(mutex, &tty_mutex)
814 		tty = tty_init_dev(ptm_driver, index);
815 
816 	retval = PTR_ERR(tty);
817 	if (IS_ERR(tty))
818 		goto out;
819 
820 	/*
821 	 * From here on out, the tty is "live", and the index and
822 	 * fsi will be killed/put by the tty_release()
823 	 */
824 	set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
825 	tty->driver_data = fsi;
826 
827 	tty_add_file(tty, filp);
828 
829 	dentry = devpts_pty_new(fsi, index, tty->link);
830 	if (IS_ERR(dentry)) {
831 		retval = PTR_ERR(dentry);
832 		goto err_release;
833 	}
834 	tty->link->driver_data = dentry;
835 
836 	retval = ptm_driver->ops->open(tty, filp);
837 	if (retval)
838 		goto err_release;
839 
840 	tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
841 
842 	tty_unlock(tty);
843 	return 0;
844 err_release:
845 	tty_unlock(tty);
846 	// This will also put-ref the fsi
847 	tty_release(inode, filp);
848 	return retval;
849 out:
850 	devpts_kill_index(fsi, index);
851 out_put_fsi:
852 	devpts_release(fsi);
853 out_free_file:
854 	tty_free_file(filp);
855 	return retval;
856 }
857 
858 static struct file_operations ptmx_fops __ro_after_init;
859 
860 static void __init unix98_pty_init(void)
861 {
862 	ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
863 			TTY_DRIVER_RESET_TERMIOS |
864 			TTY_DRIVER_REAL_RAW |
865 			TTY_DRIVER_DYNAMIC_DEV |
866 			TTY_DRIVER_DEVPTS_MEM |
867 			TTY_DRIVER_DYNAMIC_ALLOC);
868 	if (IS_ERR(ptm_driver))
869 		panic("Couldn't allocate Unix98 ptm driver");
870 	pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
871 			TTY_DRIVER_RESET_TERMIOS |
872 			TTY_DRIVER_REAL_RAW |
873 			TTY_DRIVER_DYNAMIC_DEV |
874 			TTY_DRIVER_DEVPTS_MEM |
875 			TTY_DRIVER_DYNAMIC_ALLOC);
876 	if (IS_ERR(pts_driver))
877 		panic("Couldn't allocate Unix98 pts driver");
878 
879 	ptm_driver->driver_name = "pty_master";
880 	ptm_driver->name = "ptm";
881 	ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
882 	ptm_driver->minor_start = 0;
883 	ptm_driver->type = TTY_DRIVER_TYPE_PTY;
884 	ptm_driver->subtype = PTY_TYPE_MASTER;
885 	ptm_driver->init_termios = tty_std_termios;
886 	ptm_driver->init_termios.c_iflag = 0;
887 	ptm_driver->init_termios.c_oflag = 0;
888 	ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
889 	ptm_driver->init_termios.c_lflag = 0;
890 	ptm_driver->init_termios.c_ispeed = 38400;
891 	ptm_driver->init_termios.c_ospeed = 38400;
892 	ptm_driver->other = pts_driver;
893 	tty_set_operations(ptm_driver, &ptm_unix98_ops);
894 
895 	pts_driver->driver_name = "pty_slave";
896 	pts_driver->name = "pts";
897 	pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
898 	pts_driver->minor_start = 0;
899 	pts_driver->type = TTY_DRIVER_TYPE_PTY;
900 	pts_driver->subtype = PTY_TYPE_SLAVE;
901 	pts_driver->init_termios = tty_std_termios;
902 	pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
903 	pts_driver->init_termios.c_ispeed = 38400;
904 	pts_driver->init_termios.c_ospeed = 38400;
905 	pts_driver->other = ptm_driver;
906 	tty_set_operations(pts_driver, &pty_unix98_ops);
907 
908 	if (tty_register_driver(ptm_driver))
909 		panic("Couldn't register Unix98 ptm driver");
910 	if (tty_register_driver(pts_driver))
911 		panic("Couldn't register Unix98 pts driver");
912 
913 	/* Now create the /dev/ptmx special device */
914 	tty_default_fops(&ptmx_fops);
915 	ptmx_fops.open = ptmx_open;
916 
917 	cdev_init(&ptmx_cdev, &ptmx_fops);
918 	if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
919 	    register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
920 		panic("Couldn't register /dev/ptmx driver");
921 	device_create(&tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
922 }
923 
924 #else
925 static inline void unix98_pty_init(void) { }
926 #endif
927 
928 static int __init pty_init(void)
929 {
930 	legacy_pty_init();
931 	unix98_pty_init();
932 	return 0;
933 }
934 device_initcall(pty_init);
935