1 #include <linux/types.h>
2 #include <linux/errno.h>
3 #include <linux/kmod.h>
4 #include <linux/sched.h>
5 #include <linux/interrupt.h>
6 #include <linux/tty.h>
7 #include <linux/tty_driver.h>
8 #include <linux/file.h>
9 #include <linux/mm.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/poll.h>
13 #include <linux/proc_fs.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/wait.h>
17 #include <linux/bitops.h>
18 #include <linux/seq_file.h>
19 #include <linux/uaccess.h>
20 #include <linux/ratelimit.h>
21
22 #undef LDISC_DEBUG_HANGUP
23
24 #ifdef LDISC_DEBUG_HANGUP
25 #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args)
26 #else
27 #define tty_ldisc_debug(tty, f, args...)
28 #endif
29
30 /* lockdep nested classes for tty->ldisc_sem */
31 enum {
32 LDISC_SEM_NORMAL,
33 LDISC_SEM_OTHER,
34 };
35
36
37 /*
38 * This guards the refcounted line discipline lists. The lock
39 * must be taken with irqs off because there are hangup path
40 * callers who will do ldisc lookups and cannot sleep.
41 */
42
43 static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
44 /* Line disc dispatch table */
45 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
46
47 /**
48 * tty_register_ldisc - install a line discipline
49 * @disc: ldisc number
50 * @new_ldisc: pointer to the ldisc object
51 *
52 * Installs a new line discipline into the kernel. The discipline
53 * is set up as unreferenced and then made available to the kernel
54 * from this point onwards.
55 *
56 * Locking:
57 * takes tty_ldiscs_lock to guard against ldisc races
58 */
59
tty_register_ldisc(int disc,struct tty_ldisc_ops * new_ldisc)60 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
61 {
62 unsigned long flags;
63 int ret = 0;
64
65 if (disc < N_TTY || disc >= NR_LDISCS)
66 return -EINVAL;
67
68 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
69 tty_ldiscs[disc] = new_ldisc;
70 new_ldisc->num = disc;
71 new_ldisc->refcount = 0;
72 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
73
74 return ret;
75 }
76 EXPORT_SYMBOL(tty_register_ldisc);
77
78 /**
79 * tty_unregister_ldisc - unload a line discipline
80 * @disc: ldisc number
81 * @new_ldisc: pointer to the ldisc object
82 *
83 * Remove a line discipline from the kernel providing it is not
84 * currently in use.
85 *
86 * Locking:
87 * takes tty_ldiscs_lock to guard against ldisc races
88 */
89
tty_unregister_ldisc(int disc)90 int tty_unregister_ldisc(int disc)
91 {
92 unsigned long flags;
93 int ret = 0;
94
95 if (disc < N_TTY || disc >= NR_LDISCS)
96 return -EINVAL;
97
98 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
99 if (tty_ldiscs[disc]->refcount)
100 ret = -EBUSY;
101 else
102 tty_ldiscs[disc] = NULL;
103 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
104
105 return ret;
106 }
107 EXPORT_SYMBOL(tty_unregister_ldisc);
108
get_ldops(int disc)109 static struct tty_ldisc_ops *get_ldops(int disc)
110 {
111 unsigned long flags;
112 struct tty_ldisc_ops *ldops, *ret;
113
114 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
115 ret = ERR_PTR(-EINVAL);
116 ldops = tty_ldiscs[disc];
117 if (ldops) {
118 ret = ERR_PTR(-EAGAIN);
119 if (try_module_get(ldops->owner)) {
120 ldops->refcount++;
121 ret = ldops;
122 }
123 }
124 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
125 return ret;
126 }
127
put_ldops(struct tty_ldisc_ops * ldops)128 static void put_ldops(struct tty_ldisc_ops *ldops)
129 {
130 unsigned long flags;
131
132 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
133 ldops->refcount--;
134 module_put(ldops->owner);
135 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
136 }
137
138 /**
139 * tty_ldisc_get - take a reference to an ldisc
140 * @disc: ldisc number
141 *
142 * Takes a reference to a line discipline. Deals with refcounts and
143 * module locking counts. Returns NULL if the discipline is not available.
144 * Returns a pointer to the discipline and bumps the ref count if it is
145 * available
146 *
147 * Locking:
148 * takes tty_ldiscs_lock to guard against ldisc races
149 */
150
151 #if defined(CONFIG_LDISC_AUTOLOAD)
152 #define INITIAL_AUTOLOAD_STATE 1
153 #else
154 #define INITIAL_AUTOLOAD_STATE 0
155 #endif
156 static int tty_ldisc_autoload = INITIAL_AUTOLOAD_STATE;
157
tty_ldisc_get(struct tty_struct * tty,int disc)158 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
159 {
160 struct tty_ldisc *ld;
161 struct tty_ldisc_ops *ldops;
162
163 if (disc < N_TTY || disc >= NR_LDISCS)
164 return ERR_PTR(-EINVAL);
165
166 /*
167 * Get the ldisc ops - we may need to request them to be loaded
168 * dynamically and try again.
169 */
170 ldops = get_ldops(disc);
171 if (IS_ERR(ldops)) {
172 if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
173 return ERR_PTR(-EPERM);
174 request_module("tty-ldisc-%d", disc);
175 ldops = get_ldops(disc);
176 if (IS_ERR(ldops))
177 return ERR_CAST(ldops);
178 }
179
180 /*
181 * There is no way to handle allocation failure of only 16 bytes.
182 * Let's simplify error handling and save more memory.
183 */
184 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
185 ld->ops = ldops;
186 ld->tty = tty;
187
188 return ld;
189 }
190
191 /**
192 * tty_ldisc_put - release the ldisc
193 *
194 * Complement of tty_ldisc_get().
195 */
tty_ldisc_put(struct tty_ldisc * ld)196 static inline void tty_ldisc_put(struct tty_ldisc *ld)
197 {
198 if (WARN_ON_ONCE(!ld))
199 return;
200
201 put_ldops(ld->ops);
202 kfree(ld);
203 }
204
tty_ldiscs_seq_start(struct seq_file * m,loff_t * pos)205 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
206 {
207 return (*pos < NR_LDISCS) ? pos : NULL;
208 }
209
tty_ldiscs_seq_next(struct seq_file * m,void * v,loff_t * pos)210 static 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
tty_ldiscs_seq_stop(struct seq_file * m,void * v)216 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
217 {
218 }
219
tty_ldiscs_seq_show(struct seq_file * m,void * v)220 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
221 {
222 int i = *(loff_t *)v;
223 struct tty_ldisc_ops *ldops;
224
225 ldops = get_ldops(i);
226 if (IS_ERR(ldops))
227 return 0;
228 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
229 put_ldops(ldops);
230 return 0;
231 }
232
233 static const struct seq_operations tty_ldiscs_seq_ops = {
234 .start = tty_ldiscs_seq_start,
235 .next = tty_ldiscs_seq_next,
236 .stop = tty_ldiscs_seq_stop,
237 .show = tty_ldiscs_seq_show,
238 };
239
proc_tty_ldiscs_open(struct inode * inode,struct file * file)240 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
241 {
242 return seq_open(file, &tty_ldiscs_seq_ops);
243 }
244
245 const struct file_operations tty_ldiscs_proc_fops = {
246 .owner = THIS_MODULE,
247 .open = proc_tty_ldiscs_open,
248 .read = seq_read,
249 .llseek = seq_lseek,
250 .release = seq_release,
251 };
252
253 /**
254 * tty_ldisc_ref_wait - wait for the tty ldisc
255 * @tty: tty device
256 *
257 * Dereference the line discipline for the terminal and take a
258 * reference to it. If the line discipline is in flux then
259 * wait patiently until it changes.
260 *
261 * Note: Must not be called from an IRQ/timer context. The caller
262 * must also be careful not to hold other locks that will deadlock
263 * against a discipline change, such as an existing ldisc reference
264 * (which we check for)
265 *
266 * Note: only callable from a file_operations routine (which
267 * guarantees tty->ldisc != NULL when the lock is acquired).
268 */
269
tty_ldisc_ref_wait(struct tty_struct * tty)270 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
271 {
272 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
273 WARN_ON(!tty->ldisc);
274 return tty->ldisc;
275 }
276 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
277
278 /**
279 * tty_ldisc_ref - get the tty ldisc
280 * @tty: tty device
281 *
282 * Dereference the line discipline for the terminal and take a
283 * reference to it. If the line discipline is in flux then
284 * return NULL. Can be called from IRQ and timer functions.
285 */
286
tty_ldisc_ref(struct tty_struct * tty)287 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
288 {
289 struct tty_ldisc *ld = NULL;
290
291 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
292 ld = tty->ldisc;
293 if (!ld)
294 ldsem_up_read(&tty->ldisc_sem);
295 }
296 return ld;
297 }
298 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
299
300 /**
301 * tty_ldisc_deref - free a tty ldisc reference
302 * @ld: reference to free up
303 *
304 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
305 * be called in IRQ context.
306 */
307
tty_ldisc_deref(struct tty_ldisc * ld)308 void tty_ldisc_deref(struct tty_ldisc *ld)
309 {
310 ldsem_up_read(&ld->tty->ldisc_sem);
311 }
312 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
313
314
315 static inline int __lockfunc
__tty_ldisc_lock(struct tty_struct * tty,unsigned long timeout)316 __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
317 {
318 return ldsem_down_write(&tty->ldisc_sem, timeout);
319 }
320
321 static inline int __lockfunc
__tty_ldisc_lock_nested(struct tty_struct * tty,unsigned long timeout)322 __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
323 {
324 return ldsem_down_write_nested(&tty->ldisc_sem,
325 LDISC_SEM_OTHER, timeout);
326 }
327
__tty_ldisc_unlock(struct tty_struct * tty)328 static inline void __tty_ldisc_unlock(struct tty_struct *tty)
329 {
330 ldsem_up_write(&tty->ldisc_sem);
331 }
332
333 static int __lockfunc
tty_ldisc_lock(struct tty_struct * tty,unsigned long timeout)334 tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
335 {
336 int ret;
337
338 ret = __tty_ldisc_lock(tty, timeout);
339 if (!ret)
340 return -EBUSY;
341 set_bit(TTY_LDISC_HALTED, &tty->flags);
342 return 0;
343 }
344
tty_ldisc_unlock(struct tty_struct * tty)345 static void tty_ldisc_unlock(struct tty_struct *tty)
346 {
347 clear_bit(TTY_LDISC_HALTED, &tty->flags);
348 __tty_ldisc_unlock(tty);
349 }
350
351 static int __lockfunc
tty_ldisc_lock_pair_timeout(struct tty_struct * tty,struct tty_struct * tty2,unsigned long timeout)352 tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
353 unsigned long timeout)
354 {
355 int ret;
356
357 if (tty < tty2) {
358 ret = __tty_ldisc_lock(tty, timeout);
359 if (ret) {
360 ret = __tty_ldisc_lock_nested(tty2, timeout);
361 if (!ret)
362 __tty_ldisc_unlock(tty);
363 }
364 } else {
365 /* if this is possible, it has lots of implications */
366 WARN_ON_ONCE(tty == tty2);
367 if (tty2 && tty != tty2) {
368 ret = __tty_ldisc_lock(tty2, timeout);
369 if (ret) {
370 ret = __tty_ldisc_lock_nested(tty, timeout);
371 if (!ret)
372 __tty_ldisc_unlock(tty2);
373 }
374 } else
375 ret = __tty_ldisc_lock(tty, timeout);
376 }
377
378 if (!ret)
379 return -EBUSY;
380
381 set_bit(TTY_LDISC_HALTED, &tty->flags);
382 if (tty2)
383 set_bit(TTY_LDISC_HALTED, &tty2->flags);
384 return 0;
385 }
386
387 static void __lockfunc
tty_ldisc_lock_pair(struct tty_struct * tty,struct tty_struct * tty2)388 tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
389 {
390 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
391 }
392
tty_ldisc_unlock_pair(struct tty_struct * tty,struct tty_struct * tty2)393 static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
394 struct tty_struct *tty2)
395 {
396 __tty_ldisc_unlock(tty);
397 if (tty2)
398 __tty_ldisc_unlock(tty2);
399 }
400
401 /**
402 * tty_ldisc_flush - flush line discipline queue
403 * @tty: tty
404 *
405 * Flush the line discipline queue (if any) and the tty flip buffers
406 * for this tty.
407 */
408
tty_ldisc_flush(struct tty_struct * tty)409 void tty_ldisc_flush(struct tty_struct *tty)
410 {
411 struct tty_ldisc *ld = tty_ldisc_ref(tty);
412
413 tty_buffer_flush(tty, ld);
414 if (ld)
415 tty_ldisc_deref(ld);
416 }
417 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
418
419 /**
420 * tty_set_termios_ldisc - set ldisc field
421 * @tty: tty structure
422 * @num: line discipline number
423 *
424 * This is probably overkill for real world processors but
425 * they are not on hot paths so a little discipline won't do
426 * any harm.
427 *
428 * The line discipline-related tty_struct fields are reset to
429 * prevent the ldisc driver from re-using stale information for
430 * the new ldisc instance.
431 *
432 * Locking: takes termios_rwsem
433 */
434
tty_set_termios_ldisc(struct tty_struct * tty,int num)435 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
436 {
437 down_write(&tty->termios_rwsem);
438 tty->termios.c_line = num;
439 up_write(&tty->termios_rwsem);
440
441 tty->disc_data = NULL;
442 tty->receive_room = 0;
443 }
444
445 /**
446 * tty_ldisc_open - open a line discipline
447 * @tty: tty we are opening the ldisc on
448 * @ld: discipline to open
449 *
450 * A helper opening method. Also a convenient debugging and check
451 * point.
452 *
453 * Locking: always called with BTM already held.
454 */
455
tty_ldisc_open(struct tty_struct * tty,struct tty_ldisc * ld)456 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
457 {
458 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
459 if (ld->ops->open) {
460 int ret;
461 /* BTM here locks versus a hangup event */
462 ret = ld->ops->open(tty);
463 if (ret)
464 clear_bit(TTY_LDISC_OPEN, &tty->flags);
465
466 tty_ldisc_debug(tty, "%p: opened\n", tty->ldisc);
467 return ret;
468 }
469 return 0;
470 }
471
472 /**
473 * tty_ldisc_close - close a line discipline
474 * @tty: tty we are opening the ldisc on
475 * @ld: discipline to close
476 *
477 * A helper close method. Also a convenient debugging and check
478 * point.
479 */
480
tty_ldisc_close(struct tty_struct * tty,struct tty_ldisc * ld)481 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
482 {
483 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
484 clear_bit(TTY_LDISC_OPEN, &tty->flags);
485 if (ld->ops->close)
486 ld->ops->close(tty);
487 tty_ldisc_debug(tty, "%p: closed\n", tty->ldisc);
488 }
489
490 /**
491 * tty_ldisc_restore - helper for tty ldisc change
492 * @tty: tty to recover
493 * @old: previous ldisc
494 *
495 * Restore the previous line discipline or N_TTY when a line discipline
496 * change fails due to an open error
497 */
498
tty_ldisc_restore(struct tty_struct * tty,struct tty_ldisc * old)499 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
500 {
501 struct tty_ldisc *new_ldisc;
502 int r;
503
504 /* There is an outstanding reference here so this is safe */
505 old = tty_ldisc_get(tty, old->ops->num);
506 WARN_ON(IS_ERR(old));
507 tty->ldisc = old;
508 tty_set_termios_ldisc(tty, old->ops->num);
509 if (tty_ldisc_open(tty, old) < 0) {
510 tty_ldisc_put(old);
511 /* This driver is always present */
512 new_ldisc = tty_ldisc_get(tty, N_TTY);
513 if (IS_ERR(new_ldisc))
514 panic("n_tty: get");
515 tty->ldisc = new_ldisc;
516 tty_set_termios_ldisc(tty, N_TTY);
517 r = tty_ldisc_open(tty, new_ldisc);
518 if (r < 0)
519 panic("Couldn't open N_TTY ldisc for "
520 "%s --- error %d.",
521 tty_name(tty), r);
522 }
523 }
524
525 /**
526 * tty_set_ldisc - set line discipline
527 * @tty: the terminal to set
528 * @ldisc: the line discipline
529 *
530 * Set the discipline of a tty line. Must be called from a process
531 * context. The ldisc change logic has to protect itself against any
532 * overlapping ldisc change (including on the other end of pty pairs),
533 * the close of one side of a tty/pty pair, and eventually hangup.
534 */
535
tty_set_ldisc(struct tty_struct * tty,int ldisc)536 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
537 {
538 int retval;
539 struct tty_ldisc *old_ldisc, *new_ldisc;
540
541 new_ldisc = tty_ldisc_get(tty, ldisc);
542 if (IS_ERR(new_ldisc))
543 return PTR_ERR(new_ldisc);
544
545 tty_lock(tty);
546 retval = tty_ldisc_lock(tty, 5 * HZ);
547 if (retval) {
548 tty_ldisc_put(new_ldisc);
549 tty_unlock(tty);
550 return retval;
551 }
552
553 /*
554 * Check the no-op case
555 */
556
557 if (tty->ldisc->ops->num == ldisc) {
558 tty_ldisc_unlock(tty);
559 tty_ldisc_put(new_ldisc);
560 tty_unlock(tty);
561 return 0;
562 }
563
564 old_ldisc = tty->ldisc;
565
566 if (test_bit(TTY_HUPPED, &tty->flags)) {
567 /* We were raced by the hangup method. It will have stomped
568 the ldisc data and closed the ldisc down */
569 tty_ldisc_unlock(tty);
570 tty_ldisc_put(new_ldisc);
571 tty_unlock(tty);
572 return -EIO;
573 }
574
575 /* Shutdown the old discipline. */
576 tty_ldisc_close(tty, old_ldisc);
577
578 /* Now set up the new line discipline. */
579 tty->ldisc = new_ldisc;
580 tty_set_termios_ldisc(tty, ldisc);
581
582 retval = tty_ldisc_open(tty, new_ldisc);
583 if (retval < 0) {
584 /* Back to the old one or N_TTY if we can't */
585 tty_ldisc_put(new_ldisc);
586 tty_ldisc_restore(tty, old_ldisc);
587 }
588
589 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
590 down_read(&tty->termios_rwsem);
591 tty->ops->set_ldisc(tty);
592 up_read(&tty->termios_rwsem);
593 }
594
595 /* At this point we hold a reference to the new ldisc and a
596 reference to the old ldisc, or we hold two references to
597 the old ldisc (if it was restored as part of error cleanup
598 above). In either case, releasing a single reference from
599 the old ldisc is correct. */
600
601 tty_ldisc_put(old_ldisc);
602
603 /*
604 * Allow ldisc referencing to occur again
605 */
606 tty_ldisc_unlock(tty);
607
608 /* Restart the work queue in case no characters kick it off. Safe if
609 already running */
610 tty_buffer_restart_work(tty->port);
611
612 tty_unlock(tty);
613 return retval;
614 }
615
616 /**
617 * tty_reset_termios - reset terminal state
618 * @tty: tty to reset
619 *
620 * Restore a terminal to the driver default state.
621 */
622
tty_reset_termios(struct tty_struct * tty)623 static void tty_reset_termios(struct tty_struct *tty)
624 {
625 down_write(&tty->termios_rwsem);
626 tty->termios = tty->driver->init_termios;
627 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
628 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
629 up_write(&tty->termios_rwsem);
630 }
631
632
633 /**
634 * tty_ldisc_reinit - reinitialise the tty ldisc
635 * @tty: tty to reinit
636 * @ldisc: line discipline to reinitialize
637 *
638 * Switch the tty to a line discipline and leave the ldisc
639 * state closed
640 */
641
tty_ldisc_reinit(struct tty_struct * tty,int ldisc)642 static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
643 {
644 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
645
646 if (IS_ERR(ld))
647 return -1;
648
649 tty_ldisc_close(tty, tty->ldisc);
650 tty_ldisc_put(tty->ldisc);
651 /*
652 * Switch the line discipline back
653 */
654 tty->ldisc = ld;
655 tty_set_termios_ldisc(tty, ldisc);
656
657 return 0;
658 }
659
660 /**
661 * tty_ldisc_hangup - hangup ldisc reset
662 * @tty: tty being hung up
663 *
664 * Some tty devices reset their termios when they receive a hangup
665 * event. In that situation we must also switch back to N_TTY properly
666 * before we reset the termios data.
667 *
668 * Locking: We can take the ldisc mutex as the rest of the code is
669 * careful to allow for this.
670 *
671 * In the pty pair case this occurs in the close() path of the
672 * tty itself so we must be careful about locking rules.
673 */
674
tty_ldisc_hangup(struct tty_struct * tty)675 void tty_ldisc_hangup(struct tty_struct *tty)
676 {
677 struct tty_ldisc *ld;
678 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
679 int err = 0;
680
681 tty_ldisc_debug(tty, "%p: closing\n", tty->ldisc);
682
683 ld = tty_ldisc_ref(tty);
684 if (ld != NULL) {
685 if (ld->ops->flush_buffer)
686 ld->ops->flush_buffer(tty);
687 tty_driver_flush_buffer(tty);
688 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
689 ld->ops->write_wakeup)
690 ld->ops->write_wakeup(tty);
691 if (ld->ops->hangup)
692 ld->ops->hangup(tty);
693 tty_ldisc_deref(ld);
694 }
695
696 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
697 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
698
699 /*
700 * Shutdown the current line discipline, and reset it to
701 * N_TTY if need be.
702 *
703 * Avoid racing set_ldisc or tty_ldisc_release
704 */
705 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
706
707 if (tty->ldisc) {
708
709 /* At this point we have a halted ldisc; we want to close it and
710 reopen a new ldisc. We could defer the reopen to the next
711 open but it means auditing a lot of other paths so this is
712 a FIXME */
713 if (reset == 0) {
714
715 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
716 err = tty_ldisc_open(tty, tty->ldisc);
717 else
718 err = 1;
719 }
720 /* If the re-open fails or we reset then go to N_TTY. The
721 N_TTY open cannot fail */
722 if (reset || err) {
723 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
724 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
725 }
726 }
727 tty_ldisc_unlock(tty);
728 if (reset)
729 tty_reset_termios(tty);
730
731 tty_ldisc_debug(tty, "%p: re-opened\n", tty->ldisc);
732 }
733
734 /**
735 * tty_ldisc_setup - open line discipline
736 * @tty: tty being shut down
737 * @o_tty: pair tty for pty/tty pairs
738 *
739 * Called during the initial open of a tty/pty pair in order to set up the
740 * line disciplines and bind them to the tty. This has no locking issues
741 * as the device isn't yet active.
742 */
743
tty_ldisc_setup(struct tty_struct * tty,struct tty_struct * o_tty)744 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
745 {
746 struct tty_ldisc *ld = tty->ldisc;
747 int retval;
748
749 retval = tty_ldisc_open(tty, ld);
750 if (retval)
751 return retval;
752
753 if (o_tty) {
754 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
755 if (retval) {
756 tty_ldisc_close(tty, ld);
757 return retval;
758 }
759 }
760 return 0;
761 }
762
tty_ldisc_kill(struct tty_struct * tty)763 static void tty_ldisc_kill(struct tty_struct *tty)
764 {
765 /*
766 * Now kill off the ldisc
767 */
768 tty_ldisc_close(tty, tty->ldisc);
769 tty_ldisc_put(tty->ldisc);
770 /* Force an oops if we mess this up */
771 tty->ldisc = NULL;
772
773 /* Ensure the next open requests the N_TTY ldisc */
774 tty_set_termios_ldisc(tty, N_TTY);
775 }
776
777 /**
778 * tty_ldisc_release - release line discipline
779 * @tty: tty being shut down (or one end of pty pair)
780 *
781 * Called during the final close of a tty or a pty pair in order to shut
782 * down the line discpline layer. On exit, each ldisc assigned is N_TTY and
783 * each ldisc has not been opened.
784 */
785
tty_ldisc_release(struct tty_struct * tty)786 void tty_ldisc_release(struct tty_struct *tty)
787 {
788 struct tty_struct *o_tty = tty->link;
789
790 /*
791 * Shutdown this line discipline. As this is the final close,
792 * it does not race with the set_ldisc code path.
793 */
794
795 tty_ldisc_lock_pair(tty, o_tty);
796 tty_ldisc_kill(tty);
797 if (o_tty)
798 tty_ldisc_kill(o_tty);
799 tty_ldisc_unlock_pair(tty, o_tty);
800
801 /* And the memory resources remaining (buffers, termios) will be
802 disposed of when the kref hits zero */
803
804 tty_ldisc_debug(tty, "released\n");
805 }
806
807 /**
808 * tty_ldisc_init - ldisc setup for new tty
809 * @tty: tty being allocated
810 *
811 * Set up the line discipline objects for a newly allocated tty. Note that
812 * the tty structure is not completely set up when this call is made.
813 */
814
tty_ldisc_init(struct tty_struct * tty)815 int tty_ldisc_init(struct tty_struct *tty)
816 {
817 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
818 if (IS_ERR(ld))
819 return PTR_ERR(ld);
820 tty->ldisc = ld;
821 return 0;
822 }
823
824 /**
825 * tty_ldisc_init - ldisc cleanup for new tty
826 * @tty: tty that was allocated recently
827 *
828 * The tty structure must not becompletely set up (tty_ldisc_setup) when
829 * this call is made.
830 */
tty_ldisc_deinit(struct tty_struct * tty)831 void tty_ldisc_deinit(struct tty_struct *tty)
832 {
833 tty_ldisc_put(tty->ldisc);
834 tty->ldisc = NULL;
835 }
836
tty_ldisc_begin(void)837 void tty_ldisc_begin(void)
838 {
839 /* Setup the default TTY line discipline. */
840 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
841 }
842
843 static int zero;
844 static int one = 1;
845 static struct ctl_table tty_table[] = {
846 {
847 .procname = "ldisc_autoload",
848 .data = &tty_ldisc_autoload,
849 .maxlen = sizeof(tty_ldisc_autoload),
850 .mode = 0644,
851 .proc_handler = proc_dointvec,
852 .extra1 = &zero,
853 .extra2 = &one,
854 },
855 { }
856 };
857
858 static struct ctl_table tty_dir_table[] = {
859 {
860 .procname = "tty",
861 .mode = 0555,
862 .child = tty_table,
863 },
864 { }
865 };
866
867 static struct ctl_table tty_root_table[] = {
868 {
869 .procname = "dev",
870 .mode = 0555,
871 .child = tty_dir_table,
872 },
873 { }
874 };
875
tty_sysctl_init(void)876 void tty_sysctl_init(void)
877 {
878 register_sysctl_table(tty_root_table);
879 }
880