blob: ad391e5dfdb9cc7abc45165efb44fd7fd9fad1c1 [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 */
Mark Salyzyn60636fa2016-10-24 16:22:17 -070016// for manual checking of stale entries during LogBuffer::erase()
17//#define DEBUG_CHECK_FOR_STALE_ENTRIES
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
Mark Salyzyn671e3432014-05-06 07:34:59 -070019#include <ctype.h>
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -080020#include <endian.h>
Mark Salyzyn202e1532015-02-09 08:21:05 -080021#include <errno.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080022#include <stdio.h>
23#include <string.h>
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070024#include <sys/cdefs.h>
Mark Salyzyn57a0af92014-05-09 17:44:18 -070025#include <sys/user.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080026#include <time.h>
27#include <unistd.h>
28
Mark Salyzyn511338d2015-05-19 09:12:30 -070029#include <unordered_map>
Tom Cherry10d086e2019-08-21 14:16:34 -070030#include <utility>
Mark Salyzyn511338d2015-05-19 09:12:30 -070031
Mark Salyzyn671e3432014-05-06 07:34:59 -070032#include <cutils/properties.h>
Mark Salyzynf10e2732016-09-27 13:08:23 -070033#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080034
35#include "LogBuffer.h"
Mark Salyzynb6bee332015-09-08 08:56:32 -070036#include "LogKlog.h"
Mark Salyzyn671e3432014-05-06 07:34:59 -070037#include "LogReader.h"
Mark Salyzyna2c02222016-12-13 10:31:29 -080038#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080039
Mark Salyzyn8fcfd852016-10-24 08:20:26 -070040#ifndef __predict_false
41#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
42#endif
43
Mark Salyzyndfa7a072014-02-11 12:29:31 -080044// Default
Mark Salyzyndfa7a072014-02-11 12:29:31 -080045#define log_buffer_size(id) mMaxSize[id]
Mark Salyzyn671e3432014-05-06 07:34:59 -070046
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070047void LogBuffer::init() {
Mark Salyzyndfa7a072014-02-11 12:29:31 -080048 log_id_for_each(i) {
Mark Salyzynf10e2732016-09-27 13:08:23 -070049 if (setSize(i, __android_logger_get_buffer_size(i))) {
Mark Salyzyn57a0af92014-05-09 17:44:18 -070050 setSize(i, LOG_BUFFER_MIN_SIZE);
51 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -080052 }
Tom Cherrye170d1a2020-05-01 16:45:25 -070053 // Release any sleeping reader threads to dump their current content.
Tom Cherry6ec71e92020-05-04 12:53:36 -070054 LogReaderThread::wrlock();
Mark Salyzynb75cce02015-11-30 11:35:56 -080055
56 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080057 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -070058 LogReaderThread* entry = times->get();
Tom Cherry4f227862018-10-08 17:33:50 -070059 entry->triggerReader_Locked();
Mark Salyzynb75cce02015-11-30 11:35:56 -080060 times++;
Mark Salyzynb6bee332015-09-08 08:56:32 -070061 }
Mark Salyzynb75cce02015-11-30 11:35:56 -080062
Tom Cherry6ec71e92020-05-04 12:53:36 -070063 LogReaderThread::unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080064}
65
Tom Cherry5a3db392020-05-01 17:03:20 -070066LogBuffer::LogBuffer(LastLogTimes* times, LogTags* tags, PruneList* prune)
Tom Cherryf2c27462020-04-08 14:36:05 -070067 : mTimes(*times), tags_(tags), prune_(prune) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -070068 pthread_rwlock_init(&mLogElementsLock, nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070069
Mark Salyzyna2c02222016-12-13 10:31:29 -080070 log_id_for_each(i) {
Mark Salyzynae2abf12017-03-31 10:48:39 -070071 lastLoggedElements[i] = nullptr;
72 droppedElements[i] = nullptr;
Mark Salyzyna2c02222016-12-13 10:31:29 -080073 }
74
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070075 init();
76}
77
Mark Salyzyna2c02222016-12-13 10:31:29 -080078LogBuffer::~LogBuffer() {
79 log_id_for_each(i) {
80 delete lastLoggedElements[i];
81 delete droppedElements[i];
82 }
83}
84
Tom Cherry385c2c92020-04-29 17:58:18 -070085LogBufferElementCollection::iterator LogBuffer::GetOldest(log_id_t log_id) {
86 auto it = mLogElements.begin();
Tom Cherry20118ee2020-05-04 10:17:42 -070087 if (oldest_[log_id]) {
88 it = *oldest_[log_id];
Tom Cherry385c2c92020-04-29 17:58:18 -070089 }
90 while (it != mLogElements.end() && (*it)->getLogId() != log_id) {
91 it++;
92 }
93 if (it != mLogElements.end()) {
Tom Cherry20118ee2020-05-04 10:17:42 -070094 oldest_[log_id] = it;
Tom Cherry385c2c92020-04-29 17:58:18 -070095 }
96 return it;
97}
98
Mark Salyzyn501c3732017-03-10 14:31:54 -080099enum match_type { DIFFERENT, SAME, SAME_LIBLOG };
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800100
Mark Salyzyn501c3732017-03-10 14:31:54 -0800101static enum match_type identical(LogBufferElement* elem,
102 LogBufferElement* last) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800103 // is it mostly identical?
Mark Salyzyn501c3732017-03-10 14:31:54 -0800104 // if (!elem) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700105 ssize_t lenl = elem->getMsgLen();
106 if (lenl <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800107 // if (!last) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700108 ssize_t lenr = last->getMsgLen();
109 if (lenr <= 0) return DIFFERENT; // value if this represents a chatty elem
Mark Salyzyn501c3732017-03-10 14:31:54 -0800110 // if (elem->getLogId() != last->getLogId()) return DIFFERENT;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800111 if (elem->getUid() != last->getUid()) return DIFFERENT;
112 if (elem->getPid() != last->getPid()) return DIFFERENT;
113 if (elem->getTid() != last->getTid()) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800114
115 // last is more than a minute old, stop squashing identical messages
116 if (elem->getRealTime().nsec() >
Mark Salyzyn501c3732017-03-10 14:31:54 -0800117 (last->getRealTime().nsec() + 60 * NS_PER_SEC))
118 return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800119
120 // Identical message
121 const char* msgl = elem->getMsg();
122 const char* msgr = last->getMsg();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800123 if (lenl == lenr) {
124 if (!fastcmp<memcmp>(msgl, msgr, lenl)) return SAME;
125 // liblog tagged messages (content gets summed)
126 if ((elem->getLogId() == LOG_ID_EVENTS) &&
127 (lenl == sizeof(android_log_event_int_t)) &&
Mark Salyzyn501c3732017-03-10 14:31:54 -0800128 !fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_int_t) -
129 sizeof(int32_t)) &&
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700130 (elem->getTag() == LIBLOG_LOG_TAG)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800131 return SAME_LIBLOG;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700132 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800133 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800134
135 // audit message (except sequence number) identical?
Mark Salyzynfec2e2c2018-03-13 11:06:38 -0700136 if (last->isBinary() &&
137 (lenl > static_cast<ssize_t>(sizeof(android_log_event_string_t))) &&
138 (lenr > static_cast<ssize_t>(sizeof(android_log_event_string_t)))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800139 if (fastcmp<memcmp>(msgl, msgr, sizeof(android_log_event_string_t) -
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700140 sizeof(int32_t))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800141 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700142 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800143 msgl += sizeof(android_log_event_string_t);
144 lenl -= sizeof(android_log_event_string_t);
145 msgr += sizeof(android_log_event_string_t);
146 lenr -= sizeof(android_log_event_string_t);
147 }
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700148 static const char avc[] = "): avc: ";
Mark Salyzyn501c3732017-03-10 14:31:54 -0800149 const char* avcl = android::strnstr(msgl, lenl, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800150 if (!avcl) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800151 lenl -= avcl - msgl;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800152 const char* avcr = android::strnstr(msgr, lenr, avc);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800153 if (!avcr) return DIFFERENT;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800154 lenr -= avcr - msgr;
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800155 if (lenl != lenr) return DIFFERENT;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700156 if (fastcmp<memcmp>(avcl + strlen(avc), avcr + strlen(avc),
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700157 lenl - strlen(avc))) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800158 return DIFFERENT;
Mark Salyzyn1598fe02017-03-13 15:41:59 -0700159 }
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800160 return SAME;
Mark Salyzyna2c02222016-12-13 10:31:29 -0800161}
162
Mark Salyzyn501c3732017-03-10 14:31:54 -0800163int LogBuffer::log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700164 pid_t tid, const char* msg, uint16_t len) {
Yi Kong141ccee2018-03-01 17:48:53 -0500165 if (log_id >= LOG_ID_MAX) {
Mark Salyzyn202e1532015-02-09 08:21:05 -0800166 return -EINVAL;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800167 }
Mark Salyzyne59c4692014-10-02 13:07:05 -0700168
Mark Salyzyn206ed8e2017-05-18 10:06:00 -0700169 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
170 // This prevents any chance that an outside source can request an
171 // exact entry with time specified in ms or us precision.
172 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
173
Tom Cherry2ac86de2020-02-20 13:21:51 -0800174 LogBufferElement* elem = new LogBufferElement(log_id, realtime, uid, pid, tid, msg, len);
175
176 // b/137093665: don't coalesce security messages.
177 if (log_id == LOG_ID_SECURITY) {
178 wrlock();
179 log(elem);
180 unlock();
181
182 return len;
183 }
184
185 int prio = ANDROID_LOG_INFO;
186 const char* tag = nullptr;
187 size_t tag_len = 0;
188 if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Tom Cherry1a12ae32020-05-01 16:13:18 -0700189 tag = tags_->tagToName(elem->getTag());
Tom Cherry2ac86de2020-02-20 13:21:51 -0800190 if (tag) {
191 tag_len = strlen(tag);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800192 }
Tom Cherry2ac86de2020-02-20 13:21:51 -0800193 } else {
194 prio = *msg;
195 tag = msg + 1;
196 tag_len = strnlen(tag, len - 1);
197 }
198 if (!__android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE)) {
199 // Log traffic received to total
200 wrlock();
201 stats.addTotal(elem);
202 unlock();
203 delete elem;
204 return -EACCES;
Mark Salyzyne59c4692014-10-02 13:07:05 -0700205 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800206
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700207 wrlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800208 LogBufferElement* currentLast = lastLoggedElements[log_id];
209 if (currentLast) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800210 LogBufferElement* dropped = droppedElements[log_id];
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700211 uint16_t count = dropped ? dropped->getDropped() : 0;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800212 //
213 // State Init
214 // incoming:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700215 // dropped = nullptr
216 // currentLast = nullptr;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800217 // elem = incoming message
218 // outgoing:
Mark Salyzynae2abf12017-03-31 10:48:39 -0700219 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800220 // currentLast = copy of elem
221 // log elem
222 // State 0
223 // incoming:
224 // count = 0
Mark Salyzynae2abf12017-03-31 10:48:39 -0700225 // dropped = nullptr
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800226 // currentLast = copy of last message
227 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800228 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800229 // dropped = copy of first identical message -> State 1
230 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800231 // break: if match == DIFFERENT
Mark Salyzynae2abf12017-03-31 10:48:39 -0700232 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800233 // delete copy of last message (incoming currentLast)
234 // currentLast = copy of elem
235 // log elem
236 // State 1
237 // incoming:
238 // count = 0
239 // dropped = copy of first identical message
240 // currentLast = reference to last held-back incoming
241 // message
242 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800243 // outgoing: if match == SAME
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800244 // delete copy of first identical message (dropped)
245 // dropped = reference to last held-back incoming
246 // message set to chatty count of 1 -> State 2
247 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800248 // outgoing: if match == SAME_LIBLOG
249 // dropped = copy of first identical message -> State 1
250 // take sum of currentLast and elem
251 // if sum overflows:
252 // log currentLast
253 // currentLast = reference to elem
254 // else
255 // delete currentLast
256 // currentLast = reference to elem, sum liblog.
257 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800258 // delete dropped
Mark Salyzynae2abf12017-03-31 10:48:39 -0700259 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800260 // log reference to last held-back (currentLast)
261 // currentLast = copy of elem
262 // log elem
263 // State 2
264 // incoming:
265 // count = chatty count
266 // dropped = chatty message holding count
267 // currentLast = reference to last held-back incoming
268 // message.
269 // dropped = chatty message holding count
270 // elem = incoming message
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800271 // outgoing: if match != DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800272 // delete chatty message holding count
273 // dropped = reference to last held-back incoming
274 // message, set to chatty count + 1
275 // currentLast = reference to elem
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800276 // break: if match == DIFFERENT
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800277 // log dropped (chatty message)
Mark Salyzynae2abf12017-03-31 10:48:39 -0700278 // dropped = nullptr -> State 0
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800279 // log reference to last held-back (currentLast)
280 // currentLast = copy of elem
281 // log elem
282 //
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800283 enum match_type match = identical(elem, currentLast);
284 if (match != DIFFERENT) {
Mark Salyzyna2c02222016-12-13 10:31:29 -0800285 if (dropped) {
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800286 // Sum up liblog tag messages?
287 if ((count == 0) /* at Pass 1 */ && (match == SAME_LIBLOG)) {
288 android_log_event_int_t* event =
289 reinterpret_cast<android_log_event_int_t*>(
290 const_cast<char*>(currentLast->getMsg()));
291 //
292 // To unit test, differentiate with something like:
293 // event->header.tag = htole32(CHATTY_LOG_TAG);
294 // here, then instead of delete currentLast below,
295 // log(currentLast) to see the incremental sums form.
296 //
297 uint32_t swab = event->payload.data;
298 unsigned long long total = htole32(swab);
299 event = reinterpret_cast<android_log_event_int_t*>(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800300 const_cast<char*>(elem->getMsg()));
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800301 swab = event->payload.data;
302
303 lastLoggedElements[LOG_ID_EVENTS] = elem;
304 total += htole32(swab);
305 // check for overflow
306 if (total >= UINT32_MAX) {
307 log(currentLast);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700308 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800309 return len;
310 }
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700311 stats.addTotal(currentLast);
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800312 delete currentLast;
313 swab = total;
314 event->payload.data = htole32(swab);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700315 unlock();
Mark Salyzyn1dfb4de2016-12-16 16:09:15 -0800316 return len;
317 }
Mark Salyzyna2c02222016-12-13 10:31:29 -0800318 if (count == USHRT_MAX) {
319 log(dropped);
320 count = 1;
321 } else {
322 delete dropped;
323 ++count;
324 }
325 }
326 if (count) {
Mark Salyzyn02dd2f42017-04-14 09:46:57 -0700327 stats.addTotal(currentLast);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800328 currentLast->setDropped(count);
329 }
330 droppedElements[log_id] = currentLast;
331 lastLoggedElements[log_id] = elem;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700332 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800333 return len;
334 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800335 if (dropped) { // State 1 or 2
336 if (count) { // State 2
337 log(dropped); // report chatty
338 } else { // State 1
339 delete dropped;
Mark Salyzyn8f83a352016-12-16 16:09:15 -0800340 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700341 droppedElements[log_id] = nullptr;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800342 log(currentLast); // report last message in the series
343 } else { // State 0
Mark Salyzyna2c02222016-12-13 10:31:29 -0800344 delete currentLast;
345 }
346 }
347 lastLoggedElements[log_id] = new LogBufferElement(*elem);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800348
Mark Salyzyna2c02222016-12-13 10:31:29 -0800349 log(elem);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700350 unlock();
Mark Salyzyna2c02222016-12-13 10:31:29 -0800351
352 return len;
353}
354
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700355// assumes LogBuffer::wrlock() held, owns elem, look after garbage collection
Mark Salyzyna2c02222016-12-13 10:31:29 -0800356void LogBuffer::log(LogBufferElement* elem) {
Tom Cherry65abf392019-08-21 13:17:12 -0700357 mLogElements.push_back(elem);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700358 stats.add(elem);
Mark Salyzyna2c02222016-12-13 10:31:29 -0800359 maybePrune(elem->getLogId());
Mark Salyzyn0175b072014-02-26 09:50:16 -0800360}
361
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700362// Prune at most 10% of the log entries or maxPrune, whichever is less.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800363//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700364// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn0175b072014-02-26 09:50:16 -0800365void LogBuffer::maybePrune(log_id_t id) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800366 size_t sizes = stats.sizes(id);
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700367 unsigned long maxSize = log_buffer_size(id);
368 if (sizes > maxSize) {
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700369 size_t sizeOver = sizes - ((maxSize * 9) / 10);
Mark Salyzyn58b8be82015-09-30 07:40:09 -0700370 size_t elements = stats.realElements(id);
371 size_t minElements = elements / 100;
372 if (minElements < minPrune) {
373 minElements = minPrune;
374 }
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700375 unsigned long pruneRows = elements * sizeOver / sizes;
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700376 if (pruneRows < minElements) {
Mark Salyzyn62ab0fd2015-08-10 10:23:56 -0700377 pruneRows = minElements;
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800378 }
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700379 if (pruneRows > maxPrune) {
380 pruneRows = maxPrune;
Mark Salyzynb39ed0c2015-08-19 12:20:36 -0700381 }
Mark Salyzyn740f9b42014-01-13 16:37:51 -0800382 prune(id, pruneRows);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800383 }
384}
385
Mark Salyzyn831aa292015-09-03 16:08:50 -0700386LogBufferElementCollection::iterator LogBuffer::erase(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800387 LogBufferElementCollection::iterator it, bool coalesce) {
388 LogBufferElement* element = *it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700389 log_id_t id = element->getLogId();
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700390
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700391 // Remove iterator references in the various lists that will become stale
392 // after the element is erased from the main logging list.
393
Mark Salyzyn501c3732017-03-10 14:31:54 -0800394 { // start of scope for found iterator
395 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
396 ? element->getTag()
397 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700398 LogBufferIteratorMap::iterator found = mLastWorst[id].find(key);
399 if ((found != mLastWorst[id].end()) && (it == found->second)) {
400 mLastWorst[id].erase(found);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700401 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700402 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700403
Mark Salyzyn501c3732017-03-10 14:31:54 -0800404 { // start of scope for pid found iterator
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700405 // element->getUid() may not be AID_SYSTEM for next-best-watermark.
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700406 // will not assume id != LOG_ID_EVENTS or LOG_ID_SECURITY for KISS and
407 // long term code stability, find() check should be fast for those ids.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700408 LogBufferPidIteratorMap::iterator found =
409 mLastWorstPidOfSystem[id].find(element->getPid());
Mark Salyzyn501c3732017-03-10 14:31:54 -0800410 if ((found != mLastWorstPidOfSystem[id].end()) &&
411 (it == found->second)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700412 mLastWorstPidOfSystem[id].erase(found);
413 }
414 }
415
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800416 bool setLast[LOG_ID_MAX];
417 bool doSetLast = false;
Tom Cherry20118ee2020-05-04 10:17:42 -0700418 log_id_for_each(i) { doSetLast |= setLast[i] = oldest_[i] && it == *oldest_[i]; }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700419#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
420 LogBufferElementCollection::iterator bad = it;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800421 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
422 ? element->getTag()
423 : element->getUid();
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700424#endif
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700425 it = mLogElements.erase(it);
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800426 if (doSetLast) {
427 log_id_for_each(i) {
428 if (setLast[i]) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700429 if (__predict_false(it == mLogElements.end())) {
Tom Cherry20118ee2020-05-04 10:17:42 -0700430 oldest_[i] = std::nullopt;
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800431 } else {
Tom Cherry20118ee2020-05-04 10:17:42 -0700432 oldest_[i] = it; // Store the next iterator even if it does not correspond to
Tom Cherry385c2c92020-04-29 17:58:18 -0700433 // the same log_id, as a starting point for GetOldest().
Mark Salyzyn7fd6c5c2016-01-19 16:04:41 -0800434 }
435 }
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800436 }
437 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700438#ifdef DEBUG_CHECK_FOR_STALE_ENTRIES
439 log_id_for_each(i) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800440 for (auto b : mLastWorst[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700441 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800442 android::prdebug("stale mLastWorst[%d] key=%d mykey=%d\n", i,
443 b.first, key);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700444 }
445 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800446 for (auto b : mLastWorstPidOfSystem[i]) {
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700447 if (bad == b.second) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800448 android::prdebug("stale mLastWorstPidOfSystem[%d] pid=%d\n", i,
449 b.first);
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700450 }
451 }
Mark Salyzyn60636fa2016-10-24 16:22:17 -0700452 }
453#endif
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700454 if (coalesce) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700455 stats.erase(element);
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700456 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700457 stats.subtract(element);
Mark Salyzyn831aa292015-09-03 16:08:50 -0700458 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700459 delete element;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700460
461 return it;
462}
463
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700464// Define a temporary mechanism to report the last LogBufferElement pointer
465// for the specified uid, pid and tid. Used below to help merge-sort when
466// pruning for worst UID.
467class LogBufferElementKey {
468 const union {
469 struct {
Mark Salyzyn684bdb52016-12-13 12:44:20 -0800470 uint32_t uid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700471 uint16_t pid;
472 uint16_t tid;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700473 } __packed;
474 uint64_t value;
475 } __packed;
476
Mark Salyzyn501c3732017-03-10 14:31:54 -0800477 public:
478 LogBufferElementKey(uid_t uid, pid_t pid, pid_t tid)
479 : uid(uid), pid(pid), tid(tid) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700480 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800481 explicit LogBufferElementKey(uint64_t key) : value(key) {
482 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700483
Mark Salyzyn501c3732017-03-10 14:31:54 -0800484 uint64_t getKey() {
485 return value;
486 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700487};
488
Mark Salyzyn511338d2015-05-19 09:12:30 -0700489class LogBufferElementLast {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800490 typedef std::unordered_map<uint64_t, LogBufferElement*> LogBufferElementMap;
Mark Salyzyn511338d2015-05-19 09:12:30 -0700491 LogBufferElementMap map;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700492
Mark Salyzyn501c3732017-03-10 14:31:54 -0800493 public:
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700494 bool coalesce(LogBufferElement* element, uint16_t dropped) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800495 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700496 element->getTid());
Mark Salyzyn511338d2015-05-19 09:12:30 -0700497 LogBufferElementMap::iterator it = map.find(key.getKey());
498 if (it != map.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800499 LogBufferElement* found = it->second;
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700500 uint16_t moreDropped = found->getDropped();
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700501 if ((dropped + moreDropped) > USHRT_MAX) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700502 map.erase(it);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700503 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700504 found->setDropped(dropped + moreDropped);
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700505 return true;
506 }
507 }
508 return false;
509 }
510
Mark Salyzyn501c3732017-03-10 14:31:54 -0800511 void add(LogBufferElement* element) {
512 LogBufferElementKey key(element->getUid(), element->getPid(),
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700513 element->getTid());
514 map[key.getKey()] = element;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700515 }
516
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700517 inline void clear() {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700518 map.clear();
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700519 }
520
Mark Salyzyn501c3732017-03-10 14:31:54 -0800521 void clear(LogBufferElement* element) {
Tom Cherry10d086e2019-08-21 14:16:34 -0700522 uint64_t current = element->getRealTime().nsec() - (EXPIRE_RATELIMIT * NS_PER_SEC);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800523 for (LogBufferElementMap::iterator it = map.begin(); it != map.end();) {
524 LogBufferElement* mapElement = it->second;
Tom Cherry10d086e2019-08-21 14:16:34 -0700525 if (mapElement->getDropped() >= EXPIRE_THRESHOLD &&
526 current > mapElement->getRealTime().nsec()) {
Mark Salyzyn511338d2015-05-19 09:12:30 -0700527 it = map.erase(it);
528 } else {
529 ++it;
Mark Salyzyne06a6e02015-04-20 14:08:56 -0700530 }
531 }
532 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700533};
534
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700535// If the selected reader is blocking our pruning progress, decide on
536// what kind of mitigation is necessary to unblock the situation.
Tom Cherry6ec71e92020-05-04 12:53:36 -0700537void LogBuffer::kickMe(LogReaderThread* me, log_id_t id, unsigned long pruneRows) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700538 if (stats.sizes(id) > (2 * log_buffer_size(id))) { // +100%
539 // A misbehaving or slow reader has its connection
540 // dropped if we hit too much memory pressure.
Tom Cherry21f16a02019-11-15 17:37:03 -0800541 android::prdebug("Kicking blocked reader, pid %d, from LogBuffer::kickMe()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700542 me->client()->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700543 me->release_Locked();
Tom Cherrycef47bb2020-05-04 17:10:16 -0700544 } else if (me->timeout().tv_sec || me->timeout().tv_nsec) {
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700545 // Allow a blocked WRAP timeout reader to
546 // trigger and start reporting the log data.
547 me->triggerReader_Locked();
548 } else {
549 // tell slow reader to skip entries to catch up
Tom Cherry21f16a02019-11-15 17:37:03 -0800550 android::prdebug(
551 "Skipping %lu entries from slow reader, pid %d, from LogBuffer::kickMe()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700552 pruneRows, me->client()->getPid());
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700553 me->triggerSkip_Locked(id, pruneRows);
554 }
555}
556
Mark Salyzyn0175b072014-02-26 09:50:16 -0800557// prune "pruneRows" of type "id" from the buffer.
558//
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700559// This garbage collection task is used to expire log entries. It is called to
560// remove all logs (clear), all UID logs (unprivileged clear), or every
561// 256 or 10% of the total logs (whichever is less) to prune the logs.
562//
563// First there is a prep phase where we discover the reader region lock that
564// acts as a backstop to any pruning activity to stop there and go no further.
565//
566// There are three major pruning loops that follow. All expire from the oldest
567// entries. Since there are multiple log buffers, the Android logging facility
568// will appear to drop entries 'in the middle' when looking at multiple log
569// sources and buffers. This effect is slightly more prominent when we prune
570// the worst offender by logging source. Thus the logs slowly loose content
571// and value as you move back in time. This is preferred since chatty sources
572// invariably move the logs value down faster as less chatty sources would be
573// expired in the noise.
574//
575// The first loop performs blacklisting and worst offender pruning. Falling
576// through when there are no notable worst offenders and have not hit the
577// region lock preventing further worst offender pruning. This loop also looks
578// after managing the chatty log entries and merging to help provide
579// statistical basis for blame. The chatty entries are not a notification of
580// how much logs you may have, but instead represent how much logs you would
581// have had in a virtual log buffer that is extended to cover all the in-memory
582// logs without loss. They last much longer than the represented pruned logs
583// since they get multiplied by the gains in the non-chatty log sources.
584//
585// The second loop get complicated because an algorithm of watermarks and
586// history is maintained to reduce the order and keep processing time
587// down to a minimum at scale. These algorithms can be costly in the face
588// of larger log buffers, or severly limited processing time granted to a
589// background task at lowest priority.
590//
591// This second loop does straight-up expiration from the end of the logs
592// (again, remember for the specified log buffer id) but does some whitelist
593// preservation. Thus whitelist is a Hail Mary low priority, blacklists and
594// spam filtration all take priority. This second loop also checks if a region
595// lock is causing us to buffer too much in the logs to help the reader(s),
596// and will tell the slowest reader thread to skip log entries, and if
597// persistent and hits a further threshold, kill the reader thread.
598//
599// The third thread is optional, and only gets hit if there was a whitelist
600// and more needs to be pruned against the backstop of the region lock.
601//
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700602// LogBuffer::wrlock() must be held when this function is called.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700603//
Mark Salyzync5dc9702015-09-16 15:34:00 -0700604bool LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700605 LogReaderThread* oldest = nullptr;
Mark Salyzync5dc9702015-09-16 15:34:00 -0700606 bool busy = false;
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700607 bool clearAll = pruneRows == ULONG_MAX;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800608
Tom Cherry6ec71e92020-05-04 12:53:36 -0700609 LogReaderThread::rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800610
611 // Region locked?
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700612 LastLogTimes::iterator times = mTimes.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800613 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700614 LogReaderThread* entry = times->get();
Tom Cherrycef47bb2020-05-04 17:10:16 -0700615 if (entry->IsWatching(id) && (!oldest || oldest->start() > entry->start() ||
616 (oldest->start() == entry->start() &&
617 (entry->timeout().tv_sec || entry->timeout().tv_nsec)))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800618 oldest = entry;
619 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700620 times++;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800621 }
622
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800623 LogBufferElementCollection::iterator it;
624
Mark Salyzyn501c3732017-03-10 14:31:54 -0800625 if (__predict_false(caller_uid != AID_ROOT)) { // unlikely
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700626 // Only here if clear all request from non system source, so chatty
627 // filter logistics is not required.
Tom Cherry385c2c92020-04-29 17:58:18 -0700628 it = GetOldest(id);
Mark Salyzyn507eb9f2016-01-11 10:58:09 -0800629 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800630 LogBufferElement* element = *it;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700631
Mark Salyzyn501c3732017-03-10 14:31:54 -0800632 if ((element->getLogId() != id) ||
633 (element->getUid() != caller_uid)) {
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700634 ++it;
635 continue;
636 }
637
Tom Cherrycef47bb2020-05-04 17:10:16 -0700638 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700639 busy = true;
640 kickMe(oldest, id, pruneRows);
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700641 break;
642 }
643
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700644 it = erase(it);
Mark Salyzyn43a5f312016-09-01 15:48:36 -0700645 if (--pruneRows == 0) {
646 break;
647 }
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700648 }
Tom Cherry6ec71e92020-05-04 12:53:36 -0700649 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700650 return busy;
Mark Salyzyn1a240b42014-06-12 11:16:16 -0700651 }
652
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700653 // prune by worst offenders; by blacklist, UID, and by PID of system UID
Tom Cherry5a3db392020-05-01 17:03:20 -0700654 bool hasBlacklist = (id != LOG_ID_SECURITY) && prune_->naughty();
Mark Salyzyn2b25c662015-09-16 15:34:00 -0700655 while (!clearAll && (pruneRows > 0)) {
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800656 // recalculate the worst offender on every batched pass
Mark Salyzyn501c3732017-03-10 14:31:54 -0800657 int worst = -1; // not valid for getUid() or getKey()
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800658 size_t worst_sizes = 0;
659 size_t second_worst_sizes = 0;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800660 pid_t worstPid = 0; // POSIX guarantees PID != 0
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800661
Tom Cherry5a3db392020-05-01 17:03:20 -0700662 if (worstUidEnabledForLogid(id) && prune_->worstUidEnabled()) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700663 // Calculate threshold as 12.5% of available storage
664 size_t threshold = log_buffer_size(id) / 8;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700665
Tom Cherryb6b78e92020-05-07 09:13:12 -0700666 if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) {
667 stats.WorstTwoTags(threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700668 // per-pid filter for AID_SYSTEM sources is too complex
Mark Salyzyn6a066942016-07-14 15:34:30 -0700669 } else {
Tom Cherryb6b78e92020-05-07 09:13:12 -0700670 stats.WorstTwoUids(id, threshold, &worst, &worst_sizes, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700671
Tom Cherryb6b78e92020-05-07 09:13:12 -0700672 if (worst == AID_SYSTEM && prune_->worstPidOfSystemEnabled()) {
673 stats.WorstTwoSystemPids(id, worst_sizes, &worstPid, &second_worst_sizes);
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700674 }
675 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800676 }
677
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700678 // skip if we have neither worst nor naughty filters
Mark Salyzyn6a066942016-07-14 15:34:30 -0700679 if ((worst == -1) && !hasBlacklist) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700680 break;
681 }
682
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800683 bool kick = false;
Tom Cherry385c2c92020-04-29 17:58:18 -0700684 bool leading = true; // true if starting from the oldest log entry, false if starting from
685 // a specific chatty entry.
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700686 // Perform at least one mandatory garbage collection cycle in following
687 // - clear leading chatty tags
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700688 // - coalesce chatty tags
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700689 // - check age-out of preserved logs
690 bool gc = pruneRows <= 1;
Mark Salyzyn6a066942016-07-14 15:34:30 -0700691 if (!gc && (worst != -1)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800692 { // begin scope for worst found iterator
693 LogBufferIteratorMap::iterator found =
694 mLastWorst[id].find(worst);
695 if ((found != mLastWorst[id].end()) &&
696 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700697 leading = false;
698 it = found->second;
699 }
700 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800701 if (worstPid) { // begin scope for pid worst found iterator
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700702 // FYI: worstPid only set if !LOG_ID_EVENTS and
703 // !LOG_ID_SECURITY, not going to make that assumption ...
Mark Salyzyn501c3732017-03-10 14:31:54 -0800704 LogBufferPidIteratorMap::iterator found =
705 mLastWorstPidOfSystem[id].find(worstPid);
706 if ((found != mLastWorstPidOfSystem[id].end()) &&
707 (found->second != mLogElements.end())) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700708 leading = false;
709 it = found->second;
710 }
Mark Salyzync892ea32015-08-19 17:06:11 -0700711 }
712 }
Tom Cherry385c2c92020-04-29 17:58:18 -0700713 if (leading) {
714 it = GetOldest(id);
715 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800716 static const timespec too_old = { EXPIRE_HOUR_THRESHOLD * 60 * 60, 0 };
Mark Salyzynccfe8442015-08-24 13:43:27 -0700717 LogBufferElementCollection::iterator lastt;
718 lastt = mLogElements.end();
719 --lastt;
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700720 LogBufferElementLast last;
Mark Salyzync892ea32015-08-19 17:06:11 -0700721 while (it != mLogElements.end()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800722 LogBufferElement* element = *it;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800723
Tom Cherrycef47bb2020-05-04 17:10:16 -0700724 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700725 busy = true;
Mark Salyzyn0878a7c2017-05-11 13:28:33 -0700726 // Do not let chatty eliding trigger any reader mitigation
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800727 break;
728 }
729
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700730 if (element->getLogId() != id) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800731 ++it;
732 continue;
733 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700734 // below this point element->getLogId() == id
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800735
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700736 uint16_t dropped = element->getDropped();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800737
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700738 // remove any leading drops
739 if (leading && dropped) {
740 it = erase(it);
741 continue;
742 }
743
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700744 if (dropped && last.coalesce(element, dropped)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700745 it = erase(it, true);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700746 continue;
747 }
748
Mark Salyzyn501c3732017-03-10 14:31:54 -0800749 int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY))
750 ? element->getTag()
751 : element->getUid();
Mark Salyzyn6a066942016-07-14 15:34:30 -0700752
Tom Cherry5a3db392020-05-01 17:03:20 -0700753 if (hasBlacklist && prune_->naughty(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700754 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700755 it = erase(it);
756 if (dropped) {
757 continue;
758 }
759
760 pruneRows--;
761 if (pruneRows == 0) {
762 break;
763 }
764
Mark Salyzyn6a066942016-07-14 15:34:30 -0700765 if (key == worst) {
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700766 kick = true;
767 if (worst_sizes < second_worst_sizes) {
768 break;
769 }
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700770 worst_sizes -= element->getMsgLen();
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700771 }
772 continue;
773 }
774
Mark Salyzyn501c3732017-03-10 14:31:54 -0800775 if ((element->getRealTime() < ((*lastt)->getRealTime() - too_old)) ||
776 (element->getRealTime() > (*lastt)->getRealTime())) {
Mark Salyzynccfe8442015-08-24 13:43:27 -0700777 break;
778 }
779
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700780 if (dropped) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700781 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800782 if (worstPid &&
783 ((!gc && (element->getPid() == worstPid)) ||
784 (mLastWorstPidOfSystem[id].find(element->getPid()) ==
785 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700786 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700787 // watermark if current one empty. id is not LOG_ID_EVENTS
788 // or LOG_ID_SECURITY because of worstPid check.
Mark Salyzyn1eefca22016-09-01 07:28:44 -0700789 mLastWorstPidOfSystem[id][element->getPid()] = it;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700790 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800791 if ((!gc && !worstPid && (key == worst)) ||
792 (mLastWorst[id].find(key) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700793 mLastWorst[id][key] = it;
Mark Salyzyn49afe0d2015-08-24 13:43:27 -0700794 }
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800795 ++it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700796 continue;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800797 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700798
Mark Salyzyn501c3732017-03-10 14:31:54 -0800799 if ((key != worst) ||
800 (worstPid && (element->getPid() != worstPid))) {
Mark Salyzyn59212762015-06-01 09:41:19 -0700801 leading = false;
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700802 last.clear(element);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700803 ++it;
804 continue;
805 }
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700806 // key == worst below here
807 // If worstPid set, then element->getPid() == worstPid below here
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700808
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700809 pruneRows--;
810 if (pruneRows == 0) {
811 break;
812 }
813
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700814 kick = true;
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700815
Chih-Hung Hsieh08d470b2018-08-13 14:22:56 -0700816 uint16_t len = element->getMsgLen();
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700817
818 // do not create any leading drops
819 if (leading) {
820 it = erase(it);
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700821 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700822 stats.drop(element);
823 element->setDropped(1);
824 if (last.coalesce(element, 1)) {
Mark Salyzynaaad42f2015-09-30 07:40:09 -0700825 it = erase(it, true);
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700826 } else {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700827 last.add(element);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800828 if (worstPid &&
829 (!gc || (mLastWorstPidOfSystem[id].find(worstPid) ==
830 mLastWorstPidOfSystem[id].end()))) {
Mark Salyzynfa07f9d2016-10-21 09:46:42 -0700831 // element->getUid() may not be AID_SYSTEM, next best
Mark Salyzyn8fcfd852016-10-24 08:20:26 -0700832 // watermark if current one empty. id is not
833 // LOG_ID_EVENTS or LOG_ID_SECURITY because of worstPid.
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700834 mLastWorstPidOfSystem[id][worstPid] = it;
835 }
Mark Salyzyn6a066942016-07-14 15:34:30 -0700836 if ((!gc && !worstPid) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -0800837 (mLastWorst[id].find(worst) == mLastWorst[id].end())) {
Mark Salyzyn6a066942016-07-14 15:34:30 -0700838 mLastWorst[id][worst] = it;
Mark Salyzyn5bb29722015-09-08 09:12:51 -0700839 }
Mark Salyzyn5392aac2015-05-22 10:03:31 -0700840 ++it;
841 }
Mark Salyzynab0dcf62015-03-16 12:04:09 -0700842 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700843 if (worst_sizes < second_worst_sizes) {
844 break;
845 }
846 worst_sizes -= len;
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800847 }
Mark Salyzyn2c9d9092015-04-17 15:38:04 -0700848 last.clear();
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800849
Tom Cherry5a3db392020-05-01 17:03:20 -0700850 if (!kick || !prune_->worstUidEnabled()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800851 break; // the following loop will ask bad clients to skip/drop
Mark Salyzyn64d6fe92014-02-06 18:11:13 -0800852 }
853 }
854
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800855 bool whitelist = false;
Tom Cherry5a3db392020-05-01 17:03:20 -0700856 bool hasWhitelist = (id != LOG_ID_SECURITY) && prune_->nice() && !clearAll;
Tom Cherry385c2c92020-04-29 17:58:18 -0700857 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800858 while ((pruneRows > 0) && (it != mLogElements.end())) {
859 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700860
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700861 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700862 it++;
863 continue;
864 }
865
Tom Cherrycef47bb2020-05-04 17:10:16 -0700866 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700867 busy = true;
868 if (!whitelist) kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700869 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800870 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700871
Tom Cherry5a3db392020-05-01 17:03:20 -0700872 if (hasWhitelist && !element->getDropped() && prune_->nice(element)) {
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700873 // WhiteListed
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700874 whitelist = true;
875 it++;
876 continue;
877 }
878
879 it = erase(it);
880 pruneRows--;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800881 }
882
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700883 // Do not save the whitelist if we are reader range limited
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800884 if (whitelist && (pruneRows > 0)) {
Tom Cherry385c2c92020-04-29 17:58:18 -0700885 it = GetOldest(id);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800886 while ((it != mLogElements.end()) && (pruneRows > 0)) {
887 LogBufferElement* element = *it;
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700888
Mark Salyzynbec3c3d2015-08-28 08:02:59 -0700889 if (element->getLogId() != id) {
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700890 ++it;
891 continue;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800892 }
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700893
Tom Cherrycef47bb2020-05-04 17:10:16 -0700894 if (oldest && oldest->start() <= element->getSequence()) {
Tom Cherry5e266552020-04-08 10:47:26 -0700895 busy = true;
896 kickMe(oldest, id, pruneRows);
Mark Salyzyn97c1c2b2015-03-10 13:51:35 -0700897 break;
898 }
899
900 it = erase(it);
901 pruneRows--;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800902 }
903 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800904
Tom Cherry6ec71e92020-05-04 12:53:36 -0700905 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700906
907 return (pruneRows > 0) && busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800908}
909
910// clear all rows of type "id" from the buffer.
Mark Salyzync5dc9702015-09-16 15:34:00 -0700911bool LogBuffer::clear(log_id_t id, uid_t uid) {
912 bool busy = true;
913 // If it takes more than 4 tries (seconds) to clear, then kill reader(s)
914 for (int retry = 4;;) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800915 if (retry == 1) { // last pass
Mark Salyzync5dc9702015-09-16 15:34:00 -0700916 // Check if it is still busy after the sleep, we say prune
917 // one entry, not another clear run, so we are looking for
918 // the quick side effect of the return value to tell us if
919 // we have a _blocked_ reader.
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700920 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700921 busy = prune(id, 1, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700922 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700923 // It is still busy, blocked reader(s), lets kill them all!
924 // otherwise, lets be a good citizen and preserve the slow
925 // readers and let the clear run (below) deal with determining
926 // if we are still blocked and return an error code to caller.
927 if (busy) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700928 LogReaderThread::wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700929 LastLogTimes::iterator times = mTimes.begin();
930 while (times != mTimes.end()) {
Tom Cherry6ec71e92020-05-04 12:53:36 -0700931 LogReaderThread* entry = times->get();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700932 // Killer punch
Tom Cherrycef47bb2020-05-04 17:10:16 -0700933 if (entry->IsWatching(id)) {
Tom Cherry21f16a02019-11-15 17:37:03 -0800934 android::prdebug(
935 "Kicking blocked reader, pid %d, from LogBuffer::clear()\n",
Tom Cherrycef47bb2020-05-04 17:10:16 -0700936 entry->client()->getPid());
Mark Salyzync5dc9702015-09-16 15:34:00 -0700937 entry->release_Locked();
938 }
939 times++;
940 }
Tom Cherry6ec71e92020-05-04 12:53:36 -0700941 LogReaderThread::unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700942 }
943 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700944 wrlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700945 busy = prune(id, ULONG_MAX, uid);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700946 unlock();
Mark Salyzync5dc9702015-09-16 15:34:00 -0700947 if (!busy || !--retry) {
948 break;
949 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800950 sleep(1); // Let reader(s) catch up after notification
Mark Salyzync5dc9702015-09-16 15:34:00 -0700951 }
952 return busy;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800953}
954
955// get the used space associated with "id".
956unsigned long LogBuffer::getSizeUsed(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700957 rdlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -0800958 size_t retval = stats.sizes(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700959 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800960 return retval;
961}
962
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800963// set the total space allocated to "id"
964int LogBuffer::setSize(log_id_t id, unsigned long size) {
965 // Reasonable limits ...
Mark Salyzynf10e2732016-09-27 13:08:23 -0700966 if (!__android_logger_valid_buffer_size(size)) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800967 return -1;
968 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700969 wrlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800970 log_buffer_size(id) = size;
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700971 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800972 return 0;
973}
974
975// get the total space allocated to "id"
976unsigned long LogBuffer::getSize(log_id_t id) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700977 rdlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800978 size_t retval = log_buffer_size(id);
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700979 unlock();
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800980 return retval;
981}
982
Tom Cherry10d086e2019-08-21 14:16:34 -0700983uint64_t LogBuffer::flushTo(SocketClient* reader, uint64_t start, pid_t* lastTid, bool privileged,
984 bool security,
Tom Cherry320f5962020-05-04 17:25:34 -0700985 const std::function<int(const LogBufferElement* element)>& filter) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800986 LogBufferElementCollection::iterator it;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800987 uid_t uid = reader->getUid();
988
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700989 rdlock();
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600990
Tom Cherry10d086e2019-08-21 14:16:34 -0700991 if (start <= 1) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600992 // client wants to start from the beginning
993 it = mLogElements.begin();
994 } else {
995 // Client wants to start from some specified time. Chances are
996 // we are better off starting from the end of the time sorted list.
Tom Cherry10d086e2019-08-21 14:16:34 -0700997 for (it = mLogElements.end(); it != mLogElements.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -0800998 /* do nothing */) {
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -0600999 --it;
Mark Salyzyn501c3732017-03-10 14:31:54 -08001000 LogBufferElement* element = *it;
Tom Cherry10d086e2019-08-21 14:16:34 -07001001 if (element->getSequence() <= start) {
1002 it++;
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001003 break;
1004 }
1005 }
1006 }
1007
Tom Cherry10d086e2019-08-21 14:16:34 -07001008 uint64_t curr = start;
Mark Salyzynb5b87962017-01-23 14:20:31 -08001009
Dragoslav Mitrinovic8e8e8db2015-01-15 09:29:43 -06001010 for (; it != mLogElements.end(); ++it) {
Mark Salyzyn501c3732017-03-10 14:31:54 -08001011 LogBufferElement* element = *it;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001012
1013 if (!privileged && (element->getUid() != uid)) {
1014 continue;
1015 }
1016
Mark Salyzyn8fa88962016-01-26 14:32:35 -08001017 if (!security && (element->getLogId() == LOG_ID_SECURITY)) {
1018 continue;
1019 }
1020
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001021 // NB: calling out to another object with wrlock() held (safe)
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001022 if (filter) {
Tom Cherry320f5962020-05-04 17:25:34 -07001023 int ret = filter(element);
Mark Salyzynf7c0f752015-03-03 13:39:37 -08001024 if (ret == false) {
1025 continue;
1026 }
1027 if (ret != true) {
1028 break;
1029 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001030 }
1031
Mark Salyzynae2abf12017-03-31 10:48:39 -07001032 bool sameTid = false;
1033 if (lastTid) {
1034 sameTid = lastTid[element->getLogId()] == element->getTid();
1035 // Dropped (chatty) immediately following a valid log from the
1036 // same source in the same log buffer indicates we have a
1037 // multiple identical squash. chatty that differs source
1038 // is due to spam filter. chatty to chatty of different
1039 // source is also due to spam filter.
1040 lastTid[element->getLogId()] =
1041 (element->getDropped() && !sameTid) ? 0 : element->getTid();
1042 }
Mark Salyzynb5b87962017-01-23 14:20:31 -08001043
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001044 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001045
1046 // range locking in LastLogTimes looks after us
Tom Cherry64458c72019-10-15 15:10:26 -07001047 curr = element->flushTo(reader, this, sameTid);
Mark Salyzyn0175b072014-02-26 09:50:16 -08001048
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001049 if (curr == element->FLUSH_ERROR) {
1050 return curr;
Mark Salyzyneb45db22017-05-17 19:55:12 +00001051 }
Mark Salyzyn0175b072014-02-26 09:50:16 -08001052
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001053 rdlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001054 }
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001055 unlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -08001056
Mark Salyzyn206ed8e2017-05-18 10:06:00 -07001057 return curr;
Mark Salyzyn0175b072014-02-26 09:50:16 -08001058}
Mark Salyzyn34facab2014-02-06 14:48:50 -08001059
Mark Salyzynee3b8382015-12-17 09:58:43 -08001060std::string LogBuffer::formatStatistics(uid_t uid, pid_t pid,
1061 unsigned int logMask) {
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001062 wrlock();
Mark Salyzyn34facab2014-02-06 14:48:50 -08001063
Mark Salyzynee3b8382015-12-17 09:58:43 -08001064 std::string ret = stats.format(uid, pid, logMask);
Mark Salyzyn34facab2014-02-06 14:48:50 -08001065
Mark Salyzyn3c501b52017-04-18 14:09:45 -07001066 unlock();
Mark Salyzyn73160ac2015-08-20 10:01:44 -07001067
1068 return ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001069}