Blame view

drivers/tty/pty.c 24.5 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
   *  Copyright (C) 1991, 1992  Linus Torvalds
   *
   *  Added support for a Unix98-style ptmx device.
   *    -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
   */
fe9cd962a   Alan Cox   pty: Coding style...
9
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
  #include <linux/errno.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
  #include <linux/interrupt.h>
  #include <linux/tty.h>
  #include <linux/tty_flip.h>
  #include <linux/fcntl.h>
3f07c0144   Ingo Molnar   sched/headers: Pr...
15
  #include <linux/sched/signal.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
17
18
19
  #include <linux/string.h>
  #include <linux/major.h>
  #include <linux/mm.h>
  #include <linux/init.h>
d81ed1030   Alan Cox   tty: Remove more ...
20
  #include <linux/device.h>
fe9cd962a   Alan Cox   pty: Coding style...
21
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
  #include <linux/bitops.h>
  #include <linux/devpts_fs.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
24
  #include <linux/slab.h>
d739e65bb   Alan Cox   pty: Lock the dev...
25
  #include <linux/mutex.h>
01adc8070   Peter Hurley   tty: Move packet ...
26
  #include <linux/poll.h>
54ebbfb16   Aleksa Sarai   tty: add TIOCGPTP...
27
28
29
  #include <linux/mount.h>
  #include <linux/file.h>
  #include <linux/ioctl.h>
50f45326a   Al Viro   pty: fix compat i...
30
  #include <linux/compat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31

d68477933   Peter Hurley   pty: Add debug me...
32
33
34
35
36
37
  #undef TTY_DEBUG_HANGUP
  #ifdef TTY_DEBUG_HANGUP
  # define tty_debug_hangup(tty, f, args...)	tty_debug(tty, f, ##args)
  #else
  # define tty_debug_hangup(tty, f, args...)	do {} while (0)
  #endif
fe9cd962a   Alan Cox   pty: Coding style...
38

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
  #ifdef CONFIG_UNIX98_PTYS
da2bdf9a6   Roel Kluin   Make various thin...
40
  static struct tty_driver *ptm_driver;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
  static struct tty_driver *pts_driver;
