tty_ldisc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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_set_termios_ldisc - set ldisc field
  346. * @tty: tty structure
  347. * @num: line discipline number
  348. *
  349. * This is probably overkill for real world processors but
  350. * they are not on hot paths so a little discipline won't do
  351. * any harm.
  352. *
  353. * Locking: takes termios_mutex
  354. */
  355. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  356. {
  357. mutex_lock(&tty->termios_mutex);
  358. tty->termios->c_line = num;
  359. mutex_unlock(&tty->termios_mutex);
  360. }
  361. /**
  362. * tty_ldisc_open - open a line discipline
  363. * @tty: tty we are opening the ldisc on
  364. * @ld: discipline to open
  365. *
  366. * A helper opening method. Also a convenient debugging and check
  367. * point.
  368. */
  369. static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
  370. {
  371. WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
  372. if (ld->ops->open)
  373. return ld->ops->open(tty);
  374. return 0;
  375. }
  376. /**
  377. * tty_ldisc_close - close a line discipline
  378. * @tty: tty we are opening the ldisc on
  379. * @ld: discipline to close
  380. *
  381. * A helper close method. Also a convenient debugging and check
  382. * point.
  383. */
  384. static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
  385. {
  386. WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
  387. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  388. if (ld->ops->close)
  389. ld->ops->close(tty);
  390. }
  391. /**
  392. * tty_ldisc_restore - helper for tty ldisc change
  393. * @tty: tty to recover
  394. * @old: previous ldisc
  395. *
  396. * Restore the previous line discipline or N_TTY when a line discipline
  397. * change fails due to an open error
  398. */
  399. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  400. {
  401. char buf[64];
  402. struct tty_ldisc *new_ldisc;
  403. int r;
  404. /* There is an outstanding reference here so this is safe */
  405. old = tty_ldisc_get(old->ops->num);
  406. WARN_ON(IS_ERR(old));
  407. tty_ldisc_assign(tty, old);
  408. tty_set_termios_ldisc(tty, old->ops->num);
  409. if (tty_ldisc_open(tty, old) < 0) {
  410. tty_ldisc_put(old);
  411. /* This driver is always present */
  412. new_ldisc =tty_ldisc_get(N_TTY);
  413. if (IS_ERR(new_ldisc))
  414. panic("n_tty: get");
  415. tty_ldisc_assign(tty, new_ldisc);
  416. tty_set_termios_ldisc(tty, N_TTY);
  417. r = tty_ldisc_open(tty, new_ldisc);
  418. if (r < 0)
  419. panic("Couldn't open N_TTY ldisc for "
  420. "%s --- error %d.",
  421. tty_name(tty, buf), r);
  422. }
  423. }
  424. /**
  425. * tty_ldisc_halt - shut down the line discipline
  426. * @tty: tty device
  427. *
  428. * Shut down the line discipline and work queue for this tty device.
  429. * The TTY_LDISC flag being cleared ensures no further references can
  430. * be obtained while the delayed work queue halt ensures that no more
  431. * data is fed to the ldisc.
  432. *
  433. * In order to wait for any existing references to complete see
  434. * tty_ldisc_wait_idle.
  435. */
  436. static int tty_ldisc_halt(struct tty_struct *tty)
  437. {
  438. clear_bit(TTY_LDISC, &tty->flags);
  439. return cancel_delayed_work(&tty->buf.work);
  440. }
  441. /**
  442. * tty_ldisc_wait_idle - wait for the ldisc to become idle
  443. * @tty: tty to wait for
  444. *
  445. * Wait for the line discipline to become idle. The discipline must
  446. * have been halted for this to guarantee it remains idle.
  447. *
  448. * tty_ldisc_lock protects the ref counts currently.
  449. */
  450. static int tty_ldisc_wait_idle(struct tty_struct *tty)
  451. {
  452. unsigned long flags;
  453. spin_lock_irqsave(&tty_ldisc_lock, flags);
  454. while (tty->ldisc->refcount) {
  455. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  456. if (wait_event_timeout(tty_ldisc_wait,
  457. tty->ldisc->refcount == 0, 5 * HZ) == 0)
  458. return -EBUSY;
  459. spin_lock_irqsave(&tty_ldisc_lock, flags);
  460. }
  461. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  462. return 0;
  463. }
  464. /**
  465. * tty_set_ldisc - set line discipline
  466. * @tty: the terminal to set
  467. * @ldisc: the line discipline
  468. *
  469. * Set the discipline of a tty line. Must be called from a process
  470. * context. The ldisc change logic has to protect itself against any
  471. * overlapping ldisc change (including on the other end of pty pairs),
  472. * the close of one side of a tty/pty pair, and eventually hangup.
  473. *
  474. * Locking: takes tty_ldisc_lock, termios_mutex
  475. */
  476. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  477. {
  478. int retval;
  479. struct tty_ldisc *o_ldisc, *new_ldisc;
  480. int work, o_work = 0;
  481. struct tty_struct *o_tty;
  482. new_ldisc = tty_ldisc_get(ldisc);
  483. if (IS_ERR(new_ldisc))
  484. return PTR_ERR(new_ldisc);
  485. /*
  486. * We need to look at the tty locking here for pty/tty pairs
  487. * when both sides try to change in parallel.
  488. */
  489. o_tty = tty->link; /* o_tty is the pty side or NULL */
  490. /*
  491. * Check the no-op case
  492. */
  493. if (tty->ldisc->ops->num == ldisc) {
  494. tty_ldisc_put(new_ldisc);
  495. return 0;
  496. }
  497. /*
  498. * Problem: What do we do if this blocks ?
  499. * We could deadlock here
  500. */
  501. tty_wait_until_sent(tty, 0);
  502. mutex_lock(&tty->ldisc_mutex);
  503. /*
  504. * We could be midstream of another ldisc change which has
  505. * dropped the lock during processing. If so we need to wait.
  506. */
  507. while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
  508. mutex_unlock(&tty->ldisc_mutex);
  509. wait_event(tty_ldisc_wait,
  510. test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
  511. mutex_lock(&tty->ldisc_mutex);
  512. }
  513. set_bit(TTY_LDISC_CHANGING, &tty->flags);
  514. /*
  515. * No more input please, we are switching. The new ldisc
  516. * will update this value in the ldisc open function
  517. */
  518. tty->receive_room = 0;
  519. o_ldisc = tty->ldisc;
  520. /*
  521. * Make sure we don't change while someone holds a
  522. * reference to the line discipline. The TTY_LDISC bit
  523. * prevents anyone taking a reference once it is clear.
  524. * We need the lock to avoid racing reference takers.
  525. *
  526. * We must clear the TTY_LDISC bit here to avoid a livelock
  527. * with a userspace app continually trying to use the tty in
  528. * parallel to the change and re-referencing the tty.
  529. */
  530. work = tty_ldisc_halt(tty);
  531. if (o_tty)
  532. o_work = tty_ldisc_halt(o_tty);
  533. /*
  534. * Wait for ->hangup_work and ->buf.work handlers to terminate.
  535. * We must drop the mutex here in case a hangup is also in process.
  536. */
  537. mutex_unlock(&tty->ldisc_mutex);
  538. flush_scheduled_work();
  539. /* Let any existing reference holders finish */
  540. retval = tty_ldisc_wait_idle(tty);
  541. if (retval < 0) {
  542. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  543. tty_ldisc_put(new_ldisc);
  544. return retval;
  545. }
  546. mutex_lock(&tty->ldisc_mutex);
  547. if (test_bit(TTY_HUPPED, &tty->flags)) {
  548. /* We were raced by the hangup method. It will have stomped
  549. the ldisc data and closed the ldisc down */
  550. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  551. mutex_unlock(&tty->ldisc_mutex);
  552. tty_ldisc_put(new_ldisc);
  553. return -EIO;
  554. }
  555. /* Shutdown the current discipline. */
  556. tty_ldisc_close(tty, o_ldisc);
  557. /* Now set up the new line discipline. */
  558. tty_ldisc_assign(tty, new_ldisc);
  559. tty_set_termios_ldisc(tty, ldisc);
  560. retval = tty_ldisc_open(tty, new_ldisc);
  561. if (retval < 0) {
  562. /* Back to the old one or N_TTY if we can't */
  563. tty_ldisc_put(new_ldisc);
  564. tty_ldisc_restore(tty, o_ldisc);
  565. }
  566. /* At this point we hold a reference to the new ldisc and a
  567. a reference to the old ldisc. If we ended up flipping back
  568. to the existing ldisc we have two references to it */
  569. if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
  570. tty->ops->set_ldisc(tty);
  571. tty_ldisc_put(o_ldisc);
  572. /*
  573. * Allow ldisc referencing to occur again
  574. */
  575. tty_ldisc_enable(tty);
  576. if (o_tty)
  577. tty_ldisc_enable(o_tty);
  578. /* Restart the work queue in case no characters kick it off. Safe if
  579. already running */
  580. if (work)
  581. schedule_delayed_work(&tty->buf.work, 1);
  582. if (o_work)
  583. schedule_delayed_work(&o_tty->buf.work, 1);
  584. mutex_unlock(&tty->ldisc_mutex);
  585. return retval;
  586. }
  587. /**
  588. * tty_reset_termios - reset terminal state
  589. * @tty: tty to reset
  590. *
  591. * Restore a terminal to the driver default state.
  592. */
  593. static void tty_reset_termios(struct tty_struct *tty)
  594. {
  595. mutex_lock(&tty->termios_mutex);
  596. *tty->termios = tty->driver->init_termios;
  597. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  598. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  599. mutex_unlock(&tty->termios_mutex);
  600. }
  601. /**
  602. * tty_ldisc_reinit - reinitialise the tty ldisc
  603. * @tty: tty to reinit
  604. *
  605. * Switch the tty back to N_TTY line discipline and leave the
  606. * ldisc state closed
  607. */
  608. static void tty_ldisc_reinit(struct tty_struct *tty)
  609. {
  610. struct tty_ldisc *ld;
  611. tty_ldisc_close(tty, tty->ldisc);
  612. tty_ldisc_put(tty->ldisc);
  613. tty->ldisc = NULL;
  614. /*
  615. * Switch the line discipline back
  616. */
  617. ld = tty_ldisc_get(N_TTY);
  618. BUG_ON(IS_ERR(ld));
  619. tty_ldisc_assign(tty, ld);
  620. tty_set_termios_ldisc(tty, N_TTY);
  621. }
  622. /**
  623. * tty_ldisc_hangup - hangup ldisc reset
  624. * @tty: tty being hung up
  625. *
  626. * Some tty devices reset their termios when they receive a hangup
  627. * event. In that situation we must also switch back to N_TTY properly
  628. * before we reset the termios data.
  629. *
  630. * Locking: We can take the ldisc mutex as the rest of the code is
  631. * careful to allow for this.
  632. *
  633. * In the pty pair case this occurs in the close() path of the
  634. * tty itself so we must be careful about locking rules.
  635. */
  636. void tty_ldisc_hangup(struct tty_struct *tty)
  637. {
  638. struct tty_ldisc *ld;
  639. /*
  640. * FIXME! What are the locking issues here? This may me overdoing
  641. * things... This question is especially important now that we've
  642. * removed the irqlock.
  643. */
  644. ld = tty_ldisc_ref(tty);
  645. if (ld != NULL) {
  646. /* We may have no line discipline at this point */
  647. if (ld->ops->flush_buffer)
  648. ld->ops->flush_buffer(tty);
  649. tty_driver_flush_buffer(tty);
  650. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  651. ld->ops->write_wakeup)
  652. ld->ops->write_wakeup(tty);
  653. if (ld->ops->hangup)
  654. ld->ops->hangup(tty);
  655. tty_ldisc_deref(ld);
  656. }
  657. /*
  658. * FIXME: Once we trust the LDISC code better we can wait here for
  659. * ldisc completion and fix the driver call race
  660. */
  661. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  662. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  663. /*
  664. * Shutdown the current line discipline, and reset it to
  665. * N_TTY.
  666. */
  667. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  668. /* Avoid racing set_ldisc */
  669. mutex_lock(&tty->ldisc_mutex);
  670. /* Switch back to N_TTY */
  671. tty_ldisc_reinit(tty);
  672. /* At this point we have a closed ldisc and we want to
  673. reopen it. We could defer this to the next open but
  674. it means auditing a lot of other paths so this is a FIXME */
  675. WARN_ON(tty_ldisc_open(tty, tty->ldisc));
  676. tty_ldisc_enable(tty);
  677. mutex_unlock(&tty->ldisc_mutex);
  678. tty_reset_termios(tty);
  679. }
  680. }
  681. /**
  682. * tty_ldisc_setup - open line discipline
  683. * @tty: tty being shut down
  684. * @o_tty: pair tty for pty/tty pairs
  685. *
  686. * Called during the initial open of a tty/pty pair in order to set up the
  687. * line disciplines and bind them to the tty. This has no locking issues
  688. * as the device isn't yet active.
  689. */
  690. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  691. {
  692. struct tty_ldisc *ld = tty->ldisc;
  693. int retval;
  694. retval = tty_ldisc_open(tty, ld);
  695. if (retval)
  696. return retval;
  697. if (o_tty) {
  698. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  699. if (retval) {
  700. tty_ldisc_close(tty, ld);
  701. return retval;
  702. }
  703. tty_ldisc_enable(o_tty);
  704. }
  705. tty_ldisc_enable(tty);
  706. return 0;
  707. }
  708. /**
  709. * tty_ldisc_release - release line discipline
  710. * @tty: tty being shut down
  711. * @o_tty: pair tty for pty/tty pairs
  712. *
  713. * Called during the final close of a tty/pty pair in order to shut down the
  714. * line discpline layer. On exit the ldisc assigned is N_TTY and the
  715. * ldisc has not been opened.
  716. */
  717. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  718. {
  719. /*
  720. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  721. * kill any delayed work. As this is the final close it does not
  722. * race with the set_ldisc code path.
  723. */
  724. tty_ldisc_halt(tty);
  725. flush_scheduled_work();
  726. /*
  727. * Wait for any short term users (we know they are just driver
  728. * side waiters as the file is closing so user count on the file
  729. * side is zero.
  730. */
  731. tty_ldisc_wait_idle(tty);
  732. /*
  733. * Shutdown the current line discipline, and reset it to N_TTY.
  734. *
  735. * FIXME: this MUST get fixed for the new reflocking
  736. */
  737. tty_ldisc_reinit(tty);
  738. /* This will need doing differently if we need to lock */
  739. if (o_tty)
  740. tty_ldisc_release(o_tty, NULL);
  741. }
  742. /**
  743. * tty_ldisc_init - ldisc setup for new tty
  744. * @tty: tty being allocated
  745. *
  746. * Set up the line discipline objects for a newly allocated tty. Note that
  747. * the tty structure is not completely set up when this call is made.
  748. */
  749. void tty_ldisc_init(struct tty_struct *tty)
  750. {
  751. struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
  752. if (IS_ERR(ld))
  753. panic("n_tty: init_tty");
  754. tty_ldisc_assign(tty, ld);
  755. }
  756. void tty_ldisc_begin(void)
  757. {
  758. /* Setup the default TTY line discipline. */
  759. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  760. }