diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp index 4ec2e59f2..4b3547ce1 100644 --- a/logd/LogAudit.cpp +++ b/logd/LogAudit.cpp @@ -145,7 +145,9 @@ int LogAudit::logPrint(const char *fmt, ...) { ++cp; } tid = pid; + logbuf->lock(); uid = logbuf->pidToUid(pid); + logbuf->unlock(); memmove(pidptr, cp, strlen(cp) + 1); } @@ -180,14 +182,20 @@ int LogAudit::logPrint(const char *fmt, ...) { static const char comm_str[] = " comm=\""; const char *comm = strstr(str, comm_str); const char *estr = str + strlen(str); + char *commfree = NULL; if (comm) { estr = comm; comm += sizeof(comm_str) - 1; } else if (pid == getpid()) { pid = tid; comm = "auditd"; - } else if (!(comm = logbuf->pidToName(pid))) { - comm = "unknown"; + } else { + logbuf->lock(); + comm = commfree = logbuf->pidToName(pid); + logbuf->unlock(); + if (!comm) { + comm = "unknown"; + } } const char *ecomm = strchr(comm, '"'); @@ -218,6 +226,7 @@ int LogAudit::logPrint(const char *fmt, ...) { } } + free(commfree); free(str); if (notify) { diff --git a/logd/LogBuffer.h b/logd/LogBuffer.h index 00b19b67b..a13fded8b 100644 --- a/logd/LogBuffer.h +++ b/logd/LogBuffer.h @@ -71,10 +71,12 @@ public: // *strp uses malloc, use free to release. void formatPrune(char **strp) { mPrune.format(strp); } - // helper + // helper must be protected directly or implicitly by lock()/unlock() char *pidToName(pid_t pid) { return stats.pidToName(pid); } uid_t pidToUid(pid_t pid) { return stats.pidToUid(pid); } char *uidToName(uid_t uid) { return stats.uidToName(uid); } + void lock() { pthread_mutex_lock(&mLogElementsLock); } + void unlock() { pthread_mutex_unlock(&mLogElementsLock); } private: void maybePrune(log_id_t id); diff --git a/logd/LogBufferElement.cpp b/logd/LogBufferElement.cpp index 3f5fdcef4..9fb143952 100644 --- a/logd/LogBufferElement.cpp +++ b/logd/LogBufferElement.cpp @@ -111,13 +111,17 @@ size_t LogBufferElement::populateDroppedMessage(char *&buffer, } static const char format_uid[] = "uid=%u%s%s expire %u line%s"; + parent->lock(); char *name = parent->uidToName(mUid); + parent->unlock(); char *commName = android::tidToName(mTid); if (!commName && (mTid != mPid)) { commName = android::tidToName(mPid); } if (!commName) { + parent->lock(); commName = parent->pidToName(mPid); + parent->unlock(); } size_t len = name ? strlen(name) : 0; if (len && commName && !strncmp(name, commName, len)) { diff --git a/logd/LogStatistics.h b/logd/LogStatistics.h index b9e9650a4..760d6b20e 100644 --- a/logd/LogStatistics.h +++ b/logd/LogStatistics.h @@ -334,7 +334,7 @@ public: // *strp = malloc, balance with free void format(char **strp, uid_t uid, unsigned int logMask); - // helper + // helper (must be locked directly or implicitly by mLogElementsLock) char *pidToName(pid_t pid); uid_t pidToUid(pid_t pid); char *uidToName(uid_t uid);