- improve wording
[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 int klogd_main(int argc, char **argv)
39 {
40         RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
41         int i = i; /* silence gcc */
42         char *start;
43
44         /* do normal option parsing */
45         getopt32(argc, argv, "c:n", &start);
46
47         if (option_mask32 & OPT_LEVEL) {
48                 /* Valid levels are between 1 and 8 */
49                 i = xatoul_range(start, 1, 8);
50         }
51
52         if (!(option_mask32 & OPT_FOREGROUND)) {
53 #ifdef BB_NOMMU
54                 vfork_daemon_rexec(0, 1, argc, argv, "-n");
55 #else
56                 xdaemon(0, 1);
57 #endif
58         }
59
60         openlog("kernel", 0, LOG_KERN);
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 (option_mask32 & OPT_LEVEL)
73                 klogctl(8, NULL, i);
74
75         syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
76
77         while (1) {
78                 int n;
79                 int priority;
80                 char lastc;
81
82                 /* Use kernel syscalls */
83                 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
84                 /* It will be null-terminted */
85                 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
86                 if (n < 0) {
87                         if (errno == EINTR)
88                                 continue;
89                         syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
90                                    errno);
91                         break;
92                 }
93
94                 /* klogctl buffer parsing modelled after code in dmesg.c */
95                 start = &log_buffer[0];
96                 lastc = '\0';
97                 priority = LOG_INFO;
98                 for (i = 0; i < n; i++) {
99                         if (lastc == '\0' && log_buffer[i] == '<') {
100                                 i++;
101                                 // kernel never ganerates multi-digit prios
102                                 //priority = 0;
103                                 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
104                                 //      priority = priority * 10 + (log_buffer[i] - '0');
105                                 //      i++;
106                                 //}
107                                 if (isdigit(log_buffer[i])) {
108                                         priority = (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(priority, "%s", start);
118                                 start = &log_buffer[i + 1];
119                                 priority = LOG_INFO;
120                         }
121                         lastc = log_buffer[i];
122                 }
123         }
124         if (ENABLE_FEATURE_CLEAN_UP)
125                 RELEASE_CONFIG_BUFFER(log_buffer);
126
127         return EXIT_FAILURE;
128 }