klogd.c 8.2 KB

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