From 8a5b9caaa6eaf31cce2b5842d667e38b01ffdfae Mon Sep 17 00:00:00 2001 From: William Roberts Date: Fri, 8 Apr 2016 12:13:17 -0700 Subject: [PATCH] liblog: remove android_ids and replace with getpwuid Note: This code makes the assumption that getpwuid is thread safe, which it is ONLY ON BIONIC. Thus, if you attempt to use this on a non-target build, you may get burned. Thus, an ifndef checking on __BIONIC__ is used to produce a build error if you attempt to do so. Change-Id: I61038c428b71771edcfc76f18d8fc5cbe349238b Bug: 27999086 Signed-off-by: William Roberts --- liblog/logprint.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/liblog/logprint.c b/liblog/logprint.c index 9b60d4aeb..88afc147c 100644 --- a/liblog/logprint.c +++ b/liblog/logprint.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -28,11 +29,11 @@ #include #include #include +#include #include #include #include -#include #include "log_portability.h" @@ -1352,17 +1353,17 @@ LIBLOG_ABI_PUBLIC char *android_log_formatLogLine ( 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); + /* + * This code is Android specific, bionic guarantees that + * calls to non-reentrant getpwuid() are thread safe. + */ +#ifndef __BIONIC__ +#warning "This code assumes that getpwuid is thread safe, only true with Bionic!" +#endif + struct passwd* pwd = getpwuid(entry->uid); + if (pwd && (strlen(pwd->pw_name) <= 5)) { + snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name); } else { // Not worth parsing package list, names all longer than 5 snprintf(uid, sizeof(uid), "%5d:", entry->uid);