tty_ldisc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
  423. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  424. if (ld->ops->close)
  425. ld->ops->close(tty);
  426. tty_ldisc_debug(tty, "%p: closed\n", ld);
  427. }
  428. /**
  429. * tty_ldisc_failto - helper for ldisc failback
  430. * @tty: tty to open the ldisc on
  431. * @ld: ldisc we are trying to fail back to
  432. *
  433. * Helper to try and recover a tty when switching back to the old
  434. * ldisc fails and we need something attached.
  435. */
  436. static int tty_ldisc_failto(struct tty_struct *tty, int ld)
  437. {
  438. struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
  439. int r;
  440. if (IS_ERR(disc))
  441. return PTR_ERR(disc);
  442. tty->ldisc = disc;
  443. tty_set_termios_ldisc(tty, ld);
  444. if ((r = tty_ldisc_open(tty, disc)) < 0)
  445. tty_ldisc_put(disc);
  446. return r;
  447. }
  448. /**
  449. * tty_ldisc_restore - helper for tty ldisc change
  450. * @tty: tty to recover
  451. * @old: previous ldisc
  452. *
  453. * Restore the previous line discipline or N_TTY when a line discipline
  454. * change fails due to an open error
  455. */
  456. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  457. {
  458. /* There is an outstanding reference here so this is safe */
  459. if (tty_ldisc_failto(tty, old->ops->num) < 0) {
  460. const char *name = tty_name(tty);
  461. pr_warn("Falling back ldisc for %s.\n", name);
  462. /* The traditional behaviour is to fall back to N_TTY, we
  463. want to avoid falling back to N_NULL unless we have no
  464. choice to avoid the risk of breaking anything */
  465. if (tty_ldisc_failto(tty, N_TTY) < 0 &&
  466. tty_ldisc_failto(tty, N_NULL) < 0)
  467. panic("Couldn't open N_NULL ldisc for %s.", name);
  468. }
  469. }
  470. /**
  471. * tty_set_ldisc - set line discipline
  472. * @tty: the terminal to set
  473. * @ldisc: the line discipline
  474. *
  475. * Set the discipline of a tty line. Must be called from a process
  476. * context. The ldisc change logic has to protect itself against any
  477. * overlapping ldisc change (including on the other end of pty pairs),
  478. * the close of one side of a tty/pty pair, and eventually hangup.
  479. */
  480. int tty_set_ldisc(struct tty_struct *tty, int disc)
  481. {
  482. int retval;
  483. struct tty_ldisc *old_ldisc, *new_ldisc;
  484. new_ldisc = tty_ldisc_get(tty, disc);
  485. if (IS_ERR(new_ldisc))
  486. return PTR_ERR(new_ldisc);
  487. tty_lock(tty);
  488. retval = tty_ldisc_lock(tty, 5 * HZ);
  489. if (retval)
  490. goto err;
  491. if (!tty->ldisc) {
  492. retval = -EIO;
  493. goto out;
  494. }
  495. /* Check the no-op case */
  496. if (tty->ldisc->ops->num == disc)
  497. goto out;
  498. if (test_bit(TTY_HUPPED, &tty->flags)) {
  499. /* We were raced by hangup */
  500. retval = -EIO;
  501. goto out;
  502. }
  503. old_ldisc = tty->ldisc;
  504. /* Shutdown the old discipline. */
  505. tty_ldisc_close(tty, old_ldisc);
  506. /* Now set up the new line discipline. */
  507. tty->ldisc = new_ldisc;
  508. tty_set_termios_ldisc(tty, disc);
  509. retval = tty_ldisc_open(tty, new_ldisc);
  510. if (retval < 0) {
  511. /* Back to the old one or N_TTY if we can't */
  512. tty_ldisc_put(new_ldisc);
  513. tty_ldisc_restore(tty, old_ldisc);
  514. }
  515. if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
  516. down_read(&tty->termios_rwsem);
  517. tty->ops->set_ldisc(tty);
  518. up_read(&tty->termios_rwsem);
  519. }
  520. /* At this point we hold a reference to the new ldisc and a
  521. reference to the old ldisc, or we hold two references to
  522. the old ldisc (if it was restored as part of error cleanup
  523. above). In either case, releasing a single reference from
  524. the old ldisc is correct. */
  525. new_ldisc = old_ldisc;
  526. out:
  527. tty_ldisc_unlock(tty);
  528. /* Restart the work queue in case no characters kick it off. Safe if
  529. already running */
  530. tty_buffer_restart_work(tty->port);
  531. err:
  532. tty_ldisc_put(new_ldisc); /* drop the extra reference */
  533. tty_unlock(tty);
  534. return retval;
  535. }
  536. EXPORT_SYMBOL_GPL(tty_set_ldisc);
  537. /**
  538. * tty_ldisc_kill - teardown ldisc
  539. * @tty: tty being released
  540. *
  541. * Perform final close of the ldisc and reset tty->ldisc
  542. */
  543. static void tty_ldisc_kill(struct tty_struct *tty)
  544. {
  545. if (!tty->ldisc)
  546. return;
  547. /*
  548. * Now kill off the ldisc
  549. */
  550. tty_ldisc_close(tty, tty->ldisc);
  551. tty_ldisc_put(tty->ldisc);
  552. /* Force an oops if we mess this up */
  553. tty->ldisc = NULL;
  554. }
  555. /**
  556. * tty_reset_termios - reset terminal state
  557. * @tty: tty to reset
  558. *
  559. * Restore a terminal to the driver default state.
  560. */
  561. static void tty_reset_termios(struct tty_struct *tty)
  562. {
  563. down_write(&tty->termios_rwsem);
  564. tty->termios = tty->driver->init_termios;
  565. tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
  566. tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
  567. up_write(&tty->termios_rwsem);
  568. }
  569. /**
  570. * tty_ldisc_reinit - reinitialise the tty ldisc
  571. * @tty: tty to reinit
  572. * @disc: line discipline to reinitialize
  573. *
  574. * Completely reinitialize the line discipline state, by closing the
  575. * current instance, if there is one, and opening a new instance. If
  576. * an error occurs opening the new non-N_TTY instance, the instance
  577. * is dropped and tty->ldisc reset to NULL. The caller can then retry
  578. * with N_TTY instead.
  579. *
  580. * Returns 0 if successful, otherwise error code < 0
  581. */
  582. int tty_ldisc_reinit(struct tty_struct *tty, int disc)
  583. {
  584. struct tty_ldisc *ld;
  585. int retval;
  586. ld = tty_ldisc_get(tty, disc);
  587. if (IS_ERR(ld)) {
  588. BUG_ON(disc == N_TTY);
  589. return PTR_ERR(ld);
  590. }
  591. if (tty->ldisc) {
  592. tty_ldisc_close(tty, tty->ldisc);
  593. tty_ldisc_put(tty->ldisc);
  594. }
  595. /* switch the line discipline */
  596. tty->ldisc = ld;
  597. tty_set_termios_ldisc(tty, disc);
  598. retval = tty_ldisc_open(tty, tty->ldisc);
  599. if (retval) {
  600. tty_ldisc_put(tty->ldisc);
  601. tty->ldisc = NULL;
  602. }
  603. return retval;
  604. }
  605. /**
  606. * tty_ldisc_hangup - hangup ldisc reset
  607. * @tty: tty being hung up
  608. *
  609. * Some tty devices reset their termios when they receive a hangup
  610. * event. In that situation we must also switch back to N_TTY properly
  611. * before we reset the termios data.
  612. *
  613. * Locking: We can take the ldisc mutex as the rest of the code is
  614. * careful to allow for this.
  615. *
  616. * In the pty pair case this occurs in the close() path of the
  617. * tty itself so we must be careful about locking rules.
  618. */
  619. void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
  620. {
  621. struct tty_ldisc *ld;
  622. tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
  623. ld = tty_ldisc_ref(tty);
  624. if (ld != NULL) {
  625. if (ld->ops->flush_buffer)
  626. ld->ops->flush_buffer(tty);
  627. tty_driver_flush_buffer(tty);
  628. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  629. ld->ops->write_wakeup)
  630. ld->ops->write_wakeup(tty);
  631. if (ld->ops->hangup)
  632. ld->ops->hangup(tty);
  633. tty_ldisc_deref(ld);
  634. }
  635. wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
  636. wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
  637. /*
  638. * Shutdown the current line discipline, and reset it to
  639. * N_TTY if need be.
  640. *
  641. * Avoid racing set_ldisc or tty_ldisc_release
  642. */
  643. tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
  644. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
  645. tty_reset_termios(tty);
  646. if (tty->ldisc) {
  647. if (reinit) {
  648. if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
  649. tty_ldisc_reinit(tty, N_TTY) < 0)
  650. WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
  651. } else
  652. tty_ldisc_kill(tty);
  653. }
  654. tty_ldisc_unlock(tty);
  655. }
  656. /**
  657. * tty_ldisc_setup - open line discipline
  658. * @tty: tty being shut down
  659. * @o_tty: pair tty for pty/tty pairs
  660. *
  661. * Called during the initial open of a tty/pty pair in order to set up the
  662. * line disciplines and bind them to the tty. This has no locking issues
  663. * as the device isn't yet active.
  664. */
  665. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  666. {
  667. int retval = tty_ldisc_open(tty, tty->ldisc);
  668. if (retval)
  669. return retval;
  670. if (o_tty) {
  671. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  672. if (retval) {
  673. tty_ldisc_close(tty, tty->ldisc);
  674. return retval;
  675. }
  676. }
  677. return 0;
  678. }
  679. /**
  680. * tty_ldisc_release - release line discipline
  681. * @tty: tty being shut down (or one end of pty pair)
  682. *
  683. * Called during the final close of a tty or a pty pair in order to shut
  684. * down the line discpline layer. On exit, each tty's ldisc is NULL.
  685. */
  686. void tty_ldisc_release(struct tty_struct *tty)
  687. {
  688. struct tty_struct *o_tty = tty->link;
  689. /*
  690. * Shutdown this line discipline. As this is the final close,
  691. * it does not race with the set_ldisc code path.
  692. */
  693. tty_ldisc_lock_pair(tty, o_tty);
  694. tty_ldisc_kill(tty);
  695. if (o_tty)
  696. tty_ldisc_kill(o_tty);
  697. tty_ldisc_unlock_pair(tty, o_tty);
  698. /* And the memory resources remaining (buffers, termios) will be
  699. disposed of when the kref hits zero */
  700. tty_ldisc_debug(tty, "released\n");
  701. }
  702. EXPORT_SYMBOL_GPL(tty_ldisc_release);
  703. /**
  704. * tty_ldisc_init - ldisc setup for new tty
  705. * @tty: tty being allocated
  706. *
  707. * Set up the line discipline objects for a newly allocated tty. Note that
  708. * the tty structure is not completely set up when this call is made.
  709. */
  710. int tty_ldisc_init(struct tty_struct *tty)
  711. {
  712. struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
  713. if (IS_ERR(ld))
  714. return PTR_ERR(ld);
  715. tty->ldisc = ld;
  716. return 0;
  717. }
  718. /**
  719. * tty_ldisc_deinit - ldisc cleanup for new tty
  720. * @tty: tty that was allocated recently
  721. *
  722. * The tty structure must not becompletely set up (tty_ldisc_setup) when
  723. * this call is made.
  724. */
  725. void tty_ldisc_deinit(struct tty_struct *tty)
  726. {
  727. if (tty->ldisc)
  728. tty_ldisc_put(tty->ldisc);
  729. tty->ldisc = NULL;
  730. }
  731. static int zero;
  732. static int one = 1;
  733. static struct ctl_table tty_table[] = {
  734. {
  735. .procname = "ldisc_autoload",
  736. .data = &tty_ldisc_autoload,
  737. .maxlen = sizeof(tty_ldisc_autoload),
  738. .mode = 0644,
  739. .proc_handler = proc_dointvec,
  740. .extra1 = &zero,
  741. .extra2 = &one,
  742. },
  743. { }
  744. };
  745. static struct ctl_table tty_dir_table[] = {
  746. {
  747. .procname = "tty",
  748. .mode = 0555,
  749. .child = tty_table,
  750. },
  751. { }
  752. };
  753. static struct ctl_table tty_root_table[] = {
  754. {
  755. .procname = "dev",
  756. .mode = 0555,
  757. .child = tty_dir_table,
  758. },
  759. { }
  760. };
  761. void tty_sysctl_init(void)
  762. {
  763. register_sysctl_table(tty_root_table);
  764. }