klogd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /* The Linux-specific klogctl(3) interface does not rely on the filesystem and
  22. * allows us to change the console loglevel. Alternatively, we read the
  23. * messages from _PATH_KLOG. */
  24. #if ENABLE_FEATURE_KLOGD_KLOGCTL
  25. # include <sys/klog.h>
  26. static void klogd_open(void)
  27. {
  28. /* "Open the log. Currently a NOP" */
  29. klogctl(1, NULL, 0);
  30. }
  31. static void klogd_setloglevel(int lvl)
  32. {
  33. /* "printk() prints a message on the console only if it has a loglevel
  34. * less than console_loglevel". Here we set console_loglevel = lvl. */
  35. klogctl(8, NULL, lvl);
  36. }
  37. static int klogd_read(char *bufp, int len)
  38. {
  39. return klogctl(2, bufp, len);
  40. }
  41. # define READ_ERROR "klogctl(2) error"
  42. static void klogd_close(void)
  43. {
  44. /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
  45. * via klogctl(8, NULL, 7). */
  46. klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
  47. klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
  48. }
  49. #else
  50. # include <paths.h>
  51. # ifndef _PATH_KLOG
  52. # ifdef __GNU__
  53. # define _PATH_KLOG "/dev/klog"
  54. # else
  55. # error "your system's _PATH_KLOG is unknown"
  56. # endif
  57. # endif
  58. # define PATH_PRINTK "/proc/sys/kernel/printk"
  59. enum { klogfd = 3 };
  60. static void klogd_open(void)
  61. {
  62. int fd = xopen(_PATH_KLOG, O_RDONLY);
  63. xmove_fd(fd, klogfd);
  64. }
  65. static void klogd_setloglevel(int lvl)
  66. {
  67. FILE *fp = fopen_or_warn(PATH_PRINTK, "w");
  68. if (fp) {
  69. /* This changes only first value:
  70. * "messages with a higher priority than this
  71. * [that is, with numerically lower value]
  72. * will be printed to the console".
  73. * The other three values in this pseudo-file aren't changed.
  74. */
  75. fprintf(fp, "%u\n", lvl);
  76. fclose(fp);
  77. }
  78. }
  79. static int klogd_read(char *bufp, int len)
  80. {
  81. return read(klogfd, bufp, len);
  82. }
  83. # define READ_ERROR "read error"
  84. static void klogd_close(void)
  85. {
  86. klogd_setloglevel(7);
  87. if (ENABLE_FEATURE_CLEAN_UP)
  88. close(klogfd);
  89. }
  90. #endif
  91. #define log_buffer bb_common_bufsiz1
  92. enum {
  93. KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
  94. OPT_LEVEL = (1 << 0),
  95. OPT_FOREGROUND = (1 << 1),
  96. };
  97. /* TODO: glibc openlog(LOG_KERN) reverts to LOG_USER instead,
  98. * because that's how they interpret word "default"
  99. * in the openlog() manpage:
  100. * LOG_USER (default)
  101. * generic user-level messages
  102. * and the fact that LOG_KERN is a constant 0.
  103. * glibc interprets it as "0 in openlog() call means 'use default'".
  104. * I think it means "if openlog wasn't called before syslog() is called,
  105. * use default".
  106. * Convincing glibc maintainers otherwise is, as usual, nearly impossible.
  107. * Should we open-code syslog() here to use correct facility?
  108. */
  109. int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  110. int klogd_main(int argc UNUSED_PARAM, char **argv)
  111. {
  112. int i = 0;
  113. char *opt_c;
  114. int opt;
  115. int used = 0;
  116. opt = getopt32(argv, "c:n", &opt_c);
  117. if (opt & OPT_LEVEL) {
  118. /* Valid levels are between 1 and 8 */
  119. i = xatou_range(opt_c, 1, 8);
  120. }
  121. if (!(opt & OPT_FOREGROUND)) {
  122. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  123. }
  124. logmode = LOGMODE_SYSLOG;
  125. /* klogd_open() before openlog(), since it might use fixed fd 3,
  126. * and openlog() also may use the same fd 3 if we swap them:
  127. */
  128. klogd_open();
  129. openlog("kernel", 0, LOG_KERN);
  130. if (i)
  131. klogd_setloglevel(i);
  132. bb_signals(BB_FATAL_SIGS, record_signo);
  133. signal(SIGHUP, SIG_IGN);
  134. syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
  135. while (!bb_got_signal) {
  136. int n;
  137. int priority;
  138. char *start;
  139. /* "2 -- Read from the log." */
  140. start = log_buffer + used;
  141. n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
  142. if (n < 0) {
  143. if (errno == EINTR)
  144. continue;
  145. bb_perror_msg(READ_ERROR);
  146. break;
  147. }
  148. start[n] = '\0';
  149. /* klogctl buffer parsing modelled after code in dmesg.c */
  150. /* Process each newline-terminated line in the buffer */
  151. start = log_buffer;
  152. while (1) {
  153. char *newline = strchrnul(start, '\n');
  154. if (*newline == '\0') {
  155. /* This line is incomplete... */
  156. if (start != log_buffer) {
  157. /* move it to the front of the buffer */
  158. overlapping_strcpy(log_buffer, start);
  159. used = newline - start;
  160. /* don't log it yet */
  161. break;
  162. }
  163. /* ...but if buffer is full, log it anyway */
  164. used = 0;
  165. newline = NULL;
  166. } else {
  167. *newline++ = '\0';
  168. }
  169. /* Extract the priority */
  170. priority = LOG_INFO;
  171. if (*start == '<') {
  172. start++;
  173. if (*start) {
  174. /* kernel never generates multi-digit prios */
  175. priority = (*start - '0');
  176. start++;
  177. }
  178. if (*start == '>')
  179. start++;
  180. }
  181. /* Log (only non-empty lines) */
  182. if (*start)
  183. syslog(priority, "%s", start);
  184. if (!newline)
  185. break;
  186. start = newline;
  187. }
  188. }
  189. klogd_close();
  190. syslog(LOG_NOTICE, "klogd: exiting");
  191. if (bb_got_signal)
  192. kill_myself_with_sig(bb_got_signal);
  193. return EXIT_FAILURE;
  194. }