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>
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/console.h>
14 #include <linux/timer.h>
15 #include <linux/ctype.h>
18 #include <linux/string.h>
19 #include <linux/slab.h>
20 #include <linux/poll.h>
21 #include <linux/proc_fs.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/smp_lock.h>
25 #include <linux/device.h>
26 #include <linux/wait.h>
27 #include <linux/bitops.h>
28 #include <linux/delay.h>
29 #include <linux/seq_file.h>
31 #include <linux/uaccess.h>
32 #include <asm/system.h>
34 #include <linux/kbd_kern.h>
35 #include <linux/vt_kern.h>
36 #include <linux/selection.h>
38 #include <linux/kmod.h>
39 #include <linux/nsproxy.h>
42 * This guards the refcounted line discipline lists. The lock
43 * must be taken with irqs off because there are hangup path
44 * callers who will do ldisc lookups and cannot sleep.
47 static DEFINE_SPINLOCK(tty_ldisc_lock);
48 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
49 /* Line disc dispatch table */
50 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
53 * tty_register_ldisc - install a line discipline
55 * @new_ldisc: pointer to the ldisc object
57 * Installs a new line discipline into the kernel. The discipline
58 * is set up as unreferenced and then made available to the kernel
59 * from this point onwards.
62 * takes tty_ldisc_lock to guard against ldisc races
65 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
70 if (disc < N_TTY || disc >= NR_LDISCS)
73 spin_lock_irqsave(&tty_ldisc_lock, flags);
74 tty_ldiscs[disc] = new_ldisc;
75 new_ldisc->num = disc;
76 new_ldisc->refcount = 0;
77 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
81 EXPORT_SYMBOL(tty_register_ldisc);
84 * tty_unregister_ldisc - unload a line discipline
86 * @new_ldisc: pointer to the ldisc object
88 * Remove a line discipline from the kernel providing it is not
92 * takes tty_ldisc_lock to guard against ldisc races
95 int tty_unregister_ldisc(int disc)
100 if (disc < N_TTY || disc >= NR_LDISCS)
103 spin_lock_irqsave(&tty_ldisc_lock, flags);
104 if (tty_ldiscs[disc]->refcount)
107 tty_ldiscs[disc] = NULL;
108 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
112 EXPORT_SYMBOL(tty_unregister_ldisc);
116 * tty_ldisc_try_get - try and reference an ldisc
117 * @disc: ldisc number
118 * @ld: tty ldisc structure to complete
120 * Attempt to open and lock a line discipline into place. Return
121 * the line discipline refcounted and assigned in ld. On an error
122 * report the error code back
125 static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld)
128 struct tty_ldisc_ops *ldops;
131 spin_lock_irqsave(&tty_ldisc_lock, flags);
133 ldops = tty_ldiscs[disc];
134 /* Check the entry is defined */
136 /* If the module is being unloaded we can't use it */
137 if (!try_module_get(ldops->owner))
146 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
151 * tty_ldisc_get - take a reference to an ldisc
152 * @disc: ldisc number
153 * @ld: tty line discipline structure to use
155 * Takes a reference to a line discipline. Deals with refcounts and
156 * module locking counts. Returns NULL if the discipline is not available.
157 * Returns a pointer to the discipline and bumps the ref count if it is
161 * takes tty_ldisc_lock to guard against ldisc races
164 static int tty_ldisc_get(int disc, struct tty_ldisc *ld)
168 if (disc < N_TTY || disc >= NR_LDISCS)
170 err = tty_ldisc_try_get(disc, ld);
172 request_module("tty-ldisc-%d", disc);
173 err = tty_ldisc_try_get(disc, ld);
179 * tty_ldisc_put - drop ldisc reference
180 * @disc: ldisc number
182 * Drop a reference to a line discipline. Manage refcounts and
183 * module usage counts
186 * takes tty_ldisc_lock to guard against ldisc races
189 static void tty_ldisc_put(struct tty_ldisc_ops *ld)
194 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
196 spin_lock_irqsave(&tty_ldisc_lock, flags);
197 ld = tty_ldiscs[disc];
198 BUG_ON(ld->refcount == 0);
200 module_put(ld->owner);
201 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
204 static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
206 return (*pos < NR_LDISCS) ? pos : NULL;
209 static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
212 return (*pos < NR_LDISCS) ? pos : NULL;
215 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
219 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
221 int i = *(loff_t *)v;
224 if (tty_ldisc_get(i, &ld) < 0)
226 seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i);
227 tty_ldisc_put(ld.ops);
231 static const struct seq_operations tty_ldiscs_seq_ops = {
232 .start = tty_ldiscs_seq_start,
233 .next = tty_ldiscs_seq_next,
234 .stop = tty_ldiscs_seq_stop,
235 .show = tty_ldiscs_seq_show,
238 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
240 return seq_open(file, &tty_ldiscs_seq_ops);
243 const struct file_operations tty_ldiscs_proc_fops = {
244 .owner = THIS_MODULE,
245 .open = proc_tty_ldiscs_open,
248 .release = seq_release,
252 * tty_ldisc_assign - set ldisc on a tty
253 * @tty: tty to assign
254 * @ld: line discipline
256 * Install an instance of a line discipline into a tty structure. The
257 * ldisc must have a reference count above zero to ensure it remains/
258 * The tty instance refcount starts at zero.
261 * Caller must hold references
264 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
271 * tty_ldisc_try - internal helper
274 * Make a single attempt to grab and bump the refcount on
275 * the tty ldisc. Return 0 on failure or 1 on success. This is
276 * used to implement both the waiting and non waiting versions
279 * Locking: takes tty_ldisc_lock
282 static int tty_ldisc_try(struct tty_struct *tty)
285 struct tty_ldisc *ld;
288 spin_lock_irqsave(&tty_ldisc_lock, flags);
290 if (test_bit(TTY_LDISC, &tty->flags)) {
294 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
299 * tty_ldisc_ref_wait - wait for the tty ldisc
302 * Dereference the line discipline for the terminal and take a
303 * reference to it. If the line discipline is in flux then
304 * wait patiently until it changes.
306 * Note: Must not be called from an IRQ/timer context. The caller
307 * must also be careful not to hold other locks that will deadlock
308 * against a discipline change, such as an existing ldisc reference
309 * (which we check for)
311 * Locking: call functions take tty_ldisc_lock
314 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
316 /* wait_event is a macro */
317 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
318 WARN_ON(tty->ldisc.refcount == 0);
322 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
325 * tty_ldisc_ref - get the tty ldisc
328 * Dereference the line discipline for the terminal and take a
329 * reference to it. If the line discipline is in flux then
330 * return NULL. Can be called from IRQ and timer functions.
332 * Locking: called functions take tty_ldisc_lock
335 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
337 if (tty_ldisc_try(tty))
342 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
345 * tty_ldisc_deref - free a tty ldisc reference
346 * @ld: reference to free up
348 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
349 * be called in IRQ context.
351 * Locking: takes tty_ldisc_lock
354 void tty_ldisc_deref(struct tty_ldisc *ld)
360 spin_lock_irqsave(&tty_ldisc_lock, flags);
361 if (ld->refcount == 0)
362 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
365 if (ld->refcount == 0)
366 wake_up(&tty_ldisc_wait);
367 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
370 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
373 * tty_ldisc_enable - allow ldisc use
374 * @tty: terminal to activate ldisc on
376 * Set the TTY_LDISC flag when the line discipline can be called
377 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
378 * changing flag to indicate any ldisc change is now over.
380 * Note: nobody should set the TTY_LDISC bit except via this function.
381 * Clearing directly is allowed.
384 void tty_ldisc_enable(struct tty_struct *tty)
386 set_bit(TTY_LDISC, &tty->flags);
387 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
388 wake_up(&tty_ldisc_wait);
392 * tty_set_termios_ldisc - set ldisc field
393 * @tty: tty structure
394 * @num: line discipline number
396 * This is probably overkill for real world processors but
397 * they are not on hot paths so a little discipline won't do
400 * Locking: takes termios_mutex
403 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
405 mutex_lock(&tty->termios_mutex);
406 tty->termios->c_line = num;
407 mutex_unlock(&tty->termios_mutex);
412 * tty_ldisc_restore - helper for tty ldisc change
413 * @tty: tty to recover
414 * @old: previous ldisc
416 * Restore the previous line discipline or N_TTY when a line discipline
417 * change fails due to an open error
420 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
423 struct tty_ldisc new_ldisc;
425 /* There is an outstanding reference here so this is safe */
426 tty_ldisc_get(old->ops->num, old);
427 tty_ldisc_assign(tty, old);
428 tty_set_termios_ldisc(tty, old->ops->num);
429 if (old->ops->open && (old->ops->open(tty) < 0)) {
430 tty_ldisc_put(old->ops);
431 /* This driver is always present */
432 if (tty_ldisc_get(N_TTY, &new_ldisc) < 0)
434 tty_ldisc_assign(tty, &new_ldisc);
435 tty_set_termios_ldisc(tty, N_TTY);
436 if (new_ldisc.ops->open) {
437 int r = new_ldisc.ops->open(tty);
439 panic("Couldn't open N_TTY ldisc for "
441 tty_name(tty, buf), r);
447 * tty_set_ldisc - set line discipline
448 * @tty: the terminal to set
449 * @ldisc: the line discipline
451 * Set the discipline of a tty line. Must be called from a process
454 * Locking: takes tty_ldisc_lock.
455 * called functions take termios_mutex
458 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
461 struct tty_ldisc o_ldisc, new_ldisc;
464 struct tty_struct *o_tty;
467 /* This is a bit ugly for now but means we can break the 'ldisc
468 is part of the tty struct' assumption later */
469 retval = tty_ldisc_get(ldisc, &new_ldisc);
474 * Problem: What do we do if this blocks ?
477 tty_wait_until_sent(tty, 0);
479 if (tty->ldisc.ops->num == ldisc) {
480 tty_ldisc_put(new_ldisc.ops);
485 * No more input please, we are switching. The new ldisc
486 * will update this value in the ldisc open function
489 tty->receive_room = 0;
491 o_ldisc = tty->ldisc;
495 * Make sure we don't change while someone holds a
496 * reference to the line discipline. The TTY_LDISC bit
497 * prevents anyone taking a reference once it is clear.
498 * We need the lock to avoid racing reference takers.
500 * We must clear the TTY_LDISC bit here to avoid a livelock
501 * with a userspace app continually trying to use the tty in
502 * parallel to the change and re-referencing the tty.
504 clear_bit(TTY_LDISC, &tty->flags);
506 clear_bit(TTY_LDISC, &o_tty->flags);
508 spin_lock_irqsave(&tty_ldisc_lock, flags);
509 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
510 if (tty->ldisc.refcount) {
511 /* Free the new ldisc we grabbed. Must drop the lock
513 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
514 tty_ldisc_put(o_ldisc.ops);
516 * There are several reasons we may be busy, including
517 * random momentary I/O traffic. We must therefore
518 * retry. We could distinguish between blocking ops
519 * and retries if we made tty_ldisc_wait() smarter.
520 * That is up for discussion.
522 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
526 if (o_tty && o_tty->ldisc.refcount) {
527 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
528 tty_ldisc_put(o_tty->ldisc.ops);
529 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
535 * If the TTY_LDISC bit is set, then we are racing against
536 * another ldisc change
538 if (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
539 struct tty_ldisc *ld;
540 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
541 tty_ldisc_put(new_ldisc.ops);
542 ld = tty_ldisc_ref_wait(tty);
547 * This flag is used to avoid two parallel ldisc changes. Once
548 * open and close are fine grained locked this may work better
549 * as a mutex shared with the open/close/hup paths
551 set_bit(TTY_LDISC_CHANGING, &tty->flags);
553 set_bit(TTY_LDISC_CHANGING, &o_tty->flags);
554 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
557 * From this point on we know nobody has an ldisc
558 * usage reference, nor can they obtain one until
559 * we say so later on.
562 work = cancel_delayed_work(&tty->buf.work);
564 * Wait for ->hangup_work and ->buf.work handlers to terminate
565 * MUST NOT hold locks here.
567 flush_scheduled_work();
568 /* Shutdown the current discipline. */
569 if (o_ldisc.ops->close)
570 (o_ldisc.ops->close)(tty);
572 /* Now set up the new line discipline. */
573 tty_ldisc_assign(tty, &new_ldisc);
574 tty_set_termios_ldisc(tty, ldisc);
575 if (new_ldisc.ops->open)
576 retval = (new_ldisc.ops->open)(tty);
578 tty_ldisc_put(new_ldisc.ops);
579 tty_ldisc_restore(tty, &o_ldisc);
581 /* At this point we hold a reference to the new ldisc and a
582 a reference to the old ldisc. If we ended up flipping back
583 to the existing ldisc we have two references to it */
585 if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc)
586 tty->ops->set_ldisc(tty);
588 tty_ldisc_put(o_ldisc.ops);
591 * Allow ldisc referencing to occur as soon as the driver
592 * ldisc callback completes.
595 tty_ldisc_enable(tty);
597 tty_ldisc_enable(o_tty);
599 /* Restart it in case no characters kick it off. Safe if
602 schedule_delayed_work(&tty->buf.work, 1);
608 * tty_ldisc_setup - open line discipline
609 * @tty: tty being shut down
610 * @o_tty: pair tty for pty/tty pairs
612 * Called during the initial open of a tty/pty pair in order to set up the
613 * line discplines and bind them to the tty.
616 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
618 struct tty_ldisc *ld = &tty->ldisc;
622 retval = (ld->ops->open)(tty);
626 if (o_tty && o_tty->ldisc.ops->open) {
627 retval = (o_tty->ldisc.ops->open)(o_tty);
630 (ld->ops->close)(tty);
633 tty_ldisc_enable(o_tty);
635 tty_ldisc_enable(tty);
640 * tty_ldisc_release - release line discipline
641 * @tty: tty being shut down
642 * @o_tty: pair tty for pty/tty pairs
644 * Called during the final close of a tty/pty pair in order to shut down the
645 * line discpline layer.
648 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
653 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
654 * kill any delayed work. As this is the final close it does not
655 * race with the set_ldisc code path.
657 clear_bit(TTY_LDISC, &tty->flags);
658 cancel_delayed_work(&tty->buf.work);
661 * Wait for ->hangup_work and ->buf.work handlers to terminate
664 flush_scheduled_work();
667 * Wait for any short term users (we know they are just driver
668 * side waiters as the file is closing so user count on the file
671 spin_lock_irqsave(&tty_ldisc_lock, flags);
672 while (tty->ldisc.refcount) {
673 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
674 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
675 spin_lock_irqsave(&tty_ldisc_lock, flags);
677 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
679 * Shutdown the current line discipline, and reset it to N_TTY.
681 * FIXME: this MUST get fixed for the new reflocking
683 if (tty->ldisc.ops->close)
684 (tty->ldisc.ops->close)(tty);
685 tty_ldisc_put(tty->ldisc.ops);
688 * Switch the line discipline back
690 WARN_ON(tty_ldisc_get(N_TTY, &ld));
691 tty_ldisc_assign(tty, &ld);
692 tty_set_termios_ldisc(tty, N_TTY);
694 /* FIXME: could o_tty be in setldisc here ? */
695 clear_bit(TTY_LDISC, &o_tty->flags);
696 if (o_tty->ldisc.ops->close)
697 (o_tty->ldisc.ops->close)(o_tty);
698 tty_ldisc_put(o_tty->ldisc.ops);
699 WARN_ON(tty_ldisc_get(N_TTY, &ld));
700 tty_ldisc_assign(o_tty, &ld);
701 tty_set_termios_ldisc(o_tty, N_TTY);
706 * tty_ldisc_init - ldisc setup for new tty
707 * @tty: tty being allocated
709 * Set up the line discipline objects for a newly allocated tty. Note that
710 * the tty structure is not completely set up when this call is made.
713 void tty_ldisc_init(struct tty_struct *tty)
716 if (tty_ldisc_get(N_TTY, &ld) < 0)
717 panic("n_tty: init_tty");
718 tty_ldisc_assign(tty, &ld);
721 void tty_ldisc_begin(void)
723 /* Setup the default TTY line discipline. */
724 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);