Patch from Rob Landley, Simplify organisation of arguments.
[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-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 #include <sys/klog.h>
42
43 #include "busybox.h"
44
45 static void klogd_signal(int sig)
46 {
47         klogctl(7, NULL, 0);
48         klogctl(0, 0, 0);
49         /* logMessage(0, "Kernel log daemon exiting."); */
50         syslog_msg(LOG_SYSLOG, LOG_NOTICE, "Kernel log daemon exiting.");
51         exit(TRUE);
52 }
53
54 static void doKlogd(const char console_log_level) __attribute__ ((noreturn));
55 static void doKlogd(const char console_log_level)
56 {
57         int priority = LOG_INFO;
58         char log_buffer[4096];
59         int i, n, lastc;
60         char *start;
61
62         /* Set up sig handlers */
63         signal(SIGINT, klogd_signal);
64         signal(SIGKILL, klogd_signal);
65         signal(SIGTERM, klogd_signal);
66         signal(SIGHUP, SIG_IGN);
67
68         /* "Open the log. Currently a NOP." */
69         klogctl(1, NULL, 0);
70
71         /* Set level of kernel console messaging.. */
72         if (console_log_level)
73                 klogctl(8, NULL, console_log_level);
74
75         syslog_msg(LOG_SYSLOG, LOG_NOTICE, "klogd started: " BB_BANNER);
76
77         while (1) {
78                 /* Use kernel syscalls */
79                 memset(log_buffer, '\0', sizeof(log_buffer));
80                 n = klogctl(2, log_buffer, sizeof(log_buffer));
81                 if (n < 0) {
82                         char message[80];
83
84                         if (errno == EINTR)
85                                 continue;
86                         snprintf(message, 79,
87                                          "klogd: Error return from sys_sycall: %d - %s.\n", errno,
88                                          strerror(errno));
89                         syslog_msg(LOG_SYSLOG, LOG_ERR, message);
90                         exit(1);
91                 }
92
93                 /* klogctl buffer parsing modelled after code in dmesg.c */
94                 start = &log_buffer[0];
95                 lastc = '\0';
96                 for (i = 0; i < n; i++) {
97                         if (lastc == '\0' && log_buffer[i] == '<') {
98                                 priority = 0;
99                                 i++;
100                                 while (isdigit(log_buffer[i])) {
101                                         priority = priority * 10 + (log_buffer[i] - '0');
102                                         i++;
103                                 }
104                                 if (log_buffer[i] == '>')
105                                         i++;
106                                 start = &log_buffer[i];
107                         }
108                         if (log_buffer[i] == '\n') {
109                                 log_buffer[i] = '\0';   /* zero terminate this message */
110                                 syslog_msg(LOG_KERN, priority, start);
111                                 start = &log_buffer[i + 1];
112                                 priority = LOG_INFO;
113                         }
114                         lastc = log_buffer[i];
115                 }
116         }
117 }
118
119 extern int klogd_main(int argc, char **argv)
120 {
121         /* no options, no getopt */
122         int opt;
123         int doFork = TRUE;
124         unsigned char console_log_level = 7;
125
126         /* do normal option parsing */
127         while ((opt = getopt(argc, argv, "c:n")) > 0) {
128                 switch (opt) {
129                 case 'c':
130                         if ((optarg == NULL) || (optarg[1] != '\0')) {
131                                 bb_show_usage();
132                         }
133                         /* Valid levels are between 1 and 8 */
134                         console_log_level = *optarg - '1';
135                         if (console_log_level > 7) {
136                                 bb_show_usage();
137                         }
138                         console_log_level++;
139                         
140                         break;
141                 case 'n':
142                         doFork = FALSE;
143                         break;
144                 default:
145                         bb_show_usage();
146                 }
147         }
148
149         if (doFork) {
150                 if (daemon(0, 1) < 0)
151                         bb_perror_msg_and_die("daemon");
152 #if defined(__uClinux__)
153                 vfork_daemon_rexec(argc, argv, "-n");
154 #endif
155         }
156         doKlogd(console_log_level);
157
158         return EXIT_SUCCESS;
159 }
160
161 /*
162 Local Variables
163 c-file-style: "linux"
164 c-basic-offset: 4
165 tab-width: 4
166 End:
167 */