3
0

klogd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini klogd implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>.
  6. * Changes: Made this a standalone busybox module which uses standalone
  7. * syslog() client interface.
  8. *
  9. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  10. *
  11. * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
  12. *
  13. * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
  14. *
  15. * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
  16. *
  17. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  18. */
  19. #include "libbb.h"
  20. #include <sys/syslog.h>
  21. #include <sys/klog.h>
  22. static void klogd_signal(int sig ATTRIBUTE_UNUSED)
  23. {
  24. klogctl(7, NULL, 0);
  25. klogctl(0, 0, 0);
  26. syslog(LOG_NOTICE, "Kernel log daemon exiting");
  27. exit(EXIT_SUCCESS);
  28. }
  29. #define OPT_LEVEL 1
  30. #define OPT_FOREGROUND 2
  31. #define KLOGD_LOGBUF_SIZE BUFSIZ
  32. #define log_buffer bb_common_bufsiz1
  33. int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  34. int klogd_main(int argc, char **argv)
  35. {
  36. int i = i; /* silence gcc */
  37. char *start;
  38. /* do normal option parsing */
  39. getopt32(argv, "c:n", &start);
  40. if (option_mask32 & OPT_LEVEL) {
  41. /* Valid levels are between 1 and 8 */
  42. i = xatoul_range(start, 1, 8);
  43. }
  44. if (!(option_mask32 & OPT_FOREGROUND)) {
  45. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  46. }
  47. openlog("kernel", 0, LOG_KERN);
  48. /* Set up sig handlers */
  49. signal(SIGINT, klogd_signal);
  50. signal(SIGKILL, klogd_signal);
  51. signal(SIGTERM, klogd_signal);
  52. signal(SIGHUP, SIG_IGN);
  53. /* "Open the log. Currently a NOP." */
  54. klogctl(1, NULL, 0);
  55. /* Set level of kernel console messaging. */
  56. if (option_mask32 & OPT_LEVEL)
  57. klogctl(8, NULL, i);
  58. syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
  59. /* Note: this code does not detect incomplete messages
  60. * (messages not ending with '\n' or just when kernel
  61. * generates too many messages for us to keep up)
  62. * and will split them in two separate lines */
  63. while (1) {
  64. int n;
  65. int priority;
  66. n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
  67. if (n < 0) {
  68. if (errno == EINTR)
  69. continue;
  70. syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
  71. errno);
  72. break;
  73. }
  74. log_buffer[n] = '\n';
  75. i = 0;
  76. while (i < n) {
  77. priority = LOG_INFO;
  78. start = &log_buffer[i];
  79. if (log_buffer[i] == '<') {
  80. i++;
  81. // kernel never ganerates multi-digit prios
  82. //priority = 0;
  83. //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
  84. // priority = priority * 10 + (log_buffer[i] - '0');
  85. // i++;
  86. //}
  87. if (isdigit(log_buffer[i])) {
  88. priority = (log_buffer[i] - '0');
  89. i++;
  90. }
  91. if (log_buffer[i] == '>')
  92. i++;
  93. start = &log_buffer[i];
  94. }
  95. while (log_buffer[i] != '\n')
  96. i++;
  97. log_buffer[i] = '\0';
  98. syslog(priority, "%s", start);
  99. i++;
  100. }
  101. }
  102. return EXIT_FAILURE;
  103. }