3
0

klogd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: "Kernel logger\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. 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. # ifdef __GNU__
  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(CONFIG_PID_FILE_PATH "/klogd.pid");
  203. used = 0;
  204. while (!bb_got_signal) {
  205. int n;
  206. int priority;
  207. char *start;
  208. /* "2 -- Read from the log." */
  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. priority = strtoul(start, &start, 10);
  243. if (*start == '>')
  244. start++;
  245. }
  246. /* Log (only non-empty lines) */
  247. if (*start)
  248. syslog(priority, "%s", start);
  249. if (!newline)
  250. break;
  251. start = newline;
  252. }
  253. }
  254. klogd_close();
  255. syslog(LOG_NOTICE, "klogd: exiting");
  256. remove_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
  257. if (bb_got_signal)
  258. kill_myself_with_sig(bb_got_signal);
  259. return EXIT_FAILURE;
  260. }