blob: 845c49a5ea6844db01608011b60e8780a57ef57f [file] [log] [blame]
Mark Whitley6317c4b2001-03-12 22:51:50 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini klogd implementation for busybox
4 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00005 * Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>.
Mark Whitley6317c4b2001-03-12 22:51:50 +00006 * Changes: Made this a standalone busybox module which uses standalone
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +02007 * syslog() client interface.
Mark Whitley6317c4b2001-03-12 22:51:50 +00008 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00009 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Mark Whitley6317c4b2001-03-12 22:51:50 +000010 *
11 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
12 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000013 * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
Mark Whitley6317c4b2001-03-12 22:51:50 +000014 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000015 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
Mark Whitley6bff9cc2001-03-12 23:41:34 +000016 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020017 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mark Whitley6317c4b2001-03-12 22:51:50 +000018 */
Denys Vlasenkod34f3002015-10-18 18:42:03 +020019//config:config KLOGD
20//config: bool "klogd"
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
49//applet:IF_KLOGD(APPLET(klogd, BB_DIR_SBIN, BB_SUID_DROP))
50
51//kbuild:lib-$(CONFIG_KLOGD) += klogd.o
Mark Whitley6317c4b2001-03-12 22:51:50 +000052
Pere Orga5bc8c002011-04-11 03:29:49 +020053//usage:#define klogd_trivial_usage
54//usage: "[-c N] [-n]"
55//usage:#define klogd_full_usage "\n\n"
56//usage: "Kernel logger\n"
Denys Vlasenkoaeab42e2011-05-25 11:58:56 +020057//usage: "\n -c N Print to console messages more urgent than prio N (1-8)"
Pere Orga5bc8c002011-04-11 03:29:49 +020058//usage: "\n -n Run in foreground"
59
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000060#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020061#include "common_bufsiz.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000062#include <syslog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000063
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +020064
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. */
68
69#if ENABLE_FEATURE_KLOGD_KLOGCTL
70
71# include <sys/klog.h>
72
73static void klogd_open(void)
74{
75 /* "Open the log. Currently a NOP" */
76 klogctl(1, NULL, 0);
77}
78
79static void klogd_setloglevel(int lvl)
80{
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);
84}
85
86static int klogd_read(char *bufp, int len)
87{
88 return klogctl(2, bufp, len);
89}
90# define READ_ERROR "klogctl(2) error"
91
92static void klogd_close(void)
Mark Whitley6317c4b2001-03-12 22:51:50 +000093{
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +000094 /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
95 * via klogctl(8, NULL, 7). */
96 klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
97 klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
Mark Whitley6317c4b2001-03-12 22:51:50 +000098}
99
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200100#else
101
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200102# ifndef _PATH_KLOG
103# ifdef __GNU__
104# define _PATH_KLOG "/dev/klog"
105# else
106# error "your system's _PATH_KLOG is unknown"
107# endif
108# endif
109# define PATH_PRINTK "/proc/sys/kernel/printk"
110
111enum { klogfd = 3 };
112
113static void klogd_open(void)
114{
115 int fd = xopen(_PATH_KLOG, O_RDONLY);
116 xmove_fd(fd, klogfd);
117}
118
119static void klogd_setloglevel(int lvl)
120{
121 FILE *fp = fopen_or_warn(PATH_PRINTK, "w");
122 if (fp) {
123 /* This changes only first value:
124 * "messages with a higher priority than this
125 * [that is, with numerically lower value]
126 * will be printed to the console".
127 * The other three values in this pseudo-file aren't changed.
128 */
129 fprintf(fp, "%u\n", lvl);
130 fclose(fp);
131 }
132}
133
134static int klogd_read(char *bufp, int len)
135{
136 return read(klogfd, bufp, len);
137}
138# define READ_ERROR "read error"
139
140static void klogd_close(void)
141{
142 klogd_setloglevel(7);
143 if (ENABLE_FEATURE_CLEAN_UP)
144 close(klogfd);
145}
146
147#endif
148
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200149#define log_buffer bb_common_bufsiz1
150#define sizeof_log_buffer COMMON_BUFSIZE
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000151enum {
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200152 KLOGD_LOGBUF_SIZE = sizeof_log_buffer,
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000153 OPT_LEVEL = (1 << 0),
154 OPT_FOREGROUND = (1 << 1),
155};
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000156
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200157/* TODO: glibc openlog(LOG_KERN) reverts to LOG_USER instead,
158 * because that's how they interpret word "default"
159 * in the openlog() manpage:
160 * LOG_USER (default)
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,
165 * use default".
166 * Convincing glibc maintainers otherwise is, as usual, nearly impossible.
167 * Should we open-code syslog() here to use correct facility?
168 */
169
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000170int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000171int klogd_main(int argc UNUSED_PARAM, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +0000172{
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000173 int i = 0;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000174 char *opt_c;
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000175 int opt;
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200176 int used;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000177
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000178 opt = getopt32(argv, "c:n", &opt_c);
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000179 if (opt & OPT_LEVEL) {
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000180 /* Valid levels are between 1 and 8 */
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000181 i = xatou_range(opt_c, 1, 8);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000182 }
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000183 if (!(opt & OPT_FOREGROUND)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000184 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000185 }
186
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200187 logmode = LOGMODE_SYSLOG;
188
189 /* klogd_open() before openlog(), since it might use fixed fd 3,
190 * and openlog() also may use the same fd 3 if we swap them:
191 */
192 klogd_open();
Eric Andersen36adca82004-06-22 10:07:17 +0000193 openlog("kernel", 0, LOG_KERN);
Denys Vlasenko8b6b4722011-03-07 10:57:26 +0100194 /*
195 * glibc problem: for some reason, glibc changes LOG_KERN to LOG_USER
196 * above. The logic behind this is that standard
197 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/syslog.html
198 * says the following about openlog and syslog:
199 * "LOG_USER
200 * Messages generated by arbitrary processes.
201 * This is the default facility identifier if none is specified."
202 *
203 * I believe glibc misinterpreted this text as "if openlog's
204 * third parameter is 0 (=LOG_KERN), treat it as LOG_USER".
205 * Whereas it was meant to say "if *syslog* is called with facility
206 * 0 in its 1st parameter without prior call to openlog, then perform
207 * implicit openlog(LOG_USER)".
208 *
209 * As a result of this, eh, feature, standard klogd was forced
210 * to open-code its own openlog and syslog implementation (!).
211 *
212 * Note that prohibiting openlog(LOG_KERN) on libc level does not
213 * add any security: any process can open a socket to "/dev/log"
214 * and write a string "<0>Voila, a LOG_KERN + LOG_EMERG message"
215 *
216 * Google code search tells me there is no widespread use of
217 * openlog("foo", 0, 0), thus fixing glibc won't break userspace.
218 *
219 * The bug against glibc was filed:
220 * bugzilla.redhat.com/show_bug.cgi?id=547000
221 */
Eric Andersen36adca82004-06-22 10:07:17 +0000222
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000223 if (i)
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200224 klogd_setloglevel(i);
225
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200226 signal(SIGHUP, SIG_IGN);
Denys Vlasenko8b6b4722011-03-07 10:57:26 +0100227 /* We want klogd_read to not be restarted, thus _norestart: */
228 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +0000229
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000230 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000231
Anthony G. Basile12677ac2012-12-10 14:49:39 -0500232 write_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
233
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200234 used = 0;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200235 while (!bb_got_signal) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +0000236 int n;
237 int priority;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000238 char *start;
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +0000239
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000240 /* "2 -- Read from the log." */
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000241 start = log_buffer + used;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200242 n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000243 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000244 if (errno == EINTR)
245 continue;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200246 bb_perror_msg(READ_ERROR);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000247 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000248 }
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000249 start[n] = '\0';
Denis Vlasenko93d07762008-10-04 16:40:17 +0000250
Denis Vlasenko93d07762008-10-04 16:40:17 +0000251 /* Process each newline-terminated line in the buffer */
Denis Vlasenko58a88912008-11-19 09:35:00 +0000252 start = log_buffer;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000253 while (1) {
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000254 char *newline = strchrnul(start, '\n');
Denis Vlasenko93d07762008-10-04 16:40:17 +0000255
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000256 if (*newline == '\0') {
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200257 /* This line is incomplete */
258
259 /* move it to the front of the buffer */
260 overlapping_strcpy(log_buffer, start);
261 used = newline - start;
262 if (used < KLOGD_LOGBUF_SIZE-1) {
263 /* buffer isn't full */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000264 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000265 }
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200266 /* buffer is full, log it anyway */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000267 used = 0;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000268 newline = NULL;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000269 } else {
270 *newline++ = '\0';
Mark Whitley6317c4b2001-03-12 22:51:50 +0000271 }
Denis Vlasenko93d07762008-10-04 16:40:17 +0000272
273 /* Extract the priority */
274 priority = LOG_INFO;
275 if (*start == '<') {
276 start++;
Peter Korsgaardd189b592013-01-06 00:07:18 +0100277 if (*start)
278 priority = strtoul(start, &start, 10);
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000279 if (*start == '>')
Denis Vlasenko93d07762008-10-04 16:40:17 +0000280 start++;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000281 }
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000282 /* Log (only non-empty lines) */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000283 if (*start)
284 syslog(priority, "%s", start);
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000285
Denis Vlasenko93d07762008-10-04 16:40:17 +0000286 if (!newline)
287 break;
288 start = newline;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000289 }
290 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000291
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200292 klogd_close();
293 syslog(LOG_NOTICE, "klogd: exiting");
Anthony G. Basile12677ac2012-12-10 14:49:39 -0500294 remove_pidfile(CONFIG_PID_FILE_PATH "/klogd.pid");
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200295 if (bb_got_signal)
296 kill_myself_with_sig(bb_got_signal);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000297 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000298}