1 /* vi: set sw=4 ts=4: */
3 * Mini klogd implementation for busybox
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.
9 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
11 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
13 * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
15 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
17 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
20 //config: bool "klogd (5.5 kb)"
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.
29 //config:comment "klogd should not be used together with syslog to kernel printk buffer"
30 //config: depends on KLOGD && FEATURE_KMSG_SYSLOG
32 //config:config FEATURE_KLOGD_KLOGCTL
33 //config: bool "Use the klogctl() interface"
35 //config: depends on KLOGD
36 //config: select PLATFORM_LINUX
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.
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.
47 //config: If in doubt, say 'Y'.
49 //applet:IF_KLOGD(APPLET(klogd, BB_DIR_SBIN, BB_SUID_DROP))
51 //kbuild:lib-$(CONFIG_KLOGD) += klogd.o
53 //usage:#define klogd_trivial_usage
54 //usage: "[-c N] [-n]"
55 //usage:#define klogd_full_usage "\n\n"
56 //usage: "Log kernel messages to syslog\n"
57 //usage: "\n -c N Print to console messages more urgent than prio N (1-8)"
58 //usage: "\n -n Run in foreground"
61 #include "common_bufsiz.h"
65 /* The Linux-specific klogctl(3) interface does not rely on the filesystem and
66 * allows us to change the console loglevel. Alternatively, we read the
67 * messages from _PATH_KLOG. */
69 #if ENABLE_FEATURE_KLOGD_KLOGCTL
71 # include <sys/klog.h>
73 static void klogd_open(void)
75 /* "Open the log. Currently a NOP" */
79 static void klogd_setloglevel(int lvl)
81 /* "printk() prints a message on the console only if it has a loglevel
82 * less than console_loglevel". Here we set console_loglevel = lvl. */
83 klogctl(8, NULL, lvl);
86 static int klogd_read(char *bufp, int len)
88 /* "2 -- Read from the log." */
89 return klogctl(2, bufp, len);
91 # define READ_ERROR "klogctl(2) error"
93 static void klogd_close(void)
95 /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
96 * via klogctl(8, NULL, 7). */
97 klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
98 klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
105 # define _PATH_KLOG "/dev/klog"
107 # error "your system's _PATH_KLOG is unknown"
110 # define PATH_PRINTK "/proc/sys/kernel/printk"
114 static void klogd_open(void)
116 int fd = xopen(_PATH_KLOG, O_RDONLY);
117 xmove_fd(fd, klogfd);
120 static void klogd_setloglevel(int lvl)
122 FILE *fp = fopen_or_warn(PATH_PRINTK, "w");
124 /* This changes only first value:
125 * "messages with a higher priority than this
126 * [that is, with numerically lower value]
127 * will be printed to the console".
128 * The other three values in this pseudo-file aren't changed.
130 fprintf(fp, "%u\n", lvl);
135 static int klogd_read(char *bufp, int len)
137 return read(klogfd, bufp, len);
139 # define READ_ERROR "read error"
141 static void klogd_close(void)
143 klogd_setloglevel(7);
144 if (ENABLE_FEATURE_CLEAN_UP)
150 #define log_buffer bb_common_bufsiz1
152 KLOGD_LOGBUF_SIZE = COMMON_BUFSIZE,
153 OPT_LEVEL = (1 << 0),
154 OPT_FOREGROUND = (1 << 1),
157 /* TODO: glibc openlog(LOG_KERN) reverts to LOG_USER instead,
158 * because that's how they interpret word "default"
159 * in the openlog() manpage:
161 * generic user-level messages
162 * and the fact that LOG_KERN is a constant 0.
163 * glibc interprets it as "0 in openlog() call means 'use default'".
164 * I think it means "if openlog wasn't called before syslog() is called,
166 * Convincing glibc maintainers otherwise is, as usual, nearly impossible.
167 * Should we open-code syslog() here to use correct facility?
170 int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
171 int klogd_main(int argc UNUSED_PARAM, char **argv)
178 setup_common_bufsiz();
180 opt = getopt32(argv, "c:n", &opt_c);
181 if (opt & OPT_LEVEL) {
182 /* Valid levels are between 1 and 8 */
183 i = xatou_range(opt_c, 1, 8);
185 if (!(opt & OPT_FOREGROUND)) {
186 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
189 logmode = LOGMODE_SYSLOG;
191 /* klogd_open() before openlog(), since it might use fixed fd 3,
192 * and openlog() also may use the same fd 3 if we swap them:
195 openlog("kernel", 0, LOG_KERN);
197 * glibc problem: for some reason, glibc changes LOG_KERN to LOG_USER
198 * above. The logic behind this is that standard
199 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/syslog.html
200 * says the following about openlog and syslog:
202 * Messages generated by arbitrary processes.
203 * This is the default facility identifier if none is specified."
205 * I believe glibc misinterpreted this text as "if openlog's
206 * third parameter is 0 (=LOG_KERN), treat it as LOG_USER".
207 * Whereas it was meant to say "if *syslog* is called with facility
208 * 0 in its 1st parameter without prior call to openlog, then perform
209 * implicit openlog(LOG_USER)".
211 * As a result of this, eh, feature, standard klogd was forced
212 * to open-code its own openlog and syslog implementation (!).
214 * Note that prohibiting openlog(LOG_KERN) on libc level does not
215 * add any security: any process can open a socket to "/dev/log"
216 * and write a string "<0>Voila, a LOG_KERN + LOG_EMERG message"
218 * Google code search tells me there is no widespread use of
219 * openlog("foo", 0, 0), thus fixing glibc won't break userspace.
221 * The bug against glibc was filed:
222 * bugzilla.redhat.com/show_bug.cgi?id=547000
226 klogd_setloglevel(i);
228 signal(SIGHUP, SIG_IGN);
229 /* We want klogd_read to not be restarted, thus _norestart: */
230 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
232 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
234 write_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
237 while (!bb_got_signal) {
242 start = log_buffer + used;
243 n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
247 bb_perror_msg(READ_ERROR);
252 /* Process each newline-terminated line in the buffer */
255 char *newline = strchrnul(start, '\n');
257 if (*newline == '\0') {
258 /* This line is incomplete */
260 /* move it to the front of the buffer */
261 overlapping_strcpy(log_buffer, start);
262 used = newline - start;
263 if (used < KLOGD_LOGBUF_SIZE-1) {
264 /* buffer isn't full */
267 /* buffer is full, log it anyway */
274 /* Extract the priority */
280 priority = strtoul(start, &end, 10);
286 /* Log (only non-empty lines) */
288 syslog(priority, "%s", start);
297 syslog(LOG_NOTICE, "klogd: exiting");
298 remove_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
300 kill_myself_with_sig(bb_got_signal);