2014-02-26 18:50:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-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-05-06 16:34:59 +02:00
|
|
|
#include <ctype.h>
|
2014-02-26 18:50:16 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-05-10 02:44:18 +02:00
|
|
|
#include <sys/user.h>
|
2014-02-26 18:50:16 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
#include <cutils/properties.h>
|
2014-02-26 18:50:16 +01:00
|
|
|
#include <log/logger.h>
|
|
|
|
|
|
|
|
#include "LogBuffer.h"
|
2014-05-06 16:34:59 +02:00
|
|
|
#include "LogReader.h"
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
// Default
|
2014-02-26 18:50:16 +01:00
|
|
|
#define LOG_BUFFER_SIZE (256 * 1024) // Tuned on a per-platform basis here?
|
2014-02-11 21:29:31 +01:00
|
|
|
#define log_buffer_size(id) mMaxSize[id]
|
2014-05-10 02:44:18 +02:00
|
|
|
#define LOG_BUFFER_MIN_SIZE (64 * 1024UL)
|
|
|
|
#define LOG_BUFFER_MAX_SIZE (256 * 1024 * 1024UL)
|
|
|
|
|
|
|
|
static bool valid_size(unsigned long value) {
|
|
|
|
if ((value < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
long pages = sysconf(_SC_PHYS_PAGES);
|
|
|
|
if (pages < 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
long pagesize = sysconf(_SC_PAGESIZE);
|
|
|
|
if (pagesize <= 1) {
|
|
|
|
pagesize = PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// maximum memory impact a somewhat arbitrary ~3%
|
|
|
|
pages = (pages + 31) / 32;
|
|
|
|
unsigned long maximum = pages * pagesize;
|
|
|
|
|
|
|
|
if ((maximum < LOG_BUFFER_MIN_SIZE) || (LOG_BUFFER_MAX_SIZE < maximum)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value <= maximum;
|
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
static unsigned long property_get_size(const char *key) {
|
|
|
|
char property[PROPERTY_VALUE_MAX];
|
|
|
|
property_get(key, property, "");
|
|
|
|
|
|
|
|
char *cp;
|
|
|
|
unsigned long value = strtoul(property, &cp, 10);
|
|
|
|
|
|
|
|
switch(*cp) {
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
value *= 1024;
|
|
|
|
/* FALLTHRU */
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
value *= 1024;
|
|
|
|
/* FALLTHRU */
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
|
2014-05-10 02:44:18 +02:00
|
|
|
if (!valid_size(value)) {
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-11 00:45:17 +01:00
|
|
|
void LogBuffer::init() {
|
2014-05-10 02:44:18 +02:00
|
|
|
static const char global_tuneable[] = "persist.logd.size"; // Settings App
|
|
|
|
static const char global_default[] = "ro.logd.size"; // BoardConfig.mk
|
2014-05-06 16:34:59 +02:00
|
|
|
|
2014-05-10 02:44:18 +02:00
|
|
|
unsigned long default_size = property_get_size(global_tuneable);
|
|
|
|
if (!default_size) {
|
|
|
|
default_size = property_get_size(global_default);
|
|
|
|
}
|
2014-05-06 16:34:59 +02:00
|
|
|
|
2014-05-10 02:44:18 +02:00
|
|
|
log_id_for_each(i) {
|
2014-05-06 16:34:59 +02:00
|
|
|
char key[PROP_NAME_MAX];
|
2014-05-10 02:44:18 +02:00
|
|
|
|
2014-05-06 16:34:59 +02:00
|
|
|
snprintf(key, sizeof(key), "%s.%s",
|
2014-05-10 02:44:18 +02:00
|
|
|
global_tuneable, android_log_id_to_name(i));
|
|
|
|
unsigned long property_size = property_get_size(key);
|
|
|
|
|
|
|
|
if (!property_size) {
|
|
|
|
snprintf(key, sizeof(key), "%s.%s",
|
|
|
|
global_default, android_log_id_to_name(i));
|
|
|
|
property_size = property_get_size(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!property_size) {
|
|
|
|
property_size = default_size;
|
|
|
|
}
|
2014-05-06 16:34:59 +02:00
|
|
|
|
2014-05-10 02:44:18 +02:00
|
|
|
if (!property_size) {
|
|
|
|
property_size = LOG_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setSize(i, property_size)) {
|
|
|
|
setSize(i, LOG_BUFFER_MIN_SIZE);
|
|
|
|
}
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2015-03-11 00:45:17 +01:00
|
|
|
LogBuffer::LogBuffer(LastLogTimes *times)
|
|
|
|
: mTimes(*times) {
|
|
|
|
pthread_mutex_init(&mLogElementsLock, NULL);
|
|
|
|
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2014-03-05 16:41:49 +01:00
|
|
|
void LogBuffer::log(log_id_t log_id, log_time realtime,
|
2014-03-21 00:09:38 +01:00
|
|
|
uid_t uid, pid_t pid, pid_t tid,
|
|
|
|
const char *msg, unsigned short len) {
|
2014-02-26 18:50:16 +01:00
|
|
|
if ((log_id >= LOG_ID_MAX) || (log_id < 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LogBufferElement *elem = new LogBufferElement(log_id, realtime,
|
2014-03-21 00:09:38 +01:00
|
|
|
uid, pid, tid, msg, len);
|
2014-02-26 18:50:16 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
|
|
|
|
// Insert elements in time sorted order if possible
|
|
|
|
// NB: if end is region locked, place element at end of list
|
|
|
|
LogBufferElementCollection::iterator it = mLogElements.end();
|
|
|
|
LogBufferElementCollection::iterator last = it;
|
2014-10-14 01:49:47 +02:00
|
|
|
while (last != mLogElements.begin()) {
|
|
|
|
--it;
|
2014-02-18 20:23:53 +01:00
|
|
|
if ((*it)->getRealTime() <= realtime) {
|
2014-02-26 18:50:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
last = it;
|
|
|
|
}
|
2014-02-18 20:23:53 +01:00
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
if (last == mLogElements.end()) {
|
|
|
|
mLogElements.push_back(elem);
|
|
|
|
} else {
|
2015-03-03 22:39:37 +01:00
|
|
|
uint64_t end = 1;
|
2014-02-26 18:50:16 +01:00
|
|
|
bool end_set = false;
|
|
|
|
bool end_always = false;
|
|
|
|
|
|
|
|
LogTimeEntry::lock();
|
|
|
|
|
|
|
|
LastLogTimes::iterator t = mTimes.begin();
|
|
|
|
while(t != mTimes.end()) {
|
|
|
|
LogTimeEntry *entry = (*t);
|
|
|
|
if (entry->owned_Locked()) {
|
|
|
|
if (!entry->mNonBlock) {
|
|
|
|
end_always = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!end_set || (end <= entry->mEnd)) {
|
|
|
|
end = entry->mEnd;
|
|
|
|
end_set = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_always
|
2015-03-03 22:39:37 +01:00
|
|
|
|| (end_set && (end >= (*last)->getSequence()))) {
|
2014-02-26 18:50:16 +01:00
|
|
|
mLogElements.push_back(elem);
|
|
|
|
} else {
|
|
|
|
mLogElements.insert(last,elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogTimeEntry::unlock();
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
stats.add(elem);
|
2014-02-26 18:50:16 +01:00
|
|
|
maybePrune(log_id);
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're using more than 256K of memory for log entries, prune
|
2014-01-14 01:37:51 +01:00
|
|
|
// at least 10% of the log entries.
|
2014-02-26 18:50:16 +01:00
|
|
|
//
|
|
|
|
// mLogElementsLock must be held when this function is called.
|
|
|
|
void LogBuffer::maybePrune(log_id_t id) {
|
2014-02-06 23:48:50 +01:00
|
|
|
size_t sizes = stats.sizes(id);
|
2014-02-11 21:29:31 +01:00
|
|
|
if (sizes > log_buffer_size(id)) {
|
|
|
|
size_t sizeOver90Percent = sizes - ((log_buffer_size(id) * 9) / 10);
|
2014-02-06 23:48:50 +01:00
|
|
|
size_t elements = stats.elements(id);
|
2014-01-14 01:37:51 +01:00
|
|
|
unsigned long pruneRows = elements * sizeOver90Percent / sizes;
|
|
|
|
elements /= 10;
|
|
|
|
if (pruneRows <= elements) {
|
|
|
|
pruneRows = elements;
|
|
|
|
}
|
|
|
|
prune(id, pruneRows);
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
LogBufferElementCollection::iterator LogBuffer::erase(LogBufferElementCollection::iterator it) {
|
|
|
|
LogBufferElement *e = *it;
|
|
|
|
|
|
|
|
it = mLogElements.erase(it);
|
|
|
|
stats.subtract(e);
|
|
|
|
delete e;
|
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
// prune "pruneRows" of type "id" from the buffer.
|
|
|
|
//
|
|
|
|
// mLogElementsLock must be held when this function is called.
|
2014-06-12 20:16:16 +02:00
|
|
|
void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogTimeEntry *oldest = NULL;
|
|
|
|
|
|
|
|
LogTimeEntry::lock();
|
|
|
|
|
|
|
|
// Region locked?
|
|
|
|
LastLogTimes::iterator t = mTimes.begin();
|
|
|
|
while(t != mTimes.end()) {
|
|
|
|
LogTimeEntry *entry = (*t);
|
2014-12-17 09:53:41 +01:00
|
|
|
if (entry->owned_Locked() && entry->isWatching(id)
|
2014-02-26 18:50:16 +01:00
|
|
|
&& (!oldest || (oldest->mStart > entry->mStart))) {
|
|
|
|
oldest = entry;
|
|
|
|
}
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
|
2014-02-07 03:11:13 +01:00
|
|
|
LogBufferElementCollection::iterator it;
|
|
|
|
|
2014-06-12 20:16:16 +02:00
|
|
|
if (caller_uid != AID_ROOT) {
|
|
|
|
for(it = mLogElements.begin(); it != mLogElements.end();) {
|
|
|
|
LogBufferElement *e = *it;
|
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
if (oldest && (oldest->mStart <= e->getSequence())) {
|
2014-06-12 20:16:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->getLogId() != id) {
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
if (e->getUid() == caller_uid) {
|
|
|
|
it = erase(it);
|
2014-06-12 20:16:16 +02:00
|
|
|
pruneRows--;
|
|
|
|
if (pruneRows == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LogTimeEntry::unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 03:11:13 +01:00
|
|
|
// prune by worst offender by uid
|
2015-03-10 21:51:35 +01:00
|
|
|
bool hasBlacklist = mPrune.naughty();
|
2014-02-07 03:11:13 +01:00
|
|
|
while (pruneRows > 0) {
|
|
|
|
// recalculate the worst offender on every batched pass
|
|
|
|
uid_t worst = (uid_t) -1;
|
|
|
|
size_t worst_sizes = 0;
|
|
|
|
size_t second_worst_sizes = 0;
|
|
|
|
|
2014-04-07 23:58:08 +02:00
|
|
|
if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) {
|
2015-03-16 16:26:05 +01:00
|
|
|
std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
|
2015-03-10 21:51:35 +01:00
|
|
|
|
2015-03-16 16:26:05 +01:00
|
|
|
if (sorted.get()) {
|
2015-03-10 21:51:35 +01:00
|
|
|
if (sorted[0] && sorted[1]) {
|
|
|
|
worst = sorted[0]->getKey();
|
|
|
|
worst_sizes = sorted[0]->getSizes();
|
|
|
|
second_worst_sizes = sorted[1]->getSizes();
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2014-02-07 03:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
// skip if we have neither worst nor naughty filters
|
|
|
|
if ((worst == (uid_t) -1) && !hasBlacklist) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-07 03:11:13 +01:00
|
|
|
bool kick = false;
|
2015-03-16 20:04:09 +01:00
|
|
|
bool leading = true;
|
|
|
|
LogBufferElement *last = NULL;
|
2014-02-07 03:11:13 +01:00
|
|
|
for(it = mLogElements.begin(); it != mLogElements.end();) {
|
|
|
|
LogBufferElement *e = *it;
|
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
if (oldest && (oldest->mStart <= e->getSequence())) {
|
2014-02-07 03:11:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
if (e->getLogId() != id) {
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-16 20:04:09 +01:00
|
|
|
unsigned short dropped = e->getDropped();
|
2014-02-11 21:29:31 +01:00
|
|
|
|
2015-03-16 20:04:09 +01:00
|
|
|
// remove any leading drops
|
|
|
|
if (leading && dropped) {
|
|
|
|
it = erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t pid = e->getPid();
|
|
|
|
|
|
|
|
// merge any drops
|
|
|
|
if (last && dropped
|
|
|
|
&& ((dropped + last->getDropped()) < USHRT_MAX)
|
|
|
|
&& (last->getPid() == pid)
|
|
|
|
&& (last->getTid() == e->getTid())) {
|
|
|
|
it = mLogElements.erase(it);
|
|
|
|
stats.erase(e);
|
|
|
|
delete e;
|
|
|
|
last->setDropped(dropped + last->getDropped());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
leading = false;
|
|
|
|
|
|
|
|
if (hasBlacklist && mPrune.naughty(e)) {
|
|
|
|
last = NULL;
|
|
|
|
it = erase(it);
|
|
|
|
if (dropped) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pruneRows--;
|
|
|
|
if (pruneRows == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->getUid() == worst) {
|
|
|
|
kick = true;
|
|
|
|
if (worst_sizes < second_worst_sizes) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
worst_sizes -= e->getMsgLen();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dropped) {
|
|
|
|
last = e;
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->getUid() != worst) {
|
|
|
|
last = NULL;
|
2014-02-07 03:11:13 +01:00
|
|
|
++it;
|
2015-03-10 21:51:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pruneRows--;
|
|
|
|
if (pruneRows == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
kick = true;
|
2015-03-16 20:04:09 +01:00
|
|
|
|
|
|
|
unsigned short len = e->getMsgLen();
|
|
|
|
stats.drop(e);
|
|
|
|
e->setDropped(1);
|
|
|
|
// merge any drops
|
|
|
|
if (last
|
|
|
|
&& (last->getDropped() < (USHRT_MAX - 1))
|
|
|
|
&& (last->getPid() == pid)
|
|
|
|
&& (last->getTid() == e->getTid())) {
|
|
|
|
it = mLogElements.erase(it);
|
|
|
|
stats.erase(e);
|
|
|
|
delete e;
|
|
|
|
last->setDropped(last->getDropped() + 1);
|
|
|
|
} else {
|
|
|
|
last = e;
|
|
|
|
++it;
|
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
if (worst_sizes < second_worst_sizes) {
|
|
|
|
break;
|
2014-02-07 03:11:13 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
worst_sizes -= len;
|
2014-02-07 03:11:13 +01:00
|
|
|
}
|
|
|
|
|
2014-04-02 02:19:47 +02:00
|
|
|
if (!kick || !mPrune.worstUidEnabled()) {
|
2014-02-07 03:11:13 +01:00
|
|
|
break; // the following loop will ask bad clients to skip/drop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
bool whitelist = false;
|
2015-03-10 21:51:35 +01:00
|
|
|
bool hasWhitelist = mPrune.nice();
|
2014-02-07 03:11:13 +01:00
|
|
|
it = mLogElements.begin();
|
2014-02-26 18:50:16 +01:00
|
|
|
while((pruneRows > 0) && (it != mLogElements.end())) {
|
|
|
|
LogBufferElement *e = *it;
|
2015-03-10 21:51:35 +01:00
|
|
|
|
|
|
|
if (e->getLogId() != id) {
|
|
|
|
it++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldest && (oldest->mStart <= e->getSequence())) {
|
|
|
|
if (whitelist) {
|
2014-02-26 18:50:16 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-04-02 02:19:47 +02:00
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
if (stats.sizes(id) > (2 * log_buffer_size(id))) {
|
|
|
|
// kick a misbehaving log reader client off the island
|
|
|
|
oldest->release_Locked();
|
|
|
|
} else {
|
|
|
|
oldest->triggerSkip_Locked(id, pruneRows);
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-04-02 02:19:47 +02:00
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
if (hasWhitelist && mPrune.nice(e)) { // WhiteListed
|
|
|
|
whitelist = true;
|
2014-02-26 18:50:16 +01:00
|
|
|
it++;
|
2015-03-10 21:51:35 +01:00
|
|
|
continue;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
|
|
|
|
it = erase(it);
|
|
|
|
pruneRows--;
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
// Do not save the whitelist if we are reader range limited
|
2014-02-11 21:29:31 +01:00
|
|
|
if (whitelist && (pruneRows > 0)) {
|
|
|
|
it = mLogElements.begin();
|
|
|
|
while((it != mLogElements.end()) && (pruneRows > 0)) {
|
|
|
|
LogBufferElement *e = *it;
|
2015-03-10 21:51:35 +01:00
|
|
|
|
|
|
|
if (e->getLogId() != id) {
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldest && (oldest->mStart <= e->getSequence())) {
|
|
|
|
if (stats.sizes(id) > (2 * log_buffer_size(id))) {
|
|
|
|
// kick a misbehaving log reader client off the island
|
|
|
|
oldest->release_Locked();
|
|
|
|
} else {
|
|
|
|
oldest->triggerSkip_Locked(id, pruneRows);
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
break;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
2015-03-10 21:51:35 +01:00
|
|
|
|
|
|
|
it = erase(it);
|
|
|
|
pruneRows--;
|
2014-02-11 21:29:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 18:50:16 +01:00
|
|
|
LogTimeEntry::unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear all rows of type "id" from the buffer.
|
2014-06-12 20:16:16 +02:00
|
|
|
void LogBuffer::clear(log_id_t id, uid_t uid) {
|
2014-02-26 18:50:16 +01:00
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
2014-06-12 20:16:16 +02:00
|
|
|
prune(id, ULONG_MAX, uid);
|
2014-02-26 18:50:16 +01:00
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the used space associated with "id".
|
|
|
|
unsigned long LogBuffer::getSizeUsed(log_id_t id) {
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
2014-02-06 23:48:50 +01:00
|
|
|
size_t retval = stats.sizes(id);
|
2014-02-26 18:50:16 +01:00
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
// set the total space allocated to "id"
|
|
|
|
int LogBuffer::setSize(log_id_t id, unsigned long size) {
|
|
|
|
// Reasonable limits ...
|
2014-05-10 02:44:18 +02:00
|
|
|
if (!valid_size(size)) {
|
2014-02-11 21:29:31 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
log_buffer_size(id) = size;
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the total space allocated to "id"
|
|
|
|
unsigned long LogBuffer::getSize(log_id_t id) {
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
size_t retval = log_buffer_size(id);
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
uint64_t LogBuffer::flushTo(
|
|
|
|
SocketClient *reader, const uint64_t start, bool privileged,
|
|
|
|
int (*filter)(const LogBufferElement *element, void *arg), void *arg) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogBufferElementCollection::iterator it;
|
2015-03-03 22:39:37 +01:00
|
|
|
uint64_t max = start;
|
2014-02-26 18:50:16 +01:00
|
|
|
uid_t uid = reader->getUid();
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
2015-01-15 16:29:43 +01:00
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
if (start <= 1) {
|
2015-01-15 16:29:43 +01:00
|
|
|
// client wants to start from the beginning
|
|
|
|
it = mLogElements.begin();
|
|
|
|
} else {
|
|
|
|
// Client wants to start from some specified time. Chances are
|
|
|
|
// we are better off starting from the end of the time sorted list.
|
|
|
|
for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
|
|
|
|
--it;
|
|
|
|
LogBufferElement *element = *it;
|
2015-03-03 22:39:37 +01:00
|
|
|
if (element->getSequence() <= start) {
|
2015-01-15 16:29:43 +01:00
|
|
|
it++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; it != mLogElements.end(); ++it) {
|
2014-02-26 18:50:16 +01:00
|
|
|
LogBufferElement *element = *it;
|
|
|
|
|
|
|
|
if (!privileged && (element->getUid() != uid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-03 22:39:37 +01:00
|
|
|
if (element->getSequence() <= start) {
|
2014-02-26 18:50:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: calling out to another object with mLogElementsLock held (safe)
|
2015-03-03 22:39:37 +01:00
|
|
|
if (filter) {
|
|
|
|
int ret = (*filter)(element, arg);
|
|
|
|
if (ret == false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ret != true) {
|
|
|
|
break;
|
|
|
|
}
|
2014-02-26 18:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
|
|
|
|
// range locking in LastLogTimes looks after us
|
|
|
|
max = element->flushTo(reader);
|
|
|
|
|
|
|
|
if (max == element->FLUSH_ERROR) {
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|
2014-02-06 23:48:50 +01:00
|
|
|
|
2014-02-11 21:29:31 +01:00
|
|
|
void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) {
|
2014-02-06 23:48:50 +01:00
|
|
|
pthread_mutex_lock(&mLogElementsLock);
|
|
|
|
|
2015-03-10 21:51:35 +01:00
|
|
|
stats.format(strp, uid, logMask);
|
2014-02-06 23:48:50 +01:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&mLogElementsLock);
|
|
|
|
}
|