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 //usage:#define klogd_trivial_usage
21 //usage: "[-c N] [-n]"
22 //usage:#define klogd_full_usage "\n\n"
23 //usage: "Kernel logger\n"
25 //usage: "\n -c N Only messages with level < N are printed to console"
26 //usage: "\n -n Run in foreground"
32 /* The Linux-specific klogctl(3) interface does not rely on the filesystem and
33 * allows us to change the console loglevel. Alternatively, we read the
34 * messages from _PATH_KLOG. */
36 #if ENABLE_FEATURE_KLOGD_KLOGCTL
38 # include <sys/klog.h>
40 static void klogd_open(void)
42 /* "Open the log. Currently a NOP" */
46 static void klogd_setloglevel(int lvl)
48 /* "printk() prints a message on the console only if it has a loglevel
49 * less than console_loglevel". Here we set console_loglevel = lvl. */
50 klogctl(8, NULL, lvl);
53 static int klogd_read(char *bufp, int len)
55 return klogctl(2, bufp, len);
57 # define READ_ERROR "klogctl(2) error"
59 static void klogd_close(void)
61 /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
62 * via klogctl(8, NULL, 7). */
63 klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
64 klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
72 # define _PATH_KLOG "/dev/klog"
74 # error "your system's _PATH_KLOG is unknown"
77 # define PATH_PRINTK "/proc/sys/kernel/printk"
81 static void klogd_open(void)
83 int fd = xopen(_PATH_KLOG, O_RDONLY);
87 static void klogd_setloglevel(int lvl)
89 FILE *fp = fopen_or_warn(PATH_PRINTK, "w");
91 /* This changes only first value:
92 * "messages with a higher priority than this
93 * [that is, with numerically lower value]
94 * will be printed to the console".
95 * The other three values in this pseudo-file aren't changed.
97 fprintf(fp, "%u\n", lvl);
102 static int klogd_read(char *bufp, int len)
104 return read(klogfd, bufp, len);
106 # define READ_ERROR "read error"
108 static void klogd_close(void)
110 klogd_setloglevel(7);
111 if (ENABLE_FEATURE_CLEAN_UP)
117 #define log_buffer bb_common_bufsiz1
119 KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
120 OPT_LEVEL = (1 << 0),
121 OPT_FOREGROUND = (1 << 1),
124 /* TODO: glibc openlog(LOG_KERN) reverts to LOG_USER instead,
125 * because that's how they interpret word "default"
126 * in the openlog() manpage:
128 * generic user-level messages
129 * and the fact that LOG_KERN is a constant 0.
130 * glibc interprets it as "0 in openlog() call means 'use default'".
131 * I think it means "if openlog wasn't called before syslog() is called,
133 * Convincing glibc maintainers otherwise is, as usual, nearly impossible.
134 * Should we open-code syslog() here to use correct facility?
137 int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
138 int klogd_main(int argc UNUSED_PARAM, char **argv)
145 opt = getopt32(argv, "c:n", &opt_c);
146 if (opt & OPT_LEVEL) {
147 /* Valid levels are between 1 and 8 */
148 i = xatou_range(opt_c, 1, 8);
150 if (!(opt & OPT_FOREGROUND)) {
151 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
154 logmode = LOGMODE_SYSLOG;
156 /* klogd_open() before openlog(), since it might use fixed fd 3,
157 * and openlog() also may use the same fd 3 if we swap them:
160 openlog("kernel", 0, LOG_KERN);
162 * glibc problem: for some reason, glibc changes LOG_KERN to LOG_USER
163 * above. The logic behind this is that standard
164 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/syslog.html
165 * says the following about openlog and syslog:
167 * Messages generated by arbitrary processes.
168 * This is the default facility identifier if none is specified."
170 * I believe glibc misinterpreted this text as "if openlog's
171 * third parameter is 0 (=LOG_KERN), treat it as LOG_USER".
172 * Whereas it was meant to say "if *syslog* is called with facility
173 * 0 in its 1st parameter without prior call to openlog, then perform
174 * implicit openlog(LOG_USER)".
176 * As a result of this, eh, feature, standard klogd was forced
177 * to open-code its own openlog and syslog implementation (!).
179 * Note that prohibiting openlog(LOG_KERN) on libc level does not
180 * add any security: any process can open a socket to "/dev/log"
181 * and write a string "<0>Voila, a LOG_KERN + LOG_EMERG message"
183 * Google code search tells me there is no widespread use of
184 * openlog("foo", 0, 0), thus fixing glibc won't break userspace.
186 * The bug against glibc was filed:
187 * bugzilla.redhat.com/show_bug.cgi?id=547000
191 klogd_setloglevel(i);
193 signal(SIGHUP, SIG_IGN);
194 /* We want klogd_read to not be restarted, thus _norestart: */
195 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
197 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
200 while (!bb_got_signal) {
205 /* "2 -- Read from the log." */
206 start = log_buffer + used;
207 n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
211 bb_perror_msg(READ_ERROR);
216 /* Process each newline-terminated line in the buffer */
219 char *newline = strchrnul(start, '\n');
221 if (*newline == '\0') {
222 /* 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 */
231 /* buffer is full, log it anyway */
238 /* Extract the priority */
243 /* kernel never generates multi-digit prios */
244 priority = (*start - '0');
250 /* Log (only non-empty lines) */
252 syslog(priority, "%s", start);
261 syslog(LOG_NOTICE, "klogd: exiting");
263 kill_myself_with_sig(bb_got_signal);