From 327cac6e0c9f09a9fe163c85530d749876dd9db7 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Fri, 19 Sep 2014 11:59:42 -0700 Subject: [PATCH 1/6] logd: auditd: kmsg priority (cherry pick from commit 6bdeee0ce6898abd3873a758c47601efcdcc1b7c) Change-Id: I2016fe140e2daf6c69efbd10aef205fffb931aa1 --- logd/LogAudit.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp index f8d61621c..998817882 100644 --- a/logd/LogAudit.cpp +++ b/logd/LogAudit.cpp @@ -31,6 +31,8 @@ LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmsg) , logbuf(buf) , reader(reader) , fdDmesg(-1) { + static const char auditd_message[] = "<6>logd.auditd: start\n"; + write(fdDmsg, auditd_message, sizeof(auditd_message)); logDmesg(); fdDmesg = fdDmsg; } @@ -75,13 +77,17 @@ int LogAudit::logPrint(const char *fmt, ...) { memmove(cp, cp + 1, strlen(cp + 1) + 1); } + bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded "); if (fdDmesg >= 0) { - struct iovec iov[2]; + struct iovec iov[3]; - iov[0].iov_base = str; - iov[0].iov_len = strlen(str); - iov[1].iov_base = const_cast("\n"); - iov[1].iov_len = 1; + iov[0].iov_base = info ? const_cast("<6>") + : const_cast("<4>"); + iov[0].iov_len = 3; + iov[1].iov_base = str; + iov[1].iov_len = strlen(str); + iov[2].iov_base = const_cast("\n"); + iov[2].iov_len = 1; writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0])); } @@ -175,10 +181,7 @@ int LogAudit::logPrint(const char *fmt, ...) { if (!newstr) { rc = -ENOMEM; } else { - *newstr = (strstr(str, " permissive=1") - || strstr(str, " policy loaded ")) - ? ANDROID_LOG_INFO - : ANDROID_LOG_WARN; + *newstr = info ? ANDROID_LOG_INFO : ANDROID_LOG_WARN; strlcpy(newstr + 1, comm, l); strncpy(newstr + 1 + l, str, estr - str); strcpy(newstr + 1 + l + (estr - str), ecomm); From 823a4c727a31ead7c137fed6a62b4a29810b9fbd Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Sun, 28 Sep 2014 14:41:50 -0700 Subject: [PATCH 2/6] logd: auditd: report facility LOG_AUTH (cherry pick from commit 7ee2aef8e0fd8aaa601c8c17e5429fa65b22e00d) Change-Id: Ie325e1b58f52b6c728d5cfd6f6b87287fcf32e10 --- logd/LogAudit.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp index 998817882..51feff396 100644 --- a/logd/LogAudit.cpp +++ b/logd/LogAudit.cpp @@ -22,16 +22,25 @@ #include #include #include +#include #include "libaudit.h" #include "LogAudit.h" +#define KMSG_PRIORITY(PRI) \ + '<', \ + '0' + (LOG_AUTH | (PRI)) / 10, \ + '0' + (LOG_AUTH | (PRI)) % 10, \ + '>' + LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmsg) : SocketListener(getLogSocket(), false) , logbuf(buf) , reader(reader) , fdDmesg(-1) { - static const char auditd_message[] = "<6>logd.auditd: start\n"; + static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO), + 'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':', + ' ', 's', 't', 'a', 'r', 't', '\n' }; write(fdDmsg, auditd_message, sizeof(auditd_message)); logDmesg(); fdDmesg = fdDmsg; @@ -80,10 +89,12 @@ int LogAudit::logPrint(const char *fmt, ...) { bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded "); if (fdDmesg >= 0) { struct iovec iov[3]; + static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) }; + static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) }; - iov[0].iov_base = info ? const_cast("<6>") - : const_cast("<4>"); - iov[0].iov_len = 3; + iov[0].iov_base = info ? const_cast(log_info) + : const_cast(log_warning); + iov[0].iov_len = info ? sizeof(log_info) : sizeof(log_warning); iov[1].iov_base = str; iov[1].iov_len = strlen(str); iov[2].iov_base = const_cast("\n"); From 987f1e0de311cf863bde30bb4de19f9cd7f08642 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Sun, 21 Sep 2014 14:22:18 -0700 Subject: [PATCH 3/6] logd: cleanup - simplify access and control exposure to class list - indent - compile warning - Follow standard naming convention for variables and methods - merge common fragments - Side Effects: none (cherry pick from commit e72c6e43668c8c6e1af77e2e5038557581cbf148) Bug: 17526159 Change-Id: I74796043ac34753c6dd10018719ebc0bcd94e010 --- logd/LogBuffer.cpp | 33 ++++++++++------------ logd/LogBuffer.h | 5 ++-- logd/LogStatistics.cpp | 62 +++++++++++++++++++++--------------------- logd/LogStatistics.h | 12 ++++++-- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp index cd9ea2097..8c1c3447a 100644 --- a/logd/LogBuffer.cpp +++ b/logd/LogBuffer.cpp @@ -93,9 +93,9 @@ static unsigned long property_get_size(const char *key) { } LogBuffer::LogBuffer(LastLogTimes *times) - : mTimes(*times) { + : dgramQlenStatistics(false) + , mTimes(*times) { pthread_mutex_init(&mLogElementsLock, NULL); - dgram_qlen_statistics = false; static const char global_tuneable[] = "persist.logd.size"; // Settings App static const char global_default[] = "ro.logd.size"; // BoardConfig.mk @@ -150,10 +150,10 @@ void LogBuffer::log(log_id_t log_id, log_time realtime, while (--it != mLogElements.begin()) { if ((*it)->getRealTime() <= realtime) { // halves the peak performance, use with caution - if (dgram_qlen_statistics) { + if (dgramQlenStatistics) { LogBufferElementCollection::iterator ib = it; unsigned short buckets, num = 1; - for (unsigned short i = 0; (buckets = stats.dgram_qlen(i)); ++i) { + for (unsigned short i = 0; (buckets = stats.dgramQlen(i)); ++i) { buckets -= num; num += buckets; while (buckets && (--ib != mLogElements.begin())) { @@ -267,8 +267,7 @@ void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { if (uid == caller_uid) { it = mLogElements.erase(it); - unsigned short len = e->getMsgLen(); - stats.subtract(len, id, uid, e->getPid()); + stats.subtract(e->getMsgLen(), id, uid, e->getPid()); delete e; pruneRows--; if (pruneRows == 0) { @@ -318,23 +317,19 @@ void LogBuffer::prune(log_id_t id, unsigned long pruneRows, uid_t caller_uid) { uid_t uid = e->getUid(); - if (uid == worst) { + if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed it = mLogElements.erase(it); unsigned short len = e->getMsgLen(); - stats.subtract(len, id, worst, e->getPid()); - delete e; - kick = true; - pruneRows--; - if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) { - break; - } - worst_sizes -= len; - } else if (mPrune.naughty(e)) { // BlackListed - it = mLogElements.erase(it); - stats.subtract(e->getMsgLen(), id, uid, e->getPid()); + stats.subtract(len, id, uid, e->getPid()); delete e; pruneRows--; - if (pruneRows == 0) { + if (uid == worst) { + kick = true; + if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) { + break; + } + worst_sizes -= len; + } else if (pruneRows == 0) { break; } } else { diff --git a/logd/LogBuffer.h b/logd/LogBuffer.h index 4b982a878..91175e0c0 100644 --- a/logd/LogBuffer.h +++ b/logd/LogBuffer.h @@ -37,8 +37,7 @@ class LogBuffer { pthread_mutex_t mLogElementsLock; LogStatistics stats; - - bool dgram_qlen_statistics; + bool dgramQlenStatistics; PruneList mPrune; @@ -66,7 +65,7 @@ public: void enableDgramQlenStatistics() { stats.enableDgramQlenStatistics(); - dgram_qlen_statistics = true; + dgramQlenStatistics = true; } int initPrune(char *cp) { return mPrune.init(cp); } diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp index a2f27c641..ceafab507 100644 --- a/logd/LogStatistics.cpp +++ b/logd/LogStatistics.cpp @@ -51,10 +51,7 @@ PidStatistics::~PidStatistics() { } bool PidStatistics::pidGone() { - if (mGone) { - return true; - } - if (pid == gone) { + if (mGone || (pid == gone)) { return true; } if (kill(pid, 0) && (errno != EPERM)) { @@ -90,6 +87,9 @@ void PidStatistics::addTotal(size_t size, size_t element) { } // must call free to release return value +// If only we could sniff our own logs for: +//