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
8 */
9
10 #include <linux/config.h>
11 #include <linux/module.h> /* For EXPORT_SYMBOL */
12
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/fcntl.h>
19 #include <linux/string.h>
20 #include <linux/major.h>
21 #include <linux/mm.h>
22 #include <linux/init.h>
23 #include <linux/devfs_fs_kernel.h>
24
25 #include <asm/uaccess.h>
26 #include <asm/system.h>
27 #include <asm/bitops.h>
28
29 #define BUILDING_PTY_C 1
30 #include <linux/devpts_fs.h>
31
32 struct pty_struct {
33 int magic;
34 wait_queue_head_t open_wait;
35 };
36
37 #define PTY_MAGIC 0x5001
38
39 static struct tty_driver pty_driver, pty_slave_driver;
40 static int pty_refcount;
41
42 /* Note: one set of tables for BSD and one for Unix98 */
43 static struct tty_struct *pty_table[NR_PTYS];
44 static struct termios *pty_termios[NR_PTYS];
45 static struct termios *pty_termios_locked[NR_PTYS];
46 static struct tty_struct *ttyp_table[NR_PTYS];
47 static struct termios *ttyp_termios[NR_PTYS];
48 static struct termios *ttyp_termios_locked[NR_PTYS];
49 static struct pty_struct pty_state[NR_PTYS];
50
51 #ifdef CONFIG_UNIX98_PTYS
52 /* These are global because they are accessed in tty_io.c */
53 struct tty_driver ptm_driver[UNIX98_NR_MAJORS];
54 struct tty_driver pts_driver[UNIX98_NR_MAJORS];
55
56 static struct tty_struct *ptm_table[UNIX98_NR_MAJORS][NR_PTYS];
57 static struct termios *ptm_termios[UNIX98_NR_MAJORS][NR_PTYS];
58 static struct termios *ptm_termios_locked[UNIX98_NR_MAJORS][NR_PTYS];
59 static struct tty_struct *pts_table[UNIX98_NR_MAJORS][NR_PTYS];
60 static struct termios *pts_termios[UNIX98_NR_MAJORS][NR_PTYS];
61 static struct termios *pts_termios_locked[UNIX98_NR_MAJORS][NR_PTYS];
62 static struct pty_struct ptm_state[UNIX98_NR_MAJORS][NR_PTYS];
63 #endif
64
65 #define MIN(a,b) ((a) < (b) ? (a) : (b))
66
67 static void pty_close(struct tty_struct * tty, struct file * filp)
68 {
69 if (!tty)
70 return;
71 if (tty->driver.subtype == PTY_TYPE_MASTER) {
72 if (tty->count > 1)
73 printk("master pty_close: count = %d!!\n", tty->count);
74 } else {
75 if (tty->count > 2)
76 return;
77 }
78 wake_up_interruptible(&tty->read_wait);
79 wake_up_interruptible(&tty->write_wait);
80 tty->packet = 0;
81 if (!tty->link)
82 return;
83 tty->link->packet = 0;
84 wake_up_interruptible(&tty->link->read_wait);
85 wake_up_interruptible(&tty->link->write_wait);
86 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
87 if (tty->driver.subtype == PTY_TYPE_MASTER) {
88 set_bit(TTY_OTHER_CLOSED, &tty->flags);
89 #ifdef CONFIG_UNIX98_PTYS
90 {
91 unsigned int major = MAJOR(tty->device) - UNIX98_PTY_MASTER_MAJOR;
92 if ( major < UNIX98_NR_MAJORS ) {
93 devpts_pty_kill( MINOR(tty->device)
94 - tty->driver.minor_start + tty->driver.name_base );
95 }
96 }
97 #endif
98 tty_unregister_devfs (&tty->link->driver, MINOR (tty->device));
99 tty_vhangup(tty->link);
100 }
101 }
102
103 /*
104 * The unthrottle routine is called by the line discipline to signal
105 * that it can receive more characters. For PTY's, the TTY_THROTTLED
106 * flag is always set, to force the line discipline to always call the
107 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
108 * characters in the queue. This is necessary since each time this
109 * happens, we need to wake up any sleeping processes that could be
110 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
111 * for the pty buffer to be drained.
112 */
113 static void pty_unthrottle(struct tty_struct * tty)
114 {
115 struct tty_struct *o_tty = tty->link;
116
117 if (!o_tty)
118 return;
119
120 if ((o_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
121 o_tty->ldisc.write_wakeup)
122 (o_tty->ldisc.write_wakeup)(o_tty);
123 wake_up_interruptible(&o_tty->write_wait);
124 set_bit(TTY_THROTTLED, &tty->flags);
125 }
126
127 /*
128 * WSH 05/24/97: modified to
129 * (1) use space in tty->flip instead of a shared temp buffer
130 * The flip buffers aren't being used for a pty, so there's lots
131 * of space available. The buffer is protected by a per-pty
132 * semaphore that should almost never come under contention.
133 * (2) avoid redundant copying for cases where count >> receive_room
134 * N.B. Calls from user space may now return an error code instead of
135 * a count.
136 */
137 static int pty_write(struct tty_struct * tty, int from_user,
138 const unsigned char *buf, int count)
139 {
140 struct tty_struct *to = tty->link;
141 int c=0, n, room;
142 char *temp_buffer;
143
144 if (!to || tty->stopped)
145 return 0;
146
147 if (from_user) {
148 down(&tty->flip.pty_sem);
149 temp_buffer = &tty->flip.char_buf[0];
150 while (count > 0) {
151 /* check space so we don't copy needlessly */
152 n = to->ldisc.receive_room(to);
153 if (n > count)
154 n = count;
155 if (!n) break;
156
157 n = MIN(n, PTY_BUF_SIZE);
158 n -= copy_from_user(temp_buffer, buf, n);
159 if (!n) {
160 if (!c)
161 c = -EFAULT;
162 break;
163 }
164
165 /* check again in case the buffer filled up */
166 room = to->ldisc.receive_room(to);
167 if (n > room)
168 n = room;
169 if (!n) break;
170 buf += n;
171 c += n;
172 count -= n;
173 to->ldisc.receive_buf(to, temp_buffer, 0, n);
174 }
175 up(&tty->flip.pty_sem);
176 } else {
177 c = to->ldisc.receive_room(to);
178 if (c > count)
179 c = count;
180 to->ldisc.receive_buf(to, buf, 0, c);
181 }
182
183 return c;
184 }
185
186 static int pty_write_room(struct tty_struct *tty)
187 {
188 struct tty_struct *to = tty->link;
189
190 if (!to || tty->stopped)
191 return 0;
192
193 return to->ldisc.receive_room(to);
194 }
195
196 /*
197 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior
198 * The chars_in_buffer() value is used by the ldisc select() function
199 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
200 * The pty driver chars_in_buffer() Master/Slave must behave differently:
201 *
202 * The Master side needs to allow typed-ahead commands to accumulate
203 * while being canonicalized, so we report "our buffer" as empty until
204 * some threshold is reached, and then report the count. (Any count >
205 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock
206 * the count returned must be 0 if no canonical data is available to be
207 * read. (The N_TTY ldisc.chars_in_buffer now knows this.)
208 *
209 * The Slave side passes all characters in raw mode to the Master side's
210 * buffer where they can be read immediately, so in this case we can
211 * return the true count in the buffer.
212 */
213 static int pty_chars_in_buffer(struct tty_struct *tty)
214 {
215 struct tty_struct *to = tty->link;
216 int count;
217
218 if (!to || !to->ldisc.chars_in_buffer)
219 return 0;
220
221 /* The ldisc must report 0 if no characters available to be read */
222 count = to->ldisc.chars_in_buffer(to);
223
224 if (tty->driver.subtype == PTY_TYPE_SLAVE) return count;
225
226 /* Master side driver ... if the other side's read buffer is less than
227 * half full, return 0 to allow writers to proceed; otherwise return
228 * the count. This leaves a comfortable margin to avoid overflow,
229 * and still allows half a buffer's worth of typed-ahead commands.
230 */
231 return ((count < N_TTY_BUF_SIZE/2) ? 0 : count);
232 }
233
234 /*
235 * Return the device number of a Unix98 PTY (only!). This lets us open a
236 * master pty with the multi-headed ptmx device, then find out which
237 * one we got after it is open, with an ioctl.
238 */
239 #ifdef CONFIG_UNIX98_PTYS
240 static int pty_get_device_number(struct tty_struct *tty, unsigned int *value)
241 {
242 unsigned int result = MINOR(tty->device)
243 - tty->driver.minor_start + tty->driver.name_base;
244 return put_user(result, value);
245 }
246 #endif
247
248 /* Set the lock flag on a pty */
249 static int pty_set_lock(struct tty_struct *tty, int * arg)
250 {
251 int val;
252 if (get_user(val,arg))
253 return -EFAULT;
254 if (val)
255 set_bit(TTY_PTY_LOCK, &tty->flags);
256 else
257 clear_bit(TTY_PTY_LOCK, &tty->flags);
258 return 0;
259 }
260
261 static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
262 unsigned int cmd, unsigned long arg)
263 {
264 if (!tty) {
265 printk("pty_ioctl called with NULL tty!\n");
266 return -EIO;
267 }
268 switch(cmd) {
269 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
270 return pty_set_lock(tty, (int *) arg);
271 }
272 return -ENOIOCTLCMD;
273 }
274
275 #ifdef CONFIG_UNIX98_PTYS
276 static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
277 unsigned int cmd, unsigned long arg)
278 {
279 if (!tty) {
280 printk("pty_unix98_ioctl called with NULL tty!\n");
281 return -EIO;
282 }
283 switch(cmd) {
284 case TIOCGPTN: /* Get PT Number */
285 return pty_get_device_number(tty, (unsigned int *)arg);
286 }
287
288 return pty_bsd_ioctl(tty,file,cmd,arg);
289 }
290 #endif
291
292 static void pty_flush_buffer(struct tty_struct *tty)
293 {
294 struct tty_struct *to = tty->link;
295
296 if (!to)
297 return;
298
299 if (to->ldisc.flush_buffer)
300 to->ldisc.flush_buffer(to);
301
302 if (to->packet) {
303 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
304 wake_up_interruptible(&to->read_wait);
305 }
306 }
307
308 static int pty_open(struct tty_struct *tty, struct file * filp)
309 {
310 int retval;
311 int line;
312 struct pty_struct *pty;
313
314 retval = -ENODEV;
315 if (!tty || !tty->link)
316 goto out;
317 line = MINOR(tty->device) - tty->driver.minor_start;
318 if ((line < 0) || (line >= NR_PTYS))
319 goto out;
320 pty = (struct pty_struct *)(tty->driver.driver_state) + line;
321 tty->driver_data = pty;
322
323 retval = -EIO;
324 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
325 goto out;
326 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
327 goto out;
328 if (tty->link->count != 1)
329 goto out;
330
331 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
332 wake_up_interruptible(&pty->open_wait);
333 set_bit(TTY_THROTTLED, &tty->flags);
334 /* Register a slave for the master */
335 if (tty->driver.major == PTY_MASTER_MAJOR)
336 tty_register_devfs(&tty->link->driver,
337 DEVFS_FL_AUTO_OWNER | DEVFS_FL_WAIT,
338 tty->link->driver.minor_start +
339 MINOR(tty->device)-tty->driver.minor_start);
340 retval = 0;
341 out:
342 return retval;
343 }
344
345 static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
346 {
347 tty->termios->c_cflag &= ~(CSIZE | PARENB);
348 tty->termios->c_cflag |= (CS8 | CREAD);
349 }
350
351 int __init pty_init(void)
352 {
353 int i;
354
355 /* Traditional BSD devices */
356
357 memset(&pty_state, 0, sizeof(pty_state));
358 for (i = 0; i < NR_PTYS; i++)
359 init_waitqueue_head(&pty_state[i].open_wait);
360 memset(&pty_driver, 0, sizeof(struct tty_driver));
361 pty_driver.magic = TTY_DRIVER_MAGIC;
362 pty_driver.driver_name = "pty_master";
363 #ifdef CONFIG_DEVFS_FS
364 pty_driver.name = "pty/m%d";
365 #else
366 pty_driver.name = "pty";
367 #endif
368 pty_driver.major = PTY_MASTER_MAJOR;
369 pty_driver.minor_start = 0;
370 pty_driver.num = NR_PTYS;
371 pty_driver.type = TTY_DRIVER_TYPE_PTY;
372 pty_driver.subtype = PTY_TYPE_MASTER;
373 pty_driver.init_termios = tty_std_termios;
374 pty_driver.init_termios.c_iflag = 0;
375 pty_driver.init_termios.c_oflag = 0;
376 pty_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
377 pty_driver.init_termios.c_lflag = 0;
378 pty_driver.flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
379 pty_driver.refcount = &pty_refcount;
380 pty_driver.table = pty_table;
381 pty_driver.termios = pty_termios;
382 pty_driver.termios_locked = pty_termios_locked;
383 pty_driver.driver_state = pty_state;
384 pty_driver.other = &pty_slave_driver;
385
386 pty_driver.open = pty_open;
387 pty_driver.close = pty_close;
388 pty_driver.write = pty_write;
389 pty_driver.write_room = pty_write_room;
390 pty_driver.flush_buffer = pty_flush_buffer;
391 pty_driver.chars_in_buffer = pty_chars_in_buffer;
392 pty_driver.unthrottle = pty_unthrottle;
393 pty_driver.set_termios = pty_set_termios;
394
395 pty_slave_driver = pty_driver;
396 pty_slave_driver.driver_name = "pty_slave";
397 pty_slave_driver.proc_entry = 0;
398 #ifdef CONFIG_DEVFS_FS
399 pty_slave_driver.name = "pty/s%d";
400 #else
401 pty_slave_driver.name = "ttyp";
402 #endif
403 pty_slave_driver.subtype = PTY_TYPE_SLAVE;
404 pty_slave_driver.major = PTY_SLAVE_MAJOR;
405 pty_slave_driver.minor_start = 0;
406 pty_slave_driver.init_termios = tty_std_termios;
407 pty_slave_driver.init_termios.c_cflag = B38400 | CS8 | CREAD;
408 /* Slave ptys are registered when their corresponding master pty
409 * is opened, and unregistered when the pair is closed.
410 */
411 pty_slave_driver.flags |= TTY_DRIVER_NO_DEVFS;
412 pty_slave_driver.table = ttyp_table;
413 pty_slave_driver.termios = ttyp_termios;
414 pty_slave_driver.termios_locked = ttyp_termios_locked;
415 pty_slave_driver.driver_state = pty_state;
416 pty_slave_driver.other = &pty_driver;
417
418 if (tty_register_driver(&pty_driver))
419 panic("Couldn't register pty driver");
420 if (tty_register_driver(&pty_slave_driver))
421 panic("Couldn't register pty slave driver");
422
423 /*
424 * only the master pty gets this ioctl (which is why we
425 * assign it here, instead of up with the rest of the
426 * pty_driver initialization. <cananian@alumni.princeton.edu>
427 */
428 pty_driver.ioctl = pty_bsd_ioctl;
429
430 /* Unix98 devices */
431 #ifdef CONFIG_UNIX98_PTYS
432 devfs_mk_dir (NULL, "pts", NULL);
433 printk("pty: %d Unix98 ptys configured\n", UNIX98_NR_MAJORS*NR_PTYS);
434 for ( i = 0 ; i < UNIX98_NR_MAJORS ; i++ ) {
435 int j;
436
437 ptm_driver[i] = pty_driver;
438 ptm_driver[i].name = "ptm";
439 ptm_driver[i].proc_entry = 0;
440 ptm_driver[i].major = UNIX98_PTY_MASTER_MAJOR+i;
441 ptm_driver[i].minor_start = 0;
442 ptm_driver[i].name_base = i*NR_PTYS;
443 ptm_driver[i].num = NR_PTYS;
444 ptm_driver[i].other = &pts_driver[i];
445 ptm_driver[i].flags |= TTY_DRIVER_NO_DEVFS;
446 ptm_driver[i].table = ptm_table[i];
447 ptm_driver[i].termios = ptm_termios[i];
448 ptm_driver[i].termios_locked = ptm_termios_locked[i];
449 ptm_driver[i].driver_state = ptm_state[i];
450
451 for (j = 0; j < NR_PTYS; j++)
452 init_waitqueue_head(&ptm_state[i][j].open_wait);
453
454 pts_driver[i] = pty_slave_driver;
455 #ifdef CONFIG_DEVFS_FS
456 pts_driver[i].name = "pts/%d";
457 #else
458 pts_driver[i].name = "pts";
459 #endif
460 pts_driver[i].proc_entry = 0;
461 pts_driver[i].major = UNIX98_PTY_SLAVE_MAJOR+i;
462 pts_driver[i].minor_start = 0;
463 pts_driver[i].name_base = i*NR_PTYS;
464 pts_driver[i].num = ptm_driver[i].num;
465 pts_driver[i].other = &ptm_driver[i];
466 pts_driver[i].table = pts_table[i];
467 pts_driver[i].termios = pts_termios[i];
468 pts_driver[i].termios_locked = pts_termios_locked[i];
469 pts_driver[i].driver_state = ptm_state[i];
470
471 ptm_driver[i].ioctl = pty_unix98_ioctl;
472
473 if (tty_register_driver(&ptm_driver[i]))
474 panic("Couldn't register Unix98 ptm driver major %d",
475 ptm_driver[i].major);
476 if (tty_register_driver(&pts_driver[i]))
477 panic("Couldn't register Unix98 pts driver major %d",
478 pts_driver[i].major);
479 }
480 #endif
481 return 0;
482 }
483
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.