dmesg.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <getopt.h>
  23. #include <errno.h>
  24. #include <sys/klog.h>
  25. #include "busybox.h"
  26. int dmesg_main(int argc, char **argv)
  27. {
  28. char *buf
  29. #ifdef CONFIG_FEATURE_CLEAN_UP
  30. = NULL
  31. #endif
  32. ;
  33. int bufsize = 8196;
  34. int i, n;
  35. int level = 0;
  36. int lastc;
  37. int cmd = 3;
  38. while ((i = getopt(argc, argv, "cn:s:")) > 0) {
  39. switch (i) {
  40. case 'c':
  41. cmd = 4;
  42. break;
  43. case 'n':
  44. cmd = 8;
  45. level = bb_xgetlarg(optarg, 10, 0, 10);
  46. break;
  47. case 's':
  48. /* I think a 512k max kernel ring buffer is big enough for
  49. * anybody, as the default is 16k... Could be wrong though.
  50. * If so I'm sure I'll hear about it by the enraged masses*/
  51. bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024);
  52. break;
  53. default:
  54. bb_show_usage();
  55. }
  56. }
  57. if (optind < argc) {
  58. bb_show_usage();
  59. }
  60. if (cmd == 8) {
  61. if (klogctl(cmd, NULL, level) < 0)
  62. goto die_the_death;
  63. goto all_done;
  64. }
  65. buf = xmalloc(bufsize);
  66. if ((n = klogctl(cmd, buf, bufsize)) < 0)
  67. goto die_the_death;
  68. lastc = '\n';
  69. for (i = 0; i < n; i++) {
  70. if (lastc == '\n' && buf[i] == '<') {
  71. i++;
  72. while (buf[i] >= '0' && buf[i] <= '9')
  73. i++;
  74. if (buf[i] == '>')
  75. i++;
  76. }
  77. lastc = buf[i];
  78. putchar(lastc);
  79. }
  80. if (lastc != '\n')
  81. putchar('\n');
  82. all_done:
  83. #ifdef CONFIG_FEATURE_CLEAN_UP
  84. if (buf) {
  85. free(buf);
  86. }
  87. #endif
  88. return EXIT_SUCCESS;
  89. die_the_death:
  90. bb_perror_nomsg_and_die();
  91. }