blob: aff8a467662d890f652f3005de21fcf63ad5f8a6 [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 Salyzynfa07f9d2016-10-21 09:46:42 -0700225 // Remove iterator references in the various lists that will become stale
226 // after the element is erased from the main logging list.
227
Mark Salyzyn6a066942016-07-14 15:34:30 -0700228 { // start of scope for found iterator
229 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
230 element->getTag() : element->getUid();
231 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
232 if ((found != mLastWorst[id].end()) && (it == found->second)) {
233 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700234 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700235 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700236
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700237 if ((id != LOG_ID_EVENTS) && (id != LOG_ID_SECURITY)) {
238 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700239 // start of scope for pid found iterator
240 LogBufferPidIteratorMap::iterator found =
241 mLastWorstPidOfSystem[id].find(element->getPid());
242 if ((found != mLastWorstPidOfSystem[id].end())
243 && (it == found->second)) {
244 mLastWorstPidOfSystem[id].erase(found);
245 }
246 }
247
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800248 bool setLast[LOG_ID_MAX];
249 bool doSetLast = false;
250 log_id_for_each(i) {
251 doSetLast |= setLast[i] = mLastSet[i] && (it == mLast[i]);
252 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700253 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800254 if (doSetLast) {
255 log_id_for_each(i) {
256 if (setLast[i]) {
257 if (it == mLogElements.end()) { // unlikely
258 mLastSet[i] = false;
259 } else {
260 mLast[i] = it;
261 }
262 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800263 }
264 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700265 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700266 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700267 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700268 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700269 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700270 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700271
272 return it;
273}
274
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700275// Define a temporary mechanism to report the last LogBufferElement pointer
276// for the specified uid, pid and tid. Used below to help merge-sort when
277// pruning for worst UID.
278class LogBufferElementKey {
279 const union {
280 struct {
281 uint16_t uid;
282 uint16_t pid;
283 uint16_t tid;
284 uint16_t padding;
285 } __packed;
286 uint64_t value;
287 } __packed;
288
289public:
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700290 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid):
291 uid(uid),
292 pid(pid),
293 tid(tid),
294 padding(0) {
295 }
Chih-Hung Hsieh1cc82ce2016-04-25 13:49:46 -0700296 explicit LogBufferElementKey(uint64_t key):value(key) { }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700297
298 uint64_t getKey() { return value; }
299};
300
Mark Salyzyn511338d2015-05-19 09:12:30 -0700301class LogBufferElementLast {
302
303 typedef std::unordered_map<uint64_t, LogBufferElement *> LogBufferElementMap;
304 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700305
306public:
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700307
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700308 bool coalesce(LogBufferElement *element, unsigned short dropped) {
309 LogBufferElementKey key(element->getUid(),
310 element->getPid(),
311 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700312 LogBufferElementMap::iterator it = map.find(key.getKey());
313 if (it != map.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700314 LogBufferElement *found = it->second;
315 unsigned short moreDropped = found->getDropped();
316 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700317 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700318 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700319 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700320 return true;
321 }
322 }
323 return false;
324 }
325
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700326 void add(LogBufferElement *element) {
327 LogBufferElementKey key(element->getUid(),
328 element->getPid(),
329 element->getTid());
330 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700331 }
332
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700333 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700334 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700335 }
336
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700337 void clear(LogBufferElement *element) {
338 uint64_t current = element->getRealTime().nsec()
Mark Salyzyn047cc072015-06-04 13:35:30 -0700339 - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn511338d2015-05-19 09:12:30 -0700340 for(LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700341 LogBufferElement *mapElement = it->second;
342 if ((mapElement->getDropped() >= EXPIRE_THRESHOLD)
343 && (current > mapElement->getRealTime().nsec())) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700344 it = map.erase(it);
345 } else {
346 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700347 }
348 }
349 }
350
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700351};
352
Mark Salyzyn0175b072014-02-26 09:50:16 -0800353// prune "pruneRows" of type "id" from the buffer.
354//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700355// This garbage collection task is used to expire log entries. It is called to
356// remove all logs (clear), all UID logs (unprivileged clear), or every
357// 256 or 10% of the total logs (whichever is less) to prune the logs.
358//
359// First there is a prep phase where we discover the reader region lock that
360// acts as a backstop to any pruning activity to stop there and go no further.
361//
362// There are three major pruning loops that follow. All expire from the oldest
363// entries. Since there are multiple log buffers, the Android logging facility
364// will appear to drop entries 'in the middle' when looking at multiple log
365// sources and buffers. This effect is slightly more prominent when we prune
366// the worst offender by logging source. Thus the logs slowly loose content
367// and value as you move back in time. This is preferred since chatty sources
368// invariably move the logs value down faster as less chatty sources would be
369// expired in the noise.
370//
371// The first loop performs blacklisting and worst offender pruning. Falling
372// through when there are no notable worst offenders and have not hit the
373// region lock preventing further worst offender pruning. This loop also looks
374// after managing the chatty log entries and merging to help provide
375// statistical basis for blame. The chatty entries are not a notification of
376// how much logs you may have, but instead represent how much logs you would
377// have had in a virtual log buffer that is extended to cover all the in-memory
378// logs without loss. They last much longer than the represented pruned logs
379// since they get multiplied by the gains in the non-chatty log sources.
380//
381// The second loop get complicated because an algorithm of watermarks and
382// history is maintained to reduce the order and keep processing time
383// down to a minimum at scale. These algorithms can be costly in the face
384// of larger log buffers, or severly limited processing time granted to a
385// background task at lowest priority.
386//
387// This second loop does straight-up expiration from the end of the logs
388// (again, remember for the specified log buffer id) but does some whitelist
389// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
390// spam filtration all take priority. This second loop also checks if a region
391// lock is causing us to buffer too much in the logs to help the reader(s),
392// and will tell the slowest reader thread to skip log entries, and if
393// persistent and hits a further threshold, kill the reader thread.
394//
395// The third thread is optional, and only gets hit if there was a whitelist
396// and more needs to be pruned against the backstop of the region lock.
397//
Mark Salyzyn0175b072014-02-26 09:50:16 -0800398// mLogElementsLock must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700399//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700400bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800401 LogTimeEntry *oldest = NULL;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700402 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700403 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800404
405 LogTimeEntry::lock();
406
407 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700408 LastLogTimes::iterator times = mTimes.begin();
409 while(times != mTimes.end()) {
410 LogTimeEntry *entry = (*times);
TraianX Schiauda6495d2014-12-17 10:53:41 +0200411 if (entry->owned_Locked() && entry->isWatching(id)
Mark Salyzynb75cce02015-11-30 11:35:56 -0800412 && (!oldest ||
413 (oldest->mStart > entry->mStart) ||
414 ((oldest->mStart == entry->mStart) &&
415 (entry->mTimeout.tv_sec || entry->mTimeout.tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800416 oldest = entry;
417 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700418 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800419 }
420
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800421 LogBufferElementCollection::iterator it;
422
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700423 if (caller_uid != AID_ROOT) {
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700424 // Only here if clear all request from non system source, so chatty
425 // filter logistics is not required.
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800426 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
427 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700428 LogBufferElement *element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700429
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700430 if ((element->getLogId() != id) || (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700431 ++it;
432 continue;
433 }
434
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800435 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
436 mLast[id] = it;
437 mLastSet[id] = true;
438 }
439
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700440 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700441 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800442 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
443 oldest->triggerReader_Locked();
444 } else {
445 oldest->triggerSkip_Locked(id, pruneRows);
446 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700447 break;
448 }
449
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700450 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700451 if (--pruneRows == 0) {
452 break;
453 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700454 }
455 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700456 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700457 }
458
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700459 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Mark Salyzyn083b0372015-12-04 10:59:45 -0800460 bool hasBlacklist = (id != LOG_ID_SECURITY) && mPrune.naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700461 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800462 // recalculate the worst offender on every batched pass
Mark Salyzyn6a066942016-07-14 15:34:30 -0700463 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800464 size_t worst_sizes = 0;
465 size_t second_worst_sizes = 0;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700466 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800467
Mark Salyzynae769232015-03-17 17:17:25 -0700468 if (worstUidEnabledForLogid(id) && mPrune.worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700469 // Calculate threshold as 12.5% of available storage
470 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700471
Mark Salyzyn6a066942016-07-14 15:34:30 -0700472 if ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) {
473 stats.sortTags(AID_ROOT, (pid_t)0, 2, id).findWorst(
474 worst, worst_sizes, second_worst_sizes, threshold);
475 } else {
476 stats.sort(AID_ROOT, (pid_t)0, 2, id).findWorst(
477 worst, worst_sizes, second_worst_sizes, threshold);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700478
Mark Salyzyn6a066942016-07-14 15:34:30 -0700479 if ((worst == AID_SYSTEM) && mPrune.worstPidOfSystemEnabled()) {
480 stats.sortPids(worst, (pid_t)0, 2, id).findWorst(
481 worstPid, worst_sizes, second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700482 }
483 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800484 }
485
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700486 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700487 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700488 break;
489 }
490
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800491 bool kick = false;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700492 bool leading = true;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800493 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700494 // Perform at least one mandatory garbage collection cycle in following
495 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700496 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700497 // - check age-out of preserved logs
498 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700499 if (!gc && (worst != -1)) {
500 { // begin scope for worst found iterator
501 LogBufferIteratorMap::iterator found = mLastWorst[id].find(worst);
502 if ((found != mLastWorst[id].end())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700503 && (found->second != mLogElements.end())) {
504 leading = false;
505 it = found->second;
506 }
507 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700508 if (worstPid) { // Only set if !LOG_ID_EVENTS and !LOG_ID_SECURITY
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700509 // begin scope for pid worst found iterator
510 LogBufferPidIteratorMap::iterator found
511 = mLastWorstPidOfSystem[id].find(worstPid);
512 if ((found != mLastWorstPidOfSystem[id].end())
513 && (found->second != mLogElements.end())) {
514 leading = false;
515 it = found->second;
516 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700517 }
518 }
Mark Salyzynccfe8442015-08-24 13:43:27 -0700519 static const timespec too_old = {
520 EXPIRE_HOUR_THRESHOLD * 60 * 60, 0
521 };
522 LogBufferElementCollection::iterator lastt;
523 lastt = mLogElements.end();
524 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700525 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700526 while (it != mLogElements.end()) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700527 LogBufferElement *element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800528
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700529 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700530 busy = true;
Mark Salyzynb75cce02015-11-30 11:35:56 -0800531 if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
532 oldest->triggerReader_Locked();
533 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800534 break;
535 }
536
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700537 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800538 ++it;
539 continue;
540 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700541 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800542
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800543 if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
544 mLast[id] = it;
545 mLastSet[id] = true;
546 }
547
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700548 unsigned short dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800549
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700550 // remove any leading drops
551 if (leading && dropped) {
552 it = erase(it);
553 continue;
554 }
555
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700556 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700557 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700558 continue;
559 }
560
Mark Salyzyn6a066942016-07-14 15:34:30 -0700561 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
562 element->getTag() :
563 element->getUid();
564
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700565 if (hasBlacklist && mPrune.naughty(element)) {
566 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700567 it = erase(it);
568 if (dropped) {
569 continue;
570 }
571
572 pruneRows--;
573 if (pruneRows == 0) {
574 break;
575 }
576
Mark Salyzyn6a066942016-07-14 15:34:30 -0700577 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700578 kick = true;
579 if (worst_sizes < second_worst_sizes) {
580 break;
581 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700582 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700583 }
584 continue;
585 }
586
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700587 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old))
588 || (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700589 break;
590 }
591
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700592 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700593 last.add(element);
594 if (worstPid
595 && ((!gc && (element->getPid() == worstPid))
Mark Salyzyn6a066942016-07-14 15:34:30 -0700596 || (mLastWorstPidOfSystem[id].find(element->getPid())
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700597 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700598 // element->getUid() may not be AID_SYSTEM, next best
599 // watermark if current one empty.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700600 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700601 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700602 if ((!gc && !worstPid && (key == worst))
603 || (mLastWorst[id].find(key) == mLastWorst[id].end())) {
604 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700605 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800606 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700607 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800608 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700609
Mark Salyzyn6a066942016-07-14 15:34:30 -0700610 if ((key != worst)
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700611 || (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700612 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700613 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700614 ++it;
615 continue;
616 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700617 // key == worst below here
618 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700619
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700620 pruneRows--;
621 if (pruneRows == 0) {
622 break;
623 }
624
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700625 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700626
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700627 unsigned short len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700628
629 // do not create any leading drops
630 if (leading) {
631 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700632 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700633 stats.drop(element);
634 element->setDropped(1);
635 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700636 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700637 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700638 last.add(element);
639 if (worstPid && (!gc
640 || (mLastWorstPidOfSystem[id].find(worstPid)
641 == mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700642 // element->getUid() may not be AID_SYSTEM, next best
643 // watermark if current one empty.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700644 mLastWorstPidOfSystem[id][worstPid] = it;
645 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700646 if ((!gc && !worstPid) ||
647 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
648 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700649 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700650 ++it;
651 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700652 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700653 if (worst_sizes < second_worst_sizes) {
654 break;
655 }
656 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800657 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700658 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800659
Mark Salyzyn1c950472014-04-01 17:19:47 -0700660 if (!kick || !mPrune.worstUidEnabled()) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800661 break; // the following loop will ask bad clients to skip/drop
662 }
663 }
664
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800665 bool whitelist = false;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800666 bool hasWhitelist = (id != LOG_ID_SECURITY) && mPrune.nice() && !clearAll;
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800667 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800668 while((pruneRows > 0) && (it != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700669 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700670
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700671 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700672 it++;
673 continue;
674 }
675
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800676 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
677 mLast[id] = it;
678 mLastSet[id] = true;
679 }
680
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700681 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700682 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700683 if (whitelist) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800684 break;
685 }
Mark Salyzyn1c950472014-04-01 17:19:47 -0700686
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700687 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
688 // kick a misbehaving log reader client off the island
689 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800690 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
691 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700692 } else {
693 oldest->triggerSkip_Locked(id, pruneRows);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800694 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700695 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800696 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700697
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700698 if (hasWhitelist && !element->getDropped() && mPrune.nice(element)) {
699 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700700 whitelist = true;
701 it++;
702 continue;
703 }
704
705 it = erase(it);
706 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800707 }
708
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700709 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800710 if (whitelist && (pruneRows > 0)) {
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800711 it = mLastSet[id] ? mLast[id] : mLogElements.begin();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800712 while((it != mLogElements.end()) && (pruneRows > 0)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700713 LogBufferElement *element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700714
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700715 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700716 ++it;
717 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800718 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700719
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800720 if (!mLastSet[id] || ((*mLast[id])->getLogId() != id)) {
721 mLast[id] = it;
722 mLastSet[id] = true;
723 }
724
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700725 if (oldest && (oldest->mStart <= element->getSequence())) {
Mark Salyzync5dc9702015-09-16 15:34:00 -0700726 busy = true;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700727 if (stats.sizes(id) > (2 * log_buffer_size(id))) {
728 // kick a misbehaving log reader client off the island
729 oldest->release_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -0800730 } else if (oldest->mTimeout.tv_sec || oldest->mTimeout.tv_nsec) {
731 oldest->triggerReader_Locked();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700732 } else {
733 oldest->triggerSkip_Locked(id, pruneRows);
734 }
735 break;
736 }
737
738 it = erase(it);
739 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800740 }
741 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800742
Mark Salyzyn0175b072014-02-26 09:50:16 -0800743 LogTimeEntry::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700744
745 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800746}
747
748// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700749bool LogBuffer::clear(log_id_t id, uid_t uid) {
750 bool busy = true;
751 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
752 for (int retry = 4;;) {
753 if (retry == 1) { // last pass
754 // Check if it is still busy after the sleep, we say prune
755 // one entry, not another clear run, so we are looking for
756 // the quick side effect of the return value to tell us if
757 // we have a _blocked_ reader.
758 pthread_mutex_lock(&mLogElementsLock);
759 busy = prune(id, 1, uid);
760 pthread_mutex_unlock(&mLogElementsLock);
761 // It is still busy, blocked reader(s), lets kill them all!
762 // otherwise, lets be a good citizen and preserve the slow
763 // readers and let the clear run (below) deal with determining
764 // if we are still blocked and return an error code to caller.
765 if (busy) {
766 LogTimeEntry::lock();
767 LastLogTimes::iterator times = mTimes.begin();
768 while (times != mTimes.end()) {
769 LogTimeEntry *entry = (*times);
770 // Killer punch
771 if (entry->owned_Locked() && entry->isWatching(id)) {
772 entry->release_Locked();
773 }
774 times++;
775 }
776 LogTimeEntry::unlock();
777 }
778 }
779 pthread_mutex_lock(&mLogElementsLock);
780 busy = prune(id, ULONG_MAX, uid);
781 pthread_mutex_unlock(&mLogElementsLock);
782 if (!busy || !--retry) {
783 break;
784 }
785 sleep (1); // Let reader(s) catch up after notification
786 }
787 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800788}
789
790// get the used space associated with "id".
791unsigned long LogBuffer::getSizeUsed(log_id_t id) {
792 pthread_mutex_lock(&mLogElementsLock);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800793 size_t retval = stats.sizes(id);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800794 pthread_mutex_unlock(&mLogElementsLock);
795 return retval;
796}
797
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800798// set the total space allocated to "id"
799int LogBuffer::setSize(log_id_t id, unsigned long size) {
800 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700801 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800802 return -1;
803 }
804 pthread_mutex_lock(&mLogElementsLock);
805 log_buffer_size(id) = size;
806 pthread_mutex_unlock(&mLogElementsLock);
807 return 0;
808}
809
810// get the total space allocated to "id"
811unsigned long LogBuffer::getSize(log_id_t id) {
812 pthread_mutex_lock(&mLogElementsLock);
813 size_t retval = log_buffer_size(id);
814 pthread_mutex_unlock(&mLogElementsLock);
815 return retval;
816}
817
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800818uint64_t LogBuffer::flushTo(
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800819 SocketClient *reader, const uint64_t start,
820 bool privileged, bool security,
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800821 int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800822 LogBufferElementCollection::iterator it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800823 uint64_t max = start;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800824 uid_t uid = reader->getUid();
825
826 pthread_mutex_lock(&mLogElementsLock);
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600827
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800828 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600829 // client wants to start from the beginning
830 it = mLogElements.begin();
831 } else {
832 // Client wants to start from some specified time. Chances are
833 // we are better off starting from the end of the time sorted list.
834 for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
835 --it;
836 LogBufferElement *element = *it;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800837 if (element->getSequence() <= start) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600838 it++;
839 break;
840 }
841 }
842 }
843
844 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800845 LogBufferElement *element = *it;
846
847 if (!privileged && (element->getUid() != uid)) {
848 continue;
849 }
850
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800851 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
852 continue;
853 }
854
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800855 if (element->getSequence() <= start) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800856 continue;
857 }
858
859 // NB: calling out to another object with mLogElementsLock held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800860 if (filter) {
861 int ret = (*filter)(element, arg);
862 if (ret == false) {
863 continue;
864 }
865 if (ret != true) {
866 break;
867 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800868 }
869
870 pthread_mutex_unlock(&mLogElementsLock);
871
872 // range locking in LastLogTimes looks after us
Mark Salyzyn7b873652015-12-03 15:38:35 -0800873 max = element->flushTo(reader, this, privileged);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800874
875 if (max == element->FLUSH_ERROR) {
876 return max;
877 }
878
879 pthread_mutex_lock(&mLogElementsLock);
880 }
881 pthread_mutex_unlock(&mLogElementsLock);
882
883 return max;
884}
Mark Salyzyn34facab2014-02-06 14:48:50 -0800885
Mark Salyzynee3b8382015-12-17 09:58:43 -0800886std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
887 unsigned int logMask) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800888 pthread_mutex_lock(&mLogElementsLock);
889
Mark Salyzynee3b8382015-12-17 09:58:43 -0800890 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800891
892 pthread_mutex_unlock(&mLogElementsLock);
Mark Salyzyn73160ac2015-08-20 10:01:44 -0700893
894 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800895}