Contents of /tags/mkinitrd-6_1_3/busybox/sysklogd/klogd.c
Parent Directory | Revision Log
Revision 846 -
(show annotations)
(download)
Mon May 4 18:51:23 2009 UTC (15 years, 4 months ago) by niro
File MIME type: text/plain
File size: 3232 byte(s)
Mon May 4 18:51:23 2009 UTC (15 years, 4 months ago) by niro
File MIME type: text/plain
File size: 3232 byte(s)
tagged 'mkinitrd-6_1_3'
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 *start; |
46 | int opt; |
47 | int used = 0; |
48 | |
49 | opt = getopt32(argv, "c:n", &start); |
50 | if (opt & OPT_LEVEL) { |
51 | /* Valid levels are between 1 and 8 */ |
52 | i = xatou_range(start, 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(0 |
61 | + (1 << SIGINT) |
62 | + (1 << SIGTERM) |
63 | , klogd_signal); |
64 | signal(SIGHUP, SIG_IGN); |
65 | |
66 | /* "Open the log. Currently a NOP" */ |
67 | klogctl(1, NULL, 0); |
68 | |
69 | /* "printk() prints a message on the console only if it has a loglevel |
70 | * less than console_loglevel". Here we set console_loglevel = i. */ |
71 | if (i) |
72 | klogctl(8, NULL, i); |
73 | |
74 | syslog(LOG_NOTICE, "klogd started: %s", bb_banner); |
75 | |
76 | while (1) { |
77 | int n; |
78 | int priority; |
79 | |
80 | /* "2 -- Read from the log." */ |
81 | n = klogctl(2, log_buffer + used, KLOGD_LOGBUF_SIZE-1 - used); |
82 | if (n < 0) { |
83 | if (errno == EINTR) |
84 | continue; |
85 | syslog(LOG_ERR, "klogd: error %d in klogctl(2): %m", |
86 | errno); |
87 | break; |
88 | } |
89 | log_buffer[used + n] = '\0'; |
90 | |
91 | /* klogctl buffer parsing modelled after code in dmesg.c */ |
92 | start = &log_buffer[0]; |
93 | |
94 | /* Process each newline-terminated line in the buffer */ |
95 | while (1) { |
96 | char *newline = strchr(start, '\n'); |
97 | |
98 | if (!newline) { |
99 | /* This line is incomplete... */ |
100 | if (start != log_buffer) { |
101 | /* move it to the front of the buffer */ |
102 | overlapping_strcpy(log_buffer, start); |
103 | /* don't log it yet */ |
104 | used = strlen(log_buffer); |
105 | break; |
106 | } |
107 | /* ...but buffer is full, so log it anyway */ |
108 | used = 0; |
109 | } else { |
110 | *newline++ = '\0'; |
111 | } |
112 | |
113 | /* Extract the priority */ |
114 | priority = LOG_INFO; |
115 | if (*start == '<') { |
116 | start++; |
117 | if (*start) { |
118 | /* kernel never generates multi-digit prios */ |
119 | priority = (*start - '0'); |
120 | start++; |
121 | } |
122 | if (*start == '>') { |
123 | start++; |
124 | } |
125 | } |
126 | if (*start) |
127 | syslog(priority, "%s", start); |
128 | if (!newline) |
129 | break; |
130 | start = newline; |
131 | } |
132 | } |
133 | |
134 | return EXIT_FAILURE; |
135 | } |