1 /*
2 * Copyright (C) 2012-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 // for manual checking of stale entries during LogBuffer::erase()
17 //#define DEBUG_CHECK_FOR_STALE_ENTRIES
18
19 #include <ctype.h>
20 #include <endian.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/cdefs.h>
25 #include <sys/user.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #include <unordered_map>
30
31 #include <cutils/properties.h>
32 #include <private/android_logger.h>
33
34 #include "LogBuffer.h"
35 #include "LogKlog.h"
36 #include "LogReader.h"
37 #include "LogUtils.h"
38
39 #ifndef __predict_false
40 #define __predict_false(exp) __builtin_expect((exp) != 0, 0)
41 #endif
42
43 // Default
44 #define log_buffer_size(id) mMaxSize[id]
45
46 const log_time LogBuffer::pruneMargin(3, 0);
47
init()48 void LogBuffer::init() {
49 log_id_for_each(i) {
50 mLastSet[i] = false;
51 mLast[i] = mLogElements.begin();
52
53 if (setSize(i, __android_logger_get_buffer_size(i))) {
54 setSize(i, LOG_BUFFER_MIN_SIZE);
55 }
56 }
57 bool lastMonotonic = monotonic;
58 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
59 if (lastMonotonic != monotonic) {
60 //
61 // Fixup all timestamps, may not be 100% accurate, but better than
62 // throwing what we have away when we get 'surprised' by a change.
63 // In-place element fixup so no need to check reader-lock. Entries
64 // should already be in timestamp order, but we could end up with a
65 // few out-of-order entries if new monotonics come in before we
66 // are notified of the reinit change in status. A Typical example would
67 // be:
68 // --------- beginning of system
69 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
70 // --------- beginning of kernel
71 // 0.000000 0 0 I : Initializing cgroup subsys
72 // as the act of mounting /data would trigger persist.logd.timestamp to
73 // be corrected. 1/30 corner case YMMV.
74 //
75 rdlock();
76 LogBufferElementCollection::iterator it = mLogElements.begin();
77 while ((it != mLogElements.end())) {
78 LogBufferElement* e = *it;
79 if (monotonic) {
80 if (!android::isMonotonic(e->mRealTime)) {
81 LogKlog::convertRealToMonotonic(e->mRealTime);
82 if ((e->mRealTime.tv_nsec % 1000) == 0) {
83 e->mRealTime.tv_nsec++;
84 }
85 }
86 } else {
87 if (android::isMonotonic(e->mRealTime)) {
88 LogKlog::convertMonotonicToReal(e->mRealTime);
89 if ((e->mRealTime.tv_nsec % 1000) == 0) {
90 e->mRealTime.tv_nsec++;
91 }
92 }
93 }
94 ++it;
95 }
96 unlock();
97 }
98
99 // We may have been triggered by a SIGHUP. Release any sleeping reader
100 // threads to dump their current content.
101 //
102 // NB: this is _not_ performed in the context of a SIGHUP, it is
103 // performed during startup, and in context of reinit administrative thread
104 LogTimeEntry::wrlock();
105
106 LastLogTimes::iterator times = mTimes.begin();
107 while (times != mTimes.end()) {
108 LogTimeEntry* entry = times->get();
109 entry->triggerReader_Locked();
110 times++;
111 }
112
113 LogTimeEntry::unlock();
114 }
115
LogBuffer(LastLogTimes * times)116 LogBuffer::LogBuffer(LastLogTimes* times)
117 : monotonic(android_log_clockid() == CLOCK_MONOTONIC), mTimes(*times) {
118 pthread_rwlock_init(&mLogElementsLock, nullptr);
119
120 log_id_for_each(i) {
121 lastLoggedElements[i] = nullptr;
122 droppedElements[i] = nullptr;
123 }
124
125 init();
126 }
127
~LogBuffer()128 LogBuffer::~LogBuffer() {
129 log_id_for_each(i) {
130 delete lastLoggedElements[i];
131 delete droppedElements[i];
132 }
133 }
134
135 enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
136
identical(LogBufferElement * elem,LogBufferElement * last)137 static enum match_type identical(LogBufferElement* elem,
138 LogBufferElement* last) {
139 // is it mostly identical?
140 // if (!elem) return DIFFERENT;
141 ssize_t lenl = elem->getMsgLen();
142 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
143 // if (!last) return DIFFERENT;
144 ssize_t lenr = last->getMsgLen();
145 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
146 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
147 if (elem->getUid() != last->getUid()) return DIFFERENT;
148 if (elem->getPid() != last->getPid()) return DIFFERENT;
149 if (elem->getTid() != last->getTid()) return DIFFERENT;
150
151 // last is more than a minute old, stop squashing identical messages
152 if (elem->getRealTime().nsec() >
153 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
154 return DIFFERENT;
155
156 // Identical message
157 const char* msgl = elem->getMsg();
158 const char* msgr = last->getMsg();
159 if (lenl == lenr) {
160 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
161 // liblog tagged messages (content gets summed)
162 if ((elem->getLogId() == LOG_ID_EVENTS) &&
163 (lenl == sizeof(android_log_event_int_t)) &&
164 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
165 sizeof(int32_t)) &&
166 (elem->getTag() == LIBLOG_LOG_TAG)) {
167 return SAME_LIBLOG;
168 }
169 }
170
171 // audit message (except sequence number) identical?
172 if (last->isBinary() &&
173 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
174 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
175 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
176 sizeof(int32_t))) {
177 return DIFFERENT;
178 }
179 msgl += sizeof(android_log_event_string_t);
180 lenl -= sizeof(android_log_event_string_t);
181 msgr += sizeof(android_log_event_string_t);
182 lenr -= sizeof(android_log_event_string_t);
183 }
184 static const char avc[] = "): avc: ";
185 const char* avcl = android::strnstr(msgl, lenl, avc);
186 if (!avcl) return DIFFERENT;
187 lenl -= avcl - msgl;
188 const char* avcr = android::strnstr(msgr, lenr, avc);
189 if (!avcr) return DIFFERENT;
190 lenr -= avcr - msgr;
191 if (lenl != lenr) return DIFFERENT;
192 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
193 lenl - strlen(avc))) {
194 return DIFFERENT;
195 }
196 return SAME;
197 }
198
log(log_id_t log_id,log_time realtime,uid_t uid,pid_t pid,pid_t tid,const char * msg,uint16_t len)199 int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
200 pid_t tid, const char* msg, uint16_t len) {
201 if (log_id >= LOG_ID_MAX) {
202 return -EINVAL;
203 }
204
205 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
206 // This prevents any chance that an outside source can request an
207 // exact entry with time specified in ms or us precision.
208 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
209
210 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
211
212 // b/137093665: don't coalesce security messages.
213 if (log_id == LOG_ID_SECURITY) {
214 wrlock();
215 log(elem);
216 unlock();
217
218 return len;
219 }
220
221 int prio = ANDROID_LOG_INFO;
222 const char* tag = nullptr;
223 size_t tag_len = 0;
224 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
225 tag = tagToName(elem->getTag());
226 if (tag) {
227 tag_len = strlen(tag);
228 }
229 } else {
230 prio = *msg;
231 tag = msg + 1;
232 tag_len = strnlen(tag, len - 1);
233 }
234 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
235 // Log traffic received to total
236 wrlock();
237 stats.addTotal(elem);
238 unlock();
239 delete elem;
240 return -EACCES;
241 }
242
243 wrlock();
244 LogBufferElement* currentLast = lastLoggedElements[log_id];
245 if (currentLast) {
246 LogBufferElement* dropped = droppedElements[log_id];
247 uint16_t count = dropped ? dropped->getDropped() : 0;
248 //
249 // State Init
250 // incoming:
251 // dropped = nullptr
252 // currentLast = nullptr;
253 // elem = incoming message
254 // outgoing:
255 // dropped = nullptr -> State 0
256 // currentLast = copy of elem
257 // log elem
258 // State 0
259 // incoming:
260 // count = 0
261 // dropped = nullptr
262 // currentLast = copy of last message
263 // elem = incoming message
264 // outgoing: if match != DIFFERENT
265 // dropped = copy of first identical message -> State 1
266 // currentLast = reference to elem
267 // break: if match == DIFFERENT
268 // dropped = nullptr -> State 0
269 // delete copy of last message (incoming currentLast)
270 // currentLast = copy of elem
271 // log elem
272 // State 1
273 // incoming:
274 // count = 0
275 // dropped = copy of first identical message
276 // currentLast = reference to last held-back incoming
277 // message
278 // elem = incoming message
279 // outgoing: if match == SAME
280 // delete copy of first identical message (dropped)
281 // dropped = reference to last held-back incoming
282 // message set to chatty count of 1 -> State 2
283 // currentLast = reference to elem
284 // outgoing: if match == SAME_LIBLOG
285 // dropped = copy of first identical message -> State 1
286 // take sum of currentLast and elem
287 // if sum overflows:
288 // log currentLast
289 // currentLast = reference to elem
290 // else
291 // delete currentLast
292 // currentLast = reference to elem, sum liblog.
293 // break: if match == DIFFERENT
294 // delete dropped
295 // dropped = nullptr -> State 0
296 // log reference to last held-back (currentLast)
297 // currentLast = copy of elem
298 // log elem
299 // State 2
300 // incoming:
301 // count = chatty count
302 // dropped = chatty message holding count
303 // currentLast = reference to last held-back incoming
304 // message.
305 // dropped = chatty message holding count
306 // elem = incoming message
307 // outgoing: if match != DIFFERENT
308 // delete chatty message holding count
309 // dropped = reference to last held-back incoming
310 // message, set to chatty count + 1
311 // currentLast = reference to elem
312 // break: if match == DIFFERENT
313 // log dropped (chatty message)
314 // dropped = nullptr -> State 0
315 // log reference to last held-back (currentLast)
316 // currentLast = copy of elem
317 // log elem
318 //
319 enum match_type match = identical(elem, currentLast);
320 if (match != DIFFERENT) {
321 if (dropped) {
322 // Sum up liblog tag messages?
323 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
324 android_log_event_int_t* event =
325 reinterpret_cast<android_log_event_int_t*>(
326 const_cast<char*>(currentLast->getMsg()));
327 //
328 // To unit test, differentiate with something like:
329 // event->header.tag = htole32(CHATTY_LOG_TAG);
330 // here, then instead of delete currentLast below,
331 // log(currentLast) to see the incremental sums form.
332 //
333 uint32_t swab = event->payload.data;
334 unsigned long long total = htole32(swab);
335 event = reinterpret_cast<android_log_event_int_t*>(
336 const_cast<char*>(elem->getMsg()));
337 swab = event->payload.data;
338
339 lastLoggedElements[LOG_ID_EVENTS] = elem;
340 total += htole32(swab);
341 // check for overflow
342 if (total >= UINT32_MAX) {
343 log(currentLast);
344 unlock();
345 return len;
346 }
347 stats.addTotal(currentLast);
348 delete currentLast;
349 swab = total;
350 event->payload.data = htole32(swab);
351 unlock();
352 return len;
353 }
354 if (count == USHRT_MAX) {
355 log(dropped);
356 count = 1;
357 } else {
358 delete dropped;
359 ++count;
360 }
361 }
362 if (count) {
363 stats.addTotal(currentLast);
364 currentLast->setDropped(count);
365 }
366 droppedElements[log_id] = currentLast;
367 lastLoggedElements[log_id] = elem;
368 unlock();
369 return len;
370 }
371 if (dropped) { // State 1 or 2
372 if (count) { // State 2
373 log(dropped); // report chatty
374 } else { // State 1
375 delete dropped;
376 }
377 droppedElements[log_id] = nullptr;
378 log(currentLast); // report last message in the series
379 } else { // State 0
380 delete currentLast;
381 }
382 }
383 lastLoggedElements[log_id] = new LogBufferElement(*elem);
384
385 log(elem);
386 unlock();
387
388 return len;
389 }
390
391 // assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
log(LogBufferElement * elem)392 void LogBuffer::log(LogBufferElement* elem) {
393 // cap on how far back we will sort in-place, otherwise append
394 static uint32_t too_far_back = 5; // five seconds
395 // Insert elements in time sorted order if possible
396 // NB: if end is region locked, place element at end of list
397 LogBufferElementCollection::iterator it = mLogElements.end();
398 LogBufferElementCollection::iterator last = it;
399 if (__predict_true(it != mLogElements.begin())) --it;
400 if (__predict_false(it == mLogElements.begin()) ||
401 __predict_true((*it)->getRealTime() <= elem->getRealTime()) ||
402 __predict_false((((*it)->getRealTime().tv_sec - too_far_back) >
403 elem->getRealTime().tv_sec) &&
404 (elem->getLogId() != LOG_ID_KERNEL) &&
405 ((*it)->getLogId() != LOG_ID_KERNEL))) {
406 mLogElements.push_back(elem);
407 } else {
408 log_time end(log_time::EPOCH);
409 bool end_set = false;
410 bool end_always = false;
411
412 LogTimeEntry::rdlock();
413
414 LastLogTimes::iterator times = mTimes.begin();
415 while (times != mTimes.end()) {
416 LogTimeEntry* entry = times->get();
417 if (!entry->mNonBlock) {
418 end_always = true;
419 break;
420 }
421 // it passing mEnd is blocked by the following checks.
422 if (!end_set || (end <= entry->mEnd)) {
423 end = entry->mEnd;
424 end_set = true;
425 }
426 times++;
427 }
428
429 if (end_always || (end_set && (end > (*it)->getRealTime()))) {
430 mLogElements.push_back(elem);
431 } else {
432 // should be short as timestamps are localized near end()
433 do {
434 last = it;
435 if (__predict_false(it == mLogElements.begin())) {
436 break;
437 }
438 --it;
439 } while (((*it)->getRealTime() > elem->getRealTime()) &&
440 (!end_set || (end <= (*it)->getRealTime())));
441 mLogElements.insert(last, elem);
442 }
443 LogTimeEntry::unlock();
444 }
445
446 stats.add(elem);
447 maybePrune(elem->getLogId());
448 }
449
450 // Prune at most 10% of the log entries or maxPrune, whichever is less.
451 //
452 // LogBuffer::wrlock() must be held when this function is called.
maybePrune(log_id_t id)453 void LogBuffer::maybePrune(log_id_t id) {
454 size_t sizes = stats.sizes(id);
455 unsigned long maxSize = log_buffer_size(id);
456 if (sizes > maxSize) {
457 size_t sizeOver = sizes - ((maxSize * 9) / 10);
458 size_t elements = stats.realElements(id);
459 size_t minElements = elements / 100;
460 if (minElements < minPrune) {
461 minElements = minPrune;
462 }
463 unsigned long pruneRows = elements * sizeOver / sizes;
464 if (pruneRows < minElements) {
465 pruneRows = minElements;
466 }
467 if (pruneRows > maxPrune) {
468 pruneRows = maxPrune;
469 }
470 prune(id, pruneRows);
471 }
472 }
473
erase(LogBufferElementCollection::iterator it,bool coalesce)474 LogBufferElementCollection::iterator LogBuffer::erase(
475 LogBufferElementCollection::iterator it, bool coalesce) {
476 LogBufferElement* element = *it;
477 log_id_t id = element->getLogId();
478
479 // Remove iterator references in the various lists that will become stale
480 // after the element is erased from the main logging list.
481
482 { // start of scope for found iterator
483 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
484 ? element->getTag()
485 : element->getUid();
486 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
487 if ((found != mLastWorst[id].end()) && (it == found->second)) {
488 mLastWorst[id].erase(found);
489 }
490 }
491
492 { // start of scope for pid found iterator
493 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
494 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
495 // long term code stability, find() check should be fast for those ids.
496 LogBufferPidIteratorMap::iterator found =
497 mLastWorstPidOfSystem[id].find(element->getPid());
498 if ((found != mLastWorstPidOfSystem[id].end()) &&
499 (it == found->second)) {
500 mLastWorstPidOfSystem[id].erase(found);
501 }
502 }
503
504 bool setLast[LOG_ID_MAX];
505 bool doSetLast = false;
506 log_id_for_each(i) {
507 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
508 }
509 #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
510 LogBufferElementCollection::iterator bad = it;
511 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
512 ? element->getTag()
513 : element->getUid();
514 #endif
515 it = mLogElements.erase(it);
516 if (doSetLast) {
517 log_id_for_each(i) {
518 if (setLast[i]) {
519 if (__predict_false(it == mLogElements.end())) { // impossible
520 mLastSet[i] = false;
521 mLast[i] = mLogElements.begin();
522 } else {
523 mLast[i] = it; // push down the road as next-best-watermark
524 }
525 }
526 }
527 }
528 #ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
529 log_id_for_each(i) {
530 for (auto b : mLastWorst[i]) {
531 if (bad == b.second) {
532 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
533 b.first, key);
534 }
535 }
536 for (auto b : mLastWorstPidOfSystem[i]) {
537 if (bad == b.second) {
538 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
539 b.first);
540 }
541 }
542 if (mLastSet[i] && (bad == mLast[i])) {
543 android::prdebug("stale mLast[%d]\n", i);
544 mLastSet[i] = false;
545 mLast[i] = mLogElements.begin();
546 }
547 }
548 #endif
549 if (coalesce) {
550 stats.erase(element);
551 } else {
552 stats.subtract(element);
553 }
554 delete element;
555
556 return it;
557 }
558
559 // Define a temporary mechanism to report the last LogBufferElement pointer
560 // for the specified uid, pid and tid. Used below to help merge-sort when
561 // pruning for worst UID.
562 class LogBufferElementKey {
563 const union {
564 struct {
565 uint32_t uid;
566 uint16_t pid;
567 uint16_t tid;
568 } __packed;
569 uint64_t value;
570 } __packed;
571
572 public:
LogBufferElementKey(uid_t uid,pid_t pid,pid_t tid)573 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
574 : uid(uid), pid(pid), tid(tid) {
575 }
LogBufferElementKey(uint64_t key)576 explicit LogBufferElementKey(uint64_t key) : value(key) {
577 }
578
getKey()579 uint64_t getKey() {
580 return value;
581 }
582 };
583
584 class LogBufferElementLast {
585 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
586 LogBufferElementMap map;
587
588 public:
coalesce(LogBufferElement * element,uint16_t dropped)589 bool coalesce(LogBufferElement* element, uint16_t dropped) {
590 LogBufferElementKey key(element->getUid(), element->getPid(),
591 element->getTid());
592 LogBufferElementMap::iterator it = map.find(key.getKey());
593 if (it != map.end()) {
594 LogBufferElement* found = it->second;
595 uint16_t moreDropped = found->getDropped();
596 if ((dropped + moreDropped) > USHRT_MAX) {
597 map.erase(it);
598 } else {
599 found->setDropped(dropped + moreDropped);
600 return true;
601 }
602 }
603 return false;
604 }
605
add(LogBufferElement * element)606 void add(LogBufferElement* element) {
607 LogBufferElementKey key(element->getUid(), element->getPid(),
608 element->getTid());
609 map[key.getKey()] = element;
610 }
611
clear()612 inline void clear() {
613 map.clear();
614 }
615
clear(LogBufferElement * element)616 void clear(LogBufferElement* element) {
617 log_time current =
618 element->getRealTime() - log_time(EXPIRE_RATELIMIT, 0);
619 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
620 LogBufferElement* mapElement = it->second;
621 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD) &&
622 (current > mapElement->getRealTime())) {
623 it = map.erase(it);
624 } else {
625 ++it;
626 }
627 }
628 }
629 };
630
631 // Determine if watermark is within pruneMargin + 1s from the end of the list,
632 // the caller will use this result to set an internal busy flag indicating
633 // the prune operation could not be completed because a reader is blocking
634 // the request.
isBusy(log_time watermark)635 bool LogBuffer::isBusy(log_time watermark) {
636 LogBufferElementCollection::iterator ei = mLogElements.end();
637 --ei;
638 return watermark < ((*ei)->getRealTime() - pruneMargin - log_time(1, 0));
639 }
640
641 // If the selected reader is blocking our pruning progress, decide on
642 // what kind of mitigation is necessary to unblock the situation.
kickMe(LogTimeEntry * me,log_id_t id,unsigned long pruneRows)643 void LogBuffer::kickMe(LogTimeEntry* me, log_id_t id, unsigned long pruneRows) {
644 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
645 // A misbehaving or slow reader has its connection
646 // dropped if we hit too much memory pressure.
647 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
648 me->mClient->getPid());
649 me->release_Locked();
650 } else if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
651 // Allow a blocked WRAP timeout reader to
652 // trigger and start reporting the log data.
653 me->triggerReader_Locked();
654 } else {
655 // tell slow reader to skip entries to catch up
656 android::prdebug(
657 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
658 pruneRows, me->mClient->getPid());
659 me->triggerSkip_Locked(id, pruneRows);
660 }
661 }
662
663 // prune "pruneRows" of type "id" from the buffer.
664 //
665 // This garbage collection task is used to expire log entries. It is called to
666 // remove all logs (clear), all UID logs (unprivileged clear), or every
667 // 256 or 10% of the total logs (whichever is less) to prune the logs.
668 //
669 // First there is a prep phase where we discover the reader region lock that
670 // acts as a backstop to any pruning activity to stop there and go no further.
671 //
672 // There are three major pruning loops that follow. All expire from the oldest
673 // entries. Since there are multiple log buffers, the Android logging facility
674 // will appear to drop entries 'in the middle' when looking at multiple log
675 // sources and buffers. This effect is slightly more prominent when we prune
676 // the worst offender by logging source. Thus the logs slowly loose content
677 // and value as you move back in time. This is preferred since chatty sources
678 // invariably move the logs value down faster as less chatty sources would be
679 // expired in the noise.
680 //
681 // The first loop performs blacklisting and worst offender pruning. Falling
682 // through when there are no notable worst offenders and have not hit the
683 // region lock preventing further worst offender pruning. This loop also looks
684 // after managing the chatty log entries and merging to help provide
685 // statistical basis for blame. The chatty entries are not a notification of
686 // how much logs you may have, but instead represent how much logs you would
687 // have had in a virtual log buffer that is extended to cover all the in-memory
688 // logs without loss. They last much longer than the represented pruned logs
689 // since they get multiplied by the gains in the non-chatty log sources.
690 //
691 // The second loop get complicated because an algorithm of watermarks and
692 // history is maintained to reduce the order and keep processing time
693 // down to a minimum at scale. These algorithms can be costly in the face
694 // of larger log buffers, or severly limited processing time granted to a
695 // background task at lowest priority.
696 //
697 // This second loop does straight-up expiration from the end of the logs
698 // (again, remember for the specified log buffer id) but does some whitelist
699 // preservation. Thus whitelist is a Hail Mary low priority, blacklists and
700 // spam filtration all take priority. This second loop also checks if a region
701 // lock is causing us to buffer too much in the logs to help the reader(s),
702 // and will tell the slowest reader thread to skip log entries, and if
703 // persistent and hits a further threshold, kill the reader thread.
704 //
705 // The third thread is optional, and only gets hit if there was a whitelist
706 // and more needs to be pruned against the backstop of the region lock.
707 //
708 // LogBuffer::wrlock() must be held when this function is called.
709 //
prune(log_id_t id,unsigned long pruneRows,uid_t caller_uid)710 bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
711 LogTimeEntry* oldest = nullptr;
712 bool busy = false;
713 bool clearAll = pruneRows == ULONG_MAX;
714
715 LogTimeEntry::rdlock();
716
717 // Region locked?
718 LastLogTimes::iterator times = mTimes.begin();
719 while (times != mTimes.end()) {
720 LogTimeEntry* entry = times->get();
721 if (entry->isWatching(id) &&
722 (!oldest || (oldest->mStart > entry->mStart) ||
723 ((oldest->mStart == entry->mStart) &&
724 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
725 oldest = entry;
726 }
727 times++;
728 }
729 log_time watermark(log_time::tv_sec_max, log_time::tv_nsec_max);
730 if (oldest) watermark = oldest->mStart - pruneMargin;
731
732 LogBufferElementCollection::iterator it;
733
734 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
735 // Only here if clear all request from non system source, so chatty
736 // filter logistics is not required.
737 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
738 while (it != mLogElements.end()) {
739 LogBufferElement* element = *it;
740
741 if ((element->getLogId() != id) ||
742 (element->getUid() != caller_uid)) {
743 ++it;
744 continue;
745 }
746
747 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
748 mLast[id] = it;
749 mLastSet[id] = true;
750 }
751
752 if (oldest && (watermark <= element->getRealTime())) {
753 busy = isBusy(watermark);
754 if (busy) kickMe(oldest, id, pruneRows);
755 break;
756 }
757
758 it = erase(it);
759 if (--pruneRows == 0) {
760 break;
761 }
762 }
763 LogTimeEntry::unlock();
764 return busy;
765 }
766
767 // prune by worst offenders; by blacklist, UID, and by PID of system UID
768 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
769 while (!clearAll && (pruneRows > 0)) {
770 // recalculate the worst offender on every batched pass
771 int worst = -1; // not valid for getUid() or getKey()
772 size_t worst_sizes = 0;
773 size_t second_worst_sizes = 0;
774 pid_t worstPid = 0; // POSIX guarantees PID != 0
775
776 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
777 // Calculate threshold as 12.5% of available storage
778 size_t threshold = log_buffer_size(id) / 8;
779
780 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
781 stats.sortTags(AID_ROOT, (pid_t)0, 2, id)
782 .findWorst(worst, worst_sizes, second_worst_sizes,
783 threshold);
784 // per-pid filter for AID_SYSTEM sources is too complex
785 } else {
786 stats.sort(AID_ROOT, (pid_t)0, 2, id)
787 .findWorst(worst, worst_sizes, second_worst_sizes,
788 threshold);
789
790 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
791 stats.sortPids(worst, (pid_t)0, 2, id)
792 .findWorst(worstPid, worst_sizes, second_worst_sizes);
793 }
794 }
795 }
796
797 // skip if we have neither worst nor naughty filters
798 if ((worst == -1) && !hasBlacklist) {
799 break;
800 }
801
802 bool kick = false;
803 bool leading = true;
804 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
805 // Perform at least one mandatory garbage collection cycle in following
806 // - clear leading chatty tags
807 // - coalesce chatty tags
808 // - check age-out of preserved logs
809 bool gc = pruneRows <= 1;
810 if (!gc && (worst != -1)) {
811 { // begin scope for worst found iterator
812 LogBufferIteratorMap::iterator found =
813 mLastWorst[id].find(worst);
814 if ((found != mLastWorst[id].end()) &&
815 (found->second != mLogElements.end())) {
816 leading = false;
817 it = found->second;
818 }
819 }
820 if (worstPid) { // begin scope for pid worst found iterator
821 // FYI: worstPid only set if !LOG_ID_EVENTS and
822 // !LOG_ID_SECURITY, not going to make that assumption ...
823 LogBufferPidIteratorMap::iterator found =
824 mLastWorstPidOfSystem[id].find(worstPid);
825 if ((found != mLastWorstPidOfSystem[id].end()) &&
826 (found->second != mLogElements.end())) {
827 leading = false;
828 it = found->second;
829 }
830 }
831 }
832 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
833 LogBufferElementCollection::iterator lastt;
834 lastt = mLogElements.end();
835 --lastt;
836 LogBufferElementLast last;
837 while (it != mLogElements.end()) {
838 LogBufferElement* element = *it;
839
840 if (oldest && (watermark <= element->getRealTime())) {
841 busy = isBusy(watermark);
842 // Do not let chatty eliding trigger any reader mitigation
843 break;
844 }
845
846 if (element->getLogId() != id) {
847 ++it;
848 continue;
849 }
850 // below this point element->getLogId() == id
851
852 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
853 mLast[id] = it;
854 mLastSet[id] = true;
855 }
856
857 uint16_t dropped = element->getDropped();
858
859 // remove any leading drops
860 if (leading && dropped) {
861 it = erase(it);
862 continue;
863 }
864
865 if (dropped && last.coalesce(element, dropped)) {
866 it = erase(it, true);
867 continue;
868 }
869
870 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
871 ? element->getTag()
872 : element->getUid();
873
874 if (hasBlacklist && mPrune.naughty(element)) {
875 last.clear(element);
876 it = erase(it);
877 if (dropped) {
878 continue;
879 }
880
881 pruneRows--;
882 if (pruneRows == 0) {
883 break;
884 }
885
886 if (key == worst) {
887 kick = true;
888 if (worst_sizes < second_worst_sizes) {
889 break;
890 }
891 worst_sizes -= element->getMsgLen();
892 }
893 continue;
894 }
895
896 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
897 (element->getRealTime() > (*lastt)->getRealTime())) {
898 break;
899 }
900
901 if (dropped) {
902 last.add(element);
903 if (worstPid &&
904 ((!gc && (element->getPid() == worstPid)) ||
905 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
906 mLastWorstPidOfSystem[id].end()))) {
907 // element->getUid() may not be AID_SYSTEM, next best
908 // watermark if current one empty. id is not LOG_ID_EVENTS
909 // or LOG_ID_SECURITY because of worstPid check.
910 mLastWorstPidOfSystem[id][element->getPid()] = it;
911 }
912 if ((!gc && !worstPid && (key == worst)) ||
913 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
914 mLastWorst[id][key] = it;
915 }
916 ++it;
917 continue;
918 }
919
920 if ((key != worst) ||
921 (worstPid && (element->getPid() != worstPid))) {
922 leading = false;
923 last.clear(element);
924 ++it;
925 continue;
926 }
927 // key == worst below here
928 // If worstPid set, then element->getPid() == worstPid below here
929
930 pruneRows--;
931 if (pruneRows == 0) {
932 break;
933 }
934
935 kick = true;
936
937 uint16_t len = element->getMsgLen();
938
939 // do not create any leading drops
940 if (leading) {
941 it = erase(it);
942 } else {
943 stats.drop(element);
944 element->setDropped(1);
945 if (last.coalesce(element, 1)) {
946 it = erase(it, true);
947 } else {
948 last.add(element);
949 if (worstPid &&
950 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
951 mLastWorstPidOfSystem[id].end()))) {
952 // element->getUid() may not be AID_SYSTEM, next best
953 // watermark if current one empty. id is not
954 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
955 mLastWorstPidOfSystem[id][worstPid] = it;
956 }
957 if ((!gc && !worstPid) ||
958 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
959 mLastWorst[id][worst] = it;
960 }
961 ++it;
962 }
963 }
964 if (worst_sizes < second_worst_sizes) {
965 break;
966 }
967 worst_sizes -= len;
968 }
969 last.clear();
970
971 if (!kick || !mPrune.worstUidEnabled()) {
972 break; // the following loop will ask bad clients to skip/drop
973 }
974 }
975
976 bool whitelist = false;
977 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
978 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
979 while ((pruneRows > 0) && (it != mLogElements.end())) {
980 LogBufferElement* element = *it;
981
982 if (element->getLogId() != id) {
983 it++;
984 continue;
985 }
986
987 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
988 mLast[id] = it;
989 mLastSet[id] = true;
990 }
991
992 if (oldest && (watermark <= element->getRealTime())) {
993 busy = isBusy(watermark);
994 if (!whitelist && busy) kickMe(oldest, id, pruneRows);
995 break;
996 }
997
998 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
999 // WhiteListed
1000 whitelist = true;
1001 it++;
1002 continue;
1003 }
1004
1005 it = erase(it);
1006 pruneRows--;
1007 }
1008
1009 // Do not save the whitelist if we are reader range limited
1010 if (whitelist && (pruneRows > 0)) {
1011 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
1012 while ((it != mLogElements.end()) && (pruneRows > 0)) {
1013 LogBufferElement* element = *it;
1014
1015 if (element->getLogId() != id) {
1016 ++it;
1017 continue;
1018 }
1019
1020 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
1021 mLast[id] = it;
1022 mLastSet[id] = true;
1023 }
1024
1025 if (oldest && (watermark <= element->getRealTime())) {
1026 busy = isBusy(watermark);
1027 if (busy) kickMe(oldest, id, pruneRows);
1028 break;
1029 }
1030
1031 it = erase(it);
1032 pruneRows--;
1033 }
1034 }
1035
1036 LogTimeEntry::unlock();
1037
1038 return (pruneRows > 0) && busy;
1039 }
1040
1041 // clear all rows of type "id" from the buffer.
clear(log_id_t id,uid_t uid)1042 bool LogBuffer::clear(log_id_t id, uid_t uid) {
1043 bool busy = true;
1044 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
1045 for (int retry = 4;;) {
1046 if (retry == 1) { // last pass
1047 // Check if it is still busy after the sleep, we say prune
1048 // one entry, not another clear run, so we are looking for
1049 // the quick side effect of the return value to tell us if
1050 // we have a _blocked_ reader.
1051 wrlock();
1052 busy = prune(id, 1, uid);
1053 unlock();
1054 // It is still busy, blocked reader(s), lets kill them all!
1055 // otherwise, lets be a good citizen and preserve the slow
1056 // readers and let the clear run (below) deal with determining
1057 // if we are still blocked and return an error code to caller.
1058 if (busy) {
1059 LogTimeEntry::wrlock();
1060 LastLogTimes::iterator times = mTimes.begin();
1061 while (times != mTimes.end()) {
1062 LogTimeEntry* entry = times->get();
1063 // Killer punch
1064 if (entry->isWatching(id)) {
1065 android::prdebug(
1066 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
1067 entry->mClient->getPid());
1068 entry->release_Locked();
1069 }
1070 times++;
1071 }
1072 LogTimeEntry::unlock();
1073 }
1074 }
1075 wrlock();
1076 busy = prune(id, ULONG_MAX, uid);
1077 unlock();
1078 if (!busy || !--retry) {
1079 break;
1080 }
1081 sleep(1); // Let reader(s) catch up after notification
1082 }
1083 return busy;
1084 }
1085
1086 // get the used space associated with "id".
getSizeUsed(log_id_t id)1087 unsigned long LogBuffer::getSizeUsed(log_id_t id) {
1088 rdlock();
1089 size_t retval = stats.sizes(id);
1090 unlock();
1091 return retval;
1092 }
1093
1094 // set the total space allocated to "id"
setSize(log_id_t id,unsigned long size)1095 int LogBuffer::setSize(log_id_t id, unsigned long size) {
1096 // Reasonable limits ...
1097 if (!__android_logger_valid_buffer_size(size)) {
1098 return -1;
1099 }
1100 wrlock();
1101 log_buffer_size(id) = size;
1102 unlock();
1103 return 0;
1104 }
1105
1106 // get the total space allocated to "id"
getSize(log_id_t id)1107 unsigned long LogBuffer::getSize(log_id_t id) {
1108 rdlock();
1109 size_t retval = log_buffer_size(id);
1110 unlock();
1111 return retval;
1112 }
1113
flushTo(SocketClient * reader,const log_time & start,pid_t * lastTid,bool privileged,bool security,int (* filter)(const LogBufferElement * element,void * arg),void * arg)1114 log_time LogBuffer::flushTo(SocketClient* reader, const log_time& start,
1115 pid_t* lastTid, bool privileged, bool security,
1116 int (*filter)(const LogBufferElement* element,
1117 void* arg),
1118 void* arg) {
1119 LogBufferElementCollection::iterator it;
1120 uid_t uid = reader->getUid();
1121
1122 rdlock();
1123
1124 if (start == log_time::EPOCH) {
1125 // client wants to start from the beginning
1126 it = mLogElements.begin();
1127 } else {
1128 // Cap to 300 iterations we look back for out-of-order entries.
1129 size_t count = 300;
1130
1131 // Client wants to start from some specified time. Chances are
1132 // we are better off starting from the end of the time sorted list.
1133 LogBufferElementCollection::iterator last;
1134 for (last = it = mLogElements.end(); it != mLogElements.begin();
1135 /* do nothing */) {
1136 --it;
1137 LogBufferElement* element = *it;
1138 if (element->getRealTime() > start) {
1139 last = it;
1140 } else if (element->getRealTime() == start) {
1141 last = ++it;
1142 break;
1143 } else if (!--count) {
1144 break;
1145 }
1146 }
1147 it = last;
1148 }
1149
1150 log_time curr = start;
1151
1152 LogBufferElement* lastElement = nullptr; // iterator corruption paranoia
1153 static const size_t maxSkip = 4194304; // maximum entries to skip
1154 size_t skip = maxSkip;
1155 for (; it != mLogElements.end(); ++it) {
1156 LogBufferElement* element = *it;
1157
1158 if (!--skip) {
1159 android::prdebug("reader.per: too many elements skipped");
1160 break;
1161 }
1162 if (element == lastElement) {
1163 android::prdebug("reader.per: identical elements");
1164 break;
1165 }
1166 lastElement = element;
1167
1168 if (!privileged && (element->getUid() != uid)) {
1169 continue;
1170 }
1171
1172 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1173 continue;
1174 }
1175
1176 // NB: calling out to another object with wrlock() held (safe)
1177 if (filter) {
1178 int ret = (*filter)(element, arg);
1179 if (ret == false) {
1180 continue;
1181 }
1182 if (ret != true) {
1183 break;
1184 }
1185 }
1186
1187 bool sameTid = false;
1188 if (lastTid) {
1189 sameTid = lastTid[element->getLogId()] == element->getTid();
1190 // Dropped (chatty) immediately following a valid log from the
1191 // same source in the same log buffer indicates we have a
1192 // multiple identical squash. chatty that differs source
1193 // is due to spam filter. chatty to chatty of different
1194 // source is also due to spam filter.
1195 lastTid[element->getLogId()] =
1196 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1197 }
1198
1199 unlock();
1200
1201 // range locking in LastLogTimes looks after us
1202 curr = element->flushTo(reader, this, sameTid);
1203
1204 if (curr == element->FLUSH_ERROR) {
1205 return curr;
1206 }
1207
1208 skip = maxSkip;
1209 rdlock();
1210 }
1211 unlock();
1212
1213 return curr;
1214 }
1215
formatStatistics(uid_t uid,pid_t pid,unsigned int logMask)1216 std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1217 unsigned int logMask) {
1218 wrlock();
1219
1220 std::string ret = stats.format(uid, pid, logMask);
1221
1222 unlock();
1223
1224 return ret;
1225 }
1226