blob: 4889c24139a7994c3d9cd5042bdb07b7a48940b9 [file] [log] [blame]
Mark Salyzyn12bac902014-02-26 09:50:16 -08001/*
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 Cherry1647cd42020-05-04 12:53:36 -070017#include "LogReaderThread.h"
18
Mark Salyzync959c642015-11-30 11:35:56 -080019#include <errno.h>
Mark Salyzynbec7b2d2017-03-31 10:48:39 -070020#include <string.h>
Mark Salyzyn4e5efdc2014-04-28 14:07:23 -070021#include <sys/prctl.h>
22
Tom Cherry7e3bc962020-05-04 17:10:16 -070023#include <thread>
24
Mark Salyzyn12bac902014-02-26 09:50:16 -080025#include "LogBuffer.h"
Mark Salyzyn12bac902014-02-26 09:50:16 -080026#include "LogReader.h"
27
Tom Cherrye7c73ef2020-05-04 17:25:34 -070028using namespace std::placeholders;
29
Tom Cherry5ecfbf02020-05-11 16:29:29 -070030LogReaderThread::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 Cherry1647cd42020-05-04 12:53:36 -070034 bool privileged, bool can_read_security_logs)
Tom Cherry7e3bc962020-05-04 17:10:16 -070035 : leading_dropped_(false),
36 reader_(reader),
Tom Cherry5ecfbf02020-05-11 16:29:29 -070037 reader_list_(reader_list),
Tom Cherry7e3bc962020-05-04 17:10:16 -070038 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 Cherry5ecfbf02020-05-11 16:29:29 -070046 deadline_(deadline),
Tom Cherry7e3bc962020-05-04 17:10:16 -070047 non_block_(non_block),
Tom Cherry3086c182020-05-04 11:13:55 -070048 privileged_(privileged),
49 can_read_security_logs_(can_read_security_logs) {
Tom Cherry7e3bc962020-05-04 17:10:16 -070050 memset(last_tid_, 0, sizeof(last_tid_));
Mark Salyzyncd766f92015-05-12 15:21:31 -070051 cleanSkip_Locked();
Mark Salyzyn740d7c22014-08-07 08:16:52 -070052}
Mark Salyzyn12bac902014-02-26 09:50:16 -080053
Tom Cherry1647cd42020-05-04 12:53:36 -070054bool LogReaderThread::startReader_Locked() {
Tom Cherry7e3bc962020-05-04 17:10:16 -070055 auto thread = std::thread{&LogReaderThread::ThreadFunction, this};
56 thread.detach();
57 return true;
Mark Salyzyn12bac902014-02-26 09:50:16 -080058}
59
Tom Cherry7e3bc962020-05-04 17:10:16 -070060void LogReaderThread::ThreadFunction() {
Mark Salyzyn4e5efdc2014-04-28 14:07:23 -070061 prctl(PR_SET_NAME, "logd.reader.per");
62
Tom Cherry7e3bc962020-05-04 17:10:16 -070063 SocketClient* client = client_;
Mark Salyzyn12bac902014-02-26 09:50:16 -080064
Tom Cherry5ecfbf02020-05-11 16:29:29 -070065 LogBuffer& logbuf = *reader_.log_buffer();
Mark Salyzyn12bac902014-02-26 09:50:16 -080066
Tom Cherry7e3bc962020-05-04 17:10:16 -070067 leading_dropped_ = true;
Mark Salyzyn80c1fc62015-06-04 13:35:30 -070068
Tom Cherry5ecfbf02020-05-11 16:29:29 -070069 auto lock = std::unique_lock{reader_list_.reader_threads_lock()};
Mark Salyzyn12bac902014-02-26 09:50:16 -080070
Tom Cherry7e3bc962020-05-04 17:10:16 -070071 uint64_t start = start_;
Mark Salyzyn12bac902014-02-26 09:50:16 -080072
Tom Cherry7e3bc962020-05-04 17:10:16 -070073 while (!release_) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -070074 if (deadline_.time_since_epoch().count() != 0) {
75 if (thread_triggered_condition_.wait_until(lock, deadline_) ==
76 std::cv_status::timeout) {
77 deadline_ = {};
Mark Salyzync959c642015-11-30 11:35:56 -080078 }
Tom Cherry7e3bc962020-05-04 17:10:16 -070079 if (release_) {
Mark Salyzync959c642015-11-30 11:35:56 -080080 break;
81 }
82 }
83
Tom Cherry5ecfbf02020-05-11 16:29:29 -070084 lock.unlock();
Mark Salyzyn12bac902014-02-26 09:50:16 -080085
Tom Cherry7e3bc962020-05-04 17:10:16 -070086 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 Salyzyn12bac902014-02-26 09:50:16 -080092 }
Tom Cherry7e3bc962020-05-04 17:10:16 -070093 start = logbuf.flushTo(client, start, last_tid_, privileged_, can_read_security_logs_,
94 std::bind(&LogReaderThread::FilterSecondPass, this, _1));
Mark Salyzyn12bac902014-02-26 09:50:16 -080095
Tom Cherrye8d20892019-08-21 14:53:06 -070096 // 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 Cherry7e3bc962020-05-04 17:10:16 -0700102 start_time_.tv_sec = 0;
103 start_time_.tv_nsec = 0;
Tom Cherrye8d20892019-08-21 14:53:06 -0700104
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700105 lock.lock();
Mark Salyzyn740d7c22014-08-07 08:16:52 -0700106
Mark Salyzyn12bac902014-02-26 09:50:16 -0800107 if (start == LogBufferElement::FLUSH_ERROR) {
Mark Salyzyn28cb69a2015-09-16 15:34:00 -0700108 break;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800109 }
110
Tom Cherry7e3bc962020-05-04 17:10:16 -0700111 start_ = start + 1;
Mark Salyzyn28cb69a2015-09-16 15:34:00 -0700112
Tom Cherry7e3bc962020-05-04 17:10:16 -0700113 if (non_block_ || release_) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800114 break;
115 }
116
Tom Cherry7e3bc962020-05-04 17:10:16 -0700117 cleanSkip_Locked();
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200118
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700119 if (deadline_.time_since_epoch().count() == 0) {
120 thread_triggered_condition_.wait(lock);
Mark Salyzync959c642015-11-30 11:35:56 -0800121 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800122 }
123
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700124 reader_.release(client);
Tom Cherry06e478b2018-10-08 17:33:50 -0700125 client->decRef();
126
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700127 auto& log_reader_threads = reader_list_.reader_threads();
128 auto it = std::find_if(log_reader_threads.begin(), log_reader_threads.end(),
Tom Cherry7e3bc962020-05-04 17:10:16 -0700129 [this](const auto& other) { return other.get() == this; });
Tom Cherry06e478b2018-10-08 17:33:50 -0700130
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700131 if (it != log_reader_threads.end()) {
132 log_reader_threads.erase(it);
Tom Cherry06e478b2018-10-08 17:33:50 -0700133 }
Mark Salyzyn12bac902014-02-26 09:50:16 -0800134}
135
136// A first pass to count the number of elements
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700137FlushToResult LogReaderThread::FilterFirstPass(const LogBufferElement* element) {
138 auto lock = std::lock_guard{reader_list_.reader_threads_lock()};
Mark Salyzyn12bac902014-02-26 09:50:16 -0800139
Tom Cherry7e3bc962020-05-04 17:10:16 -0700140 if (leading_dropped_) {
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700141 if (element->getDropped()) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700142 return FlushToResult::kSkip;
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700143 }
Tom Cherry7e3bc962020-05-04 17:10:16 -0700144 leading_dropped_ = false;
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700145 }
146
Tom Cherry7e3bc962020-05-04 17:10:16 -0700147 if (count_ == 0) {
148 start_ = element->getSequence();
Mark Salyzyn12bac902014-02-26 09:50:16 -0800149 }
150
Tom Cherry7e3bc962020-05-04 17:10:16 -0700151 if ((!pid_ || pid_ == element->getPid()) && IsWatching(element->getLogId()) &&
152 (start_time_ == log_time::EPOCH || start_time_ <= element->getRealTime())) {
153 ++count_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800154 }
155
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700156 return FlushToResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800157}
158
159// A second pass to send the selected elements
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700160FlushToResult LogReaderThread::FilterSecondPass(const LogBufferElement* element) {
161 auto lock = std::lock_guard{reader_list_.reader_threads_lock()};
Mark Salyzyn12bac902014-02-26 09:50:16 -0800162
Tom Cherry7e3bc962020-05-04 17:10:16 -0700163 start_ = element->getSequence();
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200164
Tom Cherry7e3bc962020-05-04 17:10:16 -0700165 if (skip_ahead_[element->getLogId()]) {
166 skip_ahead_[element->getLogId()]--;
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700167 return FlushToResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800168 }
169
Tom Cherry7e3bc962020-05-04 17:10:16 -0700170 if (leading_dropped_) {
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700171 if (element->getDropped()) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700172 return FlushToResult::kSkip;
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700173 }
Tom Cherry7e3bc962020-05-04 17:10:16 -0700174 leading_dropped_ = false;
Mark Salyzyn80c1fc62015-06-04 13:35:30 -0700175 }
176
Mark Salyzyn12bac902014-02-26 09:50:16 -0800177 // Truncate to close race between first and second pass
Tom Cherry7e3bc962020-05-04 17:10:16 -0700178 if (non_block_ && tail_ && index_ >= count_) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700179 return FlushToResult::kStop;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800180 }
181
Tom Cherry7e3bc962020-05-04 17:10:16 -0700182 if (!IsWatching(element->getLogId())) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700183 return FlushToResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800184 }
185
Tom Cherry7e3bc962020-05-04 17:10:16 -0700186 if (pid_ && pid_ != element->getPid()) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700187 return FlushToResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800188 }
189
Tom Cherry7e3bc962020-05-04 17:10:16 -0700190 if (start_time_ != log_time::EPOCH && element->getRealTime() <= start_time_) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700191 return FlushToResult::kSkip;
Tom Cherrye8d20892019-08-21 14:53:06 -0700192 }
193
Tom Cherry7e3bc962020-05-04 17:10:16 -0700194 if (release_) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700195 return FlushToResult::kStop;
Jintao_Zhu3e6f9d82018-11-11 03:13:24 -0800196 }
197
Tom Cherry7e3bc962020-05-04 17:10:16 -0700198 if (!tail_) {
Mark Salyzyn12bac902014-02-26 09:50:16 -0800199 goto ok;
200 }
201
Tom Cherry7e3bc962020-05-04 17:10:16 -0700202 ++index_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800203
Tom Cherry7e3bc962020-05-04 17:10:16 -0700204 if (count_ > tail_ && index_ <= (count_ - tail_)) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700205 return FlushToResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800206 }
207
Tom Cherry7e3bc962020-05-04 17:10:16 -0700208 if (!non_block_) {
209 tail_ = 0;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800210 }
211
212ok:
Tom Cherry7e3bc962020-05-04 17:10:16 -0700213 if (!skip_ahead_[element->getLogId()]) {
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700214 return FlushToResult::kWrite;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800215 }
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700216 return FlushToResult::kSkip;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800217}
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200218
Tom Cherry1647cd42020-05-04 12:53:36 -0700219void LogReaderThread::cleanSkip_Locked(void) {
Tom Cherry7e3bc962020-05-04 17:10:16 -0700220 memset(skip_ahead_, 0, sizeof(skip_ahead_));
TraianX Schiau6ba427e2014-12-17 10:53:41 +0200221}