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/fdtable.h>
14 #include <linux/console.h>
15 #include <linux/timer.h>
16 #include <linux/ctype.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>
32 #include <linux/uaccess.h>
33 #include <asm/system.h>
35 #include <linux/kbd_kern.h>
36 #include <linux/vt_kern.h>
37 #include <linux/selection.h>
39 #include <linux/kmod.h>
40 #include <linux/nsproxy.h>
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.
48 static DEFINE_SPINLOCK(tty_ldisc_lock
);
49 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait
);
50 /* Line disc dispatch table */
51 static struct tty_ldisc_ops
*tty_ldiscs
[NR_LDISCS
];
54 * tty_register_ldisc - install a line discipline
56 * @new_ldisc: pointer to the ldisc object
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.
63 * takes tty_ldisc_lock to guard against ldisc races
66 int tty_register_ldisc(int disc
, struct tty_ldisc_ops
*new_ldisc
)
71 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
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
);
82 EXPORT_SYMBOL(tty_register_ldisc
);
85 * tty_unregister_ldisc - unload a line discipline
87 * @new_ldisc: pointer to the ldisc object
89 * Remove a line discipline from the kernel providing it is not
93 * takes tty_ldisc_lock to guard against ldisc races
96 int tty_unregister_ldisc(int disc
)
101 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
104 spin_lock_irqsave(&tty_ldisc_lock
, flags
);
105 if (tty_ldiscs
[disc
]->refcount
)
108 tty_ldiscs
[disc
] = NULL
;
109 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
113 EXPORT_SYMBOL(tty_unregister_ldisc
);
117 * tty_ldisc_try_get - try and reference an ldisc
118 * @disc: ldisc number
119 * @ld: tty ldisc structure to complete
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
126 static int tty_ldisc_try_get(int disc
, struct tty_ldisc
*ld
)
129 struct tty_ldisc_ops
*ldops
;
132 spin_lock_irqsave(&tty_ldisc_lock
, flags
);
134 ldops
= tty_ldiscs
[disc
];
135 /* Check the entry is defined */
137 /* If the module is being unloaded we can't use it */
138 if (!try_module_get(ldops
->owner
))
147 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
152 * tty_ldisc_get - take a reference to an ldisc
153 * @disc: ldisc number
154 * @ld: tty line discipline structure to use
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
162 * takes tty_ldisc_lock to guard against ldisc races
165 static int tty_ldisc_get(int disc
, struct tty_ldisc
*ld
)
169 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
171 err
= tty_ldisc_try_get(disc
, ld
);
173 request_module("tty-ldisc-%d", disc
);
174 err
= tty_ldisc_try_get(disc
, ld
);
180 * tty_ldisc_put - drop ldisc reference
181 * @disc: ldisc number
183 * Drop a reference to a line discipline. Manage refcounts and
184 * module usage counts
187 * takes tty_ldisc_lock to guard against ldisc races
190 static void tty_ldisc_put(struct tty_ldisc_ops
*ld
)
195 BUG_ON(disc
< N_TTY
|| disc
>= NR_LDISCS
);
197 spin_lock_irqsave(&tty_ldisc_lock
, flags
);
198 ld
= tty_ldiscs
[disc
];
199 BUG_ON(ld
->refcount
== 0);
201 module_put(ld
->owner
);
202 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
205 static void * tty_ldiscs_seq_start(struct seq_file
*m
, loff_t
*pos
)
207 return (*pos
< NR_LDISCS
) ? pos
: NULL
;
210 static void * tty_ldiscs_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
213 return (*pos
< NR_LDISCS
) ? pos
: NULL
;
216 static void tty_ldiscs_seq_stop(struct seq_file
*m
, void *v
)
220 static int tty_ldiscs_seq_show(struct seq_file
*m
, void *v
)
222 int i
= *(loff_t
*)v
;
225 if (tty_ldisc_get(i
, &ld
) < 0)
227 seq_printf(m
, "%-10s %2d\n", ld
.ops
->name
? ld
.ops
->name
: "???", i
);
228 tty_ldisc_put(ld
.ops
);
232 static 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
,
239 static int proc_tty_ldiscs_open(struct inode
*inode
, struct file
*file
)
241 return seq_open(file
, &tty_ldiscs_seq_ops
);
244 const struct file_operations tty_ldiscs_proc_fops
= {
245 .owner
= THIS_MODULE
,
246 .open
= proc_tty_ldiscs_open
,
249 .release
= seq_release
,
253 * tty_ldisc_assign - set ldisc on a tty
254 * @tty: tty to assign
255 * @ld: line discipline
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.
262 * Caller must hold references
265 static void tty_ldisc_assign(struct tty_struct
*tty
, struct tty_ldisc
*ld
)
272 * tty_ldisc_try - internal helper
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
280 * Locking: takes tty_ldisc_lock
283 static int tty_ldisc_try(struct tty_struct
*tty
)
286 struct tty_ldisc
*ld
;
289 spin_lock_irqsave(&tty_ldisc_lock
, flags
);
291 if (test_bit(TTY_LDISC
, &tty
->flags
)) {
295 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
300 * tty_ldisc_ref_wait - wait for the tty ldisc
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.
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)
312 * Locking: call functions take tty_ldisc_lock
315 struct tty_ldisc
*tty_ldisc_ref_wait(struct tty_struct
*tty
)
317 /* wait_event is a macro */
318 wait_event(tty_ldisc_wait
, tty_ldisc_try(tty
));
319 if (tty
->ldisc
.refcount
== 0)
320 printk(KERN_ERR
"tty_ldisc_ref_wait\n");
324 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait
);
327 * tty_ldisc_ref - get the tty ldisc
330 * Dereference the line discipline for the terminal and take a
331 * reference to it. If the line discipline is in flux then
332 * return NULL. Can be called from IRQ and timer functions.
334 * Locking: called functions take tty_ldisc_lock
337 struct tty_ldisc
*tty_ldisc_ref(struct tty_struct
*tty
)
339 if (tty_ldisc_try(tty
))
344 EXPORT_SYMBOL_GPL(tty_ldisc_ref
);
347 * tty_ldisc_deref - free a tty ldisc reference
348 * @ld: reference to free up
350 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
351 * be called in IRQ context.
353 * Locking: takes tty_ldisc_lock
356 void tty_ldisc_deref(struct tty_ldisc
*ld
)
362 spin_lock_irqsave(&tty_ldisc_lock
, flags
);
363 if (ld
->refcount
== 0)
364 printk(KERN_ERR
"tty_ldisc_deref: no references.\n");
367 if (ld
->refcount
== 0)
368 wake_up(&tty_ldisc_wait
);
369 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
372 EXPORT_SYMBOL_GPL(tty_ldisc_deref
);
375 * tty_ldisc_enable - allow ldisc use
376 * @tty: terminal to activate ldisc on
378 * Set the TTY_LDISC flag when the line discipline can be called
379 * again. Do necessary wakeups for existing sleepers.
381 * Note: nobody should set this bit except via this function. Clearing
382 * directly is allowed.
385 void tty_ldisc_enable(struct tty_struct
*tty
)
387 set_bit(TTY_LDISC
, &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.
501 spin_lock_irqsave(&tty_ldisc_lock
, flags
);
502 if (tty
->ldisc
.refcount
|| (o_tty
&& o_tty
->ldisc
.refcount
)) {
503 if (tty
->ldisc
.refcount
) {
504 /* Free the new ldisc we grabbed. Must drop the lock
506 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
507 tty_ldisc_put(o_ldisc
.ops
);
509 * There are several reasons we may be busy, including
510 * random momentary I/O traffic. We must therefore
511 * retry. We could distinguish between blocking ops
512 * and retries if we made tty_ldisc_wait() smarter.
513 * That is up for discussion.
515 if (wait_event_interruptible(tty_ldisc_wait
, tty
->ldisc
.refcount
== 0) < 0)
519 if (o_tty
&& o_tty
->ldisc
.refcount
) {
520 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
521 tty_ldisc_put(o_tty
->ldisc
.ops
);
522 if (wait_event_interruptible(tty_ldisc_wait
, o_tty
->ldisc
.refcount
== 0) < 0)
528 * If the TTY_LDISC bit is set, then we are racing against
529 * another ldisc change
531 if (!test_bit(TTY_LDISC
, &tty
->flags
)) {
532 struct tty_ldisc
*ld
;
533 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
534 tty_ldisc_put(new_ldisc
.ops
);
535 ld
= tty_ldisc_ref_wait(tty
);
540 clear_bit(TTY_LDISC
, &tty
->flags
);
542 clear_bit(TTY_LDISC
, &o_tty
->flags
);
543 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
546 * From this point on we know nobody has an ldisc
547 * usage reference, nor can they obtain one until
548 * we say so later on.
551 work
= cancel_delayed_work(&tty
->buf
.work
);
553 * Wait for ->hangup_work and ->buf.work handlers to terminate
554 * MUST NOT hold locks here.
556 flush_scheduled_work();
557 /* Shutdown the current discipline. */
558 if (o_ldisc
.ops
->close
)
559 (o_ldisc
.ops
->close
)(tty
);
561 /* Now set up the new line discipline. */
562 tty_ldisc_assign(tty
, &new_ldisc
);
563 tty_set_termios_ldisc(tty
, ldisc
);
564 if (new_ldisc
.ops
->open
)
565 retval
= (new_ldisc
.ops
->open
)(tty
);
567 tty_ldisc_put(new_ldisc
.ops
);
568 tty_ldisc_restore(tty
, &o_ldisc
);
570 /* At this point we hold a reference to the new ldisc and a
571 a reference to the old ldisc. If we ended up flipping back
572 to the existing ldisc we have two references to it */
574 if (tty
->ldisc
.ops
->num
!= o_ldisc
.ops
->num
&& tty
->ops
->set_ldisc
)
575 tty
->ops
->set_ldisc(tty
);
577 tty_ldisc_put(o_ldisc
.ops
);
580 * Allow ldisc referencing to occur as soon as the driver
581 * ldisc callback completes.
584 tty_ldisc_enable(tty
);
586 tty_ldisc_enable(o_tty
);
588 /* Restart it in case no characters kick it off. Safe if
591 schedule_delayed_work(&tty
->buf
.work
, 1);
597 * tty_ldisc_setup - open line discipline
598 * @tty: tty being shut down
599 * @o_tty: pair tty for pty/tty pairs
601 * Called during the initial open of a tty/pty pair in order to set up the
602 * line discplines and bind them to the tty.
605 int tty_ldisc_setup(struct tty_struct
*tty
, struct tty_struct
*o_tty
)
607 struct tty_ldisc
*ld
= &tty
->ldisc
;
611 retval
= (ld
->ops
->open
)(tty
);
615 if (o_tty
&& o_tty
->ldisc
.ops
->open
) {
616 retval
= (o_tty
->ldisc
.ops
->open
)(o_tty
);
619 (ld
->ops
->close
)(tty
);
622 tty_ldisc_enable(o_tty
);
624 tty_ldisc_enable(tty
);
629 * tty_ldisc_release - release line discipline
630 * @tty: tty being shut down
631 * @o_tty: pair tty for pty/tty pairs
633 * Called during the final close of a tty/pty pair in order to shut down the
634 * line discpline layer.
637 void tty_ldisc_release(struct tty_struct
*tty
, struct tty_struct
*o_tty
)
642 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
643 * kill any delayed work. As this is the final close it does not
644 * race with the set_ldisc code path.
646 clear_bit(TTY_LDISC
, &tty
->flags
);
647 cancel_delayed_work(&tty
->buf
.work
);
650 * Wait for ->hangup_work and ->buf.work handlers to terminate
653 flush_scheduled_work();
656 * Wait for any short term users (we know they are just driver
657 * side waiters as the file is closing so user count on the file
660 spin_lock_irqsave(&tty_ldisc_lock
, flags
);
661 while (tty
->ldisc
.refcount
) {
662 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
663 wait_event(tty_ldisc_wait
, tty
->ldisc
.refcount
== 0);
664 spin_lock_irqsave(&tty_ldisc_lock
, flags
);
666 spin_unlock_irqrestore(&tty_ldisc_lock
, flags
);
668 * Shutdown the current line discipline, and reset it to N_TTY.
670 * FIXME: this MUST get fixed for the new reflocking
672 if (tty
->ldisc
.ops
->close
)
673 (tty
->ldisc
.ops
->close
)(tty
);
674 tty_ldisc_put(tty
->ldisc
.ops
);
677 * Switch the line discipline back
679 WARN_ON(tty_ldisc_get(N_TTY
, &ld
));
680 tty_ldisc_assign(tty
, &ld
);
681 tty_set_termios_ldisc(tty
, N_TTY
);
683 /* FIXME: could o_tty be in setldisc here ? */
684 clear_bit(TTY_LDISC
, &o_tty
->flags
);
685 if (o_tty
->ldisc
.ops
->close
)
686 (o_tty
->ldisc
.ops
->close
)(o_tty
);
687 tty_ldisc_put(o_tty
->ldisc
.ops
);
688 WARN_ON(tty_ldisc_get(N_TTY
, &ld
));
689 tty_ldisc_assign(o_tty
, &ld
);
690 tty_set_termios_ldisc(o_tty
, N_TTY
);
695 * tty_ldisc_init - ldisc setup for new tty
696 * @tty: tty being allocated
698 * Set up the line discipline objects for a newly allocated tty. Note that
699 * the tty structure is not completely set up when this call is made.
702 void tty_ldisc_init(struct tty_struct
*tty
)
705 if (tty_ldisc_get(N_TTY
, &ld
) < 0)
706 panic("n_tty: init_tty");
707 tty_ldisc_assign(tty
, &ld
);
710 void tty_ldisc_begin(void)
712 /* Setup the default TTY line discipline. */
713 (void) tty_register_ldisc(N_TTY
, &tty_ldisc_N_TTY
);