Merge branch 'Teaman-ND' into Teaman-RT
[tomato.git] / release / src / router / busybox / sysklogd / klogd.c
blob96c3666b76c9cd909b5612122855bf41cbf463e0
1 /* vi: set sw=4 ts=4: */
2 /*
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 #include "libbb.h"
21 #include <syslog.h>
24 /* The Linux-specific klogctl(3) interface does not rely on the filesystem and
25 * allows us to change the console loglevel. Alternatively, we read the
26 * messages from _PATH_KLOG. */
28 #if ENABLE_FEATURE_KLOGD_KLOGCTL
30 # include <sys/klog.h>
32 static void klogd_open(void)
34 /* "Open the log. Currently a NOP" */
35 klogctl(1, NULL, 0);
38 static void klogd_setloglevel(int lvl)
40 /* "printk() prints a message on the console only if it has a loglevel
41 * less than console_loglevel". Here we set console_loglevel = lvl. */
42 klogctl(8, NULL, lvl);
45 static int klogd_read(char *bufp, int len)
47 return klogctl(2, bufp, len);
49 # define READ_ERROR "klogctl(2) error"
51 static void klogd_close(void)
53 /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
54 * via klogctl(8, NULL, 7). */
55 klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
56 klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
59 #else
61 # include <paths.h>
62 # ifndef _PATH_KLOG
63 # ifdef __GNU__
64 # define _PATH_KLOG "/dev/klog"
65 # else
66 # error "your system's _PATH_KLOG is unknown"
67 # endif
68 # endif
69 # define PATH_PRINTK "/proc/sys/kernel/printk"
71 enum { klogfd = 3 };
73 static void klogd_open(void)
75 int fd = xopen(_PATH_KLOG, O_RDONLY);
76 xmove_fd(fd, klogfd);
79 static void klogd_setloglevel(int lvl)
81 FILE *fp = fopen_or_warn(PATH_PRINTK, "w");
82 if (fp) {
83 /* This changes only first value:
84 * "messages with a higher priority than this
85 * [that is, with numerically lower value]
86 * will be printed to the console".
87 * The other three values in this pseudo-file aren't changed.
89 fprintf(fp, "%u\n", lvl);
90 fclose(fp);
94 static int klogd_read(char *bufp, int len)
96 return read(klogfd, bufp, len);
98 # define READ_ERROR "read error"
100 static void klogd_close(void)
102 klogd_setloglevel(7);
103 if (ENABLE_FEATURE_CLEAN_UP)
104 close(klogfd);
107 #endif
109 char log_buffer[6 * 1024 + 1]; /* Big enough to not lose msgs at bootup */
110 enum {
111 KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
112 OPT_LEVEL = (1 << 0),
113 OPT_FOREGROUND = (1 << 1),
116 /* TODO: glibc openlog(LOG_KERN) reverts to LOG_USER instead,
117 * because that's how they interpret word "default"
118 * in the openlog() manpage:
119 * LOG_USER (default)
120 * generic user-level messages
121 * and the fact that LOG_KERN is a constant 0.
122 * glibc interprets it as "0 in openlog() call means 'use default'".
123 * I think it means "if openlog wasn't called before syslog() is called,
124 * use default".
125 * Convincing glibc maintainers otherwise is, as usual, nearly impossible.
126 * Should we open-code syslog() here to use correct facility?
129 int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
130 int klogd_main(int argc UNUSED_PARAM, char **argv)
132 int i = 0;
133 char *opt_c;
134 int opt;
135 int used;
136 unsigned int cnt;
138 opt = getopt32(argv, "c:n", &opt_c);
139 if (opt & OPT_LEVEL) {
140 /* Valid levels are between 1 and 8 */
141 i = xatou_range(opt_c, 1, 8);
143 if (!(opt & OPT_FOREGROUND)) {
144 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
147 logmode = LOGMODE_SYSLOG;
149 /* klogd_open() before openlog(), since it might use fixed fd 3,
150 * and openlog() also may use the same fd 3 if we swap them:
152 klogd_open();
153 openlog("kernel", 0, LOG_KERN);
155 * glibc problem: for some reason, glibc changes LOG_KERN to LOG_USER
156 * above. The logic behind this is that standard
157 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/syslog.html
158 * says the following about openlog and syslog:
159 * "LOG_USER
160 * Messages generated by arbitrary processes.
161 * This is the default facility identifier if none is specified."
163 * I believe glibc misinterpreted this text as "if openlog's
164 * third parameter is 0 (=LOG_KERN), treat it as LOG_USER".
165 * Whereas it was meant to say "if *syslog* is called with facility
166 * 0 in its 1st parameter without prior call to openlog, then perform
167 * implicit openlog(LOG_USER)".
169 * As a result of this, eh, feature, standard klogd was forced
170 * to open-code its own openlog and syslog implementation (!).
172 * Note that prohibiting openlog(LOG_KERN) on libc level does not
173 * add any security: any process can open a socket to "/dev/log"
174 * and write a string "<0>Voila, a LOG_KERN + LOG_EMERG message"
176 * Google code search tells me there is no widespread use of
177 * openlog("foo", 0, 0), thus fixing glibc won't break userspace.
179 * The bug against glibc was filed:
180 * bugzilla.redhat.com/show_bug.cgi?id=547000
183 if (i)
184 klogd_setloglevel(i);
186 signal(SIGHUP, SIG_IGN);
187 /* We want klogd_read to not be restarted, thus _norestart: */
188 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
190 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
192 used = 0;
193 cnt = 0;
194 while (!bb_got_signal) {
195 int n;
196 int priority;
197 char *start;
198 char *eor;
200 /* "2 -- Read from the log." */
201 start = log_buffer + used;
202 n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
203 if (n < 0) {
204 if (errno == EINTR)
205 continue;
206 bb_perror_msg(READ_ERROR);
207 break;
209 start[n] = '\0';
210 eor = &start[n];
212 /* Process each newline-terminated line in the buffer */
213 start = log_buffer;
214 while (1) {
215 char *newline = strchrnul(start, '\n');
217 if (*newline == '\0') {
218 /* This line is incomplete */
220 /* move it to the front of the buffer */
221 overlapping_strcpy(log_buffer, start);
222 used = newline - start;
223 if (used < KLOGD_LOGBUF_SIZE-1) {
224 /* buffer isn't full */
225 break;
227 /* buffer is full, log it anyway */
228 used = 0;
229 newline = NULL;
230 } else {
231 *newline++ = '\0';
234 /* Extract the priority */
235 priority = LOG_INFO;
236 if (*start == '<') {
237 start++;
238 if (*start) {
239 /* kernel never generates multi-digit prios */
240 priority = (*start - '0');
241 start++;
243 if (*start == '>')
244 start++;
246 /* Log (only non-empty lines) */
247 if (*start) {
248 syslog(priority, "%s", start);
249 /* give syslog time to catch up */
250 ++cnt;
251 if ((cnt & 0x07) == 0 && (cnt < 300 || (eor - start) > 200))
252 usleep(50 * 1000);
255 if (!newline)
256 break;
257 start = newline;
261 klogd_close();
262 syslog(LOG_NOTICE, "klogd: exiting");
263 if (bb_got_signal)
264 kill_myself_with_sig(bb_got_signal);
265 return EXIT_FAILURE;