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