PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/char/tty_io.c

https://bitbucket.org/cresqo/cm7-p500-kernel
C | 3154 lines | 1917 code | 329 blank | 908 comment | 358 complexity | 8ccc16acba2a37f67047a5ba6a1233e2 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*
  2. * linux/drivers/char/tty_io.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
  8. * or rs-channels. It also implements echoing, cooked mode etc.
  9. *
  10. * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
  11. *
  12. * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
  13. * tty_struct and tty_queue structures. Previously there was an array
  14. * of 256 tty_struct's which was statically allocated, and the
  15. * tty_queue structures were allocated at boot time. Both are now
  16. * dynamically allocated only when the tty is open.
  17. *
  18. * Also restructured routines so that there is more of a separation
  19. * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
  20. * the low-level tty routines (serial.c, pty.c, console.c). This
  21. * makes for cleaner and more compact code. -TYT, 9/17/92
  22. *
  23. * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  24. * which can be dynamically activated and de-activated by the line
  25. * discipline handling modules (like SLIP).
  26. *
  27. * NOTE: pay no attention to the line discipline code (yet); its
  28. * interface is still subject to change in this version...
  29. * -- TYT, 1/31/92
  30. *
  31. * Added functionality to the OPOST tty handling. No delays, but all
  32. * other bits should be there.
  33. * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
  34. *
  35. * Rewrote canonical mode and added more termios flags.
  36. * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
  37. *
  38. * Reorganized FASYNC support so mouse code can share it.
  39. * -- ctm@ardi.com, 9Sep95
  40. *
  41. * New TIOCLINUX variants added.
  42. * -- mj@k332.feld.cvut.cz, 19-Nov-95
  43. *
  44. * Restrict vt switching via ioctl()
  45. * -- grif@cs.ucr.edu, 5-Dec-95
  46. *
  47. * Move console and virtual terminal code to more appropriate files,
  48. * implement CONFIG_VT and generalize console device interface.
  49. * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
  50. *
  51. * Rewrote tty_init_dev and tty_release_dev to eliminate races.
  52. * -- Bill Hawes <whawes@star.net>, June 97
  53. *
  54. * Added devfs support.
  55. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
  56. *
  57. * Added support for a Unix98-style ptmx device.
  58. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  59. *
  60. * Reduced memory usage for older ARM systems
  61. * -- Russell King <rmk@arm.linux.org.uk>
  62. *
  63. * Move do_SAK() into process context. Less stack use in devfs functions.
  64. * alloc_tty_struct() always uses kmalloc()
  65. * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
  66. */
  67. #include <linux/types.h>
  68. #include <linux/major.h>
  69. #include <linux/errno.h>
  70. #include <linux/signal.h>
  71. #include <linux/fcntl.h>
  72. #include <linux/sched.h>
  73. #include <linux/interrupt.h>
  74. #include <linux/tty.h>
  75. #include <linux/tty_driver.h>
  76. #include <linux/tty_flip.h>
  77. #include <linux/devpts_fs.h>
  78. #include <linux/file.h>
  79. #include <linux/fdtable.h>
  80. #include <linux/console.h>
  81. #include <linux/timer.h>
  82. #include <linux/ctype.h>
  83. #include <linux/kd.h>
  84. #include <linux/mm.h>
  85. #include <linux/string.h>
  86. #include <linux/slab.h>
  87. #include <linux/poll.h>
  88. #include <linux/proc_fs.h>
  89. #include <linux/init.h>
  90. #include <linux/module.h>
  91. #include <linux/smp_lock.h>
  92. #include <linux/device.h>
  93. #include <linux/wait.h>
  94. #include <linux/bitops.h>
  95. #include <linux/delay.h>
  96. #include <linux/seq_file.h>
  97. #include <linux/uaccess.h>
  98. #include <asm/system.h>
  99. #include <linux/kbd_kern.h>
  100. #include <linux/vt_kern.h>
  101. #include <linux/selection.h>
  102. #include <linux/kmod.h>
  103. #include <linux/nsproxy.h>
  104. #undef TTY_DEBUG_HANGUP
  105. #define TTY_PARANOIA_CHECK 1
  106. #define CHECK_TTY_COUNT 1
  107. struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
  108. .c_iflag = ICRNL | IXON,
  109. .c_oflag = OPOST | ONLCR,
  110. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  111. .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
  112. ECHOCTL | ECHOKE | IEXTEN,
  113. .c_cc = INIT_C_CC,
  114. .c_ispeed = 38400,
  115. .c_ospeed = 38400
  116. };
  117. EXPORT_SYMBOL(tty_std_termios);
  118. /* This list gets poked at by procfs and various bits of boot up code. This
  119. could do with some rationalisation such as pulling the tty proc function
  120. into this file */
  121. LIST_HEAD(tty_drivers); /* linked list of tty drivers */
  122. /* Mutex to protect creating and releasing a tty. This is shared with
  123. vt.c for deeply disgusting hack reasons */
  124. DEFINE_MUTEX(tty_mutex);
  125. EXPORT_SYMBOL(tty_mutex);
  126. static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
  127. static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
  128. ssize_t redirected_tty_write(struct file *, const char __user *,
  129. size_t, loff_t *);
  130. static unsigned int tty_poll(struct file *, poll_table *);
  131. static int tty_open(struct inode *, struct file *);
  132. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  133. #ifdef CONFIG_COMPAT
  134. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  135. unsigned long arg);
  136. #else
  137. #define tty_compat_ioctl NULL
  138. #endif
  139. static int tty_fasync(int fd, struct file *filp, int on);
  140. static void release_tty(struct tty_struct *tty, int idx);
  141. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
  142. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
  143. /**
  144. * alloc_tty_struct - allocate a tty object
  145. *
  146. * Return a new empty tty structure. The data fields have not
  147. * been initialized in any way but has been zeroed
  148. *
  149. * Locking: none
  150. */
  151. struct tty_struct *alloc_tty_struct(void)
  152. {
  153. return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
  154. }
  155. /**
  156. * free_tty_struct - free a disused tty
  157. * @tty: tty struct to free
  158. *
  159. * Free the write buffers, tty queue and tty memory itself.
  160. *
  161. * Locking: none. Must be called after tty is definitely unused
  162. */
  163. void free_tty_struct(struct tty_struct *tty)
  164. {
  165. kfree(tty->write_buf);
  166. tty_buffer_free_all(tty);
  167. kfree(tty);
  168. }
  169. #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
  170. /**
  171. * tty_name - return tty naming
  172. * @tty: tty structure
  173. * @buf: buffer for output
  174. *
  175. * Convert a tty structure into a name. The name reflects the kernel
  176. * naming policy and if udev is in use may not reflect user space
  177. *
  178. * Locking: none
  179. */
  180. char *tty_name(struct tty_struct *tty, char *buf)
  181. {
  182. if (!tty) /* Hmm. NULL pointer. That's fun. */
  183. strcpy(buf, "NULL tty");
  184. else
  185. strcpy(buf, tty->name);
  186. return buf;
  187. }
  188. EXPORT_SYMBOL(tty_name);
  189. int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
  190. const char *routine)
  191. {
  192. #ifdef TTY_PARANOIA_CHECK
  193. if (!tty) {
  194. printk(KERN_WARNING
  195. "null TTY for (%d:%d) in %s\n",
  196. imajor(inode), iminor(inode), routine);
  197. return 1;
  198. }
  199. if (tty->magic != TTY_MAGIC) {
  200. printk(KERN_WARNING
  201. "bad magic number for tty struct (%d:%d) in %s\n",
  202. imajor(inode), iminor(inode), routine);
  203. return 1;
  204. }
  205. #endif
  206. return 0;
  207. }
  208. static int check_tty_count(struct tty_struct *tty, const char *routine)
  209. {
  210. #ifdef CHECK_TTY_COUNT
  211. struct list_head *p;
  212. int count = 0;
  213. file_list_lock();
  214. list_for_each(p, &tty->tty_files) {
  215. count++;
  216. }
  217. file_list_unlock();
  218. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  219. tty->driver->subtype == PTY_TYPE_SLAVE &&
  220. tty->link && tty->link->count)
  221. count++;
  222. if (tty->count != count) {
  223. printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
  224. "!= #fd's(%d) in %s\n",
  225. tty->name, tty->count, count, routine);
  226. return count;
  227. }
  228. #endif
  229. return 0;
  230. }
  231. /**
  232. * get_tty_driver - find device of a tty
  233. * @dev_t: device identifier
  234. * @index: returns the index of the tty
  235. *
  236. * This routine returns a tty driver structure, given a device number
  237. * and also passes back the index number.
  238. *
  239. * Locking: caller must hold tty_mutex
  240. */
  241. static struct tty_driver *get_tty_driver(dev_t device, int *index)
  242. {
  243. struct tty_driver *p;
  244. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  245. dev_t base = MKDEV(p->major, p->minor_start);
  246. if (device < base || device >= base + p->num)
  247. continue;
  248. *index = device - base;
  249. return tty_driver_kref_get(p);
  250. }
  251. return NULL;
  252. }
  253. #ifdef CONFIG_CONSOLE_POLL
  254. /**
  255. * tty_find_polling_driver - find device of a polled tty
  256. * @name: name string to match
  257. * @line: pointer to resulting tty line nr
  258. *
  259. * This routine returns a tty driver structure, given a name
  260. * and the condition that the tty driver is capable of polled
  261. * operation.
  262. */
  263. struct tty_driver *tty_find_polling_driver(char *name, int *line)
  264. {
  265. struct tty_driver *p, *res = NULL;
  266. int tty_line = 0;
  267. int len;
  268. char *str, *stp;
  269. for (str = name; *str; str++)
  270. if ((*str >= '0' && *str <= '9') || *str == ',')
  271. break;
  272. if (!*str)
  273. return NULL;
  274. len = str - name;
  275. tty_line = simple_strtoul(str, &str, 10);
  276. mutex_lock(&tty_mutex);
  277. /* Search through the tty devices to look for a match */
  278. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  279. if (strncmp(name, p->name, len) != 0)
  280. continue;
  281. stp = str;
  282. if (*stp == ',')
  283. stp++;
  284. if (*stp == '\0')
  285. stp = NULL;
  286. if (tty_line >= 0 && tty_line <= p->num && p->ops &&
  287. p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
  288. res = tty_driver_kref_get(p);
  289. *line = tty_line;
  290. break;
  291. }
  292. }
  293. mutex_unlock(&tty_mutex);
  294. return res;
  295. }
  296. EXPORT_SYMBOL_GPL(tty_find_polling_driver);
  297. #endif
  298. /**
  299. * tty_check_change - check for POSIX terminal changes
  300. * @tty: tty to check
  301. *
  302. * If we try to write to, or set the state of, a terminal and we're
  303. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  304. * ignored, go ahead and perform the operation. (POSIX 7.2)
  305. *
  306. * Locking: ctrl_lock
  307. */
  308. int tty_check_change(struct tty_struct *tty)
  309. {
  310. unsigned long flags;
  311. int ret = 0;
  312. if (current->signal->tty != tty)
  313. return 0;
  314. spin_lock_irqsave(&tty->ctrl_lock, flags);
  315. if (!tty->pgrp) {
  316. printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
  317. goto out_unlock;
  318. }
  319. if (task_pgrp(current) == tty->pgrp)
  320. goto out_unlock;
  321. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  322. if (is_ignored(SIGTTOU))
  323. goto out;
  324. if (is_current_pgrp_orphaned()) {
  325. ret = -EIO;
  326. goto out;
  327. }
  328. kill_pgrp(task_pgrp(current), SIGTTOU, 1);
  329. set_thread_flag(TIF_SIGPENDING);
  330. ret = -ERESTARTSYS;
  331. out:
  332. return ret;
  333. out_unlock:
  334. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  335. return ret;
  336. }
  337. EXPORT_SYMBOL(tty_check_change);
  338. static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
  339. size_t count, loff_t *ppos)
  340. {
  341. return 0;
  342. }
  343. static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
  344. size_t count, loff_t *ppos)
  345. {
  346. return -EIO;
  347. }
  348. /* No kernel lock held - none needed ;) */
  349. static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
  350. {
  351. return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
  352. }
  353. static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
  354. unsigned long arg)
  355. {
  356. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  357. }
  358. static long hung_up_tty_compat_ioctl(struct file *file,
  359. unsigned int cmd, unsigned long arg)
  360. {
  361. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  362. }
  363. static const struct file_operations tty_fops = {
  364. .llseek = no_llseek,
  365. .read = tty_read,
  366. .write = tty_write,
  367. .poll = tty_poll,
  368. .unlocked_ioctl = tty_ioctl,
  369. .compat_ioctl = tty_compat_ioctl,
  370. .open = tty_open,
  371. .release = tty_release,
  372. .fasync = tty_fasync,
  373. };
  374. static const struct file_operations console_fops = {
  375. .llseek = no_llseek,
  376. .read = tty_read,
  377. .write = redirected_tty_write,
  378. .poll = tty_poll,
  379. .unlocked_ioctl = tty_ioctl,
  380. .compat_ioctl = tty_compat_ioctl,
  381. .open = tty_open,
  382. .release = tty_release,
  383. .fasync = tty_fasync,
  384. };
  385. static const struct file_operations hung_up_tty_fops = {
  386. .llseek = no_llseek,
  387. .read = hung_up_tty_read,
  388. .write = hung_up_tty_write,
  389. .poll = hung_up_tty_poll,
  390. .unlocked_ioctl = hung_up_tty_ioctl,
  391. .compat_ioctl = hung_up_tty_compat_ioctl,
  392. .release = tty_release,
  393. };
  394. static DEFINE_SPINLOCK(redirect_lock);
  395. static struct file *redirect;
  396. /**
  397. * tty_wakeup - request more data
  398. * @tty: terminal
  399. *
  400. * Internal and external helper for wakeups of tty. This function
  401. * informs the line discipline if present that the driver is ready
  402. * to receive more output data.
  403. */
  404. void tty_wakeup(struct tty_struct *tty)
  405. {
  406. struct tty_ldisc *ld;
  407. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
  408. ld = tty_ldisc_ref(tty);
  409. if (ld) {
  410. if (ld->ops->write_wakeup)
  411. ld->ops->write_wakeup(tty);
  412. tty_ldisc_deref(ld);
  413. }
  414. }
  415. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  416. }
  417. EXPORT_SYMBOL_GPL(tty_wakeup);
  418. /**
  419. * do_tty_hangup - actual handler for hangup events
  420. * @work: tty device
  421. *
  422. * This can be called by the "eventd" kernel thread. That is process
  423. * synchronous but doesn't hold any locks, so we need to make sure we
  424. * have the appropriate locks for what we're doing.
  425. *
  426. * The hangup event clears any pending redirections onto the hung up
  427. * device. It ensures future writes will error and it does the needed
  428. * line discipline hangup and signal delivery. The tty object itself
  429. * remains intact.
  430. *
  431. * Locking:
  432. * BKL
  433. * redirect lock for undoing redirection
  434. * file list lock for manipulating list of ttys
  435. * tty_ldisc_lock from called functions
  436. * termios_mutex resetting termios data
  437. * tasklist_lock to walk task list for hangup event
  438. * ->siglock to protect ->signal/->sighand
  439. */
  440. static void do_tty_hangup(struct work_struct *work)
  441. {
  442. struct tty_struct *tty =
  443. container_of(work, struct tty_struct, hangup_work);
  444. struct file *cons_filp = NULL;
  445. struct file *filp, *f = NULL;
  446. struct task_struct *p;
  447. int closecount = 0, n;
  448. unsigned long flags;
  449. int refs = 0;
  450. if (!tty)
  451. return;
  452. spin_lock(&redirect_lock);
  453. if (redirect && redirect->private_data == tty) {
  454. f = redirect;
  455. redirect = NULL;
  456. }
  457. spin_unlock(&redirect_lock);
  458. /* inuse_filps is protected by the single kernel lock */
  459. lock_kernel();
  460. check_tty_count(tty, "do_tty_hangup");
  461. file_list_lock();
  462. /* This breaks for file handles being sent over AF_UNIX sockets ? */
  463. list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
  464. if (filp->f_op->write == redirected_tty_write)
  465. cons_filp = filp;
  466. if (filp->f_op->write != tty_write)
  467. continue;
  468. closecount++;
  469. tty_fasync(-1, filp, 0); /* can't block */
  470. filp->f_op = &hung_up_tty_fops;
  471. }
  472. file_list_unlock();
  473. tty_ldisc_hangup(tty);
  474. read_lock(&tasklist_lock);
  475. if (tty->session) {
  476. do_each_pid_task(tty->session, PIDTYPE_SID, p) {
  477. spin_lock_irq(&p->sighand->siglock);
  478. if (p->signal->tty == tty) {
  479. p->signal->tty = NULL;
  480. /* We defer the dereferences outside fo
  481. the tasklist lock */
  482. refs++;
  483. }
  484. if (!p->signal->leader) {
  485. spin_unlock_irq(&p->sighand->siglock);
  486. continue;
  487. }
  488. __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
  489. __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
  490. put_pid(p->signal->tty_old_pgrp); /* A noop */
  491. spin_lock_irqsave(&tty->ctrl_lock, flags);
  492. if (tty->pgrp)
  493. p->signal->tty_old_pgrp = get_pid(tty->pgrp);
  494. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  495. spin_unlock_irq(&p->sighand->siglock);
  496. } while_each_pid_task(tty->session, PIDTYPE_SID, p);
  497. }
  498. read_unlock(&tasklist_lock);
  499. spin_lock_irqsave(&tty->ctrl_lock, flags);
  500. clear_bit(TTY_THROTTLED, &tty->flags);
  501. clear_bit(TTY_PUSH, &tty->flags);
  502. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  503. put_pid(tty->session);
  504. put_pid(tty->pgrp);
  505. tty->session = NULL;
  506. tty->pgrp = NULL;
  507. tty->ctrl_status = 0;
  508. set_bit(TTY_HUPPED, &tty->flags);
  509. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  510. /* Account for the p->signal references we killed */
  511. while (refs--)
  512. tty_kref_put(tty);
  513. /*
  514. * If one of the devices matches a console pointer, we
  515. * cannot just call hangup() because that will cause
  516. * tty->count and state->count to go out of sync.
  517. * So we just call close() the right number of times.
  518. */
  519. if (cons_filp) {
  520. if (tty->ops->close)
  521. for (n = 0; n < closecount; n++)
  522. tty->ops->close(tty, cons_filp);
  523. } else if (tty->ops->hangup)
  524. (tty->ops->hangup)(tty);
  525. /*
  526. * We don't want to have driver/ldisc interactions beyond
  527. * the ones we did here. The driver layer expects no
  528. * calls after ->hangup() from the ldisc side. However we
  529. * can't yet guarantee all that.
  530. */
  531. set_bit(TTY_HUPPED, &tty->flags);
  532. tty_ldisc_enable(tty);
  533. unlock_kernel();
  534. if (f)
  535. fput(f);
  536. }
  537. /**
  538. * tty_hangup - trigger a hangup event
  539. * @tty: tty to hangup
  540. *
  541. * A carrier loss (virtual or otherwise) has occurred on this like
  542. * schedule a hangup sequence to run after this event.
  543. */
  544. void tty_hangup(struct tty_struct *tty)
  545. {
  546. #ifdef TTY_DEBUG_HANGUP
  547. char buf[64];
  548. printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
  549. #endif
  550. schedule_work(&tty->hangup_work);
  551. }
  552. EXPORT_SYMBOL(tty_hangup);
  553. /**
  554. * tty_vhangup - process vhangup
  555. * @tty: tty to hangup
  556. *
  557. * The user has asked via system call for the terminal to be hung up.
  558. * We do this synchronously so that when the syscall returns the process
  559. * is complete. That guarantee is necessary for security reasons.
  560. */
  561. void tty_vhangup(struct tty_struct *tty)
  562. {
  563. #ifdef TTY_DEBUG_HANGUP
  564. char buf[64];
  565. printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
  566. #endif
  567. do_tty_hangup(&tty->hangup_work);
  568. }
  569. EXPORT_SYMBOL(tty_vhangup);
  570. /**
  571. * tty_vhangup_self - process vhangup for own ctty
  572. *
  573. * Perform a vhangup on the current controlling tty
  574. */
  575. void tty_vhangup_self(void)
  576. {
  577. struct tty_struct *tty;
  578. tty = get_current_tty();
  579. if (tty) {
  580. tty_vhangup(tty);
  581. tty_kref_put(tty);
  582. }
  583. }
  584. /**
  585. * tty_hung_up_p - was tty hung up
  586. * @filp: file pointer of tty
  587. *
  588. * Return true if the tty has been subject to a vhangup or a carrier
  589. * loss
  590. */
  591. int tty_hung_up_p(struct file *filp)
  592. {
  593. return (filp->f_op == &hung_up_tty_fops);
  594. }
  595. EXPORT_SYMBOL(tty_hung_up_p);
  596. static void session_clear_tty(struct pid *session)
  597. {
  598. struct task_struct *p;
  599. do_each_pid_task(session, PIDTYPE_SID, p) {
  600. proc_clear_tty(p);
  601. } while_each_pid_task(session, PIDTYPE_SID, p);
  602. }
  603. /**
  604. * disassociate_ctty - disconnect controlling tty
  605. * @on_exit: true if exiting so need to "hang up" the session
  606. *
  607. * This function is typically called only by the session leader, when
  608. * it wants to disassociate itself from its controlling tty.
  609. *
  610. * It performs the following functions:
  611. * (1) Sends a SIGHUP and SIGCONT to the foreground process group
  612. * (2) Clears the tty from being controlling the session
  613. * (3) Clears the controlling tty for all processes in the
  614. * session group.
  615. *
  616. * The argument on_exit is set to 1 if called when a process is
  617. * exiting; it is 0 if called by the ioctl TIOCNOTTY.
  618. *
  619. * Locking:
  620. * BKL is taken for hysterical raisins
  621. * tty_mutex is taken to protect tty
  622. * ->siglock is taken to protect ->signal/->sighand
  623. * tasklist_lock is taken to walk process list for sessions
  624. * ->siglock is taken to protect ->signal/->sighand
  625. */
  626. void disassociate_ctty(int on_exit)
  627. {
  628. struct tty_struct *tty;
  629. struct pid *tty_pgrp = NULL;
  630. if (!current->signal->leader)
  631. return;
  632. tty = get_current_tty();
  633. if (tty) {
  634. tty_pgrp = get_pid(tty->pgrp);
  635. lock_kernel();
  636. if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
  637. tty_vhangup(tty);
  638. unlock_kernel();
  639. tty_kref_put(tty);
  640. } else if (on_exit) {
  641. struct pid *old_pgrp;
  642. spin_lock_irq(&current->sighand->siglock);
  643. old_pgrp = current->signal->tty_old_pgrp;
  644. current->signal->tty_old_pgrp = NULL;
  645. spin_unlock_irq(&current->sighand->siglock);
  646. if (old_pgrp) {
  647. kill_pgrp(old_pgrp, SIGHUP, on_exit);
  648. kill_pgrp(old_pgrp, SIGCONT, on_exit);
  649. put_pid(old_pgrp);
  650. }
  651. return;
  652. }
  653. if (tty_pgrp) {
  654. kill_pgrp(tty_pgrp, SIGHUP, on_exit);
  655. if (!on_exit)
  656. kill_pgrp(tty_pgrp, SIGCONT, on_exit);
  657. put_pid(tty_pgrp);
  658. }
  659. spin_lock_irq(&current->sighand->siglock);
  660. put_pid(current->signal->tty_old_pgrp);
  661. current->signal->tty_old_pgrp = NULL;
  662. spin_unlock_irq(&current->sighand->siglock);
  663. tty = get_current_tty();
  664. if (tty) {
  665. unsigned long flags;
  666. spin_lock_irqsave(&tty->ctrl_lock, flags);
  667. put_pid(tty->session);
  668. put_pid(tty->pgrp);
  669. tty->session = NULL;
  670. tty->pgrp = NULL;
  671. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  672. tty_kref_put(tty);
  673. } else {
  674. #ifdef TTY_DEBUG_HANGUP
  675. printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
  676. " = NULL", tty);
  677. #endif
  678. }
  679. /* Now clear signal->tty under the lock */
  680. read_lock(&tasklist_lock);
  681. session_clear_tty(task_session(current));
  682. read_unlock(&tasklist_lock);
  683. }
  684. /**
  685. *
  686. * no_tty - Ensure the current process does not have a controlling tty
  687. */
  688. void no_tty(void)
  689. {
  690. struct task_struct *tsk = current;
  691. lock_kernel();
  692. disassociate_ctty(0);
  693. unlock_kernel();
  694. proc_clear_tty(tsk);
  695. }
  696. /**
  697. * stop_tty - propagate flow control
  698. * @tty: tty to stop
  699. *
  700. * Perform flow control to the driver. For PTY/TTY pairs we
  701. * must also propagate the TIOCKPKT status. May be called
  702. * on an already stopped device and will not re-call the driver
  703. * method.
  704. *
  705. * This functionality is used by both the line disciplines for
  706. * halting incoming flow and by the driver. It may therefore be
  707. * called from any context, may be under the tty atomic_write_lock
  708. * but not always.
  709. *
  710. * Locking:
  711. * Uses the tty control lock internally
  712. */
  713. void stop_tty(struct tty_struct *tty)
  714. {
  715. unsigned long flags;
  716. spin_lock_irqsave(&tty->ctrl_lock, flags);
  717. if (tty->stopped) {
  718. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  719. return;
  720. }
  721. tty->stopped = 1;
  722. if (tty->link && tty->link->packet) {
  723. tty->ctrl_status &= ~TIOCPKT_START;
  724. tty->ctrl_status |= TIOCPKT_STOP;
  725. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  726. }
  727. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  728. if (tty->ops->stop)
  729. (tty->ops->stop)(tty);
  730. }
  731. EXPORT_SYMBOL(stop_tty);
  732. /**
  733. * start_tty - propagate flow control
  734. * @tty: tty to start
  735. *
  736. * Start a tty that has been stopped if at all possible. Perform
  737. * any necessary wakeups and propagate the TIOCPKT status. If this
  738. * is the tty was previous stopped and is being started then the
  739. * driver start method is invoked and the line discipline woken.
  740. *
  741. * Locking:
  742. * ctrl_lock
  743. */
  744. void start_tty(struct tty_struct *tty)
  745. {
  746. unsigned long flags;
  747. spin_lock_irqsave(&tty->ctrl_lock, flags);
  748. if (!tty->stopped || tty->flow_stopped) {
  749. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  750. return;
  751. }
  752. tty->stopped = 0;
  753. if (tty->link && tty->link->packet) {
  754. tty->ctrl_status &= ~TIOCPKT_STOP;
  755. tty->ctrl_status |= TIOCPKT_START;
  756. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  757. }
  758. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  759. if (tty->ops->start)
  760. (tty->ops->start)(tty);
  761. /* If we have a running line discipline it may need kicking */
  762. tty_wakeup(tty);
  763. }
  764. EXPORT_SYMBOL(start_tty);
  765. /**
  766. * tty_read - read method for tty device files
  767. * @file: pointer to tty file
  768. * @buf: user buffer
  769. * @count: size of user buffer
  770. * @ppos: unused
  771. *
  772. * Perform the read system call function on this terminal device. Checks
  773. * for hung up devices before calling the line discipline method.
  774. *
  775. * Locking:
  776. * Locks the line discipline internally while needed. Multiple
  777. * read calls may be outstanding in parallel.
  778. */
  779. static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
  780. loff_t *ppos)
  781. {
  782. int i;
  783. struct tty_struct *tty;
  784. struct inode *inode;
  785. struct tty_ldisc *ld;
  786. tty = (struct tty_struct *)file->private_data;
  787. inode = file->f_path.dentry->d_inode;
  788. if (tty_paranoia_check(tty, inode, "tty_read"))
  789. return -EIO;
  790. if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
  791. return -EIO;
  792. /* We want to wait for the line discipline to sort out in this
  793. situation */
  794. ld = tty_ldisc_ref_wait(tty);
  795. if (ld->ops->read)
  796. i = (ld->ops->read)(tty, file, buf, count);
  797. else
  798. i = -EIO;
  799. tty_ldisc_deref(ld);
  800. if (i > 0)
  801. inode->i_atime = current_fs_time(inode->i_sb);
  802. return i;
  803. }
  804. void tty_write_unlock(struct tty_struct *tty)
  805. {
  806. mutex_unlock(&tty->atomic_write_lock);
  807. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  808. }
  809. int tty_write_lock(struct tty_struct *tty, int ndelay)
  810. {
  811. if (!mutex_trylock(&tty->atomic_write_lock)) {
  812. if (ndelay)
  813. return -EAGAIN;
  814. if (mutex_lock_interruptible(&tty->atomic_write_lock))
  815. return -ERESTARTSYS;
  816. }
  817. return 0;
  818. }
  819. /*
  820. * Split writes up in sane blocksizes to avoid
  821. * denial-of-service type attacks
  822. */
  823. static inline ssize_t do_tty_write(
  824. ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
  825. struct tty_struct *tty,
  826. struct file *file,
  827. const char __user *buf,
  828. size_t count)
  829. {
  830. ssize_t ret, written = 0;
  831. unsigned int chunk;
  832. ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
  833. if (ret < 0)
  834. return ret;
  835. /*
  836. * We chunk up writes into a temporary buffer. This
  837. * simplifies low-level drivers immensely, since they
  838. * don't have locking issues and user mode accesses.
  839. *
  840. * But if TTY_NO_WRITE_SPLIT is set, we should use a
  841. * big chunk-size..
  842. *
  843. * The default chunk-size is 2kB, because the NTTY
  844. * layer has problems with bigger chunks. It will
  845. * claim to be able to handle more characters than
  846. * it actually does.
  847. *
  848. * FIXME: This can probably go away now except that 64K chunks
  849. * are too likely to fail unless switched to vmalloc...
  850. */
  851. chunk = 2048;
  852. if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
  853. chunk = 65536;
  854. if (count < chunk)
  855. chunk = count;
  856. /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
  857. if (tty->write_cnt < chunk) {
  858. unsigned char *buf_chunk;
  859. if (chunk < 1024)
  860. chunk = 1024;
  861. buf_chunk = kmalloc(chunk, GFP_KERNEL);
  862. if (!buf_chunk) {
  863. ret = -ENOMEM;
  864. goto out;
  865. }
  866. kfree(tty->write_buf);
  867. tty->write_cnt = chunk;
  868. tty->write_buf = buf_chunk;
  869. }
  870. /* Do the write .. */
  871. for (;;) {
  872. size_t size = count;
  873. if (size > chunk)
  874. size = chunk;
  875. ret = -EFAULT;
  876. if (copy_from_user(tty->write_buf, buf, size))
  877. break;
  878. ret = write(tty, file, tty->write_buf, size);
  879. if (ret <= 0)
  880. break;
  881. written += ret;
  882. buf += ret;
  883. count -= ret;
  884. if (!count)
  885. break;
  886. ret = -ERESTARTSYS;
  887. if (signal_pending(current))
  888. break;
  889. cond_resched();
  890. }
  891. if (written) {
  892. struct inode *inode = file->f_path.dentry->d_inode;
  893. inode->i_mtime = current_fs_time(inode->i_sb);
  894. ret = written;
  895. }
  896. out:
  897. tty_write_unlock(tty);
  898. return ret;
  899. }
  900. /**
  901. * tty_write_message - write a message to a certain tty, not just the console.
  902. * @tty: the destination tty_struct
  903. * @msg: the message to write
  904. *
  905. * This is used for messages that need to be redirected to a specific tty.
  906. * We don't put it into the syslog queue right now maybe in the future if
  907. * really needed.
  908. *
  909. * We must still hold the BKL and test the CLOSING flag for the moment.
  910. */
  911. void tty_write_message(struct tty_struct *tty, char *msg)
  912. {
  913. if (tty) {
  914. mutex_lock(&tty->atomic_write_lock);
  915. lock_kernel();
  916. if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
  917. unlock_kernel();
  918. tty->ops->write(tty, msg, strlen(msg));
  919. } else
  920. unlock_kernel();
  921. tty_write_unlock(tty);
  922. }
  923. return;
  924. }
  925. /**
  926. * tty_write - write method for tty device file
  927. * @file: tty file pointer
  928. * @buf: user data to write
  929. * @count: bytes to write
  930. * @ppos: unused
  931. *
  932. * Write data to a tty device via the line discipline.
  933. *
  934. * Locking:
  935. * Locks the line discipline as required
  936. * Writes to the tty driver are serialized by the atomic_write_lock
  937. * and are then processed in chunks to the device. The line discipline
  938. * write method will not be invoked in parallel for each device.
  939. */
  940. static ssize_t tty_write(struct file *file, const char __user *buf,
  941. size_t count, loff_t *ppos)
  942. {
  943. struct tty_struct *tty;
  944. struct inode *inode = file->f_path.dentry->d_inode;
  945. ssize_t ret;
  946. struct tty_ldisc *ld;
  947. tty = (struct tty_struct *)file->private_data;
  948. if (tty_paranoia_check(tty, inode, "tty_write"))
  949. return -EIO;
  950. if (!tty || !tty->ops->write ||
  951. (test_bit(TTY_IO_ERROR, &tty->flags)))
  952. return -EIO;
  953. /* Short term debug to catch buggy drivers */
  954. if (tty->ops->write_room == NULL)
  955. printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
  956. tty->driver->name);
  957. ld = tty_ldisc_ref_wait(tty);
  958. if (!ld->ops->write)
  959. ret = -EIO;
  960. else
  961. ret = do_tty_write(ld->ops->write, tty, file, buf, count);
  962. tty_ldisc_deref(ld);
  963. return ret;
  964. }
  965. ssize_t redirected_tty_write(struct file *file, const char __user *buf,
  966. size_t count, loff_t *ppos)
  967. {
  968. struct file *p = NULL;
  969. spin_lock(&redirect_lock);
  970. if (redirect) {
  971. get_file(redirect);
  972. p = redirect;
  973. }
  974. spin_unlock(&redirect_lock);
  975. if (p) {
  976. ssize_t res;
  977. res = vfs_write(p, buf, count, &p->f_pos);
  978. fput(p);
  979. return res;
  980. }
  981. return tty_write(file, buf, count, ppos);
  982. }
  983. static char ptychar[] = "pqrstuvwxyzabcde";
  984. /**
  985. * pty_line_name - generate name for a pty
  986. * @driver: the tty driver in use
  987. * @index: the minor number
  988. * @p: output buffer of at least 6 bytes
  989. *
  990. * Generate a name from a driver reference and write it to the output
  991. * buffer.
  992. *
  993. * Locking: None
  994. */
  995. static void pty_line_name(struct tty_driver *driver, int index, char *p)
  996. {
  997. int i = index + driver->name_base;
  998. /* ->name is initialized to "ttyp", but "tty" is expected */
  999. sprintf(p, "%s%c%x",
  1000. driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
  1001. ptychar[i >> 4 & 0xf], i & 0xf);
  1002. }
  1003. /**
  1004. * tty_line_name - generate name for a tty
  1005. * @driver: the tty driver in use
  1006. * @index: the minor number
  1007. * @p: output buffer of at least 7 bytes
  1008. *
  1009. * Generate a name from a driver reference and write it to the output
  1010. * buffer.
  1011. *
  1012. * Locking: None
  1013. */
  1014. static void tty_line_name(struct tty_driver *driver, int index, char *p)
  1015. {
  1016. sprintf(p, "%s%d", driver->name, index + driver->name_base);
  1017. }
  1018. /**
  1019. * tty_driver_lookup_tty() - find an existing tty, if any
  1020. * @driver: the driver for the tty
  1021. * @idx: the minor number
  1022. *
  1023. * Return the tty, if found or ERR_PTR() otherwise.
  1024. *
  1025. * Locking: tty_mutex must be held. If tty is found, the mutex must
  1026. * be held until the 'fast-open' is also done. Will change once we
  1027. * have refcounting in the driver and per driver locking
  1028. */
  1029. static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
  1030. struct inode *inode, int idx)
  1031. {
  1032. struct tty_struct *tty;
  1033. if (driver->ops->lookup)
  1034. return driver->ops->lookup(driver, inode, idx);
  1035. tty = driver->ttys[idx];
  1036. return tty;
  1037. }
  1038. /**
  1039. * tty_init_termios - helper for termios setup
  1040. * @tty: the tty to set up
  1041. *
  1042. * Initialise the termios structures for this tty. Thus runs under
  1043. * the tty_mutex currently so we can be relaxed about ordering.
  1044. */
  1045. int tty_init_termios(struct tty_struct *tty)
  1046. {
  1047. struct ktermios *tp;
  1048. int idx = tty->index;
  1049. tp = tty->driver->termios[idx];
  1050. if (tp == NULL) {
  1051. tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  1052. if (tp == NULL)
  1053. return -ENOMEM;
  1054. memcpy(tp, &tty->driver->init_termios,
  1055. sizeof(struct ktermios));
  1056. tty->driver->termios[idx] = tp;
  1057. }
  1058. tty->termios = tp;
  1059. tty->termios_locked = tp + 1;
  1060. /* Compatibility until drivers always set this */
  1061. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  1062. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  1063. return 0;
  1064. }
  1065. EXPORT_SYMBOL_GPL(tty_init_termios);
  1066. /**
  1067. * tty_driver_install_tty() - install a tty entry in the driver
  1068. * @driver: the driver for the tty
  1069. * @tty: the tty
  1070. *
  1071. * Install a tty object into the driver tables. The tty->index field
  1072. * will be set by the time this is called. This method is responsible
  1073. * for ensuring any need additional structures are allocated and
  1074. * configured.
  1075. *
  1076. * Locking: tty_mutex for now
  1077. */
  1078. static int tty_driver_install_tty(struct tty_driver *driver,
  1079. struct tty_struct *tty)
  1080. {
  1081. int idx = tty->index;
  1082. int ret;
  1083. if (driver->ops->install) {
  1084. lock_kernel();
  1085. ret = driver->ops->install(driver, tty);
  1086. unlock_kernel();
  1087. return ret;
  1088. }
  1089. if (tty_init_termios(tty) == 0) {
  1090. lock_kernel();
  1091. tty_driver_kref_get(driver);
  1092. tty->count++;
  1093. driver->ttys[idx] = tty;
  1094. unlock_kernel();
  1095. return 0;
  1096. }
  1097. return -ENOMEM;
  1098. }
  1099. /**
  1100. * tty_driver_remove_tty() - remove a tty from the driver tables
  1101. * @driver: the driver for the tty
  1102. * @idx: the minor number
  1103. *
  1104. * Remvoe a tty object from the driver tables. The tty->index field
  1105. * will be set by the time this is called.
  1106. *
  1107. * Locking: tty_mutex for now
  1108. */
  1109. static void tty_driver_remove_tty(struct tty_driver *driver,
  1110. struct tty_struct *tty)
  1111. {
  1112. if (driver->ops->remove)
  1113. driver->ops->remove(driver, tty);
  1114. else
  1115. driver->ttys[tty->index] = NULL;
  1116. }
  1117. /*
  1118. * tty_reopen() - fast re-open of an open tty
  1119. * @tty - the tty to open
  1120. *
  1121. * Return 0 on success, -errno on error.
  1122. *
  1123. * Locking: tty_mutex must be held from the time the tty was found
  1124. * till this open completes.
  1125. */
  1126. static int tty_reopen(struct tty_struct *tty)
  1127. {
  1128. struct tty_driver *driver = tty->driver;
  1129. if (test_bit(TTY_CLOSING, &tty->flags))
  1130. return -EIO;
  1131. if (driver->type == TTY_DRIVER_TYPE_PTY &&
  1132. driver->subtype == PTY_TYPE_MASTER) {
  1133. /*
  1134. * special case for PTY masters: only one open permitted,
  1135. * and the slave side open count is incremented as well.
  1136. */
  1137. if (tty->count)
  1138. return -EIO;
  1139. tty->link->count++;
  1140. }
  1141. tty->count++;
  1142. tty->driver = driver; /* N.B. why do this every time?? */
  1143. mutex_lock(&tty->ldisc_mutex);
  1144. WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
  1145. mutex_unlock(&tty->ldisc_mutex);
  1146. return 0;
  1147. }
  1148. /**
  1149. * tty_init_dev - initialise a tty device
  1150. * @driver: tty driver we are opening a device on
  1151. * @idx: device index
  1152. * @ret_tty: returned tty structure
  1153. * @first_ok: ok to open a new device (used by ptmx)
  1154. *
  1155. * Prepare a tty device. This may not be a "new" clean device but
  1156. * could also be an active device. The pty drivers require special
  1157. * handling because of this.
  1158. *
  1159. * Locking:
  1160. * The function is called under the tty_mutex, which
  1161. * protects us from the tty struct or driver itself going away.
  1162. *
  1163. * On exit the tty device has the line discipline attached and
  1164. * a reference count of 1. If a pair was created for pty/tty use
  1165. * and the other was a pty master then it too has a reference count of 1.
  1166. *
  1167. * WSH 06/09/97: Rewritten to remove races and properly clean up after a
  1168. * failed open. The new code protects the open with a mutex, so it's
  1169. * really quite straightforward. The mutex locking can probably be
  1170. * relaxed for the (most common) case of reopening a tty.
  1171. */
  1172. struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
  1173. int first_ok)
  1174. {
  1175. struct tty_struct *tty;
  1176. int retval;
  1177. lock_kernel();
  1178. /* Check if pty master is being opened multiple times */
  1179. if (driver->subtype == PTY_TYPE_MASTER &&
  1180. (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
  1181. unlock_kernel();
  1182. return ERR_PTR(-EIO);
  1183. }
  1184. unlock_kernel();
  1185. /*
  1186. * First time open is complex, especially for PTY devices.
  1187. * This code guarantees that either everything succeeds and the
  1188. * TTY is ready for operation, or else the table slots are vacated
  1189. * and the allocated memory released. (Except that the termios
  1190. * and locked termios may be retained.)
  1191. */
  1192. if (!try_module_get(driver->owner))
  1193. return ERR_PTR(-ENODEV);
  1194. tty = alloc_tty_struct();
  1195. if (!tty)
  1196. goto fail_no_mem;
  1197. initialize_tty_struct(tty, driver, idx);
  1198. retval = tty_driver_install_tty(driver, tty);
  1199. if (retval < 0) {
  1200. free_tty_struct(tty);
  1201. module_put(driver->owner);
  1202. return ERR_PTR(retval);
  1203. }
  1204. /*
  1205. * Structures all installed ... call the ldisc open routines.
  1206. * If we fail here just call release_tty to clean up. No need
  1207. * to decrement the use counts, as release_tty doesn't care.
  1208. */
  1209. retval = tty_ldisc_setup(tty, tty->link);
  1210. if (retval)
  1211. goto release_mem_out;
  1212. return tty;
  1213. fail_no_mem:
  1214. module_put(driver->owner);
  1215. return ERR_PTR(-ENOMEM);
  1216. /* call the tty release_tty routine to clean out this slot */
  1217. release_mem_out:
  1218. if (printk_ratelimit())
  1219. printk(KERN_INFO "tty_init_dev: ldisc open failed, "
  1220. "clearing slot %d\n", idx);
  1221. lock_kernel();
  1222. release_tty(tty, idx);
  1223. unlock_kernel();
  1224. return ERR_PTR(retval);
  1225. }
  1226. void tty_free_termios(struct tty_struct *tty)
  1227. {
  1228. struct ktermios *tp;
  1229. int idx = tty->index;
  1230. /* Kill this flag and push into drivers for locking etc */
  1231. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  1232. /* FIXME: Locking on ->termios array */
  1233. tp = tty->termios;
  1234. tty->driver->termios[idx] = NULL;
  1235. kfree(tp);
  1236. }
  1237. }
  1238. EXPORT_SYMBOL(tty_free_termios);
  1239. void tty_shutdown(struct tty_struct *tty)
  1240. {
  1241. tty_driver_remove_tty(tty->driver, tty);
  1242. tty_free_termios(tty);
  1243. }
  1244. EXPORT_SYMBOL(tty_shutdown);
  1245. /**
  1246. * release_one_tty - release tty structure memory
  1247. * @kref: kref of tty we are obliterating
  1248. *
  1249. * Releases memory associated with a tty structure, and clears out the
  1250. * driver table slots. This function is called when a device is no longer
  1251. * in use. It also gets called when setup of a device fails.
  1252. *
  1253. * Locking:
  1254. * tty_mutex - sometimes only
  1255. * takes the file list lock internally when working on the list
  1256. * of ttys that the driver keeps.
  1257. *
  1258. * This method gets called from a work queue so that the driver private
  1259. * cleanup ops can sleep (needed for USB at least)
  1260. */
  1261. static void release_one_tty(struct work_struct *work)
  1262. {
  1263. struct tty_struct *tty =
  1264. container_of(work, struct tty_struct, hangup_work);
  1265. struct tty_driver *driver = tty->driver;
  1266. if (tty->ops->cleanup)
  1267. tty->ops->cleanup(tty);
  1268. tty->magic = 0;
  1269. tty_driver_kref_put(driver);
  1270. module_put(driver->owner);
  1271. file_list_lock();
  1272. list_del_init(&tty->tty_files);
  1273. file_list_unlock();
  1274. put_pid(tty->pgrp);
  1275. put_pid(tty->session);
  1276. free_tty_struct(tty);
  1277. }
  1278. static void queue_release_one_tty(struct kref *kref)
  1279. {
  1280. struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
  1281. if (tty->ops->shutdown)
  1282. tty->ops->shutdown(tty);
  1283. else
  1284. tty_shutdown(tty);
  1285. /* The hangup queue is now free so we can reuse it rather than
  1286. waste a chunk of memory for each port */
  1287. INIT_WORK(&tty->hangup_work, release_one_tty);
  1288. schedule_work(&tty->hangup_work);
  1289. }
  1290. /**
  1291. * tty_kref_put - release a tty kref
  1292. * @tty: tty device
  1293. *
  1294. * Release a reference to a tty device and if need be let the kref
  1295. * layer destruct the object for us
  1296. */
  1297. void tty_kref_put(struct tty_struct *tty)
  1298. {
  1299. if (tty)
  1300. kref_put(&tty->kref, queue_release_one_tty);
  1301. }
  1302. EXPORT_SYMBOL(tty_kref_put);
  1303. /**
  1304. * release_tty - release tty structure memory
  1305. *
  1306. * Release both @tty and a possible linked partner (think pty pair),
  1307. * and decrement the refcount of the backing module.
  1308. *
  1309. * Locking:
  1310. * tty_mutex - sometimes only
  1311. * takes the file list lock internally when working on the list
  1312. * of ttys that the driver keeps.
  1313. * FIXME: should we require tty_mutex is held here ??
  1314. *
  1315. */
  1316. static void release_tty(struct tty_struct *tty, int idx)
  1317. {
  1318. /* This should always be true but check for the moment */
  1319. WARN_ON(tty->index != idx);
  1320. if (tty->link)
  1321. tty_kref_put(tty->link);
  1322. tty_kref_put(tty);
  1323. }
  1324. /**
  1325. * tty_release - vfs callback for close
  1326. * @inode: inode of tty
  1327. * @filp: file pointer for handle to tty
  1328. *
  1329. * Called the last time each file handle is closed that references
  1330. * this tty. There may however be several such references.
  1331. *
  1332. * Locking:
  1333. * Takes bkl. See tty_release_dev
  1334. *
  1335. * Even releasing the tty structures is a tricky business.. We have
  1336. * to be very careful that the structures are all released at the
  1337. * same time, as interrupts might otherwise get the wrong pointers.
  1338. *
  1339. * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
  1340. * lead to double frees or releasing memory still in use.
  1341. */
  1342. int tty_release(struct inode *inode, struct file *filp)
  1343. {
  1344. struct tty_struct *tty, *o_tty;
  1345. int pty_master, tty_closing, o_tty_closing, do_sleep;
  1346. int devpts;
  1347. int idx;
  1348. char buf[64];
  1349. tty = (struct tty_struct *)filp->private_data;
  1350. if (tty_paranoia_check(tty, inode, "tty_release_dev"))
  1351. return 0;
  1352. lock_kernel();
  1353. check_tty_count(tty, "tty_release_dev");
  1354. tty_fasync(-1, filp, 0);
  1355. idx = tty->index;
  1356. pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1357. tty->driver->subtype == PTY_TYPE_MASTER);
  1358. devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
  1359. o_tty = tty->link;
  1360. #ifdef TTY_PARANOIA_CHECK
  1361. if (idx < 0 || idx >= tty->driver->num) {
  1362. printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
  1363. "free (%s)\n", tty->name);
  1364. unlock_kernel();
  1365. return 0;
  1366. }
  1367. if (!devpts) {
  1368. if (tty != tty->driver->ttys[idx]) {
  1369. unlock_kernel();
  1370. printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
  1371. "for (%s)\n", idx, tty->name);
  1372. return 0;
  1373. }
  1374. if (tty->termios != tty->driver->termios[idx]) {
  1375. unlock_kernel();
  1376. printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
  1377. "for (%s)\n",
  1378. idx, tty->name);
  1379. return 0;
  1380. }
  1381. }
  1382. #endif
  1383. #ifdef TTY_DEBUG_HANGUP
  1384. printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
  1385. tty_name(tty, buf), tty->count);
  1386. #endif
  1387. #ifdef TTY_PARANOIA_CHECK
  1388. if (tty->driver->other &&
  1389. !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1390. if (o_tty != tty->driver->other->ttys[idx]) {
  1391. unlock_kernel();
  1392. printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
  1393. "not o_tty for (%s)\n",
  1394. idx, tty->name);
  1395. return 0 ;
  1396. }
  1397. if (o_tty->termios != tty->driver->other->termios[idx]) {
  1398. unlock_kernel();
  1399. printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
  1400. "not o_termios for (%s)\n",
  1401. idx, tty->name);
  1402. return 0;
  1403. }
  1404. if (o_tty->link != tty) {
  1405. unlock_kernel();
  1406. printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
  1407. return 0;
  1408. }
  1409. }
  1410. #endif
  1411. if (tty->ops->close)
  1412. tty->ops->close(tty, filp);
  1413. unlock_kernel();
  1414. /*
  1415. * Sanity check: if tty->count is going to zero, there shouldn't be
  1416. * any waiters on tty->read_wait or tty->write_wait. We test the
  1417. * wait queues and kick everyone out _before_ actually starting to
  1418. * close. This ensures that we won't block while releasing the tty
  1419. * structure.
  1420. *
  1421. * The test for the o_tty closing is necessary, since the master and
  1422. * slave sides may close in any order. If the slave side closes out
  1423. * first, its count will be one, since the master side holds an open.
  1424. * Thus this test wouldn't be triggered at the time the slave closes,
  1425. * so we do it now.
  1426. *
  1427. * Note that it's possible for the tty to be opened again while we're
  1428. * flushing out waiters. By recalculating the closing flags before
  1429. * each iteration we avoid any problems.
  1430. */
  1431. while (1) {
  1432. /* Guard against races with tty->count changes elsewhere and
  1433. opens on /dev/tty */
  1434. mutex_lock(&tty_mutex);
  1435. lock_kernel();
  1436. tty_closing = tty->count <= 1;
  1437. o_tty_closing = o_tty &&
  1438. (o_tty->count <= (pty_master ? 1 : 0));
  1439. do_sleep = 0;
  1440. if (tty_closing) {
  1441. if (waitqueue_active(&tty->read_wait)) {
  1442. wake_up_poll(&tty->read_wait, POLLIN);
  1443. do_sleep++;
  1444. }
  1445. if (waitqueue_active(&tty->write_wait)) {
  1446. wake_up_poll(&tty->write_wait, POLLOUT);
  1447. do_sleep++;
  1448. }
  1449. }
  1450. if (o_tty_closing) {
  1451. if (waitqueue_active(&o_tty->read_wait)) {
  1452. wake_up_poll(&o_tty->read_wait, POLLIN);
  1453. do_sleep++;
  1454. }
  1455. if (waitqueue_active(&o_tty->write_wait)) {
  1456. wake_up_poll(&o_tty->write_wait, POLLOUT);
  1457. do_sleep++;
  1458. }
  1459. }
  1460. if (!do_sleep)
  1461. break;
  1462. printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
  1463. "active!\n", tty_name(tty, buf));
  1464. unlock_kernel();
  1465. mutex_unlock(&tty_mutex);
  1466. schedule();
  1467. }
  1468. /*
  1469. * The closing flags are now consistent with the open counts on
  1470. * both sides, and we've completed the last operation that could
  1471. * block, so it's safe to proceed with closing.
  1472. */
  1473. if (pty_master) {
  1474. if (--o_tty->count < 0) {
  1475. printk(KERN_WARNING "tty_release_dev: bad pty slave count "
  1476. "(%d) for %s\n",
  1477. o_tty->count, tty_name(o_tty, buf));
  1478. o_tty->count = 0;
  1479. }
  1480. }
  1481. if (--tty->count < 0) {
  1482. printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
  1483. tty->count, tty_name(tty, buf));
  1484. tty->count = 0;
  1485. }
  1486. /*
  1487. * We've decremented tty->count, so we need to remove this file
  1488. * descriptor off the tty->tty_files list; this serves two
  1489. * purposes:
  1490. * - check_tty_count sees the correct number of file descriptors
  1491. * associated with this tty.
  1492. * - do_tty_hangup no longer sees this file descriptor as
  1493. * something that needs to be handled for hangups.
  1494. */
  1495. file_kill(filp);
  1496. filp->private_data = NULL;
  1497. /*
  1498. * Perform some housekeeping before deciding whether to return.
  1499. *
  1500. * Set the TTY_CLOSING flag if this was the last open. In the
  1501. * case of a pty we may have to wait around for the other side
  1502. * to close, and TTY_CLOSING makes sure we can't be reopened.
  1503. */
  1504. if (tty_closing)
  1505. set_bit(TTY_CLOSING, &tty->flags);
  1506. if (o_tty_closing)
  1507. set_bit(TTY_CLOSING, &o_tty->flags);
  1508. /*
  1509. * If _either_ side is closing, make sure there aren't any
  1510. * processes that still think tty or o_tty is their controlling
  1511. * tty.
  1512. */
  1513. if (tty_closing || o_tty_closing) {
  1514. read_lock(&tasklist_lock);
  1515. session_clear_tty(tty->session);
  1516. if (o_tty)
  1517. session_clear_tty(o_tty->session);
  1518. read_unlock(&tasklist_lock);
  1519. }
  1520. mutex_unlock(&tty_mutex);
  1521. /* check whether both sides are closing ... */
  1522. if (!tty_closing || (o_tty && !o_tty_closing)) {
  1523. unlock_kernel();
  1524. return 0;
  1525. }
  1526. #ifdef TTY_DEBUG_HANGUP
  1527. printk(KERN_DEBUG "freeing tty structure...");
  1528. #endif
  1529. /*
  1530. * Ask the line discipline code to release its structures
  1531. */
  1532. tty_ldisc_release(tty, o_tty);
  1533. /*
  1534. * The release_tty function takes care of the details of clearing
  1535. * the slots and preserving the termios structure.
  1536. */
  1537. release_tty(tty, idx);
  1538. /* Make this pty number available for reallocation */
  1539. if (devpts)
  1540. devpts_kill_index(inode, idx);
  1541. unlock_kernel();
  1542. return 0;
  1543. }
  1544. /**
  1545. * tty_open - open a tty device
  1546. * @inode: inode of device file
  1547. * @filp: file pointer to tty
  1548. *
  1549. * tty_open and tty_release keep up the tty count that contains the
  1550. * number of opens done on a tty. We cannot use the inode-count, as
  1551. * different inodes might point to the same tty.
  1552. *
  1553. * Open-counting is needed for pty masters, as well as for keeping
  1554. * track of serial lines: DTR is dropped when the last close happens.
  1555. * (This is not done solely through tty->count, now. - Ted 1/27/92)
  1556. *
  1557. * The termios state of a pty is reset on first open so that
  1558. * settings don't persist across reuse.
  1559. *
  1560. * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
  1561. * tty->count should protect the rest.
  1562. * ->siglock protects ->signal/->sighand
  1563. */
  1564. static int tty_open(struct inode *inode, struct file *filp)
  1565. {
  1566. struct tty_struct *tty = NULL;
  1567. int noctty, retval;
  1568. struct tty_driver *driver;
  1569. int index;
  1570. dev_t device = inode->i_rdev;
  1571. unsigned saved_flags = filp->f_flags;
  1572. nonseekable_open(inode, filp);
  1573. retry_open:
  1574. noctty = filp->f_flags & O_NOCTTY;
  1575. index = -1;
  1576. retval = 0;
  1577. mutex_lock(&tty_mutex);
  1578. lock_kernel();
  1579. if (device == MKDEV(TTYAUX_MAJOR, 0)) {
  1580. tty = get_current_tty();
  1581. if (!tty) {
  1582. unlock_kernel();
  1583. mutex_unlock(&tty_mutex);
  1584. return -ENXIO;
  1585. }
  1586. driver = tty_driver_kref_get(tty->driver);
  1587. index = tty->index;
  1588. filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
  1589. /* noctty = 1; */
  1590. /* FIXME: Should we take a driver reference ? */
  1591. tty_kref_put(tty);
  1592. goto got_driver;
  1593. }
  1594. #ifdef CONFIG_VT
  1595. if (device == MKDEV(TTY_MAJOR, 0)) {
  1596. extern struct tty_driver *console_driver;
  1597. driver = tty_driver_kref_get(console_driver);
  1598. index = fg_console;
  1599. noctty = 1;
  1600. goto got_driver;
  1601. }
  1602. #endif
  1603. if (device == MKDEV(TTYAUX_MAJOR, 1)) {
  1604. struct tty_driver *console_driver = console_device(&index);
  1605. if (console_driver) {
  1606. driver = tty_driver_kref_get(console_driver);
  1607. if (driver) {
  1608. /* Don't let /dev/console block */
  1609. filp->f_flags |= O_NONBLOCK;
  1610. noctty = 1;
  1611. goto got_driver;
  1612. }
  1613. }
  1614. unlock_kernel();
  1615. mutex_unlock(&tty_mutex);
  1616. return -ENODEV;
  1617. }
  1618. driver = get_tty_driver(device, &index);
  1619. if (!driver) {
  1620. unlock_kernel();
  1621. mutex_unlock(&tty_mutex);
  1622. return -ENODEV;
  1623. }
  1624. got_driver:
  1625. if (!tty) {
  1626. /* check whether we're reopening an existing tty */
  1627. tty = tty_driver_lookup_tty(driver, inode, index);
  1628. if (IS_ERR(tty)) {
  1629. unlock_kernel();
  1630. mutex_unlock(&tty_mutex);
  1631. return PTR_ERR(tty);
  1632. }
  1633. }
  1634. if (tty) {
  1635. retval = tty_reopen(tty);
  1636. if (retval)
  1637. tty = ERR_PTR(retval);
  1638. } else
  1639. tty = tty_init_dev(driver, index, 0);
  1640. mutex_unlock(&tty_mutex);
  1641. tty_driver_kref_put(driver);
  1642. if (IS_ERR(tty)) {
  1643. unlock_kernel();
  1644. return PTR_ERR(tty);
  1645. }
  1646. filp->private_data = tty;
  1647. file_move(filp, &tty->tty_files);
  1648. check_tty_count(tty, "tty_open");
  1649. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1650. tty->driver->subtype == PTY_TYPE_MASTER)
  1651. noctty = 1;
  1652. #ifdef TTY_DEBUG_HANGUP
  1653. printk(KERN_DEBUG "opening %s...", tty->name);
  1654. #endif
  1655. if (!retval) {
  1656. if (tty->ops->open)
  1657. retval = tty->ops->open(tty, filp);
  1658. else
  1659. retval = -ENODEV;
  1660. }
  1661. filp->f_flags = saved_flags;
  1662. if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
  1663. !capable(CAP_SYS_ADMIN))
  1664. retval = -EBUSY;
  1665. if (retval) {
  1666. #ifdef TTY_DEBUG_HANGUP
  1667. printk(KERN_DEBUG "error %d in opening %s...", retval,
  1668. tty->name);
  1669. #endif
  1670. tty_release(inode, filp);
  1671. if (retval != -ERESTARTSYS) {
  1672. unlock_kernel();
  1673. return retval;
  1674. }
  1675. if (signal_pending(current)) {
  1676. unlock_kernel();
  1677. return retval;
  1678. }
  1679. schedule();
  1680. /*
  1681. * Need to reset f_op in case a hangup happened.
  1682. */
  1683. if (filp->f_op == &hung_up_tty_fops)
  1684. filp->f_op = &tty_fops;
  1685. unlock_kernel();
  1686. goto retry_open;
  1687. }
  1688. unlock_kernel();
  1689. mutex_lock(&tty_mutex);
  1690. lock_kernel();
  1691. spin_lock_irq(&current->sighand->siglock);
  1692. if (!noctty &&
  1693. current->signal->leader &&
  1694. !current->signal->tty &&
  1695. tty->session == NULL)
  1696. __proc_set_tty(current, tty);
  1697. spin_unlock_irq(&current->sighand->siglock);
  1698. unlock_kernel();
  1699. mutex_unlock(&tty_mutex);
  1700. return 0;
  1701. }
  1702. /**
  1703. * tty_poll - check tty status
  1704. * @filp: file being polled
  1705. * @wait: poll wait structures to update
  1706. *
  1707. * Call the line discipline polling method to obtain the poll
  1708. * status of the device.
  1709. *
  1710. * Locking: locks called line discipline but ldisc poll method
  1711. * may be re-entered freely by other callers.
  1712. */
  1713. static unsigned int tty_poll(struct file *filp, poll_table *wait)
  1714. {
  1715. struct tty_struct *tty;
  1716. struct tty_ldisc *ld;
  1717. int ret = 0;
  1718. tty = (struct tty_struct *)filp->private_data;
  1719. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
  1720. return 0;
  1721. ld = tty_ldisc_ref_wait(tty);
  1722. if (ld->ops->poll)
  1723. ret = (ld->ops->poll)(tty, filp, wait);
  1724. tty_ldisc_deref(ld);
  1725. return ret;
  1726. }
  1727. static int tty_fasync(int fd, struct file *filp, int on)
  1728. {
  1729. struct tty_struct *tty;
  1730. unsigned long flags;
  1731. int retval = 0;
  1732. lock_kernel();
  1733. tty = (struct tty_struct *)filp->private_data;
  1734. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
  1735. goto out;
  1736. retval = fasync_helper(fd, filp, on, &tty->fasync);
  1737. if (retval <= 0)
  1738. goto out;
  1739. if (on) {
  1740. enum pid_type type;
  1741. struct pid *pid;
  1742. if (!waitqueue_active(&tty->read_wait))
  1743. tty->minimum_to_wake = 1;
  1744. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1745. if (tty->pgrp) {
  1746. pid = tty->pgrp;
  1747. type = PIDTYPE_PGID;
  1748. } else {
  1749. pid = task_pid(current);
  1750. type = PIDTYPE_PID;
  1751. }
  1752. get_pid(pid);
  1753. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1754. retval = __f_setown(filp, pid, type, 0);
  1755. put_pid(pid);
  1756. if (retval)
  1757. goto out;
  1758. } else {
  1759. if (!tty->fasync && !waitqueue_active(&tty->read_wait))
  1760. tty->minimum_to_wake = N_TTY_BUF_SIZE;
  1761. }
  1762. retval = 0;
  1763. out:
  1764. unlock_kernel();
  1765. return retval;
  1766. }
  1767. /**
  1768. * tiocsti - fake input character
  1769. * @tty: tty to fake input into
  1770. * @p: pointer to character
  1771. *
  1772. * Fake input to a tty device. Does the necessary locking and
  1773. * input management.
  1774. *
  1775. * FIXME: does not honour flow control ??
  1776. *
  1777. * Locking:
  1778. * Called functions take tty_ldisc_lock
  1779. * current->signal->tty check is safe without locks
  1780. *
  1781. * FIXME: may race normal receive processing
  1782. */
  1783. static int tiocsti(struct tty_struct *tty, char __user *p)
  1784. {
  1785. char ch, mbz = 0;
  1786. struct tty_ldisc *ld;
  1787. if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
  1788. return -EPERM;
  1789. if (get_user(ch, p))
  1790. return -EFAULT;
  1791. tty_audit_tiocsti(tty, ch);
  1792. ld = tty_ldisc_ref_wait(tty);
  1793. ld->ops->receive_buf(tty, &ch, &mbz, 1);
  1794. tty_ldisc_deref(ld);
  1795. return 0;
  1796. }
  1797. /**
  1798. * tiocgwinsz - implement window query ioctl
  1799. * @tty; tty
  1800. * @arg: user buffer for result
  1801. *
  1802. * Copies the kernel idea of the window size into the user buffer.
  1803. *
  1804. * Locking: tty->termios_mutex is taken to ensure the winsize data
  1805. * is consistent.
  1806. */
  1807. static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
  1808. {
  1809. int err;
  1810. mutex_lock(&tty->termios_mutex);
  1811. err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
  1812. mutex_unlock(&tty->termios_mutex);
  1813. return err ? -EFAULT: 0;
  1814. }
  1815. /**
  1816. * tty_do_resize - resize event
  1817. * @tty: tty being resized
  1818. * @rows: rows (character)
  1819. * @cols: cols (character)
  1820. *
  1821. * Update the termios variables and send the necessary signals to
  1822. * peform a terminal resize correctly
  1823. */
  1824. int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
  1825. {
  1826. struct pid *pgrp;
  1827. unsigned long flags;
  1828. /* Lock the tty */
  1829. mutex_lock(&tty->termios_mutex);
  1830. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  1831. goto done;
  1832. /* Get the PID values and reference them so we can
  1833. avoid holding the tty ctrl lock while sending signals */
  1834. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1835. pgrp = get_pid(tty->pgrp);
  1836. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1837. if (pgrp)
  1838. kill_pgrp(pgrp, SIGWINCH, 1);
  1839. put_pid(pgrp);
  1840. tty->winsize = *ws;
  1841. done:
  1842. mutex_unlock(&tty->termios_mutex);
  1843. return 0;
  1844. }
  1845. /**
  1846. * tiocswinsz - implement window size set ioctl
  1847. * @tty; tty side of tty
  1848. * @arg: user buffer for result
  1849. *
  1850. * Copies the user idea of the window size to the kernel. Traditionally
  1851. * this is just advisory information but for the Linux console it
  1852. * actually has driver level meaning and triggers a VC resize.
  1853. *
  1854. * Locking:
  1855. * Driver dependant. The default do_resize method takes the
  1856. * tty termios mutex and ctrl_lock. The console takes its own lock
  1857. * then calls into the default method.
  1858. */
  1859. static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
  1860. {
  1861. struct winsize tmp_ws;
  1862. if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
  1863. return -EFAULT;
  1864. if (tty->ops->resize)
  1865. return tty->ops->resize(tty, &tmp_ws);
  1866. else
  1867. return tty_do_resize(tty, &tmp_ws);
  1868. }
  1869. /**
  1870. * tioccons - allow admin to move logical console
  1871. * @file: the file to become console
  1872. *
  1873. * Allow the adminstrator to move the redirected console device
  1874. *
  1875. * Locking: uses redirect_lock to guard the redirect information
  1876. */
  1877. static int tioccons(struct file *file)
  1878. {
  1879. if (!capable(CAP_SYS_ADMIN))
  1880. return -EPERM;
  1881. if (file->f_op->write == redirected_tty_write) {
  1882. struct file *f;
  1883. spin_lock(&redirect_lock);
  1884. f = redirect;
  1885. redirect = NULL;
  1886. spin_unlock(&redirect_lock);
  1887. if (f)
  1888. fput(f);
  1889. return 0;
  1890. }
  1891. spin_lock(&redirect_lock);
  1892. if (redirect) {
  1893. spin_unlock(&redirect_lock);
  1894. return -EBUSY;
  1895. }
  1896. get_file(file);
  1897. redirect = file;
  1898. spin_unlock(&redirect_lock);
  1899. return 0;
  1900. }
  1901. /**
  1902. * fionbio - non blocking ioctl
  1903. * @file: file to set blocking value
  1904. * @p: user parameter
  1905. *
  1906. * Historical tty interfaces had a blocking control ioctl before
  1907. * the generic functionality existed. This piece of history is preserved
  1908. * in the expected tty API of posix OS's.
  1909. *
  1910. * Locking: none, the open file handle ensures it won't go away.
  1911. */
  1912. static int fionbio(struct file *file, int __user *p)
  1913. {
  1914. int nonblock;
  1915. if (get_user(nonblock, p))
  1916. return -EFAULT;
  1917. spin_lock(&file->f_lock);
  1918. if (nonblock)
  1919. file->f_flags |= O_NONBLOCK;
  1920. else
  1921. file->f_flags &= ~O_NONBLOCK;
  1922. spin_unlock(&file->f_lock);
  1923. return 0;
  1924. }
  1925. /**
  1926. * tiocsctty - set controlling tty
  1927. * @tty: tty structure
  1928. * @arg: user argument
  1929. *
  1930. * This ioctl is used to manage job control. It permits a session
  1931. * leader to set this tty as the controlling tty for the session.
  1932. *
  1933. * Locking:
  1934. * Takes tty_mutex() to protect tty instance
  1935. * Takes tasklist_lock internally to walk sessions
  1936. * Takes ->siglock() when updating signal->tty
  1937. */
  1938. static int tiocsctty(struct tty_struct *tty, int arg)
  1939. {
  1940. int ret = 0;
  1941. if (current->signal->leader && (task_session(current) == tty->session))
  1942. return ret;
  1943. mutex_lock(&tty_mutex);
  1944. /*
  1945. * The process must be a session leader and
  1946. * not have a controlling tty already.
  1947. */
  1948. if (!current->signal->leader || current->signal->tty) {
  1949. ret = -EPERM;
  1950. goto unlock;
  1951. }
  1952. if (tty->session) {
  1953. /*
  1954. * This tty is already the controlling
  1955. * tty for another session group!
  1956. */
  1957. if (arg == 1 && capable(CAP_SYS_ADMIN)) {
  1958. /*
  1959. * Steal it away
  1960. */
  1961. read_lock(&tasklist_lock);
  1962. session_clear_tty(tty->session);
  1963. read_unlock(&tasklist_lock);
  1964. } else {
  1965. ret = -EPERM;
  1966. goto unlock;
  1967. }
  1968. }
  1969. proc_set_tty(current, tty);
  1970. unlock:
  1971. mutex_unlock(&tty_mutex);
  1972. return ret;
  1973. }
  1974. /**
  1975. * tty_get_pgrp - return a ref counted pgrp pid
  1976. * @tty: tty to read
  1977. *
  1978. * Returns a refcounted instance of the pid struct for the process
  1979. * group controlling the tty.
  1980. */
  1981. struct pid *tty_get_pgrp(struct tty_struct *tty)
  1982. {
  1983. unsigned long flags;
  1984. struct pid *pgrp;
  1985. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1986. pgrp = get_pid(tty->pgrp);
  1987. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1988. return pgrp;
  1989. }
  1990. EXPORT_SYMBOL_GPL(tty_get_pgrp);
  1991. /**
  1992. * tiocgpgrp - get process group
  1993. * @tty: tty passed by user
  1994. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  1995. * @p: returned pid
  1996. *
  1997. * Obtain the process group of the tty. If there is no process group
  1998. * return an error.
  1999. *
  2000. * Locking: none. Reference to current->signal->tty is safe.
  2001. */
  2002. static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2003. {
  2004. struct pid *pid;
  2005. int ret;
  2006. /*
  2007. * (tty == real_tty) is a cheap way of
  2008. * testing if the tty is NOT a master pty.
  2009. */
  2010. if (tty == real_tty && current->signal->tty != real_tty)
  2011. return -ENOTTY;
  2012. pid = tty_get_pgrp(real_tty);
  2013. ret = put_user(pid_vnr(pid), p);
  2014. put_pid(pid);
  2015. return ret;
  2016. }
  2017. /**
  2018. * tiocspgrp - attempt to set process group
  2019. * @tty: tty passed by user
  2020. * @real_tty: tty side device matching tty passed by user
  2021. * @p: pid pointer
  2022. *
  2023. * Set the process group of the tty to the session passed. Only
  2024. * permitted where the tty session is our session.
  2025. *
  2026. * Locking: RCU, ctrl lock
  2027. */
  2028. static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2029. {
  2030. struct pid *pgrp;
  2031. pid_t pgrp_nr;
  2032. int retval = tty_check_change(real_tty);
  2033. unsigned long flags;
  2034. if (retval == -EIO)
  2035. return -ENOTTY;
  2036. if (retval)
  2037. return retval;
  2038. if (!current->signal->tty ||
  2039. (current->signal->tty != real_tty) ||
  2040. (real_tty->session != task_session(current)))
  2041. return -ENOTTY;
  2042. if (get_user(pgrp_nr, p))
  2043. return -EFAULT;
  2044. if (pgrp_nr < 0)
  2045. return -EINVAL;
  2046. rcu_read_lock();
  2047. pgrp = find_vpid(pgrp_nr);
  2048. retval = -ESRCH;
  2049. if (!pgrp)
  2050. goto out_unlock;
  2051. retval = -EPERM;
  2052. if (session_of_pgrp(pgrp) != task_session(current))
  2053. goto out_unlock;
  2054. retval = 0;
  2055. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2056. put_pid(real_tty->pgrp);
  2057. real_tty->pgrp = get_pid(pgrp);
  2058. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2059. out_unlock:
  2060. rcu_read_unlock();
  2061. return retval;
  2062. }
  2063. /**
  2064. * tiocgsid - get session id
  2065. * @tty: tty passed by user
  2066. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2067. * @p: pointer to returned session id
  2068. *
  2069. * Obtain the session id of the tty. If there is no session
  2070. * return an error.
  2071. *
  2072. * Locking: none. Reference to current->signal->tty is safe.
  2073. */
  2074. static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2075. {
  2076. /*
  2077. * (tty == real_tty) is a cheap way of
  2078. * testing if the tty is NOT a master pty.
  2079. */
  2080. if (tty == real_tty && current->signal->tty != real_tty)
  2081. return -ENOTTY;
  2082. if (!real_tty->session)
  2083. return -ENOTTY;
  2084. return put_user(pid_vnr(real_tty->session), p);
  2085. }
  2086. /**
  2087. * tiocsetd - set line discipline
  2088. * @tty: tty device
  2089. * @p: pointer to user data
  2090. *
  2091. * Set the line discipline according to user request.
  2092. *
  2093. * Locking: see tty_set_ldisc, this function is just a helper
  2094. */
  2095. static int tiocsetd(struct tty_struct *tty, int __user *p)
  2096. {
  2097. int ldisc;
  2098. int ret;
  2099. if (get_user(ldisc, p))
  2100. return -EFAULT;
  2101. ret = tty_set_ldisc(tty, ldisc);
  2102. return ret;
  2103. }
  2104. /**
  2105. * send_break - performed time break
  2106. * @tty: device to break on
  2107. * @duration: timeout in mS
  2108. *
  2109. * Perform a timed break on hardware that lacks its own driver level
  2110. * timed break functionality.
  2111. *
  2112. * Locking:
  2113. * atomic_write_lock serializes
  2114. *
  2115. */
  2116. static int send_break(struct tty_struct *tty, unsigned int duration)
  2117. {
  2118. int retval;
  2119. if (tty->ops->break_ctl == NULL)
  2120. return 0;
  2121. if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
  2122. retval = tty->ops->break_ctl(tty, duration);
  2123. else {
  2124. /* Do the work ourselves */
  2125. if (tty_write_lock(tty, 0) < 0)
  2126. return -EINTR;
  2127. retval = tty->ops->break_ctl(tty, -1);
  2128. if (retval)
  2129. goto out;
  2130. if (!signal_pending(current))
  2131. msleep_interruptible(duration);
  2132. retval = tty->ops->break_ctl(tty, 0);
  2133. out:
  2134. tty_write_unlock(tty);
  2135. if (signal_pending(current))
  2136. retval = -EINTR;
  2137. }
  2138. return retval;
  2139. }
  2140. /**
  2141. * tty_tiocmget - get modem status
  2142. * @tty: tty device
  2143. * @file: user file pointer
  2144. * @p: pointer to result
  2145. *
  2146. * Obtain the modem status bits from the tty driver if the feature
  2147. * is supported. Return -EINVAL if it is not available.
  2148. *
  2149. * Locking: none (up to the driver)
  2150. */
  2151. static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
  2152. {
  2153. int retval = -EINVAL;
  2154. if (tty->ops->tiocmget) {
  2155. retval = tty->ops->tiocmget(tty, file);
  2156. if (retval >= 0)
  2157. retval = put_user(retval, p);
  2158. }
  2159. return retval;
  2160. }
  2161. /**
  2162. * tty_tiocmset - set modem status
  2163. * @tty: tty device
  2164. * @file: user file pointer
  2165. * @cmd: command - clear bits, set bits or set all
  2166. * @p: pointer to desired bits
  2167. *
  2168. * Set the modem status bits from the tty driver if the feature
  2169. * is supported. Return -EINVAL if it is not available.
  2170. *
  2171. * Locking: none (up to the driver)
  2172. */
  2173. static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
  2174. unsigned __user *p)
  2175. {
  2176. int retval;
  2177. unsigned int set, clear, val;
  2178. if (tty->ops->tiocmset == NULL)
  2179. return -EINVAL;
  2180. retval = get_user(val, p);
  2181. if (retval)
  2182. return retval;
  2183. set = clear = 0;
  2184. switch (cmd) {
  2185. case TIOCMBIS:
  2186. set = val;
  2187. break;
  2188. case TIOCMBIC:
  2189. clear = val;
  2190. break;
  2191. case TIOCMSET:
  2192. set = val;
  2193. clear = ~val;
  2194. break;
  2195. }
  2196. set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP|TIOCM_CD|
  2197. TIOCM_RI|TIOCM_DSR|TIOCM_CTS;
  2198. clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP|TIOCM_CD|
  2199. TIOCM_RI|TIOCM_DSR|TIOCM_CTS;
  2200. return tty->ops->tiocmset(tty, file, set, clear);
  2201. }
  2202. struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
  2203. {
  2204. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2205. tty->driver->subtype == PTY_TYPE_MASTER)
  2206. tty = tty->link;
  2207. return tty;
  2208. }
  2209. EXPORT_SYMBOL(tty_pair_get_tty);
  2210. struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
  2211. {
  2212. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2213. tty->driver->subtype == PTY_TYPE_MASTER)
  2214. return tty;
  2215. return tty->link;
  2216. }
  2217. EXPORT_SYMBOL(tty_pair_get_pty);
  2218. /*
  2219. * Split this up, as gcc can choke on it otherwise..
  2220. */
  2221. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2222. {
  2223. struct tty_struct *tty, *real_tty;
  2224. void __user *p = (void __user *)arg;
  2225. int retval;
  2226. struct tty_ldisc *ld;
  2227. struct inode *inode = file->f_dentry->d_inode;
  2228. tty = (struct tty_struct *)file->private_data;
  2229. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  2230. return -EINVAL;
  2231. real_tty = tty_pair_get_tty(tty);
  2232. /*
  2233. * Factor out some common prep work
  2234. */
  2235. switch (cmd) {
  2236. case TIOCSETD:
  2237. case TIOCSBRK:
  2238. case TIOCCBRK:
  2239. case TCSBRK:
  2240. case TCSBRKP:
  2241. retval = tty_check_change(tty);
  2242. if (retval)
  2243. return retval;
  2244. if (cmd != TIOCCBRK) {
  2245. tty_wait_until_sent(tty, 0);
  2246. if (signal_pending(current))
  2247. return -EINTR;
  2248. }
  2249. break;
  2250. }
  2251. /*
  2252. * Now do the stuff.
  2253. */
  2254. switch (cmd) {
  2255. case TIOCSTI:
  2256. return tiocsti(tty, p);
  2257. case TIOCGWINSZ:
  2258. return tiocgwinsz(real_tty, p);
  2259. case TIOCSWINSZ:
  2260. return tiocswinsz(real_tty, p);
  2261. case TIOCCONS:
  2262. return real_tty != tty ? -EINVAL : tioccons(file);
  2263. case FIONBIO:
  2264. return fionbio(file, p);
  2265. case TIOCEXCL:
  2266. set_bit(TTY_EXCLUSIVE, &tty->flags);
  2267. return 0;
  2268. case TIOCNXCL:
  2269. clear_bit(TTY_EXCLUSIVE, &tty->flags);
  2270. return 0;
  2271. case TIOCNOTTY:
  2272. if (current->signal->tty != tty)
  2273. return -ENOTTY;
  2274. no_tty();
  2275. return 0;
  2276. case TIOCSCTTY:
  2277. return tiocsctty(tty, arg);
  2278. case TIOCGPGRP:
  2279. return tiocgpgrp(tty, real_tty, p);
  2280. case TIOCSPGRP:
  2281. return tiocspgrp(tty, real_tty, p);
  2282. case TIOCGSID:
  2283. return tiocgsid(tty, real_tty, p);
  2284. case TIOCGETD:
  2285. return put_user(tty->ldisc->ops->num, (int __user *)p);
  2286. case TIOCSETD:
  2287. return tiocsetd(tty, p);
  2288. /*
  2289. * Break handling
  2290. */
  2291. case TIOCSBRK: /* Turn break on, unconditionally */
  2292. if (tty->ops->break_ctl)
  2293. return tty->ops->break_ctl(tty, -1);
  2294. return 0;
  2295. case TIOCCBRK: /* Turn break off, unconditionally */
  2296. if (tty->ops->break_ctl)
  2297. return tty->ops->break_ctl(tty, 0);
  2298. return 0;
  2299. case TCSBRK: /* SVID version: non-zero arg --> no break */
  2300. /* non-zero arg means wait for all output data
  2301. * to be sent (performed above) but don't send break.
  2302. * This is used by the tcdrain() termios function.
  2303. */
  2304. if (!arg)
  2305. return send_break(tty, 250);
  2306. return 0;
  2307. case TCSBRKP: /* support for POSIX tcsendbreak() */
  2308. return send_break(tty, arg ? arg*100 : 250);
  2309. case TIOCMGET:
  2310. return tty_tiocmget(tty, file, p);
  2311. case TIOCMSET:
  2312. case TIOCMBIC:
  2313. case TIOCMBIS:
  2314. return tty_tiocmset(tty, file, cmd, p);
  2315. case TCFLSH:
  2316. switch (arg) {
  2317. case TCIFLUSH:
  2318. case TCIOFLUSH:
  2319. /* flush tty buffer and allow ldisc to process ioctl */
  2320. tty_buffer_flush(tty);
  2321. break;
  2322. }
  2323. break;
  2324. }
  2325. if (tty->ops->ioctl) {
  2326. retval = (tty->ops->ioctl)(tty, file, cmd, arg);
  2327. if (retval != -ENOIOCTLCMD)
  2328. return retval;
  2329. }
  2330. ld = tty_ldisc_ref_wait(tty);
  2331. retval = -EINVAL;
  2332. if (ld->ops->ioctl) {
  2333. retval = ld->ops->ioctl(tty, file, cmd, arg);
  2334. if (retval == -ENOIOCTLCMD)
  2335. retval = -EINVAL;
  2336. }
  2337. tty_ldisc_deref(ld);
  2338. return retval;
  2339. }
  2340. #ifdef CONFIG_COMPAT
  2341. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  2342. unsigned long arg)
  2343. {
  2344. struct inode *inode = file->f_dentry->d_inode;
  2345. struct tty_struct *tty = file->private_data;
  2346. struct tty_ldisc *ld;
  2347. int retval = -ENOIOCTLCMD;
  2348. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  2349. return -EINVAL;
  2350. if (tty->ops->compat_ioctl) {
  2351. retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
  2352. if (retval != -ENOIOCTLCMD)
  2353. return retval;
  2354. }
  2355. ld = tty_ldisc_ref_wait(tty);
  2356. if (ld->ops->compat_ioctl)
  2357. retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
  2358. tty_ldisc_deref(ld);
  2359. return retval;
  2360. }
  2361. #endif
  2362. /*
  2363. * This implements the "Secure Attention Key" --- the idea is to
  2364. * prevent trojan horses by killing all processes associated with this
  2365. * tty when the user hits the "Secure Attention Key". Required for
  2366. * super-paranoid applications --- see the Orange Book for more details.
  2367. *
  2368. * This code could be nicer; ideally it should send a HUP, wait a few
  2369. * seconds, then send a INT, and then a KILL signal. But you then
  2370. * have to coordinate with the init process, since all processes associated
  2371. * with the current tty must be dead before the new getty is allowed
  2372. * to spawn.
  2373. *
  2374. * Now, if it would be correct ;-/ The current code has a nasty hole -
  2375. * it doesn't catch files in flight. We may send the descriptor to ourselves
  2376. * via AF_UNIX socket, close it and later fetch from socket. FIXME.
  2377. *
  2378. * Nasty bug: do_SAK is being called in interrupt context. This can
  2379. * deadlock. We punt it up to process context. AKPM - 16Mar2001
  2380. */
  2381. void __do_SAK(struct tty_struct *tty)
  2382. {
  2383. #ifdef TTY_SOFT_SAK
  2384. tty_hangup(tty);
  2385. #else
  2386. struct task_struct *g, *p;
  2387. struct pid *session;
  2388. int i;
  2389. struct file *filp;
  2390. struct fdtable *fdt;
  2391. if (!tty)
  2392. return;
  2393. session = tty->session;
  2394. tty_ldisc_flush(tty);
  2395. tty_driver_flush_buffer(tty);
  2396. read_lock(&tasklist_lock);
  2397. /* Kill the entire session */
  2398. do_each_pid_task(session, PIDTYPE_SID, p) {
  2399. printk(KERN_NOTICE "SAK: killed process %d"
  2400. " (%s): task_session(p)==tty->session\n",
  2401. task_pid_nr(p), p->comm);
  2402. send_sig(SIGKILL, p, 1);
  2403. } while_each_pid_task(session, PIDTYPE_SID, p);
  2404. /* Now kill any processes that happen to have the
  2405. * tty open.
  2406. */
  2407. do_each_thread(g, p) {
  2408. if (p->signal->tty == tty) {
  2409. printk(KERN_NOTICE "SAK: killed process %d"
  2410. " (%s): task_session(p)==tty->session\n",
  2411. task_pid_nr(p), p->comm);
  2412. send_sig(SIGKILL, p, 1);
  2413. continue;
  2414. }
  2415. task_lock(p);
  2416. if (p->files) {
  2417. /*
  2418. * We don't take a ref to the file, so we must
  2419. * hold ->file_lock instead.
  2420. */
  2421. spin_lock(&p->files->file_lock);
  2422. fdt = files_fdtable(p->files);
  2423. for (i = 0; i < fdt->max_fds; i++) {
  2424. filp = fcheck_files(p->files, i);
  2425. if (!filp)
  2426. continue;
  2427. if (filp->f_op->read == tty_read &&
  2428. filp->private_data == tty) {
  2429. printk(KERN_NOTICE "SAK: killed process %d"
  2430. " (%s): fd#%d opened to the tty\n",
  2431. task_pid_nr(p), p->comm, i);
  2432. force_sig(SIGKILL, p);
  2433. break;
  2434. }
  2435. }
  2436. spin_unlock(&p->files->file_lock);
  2437. }
  2438. task_unlock(p);
  2439. } while_each_thread(g, p);
  2440. read_unlock(&tasklist_lock);
  2441. #endif
  2442. }
  2443. static void do_SAK_work(struct work_struct *work)
  2444. {
  2445. struct tty_struct *tty =
  2446. container_of(work, struct tty_struct, SAK_work);
  2447. __do_SAK(tty);
  2448. }
  2449. /*
  2450. * The tq handling here is a little racy - tty->SAK_work may already be queued.
  2451. * Fortunately we don't need to worry, because if ->SAK_work is already queued,
  2452. * the values which we write to it will be identical to the values which it
  2453. * already has. --akpm
  2454. */
  2455. void do_SAK(struct tty_struct *tty)
  2456. {
  2457. if (!tty)
  2458. return;
  2459. schedule_work(&tty->SAK_work);
  2460. }
  2461. EXPORT_SYMBOL(do_SAK);
  2462. /**
  2463. * initialize_tty_struct
  2464. * @tty: tty to initialize
  2465. *
  2466. * This subroutine initializes a tty structure that has been newly
  2467. * allocated.
  2468. *
  2469. * Locking: none - tty in question must not be exposed at this point
  2470. */
  2471. void initialize_tty_struct(struct tty_struct *tty,
  2472. struct tty_driver *driver, int idx)
  2473. {
  2474. memset(tty, 0, sizeof(struct tty_struct));
  2475. kref_init(&tty->kref);
  2476. tty->magic = TTY_MAGIC;
  2477. tty_ldisc_init(tty);
  2478. tty->session = NULL;
  2479. tty->pgrp = NULL;
  2480. tty->overrun_time = jiffies;
  2481. tty->buf.head = tty->buf.tail = NULL;
  2482. tty_buffer_init(tty);
  2483. mutex_init(&tty->termios_mutex);
  2484. mutex_init(&tty->ldisc_mutex);
  2485. init_waitqueue_head(&tty->write_wait);
  2486. init_waitqueue_head(&tty->read_wait);
  2487. INIT_WORK(&tty->hangup_work, do_tty_hangup);
  2488. mutex_init(&tty->atomic_read_lock);
  2489. mutex_init(&tty->atomic_write_lock);
  2490. mutex_init(&tty->output_lock);
  2491. mutex_init(&tty->echo_lock);
  2492. spin_lock_init(&tty->read_lock);
  2493. spin_lock_init(&tty->ctrl_lock);
  2494. INIT_LIST_HEAD(&tty->tty_files);
  2495. INIT_WORK(&tty->SAK_work, do_SAK_work);
  2496. tty->driver = driver;
  2497. tty->ops = driver->ops;
  2498. tty->index = idx;
  2499. tty_line_name(driver, idx, tty->name);
  2500. }
  2501. /**
  2502. * tty_put_char - write one character to a tty
  2503. * @tty: tty
  2504. * @ch: character
  2505. *
  2506. * Write one byte to the tty using the provided put_char method
  2507. * if present. Returns the number of characters successfully output.
  2508. *
  2509. * Note: the specific put_char operation in the driver layer may go
  2510. * away soon. Don't call it directly, use this method
  2511. */
  2512. int tty_put_char(struct tty_struct *tty, unsigned char ch)
  2513. {
  2514. if (tty->ops->put_char)
  2515. return tty->ops->put_char(tty, ch);
  2516. return tty->ops->write(tty, &ch, 1);
  2517. }
  2518. EXPORT_SYMBOL_GPL(tty_put_char);
  2519. struct class *tty_class;
  2520. /**
  2521. * tty_register_device - register a tty device
  2522. * @driver: the tty driver that describes the tty device
  2523. * @index: the index in the tty driver for this tty device
  2524. * @device: a struct device that is associated with this tty device.
  2525. * This field is optional, if there is no known struct device
  2526. * for this tty device it can be set to NULL safely.
  2527. *
  2528. * Returns a pointer to the struct device for this tty device
  2529. * (or ERR_PTR(-EFOO) on error).
  2530. *
  2531. * This call is required to be made to register an individual tty device
  2532. * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
  2533. * that bit is not set, this function should not be called by a tty
  2534. * driver.
  2535. *
  2536. * Locking: ??
  2537. */
  2538. struct device *tty_register_device(struct tty_driver *driver, unsigned index,
  2539. struct device *device)
  2540. {
  2541. char name[64];
  2542. dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
  2543. if (index >= driver->num) {
  2544. printk(KERN_ERR "Attempt to register invalid tty line number "
  2545. " (%d).\n", index);
  2546. return ERR_PTR(-EINVAL);
  2547. }
  2548. if (driver->type == TTY_DRIVER_TYPE_PTY)
  2549. pty_line_name(driver, index, name);
  2550. else
  2551. tty_line_name(driver, index, name);
  2552. return device_create(tty_class, device, dev, NULL, name);
  2553. }
  2554. EXPORT_SYMBOL(tty_register_device);
  2555. /**
  2556. * tty_unregister_device - unregister a tty device
  2557. * @driver: the tty driver that describes the tty device
  2558. * @index: the index in the tty driver for this tty device
  2559. *
  2560. * If a tty device is registered with a call to tty_register_device() then
  2561. * this function must be called when the tty device is gone.
  2562. *
  2563. * Locking: ??
  2564. */
  2565. void tty_unregister_device(struct tty_driver *driver, unsigned index)
  2566. {
  2567. device_destroy(tty_class,
  2568. MKDEV(driver->major, driver->minor_start) + index);
  2569. }
  2570. EXPORT_SYMBOL(tty_unregister_device);
  2571. struct tty_driver *alloc_tty_driver(int lines)
  2572. {
  2573. struct tty_driver *driver;
  2574. driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
  2575. if (driver) {
  2576. kref_init(&driver->kref);
  2577. driver->magic = TTY_DRIVER_MAGIC;
  2578. driver->num = lines;
  2579. /* later we'll move allocation of tables here */
  2580. }
  2581. return driver;
  2582. }
  2583. EXPORT_SYMBOL(alloc_tty_driver);
  2584. static void destruct_tty_driver(struct kref *kref)
  2585. {
  2586. struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
  2587. int i;
  2588. struct ktermios *tp;
  2589. void *p;
  2590. if (driver->flags & TTY_DRIVER_INSTALLED) {
  2591. /*
  2592. * Free the termios and termios_locked structures because
  2593. * we don't want to get memory leaks when modular tty
  2594. * drivers are removed from the kernel.
  2595. */
  2596. for (i = 0; i < driver->num; i++) {
  2597. tp = driver->termios[i];
  2598. if (tp) {
  2599. driver->termios[i] = NULL;
  2600. kfree(tp);
  2601. }
  2602. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
  2603. tty_unregister_device(driver, i);
  2604. }
  2605. p = driver->ttys;
  2606. proc_tty_unregister_driver(driver);
  2607. driver->ttys = NULL;
  2608. driver->termios = NULL;
  2609. kfree(p);
  2610. cdev_del(&driver->cdev);
  2611. }
  2612. kfree(driver);
  2613. }
  2614. void tty_driver_kref_put(struct tty_driver *driver)
  2615. {
  2616. kref_put(&driver->kref, destruct_tty_driver);
  2617. }
  2618. EXPORT_SYMBOL(tty_driver_kref_put);
  2619. void tty_set_operations(struct tty_driver *driver,
  2620. const struct tty_operations *op)
  2621. {
  2622. driver->ops = op;
  2623. };
  2624. EXPORT_SYMBOL(tty_set_operations);
  2625. void put_tty_driver(struct tty_driver *d)
  2626. {
  2627. tty_driver_kref_put(d);
  2628. }
  2629. EXPORT_SYMBOL(put_tty_driver);
  2630. /*
  2631. * Called by a tty driver to register itself.
  2632. */
  2633. int tty_register_driver(struct tty_driver *driver)
  2634. {
  2635. int error;
  2636. int i;
  2637. dev_t dev;
  2638. void **p = NULL;
  2639. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
  2640. p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
  2641. if (!p)
  2642. return -ENOMEM;
  2643. }
  2644. if (!driver->major) {
  2645. error = alloc_chrdev_region(&dev, driver->minor_start,
  2646. driver->num, driver->name);
  2647. if (!error) {
  2648. driver->major = MAJOR(dev);
  2649. driver->minor_start = MINOR(dev);
  2650. }
  2651. } else {
  2652. dev = MKDEV(driver->major, driver->minor_start);
  2653. error = register_chrdev_region(dev, driver->num, driver->name);
  2654. }
  2655. if (error < 0) {
  2656. kfree(p);
  2657. return error;
  2658. }
  2659. if (p) {
  2660. driver->ttys = (struct tty_struct **)p;
  2661. driver->termios = (struct ktermios **)(p + driver->num);
  2662. } else {
  2663. driver->ttys = NULL;
  2664. driver->termios = NULL;
  2665. }
  2666. cdev_init(&driver->cdev, &tty_fops);
  2667. driver->cdev.owner = driver->owner;
  2668. error = cdev_add(&driver->cdev, dev, driver->num);
  2669. if (error) {
  2670. unregister_chrdev_region(dev, driver->num);
  2671. driver->ttys = NULL;
  2672. driver->termios = NULL;
  2673. kfree(p);
  2674. return error;
  2675. }
  2676. mutex_lock(&tty_mutex);
  2677. list_add(&driver->tty_drivers, &tty_drivers);
  2678. mutex_unlock(&tty_mutex);
  2679. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
  2680. for (i = 0; i < driver->num; i++)
  2681. tty_register_device(driver, i, NULL);
  2682. }
  2683. proc_tty_register_driver(driver);
  2684. driver->flags |= TTY_DRIVER_INSTALLED;
  2685. return 0;
  2686. }
  2687. EXPORT_SYMBOL(tty_register_driver);
  2688. /*
  2689. * Called by a tty driver to unregister itself.
  2690. */
  2691. int tty_unregister_driver(struct tty_driver *driver)
  2692. {
  2693. #if 0
  2694. /* FIXME */
  2695. if (driver->refcount)
  2696. return -EBUSY;
  2697. #endif
  2698. unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
  2699. driver->num);
  2700. mutex_lock(&tty_mutex);
  2701. list_del(&driver->tty_drivers);
  2702. mutex_unlock(&tty_mutex);
  2703. return 0;
  2704. }
  2705. EXPORT_SYMBOL(tty_unregister_driver);
  2706. dev_t tty_devnum(struct tty_struct *tty)
  2707. {
  2708. return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
  2709. }
  2710. EXPORT_SYMBOL(tty_devnum);
  2711. void proc_clear_tty(struct task_struct *p)
  2712. {
  2713. unsigned long flags;
  2714. struct tty_struct *tty;
  2715. spin_lock_irqsave(&p->sighand->siglock, flags);
  2716. tty = p->signal->tty;
  2717. p->signal->tty = NULL;
  2718. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  2719. tty_kref_put(tty);
  2720. }
  2721. /* Called under the sighand lock */
  2722. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  2723. {
  2724. if (tty) {
  2725. unsigned long flags;
  2726. /* We should not have a session or pgrp to put here but.... */
  2727. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2728. put_pid(tty->session);
  2729. put_pid(tty->pgrp);
  2730. tty->pgrp = get_pid(task_pgrp(tsk));
  2731. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2732. tty->session = get_pid(task_session(tsk));
  2733. if (tsk->signal->tty) {
  2734. printk(KERN_DEBUG "tty not NULL!!\n");
  2735. tty_kref_put(tsk->signal->tty);
  2736. }
  2737. }
  2738. put_pid(tsk->signal->tty_old_pgrp);
  2739. tsk->signal->tty = tty_kref_get(tty);
  2740. tsk->signal->tty_old_pgrp = NULL;
  2741. }
  2742. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  2743. {
  2744. spin_lock_irq(&tsk->sighand->siglock);
  2745. __proc_set_tty(tsk, tty);
  2746. spin_unlock_irq(&tsk->sighand->siglock);
  2747. }
  2748. struct tty_struct *get_current_tty(void)
  2749. {
  2750. struct tty_struct *tty;
  2751. unsigned long flags;
  2752. spin_lock_irqsave(&current->sighand->siglock, flags);
  2753. tty = tty_kref_get(current->signal->tty);
  2754. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  2755. return tty;
  2756. }
  2757. EXPORT_SYMBOL_GPL(get_current_tty);
  2758. void tty_default_fops(struct file_operations *fops)
  2759. {
  2760. *fops = tty_fops;
  2761. }
  2762. /*
  2763. * Initialize the console device. This is called *early*, so
  2764. * we can't necessarily depend on lots of kernel help here.
  2765. * Just do some early initializations, and do the complex setup
  2766. * later.
  2767. */
  2768. void __init console_init(void)
  2769. {
  2770. initcall_t *call;
  2771. /* Setup the default TTY line discipline. */
  2772. tty_ldisc_begin();
  2773. /*
  2774. * set up the console device so that later boot sequences can
  2775. * inform about problems etc..
  2776. */
  2777. call = __con_initcall_start;
  2778. while (call < __con_initcall_end) {
  2779. (*call)();
  2780. call++;
  2781. }
  2782. }
  2783. static char *tty_devnode(struct device *dev, mode_t *mode)
  2784. {
  2785. if (!mode)
  2786. return NULL;
  2787. if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
  2788. dev->devt == MKDEV(TTYAUX_MAJOR, 2))
  2789. *mode = 0666;
  2790. return NULL;
  2791. }
  2792. static int __init tty_class_init(void)
  2793. {
  2794. tty_class = class_create(THIS_MODULE, "tty");
  2795. if (IS_ERR(tty_class))
  2796. return PTR_ERR(tty_class);
  2797. tty_class->devnode = tty_devnode;
  2798. return 0;
  2799. }
  2800. postcore_initcall(tty_class_init);
  2801. /* 3/2004 jmc: why do these devices exist? */
  2802. static struct cdev tty_cdev, console_cdev;
  2803. /*
  2804. * Ok, now we can initialize the rest of the tty devices and can count
  2805. * on memory allocations, interrupts etc..
  2806. */
  2807. int __init tty_init(void)
  2808. {
  2809. cdev_init(&tty_cdev, &tty_fops);
  2810. if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
  2811. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
  2812. panic("Couldn't register /dev/tty driver\n");
  2813. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
  2814. "tty");
  2815. cdev_init(&console_cdev, &console_fops);
  2816. if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
  2817. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
  2818. panic("Couldn't register /dev/console driver\n");
  2819. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
  2820. "console");
  2821. #ifdef CONFIG_VT
  2822. vty_init(&console_fops);
  2823. #endif
  2824. return 0;
  2825. }