proc_tty.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * proc_tty.c -- handles /proc/tty
  3. *
  4. * Copyright 1997, Theodore Ts'o
  5. */
  6. #include <asm/uaccess.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/errno.h>
  10. #include <linux/time.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/stat.h>
  13. #include <linux/tty.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/bitops.h>
  16. #include "internal.h"
  17. /*
  18. * The /proc/tty directory inodes...
  19. */
  20. static struct proc_dir_entry *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_puts(m, "system");
  38. if (p->subtype == SYSTEM_TYPE_TTY)
  39. seq_puts(m, ":/dev/tty");
  40. else if (p->subtype == SYSTEM_TYPE_SYSCONS)
  41. seq_puts(m, ":console");
  42. else if (p->subtype == SYSTEM_TYPE_CONSOLE)
  43. seq_puts(m, ":vtmaster");
  44. break;
  45. case TTY_DRIVER_TYPE_CONSOLE:
  46. seq_puts(m, "console");
  47. break;
  48. case TTY_DRIVER_TYPE_SERIAL:
  49. seq_puts(m, "serial");
  50. break;
  51. case TTY_DRIVER_TYPE_PTY:
  52. if (p->subtype == PTY_TYPE_MASTER)
  53. seq_puts(m, "pty:master");
  54. else if (p->subtype == PTY_TYPE_SLAVE)
  55. seq_puts(m, "pty:slave");
  56. else
  57. seq_puts(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 = list_entry(v, struct tty_driver, tty_drivers);
  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_puts(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_puts(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_puts(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_puts(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. mutex_lock(&tty_mutex);
  101. return seq_list_start(&tty_drivers, *pos);
  102. }
  103. static void *t_next(struct seq_file *m, void *v, loff_t *pos)
  104. {
  105. return seq_list_next(v, &tty_drivers, pos);
  106. }
  107. static void t_stop(struct seq_file *m, void *v)
  108. {
  109. mutex_unlock(&tty_mutex);
  110. }
  111. static const struct seq_operations tty_drivers_op = {
  112. .start = t_start,
  113. .next = t_next,
  114. .stop = t_stop,
  115. .show = show_tty_driver
  116. };
  117. static int tty_drivers_open(struct inode *inode, struct file *file)
  118. {
  119. return seq_open(file, &tty_drivers_op);
  120. }
  121. static const struct file_operations proc_tty_drivers_operations = {
  122. .open = tty_drivers_open,
  123. .read = seq_read,
  124. .llseek = seq_lseek,
  125. .release = seq_release,
  126. };
  127. /*
  128. * This function is called by tty_register_driver() to handle
  129. * registering the driver's /proc handler into /proc/tty/driver/<foo>
  130. */
  131. void proc_tty_register_driver(struct tty_driver *driver)
  132. {
  133. struct proc_dir_entry *ent;
  134. if (!driver->driver_name || driver->proc_entry ||
  135. !driver->ops->proc_fops)
  136. return;
  137. ent = proc_create_data(driver->driver_name, 0, proc_tty_driver,
  138. driver->ops->proc_fops, driver);
  139. driver->proc_entry = ent;
  140. }
  141. /*
  142. * This function is called by tty_unregister_driver()
  143. */
  144. void proc_tty_unregister_driver(struct tty_driver *driver)
  145. {
  146. struct proc_dir_entry *ent;
  147. ent = driver->proc_entry;
  148. if (!ent)
  149. return;
  150. remove_proc_entry(ent->name, proc_tty_driver);
  151. driver->proc_entry = NULL;
  152. }
  153. /*
  154. * Called by proc_root_init() to initialize the /proc/tty subtree
  155. */
  156. void __init proc_tty_init(void)
  157. {
  158. if (!proc_mkdir("tty", NULL))
  159. return;
  160. proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
  161. /*
  162. * /proc/tty/driver/serial reveals the exact character counts for
  163. * serial links which is just too easy to abuse for inferring
  164. * password lengths and inter-keystroke timings during password
  165. * entry.
  166. */
  167. proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR|S_IXUSR, NULL);
  168. proc_create("tty/ldiscs", 0, NULL, &tty_ldiscs_proc_fops);
  169. proc_create("tty/drivers", 0, NULL, &proc_tty_drivers_operations);
  170. }