tty_ldisc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. atomic_set(&ld->users, 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(atomic_read(&ld->users));
  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. atomic_inc(&ld->users);
  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(atomic_read(&tty->ldisc->users) == 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 (atomic_read(&ld->users) == 0)
  321. printk(KERN_ERR "tty_ldisc_deref: no references.\n");
  322. else if (atomic_dec_and_test(&ld->users))
  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 (atomic_read(&tty->ldisc->users)) {
  473. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  474. if (wait_event_timeout(tty_ldisc_wait,
  475. atomic_read(&tty->ldisc->users) == 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 or tty_ldisc_release */
  687. mutex_lock(&tty->ldisc_mutex);
  688. if (tty->ldisc) { /* Not yet closed */
  689. /* Switch back to N_TTY */
  690. tty_ldisc_halt(tty);
  691. tty_ldisc_wait_idle(tty);
  692. tty_ldisc_reinit(tty);
  693. /* At this point we have a closed ldisc and we want to
  694. reopen it. We could defer this to the next open but
  695. it means auditing a lot of other paths so this is
  696. a FIXME */
  697. WARN_ON(tty_ldisc_open(tty, tty->ldisc));
  698. tty_ldisc_enable(tty);
  699. }
  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. mutex_lock(&tty->ldisc_mutex);
  756. /*
  757. * Now kill off the ldisc
  758. */
  759. tty_ldisc_close(tty, tty->ldisc);
  760. tty_ldisc_put(tty->ldisc);
  761. /* Force an oops if we mess this up */
  762. tty->ldisc = NULL;
  763. /* Ensure the next open requests the N_TTY ldisc */
  764. tty_set_termios_ldisc(tty, N_TTY);
  765. mutex_unlock(&tty->ldisc_mutex);
  766. /* This will need doing differently if we need to lock */
  767. if (o_tty)
  768. tty_ldisc_release(o_tty, NULL);
  769. /* And the memory resources remaining (buffers, termios) will be
  770. disposed of when the kref hits zero */
  771. }
  772. /**
  773. * tty_ldisc_init - ldisc setup for new tty
  774. * @tty: tty being allocated
  775. *
  776. * Set up the line discipline objects for a newly allocated tty. Note that
  777. * the tty structure is not completely set up when this call is made.
  778. */
  779. void tty_ldisc_init(struct tty_struct *tty)
  780. {
  781. struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
  782. if (IS_ERR(ld))
  783. panic("n_tty: init_tty");
  784. tty_ldisc_assign(tty, ld);
  785. }
  786. void tty_ldisc_begin(void)
  787. {
  788. /* Setup the default TTY line discipline. */
  789. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  790. }