klogd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "busybox.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 4096
  32. int klogd_main(int argc, char **argv)
  33. {
  34. RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
  35. int i = i; /* silence gcc */
  36. char *start;
  37. /* do normal option parsing */
  38. getopt32(argc, argv, "c:n", &start);
  39. if (option_mask32 & OPT_LEVEL) {
  40. /* Valid levels are between 1 and 8 */
  41. i = xatoul_range(start, 1, 8);
  42. }
  43. if (!(option_mask32 & OPT_FOREGROUND)) {
  44. #ifdef BB_NOMMU
  45. vfork_daemon_rexec(0, 1, argc, argv, "-n");
  46. #else
  47. xdaemon(0, 1);
  48. #endif
  49. }
  50. openlog("kernel", 0, LOG_KERN);
  51. /* Set up sig handlers */
  52. signal(SIGINT, klogd_signal);
  53. signal(SIGKILL, klogd_signal);
  54. signal(SIGTERM, klogd_signal);
  55. signal(SIGHUP, SIG_IGN);
  56. /* "Open the log. Currently a NOP." */
  57. klogctl(1, NULL, 0);
  58. /* Set level of kernel console messaging.. */
  59. if (option_mask32 & OPT_LEVEL)
  60. klogctl(8, NULL, i);
  61. syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
  62. while (1) {
  63. int n;
  64. int priority;
  65. char lastc;
  66. /* Use kernel syscalls */
  67. memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
  68. /* It will be null-terminted */
  69. n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
  70. if (n < 0) {
  71. if (errno == EINTR)
  72. continue;
  73. syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
  74. errno);
  75. break;
  76. }
  77. /* klogctl buffer parsing modelled after code in dmesg.c */
  78. start = &log_buffer[0];
  79. lastc = '\0';
  80. priority = LOG_INFO;
  81. for (i = 0; i < n; i++) {
  82. if (lastc == '\0' && log_buffer[i] == '<') {
  83. i++;
  84. // kernel never ganerates multi-digit prios
  85. //priority = 0;
  86. //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
  87. // priority = priority * 10 + (log_buffer[i] - '0');
  88. // i++;
  89. //}
  90. if (isdigit(log_buffer[i])) {
  91. priority = (log_buffer[i] - '0');
  92. i++;
  93. }
  94. if (log_buffer[i] == '>')
  95. i++;
  96. start = &log_buffer[i];
  97. }
  98. if (log_buffer[i] == '\n') {
  99. log_buffer[i] = '\0'; /* zero terminate this message */
  100. syslog(priority, "%s", start);
  101. start = &log_buffer[i + 1];
  102. priority = LOG_INFO;
  103. }
  104. lastc = log_buffer[i];
  105. }
  106. }
  107. if (ENABLE_FEATURE_CLEAN_UP)
  108. RELEASE_CONFIG_BUFFER(log_buffer);
  109. return EXIT_FAILURE;
  110. }