diff options
author | Mark Salyzyn <salyzyn@google.com> | 2015-12-11 16:42:28 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-12-11 16:42:28 +0000 |
commit | 30edbdce9bb5e76e02f5bdb1ee33cea2b62c7ceb (patch) | |
tree | 84533ea4b14ea779691baee23ded69313c5e4d99 | |
parent | ad9dac1c2795ed28f3a011182f176d852ee269ee (diff) | |
parent | b6a8d41e2a2379c4ee489121b1fabddfaab00fee (diff) | |
download | core-30edbdce9bb5e76e02f5bdb1ee33cea2b62c7ceb.tar.gz core-30edbdce9bb5e76e02f5bdb1ee33cea2b62c7ceb.tar.bz2 core-30edbdce9bb5e76e02f5bdb1ee33cea2b62c7ceb.zip |
Merge changes I598c8c5c,I672b0f4d,Id882978b,Idff5e080,Ib124eca1, ...
* changes:
logger.h: reduce maximum payload so sum is page size
liblog: test for maximum payload can not survive change
liblog: logprint use uid name if length less then 5
liblog: logprint add uid format modifier
liblog: readlog apps get logger_entry_v4 for pstore
logd: readlog apps get logger_entry_v4
logger.h: Add definition for logger_entry_v4
-rw-r--r-- | include/log/logger.h | 17 | ||||
-rw-r--r-- | include/log/logprint.h | 2 | ||||
-rw-r--r-- | liblog/log_read.c | 27 | ||||
-rw-r--r-- | liblog/logprint.c | 62 | ||||
-rw-r--r-- | liblog/tests/liblog_test.cpp | 15 | ||||
-rw-r--r-- | logcat/logcat.cpp | 2 | ||||
-rw-r--r-- | logd/LogBuffer.cpp | 2 | ||||
-rw-r--r-- | logd/LogBufferElement.cpp | 14 | ||||
-rw-r--r-- | logd/LogBufferElement.h | 2 |
9 files changed, 108 insertions, 35 deletions
diff --git a/include/log/logger.h b/include/log/logger.h index b3928a713..60d47a2da 100644 --- a/include/log/logger.h +++ b/include/log/logger.h @@ -64,12 +64,24 @@ struct logger_entry_v3 { char msg[0]; /* the entry's payload */ } __attribute__((__packed__)); +struct logger_entry_v4 { + uint16_t len; /* length of the payload */ + uint16_t hdr_size; /* sizeof(struct logger_entry_v4) */ + int32_t pid; /* generating process's pid */ + uint32_t tid; /* generating process's tid */ + uint32_t sec; /* seconds since Epoch */ + uint32_t nsec; /* nanoseconds */ + uint32_t lid; /* log id of the payload, bottom 4 bits currently */ + uint32_t uid; /* generating process's uid */ + char msg[0]; /* the entry's payload */ +} __attribute__((__packed__)); + /* * The maximum size of the log entry payload that can be * written to the logger. An attempt to write more than * this amount will result in a truncated log entry. */ -#define LOGGER_ENTRY_MAX_PAYLOAD 4076 +#define LOGGER_ENTRY_MAX_PAYLOAD 4068 /* * The maximum size of a log entry which can be read from the @@ -83,7 +95,8 @@ struct logger_entry_v3 { struct log_msg { union { unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1]; - struct logger_entry_v3 entry; + struct logger_entry_v4 entry; + struct logger_entry_v4 entry_v4; struct logger_entry_v3 entry_v3; struct logger_entry_v2 entry_v2; struct logger_entry entry_v1; diff --git a/include/log/logprint.h b/include/log/logprint.h index 204b3f224..539d1dcff 100644 --- a/include/log/logprint.h +++ b/include/log/logprint.h @@ -44,6 +44,7 @@ typedef enum { FORMAT_MODIFIER_ZONE, /* Adds zone to date */ FORMAT_MODIFIER_EPOCH, /* Print time as seconds since Jan 1 1970 */ FORMAT_MODIFIER_MONOTONIC, /* Print cpu time as seconds since start */ + FORMAT_MODIFIER_UID, /* Adds uid */ } AndroidLogPrintFormat; typedef struct AndroidLogFormat_t AndroidLogFormat; @@ -52,6 +53,7 @@ typedef struct AndroidLogEntry_t { time_t tv_sec; long tv_nsec; android_LogPriority priority; + int32_t uid; int32_t pid; int32_t tid; const char * tag; diff --git a/liblog/log_read.c b/liblog/log_read.c index fb8675779..4503219d3 100644 --- a/liblog/log_read.c +++ b/liblog/log_read.c @@ -634,6 +634,7 @@ static int android_logger_list_read_pstore(struct logger_list *logger_list, android_log_header_t l; } buf; static uint8_t preread_count; + bool is_system; memset(log_msg, 0, sizeof(*log_msg)); @@ -690,12 +691,15 @@ static int android_logger_list_read_pstore(struct logger_list *logger_list, } uid = get_best_effective_uid(); - if (!uid_has_log_permission(uid) && (uid != buf.p.uid)) { + is_system = uid_has_log_permission(uid); + if (!is_system && (uid != buf.p.uid)) { break; } ret = TEMP_FAILURE_RETRY(read(logger_list->sock, - log_msg->entry_v3.msg, + is_system ? + log_msg->entry_v4.msg : + log_msg->entry_v3.msg, buf.p.len - sizeof(buf))); if (ret < 0) { return -errno; @@ -704,13 +708,18 @@ static int android_logger_list_read_pstore(struct logger_list *logger_list, return -EIO; } - log_msg->entry_v3.len = buf.p.len - sizeof(buf); - log_msg->entry_v3.hdr_size = sizeof(log_msg->entry_v3); - log_msg->entry_v3.pid = buf.p.pid; - log_msg->entry_v3.tid = buf.l.tid; - log_msg->entry_v3.sec = buf.l.realtime.tv_sec; - log_msg->entry_v3.nsec = buf.l.realtime.tv_nsec; - log_msg->entry_v3.lid = buf.l.id; + log_msg->entry_v4.len = buf.p.len - sizeof(buf); + log_msg->entry_v4.hdr_size = is_system ? + sizeof(log_msg->entry_v4) : + sizeof(log_msg->entry_v3); + log_msg->entry_v4.pid = buf.p.pid; + log_msg->entry_v4.tid = buf.l.tid; + log_msg->entry_v4.sec = buf.l.realtime.tv_sec; + log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec; + log_msg->entry_v4.lid = buf.l.id; + if (is_system) { + log_msg->entry_v4.uid = buf.p.uid; + } return ret; } diff --git a/liblog/logprint.c b/liblog/logprint.c index 40e13f435..746eb8aa2 100644 --- a/liblog/logprint.c +++ b/liblog/logprint.c @@ -32,6 +32,7 @@ #include <cutils/list.h> #include <log/logd.h> #include <log/logprint.h> +#include <private/android_filesystem_config.h> #define MS_PER_NSEC 1000000 #define US_PER_NSEC 1000 @@ -56,6 +57,7 @@ struct AndroidLogFormat_t { bool zone_output; bool epoch_output; bool monotonic_output; + bool uid_output; }; /* @@ -204,6 +206,7 @@ AndroidLogFormat *android_log_format_new() p_ret->zone_output = false; p_ret->epoch_output = false; p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC; + p_ret->uid_output = false; return p_ret; } @@ -258,6 +261,9 @@ int android_log_setPrintFormat(AndroidLogFormat *p_format, case FORMAT_MODIFIER_MONOTONIC: p_format->monotonic_output = true; return 0; + case FORMAT_MODIFIER_UID: + p_format->uid_output = true; + return 0; default: break; } @@ -290,6 +296,7 @@ AndroidLogPrintFormat android_log_formatFromString(const char * formatString) else if (strcmp(formatString, "zone") == 0) format = FORMAT_MODIFIER_ZONE; else if (strcmp(formatString, "epoch") == 0) format = FORMAT_MODIFIER_EPOCH; else if (strcmp(formatString, "monotonic") == 0) format = FORMAT_MODIFIER_MONOTONIC; + else if (strcmp(formatString, "uid") == 0) format = FORMAT_MODIFIER_UID; else { extern char *tzname[2]; static const char gmt[] = "GMT"; @@ -451,6 +458,7 @@ int android_log_processLogBuffer(struct logger_entry *buf, { entry->tv_sec = buf->sec; entry->tv_nsec = buf->nsec; + entry->uid = -1; entry->pid = buf->pid; entry->tid = buf->tid; @@ -482,6 +490,9 @@ int android_log_processLogBuffer(struct logger_entry *buf, struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf; if (buf2->hdr_size) { msg = ((char *)buf2) + buf2->hdr_size; + if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) { + entry->uid = ((struct logger_entry_v4 *)buf)->uid; + } } for (i = 1; i < buf->len; i++) { if (msg[i] == '\0') { @@ -734,6 +745,7 @@ int android_log_processBinaryLogBuffer(struct logger_entry *buf, entry->tv_sec = buf->sec; entry->tv_nsec = buf->nsec; entry->priority = ANDROID_LOG_INFO; + entry->uid = -1; entry->pid = buf->pid; entry->tid = buf->tid; @@ -744,6 +756,9 @@ int android_log_processBinaryLogBuffer(struct logger_entry *buf, struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf; if (buf2->hdr_size) { eventData = ((unsigned char *)buf2) + buf2->hdr_size; + if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) { + entry->uid = ((struct logger_entry_v4 *)buf)->uid; + } } inCount = buf->len; if (inCount < 4) @@ -1238,7 +1253,7 @@ char *android_log_formatLogLine ( char prefixBuf[128], suffixBuf[128]; char priChar; int prefixSuffixIsHeaderFooter = 0; - char *ret = NULL; + char *ret; time_t now; unsigned long nsec; @@ -1310,6 +1325,30 @@ char *android_log_formatLogLine ( suffixLen = MIN(suffixLen, sizeof(suffixBuf)); } + char uid[16]; + uid[0] = '\0'; + if (p_format->uid_output) { + if (entry->uid >= 0) { + const struct android_id_info *info = android_ids; + size_t i; + + for (i = 0; i < android_id_count; ++i) { + if (info->aid == (unsigned int)entry->uid) { + break; + } + ++info; + } + if ((i < android_id_count) && (strlen(info->name) <= 5)) { + snprintf(uid, sizeof(uid), "%5s:", info->name); + } else { + // Not worth parsing package list, names all longer than 5 + snprintf(uid, sizeof(uid), "%5d:", entry->uid); + } + } else { + snprintf(uid, sizeof(uid), " "); + } + } + switch (p_format->format) { case FORMAT_TAG: len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, @@ -1322,11 +1361,11 @@ char *android_log_formatLogLine ( " (%s)\n", entry->tag); suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen); len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, - "%c(%5d) ", priChar, entry->pid); + "%c(%s%5d) ", priChar, uid, entry->pid); break; case FORMAT_THREAD: len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, - "%c(%5d:%5d) ", priChar, entry->pid, entry->tid); + "%c(%s%5d:%5d) ", priChar, uid, entry->pid, entry->tid); strcpy(suffixBuf + suffixLen, "\n"); ++suffixLen; break; @@ -1338,21 +1377,26 @@ char *android_log_formatLogLine ( break; case FORMAT_TIME: len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, - "%s %c/%-8s(%5d): ", timeBuf, priChar, entry->tag, entry->pid); + "%s %c/%-8s(%s%5d): ", timeBuf, priChar, entry->tag, + uid, entry->pid); strcpy(suffixBuf + suffixLen, "\n"); ++suffixLen; break; case FORMAT_THREADTIME: + ret = strchr(uid, ':'); + if (ret) { + *ret = ' '; + } len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, - "%s %5d %5d %c %-8s: ", timeBuf, - entry->pid, entry->tid, priChar, entry->tag); + "%s %s%5d %5d %c %-8s: ", timeBuf, + uid, entry->pid, entry->tid, priChar, entry->tag); strcpy(suffixBuf + suffixLen, "\n"); ++suffixLen; break; case FORMAT_LONG: len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, - "[ %s %5d:%5d %c/%-8s ]\n", - timeBuf, entry->pid, entry->tid, priChar, entry->tag); + "[ %s %s%5d:%5d %c/%-8s ]\n", + timeBuf, uid, entry->pid, entry->tid, priChar, entry->tag); strcpy(suffixBuf + suffixLen, "\n\n"); suffixLen += 2; prefixSuffixIsHeaderFooter = 1; @@ -1360,7 +1404,7 @@ char *android_log_formatLogLine ( case FORMAT_BRIEF: default: len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, - "%c/%-8s(%5d): ", priChar, entry->tag, entry->pid); + "%c/%-8s(%s%5d): ", priChar, entry->tag, uid, entry->pid); strcpy(suffixBuf + suffixLen, "\n"); ++suffixLen; break; diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp index 597d8f694..c2bd82dd1 100644 --- a/liblog/tests/liblog_test.cpp +++ b/liblog/tests/liblog_test.cpp @@ -342,8 +342,9 @@ TEST(liblog, android_logger_list_read__cpu) { } static const char max_payload_tag[] = "TEST_max_payload_XXXX"; -static const char max_payload_buf[LOGGER_ENTRY_MAX_PAYLOAD - - sizeof(max_payload_tag) - 1] = "LEONATO\n\ +#define SIZEOF_MAX_PAYLOAD_BUF (LOGGER_ENTRY_MAX_PAYLOAD - \ + sizeof(max_payload_tag) - 1) +static const char max_payload_buf[] = "LEONATO\n\ I learn in this letter that Don Peter of Arragon\n\ comes this night to Messina\n\ MESSENGER\n\ @@ -469,7 +470,7 @@ Good Signior Leonato, you are come to meet your\n\ trouble: the fashion of the world is to avoid\n\ cost, and you encounter it\n\ LEONATO\n\ -Never came trouble to my house in the likeness"; +Never came trouble to my house in the likeness of your grace"; TEST(liblog, max_payload) { pid_t pid = getpid(); @@ -528,7 +529,7 @@ TEST(liblog, max_payload) { EXPECT_EQ(true, matches); - EXPECT_LE(sizeof(max_payload_buf), static_cast<size_t>(max_len)); + EXPECT_LE(SIZEOF_MAX_PAYLOAD_BUF, static_cast<size_t>(max_len)); } TEST(liblog, too_big_payload) { @@ -1005,7 +1006,7 @@ TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__data_too_l const int TAG = 123456782; const char SUBTAG[] = "test-subtag"; const int UID = -1; - const int DATA_LEN = sizeof(max_payload_buf); + const int DATA_LEN = SIZEOF_MAX_PAYLOAD_BUF; struct logger_list *logger_list; pid_t pid = getpid(); @@ -1076,8 +1077,8 @@ TEST(liblog, android_errorWriteWithInfoLog__android_logger_list_read__data_too_l } eventData += dataLen; - // 4 bytes for the tag, and 512 bytes for the log since the max_payload_buf should be - // truncated. + // 4 bytes for the tag, and 512 bytes for the log since the + // max_payload_buf should be truncated. ASSERT_EQ(4 + 512, eventData - original); ++count; diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp index 059916e78..32bfd9c2c 100644 --- a/logcat/logcat.cpp +++ b/logcat/logcat.cpp @@ -265,7 +265,7 @@ static void show_help(const char *cmd) " -v <format> Sets the log print format, where <format> is:\n" " --format=<format>\n" " brief color epoch long monotonic printable process raw\n" - " tag thread threadtime time usec UTC year zone\n\n" + " tag thread threadtime time uid usec UTC year zone\n\n" " -D print dividers between each log buffer\n" " --dividers\n" " -c clear (flush) the entire log and exit\n" diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp index 6770bb7f2..45b9861bf 100644 --- a/logd/LogBuffer.cpp +++ b/logd/LogBuffer.cpp @@ -846,7 +846,7 @@ uint64_t LogBuffer::flushTo( pthread_mutex_unlock(&mLogElementsLock); // range locking in LastLogTimes looks after us - max = element->flushTo(reader, this); + max = element->flushTo(reader, this, privileged); if (max == element->FLUSH_ERROR) { return max; diff --git a/logd/LogBufferElement.cpp b/logd/LogBufferElement.cpp index c4c302b0a..e1d367f52 100644 --- a/logd/LogBufferElement.cpp +++ b/logd/LogBufferElement.cpp @@ -194,21 +194,25 @@ size_t LogBufferElement::populateDroppedMessage(char *&buffer, return retval; } -uint64_t LogBufferElement::flushTo(SocketClient *reader, LogBuffer *parent) { - struct logger_entry_v3 entry; +uint64_t LogBufferElement::flushTo(SocketClient *reader, LogBuffer *parent, + bool privileged) { + struct logger_entry_v4 entry; - memset(&entry, 0, sizeof(struct logger_entry_v3)); + memset(&entry, 0, sizeof(struct logger_entry_v4)); - entry.hdr_size = sizeof(struct logger_entry_v3); + entry.hdr_size = privileged ? + sizeof(struct logger_entry_v4) : + sizeof(struct logger_entry_v3); entry.lid = mLogId; entry.pid = mPid; entry.tid = mTid; + entry.uid = mUid; entry.sec = mRealTime.tv_sec; entry.nsec = mRealTime.tv_nsec; struct iovec iovec[2]; iovec[0].iov_base = &entry; - iovec[0].iov_len = sizeof(struct logger_entry_v3); + iovec[0].iov_len = entry.hdr_size; char *buffer = NULL; diff --git a/logd/LogBufferElement.h b/logd/LogBufferElement.h index 09987ea75..e7f88b94d 100644 --- a/logd/LogBufferElement.h +++ b/logd/LogBufferElement.h @@ -80,7 +80,7 @@ public: uint32_t getTag(void) const; static const uint64_t FLUSH_ERROR; - uint64_t flushTo(SocketClient *writer, LogBuffer *parent); + uint64_t flushTo(SocketClient *writer, LogBuffer *parent, bool privileged); }; #endif |