3
0

klogd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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)
  23. {
  24. /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
  25. * via klogctl(8, NULL, 7). */
  26. klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
  27. klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
  28. syslog(LOG_NOTICE, "klogd: exiting");
  29. kill_myself_with_sig(sig);
  30. }
  31. #define log_buffer bb_common_bufsiz1
  32. enum {
  33. KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
  34. OPT_LEVEL = (1 << 0),
  35. OPT_FOREGROUND = (1 << 1),
  36. };
  37. int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int klogd_main(int argc ATTRIBUTE_UNUSED, char **argv)
  39. {
  40. int i = 0;
  41. char *start;
  42. int opt;
  43. opt = getopt32(argv, "c:n", &start);
  44. if (opt & OPT_LEVEL) {
  45. /* Valid levels are between 1 and 8 */
  46. i = xatou_range(start, 1, 8);
  47. }
  48. if (!(opt & OPT_FOREGROUND)) {
  49. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  50. }
  51. openlog("kernel", 0, LOG_KERN);
  52. bb_signals(0
  53. + (1 << SIGINT)
  54. + (1 << SIGTERM)
  55. , klogd_signal);
  56. signal(SIGHUP, SIG_IGN);
  57. /* "Open the log. Currently a NOP" */
  58. klogctl(1, NULL, 0);
  59. /* "printk() prints a message on the console only if it has a loglevel
  60. * less than console_loglevel". Here we set console_loglevel = i. */
  61. if (i)
  62. klogctl(8, NULL, i);
  63. syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
  64. /* Note: this code does not detect incomplete messages
  65. * (messages not ending with '\n' or just when kernel
  66. * generates too many messages for us to keep up)
  67. * and will split them in two separate lines */
  68. while (1) {
  69. int n;
  70. int priority;
  71. /* "2 -- Read from the log." */
  72. n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
  73. if (n < 0) {
  74. if (errno == EINTR)
  75. continue;
  76. syslog(LOG_ERR, "klogd: error %d in klogctl(2): %m",
  77. errno);
  78. break;
  79. }
  80. log_buffer[n] = '\n';
  81. i = 0;
  82. while (i < n) {
  83. priority = LOG_INFO;
  84. start = &log_buffer[i];
  85. if (log_buffer[i] == '<') {
  86. i++;
  87. // kernel never ganerates multi-digit prios
  88. //priority = 0;
  89. //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
  90. // priority = priority * 10 + (log_buffer[i] - '0');
  91. // i++;
  92. //}
  93. if (isdigit(log_buffer[i])) {
  94. priority = (log_buffer[i] - '0');
  95. i++;
  96. }
  97. if (log_buffer[i] == '>')
  98. i++;
  99. start = &log_buffer[i];
  100. }
  101. while (log_buffer[i] != '\n')
  102. i++;
  103. log_buffer[i] = '\0';
  104. syslog(priority, "%s", start);
  105. i++;
  106. }
  107. }
  108. return EXIT_FAILURE;
  109. }