tty_ldisc.c 21 KB

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