]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/char/pty.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | * | |
6 | * Added support for a Unix98-style ptmx device. | |
7 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 | |
1da177e4 | 8 | * |
fe9cd962 AC |
9 | * When reading this code see also fs/devpts. In particular note that the |
10 | * driver_data field is used by the devpts side as a binding to the devpts | |
11 | * inode. | |
1da177e4 LT |
12 | */ |
13 | ||
fe9cd962 | 14 | #include <linux/module.h> |
1da177e4 LT |
15 | |
16 | #include <linux/errno.h> | |
1da177e4 LT |
17 | #include <linux/interrupt.h> |
18 | #include <linux/tty.h> | |
19 | #include <linux/tty_flip.h> | |
20 | #include <linux/fcntl.h> | |
21 | #include <linux/string.h> | |
22 | #include <linux/major.h> | |
23 | #include <linux/mm.h> | |
24 | #include <linux/init.h> | |
1da177e4 | 25 | #include <linux/sysctl.h> |
d81ed103 | 26 | #include <linux/device.h> |
fe9cd962 | 27 | #include <linux/uaccess.h> |
1da177e4 LT |
28 | #include <linux/bitops.h> |
29 | #include <linux/devpts_fs.h> | |
30 | ||
fe9cd962 AC |
31 | #include <asm/system.h> |
32 | ||
1da177e4 | 33 | #ifdef CONFIG_UNIX98_PTYS |
da2bdf9a | 34 | static struct tty_driver *ptm_driver; |
1da177e4 LT |
35 | static struct tty_driver *pts_driver; |
36 | #endif | |
37 | ||
fe9cd962 | 38 | static void pty_close(struct tty_struct *tty, struct file *filp) |
1da177e4 | 39 | { |
fe9cd962 AC |
40 | BUG_ON(!tty); |
41 | if (tty->driver->subtype == PTY_TYPE_MASTER) | |
42 | WARN_ON(tty->count > 1); | |
43 | else { | |
1da177e4 LT |
44 | if (tty->count > 2) |
45 | return; | |
46 | } | |
47 | wake_up_interruptible(&tty->read_wait); | |
48 | wake_up_interruptible(&tty->write_wait); | |
49 | tty->packet = 0; | |
50 | if (!tty->link) | |
51 | return; | |
52 | tty->link->packet = 0; | |
53 | set_bit(TTY_OTHER_CLOSED, &tty->link->flags); | |
54 | wake_up_interruptible(&tty->link->read_wait); | |
55 | wake_up_interruptible(&tty->link->write_wait); | |
56 | if (tty->driver->subtype == PTY_TYPE_MASTER) { | |
57 | set_bit(TTY_OTHER_CLOSED, &tty->flags); | |
58 | #ifdef CONFIG_UNIX98_PTYS | |
59 | if (tty->driver == ptm_driver) | |
15f1a633 | 60 | devpts_pty_kill(tty->link); |
1da177e4 LT |
61 | #endif |
62 | tty_vhangup(tty->link); | |
63 | } | |
64 | } | |
65 | ||
66 | /* | |
67 | * The unthrottle routine is called by the line discipline to signal | |
68 | * that it can receive more characters. For PTY's, the TTY_THROTTLED | |
69 | * flag is always set, to force the line discipline to always call the | |
fe9cd962 | 70 | * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE |
1da177e4 LT |
71 | * characters in the queue. This is necessary since each time this |
72 | * happens, we need to wake up any sleeping processes that could be | |
73 | * (1) trying to send data to the pty, or (2) waiting in wait_until_sent() | |
74 | * for the pty buffer to be drained. | |
75 | */ | |
fe9cd962 | 76 | static void pty_unthrottle(struct tty_struct *tty) |
1da177e4 LT |
77 | { |
78 | struct tty_struct *o_tty = tty->link; | |
79 | ||
80 | if (!o_tty) | |
81 | return; | |
82 | ||
83 | tty_wakeup(o_tty); | |
84 | set_bit(TTY_THROTTLED, &tty->flags); | |
85 | } | |
86 | ||
87 | /* | |
fe9cd962 | 88 | * WSH 05/24/97: modified to |
1da177e4 LT |
89 | * (1) use space in tty->flip instead of a shared temp buffer |
90 | * The flip buffers aren't being used for a pty, so there's lots | |
91 | * of space available. The buffer is protected by a per-pty | |
92 | * semaphore that should almost never come under contention. | |
93 | * (2) avoid redundant copying for cases where count >> receive_room | |
94 | * N.B. Calls from user space may now return an error code instead of | |
95 | * a count. | |
96 | * | |
97 | * FIXME: Our pty_write method is called with our ldisc lock held but | |
762faaed AC |
98 | * not our partners. We can't just wait on the other one blindly without |
99 | * risking deadlocks. At some point when everything has settled down we need | |
100 | * to look into making pty_write at least able to sleep over an ldisc change. | |
101 | * | |
102 | * The return on no ldisc is a bit counter intuitive but the logic works | |
103 | * like this. During an ldisc change the other end will flush its buffers. We | |
104 | * thus return the full length which is identical to the case where we had | |
105 | * proper locking and happened to queue the bytes just before the flush during | |
106 | * the ldisc change. | |
1da177e4 | 107 | */ |
fe9cd962 AC |
108 | static int pty_write(struct tty_struct *tty, const unsigned char *buf, |
109 | int count) | |
1da177e4 LT |
110 | { |
111 | struct tty_struct *to = tty->link; | |
762faaed AC |
112 | struct tty_ldisc *ld; |
113 | int c = count; | |
1da177e4 LT |
114 | |
115 | if (!to || tty->stopped) | |
116 | return 0; | |
762faaed AC |
117 | ld = tty_ldisc_ref(to); |
118 | ||
119 | if (ld) { | |
120 | c = to->receive_room; | |
121 | if (c > count) | |
122 | c = count; | |
123 | ld->ops->receive_buf(to, buf, NULL, c); | |
124 | tty_ldisc_deref(ld); | |
125 | } | |
1da177e4 LT |
126 | return c; |
127 | } | |
128 | ||
129 | static int pty_write_room(struct tty_struct *tty) | |
130 | { | |
131 | struct tty_struct *to = tty->link; | |
132 | ||
133 | if (!to || tty->stopped) | |
134 | return 0; | |
135 | ||
33f0f88f | 136 | return to->receive_room; |
1da177e4 LT |
137 | } |
138 | ||
139 | /* | |
140 | * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior | |
fe9cd962 | 141 | * The chars_in_buffer() value is used by the ldisc select() function |
1da177e4 LT |
142 | * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256). |
143 | * The pty driver chars_in_buffer() Master/Slave must behave differently: | |
144 | * | |
145 | * The Master side needs to allow typed-ahead commands to accumulate | |
146 | * while being canonicalized, so we report "our buffer" as empty until | |
147 | * some threshold is reached, and then report the count. (Any count > | |
fe9cd962 AC |
148 | * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock |
149 | * the count returned must be 0 if no canonical data is available to be | |
1da177e4 | 150 | * read. (The N_TTY ldisc.chars_in_buffer now knows this.) |
fe9cd962 | 151 | * |
1da177e4 LT |
152 | * The Slave side passes all characters in raw mode to the Master side's |
153 | * buffer where they can be read immediately, so in this case we can | |
154 | * return the true count in the buffer. | |
155 | */ | |
156 | static int pty_chars_in_buffer(struct tty_struct *tty) | |
157 | { | |
158 | struct tty_struct *to = tty->link; | |
762faaed AC |
159 | struct tty_ldisc *ld; |
160 | int count = 0; | |
1da177e4 LT |
161 | |
162 | /* We should get the line discipline lock for "tty->link" */ | |
762faaed AC |
163 | if (!to) |
164 | return 0; | |
165 | /* We cannot take a sleeping reference here without deadlocking with | |
166 | an ldisc change - but it doesn't really matter */ | |
167 | ld = tty_ldisc_ref(to); | |
168 | if (ld == NULL) | |
1da177e4 LT |
169 | return 0; |
170 | ||
171 | /* The ldisc must report 0 if no characters available to be read */ | |
762faaed AC |
172 | if (ld->ops->chars_in_buffer) |
173 | count = ld->ops->chars_in_buffer(to); | |
174 | ||
175 | tty_ldisc_deref(ld); | |
1da177e4 | 176 | |
fe9cd962 AC |
177 | if (tty->driver->subtype == PTY_TYPE_SLAVE) |
178 | return count; | |
1da177e4 | 179 | |
fe9cd962 | 180 | /* Master side driver ... if the other side's read buffer is less than |
1da177e4 | 181 | * half full, return 0 to allow writers to proceed; otherwise return |
fe9cd962 | 182 | * the count. This leaves a comfortable margin to avoid overflow, |
1da177e4 LT |
183 | * and still allows half a buffer's worth of typed-ahead commands. |
184 | */ | |
fe9cd962 | 185 | return (count < N_TTY_BUF_SIZE/2) ? 0 : count; |
1da177e4 LT |
186 | } |
187 | ||
188 | /* Set the lock flag on a pty */ | |
fe9cd962 | 189 | static int pty_set_lock(struct tty_struct *tty, int __user *arg) |
1da177e4 LT |
190 | { |
191 | int val; | |
fe9cd962 | 192 | if (get_user(val, arg)) |
1da177e4 LT |
193 | return -EFAULT; |
194 | if (val) | |
195 | set_bit(TTY_PTY_LOCK, &tty->flags); | |
196 | else | |
197 | clear_bit(TTY_PTY_LOCK, &tty->flags); | |
198 | return 0; | |
199 | } | |
200 | ||
201 | static void pty_flush_buffer(struct tty_struct *tty) | |
202 | { | |
203 | struct tty_struct *to = tty->link; | |
04f378b1 | 204 | unsigned long flags; |
762faaed | 205 | struct tty_ldisc *ld; |
fe9cd962 | 206 | |
1da177e4 LT |
207 | if (!to) |
208 | return; | |
762faaed AC |
209 | ld = tty_ldisc_ref(to); |
210 | ||
211 | /* The other end is changing discipline */ | |
212 | if (!ld) | |
213 | return; | |
fe9cd962 | 214 | |
762faaed | 215 | if (ld->ops->flush_buffer) |
c65c9bc3 | 216 | to->ldisc->ops->flush_buffer(to); |
762faaed | 217 | tty_ldisc_deref(ld); |
fe9cd962 | 218 | |
1da177e4 | 219 | if (to->packet) { |
04f378b1 | 220 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
1da177e4 LT |
221 | tty->ctrl_status |= TIOCPKT_FLUSHWRITE; |
222 | wake_up_interruptible(&to->read_wait); | |
04f378b1 | 223 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
1da177e4 LT |
224 | } |
225 | } | |
226 | ||
fe9cd962 | 227 | static int pty_open(struct tty_struct *tty, struct file *filp) |
1da177e4 LT |
228 | { |
229 | int retval = -ENODEV; | |
230 | ||
231 | if (!tty || !tty->link) | |
232 | goto out; | |
233 | ||
234 | retval = -EIO; | |
235 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) | |
236 | goto out; | |
237 | if (test_bit(TTY_PTY_LOCK, &tty->link->flags)) | |
238 | goto out; | |
239 | if (tty->link->count != 1) | |
240 | goto out; | |
241 | ||
242 | clear_bit(TTY_OTHER_CLOSED, &tty->link->flags); | |
243 | set_bit(TTY_THROTTLED, &tty->flags); | |
1da177e4 LT |
244 | retval = 0; |
245 | out: | |
246 | return retval; | |
247 | } | |
248 | ||
fe9cd962 AC |
249 | static void pty_set_termios(struct tty_struct *tty, |
250 | struct ktermios *old_termios) | |
1da177e4 | 251 | { |
fe9cd962 AC |
252 | tty->termios->c_cflag &= ~(CSIZE | PARENB); |
253 | tty->termios->c_cflag |= (CS8 | CREAD); | |
1da177e4 LT |
254 | } |
255 | ||
fc6f6238 AC |
256 | /** |
257 | * pty_do_resize - resize event | |
258 | * @tty: tty being resized | |
c774bda2 | 259 | * @ws: window size being set. |
fc6f6238 AC |
260 | * |
261 | * Update the termios variables and send the neccessary signals to | |
262 | * peform a terminal resize correctly | |
263 | */ | |
264 | ||
265 | int pty_resize(struct tty_struct *tty, struct winsize *ws) | |
266 | { | |
267 | struct pid *pgrp, *rpgrp; | |
268 | unsigned long flags; | |
269 | struct tty_struct *pty = tty->link; | |
270 | ||
271 | /* For a PTY we need to lock the tty side */ | |
272 | mutex_lock(&tty->termios_mutex); | |
273 | if (!memcmp(ws, &tty->winsize, sizeof(*ws))) | |
274 | goto done; | |
275 | ||
276 | /* Get the PID values and reference them so we can | |
277 | avoid holding the tty ctrl lock while sending signals. | |
278 | We need to lock these individually however. */ | |
279 | ||
280 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
281 | pgrp = get_pid(tty->pgrp); | |
282 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | |
283 | ||
284 | spin_lock_irqsave(&pty->ctrl_lock, flags); | |
285 | rpgrp = get_pid(pty->pgrp); | |
286 | spin_unlock_irqrestore(&pty->ctrl_lock, flags); | |
287 | ||
288 | if (pgrp) | |
289 | kill_pgrp(pgrp, SIGWINCH, 1); | |
290 | if (rpgrp != pgrp && rpgrp) | |
291 | kill_pgrp(rpgrp, SIGWINCH, 1); | |
292 | ||
293 | put_pid(pgrp); | |
294 | put_pid(rpgrp); | |
295 | ||
296 | tty->winsize = *ws; | |
297 | pty->winsize = *ws; /* Never used so will go away soon */ | |
298 | done: | |
299 | mutex_unlock(&tty->termios_mutex); | |
300 | return 0; | |
301 | } | |
302 | ||
bf970ee4 AC |
303 | static int pty_install(struct tty_driver *driver, struct tty_struct *tty) |
304 | { | |
305 | struct tty_struct *o_tty; | |
306 | int idx = tty->index; | |
307 | int retval; | |
308 | ||
309 | o_tty = alloc_tty_struct(); | |
310 | if (!o_tty) | |
311 | return -ENOMEM; | |
312 | if (!try_module_get(driver->other->owner)) { | |
313 | /* This cannot in fact currently happen */ | |
314 | free_tty_struct(o_tty); | |
315 | return -ENOMEM; | |
316 | } | |
317 | initialize_tty_struct(o_tty, driver->other, idx); | |
318 | ||
319 | /* We always use new tty termios data so we can do this | |
320 | the easy way .. */ | |
321 | retval = tty_init_termios(tty); | |
322 | if (retval) | |
323 | goto free_mem_out; | |
324 | ||
325 | retval = tty_init_termios(o_tty); | |
326 | if (retval) { | |
327 | tty_free_termios(tty); | |
328 | goto free_mem_out; | |
329 | } | |
fe9cd962 | 330 | |
bf970ee4 AC |
331 | /* |
332 | * Everything allocated ... set up the o_tty structure. | |
333 | */ | |
334 | driver->other->ttys[idx] = o_tty; | |
335 | tty_driver_kref_get(driver->other); | |
336 | if (driver->subtype == PTY_TYPE_MASTER) | |
337 | o_tty->count++; | |
338 | /* Establish the links in both directions */ | |
339 | tty->link = o_tty; | |
340 | o_tty->link = tty; | |
341 | ||
342 | tty_driver_kref_get(driver); | |
343 | tty->count++; | |
344 | driver->ttys[idx] = tty; | |
345 | return 0; | |
346 | free_mem_out: | |
347 | module_put(o_tty->driver->owner); | |
348 | free_tty_struct(o_tty); | |
349 | return -ENOMEM; | |
350 | } | |
351 | ||
352 | ||
b68e31d0 | 353 | static const struct tty_operations pty_ops = { |
bf970ee4 | 354 | .install = pty_install, |
1da177e4 LT |
355 | .open = pty_open, |
356 | .close = pty_close, | |
357 | .write = pty_write, | |
358 | .write_room = pty_write_room, | |
359 | .flush_buffer = pty_flush_buffer, | |
360 | .chars_in_buffer = pty_chars_in_buffer, | |
361 | .unthrottle = pty_unthrottle, | |
362 | .set_termios = pty_set_termios, | |
fc6f6238 | 363 | .resize = pty_resize |
1da177e4 LT |
364 | }; |
365 | ||
366 | /* Traditional BSD devices */ | |
367 | #ifdef CONFIG_LEGACY_PTYS | |
368 | static struct tty_driver *pty_driver, *pty_slave_driver; | |
369 | ||
370 | static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file, | |
371 | unsigned int cmd, unsigned long arg) | |
372 | { | |
373 | switch (cmd) { | |
374 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ | |
375 | return pty_set_lock(tty, (int __user *) arg); | |
376 | } | |
377 | return -ENOIOCTLCMD; | |
378 | } | |
379 | ||
dc8c8587 KS |
380 | static int legacy_count = CONFIG_LEGACY_PTY_COUNT; |
381 | module_param(legacy_count, int, 0); | |
382 | ||
3e8e88ca AC |
383 | static const struct tty_operations pty_ops_bsd = { |
384 | .open = pty_open, | |
385 | .close = pty_close, | |
386 | .write = pty_write, | |
387 | .write_room = pty_write_room, | |
388 | .flush_buffer = pty_flush_buffer, | |
389 | .chars_in_buffer = pty_chars_in_buffer, | |
390 | .unthrottle = pty_unthrottle, | |
391 | .set_termios = pty_set_termios, | |
392 | .ioctl = pty_bsd_ioctl, | |
fc6f6238 | 393 | .resize = pty_resize |
3e8e88ca AC |
394 | }; |
395 | ||
1da177e4 LT |
396 | static void __init legacy_pty_init(void) |
397 | { | |
dc8c8587 KS |
398 | if (legacy_count <= 0) |
399 | return; | |
1da177e4 | 400 | |
dc8c8587 | 401 | pty_driver = alloc_tty_driver(legacy_count); |
1da177e4 LT |
402 | if (!pty_driver) |
403 | panic("Couldn't allocate pty driver"); | |
404 | ||
dc8c8587 | 405 | pty_slave_driver = alloc_tty_driver(legacy_count); |
1da177e4 LT |
406 | if (!pty_slave_driver) |
407 | panic("Couldn't allocate pty slave driver"); | |
408 | ||
409 | pty_driver->owner = THIS_MODULE; | |
410 | pty_driver->driver_name = "pty_master"; | |
411 | pty_driver->name = "pty"; | |
1da177e4 LT |
412 | pty_driver->major = PTY_MASTER_MAJOR; |
413 | pty_driver->minor_start = 0; | |
414 | pty_driver->type = TTY_DRIVER_TYPE_PTY; | |
415 | pty_driver->subtype = PTY_TYPE_MASTER; | |
416 | pty_driver->init_termios = tty_std_termios; | |
417 | pty_driver->init_termios.c_iflag = 0; | |
418 | pty_driver->init_termios.c_oflag = 0; | |
419 | pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | |
420 | pty_driver->init_termios.c_lflag = 0; | |
606d099c AC |
421 | pty_driver->init_termios.c_ispeed = 38400; |
422 | pty_driver->init_termios.c_ospeed = 38400; | |
1da177e4 LT |
423 | pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW; |
424 | pty_driver->other = pty_slave_driver; | |
425 | tty_set_operations(pty_driver, &pty_ops); | |
1da177e4 LT |
426 | |
427 | pty_slave_driver->owner = THIS_MODULE; | |
428 | pty_slave_driver->driver_name = "pty_slave"; | |
429 | pty_slave_driver->name = "ttyp"; | |
1da177e4 LT |
430 | pty_slave_driver->major = PTY_SLAVE_MAJOR; |
431 | pty_slave_driver->minor_start = 0; | |
432 | pty_slave_driver->type = TTY_DRIVER_TYPE_PTY; | |
433 | pty_slave_driver->subtype = PTY_TYPE_SLAVE; | |
434 | pty_slave_driver->init_termios = tty_std_termios; | |
435 | pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | |
606d099c AC |
436 | pty_slave_driver->init_termios.c_ispeed = 38400; |
437 | pty_slave_driver->init_termios.c_ospeed = 38400; | |
1da177e4 LT |
438 | pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS | |
439 | TTY_DRIVER_REAL_RAW; | |
440 | pty_slave_driver->other = pty_driver; | |
441 | tty_set_operations(pty_slave_driver, &pty_ops); | |
442 | ||
443 | if (tty_register_driver(pty_driver)) | |
444 | panic("Couldn't register pty driver"); | |
445 | if (tty_register_driver(pty_slave_driver)) | |
446 | panic("Couldn't register pty slave driver"); | |
447 | } | |
448 | #else | |
449 | static inline void legacy_pty_init(void) { } | |
450 | #endif | |
451 | ||
452 | /* Unix98 devices */ | |
453 | #ifdef CONFIG_UNIX98_PTYS | |
454 | /* | |
455 | * sysctl support for setting limits on the number of Unix98 ptys allocated. | |
456 | * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly. | |
457 | */ | |
458 | int pty_limit = NR_UNIX98_PTY_DEFAULT; | |
fe9cd962 | 459 | static int pty_limit_min; |
1da177e4 | 460 | static int pty_limit_max = NR_UNIX98_PTY_MAX; |
fe9cd962 | 461 | static int pty_count; |
1da177e4 | 462 | |
d81ed103 AC |
463 | static struct cdev ptmx_cdev; |
464 | ||
35834ca1 | 465 | static struct ctl_table pty_table[] = { |
1da177e4 LT |
466 | { |
467 | .ctl_name = PTY_MAX, | |
468 | .procname = "max", | |
469 | .maxlen = sizeof(int), | |
470 | .mode = 0644, | |
471 | .data = &pty_limit, | |
472 | .proc_handler = &proc_dointvec_minmax, | |
473 | .strategy = &sysctl_intvec, | |
474 | .extra1 = &pty_limit_min, | |
475 | .extra2 = &pty_limit_max, | |
476 | }, { | |
477 | .ctl_name = PTY_NR, | |
478 | .procname = "nr", | |
479 | .maxlen = sizeof(int), | |
480 | .mode = 0444, | |
bf970ee4 | 481 | .data = &pty_count, |
1da177e4 LT |
482 | .proc_handler = &proc_dointvec, |
483 | }, { | |
484 | .ctl_name = 0 | |
485 | } | |
486 | }; | |
487 | ||
35834ca1 EB |
488 | static struct ctl_table pty_kern_table[] = { |
489 | { | |
490 | .ctl_name = KERN_PTY, | |
491 | .procname = "pty", | |
492 | .mode = 0555, | |
493 | .child = pty_table, | |
494 | }, | |
495 | {} | |
496 | }; | |
497 | ||
498 | static struct ctl_table pty_root_table[] = { | |
499 | { | |
500 | .ctl_name = CTL_KERN, | |
501 | .procname = "kernel", | |
502 | .mode = 0555, | |
503 | .child = pty_kern_table, | |
504 | }, | |
505 | {} | |
506 | }; | |
507 | ||
508 | ||
1da177e4 LT |
509 | static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file, |
510 | unsigned int cmd, unsigned long arg) | |
511 | { | |
512 | switch (cmd) { | |
513 | case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ | |
514 | return pty_set_lock(tty, (int __user *)arg); | |
515 | case TIOCGPTN: /* Get PT Number */ | |
516 | return put_user(tty->index, (unsigned int __user *)arg); | |
517 | } | |
518 | ||
519 | return -ENOIOCTLCMD; | |
520 | } | |
521 | ||
99f1fe18 AC |
522 | /** |
523 | * ptm_unix98_lookup - find a pty master | |
524 | * @driver: ptm driver | |
525 | * @idx: tty index | |
526 | * | |
527 | * Look up a pty master device. Called under the tty_mutex for now. | |
528 | * This provides our locking. | |
529 | */ | |
530 | ||
15f1a633 SB |
531 | static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver, |
532 | struct inode *ptm_inode, int idx) | |
99f1fe18 | 533 | { |
15f1a633 | 534 | struct tty_struct *tty = devpts_get_tty(ptm_inode, idx); |
99f1fe18 AC |
535 | if (tty) |
536 | tty = tty->link; | |
537 | return tty; | |
538 | } | |
539 | ||
540 | /** | |
541 | * pts_unix98_lookup - find a pty slave | |
542 | * @driver: pts driver | |
543 | * @idx: tty index | |
544 | * | |
545 | * Look up a pty master device. Called under the tty_mutex for now. | |
546 | * This provides our locking. | |
547 | */ | |
548 | ||
15f1a633 SB |
549 | static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, |
550 | struct inode *pts_inode, int idx) | |
99f1fe18 | 551 | { |
15f1a633 | 552 | struct tty_struct *tty = devpts_get_tty(pts_inode, idx); |
99f1fe18 AC |
553 | /* Master must be open before slave */ |
554 | if (!tty) | |
555 | return ERR_PTR(-EIO); | |
556 | return tty; | |
557 | } | |
558 | ||
bf970ee4 | 559 | static void pty_unix98_shutdown(struct tty_struct *tty) |
feebed65 AC |
560 | { |
561 | /* We have our own method as we don't use the tty index */ | |
562 | kfree(tty->termios); | |
feebed65 AC |
563 | } |
564 | ||
8b0a88d5 AC |
565 | /* We have no need to install and remove our tty objects as devpts does all |
566 | the work for us */ | |
567 | ||
bf970ee4 | 568 | static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) |
8b0a88d5 | 569 | { |
bf970ee4 AC |
570 | struct tty_struct *o_tty; |
571 | int idx = tty->index; | |
572 | ||
573 | o_tty = alloc_tty_struct(); | |
574 | if (!o_tty) | |
575 | return -ENOMEM; | |
576 | if (!try_module_get(driver->other->owner)) { | |
577 | /* This cannot in fact currently happen */ | |
578 | free_tty_struct(o_tty); | |
579 | return -ENOMEM; | |
580 | } | |
581 | initialize_tty_struct(o_tty, driver->other, idx); | |
582 | ||
8dff04ea | 583 | tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); |
bf970ee4 AC |
584 | if (tty->termios == NULL) |
585 | goto free_mem_out; | |
586 | *tty->termios = driver->init_termios; | |
8dff04ea AC |
587 | tty->termios_locked = tty->termios + 1; |
588 | ||
589 | o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL); | |
bf970ee4 AC |
590 | if (o_tty->termios == NULL) |
591 | goto free_mem_out; | |
592 | *o_tty->termios = driver->other->init_termios; | |
8dff04ea | 593 | o_tty->termios_locked = o_tty->termios + 1; |
bf970ee4 AC |
594 | |
595 | tty_driver_kref_get(driver->other); | |
596 | if (driver->subtype == PTY_TYPE_MASTER) | |
597 | o_tty->count++; | |
598 | /* Establish the links in both directions */ | |
599 | tty->link = o_tty; | |
600 | o_tty->link = tty; | |
601 | /* | |
602 | * All structures have been allocated, so now we install them. | |
603 | * Failures after this point use release_tty to clean up, so | |
604 | * there's no need to null out the local pointers. | |
605 | */ | |
606 | tty_driver_kref_get(driver); | |
607 | tty->count++; | |
608 | pty_count++; | |
8b0a88d5 | 609 | return 0; |
bf970ee4 | 610 | free_mem_out: |
8dff04ea | 611 | kfree(o_tty->termios); |
bf970ee4 AC |
612 | module_put(o_tty->driver->owner); |
613 | free_tty_struct(o_tty); | |
8dff04ea | 614 | kfree(tty->termios); |
bf970ee4 | 615 | return -ENOMEM; |
8b0a88d5 AC |
616 | } |
617 | ||
bf970ee4 | 618 | static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty) |
8b0a88d5 | 619 | { |
bf970ee4 | 620 | pty_count--; |
8b0a88d5 AC |
621 | } |
622 | ||
feebed65 | 623 | static const struct tty_operations ptm_unix98_ops = { |
99f1fe18 | 624 | .lookup = ptm_unix98_lookup, |
bf970ee4 AC |
625 | .install = pty_unix98_install, |
626 | .remove = pty_unix98_remove, | |
3e8e88ca AC |
627 | .open = pty_open, |
628 | .close = pty_close, | |
629 | .write = pty_write, | |
630 | .write_room = pty_write_room, | |
631 | .flush_buffer = pty_flush_buffer, | |
632 | .chars_in_buffer = pty_chars_in_buffer, | |
633 | .unthrottle = pty_unthrottle, | |
634 | .set_termios = pty_set_termios, | |
feebed65 | 635 | .ioctl = pty_unix98_ioctl, |
fc6f6238 AC |
636 | .shutdown = pty_unix98_shutdown, |
637 | .resize = pty_resize | |
3e8e88ca AC |
638 | }; |
639 | ||
99f1fe18 AC |
640 | static const struct tty_operations pty_unix98_ops = { |
641 | .lookup = pts_unix98_lookup, | |
bf970ee4 AC |
642 | .install = pty_unix98_install, |
643 | .remove = pty_unix98_remove, | |
99f1fe18 AC |
644 | .open = pty_open, |
645 | .close = pty_close, | |
646 | .write = pty_write, | |
647 | .write_room = pty_write_room, | |
648 | .flush_buffer = pty_flush_buffer, | |
649 | .chars_in_buffer = pty_chars_in_buffer, | |
650 | .unthrottle = pty_unthrottle, | |
651 | .set_termios = pty_set_termios, | |
bf970ee4 | 652 | .shutdown = pty_unix98_shutdown |
99f1fe18 | 653 | }; |
d81ed103 AC |
654 | |
655 | /** | |
656 | * ptmx_open - open a unix 98 pty master | |
657 | * @inode: inode of device file | |
658 | * @filp: file pointer to tty | |
659 | * | |
660 | * Allocate a unix98 pty master device from the ptmx driver. | |
661 | * | |
662 | * Locking: tty_mutex protects the init_dev work. tty->count should | |
663 | * protect the rest. | |
664 | * allocated_ptys_lock handles the list of free pty numbers | |
665 | */ | |
666 | ||
667 | static int __ptmx_open(struct inode *inode, struct file *filp) | |
668 | { | |
669 | struct tty_struct *tty; | |
670 | int retval; | |
671 | int index; | |
672 | ||
673 | nonseekable_open(inode, filp); | |
674 | ||
675 | /* find a device that is not in use. */ | |
15f1a633 | 676 | index = devpts_new_index(inode); |
d81ed103 AC |
677 | if (index < 0) |
678 | return index; | |
679 | ||
680 | mutex_lock(&tty_mutex); | |
73ec06fc | 681 | tty = tty_init_dev(ptm_driver, index, 1); |
d81ed103 AC |
682 | mutex_unlock(&tty_mutex); |
683 | ||
73ec06fc AC |
684 | if (IS_ERR(tty)) { |
685 | retval = PTR_ERR(tty); | |
d81ed103 | 686 | goto out; |
73ec06fc | 687 | } |
d81ed103 AC |
688 | |
689 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ | |
690 | filp->private_data = tty; | |
691 | file_move(filp, &tty->tty_files); | |
692 | ||
15f1a633 | 693 | retval = devpts_pty_new(inode, tty->link); |
d81ed103 AC |
694 | if (retval) |
695 | goto out1; | |
696 | ||
697 | retval = ptm_driver->ops->open(tty, filp); | |
698 | if (!retval) | |
699 | return 0; | |
700 | out1: | |
701 | tty_release_dev(filp); | |
702 | return retval; | |
703 | out: | |
15f1a633 | 704 | devpts_kill_index(inode, index); |
d81ed103 AC |
705 | return retval; |
706 | } | |
707 | ||
708 | static int ptmx_open(struct inode *inode, struct file *filp) | |
709 | { | |
710 | int ret; | |
711 | ||
712 | lock_kernel(); | |
713 | ret = __ptmx_open(inode, filp); | |
714 | unlock_kernel(); | |
715 | return ret; | |
716 | } | |
717 | ||
718 | static struct file_operations ptmx_fops; | |
719 | ||
1da177e4 LT |
720 | static void __init unix98_pty_init(void) |
721 | { | |
1da177e4 LT |
722 | ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); |
723 | if (!ptm_driver) | |
724 | panic("Couldn't allocate Unix98 ptm driver"); | |
725 | pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); | |
726 | if (!pts_driver) | |
727 | panic("Couldn't allocate Unix98 pts driver"); | |
728 | ||
729 | ptm_driver->owner = THIS_MODULE; | |
730 | ptm_driver->driver_name = "pty_master"; | |
731 | ptm_driver->name = "ptm"; | |
732 | ptm_driver->major = UNIX98_PTY_MASTER_MAJOR; | |
733 | ptm_driver->minor_start = 0; | |
734 | ptm_driver->type = TTY_DRIVER_TYPE_PTY; | |
735 | ptm_driver->subtype = PTY_TYPE_MASTER; | |
736 | ptm_driver->init_termios = tty_std_termios; | |
737 | ptm_driver->init_termios.c_iflag = 0; | |
738 | ptm_driver->init_termios.c_oflag = 0; | |
739 | ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | |
740 | ptm_driver->init_termios.c_lflag = 0; | |
606d099c AC |
741 | ptm_driver->init_termios.c_ispeed = 38400; |
742 | ptm_driver->init_termios.c_ospeed = 38400; | |
1da177e4 | 743 | ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | |
331b8319 | 744 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; |
1da177e4 | 745 | ptm_driver->other = pts_driver; |
feebed65 | 746 | tty_set_operations(ptm_driver, &ptm_unix98_ops); |
1da177e4 LT |
747 | |
748 | pts_driver->owner = THIS_MODULE; | |
749 | pts_driver->driver_name = "pty_slave"; | |
750 | pts_driver->name = "pts"; | |
751 | pts_driver->major = UNIX98_PTY_SLAVE_MAJOR; | |
752 | pts_driver->minor_start = 0; | |
753 | pts_driver->type = TTY_DRIVER_TYPE_PTY; | |
754 | pts_driver->subtype = PTY_TYPE_SLAVE; | |
755 | pts_driver->init_termios = tty_std_termios; | |
756 | pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; | |
606d099c AC |
757 | pts_driver->init_termios.c_ispeed = 38400; |
758 | pts_driver->init_termios.c_ospeed = 38400; | |
1da177e4 | 759 | pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | |
331b8319 | 760 | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; |
1da177e4 | 761 | pts_driver->other = ptm_driver; |
99f1fe18 | 762 | tty_set_operations(pts_driver, &pty_unix98_ops); |
fe9cd962 | 763 | |
1da177e4 LT |
764 | if (tty_register_driver(ptm_driver)) |
765 | panic("Couldn't register Unix98 ptm driver"); | |
766 | if (tty_register_driver(pts_driver)) | |
767 | panic("Couldn't register Unix98 pts driver"); | |
768 | ||
fe9cd962 | 769 | register_sysctl_table(pty_root_table); |
d81ed103 AC |
770 | |
771 | /* Now create the /dev/ptmx special device */ | |
772 | tty_default_fops(&ptmx_fops); | |
773 | ptmx_fops.open = ptmx_open; | |
774 | ||
775 | cdev_init(&ptmx_cdev, &ptmx_fops); | |
776 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || | |
777 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) | |
778 | panic("Couldn't register /dev/ptmx driver\n"); | |
779 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); | |
1da177e4 | 780 | } |
d81ed103 | 781 | |
1da177e4 LT |
782 | #else |
783 | static inline void unix98_pty_init(void) { } | |
784 | #endif | |
785 | ||
786 | static int __init pty_init(void) | |
787 | { | |
788 | legacy_pty_init(); | |
789 | unix98_pty_init(); | |
790 | return 0; | |
791 | } | |
792 | module_init(pty_init); |