aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-11-19 07:59:49 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-11-19 07:59:49 +0000
commit2e7dc5d37bb3b10c486496fa15b8ff366d8a6d66 (patch)
tree6c0cbc8bba8335ceb4db5307e617bdda1f1d3cf3
parenta09a42cd83a0365b5b52dfdcce20b3c9932be801 (diff)
downloadbusybox-w32-2e7dc5d37bb3b10c486496fa15b8ff366d8a6d66.tar.gz
busybox-w32-2e7dc5d37bb3b10c486496fa15b8ff366d8a6d66.tar.bz2
busybox-w32-2e7dc5d37bb3b10c486496fa15b8ff366d8a6d66.zip
klogctl: fix a problem where we don't terminate read data with '\0'
and then misinterpret it. Code shrink while at it. function old new delta klogd_main 404 362 -42
-rw-r--r--sysklogd/klogd.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c
index 723ca8067..a0e0bf724 100644
--- a/sysklogd/klogd.c
+++ b/sysklogd/klogd.c
@@ -42,14 +42,14 @@ int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42int klogd_main(int argc UNUSED_PARAM, char **argv) 42int klogd_main(int argc UNUSED_PARAM, char **argv)
43{ 43{
44 int i = 0; 44 int i = 0;
45 char *start; 45 char *opt_c;
46 int opt; 46 int opt;
47 int used = 0; 47 int used = 0;
48 48
49 opt = getopt32(argv, "c:n", &start); 49 opt = getopt32(argv, "c:n", &opt_c);
50 if (opt & OPT_LEVEL) { 50 if (opt & OPT_LEVEL) {
51 /* Valid levels are between 1 and 8 */ 51 /* Valid levels are between 1 and 8 */
52 i = xatou_range(start, 1, 8); 52 i = xatou_range(opt_c, 1, 8);
53 } 53 }
54 if (!(opt & OPT_FOREGROUND)) { 54 if (!(opt & OPT_FOREGROUND)) {
55 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); 55 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
@@ -57,10 +57,7 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
57 57
58 openlog("kernel", 0, LOG_KERN); 58 openlog("kernel", 0, LOG_KERN);
59 59
60 bb_signals(0 60 bb_signals(BB_FATAL_SIGS, klogd_signal);
61 + (1 << SIGINT)
62 + (1 << SIGTERM)
63 , klogd_signal);
64 signal(SIGHUP, SIG_IGN); 61 signal(SIGHUP, SIG_IGN);
65 62
66 /* "Open the log. Currently a NOP" */ 63 /* "Open the log. Currently a NOP" */
@@ -73,15 +70,14 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
73 70
74 syslog(LOG_NOTICE, "klogd started: %s", bb_banner); 71 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
75 72
76 /* Initially null terminate the buffer in case of a very long line */
77 log_buffer[KLOGD_LOGBUF_SIZE - 1] = '\0';
78
79 while (1) { 73 while (1) {
80 int n; 74 int n;
81 int priority; 75 int priority;
76 char *start;
82 77
83 /* "2 -- Read from the log." */ 78 /* "2 -- Read from the log." */
84 n = klogctl(2, log_buffer + used, KLOGD_LOGBUF_SIZE-1 - used); 79 start = log_buffer + used;
80 n = klogctl(2, start, KLOGD_LOGBUF_SIZE-1 - used);
85 if (n < 0) { 81 if (n < 0) {
86 if (errno == EINTR) 82 if (errno == EINTR)
87 continue; 83 continue;
@@ -89,25 +85,25 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
89 errno); 85 errno);
90 break; 86 break;
91 } 87 }
88 start[n] = '\0';
92 89
93 /* klogctl buffer parsing modelled after code in dmesg.c */ 90 /* klogctl buffer parsing modelled after code in dmesg.c */
94 start = &log_buffer[0];
95
96 /* Process each newline-terminated line in the buffer */ 91 /* Process each newline-terminated line in the buffer */
97 while (1) { 92 while (1) {
98 char *newline = strchr(start, '\n'); 93 char *newline = strchrnul(start, '\n');
99 94
100 if (!newline) { 95 if (*newline == '\0') {
101 /* This line is incomplete... */ 96 /* This line is incomplete... */
102 if (start != log_buffer) { 97 if (start != log_buffer) {
103 /* move it to the front of the buffer */ 98 /* move it to the front of the buffer */
104 strcpy(log_buffer, start); 99 overlapping_strcpy(log_buffer, start);
100 used = newline - start;
105 /* don't log it yet */ 101 /* don't log it yet */
106 used = strlen(log_buffer);
107 break; 102 break;
108 } 103 }
109 /* ...but buffer is full, so log it anyway */ 104 /* ...but if buffer is full, log it anyway */
110 used = 0; 105 used = 0;
106 newline = NULL;
111 } else { 107 } else {
112 *newline++ = '\0'; 108 *newline++ = '\0';
113 } 109 }
@@ -121,12 +117,13 @@ int klogd_main(int argc UNUSED_PARAM, char **argv)
121 priority = (*start - '0'); 117 priority = (*start - '0');
122 start++; 118 start++;
123 } 119 }
124 if (*start == '>') { 120 if (*start == '>')
125 start++; 121 start++;
126 }
127 } 122 }
123 /* Log (only non-empty lines) */
128 if (*start) 124 if (*start)
129 syslog(priority, "%s", start); 125 syslog(priority, "%s", start);
126
130 if (!newline) 127 if (!newline)
131 break; 128 break;
132 start = newline; 129 start = newline;