Enable more tools in config.maemo
[busybox4maemo] / 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 "libbb.h"
21 #include <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, NULL, 0);
28         syslog(LOG_NOTICE, "klogd: exiting");
29         kill_myself_with_sig(sig);
30 }
31
32 #define log_buffer bb_common_bufsiz1
33 enum {
34         KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
35         OPT_LEVEL      = (1 << 0),
36         OPT_FOREGROUND = (1 << 1),
37 };
38
39 int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
40 int klogd_main(int argc ATTRIBUTE_UNUSED, char **argv)
41 {
42         int i = i; /* silence gcc */
43         char *start;
44
45         /* do normal option parsing */
46         getopt32(argv, "c:n", &start);
47
48         if (option_mask32 & OPT_LEVEL) {
49                 /* Valid levels are between 1 and 8 */
50                 i = xatoul_range(start, 1, 8);
51         }
52
53         if (!(option_mask32 & OPT_FOREGROUND)) {
54                 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
55         }
56
57         openlog("kernel", 0, LOG_KERN);
58
59         /* Set up sig handlers */
60         bb_signals(0
61                 + (1 << SIGINT)
62                 + (1 << SIGTERM)
63                 , klogd_signal);
64         signal(SIGHUP, SIG_IGN);
65
66         /* "Open the log. Currently a NOP." */
67         klogctl(1, NULL, 0);
68
69         /* Set level of kernel console messaging. */
70         if (option_mask32 & OPT_LEVEL)
71                 klogctl(8, NULL, i);
72
73         syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
74
75         /* Note: this code does not detect incomplete messages
76          * (messages not ending with '\n' or just when kernel
77          * generates too many messages for us to keep up)
78          * and will split them in two separate lines */
79         while (1) {
80                 int n;
81                 int priority;
82
83                 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
84                 if (n < 0) {
85                         if (errno == EINTR)
86                                 continue;
87                         syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
88                                         errno);
89                         break;
90                 }
91                 log_buffer[n] = '\n';
92                 i = 0;
93                 while (i < n) {
94                         priority = LOG_INFO;
95                         start = &log_buffer[i];
96                         if (log_buffer[i] == '<') {
97                                 i++;
98                                 // kernel never ganerates multi-digit prios
99                                 //priority = 0;
100                                 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
101                                 //      priority = priority * 10 + (log_buffer[i] - '0');
102                                 //      i++;
103                                 //}
104                                 if (isdigit(log_buffer[i])) {
105                                         priority = (log_buffer[i] - '0');
106                                         i++;
107                                 }
108                                 if (log_buffer[i] == '>')
109                                         i++;
110                                 start = &log_buffer[i];
111                         }
112                         while (log_buffer[i] != '\n')
113                                 i++;
114                         log_buffer[i] = '\0';
115                         syslog(priority, "%s", start);
116                         i++;
117                 }
118         }
119
120         return EXIT_FAILURE;
121 }