tty_ldisc.c 21 KB

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