tty_ldisc.c 20 KB

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