Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * 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 | |
1da177e4 | 6 | * |
1da177e4 LT |
7 | */ |
8 | ||
fe9cd962 | 9 | #include <linux/module.h> |
1da177e4 LT |
10 | |
11 | #include <linux/errno.h> | |
1da177e4 LT |
12 | #include <linux/interrupt.h> |
13 | #include <linux/tty.h> | |
14 | #include <linux/tty_flip.h> | |
15 | #include <linux/fcntl.h> | |
d43c36dc | 16 | #include <linux/sched.h> |
1da177e4 LT |
17 | #include <linux/string.h> |
18 | #include <linux/major.h> | |
19 | #include <linux/mm.h> | |
20 | #include <linux/init.h> | |
d81ed103 | 21 | #include <linux/device.h> |
fe9cd962 | 22 | #include <linux/uaccess.h> |
1da177e4 LT |
23 | #include <linux/bitops.h> |
24 | #include <linux/devpts_fs.h> | |
5a0e3ad6 | 25 | #include <linux/slab.h> |
d739e65b | 26 | #include <linux/mutex.h> |
1da177e4 | 27 | |
1da177e4 | 28 | #ifdef CONFIG_UNIX98_PTYS |
da2bdf9a | 29 | static struct tty_driver *ptm_driver; |
1da177e4 | 30 | static struct tty_driver *pts_driver; |
d739e65b | 31 | static DEFINE_MUTEX(devpts_mutex); |
1da177e4 LT |
32 | #endif |
33 | ||
fe9cd962 | 34 | static void pty_close(struct tty_struct *tty, struct file *filp) |
1da177e4 | 35 | { |
fe9cd962 AC |
36 | BUG_ON(!tty); |
37 | if (tty->driver->subtype == PTY_TYPE_MASTER) | |
38 | WARN_ON(tty->count > 1); | |
39 | else { | |
69939035 PH |
40 | if (test_bit(TTY_IO_ERROR, &tty->flags)) |
41 | return; | |
1da177e4 LT |
42 | if (tty->count > 2) |
43 | return; | |
44 | } | |
69939035 | 45 | set_bit(TTY_IO_ERROR, &tty->flags); |
1da177e4 LT |
46 | wake_up_interruptible(&tty->read_wait); |
47 | wake_up_interruptible(&tty->write_wait); | |
48 | tty->packet = 0; | |
89c8d91e | 49 | /* Review - krefs on tty_link ?? */ |
1da177e4 LT |
50 | if (!tty->link) |
51 | return; | |
1da177e4 LT |
52 | set_bit(TTY_OTHER_CLOSED, &tty->link->flags); |
53 | wake_up_interruptible(&tty->link->read_wait); | |
54 | wake_up_interruptible(&tty->link->write_wait); | |
55 | if (tty->driver->subtype == PTY_TYPE_MASTER) { | |
56 | set_bit(TTY_OTHER_CLOSED, &tty->flags); | |
ce1000dd | 57 | #ifdef CONFIG_UNIX98_PTYS |
d739e65b | 58 | if (tty->driver == ptm_driver) { |
b9f8033f | 59 | mutex_lock(&devpts_mutex); |
7acf6cd8 PH |
60 | if (tty->link->driver_data) |
61 | devpts_pty_kill(tty->link->driver_data); | |
b9f8033f | 62 | mutex_unlock(&devpts_mutex); |
d739e65b | 63 | } |
ce1000dd | 64 | #endif |
89c8d91e | 65 | tty_unlock(tty); |
11dbf203 | 66 | tty_vhangup(tty->link); |
89c8d91e | 67 | tty_lock(tty); |
1da177e4 LT |
68 | } |
69 | } | |
70 | ||
71 | /* | |
72 | * The unthrottle routine is called by the line discipline to signal | |
73 | * that it can receive more characters. For PTY's, the TTY_THROTTLED | |
74 | * flag is always set, to force the line discipline to always call the | |
fe9cd962 | 75 | * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE |
1da177e4 LT |
76 | * characters in the queue. This is necessary since each time this |
77 | * happens, we need to wake up any sleeping processes that could be | |
78 | * (1) trying to send data to the pty, or (2) waiting in wait_until_sent() | |
79 | * for the pty buffer to be drained. | |
80 | */ | |
fe9cd962 | 81 | static void pty_unthrottle(struct tty_struct *tty) |
1da177e4 | 82 | { |
d945cb9c | 83 | tty_wakeup(tty->link); |
1da177e4 LT |
84 | set_bit(TTY_THROTTLED, &tty->flags); |
85 | } | |
86 | ||
d945cb9c AC |
87 | /** |
88 | * pty_space - report space left for writing | |
89 | * @to: tty we are writing into | |
1da177e4 | 90 | * |
d945cb9c AC |
91 | * The tty buffers allow 64K but we sneak a peak and clip at 8K this |
92 | * allows a lot of overspill room for echo and other fun messes to | |
93 | * be handled properly | |
94 | */ | |
95 | ||
96 | static int pty_space(struct tty_struct *to) | |
97 | { | |
ecbbfd44 | 98 | int n = 8192 - to->port->buf.memory_used; |
d945cb9c AC |
99 | if (n < 0) |
100 | return 0; | |
101 | return n; | |
102 | } | |
103 | ||
104 | /** | |
105 | * pty_write - write to a pty | |
106 | * @tty: the tty we write from | |
107 | * @buf: kernel buffer of data | |
108 | * @count: bytes to write | |
762faaed | 109 | * |
d945cb9c AC |
110 | * Our "hardware" write method. Data is coming from the ldisc which |
111 | * may be in a non sleeping state. We simply throw this at the other | |
112 | * end of the link as if we were an IRQ handler receiving stuff for | |
113 | * the other side of the pty/tty pair. | |
1da177e4 | 114 | */ |
d945cb9c | 115 | |
ac89a917 | 116 | static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c) |
1da177e4 LT |
117 | { |
118 | struct tty_struct *to = tty->link; | |
1da177e4 | 119 | |
d945cb9c | 120 | if (tty->stopped) |
1da177e4 | 121 | return 0; |
d945cb9c | 122 | |
d945cb9c AC |
123 | if (c > 0) { |
124 | /* Stuff the data into the input queue of the other end */ | |
05c7cd39 | 125 | c = tty_insert_flip_string(to->port, buf, c); |
d945cb9c | 126 | /* And shovel */ |
202c4675 | 127 | if (c) { |
2e124b4a | 128 | tty_flip_buffer_push(to->port); |
202c4675 LT |
129 | tty_wakeup(tty); |
130 | } | |
762faaed | 131 | } |
1da177e4 LT |
132 | return c; |
133 | } | |
134 | ||
d945cb9c AC |
135 | /** |
136 | * pty_write_room - write space | |
137 | * @tty: tty we are writing from | |
138 | * | |
139 | * Report how many bytes the ldisc can send into the queue for | |
140 | * the other device. | |
141 | */ | |
142 | ||
1da177e4 LT |
143 | static int pty_write_room(struct tty_struct *tty) |
144 | { | |
85dfd81d LT |
145 | if (tty->stopped) |
146 | return 0; | |
d945cb9c | 147 | return pty_space(tty->link); |
1da177e4 LT |
148 | } |
149 | ||
d945cb9c AC |
150 | /** |
151 | * pty_chars_in_buffer - characters currently in our tx queue | |
152 | * @tty: our tty | |
fe9cd962 | 153 | * |
d945cb9c AC |
154 | * Report how much we have in the transmit queue. As everything is |
155 | * instantly at the other end this is easy to implement. | |
1da177e4 | 156 | */ |
d945cb9c | 157 | |
1da177e4 LT |
158 | static int pty_chars_in_buffer(struct tty_struct *tty) |
159 | { | |
d945cb9c | 160 | return 0; |
1da177e4 LT |
161 | } |
162 | ||
163 | /* Set the lock flag on a pty */ | |
fe9cd962 | 164 | static int pty_set_lock(struct tty_struct *tty, int __user *arg) |
1da177e4 LT |
165 | { |
166 | int val; | |
fe9cd962 | 167 | if (get_user(val, arg)) |
1da177e4 LT |
168 | return -EFAULT; |
169 | if (val) | |
170 | set_bit(TTY_PTY_LOCK, &tty->flags); | |
171 | else | |
172 | clear_bit(TTY_PTY_LOCK, &tty->flags); | |
173 | return 0; | |
174 | } | |
175 | ||
84fd7bdf CG |
176 | static int pty_get_lock(struct tty_struct *tty, int __user *arg) |
177 | { | |
178 | int locked = test_bit(TTY_PTY_LOCK, &tty->flags); | |
179 | return put_user(locked, arg); | |
180 | } | |
181 | ||
06026d91 CG |
182 | /* Set the packet mode on a pty */ |
183 | static int pty_set_pktmode(struct tty_struct *tty, int __user *arg) | |
184 | { | |
185 | unsigned long flags; | |
186 | int pktmode; | |
187 | ||
188 | if (get_user(pktmode, arg)) | |
189 | return -EFAULT; | |
190 | ||
191 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
192 | if (pktmode) { | |
193 | if (!tty->packet) { | |
194 | tty->packet = 1; | |
195 | tty->link->ctrl_status = 0; | |
196 | } | |
197 | } else | |
198 | tty->packet = 0; | |
199 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
84fd7bdf CG |
204 | /* Get the packet mode of a pty */ |
205 | static int pty_get_pktmode(struct tty_struct *tty, int __user *arg) | |
206 | { | |
207 | int pktmode = tty->packet; | |
208 | return put_user(pktmode, arg); | |
209 | } | |
210 | ||
26df6d13 | 211 | /* Send a signal to the slave */ |
212 | static int pty_signal(struct tty_struct *tty, int sig) | |
213 | { | |
214 | unsigned long flags; | |
215 | struct pid *pgrp; | |
216 | ||
4324af6a PH |
217 | if (sig != SIGINT && sig != SIGQUIT && sig != SIGTSTP) |
218 | return -EINVAL; | |
219 | ||
26df6d13 | 220 | if (tty->link) { |
221 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); | |
222 | pgrp = get_pid(tty->link->pgrp); | |
223 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); | |
224 | ||
225 | kill_pgrp(pgrp, sig, 1); | |
226 | put_pid(pgrp); | |
227 | } | |
228 | return 0; | |
229 | } | |
230 | ||
1da177e4 LT |
231 | static void pty_flush_buffer(struct tty_struct *tty) |
232 | { | |
233 | struct tty_struct *to = tty->link; | |
04f378b1 | 234 | unsigned long flags; |
fe9cd962 | 235 | |
1da177e4 LT |
236 | if (!to) |
237 | return; | |
d945cb9c | 238 | /* tty_buffer_flush(to); FIXME */ |
1da177e4 | 239 | if (to->packet) { |
04f378b1 | 240 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
1da177e4 LT |
241 | tty->ctrl_status |= TIOCPKT_FLUSHWRITE; |
242 | wake_up_interruptible(&to->read_wait); | |
04f378b1 | 243 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
1da177e4 LT |
244 | } |
245 | } | |
246 | ||
fe9cd962 | 247 | static int pty_open(struct tty_struct *tty, struct file *filp) |
1da177e4 | 248 | { |
1da177e4 | 249 | if (!tty || !tty->link) |
7c61c3d8 | 250 | return -ENODEV; |
69939035 | 251 | |
1da177e4 LT |
252 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) |
253 | goto out; | |
254 | if (test_bit(TTY_PTY_LOCK, &tty->link->flags)) | |
255 | goto out; | |
80cace72 | 256 | if (tty->driver->subtype == PTY_TYPE_SLAVE && tty->link->count != 1) |
1da177e4 LT |
257 | goto out; |
258 | ||
69939035 | 259 | clear_bit(TTY_IO_ERROR, &tty->flags); |
1da177e4 LT |
260 | clear_bit(TTY_OTHER_CLOSED, &tty->link->flags); |
261 | set_bit(TTY_THROTTLED, &tty->flags); | |
7c61c3d8 PH |
262 | return 0; |
263 | ||
1da177e4 | 264 | out: |
7c61c3d8 PH |
265 | set_bit(TTY_IO_ERROR, &tty->flags); |
266 | return -EIO; | |
1da177e4 LT |
267 | } |
268 | ||
fe9cd962 AC |
269 | static void pty_set_termios(struct tty_struct *tty, |
270 | struct ktermios *old_termios) | |
1da177e4 | 271 | { |
adc8d746 AC |
272 | tty->termios.c_cflag &= ~(CSIZE | PARENB); |
273 | tty->termios.c_cflag |= (CS8 | CREAD); | |
1da177e4 LT |
274 | } |
275 | ||
fc6f6238 AC |
276 | /** |
277 | * pty_do_resize - resize event | |
278 | * @tty: tty being resized | |
c774bda2 | 279 | * @ws: window size being set. |
fc6f6238 | 280 | * |
3ad2f3fb | 281 | * Update the termios variables and send the necessary signals to |
fc6f6238 AC |
282 | * peform a terminal resize correctly |
283 | */ | |
284 | ||
159a8e92 | 285 | static int pty_resize(struct tty_struct *tty, struct winsize *ws) |
fc6f6238 AC |
286 | { |
287 | struct pid *pgrp, *rpgrp; | |
288 | unsigned long flags; | |
289 | struct tty_struct *pty = tty->link; | |
290 | ||
291 | /* For a PTY we need to lock the tty side */ | |
292 | mutex_lock(&tty->termios_mutex); | |
293 | if (!memcmp(ws, &tty->winsize, sizeof(*ws))) | |
294 | goto done; | |
295 | ||
296 | /* Get the PID values and reference them so we can | |
297 | avoid holding the tty ctrl lock while sending signals. | |
298 | We need to lock these individually however. */ | |
299 | ||
300 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
301 | pgrp = get_pid(tty->pgrp); | |
302 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | |
303 | ||
304 | spin_lock_irqsave(&pty->ctrl_lock, flags); | |
305 | rpgrp = get_pid(pty->pgrp); | |
306 | spin_unlock_irqrestore(&pty->ctrl_lock, flags); | |
307 | ||
308 | if (pgrp) | |
309 | kill_pgrp(pgrp, SIGWINCH, 1); | |
310 | if (rpgrp != pgrp && rpgrp) | |
311 | kill_pgrp(rpgrp, SIGWINCH, 1); | |
312 | ||
313 | put_pid(pgrp); | |
314 | put_pid(rpgrp); | |
315 | ||
316 | tty->winsize = *ws; | |
317 | pty->winsize = *ws; /* Never used so will go away soon */ | |
318 | done: | |
319 | mutex_unlock(&tty->termios_mutex); | |
320 | return 0; | |
321 | } | |
322 | ||
d155255a AC |
323 | /** |
324 | * pty_common_install - set up the pty pair | |
325 | * @driver: the pty driver | |
326 | * @tty: the tty being instantiated | |
327 | * @bool: legacy, true if this is BSD style | |
328 | * | |
329 | * Perform the initial set up for the tty/pty pair. Called from the | |
330 | * tty layer when the port is first opened. | |
331 | * | |
332 | * Locking: the caller must hold the tty_mutex | |
333 | */ | |
5d249bc6 JS |
334 | static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty, |
335 | bool legacy) | |
bf970ee4 AC |
336 | { |
337 | struct tty_struct *o_tty; | |
d03702a2 | 338 | struct tty_port *ports[2]; |
bf970ee4 | 339 | int idx = tty->index; |
5d249bc6 | 340 | int retval = -ENOMEM; |
bf970ee4 AC |
341 | |
342 | o_tty = alloc_tty_struct(); | |
6f9ea7ad JS |
343 | if (!o_tty) |
344 | goto err; | |
d03702a2 JS |
345 | ports[0] = kmalloc(sizeof **ports, GFP_KERNEL); |
346 | ports[1] = kmalloc(sizeof **ports, GFP_KERNEL); | |
6f9ea7ad | 347 | if (!ports[0] || !ports[1]) |
d03702a2 | 348 | goto err_free_tty; |
bf970ee4 AC |
349 | if (!try_module_get(driver->other->owner)) { |
350 | /* This cannot in fact currently happen */ | |
8a1b8d70 | 351 | goto err_free_tty; |
bf970ee4 AC |
352 | } |
353 | initialize_tty_struct(o_tty, driver->other, idx); | |
354 | ||
5d249bc6 JS |
355 | if (legacy) { |
356 | /* We always use new tty termios data so we can do this | |
357 | the easy way .. */ | |
358 | retval = tty_init_termios(tty); | |
359 | if (retval) | |
360 | goto err_deinit_tty; | |
361 | ||
362 | retval = tty_init_termios(o_tty); | |
363 | if (retval) | |
364 | goto err_free_termios; | |
365 | ||
366 | driver->other->ttys[idx] = o_tty; | |
367 | driver->ttys[idx] = tty; | |
368 | } else { | |
adc8d746 AC |
369 | memset(&tty->termios_locked, 0, sizeof(tty->termios_locked)); |
370 | tty->termios = driver->init_termios; | |
371 | memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked)); | |
372 | o_tty->termios = driver->other->init_termios; | |
5d249bc6 | 373 | } |
fe9cd962 | 374 | |
bf970ee4 AC |
375 | /* |
376 | * Everything allocated ... set up the o_tty structure. | |
377 | */ | |
bf970ee4 AC |
378 | tty_driver_kref_get(driver->other); |
379 | if (driver->subtype == PTY_TYPE_MASTER) | |
380 | o_tty->count++; | |
381 | /* Establish the links in both directions */ | |
382 | tty->link = o_tty; | |
383 | o_tty->link = tty; | |
d03702a2 JS |
384 | tty_port_init(ports[0]); |
385 | tty_port_init(ports[1]); | |
386 | o_tty->port = ports[0]; | |
387 | tty->port = ports[1]; | |
967fab69 | 388 | o_tty->port->itty = o_tty; |
bf970ee4 AC |
389 | |
390 | tty_driver_kref_get(driver); | |
391 | tty->count++; | |
bf970ee4 | 392 | return 0; |
8a1b8d70 | 393 | err_free_termios: |
5d249bc6 JS |
394 | if (legacy) |
395 | tty_free_termios(tty); | |
a9dccddb JS |
396 | err_deinit_tty: |
397 | deinitialize_tty_struct(o_tty); | |
bf970ee4 | 398 | module_put(o_tty->driver->owner); |
8a1b8d70 | 399 | err_free_tty: |
d03702a2 JS |
400 | kfree(ports[0]); |
401 | kfree(ports[1]); | |
bf970ee4 | 402 | free_tty_struct(o_tty); |
6f9ea7ad | 403 | err: |
8a1b8d70 | 404 | return retval; |
bf970ee4 AC |
405 | } |
406 | ||
d03702a2 JS |
407 | static void pty_cleanup(struct tty_struct *tty) |
408 | { | |
81c79838 | 409 | tty_port_put(tty->port); |
d03702a2 JS |
410 | } |
411 | ||
5d249bc6 JS |
412 | /* Traditional BSD devices */ |
413 | #ifdef CONFIG_LEGACY_PTYS | |
414 | ||
415 | static int pty_install(struct tty_driver *driver, struct tty_struct *tty) | |
416 | { | |
417 | return pty_common_install(driver, tty, true); | |
418 | } | |
419 | ||
d155255a AC |
420 | static void pty_remove(struct tty_driver *driver, struct tty_struct *tty) |
421 | { | |
422 | struct tty_struct *pair = tty->link; | |
423 | driver->ttys[tty->index] = NULL; | |
424 | if (pair) | |
425 | pair->driver->ttys[pair->index] = NULL; | |
426 | } | |
427 | ||
6caa76b7 | 428 | static int pty_bsd_ioctl(struct tty_struct *tty, |
1da177e4 LT |
429 | unsigned int cmd, unsigned long arg) |
430 | { | |
431 | switch (cmd) { | |
432 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ | |
433 | return pty_set_lock(tty, (int __user *) arg); | |
84fd7bdf CG |
434 | case TIOCGPTLCK: /* Get PT Lock status */ |
435 | return pty_get_lock(tty, (int __user *)arg); | |
06026d91 CG |
436 | case TIOCPKT: /* Set PT packet mode */ |
437 | return pty_set_pktmode(tty, (int __user *)arg); | |
84fd7bdf CG |
438 | case TIOCGPKT: /* Get PT packet mode */ |
439 | return pty_get_pktmode(tty, (int __user *)arg); | |
26df6d13 | 440 | case TIOCSIG: /* Send signal to other side of pty */ |
441 | return pty_signal(tty, (int) arg); | |
ded2f295 JS |
442 | case TIOCGPTN: /* TTY returns ENOTTY, but glibc expects EINVAL here */ |
443 | return -EINVAL; | |
1da177e4 LT |
444 | } |
445 | return -ENOIOCTLCMD; | |
446 | } | |
447 | ||
dc8c8587 KS |
448 | static int legacy_count = CONFIG_LEGACY_PTY_COUNT; |
449 | module_param(legacy_count, int, 0); | |
450 | ||
342a5971 LT |
451 | /* |
452 | * The master side of a pty can do TIOCSPTLCK and thus | |
453 | * has pty_bsd_ioctl. | |
454 | */ | |
455 | static const struct tty_operations master_pty_ops_bsd = { | |
456 | .install = pty_install, | |
3e8e88ca AC |
457 | .open = pty_open, |
458 | .close = pty_close, | |
459 | .write = pty_write, | |
460 | .write_room = pty_write_room, | |
461 | .flush_buffer = pty_flush_buffer, | |
462 | .chars_in_buffer = pty_chars_in_buffer, | |
463 | .unthrottle = pty_unthrottle, | |
464 | .set_termios = pty_set_termios, | |
465 | .ioctl = pty_bsd_ioctl, | |
d03702a2 | 466 | .cleanup = pty_cleanup, |
d155255a AC |
467 | .resize = pty_resize, |
468 | .remove = pty_remove | |
3e8e88ca AC |
469 | }; |
470 | ||
342a5971 LT |
471 | static const struct tty_operations slave_pty_ops_bsd = { |
472 | .install = pty_install, | |
473 | .open = pty_open, | |
474 | .close = pty_close, | |
475 | .write = pty_write, | |
476 | .write_room = pty_write_room, | |
477 | .flush_buffer = pty_flush_buffer, | |
478 | .chars_in_buffer = pty_chars_in_buffer, | |
479 | .unthrottle = pty_unthrottle, | |
480 | .set_termios = pty_set_termios, | |
d03702a2 | 481 | .cleanup = pty_cleanup, |
d155255a AC |
482 | .resize = pty_resize, |
483 | .remove = pty_remove | |
342a5971 LT |
484 | }; |
485 | ||
1da177e4 LT |
486 | static void __init legacy_pty_init(void) |
487 | { | |
342a5971 LT |
488 | struct tty_driver *pty_driver, *pty_slave_driver; |
489 | ||
dc8c8587 KS |
490 | if (legacy_count <= 0) |
491 | return; | |
1da177e4 | 492 | |
21aca2fa JS |
493 | pty_driver = tty_alloc_driver(legacy_count, |
494 | TTY_DRIVER_RESET_TERMIOS | | |
495 | TTY_DRIVER_REAL_RAW | | |
496 | TTY_DRIVER_DYNAMIC_ALLOC); | |
c3a6344a | 497 | if (IS_ERR(pty_driver)) |
1da177e4 LT |
498 | panic("Couldn't allocate pty driver"); |
499 | ||
21aca2fa JS |
500 | pty_slave_driver = tty_alloc_driver(legacy_count, |
501 | TTY_DRIVER_RESET_TERMIOS | | |
502 | TTY_DRIVER_REAL_RAW | | |
503 | TTY_DRIVER_DYNAMIC_ALLOC); | |
c3a6344a | 504 | if (IS_ERR(pty_slave_driver)) |
1da177e4 LT |
505 | panic("Couldn't allocate pty slave driver"); |
506 | ||
1da177e4 LT |
507 | pty_driver->driver_name = "pty_master"; |
508 | pty_driver->name = "pty"; | |
1da177e4 LT |
509 | pty_driver->major = PTY_MASTER_MAJOR; |
510 | pty_driver->minor_start = 0; | |
511 | pty_driver->type = TTY_DRIVER_TYPE_PTY; | |
512 | pty_driver->subtype = PTY_TYPE_MASTER; | |
513 | pty_driver->init_termios = tty_std_termios; | |
514 | pty_driver->init_termios.c_iflag = 0; | |
515 | pty_driver->init_termios.c_oflag = 0; | |
516 | pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | |
517 | pty_driver->init_termios.c_lflag = 0; | |
606d099c AC |
518 | pty_driver->init_termios.c_ispeed = 38400; |
519 | pty_driver->init_termios.c_ospeed = 38400; | |
1da177e4 | 520 | pty_driver->other = pty_slave_driver; |
342a5971 | 521 | tty_set_operations(pty_driver, &master_pty_ops_bsd); |
1da177e4 | 522 | |
1da177e4 LT |
523 | pty_slave_driver->driver_name = "pty_slave"; |
524 | pty_slave_driver->name = "ttyp"; | |
1da177e4 LT |
525 | pty_slave_driver->major = PTY_SLAVE_MAJOR; |
526 | pty_slave_driver->minor_start = 0; | |
527 | pty_slave_driver->type = TTY_DRIVER_TYPE_PTY; | |
528 | pty_slave_driver->subtype = PTY_TYPE_SLAVE; | |
529 | pty_slave_driver->init_termios = tty_std_termios; | |
530 | pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | |
606d099c AC |
531 | pty_slave_driver->init_termios.c_ispeed = 38400; |
532 | pty_slave_driver->init_termios.c_ospeed = 38400; | |
1da177e4 | 533 | pty_slave_driver->other = pty_driver; |
342a5971 | 534 | tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd); |
1da177e4 LT |
535 | |
536 | if (tty_register_driver(pty_driver)) | |
537 | panic("Couldn't register pty driver"); | |
538 | if (tty_register_driver(pty_slave_driver)) | |
539 | panic("Couldn't register pty slave driver"); | |
540 | } | |
541 | #else | |
542 | static inline void legacy_pty_init(void) { } | |
543 | #endif | |
544 | ||
545 | /* Unix98 devices */ | |
546 | #ifdef CONFIG_UNIX98_PTYS | |
1da177e4 | 547 | |
d81ed103 AC |
548 | static struct cdev ptmx_cdev; |
549 | ||
6caa76b7 | 550 | static int pty_unix98_ioctl(struct tty_struct *tty, |
1da177e4 LT |
551 | unsigned int cmd, unsigned long arg) |
552 | { | |
553 | switch (cmd) { | |
554 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ | |
555 | return pty_set_lock(tty, (int __user *)arg); | |
84fd7bdf CG |
556 | case TIOCGPTLCK: /* Get PT Lock status */ |
557 | return pty_get_lock(tty, (int __user *)arg); | |
06026d91 CG |
558 | case TIOCPKT: /* Set PT packet mode */ |
559 | return pty_set_pktmode(tty, (int __user *)arg); | |
84fd7bdf CG |
560 | case TIOCGPKT: /* Get PT packet mode */ |
561 | return pty_get_pktmode(tty, (int __user *)arg); | |
1da177e4 LT |
562 | case TIOCGPTN: /* Get PT Number */ |
563 | return put_user(tty->index, (unsigned int __user *)arg); | |
26df6d13 | 564 | case TIOCSIG: /* Send signal to other side of pty */ |
565 | return pty_signal(tty, (int) arg); | |
1da177e4 LT |
566 | } |
567 | ||
568 | return -ENOIOCTLCMD; | |
569 | } | |
570 | ||
99f1fe18 AC |
571 | /** |
572 | * ptm_unix98_lookup - find a pty master | |
573 | * @driver: ptm driver | |
574 | * @idx: tty index | |
575 | * | |
576 | * Look up a pty master device. Called under the tty_mutex for now. | |
577 | * This provides our locking. | |
578 | */ | |
579 | ||
15f1a633 SB |
580 | static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver, |
581 | struct inode *ptm_inode, int idx) | |
99f1fe18 | 582 | { |
593a27c4 KK |
583 | /* Master must be open via /dev/ptmx */ |
584 | return ERR_PTR(-EIO); | |
99f1fe18 AC |
585 | } |
586 | ||
587 | /** | |
588 | * pts_unix98_lookup - find a pty slave | |
589 | * @driver: pts driver | |
590 | * @idx: tty index | |
591 | * | |
592 | * Look up a pty master device. Called under the tty_mutex for now. | |
d739e65b | 593 | * This provides our locking for the tty pointer. |
99f1fe18 AC |
594 | */ |
595 | ||
15f1a633 SB |
596 | static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, |
597 | struct inode *pts_inode, int idx) | |
99f1fe18 | 598 | { |
d739e65b AC |
599 | struct tty_struct *tty; |
600 | ||
601 | mutex_lock(&devpts_mutex); | |
8fcbaa2b | 602 | tty = devpts_get_priv(pts_inode); |
d739e65b | 603 | mutex_unlock(&devpts_mutex); |
99f1fe18 AC |
604 | /* Master must be open before slave */ |
605 | if (!tty) | |
606 | return ERR_PTR(-EIO); | |
607 | return tty; | |
608 | } | |
609 | ||
8b0a88d5 AC |
610 | /* We have no need to install and remove our tty objects as devpts does all |
611 | the work for us */ | |
612 | ||
bf970ee4 | 613 | static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) |
8b0a88d5 | 614 | { |
5d249bc6 | 615 | return pty_common_install(driver, tty, false); |
8b0a88d5 AC |
616 | } |
617 | ||
7171604a | 618 | static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty) |
8b0a88d5 AC |
619 | { |
620 | } | |
621 | ||
f4b208eb JS |
622 | /* this is called once with whichever end is closed last */ |
623 | static void pty_unix98_shutdown(struct tty_struct *tty) | |
624 | { | |
625 | devpts_kill_index(tty->driver_data, tty->index); | |
626 | } | |
627 | ||
feebed65 | 628 | static const struct tty_operations ptm_unix98_ops = { |
99f1fe18 | 629 | .lookup = ptm_unix98_lookup, |
bf970ee4 | 630 | .install = pty_unix98_install, |
7171604a | 631 | .remove = pty_unix98_remove, |
3e8e88ca AC |
632 | .open = pty_open, |
633 | .close = pty_close, | |
634 | .write = pty_write, | |
635 | .write_room = pty_write_room, | |
636 | .flush_buffer = pty_flush_buffer, | |
637 | .chars_in_buffer = pty_chars_in_buffer, | |
638 | .unthrottle = pty_unthrottle, | |
639 | .set_termios = pty_set_termios, | |
feebed65 | 640 | .ioctl = pty_unix98_ioctl, |
36b3c070 | 641 | .resize = pty_resize, |
fa2ecfc5 | 642 | .shutdown = pty_unix98_shutdown, |
36b3c070 | 643 | .cleanup = pty_cleanup |
3e8e88ca AC |
644 | }; |
645 | ||
99f1fe18 AC |
646 | static const struct tty_operations pty_unix98_ops = { |
647 | .lookup = pts_unix98_lookup, | |
bf970ee4 | 648 | .install = pty_unix98_install, |
7171604a | 649 | .remove = pty_unix98_remove, |
99f1fe18 AC |
650 | .open = pty_open, |
651 | .close = pty_close, | |
652 | .write = pty_write, | |
653 | .write_room = pty_write_room, | |
654 | .flush_buffer = pty_flush_buffer, | |
655 | .chars_in_buffer = pty_chars_in_buffer, | |
656 | .unthrottle = pty_unthrottle, | |
657 | .set_termios = pty_set_termios, | |
fa2ecfc5 | 658 | .shutdown = pty_unix98_shutdown, |
d03702a2 | 659 | .cleanup = pty_cleanup, |
99f1fe18 | 660 | }; |
d81ed103 AC |
661 | |
662 | /** | |
663 | * ptmx_open - open a unix 98 pty master | |
664 | * @inode: inode of device file | |
665 | * @filp: file pointer to tty | |
666 | * | |
667 | * Allocate a unix98 pty master device from the ptmx driver. | |
668 | * | |
669 | * Locking: tty_mutex protects the init_dev work. tty->count should | |
b9f8033f | 670 | * protect the rest. |
d81ed103 AC |
671 | * allocated_ptys_lock handles the list of free pty numbers |
672 | */ | |
673 | ||
64ba3dc3 | 674 | static int ptmx_open(struct inode *inode, struct file *filp) |
d81ed103 AC |
675 | { |
676 | struct tty_struct *tty; | |
162b97cf | 677 | struct inode *slave_inode; |
d81ed103 AC |
678 | int retval; |
679 | int index; | |
680 | ||
681 | nonseekable_open(inode, filp); | |
682 | ||
b0b88565 LT |
683 | /* We refuse fsnotify events on ptmx, since it's a shared resource */ |
684 | filp->f_mode |= FMODE_NONOTIFY; | |
685 | ||
fa90e1c9 JS |
686 | retval = tty_alloc_file(filp); |
687 | if (retval) | |
688 | return retval; | |
689 | ||
d81ed103 | 690 | /* find a device that is not in use. */ |
89c8d91e | 691 | mutex_lock(&devpts_mutex); |
15f1a633 | 692 | index = devpts_new_index(inode); |
fa90e1c9 JS |
693 | if (index < 0) { |
694 | retval = index; | |
05fb79e4 | 695 | mutex_unlock(&devpts_mutex); |
fa90e1c9 JS |
696 | goto err_file; |
697 | } | |
d81ed103 | 698 | |
89c8d91e AC |
699 | mutex_unlock(&devpts_mutex); |
700 | ||
d81ed103 | 701 | mutex_lock(&tty_mutex); |
593a27c4 | 702 | tty = tty_init_dev(ptm_driver, index); |
d81ed103 | 703 | |
73ec06fc AC |
704 | if (IS_ERR(tty)) { |
705 | retval = PTR_ERR(tty); | |
d81ed103 | 706 | goto out; |
73ec06fc | 707 | } |
d81ed103 | 708 | |
89c8d91e AC |
709 | /* The tty returned here is locked so we can safely |
710 | drop the mutex */ | |
711 | mutex_unlock(&tty_mutex); | |
712 | ||
d81ed103 | 713 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ |
7acf6cd8 | 714 | tty->driver_data = inode; |
ee2ffa0d | 715 | |
fa90e1c9 | 716 | tty_add_file(tty, filp); |
d81ed103 | 717 | |
f11afb61 JS |
718 | slave_inode = devpts_pty_new(inode, |
719 | MKDEV(UNIX98_PTY_SLAVE_MAJOR, index), index, | |
720 | tty->link); | |
162b97cf JS |
721 | if (IS_ERR(slave_inode)) { |
722 | retval = PTR_ERR(slave_inode); | |
1177c0ef | 723 | goto err_release; |
162b97cf | 724 | } |
7acf6cd8 | 725 | tty->link->driver_data = slave_inode; |
d81ed103 AC |
726 | |
727 | retval = ptm_driver->ops->open(tty, filp); | |
64ba3dc3 | 728 | if (retval) |
1177c0ef JS |
729 | goto err_release; |
730 | ||
89c8d91e | 731 | tty_unlock(tty); |
1177c0ef JS |
732 | return 0; |
733 | err_release: | |
89c8d91e | 734 | tty_unlock(tty); |
eeb89d91 | 735 | tty_release(inode, filp); |
d81ed103 AC |
736 | return retval; |
737 | out: | |
89c8d91e | 738 | mutex_unlock(&tty_mutex); |
d3bda529 | 739 | devpts_kill_index(inode, index); |
fa90e1c9 JS |
740 | err_file: |
741 | tty_free_file(filp); | |
64ba3dc3 | 742 | return retval; |
d81ed103 AC |
743 | } |
744 | ||
745 | static struct file_operations ptmx_fops; | |
746 | ||
1da177e4 LT |
747 | static void __init unix98_pty_init(void) |
748 | { | |
21aca2fa JS |
749 | ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX, |
750 | TTY_DRIVER_RESET_TERMIOS | | |
751 | TTY_DRIVER_REAL_RAW | | |
752 | TTY_DRIVER_DYNAMIC_DEV | | |
753 | TTY_DRIVER_DEVPTS_MEM | | |
754 | TTY_DRIVER_DYNAMIC_ALLOC); | |
c3a6344a | 755 | if (IS_ERR(ptm_driver)) |
1da177e4 | 756 | panic("Couldn't allocate Unix98 ptm driver"); |
21aca2fa JS |
757 | pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX, |
758 | TTY_DRIVER_RESET_TERMIOS | | |
759 | TTY_DRIVER_REAL_RAW | | |
760 | TTY_DRIVER_DYNAMIC_DEV | | |
761 | TTY_DRIVER_DEVPTS_MEM | | |
762 | TTY_DRIVER_DYNAMIC_ALLOC); | |
c3a6344a | 763 | if (IS_ERR(pts_driver)) |
1da177e4 LT |
764 | panic("Couldn't allocate Unix98 pts driver"); |
765 | ||
1da177e4 LT |
766 | ptm_driver->driver_name = "pty_master"; |
767 | ptm_driver->name = "ptm"; | |
768 | ptm_driver->major = UNIX98_PTY_MASTER_MAJOR; | |
769 | ptm_driver->minor_start = 0; | |
770 | ptm_driver->type = TTY_DRIVER_TYPE_PTY; | |
771 | ptm_driver->subtype = PTY_TYPE_MASTER; | |
772 | ptm_driver->init_termios = tty_std_termios; | |
773 | ptm_driver->init_termios.c_iflag = 0; | |
774 | ptm_driver->init_termios.c_oflag = 0; | |
775 | ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | |
776 | ptm_driver->init_termios.c_lflag = 0; | |
606d099c AC |
777 | ptm_driver->init_termios.c_ispeed = 38400; |
778 | ptm_driver->init_termios.c_ospeed = 38400; | |
1da177e4 | 779 | ptm_driver->other = pts_driver; |
feebed65 | 780 | tty_set_operations(ptm_driver, &ptm_unix98_ops); |
1da177e4 | 781 | |
1da177e4 LT |
782 | pts_driver->driver_name = "pty_slave"; |
783 | pts_driver->name = "pts"; | |
784 | pts_driver->major = UNIX98_PTY_SLAVE_MAJOR; | |
785 | pts_driver->minor_start = 0; | |
786 | pts_driver->type = TTY_DRIVER_TYPE_PTY; | |
787 | pts_driver->subtype = PTY_TYPE_SLAVE; | |
788 | pts_driver->init_termios = tty_std_termios; | |
789 | pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | |
606d099c AC |
790 | pts_driver->init_termios.c_ispeed = 38400; |
791 | pts_driver->init_termios.c_ospeed = 38400; | |
1da177e4 | 792 | pts_driver->other = ptm_driver; |
99f1fe18 | 793 | tty_set_operations(pts_driver, &pty_unix98_ops); |
fe9cd962 | 794 | |
1da177e4 LT |
795 | if (tty_register_driver(ptm_driver)) |
796 | panic("Couldn't register Unix98 ptm driver"); | |
797 | if (tty_register_driver(pts_driver)) | |
798 | panic("Couldn't register Unix98 pts driver"); | |
799 | ||
d81ed103 AC |
800 | /* Now create the /dev/ptmx special device */ |
801 | tty_default_fops(&ptmx_fops); | |
802 | ptmx_fops.open = ptmx_open; | |
803 | ||
804 | cdev_init(&ptmx_cdev, &ptmx_fops); | |
805 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || | |
806 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) | |
82f8c35f | 807 | panic("Couldn't register /dev/ptmx driver"); |
d81ed103 | 808 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); |
1da177e4 | 809 | } |
d81ed103 | 810 | |
1da177e4 LT |
811 | #else |
812 | static inline void unix98_pty_init(void) { } | |
813 | #endif | |
814 | ||
815 | static int __init pty_init(void) | |
816 | { | |
817 | legacy_pty_init(); | |
818 | unix98_pty_init(); | |
819 | return 0; | |
820 | } | |
821 | module_init(pty_init); |