ttyprintk.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * linux/drivers/char/ttyprintk.c
  3. *
  4. * Copyright (C) 2010 Samo Pogacnik
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the smems of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. */
  10. /*
  11. * This pseudo device allows user to make printk messages. It is possible
  12. * to store "console" messages inline with kernel messages for better analyses
  13. * of the boot process, for example.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/serial.h>
  17. #include <linux/tty.h>
  18. #include <linux/module.h>
  19. struct ttyprintk_port {
  20. struct tty_port port;
  21. struct mutex port_write_mutex;
  22. };
  23. static struct ttyprintk_port tpk_port;
  24. /*
  25. * Our simple preformatting supports transparent output of (time-stamped)
  26. * printk messages (also suitable for logging service):
  27. * - any cr is replaced by nl
  28. * - adds a ttyprintk source tag in front of each line
  29. * - too long message is fragmented, with '\'nl between fragments
  30. * - TPK_STR_SIZE isn't really the write_room limiting factor, because
  31. * it is emptied on the fly during preformatting.
  32. */
  33. #define TPK_STR_SIZE 508 /* should be bigger then max expected line length */
  34. #define TPK_MAX_ROOM 4096 /* we could assume 4K for instance */
  35. static int tpk_curr;
  36. static char tpk_buffer[TPK_STR_SIZE + 4];
  37. static void tpk_flush(void)
  38. {
  39. if (tpk_curr > 0) {
  40. tpk_buffer[tpk_curr] = '\0';
  41. pr_info("[U] %s\n", tpk_buffer);
  42. tpk_curr = 0;
  43. }
  44. }
  45. static int tpk_printk(const unsigned char *buf, int count)
  46. {
  47. int i = tpk_curr;
  48. if (buf == NULL) {
  49. tpk_flush();
  50. return i;
  51. }
  52. for (i = 0; i < count; i++) {
  53. if (tpk_curr >= TPK_STR_SIZE) {
  54. /* end of tmp buffer reached: cut the message in two */
  55. tpk_buffer[tpk_curr++] = '\\';
  56. tpk_flush();
  57. }
  58. switch (buf[i]) {
  59. case '\r':
  60. tpk_flush();
  61. if ((i + 1) < count && buf[i + 1] == '\n')
  62. i++;
  63. break;
  64. case '\n':
  65. tpk_flush();
  66. break;
  67. default:
  68. tpk_buffer[tpk_curr++] = buf[i];
  69. break;
  70. }
  71. }
  72. return count;
  73. }
  74. /*
  75. * TTY operations open function.
  76. */
  77. static int tpk_open(struct tty_struct *tty, struct file *filp)
  78. {
  79. tty->driver_data = &tpk_port;
  80. return tty_port_open(&tpk_port.port, tty, filp);
  81. }
  82. /*
  83. * TTY operations close function.
  84. */
  85. static void tpk_close(struct tty_struct *tty, struct file *filp)
  86. {
  87. struct ttyprintk_port *tpkp = tty->driver_data;
  88. mutex_lock(&tpkp->port_write_mutex);
  89. /* flush tpk_printk buffer */
  90. tpk_printk(NULL, 0);
  91. mutex_unlock(&tpkp->port_write_mutex);
  92. tty_port_close(&tpkp->port, tty, filp);
  93. }
  94. /*
  95. * TTY operations write function.
  96. */
  97. static int tpk_write(struct tty_struct *tty,
  98. const unsigned char *buf, int count)
  99. {
  100. struct ttyprintk_port *tpkp = tty->driver_data;
  101. int ret;
  102. /* exclusive use of tpk_printk within this tty */
  103. mutex_lock(&tpkp->port_write_mutex);
  104. ret = tpk_printk(buf, count);
  105. mutex_unlock(&tpkp->port_write_mutex);
  106. return ret;
  107. }
  108. /*
  109. * TTY operations write_room function.
  110. */
  111. static int tpk_write_room(struct tty_struct *tty)
  112. {
  113. return TPK_MAX_ROOM;
  114. }
  115. /*
  116. * TTY operations ioctl function.
  117. */
  118. static int tpk_ioctl(struct tty_struct *tty,
  119. unsigned int cmd, unsigned long arg)
  120. {
  121. struct ttyprintk_port *tpkp = tty->driver_data;
  122. if (!tpkp)
  123. return -EINVAL;
  124. switch (cmd) {
  125. /* Stop TIOCCONS */
  126. case TIOCCONS:
  127. return -EOPNOTSUPP;
  128. default:
  129. return -ENOIOCTLCMD;
  130. }
  131. return 0;
  132. }
  133. static const struct tty_operations ttyprintk_ops = {
  134. .open = tpk_open,
  135. .close = tpk_close,
  136. .write = tpk_write,
  137. .write_room = tpk_write_room,
  138. .ioctl = tpk_ioctl,
  139. };
  140. static const struct tty_port_operations null_ops = { };
  141. static struct tty_driver *ttyprintk_driver;
  142. static int __init ttyprintk_init(void)
  143. {
  144. int ret = -ENOMEM;
  145. mutex_init(&tpk_port.port_write_mutex);
  146. ttyprintk_driver = tty_alloc_driver(1,
  147. TTY_DRIVER_RESET_TERMIOS |
  148. TTY_DRIVER_REAL_RAW |
  149. TTY_DRIVER_UNNUMBERED_NODE);
  150. if (IS_ERR(ttyprintk_driver))
  151. return PTR_ERR(ttyprintk_driver);
  152. tty_port_init(&tpk_port.port);
  153. tpk_port.port.ops = &null_ops;
  154. ttyprintk_driver->driver_name = "ttyprintk";
  155. ttyprintk_driver->name = "ttyprintk";
  156. ttyprintk_driver->major = TTYAUX_MAJOR;
  157. ttyprintk_driver->minor_start = 3;
  158. ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  159. ttyprintk_driver->init_termios = tty_std_termios;
  160. ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
  161. tty_set_operations(ttyprintk_driver, &ttyprintk_ops);
  162. tty_port_link_device(&tpk_port.port, ttyprintk_driver, 0);
  163. ret = tty_register_driver(ttyprintk_driver);
  164. if (ret < 0) {
  165. printk(KERN_ERR "Couldn't register ttyprintk driver\n");
  166. goto error;
  167. }
  168. return 0;
  169. error:
  170. put_tty_driver(ttyprintk_driver);
  171. tty_port_destroy(&tpk_port.port);
  172. return ret;
  173. }
  174. static void __exit ttyprintk_exit(void)
  175. {
  176. tty_unregister_driver(ttyprintk_driver);
  177. put_tty_driver(ttyprintk_driver);
  178. tty_port_destroy(&tpk_port.port);
  179. }
  180. device_initcall(ttyprintk_init);
  181. module_exit(ttyprintk_exit);
  182. MODULE_LICENSE("GPL");