blob: 7a7ac7da8027ca331366c853af356f4c04f5ba26 [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>
Mark Salyzyn317bfb92016-02-23 08:55:43 -080023#include <string.h>
Mark Salyzynae4d9282014-10-15 08:49:39 -070024#include <sys/prctl.h>
25#include <sys/uio.h>
26#include <syslog.h>
27
Mark Salyzyn83b24782016-08-11 16:07:10 -070028#include <private/android_filesystem_config.h>
Mark Salyzyn501c3732017-03-10 14:31:54 -080029#include <private/android_logger.h>
Mark Salyzynae4d9282014-10-15 08:49:39 -070030
Mark Salyzyn317bfb92016-02-23 08:55:43 -080031#include "LogBuffer.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070032#include "LogKlog.h"
Mark Salyzyn317bfb92016-02-23 08:55:43 -080033#include "LogReader.h"
Mark Salyzynae4d9282014-10-15 08:49:39 -070034
Mark Salyzyn501c3732017-03-10 14:31:54 -080035#define KMSG_PRIORITY(PRI) \
36 '<', '0' + (LOG_SYSLOG | (PRI)) / 10, '0' + (LOG_SYSLOG | (PRI)) % 10, '>'
Mark Salyzynae4d9282014-10-15 08:49:39 -070037
38static const char priority_message[] = { KMSG_PRIORITY(LOG_INFO), '\0' };
39
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070040// List of the _only_ needles we supply here to android::strnstr
41static const char suspendStr[] = "PM: suspend entry ";
42static const char resumeStr[] = "PM: suspend exit ";
43static const char suspendedStr[] = "Suspended for ";
44static const char healthdStr[] = "healthd";
45static const char batteryStr[] = ": battery ";
46static const char auditStr[] = " audit(";
47static const char klogdStr[] = "logd.klogd: ";
48
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070049// Parsing is hard
50
51// called if we see a '<', s is the next character, returns pointer after '>'
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070052static char* is_prio(char* s, ssize_t len) {
53 if ((len <= 0) || !isdigit(*s++)) return nullptr;
Mark Salyzynea1a2412015-09-02 07:39:53 -070054 --len;
55 static const size_t max_prio_len = (len < 4) ? len : 4;
56 size_t priolen = 0;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070057 char c;
Mark Salyzynea1a2412015-09-02 07:39:53 -070058 while (((c = *s++)) && (++priolen <= max_prio_len)) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070059 if (!isdigit(c)) return ((c == '>') && (*s == '[')) ? s : nullptr;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070060 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070061 return nullptr;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070062}
63
64// called if we see a '[', s is the next character, returns pointer after ']'
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070065static char* is_timestamp(char* s, ssize_t len) {
66 while ((len > 0) && (*s == ' ')) {
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070067 ++s;
Mark Salyzynea1a2412015-09-02 07:39:53 -070068 --len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070069 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070070 if ((len <= 0) || !isdigit(*s++)) return nullptr;
Mark Salyzynea1a2412015-09-02 07:39:53 -070071 --len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070072 bool first_period = true;
73 char c;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070074 while ((len > 0) && ((c = *s++))) {
Mark Salyzynea1a2412015-09-02 07:39:53 -070075 --len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070076 if ((c == '.') && first_period) {
77 first_period = false;
Mark Salyzynee49c6a2015-06-12 14:59:42 -070078 } else if (!isdigit(c)) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070079 return ((c == ']') && !first_period && (*s == ' ')) ? s : nullptr;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070080 }
81 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070082 return nullptr;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070083}
84
85// Like strtok_r with "\r\n" except that we look for log signatures (regex)
Mark Salyzyn501c3732017-03-10 14:31:54 -080086// \(\(<[0-9]\{1,4\}>\)\([[] *[0-9]+[.][0-9]+[]] \)\{0,1\}\|[[]
87// *[0-9]+[.][0-9]+[]] \)
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070088// and split if we see a second one without a newline.
Mark Salyzyn151beac2015-09-04 11:37:42 -070089// We allow nuls in content, monitoring the overall length and sub-length of
90// the discovered tokens.
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070091
Mark Salyzyn501c3732017-03-10 14:31:54 -080092#define SIGNATURE_MASK 0xF0
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070093// <digit> following ('0' to '9' masked with ~SIGNATURE_MASK) added to signature
Mark Salyzyn501c3732017-03-10 14:31:54 -080094#define LESS_THAN_SIG SIGNATURE_MASK
95#define OPEN_BRACKET_SIG ((SIGNATURE_MASK << 1) & SIGNATURE_MASK)
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070096// space is one more than <digit> of 9
Mark Salyzyn3e21de22015-06-08 14:51:30 -070097#define OPEN_BRACKET_SPACE ((char)(OPEN_BRACKET_SIG | 10))
Mark Salyzyn2c3b3002015-05-22 15:23:44 -070098
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070099char* android::log_strntok_r(char* s, ssize_t& len, char*& last,
100 ssize_t& sublen) {
101 sublen = 0;
102 if (len <= 0) return nullptr;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700103 if (!s) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700104 if (!(s = last)) return nullptr;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700105 // fixup for log signature split <,
106 // LESS_THAN_SIG + <digit>
107 if ((*s & SIGNATURE_MASK) == LESS_THAN_SIG) {
108 *s = (*s & ~SIGNATURE_MASK) + '0';
109 *--s = '<';
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700110 ++len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700111 }
112 // fixup for log signature split [,
113 // OPEN_BRACKET_SPACE is space, OPEN_BRACKET_SIG + <digit>
114 if ((*s & SIGNATURE_MASK) == OPEN_BRACKET_SIG) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700115 *s = (*s == OPEN_BRACKET_SPACE) ? ' ' : (*s & ~SIGNATURE_MASK) + '0';
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700116 *--s = '[';
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700117 ++len;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700118 }
119 }
120
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700121 while ((len > 0) && ((*s == '\r') || (*s == '\n'))) {
Mark Salyzynea1a2412015-09-02 07:39:53 -0700122 ++s;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700123 --len;
Mark Salyzynea1a2412015-09-02 07:39:53 -0700124 }
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700125
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700126 if (len <= 0) return last = nullptr;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700127 char *peek, *tok = s;
128
129 for (;;) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700130 if (len <= 0) {
131 last = nullptr;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700132 return tok;
Mark Salyzynea1a2412015-09-02 07:39:53 -0700133 }
134 char c = *s++;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700135 --len;
136 ssize_t adjust;
Mark Salyzynea1a2412015-09-02 07:39:53 -0700137 switch (c) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800138 case '\r':
139 case '\n':
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700140 s[-1] = '\0';
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700141 last = s;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700142 return tok;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800143
144 case '<':
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700145 peek = is_prio(s, len);
146 if (!peek) break;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800147 if (s != (tok + 1)) { // not first?
148 s[-1] = '\0';
149 *s &= ~SIGNATURE_MASK;
150 *s |= LESS_THAN_SIG; // signature for '<'
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700151 last = s;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800152 return tok;
153 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700154 adjust = peek - s;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700155 if (adjust > len) {
156 adjust = len;
Mark Salyzynea1a2412015-09-02 07:39:53 -0700157 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700158 sublen += adjust;
159 len -= adjust;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700160 s = peek;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700161 if ((*s == '[') && ((peek = is_timestamp(s + 1, len - 1)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800162 adjust = peek - s;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700163 if (adjust > len) {
164 adjust = len;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800165 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700166 sublen += adjust;
167 len -= adjust;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800168 s = peek;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700169 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800170 break;
171
172 case '[':
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700173 peek = is_timestamp(s, len);
174 if (!peek) break;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800175 if (s != (tok + 1)) { // not first?
176 s[-1] = '\0';
177 if (*s == ' ') {
178 *s = OPEN_BRACKET_SPACE;
179 } else {
180 *s &= ~SIGNATURE_MASK;
181 *s |= OPEN_BRACKET_SIG; // signature for '['
182 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700183 last = s;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800184 return tok;
185 }
186 adjust = peek - s;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700187 if (adjust > len) {
188 adjust = len;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800189 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700190 sublen += adjust;
191 len -= adjust;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800192 s = peek;
193 break;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700194 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700195 ++sublen;
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700196 }
Mark Salyzyn618d0de2015-07-13 10:19:34 -0700197 // NOTREACHED
Mark Salyzyn2c3b3002015-05-22 15:23:44 -0700198}
199
Mark Salyzyn10b82b62015-12-28 15:33:01 -0800200log_time LogKlog::correction =
201 (log_time(CLOCK_REALTIME) < log_time(CLOCK_MONOTONIC))
202 ? log_time::EPOCH
203 : (log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC));
Mark Salyzynae4d9282014-10-15 08:49:39 -0700204
Mark Salyzyn501c3732017-03-10 14:31:54 -0800205LogKlog::LogKlog(LogBuffer* buf, LogReader* reader, int fdWrite, int fdRead,
206 bool auditd)
207 : SocketListener(fdRead, false),
208 logbuf(buf),
209 reader(reader),
210 signature(CLOCK_MONOTONIC),
211 initialized(false),
212 enableLogging(true),
213 auditd(auditd) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700214 static const char klogd_message[] = "%s%s%" PRIu64 "\n";
215 char buffer[strlen(priority_message) + strlen(klogdStr) +
216 strlen(klogd_message) + 20];
217 snprintf(buffer, sizeof(buffer), klogd_message, priority_message, klogdStr,
Mark Salyzyn501c3732017-03-10 14:31:54 -0800218 signature.nsec());
Mark Salyzynae4d9282014-10-15 08:49:39 -0700219 write(fdWrite, buffer, strlen(buffer));
220}
221
Mark Salyzyn501c3732017-03-10 14:31:54 -0800222bool LogKlog::onDataAvailable(SocketClient* cli) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700223 if (!initialized) {
224 prctl(PR_SET_NAME, "logd.klogd");
225 initialized = true;
226 enableLogging = false;
227 }
228
229 char buffer[LOGGER_ENTRY_MAX_PAYLOAD];
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700230 ssize_t len = 0;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700231
Mark Salyzyn501c3732017-03-10 14:31:54 -0800232 for (;;) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700233 ssize_t retval = 0;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700234 if (len < (ssize_t)(sizeof(buffer) - 1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800235 retval =
236 read(cli->getSocket(), buffer + len, sizeof(buffer) - 1 - len);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700237 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700238 if ((retval == 0) && (len <= 0)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700239 break;
240 }
241 if (retval < 0) {
242 return false;
243 }
244 len += retval;
245 bool full = len == (sizeof(buffer) - 1);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800246 char* ep = buffer + len;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700247 *ep = '\0';
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700248 ssize_t sublen;
249 for (char *ptr = nullptr, *tok = buffer;
250 !!(tok = android::log_strntok_r(tok, len, ptr, sublen));
251 tok = nullptr) {
Mark Salyzynea1a2412015-09-02 07:39:53 -0700252 if (((tok + sublen) >= ep) && (retval != 0) && full) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700253 if (sublen > 0) memmove(buffer, tok, sublen);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700254 len = sublen;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700255 break;
256 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700257 if ((sublen > 0) && *tok) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700258 log(tok, sublen);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700259 }
260 }
261 }
262
263 return true;
264}
265
Mark Salyzyn501c3732017-03-10 14:31:54 -0800266void LogKlog::calculateCorrection(const log_time& monotonic,
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700267 const char* real_string, ssize_t len) {
268 static const char real_format[] = "%Y-%m-%d %H:%M:%S.%09q UTC";
269 if (len < (ssize_t)(strlen(real_format) + 5)) return;
270
Mark Salyzynae4d9282014-10-15 08:49:39 -0700271 log_time real;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700272 const char* ep = real.strptime(real_string, real_format);
Mark Salyzyn10b82b62015-12-28 15:33:01 -0800273 if (!ep || (ep > &real_string[len]) || (real > log_time(CLOCK_REALTIME))) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700274 return;
275 }
276 // kernel report UTC, log_time::strptime is localtime from calendar.
277 // Bionic and liblog strptime does not support %z or %Z to pick up
278 // timezone so we are calculating our own correction.
279 time_t now = real.tv_sec;
280 struct tm tm;
281 memset(&tm, 0, sizeof(tm));
282 tm.tm_isdst = -1;
283 localtime_r(&now, &tm);
Mark Salyzynff8b8e82015-12-30 13:46:07 -0800284 if ((tm.tm_gmtoff < 0) && ((-tm.tm_gmtoff) > (long)real.tv_sec)) {
Mark Salyzyn10b82b62015-12-28 15:33:01 -0800285 real = log_time::EPOCH;
286 } else {
287 real.tv_sec += tm.tm_gmtoff;
288 }
289 if (monotonic > real) {
290 correction = log_time::EPOCH;
291 } else {
292 correction = real - monotonic;
293 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700294}
295
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700296void LogKlog::sniffTime(log_time& now, const char*& buf, ssize_t len,
Mark Salyzyn151beac2015-09-04 11:37:42 -0700297 bool reverse) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700298 if (len <= 0) return;
299
300 const char* cp = nullptr;
301 if ((len > 10) && (*buf == '[')) {
302 cp = now.strptime(buf, "[ %s.%q]"); // can index beyond buffer bounds
303 if (cp && (cp > &buf[len - 1])) cp = nullptr;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700304 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700305 if (cp) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700306 len -= cp - buf;
307 if ((len > 0) && isspace(*cp)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700308 ++cp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700309 --len;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700310 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700311 buf = cp;
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700312
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700313 if (isMonotonic()) return;
Mark Salyzynb6bee332015-09-08 08:56:32 -0700314
Mark Salyzyna2c02222016-12-13 10:31:29 -0800315 const char* b;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800316 if (((b = android::strnstr(cp, len, suspendStr))) &&
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700317 (((b += strlen(suspendStr)) - cp) < len)) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700318 len -= b - cp;
319 calculateCorrection(now, b, len);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800320 } else if (((b = android::strnstr(cp, len, resumeStr))) &&
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700321 (((b += strlen(resumeStr)) - cp) < len)) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700322 len -= b - cp;
323 calculateCorrection(now, b, len);
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700324 } else if (((b = android::strnstr(cp, len, healthdStr))) &&
325 (((b += strlen(healthdStr)) - cp) < len) &&
326 ((b = android::strnstr(b, len -= b - cp, batteryStr))) &&
327 (((b += strlen(batteryStr)) - cp) < len)) {
Mark Salyzyn10b82b62015-12-28 15:33:01 -0800328 // NB: healthd is roughly 150us late, so we use it instead to
329 // trigger a check for ntp-induced or hardware clock drift.
330 log_time real(CLOCK_REALTIME);
331 log_time mono(CLOCK_MONOTONIC);
332 correction = (real < mono) ? log_time::EPOCH : (real - mono);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800333 } else if (((b = android::strnstr(cp, len, suspendedStr))) &&
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700334 (((b += strlen(suspendStr)) - cp) < len)) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700335 len -= b - cp;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700336 log_time real;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800337 char* endp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700338 real.tv_sec = strtol(b, &endp, 10);
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700339 if ((*endp == '.') && ((endp - b) < len)) {
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700340 unsigned long multiplier = NS_PER_SEC;
341 real.tv_nsec = 0;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700342 len -= endp - b;
343 while (--len && isdigit(*++endp) && (multiplier /= 10)) {
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700344 real.tv_nsec += (*endp - '0') * multiplier;
345 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700346 if (reverse) {
Mark Salyzyn10b82b62015-12-28 15:33:01 -0800347 if (real > correction) {
348 correction = log_time::EPOCH;
349 } else {
350 correction -= real;
351 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700352 } else {
353 correction += real;
354 }
355 }
356 }
357
358 convertMonotonicToReal(now);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700359 } else {
Mark Salyzynb6bee332015-09-08 08:56:32 -0700360 if (isMonotonic()) {
361 now = log_time(CLOCK_MONOTONIC);
362 } else {
363 now = log_time(CLOCK_REALTIME);
364 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700365 }
366}
367
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700368pid_t LogKlog::sniffPid(const char*& buf, ssize_t len) {
369 if (len <= 0) return 0;
370
371 const char* cp = buf;
372 // sscanf does a strlen, let's check if the string is not nul terminated.
373 // pseudo out-of-bounds access since we always have an extra char on buffer.
374 if (((ssize_t)strnlen(cp, len) == len) && cp[len]) {
375 return 0;
376 }
Mark Salyzyn0b4a63d2016-07-15 08:54:03 -0700377 // HTC kernels with modified printk "c0 1648 "
Mark Salyzyn501c3732017-03-10 14:31:54 -0800378 if ((len > 9) && (cp[0] == 'c') && isdigit(cp[1]) &&
379 (isdigit(cp[2]) || (cp[2] == ' ')) && (cp[3] == ' ')) {
Mark Salyzyn0b4a63d2016-07-15 08:54:03 -0700380 bool gotDigit = false;
381 int i;
382 for (i = 4; i < 9; ++i) {
383 if (isdigit(cp[i])) {
384 gotDigit = true;
385 } else if (gotDigit || (cp[i] != ' ')) {
386 break;
387 }
388 }
389 if ((i == 9) && (cp[i] == ' ')) {
390 int pid = 0;
391 char dummy;
392 if (sscanf(cp + 4, "%d%c", &pid, &dummy) == 2) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700393 buf = cp + 10; // skip-it-all
Mark Salyzyn0b4a63d2016-07-15 08:54:03 -0700394 return pid;
395 }
396 }
397 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700398 while (len) {
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700399 // Mediatek kernels with modified printk
400 if (*cp == '[') {
401 int pid = 0;
402 char dummy;
403 if (sscanf(cp, "[%d:%*[a-z_./0-9:A-Z]]%c", &pid, &dummy) == 2) {
404 return pid;
405 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800406 break; // Only the first one
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700407 }
408 ++cp;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700409 --len;
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700410 }
411 return 0;
412}
413
Mark Salyzynae4d9282014-10-15 08:49:39 -0700414// kernel log prefix, convert to a kernel log priority number
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700415static int parseKernelPrio(const char*& buf, ssize_t len) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700416 int pri = LOG_USER | LOG_INFO;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700417 const char* cp = buf;
418 if ((len > 0) && (*cp == '<')) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700419 pri = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800420 while (--len && isdigit(*++cp)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700421 pri = (pri * 10) + *cp - '0';
422 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700423 if ((len > 0) && (*cp == '>')) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700424 ++cp;
425 } else {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700426 cp = buf;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700427 pri = LOG_USER | LOG_INFO;
428 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700429 buf = cp;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700430 }
431 return pri;
432}
433
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700434// Passed the entire SYSLOG_ACTION_READ_ALL buffer and interpret a
435// compensated start time.
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700436void LogKlog::synchronize(const char* buf, ssize_t len) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800437 const char* cp = android::strnstr(buf, len, suspendStr);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700438 if (!cp) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800439 cp = android::strnstr(buf, len, resumeStr);
440 if (!cp) return;
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700441 } else {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800442 const char* rp = android::strnstr(buf, len, resumeStr);
443 if (rp && (rp < cp)) cp = rp;
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700444 }
445
446 do {
447 --cp;
448 } while ((cp > buf) && (*cp != '\n'));
449 if (*cp == '\n') {
450 ++cp;
451 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700452 parseKernelPrio(cp, len - (cp - buf));
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700453
454 log_time now;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700455 sniffTime(now, cp, len - (cp - buf), true);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700456
Mark Salyzyna2c02222016-12-13 10:31:29 -0800457 const char* suspended = android::strnstr(buf, len, suspendedStr);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700458 if (!suspended || (suspended > cp)) {
459 return;
460 }
461 cp = suspended;
462
463 do {
464 --cp;
465 } while ((cp > buf) && (*cp != '\n'));
466 if (*cp == '\n') {
467 ++cp;
468 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700469 parseKernelPrio(cp, len - (cp - buf));
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700470
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700471 sniffTime(now, cp, len - (cp - buf), true);
Mark Salyzyn2d159bf2015-09-01 13:09:23 -0700472}
473
Mark Salyzynae4d9282014-10-15 08:49:39 -0700474// Convert kernel log priority number into an Android Logger priority number
475static int convertKernelPrioToAndroidPrio(int pri) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800476 switch (pri & LOG_PRIMASK) {
477 case LOG_EMERG:
Mark Salyzynae4d9282014-10-15 08:49:39 -0700478 // FALLTHRU
Mark Salyzyn501c3732017-03-10 14:31:54 -0800479 case LOG_ALERT:
Mark Salyzynae4d9282014-10-15 08:49:39 -0700480 // FALLTHRU
Mark Salyzyn501c3732017-03-10 14:31:54 -0800481 case LOG_CRIT:
482 return ANDROID_LOG_FATAL;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700483
Mark Salyzyn501c3732017-03-10 14:31:54 -0800484 case LOG_ERR:
485 return ANDROID_LOG_ERROR;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700486
Mark Salyzyn501c3732017-03-10 14:31:54 -0800487 case LOG_WARNING:
488 return ANDROID_LOG_WARN;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700489
Mark Salyzyn501c3732017-03-10 14:31:54 -0800490 default:
Mark Salyzynae4d9282014-10-15 08:49:39 -0700491 // FALLTHRU
Mark Salyzyn501c3732017-03-10 14:31:54 -0800492 case LOG_NOTICE:
Mark Salyzynae4d9282014-10-15 08:49:39 -0700493 // FALLTHRU
Mark Salyzyn501c3732017-03-10 14:31:54 -0800494 case LOG_INFO:
495 break;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700496
Mark Salyzyn501c3732017-03-10 14:31:54 -0800497 case LOG_DEBUG:
498 return ANDROID_LOG_DEBUG;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700499 }
500
501 return ANDROID_LOG_INFO;
502}
503
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700504static const char* strnrchr(const char* s, ssize_t len, char c) {
505 const char* save = nullptr;
506 for (; len > 0; ++s, len--) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800507 if (*s == c) {
508 save = s;
509 }
Mark Salyzyn47dba712015-08-26 09:30:00 -0700510 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800511 return save;
Mark Salyzyn47dba712015-08-26 09:30:00 -0700512}
513
Mark Salyzynae4d9282014-10-15 08:49:39 -0700514//
515// log a message into the kernel log buffer
516//
517// Filter rules to parse <PRI> <TIME> <tag> and <message> in order for
518// them to appear correct in the logcat output:
519//
520// LOG_KERN (0):
521// <PRI>[<TIME>] <tag> ":" <message>
522// <PRI>[<TIME>] <tag> <tag> ":" <message>
523// <PRI>[<TIME>] <tag> <tag>_work ":" <message>
524// <PRI>[<TIME>] <tag> '<tag>.<num>' ":" <message>
525// <PRI>[<TIME>] <tag> '<tag><num>' ":" <message>
526// <PRI>[<TIME>] <tag>_host '<tag>.<num>' ":" <message>
527// (unimplemented) <PRI>[<TIME>] <tag> '<num>.<tag>' ":" <message>
528// <PRI>[<TIME>] "[INFO]"<tag> : <message>
529// <PRI>[<TIME>] "------------[ cut here ]------------" (?)
530// <PRI>[<TIME>] "---[ end trace 3225a3070ca3e4ac ]---" (?)
531// LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS
532// LOG_UUCP, LOG_CRON, LOG_AUTHPRIV, LOG_FTP:
533// <PRI+TAG>[<TIME>] (see sys/syslog.h)
534// Observe:
535// Minimum tag length = 3 NB: drops things like r5:c00bbadf, but allow PM:
536// Maximum tag words = 2
537// Maximum tag length = 16 NB: we are thinking of how ugly logcat can get.
538// Not a Tag if there is no message content.
539// leading additional spaces means no tag, inherit last tag.
540// Not a Tag if <tag>: is "ERROR:", "WARNING:", "INFO:" or "CPU:"
541// Drop:
542// empty messages
543// messages with ' audit(' in them if auditd is running
544// logd.klogd:
545// return -1 if message logd.klogd: <signature>
546//
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700547int LogKlog::log(const char* buf, ssize_t len) {
548 if (auditd && android::strnstr(buf, len, auditStr)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700549 return 0;
550 }
551
Mark Salyzyna2c02222016-12-13 10:31:29 -0800552 const char* p = buf;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700553 int pri = parseKernelPrio(p, len);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700554
555 log_time now;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700556 sniffTime(now, p, len - (p - buf), false);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700557
558 // sniff for start marker
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700559 const char* start = android::strnstr(p, len - (p - buf), klogdStr);
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700560 if (start) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700561 uint64_t sig = strtoll(start + strlen(klogdStr), nullptr, 10);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700562 if (sig == signature.nsec()) {
563 if (initialized) {
564 enableLogging = true;
565 } else {
566 enableLogging = false;
567 }
568 return -1;
569 }
570 return 0;
571 }
572
573 if (!enableLogging) {
574 return 0;
575 }
576
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700577 // Parse pid, tid and uid
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700578 const pid_t pid = sniffPid(p, len - (p - buf));
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700579 const pid_t tid = pid;
Mark Salyzyn83b24782016-08-11 16:07:10 -0700580 uid_t uid = AID_ROOT;
581 if (pid) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700582 logbuf->wrlock();
Mark Salyzyn83b24782016-08-11 16:07:10 -0700583 uid = logbuf->pidToUid(pid);
584 logbuf->unlock();
585 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700586
587 // Parse (rules at top) to pull out a tag from the incoming kernel message.
588 // Some may view the following as an ugly heuristic, the desire is to
589 // beautify the kernel logs into an Android Logging format; the goal is
590 // admirable but costly.
Mark Salyzyneb298082015-11-10 15:56:41 -0800591 while ((p < &buf[len]) && (isspace(*p) || !*p)) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700592 ++p;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700593 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800594 if (p >= &buf[len]) { // timestamp, no content
Mark Salyzynae4d9282014-10-15 08:49:39 -0700595 return 0;
596 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700597 start = p;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800598 const char* tag = "";
599 const char* etag = tag;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700600 ssize_t taglen = len - (p - buf);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800601 const char* bt = p;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700602
Mark Salyzyna146a772016-08-05 08:16:37 -0700603 static const char infoBrace[] = "[INFO]";
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700604 static const ssize_t infoBraceLen = strlen(infoBrace);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800605 if ((taglen >= infoBraceLen) &&
606 !fastcmp<strncmp>(p, infoBrace, infoBraceLen)) {
Mark Salyzyna146a772016-08-05 08:16:37 -0700607 // <PRI>[<TIME>] "[INFO]"<tag> ":" message
608 bt = p + infoBraceLen;
609 taglen -= infoBraceLen;
610 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700611
Mark Salyzyn501c3732017-03-10 14:31:54 -0800612 const char* et;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700613 for (et = bt; (taglen > 0) && *et && (*et != ':') && !isspace(*et);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800614 ++et, --taglen) {
615 // skip ':' within [ ... ]
616 if (*et == '[') {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700617 while ((taglen > 0) && *et && *et != ']') {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800618 ++et;
619 --taglen;
620 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700621 if (taglen <= 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800622 break;
623 }
624 }
Mark Salyzyna146a772016-08-05 08:16:37 -0700625 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800626 const char* cp;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700627 for (cp = et; (taglen > 0) && isspace(*cp); ++cp, --taglen) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800628 }
Mark Salyzyna146a772016-08-05 08:16:37 -0700629
630 // Validate tag
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700631 ssize_t size = et - bt;
632 if ((taglen > 0) && (size > 0)) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700633 if (*cp == ':') {
Mark Salyzyna146a772016-08-05 08:16:37 -0700634 // ToDo: handle case insensitive colon separated logging stutter:
635 // <tag> : <tag>: ...
636
Mark Salyzynae4d9282014-10-15 08:49:39 -0700637 // One Word
638 tag = bt;
639 etag = et;
Mark Salyzyn151beac2015-09-04 11:37:42 -0700640 p = cp + 1;
Mark Salyzyna146a772016-08-05 08:16:37 -0700641 } else if ((taglen > size) && (tolower(*bt) == tolower(*cp))) {
642 // clean up any tag stutter
Mark Salyzyn501c3732017-03-10 14:31:54 -0800643 if (!fastcmp<strncasecmp>(bt + 1, cp + 1, size - 1)) { // no match
Mark Salyzyna146a772016-08-05 08:16:37 -0700644 // <PRI>[<TIME>] <tag> <tag> : message
645 // <PRI>[<TIME>] <tag> <tag>: message
646 // <PRI>[<TIME>] <tag> '<tag>.<num>' : message
647 // <PRI>[<TIME>] <tag> '<tag><num>' : message
648 // <PRI>[<TIME>] <tag> '<tag><stuff>' : message
Mark Salyzyn501c3732017-03-10 14:31:54 -0800649 const char* b = cp;
Mark Salyzyna146a772016-08-05 08:16:37 -0700650 cp += size;
651 taglen -= size;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700652 while ((--taglen > 0) && !isspace(*++cp) && (*cp != ':')) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800653 }
654 const char* e;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700655 for (e = cp; (taglen > 0) && isspace(*cp); ++cp, --taglen) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800656 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700657 if ((taglen > 0) && (*cp == ':')) {
Mark Salyzyna146a772016-08-05 08:16:37 -0700658 tag = b;
659 etag = e;
660 p = cp + 1;
661 }
662 } else {
663 // what about <PRI>[<TIME>] <tag>_host '<tag><stuff>' : message
664 static const char host[] = "_host";
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700665 static const ssize_t hostlen = strlen(host);
Mark Salyzyna146a772016-08-05 08:16:37 -0700666 if ((size > hostlen) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800667 !fastcmp<strncmp>(bt + size - hostlen, host, hostlen) &&
668 !fastcmp<strncmp>(bt + 1, cp + 1, size - hostlen - 1)) {
669 const char* b = cp;
Mark Salyzyna146a772016-08-05 08:16:37 -0700670 cp += size - hostlen;
671 taglen -= size - hostlen;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700672 if (*cp == '.') {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700673 while ((--taglen > 0) && !isspace(*++cp) &&
674 (*cp != ':')) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800675 }
676 const char* e;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700677 for (e = cp; (taglen > 0) && isspace(*cp);
678 ++cp, --taglen) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800679 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700680 if ((taglen > 0) && (*cp == ':')) {
Mark Salyzynae4d9282014-10-15 08:49:39 -0700681 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 }
686 } else {
Mark Salyzyna146a772016-08-05 08:16:37 -0700687 goto twoWord;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700688 }
689 }
Mark Salyzyna146a772016-08-05 08:16:37 -0700690 } else {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800691 // <PRI>[<TIME>] <tag> <stuff>' : message
692 twoWord:
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700693 while ((--taglen > 0) && !isspace(*++cp) && (*cp != ':')) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800694 }
695 const char* e;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700696 for (e = cp; (taglen > 0) && isspace(*cp); ++cp, --taglen) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800697 }
Mark Salyzyna146a772016-08-05 08:16:37 -0700698 // Two words
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700699 if ((taglen > 0) && (*cp == ':')) {
Mark Salyzyna146a772016-08-05 08:16:37 -0700700 tag = bt;
701 etag = e;
702 p = cp + 1;
703 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700704 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800705 } // else no tag
Mark Salyzyna146a772016-08-05 08:16:37 -0700706
707 static const char cpu[] = "CPU";
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700708 static const ssize_t cpuLen = strlen(cpu);
Mark Salyzyna146a772016-08-05 08:16:37 -0700709 static const char warning[] = "WARNING";
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700710 static const ssize_t warningLen = strlen(warning);
Mark Salyzyna146a772016-08-05 08:16:37 -0700711 static const char error[] = "ERROR";
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700712 static const ssize_t errorLen = strlen(error);
Mark Salyzyna146a772016-08-05 08:16:37 -0700713 static const char info[] = "INFO";
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700714 static const ssize_t infoLen = strlen(info);
Mark Salyzyna146a772016-08-05 08:16:37 -0700715
716 size = etag - tag;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800717 if ((size <= 1) ||
Mark Salyzyna146a772016-08-05 08:16:37 -0700718 // register names like x9
Mark Salyzyn501c3732017-03-10 14:31:54 -0800719 ((size == 2) && (isdigit(tag[0]) || isdigit(tag[1]))) ||
Mark Salyzyna146a772016-08-05 08:16:37 -0700720 // register names like x18 but not driver names like en0
Mark Salyzyn501c3732017-03-10 14:31:54 -0800721 ((size == 3) && (isdigit(tag[1]) && isdigit(tag[2]))) ||
Mark Salyzyna146a772016-08-05 08:16:37 -0700722 // blacklist
Mark Salyzyn501c3732017-03-10 14:31:54 -0800723 ((size == cpuLen) && !fastcmp<strncmp>(tag, cpu, cpuLen)) ||
724 ((size == warningLen) &&
725 !fastcmp<strncasecmp>(tag, warning, warningLen)) ||
726 ((size == errorLen) && !fastcmp<strncasecmp>(tag, error, errorLen)) ||
727 ((size == infoLen) && !fastcmp<strncasecmp>(tag, info, infoLen))) {
Mark Salyzyna146a772016-08-05 08:16:37 -0700728 p = start;
729 etag = tag = "";
Mark Salyzynae4d9282014-10-15 08:49:39 -0700730 }
Mark Salyzyna146a772016-08-05 08:16:37 -0700731
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700732 // Suppress additional stutter in tag:
733 // eg: [143:healthd]healthd -> [143:healthd]
Mark Salyzyn151beac2015-09-04 11:37:42 -0700734 taglen = etag - tag;
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700735 // Mediatek-special printk induced stutter
Andreas Gampe34450d72017-03-14 20:43:43 -0700736 const char* mp = strnrchr(tag, taglen, ']');
Mark Salyzyn47dba712015-08-26 09:30:00 -0700737 if (mp && (++mp < etag)) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700738 ssize_t s = etag - mp;
Mark Salyzyn0eeb06b2016-12-02 10:08:48 -0800739 if (((s + s) < taglen) && !fastcmp<memcmp>(mp, mp - 1 - s, s)) {
Mark Salyzyn47dba712015-08-26 09:30:00 -0700740 taglen = mp - tag;
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700741 }
742 }
Mark Salyzyneb298082015-11-10 15:56:41 -0800743 // Deal with sloppy and simplistic harmless p = cp + 1 etc above.
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700744 if (len < (p - buf)) {
Mark Salyzyneb298082015-11-10 15:56:41 -0800745 p = &buf[len];
746 }
Mark Salyzynee49c6a2015-06-12 14:59:42 -0700747 // skip leading space
Mark Salyzyneb298082015-11-10 15:56:41 -0800748 while ((p < &buf[len]) && (isspace(*p) || !*p)) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700749 ++p;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700750 }
Mark Salyzyn151beac2015-09-04 11:37:42 -0700751 // truncate trailing space or nuls
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700752 ssize_t b = len - (p - buf);
753 while ((b > 0) && (isspace(p[b - 1]) || !p[b - 1])) {
Mark Salyzynee49c6a2015-06-12 14:59:42 -0700754 --b;
755 }
756 // trick ... allow tag with empty content to be logged. log() drops empty
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700757 if ((b <= 0) && (taglen > 0)) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700758 p = " ";
Mark Salyzynee49c6a2015-06-12 14:59:42 -0700759 b = 1;
760 }
Mark Salyzyneb298082015-11-10 15:56:41 -0800761 // paranoid sanity check, can not happen ...
Mark Salyzynddda2122015-10-02 09:22:52 -0700762 if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
763 b = LOGGER_ENTRY_MAX_PAYLOAD;
764 }
Mark Salyzyneb298082015-11-10 15:56:41 -0800765 if (taglen > LOGGER_ENTRY_MAX_PAYLOAD) {
766 taglen = LOGGER_ENTRY_MAX_PAYLOAD;
767 }
768 // calculate buffer copy requirements
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700769 ssize_t n = 1 + taglen + 1 + b + 1;
Mark Salyzyneb298082015-11-10 15:56:41 -0800770 // paranoid sanity check, first two just can not happen ...
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700771 if ((taglen > n) || (b > n) || (n > (ssize_t)USHRT_MAX) || (n <= 0)) {
Mark Salyzyneb298082015-11-10 15:56:41 -0800772 return -EINVAL;
Mark Salyzyn47dba712015-08-26 09:30:00 -0700773 }
Mark Salyzynae4d9282014-10-15 08:49:39 -0700774
Mark Salyzyneb298082015-11-10 15:56:41 -0800775 // Careful.
776 // We are using the stack to house the log buffer for speed reasons.
777 // If we malloc'd this buffer, we could get away without n's USHRT_MAX
778 // test above, but we would then required a max(n, USHRT_MAX) as
779 // truncating length argument to logbuf->log() below. Gain is protection
780 // of stack sanity and speedup, loss is truncated long-line content.
Mark Salyzynddda2122015-10-02 09:22:52 -0700781 char newstr[n];
Mark Salyzyn501c3732017-03-10 14:31:54 -0800782 char* np = newstr;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700783
784 // Convert priority into single-byte Android logger priority
785 *np = convertKernelPrioToAndroidPrio(pri);
786 ++np;
787
788 // Copy parsed tag following priority
Mark Salyzyn151beac2015-09-04 11:37:42 -0700789 memcpy(np, tag, taglen);
Mark Salyzyn46d159d2015-08-26 09:30:00 -0700790 np += taglen;
Mark Salyzynae4d9282014-10-15 08:49:39 -0700791 *np = '\0';
792 ++np;
793
794 // Copy main message to the remainder
Mark Salyzyn151beac2015-09-04 11:37:42 -0700795 memcpy(np, p, b);
Mark Salyzynee49c6a2015-06-12 14:59:42 -0700796 np[b] = '\0';
Mark Salyzynae4d9282014-10-15 08:49:39 -0700797
Mark Salyzynb06247d2015-12-04 09:32:27 -0800798 if (!isMonotonic()) {
799 // Watch out for singular race conditions with timezone causing near
800 // integer quarter-hour jumps in the time and compensate accordingly.
801 // Entries will be temporal within near_seconds * 2. b/21868540
802 static uint32_t vote_time[3];
803 vote_time[2] = vote_time[1];
804 vote_time[1] = vote_time[0];
805 vote_time[0] = now.tv_sec;
806
807 if (vote_time[1] && vote_time[2]) {
808 static const unsigned near_seconds = 10;
809 static const unsigned timezones_seconds = 900;
810 int diff0 = (vote_time[0] - vote_time[1]) / near_seconds;
811 unsigned abs0 = (diff0 < 0) ? -diff0 : diff0;
812 int diff1 = (vote_time[1] - vote_time[2]) / near_seconds;
813 unsigned abs1 = (diff1 < 0) ? -diff1 : diff1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800814 if ((abs1 <= 1) && // last two were in agreement on timezone
815 ((abs0 + 1) % (timezones_seconds / near_seconds)) <= 2) {
Mark Salyzynb06247d2015-12-04 09:32:27 -0800816 abs0 = (abs0 + 1) / (timezones_seconds / near_seconds) *
Mark Salyzyn501c3732017-03-10 14:31:54 -0800817 timezones_seconds;
Mark Salyzynb06247d2015-12-04 09:32:27 -0800818 now.tv_sec -= (diff0 < 0) ? -abs0 : abs0;
819 }
820 }
821 }
822
Mark Salyzynae4d9282014-10-15 08:49:39 -0700823 // Log message
Mark Salyzyneb298082015-11-10 15:56:41 -0800824 int rc = logbuf->log(LOG_ID_KERNEL, now, uid, pid, tid, newstr,
Mark Salyzyn501c3732017-03-10 14:31:54 -0800825 (unsigned short)n);
Mark Salyzynae4d9282014-10-15 08:49:39 -0700826
827 // notify readers
828 if (!rc) {
Hao Wangf6e22962017-12-04 14:10:40 +0800829 reader->notifyNewLog(static_cast<log_mask_t>(1 << LOG_ID_KERNEL));
Mark Salyzynae4d9282014-10-15 08:49:39 -0700830 }
831
832 return rc;
833}