vserver 1.9.3
[linux-2.6.git] / drivers / char / pty.c
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  *  Added TTY_DO_WRITE_WAKEUP to enable n_tty to send POLL_OUT to
9  *      waiting writers -- Sapan Bhatia <sapan@corewars.org>
10  *
11  *
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>       /* For EXPORT_SYMBOL */
16
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/interrupt.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/fcntl.h>
23 #include <linux/string.h>
24 #include <linux/major.h>
25 #include <linux/mm.h>
26 #include <linux/init.h>
27 #include <linux/devfs_fs_kernel.h>
28 #include <linux/sysctl.h>
29
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/bitops.h>
33 #include <linux/devpts_fs.h>
34
35 /* These are global because they are accessed in tty_io.c */
36 #ifdef CONFIG_UNIX98_PTYS
37 struct tty_driver *ptm_driver;
38 struct tty_driver *pts_driver;
39 #endif
40
41 static void pty_close(struct tty_struct * tty, struct file * filp)
42 {
43         if (!tty)
44                 return;
45         if (tty->driver->subtype == PTY_TYPE_MASTER) {
46                 if (tty->count > 1)
47                         printk("master pty_close: count = %d!!\n", tty->count);
48         } else {
49                 if (tty->count > 2)
50                         return;
51         }
52         wake_up_interruptible(&tty->read_wait);
53         wake_up_interruptible(&tty->write_wait);
54         tty->packet = 0;
55         if (!tty->link)
56                 return;
57         tty->link->packet = 0;
58         wake_up_interruptible(&tty->link->read_wait);
59         wake_up_interruptible(&tty->link->write_wait);
60         set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
61         if (tty->driver->subtype == PTY_TYPE_MASTER) {
62                 set_bit(TTY_OTHER_CLOSED, &tty->flags);
63 #ifdef CONFIG_UNIX98_PTYS
64                 if (tty->driver == ptm_driver)
65                         devpts_pty_kill(tty->index);
66 #endif
67                 tty_vhangup(tty->link);
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
75  * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE 
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  */
81 static void pty_unthrottle(struct tty_struct * tty)
82 {
83         struct tty_struct *o_tty = tty->link;
84
85         if (!o_tty)
86                 return;
87
88         tty_wakeup(o_tty);
89         set_bit(TTY_THROTTLED, &tty->flags);
90 }
91
92 /*
93  * WSH 05/24/97: modified to 
94  *   (1) use space in tty->flip instead of a shared temp buffer
95  *       The flip buffers aren't being used for a pty, so there's lots
96  *       of space available.  The buffer is protected by a per-pty
97  *       semaphore that should almost never come under contention.
98  *   (2) avoid redundant copying for cases where count >> receive_room
99  * N.B. Calls from user space may now return an error code instead of
100  * a count.
101  *
102  * FIXME: Our pty_write method is called with our ldisc lock held but
103  * not our partners. We can't just take the other one blindly without
104  * risking deadlocks.  There is also the small matter of TTY_DONT_FLIP
105  */
106 static int pty_write(struct tty_struct * tty, int from_user,
107                        const unsigned char *buf, int count)
108 {
109         struct tty_struct *to = tty->link;
110         int     c=0, n, room;
111         char    *temp_buffer;
112
113         if (!to || tty->stopped)
114                 return 0;
115
116         if (from_user) {
117                 down(&tty->flip.pty_sem);
118                 temp_buffer = &tty->flip.char_buf[0];
119                 while (count > 0) {
120                         /* check space so we don't copy needlessly */ 
121                         n = to->ldisc.receive_room(to);
122                         if (n > count)
123                                 n = count;
124                         if (!n) break;
125
126                         n  = min(n, PTY_BUF_SIZE);
127                         n -= copy_from_user(temp_buffer, buf, n);
128                         if (!n) {
129                                 if (!c)
130                                         c = -EFAULT;
131                                 break;
132                         }
133
134                         /* check again in case the buffer filled up */
135                         room = to->ldisc.receive_room(to);
136                         if (n > room)
137                                 n = room;
138                         if (!n) break;
139                         buf   += n; 
140                         c     += n;
141                         count -= n;
142                         to->ldisc.receive_buf(to, temp_buffer, NULL, n);
143                 }
144                 up(&tty->flip.pty_sem);
145         } else {
146                 c = to->ldisc.receive_room(to);
147                 if (c > count)
148                         c = count;
149                 to->ldisc.receive_buf(to, buf, NULL, c);
150         }
151         
152         return c;
153 }
154
155 static int pty_write_room(struct tty_struct *tty)
156 {
157         struct tty_struct *to = tty->link;
158
159         if (!to || tty->stopped)
160                 return 0;
161
162         return to->ldisc.receive_room(to);
163 }
164
165 /*
166  *      WSH 05/24/97:  Modified for asymmetric MASTER/SLAVE behavior
167  *      The chars_in_buffer() value is used by the ldisc select() function 
168  *      to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256).
169  *      The pty driver chars_in_buffer() Master/Slave must behave differently:
170  *
171  *      The Master side needs to allow typed-ahead commands to accumulate
172  *      while being canonicalized, so we report "our buffer" as empty until
173  *      some threshold is reached, and then report the count. (Any count >
174  *      WAKEUP_CHARS is regarded by select() as "full".)  To avoid deadlock 
175  *      the count returned must be 0 if no canonical data is available to be 
176  *      read. (The N_TTY ldisc.chars_in_buffer now knows this.)
177  *  
178  *      The Slave side passes all characters in raw mode to the Master side's
179  *      buffer where they can be read immediately, so in this case we can
180  *      return the true count in the buffer.
181  */
182 static int pty_chars_in_buffer(struct tty_struct *tty)
183 {
184         struct tty_struct *to = tty->link;
185         int count;
186
187         if (!to || !to->ldisc.chars_in_buffer)
188                 return 0;
189
190         /* The ldisc must report 0 if no characters available to be read */
191         count = to->ldisc.chars_in_buffer(to);
192
193         if (tty->driver->subtype == PTY_TYPE_SLAVE) return count;
194
195         /* Master side driver ... if the other side's read buffer is less than 
196          * half full, return 0 to allow writers to proceed; otherwise return
197          * the count.  This leaves a comfortable margin to avoid overflow, 
198          * and still allows half a buffer's worth of typed-ahead commands.
199          */
200         return ((count < N_TTY_BUF_SIZE/2) ? 0 : count);
201 }
202
203 /* Set the lock flag on a pty */
204 static int pty_set_lock(struct tty_struct *tty, int __user * arg)
205 {
206         int val;
207         if (get_user(val,arg))
208                 return -EFAULT;
209         if (val)
210                 set_bit(TTY_PTY_LOCK, &tty->flags);
211         else
212                 clear_bit(TTY_PTY_LOCK, &tty->flags);
213         return 0;
214 }
215
216 static void pty_flush_buffer(struct tty_struct *tty)
217 {
218         struct tty_struct *to = tty->link;
219         
220         if (!to)
221                 return;
222         
223         if (to->ldisc.flush_buffer)
224                 to->ldisc.flush_buffer(to);
225         
226         if (to->packet) {
227                 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
228                 wake_up_interruptible(&to->read_wait);
229         }
230 }
231
232 static int pty_open(struct tty_struct *tty, struct file * filp)
233 {
234         int     retval = -ENODEV;
235
236         if (!tty || !tty->link)
237                 goto out;
238
239         retval = -EIO;
240         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
241                 goto out;
242         if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
243                 goto out;
244         if (tty->link->count != 1)
245                 goto out;
246
247         clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
248         set_bit(TTY_THROTTLED, &tty->flags);
249         set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
250         retval = 0;
251 out:
252         return retval;
253 }
254
255 static void pty_set_termios(struct tty_struct *tty, struct termios *old_termios)
256 {
257         tty->termios->c_cflag &= ~(CSIZE | PARENB);
258         tty->termios->c_cflag |= (CS8 | CREAD);
259 }
260
261 static struct tty_operations pty_ops = {
262         .open = pty_open,
263         .close = pty_close,
264         .write = pty_write,
265         .write_room = pty_write_room,
266         .flush_buffer = pty_flush_buffer,
267         .chars_in_buffer = pty_chars_in_buffer,
268         .unthrottle = pty_unthrottle,
269         .set_termios = pty_set_termios,
270 };
271
272 /* Traditional BSD devices */
273 #ifdef CONFIG_LEGACY_PTYS
274 static struct tty_driver *pty_driver, *pty_slave_driver;
275
276 static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
277                          unsigned int cmd, unsigned long arg)
278 {
279         switch (cmd) {
280         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
281                 return pty_set_lock(tty, (int __user *) arg);
282         }
283         return -ENOIOCTLCMD;
284 }
285
286 static void __init legacy_pty_init(void)
287 {
288
289         pty_driver = alloc_tty_driver(NR_PTYS);
290         if (!pty_driver)
291                 panic("Couldn't allocate pty driver");
292
293         pty_slave_driver = alloc_tty_driver(NR_PTYS);
294         if (!pty_slave_driver)
295                 panic("Couldn't allocate pty slave driver");
296
297         pty_driver->owner = THIS_MODULE;
298         pty_driver->driver_name = "pty_master";
299         pty_driver->name = "pty";
300         pty_driver->devfs_name = "pty/m";
301         pty_driver->major = PTY_MASTER_MAJOR;
302         pty_driver->minor_start = 0;
303         pty_driver->type = TTY_DRIVER_TYPE_PTY;
304         pty_driver->subtype = PTY_TYPE_MASTER;
305         pty_driver->init_termios = tty_std_termios;
306         pty_driver->init_termios.c_iflag = 0;
307         pty_driver->init_termios.c_oflag = 0;
308         pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
309         pty_driver->init_termios.c_lflag = 0;
310         pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
311         pty_driver->other = pty_slave_driver;
312         tty_set_operations(pty_driver, &pty_ops);
313         pty_driver->ioctl = pty_bsd_ioctl;
314
315         pty_slave_driver->owner = THIS_MODULE;
316         pty_slave_driver->driver_name = "pty_slave";
317         pty_slave_driver->name = "ttyp";
318         pty_slave_driver->devfs_name = "pty/s";
319         pty_slave_driver->major = PTY_SLAVE_MAJOR;
320         pty_slave_driver->minor_start = 0;
321         pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
322         pty_slave_driver->subtype = PTY_TYPE_SLAVE;
323         pty_slave_driver->init_termios = tty_std_termios;
324         pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
325         pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
326                                         TTY_DRIVER_REAL_RAW;
327         pty_slave_driver->other = pty_driver;
328         tty_set_operations(pty_slave_driver, &pty_ops);
329
330         if (tty_register_driver(pty_driver))
331                 panic("Couldn't register pty driver");
332         if (tty_register_driver(pty_slave_driver))
333                 panic("Couldn't register pty slave driver");
334 }
335 #else
336 static inline void legacy_pty_init(void) { }
337 #endif
338
339 /* Unix98 devices */
340 #ifdef CONFIG_UNIX98_PTYS
341 /*
342  * sysctl support for setting limits on the number of Unix98 ptys allocated.
343  * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
344  */
345 int pty_limit = NR_UNIX98_PTY_DEFAULT;
346 static int pty_limit_min = 0;
347 static int pty_limit_max = NR_UNIX98_PTY_MAX;
348
349 ctl_table pty_table[] = {
350         {
351                 .ctl_name       = PTY_MAX,
352                 .procname       = "max",
353                 .maxlen         = sizeof(int),
354                 .mode           = 0644,
355                 .data           = &pty_limit,
356                 .proc_handler   = &proc_dointvec_minmax,
357                 .strategy       = &sysctl_intvec,
358                 .extra1         = &pty_limit_min,
359                 .extra2         = &pty_limit_max,
360         }, {
361                 .ctl_name       = PTY_NR,
362                 .procname       = "nr",
363                 .maxlen         = sizeof(int),
364                 .mode           = 0444,
365                 .proc_handler   = &proc_dointvec,
366         }, {
367                 .ctl_name       = 0
368         }
369 };
370
371 static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
372                             unsigned int cmd, unsigned long arg)
373 {
374         switch (cmd) {
375         case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
376                 return pty_set_lock(tty, (int __user *)arg);
377         case TIOCGPTN: /* Get PT Number */
378                 return put_user(tty->index, (unsigned int __user *)arg);
379         }
380
381         return -ENOIOCTLCMD;
382 }
383
384 static void __init unix98_pty_init(void)
385 {
386         devfs_mk_dir("pts");
387         ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
388         if (!ptm_driver)
389                 panic("Couldn't allocate Unix98 ptm driver");
390         pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
391         if (!pts_driver)
392                 panic("Couldn't allocate Unix98 pts driver");
393
394         ptm_driver->owner = THIS_MODULE;
395         ptm_driver->driver_name = "pty_master";
396         ptm_driver->name = "ptm";
397         ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
398         ptm_driver->minor_start = 0;
399         ptm_driver->type = TTY_DRIVER_TYPE_PTY;
400         ptm_driver->subtype = PTY_TYPE_MASTER;
401         ptm_driver->init_termios = tty_std_termios;
402         ptm_driver->init_termios.c_iflag = 0;
403         ptm_driver->init_termios.c_oflag = 0;
404         ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
405         ptm_driver->init_termios.c_lflag = 0;
406         ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
407                 TTY_DRIVER_NO_DEVFS | TTY_DRIVER_DEVPTS_MEM;
408         ptm_driver->other = pts_driver;
409         tty_set_operations(ptm_driver, &pty_ops);
410         ptm_driver->ioctl = pty_unix98_ioctl;
411
412         pts_driver->owner = THIS_MODULE;
413         pts_driver->driver_name = "pty_slave";
414         pts_driver->name = "pts";
415         pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
416         pts_driver->minor_start = 0;
417         pts_driver->type = TTY_DRIVER_TYPE_PTY;
418         pts_driver->subtype = PTY_TYPE_SLAVE;
419         pts_driver->init_termios = tty_std_termios;
420         pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
421         pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
422                 TTY_DRIVER_NO_DEVFS | TTY_DRIVER_DEVPTS_MEM;
423         pts_driver->other = ptm_driver;
424         tty_set_operations(pts_driver, &pty_ops);
425         
426         if (tty_register_driver(ptm_driver))
427                 panic("Couldn't register Unix98 ptm driver");
428         if (tty_register_driver(pts_driver))
429                 panic("Couldn't register Unix98 pts driver");
430
431         pty_table[1].data = &ptm_driver->refcount;
432 }
433 #else
434 static inline void unix98_pty_init(void) { }
435 #endif
436
437 static int __init pty_init(void)
438 {
439         legacy_pty_init();
440         unix98_pty_init();
441         return 0;
442 }
443 module_init(pty_init);