tty_ldisc.c 21 KB

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