tty_ldisc.c 21 KB

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