klogd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <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, NULL, 0);
  26. syslog(LOG_NOTICE, "klogd: exiting");
  27. kill_myself_with_sig(sig);
  28. }
  29. #define log_buffer bb_common_bufsiz1
  30. enum {
  31. KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
  32. OPT_LEVEL = (1 << 0),
  33. OPT_FOREGROUND = (1 << 1),
  34. };
  35. int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  36. int klogd_main(int argc ATTRIBUTE_UNUSED, char **argv)
  37. {
  38. int i = i; /* silence gcc */
  39. char *start;
  40. /* do normal option parsing */
  41. getopt32(argv, "c:n", &start);
  42. if (option_mask32 & OPT_LEVEL) {
  43. /* Valid levels are between 1 and 8 */
  44. i = xatoul_range(start, 1, 8);
  45. }
  46. if (!(option_mask32 & OPT_FOREGROUND)) {
  47. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  48. }
  49. openlog("kernel", 0, LOG_KERN);
  50. /* Set up sig handlers */
  51. bb_signals(0
  52. + (1 << SIGINT)
  53. + (1 << SIGTERM)
  54. , 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. /* Note: this code does not detect incomplete messages
  63. * (messages not ending with '\n' or just when kernel
  64. * generates too many messages for us to keep up)
  65. * and will split them in two separate lines */
  66. while (1) {
  67. int n;
  68. int priority;
  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. log_buffer[n] = '\n';
  78. i = 0;
  79. while (i < n) {
  80. priority = LOG_INFO;
  81. start = &log_buffer[i];
  82. if (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. while (log_buffer[i] != '\n')
  99. i++;
  100. log_buffer[i] = '\0';
  101. syslog(priority, "%s", start);
  102. i++;
  103. }
  104. }
  105. return EXIT_FAILURE;
  106. }