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