proc_tty.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /*
  17. * The /proc/tty directory inodes...
  18. */
  19. static struct proc_dir_entry *proc_tty_driver;
  20. /*
  21. * This is the handler for /proc/tty/drivers
  22. */
  23. static void show_tty_range(struct seq_file *m, struct tty_driver *p,
  24. dev_t from, int num)
  25. {
  26. seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown");
  27. seq_printf(m, "/dev/%-8s ", p->name);
  28. if (p->num > 1) {
  29. seq_printf(m, "%3d %d-%d ", MAJOR(from), MINOR(from),
  30. MINOR(from) + num - 1);
  31. } else {
  32. seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from));
  33. }
  34. switch (p->type) {
  35. case TTY_DRIVER_TYPE_SYSTEM:
  36. seq_puts(m, "system");
  37. if (p->subtype == SYSTEM_TYPE_TTY)
  38. seq_puts(m, ":/dev/tty");
  39. else if (p->subtype == SYSTEM_TYPE_SYSCONS)
  40. seq_puts(m, ":console");
  41. else if (p->subtype == SYSTEM_TYPE_CONSOLE)
  42. seq_puts(m, ":vtmaster");
  43. break;
  44. case TTY_DRIVER_TYPE_CONSOLE:
  45. seq_puts(m, "console");
  46. break;
  47. case TTY_DRIVER_TYPE_SERIAL:
  48. seq_puts(m, "serial");
  49. break;
  50. case TTY_DRIVER_TYPE_PTY:
  51. if (p->subtype == PTY_TYPE_MASTER)
  52. seq_puts(m, "pty:master");
  53. else if (p->subtype == PTY_TYPE_SLAVE)
  54. seq_puts(m, "pty:slave");
  55. else
  56. seq_puts(m, "pty");
  57. break;
  58. default:
  59. seq_printf(m, "type:%d.%d", p->type, p->subtype);
  60. }
  61. seq_putc(m, '\n');
  62. }
  63. static int show_tty_driver(struct seq_file *m, void *v)
  64. {
  65. struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers);
  66. dev_t from = MKDEV(p->major, p->minor_start);
  67. dev_t to = from + p->num;
  68. if (&p->tty_drivers == tty_drivers.next) {
  69. /* pseudo-drivers first */
  70. seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty");
  71. seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0);
  72. seq_puts(m, "system:/dev/tty\n");
  73. seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console");
  74. seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1);
  75. seq_puts(m, "system:console\n");
  76. #ifdef CONFIG_UNIX98_PTYS
  77. seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx");
  78. seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2);
  79. seq_puts(m, "system\n");
  80. #endif
  81. #ifdef CONFIG_VT
  82. seq_printf(m, "%-20s /dev/%-8s ", "/dev/vc/0", "vc/0");
  83. seq_printf(m, "%3d %7d ", TTY_MAJOR, 0);
  84. seq_puts(m, "system:vtmaster\n");
  85. #endif
  86. }
  87. while (MAJOR(from) < MAJOR(to)) {
  88. dev_t next = MKDEV(MAJOR(from)+1, 0);
  89. show_tty_range(m, p, from, next - from);
  90. from = next;
  91. }
  92. if (from != to)
  93. show_tty_range(m, p, from, to - from);
  94. return 0;
  95. }
  96. /* iterator */
  97. static void *t_start(struct seq_file *m, loff_t *pos)
  98. {
  99. mutex_lock(&tty_mutex);
  100. return seq_list_start(&tty_drivers, *pos);
  101. }
  102. static void *t_next(struct seq_file *m, void *v, loff_t *pos)
  103. {
  104. return seq_list_next(v, &tty_drivers, pos);
  105. }
  106. static void t_stop(struct seq_file *m, void *v)
  107. {
  108. mutex_unlock(&tty_mutex);
  109. }
  110. static const struct seq_operations tty_drivers_op = {
  111. .start = t_start,
  112. .next = t_next,
  113. .stop = t_stop,
  114. .show = show_tty_driver
  115. };
  116. static int tty_drivers_open(struct inode *inode, struct file *file)
  117. {
  118. return seq_open(file, &tty_drivers_op);
  119. }
  120. static const struct file_operations proc_tty_drivers_operations = {
  121. .open = tty_drivers_open,
  122. .read = seq_read,
  123. .llseek = seq_lseek,
  124. .release = seq_release,
  125. };
  126. /*
  127. * This function is called by tty_register_driver() to handle
  128. * registering the driver's /proc handler into /proc/tty/driver/<foo>
  129. */
  130. void proc_tty_register_driver(struct tty_driver *driver)
  131. {
  132. struct proc_dir_entry *ent;
  133. if (!driver->driver_name || driver->proc_entry ||
  134. !driver->ops->proc_fops)
  135. return;
  136. ent = proc_create_data(driver->driver_name, 0, proc_tty_driver,
  137. driver->ops->proc_fops, driver);
  138. driver->proc_entry = ent;
  139. }
  140. /*
  141. * This function is called by tty_unregister_driver()
  142. */
  143. void proc_tty_unregister_driver(struct tty_driver *driver)
  144. {
  145. struct proc_dir_entry *ent;
  146. ent = driver->proc_entry;
  147. if (!ent)
  148. return;
  149. remove_proc_entry(driver->driver_name, proc_tty_driver);
  150. driver->proc_entry = NULL;
  151. }
  152. /*
  153. * Called by proc_root_init() to initialize the /proc/tty subtree
  154. */
  155. void __init proc_tty_init(void)
  156. {
  157. if (!proc_mkdir("tty", NULL))
  158. return;
  159. proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */
  160. /*
  161. * /proc/tty/driver/serial reveals the exact character counts for
  162. * serial links which is just too easy to abuse for inferring
  163. * password lengths and inter-keystroke timings during password
  164. * entry.
  165. */
  166. proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR|S_IXUSR, NULL);
  167. proc_create("tty/ldiscs", 0, NULL, &tty_ldiscs_proc_fops);
  168. proc_create("tty/drivers", 0, NULL, &proc_tty_drivers_operations);
  169. }