tty_ldisc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. 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. request_module("tty-ldisc-%d", disc);
  150. ldops = get_ldops(disc);
  151. if (IS_ERR(ldops))
  152. return ERR_CAST(ldops);
  153. }
  154. /*
  155. * There is no way to handle allocation failure of only 16 bytes.
  156. * Let's simplify error handling and save more memory.
  157. */
  158. ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
  159. ld->ops = ldops;
  160. ld->tty = tty;
  161. return ld;
  162. }
  163. /**
  164. * tty_ldisc_put - release the ldisc
  165. *
  166. * Complement of tty_ldisc_get().
  167. */
  168. static void tty_ldisc_put(struct tty_ldisc *ld)
  169. {
  170. if (WARN_ON_ONCE(!ld))
  171. return;
  172. put_ldops(ld->ops);
  173. kfree(ld);
  174. }
  175. static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
  176. {
  177. return (*pos < NR_LDISCS) ? pos : NULL;
  178. }
  179. static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  180. {
  181. (*pos)++;
  182. return (*pos < NR_LDISCS) ? pos : NULL;
  183. }
  184. static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
  185. {
  186. }
  187. static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
  188. {
  189. int i = *(loff_t *)v;
  190. struct tty_ldisc_ops *ldops;
  191. ldops = get_ldops(i);
  192. if (IS_ERR(ldops))
  193. return 0;
  194. seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
  195. put_ldops(ldops);
  196. return 0;
  197. }
  198. static const struct seq_operations tty_ldiscs_seq_ops = {
  199. .start = tty_ldiscs_seq_start,
  200. .next = tty_ldiscs_seq_next,
  201. .stop = tty_ldiscs_seq_stop,
  202. .show = tty_ldiscs_seq_show,
  203. };
  204. static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
  205. {
  206. return seq_open(file, &tty_ldiscs_seq_ops);
  207. }
  208. const struct file_operations tty_ldiscs_proc_fops = {
  209. .owner = THIS_MODULE,
  210. .open = proc_tty_ldiscs_open,
  211. .read = seq_read,
  212. .llseek = seq_lseek,
  213. .release = seq_release,
  214. };
  215. /**
  216. * tty_ldisc_ref_wait - wait for the tty ldisc
  217. * @tty: tty device
  218. *
  219. * Dereference the line discipline for the terminal and take a
  220. * reference to it. If the line discipline is in flux then
  221. * wait patiently until it changes.
  222. *
  223. * Returns: NULL if the tty has been hungup and not re-opened with
  224. * a new file descriptor, otherwise valid ldisc reference
  225. *
  226. * Note: Must not be called from an IRQ/timer context. The caller
  227. * must also be careful not to hold other locks that will deadlock
  228. * against a discipline change, such as an existing ldisc reference
  229. * (which we check for)
  230. *
  231. * Note: a file_operations routine (read/poll/write) should use this
  232. * function to wait for any ldisc lifetime events to finish.
  233. */
  234. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  235. {
  236. struct tty_ldisc *ld;
  237. ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
  238. ld = tty->ldisc;
  239. if (!ld)
  240. ldsem_up_read(&tty->ldisc_sem);
  241. return ld;
  242. }
  243. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  244. /**
  245. * tty_ldisc_ref - get the tty ldisc
  246. * @tty: tty device
  247. *
  248. * Dereference the line discipline for the terminal and take a
  249. * reference to it. If the line discipline is in flux then
  250. * return NULL. Can be called from IRQ and timer functions.
  251. */
  252. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  253. {
  254. struct tty_ldisc *ld = NULL;
  255. if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
  256. ld = tty->ldisc;
  257. if (!ld)
  258. ldsem_up_read(&tty->ldisc_sem);
  259. }
  260. return ld;
  261. }
  262. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  263. /**
  264. * tty_ldisc_deref - free a tty ldisc reference
  265. * @ld: reference to free up
  266. *
  267. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  268. * be called in IRQ context.
  269. */
  270. void tty_ldisc_deref(struct tty_ldisc *ld)
  271. {
  272. ldsem_up_read(&ld->tty->ldisc_sem);
  273. }
  274. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  275. static inline int
  276. __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
  277. {
  278. return ldsem_down_write(&tty->ldisc_sem, timeout);
  279. }
  280. static inline int
  281. __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
  282. {
  283. return ldsem_down_write_nested(&tty->ldisc_sem,
  284. LDISC_SEM_OTHER, timeout);
  285. }
  286. static inline void __tty_ldisc_unlock(struct tty_struct *tty)
  287. {
  288. ldsem_up_write(&tty->ldisc_sem);
  289. }
  290. int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
  291. {
  292. int ret;
  293. ret = __tty_ldisc_lock(tty, timeout);
  294. if (!ret)
  295. return -EBUSY;
  296. set_bit(TTY_LDISC_HALTED, &tty->flags);
  297. return 0;
  298. }
  299. void tty_ldisc_unlock(struct tty_struct *tty)
  300. {
  301. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  302. __tty_ldisc_unlock(tty);
  303. }
  304. static int
  305. tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
  306. unsigned long timeout)
  307. {
  308. int ret;
  309. if (tty < tty2) {
  310. ret = __tty_ldisc_lock(tty, timeout);
  311. if (ret) {
  312. ret = __tty_ldisc_lock_nested(tty2, timeout);
  313. if (!ret)
  314. __tty_ldisc_unlock(tty);
  315. }
  316. } else {
  317. /* if this is possible, it has lots of implications */
  318. WARN_ON_ONCE(tty == tty2);
  319. if (tty2 && tty != tty2) {
  320. ret = __tty_ldisc_lock(tty2, timeout);
  321. if (ret) {
  322. ret = __tty_ldisc_lock_nested(tty, timeout);
  323. if (!ret)
  324. __tty_ldisc_unlock(tty2);
  325. }
  326. } else
  327. ret = __tty_ldisc_lock(tty, timeout);
  328. }
  329. if (!ret)
  330. return -EBUSY;
  331. set_bit(TTY_LDISC_HALTED, &tty->flags);
  332. if (tty2)
  333. set_bit(TTY_LDISC_HALTED, &tty2->flags);
  334. return 0;
  335. }
  336. static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
  337. {
  338. tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
  339. }
  340. static void tty_ldisc_unlock_pair(struct tty_struct *tty,
  341. struct tty_struct *tty2)
  342. {
  343. __tty_ldisc_unlock(tty);
  344. if (tty2)
  345. __tty_ldisc_unlock(tty2);
  346. }
  347. /**
  348. * tty_ldisc_flush - flush line discipline queue
  349. * @tty: tty
  350. *
  351. * Flush the line discipline queue (if any) and the tty flip buffers
  352. * for this tty.
  353. */
  354. void tty_ldisc_flush(struct tty_struct *tty)
  355. {
  356. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  357. tty_buffer_flush(tty, ld);
  358. if (ld)
  359. tty_ldisc_deref(ld);
  360. }
  361. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  362. /**
  363. * tty_set_termios_ldisc - set ldisc field
  364. * @tty: tty structure
  365. * @disc: line discipline number
  366. *
  367. * This is probably overkill for real world processors but
  368. * they are not on hot paths so a little discipline won't do
  369. * any harm.
  370. *
  371. * The line discipline-related tty_struct fields are reset to
  372. * prevent the ldisc driver from re-using stale information for
  373. * the new ldisc instance.
  374. *
  375. * Locking: takes termios_rwsem
  376. */
  377. static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
  378. {
  379. down_write(&tty->termios_rwsem);
  380. tty->termios.c_line = disc;
  381. up_write(&tty->termios_rwsem);
  382. tty->disc_data = NULL;
  383. tty->receive_room = 0;
  384. }
  385. /**
  386. * tty_ldisc_open - open a line discipline
  387. * @tty: tty we are opening the ldisc on
  388. * @ld: discipline to open
  389. *
  390. * A helper opening method. Also a convenient debugging and check
  391. * point.
  392. *
  393. * Locking: always called with BTM already held.
  394. */
  395. static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
  396. {
  397. WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
  398. if (ld->ops->open) {
  399. int ret;
  400. /* BTM here locks versus a hangup event */
  401. ret = ld->ops->open(tty);
  402. if (ret)
  403. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  404. tty_ldisc_debug(tty, "%p: opened\n", ld);
  405. return ret;
  406. }
  407. return 0;
  408. }
  409. /**
  410. * tty_ldisc_close - close a line discipline
  411. * @tty: tty we are opening the ldisc on
  412. * @ld: discipline to close
  413. *
  414. * A helper close method. Also a convenient debugging and check
  415. * point.
  416. */
  417. static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
  418. {
  419. WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
  420. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  421. if (ld->ops->close)
  422. ld->ops->close(tty);
  423. tty_ldisc_debug(tty, "%p: closed\n", ld);
  424. }
  425. /**
  426. * tty_ldisc_failto - helper for ldisc failback
  427. * @tty: tty to open the ldisc on
  428. * @ld: ldisc we are trying to fail back to
  429. *
  430. * Helper to try and recover a tty when switching back to the old
  431. * ldisc fails and we need something attached.
  432. */
  433. static int tty_ldisc_failto(struct tty_struct *tty, int ld)
  434. {
  435. struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
  436. int r;
  437. if (IS_ERR(disc))
  438. return PTR_ERR(disc);
  439. tty->ldisc = disc;
  440. tty_set_termios_ldisc(tty, ld);
  441. if ((r = tty_ldisc_open(tty, disc)) < 0)
  442. tty_ldisc_put(disc);
  443. return r;
  444. }
  445. /**
  446. * tty_ldisc_restore - helper for tty ldisc change
  447. * @tty: tty to recover
  448. * @old: previous ldisc
  449. *
  450. * Restore the previous line discipline or N_TTY when a line discipline
  451. * change fails due to an open error
  452. */
  453. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  454. {
  455. /* There is an outstanding reference here so this is safe */
  456. if (tty_ldisc_failto(tty, old->ops->num) < 0) {
  457. const char *name = tty_name(tty);
  458. pr_warn("Falling back ldisc for %s.\n", name);
  459. /* The traditional behaviour is to fall back to N_TTY, we
  460. want to avoid falling back to N_NULL unless we have no
  461. choice to avoid the risk of breaking anything */
  462. if (tty_ldisc_failto(tty, N_TTY) < 0 &&
  463. tty_ldisc_failto(tty, N_NULL) < 0)
  464. panic("Couldn't open N_NULL ldisc for %s.", name);
  465. }
  466. }
  467. /**
  468. * tty_set_ldisc - set line discipline
  469. * @tty: the terminal to set
  470. * @ldisc: the line discipline
  471. *
  472. * Set the discipline of a tty line. Must be called from a process
  473. * context. The ldisc change logic has to protect itself against any
  474. * overlapping ldisc change (including on the other end of pty pairs),
  475. * the close of one side of a tty/pty pair, and eventually hangup.
  476. */
  477. int tty_set_ldisc(struct tty_struct *tty, int disc)
  478. {
  479. int retval;
  480. struct tty_ldisc *old_ldisc, *new_ldisc;
  481. new_ldisc = tty_ldisc_get(tty, disc);
  482. if (IS_ERR(new_ldisc))
  483. return PTR_ERR(new_ldisc);
  484. tty_lock(tty);
  485. retval = tty_ldisc_lock(tty, 5 * HZ);
  486. if (retval)
  487. goto err;
  488. if (!tty->ldisc) {
  489. retval = -EIO;
  490. goto out;
  491. }
  492. /* Check the no-op case */
  493. if (tty->ldisc->ops->num == disc)
  494. goto out;
  495. if (test_bit(TTY_HUPPED, &tty->flags)) {
  496. /* We were raced by hangup */
  497. retval = -EIO;
  498. goto out;
  499. }
  500. old_ldisc = tty->ldisc;
  501. /* Shutdown the old discipline. */
  502. tty_ldisc_close(tty, old_ldisc);
  503. /* Now set up the new line discipline. */
  504. tty->ldisc = new_ldisc;
  505. tty_set_termios_ldisc(tty, disc);
  506. retval = tty_ldisc_open(tty, new_ldisc);
  507. if (retval < 0) {
  508. /* Back to the old one or N_TTY if we can't */
  509. tty_ldisc_put(new_ldisc);
  510. tty_ldisc_restore(tty, old_ldisc);
  511. }
  512. if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
  513. down_read(&tty->termios_rwsem);
  514. tty->ops->set_ldisc(tty);
  515. up_read(&tty->termios_rwsem);
  516. }
  517. /* At this point we hold a reference to the new ldisc and a
  518. reference to the old ldisc, or we hold two references to
  519. the old ldisc (if it was restored as part of error cleanup
  520. above). In either case, releasing a single reference from
  521. the old ldisc is correct. */
  522. new_ldisc = old_ldisc;
  523. out:
  524. tty_ldisc_unlock(tty);
  525. /* Restart the work queue in case no characters kick it off. Safe if
  526. already running */
  527. tty_buffer_restart_work(tty->port);
  528. err:
  529. tty_ldisc_put(new_ldisc); /* drop the extra reference */
  530. tty_unlock(tty);
  531. return retval;
  532. }
  533. EXPORT_SYMBOL_GPL(tty_set_ldisc);
  534. /**
  535. * tty_ldisc_kill - teardown ldisc
  536. * @tty: tty being released
  537. *
  538. * Perform final close of the ldisc and reset tty->ldisc
  539. */
  540. static void tty_ldisc_kill(struct tty_struct *tty)
  541. {
  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. ld = tty_ldisc_get(tty, disc);
  584. if (IS_ERR(ld)) {
  585. BUG_ON(disc == N_TTY);
  586. return PTR_ERR(ld);
  587. }
  588. if (tty->ldisc) {
  589. tty_ldisc_close(tty, tty->ldisc);
  590. tty_ldisc_put(tty->ldisc);
  591. }
  592. /* switch the line discipline */
  593. tty->ldisc = ld;
  594. tty_set_termios_ldisc(tty, disc);
  595. retval = tty_ldisc_open(tty, tty->ldisc);
  596. if (retval) {
  597. tty_ldisc_put(tty->ldisc);
  598. tty->ldisc = NULL;
  599. }
  600. return retval;
  601. }
  602. /**
  603. * tty_ldisc_hangup - hangup ldisc reset
  604. * @tty: tty being hung up
  605. *
  606. * Some tty devices reset their termios when they receive a hangup
  607. * event. In that situation we must also switch back to N_TTY properly
  608. * before we reset the termios data.
  609. *
  610. * Locking: We can take the ldisc mutex as the rest of the code is
  611. * careful to allow for this.
  612. *
  613. * In the pty pair case this occurs in the close() path of the
  614. * tty itself so we must be careful about locking rules.
  615. */
  616. void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
  617. {
  618. struct tty_ldisc *ld;
  619. tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
  620. ld = tty_ldisc_ref(tty);
  621. if (ld != NULL) {
  622. if (ld->ops->flush_buffer)
  623. ld->ops->flush_buffer(tty);
  624. tty_driver_flush_buffer(tty);
  625. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  626. ld->ops->write_wakeup)
  627. ld->ops->write_wakeup(tty);
  628. if (ld->ops->hangup)
  629. ld->ops->hangup(tty);
  630. tty_ldisc_deref(ld);
  631. }
  632. wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
  633. wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
  634. /*
  635. * Shutdown the current line discipline, and reset it to
  636. * N_TTY if need be.
  637. *
  638. * Avoid racing set_ldisc or tty_ldisc_release
  639. */
  640. tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
  641. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
  642. tty_reset_termios(tty);
  643. if (tty->ldisc) {
  644. if (reinit) {
  645. if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
  646. tty_ldisc_reinit(tty, N_TTY) < 0)
  647. WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
  648. } else
  649. tty_ldisc_kill(tty);
  650. }
  651. tty_ldisc_unlock(tty);
  652. }
  653. /**
  654. * tty_ldisc_setup - open line discipline
  655. * @tty: tty being shut down
  656. * @o_tty: pair tty for pty/tty pairs
  657. *
  658. * Called during the initial open of a tty/pty pair in order to set up the
  659. * line disciplines and bind them to the tty. This has no locking issues
  660. * as the device isn't yet active.
  661. */
  662. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  663. {
  664. int retval = tty_ldisc_open(tty, tty->ldisc);
  665. if (retval)
  666. return retval;
  667. if (o_tty) {
  668. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  669. if (retval) {
  670. tty_ldisc_close(tty, tty->ldisc);
  671. return retval;
  672. }
  673. }
  674. return 0;
  675. }
  676. /**
  677. * tty_ldisc_release - release line discipline
  678. * @tty: tty being shut down (or one end of pty pair)
  679. *
  680. * Called during the final close of a tty or a pty pair in order to shut
  681. * down the line discpline layer. On exit, each tty's ldisc is NULL.
  682. */
  683. void tty_ldisc_release(struct tty_struct *tty)
  684. {
  685. struct tty_struct *o_tty = tty->link;
  686. /*
  687. * Shutdown this line discipline. As this is the final close,
  688. * it does not race with the set_ldisc code path.
  689. */
  690. tty_ldisc_lock_pair(tty, o_tty);
  691. tty_ldisc_kill(tty);
  692. if (o_tty)
  693. tty_ldisc_kill(o_tty);
  694. tty_ldisc_unlock_pair(tty, o_tty);
  695. /* And the memory resources remaining (buffers, termios) will be
  696. disposed of when the kref hits zero */
  697. tty_ldisc_debug(tty, "released\n");
  698. }
  699. EXPORT_SYMBOL_GPL(tty_ldisc_release);
  700. /**
  701. * tty_ldisc_init - ldisc setup for new tty
  702. * @tty: tty being allocated
  703. *
  704. * Set up the line discipline objects for a newly allocated tty. Note that
  705. * the tty structure is not completely set up when this call is made.
  706. */
  707. int tty_ldisc_init(struct tty_struct *tty)
  708. {
  709. struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
  710. if (IS_ERR(ld))
  711. return PTR_ERR(ld);
  712. tty->ldisc = ld;
  713. return 0;
  714. }
  715. /**
  716. * tty_ldisc_deinit - ldisc cleanup for new tty
  717. * @tty: tty that was allocated recently
  718. *
  719. * The tty structure must not becompletely set up (tty_ldisc_setup) when
  720. * this call is made.
  721. */
  722. void tty_ldisc_deinit(struct tty_struct *tty)
  723. {
  724. if (tty->ldisc)
  725. tty_ldisc_put(tty->ldisc);
  726. tty->ldisc = NULL;
  727. }