blob: 00099a8439d219184c131f5d24c1e60fa2c20285 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Alan Coxfe9cd962008-10-13 10:43:38 +010010#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/interrupt.h>
13#include <linux/tty.h>
14#include <linux/tty_flip.h>
15#include <linux/fcntl.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/string.h>
18#include <linux/major.h>
19#include <linux/mm.h>
20#include <linux/init.h>
Alan Coxd81ed102008-10-13 10:41:42 +010021#include <linux/device.h>
Alan Coxfe9cd962008-10-13 10:43:38 +010022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/bitops.h>
24#include <linux/devpts_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Alan Coxd739e652012-05-03 22:22:09 +010026#include <linux/mutex.h>
Peter Hurley01adc802014-09-10 15:06:32 -040027#include <linux/poll.h>
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +100028#include <linux/mount.h>
29#include <linux/file.h>
30#include <linux/ioctl.h>
Al Viro50f45322018-09-12 20:53:46 -040031#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Peter Hurleyd6847792015-07-12 22:49:13 -040033#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
Alan Coxfe9cd962008-10-13 10:43:38 +010039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#ifdef CONFIG_UNIX98_PTYS
Roel Kluinda2bdf92009-01-07 18:09:15 -080041static struct tty_driver *ptm_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static struct tty_driver *pts_driver;
Alan Coxd739e652012-05-03 22:22:09 +010043static DEFINE_MUTEX(devpts_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#endif
45
Alan Coxfe9cd962008-10-13 10:43:38 +010046static void pty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Alan Coxfe9cd962008-10-13 10:43:38 +010048 BUG_ON(!tty);
49 if (tty->driver->subtype == PTY_TYPE_MASTER)
50 WARN_ON(tty->count > 1);
51 else {
Peter Hurley18900ca2016-04-09 17:06:48 -070052 if (tty_io_error(tty))
Peter Hurley699390352013-01-30 12:43:50 -050053 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (tty->count > 2)
55 return;
56 }
Peter Hurley699390352013-01-30 12:43:50 -050057 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 wake_up_interruptible(&tty->read_wait);
59 wake_up_interruptible(&tty->write_wait);
Peter Hurley4ed60fc2014-10-16 15:33:28 -040060 spin_lock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 tty->packet = 0;
Peter Hurley4ed60fc2014-10-16 15:33:28 -040062 spin_unlock_irq(&tty->ctrl_lock);
Alan Cox89c8d912012-08-08 16:30:13 +010063 /* Review - krefs on tty_link ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!tty->link)
65 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -080067 wake_up_interruptible(&tty->link->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 wake_up_interruptible(&tty->link->write_wait);
69 if (tty->driver->subtype == PTY_TYPE_MASTER) {
70 set_bit(TTY_OTHER_CLOSED, &tty->flags);
Greg Kroah-Hartmance1000d2012-02-24 13:56:36 -080071#ifdef CONFIG_UNIX98_PTYS
Alan Coxd739e652012-05-03 22:22:09 +010072 if (tty->driver == ptm_driver) {
Cong Dingb9f80332013-01-12 05:01:22 +010073 mutex_lock(&devpts_mutex);
Eric W. Biederman311fc652017-08-24 15:13:29 -050074 if (tty->link->driver_data)
75 devpts_pty_kill(tty->link->driver_data);
Cong Dingb9f80332013-01-12 05:01:22 +010076 mutex_unlock(&devpts_mutex);
Alan Coxd739e652012-05-03 22:22:09 +010077 }
Greg Kroah-Hartmance1000d2012-02-24 13:56:36 -080078#endif
Arnd Bergmann11dbf202010-06-18 14:58:07 +020079 tty_vhangup(tty->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
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
Alan Coxfe9cd962008-10-13 10:43:38 +010087 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * 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 */
Alan Coxfe9cd962008-10-13 10:43:38 +010093static void pty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Alan Coxd945cb92009-07-07 16:39:41 +010095 tty_wakeup(tty->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 set_bit(TTY_THROTTLED, &tty->flags);
97}
98
Alan Coxd945cb92009-07-07 16:39:41 +010099/**
Alan Coxd945cb92009-07-07 16:39:41 +0100100 * 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
Linus Torvaldsac89a912009-09-05 13:27:10 -0700111static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct tty_struct *to = tty->link;
DaeRyong Jeongb6da31b2018-05-01 00:27:04 +0900114 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Alan Coxd945cb92009-07-07 16:39:41 +0100116 if (tty->stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118
Alan Coxd945cb92009-07-07 16:39:41 +0100119 if (c > 0) {
DaeRyong Jeongb6da31b2018-05-01 00:27:04 +0900120 spin_lock_irqsave(&to->port->lock, flags);
Alan Coxd945cb92009-07-07 16:39:41 +0100121 /* Stuff the data into the input queue of the other end */
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100122 c = tty_insert_flip_string(to->port, buf, c);
Alan Coxd945cb92009-07-07 16:39:41 +0100123 /* And shovel */
Peter Hurley5ede5252013-07-24 08:29:57 -0400124 if (c)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100125 tty_flip_buffer_push(to->port);
DaeRyong Jeongb6da31b2018-05-01 00:27:04 +0900126 spin_unlock_irqrestore(&to->port->lock, flags);
Alan Cox762faae2009-06-16 17:01:13 +0100127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return c;
129}
130
Alan Coxd945cb92009-07-07 16:39:41 +0100131/**
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int pty_write_room(struct tty_struct *tty)
140{
Linus Torvalds85dfd81d2009-08-10 13:21:19 -0700141 if (tty->stopped)
142 return 0;
Peter Hurleyc81622a2015-01-16 15:22:14 -0500143 return tty_buffer_space_avail(tty->link->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Alan Coxd945cb92009-07-07 16:39:41 +0100146/**
147 * pty_chars_in_buffer - characters currently in our tx queue
148 * @tty: our tty
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *
Alan Coxd945cb92009-07-07 16:39:41 +0100150 * Report how much we have in the transmit queue. As everything is
151 * instantly at the other end this is easy to implement.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Alan Coxd945cb92009-07-07 16:39:41 +0100153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int pty_chars_in_buffer(struct tty_struct *tty)
155{
Alan Coxd945cb92009-07-07 16:39:41 +0100156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/* Set the lock flag on a pty */
Alan Coxfe9cd962008-10-13 10:43:38 +0100160static int pty_set_lock(struct tty_struct *tty, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 int val;
Alan Coxfe9cd962008-10-13 10:43:38 +0100163 if (get_user(val, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 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
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +0400172static 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
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400178/* Set the packet mode on a pty */
179static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
180{
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400181 int pktmode;
182
183 if (get_user(pktmode, arg))
184 return -EFAULT;
185
Peter Hurley6054c16e2014-10-16 15:33:25 -0400186 spin_lock_irq(&tty->ctrl_lock);
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400187 if (pktmode) {
188 if (!tty->packet) {
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400189 tty->link->ctrl_status = 0;
Peter Hurley2622d732014-10-16 15:33:27 -0400190 smp_mb();
191 tty->packet = 1;
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400192 }
193 } else
194 tty->packet = 0;
Peter Hurley6054c16e2014-10-16 15:33:25 -0400195 spin_unlock_irq(&tty->ctrl_lock);
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400196
197 return 0;
198}
199
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +0400200/* Get the packet mode of a pty */
201static 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
hyc@symas.com26df6d12010-06-22 10:14:49 -0700207/* Send a signal to the slave */
208static int pty_signal(struct tty_struct *tty, int sig)
209{
hyc@symas.com26df6d12010-06-22 10:14:49 -0700210 struct pid *pgrp;
211
Peter Hurley37480a02015-01-19 13:05:03 -0500212 if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
213 return -EINVAL;
214
hyc@symas.com26df6d12010-06-22 10:14:49 -0700215 if (tty->link) {
Peter Hurley5b239542014-10-16 14:59:45 -0400216 pgrp = tty_get_pgrp(tty->link);
217 if (pgrp)
218 kill_pgrp(pgrp, sig, 1);
hyc@symas.com26df6d12010-06-22 10:14:49 -0700219 put_pid(pgrp);
220 }
221 return 0;
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static void pty_flush_buffer(struct tty_struct *tty)
225{
226 struct tty_struct *to = tty->link;
Alan Coxfe9cd962008-10-13 10:43:38 +0100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!to)
229 return;
Peter Hurley1d1d14d2015-01-17 15:42:05 -0500230
Wang YanQing77dae612017-02-22 19:37:08 +0800231 tty_buffer_flush(to, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (to->packet) {
Peter Hurley6054c16e2014-10-16 15:33:25 -0400233 spin_lock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
235 wake_up_interruptible(&to->read_wait);
Peter Hurley6054c16e2014-10-16 15:33:25 -0400236 spin_unlock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238}
239
Alan Coxfe9cd962008-10-13 10:43:38 +0100240static int pty_open(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!tty || !tty->link)
Peter Hurley7c61c3d2013-06-13 15:56:37 -0400243 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
246 goto out;
247 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
248 goto out;
Peter Hurley80cace72013-01-30 12:43:52 -0500249 if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto out;
251
Peter Hurley699390352013-01-30 12:43:50 -0500252 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
254 set_bit(TTY_THROTTLED, &tty->flags);
Peter Hurley7c61c3d2013-06-13 15:56:37 -0400255 return 0;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257out:
Peter Hurley7c61c3d2013-06-13 15:56:37 -0400258 set_bit(TTY_IO_ERROR, &tty->flags);
259 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Alan Coxfe9cd962008-10-13 10:43:38 +0100262static void pty_set_termios(struct tty_struct *tty,
263 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Peter Hurleydbfcd852014-10-16 15:33:23 -0400265 /* See if packet mode change of state. */
266 if (tty->link && tty->link->packet) {
Peter Hurley9db276f2016-01-10 20:36:15 -0800267 int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
Peter Hurleydbfcd852014-10-16 15:33:23 -0400268 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) {
Peter Hurley4d8c1df2014-10-16 15:33:24 -0400275 spin_lock_irq(&tty->ctrl_lock);
Peter Hurleydbfcd852014-10-16 15:33:23 -0400276 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;
Peter Hurley4d8c1df2014-10-16 15:33:24 -0400285 spin_unlock_irq(&tty->ctrl_lock);
Peter Hurleydbfcd852014-10-16 15:33:23 -0400286 wake_up_interruptible(&tty->link->read_wait);
287 }
288 }
289
Alan Coxadc8d742012-07-14 15:31:47 +0100290 tty->termios.c_cflag &= ~(CSIZE | PARENB);
291 tty->termios.c_cflag |= (CS8 | CREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Alan Coxfc6f6232009-01-02 13:43:17 +0000294/**
295 * pty_do_resize - resize event
296 * @tty: tty being resized
Alan Coxc774bda2009-01-11 19:46:49 +0000297 * @ws: window size being set.
Alan Coxfc6f6232009-01-02 13:43:17 +0000298 *
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800299 * Update the termios variables and send the necessary signals to
Alan Coxfc6f6232009-01-02 13:43:17 +0000300 * peform a terminal resize correctly
301 */
302
Josh Triplett159a8e92012-11-18 21:27:40 -0800303static int pty_resize(struct tty_struct *tty, struct winsize *ws)
Alan Coxfc6f6232009-01-02 13:43:17 +0000304{
305 struct pid *pgrp, *rpgrp;
Alan Coxfc6f6232009-01-02 13:43:17 +0000306 struct tty_struct *pty = tty->link;
307
308 /* For a PTY we need to lock the tty side */
Peter Hurleydee4a0b2013-07-24 16:43:51 -0400309 mutex_lock(&tty->winsize_mutex);
Alan Coxfc6f6232009-01-02 13:43:17 +0000310 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
311 goto done;
312
Peter Hurley5b239542014-10-16 14:59:45 -0400313 /* Signal the foreground process group of both ptys */
314 pgrp = tty_get_pgrp(tty);
315 rpgrp = tty_get_pgrp(pty);
Alan Coxfc6f6232009-01-02 13:43:17 +0000316
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 */
327done:
Peter Hurleydee4a0b2013-07-24 16:43:51 -0400328 mutex_unlock(&tty->winsize_mutex);
Alan Coxfc6f6232009-01-02 13:43:17 +0000329 return 0;
330}
331
Alan Coxd1552552012-07-27 18:02:54 +0100332/**
Peter Hurley01adc802014-09-10 15:06:32 -0400333 * 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 */
342static void pty_start(struct tty_struct *tty)
343{
344 unsigned long flags;
345
Peter Hurley01adc802014-09-10 15:06:32 -0400346 if (tty->link && tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400347 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurley01adc802014-09-10 15:06:32 -0400348 tty->ctrl_status &= ~TIOCPKT_STOP;
349 tty->ctrl_status |= TIOCPKT_START;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400350 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800351 wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
Peter Hurley01adc802014-09-10 15:06:32 -0400352 }
Peter Hurley01adc802014-09-10 15:06:32 -0400353}
354
355static void pty_stop(struct tty_struct *tty)
356{
357 unsigned long flags;
358
Peter Hurley01adc802014-09-10 15:06:32 -0400359 if (tty->link && tty->link->packet) {
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400360 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurley01adc802014-09-10 15:06:32 -0400361 tty->ctrl_status &= ~TIOCPKT_START;
362 tty->ctrl_status |= TIOCPKT_STOP;
Peter Hurley54e8e5f2014-10-16 15:33:26 -0400363 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800364 wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
Peter Hurley01adc802014-09-10 15:06:32 -0400365 }
Peter Hurley01adc802014-09-10 15:06:32 -0400366}
367
368/**
Alan Coxd1552552012-07-27 18:02:54 +0100369 * pty_common_install - set up the pty pair
370 * @driver: the pty driver
371 * @tty: the tty being instantiated
Rasmus Villemoes2c964a22014-07-10 21:01:22 +0200372 * @legacy: true if this is BSD style
Alan Coxd1552552012-07-27 18:02:54 +0100373 *
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 */
Jiri Slaby5d249bc2012-06-04 13:35:29 +0200379static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
380 bool legacy)
Alan Coxbf970ee2008-10-13 10:42:39 +0100381{
382 struct tty_struct *o_tty;
Jiri Slabyd03702a2012-06-04 13:35:30 +0200383 struct tty_port *ports[2];
Alan Coxbf970ee2008-10-13 10:42:39 +0100384 int idx = tty->index;
Jiri Slaby5d249bc2012-06-04 13:35:29 +0200385 int retval = -ENOMEM;
Alan Coxbf970ee2008-10-13 10:42:39 +0100386
Peter Hurley55199ea2014-11-05 12:12:49 -0500387 /* Opening the slave first has always returned -EIO */
388 if (driver->subtype != PTY_TYPE_MASTER)
389 return -EIO;
390
Jiri Slabyd03702a2012-06-04 13:35:30 +0200391 ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
392 ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
Jiri Slaby6f9ea7a2012-08-07 21:47:26 +0200393 if (!ports[0] || !ports[1])
Rasmus Villemoes2c964a22014-07-10 21:01:22 +0200394 goto err;
Alan Coxbf970ee2008-10-13 10:42:39 +0100395 if (!try_module_get(driver->other->owner)) {
396 /* This cannot in fact currently happen */
Rasmus Villemoes2c964a22014-07-10 21:01:22 +0200397 goto err;
Alan Coxbf970ee2008-10-13 10:42:39 +0100398 }
Rasmus Villemoes2c964a22014-07-10 21:01:22 +0200399 o_tty = alloc_tty_struct(driver->other, idx);
400 if (!o_tty)
401 goto err_put_module;
Alan Coxbf970ee2008-10-13 10:42:39 +0100402
Peter Hurley2febdb62014-11-05 12:13:02 -0500403 tty_set_lock_subclass(o_tty);
Peter Hurleyd2b6f442015-01-17 15:42:06 -0500404 lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
Peter Hurley2febdb62014-11-05 12:13:02 -0500405
Jiri Slaby5d249bc2012-06-04 13:35:29 +0200406 if (legacy) {
407 /* We always use new tty termios data so we can do this
408 the easy way .. */
Peter Hurleya3123fd2016-01-09 21:13:48 -0800409 tty_init_termios(tty);
410 tty_init_termios(o_tty);
Jiri Slaby5d249bc2012-06-04 13:35:29 +0200411
412 driver->other->ttys[idx] = o_tty;
413 driver->ttys[idx] = tty;
414 } else {
Alan Coxadc8d742012-07-14 15:31:47 +0100415 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;
Jiri Slaby5d249bc2012-06-04 13:35:29 +0200419 }
Alan Coxfe9cd962008-10-13 10:43:38 +0100420
Alan Coxbf970ee2008-10-13 10:42:39 +0100421 /*
422 * Everything allocated ... set up the o_tty structure.
423 */
Alan Coxbf970ee2008-10-13 10:42:39 +0100424 tty_driver_kref_get(driver->other);
Alan Coxbf970ee2008-10-13 10:42:39 +0100425 /* Establish the links in both directions */
426 tty->link = o_tty;
427 o_tty->link = tty;
Jiri Slabyd03702a2012-06-04 13:35:30 +0200428 tty_port_init(ports[0]);
429 tty_port_init(ports[1]);
Peter Hurleyc81622a2015-01-16 15:22:14 -0500430 tty_buffer_set_limit(ports[0], 8192);
431 tty_buffer_set_limit(ports[1], 8192);
Jiri Slabyd03702a2012-06-04 13:35:30 +0200432 o_tty->port = ports[0];
433 tty->port = ports[1];
Jiri Slaby967fab62012-10-18 22:26:46 +0200434 o_tty->port->itty = o_tty;
Alan Coxbf970ee2008-10-13 10:42:39 +0100435
Peter Hurley1d1d14d2015-01-17 15:42:05 -0500436 tty_buffer_set_lock_subclass(o_tty->port);
437
Alan Coxbf970ee2008-10-13 10:42:39 +0100438 tty_driver_kref_get(driver);
439 tty->count++;
Peter Hurley55199ea2014-11-05 12:12:49 -0500440 o_tty->count++;
Alan Coxbf970ee2008-10-13 10:42:39 +0100441 return 0;
Peter Hurleya3123fd2016-01-09 21:13:48 -0800442
Rasmus Villemoes2c964a22014-07-10 21:01:22 +0200443err_put_module:
Rasmus Villemoes07584d42014-07-13 01:44:00 +0200444 module_put(driver->other->owner);
Rasmus Villemoes2c964a22014-07-10 21:01:22 +0200445err:
Jiri Slabyd03702a2012-06-04 13:35:30 +0200446 kfree(ports[0]);
447 kfree(ports[1]);
Jiri Slaby8a1b8d72011-03-23 10:48:33 +0100448 return retval;
Alan Coxbf970ee2008-10-13 10:42:39 +0100449}
450
Jiri Slabyd03702a2012-06-04 13:35:30 +0200451static void pty_cleanup(struct tty_struct *tty)
452{
Jiri Slaby81c79832012-11-15 09:49:49 +0100453 tty_port_put(tty->port);
Jiri Slabyd03702a2012-06-04 13:35:30 +0200454}
455
Jiri Slaby5d249bc2012-06-04 13:35:29 +0200456/* Traditional BSD devices */
457#ifdef CONFIG_LEGACY_PTYS
458
459static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
460{
461 return pty_common_install(driver, tty, true);
462}
463
Alan Coxd1552552012-07-27 18:02:54 +0100464static 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
Alan Cox6caa76b2011-02-14 16:27:22 +0000472static int pty_bsd_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 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);
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +0400478 case TIOCGPTLCK: /* Get PT Lock status */
479 return pty_get_lock(tty, (int __user *)arg);
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400480 case TIOCPKT: /* Set PT packet mode */
481 return pty_set_pktmode(tty, (int __user *)arg);
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +0400482 case TIOCGPKT: /* Get PT packet mode */
483 return pty_get_pktmode(tty, (int __user *)arg);
hyc@symas.com26df6d12010-06-22 10:14:49 -0700484 case TIOCSIG: /* Send signal to other side of pty */
485 return pty_signal(tty, (int) arg);
Jiri Slabyded2f2952013-01-11 12:06:27 +0100486 case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 return -ENOIOCTLCMD;
490}
491
Al Viro50f45322018-09-12 20:53:46 -0400492#ifdef CONFIG_COMPAT
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000493static 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 */
Al Viro50f45322018-09-12 20:53:46 -0400500 return pty_bsd_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000501}
Al Viro50f45322018-09-12 20:53:46 -0400502#else
503#define pty_bsd_compat_ioctl NULL
504#endif
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000505
Kay Sieversdc8c8582007-08-15 12:25:38 +0200506static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
Paul Gortmaker71549882015-08-19 17:48:05 -0400507/*
508 * not really modular, but the easiest way to keep compat with existing
509 * bootargs behaviour is to continue using module_param here.
510 */
Kay Sieversdc8c8582007-08-15 12:25:38 +0200511module_param(legacy_count, int, 0);
512
Linus Torvalds342a5972009-09-30 07:49:40 -0700513/*
514 * The master side of a pty can do TIOCSPTLCK and thus
515 * has pty_bsd_ioctl.
516 */
517static const struct tty_operations master_pty_ops_bsd = {
518 .install = pty_install,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700519 .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,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700526 .ioctl = pty_bsd_ioctl,
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000527 .compat_ioctl = pty_bsd_compat_ioctl,
Jiri Slabyd03702a2012-06-04 13:35:30 +0200528 .cleanup = pty_cleanup,
Alan Coxd1552552012-07-27 18:02:54 +0100529 .resize = pty_resize,
530 .remove = pty_remove
Alan Cox3e8e88c2008-04-30 00:54:10 -0700531};
532
Linus Torvalds342a5972009-09-30 07:49:40 -0700533static 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,
Jiri Slabyd03702a2012-06-04 13:35:30 +0200543 .cleanup = pty_cleanup,
Alan Coxd1552552012-07-27 18:02:54 +0100544 .resize = pty_resize,
Peter Hurley01adc802014-09-10 15:06:32 -0400545 .start = pty_start,
546 .stop = pty_stop,
Alan Coxd1552552012-07-27 18:02:54 +0100547 .remove = pty_remove
Linus Torvalds342a5972009-09-30 07:49:40 -0700548};
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static void __init legacy_pty_init(void)
551{
Linus Torvalds342a5972009-09-30 07:49:40 -0700552 struct tty_driver *pty_driver, *pty_slave_driver;
553
Kay Sieversdc8c8582007-08-15 12:25:38 +0200554 if (legacy_count <= 0)
555 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jiri Slaby21aca2f2012-08-08 22:26:41 +0200557 pty_driver = tty_alloc_driver(legacy_count,
558 TTY_DRIVER_RESET_TERMIOS |
559 TTY_DRIVER_REAL_RAW |
560 TTY_DRIVER_DYNAMIC_ALLOC);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300561 if (IS_ERR(pty_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 panic("Couldn't allocate pty driver");
563
Jiri Slaby21aca2f2012-08-08 22:26:41 +0200564 pty_slave_driver = tty_alloc_driver(legacy_count,
565 TTY_DRIVER_RESET_TERMIOS |
566 TTY_DRIVER_REAL_RAW |
567 TTY_DRIVER_DYNAMIC_ALLOC);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300568 if (IS_ERR(pty_slave_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 panic("Couldn't allocate pty slave driver");
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 pty_driver->driver_name = "pty_master";
572 pty_driver->name = "pty";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 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;
Alan Cox606d0992006-12-08 02:38:45 -0800582 pty_driver->init_termios.c_ispeed = 38400;
583 pty_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 pty_driver->other = pty_slave_driver;
Linus Torvalds342a5972009-09-30 07:49:40 -0700585 tty_set_operations(pty_driver, &master_pty_ops_bsd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 pty_slave_driver->driver_name = "pty_slave";
588 pty_slave_driver->name = "ttyp";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 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;
Alan Cox606d0992006-12-08 02:38:45 -0800595 pty_slave_driver->init_termios.c_ispeed = 38400;
596 pty_slave_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 pty_slave_driver->other = pty_driver;
Linus Torvalds342a5972009-09-30 07:49:40 -0700598 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
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
606static inline void legacy_pty_init(void) { }
607#endif
608
609/* Unix98 devices */
610#ifdef CONFIG_UNIX98_PTYS
Alan Coxd81ed102008-10-13 10:41:42 +0100611static struct cdev ptmx_cdev;
612
Arnd Bergmann6509f302017-06-20 22:34:13 +0200613/**
Eric W. Biederman311fc652017-08-24 15:13:29 -0500614 * 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
Arnd Bergmann6509f302017-06-20 22:34:13 +0200618 *
Eric W. Biederman311fc652017-08-24 15:13:29 -0500619 * 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).
Arnd Bergmann6509f302017-06-20 22:34:13 +0200622 */
Eric W. Biederman311fc652017-08-24 15:13:29 -0500623int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
Arnd Bergmann6509f302017-06-20 22:34:13 +0200624{
625 int fd = -1;
Eric W. Biederman311fc652017-08-24 15:13:29 -0500626 struct file *filp;
Arnd Bergmann6509f302017-06-20 22:34:13 +0200627 int retval = -EINVAL;
Eric W. Biederman311fc652017-08-24 15:13:29 -0500628 struct path path;
629
630 if (tty->driver != ptm_driver)
631 return -EIO;
Arnd Bergmann6509f302017-06-20 22:34:13 +0200632
Matthijs van Duin36ecc142018-07-19 10:43:46 +0200633 fd = get_unused_fd_flags(flags);
Arnd Bergmann6509f302017-06-20 22:34:13 +0200634 if (fd < 0) {
635 retval = fd;
636 goto err;
637 }
638
Eric W. Biederman311fc652017-08-24 15:13:29 -0500639 /* 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);
Arnd Bergmann6509f302017-06-20 22:34:13 +0200649 if (IS_ERR(filp)) {
650 retval = PTR_ERR(filp);
651 goto err_put;
652 }
653
654 fd_install(fd, filp);
655 return fd;
656
657err_put:
658 put_unused_fd(fd);
659err:
660 return retval;
661}
662
Alan Cox6caa76b2011-02-14 16:27:22 +0000663static int pty_unix98_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 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);
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +0400669 case TIOCGPTLCK: /* Get PT Lock status */
670 return pty_get_lock(tty, (int __user *)arg);
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400671 case TIOCPKT: /* Set PT packet mode */
672 return pty_set_pktmode(tty, (int __user *)arg);
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +0400673 case TIOCGPKT: /* Get PT packet mode */
674 return pty_get_pktmode(tty, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 case TIOCGPTN: /* Get PT Number */
676 return put_user(tty->index, (unsigned int __user *)arg);
hyc@symas.com26df6d12010-06-22 10:14:49 -0700677 case TIOCSIG: /* Send signal to other side of pty */
678 return pty_signal(tty, (int) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681 return -ENOIOCTLCMD;
682}
683
Al Viro50f45322018-09-12 20:53:46 -0400684#ifdef CONFIG_COMPAT
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000685static 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 */
Al Viro50f45322018-09-12 20:53:46 -0400692 return pty_unix98_ioctl(tty, cmd,
693 cmd == TIOCSIG ? arg : (unsigned long)compat_ptr(arg));
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000694}
Al Viro50f45322018-09-12 20:53:46 -0400695#else
696#define pty_unix98_compat_ioctl NULL
697#endif
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000698
Alan Cox99f1fe12008-10-13 10:42:00 +0100699/**
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
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100708static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700709 struct file *file, int idx)
Alan Cox99f1fe12008-10-13 10:42:00 +0100710{
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +0400711 /* Master must be open via /dev/ptmx */
712 return ERR_PTR(-EIO);
Alan Cox99f1fe12008-10-13 10:42:00 +0100713}
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.
Alan Coxd739e652012-05-03 22:22:09 +0100721 * This provides our locking for the tty pointer.
Alan Cox99f1fe12008-10-13 10:42:00 +0100722 */
723
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100724static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700725 struct file *file, int idx)
Alan Cox99f1fe12008-10-13 10:42:00 +0100726{
Alan Coxd739e652012-05-03 22:22:09 +0100727 struct tty_struct *tty;
728
729 mutex_lock(&devpts_mutex);
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700730 tty = devpts_get_priv(file->f_path.dentry);
Alan Coxd739e652012-05-03 22:22:09 +0100731 mutex_unlock(&devpts_mutex);
Alan Cox99f1fe12008-10-13 10:42:00 +0100732 /* Master must be open before slave */
733 if (!tty)
734 return ERR_PTR(-EIO);
735 return tty;
736}
737
Alan Coxbf970ee2008-10-13 10:42:39 +0100738static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +0100739{
Jiri Slaby5d249bc2012-06-04 13:35:29 +0200740 return pty_common_install(driver, tty, false);
Alan Cox8b0a88d2008-10-13 10:42:19 +0100741}
742
Jiri Slabyf4b208e2013-04-15 07:52:05 +0200743/* this is called once with whichever end is closed last */
Peter Hurleyc1e33af2016-01-09 21:13:50 -0800744static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
Jiri Slabyf4b208e2013-04-15 07:52:05 +0200745{
Linus Torvalds67245ff2016-04-16 15:16:07 -0700746 struct pts_fs_info *fsi;
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200747
748 if (tty->driver->subtype == PTY_TYPE_MASTER)
Linus Torvalds67245ff2016-04-16 15:16:07 -0700749 fsi = tty->driver_data;
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200750 else
Linus Torvalds67245ff2016-04-16 15:16:07 -0700751 fsi = tty->link->driver_data;
Colin Ian King5353ed82016-06-20 15:40:27 +0100752
753 if (fsi) {
754 devpts_kill_index(fsi, tty->index);
755 devpts_release(fsi);
756 }
Jiri Slabyf4b208e2013-04-15 07:52:05 +0200757}
758
Masatake YAMATOd01c3282017-07-18 06:27:59 +0900759static 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
Alan Coxfeebed62008-10-13 10:41:30 +0100764static const struct tty_operations ptm_unix98_ops = {
Alan Cox99f1fe12008-10-13 10:42:00 +0100765 .lookup = ptm_unix98_lookup,
Alan Coxbf970ee2008-10-13 10:42:39 +0100766 .install = pty_unix98_install,
Jiri Slaby71716042012-06-04 13:35:28 +0200767 .remove = pty_unix98_remove,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700768 .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,
Alan Coxfeebed62008-10-13 10:41:30 +0100775 .ioctl = pty_unix98_ioctl,
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000776 .compat_ioctl = pty_unix98_compat_ioctl,
Alan Cox36b3c072012-07-17 17:06:57 +0100777 .resize = pty_resize,
Masatake YAMATOd01c3282017-07-18 06:27:59 +0900778 .cleanup = pty_cleanup,
779 .show_fdinfo = pty_show_fdinfo,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700780};
781
Alan Cox99f1fe12008-10-13 10:42:00 +0100782static const struct tty_operations pty_unix98_ops = {
783 .lookup = pts_unix98_lookup,
Alan Coxbf970ee2008-10-13 10:42:39 +0100784 .install = pty_unix98_install,
Jiri Slaby71716042012-06-04 13:35:28 +0200785 .remove = pty_unix98_remove,
Alan Cox99f1fe12008-10-13 10:42:00 +0100786 .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,
Peter Hurley01adc802014-09-10 15:06:32 -0400794 .start = pty_start,
795 .stop = pty_stop,
Jiri Slabyd03702a2012-06-04 13:35:30 +0200796 .cleanup = pty_cleanup,
Alan Cox99f1fe12008-10-13 10:42:00 +0100797};
Alan Coxd81ed102008-10-13 10:41:42 +0100798
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
Cong Dingb9f80332013-01-12 05:01:22 +0100807 * protect the rest.
Alan Coxd81ed102008-10-13 10:41:42 +0100808 * allocated_ptys_lock handles the list of free pty numbers
809 */
810
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +0200811static int ptmx_open(struct inode *inode, struct file *filp)
Alan Coxd81ed102008-10-13 10:41:42 +0100812{
Linus Torvalds67245ff2016-04-16 15:16:07 -0700813 struct pts_fs_info *fsi;
Alan Coxd81ed102008-10-13 10:41:42 +0100814 struct tty_struct *tty;
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700815 struct dentry *dentry;
Alan Coxd81ed102008-10-13 10:41:42 +0100816 int retval;
817 int index;
818
819 nonseekable_open(inode, filp);
820
Linus Torvaldsb0b88562013-05-01 07:32:21 -0700821 /* We refuse fsnotify events on ptmx, since it's a shared resource */
822 filp->f_mode |= FMODE_NONOTIFY;
823
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200824 retval = tty_alloc_file(filp);
825 if (retval)
826 return retval;
827
Linus Torvalds143c97c2017-08-23 18:16:11 -0700828 fsi = devpts_acquire(filp);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -0500829 if (IS_ERR(fsi)) {
830 retval = PTR_ERR(fsi);
Linus Torvalds67245ff2016-04-16 15:16:07 -0700831 goto out_free_file;
Eric W. Biedermaneedf2652016-06-02 10:29:47 -0500832 }
Linus Torvalds67245ff2016-04-16 15:16:07 -0700833
Alan Coxd81ed102008-10-13 10:41:42 +0100834 /* find a device that is not in use. */
Alan Cox89c8d912012-08-08 16:30:13 +0100835 mutex_lock(&devpts_mutex);
Linus Torvalds67245ff2016-04-16 15:16:07 -0700836 index = devpts_new_index(fsi);
Alan Cox6d31a882012-07-14 15:31:27 +0100837 mutex_unlock(&devpts_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100838
Linus Torvalds67245ff2016-04-16 15:16:07 -0700839 retval = index;
840 if (index < 0)
Eric W. Biedermaneedf2652016-06-02 10:29:47 -0500841 goto out_put_fsi;
Linus Torvalds67245ff2016-04-16 15:16:07 -0700842
843
Alan Cox89c8d912012-08-08 16:30:13 +0100844 mutex_lock(&tty_mutex);
845 tty = tty_init_dev(ptm_driver, index);
Alan Cox89c8d912012-08-08 16:30:13 +0100846 /* The tty returned here is locked so we can safely
847 drop the mutex */
848 mutex_unlock(&tty_mutex);
849
Linus Torvalds67245ff2016-04-16 15:16:07 -0700850 retval = PTR_ERR(tty);
851 if (IS_ERR(tty))
852 goto out;
Nick Pigginee2ffa02010-08-18 04:37:35 +1000853
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200854 /*
Linus Torvalds67245ff2016-04-16 15:16:07 -0700855 * From here on out, the tty is "live", and the index and
856 * fsi will be killed/put by the tty_release()
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200857 */
Linus Torvalds67245ff2016-04-16 15:16:07 -0700858 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
859 tty->driver_data = fsi;
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200860
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200861 tty_add_file(tty, filp);
Alan Coxd81ed102008-10-13 10:41:42 +0100862
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700863 dentry = devpts_pty_new(fsi, index, tty->link);
864 if (IS_ERR(dentry)) {
865 retval = PTR_ERR(dentry);
Jiri Slaby1177c0e2011-10-12 11:32:44 +0200866 goto err_release;
Jiri Slaby162b97c2012-10-18 22:26:28 +0200867 }
Eric W. Biederman311fc652017-08-24 15:13:29 -0500868 tty->link->driver_data = dentry;
Alan Coxd81ed102008-10-13 10:41:42 +0100869
870 retval = ptm_driver->ops->open(tty, filp);
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +0200871 if (retval)
Eric W. Biederman311fc652017-08-24 15:13:29 -0500872 goto err_release;
Jiri Slaby1177c0e2011-10-12 11:32:44 +0200873
Peter Hurleyd435cef2015-11-08 13:01:19 -0500874 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
Peter Hurleyd6847792015-07-12 22:49:13 -0400875
Alan Cox89c8d912012-08-08 16:30:13 +0100876 tty_unlock(tty);
Jiri Slaby1177c0e2011-10-12 11:32:44 +0200877 return 0;
878err_release:
Alan Cox89c8d912012-08-08 16:30:13 +0100879 tty_unlock(tty);
Linus Torvalds67245ff2016-04-16 15:16:07 -0700880 // This will also put-ref the fsi
Alan Coxeeb89d92009-11-30 13:18:29 +0000881 tty_release(inode, filp);
Alan Coxd81ed102008-10-13 10:41:42 +0100882 return retval;
883out:
Linus Torvalds67245ff2016-04-16 15:16:07 -0700884 devpts_kill_index(fsi, index);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -0500885out_put_fsi:
886 devpts_release(fsi);
Linus Torvalds67245ff2016-04-16 15:16:07 -0700887out_free_file:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200888 tty_free_file(filp);
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +0200889 return retval;
Alan Coxd81ed102008-10-13 10:41:42 +0100890}
891
Kees Cookd2ec3f72016-09-08 15:35:59 -0700892static struct file_operations ptmx_fops __ro_after_init;
Alan Coxd81ed102008-10-13 10:41:42 +0100893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894static void __init unix98_pty_init(void)
895{
Jiri Slaby21aca2f2012-08-08 22:26:41 +0200896 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);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300902 if (IS_ERR(ptm_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 panic("Couldn't allocate Unix98 ptm driver");
Jiri Slaby21aca2f2012-08-08 22:26:41 +0200904 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);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300910 if (IS_ERR(pts_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 panic("Couldn't allocate Unix98 pts driver");
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 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;
Alan Cox606d0992006-12-08 02:38:45 -0800924 ptm_driver->init_termios.c_ispeed = 38400;
925 ptm_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ptm_driver->other = pts_driver;
Alan Coxfeebed62008-10-13 10:41:30 +0100927 tty_set_operations(ptm_driver, &ptm_unix98_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 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;
Alan Cox606d0992006-12-08 02:38:45 -0800937 pts_driver->init_termios.c_ispeed = 38400;
938 pts_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 pts_driver->other = ptm_driver;
Alan Cox99f1fe12008-10-13 10:42:00 +0100940 tty_set_operations(pts_driver, &pty_unix98_ops);
Alan Coxfe9cd962008-10-13 10:43:38 +0100941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 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
Alan Coxd81ed102008-10-13 10:41:42 +0100947 /* 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)
Cong Ding82f8c352013-01-12 05:01:21 +0100954 panic("Couldn't register /dev/ptmx driver");
Alan Coxd81ed102008-10-13 10:41:42 +0100955 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
Alan Coxd81ed102008-10-13 10:41:42 +0100957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#else
959static inline void unix98_pty_init(void) { }
960#endif
961
962static int __init pty_init(void)
963{
964 legacy_pty_init();
965 unix98_pty_init();
966 return 0;
967}
Paul Gortmaker71549882015-08-19 17:48:05 -0400968device_initcall(pty_init);