ttyprintk.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. struct ttyprintk_port {
  19. struct tty_port port;
  20. struct mutex port_write_mutex;
  21. };
  22. static struct ttyprintk_port tpk_port;
  23. /*
  24. * Our simple preformatting supports transparent output of (time-stamped)
  25. * printk messages (also suitable for logging service):
  26. * - any cr is replaced by nl
  27. * - adds a ttyprintk source tag in front of each line
  28. * - too long message is fragmeted, with '\'nl between fragments
  29. * - TPK_STR_SIZE isn't really the write_room limiting factor, bcause
  30. * it is emptied on the fly during preformatting.
  31. */
  32. #define TPK_STR_SIZE 508 /* should be bigger then max expected line length */
  33. #define TPK_MAX_ROOM 4096 /* we could assume 4K for instance */
  34. static const char *tpk_tag = "[U] "; /* U for User */
  35. static int tpk_curr;
  36. static int tpk_printk(const unsigned char *buf, int count)
  37. {
  38. static char tmp[TPK_STR_SIZE + 4];
  39. int i = tpk_curr;
  40. if (buf == NULL) {
  41. /* flush tmp[] */
  42. if (tpk_curr > 0) {
  43. /* non nl or cr terminated message - add nl */
  44. tmp[tpk_curr + 0] = '\n';
  45. tmp[tpk_curr + 1] = '\0';
  46. printk(KERN_INFO "%s%s", tpk_tag, tmp);
  47. tpk_curr = 0;
  48. }
  49. return i;
  50. }
  51. for (i = 0; i < count; i++) {
  52. tmp[tpk_curr] = buf[i];
  53. if (tpk_curr < TPK_STR_SIZE) {
  54. switch (buf[i]) {
  55. case '\r':
  56. /* replace cr with nl */
  57. tmp[tpk_curr + 0] = '\n';
  58. tmp[tpk_curr + 1] = '\0';
  59. printk(KERN_INFO "%s%s", tpk_tag, tmp);
  60. tpk_curr = 0;
  61. if (buf[i + 1] == '\n')
  62. i++;
  63. break;
  64. case '\n':
  65. tmp[tpk_curr + 1] = '\0';
  66. printk(KERN_INFO "%s%s", tpk_tag, tmp);
  67. tpk_curr = 0;
  68. break;
  69. default:
  70. tpk_curr++;
  71. }
  72. } else {
  73. /* end of tmp buffer reached: cut the message in two */
  74. tmp[tpk_curr + 1] = '\\';
  75. tmp[tpk_curr + 2] = '\n';
  76. tmp[tpk_curr + 3] = '\0';
  77. printk(KERN_INFO "%s%s", tpk_tag, tmp);
  78. tpk_curr = 0;
  79. }
  80. }
  81. return count;
  82. }
  83. /*
  84. * TTY operations open function.
  85. */
  86. static int tpk_open(struct tty_struct *tty, struct file *filp)
  87. {
  88. tty->driver_data = &tpk_port;
  89. return tty_port_open(&tpk_port.port, tty, filp);
  90. }
  91. /*
  92. * TTY operations close function.
  93. */
  94. static void tpk_close(struct tty_struct *tty, struct file *filp)
  95. {
  96. struct ttyprintk_port *tpkp = tty->driver_data;
  97. mutex_lock(&tpkp->port_write_mutex);
  98. /* flush tpk_printk buffer */
  99. tpk_printk(NULL, 0);
  100. mutex_unlock(&tpkp->port_write_mutex);
  101. tty_port_close(&tpkp->port, tty, filp);
  102. }
  103. /*
  104. * TTY operations write function.
  105. */
  106. static int tpk_write(struct tty_struct *tty,
  107. const unsigned char *buf, int count)
  108. {
  109. struct ttyprintk_port *tpkp = tty->driver_data;
  110. int ret;
  111. /* exclusive use of tpk_printk within this tty */
  112. mutex_lock(&tpkp->port_write_mutex);
  113. ret = tpk_printk(buf, count);
  114. mutex_unlock(&tpkp->port_write_mutex);
  115. return ret;
  116. }
  117. /*
  118. * TTY operations write_room function.
  119. */
  120. static int tpk_write_room(struct tty_struct *tty)
  121. {
  122. return TPK_MAX_ROOM;
  123. }
  124. /*
  125. * TTY operations ioctl function.
  126. */
  127. static int tpk_ioctl(struct tty_struct *tty,
  128. unsigned int cmd, unsigned long arg)
  129. {
  130. struct ttyprintk_port *tpkp = tty->driver_data;
  131. if (!tpkp)
  132. return -EINVAL;
  133. switch (cmd) {
  134. /* Stop TIOCCONS */
  135. case TIOCCONS:
  136. return -EOPNOTSUPP;
  137. default:
  138. return -ENOIOCTLCMD;
  139. }
  140. return 0;
  141. }
  142. static const struct tty_operations ttyprintk_ops = {
  143. .open = tpk_open,
  144. .close = tpk_close,
  145. .write = tpk_write,
  146. .write_room = tpk_write_room,
  147. .ioctl = tpk_ioctl,
  148. };
  149. struct tty_port_operations null_ops = { };
  150. static struct tty_driver *ttyprintk_driver;
  151. static int __init ttyprintk_init(void)
  152. {
  153. int ret = -ENOMEM;
  154. void *rp;
  155. ttyprintk_driver = alloc_tty_driver(1);
  156. if (!ttyprintk_driver)
  157. return ret;
  158. ttyprintk_driver->owner = THIS_MODULE;
  159. ttyprintk_driver->driver_name = "ttyprintk";
  160. ttyprintk_driver->name = "ttyprintk";
  161. ttyprintk_driver->major = TTYAUX_MAJOR;
  162. ttyprintk_driver->minor_start = 3;
  163. ttyprintk_driver->num = 1;
  164. ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  165. ttyprintk_driver->init_termios = tty_std_termios;
  166. ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
  167. ttyprintk_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  168. TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  169. tty_set_operations(ttyprintk_driver, &ttyprintk_ops);
  170. ret = tty_register_driver(ttyprintk_driver);
  171. if (ret < 0) {
  172. printk(KERN_ERR "Couldn't register ttyprintk driver\n");
  173. goto error;
  174. }
  175. /* create our unnumbered device */
  176. rp = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 3), NULL,
  177. ttyprintk_driver->name);
  178. if (IS_ERR(rp)) {
  179. printk(KERN_ERR "Couldn't create ttyprintk device\n");
  180. ret = PTR_ERR(rp);
  181. goto error;
  182. }
  183. tty_port_init(&tpk_port.port);
  184. tpk_port.port.ops = &null_ops;
  185. mutex_init(&tpk_port.port_write_mutex);
  186. return 0;
  187. error:
  188. put_tty_driver(ttyprintk_driver);
  189. ttyprintk_driver = NULL;
  190. return ret;
  191. }
  192. module_init(ttyprintk_init);