tty_ldisc.c 22 KB

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