3
0

klogd.c 2.9 KB

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