blob: 0d4c2578d3c5e99edaa0a461418987bdfdec7331 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000021#include <syslog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000022
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +020023
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. */
27
28#if ENABLE_FEATURE_KLOGD_KLOGCTL
29
30# include <sys/klog.h>
31
32static void klogd_open(void)
33{
34 /* "Open the log. Currently a NOP" */
35 klogctl(1, NULL, 0);
36}
37
38static void klogd_setloglevel(int lvl)
39{
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);
43}
44
45static int klogd_read(char *bufp, int len)
46{
47 return klogctl(2, bufp, len);
48}
49# define READ_ERROR "klogctl(2) error"
50
51static void klogd_close(void)
Mark Whitley6317c4b2001-03-12 22:51:50 +000052{
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +000053 /* 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" */
Mark Whitley6317c4b2001-03-12 22:51:50 +000057}
58
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +020059#else
60
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"
70
71enum { klogfd = 3 };
72
73static void klogd_open(void)
74{
75 int fd = xopen(_PATH_KLOG, O_RDONLY);
76 xmove_fd(fd, klogfd);
77}
78
79static void klogd_setloglevel(int lvl)
80{
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.
88 */
89 fprintf(fp, "%u\n", lvl);
90 fclose(fp);
91 }
92}
93
94static int klogd_read(char *bufp, int len)
95{
96 return read(klogfd, bufp, len);
97}
98# define READ_ERROR "read error"
99
100static void klogd_close(void)
101{
102 klogd_setloglevel(7);
103 if (ENABLE_FEATURE_CLEAN_UP)
104 close(klogfd);
105}
106
107#endif
108
Denis Vlasenkoc84520d2007-02-17 14:12:10 +0000109#define log_buffer bb_common_bufsiz1
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000110enum {
111 KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
112 OPT_LEVEL = (1 << 0),
113 OPT_FOREGROUND = (1 << 1),
114};
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000115
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200116/* 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?
127 */
128
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000129int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000130int klogd_main(int argc UNUSED_PARAM, char **argv)
Mark Whitley6317c4b2001-03-12 22:51:50 +0000131{
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000132 int i = 0;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000133 char *opt_c;
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000134 int opt;
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200135 int used;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000136
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000137 opt = getopt32(argv, "c:n", &opt_c);
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000138 if (opt & OPT_LEVEL) {
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000139 /* Valid levels are between 1 and 8 */
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000140 i = xatou_range(opt_c, 1, 8);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000141 }
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000142 if (!(opt & OPT_FOREGROUND)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000143 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Bernhard Reutner-Fischer595159f2006-05-31 12:22:13 +0000144 }
145
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200146 logmode = LOGMODE_SYSLOG;
147
148 /* klogd_open() before openlog(), since it might use fixed fd 3,
149 * and openlog() also may use the same fd 3 if we swap them:
150 */
151 klogd_open();
Eric Andersen36adca82004-06-22 10:07:17 +0000152 openlog("kernel", 0, LOG_KERN);
153
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000154 if (i)
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200155 klogd_setloglevel(i);
156
157 bb_signals(BB_FATAL_SIGS, record_signo);
158 signal(SIGHUP, SIG_IGN);
Glenn L McGrathe1ad6722002-12-01 11:31:58 +0000159
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000160 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000161
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200162 used = 0;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200163 while (!bb_got_signal) {
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +0000164 int n;
165 int priority;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000166 char *start;
Bernhard Reutner-Fischer8fc40112007-01-09 15:46:36 +0000167
Denis Vlasenko7bdf0c82008-06-06 16:08:04 +0000168 /* "2 -- Read from the log." */
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000169 start = log_buffer + used;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200170 n = klogd_read(start, KLOGD_LOGBUF_SIZE-1 - used);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000171 if (n < 0) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000172 if (errno == EINTR)
173 continue;
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200174 bb_perror_msg(READ_ERROR);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000175 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000176 }
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000177 start[n] = '\0';
Denis Vlasenko93d07762008-10-04 16:40:17 +0000178
Denis Vlasenko93d07762008-10-04 16:40:17 +0000179 /* Process each newline-terminated line in the buffer */
Denis Vlasenko58a88912008-11-19 09:35:00 +0000180 start = log_buffer;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000181 while (1) {
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000182 char *newline = strchrnul(start, '\n');
Denis Vlasenko93d07762008-10-04 16:40:17 +0000183
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000184 if (*newline == '\0') {
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200185 /* This line is incomplete */
186
187 /* move it to the front of the buffer */
188 overlapping_strcpy(log_buffer, start);
189 used = newline - start;
190 if (used < KLOGD_LOGBUF_SIZE-1) {
191 /* buffer isn't full */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000192 break;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000193 }
Denys Vlasenko0016bce2010-10-19 23:07:49 +0200194 /* buffer is full, log it anyway */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000195 used = 0;
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000196 newline = NULL;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000197 } else {
198 *newline++ = '\0';
Mark Whitley6317c4b2001-03-12 22:51:50 +0000199 }
Denis Vlasenko93d07762008-10-04 16:40:17 +0000200
201 /* Extract the priority */
202 priority = LOG_INFO;
203 if (*start == '<') {
204 start++;
205 if (*start) {
206 /* kernel never generates multi-digit prios */
207 priority = (*start - '0');
208 start++;
209 }
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000210 if (*start == '>')
Denis Vlasenko93d07762008-10-04 16:40:17 +0000211 start++;
Denis Vlasenko93d07762008-10-04 16:40:17 +0000212 }
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000213 /* Log (only non-empty lines) */
Denis Vlasenko93d07762008-10-04 16:40:17 +0000214 if (*start)
215 syslog(priority, "%s", start);
Denis Vlasenko2e7dc5d2008-11-19 07:59:49 +0000216
Denis Vlasenko93d07762008-10-04 16:40:17 +0000217 if (!newline)
218 break;
219 start = newline;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000220 }
221 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000222
Jeremie Koenig63c2e7e2010-08-01 03:01:44 +0200223 klogd_close();
224 syslog(LOG_NOTICE, "klogd: exiting");
225 if (bb_got_signal)
226 kill_myself_with_sig(bb_got_signal);
Denis Vlasenkoe428e9d2007-01-04 03:07:57 +0000227 return EXIT_FAILURE;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000228}