Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.29-rc2 726 lines 18 kB view raw
1#include <linux/types.h> 2#include <linux/major.h> 3#include <linux/errno.h> 4#include <linux/signal.h> 5#include <linux/fcntl.h> 6#include <linux/sched.h> 7#include <linux/interrupt.h> 8#include <linux/tty.h> 9#include <linux/tty_driver.h> 10#include <linux/tty_flip.h> 11#include <linux/devpts_fs.h> 12#include <linux/file.h> 13#include <linux/fdtable.h> 14#include <linux/console.h> 15#include <linux/timer.h> 16#include <linux/ctype.h> 17#include <linux/kd.h> 18#include <linux/mm.h> 19#include <linux/string.h> 20#include <linux/slab.h> 21#include <linux/poll.h> 22#include <linux/proc_fs.h> 23#include <linux/init.h> 24#include <linux/module.h> 25#include <linux/smp_lock.h> 26#include <linux/device.h> 27#include <linux/wait.h> 28#include <linux/bitops.h> 29#include <linux/delay.h> 30#include <linux/seq_file.h> 31 32#include <linux/uaccess.h> 33#include <asm/system.h> 34 35#include <linux/kbd_kern.h> 36#include <linux/vt_kern.h> 37#include <linux/selection.h> 38 39#include <linux/kmod.h> 40#include <linux/nsproxy.h> 41 42/* 43 * This guards the refcounted line discipline lists. The lock 44 * must be taken with irqs off because there are hangup path 45 * callers who will do ldisc lookups and cannot sleep. 46 */ 47 48static DEFINE_SPINLOCK(tty_ldisc_lock); 49static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); 50/* Line disc dispatch table */ 51static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; 52 53/** 54 * tty_register_ldisc - install a line discipline 55 * @disc: ldisc number 56 * @new_ldisc: pointer to the ldisc object 57 * 58 * Installs a new line discipline into the kernel. The discipline 59 * is set up as unreferenced and then made available to the kernel 60 * from this point onwards. 61 * 62 * Locking: 63 * takes tty_ldisc_lock to guard against ldisc races 64 */ 65 66int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) 67{ 68 unsigned long flags; 69 int ret = 0; 70 71 if (disc < N_TTY || disc >= NR_LDISCS) 72 return -EINVAL; 73 74 spin_lock_irqsave(&tty_ldisc_lock, flags); 75 tty_ldiscs[disc] = new_ldisc; 76 new_ldisc->num = disc; 77 new_ldisc->refcount = 0; 78 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 79 80 return ret; 81} 82EXPORT_SYMBOL(tty_register_ldisc); 83 84/** 85 * tty_unregister_ldisc - unload a line discipline 86 * @disc: ldisc number 87 * @new_ldisc: pointer to the ldisc object 88 * 89 * Remove a line discipline from the kernel providing it is not 90 * currently in use. 91 * 92 * Locking: 93 * takes tty_ldisc_lock to guard against ldisc races 94 */ 95 96int tty_unregister_ldisc(int disc) 97{ 98 unsigned long flags; 99 int ret = 0; 100 101 if (disc < N_TTY || disc >= NR_LDISCS) 102 return -EINVAL; 103 104 spin_lock_irqsave(&tty_ldisc_lock, flags); 105 if (tty_ldiscs[disc]->refcount) 106 ret = -EBUSY; 107 else 108 tty_ldiscs[disc] = NULL; 109 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 110 111 return ret; 112} 113EXPORT_SYMBOL(tty_unregister_ldisc); 114 115 116/** 117 * tty_ldisc_try_get - try and reference an ldisc 118 * @disc: ldisc number 119 * @ld: tty ldisc structure to complete 120 * 121 * Attempt to open and lock a line discipline into place. Return 122 * the line discipline refcounted and assigned in ld. On an error 123 * report the error code back 124 */ 125 126static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld) 127{ 128 unsigned long flags; 129 struct tty_ldisc_ops *ldops; 130 int err = -EINVAL; 131 132 spin_lock_irqsave(&tty_ldisc_lock, flags); 133 ld->ops = NULL; 134 ldops = tty_ldiscs[disc]; 135 /* Check the entry is defined */ 136 if (ldops) { 137 /* If the module is being unloaded we can't use it */ 138 if (!try_module_get(ldops->owner)) 139 err = -EAGAIN; 140 else { 141 /* lock it */ 142 ldops->refcount++; 143 ld->ops = ldops; 144 err = 0; 145 } 146 } 147 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 148 return err; 149} 150 151/** 152 * tty_ldisc_get - take a reference to an ldisc 153 * @disc: ldisc number 154 * @ld: tty line discipline structure to use 155 * 156 * Takes a reference to a line discipline. Deals with refcounts and 157 * module locking counts. Returns NULL if the discipline is not available. 158 * Returns a pointer to the discipline and bumps the ref count if it is 159 * available 160 * 161 * Locking: 162 * takes tty_ldisc_lock to guard against ldisc races 163 */ 164 165static int tty_ldisc_get(int disc, struct tty_ldisc *ld) 166{ 167 int err; 168 169 if (disc < N_TTY || disc >= NR_LDISCS) 170 return -EINVAL; 171 err = tty_ldisc_try_get(disc, ld); 172 if (err < 0) { 173 request_module("tty-ldisc-%d", disc); 174 err = tty_ldisc_try_get(disc, ld); 175 } 176 return err; 177} 178 179/** 180 * tty_ldisc_put - drop ldisc reference 181 * @disc: ldisc number 182 * 183 * Drop a reference to a line discipline. Manage refcounts and 184 * module usage counts 185 * 186 * Locking: 187 * takes tty_ldisc_lock to guard against ldisc races 188 */ 189 190static void tty_ldisc_put(struct tty_ldisc_ops *ld) 191{ 192 unsigned long flags; 193 int disc = ld->num; 194 195 BUG_ON(disc < N_TTY || disc >= NR_LDISCS); 196 197 spin_lock_irqsave(&tty_ldisc_lock, flags); 198 ld = tty_ldiscs[disc]; 199 BUG_ON(ld->refcount == 0); 200 ld->refcount--; 201 module_put(ld->owner); 202 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 203} 204 205static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) 206{ 207 return (*pos < NR_LDISCS) ? pos : NULL; 208} 209 210static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) 211{ 212 (*pos)++; 213 return (*pos < NR_LDISCS) ? pos : NULL; 214} 215 216static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) 217{ 218} 219 220static int tty_ldiscs_seq_show(struct seq_file *m, void *v) 221{ 222 int i = *(loff_t *)v; 223 struct tty_ldisc ld; 224 225 if (tty_ldisc_get(i, &ld) < 0) 226 return 0; 227 seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i); 228 tty_ldisc_put(ld.ops); 229 return 0; 230} 231 232static const struct seq_operations tty_ldiscs_seq_ops = { 233 .start = tty_ldiscs_seq_start, 234 .next = tty_ldiscs_seq_next, 235 .stop = tty_ldiscs_seq_stop, 236 .show = tty_ldiscs_seq_show, 237}; 238 239static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) 240{ 241 return seq_open(file, &tty_ldiscs_seq_ops); 242} 243 244const struct file_operations tty_ldiscs_proc_fops = { 245 .owner = THIS_MODULE, 246 .open = proc_tty_ldiscs_open, 247 .read = seq_read, 248 .llseek = seq_lseek, 249 .release = seq_release, 250}; 251 252/** 253 * tty_ldisc_assign - set ldisc on a tty 254 * @tty: tty to assign 255 * @ld: line discipline 256 * 257 * Install an instance of a line discipline into a tty structure. The 258 * ldisc must have a reference count above zero to ensure it remains/ 259 * The tty instance refcount starts at zero. 260 * 261 * Locking: 262 * Caller must hold references 263 */ 264 265static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) 266{ 267 ld->refcount = 0; 268 tty->ldisc = *ld; 269} 270 271/** 272 * tty_ldisc_try - internal helper 273 * @tty: the tty 274 * 275 * Make a single attempt to grab and bump the refcount on 276 * the tty ldisc. Return 0 on failure or 1 on success. This is 277 * used to implement both the waiting and non waiting versions 278 * of tty_ldisc_ref 279 * 280 * Locking: takes tty_ldisc_lock 281 */ 282 283static int tty_ldisc_try(struct tty_struct *tty) 284{ 285 unsigned long flags; 286 struct tty_ldisc *ld; 287 int ret = 0; 288 289 spin_lock_irqsave(&tty_ldisc_lock, flags); 290 ld = &tty->ldisc; 291 if (test_bit(TTY_LDISC, &tty->flags)) { 292 ld->refcount++; 293 ret = 1; 294 } 295 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 296 return ret; 297} 298 299/** 300 * tty_ldisc_ref_wait - wait for the tty ldisc 301 * @tty: tty device 302 * 303 * Dereference the line discipline for the terminal and take a 304 * reference to it. If the line discipline is in flux then 305 * wait patiently until it changes. 306 * 307 * Note: Must not be called from an IRQ/timer context. The caller 308 * must also be careful not to hold other locks that will deadlock 309 * against a discipline change, such as an existing ldisc reference 310 * (which we check for) 311 * 312 * Locking: call functions take tty_ldisc_lock 313 */ 314 315struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) 316{ 317 /* wait_event is a macro */ 318 wait_event(tty_ldisc_wait, tty_ldisc_try(tty)); 319 WARN_ON(tty->ldisc.refcount == 0); 320 return &tty->ldisc; 321} 322 323EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); 324 325/** 326 * tty_ldisc_ref - get the tty ldisc 327 * @tty: tty device 328 * 329 * Dereference the line discipline for the terminal and take a 330 * reference to it. If the line discipline is in flux then 331 * return NULL. Can be called from IRQ and timer functions. 332 * 333 * Locking: called functions take tty_ldisc_lock 334 */ 335 336struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) 337{ 338 if (tty_ldisc_try(tty)) 339 return &tty->ldisc; 340 return NULL; 341} 342 343EXPORT_SYMBOL_GPL(tty_ldisc_ref); 344 345/** 346 * tty_ldisc_deref - free a tty ldisc reference 347 * @ld: reference to free up 348 * 349 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May 350 * be called in IRQ context. 351 * 352 * Locking: takes tty_ldisc_lock 353 */ 354 355void tty_ldisc_deref(struct tty_ldisc *ld) 356{ 357 unsigned long flags; 358 359 BUG_ON(ld == NULL); 360 361 spin_lock_irqsave(&tty_ldisc_lock, flags); 362 if (ld->refcount == 0) 363 printk(KERN_ERR "tty_ldisc_deref: no references.\n"); 364 else 365 ld->refcount--; 366 if (ld->refcount == 0) 367 wake_up(&tty_ldisc_wait); 368 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 369} 370 371EXPORT_SYMBOL_GPL(tty_ldisc_deref); 372 373/** 374 * tty_ldisc_enable - allow ldisc use 375 * @tty: terminal to activate ldisc on 376 * 377 * Set the TTY_LDISC flag when the line discipline can be called 378 * again. Do necessary wakeups for existing sleepers. Clear the LDISC 379 * changing flag to indicate any ldisc change is now over. 380 * 381 * Note: nobody should set the TTY_LDISC bit except via this function. 382 * Clearing directly is allowed. 383 */ 384 385void tty_ldisc_enable(struct tty_struct *tty) 386{ 387 set_bit(TTY_LDISC, &tty->flags); 388 clear_bit(TTY_LDISC_CHANGING, &tty->flags); 389 wake_up(&tty_ldisc_wait); 390} 391 392/** 393 * tty_set_termios_ldisc - set ldisc field 394 * @tty: tty structure 395 * @num: line discipline number 396 * 397 * This is probably overkill for real world processors but 398 * they are not on hot paths so a little discipline won't do 399 * any harm. 400 * 401 * Locking: takes termios_mutex 402 */ 403 404static void tty_set_termios_ldisc(struct tty_struct *tty, int num) 405{ 406 mutex_lock(&tty->termios_mutex); 407 tty->termios->c_line = num; 408 mutex_unlock(&tty->termios_mutex); 409} 410 411 412/** 413 * tty_ldisc_restore - helper for tty ldisc change 414 * @tty: tty to recover 415 * @old: previous ldisc 416 * 417 * Restore the previous line discipline or N_TTY when a line discipline 418 * change fails due to an open error 419 */ 420 421static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) 422{ 423 char buf[64]; 424 struct tty_ldisc new_ldisc; 425 426 /* There is an outstanding reference here so this is safe */ 427 tty_ldisc_get(old->ops->num, old); 428 tty_ldisc_assign(tty, old); 429 tty_set_termios_ldisc(tty, old->ops->num); 430 if (old->ops->open && (old->ops->open(tty) < 0)) { 431 tty_ldisc_put(old->ops); 432 /* This driver is always present */ 433 if (tty_ldisc_get(N_TTY, &new_ldisc) < 0) 434 panic("n_tty: get"); 435 tty_ldisc_assign(tty, &new_ldisc); 436 tty_set_termios_ldisc(tty, N_TTY); 437 if (new_ldisc.ops->open) { 438 int r = new_ldisc.ops->open(tty); 439 if (r < 0) 440 panic("Couldn't open N_TTY ldisc for " 441 "%s --- error %d.", 442 tty_name(tty, buf), r); 443 } 444 } 445} 446 447/** 448 * tty_set_ldisc - set line discipline 449 * @tty: the terminal to set 450 * @ldisc: the line discipline 451 * 452 * Set the discipline of a tty line. Must be called from a process 453 * context. 454 * 455 * Locking: takes tty_ldisc_lock. 456 * called functions take termios_mutex 457 */ 458 459int tty_set_ldisc(struct tty_struct *tty, int ldisc) 460{ 461 int retval; 462 struct tty_ldisc o_ldisc, new_ldisc; 463 int work; 464 unsigned long flags; 465 struct tty_struct *o_tty; 466 467restart: 468 /* This is a bit ugly for now but means we can break the 'ldisc 469 is part of the tty struct' assumption later */ 470 retval = tty_ldisc_get(ldisc, &new_ldisc); 471 if (retval) 472 return retval; 473 474 /* 475 * Problem: What do we do if this blocks ? 476 */ 477 478 tty_wait_until_sent(tty, 0); 479 480 if (tty->ldisc.ops->num == ldisc) { 481 tty_ldisc_put(new_ldisc.ops); 482 return 0; 483 } 484 485 /* 486 * No more input please, we are switching. The new ldisc 487 * will update this value in the ldisc open function 488 */ 489 490 tty->receive_room = 0; 491 492 o_ldisc = tty->ldisc; 493 o_tty = tty->link; 494 495 /* 496 * Make sure we don't change while someone holds a 497 * reference to the line discipline. The TTY_LDISC bit 498 * prevents anyone taking a reference once it is clear. 499 * We need the lock to avoid racing reference takers. 500 * 501 * We must clear the TTY_LDISC bit here to avoid a livelock 502 * with a userspace app continually trying to use the tty in 503 * parallel to the change and re-referencing the tty. 504 */ 505 clear_bit(TTY_LDISC, &tty->flags); 506 if (o_tty) 507 clear_bit(TTY_LDISC, &o_tty->flags); 508 509 spin_lock_irqsave(&tty_ldisc_lock, flags); 510 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) { 511 if (tty->ldisc.refcount) { 512 /* Free the new ldisc we grabbed. Must drop the lock 513 first. */ 514 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 515 tty_ldisc_put(o_ldisc.ops); 516 /* 517 * There are several reasons we may be busy, including 518 * random momentary I/O traffic. We must therefore 519 * retry. We could distinguish between blocking ops 520 * and retries if we made tty_ldisc_wait() smarter. 521 * That is up for discussion. 522 */ 523 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0) 524 return -ERESTARTSYS; 525 goto restart; 526 } 527 if (o_tty && o_tty->ldisc.refcount) { 528 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 529 tty_ldisc_put(o_tty->ldisc.ops); 530 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0) 531 return -ERESTARTSYS; 532 goto restart; 533 } 534 } 535 /* 536 * If the TTY_LDISC bit is set, then we are racing against 537 * another ldisc change 538 */ 539 if (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { 540 struct tty_ldisc *ld; 541 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 542 tty_ldisc_put(new_ldisc.ops); 543 ld = tty_ldisc_ref_wait(tty); 544 tty_ldisc_deref(ld); 545 goto restart; 546 } 547 /* 548 * This flag is used to avoid two parallel ldisc changes. Once 549 * open and close are fine grained locked this may work better 550 * as a mutex shared with the open/close/hup paths 551 */ 552 set_bit(TTY_LDISC_CHANGING, &tty->flags); 553 if (o_tty) 554 set_bit(TTY_LDISC_CHANGING, &o_tty->flags); 555 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 556 557 /* 558 * From this point on we know nobody has an ldisc 559 * usage reference, nor can they obtain one until 560 * we say so later on. 561 */ 562 563 work = cancel_delayed_work(&tty->buf.work); 564 /* 565 * Wait for ->hangup_work and ->buf.work handlers to terminate 566 * MUST NOT hold locks here. 567 */ 568 flush_scheduled_work(); 569 /* Shutdown the current discipline. */ 570 if (o_ldisc.ops->close) 571 (o_ldisc.ops->close)(tty); 572 573 /* Now set up the new line discipline. */ 574 tty_ldisc_assign(tty, &new_ldisc); 575 tty_set_termios_ldisc(tty, ldisc); 576 if (new_ldisc.ops->open) 577 retval = (new_ldisc.ops->open)(tty); 578 if (retval < 0) { 579 tty_ldisc_put(new_ldisc.ops); 580 tty_ldisc_restore(tty, &o_ldisc); 581 } 582 /* At this point we hold a reference to the new ldisc and a 583 a reference to the old ldisc. If we ended up flipping back 584 to the existing ldisc we have two references to it */ 585 586 if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc) 587 tty->ops->set_ldisc(tty); 588 589 tty_ldisc_put(o_ldisc.ops); 590 591 /* 592 * Allow ldisc referencing to occur as soon as the driver 593 * ldisc callback completes. 594 */ 595 596 tty_ldisc_enable(tty); 597 if (o_tty) 598 tty_ldisc_enable(o_tty); 599 600 /* Restart it in case no characters kick it off. Safe if 601 already running */ 602 if (work) 603 schedule_delayed_work(&tty->buf.work, 1); 604 return retval; 605} 606 607 608/** 609 * tty_ldisc_setup - open line discipline 610 * @tty: tty being shut down 611 * @o_tty: pair tty for pty/tty pairs 612 * 613 * Called during the initial open of a tty/pty pair in order to set up the 614 * line discplines and bind them to the tty. 615 */ 616 617int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) 618{ 619 struct tty_ldisc *ld = &tty->ldisc; 620 int retval; 621 622 if (ld->ops->open) { 623 retval = (ld->ops->open)(tty); 624 if (retval) 625 return retval; 626 } 627 if (o_tty && o_tty->ldisc.ops->open) { 628 retval = (o_tty->ldisc.ops->open)(o_tty); 629 if (retval) { 630 if (ld->ops->close) 631 (ld->ops->close)(tty); 632 return retval; 633 } 634 tty_ldisc_enable(o_tty); 635 } 636 tty_ldisc_enable(tty); 637 return 0; 638} 639 640/** 641 * tty_ldisc_release - release line discipline 642 * @tty: tty being shut down 643 * @o_tty: pair tty for pty/tty pairs 644 * 645 * Called during the final close of a tty/pty pair in order to shut down the 646 * line discpline layer. 647 */ 648 649void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty) 650{ 651 unsigned long flags; 652 struct tty_ldisc ld; 653 /* 654 * Prevent flush_to_ldisc() from rescheduling the work for later. Then 655 * kill any delayed work. As this is the final close it does not 656 * race with the set_ldisc code path. 657 */ 658 clear_bit(TTY_LDISC, &tty->flags); 659 cancel_delayed_work(&tty->buf.work); 660 661 /* 662 * Wait for ->hangup_work and ->buf.work handlers to terminate 663 */ 664 665 flush_scheduled_work(); 666 667 /* 668 * Wait for any short term users (we know they are just driver 669 * side waiters as the file is closing so user count on the file 670 * side is zero. 671 */ 672 spin_lock_irqsave(&tty_ldisc_lock, flags); 673 while (tty->ldisc.refcount) { 674 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 675 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0); 676 spin_lock_irqsave(&tty_ldisc_lock, flags); 677 } 678 spin_unlock_irqrestore(&tty_ldisc_lock, flags); 679 /* 680 * Shutdown the current line discipline, and reset it to N_TTY. 681 * 682 * FIXME: this MUST get fixed for the new reflocking 683 */ 684 if (tty->ldisc.ops->close) 685 (tty->ldisc.ops->close)(tty); 686 tty_ldisc_put(tty->ldisc.ops); 687 688 /* 689 * Switch the line discipline back 690 */ 691 WARN_ON(tty_ldisc_get(N_TTY, &ld)); 692 tty_ldisc_assign(tty, &ld); 693 tty_set_termios_ldisc(tty, N_TTY); 694 if (o_tty) { 695 /* FIXME: could o_tty be in setldisc here ? */ 696 clear_bit(TTY_LDISC, &o_tty->flags); 697 if (o_tty->ldisc.ops->close) 698 (o_tty->ldisc.ops->close)(o_tty); 699 tty_ldisc_put(o_tty->ldisc.ops); 700 WARN_ON(tty_ldisc_get(N_TTY, &ld)); 701 tty_ldisc_assign(o_tty, &ld); 702 tty_set_termios_ldisc(o_tty, N_TTY); 703 } 704} 705 706/** 707 * tty_ldisc_init - ldisc setup for new tty 708 * @tty: tty being allocated 709 * 710 * Set up the line discipline objects for a newly allocated tty. Note that 711 * the tty structure is not completely set up when this call is made. 712 */ 713 714void tty_ldisc_init(struct tty_struct *tty) 715{ 716 struct tty_ldisc ld; 717 if (tty_ldisc_get(N_TTY, &ld) < 0) 718 panic("n_tty: init_tty"); 719 tty_ldisc_assign(tty, &ld); 720} 721 722void tty_ldisc_begin(void) 723{ 724 /* Setup the default TTY line discipline. */ 725 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); 726}