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