dmesg.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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, char **argv)
  15. {
  16. char *size, *level;
  17. int flags = getopt32(argv, "cs:n:", &size, &level);
  18. if (flags & 4) {
  19. if (klogctl(8, NULL, xatoul_range(level, 0, 10)))
  20. bb_perror_msg_and_die("klogctl");
  21. } else {
  22. int len;
  23. char *buf;
  24. len = (flags & 2) ? xatoul_range(size, 2, INT_MAX) : 16384;
  25. buf = xmalloc(len);
  26. if (0 > (len = klogctl(3 + (flags & 1), buf, len)))
  27. bb_perror_msg_and_die("klogctl");
  28. // Skip <#> at the start of lines, and make sure we end with a newline.
  29. if (ENABLE_FEATURE_DMESG_PRETTY) {
  30. int last = '\n';
  31. int in;
  32. for (in = 0; in<len;) {
  33. if (last == '\n' && buf[in] == '<') in += 3;
  34. else bb_putchar(last = buf[in++]);
  35. }
  36. if (last != '\n') bb_putchar('\n');
  37. } else {
  38. write(1,buf,len);
  39. if (len && buf[len-1]!='\n') bb_putchar('\n');
  40. }
  41. if (ENABLE_FEATURE_CLEAN_UP) free(buf);
  42. }
  43. return 0;
  44. }