blob: 2a3f52f995d62f4a8908cf521a57da7715950b9e [file] [log] [blame]
Mark Salyzynae4d9282014-10-15 08:49:39 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
18#include <errno.h>
19#include <inttypes.h>
20#include <limits.h>
21#include <stdarg.h>
22#include <stdlib.h>
23#include <sys/prctl.h>
24#include <sys/uio.h>
25#include <syslog.h>
26
27#include <log/logger.h>
28
29#include "LogKlog.h"
30
31#define KMSG_PRIORITY(PRI) \
32 '<', \
33 '0' + (LOG_SYSLOG | (PRI)) / 10, \
34 '0' + (LOG_SYSLOG | (PRI)) % 10, \
35 '>'
36
37static const char priority_message[] = { KMSG_PRIORITY(LOG_INFO), '\0' };
38
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070039// Parsing is hard
40
41// called if we see a '<', s is the next character, returns pointer after '>'
Mark Salyzynea1a2412015-09-02 07:39:53 -070042static char *is_prio(char *s, size_t len) {
43 if (!len || !isdigit(*s++)) {
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070044 return NULL;
45 }
Mark Salyzynea1a2412015-09-02 07:39:53 -070046 --len;
47 static const size_t max_prio_len = (len < 4) ? len : 4;
48 size_t priolen = 0;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070049 char c;
Mark Salyzynea1a2412015-09-02 07:39:53 -070050 while (((c = *s++)) && (++priolen <= max_prio_len)) {
Mark Salyzynee49c6a2015-06-12 14:59:42 -070051 if (!isdigit(c)) {
Mark Salyzyn46d159d2015-08-26 09:30:00 -070052 return ((c == '>') && (*s == '[')) ? s : NULL;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070053 }
54 }
55 return NULL;
56}
57
58// called if we see a '[', s is the next character, returns pointer after ']'
Mark Salyzynea1a2412015-09-02 07:39:53 -070059static char *is_timestamp(char *s, size_t len) {
60 while (len && (*s == ' ')) {
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070061 ++s;
Mark Salyzynea1a2412015-09-02 07:39:53 -070062 --len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070063 }
Mark Salyzynea1a2412015-09-02 07:39:53 -070064 if (!len || !isdigit(*s++)) {
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070065 return NULL;
66 }
Mark Salyzynea1a2412015-09-02 07:39:53 -070067 --len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070068 bool first_period = true;
69 char c;
Mark Salyzynea1a2412015-09-02 07:39:53 -070070 while (len && ((c = *s++))) {
71 --len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070072 if ((c == '.') && first_period) {
73 first_period = false;
Mark Salyzynee49c6a2015-06-12 14:59:42 -070074 } else if (!isdigit(c)) {
75 return ((c == ']') && !first_period && (*s == ' ')) ? s : NULL;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070076 }
77 }
78 return NULL;
79}
80
81// Like strtok_r with "\r\n" except that we look for log signatures (regex)
Mark Salyzyn618d0de2015-07-13 10:19:34 -070082// \(\(<[0-9]\{1,4\}>\)\([[] *[0-9]+[.][0-9]+[]] \)\{0,1\}\|[[] *[0-9]+[.][0-9]+[]] \)
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070083// and split if we see a second one without a newline.
Mark Salyzyn151beac2015-09-04 11:37:42 -070084// We allow nuls in content, monitoring the overall length and sub-length of
85// the discovered tokens.
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070086
87#define SIGNATURE_MASK 0xF0
88// <digit> following ('0' to '9' masked with ~SIGNATURE_MASK) added to signature
89#define LESS_THAN_SIG SIGNATURE_MASK
90#define OPEN_BRACKET_SIG ((SIGNATURE_MASK << 1) & SIGNATURE_MASK)
91// space is one more than <digit> of 9
Mark Salyzyn3e21de22015-06-08 14:51:30 -070092#define OPEN_BRACKET_SPACE ((char)(OPEN_BRACKET_SIG | 10))
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070093
Mark Salyzynea1a2412015-09-02 07:39:53 -070094char *log_strntok_r(char *s, size_t *len, char **last, size_t *sublen) {
95 *sublen = 0;
96 if (!*len) {
97 return NULL;
98 }
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070099 if (!s) {
100 if (!(s = *last)) {
101 return NULL;
102 }
103 // fixup for log signature split <,
104 // LESS_THAN_SIG + <digit>
105 if ((*s & SIGNATURE_MASK) == LESS_THAN_SIG) {
106 *s = (*s & ~SIGNATURE_MASK) + '0';
107 *--s = '<';
Mark Salyzynea1a2412015-09-02 07:39:53 -0700108 ++*len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700109 }
110 // fixup for log signature split [,
111 // OPEN_BRACKET_SPACE is space, OPEN_BRACKET_SIG + <digit>
112 if ((*s & SIGNATURE_MASK) == OPEN_BRACKET_SIG) {
113 if (*s == OPEN_BRACKET_SPACE) {
114 *s = ' ';
115 } else {
116 *s = (*s & ~SIGNATURE_MASK) + '0';
117 }
118 *--s = '[';
Mark Salyzynea1a2412015-09-02 07:39:53 -0700119 ++*len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700120 }
121 }
122
Mark Salyzynea1a2412015-09-02 07:39:53 -0700123 while (*len && ((*s == '\r') || (*s == '\n'))) {
124 ++s;
125 --*len;
126 }
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700127
Mark Salyzynea1a2412015-09-02 07:39:53 -0700128 if (!*len) {
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700129 *last = NULL;
130 return NULL;
131 }
132 char *peek, *tok = s;
133
134 for (;;) {
Mark Salyzynea1a2412015-09-02 07:39:53 -0700135 if (*len == 0) {
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700136 *last = NULL;
137 return tok;
Mark Salyzynea1a2412015-09-02 07:39:53 -0700138 }
139 char c = *s++;
140 --*len;
141 size_t adjust;
142 switch (c) {
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700143 case '\r':
144 case '\n':
145 s[-1] = '\0';
146 *last = s;
147 return tok;
148
149 case '<':
Mark Salyzynea1a2412015-09-02 07:39:53 -0700150 peek = is_prio(s, *len);
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700151 if (!peek) {
152 break;
153 }
154 if (s != (tok + 1)) { // not first?
155 s[-1] = '\0';
156 *s &= ~SIGNATURE_MASK;
157 *s |= LESS_THAN_SIG; // signature for '<'
158 *last = s;
159 return tok;
160 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700161 adjust = peek - s;
162 if (adjust > *len) {
163 adjust = *len;
164 }
165 *sublen += adjust;
166 *len -= adjust;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700167 s = peek;
Mark Salyzynea1a2412015-09-02 07:39:53 -0700168 if ((*s == '[') && ((peek = is_timestamp(s + 1, *len - 1)))) {
169 adjust = peek - s;
170 if (adjust > *len) {
171 adjust = *len;
172 }
173 *sublen += adjust;
174 *len -= adjust;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700175 s = peek;
176 }
177 break;
178
179 case '[':
Mark Salyzynea1a2412015-09-02 07:39:53 -0700180 peek = is_timestamp(s, *len);
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700181 if (!peek) {
182 break;
183 }
184 if (s != (tok + 1)) { // not first?
185 s[-1] = '\0';
186 if (*s == ' ') {
187 *s = OPEN_BRACKET_SPACE;
188 } else {
189 *s &= ~SIGNATURE_MASK;
190 *s |= OPEN_BRACKET_SIG; // signature for '['
191 }
192 *last = s;
193 return tok;
194 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700195 adjust = peek - s;
196 if (adjust > *len) {
197 adjust = *len;
198 }
199 *sublen += adjust;
200 *len -= adjust;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700201 s = peek;
202 break;
203 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700204 ++*sublen;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700205 }
Mark Salyzyn618d0de2015-07-13 10:19:34 -0700206 // NOTREACHED
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700207}
208
Mark Salyzynae4d9282014-10-15 08:49:39 -0700209log_time LogKlog::correction = log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC);
210
Mark Salyzyn77187782015-05-12 15:21:31 -0700211LogKlog::LogKlog(LogBuffer *buf, LogReader *reader, int fdWrite, int fdRead, bool auditd) :
212 SocketListener(fdRead, false),
213 logbuf(buf),
214 reader(reader),
215 signature(CLOCK_MONOTONIC),
Mark Salyzyn77187782015-05-12 15:21:31 -0700216 initialized(false),
217 enableLogging(true),
218 auditd(auditd) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700219 static const char klogd_message[] = "%slogd.klogd: %" PRIu64 "\n";
220 char buffer[sizeof(priority_message) + sizeof(klogd_message) + 20 - 4];
221 snprintf(buffer, sizeof(buffer), klogd_message, priority_message,
222 signature.nsec());
223 write(fdWrite, buffer, strlen(buffer));
224}
225
226bool LogKlog::onDataAvailable(SocketClient *cli) {
227 if (!initialized) {
228 prctl(PR_SET_NAME, "logd.klogd");
229 initialized = true;
230 enableLogging = false;
231 }
232
233 char buffer[LOGGER_ENTRY_MAX_PAYLOAD];
234 size_t len = 0;
235
236 for(;;) {
237 ssize_t retval = 0;
238 if ((sizeof(buffer) - 1 - len) > 0) {
239 retval = read(cli->getSocket(), buffer + len, sizeof(buffer) - 1 - len);
240 }
241 if ((retval == 0) && (len == 0)) {
242 break;
243 }
244 if (retval < 0) {
245 return false;
246 }
247 len += retval;
248 bool full = len == (sizeof(buffer) - 1);
249 char *ep = buffer + len;
250 *ep = '\0';
Mark Salyzynea1a2412015-09-02 07:39:53 -0700251 size_t sublen;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700252 for(char *ptr = NULL, *tok = buffer;
Mark Salyzynea1a2412015-09-02 07:39:53 -0700253 ((tok = log_strntok_r(tok, &len, &ptr, &sublen)));
Mark Salyzynae4d9282014-10-15 08:49:39 -0700254 tok = NULL) {
Mark Salyzynea1a2412015-09-02 07:39:53 -0700255 if (((tok + sublen) >= ep) && (retval != 0) && full) {
256 memmove(buffer, tok, sublen);
257 len = sublen;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700258 break;
259 }
260 if (*tok) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700261 log(tok, sublen);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700262 }
263 }
264 }
265
266 return true;
267}
268
269
270void LogKlog::calculateCorrection(const log_time &monotonic,
Mark Salyzyn151beac2015-09-04 11:37:42 -0700271 const char *real_string,
272 size_t len) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700273 log_time real;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700274 const char *ep = real.strptime(real_string, "%Y-%m-%d %H:%M:%S.%09q UTC");
275 if (!ep || (ep > &real_string[len])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700276 return;
277 }
278 // kernel report UTC, log_time::strptime is localtime from calendar.
279 // Bionic and liblog strptime does not support %z or %Z to pick up
280 // timezone so we are calculating our own correction.
281 time_t now = real.tv_sec;
282 struct tm tm;
283 memset(&tm, 0, sizeof(tm));
284 tm.tm_isdst = -1;
285 localtime_r(&now, &tm);
286 real.tv_sec += tm.tm_gmtoff;
287 correction = real - monotonic;
288}
289
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700290static const char suspendStr[] = "PM: suspend entry ";
291static const char resumeStr[] = "PM: suspend exit ";
292static const char suspendedStr[] = "Suspended for ";
293
Mark Salyzyn151beac2015-09-04 11:37:42 -0700294static const char *strnstr(const char *s, size_t len, const char *needle) {
295 char c;
296
297 if (!len) {
298 return NULL;
299 }
300 if ((c = *needle++) != 0) {
301 size_t needleLen = strlen(needle);
302 do {
303 do {
304 if (len <= needleLen) {
305 return NULL;
306 }
307 --len;
308 } while (*s++ != c);
Mark Salyzynddda2122015-10-02 09:22:52 -0700309 } while (fast<memcmp>(s, needle, needleLen));
Mark Salyzyn151beac2015-09-04 11:37:42 -0700310 s--;
311 }
312 return s;
313}
314
315void LogKlog::sniffTime(log_time &now,
316 const char **buf, size_t len,
317 bool reverse) {
318 const char *cp = now.strptime(*buf, "[ %s.%q]");
319 if (cp && (cp >= &(*buf)[len])) {
320 cp = NULL;
321 }
322 len -= cp - *buf;
323 if (cp) {
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700324 static const char healthd[] = "healthd";
325 static const char battery[] = ": battery ";
Mark Salyzynae4d9282014-10-15 08:49:39 -0700326
Mark Salyzyn151beac2015-09-04 11:37:42 -0700327 if (len && isspace(*cp)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700328 ++cp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700329 --len;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700330 }
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700331 *buf = cp;
332
Mark Salyzynb6bee332015-09-08 08:56:32 -0700333 if (isMonotonic()) {
334 return;
335 }
336
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700337 const char *b;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700338 if (((b = strnstr(cp, len, suspendStr)))
339 && ((size_t)((b += sizeof(suspendStr) - 1) - cp) < len)) {
340 len -= b - cp;
341 calculateCorrection(now, b, len);
342 } else if (((b = strnstr(cp, len, resumeStr)))
343 && ((size_t)((b += sizeof(resumeStr) - 1) - cp) < len)) {
344 len -= b - cp;
345 calculateCorrection(now, b, len);
346 } else if (((b = strnstr(cp, len, healthd)))
347 && ((size_t)((b += sizeof(healthd) - 1) - cp) < len)
348 && ((b = strnstr(b, len -= b - cp, battery)))
349 && ((size_t)((b += sizeof(battery) - 1) - cp) < len)) {
350 len -= b - cp;
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700351 // NB: healthd is roughly 150us late, worth the price to deal with
352 // ntp-induced or hardware clock drift.
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700353 // look for " 2???-??-?? ??:??:??.????????? ???"
Mark Salyzyn151beac2015-09-04 11:37:42 -0700354 for (; len && *b && (*b != '\n'); ++b, --len) {
355 if ((b[0] == ' ') && (b[1] == '2') && (b[5] == '-')) {
356 calculateCorrection(now, b + 1, len - 1);
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700357 break;
358 }
359 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700360 } else if (((b = strnstr(cp, len, suspendedStr)))
361 && ((size_t)((b += sizeof(suspendStr) - 1) - cp) < len)) {
362 len -= b - cp;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700363 log_time real;
364 char *endp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700365 real.tv_sec = strtol(b, &endp, 10);
366 if ((*endp == '.') && ((size_t)(endp - b) < len)) {
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700367 unsigned long multiplier = NS_PER_SEC;
368 real.tv_nsec = 0;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700369 len -= endp - b;
370 while (--len && isdigit(*++endp) && (multiplier /= 10)) {
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700371 real.tv_nsec += (*endp - '0') * multiplier;
372 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700373 if (reverse) {
374 correction -= real;
375 } else {
376 correction += real;
377 }
378 }
379 }
380
381 convertMonotonicToReal(now);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700382 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700383 if (isMonotonic()) {
384 now = log_time(CLOCK_MONOTONIC);
385 } else {
386 now = log_time(CLOCK_REALTIME);
387 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700388 }
389}
390
Mark Salyzyn151beac2015-09-04 11:37:42 -0700391pid_t LogKlog::sniffPid(const char *cp, size_t len) {
392 while (len) {
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700393 // Mediatek kernels with modified printk
394 if (*cp == '[') {
395 int pid = 0;
396 char dummy;
397 if (sscanf(cp, "[%d:%*[a-z_./0-9:A-Z]]%c", &pid, &dummy) == 2) {
398 return pid;
399 }
400 break; // Only the first one
401 }
402 ++cp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700403 --len;
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700404 }
405 return 0;
406}
407
Mark Salyzynae4d9282014-10-15 08:49:39 -0700408// kernel log prefix, convert to a kernel log priority number
Mark Salyzyn151beac2015-09-04 11:37:42 -0700409static int parseKernelPrio(const char **buf, size_t len) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700410 int pri = LOG_USER | LOG_INFO;
411 const char *cp = *buf;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700412 if (len && (*cp == '<')) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700413 pri = 0;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700414 while(--len && isdigit(*++cp)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700415 pri = (pri * 10) + *cp - '0';
416 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700417 if (len && (*cp == '>')) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700418 ++cp;
419 } else {
420 cp = *buf;
421 pri = LOG_USER | LOG_INFO;
422 }
423 *buf = cp;
424 }
425 return pri;
426}
427
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700428// Passed the entire SYSLOG_ACTION_READ_ALL buffer and interpret a
429// compensated start time.
Mark Salyzyn151beac2015-09-04 11:37:42 -0700430void LogKlog::synchronize(const char *buf, size_t len) {
431 const char *cp = strnstr(buf, len, suspendStr);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700432 if (!cp) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700433 cp = strnstr(buf, len, resumeStr);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700434 if (!cp) {
435 return;
436 }
437 } else {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700438 const char *rp = strnstr(buf, len, resumeStr);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700439 if (rp && (rp < cp)) {
440 cp = rp;
441 }
442 }
443
444 do {
445 --cp;
446 } while ((cp > buf) && (*cp != '\n'));
447 if (*cp == '\n') {
448 ++cp;
449 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700450 parseKernelPrio(&cp, len - (cp - buf));
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700451
452 log_time now;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700453 sniffTime(now, &cp, len - (cp - buf), true);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700454
Mark Salyzyn151beac2015-09-04 11:37:42 -0700455 const char *suspended = strnstr(buf, len, suspendedStr);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700456 if (!suspended || (suspended > cp)) {
457 return;
458 }
459 cp = suspended;
460
461 do {
462 --cp;
463 } while ((cp > buf) && (*cp != '\n'));
464 if (*cp == '\n') {
465 ++cp;
466 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700467 parseKernelPrio(&cp, len - (cp - buf));
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700468
Mark Salyzyn151beac2015-09-04 11:37:42 -0700469 sniffTime(now, &cp, len - (cp - buf), true);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700470}
471
Mark Salyzynae4d9282014-10-15 08:49:39 -0700472// Convert kernel log priority number into an Android Logger priority number
473static int convertKernelPrioToAndroidPrio(int pri) {
474 switch(pri & LOG_PRIMASK) {
475 case LOG_EMERG:
476 // FALLTHRU
477 case LOG_ALERT:
478 // FALLTHRU
479 case LOG_CRIT:
480 return ANDROID_LOG_FATAL;
481
482 case LOG_ERR:
483 return ANDROID_LOG_ERROR;
484
485 case LOG_WARNING:
486 return ANDROID_LOG_WARN;
487
488 default:
489 // FALLTHRU
490 case LOG_NOTICE:
491 // FALLTHRU
492 case LOG_INFO:
493 break;
494
495 case LOG_DEBUG:
496 return ANDROID_LOG_DEBUG;
497 }
498
499 return ANDROID_LOG_INFO;
500}
501
Mark Salyzyn47dba712015-08-26 09:30:00 -0700502static const char *strnrchr(const char *s, size_t len, char c) {
503 const char *save = NULL;
504 for (;len; ++s, len--) {
505 if (*s == c) {
506 save = s;
507 }
508 }
509 return save;
510}
511
Mark Salyzynae4d9282014-10-15 08:49:39 -0700512//
513// log a message into the kernel log buffer
514//
515// Filter rules to parse <PRI> <TIME> <tag> and <message> in order for
516// them to appear correct in the logcat output:
517//
518// LOG_KERN (0):
519// <PRI>[<TIME>] <tag> ":" <message>
520// <PRI>[<TIME>] <tag> <tag> ":" <message>
521// <PRI>[<TIME>] <tag> <tag>_work ":" <message>
522// <PRI>[<TIME>] <tag> '<tag>.<num>' ":" <message>
523// <PRI>[<TIME>] <tag> '<tag><num>' ":" <message>
524// <PRI>[<TIME>] <tag>_host '<tag>.<num>' ":" <message>
525// (unimplemented) <PRI>[<TIME>] <tag> '<num>.<tag>' ":" <message>
526// <PRI>[<TIME>] "[INFO]"<tag> : <message>
527// <PRI>[<TIME>] "------------[ cut here ]------------" (?)
528// <PRI>[<TIME>] "---[ end trace 3225a3070ca3e4ac ]---" (?)
529// LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS
530// LOG_UUCP, LOG_CRON, LOG_AUTHPRIV, LOG_FTP:
531// <PRI+TAG>[<TIME>] (see sys/syslog.h)
532// Observe:
533// Minimum tag length = 3 NB: drops things like r5:c00bbadf, but allow PM:
534// Maximum tag words = 2
535// Maximum tag length = 16 NB: we are thinking of how ugly logcat can get.
536// Not a Tag if there is no message content.
537// leading additional spaces means no tag, inherit last tag.
538// Not a Tag if <tag>: is "ERROR:", "WARNING:", "INFO:" or "CPU:"
539// Drop:
540// empty messages
541// messages with ' audit(' in them if auditd is running
542// logd.klogd:
543// return -1 if message logd.klogd: <signature>
544//
Mark Salyzyn151beac2015-09-04 11:37:42 -0700545int LogKlog::log(const char *buf, size_t len) {
546 if (auditd && strnstr(buf, len, " audit(")) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700547 return 0;
548 }
549
Mark Salyzyn151beac2015-09-04 11:37:42 -0700550 const char *p = buf;
551 int pri = parseKernelPrio(&p, len);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700552
553 log_time now;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700554 sniffTime(now, &p, len - (p - buf), false);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700555
556 // sniff for start marker
557 const char klogd_message[] = "logd.klogd: ";
Mark Salyzyn151beac2015-09-04 11:37:42 -0700558 const char *start = strnstr(p, len - (p - buf), klogd_message);
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700559 if (start) {
560 uint64_t sig = strtoll(start + sizeof(klogd_message) - 1, NULL, 10);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700561 if (sig == signature.nsec()) {
562 if (initialized) {
563 enableLogging = true;
564 } else {
565 enableLogging = false;
566 }
567 return -1;
568 }
569 return 0;
570 }
571
572 if (!enableLogging) {
573 return 0;
574 }
575
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700576 // Parse pid, tid and uid
Mark Salyzyn151beac2015-09-04 11:37:42 -0700577 const pid_t pid = sniffPid(p, len - (p - buf));
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700578 const pid_t tid = pid;
579 const uid_t uid = pid ? logbuf->pidToUid(pid) : 0;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700580
581 // Parse (rules at top) to pull out a tag from the incoming kernel message.
582 // Some may view the following as an ugly heuristic, the desire is to
583 // beautify the kernel logs into an Android Logging format; the goal is
584 // admirable but costly.
Mark Salyzyneb298082015-11-10 15:56:41 -0800585 while ((p < &buf[len]) && (isspace(*p) || !*p)) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700586 ++p;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700587 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700588 if (p >= &buf[len]) { // timestamp, no content
Mark Salyzynae4d9282014-10-15 08:49:39 -0700589 return 0;
590 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700591 start = p;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700592 const char *tag = "";
593 const char *etag = tag;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700594 size_t taglen = len - (p - buf);
595 if (!isspace(*p) && *p) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700596 const char *bt, *et, *cp;
597
Mark Salyzyn151beac2015-09-04 11:37:42 -0700598 bt = p;
Mark Salyzyneb298082015-11-10 15:56:41 -0800599 if ((taglen >= 6) && !fast<strncmp>(p, "[INFO]", 6)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700600 // <PRI>[<TIME>] "[INFO]"<tag> ":" message
Mark Salyzyn151beac2015-09-04 11:37:42 -0700601 bt = p + 6;
602 taglen -= 6;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700603 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700604 for(et = bt; taglen && *et && (*et != ':') && !isspace(*et); ++et, --taglen) {
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700605 // skip ':' within [ ... ]
606 if (*et == '[') {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700607 while (taglen && *et && *et != ']') {
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700608 ++et;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700609 --taglen;
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700610 }
611 }
612 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700613 for(cp = et; taglen && isspace(*cp); ++cp, --taglen);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700614 size_t size;
615
616 if (*cp == ':') {
617 // One Word
618 tag = bt;
619 etag = et;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700620 p = cp + 1;
621 } else if (taglen) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700622 size = et - bt;
Mark Salyzyneb298082015-11-10 15:56:41 -0800623 if ((taglen > size) && // enough space for match plus trailing :
624 (*bt == *cp) && // ubber fast<strncmp> pair
625 fast<strncmp>(bt + 1, cp + 1, size - 1)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700626 // <PRI>[<TIME>] <tag>_host '<tag>.<num>' : message
Mark Salyzynddda2122015-10-02 09:22:52 -0700627 if (!fast<strncmp>(bt + size - 5, "_host", 5)
628 && !fast<strncmp>(bt + 1, cp + 1, size - 6)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700629 const char *b = cp;
630 cp += size - 5;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700631 taglen -= size - 5;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700632 if (*cp == '.') {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700633 while (--taglen && !isspace(*++cp) && (*cp != ':'));
Mark Salyzynae4d9282014-10-15 08:49:39 -0700634 const char *e;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700635 for(e = cp; taglen && isspace(*cp); ++cp, --taglen);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700636 if (*cp == ':') {
637 tag = b;
638 etag = e;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700639 p = cp + 1;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700640 }
641 }
642 } else {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700643 while (--taglen && !isspace(*++cp) && (*cp != ':'));
Mark Salyzynae4d9282014-10-15 08:49:39 -0700644 const char *e;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700645 for(e = cp; taglen && isspace(*cp); ++cp, --taglen);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700646 // Two words
647 if (*cp == ':') {
648 tag = bt;
649 etag = e;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700650 p = cp + 1;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700651 }
652 }
653 } else if (isspace(cp[size])) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700654 cp += size;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700655 taglen -= size;
656 while (--taglen && isspace(*++cp));
Mark Salyzynae4d9282014-10-15 08:49:39 -0700657 // <PRI>[<TIME>] <tag> <tag> : message
658 if (*cp == ':') {
659 tag = bt;
660 etag = et;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700661 p = cp + 1;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700662 }
663 } else if (cp[size] == ':') {
664 // <PRI>[<TIME>] <tag> <tag> : message
665 tag = bt;
666 etag = et;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700667 p = cp + size + 1;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700668 } else if ((cp[size] == '.') || isdigit(cp[size])) {
669 // <PRI>[<TIME>] <tag> '<tag>.<num>' : message
670 // <PRI>[<TIME>] <tag> '<tag><num>' : message
671 const char *b = cp;
672 cp += size;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700673 taglen -= size;
674 while (--taglen && !isspace(*++cp) && (*cp != ':'));
Mark Salyzynae4d9282014-10-15 08:49:39 -0700675 const char *e = cp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700676 while (taglen && isspace(*cp)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700677 ++cp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700678 --taglen;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700679 }
680 if (*cp == ':') {
681 tag = b;
682 etag = e;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700683 p = cp + 1;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700684 }
685 } else {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700686 while (--taglen && !isspace(*++cp) && (*cp != ':'));
Mark Salyzynae4d9282014-10-15 08:49:39 -0700687 const char *e = cp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700688 while (taglen && isspace(*cp)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700689 ++cp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700690 --taglen;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700691 }
692 // Two words
693 if (*cp == ':') {
694 tag = bt;
695 etag = e;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700696 p = cp + 1;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700697 }
698 }
Mark Salyzyneb298082015-11-10 15:56:41 -0800699 } /* else no tag */
Mark Salyzynae4d9282014-10-15 08:49:39 -0700700 size = etag - tag;
701 if ((size <= 1)
Mark Salyzyn618d0de2015-07-13 10:19:34 -0700702 // register names like x9
703 || ((size == 2) && (isdigit(tag[0]) || isdigit(tag[1])))
704 // register names like x18 but not driver names like en0
705 || ((size == 3) && (isdigit(tag[1]) && isdigit(tag[2])))
706 // blacklist
Mark Salyzynddda2122015-10-02 09:22:52 -0700707 || ((size == 3) && !fast<strncmp>(tag, "CPU", 3))
708 || ((size == 7) && !fast<strncasecmp>(tag, "WARNING", 7))
709 || ((size == 5) && !fast<strncasecmp>(tag, "ERROR", 5))
710 || ((size == 4) && !fast<strncasecmp>(tag, "INFO", 4))) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700711 p = start;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700712 etag = tag = "";
713 }
714 }
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700715 // Suppress additional stutter in tag:
716 // eg: [143:healthd]healthd -> [143:healthd]
Mark Salyzyn151beac2015-09-04 11:37:42 -0700717 taglen = etag - tag;
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700718 // Mediatek-special printk induced stutter
Mark Salyzyn47dba712015-08-26 09:30:00 -0700719 const char *mp = strnrchr(tag, ']', taglen);
720 if (mp && (++mp < etag)) {
721 size_t s = etag - mp;
Mark Salyzynddda2122015-10-02 09:22:52 -0700722 if (((s + s) < taglen) && !fast<memcmp>(mp, mp - 1 - s, s)) {
Mark Salyzyn47dba712015-08-26 09:30:00 -0700723 taglen = mp - tag;
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700724 }
725 }
Mark Salyzyneb298082015-11-10 15:56:41 -0800726 // Deal with sloppy and simplistic harmless p = cp + 1 etc above.
727 if (len < (size_t)(p - buf)) {
728 p = &buf[len];
729 }
Mark Salyzynee49c6a2015-06-12 14:59:42 -0700730 // skip leading space
Mark Salyzyneb298082015-11-10 15:56:41 -0800731 while ((p < &buf[len]) && (isspace(*p) || !*p)) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700732 ++p;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700733 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700734 // truncate trailing space or nuls
735 size_t b = len - (p - buf);
736 while (b && (isspace(p[b-1]) || !p[b-1])) {
Mark Salyzynee49c6a2015-06-12 14:59:42 -0700737 --b;
738 }
739 // trick ... allow tag with empty content to be logged. log() drops empty
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700740 if (!b && taglen) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700741 p = " ";
Mark Salyzynee49c6a2015-06-12 14:59:42 -0700742 b = 1;
743 }
Mark Salyzyneb298082015-11-10 15:56:41 -0800744 // paranoid sanity check, can not happen ...
Mark Salyzynddda2122015-10-02 09:22:52 -0700745 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
746 b = LOGGER_ENTRY_MAX_PAYLOAD;
747 }
Mark Salyzyneb298082015-11-10 15:56:41 -0800748 if (taglen > LOGGER_ENTRY_MAX_PAYLOAD) {
749 taglen = LOGGER_ENTRY_MAX_PAYLOAD;
750 }
751 // calculate buffer copy requirements
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700752 size_t n = 1 + taglen + 1 + b + 1;
Mark Salyzyneb298082015-11-10 15:56:41 -0800753 // paranoid sanity check, first two just can not happen ...
754 if ((taglen > n) || (b > n) || (n > USHRT_MAX)) {
755 return -EINVAL;
Mark Salyzyn47dba712015-08-26 09:30:00 -0700756 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700757
Mark Salyzyneb298082015-11-10 15:56:41 -0800758 // Careful.
759 // We are using the stack to house the log buffer for speed reasons.
760 // If we malloc'd this buffer, we could get away without n's USHRT_MAX
761 // test above, but we would then required a max(n, USHRT_MAX) as
762 // truncating length argument to logbuf->log() below. Gain is protection
763 // of stack sanity and speedup, loss is truncated long-line content.
Mark Salyzynddda2122015-10-02 09:22:52 -0700764 char newstr[n];
Mark Salyzyn47dba712015-08-26 09:30:00 -0700765 char *np = newstr;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700766
767 // Convert priority into single-byte Android logger priority
768 *np = convertKernelPrioToAndroidPrio(pri);
769 ++np;
770
771 // Copy parsed tag following priority
Mark Salyzyn151beac2015-09-04 11:37:42 -0700772 memcpy(np, tag, taglen);
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700773 np += taglen;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700774 *np = '\0';
775 ++np;
776
777 // Copy main message to the remainder
Mark Salyzyn151beac2015-09-04 11:37:42 -0700778 memcpy(np, p, b);
Mark Salyzynee49c6a2015-06-12 14:59:42 -0700779 np[b] = '\0';
Mark Salyzynae4d9282014-10-15 08:49:39 -0700780
781 // Log message
Mark Salyzyneb298082015-11-10 15:56:41 -0800782 int rc = logbuf->log(LOG_ID_KERNEL, now, uid, pid, tid, newstr,
783 (unsigned short) n);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700784
785 // notify readers
786 if (!rc) {
787 reader->notifyNewLog();
788 }
789
790 return rc;
791}