dmesg.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Fischer <rep.nop@aon.at>
  8. *
  9. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  10. */
  11. #include <sys/klog.h>
  12. #include "libbb.h"
  13. int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  14. int dmesg_main(int argc ATTRIBUTE_UNUSED, char **argv)
  15. {
  16. int len;
  17. char *buf;
  18. char *size, *level;
  19. unsigned flags = getopt32(argv, "cs:n:", &size, &level);
  20. enum {
  21. OPT_c = 1<<0,
  22. OPT_s = 1<<1,
  23. OPT_n = 1<<2
  24. };
  25. if (flags & OPT_n) {
  26. if (klogctl(8, NULL, xatoul_range(level, 0, 10)))
  27. bb_perror_msg_and_die("klogctl");
  28. return EXIT_SUCCESS;
  29. }
  30. len = (flags & OPT_s) ? xatoul_range(size, 2, INT_MAX) : 16384;
  31. buf = xmalloc(len);
  32. len = klogctl(3 + (flags & OPT_c), buf, len);
  33. if (len < 0)
  34. bb_perror_msg_and_die("klogctl");
  35. if (len == 0)
  36. return EXIT_SUCCESS;
  37. /* Skip <#> at the start of lines, and make sure we end with a newline. */
  38. if (ENABLE_FEATURE_DMESG_PRETTY) {
  39. int last = '\n';
  40. int in = 0;
  41. do {
  42. if (last == '\n' && buf[in] == '<')
  43. in += 3;
  44. else {
  45. last = buf[in++];
  46. bb_putchar(last);
  47. }
  48. } while (in < len);
  49. if (last != '\n')
  50. bb_putchar('\n');
  51. } else {
  52. full_write(STDOUT_FILENO, buf, len);
  53. if (buf[len-1] != '\n')
  54. bb_putchar('\n');
  55. }
  56. if (ENABLE_FEATURE_CLEAN_UP) free(buf);
  57. return EXIT_SUCCESS;
  58. }