2014-02-26 18:50:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-11-30 20:35:56 +01:00
|
|
|
#include <errno.h>
|
2017-03-31 19:48:39 +02:00
|
|
|
#include <string.h>
|
2014-04-28 23:07:23 +02:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
2017-03-10 17:44:14 +01:00
|
|
|
#include <private/android_logger.h>
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
#include "FlushCommand.h"
|
|
|
|
#include "LogBuffer.h"
|
|
|
|
#include "LogReader.h"
|
2017-03-10 23:31:54 +01:00
|
|
|
#include "LogTimes.h"
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
LogTimeEntry::LogTimeEntry(LogReader& reader, SocketClient* client,
|
2017-12-04 07:10:40 +01:00
|
|
|
bool nonBlock, unsigned long tail, log_mask_t logMask,
|
|
|
|
pid_t pid, log_time start, uint64_t timeout)
|
2018-10-09 02:33:50 +02:00
|
|
|
: leadingDropped(false),
|
2017-03-10 23:31:54 +01:00
|
|
|
mReader(reader),
|
|
|
|
mLogMask(logMask),
|
|
|
|
mPid(pid),
|
|
|
|
mCount(0),
|
|
|
|
mTail(tail),
|
|
|
|
mIndex(0),
|
|
|
|
mClient(client),
|
|
|
|
mStart(start),
|
|
|
|
mNonBlock(nonBlock),
|
2017-03-10 17:44:14 +01:00
|
|
|
mEnd(log_time(android_log_clockid())) {
|
2015-11-30 20:35:56 +01:00
|
|
|
mTimeout.tv_sec = timeout / NS_PER_SEC;
|
|
|
|
mTimeout.tv_nsec = timeout % NS_PER_SEC;
|
2017-03-31 19:48:39 +02:00
|
|
|
memset(mLastTid, 0, sizeof(mLastTid));
|
|
|
|
pthread_cond_init(&threadTriggeredCondition, nullptr);
|
2015-05-13 00:21:31 +02:00
|
|
|
cleanSkip_Locked();
|
2014-08-07 17:16:52 +02:00
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2018-10-09 02:33:50 +02:00
|
|
|
bool LogTimeEntry::startReader_Locked() {
|
2014-04-04 21:47:44 +02:00
|
|
|
pthread_attr_t attr;
|
|
|
|
|
|
|
|
if (!pthread_attr_init(&attr)) {
|
|
|
|
if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
|
2017-03-10 23:31:54 +01:00
|
|
|
if (!pthread_create(&mThread, &attr, LogTimeEntry::threadStart,
|
|
|
|
this)) {
|
2014-04-04 21:47:44 +02:00
|
|
|
pthread_attr_destroy(&attr);
|
2018-10-09 02:33:50 +02:00
|
|
|
return true;
|
2014-04-04 21:47:44 +02:00
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
2014-04-04 21:47:44 +02:00
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2018-10-09 02:33:50 +02:00
|
|
|
return false;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
void* LogTimeEntry::threadStart(void* obj) {
|
2014-04-28 23:07:23 +02:00
|
|
|
prctl(PR_SET_NAME, "logd.reader.per");
|
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj);
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
SocketClient* client = me->mClient;
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
LogBuffer& logbuf = me->mReader.logbuf();
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
bool privileged = FlushCommand::hasReadLogs(client);
|
2016-01-26 23:32:35 +01:00
|
|
|
bool security = FlushCommand::hasSecurityLogs(client);
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2015-06-04 22:35:30 +02:00
|
|
|
me->leadingDropped = true;
|
|
|
|
|
2017-04-18 23:09:45 +02:00
|
|
|
wrlock();
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2017-03-10 17:44:14 +01:00
|
|
|
log_time start = me->mStart;
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2018-10-09 02:33:50 +02:00
|
|
|
while (!me->mRelease) {
|
2015-11-30 20:35:56 +01:00
|
|
|
if (me->mTimeout.tv_sec || me->mTimeout.tv_nsec) {
|
|
|
|
if (pthread_cond_timedwait(&me->threadTriggeredCondition,
|
2017-03-10 23:31:54 +01:00
|
|
|
×Lock, &me->mTimeout) == ETIMEDOUT) {
|
2015-11-30 20:35:56 +01:00
|
|
|
me->mTimeout.tv_sec = 0;
|
|
|
|
me->mTimeout.tv_nsec = 0;
|
|
|
|
}
|
2018-10-09 02:33:50 +02:00
|
|
|
if (me->mRelease) {
|
2015-11-30 20:35:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
unlock();
|
|
|
|
|
|
|
|
if (me->mTail) {
|
2017-03-31 19:48:39 +02:00
|
|
|
logbuf.flushTo(client, start, nullptr, privileged, security,
|
|
|
|
FilterFirstPass, me);
|
2015-06-04 22:35:30 +02:00
|
|
|
me->leadingDropped = true;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
2017-03-31 19:48:39 +02:00
|
|
|
start = logbuf.flushTo(client, start, me->mLastTid, privileged,
|
|
|
|
security, FilterSecondPass, me);
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2017-04-18 23:09:45 +02:00
|
|
|
wrlock();
|
2014-08-07 17:16:52 +02:00
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
if (start == LogBufferElement::FLUSH_ERROR) {
|
2015-09-17 00:34:00 +02:00
|
|
|
break;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 17:44:14 +01:00
|
|
|
me->mStart = start + log_time(0, 1);
|
2015-09-17 00:34:00 +02:00
|
|
|
|
2018-10-09 02:33:50 +02:00
|
|
|
if (me->mNonBlock || me->mRelease) {
|
2014-02-26 18:50:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:53:41 +01:00
|
|
|
me->cleanSkip_Locked();
|
|
|
|
|
2015-11-30 20:35:56 +01:00
|
|
|
if (!me->mTimeout.tv_sec && !me->mTimeout.tv_nsec) {
|
|
|
|
pthread_cond_wait(&me->threadTriggeredCondition, ×Lock);
|
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2018-10-09 02:33:50 +02:00
|
|
|
LogReader& reader = me->mReader;
|
|
|
|
reader.release(client);
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2018-10-09 02:33:50 +02:00
|
|
|
client->decRef();
|
|
|
|
|
|
|
|
LastLogTimes& times = reader.logbuf().mTimes;
|
|
|
|
auto it =
|
|
|
|
std::find_if(times.begin(), times.end(),
|
|
|
|
[&me](const auto& other) { return other.get() == me; });
|
|
|
|
|
|
|
|
if (it != times.end()) {
|
|
|
|
times.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2017-03-31 19:48:39 +02:00
|
|
|
return nullptr;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A first pass to count the number of elements
|
2017-03-10 23:31:54 +01:00
|
|
|
int LogTimeEntry::FilterFirstPass(const LogBufferElement* element, void* obj) {
|
|
|
|
LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj);
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2017-04-18 23:09:45 +02:00
|
|
|
LogTimeEntry::wrlock();
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2015-06-04 22:35:30 +02:00
|
|
|
if (me->leadingDropped) {
|
|
|
|
if (element->getDropped()) {
|
|
|
|
LogTimeEntry::unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
me->leadingDropped = false;
|
|
|
|
}
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
if (me->mCount == 0) {
|
2017-03-10 17:44:14 +01:00
|
|
|
me->mStart = element->getRealTime();
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 23:31:54 +01:00
|
|
|
if ((!me->mPid || (me->mPid == element->getPid())) &&
|
|
|
|
(me->isWatching(element->getLogId()))) {
|
2014-02-26 18:50:16 +01:00
|
|
|
++me->mCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogTimeEntry::unlock();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A second pass to send the selected elements
|
2017-03-10 23:31:54 +01:00
|
|
|
int LogTimeEntry::FilterSecondPass(const LogBufferElement* element, void* obj) {
|
|
|
|
LogTimeEntry* me = reinterpret_cast<LogTimeEntry*>(obj);
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2017-04-18 23:09:45 +02:00
|
|
|
LogTimeEntry::wrlock();
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2017-03-10 17:44:14 +01:00
|
|
|
me->mStart = element->getRealTime();
|
2014-12-17 09:53:41 +01:00
|
|
|
|
|
|
|
if (me->skipAhead[element->getLogId()]) {
|
|
|
|
me->skipAhead[element->getLogId()]--;
|
2014-06-06 00:58:43 +02:00
|
|
|
goto skip;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2015-06-04 22:35:30 +02:00
|
|
|
if (me->leadingDropped) {
|
|
|
|
if (element->getDropped()) {
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
me->leadingDropped = false;
|
|
|
|
}
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
// Truncate to close race between first and second pass
|
|
|
|
if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
|
2015-03-03 22:39:37 +01:00
|
|
|
goto stop;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2014-12-17 09:53:41 +01:00
|
|
|
if (!me->isWatching(element->getLogId())) {
|
2014-02-26 18:50:16 +01:00
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me->mPid && (me->mPid != element->getPid())) {
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
2018-11-11 12:13:24 +01:00
|
|
|
if (me->mRelease) {
|
|
|
|
goto stop;
|
|
|
|
}
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
if (!me->mTail) {
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
++me->mIndex;
|
|
|
|
|
|
|
|
if ((me->mCount > me->mTail) && (me->mIndex <= (me->mCount - me->mTail))) {
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!me->mNonBlock) {
|
|
|
|
me->mTail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok:
|
2014-12-17 09:53:41 +01:00
|
|
|
if (!me->skipAhead[element->getLogId()]) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogTimeEntry::unlock();
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-10 23:31:54 +01:00
|
|
|
// FALLTHRU
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
skip:
|
|
|
|
LogTimeEntry::unlock();
|
|
|
|
return false;
|
2015-03-03 22:39:37 +01:00
|
|
|
|
|
|
|
stop:
|
|
|
|
LogTimeEntry::unlock();
|
|
|
|
return -1;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
2014-12-17 09:53:41 +01:00
|
|
|
|
|
|
|
void LogTimeEntry::cleanSkip_Locked(void) {
|
2017-03-31 19:48:39 +02:00
|
|
|
memset(skipAhead, 0, sizeof(skipAhead));
|
2014-12-17 09:53:41 +01:00
|
|
|
}
|