aboutsummaryrefslogtreecommitdiff
path: root/sysklogd/klogd.c
diff options
context:
space:
mode:
Diffstat (limited to 'sysklogd/klogd.c')
-rw-r--r--sysklogd/klogd.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c
new file mode 100644
index 000000000..f735d9fc1
--- /dev/null
+++ b/sysklogd/klogd.c
@@ -0,0 +1,123 @@
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
24static void klogd_signal(int sig ATTRIBUTE_UNUSED)
25{
26 klogctl(7, NULL, 0);
27 klogctl(0, 0, 0);
28 /* logMessage(0, "Kernel log daemon exiting."); */
29 syslog(LOG_NOTICE, "Kernel log daemon exiting.");
30 exit(EXIT_SUCCESS);
31}
32
33#define OPT_LEVEL 1
34#define OPT_FOREGROUND 2
35
36#define KLOGD_LOGBUF_SIZE 4096
37
38int klogd_main(int argc, char **argv)
39{
40 RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
41 int console_log_level = -1;
42 int priority = LOG_INFO;
43 int i, n, lastc;
44 char *start;
45
46 {
47 unsigned opt;
48
49 /* do normal option parsing */
50 opt = getopt32(argc, argv, "c:n", &start);
51
52 if (opt & OPT_LEVEL) {
53 /* Valid levels are between 1 and 8 */
54 console_log_level = xatoul_range(start, 1, 8);
55 }
56
57 if (!(opt & OPT_FOREGROUND)) {
58#ifdef BB_NOMMU
59 vfork_daemon_rexec(0, 1, argc, argv, "-n");
60#else
61 xdaemon(0, 1);
62#endif
63 }
64 }
65
66 openlog("kernel", 0, LOG_KERN);
67
68 /* Set up sig handlers */
69 signal(SIGINT, klogd_signal);
70 signal(SIGKILL, klogd_signal);
71 signal(SIGTERM, klogd_signal);
72 signal(SIGHUP, SIG_IGN);
73
74 /* "Open the log. Currently a NOP." */
75 klogctl(1, NULL, 0);
76
77 /* Set level of kernel console messaging.. */
78 if (console_log_level != -1)
79 klogctl(8, NULL, console_log_level);
80
81 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
82
83 while (1) {
84 /* Use kernel syscalls */
85 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
86 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE);
87 if (n < 0) {
88 if (errno == EINTR)
89 continue;
90 syslog(LOG_ERR, "klogd: Error from sys_sycall: %d - %m.\n",
91 errno);
92 exit(EXIT_FAILURE);
93 }
94
95 /* klogctl buffer parsing modelled after code in dmesg.c */
96 start = &log_buffer[0];
97 lastc = '\0';
98 for (i = 0; i < n; i++) {
99 if (lastc == '\0' && log_buffer[i] == '<') {
100 priority = 0;
101 i++;
102 while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
103 priority = priority * 10 + (log_buffer[i] - '0');
104 i++;
105 }
106 if (log_buffer[i] == '>')
107 i++;
108 start = &log_buffer[i];
109 }
110 if (log_buffer[i] == '\n') {
111 log_buffer[i] = '\0'; /* zero terminate this message */
112 syslog(priority, "%s", start);
113 start = &log_buffer[i + 1];
114 priority = LOG_INFO;
115 }
116 lastc = log_buffer[i];
117 }
118 }
119 if (ENABLE_FEATURE_CLEAN_UP)
120 RELEASE_CONFIG_BUFFER(log_buffer);
121
122 return EXIT_SUCCESS;
123}