- reinstate static for the command string arrray to be gentle to gcc-3.x
[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 "busybox.h"
21 #include <sys/syslog.h>
22 #include <sys/klog.h>
23
24 static void klogd_signal(int sig ATTRIBUTE_UNUSED)
25 {
26         klogctl(7, NULL, 0);
27         klogctl(0, 0, 0);
28         syslog(LOG_NOTICE, "Kernel log daemon exiting");
29         exit(EXIT_SUCCESS);
30 }
31
32 #define OPT_LEVEL        1
33 #define OPT_FOREGROUND   2
34
35 #define KLOGD_LOGBUF_SIZE 4096
36
37 int klogd_main(int argc, char **argv)
38 {
39         RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
40         int i = i; /* silence gcc */
41         char *start;
42
43         /* do normal option parsing */
44         getopt32(argc, argv, "c:n", &start);
45
46         if (option_mask32 & OPT_LEVEL) {
47                 /* Valid levels are between 1 and 8 */
48                 i = xatoul_range(start, 1, 8);
49         }
50
51         if (!(option_mask32 & OPT_FOREGROUND)) {
52 #ifdef BB_NOMMU
53                 vfork_daemon_rexec(0, 1, argc, argv, "-n");
54 #else
55                 xdaemon(0, 1);
56 #endif
57         }
58
59         openlog("kernel", 0, LOG_KERN);
60
61         /* Set up sig handlers */
62         signal(SIGINT, klogd_signal);
63         signal(SIGKILL, klogd_signal);
64         signal(SIGTERM, klogd_signal);
65         signal(SIGHUP, SIG_IGN);
66
67         /* "Open the log. Currently a NOP." */
68         klogctl(1, NULL, 0);
69
70         /* Set level of kernel console messaging.. */
71         if (option_mask32 & OPT_LEVEL)
72                 klogctl(8, NULL, i);
73
74         syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
75
76         while (1) {
77                 int n;
78                 int priority;
79                 char lastc;
80
81                 /* Use kernel syscalls */
82                 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
83                 /* It will be null-terminted */
84                 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
85                 if (n < 0) {
86                         if (errno == EINTR)
87                                 continue;
88                         syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
89                                    errno);
90                         break;
91                 }
92
93                 /* klogctl buffer parsing modelled after code in dmesg.c */
94                 start = &log_buffer[0];
95                 lastc = '\0';
96                 priority = LOG_INFO;
97                 for (i = 0; i < n; i++) {
98                         if (lastc == '\0' && log_buffer[i] == '<') {
99                                 i++;
100                                 // kernel never ganerates multi-digit prios
101                                 //priority = 0;
102                                 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
103                                 //      priority = priority * 10 + (log_buffer[i] - '0');
104                                 //      i++;
105                                 //}
106                                 if (isdigit(log_buffer[i])) {
107                                         priority = (log_buffer[i] - '0');
108                                         i++;
109                                 }
110                                 if (log_buffer[i] == '>')
111                                         i++;
112                                 start = &log_buffer[i];
113                         }
114                         if (log_buffer[i] == '\n') {
115                                 log_buffer[i] = '\0';   /* zero terminate this message */
116                                 syslog(priority, "%s", start);
117                                 start = &log_buffer[i + 1];
118                                 priority = LOG_INFO;
119                         }
120                         lastc = log_buffer[i];
121                 }
122         }
123         if (ENABLE_FEATURE_CLEAN_UP)
124                 RELEASE_CONFIG_BUFFER(log_buffer);
125
126         return EXIT_FAILURE;
127 }