klogd.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 GPLv2 or later, see file LICENSE in this source tree.
  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;
  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. /*
  131. * glibc problem: for some reason, glibc changes LOG_KERN to LOG_USER
  132. * above. The logic behind this is that standard
  133. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/syslog.html
  134. * says the following about openlog and syslog:
  135. * "LOG_USER
  136. * Messages generated by arbitrary processes.
  137. * This is the default facility identifier if none is specified."
  138. *
  139. * I believe glibc misinterpreted this text as "if openlog's
  140. * third parameter is 0 (=LOG_KERN), treat it as LOG_USER".
  141. * Whereas it was meant to say "if *syslog* is called with facility
  142. * 0 in its 1st parameter without prior call to openlog, then perform
  143. * implicit openlog(LOG_USER)".
  144. *
  145. * As a result of this, eh, feature, standard klogd was forced
  146. * to open-code its own openlog and syslog implementation (!).
  147. *
  148. * Note that prohibiting openlog(LOG_KERN) on libc level does not
  149. * add any security: any process can open a socket to "/dev/log"
  150. * and write a string "<0>Voila, a LOG_KERN + LOG_EMERG message"
  151. *
  152. * Google code search tells me there is no widespread use of
  153. * openlog("foo", 0, 0), thus fixing glibc won't break userspace.
  154. *
  155. * The bug against glibc was filed:
  156. * bugzilla.redhat.com/show_bug.cgi?id=547000
  157. */
  158. if (i)
  159. klogd_setloglevel(i);
  160. signal(SIGHUP, SIG_IGN);
  161. /* We want klogd_read to not be restarted, thus _norestart: */
  162. bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
  163. syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
  164. used = 0;
  165. while (!bb_got_signal) {
  166. int n;
  167. int priority;
  168. char *start;
  169. /* "2 -- Read from the log." */
  170. start = log_buffer + used;
  171. n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
  172. if (n < 0) {
  173. if (errno == EINTR)
  174. continue;
  175. bb_perror_msg(READ_ERROR);
  176. break;
  177. }
  178. start[n] = '\0';
  179. /* Process each newline-terminated line in the buffer */
  180. start = log_buffer;
  181. while (1) {
  182. char *newline = strchrnul(start, '\n');
  183. if (*newline == '\0') {
  184. /* This line is incomplete */
  185. /* move it to the front of the buffer */
  186. overlapping_strcpy(log_buffer, start);
  187. used = newline - start;
  188. if (used < KLOGD_LOGBUF_SIZE-1) {
  189. /* buffer isn't full */
  190. break;
  191. }
  192. /* buffer is full, log it anyway */
  193. used = 0;
  194. newline = NULL;
  195. } else {
  196. *newline++ = '\0';
  197. }
  198. /* Extract the priority */
  199. priority = LOG_INFO;
  200. if (*start == '<') {
  201. start++;
  202. if (*start) {
  203. /* kernel never generates multi-digit prios */
  204. priority = (*start - '0');
  205. start++;
  206. }
  207. if (*start == '>')
  208. start++;
  209. }
  210. /* Log (only non-empty lines) */
  211. if (*start)
  212. syslog(priority, "%s", start);
  213. if (!newline)
  214. break;
  215. start = newline;
  216. }
  217. }
  218. klogd_close();
  219. syslog(LOG_NOTICE, "klogd: exiting");
  220. if (bb_got_signal)
  221. kill_myself_with_sig(bb_got_signal);
  222. return EXIT_FAILURE;
  223. }