PageRenderTime 1687ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/tty/tty_io.c

https://github.com/Mengqi/linux-2.6
C | 1923 lines | 1126 code | 211 blank | 586 comment | 207 complexity | eda5b076f2e068263b40367008e28ede MD5 | raw file
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. */
  4. /*
  5. * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
  6. * or rs-channels. It also implements echoing, cooked mode etc.
  7. *
  8. * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
  9. *
  10. * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
  11. * tty_struct and tty_queue structures. Previously there was an array
  12. * of 256 tty_struct's which was statically allocated, and the
  13. * tty_queue structures were allocated at boot time. Both are now
  14. * dynamically allocated only when the tty is open.
  15. *
  16. * Also restructured routines so that there is more of a separation
  17. * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
  18. * the low-level tty routines (serial.c, pty.c, console.c). This
  19. * makes for cleaner and more compact code. -TYT, 9/17/92
  20. *
  21. * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  22. * which can be dynamically activated and de-activated by the line
  23. * discipline handling modules (like SLIP).
  24. *
  25. * NOTE: pay no attention to the line discipline code (yet); its
  26. * interface is still subject to change in this version...
  27. * -- TYT, 1/31/92
  28. *
  29. * Added functionality to the OPOST tty handling. No delays, but all
  30. * other bits should be there.
  31. * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
  32. *
  33. * Rewrote canonical mode and added more termios flags.
  34. * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
  35. *
  36. * Reorganized FASYNC support so mouse code can share it.
  37. * -- ctm@ardi.com, 9Sep95
  38. *
  39. * New TIOCLINUX variants added.
  40. * -- mj@k332.feld.cvut.cz, 19-Nov-95
  41. *
  42. * Restrict vt switching via ioctl()
  43. * -- grif@cs.ucr.edu, 5-Dec-95
  44. *
  45. * Move console and virtual terminal code to more appropriate files,
  46. * implement CONFIG_VT and generalize console device interface.
  47. * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
  48. *
  49. * Rewrote tty_init_dev and tty_release_dev to eliminate races.
  50. * -- Bill Hawes <whawes@star.net>, June 97
  51. *
  52. * Added devfs support.
  53. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
  54. *
  55. * Added support for a Unix98-style ptmx device.
  56. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  57. *
  58. * Reduced memory usage for older ARM systems
  59. * -- Russell King <rmk@arm.linux.org.uk>
  60. *
  61. * Move do_SAK() into process context. Less stack use in devfs functions.
  62. * alloc_tty_struct() always uses kmalloc()
  63. * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
  64. */
  65. #include <linux/types.h>
  66. #include <linux/major.h>
  67. #include <linux/errno.h>
  68. #include <linux/signal.h>
  69. #include <linux/fcntl.h>
  70. #include <linux/sched.h>
  71. #include <linux/interrupt.h>
  72. #include <linux/tty.h>
  73. #include <linux/tty_driver.h>
  74. #include <linux/tty_flip.h>
  75. #include <linux/devpts_fs.h>
  76. #include <linux/file.h>
  77. #include <linux/fdtable.h>
  78. #include <linux/console.h>
  79. #include <linux/timer.h>
  80. #include <linux/ctype.h>
  81. #include <linux/kd.h>
  82. #include <linux/mm.h>
  83. #include <linux/string.h>
  84. #include <linux/slab.h>
  85. #include <linux/poll.h>
  86. #include <linux/proc_fs.h>
  87. #include <linux/init.h>
  88. #include <linux/module.h>
  89. #include <linux/device.h>
  90. #include <linux/wait.h>
  91. #include <linux/bitops.h>
  92. #include <linux/delay.h>
  93. #include <linux/seq_file.h>
  94. #include <linux/serial.h>
  95. #include <linux/ratelimit.h>
  96. #include <linux/uaccess.h>
  97. #include <asm/system.h>
  98. #include <linux/kbd_kern.h>
  99. #include <linux/vt_kern.h>
  100. #include <linux/selection.h>
  101. #include <linux/kmod.h>
  102. #include <linux/nsproxy.h>
  103. #undef TTY_DEBUG_HANGUP
  104. #define TTY_PARANOIA_CHECK 1
  105. #define CHECK_TTY_COUNT 1
  106. struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
  107. .c_iflag = ICRNL | IXON,
  108. .c_oflag = OPOST | ONLCR,
  109. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  110. .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
  111. ECHOCTL | ECHOKE | IEXTEN,
  112. .c_cc = INIT_C_CC,
  113. .c_ispeed = 38400,
  114. .c_ospeed = 38400
  115. };
  116. EXPORT_SYMBOL(tty_std_termios);
  117. /* This list gets poked at by procfs and various bits of boot up code. This
  118. could do with some rationalisation such as pulling the tty proc function
  119. into this file */
  120. LIST_HEAD(tty_drivers); /* linked list of tty drivers */
  121. /* Mutex to protect creating and releasing a tty. This is shared with
  122. vt.c for deeply disgusting hack reasons */
  123. DEFINE_MUTEX(tty_mutex);
  124. EXPORT_SYMBOL(tty_mutex);
  125. /* Spinlock to protect the tty->tty_files list */
  126. DEFINE_SPINLOCK(tty_files_lock);
  127. static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
  128. static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
  129. ssize_t redirected_tty_write(struct file *, const char __user *,
  130. size_t, loff_t *);
  131. static unsigned int tty_poll(struct file *, poll_table *);
  132. static int tty_open(struct inode *, struct file *);
  133. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  134. #ifdef CONFIG_COMPAT
  135. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  136. unsigned long arg);
  137. #else
  138. #define tty_compat_ioctl NULL
  139. #endif
  140. static int __tty_fasync(int fd, struct file *filp, int on);
  141. static int tty_fasync(int fd, struct file *filp, int on);
  142. static void release_tty(struct tty_struct *tty, int idx);
  143. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
  144. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
  145. /**
  146. * alloc_tty_struct - allocate a tty object
  147. *
  148. * Return a new empty tty structure. The data fields have not
  149. * been initialized in any way but has been zeroed
  150. *
  151. * Locking: none
  152. */
  153. struct tty_struct *alloc_tty_struct(void)
  154. {
  155. return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
  156. }
  157. /**
  158. * free_tty_struct - free a disused tty
  159. * @tty: tty struct to free
  160. *
  161. * Free the write buffers, tty queue and tty memory itself.
  162. *
  163. * Locking: none. Must be called after tty is definitely unused
  164. */
  165. void free_tty_struct(struct tty_struct *tty)
  166. {
  167. if (tty->dev)
  168. put_device(tty->dev);
  169. kfree(tty->write_buf);
  170. tty_buffer_free_all(tty);
  171. kfree(tty);
  172. }
  173. static inline struct tty_struct *file_tty(struct file *file)
  174. {
  175. return ((struct tty_file_private *)file->private_data)->tty;
  176. }
  177. /* Associate a new file with the tty structure */
  178. int tty_add_file(struct tty_struct *tty, struct file *file)
  179. {
  180. struct tty_file_private *priv;
  181. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  182. if (!priv)
  183. return -ENOMEM;
  184. priv->tty = tty;
  185. priv->file = file;
  186. file->private_data = priv;
  187. spin_lock(&tty_files_lock);
  188. list_add(&priv->list, &tty->tty_files);
  189. spin_unlock(&tty_files_lock);
  190. return 0;
  191. }
  192. /* Delete file from its tty */
  193. void tty_del_file(struct file *file)
  194. {
  195. struct tty_file_private *priv = file->private_data;
  196. spin_lock(&tty_files_lock);
  197. list_del(&priv->list);
  198. spin_unlock(&tty_files_lock);
  199. file->private_data = NULL;
  200. kfree(priv);
  201. }
  202. #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
  203. /**
  204. * tty_name - return tty naming
  205. * @tty: tty structure
  206. * @buf: buffer for output
  207. *
  208. * Convert a tty structure into a name. The name reflects the kernel
  209. * naming policy and if udev is in use may not reflect user space
  210. *
  211. * Locking: none
  212. */
  213. char *tty_name(struct tty_struct *tty, char *buf)
  214. {
  215. if (!tty) /* Hmm. NULL pointer. That's fun. */
  216. strcpy(buf, "NULL tty");
  217. else
  218. strcpy(buf, tty->name);
  219. return buf;
  220. }
  221. EXPORT_SYMBOL(tty_name);
  222. int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
  223. const char *routine)
  224. {
  225. #ifdef TTY_PARANOIA_CHECK
  226. if (!tty) {
  227. printk(KERN_WARNING
  228. "null TTY for (%d:%d) in %s\n",
  229. imajor(inode), iminor(inode), routine);
  230. return 1;
  231. }
  232. if (tty->magic != TTY_MAGIC) {
  233. printk(KERN_WARNING
  234. "bad magic number for tty struct (%d:%d) in %s\n",
  235. imajor(inode), iminor(inode), routine);
  236. return 1;
  237. }
  238. #endif
  239. return 0;
  240. }
  241. static int check_tty_count(struct tty_struct *tty, const char *routine)
  242. {
  243. #ifdef CHECK_TTY_COUNT
  244. struct list_head *p;
  245. int count = 0;
  246. spin_lock(&tty_files_lock);
  247. list_for_each(p, &tty->tty_files) {
  248. count++;
  249. }
  250. spin_unlock(&tty_files_lock);
  251. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  252. tty->driver->subtype == PTY_TYPE_SLAVE &&
  253. tty->link && tty->link->count)
  254. count++;
  255. if (tty->count != count) {
  256. printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
  257. "!= #fd's(%d) in %s\n",
  258. tty->name, tty->count, count, routine);
  259. return count;
  260. }
  261. #endif
  262. return 0;
  263. }
  264. /**
  265. * get_tty_driver - find device of a tty
  266. * @dev_t: device identifier
  267. * @index: returns the index of the tty
  268. *
  269. * This routine returns a tty driver structure, given a device number
  270. * and also passes back the index number.
  271. *
  272. * Locking: caller must hold tty_mutex
  273. */
  274. static struct tty_driver *get_tty_driver(dev_t device, int *index)
  275. {
  276. struct tty_driver *p;
  277. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  278. dev_t base = MKDEV(p->major, p->minor_start);
  279. if (device < base || device >= base + p->num)
  280. continue;
  281. *index = device - base;
  282. return tty_driver_kref_get(p);
  283. }
  284. return NULL;
  285. }
  286. #ifdef CONFIG_CONSOLE_POLL
  287. /**
  288. * tty_find_polling_driver - find device of a polled tty
  289. * @name: name string to match
  290. * @line: pointer to resulting tty line nr
  291. *
  292. * This routine returns a tty driver structure, given a name
  293. * and the condition that the tty driver is capable of polled
  294. * operation.
  295. */
  296. struct tty_driver *tty_find_polling_driver(char *name, int *line)
  297. {
  298. struct tty_driver *p, *res = NULL;
  299. int tty_line = 0;
  300. int len;
  301. char *str, *stp;
  302. for (str = name; *str; str++)
  303. if ((*str >= '0' && *str <= '9') || *str == ',')
  304. break;
  305. if (!*str)
  306. return NULL;
  307. len = str - name;
  308. tty_line = simple_strtoul(str, &str, 10);
  309. mutex_lock(&tty_mutex);
  310. /* Search through the tty devices to look for a match */
  311. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  312. if (strncmp(name, p->name, len) != 0)
  313. continue;
  314. stp = str;
  315. if (*stp == ',')
  316. stp++;
  317. if (*stp == '\0')
  318. stp = NULL;
  319. if (tty_line >= 0 && tty_line < p->num && p->ops &&
  320. p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
  321. res = tty_driver_kref_get(p);
  322. *line = tty_line;
  323. break;
  324. }
  325. }
  326. mutex_unlock(&tty_mutex);
  327. return res;
  328. }
  329. EXPORT_SYMBOL_GPL(tty_find_polling_driver);
  330. #endif
  331. /**
  332. * tty_check_change - check for POSIX terminal changes
  333. * @tty: tty to check
  334. *
  335. * If we try to write to, or set the state of, a terminal and we're
  336. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  337. * ignored, go ahead and perform the operation. (POSIX 7.2)
  338. *
  339. * Locking: ctrl_lock
  340. */
  341. int tty_check_change(struct tty_struct *tty)
  342. {
  343. unsigned long flags;
  344. int ret = 0;
  345. if (current->signal->tty != tty)
  346. return 0;
  347. spin_lock_irqsave(&tty->ctrl_lock, flags);
  348. if (!tty->pgrp) {
  349. printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
  350. goto out_unlock;
  351. }
  352. if (task_pgrp(current) == tty->pgrp)
  353. goto out_unlock;
  354. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  355. if (is_ignored(SIGTTOU))
  356. goto out;
  357. if (is_current_pgrp_orphaned()) {
  358. ret = -EIO;
  359. goto out;
  360. }
  361. kill_pgrp(task_pgrp(current), SIGTTOU, 1);
  362. set_thread_flag(TIF_SIGPENDING);
  363. ret = -ERESTARTSYS;
  364. out:
  365. return ret;
  366. out_unlock:
  367. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  368. return ret;
  369. }
  370. EXPORT_SYMBOL(tty_check_change);
  371. static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
  372. size_t count, loff_t *ppos)
  373. {
  374. return 0;
  375. }
  376. static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
  377. size_t count, loff_t *ppos)
  378. {
  379. return -EIO;
  380. }
  381. /* No kernel lock held - none needed ;) */
  382. static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
  383. {
  384. return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
  385. }
  386. static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
  387. unsigned long arg)
  388. {
  389. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  390. }
  391. static long hung_up_tty_compat_ioctl(struct file *file,
  392. unsigned int cmd, unsigned long arg)
  393. {
  394. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  395. }
  396. static const struct file_operations tty_fops = {
  397. .llseek = no_llseek,
  398. .read = tty_read,
  399. .write = tty_write,
  400. .poll = tty_poll,
  401. .unlocked_ioctl = tty_ioctl,
  402. .compat_ioctl = tty_compat_ioctl,
  403. .open = tty_open,
  404. .release = tty_release,
  405. .fasync = tty_fasync,
  406. };
  407. static const struct file_operations console_fops = {
  408. .llseek = no_llseek,
  409. .read = tty_read,
  410. .write = redirected_tty_write,
  411. .poll = tty_poll,
  412. .unlocked_ioctl = tty_ioctl,
  413. .compat_ioctl = tty_compat_ioctl,
  414. .open = tty_open,
  415. .release = tty_release,
  416. .fasync = tty_fasync,
  417. };
  418. static const struct file_operations hung_up_tty_fops = {
  419. .llseek = no_llseek,
  420. .read = hung_up_tty_read,
  421. .write = hung_up_tty_write,
  422. .poll = hung_up_tty_poll,
  423. .unlocked_ioctl = hung_up_tty_ioctl,
  424. .compat_ioctl = hung_up_tty_compat_ioctl,
  425. .release = tty_release,
  426. };
  427. static DEFINE_SPINLOCK(redirect_lock);
  428. static struct file *redirect;
  429. /**
  430. * tty_wakeup - request more data
  431. * @tty: terminal
  432. *
  433. * Internal and external helper for wakeups of tty. This function
  434. * informs the line discipline if present that the driver is ready
  435. * to receive more output data.
  436. */
  437. void tty_wakeup(struct tty_struct *tty)
  438. {
  439. struct tty_ldisc *ld;
  440. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
  441. ld = tty_ldisc_ref(tty);
  442. if (ld) {
  443. if (ld->ops->write_wakeup)
  444. ld->ops->write_wakeup(tty);
  445. tty_ldisc_deref(ld);
  446. }
  447. }
  448. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  449. }
  450. EXPORT_SYMBOL_GPL(tty_wakeup);
  451. /**
  452. * __tty_hangup - actual handler for hangup events
  453. * @work: tty device
  454. *
  455. * This can be called by the "eventd" kernel thread. That is process
  456. * synchronous but doesn't hold any locks, so we need to make sure we
  457. * have the appropriate locks for what we're doing.
  458. *
  459. * The hangup event clears any pending redirections onto the hung up
  460. * device. It ensures future writes will error and it does the needed
  461. * line discipline hangup and signal delivery. The tty object itself
  462. * remains intact.
  463. *
  464. * Locking:
  465. * BTM
  466. * redirect lock for undoing redirection
  467. * file list lock for manipulating list of ttys
  468. * tty_ldisc_lock from called functions
  469. * termios_mutex resetting termios data
  470. * tasklist_lock to walk task list for hangup event
  471. * ->siglock to protect ->signal/->sighand
  472. */
  473. void __tty_hangup(struct tty_struct *tty)
  474. {
  475. struct file *cons_filp = NULL;
  476. struct file *filp, *f = NULL;
  477. struct task_struct *p;
  478. struct tty_file_private *priv;
  479. int closecount = 0, n;
  480. unsigned long flags;
  481. int refs = 0;
  482. if (!tty)
  483. return;
  484. spin_lock(&redirect_lock);
  485. if (redirect && file_tty(redirect) == tty) {
  486. f = redirect;
  487. redirect = NULL;
  488. }
  489. spin_unlock(&redirect_lock);
  490. tty_lock();
  491. /* some functions below drop BTM, so we need this bit */
  492. set_bit(TTY_HUPPING, &tty->flags);
  493. /* inuse_filps is protected by the single tty lock,
  494. this really needs to change if we want to flush the
  495. workqueue with the lock held */
  496. check_tty_count(tty, "tty_hangup");
  497. spin_lock(&tty_files_lock);
  498. /* This breaks for file handles being sent over AF_UNIX sockets ? */
  499. list_for_each_entry(priv, &tty->tty_files, list) {
  500. filp = priv->file;
  501. if (filp->f_op->write == redirected_tty_write)
  502. cons_filp = filp;
  503. if (filp->f_op->write != tty_write)
  504. continue;
  505. closecount++;
  506. __tty_fasync(-1, filp, 0); /* can't block */
  507. filp->f_op = &hung_up_tty_fops;
  508. }
  509. spin_unlock(&tty_files_lock);
  510. /*
  511. * it drops BTM and thus races with reopen
  512. * we protect the race by TTY_HUPPING
  513. */
  514. tty_ldisc_hangup(tty);
  515. read_lock(&tasklist_lock);
  516. if (tty->session) {
  517. do_each_pid_task(tty->session, PIDTYPE_SID, p) {
  518. spin_lock_irq(&p->sighand->siglock);
  519. if (p->signal->tty == tty) {
  520. p->signal->tty = NULL;
  521. /* We defer the dereferences outside fo
  522. the tasklist lock */
  523. refs++;
  524. }
  525. if (!p->signal->leader) {
  526. spin_unlock_irq(&p->sighand->siglock);
  527. continue;
  528. }
  529. __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
  530. __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
  531. put_pid(p->signal->tty_old_pgrp); /* A noop */
  532. spin_lock_irqsave(&tty->ctrl_lock, flags);
  533. if (tty->pgrp)
  534. p->signal->tty_old_pgrp = get_pid(tty->pgrp);
  535. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  536. spin_unlock_irq(&p->sighand->siglock);
  537. } while_each_pid_task(tty->session, PIDTYPE_SID, p);
  538. }
  539. read_unlock(&tasklist_lock);
  540. spin_lock_irqsave(&tty->ctrl_lock, flags);
  541. clear_bit(TTY_THROTTLED, &tty->flags);
  542. clear_bit(TTY_PUSH, &tty->flags);
  543. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  544. put_pid(tty->session);
  545. put_pid(tty->pgrp);
  546. tty->session = NULL;
  547. tty->pgrp = NULL;
  548. tty->ctrl_status = 0;
  549. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  550. /* Account for the p->signal references we killed */
  551. while (refs--)
  552. tty_kref_put(tty);
  553. /*
  554. * If one of the devices matches a console pointer, we
  555. * cannot just call hangup() because that will cause
  556. * tty->count and state->count to go out of sync.
  557. * So we just call close() the right number of times.
  558. */
  559. if (cons_filp) {
  560. if (tty->ops->close)
  561. for (n = 0; n < closecount; n++)
  562. tty->ops->close(tty, cons_filp);
  563. } else if (tty->ops->hangup)
  564. (tty->ops->hangup)(tty);
  565. /*
  566. * We don't want to have driver/ldisc interactions beyond
  567. * the ones we did here. The driver layer expects no
  568. * calls after ->hangup() from the ldisc side. However we
  569. * can't yet guarantee all that.
  570. */
  571. set_bit(TTY_HUPPED, &tty->flags);
  572. clear_bit(TTY_HUPPING, &tty->flags);
  573. tty_ldisc_enable(tty);
  574. tty_unlock();
  575. if (f)
  576. fput(f);
  577. }
  578. static void do_tty_hangup(struct work_struct *work)
  579. {
  580. struct tty_struct *tty =
  581. container_of(work, struct tty_struct, hangup_work);
  582. __tty_hangup(tty);
  583. }
  584. /**
  585. * tty_hangup - trigger a hangup event
  586. * @tty: tty to hangup
  587. *
  588. * A carrier loss (virtual or otherwise) has occurred on this like
  589. * schedule a hangup sequence to run after this event.
  590. */
  591. void tty_hangup(struct tty_struct *tty)
  592. {
  593. #ifdef TTY_DEBUG_HANGUP
  594. char buf[64];
  595. printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
  596. #endif
  597. schedule_work(&tty->hangup_work);
  598. }
  599. EXPORT_SYMBOL(tty_hangup);
  600. /**
  601. * tty_vhangup - process vhangup
  602. * @tty: tty to hangup
  603. *
  604. * The user has asked via system call for the terminal to be hung up.
  605. * We do this synchronously so that when the syscall returns the process
  606. * is complete. That guarantee is necessary for security reasons.
  607. */
  608. void tty_vhangup(struct tty_struct *tty)
  609. {
  610. #ifdef TTY_DEBUG_HANGUP
  611. char buf[64];
  612. printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
  613. #endif
  614. __tty_hangup(tty);
  615. }
  616. EXPORT_SYMBOL(tty_vhangup);
  617. /**
  618. * tty_vhangup_self - process vhangup for own ctty
  619. *
  620. * Perform a vhangup on the current controlling tty
  621. */
  622. void tty_vhangup_self(void)
  623. {
  624. struct tty_struct *tty;
  625. tty = get_current_tty();
  626. if (tty) {
  627. tty_vhangup(tty);
  628. tty_kref_put(tty);
  629. }
  630. }
  631. /**
  632. * tty_hung_up_p - was tty hung up
  633. * @filp: file pointer of tty
  634. *
  635. * Return true if the tty has been subject to a vhangup or a carrier
  636. * loss
  637. */
  638. int tty_hung_up_p(struct file *filp)
  639. {
  640. return (filp->f_op == &hung_up_tty_fops);
  641. }
  642. EXPORT_SYMBOL(tty_hung_up_p);
  643. static void session_clear_tty(struct pid *session)
  644. {
  645. struct task_struct *p;
  646. do_each_pid_task(session, PIDTYPE_SID, p) {
  647. proc_clear_tty(p);
  648. } while_each_pid_task(session, PIDTYPE_SID, p);
  649. }
  650. /**
  651. * disassociate_ctty - disconnect controlling tty
  652. * @on_exit: true if exiting so need to "hang up" the session
  653. *
  654. * This function is typically called only by the session leader, when
  655. * it wants to disassociate itself from its controlling tty.
  656. *
  657. * It performs the following functions:
  658. * (1) Sends a SIGHUP and SIGCONT to the foreground process group
  659. * (2) Clears the tty from being controlling the session
  660. * (3) Clears the controlling tty for all processes in the
  661. * session group.
  662. *
  663. * The argument on_exit is set to 1 if called when a process is
  664. * exiting; it is 0 if called by the ioctl TIOCNOTTY.
  665. *
  666. * Locking:
  667. * BTM is taken for hysterical raisins, and held when
  668. * called from no_tty().
  669. * tty_mutex is taken to protect tty
  670. * ->siglock is taken to protect ->signal/->sighand
  671. * tasklist_lock is taken to walk process list for sessions
  672. * ->siglock is taken to protect ->signal/->sighand
  673. */
  674. void disassociate_ctty(int on_exit)
  675. {
  676. struct tty_struct *tty;
  677. struct pid *tty_pgrp = NULL;
  678. if (!current->signal->leader)
  679. return;
  680. tty = get_current_tty();
  681. if (tty) {
  682. tty_pgrp = get_pid(tty->pgrp);
  683. if (on_exit) {
  684. if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
  685. tty_vhangup(tty);
  686. }
  687. tty_kref_put(tty);
  688. } else if (on_exit) {
  689. struct pid *old_pgrp;
  690. spin_lock_irq(&current->sighand->siglock);
  691. old_pgrp = current->signal->tty_old_pgrp;
  692. current->signal->tty_old_pgrp = NULL;
  693. spin_unlock_irq(&current->sighand->siglock);
  694. if (old_pgrp) {
  695. kill_pgrp(old_pgrp, SIGHUP, on_exit);
  696. kill_pgrp(old_pgrp, SIGCONT, on_exit);
  697. put_pid(old_pgrp);
  698. }
  699. return;
  700. }
  701. if (tty_pgrp) {
  702. kill_pgrp(tty_pgrp, SIGHUP, on_exit);
  703. if (!on_exit)
  704. kill_pgrp(tty_pgrp, SIGCONT, on_exit);
  705. put_pid(tty_pgrp);
  706. }
  707. spin_lock_irq(&current->sighand->siglock);
  708. put_pid(current->signal->tty_old_pgrp);
  709. current->signal->tty_old_pgrp = NULL;
  710. spin_unlock_irq(&current->sighand->siglock);
  711. tty = get_current_tty();
  712. if (tty) {
  713. unsigned long flags;
  714. spin_lock_irqsave(&tty->ctrl_lock, flags);
  715. put_pid(tty->session);
  716. put_pid(tty->pgrp);
  717. tty->session = NULL;
  718. tty->pgrp = NULL;
  719. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  720. tty_kref_put(tty);
  721. } else {
  722. #ifdef TTY_DEBUG_HANGUP
  723. printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
  724. " = NULL", tty);
  725. #endif
  726. }
  727. /* Now clear signal->tty under the lock */
  728. read_lock(&tasklist_lock);
  729. session_clear_tty(task_session(current));
  730. read_unlock(&tasklist_lock);
  731. }
  732. /**
  733. *
  734. * no_tty - Ensure the current process does not have a controlling tty
  735. */
  736. void no_tty(void)
  737. {
  738. struct task_struct *tsk = current;
  739. tty_lock();
  740. disassociate_ctty(0);
  741. tty_unlock();
  742. proc_clear_tty(tsk);
  743. }
  744. /**
  745. * stop_tty - propagate flow control
  746. * @tty: tty to stop
  747. *
  748. * Perform flow control to the driver. For PTY/TTY pairs we
  749. * must also propagate the TIOCKPKT status. May be called
  750. * on an already stopped device and will not re-call the driver
  751. * method.
  752. *
  753. * This functionality is used by both the line disciplines for
  754. * halting incoming flow and by the driver. It may therefore be
  755. * called from any context, may be under the tty atomic_write_lock
  756. * but not always.
  757. *
  758. * Locking:
  759. * Uses the tty control lock internally
  760. */
  761. void stop_tty(struct tty_struct *tty)
  762. {
  763. unsigned long flags;
  764. spin_lock_irqsave(&tty->ctrl_lock, flags);
  765. if (tty->stopped) {
  766. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  767. return;
  768. }
  769. tty->stopped = 1;
  770. if (tty->link && tty->link->packet) {
  771. tty->ctrl_status &= ~TIOCPKT_START;
  772. tty->ctrl_status |= TIOCPKT_STOP;
  773. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  774. }
  775. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  776. if (tty->ops->stop)
  777. (tty->ops->stop)(tty);
  778. }
  779. EXPORT_SYMBOL(stop_tty);
  780. /**
  781. * start_tty - propagate flow control
  782. * @tty: tty to start
  783. *
  784. * Start a tty that has been stopped if at all possible. Perform
  785. * any necessary wakeups and propagate the TIOCPKT status. If this
  786. * is the tty was previous stopped and is being started then the
  787. * driver start method is invoked and the line discipline woken.
  788. *
  789. * Locking:
  790. * ctrl_lock
  791. */
  792. void start_tty(struct tty_struct *tty)
  793. {
  794. unsigned long flags;
  795. spin_lock_irqsave(&tty->ctrl_lock, flags);
  796. if (!tty->stopped || tty->flow_stopped) {
  797. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  798. return;
  799. }
  800. tty->stopped = 0;
  801. if (tty->link && tty->link->packet) {
  802. tty->ctrl_status &= ~TIOCPKT_STOP;
  803. tty->ctrl_status |= TIOCPKT_START;
  804. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  805. }
  806. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  807. if (tty->ops->start)
  808. (tty->ops->start)(tty);
  809. /* If we have a running line discipline it may need kicking */
  810. tty_wakeup(tty);
  811. }
  812. EXPORT_SYMBOL(start_tty);
  813. /**
  814. * tty_read - read method for tty device files
  815. * @file: pointer to tty file
  816. * @buf: user buffer
  817. * @count: size of user buffer
  818. * @ppos: unused
  819. *
  820. * Perform the read system call function on this terminal device. Checks
  821. * for hung up devices before calling the line discipline method.
  822. *
  823. * Locking:
  824. * Locks the line discipline internally while needed. Multiple
  825. * read calls may be outstanding in parallel.
  826. */
  827. static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
  828. loff_t *ppos)
  829. {
  830. int i;
  831. struct inode *inode = file->f_path.dentry->d_inode;
  832. struct tty_struct *tty = file_tty(file);
  833. struct tty_ldisc *ld;
  834. if (tty_paranoia_check(tty, inode, "tty_read"))
  835. return -EIO;
  836. if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
  837. return -EIO;
  838. /* We want to wait for the line discipline to sort out in this
  839. situation */
  840. ld = tty_ldisc_ref_wait(tty);
  841. if (ld->ops->read)
  842. i = (ld->ops->read)(tty, file, buf, count);
  843. else
  844. i = -EIO;
  845. tty_ldisc_deref(ld);
  846. if (i > 0)
  847. inode->i_atime = current_fs_time(inode->i_sb);
  848. return i;
  849. }
  850. void tty_write_unlock(struct tty_struct *tty)
  851. __releases(&tty->atomic_write_lock)
  852. {
  853. mutex_unlock(&tty->atomic_write_lock);
  854. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  855. }
  856. int tty_write_lock(struct tty_struct *tty, int ndelay)
  857. __acquires(&tty->atomic_write_lock)
  858. {
  859. if (!mutex_trylock(&tty->atomic_write_lock)) {
  860. if (ndelay)
  861. return -EAGAIN;
  862. if (mutex_lock_interruptible(&tty->atomic_write_lock))
  863. return -ERESTARTSYS;
  864. }
  865. return 0;
  866. }
  867. /*
  868. * Split writes up in sane blocksizes to avoid
  869. * denial-of-service type attacks
  870. */
  871. static inline ssize_t do_tty_write(
  872. ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
  873. struct tty_struct *tty,
  874. struct file *file,
  875. const char __user *buf,
  876. size_t count)
  877. {
  878. ssize_t ret, written = 0;
  879. unsigned int chunk;
  880. ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
  881. if (ret < 0)
  882. return ret;
  883. /*
  884. * We chunk up writes into a temporary buffer. This
  885. * simplifies low-level drivers immensely, since they
  886. * don't have locking issues and user mode accesses.
  887. *
  888. * But if TTY_NO_WRITE_SPLIT is set, we should use a
  889. * big chunk-size..
  890. *
  891. * The default chunk-size is 2kB, because the NTTY
  892. * layer has problems with bigger chunks. It will
  893. * claim to be able to handle more characters than
  894. * it actually does.
  895. *
  896. * FIXME: This can probably go away now except that 64K chunks
  897. * are too likely to fail unless switched to vmalloc...
  898. */
  899. chunk = 2048;
  900. if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
  901. chunk = 65536;
  902. if (count < chunk)
  903. chunk = count;
  904. /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
  905. if (tty->write_cnt < chunk) {
  906. unsigned char *buf_chunk;
  907. if (chunk < 1024)
  908. chunk = 1024;
  909. buf_chunk = kmalloc(chunk, GFP_KERNEL);
  910. if (!buf_chunk) {
  911. ret = -ENOMEM;
  912. goto out;
  913. }
  914. kfree(tty->write_buf);
  915. tty->write_cnt = chunk;
  916. tty->write_buf = buf_chunk;
  917. }
  918. /* Do the write .. */
  919. for (;;) {
  920. size_t size = count;
  921. if (size > chunk)
  922. size = chunk;
  923. ret = -EFAULT;
  924. if (copy_from_user(tty->write_buf, buf, size))
  925. break;
  926. ret = write(tty, file, tty->write_buf, size);
  927. if (ret <= 0)
  928. break;
  929. written += ret;
  930. buf += ret;
  931. count -= ret;
  932. if (!count)
  933. break;
  934. ret = -ERESTARTSYS;
  935. if (signal_pending(current))
  936. break;
  937. cond_resched();
  938. }
  939. if (written) {
  940. struct inode *inode = file->f_path.dentry->d_inode;
  941. inode->i_mtime = current_fs_time(inode->i_sb);
  942. ret = written;
  943. }
  944. out:
  945. tty_write_unlock(tty);
  946. return ret;
  947. }
  948. /**
  949. * tty_write_message - write a message to a certain tty, not just the console.
  950. * @tty: the destination tty_struct
  951. * @msg: the message to write
  952. *
  953. * This is used for messages that need to be redirected to a specific tty.
  954. * We don't put it into the syslog queue right now maybe in the future if
  955. * really needed.
  956. *
  957. * We must still hold the BTM and test the CLOSING flag for the moment.
  958. */
  959. void tty_write_message(struct tty_struct *tty, char *msg)
  960. {
  961. if (tty) {
  962. mutex_lock(&tty->atomic_write_lock);
  963. tty_lock();
  964. if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
  965. tty_unlock();
  966. tty->ops->write(tty, msg, strlen(msg));
  967. } else
  968. tty_unlock();
  969. tty_write_unlock(tty);
  970. }
  971. return;
  972. }
  973. /**
  974. * tty_write - write method for tty device file
  975. * @file: tty file pointer
  976. * @buf: user data to write
  977. * @count: bytes to write
  978. * @ppos: unused
  979. *
  980. * Write data to a tty device via the line discipline.
  981. *
  982. * Locking:
  983. * Locks the line discipline as required
  984. * Writes to the tty driver are serialized by the atomic_write_lock
  985. * and are then processed in chunks to the device. The line discipline
  986. * write method will not be invoked in parallel for each device.
  987. */
  988. static ssize_t tty_write(struct file *file, const char __user *buf,
  989. size_t count, loff_t *ppos)
  990. {
  991. struct inode *inode = file->f_path.dentry->d_inode;
  992. struct tty_struct *tty = file_tty(file);
  993. struct tty_ldisc *ld;
  994. ssize_t ret;
  995. if (tty_paranoia_check(tty, inode, "tty_write"))
  996. return -EIO;
  997. if (!tty || !tty->ops->write ||
  998. (test_bit(TTY_IO_ERROR, &tty->flags)))
  999. return -EIO;
  1000. /* Short term debug to catch buggy drivers */
  1001. if (tty->ops->write_room == NULL)
  1002. printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
  1003. tty->driver->name);
  1004. ld = tty_ldisc_ref_wait(tty);
  1005. if (!ld->ops->write)
  1006. ret = -EIO;
  1007. else
  1008. ret = do_tty_write(ld->ops->write, tty, file, buf, count);
  1009. tty_ldisc_deref(ld);
  1010. return ret;
  1011. }
  1012. ssize_t redirected_tty_write(struct file *file, const char __user *buf,
  1013. size_t count, loff_t *ppos)
  1014. {
  1015. struct file *p = NULL;
  1016. spin_lock(&redirect_lock);
  1017. if (redirect) {
  1018. get_file(redirect);
  1019. p = redirect;
  1020. }
  1021. spin_unlock(&redirect_lock);
  1022. if (p) {
  1023. ssize_t res;
  1024. res = vfs_write(p, buf, count, &p->f_pos);
  1025. fput(p);
  1026. return res;
  1027. }
  1028. return tty_write(file, buf, count, ppos);
  1029. }
  1030. static char ptychar[] = "pqrstuvwxyzabcde";
  1031. /**
  1032. * pty_line_name - generate name for a pty
  1033. * @driver: the tty driver in use
  1034. * @index: the minor number
  1035. * @p: output buffer of at least 6 bytes
  1036. *
  1037. * Generate a name from a driver reference and write it to the output
  1038. * buffer.
  1039. *
  1040. * Locking: None
  1041. */
  1042. static void pty_line_name(struct tty_driver *driver, int index, char *p)
  1043. {
  1044. int i = index + driver->name_base;
  1045. /* ->name is initialized to "ttyp", but "tty" is expected */
  1046. sprintf(p, "%s%c%x",
  1047. driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
  1048. ptychar[i >> 4 & 0xf], i & 0xf);
  1049. }
  1050. /**
  1051. * tty_line_name - generate name for a tty
  1052. * @driver: the tty driver in use
  1053. * @index: the minor number
  1054. * @p: output buffer of at least 7 bytes
  1055. *
  1056. * Generate a name from a driver reference and write it to the output
  1057. * buffer.
  1058. *
  1059. * Locking: None
  1060. */
  1061. static void tty_line_name(struct tty_driver *driver, int index, char *p)
  1062. {
  1063. sprintf(p, "%s%d", driver->name, index + driver->name_base);
  1064. }
  1065. /**
  1066. * tty_driver_lookup_tty() - find an existing tty, if any
  1067. * @driver: the driver for the tty
  1068. * @idx: the minor number
  1069. *
  1070. * Return the tty, if found or ERR_PTR() otherwise.
  1071. *
  1072. * Locking: tty_mutex must be held. If tty is found, the mutex must
  1073. * be held until the 'fast-open' is also done. Will change once we
  1074. * have refcounting in the driver and per driver locking
  1075. */
  1076. static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
  1077. struct inode *inode, int idx)
  1078. {
  1079. struct tty_struct *tty;
  1080. if (driver->ops->lookup)
  1081. return driver->ops->lookup(driver, inode, idx);
  1082. tty = driver->ttys[idx];
  1083. return tty;
  1084. }
  1085. /**
  1086. * tty_init_termios - helper for termios setup
  1087. * @tty: the tty to set up
  1088. *
  1089. * Initialise the termios structures for this tty. Thus runs under
  1090. * the tty_mutex currently so we can be relaxed about ordering.
  1091. */
  1092. int tty_init_termios(struct tty_struct *tty)
  1093. {
  1094. struct ktermios *tp;
  1095. int idx = tty->index;
  1096. tp = tty->driver->termios[idx];
  1097. if (tp == NULL) {
  1098. tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  1099. if (tp == NULL)
  1100. return -ENOMEM;
  1101. memcpy(tp, &tty->driver->init_termios,
  1102. sizeof(struct ktermios));
  1103. tty->driver->termios[idx] = tp;
  1104. }
  1105. tty->termios = tp;
  1106. tty->termios_locked = tp + 1;
  1107. /* Compatibility until drivers always set this */
  1108. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  1109. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  1110. return 0;
  1111. }
  1112. EXPORT_SYMBOL_GPL(tty_init_termios);
  1113. /**
  1114. * tty_driver_install_tty() - install a tty entry in the driver
  1115. * @driver: the driver for the tty
  1116. * @tty: the tty
  1117. *
  1118. * Install a tty object into the driver tables. The tty->index field
  1119. * will be set by the time this is called. This method is responsible
  1120. * for ensuring any need additional structures are allocated and
  1121. * configured.
  1122. *
  1123. * Locking: tty_mutex for now
  1124. */
  1125. static int tty_driver_install_tty(struct tty_driver *driver,
  1126. struct tty_struct *tty)
  1127. {
  1128. int idx = tty->index;
  1129. int ret;
  1130. if (driver->ops->install) {
  1131. ret = driver->ops->install(driver, tty);
  1132. return ret;
  1133. }
  1134. if (tty_init_termios(tty) == 0) {
  1135. tty_driver_kref_get(driver);
  1136. tty->count++;
  1137. driver->ttys[idx] = tty;
  1138. return 0;
  1139. }
  1140. return -ENOMEM;
  1141. }
  1142. /**
  1143. * tty_driver_remove_tty() - remove a tty from the driver tables
  1144. * @driver: the driver for the tty
  1145. * @idx: the minor number
  1146. *
  1147. * Remvoe a tty object from the driver tables. The tty->index field
  1148. * will be set by the time this is called.
  1149. *
  1150. * Locking: tty_mutex for now
  1151. */
  1152. void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
  1153. {
  1154. if (driver->ops->remove)
  1155. driver->ops->remove(driver, tty);
  1156. else
  1157. driver->ttys[tty->index] = NULL;
  1158. }
  1159. /*
  1160. * tty_reopen() - fast re-open of an open tty
  1161. * @tty - the tty to open
  1162. *
  1163. * Return 0 on success, -errno on error.
  1164. *
  1165. * Locking: tty_mutex must be held from the time the tty was found
  1166. * till this open completes.
  1167. */
  1168. static int tty_reopen(struct tty_struct *tty)
  1169. {
  1170. struct tty_driver *driver = tty->driver;
  1171. if (test_bit(TTY_CLOSING, &tty->flags) ||
  1172. test_bit(TTY_HUPPING, &tty->flags) ||
  1173. test_bit(TTY_LDISC_CHANGING, &tty->flags))
  1174. return -EIO;
  1175. if (driver->type == TTY_DRIVER_TYPE_PTY &&
  1176. driver->subtype == PTY_TYPE_MASTER) {
  1177. /*
  1178. * special case for PTY masters: only one open permitted,
  1179. * and the slave side open count is incremented as well.
  1180. */
  1181. if (tty->count)
  1182. return -EIO;
  1183. tty->link->count++;
  1184. }
  1185. tty->count++;
  1186. tty->driver = driver; /* N.B. why do this every time?? */
  1187. mutex_lock(&tty->ldisc_mutex);
  1188. WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
  1189. mutex_unlock(&tty->ldisc_mutex);
  1190. return 0;
  1191. }
  1192. /**
  1193. * tty_init_dev - initialise a tty device
  1194. * @driver: tty driver we are opening a device on
  1195. * @idx: device index
  1196. * @ret_tty: returned tty structure
  1197. * @first_ok: ok to open a new device (used by ptmx)
  1198. *
  1199. * Prepare a tty device. This may not be a "new" clean device but
  1200. * could also be an active device. The pty drivers require special
  1201. * handling because of this.
  1202. *
  1203. * Locking:
  1204. * The function is called under the tty_mutex, which
  1205. * protects us from the tty struct or driver itself going away.
  1206. *
  1207. * On exit the tty device has the line discipline attached and
  1208. * a reference count of 1. If a pair was created for pty/tty use
  1209. * and the other was a pty master then it too has a reference count of 1.
  1210. *
  1211. * WSH 06/09/97: Rewritten to remove races and properly clean up after a
  1212. * failed open. The new code protects the open with a mutex, so it's
  1213. * really quite straightforward. The mutex locking can probably be
  1214. * relaxed for the (most common) case of reopening a tty.
  1215. */
  1216. struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
  1217. int first_ok)
  1218. {
  1219. struct tty_struct *tty;
  1220. int retval;
  1221. /* Check if pty master is being opened multiple times */
  1222. if (driver->subtype == PTY_TYPE_MASTER &&
  1223. (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
  1224. return ERR_PTR(-EIO);
  1225. }
  1226. /*
  1227. * First time open is complex, especially for PTY devices.
  1228. * This code guarantees that either everything succeeds and the
  1229. * TTY is ready for operation, or else the table slots are vacated
  1230. * and the allocated memory released. (Except that the termios
  1231. * and locked termios may be retained.)
  1232. */
  1233. if (!try_module_get(driver->owner))
  1234. return ERR_PTR(-ENODEV);
  1235. tty = alloc_tty_struct();
  1236. if (!tty) {
  1237. retval = -ENOMEM;
  1238. goto err_module_put;
  1239. }
  1240. initialize_tty_struct(tty, driver, idx);
  1241. retval = tty_driver_install_tty(driver, tty);
  1242. if (retval < 0)
  1243. goto err_deinit_tty;
  1244. /*
  1245. * Structures all installed ... call the ldisc open routines.
  1246. * If we fail here just call release_tty to clean up. No need
  1247. * to decrement the use counts, as release_tty doesn't care.
  1248. */
  1249. retval = tty_ldisc_setup(tty, tty->link);
  1250. if (retval)
  1251. goto err_release_tty;
  1252. return tty;
  1253. err_deinit_tty:
  1254. deinitialize_tty_struct(tty);
  1255. free_tty_struct(tty);
  1256. err_module_put:
  1257. module_put(driver->owner);
  1258. return ERR_PTR(retval);
  1259. /* call the tty release_tty routine to clean out this slot */
  1260. err_release_tty:
  1261. printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
  1262. "clearing slot %d\n", idx);
  1263. release_tty(tty, idx);
  1264. return ERR_PTR(retval);
  1265. }
  1266. void tty_free_termios(struct tty_struct *tty)
  1267. {
  1268. struct ktermios *tp;
  1269. int idx = tty->index;
  1270. /* Kill this flag and push into drivers for locking etc */
  1271. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  1272. /* FIXME: Locking on ->termios array */
  1273. tp = tty->termios;
  1274. tty->driver->termios[idx] = NULL;
  1275. kfree(tp);
  1276. }
  1277. }
  1278. EXPORT_SYMBOL(tty_free_termios);
  1279. void tty_shutdown(struct tty_struct *tty)
  1280. {
  1281. tty_driver_remove_tty(tty->driver, tty);
  1282. tty_free_termios(tty);
  1283. }
  1284. EXPORT_SYMBOL(tty_shutdown);
  1285. /**
  1286. * release_one_tty - release tty structure memory
  1287. * @kref: kref of tty we are obliterating
  1288. *
  1289. * Releases memory associated with a tty structure, and clears out the
  1290. * driver table slots. This function is called when a device is no longer
  1291. * in use. It also gets called when setup of a device fails.
  1292. *
  1293. * Locking:
  1294. * tty_mutex - sometimes only
  1295. * takes the file list lock internally when working on the list
  1296. * of ttys that the driver keeps.
  1297. *
  1298. * This method gets called from a work queue so that the driver private
  1299. * cleanup ops can sleep (needed for USB at least)
  1300. */
  1301. static void release_one_tty(struct work_struct *work)
  1302. {
  1303. struct tty_struct *tty =
  1304. container_of(work, struct tty_struct, hangup_work);
  1305. struct tty_driver *driver = tty->driver;
  1306. if (tty->ops->cleanup)
  1307. tty->ops->cleanup(tty);
  1308. tty->magic = 0;
  1309. tty_driver_kref_put(driver);
  1310. module_put(driver->owner);
  1311. spin_lock(&tty_files_lock);
  1312. list_del_init(&tty->tty_files);
  1313. spin_unlock(&tty_files_lock);
  1314. put_pid(tty->pgrp);
  1315. put_pid(tty->session);
  1316. free_tty_struct(tty);
  1317. }
  1318. static void queue_release_one_tty(struct kref *kref)
  1319. {
  1320. struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
  1321. if (tty->ops->shutdown)
  1322. tty->ops->shutdown(tty);
  1323. else
  1324. tty_shutdown(tty);
  1325. /* The hangup queue is now free so we can reuse it rather than
  1326. waste a chunk of memory for each port */
  1327. INIT_WORK(&tty->hangup_work, release_one_tty);
  1328. schedule_work(&tty->hangup_work);
  1329. }
  1330. /**
  1331. * tty_kref_put - release a tty kref
  1332. * @tty: tty device
  1333. *
  1334. * Release a reference to a tty device and if need be let the kref
  1335. * layer destruct the object for us
  1336. */
  1337. void tty_kref_put(struct tty_struct *tty)
  1338. {
  1339. if (tty)
  1340. kref_put(&tty->kref, queue_release_one_tty);
  1341. }
  1342. EXPORT_SYMBOL(tty_kref_put);
  1343. /**
  1344. * release_tty - release tty structure memory
  1345. *
  1346. * Release both @tty and a possible linked partner (think pty pair),
  1347. * and decrement the refcount of the backing module.
  1348. *
  1349. * Locking:
  1350. * tty_mutex - sometimes only
  1351. * takes the file list lock internally when working on the list
  1352. * of ttys that the driver keeps.
  1353. * FIXME: should we require tty_mutex is held here ??
  1354. *
  1355. */
  1356. static void release_tty(struct tty_struct *tty, int idx)
  1357. {
  1358. /* This should always be true but check for the moment */
  1359. WARN_ON(tty->index != idx);
  1360. if (tty->link)
  1361. tty_kref_put(tty->link);
  1362. tty_kref_put(tty);
  1363. }
  1364. /**
  1365. * tty_release - vfs callback for close
  1366. * @inode: inode of tty
  1367. * @filp: file pointer for handle to tty
  1368. *
  1369. * Called the last time each file handle is closed that references
  1370. * this tty. There may however be several such references.
  1371. *
  1372. * Locking:
  1373. * Takes bkl. See tty_release_dev
  1374. *
  1375. * Even releasing the tty structures is a tricky business.. We have
  1376. * to be very careful that the structures are all released at the
  1377. * same time, as interrupts might otherwise get the wrong pointers.
  1378. *
  1379. * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
  1380. * lead to double frees or releasing memory still in use.
  1381. */
  1382. int tty_release(struct inode *inode, struct file *filp)
  1383. {
  1384. struct tty_struct *tty = file_tty(filp);
  1385. struct tty_struct *o_tty;
  1386. int pty_master, tty_closing, o_tty_closing, do_sleep;
  1387. int devpts;
  1388. int idx;
  1389. char buf[64];
  1390. if (tty_paranoia_check(tty, inode, "tty_release_dev"))
  1391. return 0;
  1392. tty_lock();
  1393. check_tty_count(tty, "tty_release_dev");
  1394. __tty_fasync(-1, filp, 0);
  1395. idx = tty->index;
  1396. pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1397. tty->driver->subtype == PTY_TYPE_MASTER);
  1398. devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
  1399. o_tty = tty->link;
  1400. #ifdef TTY_PARANOIA_CHECK
  1401. if (idx < 0 || idx >= tty->driver->num) {
  1402. printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
  1403. "free (%s)\n", tty->name);
  1404. tty_unlock();
  1405. return 0;
  1406. }
  1407. if (!devpts) {
  1408. if (tty != tty->driver->ttys[idx]) {
  1409. tty_unlock();
  1410. printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
  1411. "for (%s)\n", idx, tty->name);
  1412. return 0;
  1413. }
  1414. if (tty->termios != tty->driver->termios[idx]) {
  1415. tty_unlock();
  1416. printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
  1417. "for (%s)\n",
  1418. idx, tty->name);
  1419. return 0;
  1420. }
  1421. }
  1422. #endif
  1423. #ifdef TTY_DEBUG_HANGUP
  1424. printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
  1425. tty_name(tty, buf), tty->count);
  1426. #endif
  1427. #ifdef TTY_PARANOIA_CHECK
  1428. if (tty->driver->other &&
  1429. !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1430. if (o_tty != tty->driver->other->ttys[idx]) {
  1431. tty_unlock();
  1432. printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
  1433. "not o_tty for (%s)\n",
  1434. idx, tty->name);
  1435. return 0 ;
  1436. }
  1437. if (o_tty->termios != tty->driver->other->termios[idx]) {
  1438. tty_unlock();
  1439. printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
  1440. "not o_termios for (%s)\n",
  1441. idx, tty->name);
  1442. return 0;
  1443. }
  1444. if (o_tty->link != tty) {
  1445. tty_unlock();
  1446. printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
  1447. return 0;
  1448. }
  1449. }
  1450. #endif
  1451. if (tty->ops->close)
  1452. tty->ops->close(tty, filp);
  1453. tty_unlock();
  1454. /*
  1455. * Sanity check: if tty->count is going to zero, there shouldn't be
  1456. * any waiters on tty->read_wait or tty->write_wait. We test the
  1457. * wait queues and kick everyone out _before_ actually starting to
  1458. * close. This ensures that we won't block while releasing the tty
  1459. * structure.
  1460. *
  1461. * The test for the o_tty closing is necessary, since the master and
  1462. * slave sides may close in any order. If the slave side closes out
  1463. * first, its count will be one, since the master side holds an open.
  1464. * Thus this test wouldn't be triggered at the time the slave closes,
  1465. * so we do it now.
  1466. *
  1467. * Note that it's possible for the tty to be opened again while we're
  1468. * flushing out waiters. By recalculating the closing flags before
  1469. * each iteration we avoid any problems.
  1470. */
  1471. while (1) {
  1472. /* Guard against races with tty->count changes elsewhere and
  1473. opens on /dev/tty */
  1474. mutex_lock(&tty_mutex);
  1475. tty_lock();
  1476. tty_closing = tty->count <= 1;
  1477. o_tty_closing = o_tty &&
  1478. (o_tty->count <= (pty_master ? 1 : 0));
  1479. do_sleep = 0;
  1480. if (tty_closing) {
  1481. if (waitqueue_active(&tty->read_wait)) {
  1482. wake_up_poll(&tty->read_wait, POLLIN);
  1483. do_sleep++;
  1484. }
  1485. if (waitqueue_active(&tty->write_wait)) {
  1486. wake_up_poll(&tty->write_wait, POLLOUT);
  1487. do_sleep++;
  1488. }
  1489. }
  1490. if (o_tty_closing) {
  1491. if (waitqueue_active(&o_tty->read_wait)) {
  1492. wake_up_poll(&o_tty->read_wait, POLLIN);
  1493. do_sleep++;
  1494. }
  1495. if (waitqueue_active(&o_tty->write_wait)) {
  1496. wake_up_poll(&o_tty->write_wait, POLLOUT);
  1497. do_sleep++;
  1498. }
  1499. }
  1500. if (!do_sleep)
  1501. break;
  1502. printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
  1503. "active!\n", tty_name(tty, buf));
  1504. tty_unlock();
  1505. mutex_unlock(&tty_mutex);
  1506. schedule();
  1507. }
  1508. /*
  1509. * The closing flags are now consistent with the open counts on
  1510. * both sides, and we've completed the last operation that could
  1511. * block, so it's safe to proceed with closing.
  1512. */
  1513. if (pty_master) {
  1514. if (--o_tty->count < 0) {
  1515. printk(KERN_WARNING "tty_release_dev: bad pty slave count "
  1516. "(%d) for %s\n",
  1517. o_tty->count, tty_name(o_tty, buf));
  1518. o_tty->count = 0;
  1519. }
  1520. }
  1521. if (--tty->count < 0) {
  1522. printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
  1523. tty->count, tty_name(tty, buf));
  1524. tty->count = 0;
  1525. }
  1526. /*
  1527. * We've decremented tty->count, so we need to remove this file
  1528. * descriptor off the tty->tty_files list; this serves two
  1529. * purposes:
  1530. * - check_tty_count sees the correct number of file descriptors
  1531. * associated with this tty.
  1532. * - do_tty_hangup no longer sees this file descriptor as
  1533. * something that needs to be handled for hangups.
  1534. */
  1535. tty_del_file(filp);
  1536. /*
  1537. * Perform some housekeeping before deciding whether to return.
  1538. *
  1539. * Set the TTY_CLOSING flag if this was the last open. In the
  1540. * case of a pty we may have to wait around for the other side
  1541. * to close, and TTY_CLOSING makes sure we can't be reopened.
  1542. */
  1543. if (tty_closing)
  1544. set_bit(TTY_CLOSING, &tty->flags);
  1545. if (o_tty_closing)
  1546. set_bit(TTY_CLOSING, &o_tty->flags);
  1547. /*
  1548. * If _either_ side is closing, make sure there aren't any
  1549. * processes that still think tty or o_tty is their controlling
  1550. * tty.
  1551. */
  1552. if (tty_closing || o_tty_closing) {
  1553. read_lock(&tasklist_lock);
  1554. session_clear_tty(tty->session);
  1555. if (o_tty)
  1556. session_clear_tty(o_tty->session);
  1557. read_unlock(&tasklist_lock);
  1558. }
  1559. mutex_unlock(&tty_mutex);
  1560. /* check whether both sides are closing ... */
  1561. if (!tty_closing || (o_tty && !o_tty_closing)) {
  1562. tty_unlock();
  1563. return 0;
  1564. }
  1565. #ifdef TTY_DEBUG_HANGUP
  1566. printk(KERN_DEBUG "freeing tty structure...");
  1567. #endif
  1568. /*
  1569. * Ask the line discipline code to release its structures
  1570. */
  1571. tty_ldisc_release(tty, o_tty);
  1572. /*
  1573. * The release_tty function takes care of the details of clearing
  1574. * the slots and preserving the termios structure.
  1575. */
  1576. release_tty(tty, idx);
  1577. /* Make this pty number available for reallocation */
  1578. if (devpts)
  1579. devpts_kill_index(inode, idx);
  1580. tty_unlock();
  1581. return 0;
  1582. }
  1583. /**
  1584. * tty_open - open a tty device
  1585. * @inode: inode of device file
  1586. * @filp: file pointer to tty
  1587. *
  1588. * tty_open and tty_release keep up the tty count that contains the
  1589. * number of opens done on a tty. We cannot use the inode-count, as
  1590. * different inodes might point to the same tty.
  1591. *
  1592. * Open-counting is needed for pty masters, as well as for keeping
  1593. * track of serial lines: DTR is dropped when the last close happens.
  1594. * (This is not done solely through tty->count, now. - Ted 1/27/92)
  1595. *
  1596. * The termios state of a pty is reset on first open so that
  1597. * settings don't persist across reuse.
  1598. *
  1599. * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
  1600. * tty->count should protect the rest.
  1601. * ->siglock protects ->signal/->sighand
  1602. */
  1603. static int tty_open(struct inode *inode, struct file *filp)
  1604. {
  1605. struct tty_struct *tty = NULL;
  1606. int noctty, retval;
  1607. struct tty_driver *driver;
  1608. int index;
  1609. dev_t device = inode->i_rdev;
  1610. unsigned saved_flags = filp->f_flags;
  1611. nonseekable_open(inode, filp);
  1612. retry_open:
  1613. noctty = filp->f_flags & O_NOCTTY;
  1614. index = -1;
  1615. retval = 0;
  1616. mutex_lock(&tty_mutex);
  1617. tty_lock();
  1618. if (device == MKDEV(TTYAUX_MAJOR, 0)) {
  1619. tty = get_current_tty();
  1620. if (!tty) {
  1621. tty_unlock();
  1622. mutex_unlock(&tty_mutex);
  1623. return -ENXIO;
  1624. }
  1625. driver = tty_driver_kref_get(tty->driver);
  1626. index = tty->index;
  1627. filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
  1628. /* noctty = 1; */
  1629. /* FIXME: Should we take a driver reference ? */
  1630. tty_kref_put(tty);
  1631. goto got_driver;
  1632. }
  1633. #ifdef CONFIG_VT
  1634. if (device == MKDEV(TTY_MAJOR, 0)) {
  1635. extern struct tty_driver *console_driver;
  1636. driver = tty_driver_kref_get(console_driver);
  1637. index = fg_console;
  1638. noctty = 1;
  1639. goto got_driver;
  1640. }
  1641. #endif
  1642. if (device == MKDEV(TTYAUX_MAJOR, 1)) {
  1643. struct tty_driver *console_driver = console_device(&index);
  1644. if (console_driver) {
  1645. driver = tty_driver_kref_get(console_driver);
  1646. if (driver) {
  1647. /* Don't let /dev/console block */
  1648. filp->f_flags |= O_NONBLOCK;
  1649. noctty = 1;
  1650. goto got_driver;
  1651. }
  1652. }
  1653. tty_unlock();
  1654. mutex_unlock(&tty_mutex);
  1655. return -ENODEV;
  1656. }
  1657. driver = get_tty_driver(device, &index);
  1658. if (!driver) {
  1659. tty_unlock();
  1660. mutex_unlock(&tty_mutex);
  1661. return -ENODEV;
  1662. }
  1663. got_driver:
  1664. if (!tty) {
  1665. /* check whether we're reopening an existing tty */
  1666. tty = tty_driver_lookup_tty(driver, inode, index);
  1667. if (IS_ERR(tty)) {
  1668. tty_unlock();
  1669. mutex_unlock(&tty_mutex);
  1670. return PTR_ERR(tty);
  1671. }
  1672. }
  1673. if (tty) {
  1674. retval = tty_reopen(tty);
  1675. if (retval)
  1676. tty = ERR_PTR(retval);
  1677. } else
  1678. tty = tty_init_dev(driver, index, 0);
  1679. mutex_unlock(&tty_mutex);
  1680. tty_driver_kref_put(driver);
  1681. if (IS_ERR(tty)) {
  1682. tty_unlock();
  1683. return PTR_ERR(tty);
  1684. }
  1685. retval = tty_add_file(tty, filp);
  1686. if (retval) {
  1687. tty_unlock();
  1688. tty_release(inode, filp);
  1689. return retval;
  1690. }
  1691. check_tty_count(tty, "tty_open");
  1692. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1693. tty->driver->subtype == PTY_TYPE_MASTER)
  1694. noctty = 1;
  1695. #ifdef TTY_DEBUG_HANGUP
  1696. printk(KERN_DEBUG "opening %s...", tty->name);
  1697. #endif
  1698. if (tty->ops->open)
  1699. retval = tty->ops->open(tty, filp);
  1700. else
  1701. retval = -ENODEV;
  1702. filp->f_flags = saved_flags;
  1703. if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
  1704. !capable(CAP_SYS_ADMIN))
  1705. retval = -EBUSY;
  1706. if (retval) {
  1707. #ifdef TTY_DEBUG_HANGUP
  1708. printk(KERN_DEBUG "error %d in opening %s...", retval,
  1709. tty->name);
  1710. #endif
  1711. tty_unlock(); /* need to call tty_release without BTM */
  1712. tty_re