3
0

dmesg.c 3.4 KB

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