tty_ldisc.c 21 KB

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