aboutsummaryrefslogtreecommitdiff
path: root/busybox/sysklogd/klogd.c
diff options
context:
space:
mode:
Diffstat (limited to 'busybox/sysklogd/klogd.c')
-rw-r--r--busybox/sysklogd/klogd.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/busybox/sysklogd/klogd.c b/busybox/sysklogd/klogd.c
new file mode 100644
index 000000000..c908b593c
--- /dev/null
+++ b/busybox/sysklogd/klogd.c
@@ -0,0 +1,165 @@
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 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 *
31 */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <signal.h> /* for our signal() handlers */
36#include <string.h> /* strncpy() */
37#include <errno.h> /* errno and friends */
38#include <unistd.h>
39#include <ctype.h>
40#include <sys/syslog.h>
41#include <sys/klog.h>
42
43#include "busybox.h"
44
45static void klogd_signal(int sig)
46{
47 klogctl(7, NULL, 0);
48 klogctl(0, 0, 0);
49 /* logMessage(0, "Kernel log daemon exiting."); */
50 syslog(LOG_NOTICE, "Kernel log daemon exiting.");
51 exit(EXIT_SUCCESS);
52}
53
54static void doKlogd(const int console_log_level) __attribute__ ((noreturn));
55static void doKlogd(const int console_log_level)
56{
57 int priority = LOG_INFO;
58 char log_buffer[4096];
59 int i, n, lastc;
60 char *start;
61
62 openlog("kernel", 0, LOG_KERN);
63
64 /* Set up sig handlers */
65 signal(SIGINT, klogd_signal);
66 signal(SIGKILL, klogd_signal);
67 signal(SIGTERM, klogd_signal);
68 signal(SIGHUP, SIG_IGN);
69
70 /* "Open the log. Currently a NOP." */
71 klogctl(1, NULL, 0);
72
73 /* Set level of kernel console messaging.. */
74 if (console_log_level != -1)
75 klogctl(8, NULL, console_log_level);
76
77 syslog(LOG_NOTICE, "klogd started: " BB_BANNER);
78
79 while (1) {
80 /* Use kernel syscalls */
81 memset(log_buffer, '\0', sizeof(log_buffer));
82 n = klogctl(2, log_buffer, sizeof(log_buffer));
83 if (n < 0) {
84 if (errno == EINTR)
85 continue;
86 syslog(LOG_ERR, "klogd: Error return from sys_sycall: %d - %m.\n", errno);
87 exit(EXIT_FAILURE);
88 }
89
90 /* klogctl buffer parsing modelled after code in dmesg.c */
91 start = &log_buffer[0];
92 lastc = '\0';
93 for (i = 0; i < n; i++) {
94 if (lastc == '\0' && log_buffer[i] == '<') {
95 priority = 0;
96 i++;
97 while (isdigit(log_buffer[i])) {
98 priority = priority * 10 + (log_buffer[i] - '0');
99 i++;
100 }
101 if (log_buffer[i] == '>')
102 i++;
103 start = &log_buffer[i];
104 }
105 if (log_buffer[i] == '\n') {
106 log_buffer[i] = '\0'; /* zero terminate this message */
107 syslog(priority, "%s", start);
108 start = &log_buffer[i + 1];
109 priority = LOG_INFO;
110 }
111 lastc = log_buffer[i];
112 }
113 }
114}
115
116extern int klogd_main(int argc, char **argv)
117{
118 /* no options, no getopt */
119 int opt;
120 int doFork = TRUE;
121 unsigned char console_log_level = -1;
122
123 /* do normal option parsing */
124 while ((opt = getopt(argc, argv, "c:n")) > 0) {
125 switch (opt) {
126 case 'c':
127 if ((optarg == NULL) || (optarg[1] != '\0')) {
128 bb_show_usage();
129 }
130 /* Valid levels are between 1 and 8 */
131 console_log_level = *optarg - '1';
132 if (console_log_level > 7) {
133 bb_show_usage();
134 }
135 console_log_level++;
136
137 break;
138 case 'n':
139 doFork = FALSE;
140 break;
141 default:
142 bb_show_usage();
143 }
144 }
145
146 if (doFork) {
147#if defined(__uClinux__)
148 vfork_daemon_rexec(0, 1, argc, argv, "-n");
149#else /* __uClinux__ */
150 if (daemon(0, 1) < 0)
151 bb_perror_msg_and_die("daemon");
152#endif /* __uClinux__ */
153 }
154 doKlogd(console_log_level);
155
156 return EXIT_SUCCESS;
157}
158
159/*
160Local Variables
161c-file-style: "linux"
162c-basic-offset: 4
163tab-width: 4
164End:
165*/