Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/sysklogd/klogd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (show annotations) (download)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 3276 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
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)
25 {
26 /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
27 * via klogctl(8, NULL, 7). */
28 klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
29 klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
30 syslog(LOG_NOTICE, "klogd: exiting");
31 kill_myself_with_sig(sig);
32 }
33
34 #define log_buffer bb_common_bufsiz1
35 enum {
36 KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
37 OPT_LEVEL = (1 << 0),
38 OPT_FOREGROUND = (1 << 1),
39 };
40
41 int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int klogd_main(int argc UNUSED_PARAM, char **argv)
43 {
44 int i = 0;
45 char *opt_c;
46 int opt;
47 int used = 0;
48
49 opt = getopt32(argv, "c:n", &opt_c);
50 if (opt & OPT_LEVEL) {
51 /* Valid levels are between 1 and 8 */
52 i = xatou_range(opt_c, 1, 8);
53 }
54 if (!(opt & OPT_FOREGROUND)) {
55 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
56 }
57
58 openlog("kernel", 0, LOG_KERN);
59
60 bb_signals(BB_FATAL_SIGS, klogd_signal);
61 signal(SIGHUP, SIG_IGN);
62
63 /* "Open the log. Currently a NOP" */
64 klogctl(1, NULL, 0);
65
66 /* "printk() prints a message on the console only if it has a loglevel
67 * less than console_loglevel". Here we set console_loglevel = i. */
68 if (i)
69 klogctl(8, NULL, i);
70
71 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
72
73 while (1) {
74 int n;
75 int priority;
76 char *start;
77
78 /* "2 -- Read from the log." */
79 start = log_buffer + used;
80 n = klogctl(2, start, KLOGD_LOGBUF_SIZE-1 - used);
81 if (n < 0) {
82 if (errno == EINTR)
83 continue;
84 syslog(LOG_ERR, "klogd: error %d in klogctl(2): %m",
85 errno);
86 break;
87 }
88 start[n] = '\0';
89
90 /* klogctl buffer parsing modelled after code in dmesg.c */
91 /* Process each newline-terminated line in the buffer */
92 start = log_buffer;
93 while (1) {
94 char *newline = strchrnul(start, '\n');
95
96 if (*newline == '\0') {
97 /* This line is incomplete... */
98 if (start != log_buffer) {
99 /* move it to the front of the buffer */
100 overlapping_strcpy(log_buffer, start);
101 used = newline - start;
102 /* don't log it yet */
103 break;
104 }
105 /* ...but if buffer is full, log it anyway */
106 used = 0;
107 newline = NULL;
108 } else {
109 *newline++ = '\0';
110 }
111
112 /* Extract the priority */
113 priority = LOG_INFO;
114 if (*start == '<') {
115 start++;
116 if (*start) {
117 /* kernel never generates multi-digit prios */
118 priority = (*start - '0');
119 start++;
120 }
121 if (*start == '>')
122 start++;
123 }
124 /* Log (only non-empty lines) */
125 if (*start)
126 syslog(priority, "%s", start);
127
128 if (!newline)
129 break;
130 start = newline;
131 }
132 }
133
134 return EXIT_FAILURE;
135 }