klogd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. //config:config KLOGD
  20. //config: bool "klogd (5.7 kb)"
  21. //config: default y
  22. //config: help
  23. //config: klogd is a utility which intercepts and logs all
  24. //config: messages from the Linux kernel and sends the messages
  25. //config: out to the 'syslogd' utility so they can be logged. If
  26. //config: you wish to record the messages produced by the kernel,
  27. //config: you should enable this option.
  28. //config:
  29. //config:comment "klogd should not be used together with syslog to kernel printk buffer"
  30. //config: depends on KLOGD && FEATURE_KMSG_SYSLOG
  31. //config:
  32. //config:config FEATURE_KLOGD_KLOGCTL
  33. //config: bool "Use the klogctl() interface"
  34. //config: default y
  35. //config: depends on KLOGD
  36. //config: help
  37. //config: The klogd applet supports two interfaces for reading
  38. //config: kernel messages. Linux provides the klogctl() interface
  39. //config: which allows reading messages from the kernel ring buffer
  40. //config: independently from the file system.
  41. //config:
  42. //config: If you answer 'N' here, klogd will use the more portable
  43. //config: approach of reading them from /proc or a device node.
  44. //config: However, this method requires the file to be available.
  45. //config:
  46. //config: If in doubt, say 'Y'.
  47. //applet:IF_KLOGD(APPLET(klogd, BB_DIR_SBIN, BB_SUID_DROP))
  48. //kbuild:lib-$(CONFIG_KLOGD) += klogd.o
  49. //usage:#define klogd_trivial_usage
  50. //usage: "[-c N] [-n]"
  51. //usage:#define klogd_full_usage "\n\n"
  52. //usage: "Log kernel messages to syslog\n"
  53. //usage: "\n -c N Print to console messages more urgent than prio N (1-8)"
  54. //usage: "\n -n Run in foreground"
  55. #include "libbb.h"
  56. #include "common_bufsiz.h"
  57. #include <syslog.h>
  58. /* The Linux-specific klogctl(3) interface does not rely on the filesystem and
  59. * allows us to change the console loglevel. Alternatively, we read the
  60. * messages from _PATH_KLOG. */
  61. #if ENABLE_FEATURE_KLOGD_KLOGCTL
  62. # include <sys/klog.h>
  63. static void klogd_open(void)
  64. {
  65. /* "Open the log. Currently a NOP" */
  66. klogctl(1, NULL, 0);
  67. }
  68. static void klogd_setloglevel(int lvl)
  69. {
  70. /* "printk() prints a message on the console only if it has a loglevel
  71. * less than console_loglevel". Here we set console_loglevel = lvl. */
  72. klogctl(8, NULL, lvl);
  73. }
  74. static int klogd_read(char *bufp, int len)
  75. {
  76. /* "2 -- Read from the log." */
  77. return klogctl(2, bufp, len);
  78. }
  79. # define READ_ERROR "klogctl(2) error"
  80. static void klogd_close(void)
  81. {
  82. /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
  83. * via klogctl(8, NULL, 7). */
  84. klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
  85. klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
  86. }
  87. #else
  88. # ifndef _PATH_KLOG
  89. # if defined(__GNU__) || defined (__FreeBSD__)
  90. # define _PATH_KLOG "/dev/klog"
  91. # else
  92. # error "your system's _PATH_KLOG is unknown"
  93. # endif
  94. # endif
  95. # define PATH_PRINTK "/proc/sys/kernel/printk"
  96. enum { klogfd = 3 };
  97. static void klogd_open(void)
  98. {
  99. int fd = xopen(_PATH_KLOG, O_RDONLY);
  100. xmove_fd(fd, klogfd);
  101. }
  102. static void klogd_setloglevel(int lvl)
  103. {
  104. FILE *fp = fopen_or_warn(PATH_PRINTK, "w");
  105. if (fp) {
  106. /* This changes only first value:
  107. * "messages with a higher priority than this
  108. * [that is, with numerically lower value]
  109. * will be printed to the console".
  110. * The other three values in this pseudo-file aren't changed.
  111. */
  112. fprintf(fp, "%u\n", lvl);
  113. fclose(fp);
  114. }
  115. }
  116. static int klogd_read(char *bufp, int len)
  117. {
  118. return read(klogfd, bufp, len);
  119. }
  120. # define READ_ERROR "read error"
  121. static void klogd_close(void)
  122. {
  123. klogd_setloglevel(7);
  124. if (ENABLE_FEATURE_CLEAN_UP)
  125. close(klogfd);
  126. }
  127. #endif
  128. #define log_buffer bb_common_bufsiz1
  129. enum {
  130. KLOGD_LOGBUF_SIZE = COMMON_BUFSIZE,
  131. OPT_LEVEL = (1 << 0),
  132. OPT_FOREGROUND = (1 << 1),
  133. };
  134. /* TODO: glibc openlog(LOG_KERN) reverts to LOG_USER instead,
  135. * because that's how they interpret word "default"
  136. * in the openlog() manpage:
  137. * LOG_USER (default)
  138. * generic user-level messages
  139. * and the fact that LOG_KERN is a constant 0.
  140. * glibc interprets it as "0 in openlog() call means 'use default'".
  141. * I think it means "if openlog wasn't called before syslog() is called,
  142. * use default".
  143. * Convincing glibc maintainers otherwise is, as usual, nearly impossible.
  144. * Should we open-code syslog() here to use correct facility?
  145. */
  146. int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  147. int klogd_main(int argc UNUSED_PARAM, char **argv)
  148. {
  149. int i = 0;
  150. char *opt_c;
  151. int opt;
  152. int used;
  153. setup_common_bufsiz();
  154. opt = getopt32(argv, "c:n", &opt_c);
  155. if (opt & OPT_LEVEL) {
  156. /* Valid levels are between 1 and 8 */
  157. i = xatou_range(opt_c, 1, 8);
  158. }
  159. if (!(opt & OPT_FOREGROUND)) {
  160. bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
  161. }
  162. logmode = LOGMODE_SYSLOG;
  163. /* klogd_open() before openlog(), since it might use fixed fd 3,
  164. * and openlog() also may use the same fd 3 if we swap them:
  165. */
  166. klogd_open();
  167. openlog("kernel", 0, LOG_KERN);
  168. /*
  169. * glibc problem: for some reason, glibc changes LOG_KERN to LOG_USER
  170. * above. The logic behind this is that standard
  171. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/syslog.html
  172. * says the following about openlog and syslog:
  173. * "LOG_USER
  174. * Messages generated by arbitrary processes.
  175. * This is the default facility identifier if none is specified."
  176. *
  177. * I believe glibc misinterpreted this text as "if openlog's
  178. * third parameter is 0 (=LOG_KERN), treat it as LOG_USER".
  179. * Whereas it was meant to say "if *syslog* is called with facility
  180. * 0 in its 1st parameter without prior call to openlog, then perform
  181. * implicit openlog(LOG_USER)".
  182. *
  183. * As a result of this, eh, feature, standard klogd was forced
  184. * to open-code its own openlog and syslog implementation (!).
  185. *
  186. * Note that prohibiting openlog(LOG_KERN) on libc level does not
  187. * add any security: any process can open a socket to "/dev/log"
  188. * and write a string "<0>Voila, a LOG_KERN + LOG_EMERG message"
  189. *
  190. * Google code search tells me there is no widespread use of
  191. * openlog("foo", 0, 0), thus fixing glibc won't break userspace.
  192. *
  193. * The bug against glibc was filed:
  194. * bugzilla.redhat.com/show_bug.cgi?id=547000
  195. */
  196. if (i)
  197. klogd_setloglevel(i);
  198. signal(SIGHUP, SIG_IGN);
  199. /* We want klogd_read to not be restarted, thus _norestart: */
  200. bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
  201. syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
  202. write_pidfile_std_path_and_ext("klogd");
  203. used = 0;
  204. while (!bb_got_signal) {
  205. int n;
  206. int priority;
  207. char *start;
  208. start = log_buffer + used;
  209. n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
  210. if (n < 0) {
  211. if (errno == EINTR)
  212. continue;
  213. bb_simple_perror_msg(READ_ERROR);
  214. break;
  215. }
  216. start[n] = '\0';
  217. /* Process each newline-terminated line in the buffer */
  218. start = log_buffer;
  219. while (1) {
  220. char *newline = strchrnul(start, '\n');
  221. if (*newline == '\0') {
  222. /* This line is incomplete */
  223. /* move it to the front of the buffer */
  224. overlapping_strcpy(log_buffer, start);
  225. used = newline - start;
  226. if (used < KLOGD_LOGBUF_SIZE-1) {
  227. /* buffer isn't full */
  228. break;
  229. }
  230. /* buffer is full, log it anyway */
  231. used = 0;
  232. newline = NULL;
  233. } else {
  234. *newline++ = '\0';
  235. }
  236. /* Extract the priority */
  237. priority = LOG_INFO;
  238. if (*start == '<') {
  239. start++;
  240. if (*start) {
  241. char *end;
  242. priority = strtoul(start, &end, 10);
  243. if (*end == '>')
  244. end++;
  245. start = end;
  246. }
  247. }
  248. /* Log (only non-empty lines) */
  249. if (*start)
  250. syslog(priority, "%s", start);
  251. if (!newline)
  252. break;
  253. start = newline;
  254. }
  255. }
  256. klogd_close();
  257. syslog(LOG_NOTICE, "klogd: exiting");
  258. remove_pidfile_std_path_and_ext("klogd");
  259. if (bb_got_signal)
  260. kill_myself_with_sig(bb_got_signal);
  261. return EXIT_FAILURE;
  262. }