blob: 5554d5327bffb701ae68c89943578d6080ba1dd5 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
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
Mark Salyzyn671e3432014-05-06 07:34:59 -070017#include <ctype.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080018#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080019#include <stdio.h>
20#include <string.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070021#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <time.h>
23#include <unistd.h>
24
Mark Salyzyn511338d2015-05-19 09:12:30 -070025#include <unordered_map>
26
Mark Salyzyn671e3432014-05-06 07:34:59 -070027#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070028#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080029
30#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070031#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070032#include "LogReader.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
Mark Salyzyndfa7a072014-02-11 12:29:31 -080034// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080035#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070036
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070037void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080038 log_id_for_each(i) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -080039 mLastSet[i] = false;
40 mLast[i] = mLogElements.begin();
41
Mark Salyzynf10e2732016-09-27 13:08:23 -070042 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070043 setSize(i, LOG_BUFFER_MIN_SIZE);
44 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080045 }
Mark Salyzynb6bee332015-09-08 08:56:32 -070046 bool lastMonotonic = monotonic;
Mark Salyzynba7a9a02015-12-01 15:57:25 -080047 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzynb75cce02015-11-30 11:35:56 -080048 if (lastMonotonic != monotonic) {
49 //
50 // Fixup all timestamps, may not be 100% accurate, but better than
51 // throwing what we have away when we get 'surprised' by a change.
52 // In-place element fixup so no need to check reader-lock. Entries
53 // should already be in timestamp order, but we could end up with a
54 // few out-of-order entries if new monotonics come in before we
55 // are notified of the reinit change in status. A Typical example would
56 // be:
57 // --------- beginning of system
58 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
59 // --------- beginning of kernel
60 // 0.000000 0 0 I : Initializing cgroup subsys
61 // as the act of mounting /data would trigger persist.logd.timestamp to
62 // be corrected. 1/30 corner case YMMV.
63 //
64 pthread_mutex_lock(&mLogElementsLock);
65 LogBufferElementCollection::iterator it = mLogElements.begin();
66 while((it != mLogElements.end())) {
67 LogBufferElement *e = *it;
68 if (monotonic) {
69 if (!android::isMonotonic(e->mRealTime)) {
70 LogKlog::convertRealToMonotonic(e->mRealTime);
71 }
72 } else {
73 if (android::isMonotonic(e->mRealTime)) {
74 LogKlog::convertMonotonicToReal(e->mRealTime);
75 }
76 }
77 ++it;
78 }
79 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzynb6bee332015-09-08 08:56:32 -070080 }
81
Mark Salyzynb75cce02015-11-30 11:35:56 -080082 // We may have been triggered by a SIGHUP. Release any sleeping reader
83 // threads to dump their current content.
Mark Salyzynb6bee332015-09-08 08:56:32 -070084 //
Mark Salyzynb75cce02015-11-30 11:35:56 -080085 // NB: this is _not_ performed in the context of a SIGHUP, it is
86 // performed during startup, and in context of reinit administrative thread
87 LogTimeEntry::lock();
88
89 LastLogTimes::iterator times = mTimes.begin();
90 while(times != mTimes.end()) {
91 LogTimeEntry *entry = (*times);
92 if (entry->owned_Locked()) {
93 entry->triggerReader_Locked();
Mark Salyzynb6bee332015-09-08 08:56:32 -070094 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080095 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -070096 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080097
98 LogTimeEntry::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080099}
100
Mark Salyzynb6bee332015-09-08 08:56:32 -0700101LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800102 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzynb6bee332015-09-08 08:56:32 -0700103 mTimes(*times) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700104 pthread_mutex_init(&mLogElementsLock, NULL);
105
106 init();
107}
108
Mark Salyzyn202e1532015-02-09 08:21:05 -0800109int LogBuffer::log(log_id_t log_id, log_time realtime,
110 uid_t uid, pid_t pid, pid_t tid,
111 const char *msg, unsigned short len) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800112 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800113 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800114 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700115
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700117 uid, pid, tid, msg, len);
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000118 if (log_id != LOG_ID_SECURITY) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800119 int prio = ANDROID_LOG_INFO;
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000120 const char *tag = NULL;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700121 size_t len = 0;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800122 if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700123 tag = android::tagToName(&len, elem->getTag());
Mark Salyzyn083b0372015-12-04 10:59:45 -0800124 } else {
125 prio = *msg;
126 tag = msg + 1;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700127 len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800128 }
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700129 if (!__android_log_is_loggable_len(prio, tag, len, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800130 // Log traffic received to total
131 pthread_mutex_lock(&mLogElementsLock);
132 stats.add(elem);
133 stats.subtract(elem);
134 pthread_mutex_unlock(&mLogElementsLock);
135 delete elem;
136 return -EACCES;
137 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700138 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800139
140 pthread_mutex_lock(&mLogElementsLock);
141
142 // Insert elements in time sorted order if possible
143 // NB: if end is region locked, place element at end of list
144 LogBufferElementCollection::iterator it = mLogElements.end();
145 LogBufferElementCollection::iterator last = it;
Mark Salyzyneae155e2014-10-13 16:49:47 -0700146 while (last != mLogElements.begin()) {
147 --it;
Mark Salyzync03e72c2014-02-18 11:23:53 -0800148 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800149 break;
150 }
151 last = it;
152 }
Mark Salyzync03e72c2014-02-18 11:23:53 -0800153
Mark Salyzyn0175b072014-02-26 09:50:16 -0800154 if (last == mLogElements.end()) {
155 mLogElements.push_back(elem);
156 } else {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800157 uint64_t end = 1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800158 bool end_set = false;
159 bool end_always = false;
160
161 LogTimeEntry::lock();
162
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700163 LastLogTimes::iterator times = mTimes.begin();
164 while(times != mTimes.end()) {
165 LogTimeEntry *entry = (*times);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800166 if (entry->owned_Locked()) {
167 if (!entry->mNonBlock) {
168 end_always = true;
169 break;
170 }
171 if (!end_set || (end <= entry->mEnd)) {
172 end = entry->mEnd;
173 end_set = true;
174 }
175 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700176 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800177 }
178
179 if (end_always
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800180 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800181 mLogElements.push_back(elem);
182 } else {
183 mLogElements.insert(last,elem);
184 }
185
186 LogTimeEntry::unlock();
187 }
188
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700189 stats.add(elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800190 maybePrune(log_id);
191 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn202e1532015-02-09 08:21:05 -0800192
193 return len;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800194}
195
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700196// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800197//
198// mLogElementsLock must be held when this function is called.
199void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800200 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700201 unsigned long maxSize = log_buffer_size(id);
202 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700203 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700204 size_t elements = stats.realElements(id);
205 size_t minElements = elements / 100;
206 if (minElements < minPrune) {
207 minElements = minPrune;
208 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700209 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700210 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700211 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800212 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700213 if (pruneRows > maxPrune) {
214 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700215 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800216 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800217 }
218}
219
Mark Salyzyn831aa292015-09-03 16:08:50 -0700220LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700221 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700222 LogBufferElement *element = *it;
223 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700224
Mark Salyzyn6a066942016-07-14 15:34:30 -0700225 { // start of scope for found iterator
226 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
227 element->getTag() : element->getUid();
228 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
229 if ((found != mLastWorst[id].end()) && (it == found->second)) {
230 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700231 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700232 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700233
Mark Salyzyn6a066942016-07-14 15:34:30 -0700234 if ((id != LOG_ID_EVENTS) && (id != LOG_ID_SECURITY) && (element->getUid() == AID_SYSTEM)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700235 // start of scope for pid found iterator
236 LogBufferPidIteratorMap::iterator found =
237 mLastWorstPidOfSystem[id].find(element->getPid());
238 if ((found != mLastWorstPidOfSystem[id].end())
239 && (it == found->second)) {
240 mLastWorstPidOfSystem[id].erase(found);
241 }
242 }
243
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800244 bool setLast[LOG_ID_MAX];
245 bool doSetLast = false;
246 log_id_for_each(i) {
247 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
248 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700249 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800250 if (doSetLast) {
251 log_id_for_each(i) {
252 if (setLast[i]) {
253 if (it == mLogElements.end()) { // unlikely
254 mLastSet[i] = false;
255 } else {
256 mLast[i] = it;
257 }
258 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800259 }
260 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700261 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700262 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700263 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700264 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700265 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700266 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700267
268 return it;
269}
270
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700271// Define a temporary mechanism to report the last LogBufferElement pointer
272// for the specified uid, pid and tid. Used below to help merge-sort when
273// pruning for worst UID.
274class LogBufferElementKey {
275 const union {
276 struct {
277 uint16_t uid;
278 uint16_t pid;
279 uint16_t tid;
280 uint16_t padding;
281 } __packed;
282 uint64_t value;
283 } __packed;
284
285public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700286 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
287 uid(uid),
288 pid(pid),
289 tid(tid),
290 padding(0) {
291 }
Chih-Hung Hsieh1cc82ce2016-04-25 13:49:46 -0700292 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700293
294 uint64_t getKey() { return value; }
295};
296
Mark Salyzyn511338d2015-05-19 09:12:30 -0700297class LogBufferElementLast {
298
299 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
300 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700301
302public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700303
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700304 bool coalesce(LogBufferElement *element, unsigned short dropped) {
305 LogBufferElementKey key(element->getUid(),
306 element->getPid(),
307 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700308 LogBufferElementMap::iterator it = map.find(key.getKey());
309 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700310 LogBufferElement *found = it->second;
311 unsigned short moreDropped = found->getDropped();
312 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700313 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700314 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700315 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700316 return true;
317 }
318 }
319 return false;
320 }
321
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700322 void add(LogBufferElement *element) {
323 LogBufferElementKey key(element->getUid(),
324 element->getPid(),
325 element->getTid());
326 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700327 }
328
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700329 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700330 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700331 }
332
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700333 void clear(LogBufferElement *element) {
334 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700335 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700336 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700337 LogBufferElement *mapElement = it->second;
338 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
339 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700340 it = map.erase(it);
341 } else {
342 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700343 }
344 }
345 }
346
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700347};
348
Mark Salyzyn0175b072014-02-26 09:50:16 -0800349// prune "pruneRows" of type "id" from the buffer.
350//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700351// This garbage collection task is used to expire log entries. It is called to
352// remove all logs (clear), all UID logs (unprivileged clear), or every
353// 256 or 10% of the total logs (whichever is less) to prune the logs.
354//
355// First there is a prep phase where we discover the reader region lock that
356// acts as a backstop to any pruning activity to stop there and go no further.
357//
358// There are three major pruning loops that follow. All expire from the oldest
359// entries. Since there are multiple log buffers, the Android logging facility
360// will appear to drop entries 'in the middle' when looking at multiple log
361// sources and buffers. This effect is slightly more prominent when we prune
362// the worst offender by logging source. Thus the logs slowly loose content
363// and value as you move back in time. This is preferred since chatty sources
364// invariably move the logs value down faster as less chatty sources would be
365// expired in the noise.
366//
367// The first loop performs blacklisting and worst offender pruning. Falling
368// through when there are no notable worst offenders and have not hit the
369// region lock preventing further worst offender pruning. This loop also looks
370// after managing the chatty log entries and merging to help provide
371// statistical basis for blame. The chatty entries are not a notification of
372// how much logs you may have, but instead represent how much logs you would
373// have had in a virtual log buffer that is extended to cover all the in-memory
374// logs without loss. They last much longer than the represented pruned logs
375// since they get multiplied by the gains in the non-chatty log sources.
376//
377// The second loop get complicated because an algorithm of watermarks and
378// history is maintained to reduce the order and keep processing time
379// down to a minimum at scale. These algorithms can be costly in the face
380// of larger log buffers, or severly limited processing time granted to a
381// background task at lowest priority.
382//
383// This second loop does straight-up expiration from the end of the logs
384// (again, remember for the specified log buffer id) but does some whitelist
385// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
386// spam filtration all take priority. This second loop also checks if a region
387// lock is causing us to buffer too much in the logs to help the reader(s),
388// and will tell the slowest reader thread to skip log entries, and if
389// persistent and hits a further threshold, kill the reader thread.
390//
391// The third thread is optional, and only gets hit if there was a whitelist
392// and more needs to be pruned against the backstop of the region lock.
393//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800394// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700395//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700396bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800397 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700398 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700399 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800400
401 LogTimeEntry::lock();
402
403 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700404 LastLogTimes::iterator times = mTimes.begin();
405 while(times != mTimes.end()) {
406 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200407 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800408 && (!oldest ||
409 (oldest->mStart > entry->mStart) ||
410 ((oldest->mStart == entry->mStart) &&
411 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800412 oldest = entry;
413 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700414 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800415 }
416
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800417 LogBufferElementCollection::iterator it;
418
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700419 if (caller_uid != AID_ROOT) {
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700420 // Only here if clear all request from non system source, so chatty
421 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800422 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
423 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700424 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700425
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700426 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700427 ++it;
428 continue;
429 }
430
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800431 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
432 mLast[id] = it;
433 mLastSet[id] = true;
434 }
435
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700436 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700437 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800438 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
439 oldest->triggerReader_Locked();
440 } else {
441 oldest->triggerSkip_Locked(id, pruneRows);
442 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700443 break;
444 }
445
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700446 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700447 if (--pruneRows == 0) {
448 break;
449 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700450 }
451 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700452 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700453 }
454
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700455 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800456 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700457 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800458 // recalculate the worst offender on every batched pass
Mark Salyzyn6a066942016-07-14 15:34:30 -0700459 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800460 size_t worst_sizes = 0;
461 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700462 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800463
Mark Salyzynae769232015-03-17 17:17:25 -0700464 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700465 // Calculate threshold as 12.5% of available storage
466 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700467
Mark Salyzyn6a066942016-07-14 15:34:30 -0700468 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
469 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
470 worst, worst_sizes, second_worst_sizes, threshold);
471 } else {
472 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
473 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700474
Mark Salyzyn6a066942016-07-14 15:34:30 -0700475 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
476 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
477 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700478 }
479 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800480 }
481
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700482 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700483 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700484 break;
485 }
486
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800487 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700488 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800489 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700490 // Perform at least one mandatory garbage collection cycle in following
491 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700492 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700493 // - check age-out of preserved logs
494 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700495 if (!gc && (worst != -1)) {
496 { // begin scope for worst found iterator
497 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
498 if ((found != mLastWorst[id].end())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700499 && (found->second != mLogElements.end())) {
500 leading = false;
501 it = found->second;
502 }
503 }
504 if (worstPid) {
505 // begin scope for pid worst found iterator
506 LogBufferPidIteratorMap::iterator found
507 = mLastWorstPidOfSystem[id].find(worstPid);
508 if ((found != mLastWorstPidOfSystem[id].end())
509 && (found->second != mLogElements.end())) {
510 leading = false;
511 it = found->second;
512 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700513 }
514 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700515 static const timespec too_old = {
516 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
517 };
518 LogBufferElementCollection::iterator lastt;
519 lastt = mLogElements.end();
520 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700521 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700522 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700523 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800524
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700525 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700526 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800527 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
528 oldest->triggerReader_Locked();
529 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800530 break;
531 }
532
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700533 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800534 ++it;
535 continue;
536 }
537
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800538 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
539 mLast[id] = it;
540 mLastSet[id] = true;
541 }
542
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700543 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800544
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700545 // remove any leading drops
546 if (leading && dropped) {
547 it = erase(it);
548 continue;
549 }
550
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700551 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700552 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700553 continue;
554 }
555
Mark Salyzyn6a066942016-07-14 15:34:30 -0700556 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
557 element->getTag() :
558 element->getUid();
559
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700560 if (hasBlacklist && mPrune.naughty(element)) {
561 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700562 it = erase(it);
563 if (dropped) {
564 continue;
565 }
566
567 pruneRows--;
568 if (pruneRows == 0) {
569 break;
570 }
571
Mark Salyzyn6a066942016-07-14 15:34:30 -0700572 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700573 kick = true;
574 if (worst_sizes < second_worst_sizes) {
575 break;
576 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700577 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700578 }
579 continue;
580 }
581
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700582 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
583 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700584 break;
585 }
586
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700587 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700588 last.add(element);
589 if (worstPid
590 && ((!gc && (element->getPid() == worstPid))
Mark Salyzyn6a066942016-07-14 15:34:30 -0700591 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700592 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700593 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700594 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700595 if ((!gc && !worstPid && (key == worst))
596 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
597 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700598 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800599 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700600 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800601 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700602
Mark Salyzyn6a066942016-07-14 15:34:30 -0700603 if ((key != worst)
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700604 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700605 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700606 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700607 ++it;
608 continue;
609 }
610
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700611 pruneRows--;
612 if (pruneRows == 0) {
613 break;
614 }
615
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700616 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700617
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700618 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700619
620 // do not create any leading drops
621 if (leading) {
622 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700623 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700624 stats.drop(element);
625 element->setDropped(1);
626 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700627 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700628 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700629 last.add(element);
630 if (worstPid && (!gc
631 || (mLastWorstPidOfSystem[id].find(worstPid)
632 == mLastWorstPidOfSystem[id].end()))) {
633 mLastWorstPidOfSystem[id][worstPid] = it;
634 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700635 if ((!gc && !worstPid) ||
636 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
637 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700638 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700639 ++it;
640 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700641 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700642 if (worst_sizes < second_worst_sizes) {
643 break;
644 }
645 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800646 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700647 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800648
Mark Salyzyn1c950472014-04-01 17:19:47 -0700649 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800650 break; // the following loop will ask bad clients to skip/drop
651 }
652 }
653
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800654 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800655 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800656 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800657 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700658 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700659
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700660 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700661 it++;
662 continue;
663 }
664
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800665 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
666 mLast[id] = it;
667 mLastSet[id] = true;
668 }
669
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700670 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700671 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700672 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800673 break;
674 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700675
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700676 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
677 // kick a misbehaving log reader client off the island
678 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800679 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
680 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700681 } else {
682 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800683 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700684 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800685 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700686
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700687 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
688 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700689 whitelist = true;
690 it++;
691 continue;
692 }
693
694 it = erase(it);
695 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800696 }
697
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700698 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800699 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800700 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800701 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700702 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700703
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700704 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700705 ++it;
706 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800707 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700708
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800709 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
710 mLast[id] = it;
711 mLastSet[id] = true;
712 }
713
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700714 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700715 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700716 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
717 // kick a misbehaving log reader client off the island
718 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800719 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
720 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700721 } else {
722 oldest->triggerSkip_Locked(id, pruneRows);
723 }
724 break;
725 }
726
727 it = erase(it);
728 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800729 }
730 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800731
Mark Salyzyn0175b072014-02-26 09:50:16 -0800732 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700733
734 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800735}
736
737// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700738bool LogBuffer::clear(log_id_t id, uid_t uid) {
739 bool busy = true;
740 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
741 for (int retry = 4;;) {
742 if (retry == 1) { // last pass
743 // Check if it is still busy after the sleep, we say prune
744 // one entry, not another clear run, so we are looking for
745 // the quick side effect of the return value to tell us if
746 // we have a _blocked_ reader.
747 pthread_mutex_lock(&mLogElementsLock);
748 busy = prune(id, 1, uid);
749 pthread_mutex_unlock(&mLogElementsLock);
750 // It is still busy, blocked reader(s), lets kill them all!
751 // otherwise, lets be a good citizen and preserve the slow
752 // readers and let the clear run (below) deal with determining
753 // if we are still blocked and return an error code to caller.
754 if (busy) {
755 LogTimeEntry::lock();
756 LastLogTimes::iterator times = mTimes.begin();
757 while (times != mTimes.end()) {
758 LogTimeEntry *entry = (*times);
759 // Killer punch
760 if (entry->owned_Locked() && entry->isWatching(id)) {
761 entry->release_Locked();
762 }
763 times++;
764 }
765 LogTimeEntry::unlock();
766 }
767 }
768 pthread_mutex_lock(&mLogElementsLock);
769 busy = prune(id, ULONG_MAX, uid);
770 pthread_mutex_unlock(&mLogElementsLock);
771 if (!busy || !--retry) {
772 break;
773 }
774 sleep (1); // Let reader(s) catch up after notification
775 }
776 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800777}
778
779// get the used space associated with "id".
780unsigned long LogBuffer::getSizeUsed(log_id_t id) {
781 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800782 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800783 pthread_mutex_unlock(&mLogElementsLock);
784 return retval;
785}
786
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800787// set the total space allocated to "id"
788int LogBuffer::setSize(log_id_t id, unsigned long size) {
789 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700790 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800791 return -1;
792 }
793 pthread_mutex_lock(&mLogElementsLock);
794 log_buffer_size(id) = size;
795 pthread_mutex_unlock(&mLogElementsLock);
796 return 0;
797}
798
799// get the total space allocated to "id"
800unsigned long LogBuffer::getSize(log_id_t id) {
801 pthread_mutex_lock(&mLogElementsLock);
802 size_t retval = log_buffer_size(id);
803 pthread_mutex_unlock(&mLogElementsLock);
804 return retval;
805}
806
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800807uint64_t LogBuffer::flushTo(
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800808 SocketClient *reader, const uint64_t start,
809 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800810 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800811 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800812 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800813 uid_t uid = reader->getUid();
814
815 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600816
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800817 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600818 // client wants to start from the beginning
819 it = mLogElements.begin();
820 } else {
821 // Client wants to start from some specified time. Chances are
822 // we are better off starting from the end of the time sorted list.
823 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
824 --it;
825 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800826 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600827 it++;
828 break;
829 }
830 }
831 }
832
833 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800834 LogBufferElement *element = *it;
835
836 if (!privileged && (element->getUid() != uid)) {
837 continue;
838 }
839
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800840 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
841 continue;
842 }
843
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800844 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800845 continue;
846 }
847
848 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800849 if (filter) {
850 int ret = (*filter)(element, arg);
851 if (ret == false) {
852 continue;
853 }
854 if (ret != true) {
855 break;
856 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800857 }
858
859 pthread_mutex_unlock(&mLogElementsLock);
860
861 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -0800862 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800863
864 if (max == element->FLUSH_ERROR) {
865 return max;
866 }
867
868 pthread_mutex_lock(&mLogElementsLock);
869 }
870 pthread_mutex_unlock(&mLogElementsLock);
871
872 return max;
873}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800874
Mark Salyzynee3b8382015-12-17 09:58:43 -0800875std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
876 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800877 pthread_mutex_lock(&mLogElementsLock);
878
Mark Salyzynee3b8382015-12-17 09:58:43 -0800879 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800880
881 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700882
883 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800884}