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.
|
|
|
|
*/
|
|
|
|
|
2014-04-28 23:07:23 +02:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
#include "FlushCommand.h"
|
|
|
|
#include "LogBuffer.h"
|
|
|
|
#include "LogTimes.h"
|
|
|
|
#include "LogReader.h"
|
|
|
|
|
|
|
|
pthread_mutex_t LogTimeEntry::timesLock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
LogTimeEntry::LogTimeEntry(LogReader &reader, SocketClient *client,
|
|
|
|
bool nonBlock, unsigned long tail,
|
2014-02-15 01:05:05 +01:00
|
|
|
unsigned int logMask, pid_t pid,
|
2015-05-13 00:21:31 +02:00
|
|
|
uint64_t start) :
|
|
|
|
mRefCount(1),
|
|
|
|
mRelease(false),
|
|
|
|
mError(false),
|
|
|
|
threadRunning(false),
|
2015-06-04 22:35:30 +02:00
|
|
|
leadingDropped(false),
|
2015-05-13 00:21:31 +02:00
|
|
|
mReader(reader),
|
|
|
|
mLogMask(logMask),
|
|
|
|
mPid(pid),
|
|
|
|
mCount(0),
|
|
|
|
mTail(tail),
|
|
|
|
mIndex(0),
|
|
|
|
mClient(client),
|
|
|
|
mStart(start),
|
|
|
|
mNonBlock(nonBlock),
|
|
|
|
mEnd(LogBufferElement::getCurrentSequence()) {
|
|
|
|
pthread_cond_init(&threadTriggeredCondition, NULL);
|
|
|
|
cleanSkip_Locked();
|
2014-08-07 17:16:52 +02:00
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
void LogTimeEntry::startReader_Locked(void) {
|
2014-04-04 21:47:44 +02:00
|
|
|
pthread_attr_t attr;
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
threadRunning = true;
|
2014-04-04 21:47:44 +02:00
|
|
|
|
|
|
|
if (!pthread_attr_init(&attr)) {
|
|
|
|
if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
|
|
|
|
if (!pthread_create(&mThread, &attr,
|
|
|
|
LogTimeEntry::threadStart, this)) {
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
2014-04-04 21:47:44 +02:00
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
}
|
|
|
|
threadRunning = false;
|
|
|
|
if (mClient) {
|
|
|
|
mClient->decRef();
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
2014-04-04 21:47:44 +02:00
|
|
|
decRef_Locked();
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogTimeEntry::threadStop(void *obj) {
|
|
|
|
LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
|
|
|
|
|
|
|
|
lock();
|
|
|
|
|
|
|
|
if (me->mNonBlock) {
|
|
|
|
me->error_Locked();
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketClient *client = me->mClient;
|
|
|
|
|
|
|
|
if (me->isError_Locked()) {
|
|
|
|
LogReader &reader = me->mReader;
|
|
|
|
LastLogTimes × = reader.logbuf().mTimes;
|
|
|
|
|
|
|
|
LastLogTimes::iterator it = times.begin();
|
|
|
|
while(it != times.end()) {
|
|
|
|
if (*it == me) {
|
|
|
|
times.erase(it);
|
|
|
|
me->release_Locked();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
me->mClient = NULL;
|
|
|
|
reader.release(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client) {
|
|
|
|
client->decRef();
|
|
|
|
}
|
|
|
|
|
2014-08-07 17:16:52 +02:00
|
|
|
me->threadRunning = false;
|
2014-02-26 18:50:16 +01:00
|
|
|
me->decRef_Locked();
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void *LogTimeEntry::threadStart(void *obj) {
|
2014-04-28 23:07:23 +02:00
|
|
|
prctl(PR_SET_NAME, "logd.reader.per");
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
|
|
|
|
|
|
|
|
pthread_cleanup_push(threadStop, obj);
|
|
|
|
|
|
|
|
SocketClient *client = me->mClient;
|
|
|
|
if (!client) {
|
|
|
|
me->error();
|
2014-08-07 17:16:52 +02:00
|
|
|
return NULL;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LogBuffer &logbuf = me->mReader.logbuf();
|
|
|
|
|
|
|
|
bool privileged = FlushCommand::hasReadLogs(client);
|
|
|
|
|
2015-06-04 22:35:30 +02:00
|
|
|
me->leadingDropped = true;
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
lock();
|
|
|
|
|
2014-08-07 17:16:52 +02:00
|
|
|
while (me->threadRunning && !me->isError_Locked()) {
|
2015-03-03 22:39:37 +01:00
|
|
|
uint64_t start = me->mStart;
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
if (me->mTail) {
|
|
|
|
logbuf.flushTo(client, start, privileged, FilterFirstPass, me);
|
2015-06-04 22:35:30 +02:00
|
|
|
me->leadingDropped = true;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
start = logbuf.flushTo(client, start, privileged, FilterSecondPass, me);
|
|
|
|
|
2014-08-07 17:16:52 +02:00
|
|
|
lock();
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
if (start == LogBufferElement::FLUSH_ERROR) {
|
2014-08-07 17:16:52 +02:00
|
|
|
me->error_Locked();
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2014-08-07 17:16:52 +02:00
|
|
|
if (me->mNonBlock || !me->threadRunning || me->isError_Locked()) {
|
2014-02-26 18:50:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:53:41 +01:00
|
|
|
me->cleanSkip_Locked();
|
|
|
|
|
2014-08-07 17:16:52 +02:00
|
|
|
pthread_cond_wait(&me->threadTriggeredCondition, ×Lock);
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
pthread_cleanup_pop(true);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A first pass to count the number of elements
|
2015-03-03 22:39:37 +01:00
|
|
|
int LogTimeEntry::FilterFirstPass(const LogBufferElement *element, void *obj) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
|
|
|
|
|
|
|
|
LogTimeEntry::lock();
|
|
|
|
|
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) {
|
2015-03-03 22:39:37 +01:00
|
|
|
me->mStart = element->getSequence();
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((!me->mPid || (me->mPid == element->getPid()))
|
2014-12-17 09:53:41 +01:00
|
|
|
&& (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
|
2015-03-03 22:39:37 +01:00
|
|
|
int LogTimeEntry::FilterSecondPass(const LogBufferElement *element, void *obj) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogTimeEntry *me = reinterpret_cast<LogTimeEntry *>(obj);
|
|
|
|
|
|
|
|
LogTimeEntry::lock();
|
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
me->mStart = element->getSequence();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me->isError_Locked()) {
|
2015-03-03 22:39:37 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
// FALLTHRU
|
|
|
|
|
|
|
|
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) {
|
|
|
|
for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1)) {
|
|
|
|
skipAhead[i] = 0;
|
|
|
|
}
|
|
|
|
}
|