Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 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 "LogBufferElement.h" |
| 20 | |
| 21 | SimpleLogBuffer::SimpleLogBuffer(LogReaderList* reader_list, LogTags* tags, LogStatistics* stats) |
| 22 | : reader_list_(reader_list), tags_(tags), stats_(stats) { |
| 23 | Init(); |
| 24 | } |
| 25 | |
| 26 | SimpleLogBuffer::~SimpleLogBuffer() {} |
| 27 | |
| 28 | void SimpleLogBuffer::Init() { |
| 29 | log_id_for_each(i) { |
| 30 | if (SetSize(i, __android_logger_get_buffer_size(i))) { |
| 31 | SetSize(i, LOG_BUFFER_MIN_SIZE); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Release any sleeping reader threads to dump their current content. |
| 36 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 37 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 38 | reader_thread->triggerReader_Locked(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | std::list<LogBufferElement>::iterator SimpleLogBuffer::GetOldest(log_id_t log_id) { |
| 43 | auto it = logs().begin(); |
| 44 | if (oldest_[log_id]) { |
| 45 | it = *oldest_[log_id]; |
| 46 | } |
| 47 | while (it != logs().end() && it->getLogId() != log_id) { |
| 48 | it++; |
| 49 | } |
| 50 | if (it != logs().end()) { |
| 51 | oldest_[log_id] = it; |
| 52 | } |
| 53 | return it; |
| 54 | } |
| 55 | |
| 56 | bool SimpleLogBuffer::ShouldLog(log_id_t log_id, const char* msg, uint16_t len) { |
| 57 | if (log_id == LOG_ID_SECURITY) { |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | int prio = ANDROID_LOG_INFO; |
| 62 | const char* tag = nullptr; |
| 63 | size_t tag_len = 0; |
| 64 | if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) { |
| 65 | if (len < sizeof(android_event_header_t)) { |
| 66 | return false; |
| 67 | } |
| 68 | int32_t numeric_tag = reinterpret_cast<const android_event_header_t*>(msg)->tag; |
| 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 | |
| 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{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 | |
| 104 | void SimpleLogBuffer::LogInternal(LogBufferElement&& elem) { |
| 105 | log_id_t log_id = elem.getLogId(); |
| 106 | |
| 107 | logs_.emplace_back(std::move(elem)); |
| 108 | stats_->Add(&logs_.back()); |
| 109 | MaybePrune(log_id); |
| 110 | reader_list_->NotifyNewLog(1 << log_id); |
| 111 | } |
| 112 | |
| 113 | uint64_t SimpleLogBuffer::FlushTo( |
| 114 | LogWriter* writer, uint64_t start, pid_t* last_tid, |
Tom Cherry | 70fadea | 2020-05-27 14:43:19 -0700 | [diff] [blame^] | 115 | const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence, |
| 116 | log_time realtime, uint16_t dropped_count)>& filter) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 117 | auto shared_lock = SharedLock{lock_}; |
| 118 | |
| 119 | std::list<LogBufferElement>::iterator it; |
| 120 | if (start <= 1) { |
| 121 | // client wants to start from the beginning |
| 122 | it = logs_.begin(); |
| 123 | } else { |
| 124 | // Client wants to start from some specified time. Chances are |
| 125 | // we are better off starting from the end of the time sorted list. |
| 126 | for (it = logs_.end(); it != logs_.begin(); |
| 127 | /* do nothing */) { |
| 128 | --it; |
| 129 | if (it->getSequence() <= start) { |
| 130 | it++; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | uint64_t curr = start; |
| 137 | |
| 138 | for (; it != logs_.end(); ++it) { |
| 139 | LogBufferElement& element = *it; |
| 140 | |
| 141 | if (!writer->privileged() && element.getUid() != writer->uid()) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | if (!writer->can_read_security_logs() && element.getLogId() == LOG_ID_SECURITY) { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | if (filter) { |
Tom Cherry | 70fadea | 2020-05-27 14:43:19 -0700 | [diff] [blame^] | 150 | FilterResult ret = filter(element.getLogId(), element.getPid(), element.getSequence(), |
| 151 | element.getRealTime(), element.getDropped()); |
Tom Cherry | 3e61a13 | 2020-05-27 10:46:37 -0700 | [diff] [blame] | 152 | if (ret == FilterResult::kSkip) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 153 | continue; |
| 154 | } |
Tom Cherry | 3e61a13 | 2020-05-27 10:46:37 -0700 | [diff] [blame] | 155 | if (ret == FilterResult::kStop) { |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | bool same_tid = false; |
| 161 | if (last_tid) { |
| 162 | same_tid = last_tid[element.getLogId()] == element.getTid(); |
| 163 | // Dropped (chatty) immediately following a valid log from the |
| 164 | // same source in the same log buffer indicates we have a |
| 165 | // multiple identical squash. chatty that differs source |
| 166 | // is due to spam filter. chatty to chatty of different |
| 167 | // source is also due to spam filter. |
| 168 | last_tid[element.getLogId()] = |
| 169 | (element.getDropped() && !same_tid) ? 0 : element.getTid(); |
| 170 | } |
| 171 | |
| 172 | shared_lock.unlock(); |
| 173 | |
| 174 | // We never prune logs equal to or newer than any LogReaderThreads' `start` value, so the |
| 175 | // `element` pointer is safe here without the lock |
| 176 | curr = element.getSequence(); |
| 177 | if (!element.FlushTo(writer, stats_, same_tid)) { |
| 178 | return FLUSH_ERROR; |
| 179 | } |
| 180 | |
| 181 | shared_lock.lock_shared(); |
| 182 | } |
| 183 | |
| 184 | return curr; |
| 185 | } |
| 186 | |
| 187 | // clear all rows of type "id" from the buffer. |
| 188 | bool SimpleLogBuffer::Clear(log_id_t id, uid_t uid) { |
| 189 | bool busy = true; |
| 190 | // If it takes more than 4 tries (seconds) to clear, then kill reader(s) |
| 191 | for (int retry = 4;;) { |
| 192 | if (retry == 1) { // last pass |
| 193 | // Check if it is still busy after the sleep, we say prune |
| 194 | // one entry, not another clear run, so we are looking for |
| 195 | // the quick side effect of the return value to tell us if |
| 196 | // we have a _blocked_ reader. |
| 197 | { |
| 198 | auto lock = std::lock_guard{lock_}; |
| 199 | busy = Prune(id, 1, uid); |
| 200 | } |
| 201 | // It is still busy, blocked reader(s), lets kill them all! |
| 202 | // otherwise, lets be a good citizen and preserve the slow |
| 203 | // readers and let the clear run (below) deal with determining |
| 204 | // if we are still blocked and return an error code to caller. |
| 205 | if (busy) { |
| 206 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 207 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 208 | if (reader_thread->IsWatching(id)) { |
| 209 | android::prdebug("Kicking blocked reader, %s, from LogBuffer::clear()\n", |
| 210 | reader_thread->name().c_str()); |
| 211 | reader_thread->release_Locked(); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | { |
| 217 | auto lock = std::lock_guard{lock_}; |
| 218 | busy = Prune(id, ULONG_MAX, uid); |
| 219 | } |
| 220 | |
| 221 | if (!busy || !--retry) { |
| 222 | break; |
| 223 | } |
| 224 | sleep(1); // Let reader(s) catch up after notification |
| 225 | } |
| 226 | return busy; |
| 227 | } |
| 228 | |
| 229 | // get the total space allocated to "id" |
| 230 | unsigned long SimpleLogBuffer::GetSize(log_id_t id) { |
| 231 | auto lock = SharedLock{lock_}; |
| 232 | size_t retval = max_size_[id]; |
| 233 | return retval; |
| 234 | } |
| 235 | |
| 236 | // set the total space allocated to "id" |
| 237 | int SimpleLogBuffer::SetSize(log_id_t id, unsigned long size) { |
| 238 | // Reasonable limits ... |
| 239 | if (!__android_logger_valid_buffer_size(size)) { |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | auto lock = std::lock_guard{lock_}; |
| 244 | max_size_[id] = size; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | void SimpleLogBuffer::MaybePrune(log_id_t id) { |
| 249 | unsigned long prune_rows; |
| 250 | if (stats_->ShouldPrune(id, max_size_[id], &prune_rows)) { |
| 251 | Prune(id, prune_rows, 0); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | bool SimpleLogBuffer::Prune(log_id_t id, unsigned long prune_rows, uid_t caller_uid) { |
| 256 | auto reader_threads_lock = std::lock_guard{reader_list_->reader_threads_lock()}; |
| 257 | |
| 258 | // Don't prune logs that are newer than the point at which any reader threads are reading from. |
| 259 | LogReaderThread* oldest = nullptr; |
| 260 | for (const auto& reader_thread : reader_list_->reader_threads()) { |
| 261 | if (!reader_thread->IsWatching(id)) { |
| 262 | continue; |
| 263 | } |
| 264 | if (!oldest || oldest->start() > reader_thread->start() || |
| 265 | (oldest->start() == reader_thread->start() && |
| 266 | reader_thread->deadline().time_since_epoch().count() != 0)) { |
| 267 | oldest = reader_thread.get(); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | auto it = GetOldest(id); |
| 272 | |
| 273 | while (it != logs_.end()) { |
| 274 | LogBufferElement& element = *it; |
| 275 | |
| 276 | if (element.getLogId() != id) { |
| 277 | ++it; |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | if (caller_uid != 0 && element.getUid() != caller_uid) { |
| 282 | ++it; |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | if (oldest && oldest->start() <= element.getSequence()) { |
| 287 | KickReader(oldest, id, prune_rows); |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | stats_->Subtract(&element); |
| 292 | it = Erase(it); |
| 293 | if (--prune_rows == 0) { |
| 294 | return false; |
| 295 | } |
| 296 | } |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | std::list<LogBufferElement>::iterator SimpleLogBuffer::Erase( |
| 301 | std::list<LogBufferElement>::iterator it) { |
| 302 | bool oldest_is_it[LOG_ID_MAX]; |
| 303 | log_id_for_each(i) { oldest_is_it[i] = oldest_[i] && it == *oldest_[i]; } |
| 304 | |
| 305 | it = logs_.erase(it); |
| 306 | |
| 307 | log_id_for_each(i) { |
| 308 | if (oldest_is_it[i]) { |
| 309 | if (__predict_false(it == logs().end())) { |
| 310 | oldest_[i] = std::nullopt; |
| 311 | } else { |
| 312 | oldest_[i] = it; // Store the next iterator even if it does not correspond to |
| 313 | // the same log_id, as a starting point for GetOldest(). |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return it; |
| 319 | } |
| 320 | |
| 321 | // If the selected reader is blocking our pruning progress, decide on |
| 322 | // what kind of mitigation is necessary to unblock the situation. |
| 323 | void SimpleLogBuffer::KickReader(LogReaderThread* reader, log_id_t id, unsigned long prune_rows) { |
| 324 | if (stats_->Sizes(id) > (2 * max_size_[id])) { // +100% |
| 325 | // A misbehaving or slow reader has its connection |
| 326 | // dropped if we hit too much memory pressure. |
| 327 | android::prdebug("Kicking blocked reader, %s, from LogBuffer::kickMe()\n", |
| 328 | reader->name().c_str()); |
| 329 | reader->release_Locked(); |
| 330 | } else if (reader->deadline().time_since_epoch().count() != 0) { |
| 331 | // Allow a blocked WRAP deadline reader to trigger and start reporting the log data. |
| 332 | reader->triggerReader_Locked(); |
| 333 | } else { |
| 334 | // tell slow reader to skip entries to catch up |
| 335 | android::prdebug("Skipping %lu entries from slow reader, %s, from LogBuffer::kickMe()\n", |
| 336 | prune_rows, reader->name().c_str()); |
| 337 | reader->triggerSkip_Locked(id, prune_rows); |
| 338 | } |
| 339 | } |