consoles.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010 Werner Fink, Jiri Slaby
  4. */
  5. #include <linux/console.h>
  6. #include <linux/kernel.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/tty_driver.h>
  10. /*
  11. * This is handler for /proc/consoles
  12. */
  13. static int show_console_dev(struct seq_file *m, void *v)
  14. {
  15. static const struct {
  16. short flag;
  17. char name;
  18. } con_flags[] = {
  19. { CON_ENABLED, 'E' },
  20. { CON_CONSDEV, 'C' },
  21. { CON_BOOT, 'B' },
  22. { CON_NBCON, 'N' },
  23. { CON_PRINTBUFFER, 'p' },
  24. { CON_BRL, 'b' },
  25. { CON_ANYTIME, 'a' },
  26. };
  27. char flags[ARRAY_SIZE(con_flags) + 1];
  28. struct console *con = v;
  29. unsigned int a;
  30. dev_t dev = 0;
  31. if (con->device) {
  32. const struct tty_driver *driver;
  33. int index;
  34. /*
  35. * Take console_lock to serialize device() callback with
  36. * other console operations. For example, fg_console is
  37. * modified under console_lock when switching vt.
  38. */
  39. console_lock();
  40. driver = con->device(con, &index);
  41. console_unlock();
  42. if (driver) {
  43. dev = MKDEV(driver->major, driver->minor_start);
  44. dev += index;
  45. }
  46. }
  47. for (a = 0; a < ARRAY_SIZE(con_flags); a++)
  48. flags[a] = (con->flags & con_flags[a].flag) ?
  49. con_flags[a].name : ' ';
  50. flags[a] = 0;
  51. seq_setwidth(m, 21 - 1);
  52. seq_printf(m, "%s%d", con->name, con->index);
  53. seq_pad(m, ' ');
  54. seq_printf(m, "%c%c%c (%s)", con->read ? 'R' : '-',
  55. ((con->flags & CON_NBCON) || con->write) ? 'W' : '-',
  56. con->unblank ? 'U' : '-', flags);
  57. if (dev)
  58. seq_printf(m, " %4d:%d", MAJOR(dev), MINOR(dev));
  59. seq_putc(m, '\n');
  60. return 0;
  61. }
  62. static void *c_start(struct seq_file *m, loff_t *pos)
  63. __acquires(&console_mutex)
  64. {
  65. struct console *con;
  66. loff_t off = 0;
  67. /*
  68. * Hold the console_list_lock to guarantee safe traversal of the
  69. * console list. SRCU cannot be used because there is no
  70. * place to store the SRCU cookie.
  71. */
  72. console_list_lock();
  73. for_each_console(con)
  74. if (off++ == *pos)
  75. break;
  76. return con;
  77. }
  78. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  79. {
  80. struct console *con = v;
  81. ++*pos;
  82. return hlist_entry_safe(con->node.next, struct console, node);
  83. }
  84. static void c_stop(struct seq_file *m, void *v)
  85. __releases(&console_mutex)
  86. {
  87. console_list_unlock();
  88. }
  89. static const struct seq_operations consoles_op = {
  90. .start = c_start,
  91. .next = c_next,
  92. .stop = c_stop,
  93. .show = show_console_dev
  94. };
  95. static int __init proc_consoles_init(void)
  96. {
  97. proc_create_seq("consoles", 0, NULL, &consoles_op);
  98. return 0;
  99. }
  100. fs_initcall(proc_consoles_init);