blob: ec323934b26608c71d47653088d230f5722c5cfa [file] [log] [blame]
Mark Salyzyn12bac902014-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 Salyzyn4e756fb2014-05-06 07:34:59 -070017#include <ctype.h>
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -080018#include <errno.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080019#include <stdio.h>
20#include <string.h>
Mark Salyzyn7b85c592014-05-09 17:44:18 -070021#include <sys/user.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080022#include <time.h>
23#include <unistd.h>
24
Mark Salyzyn6a404192015-05-19 09:12:30 -070025#include <unordered_map>
26
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070027#include <cutils/properties.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080028#include <log/logger.h>
29
30#include "LogBuffer.h"
Mark Salyzyn2b4a7632015-09-08 08:56:32 -070031#include "LogKlog.h"
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070032#include "LogReader.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080033
Mark Salyzync89839a2014-02-11 12:29:31 -080034// Default
Mark Salyzyn12bac902014-02-26 09:50:16 -080035#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
Mark Salyzync89839a2014-02-11 12:29:31 -080036#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn7b85c592014-05-09 17:44:18 -070037#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
38#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
39
40static bool valid_size(unsigned long value) {
41 if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
42 return false;
43 }
44
45 long pages = sysconf(_SC_PHYS_PAGES);
46 if (pages < 1) {
47 return true;
48 }
49
50 long pagesize = sysconf(_SC_PAGESIZE);
51 if (pagesize <= 1) {
52 pagesize = PAGE_SIZE;
53 }
54
55 // maximum memory impact a somewhat arbitrary ~3%
56 pages = (pages + 31) / 32;
57 unsigned long maximum = pages * pagesize;
58
59 if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
60 return true;
61 }
62
63 return value <= maximum;
64}
Mark Salyzyn12bac902014-02-26 09:50:16 -080065
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070066static unsigned long property_get_size(const char *key) {
67 char property[PROPERTY_VALUE_MAX];
68 property_get(key, property, "");
69
70 char *cp;
71 unsigned long value = strtoul(property, &cp, 10);
72
73 switch(*cp) {
74 case 'm':
75 case 'M':
76 value *= 1024;
77 /* FALLTHRU */
78 case 'k':
79 case 'K':
80 value *= 1024;
81 /* FALLTHRU */
82 case '\0':
83 break;
84
85 default:
86 value = 0;
87 }
88
Mark Salyzyn7b85c592014-05-09 17:44:18 -070089 if (!valid_size(value)) {
90 value = 0;
91 }
92
Mark Salyzyn4e756fb2014-05-06 07:34:59 -070093 return value;
94}
95
Mark Salyzyn3fe25932015-03-10 16:45:17 -070096void LogBuffer::init() {
Mark Salyzyn7b85c592014-05-09 17:44:18 -070097 static const char global_tuneable[] = "persist.logd.size"; // Settings App
98 static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
99
100 unsigned long default_size = property_get_size(global_tuneable);
101 if (!default_size) {
102 default_size = property_get_size(global_default);
Mark Salyzyn973d18c2015-12-14 13:07:12 -0800103 if (!default_size) {
Mark Salyzyne19143e2015-12-14 16:40:12 -0800104 default_size = property_get_bool("ro.config.low_ram",
105 BOOL_DEFAULT_FALSE)
106 ? LOG_BUFFER_MIN_SIZE // 64K
107 : LOG_BUFFER_SIZE; // 256K
Mark Salyzyn973d18c2015-12-14 13:07:12 -0800108 }
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700109 }
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700110
Mark Salyzync89839a2014-02-11 12:29:31 -0800111 log_id_for_each(i) {
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700112 char key[PROP_NAME_MAX];
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700113
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700114 snprintf(key, sizeof(key), "%s.%s",
115 global_tuneable, android_log_id_to_name(i));
116 unsigned long property_size = property_get_size(key);
117
118 if (!property_size) {
119 snprintf(key, sizeof(key), "%s.%s",
120 global_default, android_log_id_to_name(i));
121 property_size = property_get_size(key);
122 }
123
124 if (!property_size) {
125 property_size = default_size;
126 }
127
128 if (!property_size) {
129 property_size = LOG_BUFFER_SIZE;
130 }
131
132 if (setSize(i, property_size)) {
133 setSize(i, LOG_BUFFER_MIN_SIZE);
134 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800135 }
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700136 bool lastMonotonic = monotonic;
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -0800137 monotonic = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800138 if (lastMonotonic != monotonic) {
139 //
140 // Fixup all timestamps, may not be 100% accurate, but better than
141 // throwing what we have away when we get 'surprised' by a change.
142 // In-place element fixup so no need to check reader-lock. Entries
143 // should already be in timestamp order, but we could end up with a
144 // few out-of-order entries if new monotonics come in before we
145 // are notified of the reinit change in status. A Typical example would
146 // be:
147 // --------- beginning of system
148 // 10.494082 184 201 D Cryptfs : Just triggered post_fs_data
149 // --------- beginning of kernel
150 // 0.000000 0 0 I : Initializing cgroup subsys
151 // as the act of mounting /data would trigger persist.logd.timestamp to
152 // be corrected. 1/30 corner case YMMV.
153 //
154 pthread_mutex_lock(&mLogElementsLock);
155 LogBufferElementCollection::iterator it = mLogElements.begin();
156 while((it != mLogElements.end())) {
157 LogBufferElement *e = *it;
158 if (monotonic) {
159 if (!android::isMonotonic(e->mRealTime)) {
160 LogKlog::convertRealToMonotonic(e->mRealTime);
161 }
162 } else {
163 if (android::isMonotonic(e->mRealTime)) {
164 LogKlog::convertMonotonicToReal(e->mRealTime);
165 }
166 }
167 ++it;
168 }
169 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700170 }
171
Mark Salyzyn552b4752015-11-30 11:35:56 -0800172 // We may have been triggered by a SIGHUP. Release any sleeping reader
173 // threads to dump their current content.
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700174 //
Mark Salyzyn552b4752015-11-30 11:35:56 -0800175 // NB: this is _not_ performed in the context of a SIGHUP, it is
176 // performed during startup, and in context of reinit administrative thread
177 LogTimeEntry::lock();
178
179 LastLogTimes::iterator times = mTimes.begin();
180 while(times != mTimes.end()) {
181 LogTimeEntry *entry = (*times);
182 if (entry->owned_Locked()) {
183 entry->triggerReader_Locked();
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700184 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800185 times++;
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700186 }
Mark Salyzyn552b4752015-11-30 11:35:56 -0800187
188 LogTimeEntry::unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800189}
190
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700191LogBuffer::LogBuffer(LastLogTimes *times):
Mark Salyzyn2d4f9bc2015-12-01 15:57:25 -0800192 monotonic(android_log_clockid() == CLOCK_MONOTONIC),
Mark Salyzyn2b4a7632015-09-08 08:56:32 -0700193 mTimes(*times) {
Mark Salyzyn3fe25932015-03-10 16:45:17 -0700194 pthread_mutex_init(&mLogElementsLock, NULL);
195
196 init();
197}
198
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800199int LogBuffer::log(log_id_t log_id, log_time realtime,
200 uid_t uid, pid_t pid, pid_t tid,
201 const char *msg, unsigned short len) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800202 if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800203 return -EINVAL;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800204 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700205
Mark Salyzyn12bac902014-02-26 09:50:16 -0800206 LogBufferElement *elem = new LogBufferElement(log_id, realtime,
Mark Salyzyn3661dd02014-03-20 16:09:38 -0700207 uid, pid, tid, msg, len);
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800208 if (log_id != LOG_ID_SECURITY) {
209 int prio = ANDROID_LOG_INFO;
210 const char *tag = NULL;
211 if (log_id == LOG_ID_EVENTS) {
212 tag = android::tagToName(elem->getTag());
213 } else {
214 prio = *msg;
215 tag = msg + 1;
216 }
217 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
218 // Log traffic received to total
219 pthread_mutex_lock(&mLogElementsLock);
220 stats.add(elem);
221 stats.subtract(elem);
222 pthread_mutex_unlock(&mLogElementsLock);
223 delete elem;
224 return -EACCES;
225 }
Mark Salyzyn19c91112014-10-02 13:07:05 -0700226 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800227
228 pthread_mutex_lock(&mLogElementsLock);
229
230 // Insert elements in time sorted order if possible
231 // NB: if end is region locked, place element at end of list
232 LogBufferElementCollection::iterator it = mLogElements.end();
233 LogBufferElementCollection::iterator last = it;
Mark Salyzyna222a772014-10-13 16:49:47 -0700234 while (last != mLogElements.begin()) {
235 --it;
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800236 if ((*it)->getRealTime() <= realtime) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800237 break;
238 }
239 last = it;
240 }
Mark Salyzyn5b7a8d82014-02-18 11:23:53 -0800241
Mark Salyzyn12bac902014-02-26 09:50:16 -0800242 if (last == mLogElements.end()) {
243 mLogElements.push_back(elem);
244 } else {
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800245 uint64_t end = 1;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800246 bool end_set = false;
247 bool end_always = false;
248
249 LogTimeEntry::lock();
250
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700251 LastLogTimes::iterator times = mTimes.begin();
252 while(times != mTimes.end()) {
253 LogTimeEntry *entry = (*times);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800254 if (entry->owned_Locked()) {
255 if (!entry->mNonBlock) {
256 end_always = true;
257 break;
258 }
259 if (!end_set || (end <= entry->mEnd)) {
260 end = entry->mEnd;
261 end_set = true;
262 }
263 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700264 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800265 }
266
267 if (end_always
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800268 || (end_set && (end >= (*last)->getSequence()))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800269 mLogElements.push_back(elem);
270 } else {
271 mLogElements.insert(last,elem);
272 }
273
274 LogTimeEntry::unlock();
275 }
276
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700277 stats.add(elem);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800278 maybePrune(log_id);
279 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn88fe7cc2015-02-09 08:21:05 -0800280
281 return len;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800282}
283
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700284// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn12bac902014-02-26 09:50:16 -0800285//
286// mLogElementsLock must be held when this function is called.
287void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800288 size_t sizes = stats.sizes(id);
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700289 unsigned long maxSize = log_buffer_size(id);
290 if (sizes > maxSize) {
Mark Salyzyn63745722015-08-19 12:20:36 -0700291 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzynd745c722015-09-30 07:40:09 -0700292 size_t elements = stats.realElements(id);
293 size_t minElements = elements / 100;
294 if (minElements < minPrune) {
295 minElements = minPrune;
296 }
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700297 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700298 if (pruneRows < minElements) {
Mark Salyzyn4436d4c2015-08-10 10:23:56 -0700299 pruneRows = minElements;
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800300 }
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700301 if (pruneRows > maxPrune) {
302 pruneRows = maxPrune;
Mark Salyzyn63745722015-08-19 12:20:36 -0700303 }
Mark Salyzyn9d4e34e2014-01-13 16:37:51 -0800304 prune(id, pruneRows);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800305 }
306}
307
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700308LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700309 LogBufferElementCollection::iterator it, bool coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700310 LogBufferElement *element = *it;
311 log_id_t id = element->getLogId();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700312
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700313 { // start of scope for uid found iterator
314 LogBufferIteratorMap::iterator found =
315 mLastWorstUid[id].find(element->getUid());
316 if ((found != mLastWorstUid[id].end())
317 && (it == found->second)) {
318 mLastWorstUid[id].erase(found);
319 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700320 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700321
322 if (element->getUid() == AID_SYSTEM) {
323 // start of scope for pid found iterator
324 LogBufferPidIteratorMap::iterator found =
325 mLastWorstPidOfSystem[id].find(element->getPid());
326 if ((found != mLastWorstPidOfSystem[id].end())
327 && (it == found->second)) {
328 mLastWorstPidOfSystem[id].erase(found);
329 }
330 }
331
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700332 it = mLogElements.erase(it);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700333 if (coalesce) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700334 stats.erase(element);
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700335 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700336 stats.subtract(element);
Mark Salyzyn57b82a62015-09-03 16:08:50 -0700337 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700338 delete element;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700339
340 return it;
341}
342
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700343// Define a temporary mechanism to report the last LogBufferElement pointer
344// for the specified uid, pid and tid. Used below to help merge-sort when
345// pruning for worst UID.
346class LogBufferElementKey {
347 const union {
348 struct {
349 uint16_t uid;
350 uint16_t pid;
351 uint16_t tid;
352 uint16_t padding;
353 } __packed;
354 uint64_t value;
355 } __packed;
356
357public:
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700358 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
359 uid(uid),
360 pid(pid),
361 tid(tid),
362 padding(0) {
363 }
364 LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700365
366 uint64_t getKey() { return value; }
367};
368
Mark Salyzyn6a404192015-05-19 09:12:30 -0700369class LogBufferElementLast {
370
371 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
372 LogBufferElementMap map;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700373
374public:
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700375
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700376 bool coalesce(LogBufferElement *element, unsigned short dropped) {
377 LogBufferElementKey key(element->getUid(),
378 element->getPid(),
379 element->getTid());
Mark Salyzyn6a404192015-05-19 09:12:30 -0700380 LogBufferElementMap::iterator it = map.find(key.getKey());
381 if (it != map.end()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700382 LogBufferElement *found = it->second;
383 unsigned short moreDropped = found->getDropped();
384 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700385 map.erase(it);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700386 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700387 found->setDropped(dropped + moreDropped);
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700388 return true;
389 }
390 }
391 return false;
392 }
393
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700394 void add(LogBufferElement *element) {
395 LogBufferElementKey key(element->getUid(),
396 element->getPid(),
397 element->getTid());
398 map[key.getKey()] = element;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700399 }
400
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700401 inline void clear() {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700402 map.clear();
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700403 }
404
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700405 void clear(LogBufferElement *element) {
406 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700407 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn6a404192015-05-19 09:12:30 -0700408 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700409 LogBufferElement *mapElement = it->second;
410 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
411 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn6a404192015-05-19 09:12:30 -0700412 it = map.erase(it);
413 } else {
414 ++it;
Mark Salyzynbe1a30c2015-04-20 14:08:56 -0700415 }
416 }
417 }
418
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700419};
420
Mark Salyzyn12bac902014-02-26 09:50:16 -0800421// prune "pruneRows" of type "id" from the buffer.
422//
Mark Salyzyn50e21082015-09-08 09:12:51 -0700423// This garbage collection task is used to expire log entries. It is called to
424// remove all logs (clear), all UID logs (unprivileged clear), or every
425// 256 or 10% of the total logs (whichever is less) to prune the logs.
426//
427// First there is a prep phase where we discover the reader region lock that
428// acts as a backstop to any pruning activity to stop there and go no further.
429//
430// There are three major pruning loops that follow. All expire from the oldest
431// entries. Since there are multiple log buffers, the Android logging facility
432// will appear to drop entries 'in the middle' when looking at multiple log
433// sources and buffers. This effect is slightly more prominent when we prune
434// the worst offender by logging source. Thus the logs slowly loose content
435// and value as you move back in time. This is preferred since chatty sources
436// invariably move the logs value down faster as less chatty sources would be
437// expired in the noise.
438//
439// The first loop performs blacklisting and worst offender pruning. Falling
440// through when there are no notable worst offenders and have not hit the
441// region lock preventing further worst offender pruning. This loop also looks
442// after managing the chatty log entries and merging to help provide
443// statistical basis for blame. The chatty entries are not a notification of
444// how much logs you may have, but instead represent how much logs you would
445// have had in a virtual log buffer that is extended to cover all the in-memory
446// logs without loss. They last much longer than the represented pruned logs
447// since they get multiplied by the gains in the non-chatty log sources.
448//
449// The second loop get complicated because an algorithm of watermarks and
450// history is maintained to reduce the order and keep processing time
451// down to a minimum at scale. These algorithms can be costly in the face
452// of larger log buffers, or severly limited processing time granted to a
453// background task at lowest priority.
454//
455// This second loop does straight-up expiration from the end of the logs
456// (again, remember for the specified log buffer id) but does some whitelist
457// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
458// spam filtration all take priority. This second loop also checks if a region
459// lock is causing us to buffer too much in the logs to help the reader(s),
460// and will tell the slowest reader thread to skip log entries, and if
461// persistent and hits a further threshold, kill the reader thread.
462//
463// The third thread is optional, and only gets hit if there was a whitelist
464// and more needs to be pruned against the backstop of the region lock.
465//
Mark Salyzyn12bac902014-02-26 09:50:16 -0800466// mLogElementsLock must be held when this function is called.
Mark Salyzyn50e21082015-09-08 09:12:51 -0700467//
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700468bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800469 LogTimeEntry *oldest = NULL;
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700470 bool busy = false;
Mark Salyzynd280d812015-09-16 15:34:00 -0700471 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800472
473 LogTimeEntry::lock();
474
475 // Region locked?
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700476 LastLogTimes::iterator times = mTimes.begin();
477 while(times != mTimes.end()) {
478 LogTimeEntry *entry = (*times);
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200479 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzyn552b4752015-11-30 11:35:56 -0800480 && (!oldest ||
481 (oldest->mStart > entry->mStart) ||
482 ((oldest->mStart == entry->mStart) &&
483 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800484 oldest = entry;
485 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700486 times++;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800487 }
488
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800489 LogBufferElementCollection::iterator it;
490
Mark Salyzyn276d5352014-06-12 11:16:16 -0700491 if (caller_uid != AID_ROOT) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700492 // Only here if clearAll condition (pruneRows == ULONG_MAX)
Mark Salyzyn276d5352014-06-12 11:16:16 -0700493 for(it = mLogElements.begin(); it != mLogElements.end();) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700494 LogBufferElement *element = *it;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700495
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700496 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzynd280d812015-09-16 15:34:00 -0700497 ++it;
498 continue;
499 }
500
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700501 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700502 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800503 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
504 oldest->triggerReader_Locked();
505 } else {
506 oldest->triggerSkip_Locked(id, pruneRows);
507 }
Mark Salyzyn276d5352014-06-12 11:16:16 -0700508 break;
509 }
510
Mark Salyzynd280d812015-09-16 15:34:00 -0700511 it = erase(it);
512 pruneRows--;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700513 }
514 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700515 return busy;
Mark Salyzyn276d5352014-06-12 11:16:16 -0700516 }
517
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700518 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800519 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzynd280d812015-09-16 15:34:00 -0700520 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800521 // recalculate the worst offender on every batched pass
522 uid_t worst = (uid_t) -1;
523 size_t worst_sizes = 0;
524 size_t second_worst_sizes = 0;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700525 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800526
Mark Salyzyn0e765c62015-03-17 17:17:25 -0700527 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700528 { // begin scope for UID sorted list
529 std::unique_ptr<const UidEntry *[]> sorted = stats.sort(
530 AID_ROOT, (pid_t)0, 2, id);
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700531
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700532 if (sorted.get() && sorted[0] && sorted[1]) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700533 worst_sizes = sorted[0]->getSizes();
Mark Salyzyn84a83982015-04-20 15:36:12 -0700534 // Calculate threshold as 12.5% of available storage
535 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn2b9465b2015-10-12 13:45:51 -0700536 if ((worst_sizes > threshold)
537 // Allow time horizon to extend roughly tenfold, assume
538 // average entry length is 100 characters.
539 && (worst_sizes > (10 * sorted[0]->getDropped()))) {
Mark Salyzyn84a83982015-04-20 15:36:12 -0700540 worst = sorted[0]->getKey();
541 second_worst_sizes = sorted[1]->getSizes();
542 if (second_worst_sizes < threshold) {
543 second_worst_sizes = threshold;
544 }
545 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800546 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800547 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700548
549 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
550 // begin scope of PID sorted list
551 std::unique_ptr<const PidEntry *[]> sorted = stats.sort(
552 worst, (pid_t)0, 2, id, worst);
553 if (sorted.get() && sorted[0] && sorted[1]) {
554 worstPid = sorted[0]->getKey();
555 second_worst_sizes = worst_sizes
556 - sorted[0]->getSizes()
557 + sorted[1]->getSizes();
558 }
559 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800560 }
561
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700562 // skip if we have neither worst nor naughty filters
563 if ((worst == (uid_t) -1) && !hasBlacklist) {
564 break;
565 }
566
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800567 bool kick = false;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700568 bool leading = true;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700569 it = mLogElements.begin();
Mark Salyzyn50e21082015-09-08 09:12:51 -0700570 // Perform at least one mandatory garbage collection cycle in following
571 // - clear leading chatty tags
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700572 // - coalesce chatty tags
Mark Salyzyn50e21082015-09-08 09:12:51 -0700573 // - check age-out of preserved logs
574 bool gc = pruneRows <= 1;
575 if (!gc && (worst != (uid_t) -1)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700576 { // begin scope for uid worst found iterator
577 LogBufferIteratorMap::iterator found = mLastWorstUid[id].find(worst);
578 if ((found != mLastWorstUid[id].end())
579 && (found->second != mLogElements.end())) {
580 leading = false;
581 it = found->second;
582 }
583 }
584 if (worstPid) {
585 // begin scope for pid worst found iterator
586 LogBufferPidIteratorMap::iterator found
587 = mLastWorstPidOfSystem[id].find(worstPid);
588 if ((found != mLastWorstPidOfSystem[id].end())
589 && (found->second != mLogElements.end())) {
590 leading = false;
591 it = found->second;
592 }
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700593 }
594 }
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700595 static const timespec too_old = {
596 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
597 };
598 LogBufferElementCollection::iterator lastt;
599 lastt = mLogElements.end();
600 --lastt;
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700601 LogBufferElementLast last;
Mark Salyzyncfcffdaf2015-08-19 17:06:11 -0700602 while (it != mLogElements.end()) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700603 LogBufferElement *element = *it;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800604
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700605 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700606 busy = true;
Mark Salyzyn552b4752015-11-30 11:35:56 -0800607 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
608 oldest->triggerReader_Locked();
609 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800610 break;
611 }
612
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700613 if (element->getLogId() != id) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800614 ++it;
615 continue;
616 }
617
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700618 unsigned short dropped = element->getDropped();
Mark Salyzync89839a2014-02-11 12:29:31 -0800619
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700620 // remove any leading drops
621 if (leading && dropped) {
622 it = erase(it);
623 continue;
624 }
625
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700626 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700627 it = erase(it, true);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700628 continue;
629 }
630
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700631 if (hasBlacklist && mPrune.naughty(element)) {
632 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700633 it = erase(it);
634 if (dropped) {
635 continue;
636 }
637
638 pruneRows--;
639 if (pruneRows == 0) {
640 break;
641 }
642
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700643 if (element->getUid() == worst) {
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700644 kick = true;
645 if (worst_sizes < second_worst_sizes) {
646 break;
647 }
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700648 worst_sizes -= element->getMsgLen();
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700649 }
650 continue;
651 }
652
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700653 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
654 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzyn4e29e172015-08-24 13:43:27 -0700655 break;
656 }
657
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700658 if (dropped) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700659 last.add(element);
660 if (worstPid
661 && ((!gc && (element->getPid() == worstPid))
662 || (mLastWorstPidOfSystem[id].find(element->getPid())
663 == mLastWorstPidOfSystem[id].end()))) {
664 mLastWorstPidOfSystem[id][element->getUid()] = it;
665 }
666 if ((!gc && !worstPid && (element->getUid() == worst))
667 || (mLastWorstUid[id].find(element->getUid())
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700668 == mLastWorstUid[id].end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700669 mLastWorstUid[id][element->getUid()] = it;
Mark Salyzyncb15c7c2015-08-24 13:43:27 -0700670 }
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800671 ++it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700672 continue;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800673 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700674
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700675 if ((element->getUid() != worst)
676 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzynfd318a82015-06-01 09:41:19 -0700677 leading = false;
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700678 last.clear(element);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700679 ++it;
680 continue;
681 }
682
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700683 pruneRows--;
684 if (pruneRows == 0) {
685 break;
686 }
687
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700688 kick = true;
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700689
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700690 unsigned short len = element->getMsgLen();
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700691
692 // do not create any leading drops
693 if (leading) {
694 it = erase(it);
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700695 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700696 stats.drop(element);
697 element->setDropped(1);
698 if (last.coalesce(element, 1)) {
Mark Salyzyn95ff2482015-09-30 07:40:09 -0700699 it = erase(it, true);
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700700 } else {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700701 last.add(element);
702 if (worstPid && (!gc
703 || (mLastWorstPidOfSystem[id].find(worstPid)
704 == mLastWorstPidOfSystem[id].end()))) {
705 mLastWorstPidOfSystem[id][worstPid] = it;
706 }
707 if ((!gc && !worstPid) || (mLastWorstUid[id].find(worst)
Mark Salyzyn50e21082015-09-08 09:12:51 -0700708 == mLastWorstUid[id].end())) {
709 mLastWorstUid[id][worst] = it;
710 }
Mark Salyzyn65ec8602015-05-22 10:03:31 -0700711 ++it;
712 }
Mark Salyzyna69d5a42015-03-16 12:04:09 -0700713 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700714 if (worst_sizes < second_worst_sizes) {
715 break;
716 }
717 worst_sizes -= len;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800718 }
Mark Salyzynd84b27e2015-04-17 15:38:04 -0700719 last.clear();
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800720
Mark Salyzync402bc72014-04-01 17:19:47 -0700721 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800722 break; // the following loop will ask bad clients to skip/drop
723 }
724 }
725
Mark Salyzync89839a2014-02-11 12:29:31 -0800726 bool whitelist = false;
Mark Salyzyn8885daf2015-12-04 10:59:45 -0800727 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn5c06ca42014-02-06 18:11:13 -0800728 it = mLogElements.begin();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800729 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700730 LogBufferElement *element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700731
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700732 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700733 it++;
734 continue;
735 }
736
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700737 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700738 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700739 if (whitelist) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800740 break;
741 }
Mark Salyzync402bc72014-04-01 17:19:47 -0700742
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700743 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
744 // kick a misbehaving log reader client off the island
745 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800746 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
747 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700748 } else {
749 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzync89839a2014-02-11 12:29:31 -0800750 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700751 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800752 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700753
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700754 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
755 // WhiteListed
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700756 whitelist = true;
757 it++;
758 continue;
759 }
760
761 it = erase(it);
762 pruneRows--;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800763 }
764
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700765 // Do not save the whitelist if we are reader range limited
Mark Salyzync89839a2014-02-11 12:29:31 -0800766 if (whitelist && (pruneRows > 0)) {
767 it = mLogElements.begin();
768 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700769 LogBufferElement *element = *it;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700770
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700771 if (element->getLogId() != id) {
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700772 ++it;
773 continue;
Mark Salyzync89839a2014-02-11 12:29:31 -0800774 }
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700775
Mark Salyzyn538c6a92015-08-28 08:02:59 -0700776 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700777 busy = true;
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700778 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
779 // kick a misbehaving log reader client off the island
780 oldest->release_Locked();
Mark Salyzyn552b4752015-11-30 11:35:56 -0800781 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
782 oldest->triggerReader_Locked();
Mark Salyzyn9cf5d402015-03-10 13:51:35 -0700783 } else {
784 oldest->triggerSkip_Locked(id, pruneRows);
785 }
786 break;
787 }
788
789 it = erase(it);
790 pruneRows--;
Mark Salyzync89839a2014-02-11 12:29:31 -0800791 }
792 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800793
Mark Salyzyn12bac902014-02-26 09:50:16 -0800794 LogTimeEntry::unlock();
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700795
796 return (pruneRows > 0) && busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800797}
798
799// clear all rows of type "id" from the buffer.
Mark Salyzyn7702c3e2015-09-16 15:34:00 -0700800bool LogBuffer::clear(log_id_t id, uid_t uid) {
801 bool busy = true;
802 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
803 for (int retry = 4;;) {
804 if (retry == 1) { // last pass
805 // Check if it is still busy after the sleep, we say prune
806 // one entry, not another clear run, so we are looking for
807 // the quick side effect of the return value to tell us if
808 // we have a _blocked_ reader.
809 pthread_mutex_lock(&mLogElementsLock);
810 busy = prune(id, 1, uid);
811 pthread_mutex_unlock(&mLogElementsLock);
812 // It is still busy, blocked reader(s), lets kill them all!
813 // otherwise, lets be a good citizen and preserve the slow
814 // readers and let the clear run (below) deal with determining
815 // if we are still blocked and return an error code to caller.
816 if (busy) {
817 LogTimeEntry::lock();
818 LastLogTimes::iterator times = mTimes.begin();
819 while (times != mTimes.end()) {
820 LogTimeEntry *entry = (*times);
821 // Killer punch
822 if (entry->owned_Locked() && entry->isWatching(id)) {
823 entry->release_Locked();
824 }
825 times++;
826 }
827 LogTimeEntry::unlock();
828 }
829 }
830 pthread_mutex_lock(&mLogElementsLock);
831 busy = prune(id, ULONG_MAX, uid);
832 pthread_mutex_unlock(&mLogElementsLock);
833 if (!busy || !--retry) {
834 break;
835 }
836 sleep (1); // Let reader(s) catch up after notification
837 }
838 return busy;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800839}
840
841// get the used space associated with "id".
842unsigned long LogBuffer::getSizeUsed(log_id_t id) {
843 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800844 size_t retval = stats.sizes(id);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800845 pthread_mutex_unlock(&mLogElementsLock);
846 return retval;
847}
848
Mark Salyzync89839a2014-02-11 12:29:31 -0800849// set the total space allocated to "id"
850int LogBuffer::setSize(log_id_t id, unsigned long size) {
851 // Reasonable limits ...
Mark Salyzyn7b85c592014-05-09 17:44:18 -0700852 if (!valid_size(size)) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800853 return -1;
854 }
855 pthread_mutex_lock(&mLogElementsLock);
856 log_buffer_size(id) = size;
857 pthread_mutex_unlock(&mLogElementsLock);
858 return 0;
859}
860
861// get the total space allocated to "id"
862unsigned long LogBuffer::getSize(log_id_t id) {
863 pthread_mutex_lock(&mLogElementsLock);
864 size_t retval = log_buffer_size(id);
865 pthread_mutex_unlock(&mLogElementsLock);
866 return retval;
867}
868
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800869uint64_t LogBuffer::flushTo(
870 SocketClient *reader, const uint64_t start, bool privileged,
871 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800872 LogBufferElementCollection::iterator it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800873 uint64_t max = start;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800874 uid_t uid = reader->getUid();
875
876 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600877
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800878 if (start <= 1) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600879 // client wants to start from the beginning
880 it = mLogElements.begin();
881 } else {
882 // Client wants to start from some specified time. Chances are
883 // we are better off starting from the end of the time sorted list.
884 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
885 --it;
886 LogBufferElement *element = *it;
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800887 if (element->getSequence() <= start) {
Dragoslav Mitrinovic81624f22015-01-15 09:29:43 -0600888 it++;
889 break;
890 }
891 }
892 }
893
894 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800895 LogBufferElement *element = *it;
896
897 if (!privileged && (element->getUid() != uid)) {
898 continue;
899 }
900
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800901 if (element->getSequence() <= start) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800902 continue;
903 }
904
905 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzyn0aaf6cd2015-03-03 13:39:37 -0800906 if (filter) {
907 int ret = (*filter)(element, arg);
908 if (ret == false) {
909 continue;
910 }
911 if (ret != true) {
912 break;
913 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800914 }
915
916 pthread_mutex_unlock(&mLogElementsLock);
917
918 // range locking in LastLogTimes looks after us
Mark Salyzyn3443fda2015-12-03 15:38:35 -0800919 max = element->flushTo(reader, this, privileged);
Mark Salyzyn12bac902014-02-26 09:50:16 -0800920
921 if (max == element->FLUSH_ERROR) {
922 return max;
923 }
924
925 pthread_mutex_lock(&mLogElementsLock);
926 }
927 pthread_mutex_unlock(&mLogElementsLock);
928
929 return max;
930}
Mark Salyzynd774bce2014-02-06 14:48:50 -0800931
Mark Salyzynb921ab52015-12-17 09:58:43 -0800932std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
933 unsigned int logMask) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800934 pthread_mutex_lock(&mLogElementsLock);
935
Mark Salyzynb921ab52015-12-17 09:58:43 -0800936 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzynd774bce2014-02-06 14:48:50 -0800937
938 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn1c7b6fb2015-08-20 10:01:44 -0700939
940 return ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800941}