tty_ldisc.c 22 KB

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