dmesg.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. *
  4. * dmesg - display/control kernel ring buffer.
  5. *
  6. * Copyright 2006 Rob Landley <rob@landley.net>
  7. * Copyright 2006 Bernhard Reutner-Fischer <rep.nop@aon.at>
  8. *
  9. * Licensed under GPLv2, see file LICENSE in this source tree.
  10. */
  11. //config:config DMESG
  12. //config: bool "dmesg (3.5 kb)"
  13. //config: default y
  14. //config: select PLATFORM_LINUX
  15. //config: help
  16. //config: dmesg is used to examine or control the kernel ring buffer. When the
  17. //config: Linux kernel prints messages to the system log, they are stored in
  18. //config: the kernel ring buffer. You can use dmesg to print the kernel's ring
  19. //config: buffer, clear the kernel ring buffer, change the size of the kernel
  20. //config: ring buffer, and change the priority level at which kernel messages
  21. //config: are also logged to the system console. Enable this option if you
  22. //config: wish to enable the 'dmesg' utility.
  23. //config:
  24. //config:config FEATURE_DMESG_PRETTY
  25. //config: bool "Pretty output"
  26. //config: default y
  27. //config: depends on DMESG
  28. //config: help
  29. //config: If you wish to scrub the syslog level from the output, say 'Y' here.
  30. //config: The syslog level is a string prefixed to every line with the form
  31. //config: "<#>".
  32. //config:
  33. //config: With this option you will see:
  34. //config: # dmesg
  35. //config: Linux version 2.6.17.4 .....
  36. //config: BIOS-provided physical RAM map:
  37. //config: BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
  38. //config:
  39. //config: Without this option you will see:
  40. //config: # dmesg
  41. //config: <5>Linux version 2.6.17.4 .....
  42. //config: <6>BIOS-provided physical RAM map:
  43. //config: <6> BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
  44. //applet:IF_DMESG(APPLET(dmesg, BB_DIR_BIN, BB_SUID_DROP))
  45. //kbuild:lib-$(CONFIG_DMESG) += dmesg.o
  46. //usage:#define dmesg_trivial_usage
  47. //usage: "[-c] [-n LEVEL] [-s SIZE]"
  48. //usage:#define dmesg_full_usage "\n\n"
  49. //usage: "Print or control the kernel ring buffer\n"
  50. //usage: "\n -c Clear ring buffer after printing"
  51. //usage: "\n -n LEVEL Set console logging level"
  52. //usage: "\n -s SIZE Buffer size"
  53. //usage: "\n -r Print raw message buffer"
  54. #include <sys/klog.h>
  55. #include "libbb.h"
  56. int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  57. int dmesg_main(int argc UNUSED_PARAM, char **argv)
  58. {
  59. int len, level;
  60. char *buf;
  61. unsigned opts;
  62. enum {
  63. OPT_c = 1 << 0,
  64. OPT_s = 1 << 1,
  65. OPT_n = 1 << 2,
  66. OPT_r = 1 << 3
  67. };
  68. opts = getopt32(argv, "cs:+n:+r", &len, &level);
  69. if (opts & OPT_n) {
  70. if (klogctl(8, NULL, (long) level))
  71. bb_perror_msg_and_die("klogctl");
  72. return EXIT_SUCCESS;
  73. }
  74. if (!(opts & OPT_s))
  75. len = klogctl(10, NULL, 0); /* read ring buffer size */
  76. if (len < 16*1024)
  77. len = 16*1024;
  78. if (len > 16*1024*1024)
  79. len = 16*1024*1024;
  80. buf = xmalloc(len);
  81. len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
  82. if (len < 0)
  83. bb_perror_msg_and_die("klogctl");
  84. if (len == 0)
  85. return EXIT_SUCCESS;
  86. if (ENABLE_FEATURE_DMESG_PRETTY && !(opts & OPT_r)) {
  87. int last = '\n';
  88. int in = 0;
  89. /* Skip <[0-9]+> at the start of lines */
  90. while (1) {
  91. if (last == '\n' && buf[in] == '<') {
  92. while (buf[in++] != '>' && in < len)
  93. ;
  94. } else {
  95. last = buf[in++];
  96. putchar(last);
  97. }
  98. if (in >= len)
  99. break;
  100. }
  101. /* Make sure we end with a newline */
  102. if (last != '\n')
  103. bb_putchar('\n');
  104. } else {
  105. full_write(STDOUT_FILENO, buf, len);
  106. if (buf[len-1] != '\n')
  107. bb_putchar('\n');
  108. }
  109. if (ENABLE_FEATURE_CLEAN_UP) free(buf);
  110. return EXIT_SUCCESS;
  111. }