4132eb68ef55b251adfd3179a6fa73c7d3e22408
[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,2000 by Lineo, inc. and Erik Andersen
10  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
11  *
12  * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
13  *
14  * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
15  *
16  * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26  * General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <signal.h>             /* for our signal() handlers */
37 #include <string.h>             /* strncpy() */
38 #include <errno.h>              /* errno and friends */
39 #include <unistd.h>
40 #include <ctype.h>
41 #include <sys/syslog.h>
42
43 #if __GNU_LIBRARY__ < 5
44 # ifdef __alpha__
45 #   define klogctl syslog
46 # endif
47 #else
48 # include <sys/klog.h>
49 #endif
50
51 #include "busybox.h"
52
53 static void klogd_signal(int sig)
54 {
55         klogctl(7, NULL, 0);
56         klogctl(0, 0, 0);
57         /* logMessage(0, "Kernel log daemon exiting."); */
58         syslog_msg(LOG_SYSLOG, LOG_NOTICE, "Kernel log daemon exiting.");
59         exit(TRUE);
60 }
61
62 static void doKlogd(const char console_log_level) __attribute__ ((noreturn));
63 static void doKlogd(const char console_log_level)
64 {
65         int priority = LOG_INFO;
66         char log_buffer[4096];
67         int i, n, lastc;
68         char *start;
69
70         /* Set up sig handlers */
71         signal(SIGINT, klogd_signal);
72         signal(SIGKILL, klogd_signal);
73         signal(SIGTERM, klogd_signal);
74         signal(SIGHUP, SIG_IGN);
75
76         /* "Open the log. Currently a NOP." */
77         klogctl(1, NULL, 0);
78
79         /* Set level of kernel console messaging.. */
80         if (console_log_level)
81                 klogctl(8, NULL, console_log_level);
82
83         syslog_msg(LOG_SYSLOG, LOG_NOTICE, "klogd started: " BB_BANNER);
84
85         while (1) {
86                 /* Use kernel syscalls */
87                 memset(log_buffer, '\0', sizeof(log_buffer));
88                 n = klogctl(2, log_buffer, sizeof(log_buffer));
89                 if (n < 0) {
90                         char message[80];
91
92                         if (errno == EINTR)
93                                 continue;
94                         snprintf(message, 79,
95                                          "klogd: Error return from sys_sycall: %d - %s.\n", errno,
96                                          strerror(errno));
97                         syslog_msg(LOG_SYSLOG, LOG_ERR, message);
98                         exit(1);
99                 }
100
101                 /* klogctl buffer parsing modelled after code in dmesg.c */
102                 start = &log_buffer[0];
103                 lastc = '\0';
104                 for (i = 0; i < n; i++) {
105                         if (lastc == '\0' && log_buffer[i] == '<') {
106                                 priority = 0;
107                                 i++;
108                                 while (isdigit(log_buffer[i])) {
109                                         priority = priority * 10 + (log_buffer[i] - '0');
110                                         i++;
111                                 }
112                                 if (log_buffer[i] == '>')
113                                         i++;
114                                 start = &log_buffer[i];
115                         }
116                         if (log_buffer[i] == '\n') {
117                                 log_buffer[i] = '\0';   /* zero terminate this message */
118                                 syslog_msg(LOG_KERN, priority, start);
119                                 start = &log_buffer[i + 1];
120                                 priority = LOG_INFO;
121                         }
122                         lastc = log_buffer[i];
123                 }
124         }
125 }
126
127 extern int klogd_main(int argc, char **argv)
128 {
129         /* no options, no getopt */
130         int opt;
131         int doFork = TRUE;
132         unsigned char console_log_level = 7;
133
134         /* do normal option parsing */
135         while ((opt = getopt(argc, argv, "c:n")) > 0) {
136                 switch (opt) {
137                 case 'c':
138                         if ((optarg == NULL) || (optarg[1] != '\0')) {
139                                 show_usage();
140                         }
141                         /* Valid levels are between 1 and 8 */
142                         console_log_level = *optarg - '1';
143                         if (console_log_level > 7) {
144                                 show_usage();
145                         }
146                         console_log_level++;
147                         
148                         break;
149                 case 'n':
150                         doFork = FALSE;
151                         break;
152                 default:
153                         show_usage();
154                 }
155         }
156
157         if (doFork) {
158 #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
159                 if (daemon(0, 1) < 0)
160                         perror_msg_and_die("daemon");
161 #else
162                 error_msg_and_die("daemon not supported");
163 #endif
164         }
165         doKlogd(console_log_level);
166
167         return EXIT_SUCCESS;
168 }
169
170 /*
171 Local Variables
172 c-file-style: "linux"
173 c-basic-offset: 4
174 tab-width: 4
175 End:
176 */