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