klogd.c 6.8 KB

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