tty_ldisc.c 23 KB

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