Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | 317bfb9 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 23 | #include <string.h> |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 24 | #include <sys/prctl.h> |
| 25 | #include <sys/uio.h> |
| 26 | #include <syslog.h> |
| 27 | |
Mark Salyzyn | 83b2478 | 2016-08-11 16:07:10 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 29 | #include <private/android_logger.h> |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 30 | |
Mark Salyzyn | 317bfb9 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 31 | #include "LogBuffer.h" |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 32 | #include "LogKlog.h" |
Mark Salyzyn | 317bfb9 | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 33 | #include "LogReader.h" |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 34 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 35 | #define KMSG_PRIORITY(PRI) \ |
| 36 | '<', '0' + (LOG_SYSLOG | (PRI)) / 10, '0' + (LOG_SYSLOG | (PRI)) % 10, '>' |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 37 | |
| 38 | static const char priority_message[] = { KMSG_PRIORITY(LOG_INFO), '\0' }; |
| 39 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 40 | // List of the _only_ needles we supply here to android::strnstr |
| 41 | static const char suspendStr[] = "PM: suspend entry "; |
| 42 | static const char resumeStr[] = "PM: suspend exit "; |
| 43 | static const char suspendedStr[] = "Suspended for "; |
| 44 | static const char healthdStr[] = "healthd"; |
| 45 | static const char batteryStr[] = ": battery "; |
| 46 | static const char auditStr[] = " audit("; |
| 47 | static const char klogdStr[] = "logd.klogd: "; |
| 48 | |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 49 | // Parsing is hard |
| 50 | |
| 51 | // called if we see a '<', s is the next character, returns pointer after '>' |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 52 | static char* is_prio(char* s, ssize_t len) { |
| 53 | if ((len <= 0) || !isdigit(*s++)) return nullptr; |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 54 | --len; |
| 55 | static const size_t max_prio_len = (len < 4) ? len : 4; |
| 56 | size_t priolen = 0; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 57 | char c; |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 58 | while (((c = *s++)) && (++priolen <= max_prio_len)) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 59 | if (!isdigit(c)) return ((c == '>') && (*s == '[')) ? s : nullptr; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 60 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 61 | return nullptr; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // called if we see a '[', s is the next character, returns pointer after ']' |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 65 | static char* is_timestamp(char* s, ssize_t len) { |
| 66 | while ((len > 0) && (*s == ' ')) { |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 67 | ++s; |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 68 | --len; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 69 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 70 | if ((len <= 0) || !isdigit(*s++)) return nullptr; |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 71 | --len; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 72 | bool first_period = true; |
| 73 | char c; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 74 | while ((len > 0) && ((c = *s++))) { |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 75 | --len; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 76 | if ((c == '.') && first_period) { |
| 77 | first_period = false; |
Mark Salyzyn | ee49c6a | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 78 | } else if (!isdigit(c)) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 79 | return ((c == ']') && !first_period && (*s == ' ')) ? s : nullptr; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 82 | return nullptr; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Like strtok_r with "\r\n" except that we look for log signatures (regex) |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 86 | // \(\(<[0-9]\{1,4\}>\)\([[] *[0-9]+[.][0-9]+[]] \)\{0,1\}\|[[] |
| 87 | // *[0-9]+[.][0-9]+[]] \) |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 88 | // and split if we see a second one without a newline. |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 89 | // We allow nuls in content, monitoring the overall length and sub-length of |
| 90 | // the discovered tokens. |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 91 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 92 | #define SIGNATURE_MASK 0xF0 |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 93 | // <digit> following ('0' to '9' masked with ~SIGNATURE_MASK) added to signature |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 94 | #define LESS_THAN_SIG SIGNATURE_MASK |
| 95 | #define OPEN_BRACKET_SIG ((SIGNATURE_MASK << 1) & SIGNATURE_MASK) |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 96 | // space is one more than <digit> of 9 |
Mark Salyzyn | 3e21de2 | 2015-06-08 14:51:30 -0700 | [diff] [blame] | 97 | #define OPEN_BRACKET_SPACE ((char)(OPEN_BRACKET_SIG | 10)) |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 98 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 99 | char* 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 Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 103 | if (!s) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 104 | if (!(s = last)) return nullptr; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 105 | // 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 Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 110 | ++len; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 111 | } |
| 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 Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 115 | *s = (*s == OPEN_BRACKET_SPACE) ? ' ' : (*s & ~SIGNATURE_MASK) + '0'; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 116 | *--s = '['; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 117 | ++len; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 121 | while ((len > 0) && ((*s == '\r') || (*s == '\n'))) { |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 122 | ++s; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 123 | --len; |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 124 | } |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 125 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 126 | if (len <= 0) return last = nullptr; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 127 | char *peek, *tok = s; |
| 128 | |
| 129 | for (;;) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 130 | if (len <= 0) { |
| 131 | last = nullptr; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 132 | return tok; |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 133 | } |
| 134 | char c = *s++; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 135 | --len; |
| 136 | ssize_t adjust; |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 137 | switch (c) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 138 | case '\r': |
| 139 | case '\n': |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 140 | s[-1] = '\0'; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 141 | last = s; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 142 | return tok; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 143 | |
| 144 | case '<': |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 145 | peek = is_prio(s, len); |
| 146 | if (!peek) break; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 147 | if (s != (tok + 1)) { // not first? |
| 148 | s[-1] = '\0'; |
| 149 | *s &= ~SIGNATURE_MASK; |
| 150 | *s |= LESS_THAN_SIG; // signature for '<' |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 151 | last = s; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 152 | return tok; |
| 153 | } |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 154 | adjust = peek - s; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 155 | if (adjust > len) { |
| 156 | adjust = len; |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 157 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 158 | sublen += adjust; |
| 159 | len -= adjust; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 160 | s = peek; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 161 | if ((*s == '[') && ((peek = is_timestamp(s + 1, len - 1)))) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 162 | adjust = peek - s; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 163 | if (adjust > len) { |
| 164 | adjust = len; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 165 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 166 | sublen += adjust; |
| 167 | len -= adjust; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 168 | s = peek; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 169 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 170 | break; |
| 171 | |
| 172 | case '[': |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 173 | peek = is_timestamp(s, len); |
| 174 | if (!peek) break; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 175 | 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 Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 183 | last = s; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 184 | return tok; |
| 185 | } |
| 186 | adjust = peek - s; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 187 | if (adjust > len) { |
| 188 | adjust = len; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 189 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 190 | sublen += adjust; |
| 191 | len -= adjust; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 192 | s = peek; |
| 193 | break; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 194 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 195 | ++sublen; |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 196 | } |
Mark Salyzyn | 618d0de | 2015-07-13 10:19:34 -0700 | [diff] [blame] | 197 | // NOTREACHED |
Mark Salyzyn | 2c3b300 | 2015-05-22 15:23:44 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 200 | log_time LogKlog::correction = (log_time(CLOCK_REALTIME) < log_time(CLOCK_MONOTONIC)) |
| 201 | ? log_time(log_time::EPOCH) |
| 202 | : (log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC)); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 203 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 204 | LogKlog::LogKlog(LogBuffer* buf, LogReader* reader, int fdWrite, int fdRead, |
| 205 | bool auditd) |
| 206 | : SocketListener(fdRead, false), |
| 207 | logbuf(buf), |
| 208 | reader(reader), |
| 209 | signature(CLOCK_MONOTONIC), |
| 210 | initialized(false), |
| 211 | enableLogging(true), |
| 212 | auditd(auditd) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 213 | static const char klogd_message[] = "%s%s%" PRIu64 "\n"; |
| 214 | char buffer[strlen(priority_message) + strlen(klogdStr) + |
| 215 | strlen(klogd_message) + 20]; |
| 216 | snprintf(buffer, sizeof(buffer), klogd_message, priority_message, klogdStr, |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 217 | signature.nsec()); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 218 | write(fdWrite, buffer, strlen(buffer)); |
| 219 | } |
| 220 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 221 | bool LogKlog::onDataAvailable(SocketClient* cli) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 222 | if (!initialized) { |
| 223 | prctl(PR_SET_NAME, "logd.klogd"); |
| 224 | initialized = true; |
| 225 | enableLogging = false; |
| 226 | } |
| 227 | |
| 228 | char buffer[LOGGER_ENTRY_MAX_PAYLOAD]; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 229 | ssize_t len = 0; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 230 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 231 | for (;;) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 232 | ssize_t retval = 0; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 233 | if (len < (ssize_t)(sizeof(buffer) - 1)) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 234 | retval = |
| 235 | read(cli->getSocket(), buffer + len, sizeof(buffer) - 1 - len); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 236 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 237 | if ((retval == 0) && (len <= 0)) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | if (retval < 0) { |
| 241 | return false; |
| 242 | } |
| 243 | len += retval; |
| 244 | bool full = len == (sizeof(buffer) - 1); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 245 | char* ep = buffer + len; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 246 | *ep = '\0'; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 247 | ssize_t sublen; |
| 248 | for (char *ptr = nullptr, *tok = buffer; |
| 249 | !!(tok = android::log_strntok_r(tok, len, ptr, sublen)); |
| 250 | tok = nullptr) { |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 251 | if (((tok + sublen) >= ep) && (retval != 0) && full) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 252 | if (sublen > 0) memmove(buffer, tok, sublen); |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 253 | len = sublen; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 254 | break; |
| 255 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 256 | if ((sublen > 0) && *tok) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 257 | log(tok, sublen); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return true; |
| 263 | } |
| 264 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 265 | void LogKlog::calculateCorrection(const log_time& monotonic, |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 266 | const char* real_string, ssize_t len) { |
| 267 | static const char real_format[] = "%Y-%m-%d %H:%M:%S.%09q UTC"; |
| 268 | if (len < (ssize_t)(strlen(real_format) + 5)) return; |
| 269 | |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 270 | log_time real(log_time::EPOCH); |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 271 | const char* ep = real.strptime(real_string, real_format); |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 272 | if (!ep || (ep > &real_string[len]) || (real > log_time(CLOCK_REALTIME))) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | // kernel report UTC, log_time::strptime is localtime from calendar. |
| 276 | // Bionic and liblog strptime does not support %z or %Z to pick up |
| 277 | // timezone so we are calculating our own correction. |
| 278 | time_t now = real.tv_sec; |
| 279 | struct tm tm; |
| 280 | memset(&tm, 0, sizeof(tm)); |
| 281 | tm.tm_isdst = -1; |
| 282 | localtime_r(&now, &tm); |
Mark Salyzyn | ff8b8e8 | 2015-12-30 13:46:07 -0800 | [diff] [blame] | 283 | if ((tm.tm_gmtoff < 0) && ((-tm.tm_gmtoff) > (long)real.tv_sec)) { |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 284 | real = log_time(log_time::EPOCH); |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 285 | } else { |
| 286 | real.tv_sec += tm.tm_gmtoff; |
| 287 | } |
| 288 | if (monotonic > real) { |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 289 | correction = log_time(log_time::EPOCH); |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 290 | } else { |
| 291 | correction = real - monotonic; |
| 292 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 295 | log_time LogKlog::sniffTime(const char*& buf, ssize_t len, bool reverse) { |
| 296 | log_time now(log_time::EPOCH); |
| 297 | if (len <= 0) return now; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 298 | |
| 299 | const char* cp = nullptr; |
| 300 | if ((len > 10) && (*buf == '[')) { |
| 301 | cp = now.strptime(buf, "[ %s.%q]"); // can index beyond buffer bounds |
| 302 | if (cp && (cp > &buf[len - 1])) cp = nullptr; |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 303 | } |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 304 | if (cp) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 305 | len -= cp - buf; |
| 306 | if ((len > 0) && isspace(*cp)) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 307 | ++cp; |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 308 | --len; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 309 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 310 | buf = cp; |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 311 | |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 312 | if (isMonotonic()) return now; |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 313 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 314 | const char* b; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 315 | if (((b = android::strnstr(cp, len, suspendStr))) && |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 316 | (((b += strlen(suspendStr)) - cp) < len)) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 317 | len -= b - cp; |
| 318 | calculateCorrection(now, b, len); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 319 | } else if (((b = android::strnstr(cp, len, resumeStr))) && |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 320 | (((b += strlen(resumeStr)) - cp) < len)) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 321 | len -= b - cp; |
| 322 | calculateCorrection(now, b, len); |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 323 | } else if (((b = android::strnstr(cp, len, healthdStr))) && |
| 324 | (((b += strlen(healthdStr)) - cp) < len) && |
| 325 | ((b = android::strnstr(b, len -= b - cp, batteryStr))) && |
| 326 | (((b += strlen(batteryStr)) - cp) < len)) { |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 327 | // NB: healthd is roughly 150us late, so we use it instead to |
| 328 | // trigger a check for ntp-induced or hardware clock drift. |
| 329 | log_time real(CLOCK_REALTIME); |
| 330 | log_time mono(CLOCK_MONOTONIC); |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 331 | correction = (real < mono) ? log_time(log_time::EPOCH) : (real - mono); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 332 | } else if (((b = android::strnstr(cp, len, suspendedStr))) && |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 333 | (((b += strlen(suspendStr)) - cp) < len)) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 334 | len -= b - cp; |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 335 | log_time real(log_time::EPOCH); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 336 | char* endp; |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 337 | real.tv_sec = strtol(b, &endp, 10); |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 338 | if ((*endp == '.') && ((endp - b) < len)) { |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 339 | unsigned long multiplier = NS_PER_SEC; |
| 340 | real.tv_nsec = 0; |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 341 | len -= endp - b; |
| 342 | while (--len && isdigit(*++endp) && (multiplier /= 10)) { |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 343 | real.tv_nsec += (*endp - '0') * multiplier; |
| 344 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 345 | if (reverse) { |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 346 | if (real > correction) { |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 347 | correction = log_time(log_time::EPOCH); |
Mark Salyzyn | 10b82b6 | 2015-12-28 15:33:01 -0800 | [diff] [blame] | 348 | } else { |
| 349 | correction -= real; |
| 350 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 351 | } else { |
| 352 | correction += real; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | convertMonotonicToReal(now); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 358 | } else { |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 359 | if (isMonotonic()) { |
| 360 | now = log_time(CLOCK_MONOTONIC); |
| 361 | } else { |
| 362 | now = log_time(CLOCK_REALTIME); |
| 363 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 364 | } |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 365 | return now; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 368 | pid_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 Salyzyn | 0b4a63d | 2016-07-15 08:54:03 -0700 | [diff] [blame] | 377 | // HTC kernels with modified printk "c0 1648 " |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 378 | if ((len > 9) && (cp[0] == 'c') && isdigit(cp[1]) && |
| 379 | (isdigit(cp[2]) || (cp[2] == ' ')) && (cp[3] == ' ')) { |
Mark Salyzyn | 0b4a63d | 2016-07-15 08:54:03 -0700 | [diff] [blame] | 380 | 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 Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 393 | buf = cp + 10; // skip-it-all |
Mark Salyzyn | 0b4a63d | 2016-07-15 08:54:03 -0700 | [diff] [blame] | 394 | return pid; |
| 395 | } |
| 396 | } |
| 397 | } |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 398 | while (len) { |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 399 | // 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 Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 406 | break; // Only the first one |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 407 | } |
| 408 | ++cp; |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 409 | --len; |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 410 | } |
| 411 | return 0; |
| 412 | } |
| 413 | |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 414 | // kernel log prefix, convert to a kernel log priority number |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 415 | static int parseKernelPrio(const char*& buf, ssize_t len) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 416 | int pri = LOG_USER | LOG_INFO; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 417 | const char* cp = buf; |
| 418 | if ((len > 0) && (*cp == '<')) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 419 | pri = 0; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 420 | while (--len && isdigit(*++cp)) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 421 | pri = (pri * 10) + *cp - '0'; |
| 422 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 423 | if ((len > 0) && (*cp == '>')) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 424 | ++cp; |
| 425 | } else { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 426 | cp = buf; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 427 | pri = LOG_USER | LOG_INFO; |
| 428 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 429 | buf = cp; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 430 | } |
| 431 | return pri; |
| 432 | } |
| 433 | |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 434 | // Passed the entire SYSLOG_ACTION_READ_ALL buffer and interpret a |
| 435 | // compensated start time. |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 436 | void LogKlog::synchronize(const char* buf, ssize_t len) { |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 437 | const char* cp = android::strnstr(buf, len, suspendStr); |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 438 | if (!cp) { |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 439 | cp = android::strnstr(buf, len, resumeStr); |
| 440 | if (!cp) return; |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 441 | } else { |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 442 | const char* rp = android::strnstr(buf, len, resumeStr); |
| 443 | if (rp && (rp < cp)) cp = rp; |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | do { |
| 447 | --cp; |
| 448 | } while ((cp > buf) && (*cp != '\n')); |
| 449 | if (*cp == '\n') { |
| 450 | ++cp; |
| 451 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 452 | parseKernelPrio(cp, len - (cp - buf)); |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 453 | |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 454 | log_time now = sniffTime(cp, len - (cp - buf), true); |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 455 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 456 | const char* suspended = android::strnstr(buf, len, suspendedStr); |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 457 | if (!suspended || (suspended > cp)) { |
| 458 | return; |
| 459 | } |
| 460 | cp = suspended; |
| 461 | |
| 462 | do { |
| 463 | --cp; |
| 464 | } while ((cp > buf) && (*cp != '\n')); |
| 465 | if (*cp == '\n') { |
| 466 | ++cp; |
| 467 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 468 | parseKernelPrio(cp, len - (cp - buf)); |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 469 | |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 470 | sniffTime(cp, len - (cp - buf), true); |
Mark Salyzyn | 2d159bf | 2015-09-01 13:09:23 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 473 | // Convert kernel log priority number into an Android Logger priority number |
| 474 | static int convertKernelPrioToAndroidPrio(int pri) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 475 | switch (pri & LOG_PRIMASK) { |
| 476 | case LOG_EMERG: |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 477 | case LOG_ALERT: |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 478 | case LOG_CRIT: |
| 479 | return ANDROID_LOG_FATAL; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 480 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 481 | case LOG_ERR: |
| 482 | return ANDROID_LOG_ERROR; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 483 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 484 | case LOG_WARNING: |
| 485 | return ANDROID_LOG_WARN; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 486 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 487 | default: |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 488 | case LOG_NOTICE: |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 489 | case LOG_INFO: |
| 490 | break; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 491 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 492 | case LOG_DEBUG: |
| 493 | return ANDROID_LOG_DEBUG; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | return ANDROID_LOG_INFO; |
| 497 | } |
| 498 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 499 | static const char* strnrchr(const char* s, ssize_t len, char c) { |
| 500 | const char* save = nullptr; |
| 501 | for (; len > 0; ++s, len--) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 502 | if (*s == c) { |
| 503 | save = s; |
| 504 | } |
Mark Salyzyn | 47dba71 | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 505 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 506 | return save; |
Mark Salyzyn | 47dba71 | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 509 | // |
| 510 | // log a message into the kernel log buffer |
| 511 | // |
| 512 | // Filter rules to parse <PRI> <TIME> <tag> and <message> in order for |
| 513 | // them to appear correct in the logcat output: |
| 514 | // |
| 515 | // LOG_KERN (0): |
| 516 | // <PRI>[<TIME>] <tag> ":" <message> |
| 517 | // <PRI>[<TIME>] <tag> <tag> ":" <message> |
| 518 | // <PRI>[<TIME>] <tag> <tag>_work ":" <message> |
| 519 | // <PRI>[<TIME>] <tag> '<tag>.<num>' ":" <message> |
| 520 | // <PRI>[<TIME>] <tag> '<tag><num>' ":" <message> |
| 521 | // <PRI>[<TIME>] <tag>_host '<tag>.<num>' ":" <message> |
| 522 | // (unimplemented) <PRI>[<TIME>] <tag> '<num>.<tag>' ":" <message> |
| 523 | // <PRI>[<TIME>] "[INFO]"<tag> : <message> |
| 524 | // <PRI>[<TIME>] "------------[ cut here ]------------" (?) |
| 525 | // <PRI>[<TIME>] "---[ end trace 3225a3070ca3e4ac ]---" (?) |
| 526 | // LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS |
| 527 | // LOG_UUCP, LOG_CRON, LOG_AUTHPRIV, LOG_FTP: |
| 528 | // <PRI+TAG>[<TIME>] (see sys/syslog.h) |
| 529 | // Observe: |
| 530 | // Minimum tag length = 3 NB: drops things like r5:c00bbadf, but allow PM: |
| 531 | // Maximum tag words = 2 |
| 532 | // Maximum tag length = 16 NB: we are thinking of how ugly logcat can get. |
| 533 | // Not a Tag if there is no message content. |
| 534 | // leading additional spaces means no tag, inherit last tag. |
| 535 | // Not a Tag if <tag>: is "ERROR:", "WARNING:", "INFO:" or "CPU:" |
| 536 | // Drop: |
| 537 | // empty messages |
| 538 | // messages with ' audit(' in them if auditd is running |
| 539 | // logd.klogd: |
| 540 | // return -1 if message logd.klogd: <signature> |
| 541 | // |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 542 | int LogKlog::log(const char* buf, ssize_t len) { |
| 543 | if (auditd && android::strnstr(buf, len, auditStr)) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | |
Mark Salyzyn | a2c0222 | 2016-12-13 10:31:29 -0800 | [diff] [blame] | 547 | const char* p = buf; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 548 | int pri = parseKernelPrio(p, len); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 549 | |
Bernie Innocenti | 804e7d8 | 2019-01-16 15:25:18 +0900 | [diff] [blame] | 550 | log_time now = sniffTime(p, len - (p - buf), false); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 551 | |
| 552 | // sniff for start marker |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 553 | const char* start = android::strnstr(p, len - (p - buf), klogdStr); |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 554 | if (start) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 555 | uint64_t sig = strtoll(start + strlen(klogdStr), nullptr, 10); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 556 | if (sig == signature.nsec()) { |
| 557 | if (initialized) { |
| 558 | enableLogging = true; |
| 559 | } else { |
| 560 | enableLogging = false; |
| 561 | } |
| 562 | return -1; |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | if (!enableLogging) { |
| 568 | return 0; |
| 569 | } |
| 570 | |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 571 | // Parse pid, tid and uid |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 572 | const pid_t pid = sniffPid(p, len - (p - buf)); |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 573 | const pid_t tid = pid; |
Mark Salyzyn | 83b2478 | 2016-08-11 16:07:10 -0700 | [diff] [blame] | 574 | uid_t uid = AID_ROOT; |
| 575 | if (pid) { |
Mark Salyzyn | 3c501b5 | 2017-04-18 14:09:45 -0700 | [diff] [blame] | 576 | logbuf->wrlock(); |
Mark Salyzyn | 83b2478 | 2016-08-11 16:07:10 -0700 | [diff] [blame] | 577 | uid = logbuf->pidToUid(pid); |
| 578 | logbuf->unlock(); |
| 579 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 580 | |
| 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 Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 585 | while ((p < &buf[len]) && (isspace(*p) || !*p)) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 586 | ++p; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 587 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 588 | if (p >= &buf[len]) { // timestamp, no content |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 589 | return 0; |
| 590 | } |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 591 | start = p; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 592 | const char* tag = ""; |
| 593 | const char* etag = tag; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 594 | ssize_t taglen = len - (p - buf); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 595 | const char* bt = p; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 596 | |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 597 | static const char infoBrace[] = "[INFO]"; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 598 | static const ssize_t infoBraceLen = strlen(infoBrace); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 599 | if ((taglen >= infoBraceLen) && |
| 600 | !fastcmp<strncmp>(p, infoBrace, infoBraceLen)) { |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 601 | // <PRI>[<TIME>] "[INFO]"<tag> ":" message |
| 602 | bt = p + infoBraceLen; |
| 603 | taglen -= infoBraceLen; |
| 604 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 605 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 606 | const char* et; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 607 | for (et = bt; (taglen > 0) && *et && (*et != ':') && !isspace(*et); |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 608 | ++et, --taglen) { |
| 609 | // skip ':' within [ ... ] |
| 610 | if (*et == '[') { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 611 | while ((taglen > 0) && *et && *et != ']') { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 612 | ++et; |
| 613 | --taglen; |
| 614 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 615 | if (taglen <= 0) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 616 | break; |
| 617 | } |
| 618 | } |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 619 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 620 | const char* cp; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 621 | for (cp = et; (taglen > 0) && isspace(*cp); ++cp, --taglen) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 622 | } |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 623 | |
| 624 | // Validate tag |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 625 | ssize_t size = et - bt; |
| 626 | if ((taglen > 0) && (size > 0)) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 627 | if (*cp == ':') { |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 628 | // ToDo: handle case insensitive colon separated logging stutter: |
| 629 | // <tag> : <tag>: ... |
| 630 | |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 631 | // One Word |
| 632 | tag = bt; |
| 633 | etag = et; |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 634 | p = cp + 1; |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 635 | } else if ((taglen > size) && (tolower(*bt) == tolower(*cp))) { |
| 636 | // clean up any tag stutter |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 637 | if (!fastcmp<strncasecmp>(bt + 1, cp + 1, size - 1)) { // no match |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 638 | // <PRI>[<TIME>] <tag> <tag> : message |
| 639 | // <PRI>[<TIME>] <tag> <tag>: message |
| 640 | // <PRI>[<TIME>] <tag> '<tag>.<num>' : message |
| 641 | // <PRI>[<TIME>] <tag> '<tag><num>' : message |
| 642 | // <PRI>[<TIME>] <tag> '<tag><stuff>' : message |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 643 | const char* b = cp; |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 644 | cp += size; |
| 645 | taglen -= size; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 646 | while ((--taglen > 0) && !isspace(*++cp) && (*cp != ':')) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 647 | } |
| 648 | const char* e; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 649 | for (e = cp; (taglen > 0) && isspace(*cp); ++cp, --taglen) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 650 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 651 | if ((taglen > 0) && (*cp == ':')) { |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 652 | tag = b; |
| 653 | etag = e; |
| 654 | p = cp + 1; |
| 655 | } |
| 656 | } else { |
| 657 | // what about <PRI>[<TIME>] <tag>_host '<tag><stuff>' : message |
| 658 | static const char host[] = "_host"; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 659 | static const ssize_t hostlen = strlen(host); |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 660 | if ((size > hostlen) && |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 661 | !fastcmp<strncmp>(bt + size - hostlen, host, hostlen) && |
| 662 | !fastcmp<strncmp>(bt + 1, cp + 1, size - hostlen - 1)) { |
| 663 | const char* b = cp; |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 664 | cp += size - hostlen; |
| 665 | taglen -= size - hostlen; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 666 | if (*cp == '.') { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 667 | while ((--taglen > 0) && !isspace(*++cp) && |
| 668 | (*cp != ':')) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 669 | } |
| 670 | const char* e; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 671 | for (e = cp; (taglen > 0) && isspace(*cp); |
| 672 | ++cp, --taglen) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 673 | } |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 674 | if ((taglen > 0) && (*cp == ':')) { |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 675 | tag = b; |
| 676 | etag = e; |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 677 | p = cp + 1; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | } else { |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 681 | goto twoWord; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 684 | } else { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 685 | // <PRI>[<TIME>] <tag> <stuff>' : message |
| 686 | twoWord: |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 687 | while ((--taglen > 0) && !isspace(*++cp) && (*cp != ':')) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 688 | } |
| 689 | const char* e; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 690 | for (e = cp; (taglen > 0) && isspace(*cp); ++cp, --taglen) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 691 | } |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 692 | // Two words |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 693 | if ((taglen > 0) && (*cp == ':')) { |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 694 | tag = bt; |
| 695 | etag = e; |
| 696 | p = cp + 1; |
| 697 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 698 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 699 | } // else no tag |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 700 | |
| 701 | static const char cpu[] = "CPU"; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 702 | static const ssize_t cpuLen = strlen(cpu); |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 703 | static const char warning[] = "WARNING"; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 704 | static const ssize_t warningLen = strlen(warning); |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 705 | static const char error[] = "ERROR"; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 706 | static const ssize_t errorLen = strlen(error); |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 707 | static const char info[] = "INFO"; |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 708 | static const ssize_t infoLen = strlen(info); |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 709 | |
| 710 | size = etag - tag; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 711 | if ((size <= 1) || |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 712 | // register names like x9 |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 713 | ((size == 2) && (isdigit(tag[0]) || isdigit(tag[1]))) || |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 714 | // register names like x18 but not driver names like en0 |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 715 | ((size == 3) && (isdigit(tag[1]) && isdigit(tag[2]))) || |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 716 | // blacklist |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 717 | ((size == cpuLen) && !fastcmp<strncmp>(tag, cpu, cpuLen)) || |
| 718 | ((size == warningLen) && |
| 719 | !fastcmp<strncasecmp>(tag, warning, warningLen)) || |
| 720 | ((size == errorLen) && !fastcmp<strncasecmp>(tag, error, errorLen)) || |
| 721 | ((size == infoLen) && !fastcmp<strncasecmp>(tag, info, infoLen))) { |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 722 | p = start; |
| 723 | etag = tag = ""; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 724 | } |
Mark Salyzyn | a146a77 | 2016-08-05 08:16:37 -0700 | [diff] [blame] | 725 | |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 726 | // Suppress additional stutter in tag: |
| 727 | // eg: [143:healthd]healthd -> [143:healthd] |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 728 | taglen = etag - tag; |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 729 | // Mediatek-special printk induced stutter |
Andreas Gampe | 34450d7 | 2017-03-14 20:43:43 -0700 | [diff] [blame] | 730 | const char* mp = strnrchr(tag, taglen, ']'); |
Mark Salyzyn | 47dba71 | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 731 | if (mp && (++mp < etag)) { |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 732 | ssize_t s = etag - mp; |
Mark Salyzyn | 0eeb06b | 2016-12-02 10:08:48 -0800 | [diff] [blame] | 733 | if (((s + s) < taglen) && !fastcmp<memcmp>(mp, mp - 1 - s, s)) { |
Mark Salyzyn | 47dba71 | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 734 | taglen = mp - tag; |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
Mark Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 737 | // Deal with sloppy and simplistic harmless p = cp + 1 etc above. |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 738 | if (len < (p - buf)) { |
Mark Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 739 | p = &buf[len]; |
| 740 | } |
Mark Salyzyn | ee49c6a | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 741 | // skip leading space |
Mark Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 742 | while ((p < &buf[len]) && (isspace(*p) || !*p)) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 743 | ++p; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 744 | } |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 745 | // truncate trailing space or nuls |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 746 | ssize_t b = len - (p - buf); |
| 747 | while ((b > 0) && (isspace(p[b - 1]) || !p[b - 1])) { |
Mark Salyzyn | ee49c6a | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 748 | --b; |
| 749 | } |
| 750 | // trick ... allow tag with empty content to be logged. log() drops empty |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 751 | if ((b <= 0) && (taglen > 0)) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 752 | p = " "; |
Mark Salyzyn | ee49c6a | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 753 | b = 1; |
| 754 | } |
Mark Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 755 | // paranoid sanity check, can not happen ... |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 756 | if (b > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 757 | b = LOGGER_ENTRY_MAX_PAYLOAD; |
| 758 | } |
Mark Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 759 | if (taglen > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 760 | taglen = LOGGER_ENTRY_MAX_PAYLOAD; |
| 761 | } |
| 762 | // calculate buffer copy requirements |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 763 | ssize_t n = 1 + taglen + 1 + b + 1; |
Mark Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 764 | // paranoid sanity check, first two just can not happen ... |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 765 | if ((taglen > n) || (b > n) || (n > (ssize_t)USHRT_MAX) || (n <= 0)) { |
Mark Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 766 | return -EINVAL; |
Mark Salyzyn | 47dba71 | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 767 | } |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 768 | |
Mark Salyzyn | eb29808 | 2015-11-10 15:56:41 -0800 | [diff] [blame] | 769 | // Careful. |
| 770 | // We are using the stack to house the log buffer for speed reasons. |
| 771 | // If we malloc'd this buffer, we could get away without n's USHRT_MAX |
| 772 | // test above, but we would then required a max(n, USHRT_MAX) as |
| 773 | // truncating length argument to logbuf->log() below. Gain is protection |
| 774 | // of stack sanity and speedup, loss is truncated long-line content. |
Mark Salyzyn | ddda212 | 2015-10-02 09:22:52 -0700 | [diff] [blame] | 775 | char newstr[n]; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 776 | char* np = newstr; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 777 | |
| 778 | // Convert priority into single-byte Android logger priority |
| 779 | *np = convertKernelPrioToAndroidPrio(pri); |
| 780 | ++np; |
| 781 | |
| 782 | // Copy parsed tag following priority |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 783 | memcpy(np, tag, taglen); |
Mark Salyzyn | 46d159d | 2015-08-26 09:30:00 -0700 | [diff] [blame] | 784 | np += taglen; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 785 | *np = '\0'; |
| 786 | ++np; |
| 787 | |
| 788 | // Copy main message to the remainder |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 789 | memcpy(np, p, b); |
Mark Salyzyn | ee49c6a | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 790 | np[b] = '\0'; |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 791 | |
Mark Salyzyn | b06247d | 2015-12-04 09:32:27 -0800 | [diff] [blame] | 792 | if (!isMonotonic()) { |
| 793 | // Watch out for singular race conditions with timezone causing near |
| 794 | // integer quarter-hour jumps in the time and compensate accordingly. |
| 795 | // Entries will be temporal within near_seconds * 2. b/21868540 |
| 796 | static uint32_t vote_time[3]; |
| 797 | vote_time[2] = vote_time[1]; |
| 798 | vote_time[1] = vote_time[0]; |
| 799 | vote_time[0] = now.tv_sec; |
| 800 | |
| 801 | if (vote_time[1] && vote_time[2]) { |
| 802 | static const unsigned near_seconds = 10; |
| 803 | static const unsigned timezones_seconds = 900; |
| 804 | int diff0 = (vote_time[0] - vote_time[1]) / near_seconds; |
| 805 | unsigned abs0 = (diff0 < 0) ? -diff0 : diff0; |
| 806 | int diff1 = (vote_time[1] - vote_time[2]) / near_seconds; |
| 807 | unsigned abs1 = (diff1 < 0) ? -diff1 : diff1; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 808 | if ((abs1 <= 1) && // last two were in agreement on timezone |
| 809 | ((abs0 + 1) % (timezones_seconds / near_seconds)) <= 2) { |
Mark Salyzyn | b06247d | 2015-12-04 09:32:27 -0800 | [diff] [blame] | 810 | abs0 = (abs0 + 1) / (timezones_seconds / near_seconds) * |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 811 | timezones_seconds; |
Mark Salyzyn | b06247d | 2015-12-04 09:32:27 -0800 | [diff] [blame] | 812 | now.tv_sec -= (diff0 < 0) ? -abs0 : abs0; |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 817 | // Log message |
Chih-Hung Hsieh | 08d470b | 2018-08-13 14:22:56 -0700 | [diff] [blame] | 818 | int rc = logbuf->log(LOG_ID_KERNEL, now, uid, pid, tid, newstr, (uint16_t)n); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 819 | |
| 820 | // notify readers |
Tom Cherry | cbfebdb | 2018-04-24 14:13:30 -0700 | [diff] [blame] | 821 | if (rc > 0) { |
Hao Wang | f6e2296 | 2017-12-04 14:10:40 +0800 | [diff] [blame] | 822 | reader->notifyNewLog(static_cast<log_mask_t>(1 << LOG_ID_KERNEL)); |
Mark Salyzyn | ae4d928 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | return rc; |
| 826 | } |