proc_tty.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * proc_tty.c -- handles /proc/tty
  3. *
  4. * Copyright 1997, Theodore Ts'o
  5. */
  6. #include <asm/uaccess.h>
  7. #include <linux/init.h>
  8. #include <linux/errno.h>
  9. #include <linux/time.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/stat.h>
  12. #include <linux/tty.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/bitops.h>
  15. /*
  16. * The /proc/tty directory inodes...
  17. */
  18. static struct proc_dir_entry *proc_tty_ldisc, *proc_tty_driver;
  19. /*
  20. * This is the handler for /proc/tty/drivers
  21. */
  22. static void show_tty_range(struct seq_file *m, struct tty_driver *p,
  23. dev_t from, int num)
  24. {
  25. seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown");
  26. seq_printf(m, "/dev/%-8s ", p->name);
  27. if (p->num > 1) {
  28. seq_printf(m, "%3d %d-%d ", MAJOR(from), MINOR(from),
  29. MINOR(from) + num - 1);
  30. } else {
  31. seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from));
  32. }
  33. switch (p->type) {
  34. case TTY_DRIVER_TYPE_SYSTEM:
  35. seq_printf(m, "system");
  36. if (p->subtype == SYSTEM_TYPE_TTY)
  37. seq_printf(m, ":/dev/tty");
  38. else if (p->subtype == SYSTEM_TYPE_SYSCONS)
  39. seq_printf(m, ":console");
  40. else if (p->subtype == SYSTEM_TYPE_CONSOLE)
  41. seq_printf(m, ":vtmaster");
  42. break;
  43. case TTY_DRIVER_TYPE_CONSOLE:
  44. seq_printf(m, "console");
  45. break;
  46. case TTY_DRIVER_TYPE_SERIAL:
  47. seq_printf(m, "serial");
  48. break;
  49. case TTY_DRIVER_TYPE_PTY:
  50. if (p->subtype == PTY_TYPE_MASTER)
  51. seq_printf(m, "pty:master");
  52. else if (p->subtype == PTY_TYPE_SLAVE)
  53. seq_printf(m, "pty:slave");
  54. else
  55. seq_printf(m, "pty");
  56. break;
  57. default:
  58. seq_printf(m, "type:%d.%d", p->type, p->subtype);
  59. }
  60. seq_putc(m, '\n');
  61. }
  62. static int show_tty_driver(struct seq_file *m, void *v)
  63. {
  64. struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers);
  65. dev_t from = MKDEV(p->major, p->minor_start);
  66. dev_t to = from + p->num;
  67. if (&p->tty_drivers == tty_drivers.next) {
  68. /* pseudo-drivers first */
  69. seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty");
  70. seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0);
  71. seq_printf(m, "system:/dev/tty\n");
  72. seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console");
  73. seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1);
  74. seq_printf(m, "system:console\n");
  75. #ifdef CONFIG_UNIX98_PTYS
  76. seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx");
  77. seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2);
  78. seq_printf(m, "system\n");
  79. #endif
  80. #ifdef CONFIG_VT
  81. seq_printf(m, "%-20s /dev/%-8s ", "/dev/vc/0", "vc/0");
  82. seq_printf(m, "%3d %7d ", TTY_MAJOR, 0);
  83. seq_printf(m, "system:vtmaster\n");
  84. #endif
  85. }
  86. while (MAJOR(from) < MAJOR(to)) {
  87. dev_t next = MKDEV(MAJOR(from)+1, 0);
  88. show_tty_range(m, p, from, next - from);
  89. from = next;
  90. }
  91. if (from != to)
  92. show_tty_range(m, p, from, to - from);
  93. return 0;
  94. }
  95. /* iterator */
  96. static void *t_start(struct seq_file *m, loff_t *pos)
  97. {
  98. mutex_lock(&tty_mutex);
  99. return seq_list_start(&tty_drivers, *pos);
  100. }
  101. static void *t_next(struct seq_file *m, void *v, loff_t *pos)
  102. {
  103. return seq_list_next(v, &tty_drivers, pos);
  104. }
  105. static void t_stop(struct seq_file *m, void *v)
  106. {
  107. mutex_unlock(&tty_mutex);
  108. }
  109. static const struct seq_operations tty_drivers_op = {
  110. .start = t_start,
  111. .next = t_next,
  112. .stop = t_stop,
  113. .show = show_tty_driver
  114. };
  115. static int tty_drivers_open(struct inode *inode, struct file *file)
  116. {
  117. return seq_open(file, &tty_drivers_op);
  118. }
  119. static const struct file_operations proc_tty_drivers_operations = {
  120. .open = tty_drivers_open,
  121. .read = seq_read,
  122. .llseek = seq_lseek,
  123. .release = seq_release,
  124. };
  125. /*
  126. * This is the handler for /proc/tty/ldiscs
  127. */
  128. static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
  129. int count, int *eof, void *data)
  130. {
  131. int i;
  132. int len = 0;
  133. off_t begin = 0;
  134. struct tty_ldisc *ld;
  135. for (i=0; i < NR_LDISCS; i++) {
  136. ld = tty_ldisc_get(i);
  137. if (ld == NULL)
  138. continue;
  139. len += sprintf(page+len, "%-10s %2d\n",
  140. ld->name ? ld->name : "???", i);
  141. tty_ldisc_put(i);
  142. if (len+begin > off+count)
  143. break;
  144. if (len+begin < off) {
  145. begin += len;
  146. len = 0;
  147. }
  148. }
  149. if (i >= NR_LDISCS)
  150. *eof = 1;
  151. if (off >= len+begin)
  152. return 0;
  153. *start = page + (off-begin);
  154. return ((count < begin+len-off) ? count : begin+len-off);
  155. }
  156. /*
  157. * This function is called by tty_register_driver() to handle
  158. * registering the driver's /proc handler into /proc/tty/driver/<foo>
  159. */
  160. void proc_tty_register_driver(struct tty_driver *driver)
  161. {
  162. struct proc_dir_entry *ent;
  163. if ((!driver->read_proc && !driver->write_proc) ||
  164. !driver->driver_name ||
  165. driver->proc_entry)
  166. return;
  167. ent = create_proc_entry(driver->driver_name, 0, proc_tty_driver);
  168. if (!ent)
  169. return;
  170. ent->read_proc = driver->read_proc;
  171. ent->write_proc = driver->write_proc;
  172. ent->owner = driver->owner;
  173. ent->data = driver;
  174. driver->proc_entry = ent;
  175. }
  176. /*
  177. * This function is called by tty_unregister_driver()
  178. */
  179. void proc_tty_unregister_driver(struct tty_driver *driver)
  180. {
  181. struct proc_dir_entry *ent;
  182. ent = driver->proc_entry;
  183. if (!ent)
  184. return;
  185. remove_proc_entry(driver->driver_name, proc_tty_driver);
  186. driver->proc_entry = NULL;
  187. }
  188. /*
  189. * Called by proc_root_init() to initialize the /proc/tty subtree
  190. */
  191. void __init proc_tty_init(void)
  192. {
  193. struct proc_dir_entry *entry;
  194. if (!proc_mkdir("tty", NULL))
  195. return;
  196. proc_tty_ldisc = proc_mkdir("tty/ldisc", NULL);
  197. /*
  198. * /proc/tty/driver/serial reveals the exact character counts for
  199. * serial links which is just too easy to abuse for inferring
  200. * password lengths and inter-keystroke timings during password
  201. * entry.
  202. */
  203. proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR | S_IXUSR, NULL);
  204. create_proc_read_entry("tty/ldiscs", 0, NULL, tty_ldiscs_read_proc, NULL);
  205. entry = create_proc_entry("tty/drivers", 0, NULL);
  206. if (entry)
  207. entry->proc_fops = &proc_tty_drivers_operations;
  208. }