consoles.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_PRINTBUFFER, 'p' },
  23. { CON_BRL, 'b' },
  24. { CON_ANYTIME, 'a' },
  25. };
  26. char flags[ARRAY_SIZE(con_flags) + 1];
  27. struct console *con = v;
  28. unsigned int a;
  29. dev_t dev = 0;
  30. if (con->device) {
  31. const struct tty_driver *driver;
  32. int index;
  33. driver = con->device(con, &index);
  34. if (driver) {
  35. dev = MKDEV(driver->major, driver->minor_start);
  36. dev += index;
  37. }
  38. }
  39. for (a = 0; a < ARRAY_SIZE(con_flags); a++)
  40. flags[a] = (con->flags & con_flags[a].flag) ?
  41. con_flags[a].name : ' ';
  42. flags[a] = 0;
  43. seq_setwidth(m, 21 - 1);
  44. seq_printf(m, "%s%d", con->name, con->index);
  45. seq_pad(m, ' ');
  46. seq_printf(m, "%c%c%c (%s)", con->read ? 'R' : '-',
  47. con->write ? 'W' : '-', con->unblank ? 'U' : '-',
  48. flags);
  49. if (dev)
  50. seq_printf(m, " %4d:%d", MAJOR(dev), MINOR(dev));
  51. seq_putc(m, '\n');
  52. return 0;
  53. }
  54. static void *c_start(struct seq_file *m, loff_t *pos)
  55. {
  56. struct console *con;
  57. loff_t off = 0;
  58. console_lock();
  59. for_each_console(con)
  60. if (off++ == *pos)
  61. break;
  62. return con;
  63. }
  64. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  65. {
  66. struct console *con = v;
  67. ++*pos;
  68. return con->next;
  69. }
  70. static void c_stop(struct seq_file *m, void *v)
  71. {
  72. console_unlock();
  73. }
  74. static const struct seq_operations consoles_op = {
  75. .start = c_start,
  76. .next = c_next,
  77. .stop = c_stop,
  78. .show = show_console_dev
  79. };
  80. static int __init proc_consoles_init(void)
  81. {
  82. proc_create_seq("consoles", 0, NULL, &consoles_op);
  83. return 0;
  84. }
  85. fs_initcall(proc_consoles_init);