klogd.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini klogd implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Gennady Feldman <gfeldman@cachier.com>.
  6. * Changes: Made this a standalone busybox module which uses standalone
  7. * syslog() client interface.
  8. *
  9. * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
  10. * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
  11. *
  12. * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
  13. *
  14. * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@mail.com>
  15. *
  16. * Maintainer: Gennady Feldman <gena01@cachier.com> as of Mar 12, 2001
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  31. *
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <signal.h> /* for our signal() handlers */
  36. #include <string.h> /* strncpy() */
  37. #include <errno.h> /* errno and friends */
  38. #include <unistd.h>
  39. #include <ctype.h>
  40. #include <sys/syslog.h>
  41. #if __GNU_LIBRARY__ < 5
  42. # ifdef __alpha__
  43. # define klogctl syslog
  44. # endif
  45. #else
  46. # include <sys/klog.h>
  47. #endif
  48. #include "busybox.h"
  49. static void klogd_signal(int sig)
  50. {
  51. klogctl(7, NULL, 0);
  52. klogctl(0, 0, 0);
  53. //logMessage(0, "Kernel log daemon exiting.");
  54. syslog_msg(LOG_SYSLOG, LOG_NOTICE, "Kernel log daemon exiting.");
  55. exit(TRUE);
  56. }
  57. static void doKlogd (void) __attribute__ ((noreturn));
  58. static void doKlogd (void)
  59. {
  60. int priority = LOG_INFO;
  61. char log_buffer[4096];
  62. int i, n, lastc;
  63. char *start;
  64. /* Set up sig handlers */
  65. signal(SIGINT, klogd_signal);
  66. signal(SIGKILL, klogd_signal);
  67. signal(SIGTERM, klogd_signal);
  68. signal(SIGHUP, SIG_IGN);
  69. /* "Open the log. Currently a NOP." */
  70. klogctl(1, NULL, 0);
  71. syslog_msg(LOG_SYSLOG, LOG_NOTICE, "klogd started: " BB_BANNER);
  72. while (1) {
  73. /* Use kernel syscalls */
  74. memset(log_buffer, '\0', sizeof(log_buffer));
  75. n = klogctl(2, log_buffer, sizeof(log_buffer));
  76. if (n < 0) {
  77. char message[80];
  78. if (errno == EINTR)
  79. continue;
  80. snprintf(message, 79, "klogd: Error return from sys_sycall: %d - %s.\n",
  81. errno, strerror(errno));
  82. syslog_msg(LOG_SYSLOG, LOG_ERR, message);
  83. exit(1);
  84. }
  85. /* klogctl buffer parsing modelled after code in dmesg.c */
  86. start=&log_buffer[0];
  87. lastc='\0';
  88. for (i=0; i<n; i++) {
  89. if (lastc == '\0' && log_buffer[i] == '<') {
  90. priority = 0;
  91. i++;
  92. while (isdigit(log_buffer[i])) {
  93. priority = priority*10+(log_buffer[i]-'0');
  94. i++;
  95. }
  96. if (log_buffer[i] == '>') i++;
  97. start = &log_buffer[i];
  98. }
  99. if (log_buffer[i] == '\n') {
  100. log_buffer[i] = '\0'; /* zero terminate this message */
  101. syslog_msg(LOG_KERN, priority, start);
  102. start = &log_buffer[i+1];
  103. priority = LOG_INFO;
  104. }
  105. lastc = log_buffer[i];
  106. }
  107. }
  108. }
  109. extern int klogd_main(int argc, char **argv)
  110. {
  111. /* no options, no getopt */
  112. int opt;
  113. #ifndef __uClinux__ /* fork() not available in uClinux */
  114. int doFork = TRUE;
  115. #endif /* __uClinux__ */
  116. /* do normal option parsing */
  117. while ((opt = getopt(argc, argv, "n")) > 0) {
  118. switch (opt) {
  119. case 'n':
  120. #ifndef __uClinux__ /* fork() not available in uClinux */
  121. doFork = FALSE;
  122. #endif /* __uClinux__ */
  123. break;
  124. default:
  125. show_usage();
  126. }
  127. }
  128. #ifndef __uClinux__ /* fork() not available in uClinux */
  129. if (doFork == TRUE) {
  130. if (daemon(0, 1) < 0)
  131. perror_msg_and_die("daemon");
  132. }
  133. #endif /* __uClinux__ */
  134. doKlogd();
  135. return EXIT_SUCCESS;
  136. }
  137. /*
  138. Local Variables
  139. c-file-style: "linux"
  140. c-basic-offset: 4
  141. tab-width: 4
  142. End:
  143. */