Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 17 | #include "LogReaderThread.h" |
| 18 | |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Mark Salyzyn | bec7b2d | 2017-03-31 10:48:39 -0700 | [diff] [blame] | 20 | #include <string.h> |
Mark Salyzyn | 4e5efdc | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 21 | #include <sys/prctl.h> |
| 22 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 23 | #include <thread> |
| 24 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | #include "LogBuffer.h" |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 26 | #include "LogReader.h" |
| 27 | |
Tom Cherry | e7c73ef | 2020-05-04 17:25:34 -0700 | [diff] [blame] | 28 | using namespace std::placeholders; |
| 29 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 30 | LogReaderThread::LogReaderThread(LogReader& reader, LogReaderList& reader_list, |
| 31 | SocketClient* client, bool non_block, unsigned long tail, |
| 32 | unsigned int log_mask, pid_t pid, log_time start_time, |
| 33 | uint64_t start, std::chrono::steady_clock::time_point deadline, |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 34 | bool privileged, bool can_read_security_logs) |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 35 | : leading_dropped_(false), |
| 36 | reader_(reader), |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 37 | reader_list_(reader_list), |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 38 | log_mask_(log_mask), |
| 39 | pid_(pid), |
| 40 | tail_(tail), |
| 41 | count_(0), |
| 42 | index_(0), |
| 43 | client_(client), |
| 44 | start_time_(start_time), |
| 45 | start_(start), |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 46 | deadline_(deadline), |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 47 | non_block_(non_block), |
Tom Cherry | 3086c18 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 48 | privileged_(privileged), |
| 49 | can_read_security_logs_(can_read_security_logs) { |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 50 | memset(last_tid_, 0, sizeof(last_tid_)); |
Mark Salyzyn | cd766f9 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 51 | cleanSkip_Locked(); |
Mark Salyzyn | 740d7c2 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 52 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 53 | |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 54 | bool LogReaderThread::startReader_Locked() { |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 55 | auto thread = std::thread{&LogReaderThread::ThreadFunction, this}; |
| 56 | thread.detach(); |
| 57 | return true; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 60 | void LogReaderThread::ThreadFunction() { |
Mark Salyzyn | 4e5efdc | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 61 | prctl(PR_SET_NAME, "logd.reader.per"); |
| 62 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 63 | SocketClient* client = client_; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 65 | LogBuffer& logbuf = *reader_.log_buffer(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 66 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 67 | leading_dropped_ = true; |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 68 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 69 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 70 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 71 | uint64_t start = start_; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 72 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 73 | while (!release_) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 74 | if (deadline_.time_since_epoch().count() != 0) { |
| 75 | if (thread_triggered_condition_.wait_until(lock, deadline_) == |
| 76 | std::cv_status::timeout) { |
| 77 | deadline_ = {}; |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 78 | } |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 79 | if (release_) { |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 84 | lock.unlock(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 85 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 86 | if (tail_) { |
| 87 | logbuf.flushTo(client, start, nullptr, privileged_, can_read_security_logs_, |
| 88 | std::bind(&LogReaderThread::FilterFirstPass, this, _1)); |
| 89 | leading_dropped_ = |
| 90 | true; // TODO: Likely a bug, if leading_dropped_ was not true before calling |
| 91 | // flushTo(), then it should not be reset to true after. |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 92 | } |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 93 | start = logbuf.flushTo(client, start, last_tid_, privileged_, can_read_security_logs_, |
| 94 | std::bind(&LogReaderThread::FilterSecondPass, this, _1)); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 95 | |
Tom Cherry | e8d2089 | 2019-08-21 14:53:06 -0700 | [diff] [blame] | 96 | // We only ignore entries before the original start time for the first flushTo(), if we |
| 97 | // get entries after this first flush before the original start time, then the client |
| 98 | // wouldn't have seen them. |
| 99 | // Note: this is still racy and may skip out of order events that came in since the last |
| 100 | // time the client disconnected and then reconnected with the new start time. The long term |
| 101 | // solution here is that clients must request events since a specific sequence number. |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 102 | start_time_.tv_sec = 0; |
| 103 | start_time_.tv_nsec = 0; |
Tom Cherry | e8d2089 | 2019-08-21 14:53:06 -0700 | [diff] [blame] | 104 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 105 | lock.lock(); |
Mark Salyzyn | 740d7c2 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 106 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 107 | if (start == LogBufferElement::FLUSH_ERROR) { |
Mark Salyzyn | 28cb69a | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 108 | break; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 111 | start_ = start + 1; |
Mark Salyzyn | 28cb69a | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 112 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 113 | if (non_block_ || release_) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 117 | cleanSkip_Locked(); |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 118 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 119 | if (deadline_.time_since_epoch().count() == 0) { |
| 120 | thread_triggered_condition_.wait(lock); |
Mark Salyzyn | c959c64 | 2015-11-30 11:35:56 -0800 | [diff] [blame] | 121 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 124 | reader_.release(client); |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 125 | client->decRef(); |
| 126 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 127 | auto& log_reader_threads = reader_list_.reader_threads(); |
| 128 | auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(), |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 129 | [this](const auto& other) { return other.get() == this; }); |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 130 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 131 | if (it != log_reader_threads.end()) { |
| 132 | log_reader_threads.erase(it); |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 133 | } |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // A first pass to count the number of elements |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 137 | FlushToResult LogReaderThread::FilterFirstPass(const LogBufferElement* element) { |
| 138 | auto lock = std::lock_guard{reader_list_.reader_threads_lock()}; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 139 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 140 | if (leading_dropped_) { |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 141 | if (element->getDropped()) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 142 | return FlushToResult::kSkip; |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 143 | } |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 144 | leading_dropped_ = false; |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 147 | if (count_ == 0) { |
| 148 | start_ = element->getSequence(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 151 | if ((!pid_ || pid_ == element->getPid()) && IsWatching(element->getLogId()) && |
| 152 | (start_time_ == log_time::EPOCH || start_time_ <= element->getRealTime())) { |
| 153 | ++count_; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 156 | return FlushToResult::kSkip; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // A second pass to send the selected elements |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 160 | FlushToResult LogReaderThread::FilterSecondPass(const LogBufferElement* element) { |
| 161 | auto lock = std::lock_guard{reader_list_.reader_threads_lock()}; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 162 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 163 | start_ = element->getSequence(); |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 164 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 165 | if (skip_ahead_[element->getLogId()]) { |
| 166 | skip_ahead_[element->getLogId()]--; |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 167 | return FlushToResult::kSkip; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 170 | if (leading_dropped_) { |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 171 | if (element->getDropped()) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 172 | return FlushToResult::kSkip; |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 173 | } |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 174 | leading_dropped_ = false; |
Mark Salyzyn | 80c1fc6 | 2015-06-04 13:35:30 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 177 | // Truncate to close race between first and second pass |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 178 | if (non_block_ && tail_ && index_ >= count_) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 179 | return FlushToResult::kStop; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 182 | if (!IsWatching(element->getLogId())) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 183 | return FlushToResult::kSkip; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 186 | if (pid_ && pid_ != element->getPid()) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 187 | return FlushToResult::kSkip; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 190 | if (start_time_ != log_time::EPOCH && element->getRealTime() <= start_time_) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 191 | return FlushToResult::kSkip; |
Tom Cherry | e8d2089 | 2019-08-21 14:53:06 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 194 | if (release_) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 195 | return FlushToResult::kStop; |
Jintao_Zhu | 3e6f9d8 | 2018-11-11 03:13:24 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 198 | if (!tail_) { |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 199 | goto ok; |
| 200 | } |
| 201 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 202 | ++index_; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 203 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 204 | if (count_ > tail_ && index_ <= (count_ - tail_)) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 205 | return FlushToResult::kSkip; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 208 | if (!non_block_) { |
| 209 | tail_ = 0; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | ok: |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 213 | if (!skip_ahead_[element->getLogId()]) { |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 214 | return FlushToResult::kWrite; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 215 | } |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 216 | return FlushToResult::kSkip; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 217 | } |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 218 | |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 219 | void LogReaderThread::cleanSkip_Locked(void) { |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 220 | memset(skip_ahead_, 0, sizeof(skip_ahead_)); |
TraianX Schiau | 6ba427e | 2014-12-17 10:53:41 +0200 | [diff] [blame] | 221 | } |