klogd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <signal.h> /* for our signal() handlers */
  35. #include <string.h> /* strncpy() */
  36. #include <errno.h> /* errno and friends */
  37. #include <unistd.h>
  38. #include <ctype.h>
  39. #include <sys/syslog.h>
  40. #include <sys/klog.h>
  41. #include "busybox.h"
  42. static void klogd_signal(int sig)
  43. {
  44. klogctl(7, NULL, 0);
  45. klogctl(0, 0, 0);
  46. /* logMessage(0, "Kernel log daemon exiting."); */
  47. syslog(LOG_NOTICE, "Kernel log daemon exiting.");
  48. exit(EXIT_SUCCESS);
  49. }
  50. static void doKlogd(const int console_log_level) __attribute__ ((noreturn));
  51. static void doKlogd(const int console_log_level)
  52. {
  53. int priority = LOG_INFO;
  54. char log_buffer[4096];
  55. int i, n, lastc;
  56. char *start;
  57. openlog("kernel", 0, LOG_KERN);
  58. /* Set up sig handlers */
  59. signal(SIGINT, klogd_signal);
  60. signal(SIGKILL, klogd_signal);
  61. signal(SIGTERM, klogd_signal);
  62. signal(SIGHUP, SIG_IGN);
  63. /* "Open the log. Currently a NOP." */
  64. klogctl(1, NULL, 0);
  65. /* Set level of kernel console messaging.. */
  66. if (console_log_level != -1)
  67. klogctl(8, NULL, console_log_level);
  68. syslog(LOG_NOTICE, "klogd started: " BB_BANNER);
  69. while (1) {
  70. /* Use kernel syscalls */
  71. memset(log_buffer, '\0', sizeof(log_buffer));
  72. n = klogctl(2, log_buffer, sizeof(log_buffer));
  73. if (n < 0) {
  74. if (errno == EINTR)
  75. continue;
  76. syslog(LOG_ERR, "klogd: Error return from sys_sycall: %d - %m.\n", 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 (isdigit(log_buffer[i])) {
  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. }
  104. extern int klogd_main(int argc, char **argv)
  105. {
  106. /* no options, no getopt */
  107. int opt;
  108. int doFork = TRUE;
  109. unsigned char console_log_level = -1;
  110. /* do normal option parsing */
  111. while ((opt = getopt(argc, argv, "c:n")) > 0) {
  112. switch (opt) {
  113. case 'c':
  114. if ((optarg == NULL) || (optarg[1] != '\0')) {
  115. bb_show_usage();
  116. }
  117. /* Valid levels are between 1 and 8 */
  118. console_log_level = *optarg - '1';
  119. if (console_log_level > 7) {
  120. bb_show_usage();
  121. }
  122. console_log_level++;
  123. break;
  124. case 'n':
  125. doFork = FALSE;
  126. break;
  127. default:
  128. bb_show_usage();
  129. }
  130. }
  131. if (doFork) {
  132. #if defined(__uClinux__)
  133. vfork_daemon_rexec(0, 1, argc, argv, "-n");
  134. #else /* __uClinux__ */
  135. if (daemon(0, 1) < 0)
  136. bb_perror_msg_and_die("daemon");
  137. #endif /* __uClinux__ */
  138. }
  139. doKlogd(console_log_level);
  140. return EXIT_SUCCESS;
  141. }
  142. /*
  143. Local Variables
  144. c-file-style: "linux"
  145. c-basic-offset: 4
  146. tab-width: 4
  147. End:
  148. */