d739e65bb   Alan Cox   pty: Lock the dev...
42
  static DEFINE_MUTEX(devpts_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
  #endif
fe9cd962a   Alan Cox   pty: Coding style...
44
  static void pty_close(struct tty_struct *tty, struct file *filp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
  {
fe9cd962a   Alan Cox   pty: Coding style...
46
47
48
49
  	BUG_ON(!tty);
  	if (tty->driver->subtype == PTY_TYPE_MASTER)
  		WARN_ON(tty->count > 1);
  	else {
18900ca65   Peter Hurley   tty: Replace TTY_...
50
  		if (tty_io_error(tty))
699390354   Peter Hurley   pty: Ignore slave...
51
  			return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
  		if (tty->count > 2)
  			return;
  	}
699390354   Peter Hurley   pty: Ignore slave...
55
  	set_bit(TTY_IO_ERROR, &tty->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
  	wake_up_interruptible(&tty->read_wait);
  	wake_up_interruptible(&tty->write_wait);
4ed60fc25   Peter Hurley   pty: Hold ctrl_lo...
58
  	spin_lock_irq(&tty->ctrl_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  	tty->packet = 0;
4ed60fc25   Peter Hurley   pty: Hold ctrl_lo...
60
  	spin_unlock_irq(&tty->ctrl_lock);
89c8d91e3   Alan Cox   tty: localise the...
61
  	/* Review - krefs on tty_link ?? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
  	if (!tty->link)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
  	set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
0f40fbbcc   Brian Bloniarz   Fix OpenSSH pty r...
65
  	wake_up_interruptible(&tty->link->read_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
67
68
  	wake_up_interruptible(&tty->link->write_wait);
  	if (tty->driver->subtype == PTY_TYPE_MASTER) {
  		set_bit(TTY_OTHER_CLOSED, &tty->flags);
ce1000ddc   Greg Kroah-Hartman   Revert "TTY: pty,...
69
  #ifdef CONFIG_UNIX98_PTYS
d739e65bb   Alan Cox   pty: Lock the dev...
70
  		if (tty->driver == ptm_driver) {
b9f8033f2   Cong Ding   tty: cleanup chec...
71
  			mutex_lock(&devpts_mutex);
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
72
73
  			if (tty->link->driver_data)
  				devpts_pty_kill(tty->link->driver_data);
b9f8033f2   Cong Ding   tty: cleanup chec...
74
  			mutex_unlock(&devpts_mutex);
d739e65bb   Alan Cox   pty: Lock the dev...
75
  		}
ce1000ddc   Greg Kroah-Hartman   Revert "TTY: pty,...
76
  #endif
11dbf2039   Arnd Bergmann   tty: avoid recurs...
77
  		tty_vhangup(tty->link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
80
81
82
83
84
  	}
  }
  
  /*
   * The unthrottle routine is called by the line discipline to signal
   * that it can receive more characters.  For PTY's, the TTY_THROTTLED
   * flag is always set, to force the line discipline to always call the
fe9cd962a   Alan Cox   pty: Coding style...
85
   * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
   * characters in the queue.  This is necessary since each time this
   * happens, we need to wake up any sleeping processes that could be
   * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
   * for the pty buffer to be drained.
   */
fe9cd962a   Alan Cox   pty: Coding style...
91
  static void pty_unthrottle(struct tty_struct *tty)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  {
d945cb9cc   Alan Cox   pty: Rework the p...
93
  	tty_wakeup(tty->link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
  	set_bit(TTY_THROTTLED, &tty->flags);
  }
d945cb9cc   Alan Cox   pty: Rework the p...
96
  /**
d945cb9cc   Alan Cox   pty: Rework the p...
97
98
99
   *	pty_write		-	write to a pty
   *	@tty: the tty we write from
   *	@buf: kernel buffer of data
fa4419545   Jiri Slaby   tty: fix kernel-doc
100
   *	@c: bytes to write
762faaed9   Alan Cox   pty: Narrow the r...
101
   *
d945cb9cc   Alan Cox   pty: Rework the p...
102
103
104
105
   *	Our "hardware" write method. Data is coming from the ldisc which
   *	may be in a non sleeping state. We simply throw this at the other
   *	end of the link as if we were an IRQ handler receiving stuff for
   *	the other side of the pty/tty pair.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
   */
d945cb9cc   Alan Cox   pty: Rework the p...
107

ac89a9174   Linus Torvalds   pty: don't limit ...
108
  static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  {
  	struct tty_struct *to = tty->link;
b6da31b2c   DaeRyong Jeong   tty: Fix data rac...
111
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112

d945cb9cc   Alan Cox   pty: Rework the p...
113
  	if (tty->stopped)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  		return 0;
d945cb9cc   Alan Cox   pty: Rework the p...
115

d945cb9cc   Alan Cox   pty: Rework the p...
116
  	if (c > 0) {
b6da31b2c   DaeRyong Jeong   tty: Fix data rac...
117
  		spin_lock_irqsave(&to->port->lock, flags);
d945cb9cc   Alan Cox   pty: Rework the p...
118
  		/* Stuff the data into the input queue of the other end */
05c7cd399   Jiri Slaby   TTY: switch tty_i...
119
  		c = tty_insert_flip_string(to->port, buf, c);
71a174b39   Artem Savkov   pty: do tty_flip_...
120
  		spin_unlock_irqrestore(&to->port->lock, flags);
d945cb9cc   Alan Cox   pty: Rework the p...
121
  		/* And shovel */
5ede52538   Peter Hurley   tty: Remove extra...
122
  		if (c)
2e124b4a3   Jiri Slaby   TTY: switch tty_f...
123
  			tty_flip_buffer_push(to->port);
762faaed9   Alan Cox   pty: Narrow the r...
124
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
  	return c;
  }
d945cb9cc   Alan Cox   pty: Rework the p...
127
128
129
130
131
132
133
  /**
   *	pty_write_room	-	write space
   *	@tty: tty we are writing from
   *
   *	Report how many bytes the ldisc can send into the queue for
   *	the other device.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
135
  static int pty_write_room(struct tty_struct *tty)
  {
85dfd81dc   Linus Torvalds   pty: fix data los...
136
137
  	if (tty->stopped)
  		return 0;
c81622ac4   Peter Hurley   pty: Fix overlimi...
138
  	return tty_buffer_space_avail(tty->link->port);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  }
d945cb9cc   Alan Cox   pty: Rework the p...
140
141
142
  /**
   *	pty_chars_in_buffer	-	characters currently in our tx queue
   *	@tty: our tty
fe9cd962a   Alan Cox   pty: Coding style...
143
   *
d945cb9cc   Alan Cox   pty: Rework the p...
144
145
   *	Report how much we have in the transmit queue. As everything is
   *	instantly at the other end this is easy to implement.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
   */
d945cb9cc   Alan Cox   pty: Rework the p...
147

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
  static int pty_chars_in_buffer(struct tty_struct *tty)
  {
d945cb9cc   Alan Cox   pty: Rework the p...
150
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
153
  }
  
  /* Set the lock flag on a pty */
fe9cd962a   Alan Cox   pty: Coding style...
154
  static int pty_set_lock(struct tty_struct *tty, int __user *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
  {
  	int val;
fe9cd962a   Alan Cox   pty: Coding style...
157
  	if (get_user(val, arg))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
160
161
162
163
164
  		return -EFAULT;
  	if (val)
  		set_bit(TTY_PTY_LOCK, &tty->flags);
  	else
  		clear_bit(TTY_PTY_LOCK, &tty->flags);
  	return 0;
  }
84fd7bdf1   Cyrill Gorcunov   tty: Add get- ioc...
165
166
167
168
169
  static int pty_get_lock(struct tty_struct *tty, int __user *arg)
  {
  	int locked = test_bit(TTY_PTY_LOCK, &tty->flags);
  	return put_user(locked, arg);
  }
06026d911   Cyrill Gorcunov   tty: pty - Move T...
170
171
172
  /* Set the packet mode on a pty */
  static int pty_set_pktmode(struct tty_struct *tty, int __user *arg)
  {
06026d911   Cyrill Gorcunov   tty: pty - Move T...
173
174
175
176
  	int pktmode;
  
  	if (get_user(pktmode, arg))
  		return -EFAULT;
6054c16e8   Peter Hurley   tty: Use spin_loc...
177
  	spin_lock_irq(&tty->ctrl_lock);
06026d911   Cyrill Gorcunov   tty: pty - Move T...
178
179
  	if (pktmode) {
  		if (!tty->packet) {
06026d911   Cyrill Gorcunov   tty: pty - Move T...
180
  			tty->link->ctrl_status = 0;
2622d73e5   Peter Hurley   pty: Fix packet m...
181
182
  			smp_mb();
  			tty->packet = 1;
06026d911   Cyrill Gorcunov   tty: pty - Move T...
183
184
185
  		}
  	} else
  		tty->packet = 0;
6054c16e8   Peter Hurley   tty: Use spin_loc...
186
  	spin_unlock_irq(&tty->ctrl_lock);
06026d911   Cyrill Gorcunov   tty: pty - Move T...
187
188
189
  
  	return 0;
  }
84fd7bdf1   Cyrill Gorcunov   tty: Add get- ioc...
190
191
192
193
194
195
  /* Get the packet mode of a pty */
  static int pty_get_pktmode(struct tty_struct *tty, int __user *arg)
  {
  	int pktmode = tty->packet;
  	return put_user(pktmode, arg);
  }
26df6d134   hyc@symas.com   tty: Add EXTPROC ...
196
197
198
  /* Send a signal to the slave */
  static int pty_signal(struct tty_struct *tty, int sig)
  {
26df6d134   hyc@symas.com   tty: Add EXTPROC ...
199
  	struct pid *pgrp;
37480a056   Peter Hurley   tty: Prevent untr...
200
201
  	if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP)
  		return -EINVAL;
26df6d134   hyc@symas.com   tty: Add EXTPROC ...
202
  	if (tty->link) {
5b2395422   Peter Hurley   tty: Replace open...
203
204
205
  		pgrp = tty_get_pgrp(tty->link);
  		if (pgrp)
  			kill_pgrp(pgrp, sig, 1);
26df6d134   hyc@symas.com   tty: Add EXTPROC ...
206
207
208
209
  		put_pid(pgrp);
  	}
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
212
  static void pty_flush_buffer(struct tty_struct *tty)
  {
  	struct tty_struct *to = tty->link;
fe9cd962a   Alan Cox   pty: Coding style...
213

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
  	if (!to)
  		return;
1d1d14da1   Peter Hurley   pty: Fix buffer f...
216

77dae6134   Wang YanQing   tty: pty: Fix ldi...
217
  	tty_buffer_flush(to, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  	if (to->packet) {
6054c16e8   Peter Hurley   tty: Use spin_loc...
219
  		spin_lock_irq(&tty->ctrl_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
  		tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  		wake_up_interruptible(&to->read_wait);
6054c16e8   Peter Hurley   tty: Use spin_loc...
222
  		spin_unlock_irq(&tty->ctrl_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
224
  	}
  }
fe9cd962a   Alan Cox   pty: Coding style...
225
  static int pty_open(struct tty_struct *tty, struct file *filp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  	if (!tty || !tty->link)
7c61c3d8f   Peter Hurley   tty: Fix transien...
228
  		return -ENODEV;
699390354   Peter Hurley   pty: Ignore slave...
229

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
232
233
  	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  		goto out;
  	if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  		goto out;
80cace725   Peter Hurley   pty: Ignore slave...
234
  	if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
  		goto out;
699390354   Peter Hurley   pty: Ignore slave...
236
  	clear_bit(TTY_IO_ERROR, &tty->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
  	clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  	set_bit(TTY_THROTTLED, &tty->flags);
7c61c3d8f   Peter Hurley   tty: Fix transien...
239
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
  out:
7c61c3d8f   Peter Hurley   tty: Fix transien...
241
242
  	set_bit(TTY_IO_ERROR, &tty->flags);
  	return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  }
fe9cd962a   Alan Cox   pty: Coding style...
244
245
  static void pty_set_termios(struct tty_struct *tty,
  					struct ktermios *old_termios)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
  {
dbfcd851a   Peter Hurley   tty: Move pty-spe...
247
248
  	/* See if packet mode change of state. */
  	if (tty->link && tty->link->packet) {
9db276f8f   Peter Hurley   tty: Use termios ...
249
  		int extproc = (old_termios->c_lflag & EXTPROC) | L_EXTPROC(tty);
dbfcd851a   Peter Hurley   tty: Move pty-spe...
250
251
252
253
254
255
256
  		int old_flow = ((old_termios->c_iflag & IXON) &&
  				(old_termios->c_cc[VSTOP] == '\023') &&
  				(old_termios->c_cc[VSTART] == '\021'));
  		int new_flow = (I_IXON(tty) &&
  				STOP_CHAR(tty) == '\023' &&
  				START_CHAR(tty) == '\021');
  		if ((old_flow != new_flow) || extproc) {
4d8c1dff6   Peter Hurley   pty: Use spin_loc...
257
  			spin_lock_irq(&tty->ctrl_lock);
dbfcd851a   Peter Hurley   tty: Move pty-spe...
258
259
260
261
262
263
264
265
266
  			if (old_flow != new_flow) {
  				tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
  				if (new_flow)
  					tty->ctrl_status |= TIOCPKT_DOSTOP;
  				else
  					tty->ctrl_status |= TIOCPKT_NOSTOP;
  			}
  			if (extproc)
  				tty->ctrl_status |= TIOCPKT_IOCTL;
4d8c1dff6   Peter Hurley   pty: Use spin_loc...
267
  			spin_unlock_irq(&tty->ctrl_lock);
dbfcd851a   Peter Hurley   tty: Move pty-spe...
268
269
270
  			wake_up_interruptible(&tty->link->read_wait);
  		}
  	}
adc8d746c   Alan Cox   tty: move the ter...
271
272
  	tty->termios.c_cflag &= ~(CSIZE | PARENB);
  	tty->termios.c_cflag |= (CS8 | CREAD);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  }
fc6f62382   Alan Cox   pty: simplify resize
274
275
276
  /**
   *	pty_do_resize		-	resize event
   *	@tty: tty being resized
c774bda2f   Alan Cox   pty: Fix document...
277
   *	@ws: window size being set.
fc6f62382   Alan Cox   pty: simplify resize
278
   *
3ad2f3fbb   Daniel Mack   tree-wide: Assort...
279
   *	Update the termios variables and send the necessary signals to
fc6f62382   Alan Cox   pty: simplify resize
280
281
   *	peform a terminal resize correctly
   */
159a8e92f   Josh Triplett   pty: Mark pty_res...
282
  static int pty_resize(struct tty_struct *tty,  struct winsize *ws)
fc6f62382   Alan Cox   pty: simplify resize
283
284
  {
  	struct pid *pgrp, *rpgrp;
fc6f62382   Alan Cox   pty: simplify resize
285
286
287
  	struct tty_struct *pty = tty->link;
  
  	/* For a PTY we need to lock the tty side */
dee4a0be6   Peter Hurley   tty: Fix lock ord...
288
  	mutex_lock(&tty->winsize_mutex);
fc6f62382   Alan Cox   pty: simplify resize
289
290
  	if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  		goto done;
5b2395422   Peter Hurley   tty: Replace open...
291
292
293
  	/* Signal the foreground process group of both ptys */
  	pgrp = tty_get_pgrp(tty);
  	rpgrp = tty_get_pgrp(pty);
fc6f62382   Alan Cox   pty: simplify resize
294
295
296
297
298
299
300
301
302
303
304
305
  
  	if (pgrp)
  		kill_pgrp(pgrp, SIGWINCH, 1);
  	if (rpgrp != pgrp && rpgrp)
  		kill_pgrp(rpgrp, SIGWINCH, 1);
  
  	put_pid(pgrp);
  	put_pid(rpgrp);
  
  	tty->winsize = *ws;
  	pty->winsize = *ws;	/* Never used so will go away soon */
  done:
dee4a0be6   Peter Hurley   tty: Fix lock ord...
306
  	mutex_unlock(&tty->winsize_mutex);
fc6f62382   Alan Cox   pty: simplify resize
307
308
  	return 0;
  }
d155255a3   Alan Cox   tty: Fix race in ...
309
  /**
01adc8070   Peter Hurley   tty: Move packet ...
310
311
312
313
314
315
316
317
318
319
320
321
   *	pty_start - start() handler
   *	pty_stop  - stop() handler
   *	@tty: tty being flow-controlled
   *
   *	Propagates the TIOCPKT status to the master pty.
   *
   *	NB: only the master pty can be in packet mode so only the slave
   *	    needs start()/stop() handlers
   */
  static void pty_start(struct tty_struct *tty)
  {
  	unsigned long flags;
01adc8070   Peter Hurley   tty: Move packet ...
322
  	if (tty->link && tty->link->packet) {
54e8e5fca   Peter Hurley   pty: Don't claim ...
323
  		spin_lock_irqsave(&tty->ctrl_lock, flags);
01adc8070   Peter Hurley   tty: Move packet ...
324
325
  		tty->ctrl_status &= ~TIOCPKT_STOP;
  		tty->ctrl_status |= TIOCPKT_START;
54e8e5fca   Peter Hurley   pty: Don't claim ...
326
  		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
327
  		wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
01adc8070   Peter Hurley   tty: Move packet ...
328
  	}
01adc8070   Peter Hurley   tty: Move packet ...
329
330
331
332
333
  }
  
  static void pty_stop(struct tty_struct *tty)
  {
  	unsigned long flags;
01adc8070   Peter Hurley   tty: Move packet ...
334
  	if (tty->link && tty->link->packet) {
54e8e5fca   Peter Hurley   pty: Don't claim ...
335
  		spin_lock_irqsave(&tty->ctrl_lock, flags);
01adc8070   Peter Hurley   tty: Move packet ...
336
337
  		tty->ctrl_status &= ~TIOCPKT_START;
  		tty->ctrl_status |= TIOCPKT_STOP;
54e8e5fca   Peter Hurley   pty: Don't claim ...
338
  		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
339
  		wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN);
01adc8070   Peter Hurley   tty: Move packet ...
340
  	}
01adc8070   Peter Hurley   tty: Move packet ...
341
342
343
  }
  
  /**
d155255a3   Alan Cox   tty: Fix race in ...
344
345
346
   *	pty_common_install		-	set up the pty pair
   *	@driver: the pty driver
   *	@tty: the tty being instantiated
2c964a2f4   Rasmus Villemoes   drivers: tty: Mer...
347
   *	@legacy: true if this is BSD style
d155255a3   Alan Cox   tty: Fix race in ...
348
349
350
351
352
353
   *
   *	Perform the initial set up for the tty/pty pair. Called from the
   *	tty layer when the port is first opened.
   *
   *	Locking: the caller must hold the tty_mutex
   */
5d249bc6a   Jiri Slaby   PTY: merge pty_in...
354
355
  static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
  		bool legacy)
bf970ee46   Alan Cox   tty: extract the ...
356
357
  {
  	struct tty_struct *o_tty;
d03702a27   Jiri Slaby   PTY: add tty_port
358
  	struct tty_port *ports[2];
bf970ee46   Alan Cox   tty: extract the ...
359
  	int idx = tty->index;
5d249bc6a   Jiri Slaby   PTY: merge pty_in...
360
  	int retval = -ENOMEM;
bf970ee46   Alan Cox   tty: extract the ...
361

55199ea3b   Peter Hurley   pty: Always retur...
362
363
364
  	/* Opening the slave first has always returned -EIO */
  	if (driver->subtype != PTY_TYPE_MASTER)
  		return -EIO;
d03702a27   Jiri Slaby   PTY: add tty_port
365
366
  	ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
  	ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
6f9ea7ad7   Jiri Slaby   TTY: pty, stop pa...
367
  	if (!ports[0] || !ports[1])
2c964a2f4   Rasmus Villemoes   drivers: tty: Mer...
368
  		goto err;
bf970ee46   Alan Cox   tty: extract the ...
369
370
  	if (!try_module_get(driver->other->owner)) {
  		/* This cannot in fact currently happen */
2c964a2f4   Rasmus Villemoes   drivers: tty: Mer...
371
  		goto err;
bf970ee46   Alan Cox   tty: extract the ...
372
  	}
2c964a2f4   Rasmus Villemoes   drivers: tty: Mer...
373
374
375
  	o_tty = alloc_tty_struct(driver->other, idx);
  	if (!o_tty)
  		goto err_put_module;
bf970ee46   Alan Cox   tty: extract the ...
376

2febdb632   Peter Hurley   tty: Preset lock ...
377
  	tty_set_lock_subclass(o_tty);
d2b6f4477   Peter Hurley   n_tty: Fix signal...
378
  	lockdep_set_subclass(&o_tty->termios_rwsem, TTY_LOCK_SLAVE);
2febdb632   Peter Hurley   tty: Preset lock ...
379

5d249bc6a   Jiri Slaby   PTY: merge pty_in...
380
381
382
  	if (legacy) {
  		/* We always use new tty termios data so we can do this
  		   the easy way .. */
a3123fd0a   Peter Hurley   tty: Fix tty_init...
383
384
  		tty_init_termios(tty);
  		tty_init_termios(o_tty);
5d249bc6a   Jiri Slaby   PTY: merge pty_in...
385
386
387
388
  
  		driver->other->ttys[idx] = o_tty;
  		driver->ttys[idx] = tty;
  	} else {
adc8d746c   Alan Cox   tty: move the ter...
389
390
391
392
  		memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
  		tty->termios = driver->init_termios;
  		memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
  		o_tty->termios = driver->other->init_termios;
5d249bc6a   Jiri Slaby   PTY: merge pty_in...
393
  	}
fe9cd962a   Alan Cox   pty: Coding style...
394

bf970ee46   Alan Cox   tty: extract the ...
395
396
397
  	/*
  	 * Everything allocated ... set up the o_tty structure.
  	 */
bf970ee46   Alan Cox   tty: extract the ...
398
  	tty_driver_kref_get(driver->other);
bf970ee46   Alan Cox   tty: extract the ...
399
400
401
  	/* Establish the links in both directions */
  	tty->link   = o_tty;
  	o_tty->link = tty;
d03702a27   Jiri Slaby   PTY: add tty_port
402
403
  	tty_port_init(ports[0]);
  	tty_port_init(ports[1]);
c81622ac4   Peter Hurley   pty: Fix overlimi...
404
405
  	tty_buffer_set_limit(ports[0], 8192);
  	tty_buffer_set_limit(ports[1], 8192);
d03702a27   Jiri Slaby   PTY: add tty_port
406
407
  	o_tty->port = ports[0];
  	tty->port = ports[1];
967fab691   Jiri Slaby   TTY: add port -> ...
408
  	o_tty->port->itty = o_tty;
bf970ee46   Alan Cox   tty: extract the ...
409

1d1d14da1   Peter Hurley   pty: Fix buffer f...
410
  	tty_buffer_set_lock_subclass(o_tty->port);
bf970ee46   Alan Cox   tty: extract the ...
411
412
  	tty_driver_kref_get(driver);
  	tty->count++;
55199ea3b   Peter Hurley   pty: Always retur...
413
  	o_tty->count++;
bf970ee46   Alan Cox   tty: extract the ...
414
  	return 0;
a3123fd0a   Peter Hurley   tty: Fix tty_init...
415

2c964a2f4   Rasmus Villemoes   drivers: tty: Mer...
416
  err_put_module:
07584d4a3   Rasmus Villemoes   drivers: tty: Fix...
417
  	module_put(driver->other->owner);
2c964a2f4   Rasmus Villemoes   drivers: tty: Mer...
418
  err:
d03702a27   Jiri Slaby   PTY: add tty_port
419
420
  	kfree(ports[0]);
  	kfree(ports[1]);
8a1b8d70a   Jiri Slaby   TTY: unify pty_in...
421
  	return retval;
bf970ee46   Alan Cox   tty: extract the ...
422
  }
d03702a27   Jiri Slaby   PTY: add tty_port
423
424
  static void pty_cleanup(struct tty_struct *tty)
  {
81c79838c   Jiri Slaby   TTY: pty, fix tty...
425
  	tty_port_put(tty->port);
d03702a27   Jiri Slaby   PTY: add tty_port
426
  }
5d249bc6a   Jiri Slaby   PTY: merge pty_in...
427
428
429
430
431
432
433
  /* Traditional BSD devices */
  #ifdef CONFIG_LEGACY_PTYS
  
  static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  {
  	return pty_common_install(driver, tty, true);
  }
d155255a3   Alan Cox   tty: Fix race in ...
434
435
436
437
438
439
440
  static void pty_remove(struct tty_driver *driver, struct tty_struct *tty)
  {
  	struct tty_struct *pair = tty->link;
  	driver->ttys[tty->index] = NULL;
  	if (pair)
  		pair->driver->ttys[pair->index] = NULL;
  }
6caa76b77   Alan Cox   tty: now phase ou...
441
  static int pty_bsd_ioctl(struct tty_struct *tty,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
443
444
445
446
  			 unsigned int cmd, unsigned long arg)
  {
  	switch (cmd) {
  	case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  		return pty_set_lock(tty, (int __user *) arg);
84fd7bdf1   Cyrill Gorcunov   tty: Add get- ioc...
447
448
  	case TIOCGPTLCK: /* Get PT Lock status */
  		return pty_get_lock(tty, (int __user *)arg);
06026d911   Cyrill Gorcunov   tty: pty - Move T...
449
450
  	case TIOCPKT: /* Set PT packet mode */
  		return pty_set_pktmode(tty, (int __user *)arg);
84fd7bdf1   Cyrill Gorcunov   tty: Add get- ioc...
451
452
  	case TIOCGPKT: /* Get PT packet mode */
  		return pty_get_pktmode(tty, (int __user *)arg);
26df6d134   hyc@symas.com   tty: Add EXTPROC ...
453
454
  	case TIOCSIG:    /* Send signal to other side of pty */
  		return pty_signal(tty, (int) arg);
ded2f295a   Jiri Slaby   pty: return EINVA...
455
456
  	case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
457
458
459
  	}
  	return -ENOIOCTLCMD;
  }
50f45326a   Al Viro   pty: fix compat i...
460
  #ifdef CONFIG_COMPAT
5f0f187fd   Aleksa Sarai   tty: add compat_i...
461
462
463
464
465
466
467
  static long pty_bsd_compat_ioctl(struct tty_struct *tty,
  				 unsigned int cmd, unsigned long arg)
  {
  	/*
  	 * PTY ioctls don't require any special translation between 32-bit and
  	 * 64-bit userspace, they are already compatible.
  	 */
50f45326a   Al Viro   pty: fix compat i...
468
  	return pty_bsd_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
5f0f187fd   Aleksa Sarai   tty: add compat_i...
469
  }
50f45326a   Al Viro   pty: fix compat i...
470
471
472
  #else
  #define pty_bsd_compat_ioctl NULL
  #endif
5f0f187fd   Aleksa Sarai   tty: add compat_i...
473

dc8c85871   Kay Sievers   PTY: add kernel p...
474
  static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
7154988fe   Paul Gortmaker   drivers/tty: make...
475
476
477
478
  /*
   * not really modular, but the easiest way to keep compat with existing
   * bootargs behaviour is to continue using module_param here.
   */
dc8c85871   Kay Sievers   PTY: add kernel p...
479
  module_param(legacy_count, int, 0);
342a59714   Linus Torvalds   pty: reconnect th...
480
481
482
483
484
485
  /*
   * The master side of a pty can do TIOCSPTLCK and thus
   * has pty_bsd_ioctl.
   */
  static const struct tty_operations master_pty_ops_bsd = {
  	.install = pty_install,
3e8e88ca0   Alan Cox   pty: prepare for ...
486
487
488
489
490
491
492
  	.open = pty_open,
  	.close = pty_close,
  	.write = pty_write,
  	.write_room = pty_write_room,
  	.flush_buffer = pty_flush_buffer,
  	.chars_in_buffer = pty_chars_in_buffer,
  	.unthrottle = pty_unthrottle,
3e8e88ca0   Alan Cox   pty: prepare for ...
493
  	.ioctl = pty_bsd_ioctl,
5f0f187fd   Aleksa Sarai   tty: add compat_i...
494
  	.compat_ioctl = pty_bsd_compat_ioctl,
d03702a27   Jiri Slaby   PTY: add tty_port
495
  	.cleanup = pty_cleanup,
d155255a3   Alan Cox   tty: Fix race in ...
496
497
  	.resize = pty_resize,
  	.remove = pty_remove
3e8e88ca0   Alan Cox   pty: prepare for ...
498
  };
342a59714   Linus Torvalds   pty: reconnect th...
499
500
501
502
503
504
505
506
507
508
  static const struct tty_operations slave_pty_ops_bsd = {
  	.install = pty_install,
  	.open = pty_open,
  	.close = pty_close,
  	.write = pty_write,
  	.write_room = pty_write_room,
  	.flush_buffer = pty_flush_buffer,
  	.chars_in_buffer = pty_chars_in_buffer,
  	.unthrottle = pty_unthrottle,
  	.set_termios = pty_set_termios,
d03702a27   Jiri Slaby   PTY: add tty_port
509
  	.cleanup = pty_cleanup,
d155255a3   Alan Cox   tty: Fix race in ...
510
  	.resize = pty_resize,
01adc8070   Peter Hurley   tty: Move packet ...
511
512
  	.start = pty_start,
  	.stop = pty_stop,
d155255a3   Alan Cox   tty: Fix race in ...
513
  	.remove = pty_remove
342a59714   Linus Torvalds   pty: reconnect th...
514
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
  static void __init legacy_pty_init(void)
  {
342a59714   Linus Torvalds   pty: reconnect th...
517
  	struct tty_driver *pty_driver, *pty_slave_driver;
dc8c85871   Kay Sievers   PTY: add kernel p...
518
519
  	if (legacy_count <= 0)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520

21aca2fa0   Jiri Slaby   TTY: pty, switch ...
521
522
523
524
  	pty_driver = tty_alloc_driver(legacy_count,
  			TTY_DRIVER_RESET_TERMIOS |
  			TTY_DRIVER_REAL_RAW |
  			TTY_DRIVER_DYNAMIC_ALLOC);
c3a6344ae   Dan Carpenter   TTY: tty_alloc_dr...
525
  	if (IS_ERR(pty_driver))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
  		panic("Couldn't allocate pty driver");
21aca2fa0   Jiri Slaby   TTY: pty, switch ...
527
528
529
530
  	pty_slave_driver = tty_alloc_driver(legacy_count,
  			TTY_DRIVER_RESET_TERMIOS |
  			TTY_DRIVER_REAL_RAW |
  			TTY_DRIVER_DYNAMIC_ALLOC);
c3a6344ae   Dan Carpenter   TTY: tty_alloc_dr...
531
  	if (IS_ERR(pty_slave_driver))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532
  		panic("Couldn't allocate pty slave driver");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
533
534
  	pty_driver->driver_name = "pty_master";
  	pty_driver->name = "pty";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
536
537
538
539
540
541
542
543
  	pty_driver->major = PTY_MASTER_MAJOR;
  	pty_driver->minor_start = 0;
  	pty_driver->type = TTY_DRIVER_TYPE_PTY;
  	pty_driver->subtype = PTY_TYPE_MASTER;
  	pty_driver->init_termios = tty_std_termios;
  	pty_driver->init_termios.c_iflag = 0;
  	pty_driver->init_termios.c_oflag = 0;
  	pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  	pty_driver->init_termios.c_lflag = 0;
606d099cd   Alan Cox   [PATCH] tty: swit...
544
545
  	pty_driver->init_termios.c_ispeed = 38400;
  	pty_driver->init_termios.c_ospeed = 38400;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
  	pty_driver->other = pty_slave_driver;
342a59714   Linus Torvalds   pty: reconnect th...
547
  	tty_set_operations(pty_driver, &master_pty_ops_bsd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549
550
  	pty_slave_driver->driver_name = "pty_slave";
  	pty_slave_driver->name = "ttyp";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
552
553
554
555
556
  	pty_slave_driver->major = PTY_SLAVE_MAJOR;
  	pty_slave_driver->minor_start = 0;
  	pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  	pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  	pty_slave_driver->init_termios = tty_std_termios;
  	pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
606d099cd   Alan Cox   [PATCH] tty: swit...
557
558
  	pty_slave_driver->init_termios.c_ispeed = 38400;
  	pty_slave_driver->init_termios.c_ospeed = 38400;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
  	pty_slave_driver->other = pty_driver;
342a59714   Linus Torvalds   pty: reconnect th...
560
  	tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
562
563
564
565
566
567
568
569
570
571
572
  
  	if (tty_register_driver(pty_driver))
  		panic("Couldn't register pty driver");
  	if (tty_register_driver(pty_slave_driver))
  		panic("Couldn't register pty slave driver");
  }
  #else
  static inline void legacy_pty_init(void) { }
  #endif
  
  /* Unix98 devices */
  #ifdef CONFIG_UNIX98_PTYS
d81ed1030   Alan Cox   tty: Remove more ...
573
  static struct cdev ptmx_cdev;
6509f3096   Arnd Bergmann   tty: hide unused ...
574
  /**
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
575
576
577
578
   *	ptm_open_peer - open the peer of a pty
   *	@master: the open struct file of the ptmx device node
   *	@tty: the master of the pty being opened
   *	@flags: the flags for open
6509f3096   Arnd Bergmann   tty: hide unused ...
579
   *
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
580
581
582
   *	Provide a race free way for userspace to open the slave end of a pty
   *	(where they have the master fd and cannot access or trust the mount
   *	namespace /dev/pts was mounted inside).
6509f3096   Arnd Bergmann   tty: hide unused ...
583
   */
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
584
  int ptm_open_peer(struct file *master, struct tty_struct *tty, int flags)
6509f3096   Arnd Bergmann   tty: hide unused ...
585
586
  {
  	int fd = -1;
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
587
  	struct file *filp;
6509f3096   Arnd Bergmann   tty: hide unused ...
588
  	int retval = -EINVAL;
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
589
590
591
592
  	struct path path;
  
  	if (tty->driver != ptm_driver)
  		return -EIO;
6509f3096   Arnd Bergmann   tty: hide unused ...
593

36ecc1481   Matthijs van Duin   pty: fix O_CLOEXE...
594
  	fd = get_unused_fd_flags(flags);
6509f3096   Arnd Bergmann   tty: hide unused ...
595
596
597
598
  	if (fd < 0) {
  		retval = fd;
  		goto err;
  	}
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
599
600
601
602
603
604
605
606
607
608
  	/* Compute the slave's path */
  	path.mnt = devpts_mntget(master, tty->driver_data);
  	if (IS_ERR(path.mnt)) {
  		retval = PTR_ERR(path.mnt);
  		goto err_put;
  	}
  	path.dentry = tty->link->driver_data;
  
  	filp = dentry_open(&path, flags, current_cred());
  	mntput(path.mnt);
6509f3096   Arnd Bergmann   tty: hide unused ...
609
610
611
612
613
614
615
616
617
618
619
620
621
  	if (IS_ERR(filp)) {
  		retval = PTR_ERR(filp);
  		goto err_put;
  	}
  
  	fd_install(fd, filp);
  	return fd;
  
  err_put:
  	put_unused_fd(fd);
  err:
  	return retval;
  }
6caa76b77   Alan Cox   tty: now phase ou...
622
  static int pty_unix98_ioctl(struct tty_struct *tty,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623
624
625
626
627
  			    unsigned int cmd, unsigned long arg)
  {
  	switch (cmd) {
  	case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  		return pty_set_lock(tty, (int __user *)arg);
84fd7bdf1   Cyrill Gorcunov   tty: Add get- ioc...
628
629
  	case TIOCGPTLCK: /* Get PT Lock status */
  		return pty_get_lock(tty, (int __user *)arg);
06026d911   Cyrill Gorcunov   tty: pty - Move T...
630
631
  	case TIOCPKT: /* Set PT packet mode */
  		return pty_set_pktmode(tty, (int __user *)arg);
84fd7bdf1   Cyrill Gorcunov   tty: Add get- ioc...
632
633
  	case TIOCGPKT: /* Get PT packet mode */
  		return pty_get_pktmode(tty, (int __user *)arg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
635
  	case TIOCGPTN: /* Get PT Number */
  		return put_user(tty->index, (unsigned int __user *)arg);
26df6d134   hyc@symas.com   tty: Add EXTPROC ...
636
637
  	case TIOCSIG:    /* Send signal to other side of pty */
  		return pty_signal(tty, (int) arg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
638
639
640
641
  	}
  
  	return -ENOIOCTLCMD;
  }
50f45326a   Al Viro   pty: fix compat i...
642
  #ifdef CONFIG_COMPAT
5f0f187fd   Aleksa Sarai   tty: add compat_i...
643
644
645
646
647
648
649
  static long pty_unix98_compat_ioctl(struct tty_struct *tty,
  				 unsigned int cmd, unsigned long arg)
  {
  	/*
  	 * PTY ioctls don't require any special translation between 32-bit and
  	 * 64-bit userspace, they are already compatible.
  	 */
50f45326a   Al Viro   pty: fix compat i...
650
651
  	return pty_unix98_ioctl(tty, cmd,
  		cmd == TIOCSIG ? arg : (unsigned long)compat_ptr(arg));
5f0f187fd   Aleksa Sarai   tty: add compat_i...
652
  }
50f45326a   Al Viro   pty: fix compat i...
653
654
655
  #else
  #define pty_unix98_compat_ioctl NULL
  #endif
5f0f187fd   Aleksa Sarai   tty: add compat_i...
656

99f1fe189   Alan Cox   tty: Clean up the...
657
658
659
660
661
662
663
664
  /**
   *	ptm_unix98_lookup	-	find a pty master
   *	@driver: ptm driver
   *	@idx: tty index
   *
   *	Look up a pty master device. Called under the tty_mutex for now.
   *	This provides our locking.
   */
15f1a6338   Sukadev Bhattiprolu   Add an instance p...
665
  static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
8ead9dd54   Linus Torvalds   devpts: more pty ...
666
  		struct file *file, int idx)
99f1fe189   Alan Cox   tty: Clean up the...
667
  {
593a27c4b   Konstantin Khlebnikov   tty: cleanup proh...
668
669
  	/* Master must be open via /dev/ptmx */
  	return ERR_PTR(-EIO);
99f1fe189   Alan Cox   tty: Clean up the...
670
671
672
673
674
675
676
677
  }
  
  /**
   *	pts_unix98_lookup	-	find a pty slave
   *	@driver: pts driver
   *	@idx: tty index
   *
   *	Look up a pty master device. Called under the tty_mutex for now.
d739e65bb   Alan Cox   pty: Lock the dev...
678
   *	This provides our locking for the tty pointer.
99f1fe189   Alan Cox   tty: Clean up the...
679
   */
15f1a6338   Sukadev Bhattiprolu   Add an instance p...
680
  static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
8ead9dd54   Linus Torvalds   devpts: more pty ...
681
  		struct file *file, int idx)
99f1fe189   Alan Cox   tty: Clean up the...
682
  {
d739e65bb   Alan Cox   pty: Lock the dev...
683
684
685
  	struct tty_struct *tty;
  
  	mutex_lock(&devpts_mutex);
8ead9dd54   Linus Torvalds   devpts: more pty ...
686
  	tty = devpts_get_priv(file->f_path.dentry);
d739e65bb   Alan Cox   pty: Lock the dev...
687
  	mutex_unlock(&devpts_mutex);
99f1fe189   Alan Cox   tty: Clean up the...
688
689
690
691
692
  	/* Master must be open before slave */
  	if (!tty)
  		return ERR_PTR(-EIO);
  	return tty;
  }
bf970ee46   Alan Cox   tty: extract the ...
693
  static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
8b0a88d59   Alan Cox   tty: More driver ...
694
  {
5d249bc6a   Jiri Slaby   PTY: merge pty_in...
695
  	return pty_common_install(driver, tty, false);
8b0a88d59   Alan Cox   tty: More driver ...
696
  }
f4b208eb9   Jiri Slaby   TTY: pty, fix com...
697
  /* this is called once with whichever end is closed last */
c1e33af1e   Peter Hurley   pty: Remove pty_u...
698
  static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
f4b208eb9   Jiri Slaby   TTY: pty, fix com...
699
  {
67245ff33   Linus Torvalds   devpts: clean up ...
700
  	struct pts_fs_info *fsi;
2831c89f4   Herton R. Krzesinski   pty: fix possible...
701
702
  
  	if (tty->driver->subtype == PTY_TYPE_MASTER)
67245ff33   Linus Torvalds   devpts: clean up ...
703
  		fsi = tty->driver_data;
2831c89f4   Herton R. Krzesinski   pty: fix possible...
704
  	else
67245ff33   Linus Torvalds   devpts: clean up ...
705
  		fsi = tty->link->driver_data;
5353ed8de   Colin Ian King   devpts: fix null ...
706
707
708
709
710
  
  	if (fsi) {
  		devpts_kill_index(fsi, tty->index);
  		devpts_release(fsi);
  	}
f4b208eb9   Jiri Slaby   TTY: pty, fix com...
711
  }
d01c3289e   Masatake YAMATO   pty: show associa...
712
713
714
715
716
  static void pty_show_fdinfo(struct tty_struct *tty, struct seq_file *m)
  {
  	seq_printf(m, "tty-index:\t%d
  ", tty->index);
  }
feebed651   Alan Cox   tty: shutdown method
717
  static const struct tty_operations ptm_unix98_ops = {
99f1fe189   Alan Cox   tty: Clean up the...
718
  	.lookup = ptm_unix98_lookup,
bf970ee46   Alan Cox   tty: extract the ...
719
  	.install = pty_unix98_install,
7171604ae   Jiri Slaby   PTY: remove one e...
720
  	.remove = pty_unix98_remove,
3e8e88ca0   Alan Cox   pty: prepare for ...
721
722
723
724
725
726
727
  	.open = pty_open,
  	.close = pty_close,
  	.write = pty_write,
  	.write_room = pty_write_room,
  	.flush_buffer = pty_flush_buffer,
  	.chars_in_buffer = pty_chars_in_buffer,
  	.unthrottle = pty_unthrottle,
feebed651   Alan Cox   tty: shutdown method
728
  	.ioctl = pty_unix98_ioctl,
5f0f187fd   Aleksa Sarai   tty: add compat_i...
729
  	.compat_ioctl = pty_unix98_compat_ioctl,
36b3c070d   Alan Cox   tty: Move the han...
730
  	.resize = pty_resize,
d01c3289e   Masatake YAMATO   pty: show associa...
731
732
  	.cleanup = pty_cleanup,
  	.show_fdinfo = pty_show_fdinfo,
3e8e88ca0   Alan Cox   pty: prepare for ...
733
  };
99f1fe189   Alan Cox   tty: Clean up the...
734
735
  static const struct tty_operations pty_unix98_ops = {
  	.lookup = pts_unix98_lookup,
bf970ee46   Alan Cox   tty: extract the ...
736
  	.install = pty_unix98_install,
7171604ae   Jiri Slaby   PTY: remove one e...
737
  	.remove = pty_unix98_remove,
99f1fe189   Alan Cox   tty: Clean up the...
738
739
740
741
742
743
744
745
  	.open = pty_open,
  	.close = pty_close,
  	.write = pty_write,
  	.write_room = pty_write_room,
  	.flush_buffer = pty_flush_buffer,
  	.chars_in_buffer = pty_chars_in_buffer,
  	.unthrottle = pty_unthrottle,
  	.set_termios = pty_set_termios,
01adc8070   Peter Hurley   tty: Move packet ...
746
747
  	.start = pty_start,
  	.stop = pty_stop,
d03702a27   Jiri Slaby   PTY: add tty_port
748
  	.cleanup = pty_cleanup,
99f1fe189   Alan Cox   tty: Clean up the...
749
  };
d81ed1030   Alan Cox   tty: Remove more ...
750
751
752
753
754
755
756
757
758
  
  /**
   *	ptmx_open		-	open a unix 98 pty master
   *	@inode: inode of device file
   *	@filp: file pointer to tty
   *
   *	Allocate a unix98 pty master device from the ptmx driver.
   *
   *	Locking: tty_mutex protects the init_dev work. tty->count should
b9f8033f2   Cong Ding   tty: cleanup chec...
759
   *		protect the rest.
d81ed1030   Alan Cox   tty: Remove more ...
760
761
   *		allocated_ptys_lock handles the list of free pty numbers
   */
64ba3dc31   Arnd Bergmann   tty: never hold B...
762
  static int ptmx_open(struct inode *inode, struct file *filp)
d81ed1030   Alan Cox   tty: Remove more ...
763
  {
67245ff33   Linus Torvalds   devpts: clean up ...
764
  	struct pts_fs_info *fsi;
d81ed1030   Alan Cox   tty: Remove more ...
765
  	struct tty_struct *tty;
8ead9dd54   Linus Torvalds   devpts: more pty ...
766
  	struct dentry *dentry;
d81ed1030   Alan Cox   tty: Remove more ...
767
768
769
770
  	int retval;
  	int index;
  
  	nonseekable_open(inode, filp);
b0b885657   Linus Torvalds   tty: fix up atime...
771
772
  	/* We refuse fsnotify events on ptmx, since it's a shared resource */
  	filp->f_mode |= FMODE_NONOTIFY;
fa90e1c93   Jiri Slaby   TTY: make tty_add...
773
774
775
  	retval = tty_alloc_file(filp);
  	if (retval)
  		return retval;
143c97cc6   Linus Torvalds   Revert "pty: fix ...
776
  	fsi = devpts_acquire(filp);
eedf265aa   Eric W. Biederman   devpts: Make each...
777
778
  	if (IS_ERR(fsi)) {
  		retval = PTR_ERR(fsi);
67245ff33   Linus Torvalds   devpts: clean up ...
779
  		goto out_free_file;
eedf265aa   Eric W. Biederman   devpts: Make each...
780
  	}
67245ff33   Linus Torvalds   devpts: clean up ...
781

d81ed1030   Alan Cox   tty: Remove more ...
782
  	/* find a device that is not in use. */
89c8d91e3   Alan Cox   tty: localise the...
783
  	mutex_lock(&devpts_mutex);
67245ff33   Linus Torvalds   devpts: clean up ...
784
  	index = devpts_new_index(fsi);
89c8d91e3   Alan Cox   tty: localise the...
785
  	mutex_unlock(&devpts_mutex);
67245ff33   Linus Torvalds   devpts: clean up ...
786
787
  	retval = index;
  	if (index < 0)
eedf265aa   Eric W. Biederman   devpts: Make each...
788
  		goto out_put_fsi;
d81ed1030   Alan Cox   tty: Remove more ...
789

d81ed1030   Alan Cox   tty: Remove more ...
790

67245ff33   Linus Torvalds   devpts: clean up ...
791
792
  	mutex_lock(&tty_mutex);
  	tty = tty_init_dev(ptm_driver, index);
89c8d91e3   Alan Cox   tty: localise the...
793
794
795
  	/* The tty returned here is locked so we can safely
  	   drop the mutex */
  	mutex_unlock(&tty_mutex);
67245ff33   Linus Torvalds   devpts: clean up ...
796
797
798
  	retval = PTR_ERR(tty);
  	if (IS_ERR(tty))
  		goto out;
ee2ffa0df   Nick Piggin   fs: cleanup files...
799

2831c89f4   Herton R. Krzesinski   pty: fix possible...
800
  	/*
67245ff33   Linus Torvalds   devpts: clean up ...
801
802
  	 * From here on out, the tty is "live", and the index and
  	 * fsi will be killed/put by the tty_release()
2831c89f4   Herton R. Krzesinski   pty: fix possible...
803
  	 */
67245ff33   Linus Torvalds   devpts: clean up ...
804
805
  	set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  	tty->driver_data = fsi;
2831c89f4   Herton R. Krzesinski   pty: fix possible...
806

fa90e1c93   Jiri Slaby   TTY: make tty_add...
807
  	tty_add_file(tty, filp);
d81ed1030   Alan Cox   tty: Remove more ...
808

8ead9dd54   Linus Torvalds   devpts: more pty ...
809
810
811
  	dentry = devpts_pty_new(fsi, index, tty->link);
  	if (IS_ERR(dentry)) {
  		retval = PTR_ERR(dentry);
1177c0efc   Jiri Slaby   TTY: pty, release...
812
  		goto err_release;
162b97cfa   Jiri Slaby   TTY: devpts, retu...
813
  	}
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
814
  	tty->link->driver_data = dentry;
d81ed1030   Alan Cox   tty: Remove more ...
815
816
  
  	retval = ptm_driver->ops->open(tty, filp);
64ba3dc31   Arnd Bergmann   tty: never hold B...
817
  	if (retval)
311fc65c9   Eric W. Biederman   pty: Repair TIOCG...
818
  		goto err_release;
1177c0efc   Jiri Slaby   TTY: pty, release...
819

d435cefe9   Peter Hurley   tty: Remove __fun...
820
821
  	tty_debug_hangup(tty, "opening (count=%d)
  ", tty->count);
d68477933   Peter Hurley   pty: Add debug me...
822

89c8d91e3   Alan Cox   tty: localise the...
823
  	tty_unlock(tty);
1177c0efc   Jiri Slaby   TTY: pty, release...
824
825
  	return 0;
  err_release:
89c8d91e3   Alan Cox   tty: localise the...
826
  	tty_unlock(tty);
67245ff33   Linus Torvalds   devpts: clean up ...
827
  	// This will also put-ref the fsi
eeb89d918   Alan Cox   tty: push the BKL...
828
  	tty_release(inode, filp);
d81ed1030   Alan Cox   tty: Remove more ...
829
830
  	return retval;
  out:
67245ff33   Linus Torvalds   devpts: clean up ...
831
  	devpts_kill_index(fsi, index);
eedf265aa   Eric W. Biederman   devpts: Make each...
832
833
  out_put_fsi:
  	devpts_release(fsi);
67245ff33   Linus Torvalds   devpts: clean up ...
834
  out_free_file:
fa90e1c93   Jiri Slaby   TTY: make tty_add...
835
  	tty_free_file(filp);
64ba3dc31   Arnd Bergmann   tty: never hold B...
836
  	return retval;
d81ed1030   Alan Cox   tty: Remove more ...
837
  }
d2ec3f77d   Kees Cook   pty: make ptmx fi...
838
  static struct file_operations ptmx_fops __ro_after_init;
d81ed1030   Alan Cox   tty: Remove more ...
839

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
840
841
  static void __init unix98_pty_init(void)
  {
21aca2fa0   Jiri Slaby   TTY: pty, switch ...
842
843
844
845
846
847
  	ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  			TTY_DRIVER_RESET_TERMIOS |
  			TTY_DRIVER_REAL_RAW |
  			TTY_DRIVER_DYNAMIC_DEV |
  			TTY_DRIVER_DEVPTS_MEM |
  			TTY_DRIVER_DYNAMIC_ALLOC);
c3a6344ae   Dan Carpenter   TTY: tty_alloc_dr...
848
  	if (IS_ERR(ptm_driver))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
849
  		panic("Couldn't allocate Unix98 ptm driver");
21aca2fa0   Jiri Slaby   TTY: pty, switch ...
850
851
852
853
854
855
  	pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX,
  			TTY_DRIVER_RESET_TERMIOS |
  			TTY_DRIVER_REAL_RAW |
  			TTY_DRIVER_DYNAMIC_DEV |
  			TTY_DRIVER_DEVPTS_MEM |
  			TTY_DRIVER_DYNAMIC_ALLOC);
c3a6344ae   Dan Carpenter   TTY: tty_alloc_dr...
856
  	if (IS_ERR(pts_driver))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
857
  		panic("Couldn't allocate Unix98 pts driver");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
858
859
860
861
862
863
864
865
866
867
868
  	ptm_driver->driver_name = "pty_master";
  	ptm_driver->name = "ptm";
  	ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  	ptm_driver->minor_start = 0;
  	ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  	ptm_driver->subtype = PTY_TYPE_MASTER;
  	ptm_driver->init_termios = tty_std_termios;
  	ptm_driver->init_termios.c_iflag = 0;
  	ptm_driver->init_termios.c_oflag = 0;
  	ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  	ptm_driver->init_termios.c_lflag = 0;
606d099cd   Alan Cox   [PATCH] tty: swit...
869
870
  	ptm_driver->init_termios.c_ispeed = 38400;
  	ptm_driver->init_termios.c_ospeed = 38400;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
871
  	ptm_driver->other = pts_driver;
feebed651   Alan Cox   tty: shutdown method
872
  	tty_set_operations(ptm_driver, &ptm_unix98_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
873

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
874
875
876
877
878
879
880
881
  	pts_driver->driver_name = "pty_slave";
  	pts_driver->name = "pts";
  	pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  	pts_driver->minor_start = 0;
  	pts_driver->type = TTY_DRIVER_TYPE_PTY;
  	pts_driver->subtype = PTY_TYPE_SLAVE;
  	pts_driver->init_termios = tty_std_termios;
  	pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
606d099cd   Alan Cox   [PATCH] tty: swit...
882
883
  	pts_driver->init_termios.c_ispeed = 38400;
  	pts_driver->init_termios.c_ospeed = 38400;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
884
  	pts_driver->other = ptm_driver;
99f1fe189   Alan Cox   tty: Clean up the...
885
  	tty_set_operations(pts_driver, &pty_unix98_ops);
fe9cd962a   Alan Cox   pty: Coding style...
886

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887
888
889
890
  	if (tty_register_driver(ptm_driver))
  		panic("Couldn't register Unix98 ptm driver");
  	if (tty_register_driver(pts_driver))
  		panic("Couldn't register Unix98 pts driver");
d81ed1030   Alan Cox   tty: Remove more ...
891
892
893
894
895
896
897
  	/* Now create the /dev/ptmx special device */
  	tty_default_fops(&ptmx_fops);
  	ptmx_fops.open = ptmx_open;
  
  	cdev_init(&ptmx_cdev, &ptmx_fops);
  	if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  	    register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
82f8c35f8   Cong Ding   tty: cleanup the ...
898
  		panic("Couldn't register /dev/ptmx driver");
d81ed1030   Alan Cox   tty: Remove more ...
899
  	device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900
  }
d81ed1030   Alan Cox   tty: Remove more ...
901

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
902
903
904
905
906
907
908
909
910
911
  #else
  static inline void unix98_pty_init(void) { }
  #endif
  
  static int __init pty_init(void)
  {
  	legacy_pty_init();
  	unix98_pty_init();
  	return 0;
  }
7154988fe   Paul Gortmaker   drivers/tty: make...
912
  device_initcall(pty_init);