dmesg.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* vi: set sw=4 ts=4: */
  2. /* dmesg.c -- Print out the contents of the kernel ring buffer
  3. * Created: Sat Oct 9 16:19:47 1993
  4. * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu
  5. * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu)
  6. * This program comes with ABSOLUTELY NO WARRANTY.
  7. * Modifications by Rick Sladkey (jrs@world.std.com)
  8. * Larger buffersize 3 June 1998 by Nicolai Langfeldt, based on a patch
  9. * by Peeter Joot. This was also suggested by John Hudson.
  10. * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
  11. * - added Native Language Support
  12. *
  13. * from util-linux -- adapted for busybox by
  14. * Erik Andersen <andersen@codepoet.org>. I ripped out Native Language
  15. * Support, replaced getopt, added some gotos for redundant stuff.
  16. *
  17. * Audited and cleaned up on 7 March 2003 to reduce size of
  18. * check error handling by Erik Andersen <andersen@codepoet.org>
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include <sys/klog.h>
  24. #include "busybox.h"
  25. int dmesg_main(int argc, char **argv)
  26. {
  27. char *buf, *tmp;
  28. int bufsize = 8196;
  29. int i, n = 0;
  30. int c = 3;
  31. i = bb_getopt_ulflags(argc, argv, "cn:s:", &buf, &tmp);
  32. if (i & 1)
  33. c = 4;
  34. if (i & 2) {
  35. c = 8;
  36. n = bb_xgetlarg(buf, 10, 0, 10);
  37. }
  38. if (i & 4)
  39. /* I think a 512k max kernel ring buffer is big enough for
  40. * anybody, as the default is 16k... Could be wrong though.
  41. * If so I'm sure I'll hear about it by the enraged masses*/
  42. bufsize = bb_xgetlarg(tmp, 10, 4096, 512*1024);
  43. if (c == 8) {
  44. if (klogctl(c, NULL, n) < 0)
  45. goto die_the_death;
  46. goto all_done;
  47. }
  48. buf = xmalloc(bufsize);
  49. if ((n = klogctl(c, buf, bufsize)) < 0)
  50. goto die_the_death;
  51. c = '\n';
  52. for (i = 0; i < n; i++) {
  53. if (c == '\n' && buf[i] == '<') {
  54. i++;
  55. while (buf[i] >= '0' && buf[i] <= '9')
  56. i++;
  57. if (buf[i] == '>')
  58. i++;
  59. }
  60. c = buf[i];
  61. putchar(c);
  62. }
  63. if (c != '\n')
  64. putchar('\n');
  65. all_done:
  66. if (ENABLE_FEATURE_CLEAN_UP) {
  67. free(buf);
  68. }
  69. return EXIT_SUCCESS;
  70. die_the_death:
  71. bb_perror_nomsg_and_die();
  72. }