ttyprintk.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 fragmeted, with '\'nl between fragments
  30. * - TPK_STR_SIZE isn't really the write_room limiting factor, bcause
  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 const char *tpk_tag = "[U] "; /* U for User */
  36. static int tpk_curr;
  37. static int tpk_printk(const unsigned char *buf, int count)
  38. {
  39. static char tmp[TPK_STR_SIZE + 4];
  40. int i = tpk_curr;
  41. if (buf == NULL) {
  42. /* flush tmp[] */
  43. if (tpk_curr > 0) {
  44. /* non nl or cr terminated message - add nl */
  45. tmp[tpk_curr + 0] = '\n';
  46. tmp[tpk_curr + 1] = '\0';
  47. printk(KERN_INFO "%s%s", tpk_tag, tmp);
  48. tpk_curr = 0;
  49. }
  50. return i;
  51. }
  52. for (i = 0; i < count; i++) {
  53. tmp[tpk_curr] = buf[i];
  54. if (tpk_curr < TPK_STR_SIZE) {
  55. switch (buf[i]) {
  56. case '\r':
  57. /* replace cr with nl */
  58. tmp[tpk_curr + 0] = '\n';
  59. tmp[tpk_curr + 1] = '\0';
  60. printk(KERN_INFO "%s%s", tpk_tag, tmp);
  61. tpk_curr = 0;
  62. if ((i + 1) < count && buf[i + 1] == '\n')
  63. i++;
  64. break;
  65. case '\n':
  66. tmp[tpk_curr + 1] = '\0';
  67. printk(KERN_INFO "%s%s", tpk_tag, tmp);
  68. tpk_curr = 0;
  69. break;
  70. default:
  71. tpk_curr++;
  72. }
  73. } else {
  74. /* end of tmp buffer reached: cut the message in two */
  75. tmp[tpk_curr + 1] = '\\';
  76. tmp[tpk_curr + 2] = '\n';
  77. tmp[tpk_curr + 3] = '\0';
  78. printk(KERN_INFO "%s%s", tpk_tag, tmp);
  79. tpk_curr = 0;
  80. }
  81. }
  82. return count;
  83. }
  84. /*
  85. * TTY operations open function.
  86. */
  87. static int tpk_open(struct tty_struct *tty, struct file *filp)
  88. {
  89. tty->driver_data = &tpk_port;
  90. return tty_port_open(&tpk_port.port, tty, filp);
  91. }
  92. /*
  93. * TTY operations close function.
  94. */
  95. static void tpk_close(struct tty_struct *tty, struct file *filp)
  96. {
  97. struct ttyprintk_port *tpkp = tty->driver_data;
  98. mutex_lock(&tpkp->port_write_mutex);
  99. /* flush tpk_printk buffer */
  100. tpk_printk(NULL, 0);
  101. mutex_unlock(&tpkp->port_write_mutex);
  102. tty_port_close(&tpkp->port, tty, filp);
  103. }
  104. /*
  105. * TTY operations write function.
  106. */
  107. static int tpk_write(struct tty_struct *tty,
  108. const unsigned char *buf, int count)
  109. {
  110. struct ttyprintk_port *tpkp = tty->driver_data;
  111. int ret;
  112. /* exclusive use of tpk_printk within this tty */
  113. mutex_lock(&tpkp->port_write_mutex);
  114. ret = tpk_printk(buf, count);
  115. mutex_unlock(&tpkp->port_write_mutex);
  116. return ret;
  117. }
  118. /*
  119. * TTY operations write_room function.
  120. */
  121. static int tpk_write_room(struct tty_struct *tty)
  122. {
  123. return TPK_MAX_ROOM;
  124. }
  125. /*
  126. * TTY operations ioctl function.
  127. */
  128. static int tpk_ioctl(struct tty_struct *tty,
  129. unsigned int cmd, unsigned long arg)
  130. {
  131. struct ttyprintk_port *tpkp = tty->driver_data;
  132. if (!tpkp)
  133. return -EINVAL;
  134. switch (cmd) {
  135. /* Stop TIOCCONS */
  136. case TIOCCONS:
  137. return -EOPNOTSUPP;
  138. default:
  139. return -ENOIOCTLCMD;
  140. }
  141. return 0;
  142. }
  143. static const struct tty_operations ttyprintk_ops = {
  144. .open = tpk_open,
  145. .close = tpk_close,
  146. .write = tpk_write,
  147. .write_room = tpk_write_room,
  148. .ioctl = tpk_ioctl,
  149. };
  150. static struct tty_port_operations null_ops = { };
  151. static struct tty_driver *ttyprintk_driver;
  152. static int __init ttyprintk_init(void)
  153. {
  154. int ret = -ENOMEM;
  155. mutex_init(&tpk_port.port_write_mutex);
  156. ttyprintk_driver = tty_alloc_driver(1,
  157. TTY_DRIVER_RESET_TERMIOS |
  158. TTY_DRIVER_REAL_RAW |
  159. TTY_DRIVER_UNNUMBERED_NODE);
  160. if (IS_ERR(ttyprintk_driver))
  161. return PTR_ERR(ttyprintk_driver);
  162. tty_port_init(&tpk_port.port);
  163. tpk_port.port.ops = &null_ops;
  164. ttyprintk_driver->driver_name = "ttyprintk";
  165. ttyprintk_driver->name = "ttyprintk";
  166. ttyprintk_driver->major = TTYAUX_MAJOR;
  167. ttyprintk_driver->minor_start = 3;
  168. ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  169. ttyprintk_driver->init_termios = tty_std_termios;
  170. ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
  171. tty_set_operations(ttyprintk_driver, &ttyprintk_ops);
  172. tty_port_link_device(&tpk_port.port, ttyprintk_driver, 0);
  173. ret = tty_register_driver(ttyprintk_driver);
  174. if (ret < 0) {
  175. printk(KERN_ERR "Couldn't register ttyprintk driver\n");
  176. goto error;
  177. }
  178. return 0;
  179. error:
  180. put_tty_driver(ttyprintk_driver);
  181. tty_port_destroy(&tpk_port.port);
  182. return ret;
  183. }
  184. static void __exit ttyprintk_exit(void)
  185. {
  186. tty_unregister_driver(ttyprintk_driver);
  187. put_tty_driver(ttyprintk_driver);
  188. tty_port_destroy(&tpk_port.port);
  189. }
  190. device_initcall(ttyprintk_init);
  191. module_exit(ttyprintk_exit);
  192. MODULE_LICENSE("GPL");