blob: 7a6f84b70c03378ebee40779f8c4d3cb8f4b4e8e [file] [log] [blame]
Mark Salyzyn0175b072014-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
Mark Salyzynb75cce02015-11-30 11:35:56 -080017#include <errno.h>
Mark Salyzynae2abf12017-03-31 10:48:39 -070018#include <string.h>
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070019#include <sys/prctl.h>
20
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080021#include <private/android_logger.h>
22
Mark Salyzyn0175b072014-02-26 09:50:16 -080023#include "FlushCommand.h"
24#include "LogBuffer.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include "LogReader.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080026#include "LogTimes.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080027
28pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER;
29
Mark Salyzyn501c3732017-03-10 14:31:54 -080030LogTimeEntry::LogTimeEntry(LogReader& reader, SocketClient* client,
Hao Wangf6e22962017-12-04 14:10:40 +080031 bool nonBlock, unsigned long tail, log_mask_t logMask,
32 pid_t pid, log_time start, uint64_t timeout)
Mark Salyzyn501c3732017-03-10 14:31:54 -080033 : mRefCount(1),
34 mRelease(false),
35 mError(false),
36 threadRunning(false),
37 leadingDropped(false),
38 mReader(reader),
39 mLogMask(logMask),
40 mPid(pid),
41 mCount(0),
42 mTail(tail),
43 mIndex(0),
44 mClient(client),
45 mStart(start),
46 mNonBlock(nonBlock),
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -080047 mEnd(log_time(android_log_clockid())) {
Mark Salyzynb75cce02015-11-30 11:35:56 -080048 mTimeout.tv_sec = timeout / NS_PER_SEC;
49 mTimeout.tv_nsec = timeout % NS_PER_SEC;
Mark Salyzynae2abf12017-03-31 10:48:39 -070050 memset(mLastTid, 0, sizeof(mLastTid));
51 pthread_cond_init(&threadTriggeredCondition, nullptr);
Mark Salyzyn77187782015-05-12 15:21:31 -070052 cleanSkip_Locked();
Mark Salyzyna16f7612014-08-07 08:16:52 -070053}
Mark Salyzyn0175b072014-02-26 09:50:16 -080054
55void LogTimeEntry::startReader_Locked(void) {
Mark Salyzyn765f7822014-04-04 12:47:44 -070056 pthread_attr_t attr;
57
Mark Salyzyn0175b072014-02-26 09:50:16 -080058 threadRunning = true;
Mark Salyzyn765f7822014-04-04 12:47:44 -070059
60 if (!pthread_attr_init(&attr)) {
61 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080062 if (!pthread_create(&mThread, &attr, LogTimeEntry::threadStart,
63 this)) {
Mark Salyzyn765f7822014-04-04 12:47:44 -070064 pthread_attr_destroy(&attr);
65 return;
66 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080067 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070068 pthread_attr_destroy(&attr);
Mark Salyzyn0175b072014-02-26 09:50:16 -080069 }
Mark Salyzyn765f7822014-04-04 12:47:44 -070070 threadRunning = false;
71 if (mClient) {
72 mClient->decRef();
73 }
74 decRef_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080075}
76
Mark Salyzyn501c3732017-03-10 14:31:54 -080077void LogTimeEntry::threadStop(void* obj) {
78 LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj);
Mark Salyzyn0175b072014-02-26 09:50:16 -080079
Mark Salyzyn3c501b52017-04-18 14:09:45 -070080 wrlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -080081
Mark Salyzyn0175b072014-02-26 09:50:16 -080082 if (me->mNonBlock) {
83 me->error_Locked();
84 }
85
Mark Salyzyn501c3732017-03-10 14:31:54 -080086 SocketClient* client = me->mClient;
Mark Salyzyn0175b072014-02-26 09:50:16 -080087
88 if (me->isError_Locked()) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080089 LogReader& reader = me->mReader;
90 LastLogTimes& times = reader.logbuf().mTimes;
Mark Salyzyn0175b072014-02-26 09:50:16 -080091
92 LastLogTimes::iterator it = times.begin();
Mark Salyzyn501c3732017-03-10 14:31:54 -080093 while (it != times.end()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080094 if (*it == me) {
95 times.erase(it);
Mark Salyzync3484162016-03-01 14:59:32 -080096 me->release_nodelete_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080097 break;
98 }
99 it++;
100 }
101
Mark Salyzynae2abf12017-03-31 10:48:39 -0700102 me->mClient = nullptr;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800103 reader.release(client);
104 }
105
106 if (client) {
107 client->decRef();
108 }
109
Mark Salyzyna16f7612014-08-07 08:16:52 -0700110 me->threadRunning = false;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800111 me->decRef_Locked();
112
113 unlock();
114}
115
Mark Salyzyn501c3732017-03-10 14:31:54 -0800116void* LogTimeEntry::threadStart(void* obj) {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -0700117 prctl(PR_SET_NAME, "logd.reader.per");
118
Mark Salyzyn501c3732017-03-10 14:31:54 -0800119 LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800120
121 pthread_cleanup_push(threadStop, obj);
122
Mark Salyzyn501c3732017-03-10 14:31:54 -0800123 SocketClient* client = me->mClient;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800124 if (!client) {
125 me->error();
Mark Salyzynae2abf12017-03-31 10:48:39 -0700126 return nullptr;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800127 }
128
Mark Salyzyn501c3732017-03-10 14:31:54 -0800129 LogBuffer& logbuf = me->mReader.logbuf();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800130
131 bool privileged = FlushCommand::hasReadLogs(client);
Mark Salyzyn8fa88962016-01-26 14:32:35 -0800132 bool security = FlushCommand::hasSecurityLogs(client);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800133
Mark Salyzyn047cc072015-06-04 13:35:30 -0700134 me->leadingDropped = true;
135
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700136 wrlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800137
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800138 log_time start = me->mStart;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800139
Mark Salyzynde4bb9c2015-09-16 15:34:00 -0700140 while (me->threadRunning && !me->isError_Locked()) {
Mark Salyzynb75cce02015-11-30 11:35:56 -0800141 if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
142 if (pthread_cond_timedwait(&me->threadTriggeredCondition,
Mark Salyzyn501c3732017-03-10 14:31:54 -0800143 &timesLock, &me->mTimeout) == ETIMEDOUT) {
Mark Salyzynb75cce02015-11-30 11:35:56 -0800144 me->mTimeout.tv_sec = 0;
145 me->mTimeout.tv_nsec = 0;
146 }
147 if (!me->threadRunning || me->isError_Locked()) {
148 break;
149 }
150 }
151
Mark Salyzyn0175b072014-02-26 09:50:16 -0800152 unlock();
153
154 if (me->mTail) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700155 logbuf.flushTo(client, start, nullptr, privileged, security,
156 FilterFirstPass, me);
Mark Salyzyn047cc072015-06-04 13:35:30 -0700157 me->leadingDropped = true;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800158 }
Mark Salyzynae2abf12017-03-31 10:48:39 -0700159 start = logbuf.flushTo(client, start, me->mLastTid, privileged,
160 security, FilterSecondPass, me);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800161
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700162 wrlock();
Mark Salyzyna16f7612014-08-07 08:16:52 -0700163
Mark Salyzyn0175b072014-02-26 09:50:16 -0800164 if (start == LogBufferElement::FLUSH_ERROR) {
Mark Salyzyna16f7612014-08-07 08:16:52 -0700165 me->error_Locked();
Mark Salyzynde4bb9c2015-09-16 15:34:00 -0700166 break;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800167 }
168
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800169 me->mStart = start + log_time(0, 1);
Mark Salyzynde4bb9c2015-09-16 15:34:00 -0700170
Mark Salyzyna16f7612014-08-07 08:16:52 -0700171 if (me->mNonBlock || !me->threadRunning || me->isError_Locked()) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800172 break;
173 }
174
TraianX Schiauda6495d2014-12-17 10:53:41 +0200175 me->cleanSkip_Locked();
176
Mark Salyzynb75cce02015-11-30 11:35:56 -0800177 if (!me->mTimeout.tv_sec && !me->mTimeout.tv_nsec) {
178 pthread_cond_wait(&me->threadTriggeredCondition, &timesLock);
179 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800180 }
181
182 unlock();
183
Mark Salyzyn0175b072014-02-26 09:50:16 -0800184 pthread_cleanup_pop(true);
185
Mark Salyzynae2abf12017-03-31 10:48:39 -0700186 return nullptr;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800187}
188
189// A first pass to count the number of elements
Mark Salyzyn501c3732017-03-10 14:31:54 -0800190int LogTimeEntry::FilterFirstPass(const LogBufferElement* element, void* obj) {
191 LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800192
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700193 LogTimeEntry::wrlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800194
Mark Salyzyn047cc072015-06-04 13:35:30 -0700195 if (me->leadingDropped) {
196 if (element->getDropped()) {
197 LogTimeEntry::unlock();
198 return false;
199 }
200 me->leadingDropped = false;
201 }
202
Mark Salyzyn0175b072014-02-26 09:50:16 -0800203 if (me->mCount == 0) {
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800204 me->mStart = element->getRealTime();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800205 }
206
Mark Salyzyn501c3732017-03-10 14:31:54 -0800207 if ((!me->mPid || (me->mPid == element->getPid())) &&
208 (me->isWatching(element->getLogId()))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800209 ++me->mCount;
210 }
211
212 LogTimeEntry::unlock();
213
214 return false;
215}
216
217// A second pass to send the selected elements
Mark Salyzyn501c3732017-03-10 14:31:54 -0800218int LogTimeEntry::FilterSecondPass(const LogBufferElement* element, void* obj) {
219 LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800220
Mark Salyzyn3c501b52017-04-18 14:09:45 -0700221 LogTimeEntry::wrlock();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800222
Mark Salyzyn5a34d6e2017-03-10 08:44:14 -0800223 me->mStart = element->getRealTime();
TraianX Schiauda6495d2014-12-17 10:53:41 +0200224
225 if (me->skipAhead[element->getLogId()]) {
226 me->skipAhead[element->getLogId()]--;
Mark Salyzyn8d7656b2014-06-05 15:58:43 -0700227 goto skip;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800228 }
229
Mark Salyzyn047cc072015-06-04 13:35:30 -0700230 if (me->leadingDropped) {
231 if (element->getDropped()) {
232 goto skip;
233 }
234 me->leadingDropped = false;
235 }
236
Mark Salyzyn0175b072014-02-26 09:50:16 -0800237 // Truncate to close race between first and second pass
238 if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800239 goto stop;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800240 }
241
TraianX Schiauda6495d2014-12-17 10:53:41 +0200242 if (!me->isWatching(element->getLogId())) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800243 goto skip;
244 }
245
246 if (me->mPid && (me->mPid != element->getPid())) {
247 goto skip;
248 }
249
250 if (me->isError_Locked()) {
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800251 goto stop;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800252 }
253
254 if (!me->mTail) {
255 goto ok;
256 }
257
258 ++me->mIndex;
259
260 if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) {
261 goto skip;
262 }
263
264 if (!me->mNonBlock) {
265 me->mTail = 0;
266 }
267
268ok:
TraianX Schiauda6495d2014-12-17 10:53:41 +0200269 if (!me->skipAhead[element->getLogId()]) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800270 LogTimeEntry::unlock();
271 return true;
272 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800273// FALLTHRU
Mark Salyzyn0175b072014-02-26 09:50:16 -0800274
275skip:
276 LogTimeEntry::unlock();
277 return false;
Mark Salyzynf7c0f752015-03-03 13:39:37 -0800278
279stop:
280 LogTimeEntry::unlock();
281 return -1;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800282}
TraianX Schiauda6495d2014-12-17 10:53:41 +0200283
284void LogTimeEntry::cleanSkip_Locked(void) {
Mark Salyzynae2abf12017-03-31 10:48:39 -0700285 memset(skipAhead, 0, sizeof(skipAhead));
TraianX Schiauda6495d2014-12-17 10:53:41 +0200286}