1 /*
2 * Copyright (C) 2020 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
17 #include "SimpleLogBuffer.h"
18
19 #include <android-base/logging.h>
20
21 #include "LogBufferElement.h"
22 #include "LogSize.h"
23
SimpleLogBuffer(LogReaderList * reader_list,LogTags * tags,LogStatistics * stats)24 SimpleLogBuffer::SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats)
25 : reader_list_(reader_list), tags_(tags), stats_(stats) {
26 Init();
27 }
28
~SimpleLogBuffer()29 SimpleLogBuffer::~SimpleLogBuffer() {}
30
Init()31 void SimpleLogBuffer::Init() {
32 log_id_for_each(i) {
33 if (!SetSize(i, GetBufferSizeFromProperties(i))) {
34 SetSize(i, kLogBufferMinSize);
35 }
36 }
37
38 // Release any sleeping reader threads to dump their current content.
39 auto lock = std::lock_guard{logd_lock};
40 for (const auto& reader_thread : reader_list_->reader_threads()) {
41 reader_thread->TriggerReader();
42 }
43 }
44
GetOldest(log_id_t log_id)45 std::list<LogBufferElement>::iterator SimpleLogBuffer::GetOldest(log_id_t log_id) {
46 auto it = logs().begin();
47 if (oldest_[log_id]) {
48 it = *oldest_[log_id];
49 }
50 while (it != logs().end() && it->log_id() != log_id) {
51 it++;
52 }
53 if (it != logs().end()) {
54 oldest_[log_id] = it;
55 }
56 return it;
57 }
58
ShouldLog(log_id_t log_id,const char * msg,uint16_t len)59 bool SimpleLogBuffer::ShouldLog(log_id_t log_id, const char* msg, uint16_t len) {
60 if (log_id == LOG_ID_SECURITY) {
61 return true;
62 }
63
64 int prio = ANDROID_LOG_INFO;
65 const char* tag = nullptr;
66 size_t tag_len = 0;
67 if (IsBinary(log_id)) {
68 int32_t numeric_tag = MsgToTag(msg, len);
69 tag = tags_->tagToName(numeric_tag);
70 if (tag) {
71 tag_len = strlen(tag);
72 }
73 } else {
74 prio = *msg;
75 tag = msg + 1;
76 tag_len = strnlen(tag, len - 1);
77 }
78 return __android_log_is_loggable_len(prio, tag, tag_len, ANDROID_LOG_VERBOSE);
79 }
80
Log(log_id_t log_id,log_time realtime,uid_t uid,pid_t pid,pid_t tid,const char * msg,uint16_t len)81 int SimpleLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
82 const char* msg, uint16_t len) {
83 if (log_id >= LOG_ID_MAX) {
84 return -EINVAL;
85 }
86
87 if (!ShouldLog(log_id, msg, len)) {
88 // Log traffic received to total
89 stats_->AddTotal(log_id, len);
90 return -EACCES;
91 }
92
93 // Slip the time by 1 nsec if the incoming lands on xxxxxx000 ns.
94 // This prevents any chance that an outside source can request an
95 // exact entry with time specified in ms or us precision.
96 if ((realtime.tv_nsec % 1000) == 0) ++realtime.tv_nsec;
97
98 auto lock = std::lock_guard{logd_lock};
99 auto sequence = sequence_.fetch_add(1, std::memory_order_relaxed);
100 LogInternal(LogBufferElement(log_id, realtime, uid, pid, tid, sequence, msg, len));
101 return len;
102 }
103
LogInternal(LogBufferElement && elem)104 void SimpleLogBuffer::LogInternal(LogBufferElement&& elem) {
105 log_id_t log_id = elem.log_id();
106
107 logs_.emplace_back(std::move(elem));
108 stats_->Add(logs_.back().ToLogStatisticsElement());
109 MaybePrune(log_id);
110 reader_list_->NotifyNewLog(1 << log_id);
111 }
112
113 // These extra parameters are only required for chatty, but since they're a no-op for
114 // SimpleLogBuffer, it's easier to include them here, then to duplicate FlushTo() for
115 // ChattyLogBuffer.
116 class ChattyFlushToState : public FlushToState {
117 public:
ChattyFlushToState(uint64_t start,LogMask log_mask)118 ChattyFlushToState(uint64_t start, LogMask log_mask) : FlushToState(start, log_mask) {}
119
last_tid()120 pid_t* last_tid() { return last_tid_; }
121
drop_chatty_messages() const122 bool drop_chatty_messages() const { return drop_chatty_messages_; }
set_drop_chatty_messages(bool value)123 void set_drop_chatty_messages(bool value) { drop_chatty_messages_ = value; }
124
125 private:
126 pid_t last_tid_[LOG_ID_MAX] = {};
127 bool drop_chatty_messages_ = true;
128 };
129
CreateFlushToState(uint64_t start,LogMask log_mask)130 std::unique_ptr<FlushToState> SimpleLogBuffer::CreateFlushToState(uint64_t start,
131 LogMask log_mask) {
132 return std::make_unique<ChattyFlushToState>(start, log_mask);
133 }
134
FlushTo(LogWriter * writer,FlushToState & abstract_state,const std::function<FilterResult (log_id_t log_id,pid_t pid,uint64_t sequence,log_time realtime)> & filter)135 bool SimpleLogBuffer::FlushTo(
136 LogWriter* writer, FlushToState& abstract_state,
137 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
138 log_time realtime)>& filter) {
139 auto& state = reinterpret_cast<ChattyFlushToState&>(abstract_state);
140
141 std::list<LogBufferElement>::iterator it;
142 if (state.start() <= 1) {
143 // client wants to start from the beginning
144 it = logs_.begin();
145 } else {
146 // Client wants to start from some specified time. Chances are
147 // we are better off starting from the end of the time sorted list.
148 for (it = logs_.end(); it != logs_.begin();
149 /* do nothing */) {
150 --it;
151 if (it->sequence() == state.start()) {
152 break;
153 } else if (it->sequence() < state.start()) {
154 it++;
155 break;
156 }
157 }
158 }
159
160 for (; it != logs_.end(); ++it) {
161 LogBufferElement& element = *it;
162
163 state.set_start(element.sequence());
164
165 if (!writer->privileged() && element.uid() != writer->uid()) {
166 continue;
167 }
168
169 if (((1 << element.log_id()) & state.log_mask()) == 0) {
170 continue;
171 }
172
173 if (filter) {
174 FilterResult ret =
175 filter(element.log_id(), element.pid(), element.sequence(), element.realtime());
176 if (ret == FilterResult::kSkip) {
177 continue;
178 }
179 if (ret == FilterResult::kStop) {
180 break;
181 }
182 }
183
184 // drop_chatty_messages is initialized to true, so if the first message that we attempt to
185 // flush is a chatty message, we drop it. Once we see a non-chatty message it gets set to
186 // false to let further chatty messages be printed.
187 if (state.drop_chatty_messages()) {
188 if (element.dropped_count() != 0) {
189 continue;
190 }
191 state.set_drop_chatty_messages(false);
192 }
193
194 bool same_tid = state.last_tid()[element.log_id()] == element.tid();
195 // Dropped (chatty) immediately following a valid log from the same source in the same log
196 // buffer indicates we have a multiple identical squash. chatty that differs source is due
197 // to spam filter. chatty to chatty of different source is also due to spam filter.
198 state.last_tid()[element.log_id()] =
199 (element.dropped_count() && !same_tid) ? 0 : element.tid();
200
201 logd_lock.unlock();
202 // We never prune logs equal to or newer than any LogReaderThreads' `start` value, so the
203 // `element` pointer is safe here without the lock
204 if (!element.FlushTo(writer, stats_, same_tid)) {
205 logd_lock.lock();
206 return false;
207 }
208 logd_lock.lock();
209 }
210
211 state.set_start(state.start() + 1);
212 return true;
213 }
214
Clear(log_id_t id,uid_t uid)215 bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) {
216 // Try three times to clear, then disconnect the readers and try one final time.
217 for (int retry = 0; retry < 3; ++retry) {
218 {
219 auto lock = std::lock_guard{logd_lock};
220 if (Prune(id, ULONG_MAX, uid)) {
221 return true;
222 }
223 }
224 sleep(1);
225 }
226 // Check if it is still busy after the sleep, we try to prune one entry, not another clear run,
227 // so we are looking for the quick side effect of the return value to tell us if we have a
228 // _blocked_ reader.
229 bool busy = false;
230 {
231 auto lock = std::lock_guard{logd_lock};
232 busy = !Prune(id, 1, uid);
233 }
234 // It is still busy, disconnect all readers.
235 if (busy) {
236 auto lock = std::lock_guard{logd_lock};
237 for (const auto& reader_thread : reader_list_->reader_threads()) {
238 if (reader_thread->IsWatching(id)) {
239 LOG(WARNING) << "Kicking blocked reader, " << reader_thread->name()
240 << ", from LogBuffer::clear()";
241 reader_thread->Release();
242 }
243 }
244 }
245 auto lock = std::lock_guard{logd_lock};
246 return Prune(id, ULONG_MAX, uid);
247 }
248
249 // get the total space allocated to "id"
GetSize(log_id_t id)250 size_t SimpleLogBuffer::GetSize(log_id_t id) {
251 auto lock = std::lock_guard{logd_lock};
252 size_t retval = max_size_[id];
253 return retval;
254 }
255
256 // set the total space allocated to "id"
SetSize(log_id_t id,size_t size)257 bool SimpleLogBuffer::SetSize(log_id_t id, size_t size) {
258 // Reasonable limits ...
259 if (!IsValidBufferSize(size)) {
260 return false;
261 }
262
263 auto lock = std::lock_guard{logd_lock};
264 max_size_[id] = size;
265 return true;
266 }
267
MaybePrune(log_id_t id)268 void SimpleLogBuffer::MaybePrune(log_id_t id) {
269 unsigned long prune_rows;
270 if (stats_->ShouldPrune(id, max_size_[id], &prune_rows)) {
271 Prune(id, prune_rows, 0);
272 }
273 }
274
Prune(log_id_t id,unsigned long prune_rows,uid_t caller_uid)275 bool SimpleLogBuffer::Prune(log_id_t id, unsigned long prune_rows, uid_t caller_uid) {
276 // Don't prune logs that are newer than the point at which any reader threads are reading from.
277 LogReaderThread* oldest = nullptr;
278 for (const auto& reader_thread : reader_list_->reader_threads()) {
279 if (!reader_thread->IsWatching(id)) {
280 continue;
281 }
282 if (!oldest || oldest->start() > reader_thread->start() ||
283 (oldest->start() == reader_thread->start() &&
284 reader_thread->deadline().time_since_epoch().count() != 0)) {
285 oldest = reader_thread.get();
286 }
287 }
288
289 auto it = GetOldest(id);
290
291 while (it != logs_.end()) {
292 LogBufferElement& element = *it;
293
294 if (element.log_id() != id) {
295 ++it;
296 continue;
297 }
298
299 if (caller_uid != 0 && element.uid() != caller_uid) {
300 ++it;
301 continue;
302 }
303
304 if (oldest && oldest->start() <= element.sequence()) {
305 KickReader(oldest, id, prune_rows);
306 return false;
307 }
308
309 stats_->Subtract(element.ToLogStatisticsElement());
310 it = Erase(it);
311 if (--prune_rows == 0) {
312 return true;
313 }
314 }
315 return true;
316 }
317
Erase(std::list<LogBufferElement>::iterator it)318 std::list<LogBufferElement>::iterator SimpleLogBuffer::Erase(
319 std::list<LogBufferElement>::iterator it) {
320 bool oldest_is_it[LOG_ID_MAX];
321 log_id_for_each(i) { oldest_is_it[i] = oldest_[i] && it == *oldest_[i]; }
322
323 it = logs_.erase(it);
324
325 log_id_for_each(i) {
326 if (oldest_is_it[i]) {
327 if (__predict_false(it == logs().end())) {
328 oldest_[i] = std::nullopt;
329 } else {
330 oldest_[i] = it; // Store the next iterator even if it does not correspond to
331 // the same log_id, as a starting point for GetOldest().
332 }
333 }
334 }
335
336 return it;
337 }
338
339 // If the selected reader is blocking our pruning progress, decide on
340 // what kind of mitigation is necessary to unblock the situation.
KickReader(LogReaderThread * reader,log_id_t id,unsigned long prune_rows)341 void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) {
342 if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100%
343 // A misbehaving or slow reader has its connection
344 // dropped if we hit too much memory pressure.
345 LOG(WARNING) << "Kicking blocked reader, " << reader->name()
346 << ", from LogBuffer::kickMe()";
347 reader->Release();
348 } else if (reader->deadline().time_since_epoch().count() != 0) {
349 // Allow a blocked WRAP deadline reader to trigger and start reporting the log data.
350 reader->TriggerReader();
351 } else {
352 // tell slow reader to skip entries to catch up
353 LOG(WARNING) << "Skipping " << prune_rows << " entries from slow reader, " << reader->name()
354 << ", from LogBuffer::kickMe()";
355 reader->TriggerSkip(id, prune_rows);
356 }
357 }
358