2014-10-15 17:49:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
2016-02-23 17:55:43 +01:00
|
|
|
#include <string.h>
|
2014-10-15 17:49:39 +02:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
2016-08-12 01:07:10 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2017-03-10 23:31:54 +01:00
|
|
|
#include <private/android_logger.h>
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2016-02-23 17:55:43 +01:00
|
|
|
#include "LogBuffer.h"
|
2014-10-15 17:49:39 +02:00
|
|
|
#include "LogKlog.h"
|
2016-02-23 17:55:43 +01:00
|
|
|
#include "LogReader.h"
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
#define KMSG_PRIORITY(PRI) \
|
|
|
|
'<', '0' + (LOG_SYSLOG | (PRI)) / 10, '0' + (LOG_SYSLOG | (PRI)) % 10, '>'
|
2014-10-15 17:49:39 +02:00
|
|
|
|
|
|
|
static const char priority_message[] = { KMSG_PRIORITY(LOG_INFO), '\0' };
|
|
|
|
|
2016-08-11 17:02:06 +02:00
|
|
|
// List of the _only_ needles we supply here to android::strnstr
|
|
|
|
static const char suspendStr[] = "PM: suspend entry ";
|
|
|
|
static const char resumeStr[] = "PM: suspend exit ";
|
|
|
|
static const char suspendedStr[] = "Suspended for ";
|
|
|
|
static const char healthdStr[] = "healthd";
|
|
|
|
static const char batteryStr[] = ": battery ";
|
|
|
|
static const char auditStr[] = " audit(";
|
|
|
|
static const char klogdStr[] = "logd.klogd: ";
|
|
|
|
|
2015-05-23 00:23:44 +02:00
|
|
|
// Parsing is hard
|
|
|
|
|
|
|
|
// called if we see a '<', s is the next character, returns pointer after '>'
|
2016-08-11 17:02:06 +02:00
|
|
|
static char* is_prio(char* s, ssize_t len) {
|
|
|
|
if ((len <= 0) || !isdigit(*s++)) return nullptr;
|
2015-09-02 16:39:53 +02:00
|
|
|
--len;
|
|
|
|
static const size_t max_prio_len = (len < 4) ? len : 4;
|
|
|
|
size_t priolen = 0;
|
2015-05-23 00:23:44 +02:00
|
|
|
char c;
|
2015-09-02 16:39:53 +02:00
|
|
|
while (((c = *s++)) && (++priolen <= max_prio_len)) {
|
2016-08-11 17:02:06 +02:00
|
|
|
if (!isdigit(c)) return ((c == '>') && (*s == '[')) ? s : nullptr;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
return nullptr;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// called if we see a '[', s is the next character, returns pointer after ']'
|
2016-08-11 17:02:06 +02:00
|
|
|
static char* is_timestamp(char* s, ssize_t len) {
|
|
|
|
while ((len > 0) && (*s == ' ')) {
|
2015-05-23 00:23:44 +02:00
|
|
|
++s;
|
2015-09-02 16:39:53 +02:00
|
|
|
--len;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((len <= 0) || !isdigit(*s++)) return nullptr;
|
2015-09-02 16:39:53 +02:00
|
|
|
--len;
|
2015-05-23 00:23:44 +02:00
|
|
|
bool first_period = true;
|
|
|
|
char c;
|
2016-08-11 17:02:06 +02:00
|
|
|
while ((len > 0) && ((c = *s++))) {
|
2015-09-02 16:39:53 +02:00
|
|
|
--len;
|
2015-05-23 00:23:44 +02:00
|
|
|
if ((c == '.') && first_period) {
|
|
|
|
first_period = false;
|
2015-06-12 23:59:42 +02:00
|
|
|
} else if (!isdigit(c)) {
|
2016-08-11 17:02:06 +02:00
|
|
|
return ((c == ']') && !first_period && (*s == ' ')) ? s : nullptr;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
return nullptr;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Like strtok_r with "\r\n" except that we look for log signatures (regex)
|
2017-03-10 23:31:54 +01:00
|
|
|
// \(\(<[0-9]\{1,4\}>\)\([[] *[0-9]+[.][0-9]+[]] \)\{0,1\}\|[[]
|
|
|
|
// *[0-9]+[.][0-9]+[]] \)
|
2015-05-23 00:23:44 +02:00
|
|
|
// and split if we see a second one without a newline.
|
2015-09-04 20:37:42 +02:00
|
|
|
// We allow nuls in content, monitoring the overall length and sub-length of
|
|
|
|
// the discovered tokens.
|
2015-05-23 00:23:44 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
#define SIGNATURE_MASK 0xF0
|
2015-05-23 00:23:44 +02:00
|
|
|
// <digit> following ('0' to '9' masked with ~SIGNATURE_MASK) added to signature
|
2017-03-10 23:31:54 +01:00
|
|
|
#define LESS_THAN_SIG SIGNATURE_MASK
|
|
|
|
#define OPEN_BRACKET_SIG ((SIGNATURE_MASK << 1) & SIGNATURE_MASK)
|
2015-05-23 00:23:44 +02:00
|
|
|
// space is one more than <digit> of 9
|
2015-06-08 23:51:30 +02:00
|
|
|
#define OPEN_BRACKET_SPACE ((char)(OPEN_BRACKET_SIG | 10))
|
2015-05-23 00:23:44 +02:00
|
|
|
|
2016-08-11 17:02:06 +02:00
|
|
|
char* android::log_strntok_r(char* s, ssize_t& len, char*& last,
|
|
|
|
ssize_t& sublen) {
|
|
|
|
sublen = 0;
|
|
|
|
if (len <= 0) return nullptr;
|
2015-05-23 00:23:44 +02:00
|
|
|
if (!s) {
|
2016-08-11 17:02:06 +02:00
|
|
|
if (!(s = last)) return nullptr;
|
2015-05-23 00:23:44 +02:00
|
|
|
// fixup for log signature split <,
|
|
|
|
// LESS_THAN_SIG + <digit>
|
|
|
|
if ((*s & SIGNATURE_MASK) == LESS_THAN_SIG) {
|
|
|
|
*s = (*s & ~SIGNATURE_MASK) + '0';
|
|
|
|
*--s = '<';
|
2016-08-11 17:02:06 +02:00
|
|
|
++len;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
|
|
|
// fixup for log signature split [,
|
|
|
|
// OPEN_BRACKET_SPACE is space, OPEN_BRACKET_SIG + <digit>
|
|
|
|
if ((*s & SIGNATURE_MASK) == OPEN_BRACKET_SIG) {
|
2016-08-11 17:02:06 +02:00
|
|
|
*s = (*s == OPEN_BRACKET_SPACE) ? ' ' : (*s & ~SIGNATURE_MASK) + '0';
|
2015-05-23 00:23:44 +02:00
|
|
|
*--s = '[';
|
2016-08-11 17:02:06 +02:00
|
|
|
++len;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 17:02:06 +02:00
|
|
|
while ((len > 0) && ((*s == '\r') || (*s == '\n'))) {
|
2015-09-02 16:39:53 +02:00
|
|
|
++s;
|
2016-08-11 17:02:06 +02:00
|
|
|
--len;
|
2015-09-02 16:39:53 +02:00
|
|
|
}
|
2015-05-23 00:23:44 +02:00
|
|
|
|
2016-08-11 17:02:06 +02:00
|
|
|
if (len <= 0) return last = nullptr;
|
2015-05-23 00:23:44 +02:00
|
|
|
char *peek, *tok = s;
|
|
|
|
|
|
|
|
for (;;) {
|
2016-08-11 17:02:06 +02:00
|
|
|
if (len <= 0) {
|
|
|
|
last = nullptr;
|
2015-05-23 00:23:44 +02:00
|
|
|
return tok;
|
2015-09-02 16:39:53 +02:00
|
|
|
}
|
|
|
|
char c = *s++;
|
2016-08-11 17:02:06 +02:00
|
|
|
--len;
|
|
|
|
ssize_t adjust;
|
2015-09-02 16:39:53 +02:00
|
|
|
switch (c) {
|
2017-03-10 23:31:54 +01:00
|
|
|
case '\r':
|
|
|
|
case '\n':
|
2015-05-23 00:23:44 +02:00
|
|
|
s[-1] = '\0';
|
2016-08-11 17:02:06 +02:00
|
|
|
last = s;
|
2015-05-23 00:23:44 +02:00
|
|
|
return tok;
|
2017-03-10 23:31:54 +01:00
|
|
|
|
|
|
|
case '<':
|
2016-08-11 17:02:06 +02:00
|
|
|
peek = is_prio(s, len);
|
|
|
|
if (!peek) break;
|
2017-03-10 23:31:54 +01:00
|
|
|
if (s != (tok + 1)) { // not first?
|
|
|
|
s[-1] = '\0';
|
|
|
|
*s &= ~SIGNATURE_MASK;
|
|
|
|
*s |= LESS_THAN_SIG; // signature for '<'
|
2016-08-11 17:02:06 +02:00
|
|
|
last = s;
|
2017-03-10 23:31:54 +01:00
|
|
|
return tok;
|
|
|
|
}
|
2015-09-02 16:39:53 +02:00
|
|
|
adjust = peek - s;
|
2016-08-11 17:02:06 +02:00
|
|
|
if (adjust > len) {
|
|
|
|
adjust = len;
|
2015-09-02 16:39:53 +02:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
sublen += adjust;
|
|
|
|
len -= adjust;
|
2015-05-23 00:23:44 +02:00
|
|
|
s = peek;
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((*s == '[') && ((peek = is_timestamp(s + 1, len - 1)))) {
|
2017-03-10 23:31:54 +01:00
|
|
|
adjust = peek - s;
|
2016-08-11 17:02:06 +02:00
|
|
|
if (adjust > len) {
|
|
|
|
adjust = len;
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
sublen += adjust;
|
|
|
|
len -= adjust;
|
2017-03-10 23:31:54 +01:00
|
|
|
s = peek;
|
|
|
|
}
|
2015-05-23 00:23:44 +02:00
|
|
|
break;
|
2017-03-10 23:31:54 +01:00
|
|
|
|
|
|
|
case '[':
|
2016-08-11 17:02:06 +02:00
|
|
|
peek = is_timestamp(s, len);
|
|
|
|
if (!peek) break;
|
2017-03-10 23:31:54 +01:00
|
|
|
if (s != (tok + 1)) { // not first?
|
|
|
|
s[-1] = '\0';
|
|
|
|
if (*s == ' ') {
|
|
|
|
*s = OPEN_BRACKET_SPACE;
|
|
|
|
} else {
|
|
|
|
*s &= ~SIGNATURE_MASK;
|
|
|
|
*s |= OPEN_BRACKET_SIG; // signature for '['
|
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
last = s;
|
2017-03-10 23:31:54 +01:00
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
adjust = peek - s;
|
2016-08-11 17:02:06 +02:00
|
|
|
if (adjust > len) {
|
|
|
|
adjust = len;
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
sublen += adjust;
|
|
|
|
len -= adjust;
|
2017-03-10 23:31:54 +01:00
|
|
|
s = peek;
|
|
|
|
break;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
++sublen;
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
2015-07-13 19:19:34 +02:00
|
|
|
// NOTREACHED
|
2015-05-23 00:23:44 +02:00
|
|
|
}
|
|
|
|
|
2019-01-16 07:25:18 +01:00
|
|
|
log_time LogKlog::correction = (log_time(CLOCK_REALTIME) < log_time(CLOCK_MONOTONIC))
|
|
|
|
? log_time(log_time::EPOCH)
|
|
|
|
: (log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC));
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
LogKlog::LogKlog(LogBuffer* buf, LogReader* reader, int fdWrite, int fdRead,
|
|
|
|
bool auditd)
|
|
|
|
: SocketListener(fdRead, false),
|
|
|
|
logbuf(buf),
|
|
|
|
reader(reader),
|
|
|
|
signature(CLOCK_MONOTONIC),
|
|
|
|
initialized(false),
|
|
|
|
enableLogging(true),
|
|
|
|
auditd(auditd) {
|
2016-08-11 17:02:06 +02:00
|
|
|
static const char klogd_message[] = "%s%s%" PRIu64 "\n";
|
|
|
|
char buffer[strlen(priority_message) + strlen(klogdStr) +
|
|
|
|
strlen(klogd_message) + 20];
|
|
|
|
snprintf(buffer, sizeof(buffer), klogd_message, priority_message, klogdStr,
|
2017-03-10 23:31:54 +01:00
|
|
|
signature.nsec());
|
2014-10-15 17:49:39 +02:00
|
|
|
write(fdWrite, buffer, strlen(buffer));
|
|
|
|
}
|
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
bool LogKlog::onDataAvailable(SocketClient* cli) {
|
2014-10-15 17:49:39 +02:00
|
|
|
if (!initialized) {
|
|
|
|
prctl(PR_SET_NAME, "logd.klogd");
|
|
|
|
initialized = true;
|
|
|
|
enableLogging = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[LOGGER_ENTRY_MAX_PAYLOAD];
|
2016-08-11 17:02:06 +02:00
|
|
|
ssize_t len = 0;
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
for (;;) {
|
2014-10-15 17:49:39 +02:00
|
|
|
ssize_t retval = 0;
|
2016-08-11 17:02:06 +02:00
|
|
|
if (len < (ssize_t)(sizeof(buffer) - 1)) {
|
2017-03-10 23:31:54 +01:00
|
|
|
retval =
|
|
|
|
read(cli->getSocket(), buffer + len, sizeof(buffer) - 1 - len);
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((retval == 0) && (len <= 0)) {
|
2014-10-15 17:49:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (retval < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
len += retval;
|
|
|
|
bool full = len == (sizeof(buffer) - 1);
|
2017-03-10 23:31:54 +01:00
|
|
|
char* ep = buffer + len;
|
2014-10-15 17:49:39 +02:00
|
|
|
*ep = '\0';
|
2016-08-11 17:02:06 +02:00
|
|
|
ssize_t sublen;
|
|
|
|
for (char *ptr = nullptr, *tok = buffer;
|
|
|
|
!!(tok = android::log_strntok_r(tok, len, ptr, sublen));
|
|
|
|
tok = nullptr) {
|
2015-09-02 16:39:53 +02:00
|
|
|
if (((tok + sublen) >= ep) && (retval != 0) && full) {
|
2016-08-11 17:02:06 +02:00
|
|
|
if (sublen > 0) memmove(buffer, tok, sublen);
|
2015-09-02 16:39:53 +02:00
|
|
|
len = sublen;
|
2014-10-15 17:49:39 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((sublen > 0) && *tok) {
|
2015-09-04 20:37:42 +02:00
|
|
|
log(tok, sublen);
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
void LogKlog::calculateCorrection(const log_time& monotonic,
|
2016-08-11 17:02:06 +02:00
|
|
|
const char* real_string, ssize_t len) {
|
|
|
|
static const char real_format[] = "%Y-%m-%d %H:%M:%S.%09q UTC";
|
|
|
|
if (len < (ssize_t)(strlen(real_format) + 5)) return;
|
|
|
|
|
2019-01-16 07:25:18 +01:00
|
|
|
log_time real(log_time::EPOCH);
|
2016-08-11 17:02:06 +02:00
|
|
|
const char* ep = real.strptime(real_string, real_format);
|
2015-12-29 00:33:01 +01:00
|
|
|
if (!ep || (ep > &real_string[len]) || (real > log_time(CLOCK_REALTIME))) {
|
2014-10-15 17:49:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// kernel report UTC, log_time::strptime is localtime from calendar.
|
|
|
|
// Bionic and liblog strptime does not support %z or %Z to pick up
|
|
|
|
// timezone so we are calculating our own correction.
|
|
|
|
time_t now = real.tv_sec;
|
|
|
|
struct tm tm;
|
|
|
|
memset(&tm, 0, sizeof(tm));
|
|
|
|
tm.tm_isdst = -1;
|
|
|
|
localtime_r(&now, &tm);
|
2015-12-30 22:46:07 +01:00
|
|
|
if ((tm.tm_gmtoff < 0) && ((-tm.tm_gmtoff) > (long)real.tv_sec)) {
|
2019-01-16 07:25:18 +01:00
|
|
|
real = log_time(log_time::EPOCH);
|
2015-12-29 00:33:01 +01:00
|
|
|
} else {
|
|
|
|
real.tv_sec += tm.tm_gmtoff;
|
|
|
|
}
|
|
|
|
if (monotonic > real) {
|
2019-01-16 07:25:18 +01:00
|
|
|
correction = log_time(log_time::EPOCH);
|
2015-12-29 00:33:01 +01:00
|
|
|
} else {
|
|
|
|
correction = real - monotonic;
|
|
|
|
}
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-16 07:25:18 +01:00
|
|
|
log_time LogKlog::sniffTime(const char*& buf, ssize_t len, bool reverse) {
|
|
|
|
log_time now(log_time::EPOCH);
|
|
|
|
if (len <= 0) return now;
|
2016-08-11 17:02:06 +02:00
|
|
|
|
|
|
|
const char* cp = nullptr;
|
|
|
|
if ((len > 10) && (*buf == '[')) {
|
|
|
|
cp = now.strptime(buf, "[ %s.%q]"); // can index beyond buffer bounds
|
|
|
|
if (cp && (cp > &buf[len - 1])) cp = nullptr;
|
2015-09-04 20:37:42 +02:00
|
|
|
}
|
|
|
|
if (cp) {
|
2016-08-11 17:02:06 +02:00
|
|
|
len -= cp - buf;
|
|
|
|
if ((len > 0) && isspace(*cp)) {
|
2014-10-15 17:49:39 +02:00
|
|
|
++cp;
|
2015-09-04 20:37:42 +02:00
|
|
|
--len;
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
buf = cp;
|
2015-09-01 22:09:23 +02:00
|
|
|
|
2019-01-16 07:25:18 +01:00
|
|
|
if (isMonotonic()) return now;
|
2015-09-08 17:56:32 +02:00
|
|
|
|
2016-12-13 19:31:29 +01:00
|
|
|
const char* b;
|
2017-03-10 23:31:54 +01:00
|
|
|
if (((b = android::strnstr(cp, len, suspendStr))) &&
|
2016-08-11 17:02:06 +02:00
|
|
|
(((b += strlen(suspendStr)) - cp) < len)) {
|
2015-09-04 20:37:42 +02:00
|
|
|
len -= b - cp;
|
|
|
|
calculateCorrection(now, b, len);
|
2017-03-10 23:31:54 +01:00
|
|
|
} else if (((b = android::strnstr(cp, len, resumeStr))) &&
|
2016-08-11 17:02:06 +02:00
|
|
|
(((b += strlen(resumeStr)) - cp) < len)) {
|
2015-09-04 20:37:42 +02:00
|
|
|
len -= b - cp;
|
|
|
|
calculateCorrection(now, b, len);
|
2016-08-11 17:02:06 +02:00
|
|
|
} else if (((b = android::strnstr(cp, len, healthdStr))) &&
|
|
|
|
(((b += strlen(healthdStr)) - cp) < len) &&
|
|
|
|
((b = android::strnstr(b, len -= b - cp, batteryStr))) &&
|
|
|
|
(((b += strlen(batteryStr)) - cp) < len)) {
|
2015-12-29 00:33:01 +01:00
|
|
|
// NB: healthd is roughly 150us late, so we use it instead to
|
|
|
|
// trigger a check for ntp-induced or hardware clock drift.
|
|
|
|
log_time real(CLOCK_REALTIME);
|
|
|
|
log_time mono(CLOCK_MONOTONIC);
|
2019-01-16 07:25:18 +01:00
|
|
|
correction = (real < mono) ? log_time(log_time::EPOCH) : (real - mono);
|
2017-03-10 23:31:54 +01:00
|
|
|
} else if (((b = android::strnstr(cp, len, suspendedStr))) &&
|
2016-08-11 17:02:06 +02:00
|
|
|
(((b += strlen(suspendStr)) - cp) < len)) {
|
2015-09-04 20:37:42 +02:00
|
|
|
len -= b - cp;
|
2019-01-16 07:25:18 +01:00
|
|
|
log_time real(log_time::EPOCH);
|
2017-03-10 23:31:54 +01:00
|
|
|
char* endp;
|
2015-09-04 20:37:42 +02:00
|
|
|
real.tv_sec = strtol(b, &endp, 10);
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((*endp == '.') && ((endp - b) < len)) {
|
2015-09-01 22:09:23 +02:00
|
|
|
unsigned long multiplier = NS_PER_SEC;
|
|
|
|
real.tv_nsec = 0;
|
2015-09-04 20:37:42 +02:00
|
|
|
len -= endp - b;
|
|
|
|
while (--len && isdigit(*++endp) && (multiplier /= 10)) {
|
2015-09-01 22:09:23 +02:00
|
|
|
real.tv_nsec += (*endp - '0') * multiplier;
|
|
|
|
}
|
2014-10-15 17:49:39 +02:00
|
|
|
if (reverse) {
|
2015-12-29 00:33:01 +01:00
|
|
|
if (real > correction) {
|
2019-01-16 07:25:18 +01:00
|
|
|
correction = log_time(log_time::EPOCH);
|
2015-12-29 00:33:01 +01:00
|
|
|
} else {
|
|
|
|
correction -= real;
|
|
|
|
}
|
2014-10-15 17:49:39 +02:00
|
|
|
} else {
|
|
|
|
correction += real;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
convertMonotonicToReal(now);
|
|
|
|
} else {
|
2015-09-08 17:56:32 +02:00
|
|
|
if (isMonotonic()) {
|
|
|
|
now = log_time(CLOCK_MONOTONIC);
|
|
|
|
} else {
|
|
|
|
now = log_time(CLOCK_REALTIME);
|
|
|
|
}
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
2019-01-16 07:25:18 +01:00
|
|
|
return now;
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 17:02:06 +02:00
|
|
|
pid_t LogKlog::sniffPid(const char*& buf, ssize_t len) {
|
|
|
|
if (len <= 0) return 0;
|
|
|
|
|
|
|
|
const char* cp = buf;
|
|
|
|
// sscanf does a strlen, let's check if the string is not nul terminated.
|
|
|
|
// pseudo out-of-bounds access since we always have an extra char on buffer.
|
|
|
|
if (((ssize_t)strnlen(cp, len) == len) && cp[len]) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-15 17:54:03 +02:00
|
|
|
// HTC kernels with modified printk "c0 1648 "
|
2017-03-10 23:31:54 +01:00
|
|
|
if ((len > 9) && (cp[0] == 'c') && isdigit(cp[1]) &&
|
|
|
|
(isdigit(cp[2]) || (cp[2] == ' ')) && (cp[3] == ' ')) {
|
2016-07-15 17:54:03 +02:00
|
|
|
bool gotDigit = false;
|
|
|
|
int i;
|
|
|
|
for (i = 4; i < 9; ++i) {
|
|
|
|
if (isdigit(cp[i])) {
|
|
|
|
gotDigit = true;
|
|
|
|
} else if (gotDigit || (cp[i] != ' ')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((i == 9) && (cp[i] == ' ')) {
|
|
|
|
int pid = 0;
|
|
|
|
char dummy;
|
|
|
|
if (sscanf(cp + 4, "%d%c", &pid, &dummy) == 2) {
|
2016-08-11 17:02:06 +02:00
|
|
|
buf = cp + 10; // skip-it-all
|
2016-07-15 17:54:03 +02:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 20:37:42 +02:00
|
|
|
while (len) {
|
2015-08-26 18:30:00 +02:00
|
|
|
// Mediatek kernels with modified printk
|
|
|
|
if (*cp == '[') {
|
|
|
|
int pid = 0;
|
|
|
|
char dummy;
|
|
|
|
if (sscanf(cp, "[%d:%*[a-z_./0-9:A-Z]]%c", &pid, &dummy) == 2) {
|
|
|
|
return pid;
|
|
|
|
}
|
2017-03-10 23:31:54 +01:00
|
|
|
break; // Only the first one
|
2015-08-26 18:30:00 +02:00
|
|
|
}
|
|
|
|
++cp;
|
2015-09-04 20:37:42 +02:00
|
|
|
--len;
|
2015-08-26 18:30:00 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-01 22:09:23 +02:00
|
|
|
// kernel log prefix, convert to a kernel log priority number
|
2016-08-11 17:02:06 +02:00
|
|
|
static int parseKernelPrio(const char*& buf, ssize_t len) {
|
2015-09-01 22:09:23 +02:00
|
|
|
int pri = LOG_USER | LOG_INFO;
|
2016-08-11 17:02:06 +02:00
|
|
|
const char* cp = buf;
|
|
|
|
if ((len > 0) && (*cp == '<')) {
|
2015-09-01 22:09:23 +02:00
|
|
|
pri = 0;
|
2017-03-10 23:31:54 +01:00
|
|
|
while (--len && isdigit(*++cp)) {
|
2015-09-01 22:09:23 +02:00
|
|
|
pri = (pri * 10) + *cp - '0';
|
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((len > 0) && (*cp == '>')) {
|
2015-09-01 22:09:23 +02:00
|
|
|
++cp;
|
|
|
|
} else {
|
2016-08-11 17:02:06 +02:00
|
|
|
cp = buf;
|
2015-09-01 22:09:23 +02:00
|
|
|
pri = LOG_USER | LOG_INFO;
|
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
buf = cp;
|
2015-09-01 22:09:23 +02:00
|
|
|
}
|
|
|
|
return pri;
|
|
|
|
}
|
|
|
|
|
2014-10-15 17:49:39 +02:00
|
|
|
// Passed the entire SYSLOG_ACTION_READ_ALL buffer and interpret a
|
|
|
|
// compensated start time.
|
2016-08-11 17:02:06 +02:00
|
|
|
void LogKlog::synchronize(const char* buf, ssize_t len) {
|
2016-12-13 19:31:29 +01:00
|
|
|
const char* cp = android::strnstr(buf, len, suspendStr);
|
2014-10-15 17:49:39 +02:00
|
|
|
if (!cp) {
|
2016-12-13 19:31:29 +01:00
|
|
|
cp = android::strnstr(buf, len, resumeStr);
|
|
|
|
if (!cp) return;
|
2015-09-01 22:09:23 +02:00
|
|
|
} else {
|
2016-12-13 19:31:29 +01:00
|
|
|
const char* rp = android::strnstr(buf, len, resumeStr);
|
|
|
|
if (rp && (rp < cp)) cp = rp;
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
--cp;
|
2015-09-01 22:09:23 +02:00
|
|
|
} while ((cp > buf) && (*cp != '\n'));
|
|
|
|
if (*cp == '\n') {
|
|
|
|
++cp;
|
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
parseKernelPrio(cp, len - (cp - buf));
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2019-01-16 07:25:18 +01:00
|
|
|
log_time now = sniffTime(cp, len - (cp - buf), true);
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2016-12-13 19:31:29 +01:00
|
|
|
const char* suspended = android::strnstr(buf, len, suspendedStr);
|
2014-10-15 17:49:39 +02:00
|
|
|
if (!suspended || (suspended > cp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cp = suspended;
|
|
|
|
|
|
|
|
do {
|
|
|
|
--cp;
|
2015-09-01 22:09:23 +02:00
|
|
|
} while ((cp > buf) && (*cp != '\n'));
|
|
|
|
if (*cp == '\n') {
|
|
|
|
++cp;
|
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
parseKernelPrio(cp, len - (cp - buf));
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2019-01-16 07:25:18 +01:00
|
|
|
sniffTime(cp, len - (cp - buf), true);
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert kernel log priority number into an Android Logger priority number
|
|
|
|
static int convertKernelPrioToAndroidPrio(int pri) {
|
2017-03-10 23:31:54 +01:00
|
|
|
switch (pri & LOG_PRIMASK) {
|
|
|
|
case LOG_EMERG:
|
|
|
|
case LOG_ALERT:
|
|
|
|
case LOG_CRIT:
|
|
|
|
return ANDROID_LOG_FATAL;
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
case LOG_ERR:
|
|
|
|
return ANDROID_LOG_ERROR;
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
case LOG_WARNING:
|
|
|
|
return ANDROID_LOG_WARN;
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
default:
|
|
|
|
case LOG_NOTICE:
|
|
|
|
case LOG_INFO:
|
|
|
|
break;
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
case LOG_DEBUG:
|
|
|
|
return ANDROID_LOG_DEBUG;
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ANDROID_LOG_INFO;
|
|
|
|
}
|
|
|
|
|
2016-08-11 17:02:06 +02:00
|
|
|
static const char* strnrchr(const char* s, ssize_t len, char c) {
|
|
|
|
const char* save = nullptr;
|
|
|
|
for (; len > 0; ++s, len--) {
|
2017-03-10 23:31:54 +01:00
|
|
|
if (*s == c) {
|
|
|
|
save = s;
|
|
|
|
}
|
2015-08-26 18:30:00 +02:00
|
|
|
}
|
2017-03-10 23:31:54 +01:00
|
|
|
return save;
|
2015-08-26 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-15 17:49:39 +02:00
|
|
|
//
|
|
|
|
// log a message into the kernel log buffer
|
|
|
|
//
|
|
|
|
// Filter rules to parse <PRI> <TIME> <tag> and <message> in order for
|
|
|
|
// them to appear correct in the logcat output:
|
|
|
|
//
|
|
|
|
// LOG_KERN (0):
|
|
|
|
// <PRI>[<TIME>] <tag> ":" <message>
|
|
|
|
// <PRI>[<TIME>] <tag> <tag> ":" <message>
|
|
|
|
// <PRI>[<TIME>] <tag> <tag>_work ":" <message>
|
|
|
|
// <PRI>[<TIME>] <tag> '<tag>.<num>' ":" <message>
|
|
|
|
// <PRI>[<TIME>] <tag> '<tag><num>' ":" <message>
|
|
|
|
// <PRI>[<TIME>] <tag>_host '<tag>.<num>' ":" <message>
|
|
|
|
// (unimplemented) <PRI>[<TIME>] <tag> '<num>.<tag>' ":" <message>
|
|
|
|
// <PRI>[<TIME>] "[INFO]"<tag> : <message>
|
|
|
|
// <PRI>[<TIME>] "------------[ cut here ]------------" (?)
|
|
|
|
// <PRI>[<TIME>] "---[ end trace 3225a3070ca3e4ac ]---" (?)
|
|
|
|
// LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS
|
|
|
|
// LOG_UUCP, LOG_CRON, LOG_AUTHPRIV, LOG_FTP:
|
|
|
|
// <PRI+TAG>[<TIME>] (see sys/syslog.h)
|
|
|
|
// Observe:
|
|
|
|
// Minimum tag length = 3 NB: drops things like r5:c00bbadf, but allow PM:
|
|
|
|
// Maximum tag words = 2
|
|
|
|
// Maximum tag length = 16 NB: we are thinking of how ugly logcat can get.
|
|
|
|
// Not a Tag if there is no message content.
|
|
|
|
// leading additional spaces means no tag, inherit last tag.
|
|
|
|
// Not a Tag if <tag>: is "ERROR:", "WARNING:", "INFO:" or "CPU:"
|
|
|
|
// Drop:
|
|
|
|
// empty messages
|
|
|
|
// messages with ' audit(' in them if auditd is running
|
|
|
|
// logd.klogd:
|
|
|
|
// return -1 if message logd.klogd: <signature>
|
|
|
|
//
|
2016-08-11 17:02:06 +02:00
|
|
|
int LogKlog::log(const char* buf, ssize_t len) {
|
|
|
|
if (auditd && android::strnstr(buf, len, auditStr)) {
|
2014-10-15 17:49:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-13 19:31:29 +01:00
|
|
|
const char* p = buf;
|
2016-08-11 17:02:06 +02:00
|
|
|
int pri = parseKernelPrio(p, len);
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2019-01-16 07:25:18 +01:00
|
|
|
log_time now = sniffTime(p, len - (p - buf), false);
|
2014-10-15 17:49:39 +02:00
|
|
|
|
|
|
|
// sniff for start marker
|
2016-08-11 17:02:06 +02:00
|
|
|
const char* start = android::strnstr(p, len - (p - buf), klogdStr);
|
2015-08-26 18:30:00 +02:00
|
|
|
if (start) {
|
2016-08-11 17:02:06 +02:00
|
|
|
uint64_t sig = strtoll(start + strlen(klogdStr), nullptr, 10);
|
2014-10-15 17:49:39 +02:00
|
|
|
if (sig == signature.nsec()) {
|
|
|
|
if (initialized) {
|
|
|
|
enableLogging = true;
|
|
|
|
} else {
|
|
|
|
enableLogging = false;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enableLogging) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:30:00 +02:00
|
|
|
// Parse pid, tid and uid
|
2016-08-11 17:02:06 +02:00
|
|
|
const pid_t pid = sniffPid(p, len - (p - buf));
|
2015-08-26 18:30:00 +02:00
|
|
|
const pid_t tid = pid;
|
2016-08-12 01:07:10 +02:00
|
|
|
uid_t uid = AID_ROOT;
|
|
|
|
if (pid) {
|
2017-04-18 23:09:45 +02:00
|
|
|
logbuf->wrlock();
|
2016-08-12 01:07:10 +02:00
|
|
|
uid = logbuf->pidToUid(pid);
|
|
|
|
logbuf->unlock();
|
|
|
|
}
|
2014-10-15 17:49:39 +02:00
|
|
|
|
|
|
|
// Parse (rules at top) to pull out a tag from the incoming kernel message.
|
|
|
|
// Some may view the following as an ugly heuristic, the desire is to
|
|
|
|
// beautify the kernel logs into an Android Logging format; the goal is
|
|
|
|
// admirable but costly.
|
2015-11-11 00:56:41 +01:00
|
|
|
while ((p < &buf[len]) && (isspace(*p) || !*p)) {
|
2015-09-04 20:37:42 +02:00
|
|
|
++p;
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
2017-03-10 23:31:54 +01:00
|
|
|
if (p >= &buf[len]) { // timestamp, no content
|
2014-10-15 17:49:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-09-04 20:37:42 +02:00
|
|
|
start = p;
|
2017-03-10 23:31:54 +01:00
|
|
|
const char* tag = "";
|
|
|
|
const char* etag = tag;
|
2016-08-11 17:02:06 +02:00
|
|
|
ssize_t taglen = len - (p - buf);
|
2017-03-10 23:31:54 +01:00
|
|
|
const char* bt = p;
|
2016-08-05 17:16:37 +02:00
|
|
|
|
|
|
|
static const char infoBrace[] = "[INFO]";
|
2016-08-11 17:02:06 +02:00
|
|
|
static const ssize_t infoBraceLen = strlen(infoBrace);
|
2017-03-10 23:31:54 +01:00
|
|
|
if ((taglen >= infoBraceLen) &&
|
|
|
|
!fastcmp<strncmp>(p, infoBrace, infoBraceLen)) {
|
2016-08-05 17:16:37 +02:00
|
|
|
// <PRI>[<TIME>] "[INFO]"<tag> ":" message
|
|
|
|
bt = p + infoBraceLen;
|
|
|
|
taglen -= infoBraceLen;
|
|
|
|
}
|
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
const char* et;
|
2016-08-11 17:02:06 +02:00
|
|
|
for (et = bt; (taglen > 0) && *et && (*et != ':') && !isspace(*et);
|
2017-03-10 23:31:54 +01:00
|
|
|
++et, --taglen) {
|
|
|
|
// skip ':' within [ ... ]
|
|
|
|
if (*et == '[') {
|
2016-08-11 17:02:06 +02:00
|
|
|
while ((taglen > 0) && *et && *et != ']') {
|
2017-03-10 23:31:54 +01:00
|
|
|
++et;
|
|
|
|
--taglen;
|
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
if (taglen <= 0) {
|
2017-03-10 23:31:54 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const char* cp;
|
2016-08-11 17:02:06 +02:00
|
|
|
for (cp = et; (taglen > 0) && isspace(*cp); ++cp, --taglen) {
|
2016-08-05 17:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate tag
|
2016-08-11 17:02:06 +02:00
|
|
|
ssize_t size = et - bt;
|
|
|
|
if ((taglen > 0) && (size > 0)) {
|
2014-10-15 17:49:39 +02:00
|
|
|
if (*cp == ':') {
|
2016-08-05 17:16:37 +02:00
|
|
|
// ToDo: handle case insensitive colon separated logging stutter:
|
|
|
|
// <tag> : <tag>: ...
|
|
|
|
|
2014-10-15 17:49:39 +02:00
|
|
|
// One Word
|
|
|
|
tag = bt;
|
|
|
|
etag = et;
|
2015-09-04 20:37:42 +02:00
|
|
|
p = cp + 1;
|
2016-08-05 17:16:37 +02:00
|
|
|
} else if ((taglen > size) && (tolower(*bt) == tolower(*cp))) {
|
|
|
|
// clean up any tag stutter
|
2017-03-10 23:31:54 +01:00
|
|
|
if (!fastcmp<strncasecmp>(bt + 1, cp + 1, size - 1)) { // no match
|
2014-10-15 17:49:39 +02:00
|
|
|
// <PRI>[<TIME>] <tag> <tag> : message
|
2016-08-05 17:16:37 +02:00
|
|
|
// <PRI>[<TIME>] <tag> <tag>: message
|
2014-10-15 17:49:39 +02:00
|
|
|
// <PRI>[<TIME>] <tag> '<tag>.<num>' : message
|
|
|
|
// <PRI>[<TIME>] <tag> '<tag><num>' : message
|
2016-08-05 17:16:37 +02:00
|
|
|
// <PRI>[<TIME>] <tag> '<tag><stuff>' : message
|
2017-03-10 23:31:54 +01:00
|
|
|
const char* b = cp;
|
2014-10-15 17:49:39 +02:00
|
|
|
cp += size;
|
2015-09-04 20:37:42 +02:00
|
|
|
taglen -= size;
|
2016-08-11 17:02:06 +02:00
|
|
|
while ((--taglen > 0) && !isspace(*++cp) && (*cp != ':')) {
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
|
|
|
const char* e;
|
2016-08-11 17:02:06 +02:00
|
|
|
for (e = cp; (taglen > 0) && isspace(*cp); ++cp, --taglen) {
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((taglen > 0) && (*cp == ':')) {
|
2014-10-15 17:49:39 +02:00
|
|
|
tag = b;
|
|
|
|
etag = e;
|
2015-09-04 20:37:42 +02:00
|
|
|
p = cp + 1;
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-08-05 17:16:37 +02:00
|
|
|
// what about <PRI>[<TIME>] <tag>_host '<tag><stuff>' : message
|
|
|
|
static const char host[] = "_host";
|
2016-08-11 17:02:06 +02:00
|
|
|
static const ssize_t hostlen = strlen(host);
|
2016-08-05 17:16:37 +02:00
|
|
|
if ((size > hostlen) &&
|
2017-03-10 23:31:54 +01:00
|
|
|
!fastcmp<strncmp>(bt + size - hostlen, host, hostlen) &&
|
|
|
|
!fastcmp<strncmp>(bt + 1, cp + 1, size - hostlen - 1)) {
|
|
|
|
const char* b = cp;
|
2016-08-05 17:16:37 +02:00
|
|
|
cp += size - hostlen;
|
|
|
|
taglen -= size - hostlen;
|
|
|
|
if (*cp == '.') {
|
2016-08-11 17:02:06 +02:00
|
|
|
while ((--taglen > 0) && !isspace(*++cp) &&
|
|
|
|
(*cp != ':')) {
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
|
|
|
const char* e;
|
2016-08-11 17:02:06 +02:00
|
|
|
for (e = cp; (taglen > 0) && isspace(*cp);
|
|
|
|
++cp, --taglen) {
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((taglen > 0) && (*cp == ':')) {
|
2016-08-05 17:16:37 +02:00
|
|
|
tag = b;
|
|
|
|
etag = e;
|
|
|
|
p = cp + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
goto twoWord;
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-05 17:16:37 +02:00
|
|
|
} else {
|
2017-03-10 23:31:54 +01:00
|
|
|
// <PRI>[<TIME>] <tag> <stuff>' : message
|
|
|
|
twoWord:
|
2016-08-11 17:02:06 +02:00
|
|
|
while ((--taglen > 0) && !isspace(*++cp) && (*cp != ':')) {
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
|
|
|
const char* e;
|
2016-08-11 17:02:06 +02:00
|
|
|
for (e = cp; (taglen > 0) && isspace(*cp); ++cp, --taglen) {
|
2017-03-10 23:31:54 +01:00
|
|
|
}
|
2016-08-05 17:16:37 +02:00
|
|
|
// Two words
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((taglen > 0) && (*cp == ':')) {
|
2016-08-05 17:16:37 +02:00
|
|
|
tag = bt;
|
|
|
|
etag = e;
|
|
|
|
p = cp + 1;
|
|
|
|
}
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
2017-03-10 23:31:54 +01:00
|
|
|
} // else no tag
|
2016-08-05 17:16:37 +02:00
|
|
|
|
|
|
|
static const char cpu[] = "CPU";
|
2016-08-11 17:02:06 +02:00
|
|
|
static const ssize_t cpuLen = strlen(cpu);
|
2016-08-05 17:16:37 +02:00
|
|
|
static const char warning[] = "WARNING";
|
2016-08-11 17:02:06 +02:00
|
|
|
static const ssize_t warningLen = strlen(warning);
|
2016-08-05 17:16:37 +02:00
|
|
|
static const char error[] = "ERROR";
|
2016-08-11 17:02:06 +02:00
|
|
|
static const ssize_t errorLen = strlen(error);
|
2016-08-05 17:16:37 +02:00
|
|
|
static const char info[] = "INFO";
|
2016-08-11 17:02:06 +02:00
|
|
|
static const ssize_t infoLen = strlen(info);
|
2016-08-05 17:16:37 +02:00
|
|
|
|
|
|
|
size = etag - tag;
|
2017-03-10 23:31:54 +01:00
|
|
|
if ((size <= 1) ||
|
2016-08-05 17:16:37 +02:00
|
|
|
// register names like x9
|
2017-03-10 23:31:54 +01:00
|
|
|
((size == 2) && (isdigit(tag[0]) || isdigit(tag[1]))) ||
|
2016-08-05 17:16:37 +02:00
|
|
|
// register names like x18 but not driver names like en0
|
2017-03-10 23:31:54 +01:00
|
|
|
((size == 3) && (isdigit(tag[1]) && isdigit(tag[2]))) ||
|
2016-08-05 17:16:37 +02:00
|
|
|
// blacklist
|
2017-03-10 23:31:54 +01:00
|
|
|
((size == cpuLen) && !fastcmp<strncmp>(tag, cpu, cpuLen)) ||
|
|
|
|
((size == warningLen) &&
|
|
|
|
!fastcmp<strncasecmp>(tag, warning, warningLen)) ||
|
|
|
|
((size == errorLen) && !fastcmp<strncasecmp>(tag, error, errorLen)) ||
|
|
|
|
((size == infoLen) && !fastcmp<strncasecmp>(tag, info, infoLen))) {
|
2016-08-05 17:16:37 +02:00
|
|
|
p = start;
|
|
|
|
etag = tag = "";
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
2016-08-05 17:16:37 +02:00
|
|
|
|
2015-08-26 18:30:00 +02:00
|
|
|
// Suppress additional stutter in tag:
|
|
|
|
// eg: [143:healthd]healthd -> [143:healthd]
|
2015-09-04 20:37:42 +02:00
|
|
|
taglen = etag - tag;
|
2015-08-26 18:30:00 +02:00
|
|
|
// Mediatek-special printk induced stutter
|
2017-03-15 04:43:43 +01:00
|
|
|
const char* mp = strnrchr(tag, taglen, ']');
|
2015-08-26 18:30:00 +02:00
|
|
|
if (mp && (++mp < etag)) {
|
2016-08-11 17:02:06 +02:00
|
|
|
ssize_t s = etag - mp;
|
2016-12-02 19:08:48 +01:00
|
|
|
if (((s + s) < taglen) && !fastcmp<memcmp>(mp, mp - 1 - s, s)) {
|
2015-08-26 18:30:00 +02:00
|
|
|
taglen = mp - tag;
|
2015-08-26 18:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-11 00:56:41 +01:00
|
|
|
// Deal with sloppy and simplistic harmless p = cp + 1 etc above.
|
2016-08-11 17:02:06 +02:00
|
|
|
if (len < (p - buf)) {
|
2015-11-11 00:56:41 +01:00
|
|
|
p = &buf[len];
|
|
|
|
}
|
2015-06-12 23:59:42 +02:00
|
|
|
// skip leading space
|
2015-11-11 00:56:41 +01:00
|
|
|
while ((p < &buf[len]) && (isspace(*p) || !*p)) {
|
2015-09-04 20:37:42 +02:00
|
|
|
++p;
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
2015-09-04 20:37:42 +02:00
|
|
|
// truncate trailing space or nuls
|
2016-08-11 17:02:06 +02:00
|
|
|
ssize_t b = len - (p - buf);
|
|
|
|
while ((b > 0) && (isspace(p[b - 1]) || !p[b - 1])) {
|
2015-06-12 23:59:42 +02:00
|
|
|
--b;
|
|
|
|
}
|
|
|
|
// trick ... allow tag with empty content to be logged. log() drops empty
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((b <= 0) && (taglen > 0)) {
|
2015-09-04 20:37:42 +02:00
|
|
|
p = " ";
|
2015-06-12 23:59:42 +02:00
|
|
|
b = 1;
|
|
|
|
}
|
2015-11-11 00:56:41 +01:00
|
|
|
// paranoid sanity check, can not happen ...
|
2015-10-02 18:22:52 +02:00
|
|
|
if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
|
|
|
|
b = LOGGER_ENTRY_MAX_PAYLOAD;
|
|
|
|
}
|
2015-11-11 00:56:41 +01:00
|
|
|
if (taglen > LOGGER_ENTRY_MAX_PAYLOAD) {
|
|
|
|
taglen = LOGGER_ENTRY_MAX_PAYLOAD;
|
|
|
|
}
|
|
|
|
// calculate buffer copy requirements
|
2016-08-11 17:02:06 +02:00
|
|
|
ssize_t n = 1 + taglen + 1 + b + 1;
|
2015-11-11 00:56:41 +01:00
|
|
|
// paranoid sanity check, first two just can not happen ...
|
2016-08-11 17:02:06 +02:00
|
|
|
if ((taglen > n) || (b > n) || (n > (ssize_t)USHRT_MAX) || (n <= 0)) {
|
2015-11-11 00:56:41 +01:00
|
|
|
return -EINVAL;
|
2015-08-26 18:30:00 +02:00
|
|
|
}
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2015-11-11 00:56:41 +01:00
|
|
|
// Careful.
|
|
|
|
// We are using the stack to house the log buffer for speed reasons.
|
|
|
|
// If we malloc'd this buffer, we could get away without n's USHRT_MAX
|
|
|
|
// test above, but we would then required a max(n, USHRT_MAX) as
|
|
|
|
// truncating length argument to logbuf->log() below. Gain is protection
|
|
|
|
// of stack sanity and speedup, loss is truncated long-line content.
|
2015-10-02 18:22:52 +02:00
|
|
|
char newstr[n];
|
2017-03-10 23:31:54 +01:00
|
|
|
char* np = newstr;
|
2014-10-15 17:49:39 +02:00
|
|
|
|
|
|
|
// Convert priority into single-byte Android logger priority
|
|
|
|
*np = convertKernelPrioToAndroidPrio(pri);
|
|
|
|
++np;
|
|
|
|
|
|
|
|
// Copy parsed tag following priority
|
2015-09-04 20:37:42 +02:00
|
|
|
memcpy(np, tag, taglen);
|
2015-08-26 18:30:00 +02:00
|
|
|
np += taglen;
|
2014-10-15 17:49:39 +02:00
|
|
|
*np = '\0';
|
|
|
|
++np;
|
|
|
|
|
|
|
|
// Copy main message to the remainder
|
2015-09-04 20:37:42 +02:00
|
|
|
memcpy(np, p, b);
|
2015-06-12 23:59:42 +02:00
|
|
|
np[b] = '\0';
|
2014-10-15 17:49:39 +02:00
|
|
|
|
2015-12-04 18:32:27 +01:00
|
|
|
if (!isMonotonic()) {
|
|
|
|
// Watch out for singular race conditions with timezone causing near
|
|
|
|
// integer quarter-hour jumps in the time and compensate accordingly.
|
|
|
|
// Entries will be temporal within near_seconds * 2. b/21868540
|
|
|
|
static uint32_t vote_time[3];
|
|
|
|
vote_time[2] = vote_time[1];
|
|
|
|
vote_time[1] = vote_time[0];
|
|
|
|
vote_time[0] = now.tv_sec;
|
|
|
|
|
|
|
|
if (vote_time[1] && vote_time[2]) {
|
|
|
|
static const unsigned near_seconds = 10;
|
|
|
|
static const unsigned timezones_seconds = 900;
|
|
|
|
int diff0 = (vote_time[0] - vote_time[1]) / near_seconds;
|
|
|
|
unsigned abs0 = (diff0 < 0) ? -diff0 : diff0;
|
|
|
|
int diff1 = (vote_time[1] - vote_time[2]) / near_seconds;
|
|
|
|
unsigned abs1 = (diff1 < 0) ? -diff1 : diff1;
|
2017-03-10 23:31:54 +01:00
|
|
|
if ((abs1 <= 1) && // last two were in agreement on timezone
|
|
|
|
((abs0 + 1) % (timezones_seconds / near_seconds)) <= 2) {
|
2015-12-04 18:32:27 +01:00
|
|
|
abs0 = (abs0 + 1) / (timezones_seconds / near_seconds) *
|
2017-03-10 23:31:54 +01:00
|
|
|
timezones_seconds;
|
2015-12-04 18:32:27 +01:00
|
|
|
now.tv_sec -= (diff0 < 0) ? -abs0 : abs0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 17:49:39 +02:00
|
|
|
// Log message
|
2018-08-13 23:22:56 +02:00
|
|
|
int rc = logbuf->log(LOG_ID_KERNEL, now, uid, pid, tid, newstr, (uint16_t)n);
|
2014-10-15 17:49:39 +02:00
|
|
|
|
|
|
|
// notify readers
|
2018-04-24 23:13:30 +02:00
|
|
|
if (rc > 0) {
|
2017-12-04 07:10:40 +01:00
|
|
|
reader->notifyNewLog(static_cast<log_mask_t>(1 << LOG_ID_KERNEL));
|
2014-10-15 17:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|