Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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