Merge "GateKeeper proxy service"
diff --git a/base/include/base/memory.h b/base/include/base/memory.h
new file mode 100644
index 0000000..882582f
--- /dev/null
+++ b/base/include/base/memory.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#ifndef BASE_MEMORY_H
+#define BASE_MEMORY_H
+
+namespace android {
+namespace base {
+
+// Use packed structures for access to unaligned data on targets with alignment
+// restrictions.  The compiler will generate appropriate code to access these
+// structures without generating alignment exceptions.
+template <typename T>
+static inline T get_unaligned(const T* address) {
+  struct unaligned {
+    T v;
+  } __attribute__((packed));
+  const unaligned* p = reinterpret_cast<const unaligned*>(address);
+  return p->v;
+}
+
+template <typename T>
+static inline void put_unaligned(T* address, T v) {
+  struct unaligned {
+    T v;
+  } __attribute__((packed));
+  unaligned* p = reinterpret_cast<unaligned*>(address);
+  p->v = v;
+}
+
+} // namespace base
+} // namespace android
+
+#endif // BASE_MEMORY_H
diff --git a/fs_mgr/Android.mk b/fs_mgr/Android.mk
index 1179eaa..08d0671 100644
--- a/fs_mgr/Android.mk
+++ b/fs_mgr/Android.mk
@@ -8,8 +8,8 @@
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
 
 LOCAL_MODULE:= libfs_mgr
-LOCAL_STATIC_LIBRARIES := liblogwrap libmincrypt libext4_utils_static
-LOCAL_C_INCLUDES += system/extras/ext4_utils
+LOCAL_STATIC_LIBRARIES := liblogwrap libmincrypt libext4_utils_static libsquashfs_utils
+LOCAL_C_INCLUDES += system/extras/ext4_utils system/extras/squashfs_utils
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 LOCAL_CFLAGS := -Werror
 
@@ -34,7 +34,7 @@
 LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)/sbin
 LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
 
-LOCAL_STATIC_LIBRARIES := libfs_mgr liblogwrap libcutils liblog libc libmincrypt libext4_utils_static
+LOCAL_STATIC_LIBRARIES := libfs_mgr liblogwrap libcutils liblog libc libmincrypt libext4_utils_static libsquashfs_utils
 LOCAL_CXX_STL := libc++_static
 
 LOCAL_CFLAGS := -Werror
diff --git a/fs_mgr/fs_mgr_verity.c b/fs_mgr/fs_mgr_verity.c
index c55a49f..6ef46ba 100644
--- a/fs_mgr/fs_mgr_verity.c
+++ b/fs_mgr/fs_mgr_verity.c
@@ -38,6 +38,7 @@
 #include "mincrypt/sha256.h"
 
 #include "ext4_sb.h"
+#include "squashfs_utils.h"
 
 #include "fs_mgr_priv.h"
 #include "fs_mgr_priv_verity.h"
@@ -140,7 +141,19 @@
     return retval;
 }
 
-static int get_target_device_size(char *blk_device, uint64_t *device_size)
+static int squashfs_get_target_device_size(char *blk_device, uint64_t *device_size)
+{
+    struct squashfs_info sq_info;
+
+    if (squashfs_parse_sb(blk_device, &sq_info) >= 0) {
+        *device_size = sq_info.bytes_used_4K_padded;
+        return 0;
+    } else {
+        return -1;
+    }
+}
+
+static int ext4_get_target_device_size(char *blk_device, uint64_t *device_size)
 {
     int data_device;
     struct ext4_super_block sb;
@@ -173,11 +186,29 @@
     return 0;
 }
 
-static int read_verity_metadata(char *block_device, char **signature, char **table)
+static int get_fs_size(char *fs_type, char *blk_device, uint64_t *device_size) {
+    if (!strcmp(fs_type, "ext4")) {
+        if (ext4_get_target_device_size(blk_device, device_size) < 0) {
+            ERROR("Failed to get ext4 fs size on %s.", blk_device);
+            return -1;
+        }
+    } else if (!strcmp(fs_type, "squashfs")) {
+        if (squashfs_get_target_device_size(blk_device, device_size) < 0) {
+            ERROR("Failed to get squashfs fs size on %s.", blk_device);
+            return -1;
+        }
+    } else {
+        ERROR("%s: Unsupported filesystem for verity.", fs_type);
+        return -1;
+    }
+    return 0;
+}
+
+static int read_verity_metadata(uint64_t device_size, char *block_device, char **signature,
+        char **table)
 {
     unsigned magic_number;
     unsigned table_length;
-    uint64_t device_length;
     int protocol_version;
     int device;
     int retval = FS_MGR_SETUP_VERITY_FAIL;
@@ -194,12 +225,7 @@
         goto out;
     }
 
