proc_tty.c 5.8 KB

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