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