723ca80677358de428ac7f166367fa7bae454414
[oweals/busybox.git] / sysklogd / klogd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini klogd implementation for busybox
4  *
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.
8  *
9  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
10  *
11  * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
12  *
13  * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
14  *
15  * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
16  *
17  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
18  */
19
20 #include "libbb.h"
21 #include <syslog.h>
22 #include <sys/klog.h>
23
24 static void klogd_signal(int sig)
25 {
26         /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
27          * via klogctl(8, NULL, 7). */
28         klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
29         klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
30         syslog(LOG_NOTICE, "klogd: exiting");
31         kill_myself_with_sig(sig);
32 }
33
34 #define log_buffer bb_common_bufsiz1
35 enum {
36         KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
37         OPT_LEVEL      = (1 << 0),
38         OPT_FOREGROUND = (1 << 1),
39 };
40
41 int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int klogd_main(int argc UNUSED_PARAM, char **argv)
43 {
44         int i = 0;
45         char *start;
46         int opt;
47         int used = 0;
48
49         opt = getopt32(argv, "c:n", &start);
50         if (opt & OPT_LEVEL) {
51                 /* Valid levels are between 1 and 8 */
52                 i = xatou_range(start, 1, 8);
53         }
54         if (!(opt & OPT_FOREGROUND)) {
55                 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
56         }
57
58         openlog("kernel", 0, LOG_KERN);
59
60         bb_signals(0
61                 + (1 << SIGINT)
62                 + (1 << SIGTERM)
63                 , klogd_signal);
64         signal(SIGHUP, SIG_IGN);
65
66         /* "Open the log. Currently a NOP" */
67         klogctl(1, NULL, 0);
68
69         /* "printk() prints a message on the console only if it has a loglevel
70          * less than console_loglevel". Here we set console_loglevel = i. */
71         if (i)
72                 klogctl(8, NULL, i);
73
74         syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
75
76         /* Initially null terminate the buffer in case of a very long line */
77         log_buffer[KLOGD_LOGBUF_SIZE - 1] = '\0';
78
79         while (1) {
80                 int n;
81                 int priority;
82
83                 /* "2 -- Read from the log." */
84                 n = klogctl(2, log_buffer + used, KLOGD_LOGBUF_SIZE-1 - used);
85                 if (n < 0) {
86                         if (errno == EINTR)
87                                 continue;
88                         syslog(LOG_ERR, "klogd: error %d in klogctl(2): %m",
89                                         errno);
90                         break;
91                 }
92
93                 /* klogctl buffer parsing modelled after code in dmesg.c */
94                 start = &log_buffer[0];
95
96                 /* Process each newline-terminated line in the buffer */
97                 while (1) {
98                         char *newline = strchr(start, '\n');
99
100                         if (!newline) {
101                                 /* This line is incomplete... */
102                                 if (start != log_buffer) {
103                                         /* move it to the front of the buffer */
104                                         strcpy(log_buffer, start);
105                                         /* don't log it yet */
106                                         used = strlen(log_buffer);
107                                         break;
108                                 }
109                                 /* ...but buffer is full, so log it anyway */
110                                 used = 0;
111                         } else {
112                                 *newline++ = '\0';
113                         }
114
115                         /* Extract the priority */
116                         priority = LOG_INFO;
117                         if (*start == '<') {
118                                 start++;
119                                 if (*start) {
120                                         /* kernel never generates multi-digit prios */
121                                         priority = (*start - '0');
122                                         start++;
123                                 }
124                                 if (*start == '>') {
125                                         start++;
126                                 }
127                         }
128                         if (*start)
129                                 syslog(priority, "%s", start);
130                         if (!newline)
131                                 break;
132                         start = newline;
133                 }
134         }
135
136         return EXIT_FAILURE;
137 }