klogd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <stdio.h>
  20. #include <stdlib.h>
  21. #include <signal.h> /* for our signal() handlers */
  22. #include <string.h> /* strncpy() */
  23. #include <errno.h> /* errno and friends */
  24. #include <unistd.h>
  25. #include <ctype.h>
  26. #include <sys/syslog.h>
  27. #include <sys/klog.h>
  28. #include "busybox.h"
  29. static void klogd_signal(int sig)
  30. {
  31. klogctl(7, NULL, 0);
  32. klogctl(0, 0, 0);
  33. /* logMessage(0, "Kernel log daemon exiting."); */
  34. syslog(LOG_NOTICE, "Kernel log daemon exiting.");
  35. exit(EXIT_SUCCESS);
  36. }
  37. static void doKlogd(const int console_log_level) ATTRIBUTE_NORETURN;
  38. static void doKlogd(const int console_log_level)
  39. {
  40. int priority = LOG_INFO;
  41. char log_buffer[4096];
  42. int i, n, lastc;
  43. char *start;
  44. openlog("kernel", 0, LOG_KERN);
  45. /* Set up sig handlers */
  46. signal(SIGINT, klogd_signal);
  47. signal(SIGKILL, klogd_signal);
  48. signal(SIGTERM, klogd_signal);
  49. signal(SIGHUP, SIG_IGN);
  50. /* "Open the log. Currently a NOP." */
  51. klogctl(1, NULL, 0);
  52. /* Set level of kernel console messaging.. */
  53. if (console_log_level != -1)
  54. klogctl(8, NULL, console_log_level);
  55. syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
  56. while (1) {
  57. /* Use kernel syscalls */
  58. memset(log_buffer, '\0', sizeof(log_buffer));
  59. n = klogctl(2, log_buffer, sizeof(log_buffer));
  60. if (n < 0) {
  61. if (errno == EINTR)
  62. continue;
  63. syslog(LOG_ERR, "klogd: Error return from sys_sycall: %d - %m.\n", errno);
  64. exit(EXIT_FAILURE);
  65. }
  66. /* klogctl buffer parsing modelled after code in dmesg.c */
  67. start = &log_buffer[0];
  68. lastc = '\0';
  69. for (i = 0; i < n; i++) {
  70. if (lastc == '\0' && log_buffer[i] == '<') {
  71. priority = 0;
  72. i++;
  73. while (isdigit(log_buffer[i])) {
  74. priority = priority * 10 + (log_buffer[i] - '0');
  75. i++;
  76. }
  77. if (log_buffer[i] == '>')
  78. i++;
  79. start = &log_buffer[i];
  80. }
  81. if (log_buffer[i] == '\n') {
  82. log_buffer[i] = '\0'; /* zero terminate this message */
  83. syslog(priority, "%s", start);
  84. start = &log_buffer[i + 1];
  85. priority = LOG_INFO;
  86. }
  87. lastc = log_buffer[i];
  88. }
  89. }
  90. }
  91. #define OPT_LEVEL 1
  92. #define OPT_FOREGROUND 2
  93. int klogd_main(int argc, char **argv)
  94. {
  95. unsigned long opt;
  96. char *c_arg;
  97. int console_log_level = -1;
  98. /* do normal option parsing */
  99. opt = bb_getopt_ulflags (argc, argv, "c:n", &c_arg);
  100. if (opt & OPT_LEVEL) {
  101. /* Valid levels are between 1 and 8 */
  102. console_log_level = bb_xgetlarg(c_arg, 10, 1, 8);
  103. }
  104. if (!(opt & OPT_FOREGROUND)) {
  105. #if defined(__uClinux__)
  106. vfork_daemon_rexec(0, 1, argc, argv, "-n");
  107. #else /* __uClinux__ */
  108. if (daemon(0, 1) < 0)
  109. bb_perror_msg_and_die("daemon");
  110. #endif /* __uClinux__ */
  111. }
  112. doKlogd(console_log_level);
  113. return EXIT_SUCCESS;
  114. }
  115. /*
  116. Local Variables
  117. c-file-style: "linux"
  118. c-basic-offset: 4
  119. tab-width: 4
  120. End:
  121. */