tty_ldisc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. #include <linux/types.h>
  2. #include <linux/major.h>
  3. #include <linux/errno.h>
  4. #include <linux/signal.h>
  5. #include <linux/fcntl.h>
  6. #include <linux/sched.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/tty.h>
  9. #include <linux/tty_driver.h>
  10. #include <linux/tty_flip.h>
  11. #include <linux/devpts_fs.h>
  12. #include <linux/file.h>
  13. #include <linux/console.h>
  14. #include <linux/timer.h>
  15. #include <linux/ctype.h>
  16. #include <linux/kd.h>
  17. #include <linux/mm.h>
  18. #include <linux/string.h>
  19. #include <linux/slab.h>
  20. #include <linux/poll.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/wait.h>
  26. #include <linux/bitops.h>
  27. #include <linux/delay.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/system.h>
  31. #include <linux/kbd_kern.h>
  32. #include <linux/vt_kern.h>
  33. #include <linux/selection.h>
  34. #include <linux/kmod.h>
  35. #include <linux/nsproxy.h>
  36. #include <linux/ratelimit.h>
  37. /*
  38. * This guards the refcounted line discipline lists. The lock
  39. * must be taken with irqs off because there are hangup path
  40. * callers who will do ldisc lookups and cannot sleep.
  41. */
  42. static DEFINE_SPINLOCK(tty_ldisc_lock);
  43. static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
  44. static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle);
  45. /* Line disc dispatch table */
  46. static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
  47. static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
  48. {
  49. if (ld)
  50. atomic_inc(&ld->users);
  51. return ld;
  52. }
  53. static void put_ldisc(struct tty_ldisc *ld)
  54. {
  55. unsigned long flags;
  56. if (WARN_ON_ONCE(!ld))
  57. return;
  58. /*
  59. * If this is the last user, free the ldisc, and
  60. * release the ldisc ops.
  61. *
  62. * We really want an "atomic_dec_and_lock_irqsave()",
  63. * but we don't have it, so this does it by hand.
  64. */
  65. local_irq_save(flags);
  66. if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
  67. struct tty_ldisc_ops *ldo = ld->ops;
  68. ldo->refcount--;
  69. module_put(ldo->owner);
  70. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  71. kfree(ld);
  72. return;
  73. }
  74. local_irq_restore(flags);
  75. wake_up(&tty_ldisc_idle);
  76. }
  77. /**
  78. * tty_register_ldisc - install a line discipline
  79. * @disc: ldisc number
  80. * @new_ldisc: pointer to the ldisc object
  81. *
  82. * Installs a new line discipline into the kernel. The discipline
  83. * is set up as unreferenced and then made available to the kernel
  84. * from this point onwards.
  85. *
  86. * Locking:
  87. * takes tty_ldisc_lock to guard against ldisc races
  88. */
  89. int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
  90. {
  91. unsigned long flags;
  92. int ret = 0;
  93. if (disc < N_TTY || disc >= NR_LDISCS)
  94. return -EINVAL;
  95. spin_lock_irqsave(&tty_ldisc_lock, flags);
  96. tty_ldiscs[disc] = new_ldisc;
  97. new_ldisc->num = disc;
  98. new_ldisc->refcount = 0;
  99. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  100. return ret;
  101. }
  102. EXPORT_SYMBOL(tty_register_ldisc);
  103. /**
  104. * tty_unregister_ldisc - unload a line discipline
  105. * @disc: ldisc number
  106. * @new_ldisc: pointer to the ldisc object
  107. *
  108. * Remove a line discipline from the kernel providing it is not
  109. * currently in use.
  110. *
  111. * Locking:
  112. * takes tty_ldisc_lock to guard against ldisc races
  113. */
  114. int tty_unregister_ldisc(int disc)
  115. {
  116. unsigned long flags;
  117. int ret = 0;
  118. if (disc < N_TTY || disc >= NR_LDISCS)
  119. return -EINVAL;
  120. spin_lock_irqsave(&tty_ldisc_lock, flags);
  121. if (tty_ldiscs[disc]->refcount)
  122. ret = -EBUSY;
  123. else
  124. tty_ldiscs[disc] = NULL;
  125. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  126. return ret;
  127. }
  128. EXPORT_SYMBOL(tty_unregister_ldisc);
  129. static struct tty_ldisc_ops *get_ldops(int disc)
  130. {
  131. unsigned long flags;
  132. struct tty_ldisc_ops *ldops, *ret;
  133. spin_lock_irqsave(&tty_ldisc_lock, flags);
  134. ret = ERR_PTR(-EINVAL);
  135. ldops = tty_ldiscs[disc];
  136. if (ldops) {
  137. ret = ERR_PTR(-EAGAIN);
  138. if (try_module_get(ldops->owner)) {
  139. ldops->refcount++;
  140. ret = ldops;
  141. }
  142. }
  143. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  144. return ret;
  145. }
  146. static void put_ldops(struct tty_ldisc_ops *ldops)
  147. {
  148. unsigned long flags;
  149. spin_lock_irqsave(&tty_ldisc_lock, flags);
  150. ldops->refcount--;
  151. module_put(ldops->owner);
  152. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  153. }
  154. /**
  155. * tty_ldisc_get - take a reference to an ldisc
  156. * @disc: ldisc number
  157. *
  158. * Takes a reference to a line discipline. Deals with refcounts and
  159. * module locking counts. Returns NULL if the discipline is not available.
  160. * Returns a pointer to the discipline and bumps the ref count if it is
  161. * available
  162. *
  163. * Locking:
  164. * takes tty_ldisc_lock to guard against ldisc races
  165. */
  166. static struct tty_ldisc *tty_ldisc_get(int disc)
  167. {
  168. struct tty_ldisc *ld;
  169. struct tty_ldisc_ops *ldops;
  170. if (disc < N_TTY || disc >= NR_LDISCS)
  171. return ERR_PTR(-EINVAL);
  172. /*
  173. * Get the ldisc ops - we may need to request them to be loaded
  174. * dynamically and try again.
  175. */
  176. ldops = get_ldops(disc);
  177. if (IS_ERR(ldops)) {
  178. request_module("tty-ldisc-%d", disc);
  179. ldops = get_ldops(disc);
  180. if (IS_ERR(ldops))
  181. return ERR_CAST(ldops);
  182. }
  183. ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
  184. if (ld == NULL) {
  185. put_ldops(ldops);
  186. return ERR_PTR(-ENOMEM);
  187. }
  188. ld->ops = ldops;
  189. atomic_set(&ld->users, 1);
  190. return ld;
  191. }
  192. static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
  193. {
  194. return (*pos < NR_LDISCS) ? pos : NULL;
  195. }
  196. static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  197. {
  198. (*pos)++;
  199. return (*pos < NR_LDISCS) ? pos : NULL;
  200. }
  201. static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
  202. {
  203. }
  204. static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
  205. {
  206. int i = *(loff_t *)v;
  207. struct tty_ldisc_ops *ldops;
  208. ldops = get_ldops(i);
  209. if (IS_ERR(ldops))
  210. return 0;
  211. seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
  212. put_ldops(ldops);
  213. return 0;
  214. }
  215. static const struct seq_operations tty_ldiscs_seq_ops = {
  216. .start = tty_ldiscs_seq_start,
  217. .next = tty_ldiscs_seq_next,
  218. .stop = tty_ldiscs_seq_stop,
  219. .show = tty_ldiscs_seq_show,
  220. };
  221. static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
  222. {
  223. return seq_open(file, &tty_ldiscs_seq_ops);
  224. }
  225. const struct file_operations tty_ldiscs_proc_fops = {
  226. .owner = THIS_MODULE,
  227. .open = proc_tty_ldiscs_open,
  228. .read = seq_read,
  229. .llseek = seq_lseek,
  230. .release = seq_release,
  231. };
  232. /**
  233. * tty_ldisc_assign - set ldisc on a tty
  234. * @tty: tty to assign
  235. * @ld: line discipline
  236. *
  237. * Install an instance of a line discipline into a tty structure. The
  238. * ldisc must have a reference count above zero to ensure it remains.
  239. * The tty instance refcount starts at zero.
  240. *
  241. * Locking:
  242. * Caller must hold references
  243. */
  244. static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
  245. {
  246. tty->ldisc = ld;
  247. }
  248. /**
  249. * tty_ldisc_try - internal helper
  250. * @tty: the tty
  251. *
  252. * Make a single attempt to grab and bump the refcount on
  253. * the tty ldisc. Return 0 on failure or 1 on success. This is
  254. * used to implement both the waiting and non waiting versions
  255. * of tty_ldisc_ref
  256. *
  257. * Locking: takes tty_ldisc_lock
  258. */
  259. static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
  260. {
  261. unsigned long flags;
  262. struct tty_ldisc *ld;
  263. spin_lock_irqsave(&tty_ldisc_lock, flags);
  264. ld = NULL;
  265. if (test_bit(TTY_LDISC, &tty->flags))
  266. ld = get_ldisc(tty->ldisc);
  267. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  268. return ld;
  269. }
  270. /**
  271. * tty_ldisc_ref_wait - wait for the tty ldisc
  272. * @tty: tty device
  273. *
  274. * Dereference the line discipline for the terminal and take a
  275. * reference to it. If the line discipline is in flux then
  276. * wait patiently until it changes.
  277. *
  278. * Note: Must not be called from an IRQ/timer context. The caller
  279. * must also be careful not to hold other locks that will deadlock
  280. * against a discipline change, such as an existing ldisc reference
  281. * (which we check for)
  282. *
  283. * Locking: call functions take tty_ldisc_lock
  284. */
  285. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  286. {
  287. struct tty_ldisc *ld;
  288. /* wait_event is a macro */
  289. wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
  290. return ld;
  291. }
  292. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  293. /**
  294. * tty_ldisc_ref - get the tty ldisc
  295. * @tty: tty device
  296. *
  297. * Dereference the line discipline for the terminal and take a
  298. * reference to it. If the line discipline is in flux then
  299. * return NULL. Can be called from IRQ and timer functions.
  300. *
  301. * Locking: called functions take tty_ldisc_lock
  302. */
  303. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  304. {
  305. return tty_ldisc_try(tty);
  306. }
  307. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  308. /**
  309. * tty_ldisc_deref - free a tty ldisc reference
  310. * @ld: reference to free up
  311. *
  312. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  313. * be called in IRQ context.
  314. *
  315. * Locking: takes tty_ldisc_lock
  316. */
  317. void tty_ldisc_deref(struct tty_ldisc *ld)
  318. {
  319. put_ldisc(ld);
  320. }
  321. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  322. static inline void tty_ldisc_put(struct tty_ldisc *ld)
  323. {
  324. put_ldisc(ld);
  325. }
  326. /**
  327. * tty_ldisc_enable - allow ldisc use
  328. * @tty: terminal to activate ldisc on
  329. *
  330. * Set the TTY_LDISC flag when the line discipline can be called
  331. * again. Do necessary wakeups for existing sleepers. Clear the LDISC
  332. * changing flag to indicate any ldisc change is now over.
  333. *
  334. * Note: nobody should set the TTY_LDISC bit except via this function.
  335. * Clearing directly is allowed.
  336. */
  337. void tty_ldisc_enable(struct tty_struct *tty)
  338. {
  339. set_bit(TTY_LDISC, &tty->flags);
  340. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  341. wake_up(&tty_ldisc_wait);
  342. }
  343. /**
  344. * tty_ldisc_flush - flush line discipline queue
  345. * @tty: tty
  346. *
  347. * Flush the line discipline queue (if any) for this tty. If there
  348. * is no line discipline active this is a no-op.
  349. */
  350. void tty_ldisc_flush(struct tty_struct *tty)
  351. {
  352. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  353. if (ld) {
  354. if (ld->ops->flush_buffer)
  355. ld->ops->flush_buffer(tty);
  356. tty_ldisc_deref(ld);
  357. }
  358. tty_buffer_flush(tty);
  359. }
  360. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  361. /**
  362. * tty_set_termios_ldisc - set ldisc field
  363. * @tty: tty structure
  364. * @num: line discipline number
  365. *
  366. * This is probably overkill for real world processors but
  367. * they are not on hot paths so a little discipline won't do
  368. * any harm.
  369. *
  370. * Locking: takes termios_mutex
  371. */
  372. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  373. {
  374. mutex_lock(&tty->termios_mutex);
  375. tty->termios->c_line = num;
  376. mutex_unlock(&tty->termios_mutex);
  377. }
  378. /**
  379. * tty_ldisc_open - open a line discipline
  380. * @tty: tty we are opening the ldisc on
  381. * @ld: discipline to open
  382. *
  383. * A helper opening method. Also a convenient debugging and check
  384. * point.
  385. *
  386. * Locking: always called with BTM already held.
  387. */
  388. static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
  389. {
  390. WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
  391. if (ld->ops->open) {
  392. int ret;
  393. /* BTM here locks versus a hangup event */
  394. WARN_ON(!tty_locked());
  395. ret = ld->ops->open(tty);
  396. if (ret)
  397. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  398. return ret;
  399. }
  400. return 0;
  401. }
  402. /**
  403. * tty_ldisc_close - close a line discipline
  404. * @tty: tty we are opening the ldisc on
  405. * @ld: discipline to close
  406. *
  407. * A helper close method. Also a convenient debugging and check
  408. * point.
  409. */
  410. static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
  411. {
  412. WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
  413. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  414. if (ld->ops->close)
  415. ld->ops->close(tty);
  416. }
  417. /**
  418. * tty_ldisc_restore - helper for tty ldisc change
  419. * @tty: tty to recover
  420. * @old: previous ldisc
  421. *
  422. * Restore the previous line discipline or N_TTY when a line discipline
  423. * change fails due to an open error
  424. */
  425. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  426. {
  427. char buf[64];
  428. struct tty_ldisc *new_ldisc;
  429. int r;
  430. /* There is an outstanding reference here so this is safe */
  431. old = tty_ldisc_get(old->ops->num);
  432. WARN_ON(IS_ERR(old));
  433. tty_ldisc_assign(tty, old);
  434. tty_set_termios_ldisc(tty, old->ops->num);
  435. if (tty_ldisc_open(tty, old) < 0) {
  436. tty_ldisc_put(old);
  437. /* This driver is always present */
  438. new_ldisc = tty_ldisc_get(N_TTY);
  439. if (IS_ERR(new_ldisc))
  440. panic("n_tty: get");
  441. tty_ldisc_assign(tty, new_ldisc);
  442. tty_set_termios_ldisc(tty, N_TTY);
  443. r = tty_ldisc_open(tty, new_ldisc);
  444. if (r < 0)
  445. panic("Couldn't open N_TTY ldisc for "
  446. "%s --- error %d.",
  447. tty_name(tty, buf), r);
  448. }
  449. }
  450. /**
  451. * tty_ldisc_halt - shut down the line discipline
  452. * @tty: tty device
  453. *
  454. * Shut down the line discipline and work queue for this tty device.
  455. * The TTY_LDISC flag being cleared ensures no further references can
  456. * be obtained while the delayed work queue halt ensures that no more
  457. * data is fed to the ldisc.
  458. *
  459. * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
  460. * in order to make sure any currently executing ldisc work is also
  461. * flushed.
  462. */
  463. static int tty_ldisc_halt(struct tty_struct *tty)
  464. {
  465. clear_bit(TTY_LDISC, &tty->flags);
  466. return cancel_work_sync(&tty->buf.work);
  467. }
  468. /**
  469. * tty_ldisc_flush_works - flush all works of a tty
  470. * @tty: tty device to flush works for
  471. *
  472. * Sync flush all works belonging to @tty.
  473. */
  474. static void tty_ldisc_flush_works(struct tty_struct *tty)
  475. {
  476. flush_work_sync(&tty->hangup_work);
  477. flush_work_sync(&tty->SAK_work);
  478. flush_work_sync(&tty->buf.work);
  479. }
  480. /**
  481. * tty_ldisc_wait_idle - wait for the ldisc to become idle
  482. * @tty: tty to wait for
  483. * @timeout: for how long to wait at most
  484. *
  485. * Wait for the line discipline to become idle. The discipline must
  486. * have been halted for this to guarantee it remains idle.
  487. */
  488. static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
  489. {
  490. long ret;
  491. ret = wait_event_timeout(tty_ldisc_idle,
  492. atomic_read(&tty->ldisc->users) == 1, timeout);
  493. if (ret < 0)
  494. return ret;
  495. return ret > 0 ? 0 : -EBUSY;
  496. }
  497. /**
  498. * tty_set_ldisc - set line discipline
  499. * @tty: the terminal to set
  500. * @ldisc: the line discipline
  501. *
  502. * Set the discipline of a tty line. Must be called from a process
  503. * context. The ldisc change logic has to protect itself against any
  504. * overlapping ldisc change (including on the other end of pty pairs),
  505. * the close of one side of a tty/pty pair, and eventually hangup.
  506. *
  507. * Locking: takes tty_ldisc_lock, termios_mutex
  508. */
  509. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  510. {
  511. int retval;
  512. struct tty_ldisc *o_ldisc, *new_ldisc;
  513. int work, o_work = 0;
  514. struct tty_struct *o_tty;
  515. new_ldisc = tty_ldisc_get(ldisc);
  516. if (IS_ERR(new_ldisc))
  517. return PTR_ERR(new_ldisc);
  518. tty_lock();
  519. /*
  520. * We need to look at the tty locking here for pty/tty pairs
  521. * when both sides try to change in parallel.
  522. */
  523. o_tty = tty->link; /* o_tty is the pty side or NULL */
  524. /*
  525. * Check the no-op case
  526. */
  527. if (tty->ldisc->ops->num == ldisc) {
  528. tty_unlock();
  529. tty_ldisc_put(new_ldisc);
  530. return 0;
  531. }
  532. tty_unlock();
  533. /*
  534. * Problem: What do we do if this blocks ?
  535. * We could deadlock here
  536. */
  537. tty_wait_until_sent(tty, 0);
  538. tty_lock();
  539. mutex_lock(&tty->ldisc_mutex);
  540. /*
  541. * We could be midstream of another ldisc change which has
  542. * dropped the lock during processing. If so we need to wait.
  543. */
  544. while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
  545. mutex_unlock(&tty->ldisc_mutex);
  546. tty_unlock();
  547. wait_event(tty_ldisc_wait,
  548. test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
  549. tty_lock();
  550. mutex_lock(&tty->ldisc_mutex);
  551. }
  552. set_bit(TTY_LDISC_CHANGING, &tty->flags);
  553. /*
  554. * No more input please, we are switching. The new ldisc
  555. * will update this value in the ldisc open function
  556. */
  557. tty->receive_room = 0;
  558. o_ldisc = tty->ldisc;
  559. tty_unlock();
  560. /*
  561. * Make sure we don't change while someone holds a
  562. * reference to the line discipline. The TTY_LDISC bit
  563. * prevents anyone taking a reference once it is clear.
  564. * We need the lock to avoid racing reference takers.
  565. *
  566. * We must clear the TTY_LDISC bit here to avoid a livelock
  567. * with a userspace app continually trying to use the tty in
  568. * parallel to the change and re-referencing the tty.
  569. */
  570. work = tty_ldisc_halt(tty);
  571. if (o_tty)
  572. o_work = tty_ldisc_halt(o_tty);
  573. /*
  574. * Wait for ->hangup_work and ->buf.work handlers to terminate.
  575. * We must drop the mutex here in case a hangup is also in process.
  576. */
  577. mutex_unlock(&tty->ldisc_mutex);
  578. tty_ldisc_flush_works(tty);
  579. retval = tty_ldisc_wait_idle(tty, 5 * HZ);
  580. tty_lock();
  581. mutex_lock(&tty->ldisc_mutex);
  582. /* handle wait idle failure locked */
  583. if (retval) {
  584. tty_ldisc_put(new_ldisc);
  585. goto enable;
  586. }
  587. if (test_bit(TTY_HUPPED, &tty->flags)) {
  588. /* We were raced by the hangup method. It will have stomped
  589. the ldisc data and closed the ldisc down */
  590. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  591. mutex_unlock(&tty->ldisc_mutex);
  592. tty_ldisc_put(new_ldisc);
  593. tty_unlock();
  594. return -EIO;
  595. }
  596. /* Shutdown the current discipline. */
  597. tty_ldisc_close(tty, o_ldisc);
  598. /* Now set up the new line discipline. */
  599. tty_ldisc_assign(tty, new_ldisc);
  600. tty_set_termios_ldisc(tty, ldisc);
  601. retval = tty_ldisc_open(tty, new_ldisc);
  602. if (retval < 0) {
  603. /* Back to the old one or N_TTY if we can't */
  604. tty_ldisc_put(new_ldisc);
  605. tty_ldisc_restore(tty, o_ldisc);
  606. }
  607. /* At this point we hold a reference to the new ldisc and a
  608. a reference to the old ldisc. If we ended up flipping back
  609. to the existing ldisc we have two references to it */
  610. if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
  611. tty->ops->set_ldisc(tty);
  612. tty_ldisc_put(o_ldisc);
  613. enable:
  614. /*
  615. * Allow ldisc referencing to occur again
  616. */
  617. tty_ldisc_enable(tty);
  618. if (o_tty)
  619. tty_ldisc_enable(o_tty);
  620. /* Restart the work queue in case no characters kick it off. Safe if
  621. already running */
  622. if (work)
  623. schedule_work(&tty->buf.work);
  624. if (o_work)
  625. schedule_work(&o_tty->buf.work);
  626. mutex_unlock(&tty->ldisc_mutex);
  627. tty_unlock();
  628. return retval;
  629. }
  630. /**
  631. * tty_reset_termios - reset terminal state
  632. * @tty: tty to reset
  633. *
  634. * Restore a terminal to the driver default state.
  635. */
  636. static void tty_reset_termios(struct tty_struct *tty)
  637. {
  638. mutex_lock(&tty->termios_mutex);
  639. *tty->termios = tty->driver->init_termios;
  640. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  641. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  642. mutex_unlock(&tty->termios_mutex);
  643. }
  644. /**
  645. * tty_ldisc_reinit - reinitialise the tty ldisc
  646. * @tty: tty to reinit
  647. * @ldisc: line discipline to reinitialize
  648. *
  649. * Switch the tty to a line discipline and leave the ldisc
  650. * state closed
  651. */
  652. static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
  653. {
  654. struct tty_ldisc *ld = tty_ldisc_get(ldisc);
  655. if (IS_ERR(ld))
  656. return -1;
  657. tty_ldisc_close(tty, tty->ldisc);
  658. tty_ldisc_put(tty->ldisc);
  659. tty->ldisc = NULL;
  660. /*
  661. * Switch the line discipline back
  662. */
  663. tty_ldisc_assign(tty, ld);
  664. tty_set_termios_ldisc(tty, ldisc);
  665. return 0;
  666. }
  667. /**
  668. * tty_ldisc_hangup - hangup ldisc reset
  669. * @tty: tty being hung up
  670. *
  671. * Some tty devices reset their termios when they receive a hangup
  672. * event. In that situation we must also switch back to N_TTY properly
  673. * before we reset the termios data.
  674. *
  675. * Locking: We can take the ldisc mutex as the rest of the code is
  676. * careful to allow for this.
  677. *
  678. * In the pty pair case this occurs in the close() path of the
  679. * tty itself so we must be careful about locking rules.
  680. */
  681. void tty_ldisc_hangup(struct tty_struct *tty)
  682. {
  683. struct tty_ldisc *ld;
  684. int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
  685. int err = 0;
  686. /*
  687. * FIXME! What are the locking issues here? This may me overdoing
  688. * things... This question is especially important now that we've
  689. * removed the irqlock.
  690. */
  691. ld = tty_ldisc_ref(tty);
  692. if (ld != NULL) {
  693. /* We may have no line discipline at this point */
  694. if (ld->ops->flush_buffer)
  695. ld->ops->flush_buffer(tty);
  696. tty_driver_flush_buffer(tty);
  697. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  698. ld->ops->write_wakeup)
  699. ld->ops->write_wakeup(tty);
  700. if (ld->ops->hangup)
  701. ld->ops->hangup(tty);
  702. tty_ldisc_deref(ld);
  703. }
  704. /*
  705. * FIXME: Once we trust the LDISC code better we can wait here for
  706. * ldisc completion and fix the driver call race
  707. */
  708. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  709. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  710. /*
  711. * Shutdown the current line discipline, and reset it to
  712. * N_TTY if need be.
  713. *
  714. * Avoid racing set_ldisc or tty_ldisc_release
  715. */
  716. mutex_lock(&tty->ldisc_mutex);
  717. /*
  718. * this is like tty_ldisc_halt, but we need to give up
  719. * the BTM before calling cancel_work_sync, which may
  720. * need to wait for another function taking the BTM
  721. */
  722. clear_bit(TTY_LDISC, &tty->flags);
  723. tty_unlock();
  724. cancel_work_sync(&tty->buf.work);
  725. mutex_unlock(&tty->ldisc_mutex);
  726. retry:
  727. tty_lock();
  728. mutex_lock(&tty->ldisc_mutex);
  729. /* At this point we have a closed ldisc and we want to
  730. reopen it. We could defer this to the next open but
  731. it means auditing a lot of other paths so this is
  732. a FIXME */
  733. if (tty->ldisc) { /* Not yet closed */
  734. if (atomic_read(&tty->ldisc->users) != 1) {
  735. char cur_n[TASK_COMM_LEN], tty_n[64];
  736. long timeout = 3 * HZ;
  737. tty_unlock();
  738. while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
  739. timeout = MAX_SCHEDULE_TIMEOUT;
  740. printk_ratelimited(KERN_WARNING
  741. "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
  742. __func__, get_task_comm(cur_n, current),
  743. tty_name(tty, tty_n));
  744. }
  745. mutex_unlock(&tty->ldisc_mutex);
  746. goto retry;
  747. }
  748. if (reset == 0) {
  749. if (!tty_ldisc_reinit(tty, tty->termios->c_line))
  750. err = tty_ldisc_open(tty, tty->ldisc);
  751. else
  752. err = 1;
  753. }
  754. /* If the re-open fails or we reset then go to N_TTY. The
  755. N_TTY open cannot fail */
  756. if (reset || err) {
  757. BUG_ON(tty_ldisc_reinit(tty, N_TTY));
  758. WARN_ON(tty_ldisc_open(tty, tty->ldisc));
  759. }
  760. tty_ldisc_enable(tty);
  761. }
  762. mutex_unlock(&tty->ldisc_mutex);
  763. if (reset)
  764. tty_reset_termios(tty);
  765. }
  766. /**
  767. * tty_ldisc_setup - open line discipline
  768. * @tty: tty being shut down
  769. * @o_tty: pair tty for pty/tty pairs
  770. *
  771. * Called during the initial open of a tty/pty pair in order to set up the
  772. * line disciplines and bind them to the tty. This has no locking issues
  773. * as the device isn't yet active.
  774. */
  775. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  776. {
  777. struct tty_ldisc *ld = tty->ldisc;
  778. int retval;
  779. retval = tty_ldisc_open(tty, ld);
  780. if (retval)
  781. return retval;
  782. if (o_tty) {
  783. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  784. if (retval) {
  785. tty_ldisc_close(tty, ld);
  786. return retval;
  787. }
  788. tty_ldisc_enable(o_tty);
  789. }
  790. tty_ldisc_enable(tty);
  791. return 0;
  792. }
  793. /**
  794. * tty_ldisc_release - release line discipline
  795. * @tty: tty being shut down
  796. * @o_tty: pair tty for pty/tty pairs
  797. *
  798. * Called during the final close of a tty/pty pair in order to shut down
  799. * the line discpline layer. On exit the ldisc assigned is N_TTY and the
  800. * ldisc has not been opened.
  801. */
  802. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  803. {
  804. /*
  805. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  806. * kill any delayed work. As this is the final close it does not
  807. * race with the set_ldisc code path.
  808. */
  809. tty_unlock();
  810. tty_ldisc_halt(tty);
  811. tty_ldisc_flush_works(tty);
  812. tty_lock();
  813. mutex_lock(&tty->ldisc_mutex);
  814. /*
  815. * Now kill off the ldisc
  816. */
  817. tty_ldisc_close(tty, tty->ldisc);
  818. tty_ldisc_put(tty->ldisc);
  819. /* Force an oops if we mess this up */
  820. tty->ldisc = NULL;
  821. /* Ensure the next open requests the N_TTY ldisc */
  822. tty_set_termios_ldisc(tty, N_TTY);
  823. mutex_unlock(&tty->ldisc_mutex);
  824. /* This will need doing differently if we need to lock */
  825. if (o_tty)
  826. tty_ldisc_release(o_tty, NULL);
  827. /* And the memory resources remaining (buffers, termios) will be
  828. disposed of when the kref hits zero */
  829. }
  830. /**
  831. * tty_ldisc_init - ldisc setup for new tty
  832. * @tty: tty being allocated
  833. *
  834. * Set up the line discipline objects for a newly allocated tty. Note that
  835. * the tty structure is not completely set up when this call is made.
  836. */
  837. void tty_ldisc_init(struct tty_struct *tty)
  838. {
  839. struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
  840. if (IS_ERR(ld))
  841. panic("n_tty: init_tty");
  842. tty_ldisc_assign(tty, ld);
  843. }
  844. /**
  845. * tty_ldisc_init - ldisc cleanup for new tty
  846. * @tty: tty that was allocated recently
  847. *
  848. * The tty structure must not becompletely set up (tty_ldisc_setup) when
  849. * this call is made.
  850. */
  851. void tty_ldisc_deinit(struct tty_struct *tty)
  852. {
  853. put_ldisc(tty->ldisc);
  854. tty_ldisc_assign(tty, NULL);
  855. }
  856. void tty_ldisc_begin(void)
  857. {
  858. /* Setup the default TTY line discipline. */
  859. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  860. }