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/module.h> /* For EXPORT_SYMBOL */ 15 16#include <linux/errno.h> 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> 25#include <linux/sysctl.h> 26 27#include <asm/uaccess.h> 28#include <asm/system.h> 29#include <linux/bitops.h> 30#include <linux/devpts_fs.h> 31 32/* These are global because they are accessed in tty_io.c */ 33#ifdef CONFIG_UNIX98_PTYS 34struct tty_driver *ptm_driver; 35static struct tty_driver *pts_driver; 36#endif 37 38static void pty_close(struct tty_struct * tty, struct file * filp) 39{ 40 if (!tty) 41 return; 42 if (tty->driver->subtype == PTY_TYPE_MASTER) { 43 if (tty->count > 1) 44 printk("master pty_close: count = %d!!\n", tty->count); 45 } else { 46 if (tty->count > 2) 47 return; 48 } 49 wake_up_interruptible(&tty->read_wait); 50 wake_up_interruptible(&tty->write_wait); 51 tty->packet = 0; 52 if (!tty->link) 53 return; 54 tty->link->packet = 0; 55 set_bit(TTY_OTHER_CLOSED, &tty->link->flags); 56 wake_up_interruptible(&tty->link->read_wait); 57 wake_up_interruptible(&tty->link->write_wait); 58 if (tty->driver->subtype == PTY_TYPE_MASTER) { 59 set_bit(TTY_OTHER_CLOSED, &tty->flags); 60#ifdef CONFIG_UNIX98_PTYS 61 if (tty->driver == ptm_driver) 62 devpts_pty_kill(tty->index); 63#endif 64 tty_vhangup(tty->link); 65 } 66} 67 68/* 69 * The unthrottle routine is called by the line discipline to signal 70 * that it can receive more characters. For PTY's, the TTY_THROTTLED 71 * flag is always set, to force the line discipline to always call the 72 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE 73 * characters in the queue. This is necessary since each time this 74 * happens, we need to wake up any sleeping processes that could be 75 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent() 76 * for the pty buffer to be drained. 77 */ 78static void pty_unthrottle(struct tty_struct * tty) 79{ 80 struct tty_struct *o_tty = tty->link; 81 82 if (!o_tty) 83 return; 84 85 tty_wakeup(o_tty); 86 set_bit(TTY_THROTTLED, &tty->flags); 87} 88 89static int pty_write(struct tty_struct * tty, const unsigned char *buf, int count) 90{ 91 struct tty_struct *to = tty->link; 92 int c; 93 94 if (!to || tty->stopped) 95 return 0; 96 97 c = to->receive_room; 98 if (c > count) 99 c = count; 100 to->ldisc.receive_buf(to, buf, NULL, c); 101 102 return c; 103} 104 105static int pty_write_room(struct tty_struct *tty) 106{ 107 struct tty_struct *to = tty->link; 108 109 if (!to || tty->stopped) 110 return 0; 111 112 return to->receive_room; 113} 114 115/* 116 * WSH 05/24/97: Modified for asymmetric MASTER/SLAVE behavior 117 * The chars_in_buffer() value is used by the ldisc select() function 118 * to hold off writing when chars_in_buffer > WAKEUP_CHARS (== 256). 119 * The pty driver chars_in_buffer() Master/Slave must behave differently: 120 * 121 * The Master side needs to allow typed-ahead commands to accumulate 122 * while being canonicalized, so we report "our buffer" as empty until 123 * some threshold is reached, and then report the count. (Any count > 124 * WAKEUP_CHARS is regarded by select() as "full".) To avoid deadlock 125 * the count returned must be 0 if no canonical data is available to be 126 * read. (The N_TTY ldisc.chars_in_buffer now knows this.) 127 * 128 * The Slave side passes all characters in raw mode to the Master side's 129 * buffer where they can be read immediately, so in this case we can 130 * return the true count in the buffer. 131 */ 132static int pty_chars_in_buffer(struct tty_struct *tty) 133{ 134 struct tty_struct *to = tty->link; 135 int count; 136 137 /* We should get the line discipline lock for "tty->link" */ 138 if (!to || !to->ldisc.chars_in_buffer) 139 return 0; 140 141 /* The ldisc must report 0 if no characters available to be read */ 142 count = to->ldisc.chars_in_buffer(to); 143 144 if (tty->driver->subtype == PTY_TYPE_SLAVE) return count; 145 146 /* Master side driver ... if the other side's read buffer is less than 147 * half full, return 0 to allow writers to proceed; otherwise return 148 * the count. This leaves a comfortable margin to avoid overflow, 149 * and still allows half a buffer's worth of typed-ahead commands. 150 */ 151 return ((count < N_TTY_BUF_SIZE/2) ? 0 : count); 152} 153 154/* Set the lock flag on a pty */ 155static int pty_set_lock(struct tty_struct *tty, int __user * arg) 156{ 157 int val; 158 if (get_user(val,arg)) 159 return -EFAULT; 160 if (val) 161 set_bit(TTY_PTY_LOCK, &tty->flags); 162 else 163 clear_bit(TTY_PTY_LOCK, &tty->flags); 164 return 0; 165} 166 167static void pty_flush_buffer(struct tty_struct *tty) 168{ 169 struct tty_struct *to = tty->link; 170 171 if (!to) 172 return; 173 174 if (to->ldisc.flush_buffer) 175 to->ldisc.flush_buffer(to); 176 177 if (to->packet) { 178 tty->ctrl_status |= TIOCPKT_FLUSHWRITE; 179 wake_up_interruptible(&to->read_wait); 180 } 181} 182 183static int pty_open(struct tty_struct *tty, struct file * filp) 184{ 185 int retval = -ENODEV; 186 187 if (!tty || !tty->link) 188 goto out; 189 190 retval = -EIO; 191 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) 192 goto out; 193 if (test_bit(TTY_PTY_LOCK, &tty->link->flags)) 194 goto out; 195 if (tty->link->count != 1) 196 goto out; 197 198 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags); 199 set_bit(TTY_THROTTLED, &tty->flags); 200 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 201 retval = 0; 202out: 203 return retval; 204} 205 206static void pty_set_termios(struct tty_struct *tty, struct ktermios *old_termios) 207{ 208 tty->termios->c_cflag &= ~(CSIZE | PARENB); 209 tty->termios->c_cflag |= (CS8 | CREAD); 210} 211 212static const struct tty_operations pty_ops = { 213 .open = pty_open, 214 .close = pty_close, 215 .write = pty_write, 216 .write_room = pty_write_room, 217 .flush_buffer = pty_flush_buffer, 218 .chars_in_buffer = pty_chars_in_buffer, 219 .unthrottle = pty_unthrottle, 220 .set_termios = pty_set_termios, 221}; 222 223/* Traditional BSD devices */ 224#ifdef CONFIG_LEGACY_PTYS 225static struct tty_driver *pty_driver, *pty_slave_driver; 226 227static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file, 228 unsigned int cmd, unsigned long arg) 229{ 230 switch (cmd) { 231 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ 232 return pty_set_lock(tty, (int __user *) arg); 233 } 234 return -ENOIOCTLCMD; 235} 236 237static void __init legacy_pty_init(void) 238{ 239 240 pty_driver = alloc_tty_driver(NR_PTYS); 241 if (!pty_driver) 242 panic("Couldn't allocate pty driver"); 243 244 pty_slave_driver = alloc_tty_driver(NR_PTYS); 245 if (!pty_slave_driver) 246 panic("Couldn't allocate pty slave driver"); 247 248 pty_driver->owner = THIS_MODULE; 249 pty_driver->driver_name = "pty_master"; 250 pty_driver->name = "pty"; 251 pty_driver->major = PTY_MASTER_MAJOR; 252 pty_driver->minor_start = 0; 253 pty_driver->type = TTY_DRIVER_TYPE_PTY; 254 pty_driver->subtype = PTY_TYPE_MASTER; 255 pty_driver->init_termios = tty_std_termios; 256 pty_driver->init_termios.c_iflag = 0; 257 pty_driver->init_termios.c_oflag = 0; 258 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; 259 pty_driver->init_termios.c_lflag = 0; 260 pty_driver->init_termios.c_ispeed = 38400; 261 pty_driver->init_termios.c_ospeed = 38400; 262 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW; 263 pty_driver->other = pty_slave_driver; 264 tty_set_operations(pty_driver, &pty_ops); 265 pty_driver->ioctl = pty_bsd_ioctl; 266 267 pty_slave_driver->owner = THIS_MODULE; 268 pty_slave_driver->driver_name = "pty_slave"; 269 pty_slave_driver->name = "ttyp"; 270 pty_slave_driver->major = PTY_SLAVE_MAJOR; 271 pty_slave_driver->minor_start = 0; 272 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY; 273 pty_slave_driver->subtype = PTY_TYPE_SLAVE; 274 pty_slave_driver->init_termios = tty_std_termios; 275 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; 276 pty_slave_driver->init_termios.c_ispeed = 38400; 277 pty_slave_driver->init_termios.c_ospeed = 38400; 278 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS | 279 TTY_DRIVER_REAL_RAW; 280 pty_slave_driver->other = pty_driver; 281 tty_set_operations(pty_slave_driver, &pty_ops); 282 283 if (tty_register_driver(pty_driver)) 284 panic("Couldn't register pty driver"); 285 if (tty_register_driver(pty_slave_driver)) 286 panic("Couldn't register pty slave driver"); 287} 288#else 289static inline void legacy_pty_init(void) { } 290#endif 291 292/* Unix98 devices */ 293#ifdef CONFIG_UNIX98_PTYS 294/* 295 * sysctl support for setting limits on the number of Unix98 ptys allocated. 296 * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly. 297 */ 298int pty_limit = NR_UNIX98_PTY_DEFAULT; 299static int pty_limit_min = 0; 300static int pty_limit_max = NR_UNIX98_PTY_MAX; 301 302ctl_table pty_table[] = { 303 { 304 .ctl_name = PTY_MAX, 305 .procname = "max", 306 .maxlen = sizeof(int), 307 .mode = 0644, 308 .data = &pty_limit, 309 .proc_handler = &proc_dointvec_minmax, 310 .strategy = &sysctl_intvec, 311 .extra1 = &pty_limit_min, 312 .extra2 = &pty_limit_max, 313 }, { 314 .ctl_name = PTY_NR, 315 .procname = "nr", 316 .maxlen = sizeof(int), 317 .mode = 0444, 318 .proc_handler = &proc_dointvec, 319 }, { 320 .ctl_name = 0 321 } 322}; 323 324static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file, 325 unsigned int cmd, unsigned long arg) 326{ 327 switch (cmd) { 328 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */ 329 return pty_set_lock(tty, (int __user *)arg); 330 case TIOCGPTN: /* Get PT Number */ 331 return put_user(tty->index, (unsigned int __user *)arg); 332 } 333 334 return -ENOIOCTLCMD; 335} 336 337static void __init unix98_pty_init(void) 338{ 339 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); 340 if (!ptm_driver) 341 panic("Couldn't allocate Unix98 ptm driver"); 342 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX); 343 if (!pts_driver) 344 panic("Couldn't allocate Unix98 pts driver"); 345 346 ptm_driver->owner = THIS_MODULE; 347 ptm_driver->driver_name = "pty_master"; 348 ptm_driver->name = "ptm"; 349 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR; 350 ptm_driver->minor_start = 0; 351 ptm_driver->type = TTY_DRIVER_TYPE_PTY; 352 ptm_driver->subtype = PTY_TYPE_MASTER; 353 ptm_driver->init_termios = tty_std_termios; 354 ptm_driver->init_termios.c_iflag = 0; 355 ptm_driver->init_termios.c_oflag = 0; 356 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; 357 ptm_driver->init_termios.c_lflag = 0; 358 ptm_driver->init_termios.c_ispeed = 38400; 359 ptm_driver->init_termios.c_ospeed = 38400; 360 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | 361 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; 362 ptm_driver->other = pts_driver; 363 tty_set_operations(ptm_driver, &pty_ops); 364 ptm_driver->ioctl = pty_unix98_ioctl; 365 366 pts_driver->owner = THIS_MODULE; 367 pts_driver->driver_name = "pty_slave"; 368 pts_driver->name = "pts"; 369 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR; 370 pts_driver->minor_start = 0; 371 pts_driver->type = TTY_DRIVER_TYPE_PTY; 372 pts_driver->subtype = PTY_TYPE_SLAVE; 373 pts_driver->init_termios = tty_std_termios; 374 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; 375 pts_driver->init_termios.c_ispeed = 38400; 376 pts_driver->init_termios.c_ospeed = 38400; 377 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | 378 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM; 379 pts_driver->other = ptm_driver; 380 tty_set_operations(pts_driver, &pty_ops); 381 382 if (tty_register_driver(ptm_driver)) 383 panic("Couldn't register Unix98 ptm driver"); 384 if (tty_register_driver(pts_driver)) 385 panic("Couldn't register Unix98 pts driver"); 386 387 pty_table[1].data = &ptm_driver->refcount; 388} 389#else 390static inline void unix98_pty_init(void) { } 391#endif 392 393static int __init pty_init(void) 394{ 395 legacy_pty_init(); 396 unix98_pty_init(); 397 return 0; 398} 399module_init(pty_init); 400