-    // find the start of the verity metadata
-    if (get_target_device_size(block_device, &device_length) < 0) {
-        ERROR("Could not get target device size.\n");
-        goto out;
-    }
-    if (TEMP_FAILURE_RETRY(lseek64(device, device_length, SEEK_SET)) < 0) {
+    if (TEMP_FAILURE_RETRY(lseek64(device, device_size, SEEK_SET)) < 0) {
         ERROR("Could not seek to start of verity metadata block.\n");
         goto out;
     }
@@ -220,8 +246,7 @@
 #endif
 
     if (magic_number != VERITY_METADATA_MAGIC_NUMBER) {
-        ERROR("Couldn't find verity metadata at offset %"PRIu64"!\n",
-              device_length);
+        ERROR("Couldn't find verity metadata at offset %"PRIu64"!\n", device_size);
         goto out;
     }
 
@@ -330,17 +355,12 @@
     return 0;
 }
 
-static int load_verity_table(struct dm_ioctl *io, char *name, char *blockdev, int fd, char *table,
+static int load_verity_table(struct dm_ioctl *io, char *name, uint64_t device_size, int fd, char *table,
         int mode)
 {
     char *verity_params;
     char *buffer = (char*) io;
     size_t bufsize;
-    uint64_t device_size = 0;
-
-    if (get_target_device_size(blockdev, &device_size) < 0) {
-        return -1;
-    }
 
     verity_ioctl_init(io, name, DM_STATUS_TABLE_FLAG);
 
@@ -665,10 +685,17 @@
     uint8_t curr[SHA256_DIGEST_SIZE];
     uint8_t prev[SHA256_DIGEST_SIZE];
     off64_t offset = 0;
+    uint64_t device_size;
 
     *match = 1;
 
-    if (read_verity_metadata(fstab->blk_device, &signature, NULL) < 0) {
+    // get verity filesystem size
+    if (get_fs_size(fstab->fs_type, fstab->blk_device, &device_size) < 0) {
+        ERROR("Failed to get filesystem size\n");
+        goto out;
+    }
+
+    if (read_verity_metadata(device_size, fstab->blk_device, &signature, NULL) < 0) {
         ERROR("Failed to read verity signature from %s\n", fstab->mount_point);
         goto out;
     }
@@ -901,6 +928,7 @@
     char *verity_blk_name = 0;
     char *verity_table = 0;
     char *verity_table_signature = 0;
+    uint64_t device_size = 0;
 
     _Alignas(struct dm_ioctl) char buffer[DM_BUF_SIZE];
     struct dm_ioctl *io = (struct dm_ioctl *) buffer;
@@ -910,16 +938,15 @@
     io->flags |= 1;
     io->target_count = 1;
 
-    // check to ensure that the verity device is ext4
-    // TODO: support non-ext4 filesystems
-    if (strcmp(fstab->fs_type, "ext4")) {
-        ERROR("Cannot verify non-ext4 device (%s)", fstab->fs_type);
+    // get verity filesystem size
+    if (get_fs_size(fstab->fs_type, fstab->blk_device, &device_size) < 0) {
         return retval;
     }
 
     // read the verity block at the end of the block device
     // send error code up the chain so we can detect attempts to disable verity
-    retval = read_verity_metadata(fstab->blk_device,
+    retval = read_verity_metadata(device_size,
+                                  fstab->blk_device,
                                   &verity_table_signature,
                                   &verity_table);
     if (retval < 0) {
@@ -964,7 +991,7 @@
     INFO("Enabling dm-verity for %s (mode %d)\n",  mount_point, mode);
 
     // load the verity mapping table
-    if (load_verity_table(io, mount_point, fstab->blk_device, fd, verity_table,
+    if (load_verity_table(io, mount_point, device_size, fd, verity_table,
             mode) < 0) {
         goto out;
     }
diff --git a/init/Android.mk b/init/Android.mk
index 9d91a3f..4bd4f3d 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -58,6 +58,7 @@
 LOCAL_STATIC_LIBRARIES := \
     libinit \
     libfs_mgr \
+    libsquashfs_utils \
     liblogwrap \
     libcutils \
     libbase \
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index d11b129..a5844a3 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -16,7 +16,6 @@
 
 #include <ctype.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <sys/user.h>
 #include <time.h>
@@ -281,15 +280,14 @@
         size_t second_worst_sizes = 0;
 
         if ((id != LOG_ID_CRASH) && mPrune.worstUidEnabled()) {
-            const UidEntry **sorted = stats.sort(2, id);
+            std::unique_ptr<const UidEntry *[]> sorted = stats.sort(2, id);
 
-            if (sorted) {
+            if (sorted.get()) {
                 if (sorted[0] && sorted[1]) {
                     worst = sorted[0]->getKey();
                     worst_sizes = sorted[0]->getSizes();
                     second_worst_sizes = sorted[1]->getSizes();
                 }
-                delete [] sorted;
             }
         }
 
diff --git a/logd/LogBufferElement.h b/logd/LogBufferElement.h
index 0628d3e..93671da 100644
--- a/logd/LogBufferElement.h
+++ b/logd/LogBufferElement.h
@@ -24,6 +24,14 @@
 #include <log/log.h>
 #include <log/log_read.h>
 
+namespace android {
+
+// Furnished in main.cpp. Caller must own and free returned value
+// This function is designed for a single caller and is NOT thread-safe
+char *uidToName(uid_t uid);
+
+}
+
 class LogBufferElement {
     const log_id_t mLogId;
     const uid_t mUid;
diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp
index accd660..3fbcfd6 100644
--- a/logd/LogStatistics.cpp
+++ b/logd/LogStatistics.cpp
@@ -17,7 +17,6 @@
 #include <algorithm> // std::max
 #include <fcntl.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -27,7 +26,8 @@
 
 #include "LogStatistics.h"
 
-LogStatistics::LogStatistics() {
+LogStatistics::LogStatistics()
+        : enable(false) {
     log_id_for_each(id) {
         mSizes[id] = 0;
         mElements[id] = 0;
@@ -36,8 +36,10 @@
     }
 }
 
+namespace android {
+
 // caller must own and free character string
-char *LogStatistics::pidToName(pid_t pid) {
+static char *pidToName(pid_t pid) {
     char *retval = NULL;
     if (pid == 0) { // special case from auditd for kernel
         retval = strdup("logd.auditd");
@@ -60,6 +62,8 @@
     return retval;
 }
 
+}
+
 void LogStatistics::add(LogBufferElement *e) {
     log_id_t log_id = e->getLogId();
     unsigned short size = e->getMsgLen();
@@ -81,6 +85,31 @@
 
     mSizesTotal[log_id] += size;
     ++mElementsTotal[log_id];
+
+    if (!enable) {
+        return;
+    }
+
+    pid_t pid = e->getPid();
+    hash = android::hash_type(pid);
+    index = pidTable.find(-1, hash, pid);
+    if (index == -1) {
+        PidEntry initEntry(pid, uid, android::pidToName(pid));
+        initEntry.add(size);
+        pidTable.add(hash, initEntry);
+    } else {
+        PidEntry &entry = pidTable.editEntryAt(index);
+        if (entry.getUid() != uid) {
+            entry.setUid(uid);
+            entry.setName(android::pidToName(pid));
+        } else if (!entry.getName()) {
+            char *name = android::pidToName(pid);
+            if (name) {
+                entry.setName(name);
+            }
+        }
+        entry.add(size);
+    }
 }
 
 void LogStatistics::subtract(LogBufferElement *e) {
@@ -99,33 +128,20 @@
             table.removeAt(index);
         }
     }
-}
 
-// caller must own and delete UidEntry array
-const UidEntry **LogStatistics::sort(size_t n, log_id id) {
-    if (!n) {
-        return NULL;
+    if (!enable) {
+        return;
     }
 
-    const UidEntry **retval = new const UidEntry* [n];
-    memset(retval, 0, sizeof(*retval) * n);
-
-    uidTable_t &table = uidTable[id];
-    ssize_t index = -1;
-    while ((index = table.next(index)) >= 0) {
-        const UidEntry &entry = table.entryAt(index);
-        size_t s = entry.getSizes();
-        ssize_t i = n - 1;
-        while ((!retval[i] || (s > retval[i]->getSizes())) && (--i >= 0));
-        if (++i < (ssize_t)n) {
-            size_t b = n - i - 1;
-            if (b) {
-                memmove(&retval[i+1], &retval[i], b * sizeof(retval[0]));
-            }
-            retval[i] = &entry;
+    pid_t pid = e->getPid();
+    hash = android::hash_type(pid);
+    index = pidTable.find(-1, hash, pid);
+    if (index != -1) {
+        PidEntry &entry = pidTable.editEntryAt(index);
+        if (entry.subtract(size)) {
+            pidTable.removeAt(index);
         }
     }
-    return retval;
 }
 
 // caller must own and free character string
@@ -145,8 +161,33 @@
         ++info;
     }
 
+    // Parse /data/system/packages.list
+    char *name = android::uidToName(uid);
+    if (name) {
+        return name;
+    }
+
+    // report uid -> pid(s) -> pidToName if unique
+    ssize_t index = -1;
+    while ((index = pidTable.next(index)) != -1) {
+        const PidEntry &entry = pidTable.entryAt(index);
+
+        if (entry.getUid() == uid) {
+            const char *n = entry.getName();
+
+            if (n) {
+                if (!name) {
+                    name = strdup(n);
+                } else if (strcmp(name, n)) {
+                    free(name);
+                    return NULL;
+                }
+            }
+        }
+    }
+
     // No one
-    return NULL;
+    return name;
 }
 
 static void format_line(android::String8 &output,
@@ -223,37 +264,23 @@
     // Report on Chattiest
 
     // Chattiest by application (UID)
+    static const size_t maximum_sorted_entries = 32;
     log_id_for_each(id) {
         if (!(logMask & (1 << id))) {
             continue;
         }
 
-        static const size_t maximum_sorted_entries = 32;
-        const UidEntry **sorted = sort(maximum_sorted_entries, id);
-
-        if (!sorted) {
-            continue;
-        }
-
-        bool print = false;
-        for(size_t index = 0; index < maximum_sorted_entries; ++index) {
+        bool headerPrinted = false;
+        std::unique_ptr<const UidEntry *[]> sorted = sort(maximum_sorted_entries, id);
+        ssize_t index = -1;
+        while ((index = uidTable_t::next(index, sorted, maximum_sorted_entries)) >= 0) {
             const UidEntry *entry = sorted[index];
-
-            if (!entry) {
-                break;
-            }
-
-            size_t sizes = entry->getSizes();
-            if (sizes < (65536/100)) {
-                break;
-            }
-
             uid_t u = entry->getKey();
             if ((uid != AID_ROOT) && (u != uid)) {
                 continue;
             }
 
-            if (!print) {
+            if (!headerPrinted) {
                 if (uid == AID_ROOT) {
                     output.appendFormat(
                         "\n\nChattiest UIDs in %s:\n",
@@ -266,7 +293,7 @@
                         "\n\nLogging for your UID in %s:\n",
                         android_log_id_to_name(id));
                 }
-                print = true;
+                headerPrinted = true;
             }
 
             android::String8 name("");
@@ -278,18 +305,61 @@
             }
 
             android::String8 size("");
-            size.appendFormat("%zu", sizes);
+            size.appendFormat("%zu", entry->getSizes());
 
             format_line(output, name, size);
         }
+    }
 
-        delete [] sorted;
+    if (enable) {
+        bool headerPrinted = false;
+        std::unique_ptr<const PidEntry *[]> sorted = pidTable.sort(maximum_sorted_entries);
+        ssize_t index = -1;
+        while ((index = pidTable.next(index, sorted, maximum_sorted_entries)) >= 0) {
+            const PidEntry *entry = sorted[index];
+            uid_t u = entry->getUid();
+            if ((uid != AID_ROOT) && (u != uid)) {
+                continue;
+            }
+
+            if (!headerPrinted) {
+                if (uid == AID_ROOT) {
+                    output.appendFormat("\n\nChattiest PIDs:\n");
+                } else {
+                    output.appendFormat("\n\nLogging for this PID:\n");
+                }
+                android::String8 name("  PID/UID");
+                android::String8 size("Size");
+                format_line(output, name, size);
+                headerPrinted = true;
+            }
+
+            android::String8 name("");
+            name.appendFormat("%5u/%u", entry->getKey(), u);
+            const char *n = entry->getName();
+            if (n) {
+                name.appendFormat("%*s%s", (int)std::max(12 - name.length(), (size_t)1), "", n);
+            } else {
+                char *un = uidToName(u);
+                if (un) {
+                    name.appendFormat("%*s%s", (int)std::max(12 - name.length(), (size_t)1), "", un);
+                    free(un);
+                }
+            }
+
+            android::String8 size("");
+            size.appendFormat("%zu", entry->getSizes());
+
+            format_line(output, name, size);
+        }
     }
 
     *buf = strdup(output.string());
 }
 
-uid_t LogStatistics::pidToUid(pid_t pid) {
+namespace android {
+
+uid_t pidToUid(pid_t pid) {
     char buffer[512];
     snprintf(buffer, sizeof(buffer), "/proc/%u/status", pid);
     FILE *fp = fopen(buffer, "r");
@@ -305,3 +375,52 @@
     }
     return getuid(); // associate this with the logger
 }
+
+}
+
+uid_t LogStatistics::pidToUid(pid_t pid) {
+    uid_t uid;
+    android::hash_t hash = android::hash_type(pid);
+    ssize_t index = pidTable.find(-1, hash, pid);
+    if (index == -1) {
+        uid = android::pidToUid(pid);
+        PidEntry initEntry(pid, uid, android::pidToName(pid));
+        pidTable.add(hash, initEntry);
+    } else {
+        PidEntry &entry = pidTable.editEntryAt(index);
+        if (!entry.getName()) {
+            char *name = android::pidToName(pid);
+            if (name) {
+                entry.setName(name);
+            }
+        }
+        uid = entry.getUid();
+    }
+    return uid;
+}
+
+// caller must free character string
+char *LogStatistics::pidToName(pid_t pid) {
+    char *name;
+
+    android::hash_t hash = android::hash_type(pid);
+    ssize_t index = pidTable.find(-1, hash, pid);
+    if (index == -1) {
+        name = android::pidToName(pid);
+        PidEntry initEntry(pid, android::pidToUid(pid), name ? strdup(name) : NULL);
+        pidTable.add(hash, initEntry);
+    } else {
+        PidEntry &entry = pidTable.editEntryAt(index);
+        const char *n = entry.getName();
+        if (n) {
+            name = strdup(n);
+        } else {
+            name = android::pidToName(pid);
+            if (name) {
+                entry.setName(strdup(name));
+            }
+        }
+    }
+
+    return name;
+}
diff --git a/logd/LogStatistics.h b/logd/LogStatistics.h
index d5b8762..a65ffe0 100644
--- a/logd/LogStatistics.h
+++ b/logd/LogStatistics.h
@@ -17,6 +17,8 @@
 #ifndef _LOGD_LOG_STATISTICS_H__
 #define _LOGD_LOG_STATISTICS_H__
 
+#include <memory>
+#include <stdlib.h>
 #include <sys/types.h>
 
 #include <log/log.h>
@@ -27,6 +29,52 @@
 #define log_id_for_each(i) \
     for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1))
 
+template <typename TKey, typename TEntry>
+class LogHashtable : public android::BasicHashtable<TKey, TEntry> {
+public:
+    std::unique_ptr<const TEntry *[]> sort(size_t n) {
+        if (!n) {
+            std::unique_ptr<const TEntry *[]> sorted(NULL);
+            return sorted;
+        }
+
+        const TEntry **retval = new const TEntry* [n];
+        memset(retval, 0, sizeof(*retval) * n);
+
+        ssize_t index = -1;
+        while ((index = android::BasicHashtable<TKey, TEntry>::next(index)) >= 0) {
+            const TEntry &entry = android::BasicHashtable<TKey, TEntry>::entryAt(index);
+            size_t s = entry.getSizes();
+            ssize_t i = n - 1;
+            while ((!retval[i] || (s > retval[i]->getSizes())) && (--i >= 0))
+                ;
+            if (++i < (ssize_t)n) {
+                size_t b = n - i - 1;
+                if (b) {
+                    memmove(&retval[i+1], &retval[i], b * sizeof(retval[0]));
+                }
+                retval[i] = &entry;
+            }
+        }
+        std::unique_ptr<const TEntry *[]> sorted(retval);
+        return sorted;
+    }
+
+    // Iteration handler for the sort method output
+    static ssize_t next(ssize_t index, std::unique_ptr<const TEntry *[]> &sorted, size_t n) {
+        ++index;
+        if (!sorted.get() || (index < 0) || (n <= (size_t)index) || !sorted[index]
+         || (sorted[index]->getSizes() <= (sorted[0]->getSizes() / 100))) {
+            return -1;
+        }
+        return index;
+    }
+
+    ssize_t next(ssize_t index) {
+        return android::BasicHashtable<TKey, TEntry>::next(index);
+    }
+};
+
 struct UidEntry {
     const uid_t uid;
     size_t size;
@@ -39,27 +87,55 @@
     inline bool subtract(size_t s) { size -= s; return !size; }
 };
 
+struct PidEntry {
+    const pid_t pid;
+    uid_t uid;
+    char *name;
+    size_t size;
+
+    PidEntry(pid_t p, uid_t u, char *n):pid(p),uid(u),name(n),size(0) { }
+    PidEntry(const PidEntry &c):
+        pid(c.pid),
+        uid(c.uid),
+        name(c.name ? strdup(c.name) : NULL),
+        size(c.size) { }
+    ~PidEntry() { free(name); }
+
+    const pid_t&getKey() const { return pid; }
+    const uid_t&getUid() const { return uid; }
+    uid_t&setUid(uid_t u) { return uid = u; }
+    const char*getName() const { return name; }
+    char *setName(char *n) { free(name); return name = n; }
+    size_t getSizes() const { return size; }
+    inline void add(size_t s) { size += s; }
+    inline bool subtract(size_t s) { size -= s; return !size; }
+};
+
 // Log Statistics
 class LogStatistics {
     size_t mSizes[LOG_ID_MAX];
     size_t mElements[LOG_ID_MAX];
     size_t mSizesTotal[LOG_ID_MAX];
     size_t mElementsTotal[LOG_ID_MAX];
+    bool enable;
 
     // uid to size list
-    typedef android::BasicHashtable<uid_t, UidEntry> uidTable_t;
+    typedef LogHashtable<uid_t, UidEntry> uidTable_t;
     uidTable_t uidTable[LOG_ID_MAX];
 
+    // pid to uid list
+    typedef LogHashtable<pid_t, PidEntry> pidTable_t;
+    pidTable_t pidTable;
+
 public:
     LogStatistics();
 
-    void enableStatistics() { }
+    void enableStatistics() { enable = true; }
 
     void add(LogBufferElement *entry);
     void subtract(LogBufferElement *entry);
 
-    // Caller must delete array
-    const UidEntry **sort(size_t n, log_id i);
+    std::unique_ptr<const UidEntry *[]> sort(size_t n, log_id i) { return uidTable[i].sort(n); }
 
     // fast track current value by id only
     size_t sizes(log_id_t id) const { return mSizes[id]; }
diff --git a/logd/main.cpp b/logd/main.cpp
index a61beff..1ffb8b0 100644
--- a/logd/main.cpp
+++ b/logd/main.cpp
@@ -143,6 +143,10 @@
 //   write(fdDmesg, "I am here\n", 10);
 static int fdDmesg = -1;
 
+static sem_t uidName;
+static uid_t uid;
+static char *name;
+
 static sem_t reinit;
 static bool reinit_running = false;
 static LogBuffer *logBuf = NULL;
@@ -151,10 +155,45 @@
     prctl(PR_SET_NAME, "logd.daemon");
     set_sched_policy(0, SP_BACKGROUND);
 
-    setgid(AID_LOGD);
-    setuid(AID_LOGD);
+    setgid(AID_SYSTEM);
+    setuid(AID_SYSTEM);
 
     while (reinit_running && !sem_wait(&reinit) && reinit_running) {
+
+        // uidToName Privileged Worker
+        if (uid) {
+            name = NULL;
+
+            FILE *fp = fopen("/data/system/packages.list", "r");
+            if (fp) {
+                // This simple parser is sensitive to format changes in
+                // frameworks/base/services/core/java/com/android/server/pm/Settings.java
+                // A dependency note has been added to that file to correct
+                // this parser.
+
+                char *buffer = NULL;
+                size_t len;
+                while (getline(&buffer, &len, fp) > 0) {
+                    char *userId = strchr(buffer, ' ');
+                    if (!userId) {
+                        continue;
+                    }
+                    *userId = '\0';
+                    unsigned long value = strtoul(userId + 1, NULL, 10);
+                    if (value != uid) {
+                        continue;
+                    }
+                    name = strdup(buffer);
+                    break;
+                }
+                free(buffer);
+                fclose(fp);
+            }
+            uid = 0;
+            sem_post(&uidName);
+            continue;
+        }
+
         if (fdDmesg >= 0) {
             static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
                 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':',
@@ -171,6 +210,20 @@
     return NULL;
 }
 
+char *android::uidToName(uid_t u) {
+    if (!u || !reinit_running) {
+        return NULL;
+    }
+
+    // Not multi-thread safe, we know there is only one caller
+    uid = u;
+
+    name = NULL;
+    sem_post(&reinit);
+    sem_wait(&uidName);
+    return name;
+}
+
 // Serves as a global method to trigger reinitialization
 // and as a function that can be provided to signal().
 void reinit_signal_handler(int /*signal*/) {
@@ -223,6 +276,7 @@
 
     // Reinit Thread
     sem_init(&reinit, 0, 0);
+    sem_init(&uidName, 0, 0);
     pthread_attr_t attr;
     if (!pthread_attr_init(&attr)) {
         struct sched_param param;
diff --git a/mkbootimg/mkbootimg.c b/mkbootimg/mkbootimg.c
index fc92b4d..a4ab7e1 100644
--- a/mkbootimg/mkbootimg.c
+++ b/mkbootimg/mkbootimg.c
@@ -59,7 +59,7 @@
 {
     fprintf(stderr,"usage: mkbootimg\n"
             "       --kernel <filename>\n"
-            "       --ramdisk <filename>\n"
+            "       [ --ramdisk <filename> ]\n"
             "       [ --second <2ndbootloader-filename> ]\n"
             "       [ --cmdline <kernel-commandline> ]\n"
             "       [ --board <boardname> ]\n"
@@ -179,11 +179,6 @@
         return usage();
     }
 
-    if(ramdisk_fn == 0) {
-        fprintf(stderr,"error: no ramdisk image specified\n");
-        return usage();
-    }
-
     if(strlen(board) >= BOOT_NAME_SIZE) {
         fprintf(stderr,"error: board name too large\n");
         return usage();
@@ -213,7 +208,7 @@
         return 1;
     }
 
-    if(!strcmp(ramdisk_fn,"NONE")) {
+    if(ramdisk_fn == 0) {
         ramdisk_data = 0;
         hdr.ramdisk_size = 0;
     } else {
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 6f5567d..ad99a39 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -63,7 +63,6 @@
 ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS)
 
 LOCAL_SRC_FILES := \
-    dynarray.c \
     toolbox.c \
     $(patsubst %,%.c,$(OUR_TOOLS)) \
 
diff --git a/toolbox/dynarray.c b/toolbox/dynarray.c
deleted file mode 100644
index 47594e0..0000000
--- a/toolbox/dynarray.c
+++ /dev/null
@@ -1,104 +0,0 @@
-#include "dynarray.h"
-#include <limits.h>
-#include <stdlib.h>
-#include <string.h>
-
-void
-dynarray_init( dynarray_t *a )
-{
-    a->count = a->capacity = 0;
-    a->items = NULL;
-}
-
-
-static void
-dynarray_reserve_more( dynarray_t *a, int count )
-{
-    int old_cap = a->capacity;
-    int new_cap = old_cap;
-    const int max_cap = INT_MAX/sizeof(void*);
-    void** new_items;
-    int new_count = a->count + count;
-
-    if (count <= 0)
-        return;
-
-    if (count > max_cap - a->count)
-        abort();
-
-    new_count = a->count + count;
-
-    while (new_cap < new_count) {
-        old_cap = new_cap;
-        new_cap += (new_cap >> 2) + 4;
-        if (new_cap < old_cap || new_cap > max_cap) {
-            new_cap = max_cap;
-        }
-    }
-    new_items = realloc(a->items, new_cap*sizeof(void*));
-    if (new_items == NULL)
-        abort();
-
-    a->items = new_items;
-    a->capacity = new_cap;
-}
-
-void
-dynarray_append( dynarray_t *a, void* item )
-{
-    if (a->count >= a->capacity)
-        dynarray_reserve_more(a, 1);
-
-    a->items[a->count++] = item;
-}
-
-void
-dynarray_done( dynarray_t *a )
-{
-    free(a->items);
-    a->items = NULL;
-    a->count = a->capacity = 0;
-}
-
-// string arrays
-
-void strlist_init( strlist_t *list )
-{
-    dynarray_init(list);
-}
-
-void strlist_append_b( strlist_t *list, const void* str, size_t  slen )
-{
-    char *copy = malloc(slen+1);
-    memcpy(copy, str, slen);
-    copy[slen] = '\0';
-    dynarray_append(list, copy);
-}
-
-void strlist_append_dup( strlist_t *list, const char *str)
-{
-    strlist_append_b(list, str, strlen(str));
-}
-
-void strlist_done( strlist_t *list )
-{
-    STRLIST_FOREACH(list, string, free(string));
-    dynarray_done(list);
-}
-
-static int strlist_compare_strings(const void* a, const void* b)
-{
-    const char *sa = *(const char **)a;
-    const char *sb = *(const char **)b;
-    return strcmp(sa, sb);
-}
-
-void strlist_sort( strlist_t *list )
-{
-    if (list->count > 0) {
-        qsort(list->items,
-              (size_t)list->count,
-              sizeof(void*),
-              strlist_compare_strings);
-    }
-}
diff --git a/toolbox/dynarray.h b/toolbox/dynarray.h
deleted file mode 100644
index f73fb3b..0000000
--- a/toolbox/dynarray.h
+++ /dev/null
@@ -1,80 +0,0 @@
-#ifndef DYNARRAY_H
-#define DYNARRAY_H
-
-#include <stddef.h>
-
-/* simple dynamic array of pointers */
-typedef struct {
-    int count;
-    int capacity;
-    void** items;
-} dynarray_t;
-
-#define DYNARRAY_INITIALIZER  { 0, 0, NULL }
-
-void dynarray_init( dynarray_t *a );
-void dynarray_done( dynarray_t *a );
-
-void dynarray_append( dynarray_t *a, void* item );
-
-/* Used to iterate over a dynarray_t
- * _array :: pointer to the array
- * _item_type :: type of objects pointed to by the array
- * _item      :: name of a local variable defined within the loop
- *               with type '_item_type'
- * _stmnt     :: C statement that will be executed in each iteration.
- *
- * You case use 'break' and 'continue' within _stmnt
- *
- * This macro is only intended for simple uses. I.e. do not add or
- * remove items from the array during iteration.
- */
-#define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \
-    do { \
-        int _nn_##__LINE__ = 0; \
-        for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \
-            _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \
-            _stmnt; \
-        } \
-    } while (0)
-
-#define DYNARRAY_FOREACH(_array,_item,_stmnt) \
-    DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt)
-
-/* Simple dynamic string arrays
- *
- * NOTE: A strlist_t owns the strings it references.
- */
-typedef dynarray_t  strlist_t;
-
-#define  STRLIST_INITIALIZER  DYNARRAY_INITIALIZER
-
-/* Used to iterate over a strlist_t
- * _list   :: pointer to strlist_t object
- * _string :: name of local variable name defined within the loop with
- *            type 'char*'
- * _stmnt  :: C statement executed in each iteration
- *
- * This macro is only intended for simple uses. Do not add or remove items
- * to/from the list during iteration.
- */
-#define  STRLIST_FOREACH(_list,_string,_stmnt) \
-    DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt)
-
-void strlist_init( strlist_t *list );
-
-/* note: strlist_done will free all the strings owned by the list */
-void strlist_done( strlist_t *list );
-
-/* append a new string made of the first 'slen' characters from 'str'
- * followed by a trailing zero.
- */
-void strlist_append_b( strlist_t *list, const void* str, size_t  slen );
-
-/* append the copy of a given input string to a strlist_t */
-void strlist_append_dup( strlist_t *list, const char *str);
-
-/* sort the strings in a given list (using strcmp) */
-void strlist_sort( strlist_t *list );
-
-#endif /* DYNARRAY_H */
\ No newline at end of file
diff --git a/toolbox/ls.c b/toolbox/ls.c
index 963fcb5..9a89dd4 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -1,23 +1,119 @@
+#include <dirent.h>
+#include <errno.h>
+#include <grp.h>
+#include <limits.h>
+#include <pwd.h>
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/sysmacros.h>
 #include <sys/types.h>
-#include <dirent.h>
-#include <errno.h>
+#include <time.h>
+#include <unistd.h>
 
 #include <selinux/selinux.h>
 
-#include <sys/stat.h>
-#include <unistd.h>
-#include <time.h>
+// simple dynamic array of strings.
+typedef struct {
+    int count;
+    int capacity;
+    void** items;
+} strlist_t;
 
-#include <pwd.h>
-#include <grp.h>
+#define STRLIST_INITIALIZER { 0, 0, NULL }
 
-#include <linux/kdev_t.h>
-#include <limits.h>
+/* Used to iterate over a strlist_t
+ * _list   :: pointer to strlist_t object
+ * _item   :: name of local variable name defined within the loop with
+ *            type 'char*'
+ * _stmnt  :: C statement executed in each iteration
+ *
+ * This macro is only intended for simple uses. Do not add or remove items
+ * to/from the list during iteration.
+ */
+#define  STRLIST_FOREACH(_list,_item,_stmnt) \
+    do { \
+        int _nn_##__LINE__ = 0; \
+        for (;_nn_##__LINE__ < (_list)->count; ++ _nn_##__LINE__) { \
+            char* _item = (char*)(_list)->items[_nn_##__LINE__]; \
+            _stmnt; \
+        } \
+    } while (0)
 
-#include "dynarray.h"
+static void dynarray_reserve_more( strlist_t *a, int count ) {
+    int old_cap = a->capacity;
+    int new_cap = old_cap;
+    const int max_cap = INT_MAX/sizeof(void*);
+    void** new_items;
+    int new_count = a->count + count;
+
+    if (count <= 0)
+        return;
+
+    if (count > max_cap - a->count)
+        abort();
+
+    new_count = a->count + count;
+
+    while (new_cap < new_count) {
+        old_cap = new_cap;
+        new_cap += (new_cap >> 2) + 4;
+        if (new_cap < old_cap || new_cap > max_cap) {
+            new_cap = max_cap;
+        }
+    }
+    new_items = realloc(a->items, new_cap*sizeof(void*));
+    if (new_items == NULL)
+        abort();
+
+    a->items = new_items;
+    a->capacity = new_cap;
+}
+
+void strlist_init( strlist_t *list ) {
+    list->count = list->capacity = 0;
+    list->items = NULL;
+}
+
+// append a new string made of the first 'slen' characters from 'str'
+// followed by a trailing zero.
+void strlist_append_b( strlist_t *list, const void* str, size_t  slen ) {
+    char *copy = malloc(slen+1);
+    memcpy(copy, str, slen);
+    copy[slen] = '\0';
+    if (list->count >= list->capacity)
+        dynarray_reserve_more(list, 1);
+    list->items[list->count++] = copy;
+}
+
+// append the copy of a given input string to a strlist_t.
+void strlist_append_dup( strlist_t *list, const char *str) {
+    strlist_append_b(list, str, strlen(str));
+}
+
+// note: strlist_done will free all the strings owned by the list.
+void strlist_done( strlist_t *list ) {
+    STRLIST_FOREACH(list, string, free(string));
+    free(list->items);
+    list->items = NULL;
+    list->count = list->capacity = 0;
+}
+
+static int strlist_compare_strings(const void* a, const void* b) {
+    const char *sa = *(const char **)a;
+    const char *sb = *(const char **)b;
+    return strcmp(sa, sb);
+}
+
+/* sort the strings in a given list (using strcmp) */
+void strlist_sort( strlist_t *list ) {
+    if (list->count > 0) {
+        qsort(list->items, (size_t)list->count, sizeof(void*), strlist_compare_strings);
+    }
+}
+
 
 // bits for flags argument
 #define LIST_LONG           (1 << 0)
@@ -200,7 +296,7 @@
     case S_IFCHR:
         printf("%s %-8s %-8s %3d, %3d %s %s\n",
                mode, user, group,
-               (int) MAJOR(s->st_rdev), (int) MINOR(s->st_rdev),
+               major(s->st_rdev), minor(s->st_rdev),
                date, name);
         break;
     case S_IFREG: