blob: 27995e57028f09733dbad1d1a0e2c56cbeb7bb26 [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 */
19
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage:#define klogd_trivial_usage
21//usage: "[-c N] [-n]"
22//usage:#define klogd_full_usage "\n\n"
23//usage: "Kernel logger\n"
24//usage: "\nOptions:"
Denys Vlasenkoaeab42e2011-05-25 11:58:56 +020025//usage: "\n -c N Print to console messages more urgent than prio N (1-8)"
Pere Orga5bc8c002011-04-11 03:29:49 +020026//usage: "\n -n Run in foreground"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000029#include <syslog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000030
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +020031
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. */
35
36#if ENABLE_FEATURE_KLOGD_KLOGCTL
37
38# include <sys/klog.h>
39
40static void klogd_open(void)
41{
42 /* "Open the log. Currently a NOP" */
43 klogctl(1, NULL, 0);
44}
45
46static void klogd_setloglevel(int lvl)
47{
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);
51}
52
53static int klogd_read(char *bufp, int len)
54{
55 return klogctl(2, bufp, len);
56}
57# define READ_ERROR "klogctl(2) error"
58
59static void klogd_close(void)
Mark Whitley6317c4b2001-03-12 22:51:50 +000060{
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +000061 /* 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" */
Mark Whitley6317c4b2001-03-12 22:51:50 +000065}
66
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +020067#else
68
69# include <paths.h>
70# ifndef _PATH_KLOG
71# ifdef __GNU__
72# define _PATH_KLOG "/dev/klog"
73# else
74# error "your system's _PATH_KLOG is unknown"
75# endif
76# endif
77# define PATH_PRINTK "/proc/sys/kernel/printk"
78
79enum { klogfd = 3 };
80
81static void klogd_open(void)
82{
83 int fd = xopen(_PATH_KLOG, O_RDONLY);
84 xmove_fd(fd, klogfd);
85}
86
87static void klogd_setloglevel(int lvl)
88{
89 FILE *fp = fopen_or_warn(PATH_PRINTK, "w");
90 if (fp) {
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.
96 */
97 fprintf(fp, "%u\n", lvl);
98 fclose(fp);
99 }
100}
101
102static int klogd_read(char *bufp, int len)
103{
104 return read(klogfd, bufp, len);
105}
106# define READ_ERROR "read error"
107
108static void klogd_close(void)
109{
110 klogd_setloglevel(7);
111 if (ENABLE_FEATURE_CLEAN_UP)
112 close(klogfd);
113}
114
115#endif
116
Denis Vlasenkoc84520d2007-02-17 14:12:10 +0000117#define log_buffer bb_common_bufsiz1
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000118enum {
119 KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
120 OPT_LEVEL = (1 << 0),
121 OPT_FOREGROUND = (1 << 1),
122};
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000123
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200124/* TODO: glibc openlog(LOG_KERN) reverts to LOG_USER instead,
125 * because that's how they interpret word "default"
126 * in the openlog() manpage:
127 * LOG_USER (default)
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,
132 * use default".
133 * Convincing glibc maintainers otherwise is, as usual, nearly impossible.
134 * Should we open-code syslog() here to use correct facility?
135 */
136
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000137int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000138int klogd_main(int argc UNUSED_PARAM, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +0000139{
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000140 int i = 0;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000141 char *opt_c;
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000142 int opt;
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200143 int used;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000144
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000145 opt = getopt32(argv, "c:n", &opt_c);
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000146 if (opt & OPT_LEVEL) {
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000147 /* Valid levels are between 1 and 8 */
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000148 i = xatou_range(opt_c, 1, 8);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000149 }
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000150 if (!(opt & OPT_FOREGROUND)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000151 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000152 }
153
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200154 logmode = LOGMODE_SYSLOG;
155
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:
158 */
159 klogd_open();
Eric Andersen36adca82004-06-22 10:07:17 +0000160 openlog("kernel", 0, LOG_KERN);
Denys Vlasenko8b6b4722011-03-07 10:57:26 +0100161 /*
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:
166 * "LOG_USER
167 * Messages generated by arbitrary processes.
168 * This is the default facility identifier if none is specified."
169 *
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)".
175 *
176 * As a result of this, eh, feature, standard klogd was forced
177 * to open-code its own openlog and syslog implementation (!).
178 *
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"
182 *
183 * Google code search tells me there is no widespread use of
184 * openlog("foo", 0, 0), thus fixing glibc won't break userspace.
185 *
186 * The bug against glibc was filed:
187 * bugzilla.redhat.com/show_bug.cgi?id=547000
188 */
Eric Andersen36adca82004-06-22 10:07:17 +0000189
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000190 if (i)
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200191 klogd_setloglevel(i);
192
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200193 signal(SIGHUP, SIG_IGN);
Denys Vlasenko8b6b4722011-03-07 10:57:26 +0100194 /* We want klogd_read to not be restarted, thus _norestart: */
195 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +0000196
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000197 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000198
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200199 used = 0;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200200 while (!bb_got_signal) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +0000201 int n;
202 int priority;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000203 char *start;
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +0000204
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000205 /* "2 -- Read from the log." */
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000206 start = log_buffer + used;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200207 n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000208 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000209 if (errno == EINTR)
210 continue;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200211 bb_perror_msg(READ_ERROR);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000212 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000213 }
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000214 start[n] = '\0';
Denis Vlasenko93d07762008-10-04 16:40:17 +0000215
Denis Vlasenko93d07762008-10-04 16:40:17 +0000216 /* Process each newline-terminated line in the buffer */
Denis Vlasenko58a88912008-11-19 09:35:00 +0000217 start = log_buffer;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000218 while (1) {
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000219 char *newline = strchrnul(start, '\n');
Denis Vlasenko93d07762008-10-04 16:40:17 +0000220
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000221 if (*newline == '\0') {
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200222 /* This line is incomplete */
223
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 */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000229 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000230 }
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200231 /* buffer is full, log it anyway */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000232 used = 0;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000233 newline = NULL;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000234 } else {
235 *newline++ = '\0';
Mark Whitley6317c4b2001-03-12 22:51:50 +0000236 }
Denis Vlasenko93d07762008-10-04 16:40:17 +0000237
238 /* Extract the priority */
239 priority = LOG_INFO;
240 if (*start == '<') {
241 start++;
242 if (*start) {
243 /* kernel never generates multi-digit prios */
244 priority = (*start - '0');
245 start++;
246 }
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000247 if (*start == '>')
Denis Vlasenko93d07762008-10-04 16:40:17 +0000248 start++;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000249 }
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000250 /* Log (only non-empty lines) */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000251 if (*start)
252 syslog(priority, "%s", start);
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000253
Denis Vlasenko93d07762008-10-04 16:40:17 +0000254 if (!newline)
255 break;
256 start = newline;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000257 }
258 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000259
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200260 klogd_close();
261 syslog(LOG_NOTICE, "klogd: exiting");
262 if (bb_got_signal)
263 kill_myself_with_sig(bb_got_signal);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000264 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000265}