tty_ldisc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. * @ld: tty ldisc structure to complete
  102. *
  103. * Attempt to open and lock a line discipline into place. Return
  104. * the line discipline refcounted and assigned in ld. On an error
  105. * report the error code back
  106. */
  107. static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld)
  108. {
  109. unsigned long flags;
  110. struct tty_ldisc_ops *ldops;
  111. int err = -EINVAL;
  112. spin_lock_irqsave(&tty_ldisc_lock, flags);
  113. ld->ops = NULL;
  114. ldops = tty_ldiscs[disc];
  115. /* Check the entry is defined */
  116. if (ldops) {
  117. /* If the module is being unloaded we can't use it */
  118. if (!try_module_get(ldops->owner))
  119. err = -EAGAIN;
  120. else {
  121. /* lock it */
  122. ldops->refcount++;
  123. ld->ops = ldops;
  124. err = 0;
  125. }
  126. }
  127. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  128. return err;
  129. }
  130. /**
  131. * tty_ldisc_get - take a reference to an ldisc
  132. * @disc: ldisc number
  133. * @ld: tty line discipline structure to use
  134. *
  135. * Takes a reference to a line discipline. Deals with refcounts and
  136. * module locking counts. Returns NULL if the discipline is not available.
  137. * Returns a pointer to the discipline and bumps the ref count if it is
  138. * available
  139. *
  140. * Locking:
  141. * takes tty_ldisc_lock to guard against ldisc races
  142. */
  143. static int tty_ldisc_get(int disc, struct tty_ldisc *ld)
  144. {
  145. int err;
  146. if (disc < N_TTY || disc >= NR_LDISCS)
  147. return -EINVAL;
  148. err = tty_ldisc_try_get(disc, ld);
  149. if (err < 0) {
  150. request_module("tty-ldisc-%d", disc);
  151. err = tty_ldisc_try_get(disc, ld);
  152. }
  153. return err;
  154. }
  155. /**
  156. * tty_ldisc_put - drop ldisc reference
  157. * @disc: ldisc number
  158. *
  159. * Drop a reference to a line discipline. Manage refcounts and
  160. * module usage counts
  161. *
  162. * Locking:
  163. * takes tty_ldisc_lock to guard against ldisc races
  164. */
  165. static void tty_ldisc_put(struct tty_ldisc_ops *ld)
  166. {
  167. unsigned long flags;
  168. int disc = ld->num;
  169. BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
  170. spin_lock_irqsave(&tty_ldisc_lock, flags);
  171. ld = tty_ldiscs[disc];
  172. BUG_ON(ld->refcount == 0);
  173. ld->refcount--;
  174. module_put(ld->owner);
  175. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  176. }
  177. static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
  178. {
  179. return (*pos < NR_LDISCS) ? pos : NULL;
  180. }
  181. static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  182. {
  183. (*pos)++;
  184. return (*pos < NR_LDISCS) ? pos : NULL;
  185. }
  186. static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
  187. {
  188. }
  189. static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
  190. {
  191. int i = *(loff_t *)v;
  192. struct tty_ldisc ld;
  193. if (tty_ldisc_get(i, &ld) < 0)
  194. return 0;
  195. seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i);
  196. tty_ldisc_put(ld.ops);
  197. return 0;
  198. }
  199. static const struct seq_operations tty_ldiscs_seq_ops = {
  200. .start = tty_ldiscs_seq_start,
  201. .next = tty_ldiscs_seq_next,
  202. .stop = tty_ldiscs_seq_stop,
  203. .show = tty_ldiscs_seq_show,
  204. };
  205. static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
  206. {
  207. return seq_open(file, &tty_ldiscs_seq_ops);
  208. }
  209. const struct file_operations tty_ldiscs_proc_fops = {
  210. .owner = THIS_MODULE,
  211. .open = proc_tty_ldiscs_open,
  212. .read = seq_read,
  213. .llseek = seq_lseek,
  214. .release = seq_release,
  215. };
  216. /**
  217. * tty_ldisc_assign - set ldisc on a tty
  218. * @tty: tty to assign
  219. * @ld: line discipline
  220. *
  221. * Install an instance of a line discipline into a tty structure. The
  222. * ldisc must have a reference count above zero to ensure it remains/
  223. * The tty instance refcount starts at zero.
  224. *
  225. * Locking:
  226. * Caller must hold references
  227. */
  228. static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
  229. {
  230. ld->refcount = 0;
  231. tty->ldisc = *ld;
  232. }
  233. /**
  234. * tty_ldisc_try - internal helper
  235. * @tty: the tty
  236. *
  237. * Make a single attempt to grab and bump the refcount on
  238. * the tty ldisc. Return 0 on failure or 1 on success. This is
  239. * used to implement both the waiting and non waiting versions
  240. * of tty_ldisc_ref
  241. *
  242. * Locking: takes tty_ldisc_lock
  243. */
  244. static int tty_ldisc_try(struct tty_struct *tty)
  245. {
  246. unsigned long flags;
  247. struct tty_ldisc *ld;
  248. int ret = 0;
  249. spin_lock_irqsave(&tty_ldisc_lock, flags);
  250. ld = &tty->ldisc;
  251. if (test_bit(TTY_LDISC, &tty->flags)) {
  252. ld->refcount++;
  253. ret = 1;
  254. }
  255. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  256. return ret;
  257. }
  258. /**
  259. * tty_ldisc_ref_wait - wait for the tty ldisc
  260. * @tty: tty device
  261. *
  262. * Dereference the line discipline for the terminal and take a
  263. * reference to it. If the line discipline is in flux then
  264. * wait patiently until it changes.
  265. *
  266. * Note: Must not be called from an IRQ/timer context. The caller
  267. * must also be careful not to hold other locks that will deadlock
  268. * against a discipline change, such as an existing ldisc reference
  269. * (which we check for)
  270. *
  271. * Locking: call functions take tty_ldisc_lock
  272. */
  273. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  274. {
  275. /* wait_event is a macro */
  276. wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
  277. WARN_ON(tty->ldisc.refcount == 0);
  278. return &tty->ldisc;
  279. }
  280. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  281. /**
  282. * tty_ldisc_ref - get the tty ldisc
  283. * @tty: tty device
  284. *
  285. * Dereference the line discipline for the terminal and take a
  286. * reference to it. If the line discipline is in flux then
  287. * return NULL. Can be called from IRQ and timer functions.
  288. *
  289. * Locking: called functions take tty_ldisc_lock
  290. */
  291. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  292. {
  293. if (tty_ldisc_try(tty))
  294. return &tty->ldisc;
  295. return NULL;
  296. }
  297. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  298. /**
  299. * tty_ldisc_deref - free a tty ldisc reference
  300. * @ld: reference to free up
  301. *
  302. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  303. * be called in IRQ context.
  304. *
  305. * Locking: takes tty_ldisc_lock
  306. */
  307. void tty_ldisc_deref(struct tty_ldisc *ld)
  308. {
  309. unsigned long flags;
  310. BUG_ON(ld == NULL);
  311. spin_lock_irqsave(&tty_ldisc_lock, flags);
  312. if (ld->refcount == 0)
  313. printk(KERN_ERR "tty_ldisc_deref: no references.\n");
  314. else
  315. ld->refcount--;
  316. if (ld->refcount == 0)
  317. wake_up(&tty_ldisc_wait);
  318. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  319. }
  320. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  321. /**
  322. * tty_ldisc_enable - allow ldisc use
  323. * @tty: terminal to activate ldisc on
  324. *
  325. * Set the TTY_LDISC flag when the line discipline can be called
  326. * again. Do necessary wakeups for existing sleepers. Clear the LDISC
  327. * changing flag to indicate any ldisc change is now over.
  328. *
  329. * Note: nobody should set the TTY_LDISC bit except via this function.
  330. * Clearing directly is allowed.
  331. */
  332. void tty_ldisc_enable(struct tty_struct *tty)
  333. {
  334. set_bit(TTY_LDISC, &tty->flags);
  335. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  336. wake_up(&tty_ldisc_wait);
  337. }
  338. /**
  339. * tty_set_termios_ldisc - set ldisc field
  340. * @tty: tty structure
  341. * @num: line discipline number
  342. *
  343. * This is probably overkill for real world processors but
  344. * they are not on hot paths so a little discipline won't do
  345. * any harm.
  346. *
  347. * Locking: takes termios_mutex
  348. */
  349. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  350. {
  351. mutex_lock(&tty->termios_mutex);
  352. tty->termios->c_line = num;
  353. mutex_unlock(&tty->termios_mutex);
  354. }
  355. /**
  356. * tty_ldisc_restore - helper for tty ldisc change
  357. * @tty: tty to recover
  358. * @old: previous ldisc
  359. *
  360. * Restore the previous line discipline or N_TTY when a line discipline
  361. * change fails due to an open error
  362. */
  363. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  364. {
  365. char buf[64];
  366. struct tty_ldisc new_ldisc;
  367. /* There is an outstanding reference here so this is safe */
  368. tty_ldisc_get(old->ops->num, old);
  369. tty_ldisc_assign(tty, old);
  370. tty_set_termios_ldisc(tty, old->ops->num);
  371. if (old->ops->open && (old->ops->open(tty) < 0)) {
  372. tty_ldisc_put(old->ops);
  373. /* This driver is always present */
  374. if (tty_ldisc_get(N_TTY, &new_ldisc) < 0)
  375. panic("n_tty: get");
  376. tty_ldisc_assign(tty, &new_ldisc);
  377. tty_set_termios_ldisc(tty, N_TTY);
  378. if (new_ldisc.ops->open) {
  379. int r = new_ldisc.ops->open(tty);
  380. if (r < 0)
  381. panic("Couldn't open N_TTY ldisc for "
  382. "%s --- error %d.",
  383. tty_name(tty, buf), r);
  384. }
  385. }
  386. }
  387. /**
  388. * tty_set_ldisc - set line discipline
  389. * @tty: the terminal to set
  390. * @ldisc: the line discipline
  391. *
  392. * Set the discipline of a tty line. Must be called from a process
  393. * context.
  394. *
  395. * Locking: takes tty_ldisc_lock.
  396. * called functions take termios_mutex
  397. */
  398. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  399. {
  400. int retval;
  401. struct tty_ldisc o_ldisc, new_ldisc;
  402. int work;
  403. unsigned long flags;
  404. struct tty_struct *o_tty;
  405. restart:
  406. /* This is a bit ugly for now but means we can break the 'ldisc
  407. is part of the tty struct' assumption later */
  408. retval = tty_ldisc_get(ldisc, &new_ldisc);
  409. if (retval)
  410. return retval;
  411. /*
  412. * Problem: What do we do if this blocks ?
  413. */
  414. tty_wait_until_sent(tty, 0);
  415. if (tty->ldisc.ops->num == ldisc) {
  416. tty_ldisc_put(new_ldisc.ops);
  417. return 0;
  418. }
  419. /*
  420. * No more input please, we are switching. The new ldisc
  421. * will update this value in the ldisc open function
  422. */
  423. tty->receive_room = 0;
  424. o_ldisc = tty->ldisc;
  425. o_tty = tty->link;
  426. /*
  427. * Make sure we don't change while someone holds a
  428. * reference to the line discipline. The TTY_LDISC bit
  429. * prevents anyone taking a reference once it is clear.
  430. * We need the lock to avoid racing reference takers.
  431. *
  432. * We must clear the TTY_LDISC bit here to avoid a livelock
  433. * with a userspace app continually trying to use the tty in
  434. * parallel to the change and re-referencing the tty.
  435. */
  436. clear_bit(TTY_LDISC, &tty->flags);
  437. if (o_tty)
  438. clear_bit(TTY_LDISC, &o_tty->flags);
  439. spin_lock_irqsave(&tty_ldisc_lock, flags);
  440. if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
  441. if (tty->ldisc.refcount) {
  442. /* Free the new ldisc we grabbed. Must drop the lock
  443. first. */
  444. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  445. tty_ldisc_put(o_ldisc.ops);
  446. /*
  447. * There are several reasons we may be busy, including
  448. * random momentary I/O traffic. We must therefore
  449. * retry. We could distinguish between blocking ops
  450. * and retries if we made tty_ldisc_wait() smarter.
  451. * That is up for discussion.
  452. */
  453. if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
  454. return -ERESTARTSYS;
  455. goto restart;
  456. }
  457. if (o_tty && o_tty->ldisc.refcount) {
  458. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  459. tty_ldisc_put(o_tty->ldisc.ops);
  460. if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
  461. return -ERESTARTSYS;
  462. goto restart;
  463. }
  464. }
  465. /*
  466. * If the TTY_LDISC bit is set, then we are racing against
  467. * another ldisc change
  468. */
  469. if (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
  470. struct tty_ldisc *ld;
  471. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  472. tty_ldisc_put(new_ldisc.ops);
  473. ld = tty_ldisc_ref_wait(tty);
  474. tty_ldisc_deref(ld);
  475. goto restart;
  476. }
  477. /*
  478. * This flag is used to avoid two parallel ldisc changes. Once
  479. * open and close are fine grained locked this may work better
  480. * as a mutex shared with the open/close/hup paths
  481. */
  482. set_bit(TTY_LDISC_CHANGING, &tty->flags);
  483. if (o_tty)
  484. set_bit(TTY_LDISC_CHANGING, &o_tty->flags);
  485. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  486. /*
  487. * From this point on we know nobody has an ldisc
  488. * usage reference, nor can they obtain one until
  489. * we say so later on.
  490. */
  491. work = cancel_delayed_work(&tty->buf.work);
  492. /*
  493. * Wait for ->hangup_work and ->buf.work handlers to terminate
  494. * MUST NOT hold locks here.
  495. */
  496. flush_scheduled_work();
  497. /* Shutdown the current discipline. */
  498. if (o_ldisc.ops->close)
  499. (o_ldisc.ops->close)(tty);
  500. /* Now set up the new line discipline. */
  501. tty_ldisc_assign(tty, &new_ldisc);
  502. tty_set_termios_ldisc(tty, ldisc);
  503. if (new_ldisc.ops->open)
  504. retval = (new_ldisc.ops->open)(tty);
  505. if (retval < 0) {
  506. tty_ldisc_put(new_ldisc.ops);
  507. tty_ldisc_restore(tty, &o_ldisc);
  508. }
  509. /* At this point we hold a reference to the new ldisc and a
  510. a reference to the old ldisc. If we ended up flipping back
  511. to the existing ldisc we have two references to it */
  512. if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc)
  513. tty->ops->set_ldisc(tty);
  514. tty_ldisc_put(o_ldisc.ops);
  515. /*
  516. * Allow ldisc referencing to occur as soon as the driver
  517. * ldisc callback completes.
  518. */
  519. tty_ldisc_enable(tty);
  520. if (o_tty)
  521. tty_ldisc_enable(o_tty);
  522. /* Restart it in case no characters kick it off. Safe if
  523. already running */
  524. if (work)
  525. schedule_delayed_work(&tty->buf.work, 1);
  526. return retval;
  527. }
  528. /**
  529. * tty_ldisc_setup - open line discipline
  530. * @tty: tty being shut down
  531. * @o_tty: pair tty for pty/tty pairs
  532. *
  533. * Called during the initial open of a tty/pty pair in order to set up the
  534. * line discplines and bind them to the tty.
  535. */
  536. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  537. {
  538. struct tty_ldisc *ld = &tty->ldisc;
  539. int retval;
  540. if (ld->ops->open) {
  541. retval = (ld->ops->open)(tty);
  542. if (retval)
  543. return retval;
  544. }
  545. if (o_tty && o_tty->ldisc.ops->open) {
  546. retval = (o_tty->ldisc.ops->open)(o_tty);
  547. if (retval) {
  548. if (ld->ops->close)
  549. (ld->ops->close)(tty);
  550. return retval;
  551. }
  552. tty_ldisc_enable(o_tty);
  553. }
  554. tty_ldisc_enable(tty);
  555. return 0;
  556. }
  557. /**
  558. * tty_ldisc_release - release line discipline
  559. * @tty: tty being shut down
  560. * @o_tty: pair tty for pty/tty pairs
  561. *
  562. * Called during the final close of a tty/pty pair in order to shut down the
  563. * line discpline layer.
  564. */
  565. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  566. {
  567. unsigned long flags;
  568. struct tty_ldisc ld;
  569. /*
  570. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  571. * kill any delayed work. As this is the final close it does not
  572. * race with the set_ldisc code path.
  573. */
  574. clear_bit(TTY_LDISC, &tty->flags);
  575. cancel_delayed_work(&tty->buf.work);
  576. /*
  577. * Wait for ->hangup_work and ->buf.work handlers to terminate
  578. */
  579. flush_scheduled_work();
  580. /*
  581. * Wait for any short term users (we know they are just driver
  582. * side waiters as the file is closing so user count on the file
  583. * side is zero.
  584. */
  585. spin_lock_irqsave(&tty_ldisc_lock, flags);
  586. while (tty->ldisc.refcount) {
  587. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  588. wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
  589. spin_lock_irqsave(&tty_ldisc_lock, flags);
  590. }
  591. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  592. /*
  593. * Shutdown the current line discipline, and reset it to N_TTY.
  594. *
  595. * FIXME: this MUST get fixed for the new reflocking
  596. */
  597. if (tty->ldisc.ops->close)
  598. (tty->ldisc.ops->close)(tty);
  599. tty_ldisc_put(tty->ldisc.ops);
  600. /*
  601. * Switch the line discipline back
  602. */
  603. WARN_ON(tty_ldisc_get(N_TTY, &ld));
  604. tty_ldisc_assign(tty, &ld);
  605. tty_set_termios_ldisc(tty, N_TTY);
  606. if (o_tty) {
  607. /* FIXME: could o_tty be in setldisc here ? */
  608. clear_bit(TTY_LDISC, &o_tty->flags);
  609. if (o_tty->ldisc.ops->close)
  610. (o_tty->ldisc.ops->close)(o_tty);
  611. tty_ldisc_put(o_tty->ldisc.ops);
  612. WARN_ON(tty_ldisc_get(N_TTY, &ld));
  613. tty_ldisc_assign(o_tty, &ld);
  614. tty_set_termios_ldisc(o_tty, N_TTY);
  615. }
  616. }
  617. /**
  618. * tty_ldisc_init - ldisc setup for new tty
  619. * @tty: tty being allocated
  620. *
  621. * Set up the line discipline objects for a newly allocated tty. Note that
  622. * the tty structure is not completely set up when this call is made.
  623. */
  624. void tty_ldisc_init(struct tty_struct *tty)
  625. {
  626. struct tty_ldisc ld;
  627. if (tty_ldisc_get(N_TTY, &ld) < 0)
  628. panic("n_tty: init_tty");
  629. tty_ldisc_assign(tty, &ld);
  630. }
  631. void tty_ldisc_begin(void)
  632. {
  633. /* Setup the default TTY line discipline. */
  634. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  635. }