blob: 284749fb0f6b96d3d6d667332e569ba0d745e048 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 *
4 * Added support for a Unix98-style ptmx device.
5 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Alan Coxfe9cd962008-10-13 10:43:38 +01009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/interrupt.h>
12#include <linux/tty.h>
13#include <linux/tty_flip.h>
14#include <linux/fcntl.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010015#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
17#include <linux/major.h>
18#include <linux/mm.h>
19#include <linux/init.h>
Alan Coxd81ed102008-10-13 10:41:42 +010020#include <linux/device.h>
Alan Coxfe9cd962008-10-13 10:43:38 +010021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/bitops.h>
23#include <linux/devpts_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Alan Coxd739e652012-05-03 22:22:09 +010025#include <linux/mutex.h>
Peter Hurley01adc802014-09-10 15:06:32 -040026#include <linux/poll.h>
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +100027#include <linux/mount.h>
28#include <linux/file.h>
29#include <linux/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Peter Hurleyd6847792015-07-12 22:49:13 -040031#undef TTY_DEBUG_HANGUP
32#ifdef TTY_DEBUG_HANGUP
33# define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
34#else
35# define tty_debug_hangup(tty, f, args...) do {} while (0)
36#endif
Alan Coxfe9cd962008-10-13 10:43:38 +010037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#ifdef CONFIG_UNIX98_PTYS
Roel Kluinda2bdf92009-01-07 18:09:15 -080039static struct tty_driver *ptm_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static struct tty_driver *pts_driver;
Alan Coxd739e652012-05-03 22:22:09 +010041static DEFINE_MUTEX(devpts_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#endif
43
Alan Coxfe9cd962008-10-13 10:43:38 +010044static void pty_close(struct tty_struct *tty, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Alan Coxfe9cd962008-10-13 10:43:38 +010046 BUG_ON(!tty);
47 if (tty->driver->subtype == PTY_TYPE_MASTER)
48 WARN_ON(tty->count > 1);
49 else {
Peter Hurley18900ca2016-04-09 17:06:48 -070050 if (tty_io_error(tty))
Peter Hurley699390352013-01-30 12:43:50 -050051 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (tty->count > 2)
53 return;
54 }
Peter Hurley699390352013-01-30 12:43:50 -050055 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 wake_up_interruptible(&tty->read_wait);
57 wake_up_interruptible(&tty->write_wait);
Peter Hurley4ed60fc2014-10-16 15:33:28 -040058 spin_lock_irq(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 tty->packet = 0;
Peter Hurley4ed60fc2014-10-16 15:33:28 -040060 spin_unlock_irq(&tty->ctrl_lock);
Alan Cox89c8d912012-08-08 16:30:13 +010061 /* Review - krefs on tty_link ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (!tty->link)
63 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
Brian Bloniarz0f40fbb2016-03-06 13:16:30 -080065 wake_up_interruptible(&tty->link->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 wake_up_interruptible(&tty->link->write_wait);
67 if (tty->driver->subtype == PTY_TYPE_MASTER) {
68 set_bit(TTY_OTHER_CLOSED, &tty->flags);
Greg Kroah-Hartmance1000d2012-02-24 13:56:36 -080069#ifdef CONFIG_UNIX98_PTYS
Alan Coxd739e652012-05-03 22:22:09 +010070 if (tty->driver == ptm_driver) {
Cong Dingb9f80332013-01-12 05:01:22 +010071 mutex_lock(&devpts_mutex);
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +100072 if (tty->link->driver_data) {
73 struct path *path = tty->link->driver_data;
74
75 devpts_pty_kill(path->dentry);
76 path_put(path);
77 kfree(path);
78 }
Cong Dingb9f80332013-01-12 05:01:22 +010079 mutex_unlock(&devpts_mutex);
Alan Coxd739e652012-05-03 22:22:09 +010080 }
Greg Kroah-Hartmance1000d2012-02-24 13:56:36 -080081#endif
Arnd Bergmann11dbf202010-06-18 14:58:07 +020082 tty_vhangup(tty->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84}
85
86/*
87 * The unthrottle routine is called by the line discipline to signal
88 * that it can receive more characters. For PTY's, the TTY_THROTTLED
89 * flag is always set, to force the line discipline to always call the
Alan Coxfe9cd962008-10-13 10:43:38 +010090 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * characters in the queue. This is necessary since each time this
92 * happens, we need to wake up any sleeping processes that could be
93 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
94 * for the pty buffer to be drained.
95 */
Alan Coxfe9cd962008-10-13 10:43:38 +010096static void pty_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Alan Coxd945cb92009-07-07 16:39:41 +010098 tty_wakeup(tty->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 set_bit(TTY_THROTTLED, &tty->flags);
100}
101
Alan Coxd945cb92009-07-07 16:39:41 +0100102/**
Alan Coxd945cb92009-07-07 16:39:41 +0100103 * pty_write - write to a pty
104 * @tty: the tty we write from
105 * @buf: kernel buffer of data
106 * @count: bytes to write
107 *
108 * Our "hardware" write method. Data is coming from the ldisc which
109 * may be in a non sleeping state. We simply throw this at the other
110 * end of the link as if we were an IRQ handler receiving stuff for
111 * the other side of the pty/tty pair.
112 */
113
Linus Torvaldsac89a912009-09-05 13:27:10 -0700114static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct tty_struct *to = tty->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Alan Coxd945cb92009-07-07 16:39:41 +0100118 if (tty->stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return 0;
120
Alan Coxd945cb92009-07-07 16:39:41 +0100121 if (c > 0) {
122 /* Stuff the data into the input queue of the other end */
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100123 c = tty_insert_flip_string(to->port, buf, c);
Alan Coxd945cb92009-07-07 16:39:41 +0100124 /* And shovel */
Peter Hurley5ede5252013-07-24 08:29:57 -0400125 if (c)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100126 tty_flip_buffer_push(to->port);
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);
Peter Hurley01adc802014-09-10 15:06:32 -0400351 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
352 }
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);
Peter Hurley01adc802014-09-10 15:06:32 -0400364 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
365 }
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
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000492static long pty_bsd_compat_ioctl(struct tty_struct *tty,
493 unsigned int cmd, unsigned long arg)
494{
495 /*
496 * PTY ioctls don't require any special translation between 32-bit and
497 * 64-bit userspace, they are already compatible.
498 */
499 return pty_bsd_ioctl(tty, cmd, arg);
500}
501
Kay Sieversdc8c8582007-08-15 12:25:38 +0200502static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
Paul Gortmaker71549882015-08-19 17:48:05 -0400503/*
504 * not really modular, but the easiest way to keep compat with existing
505 * bootargs behaviour is to continue using module_param here.
506 */
Kay Sieversdc8c8582007-08-15 12:25:38 +0200507module_param(legacy_count, int, 0);
508
Linus Torvalds342a5972009-09-30 07:49:40 -0700509/*
510 * The master side of a pty can do TIOCSPTLCK and thus
511 * has pty_bsd_ioctl.
512 */
513static const struct tty_operations master_pty_ops_bsd = {
514 .install = pty_install,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700515 .open = pty_open,
516 .close = pty_close,
517 .write = pty_write,
518 .write_room = pty_write_room,
519 .flush_buffer = pty_flush_buffer,
520 .chars_in_buffer = pty_chars_in_buffer,
521 .unthrottle = pty_unthrottle,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700522 .ioctl = pty_bsd_ioctl,
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000523 .compat_ioctl = pty_bsd_compat_ioctl,
Jiri Slabyd03702a2012-06-04 13:35:30 +0200524 .cleanup = pty_cleanup,
Alan Coxd1552552012-07-27 18:02:54 +0100525 .resize = pty_resize,
526 .remove = pty_remove
Alan Cox3e8e88c2008-04-30 00:54:10 -0700527};
528
Linus Torvalds342a5972009-09-30 07:49:40 -0700529static const struct tty_operations slave_pty_ops_bsd = {
530 .install = pty_install,
531 .open = pty_open,
532 .close = pty_close,
533 .write = pty_write,
534 .write_room = pty_write_room,
535 .flush_buffer = pty_flush_buffer,
536 .chars_in_buffer = pty_chars_in_buffer,
537 .unthrottle = pty_unthrottle,
538 .set_termios = pty_set_termios,
Jiri Slabyd03702a2012-06-04 13:35:30 +0200539 .cleanup = pty_cleanup,
Alan Coxd1552552012-07-27 18:02:54 +0100540 .resize = pty_resize,
Peter Hurley01adc802014-09-10 15:06:32 -0400541 .start = pty_start,
542 .stop = pty_stop,
Alan Coxd1552552012-07-27 18:02:54 +0100543 .remove = pty_remove
Linus Torvalds342a5972009-09-30 07:49:40 -0700544};
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static void __init legacy_pty_init(void)
547{
Linus Torvalds342a5972009-09-30 07:49:40 -0700548 struct tty_driver *pty_driver, *pty_slave_driver;
549
Kay Sieversdc8c8582007-08-15 12:25:38 +0200550 if (legacy_count <= 0)
551 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jiri Slaby21aca2f2012-08-08 22:26:41 +0200553 pty_driver = tty_alloc_driver(legacy_count,
554 TTY_DRIVER_RESET_TERMIOS |
555 TTY_DRIVER_REAL_RAW |
556 TTY_DRIVER_DYNAMIC_ALLOC);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300557 if (IS_ERR(pty_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 panic("Couldn't allocate pty driver");
559
Jiri Slaby21aca2f2012-08-08 22:26:41 +0200560 pty_slave_driver = tty_alloc_driver(legacy_count,
561 TTY_DRIVER_RESET_TERMIOS |
562 TTY_DRIVER_REAL_RAW |
563 TTY_DRIVER_DYNAMIC_ALLOC);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300564 if (IS_ERR(pty_slave_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 panic("Couldn't allocate pty slave driver");
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 pty_driver->driver_name = "pty_master";
568 pty_driver->name = "pty";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 pty_driver->major = PTY_MASTER_MAJOR;
570 pty_driver->minor_start = 0;
571 pty_driver->type = TTY_DRIVER_TYPE_PTY;
572 pty_driver->subtype = PTY_TYPE_MASTER;
573 pty_driver->init_termios = tty_std_termios;
574 pty_driver->init_termios.c_iflag = 0;
575 pty_driver->init_termios.c_oflag = 0;
576 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
577 pty_driver->init_termios.c_lflag = 0;
Alan Cox606d0992006-12-08 02:38:45 -0800578 pty_driver->init_termios.c_ispeed = 38400;
579 pty_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 pty_driver->other = pty_slave_driver;
Linus Torvalds342a5972009-09-30 07:49:40 -0700581 tty_set_operations(pty_driver, &master_pty_ops_bsd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 pty_slave_driver->driver_name = "pty_slave";
584 pty_slave_driver->name = "ttyp";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pty_slave_driver->major = PTY_SLAVE_MAJOR;
586 pty_slave_driver->minor_start = 0;
587 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
588 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
589 pty_slave_driver->init_termios = tty_std_termios;
590 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
Alan Cox606d0992006-12-08 02:38:45 -0800591 pty_slave_driver->init_termios.c_ispeed = 38400;
592 pty_slave_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 pty_slave_driver->other = pty_driver;
Linus Torvalds342a5972009-09-30 07:49:40 -0700594 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 if (tty_register_driver(pty_driver))
597 panic("Couldn't register pty driver");
598 if (tty_register_driver(pty_slave_driver))
599 panic("Couldn't register pty slave driver");
600}
601#else
602static inline void legacy_pty_init(void) { }
603#endif
604
605/* Unix98 devices */
606#ifdef CONFIG_UNIX98_PTYS
Alan Coxd81ed102008-10-13 10:41:42 +0100607static struct cdev ptmx_cdev;
608
Arnd Bergmann6509f302017-06-20 22:34:13 +0200609/**
610 * pty_open_peer - open the peer of a pty
611 * @tty: the peer of the pty being opened
612 *
613 * Open the cached dentry in tty->link, providing a safe way for userspace
614 * to get the slave end of a pty (where they have the master fd and cannot
615 * access or trust the mount namespace /dev/pts was mounted inside).
616 */
617static struct file *pty_open_peer(struct tty_struct *tty, int flags)
618{
619 if (tty->driver->subtype != PTY_TYPE_MASTER)
620 return ERR_PTR(-EIO);
621 return dentry_open(tty->link->driver_data, flags, current_cred());
622}
623
624static int pty_get_peer(struct tty_struct *tty, int flags)
625{
626 int fd = -1;
627 struct file *filp = NULL;
628 int retval = -EINVAL;
629
630 fd = get_unused_fd_flags(0);
631 if (fd < 0) {
632 retval = fd;
633 goto err;
634 }
635
636 filp = pty_open_peer(tty, flags);
637 if (IS_ERR(filp)) {
638 retval = PTR_ERR(filp);
639 goto err_put;
640 }
641
642 fd_install(fd, filp);
643 return fd;
644
645err_put:
646 put_unused_fd(fd);
647err:
648 return retval;
649}
650
Alan Cox6caa76b2011-02-14 16:27:22 +0000651static int pty_unix98_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unsigned int cmd, unsigned long arg)
653{
654 switch (cmd) {
655 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
656 return pty_set_lock(tty, (int __user *)arg);
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +0400657 case TIOCGPTLCK: /* Get PT Lock status */
658 return pty_get_lock(tty, (int __user *)arg);
Cyrill Gorcunov06026d92012-10-24 23:43:20 +0400659 case TIOCPKT: /* Set PT packet mode */
660 return pty_set_pktmode(tty, (int __user *)arg);
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +0400661 case TIOCGPKT: /* Get PT packet mode */
662 return pty_get_pktmode(tty, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 case TIOCGPTN: /* Get PT Number */
664 return put_user(tty->index, (unsigned int __user *)arg);
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +1000665 case TIOCGPTPEER: /* Open the other end */
666 return pty_get_peer(tty, (int) arg);
hyc@symas.com26df6d12010-06-22 10:14:49 -0700667 case TIOCSIG: /* Send signal to other side of pty */
668 return pty_signal(tty, (int) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
671 return -ENOIOCTLCMD;
672}
673
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000674static long pty_unix98_compat_ioctl(struct tty_struct *tty,
675 unsigned int cmd, unsigned long arg)
676{
677 /*
678 * PTY ioctls don't require any special translation between 32-bit and
679 * 64-bit userspace, they are already compatible.
680 */
681 return pty_unix98_ioctl(tty, cmd, arg);
682}
683
Alan Cox99f1fe12008-10-13 10:42:00 +0100684/**
685 * ptm_unix98_lookup - find a pty master
686 * @driver: ptm driver
687 * @idx: tty index
688 *
689 * Look up a pty master device. Called under the tty_mutex for now.
690 * This provides our locking.
691 */
692
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100693static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700694 struct file *file, int idx)
Alan Cox99f1fe12008-10-13 10:42:00 +0100695{
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +0400696 /* Master must be open via /dev/ptmx */
697 return ERR_PTR(-EIO);
Alan Cox99f1fe12008-10-13 10:42:00 +0100698}
699
700/**
701 * pts_unix98_lookup - find a pty slave
702 * @driver: pts driver
703 * @idx: tty index
704 *
705 * Look up a pty master device. Called under the tty_mutex for now.
Alan Coxd739e652012-05-03 22:22:09 +0100706 * This provides our locking for the tty pointer.
Alan Cox99f1fe12008-10-13 10:42:00 +0100707 */
708
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +0100709static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700710 struct file *file, int idx)
Alan Cox99f1fe12008-10-13 10:42:00 +0100711{
Alan Coxd739e652012-05-03 22:22:09 +0100712 struct tty_struct *tty;
713
714 mutex_lock(&devpts_mutex);
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700715 tty = devpts_get_priv(file->f_path.dentry);
Alan Coxd739e652012-05-03 22:22:09 +0100716 mutex_unlock(&devpts_mutex);
Alan Cox99f1fe12008-10-13 10:42:00 +0100717 /* Master must be open before slave */
718 if (!tty)
719 return ERR_PTR(-EIO);
720 return tty;
721}
722
Alan Coxbf970ee2008-10-13 10:42:39 +0100723static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +0100724{
Jiri Slaby5d249bc2012-06-04 13:35:29 +0200725 return pty_common_install(driver, tty, false);
Alan Cox8b0a88d2008-10-13 10:42:19 +0100726}
727
Jiri Slabyf4b208e2013-04-15 07:52:05 +0200728/* this is called once with whichever end is closed last */
Peter Hurleyc1e33af2016-01-09 21:13:50 -0800729static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
Jiri Slabyf4b208e2013-04-15 07:52:05 +0200730{
Linus Torvalds67245ff2016-04-16 15:16:07 -0700731 struct pts_fs_info *fsi;
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200732
733 if (tty->driver->subtype == PTY_TYPE_MASTER)
Linus Torvalds67245ff2016-04-16 15:16:07 -0700734 fsi = tty->driver_data;
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200735 else
Linus Torvalds67245ff2016-04-16 15:16:07 -0700736 fsi = tty->link->driver_data;
Colin Ian King5353ed82016-06-20 15:40:27 +0100737
738 if (fsi) {
739 devpts_kill_index(fsi, tty->index);
740 devpts_release(fsi);
741 }
Jiri Slabyf4b208e2013-04-15 07:52:05 +0200742}
743
Alan Coxfeebed62008-10-13 10:41:30 +0100744static const struct tty_operations ptm_unix98_ops = {
Alan Cox99f1fe12008-10-13 10:42:00 +0100745 .lookup = ptm_unix98_lookup,
Alan Coxbf970ee2008-10-13 10:42:39 +0100746 .install = pty_unix98_install,
Jiri Slaby71716042012-06-04 13:35:28 +0200747 .remove = pty_unix98_remove,
Alan Cox3e8e88c2008-04-30 00:54:10 -0700748 .open = pty_open,
749 .close = pty_close,
750 .write = pty_write,
751 .write_room = pty_write_room,
752 .flush_buffer = pty_flush_buffer,
753 .chars_in_buffer = pty_chars_in_buffer,
754 .unthrottle = pty_unthrottle,
Alan Coxfeebed62008-10-13 10:41:30 +0100755 .ioctl = pty_unix98_ioctl,
Aleksa Sarai5f0f1872017-06-04 00:15:14 +1000756 .compat_ioctl = pty_unix98_compat_ioctl,
Alan Cox36b3c072012-07-17 17:06:57 +0100757 .resize = pty_resize,
758 .cleanup = pty_cleanup
Alan Cox3e8e88c2008-04-30 00:54:10 -0700759};
760
Alan Cox99f1fe12008-10-13 10:42:00 +0100761static const struct tty_operations pty_unix98_ops = {
762 .lookup = pts_unix98_lookup,
Alan Coxbf970ee2008-10-13 10:42:39 +0100763 .install = pty_unix98_install,
Jiri Slaby71716042012-06-04 13:35:28 +0200764 .remove = pty_unix98_remove,
Alan Cox99f1fe12008-10-13 10:42:00 +0100765 .open = pty_open,
766 .close = pty_close,
767 .write = pty_write,
768 .write_room = pty_write_room,
769 .flush_buffer = pty_flush_buffer,
770 .chars_in_buffer = pty_chars_in_buffer,
771 .unthrottle = pty_unthrottle,
772 .set_termios = pty_set_termios,
Peter Hurley01adc802014-09-10 15:06:32 -0400773 .start = pty_start,
774 .stop = pty_stop,
Jiri Slabyd03702a2012-06-04 13:35:30 +0200775 .cleanup = pty_cleanup,
Alan Cox99f1fe12008-10-13 10:42:00 +0100776};
Alan Coxd81ed102008-10-13 10:41:42 +0100777
778/**
779 * ptmx_open - open a unix 98 pty master
780 * @inode: inode of device file
781 * @filp: file pointer to tty
782 *
783 * Allocate a unix98 pty master device from the ptmx driver.
784 *
785 * Locking: tty_mutex protects the init_dev work. tty->count should
Cong Dingb9f80332013-01-12 05:01:22 +0100786 * protect the rest.
Alan Coxd81ed102008-10-13 10:41:42 +0100787 * allocated_ptys_lock handles the list of free pty numbers
788 */
789
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +0200790static int ptmx_open(struct inode *inode, struct file *filp)
Alan Coxd81ed102008-10-13 10:41:42 +0100791{
Linus Torvalds67245ff2016-04-16 15:16:07 -0700792 struct pts_fs_info *fsi;
Alan Coxd81ed102008-10-13 10:41:42 +0100793 struct tty_struct *tty;
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +1000794 struct path *pts_path;
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700795 struct dentry *dentry;
Alan Coxd81ed102008-10-13 10:41:42 +0100796 int retval;
797 int index;
798
799 nonseekable_open(inode, filp);
800
Linus Torvaldsb0b88562013-05-01 07:32:21 -0700801 /* We refuse fsnotify events on ptmx, since it's a shared resource */
802 filp->f_mode |= FMODE_NONOTIFY;
803
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200804 retval = tty_alloc_file(filp);
805 if (retval)
806 return retval;
807
Linus Torvalds143c97c2017-08-23 18:16:11 -0700808 fsi = devpts_acquire(filp);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -0500809 if (IS_ERR(fsi)) {
810 retval = PTR_ERR(fsi);
Linus Torvalds67245ff2016-04-16 15:16:07 -0700811 goto out_free_file;
Eric W. Biedermaneedf2652016-06-02 10:29:47 -0500812 }
Linus Torvalds67245ff2016-04-16 15:16:07 -0700813
Alan Coxd81ed102008-10-13 10:41:42 +0100814 /* find a device that is not in use. */
Alan Cox89c8d912012-08-08 16:30:13 +0100815 mutex_lock(&devpts_mutex);
Linus Torvalds67245ff2016-04-16 15:16:07 -0700816 index = devpts_new_index(fsi);
Alan Cox6d31a882012-07-14 15:31:27 +0100817 mutex_unlock(&devpts_mutex);
Alan Cox89c8d912012-08-08 16:30:13 +0100818
Linus Torvalds67245ff2016-04-16 15:16:07 -0700819 retval = index;
820 if (index < 0)
Eric W. Biedermaneedf2652016-06-02 10:29:47 -0500821 goto out_put_fsi;
Linus Torvalds67245ff2016-04-16 15:16:07 -0700822
823
Alan Cox89c8d912012-08-08 16:30:13 +0100824 mutex_lock(&tty_mutex);
825 tty = tty_init_dev(ptm_driver, index);
Alan Cox89c8d912012-08-08 16:30:13 +0100826 /* The tty returned here is locked so we can safely
827 drop the mutex */
828 mutex_unlock(&tty_mutex);
829
Linus Torvalds67245ff2016-04-16 15:16:07 -0700830 retval = PTR_ERR(tty);
831 if (IS_ERR(tty))
832 goto out;
Nick Pigginee2ffa02010-08-18 04:37:35 +1000833
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200834 /*
Linus Torvalds67245ff2016-04-16 15:16:07 -0700835 * From here on out, the tty is "live", and the index and
836 * fsi will be killed/put by the tty_release()
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200837 */
Linus Torvalds67245ff2016-04-16 15:16:07 -0700838 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
839 tty->driver_data = fsi;
Herton R. Krzesinski2831c892016-01-11 12:07:43 -0200840
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200841 tty_add_file(tty, filp);
Alan Coxd81ed102008-10-13 10:41:42 +0100842
Linus Torvalds8ead9dd2016-04-25 20:04:08 -0700843 dentry = devpts_pty_new(fsi, index, tty->link);
844 if (IS_ERR(dentry)) {
845 retval = PTR_ERR(dentry);
Jiri Slaby1177c0e2011-10-12 11:32:44 +0200846 goto err_release;
Jiri Slaby162b97c2012-10-18 22:26:28 +0200847 }
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +1000848 /* We need to cache a fake path for TIOCGPTPEER. */
849 pts_path = kmalloc(sizeof(struct path), GFP_KERNEL);
850 if (!pts_path)
851 goto err_release;
Linus Torvalds143c97c2017-08-23 18:16:11 -0700852 pts_path->mnt = filp->f_path.mnt;
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +1000853 pts_path->dentry = dentry;
854 path_get(pts_path);
855 tty->link->driver_data = pts_path;
Alan Coxd81ed102008-10-13 10:41:42 +0100856
857 retval = ptm_driver->ops->open(tty, filp);
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +0200858 if (retval)
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +1000859 goto err_path_put;
Jiri Slaby1177c0e2011-10-12 11:32:44 +0200860
Peter Hurleyd435cef2015-11-08 13:01:19 -0500861 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
Peter Hurleyd6847792015-07-12 22:49:13 -0400862
Alan Cox89c8d912012-08-08 16:30:13 +0100863 tty_unlock(tty);
Jiri Slaby1177c0e2011-10-12 11:32:44 +0200864 return 0;
Aleksa Sarai54ebbfb2017-06-04 00:15:15 +1000865err_path_put:
866 path_put(pts_path);
867 kfree(pts_path);
Jiri Slaby1177c0e2011-10-12 11:32:44 +0200868err_release:
Alan Cox89c8d912012-08-08 16:30:13 +0100869 tty_unlock(tty);
Linus Torvalds67245ff2016-04-16 15:16:07 -0700870 // This will also put-ref the fsi
Alan Coxeeb89d92009-11-30 13:18:29 +0000871 tty_release(inode, filp);
Alan Coxd81ed102008-10-13 10:41:42 +0100872 return retval;
873out:
Linus Torvalds67245ff2016-04-16 15:16:07 -0700874 devpts_kill_index(fsi, index);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -0500875out_put_fsi:
876 devpts_release(fsi);
Linus Torvalds67245ff2016-04-16 15:16:07 -0700877out_free_file:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200878 tty_free_file(filp);
Arnd Bergmann64ba3dc32010-06-01 22:53:02 +0200879 return retval;
Alan Coxd81ed102008-10-13 10:41:42 +0100880}
881
Kees Cookd2ec3f72016-09-08 15:35:59 -0700882static struct file_operations ptmx_fops __ro_after_init;
Alan Coxd81ed102008-10-13 10:41:42 +0100883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static void __init unix98_pty_init(void)
885{
Jiri Slaby21aca2f2012-08-08 22:26:41 +0200886 ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
887 TTY_DRIVER_RESET_TERMIOS |
888 TTY_DRIVER_REAL_RAW |
889 TTY_DRIVER_DYNAMIC_DEV |
890 TTY_DRIVER_DEVPTS_MEM |
891 TTY_DRIVER_DYNAMIC_ALLOC);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300892 if (IS_ERR(ptm_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 panic("Couldn't allocate Unix98 ptm driver");
Jiri Slaby21aca2f2012-08-08 22:26:41 +0200894 pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
895 TTY_DRIVER_RESET_TERMIOS |
896 TTY_DRIVER_REAL_RAW |
897 TTY_DRIVER_DYNAMIC_DEV |
898 TTY_DRIVER_DEVPTS_MEM |
899 TTY_DRIVER_DYNAMIC_ALLOC);
Dan Carpenterc3a63442012-08-16 16:16:56 +0300900 if (IS_ERR(pts_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 panic("Couldn't allocate Unix98 pts driver");
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 ptm_driver->driver_name = "pty_master";
904 ptm_driver->name = "ptm";
905 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
906 ptm_driver->minor_start = 0;
907 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
908 ptm_driver->subtype = PTY_TYPE_MASTER;
909 ptm_driver->init_termios = tty_std_termios;
910 ptm_driver->init_termios.c_iflag = 0;
911 ptm_driver->init_termios.c_oflag = 0;
912 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
913 ptm_driver->init_termios.c_lflag = 0;
Alan Cox606d0992006-12-08 02:38:45 -0800914 ptm_driver->init_termios.c_ispeed = 38400;
915 ptm_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 ptm_driver->other = pts_driver;
Alan Coxfeebed62008-10-13 10:41:30 +0100917 tty_set_operations(ptm_driver, &ptm_unix98_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 pts_driver->driver_name = "pty_slave";
920 pts_driver->name = "pts";
921 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
922 pts_driver->minor_start = 0;
923 pts_driver->type = TTY_DRIVER_TYPE_PTY;
924 pts_driver->subtype = PTY_TYPE_SLAVE;
925 pts_driver->init_termios = tty_std_termios;
926 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
Alan Cox606d0992006-12-08 02:38:45 -0800927 pts_driver->init_termios.c_ispeed = 38400;
928 pts_driver->init_termios.c_ospeed = 38400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 pts_driver->other = ptm_driver;
Alan Cox99f1fe12008-10-13 10:42:00 +0100930 tty_set_operations(pts_driver, &pty_unix98_ops);
Alan Coxfe9cd962008-10-13 10:43:38 +0100931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (tty_register_driver(ptm_driver))
933 panic("Couldn't register Unix98 ptm driver");
934 if (tty_register_driver(pts_driver))
935 panic("Couldn't register Unix98 pts driver");
936
Alan Coxd81ed102008-10-13 10:41:42 +0100937 /* Now create the /dev/ptmx special device */
938 tty_default_fops(&ptmx_fops);
939 ptmx_fops.open = ptmx_open;
940
941 cdev_init(&ptmx_cdev, &ptmx_fops);
942 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
943 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
Cong Ding82f8c352013-01-12 05:01:21 +0100944 panic("Couldn't register /dev/ptmx driver");
Alan Coxd81ed102008-10-13 10:41:42 +0100945 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
Alan Coxd81ed102008-10-13 10:41:42 +0100947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948#else
949static inline void unix98_pty_init(void) { }
950#endif
951
952static int __init pty_init(void)
953{
954 legacy_pty_init();
955 unix98_pty_init();
956 return 0;
957}
Paul Gortmaker71549882015-08-19 17:48:05 -0400958device_initcall(pty_init);