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