klogd.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 UNUSED_PARAM, char **argv)
  39. {
  40. int i = 0;
  41. char *opt_c;
  42. int opt;
  43. int used = 0;
  44. opt = getopt32(argv, "c:n", &opt_c);
  45. if (opt & OPT_LEVEL) {
  46. /* Valid levels are between 1 and 8 */
  47. i = xatou_range(opt_c, 1, 8);
  48. }
  49. if (!(opt & OPT_FOREGROUND)) {
  50. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  51. }
  52. openlog("kernel", 0, LOG_KERN);
  53. bb_signals(BB_FATAL_SIGS, klogd_signal);
  54. signal(SIGHUP, SIG_IGN);
  55. /* "Open the log. Currently a NOP" */
  56. klogctl(1, NULL, 0);
  57. /* "printk() prints a message on the console only if it has a loglevel
  58. * less than console_loglevel". Here we set console_loglevel = i. */
  59. if (i)
  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 *start;
  66. /* "2 -- Read from the log." */
  67. start = log_buffer + used;
  68. n = klogctl(2, start, KLOGD_LOGBUF_SIZE-1 - used);
  69. if (n < 0) {
  70. if (errno == EINTR)
  71. continue;
  72. syslog(LOG_ERR, "klogd: error %d in klogctl(2): %m",
  73. errno);
  74. break;
  75. }
  76. start[n] = '\0';
  77. /* klogctl buffer parsing modelled after code in dmesg.c */
  78. /* Process each newline-terminated line in the buffer */
  79. start = log_buffer;
  80. while (1) {
  81. char *newline = strchrnul(start, '\n');
  82. if (*newline == '\0') {
  83. /* This line is incomplete... */
  84. if (start != log_buffer) {
  85. /* move it to the front of the buffer */
  86. overlapping_strcpy(log_buffer, start);
  87. used = newline - start;
  88. /* don't log it yet */
  89. break;
  90. }
  91. /* ...but if buffer is full, log it anyway */
  92. used = 0;
  93. newline = NULL;
  94. } else {
  95. *newline++ = '\0';
  96. }
  97. /* Extract the priority */
  98. priority = LOG_INFO;
  99. if (*start == '<') {
  100. start++;
  101. if (*start) {
  102. /* kernel never generates multi-digit prios */
  103. priority = (*start - '0');
  104. start++;
  105. }
  106. if (*start == '>')
  107. start++;
  108. }
  109. /* Log (only non-empty lines) */
  110. if (*start)
  111. syslog(priority, "%s", start);
  112. if (!newline)
  113. break;
  114. start = newline;
  115. }
  116. }
  117. return EXIT_FAILURE;
  118. }