tty_ldisc.c 22 KB

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