Merge "Remove the pt_regs_mips_t structure."
diff --git a/adb/services.c b/adb/services.c
index d5a4642..e4ce0bc 100644
--- a/adb/services.c
+++ b/adb/services.c
@@ -455,7 +455,7 @@
         ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
     } else if(!strncmp(name, "tcpip:", 6)) {
         int port;
-        if (sscanf(name + 6, "%d", &port) == 0) {
+        if (sscanf(name + 6, "%d", &port) != 1) {
             port = 0;
         }
         ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
@@ -527,7 +527,7 @@
         }
         // zero terminate the host at the point we found the colon
         hostbuf[portstr - host] = 0;
-        if (sscanf(portstr + 1, "%d", &port) == 0) {
+        if (sscanf(portstr + 1, "%d", &port) != 1) {
             snprintf(buffer, buffer_size, "bad port number %s", portstr);
             return;
         }
diff --git a/fastbootd/Android.mk b/fastbootd/Android.mk
index 1e69e7a..c74f906 100644
--- a/fastbootd/Android.mk
+++ b/fastbootd/Android.mk
@@ -37,7 +37,6 @@
 LOCAL_MODULE := fastbootd
 LOCAL_MODULE_TAGS := optional
 LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter -Wno-deprecated-declarations -DFLASH_CERT
-LOCAL_LDFLAGS := -ldl
 
 LOCAL_STATIC_LIBRARIES := \
     libc \
diff --git a/healthd/Android.mk b/healthd/Android.mk
index 1d238b1..07e1d73 100644
--- a/healthd/Android.mk
+++ b/healthd/Android.mk
@@ -1,7 +1,5 @@
 # Copyright 2013 The Android Open Source Project
 
-ifneq ($(BUILD_TINY_ANDROID),true)
-
 LOCAL_PATH := $(call my-dir)
 
 include $(CLEAR_VARS)
@@ -77,5 +75,3 @@
 
 _add-charger-image :=
 _img_modules :=
-
-endif
diff --git a/include/private/android_logger.h b/include/private/android_logger.h
new file mode 100644
index 0000000..cc7ba30
--- /dev/null
+++ b/include/private/android_logger.h
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+/* This file is used to define the internal protocol for the Android Logger */
+
+#ifndef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+#define _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+
+#include <stdint.h>
+
+#include <log/log.h>
+#include <log/log_read.h>
+
+#define LOGGER_MAGIC 'l'
+
+/* Header Structure to pstore */
+typedef struct __attribute__((__packed__)) {
+    uint8_t magic;
+    uint16_t len;
+    uint16_t uid;
+    uint16_t pid;
+} android_pmsg_log_header_t;
+
+/* Header Structure to logd, and second header for pstore */
+typedef struct __attribute__((__packed__)) {
+    typeof_log_id_t id;
+    uint16_t tid;
+    log_time realtime;
+} android_log_header_t;
+
+#endif
diff --git a/init/Android.mk b/init/Android.mk
index 8cda879..4df4489 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -33,10 +33,6 @@
 
 LOCAL_MODULE:= init
 
-# Currently, init doesn't start when built with clang.
-# Needs further investigation.
-LOCAL_CLANG := false
-
 LOCAL_FORCE_STATIC_EXECUTABLE := true
 LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
 LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
diff --git a/liblog/logd_write.c b/liblog/logd_write.c
index b2668ce..1e9b591 100644
--- a/liblog/logd_write.c
+++ b/liblog/logd_write.c
@@ -39,6 +39,7 @@
 #include <log/logger.h>
 #include <log/log_read.h>
 #include <private/android_filesystem_config.h>
+#include <private/android_logger.h>
 
 #define LOG_BUF_SIZE 1024
 
@@ -61,6 +62,7 @@
 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
 #else
 static int logd_fd = -1;
+static int pstore_fd = -1;
 #endif
 
 /*
@@ -109,6 +111,12 @@
         logd_fd = -1;
         close(i);
     }
+    if (pstore_fd >= 0) {
+        i = pstore_fd;
+        pstore_fd = -1;
+        close(i);
+    }
+    pstore_fd = open("/dev/pmsg0", O_WRONLY);
 
     i = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
     if (i < 0) {
@@ -155,37 +163,27 @@
         }
     } while (ret == -EINTR);
 #else
-    static const unsigned header_length = 3;
+    static const unsigned header_length = 2;
     struct iovec newVec[nr + header_length];
-    typeof_log_id_t log_id_buf;
-    uint16_t tid;
+    android_log_header_t header;
+    android_pmsg_log_header_t pmsg_header;
     struct timespec ts;
-    log_time realtime_ts;
     size_t i, payload_size;
     static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
+    static pid_t last_pid = (pid_t) -1;
 
     if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
         last_uid = getuid();
     }
-    if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
-        /*
-         * ignore log messages we send to ourself (logd).
-         * Such log messages are often generated by libraries we depend on
-         * which use standard Android logging.
-         */
-        return 0;
+    if (last_pid == (pid_t) -1) {
+        last_pid = getpid();
     }
-
-    if (logd_fd < 0) {
-        return -EBADF;
-    }
-
     /*
      *  struct {
-     *      // what we provide
-     *      typeof_log_id_t  log_id;
-     *      u16              tid;
-     *      log_time         realtime;
+     *      // whate we provire to pstore
+     *      android_pmsg_log_header_t pmsg_header;
+     *      // what we provide to socket
+     *      android_log_header_t header;
      *      // caller provides
      *      union {
      *          struct {
@@ -201,18 +199,21 @@
      */
 
     clock_gettime(CLOCK_REALTIME, &ts);
-    realtime_ts.tv_sec = ts.tv_sec;
-    realtime_ts.tv_nsec = ts.tv_nsec;
 
-    log_id_buf = log_id;
-    tid = gettid();
+    pmsg_header.magic = LOGGER_MAGIC;
+    pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
+    pmsg_header.uid = last_uid;
+    pmsg_header.pid = last_pid;
 
-    newVec[0].iov_base   = (unsigned char *) &log_id_buf;
-    newVec[0].iov_len    = sizeof_log_id_t;
-    newVec[1].iov_base   = (unsigned char *) &tid;
-    newVec[1].iov_len    = sizeof(tid);
-    newVec[2].iov_base   = (unsigned char *) &realtime_ts;
-    newVec[2].iov_len    = sizeof(log_time);
+    header.id = log_id;
+    header.tid = gettid();
+    header.realtime.tv_sec = ts.tv_sec;
+    header.realtime.tv_nsec = ts.tv_nsec;
+
+    newVec[0].iov_base   = (unsigned char *) &pmsg_header;
+    newVec[0].iov_len    = sizeof(pmsg_header);
+    newVec[1].iov_base   = (unsigned char *) &header;
+    newVec[1].iov_len    = sizeof(header);
 
     for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
         newVec[i].iov_base = vec[i - header_length].iov_base;
@@ -223,17 +224,38 @@
             if (newVec[i].iov_len) {
                 ++i;
             }
+            payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
             break;
         }
     }
+    pmsg_header.len += payload_size;
+
+    if (pstore_fd >= 0) {
+        TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
+    }
+
+    if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
+        /*
+         * ignore log messages we send to ourself (logd).
+         * Such log messages are often generated by libraries we depend on
+         * which use standard Android logging.
+         */
+        return 0;
+    }
+
+    if (logd_fd < 0) {
+        return -EBADF;
+    }
 
     /*
      * The write below could be lost, but will never block.
      *
+     * To logd, we drop the pmsg_header
+     *
      * ENOTCONN occurs if logd dies.
      * EAGAIN occurs if logd is overloaded.
      */
-    ret = writev(logd_fd, newVec, i);
+    ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
     if (ret < 0) {
         ret = -errno;
         if (ret == -ENOTCONN) {
@@ -249,15 +271,15 @@
                 return ret;
             }
 
-            ret = writev(logd_fd, newVec, nr + header_length);
+            ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
             if (ret < 0) {
                 ret = -errno;
             }
         }
     }
 
-    if (ret > (ssize_t)(sizeof_log_id_t + sizeof(tid) + sizeof(log_time))) {
-        ret -= sizeof_log_id_t + sizeof(tid) + sizeof(log_time);
+    if (ret > (ssize_t)sizeof(header)) {
+        ret -= sizeof(header);
     }
 #endif
 
diff --git a/libpixelflinger/Android.mk b/libpixelflinger/Android.mk
index f1bd522..acd18b0 100644
--- a/libpixelflinger/Android.mk
+++ b/libpixelflinger/Android.mk
@@ -64,12 +64,10 @@
 LOCAL_CFLAGS := $(PIXELFLINGER_CFLAGS)
 LOCAL_SHARED_LIBRARIES := libcutils liblog
 
-ifneq ($(BUILD_TINY_ANDROID),true)
 # Really this should go away entirely or at least not depend on
 # libhardware, but this at least gets us built.
 LOCAL_SHARED_LIBRARIES += libhardware_legacy
 LOCAL_CFLAGS += -DWITH_LIB_HARDWARE
-endif
 # t32cb16blend.S does not compile with Clang.
 LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
 # arch-arm64/col32cb16blend.S does not compile with Clang.
diff --git a/libsysutils/Android.mk b/libsysutils/Android.mk
index b902a81..7bf53e3 100644
--- a/libsysutils/Android.mk
+++ b/libsysutils/Android.mk
@@ -1,5 +1,3 @@
-ifneq ($(BUILD_TINY_ANDROID),true)
-
 LOCAL_PATH:= $(call my-dir)
 
 include $(CLEAR_VARS)
@@ -25,4 +23,3 @@
 
 include $(BUILD_SHARED_LIBRARY)
 
-endif
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index 6307bed..3d0b38f 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -445,7 +445,24 @@
     uid_t uid = reader->getUid();
 
     pthread_mutex_lock(&mLogElementsLock);
-    for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
+
+    if (start == LogTimeEntry::EPOCH) {
+        // client wants to start from the beginning
+        it = mLogElements.begin();
+    } else {
+        // Client wants to start from some specified time. Chances are
+        // we are better off starting from the end of the time sorted list.
+        for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
+            --it;
+            LogBufferElement *element = *it;
+            if (element->getMonotonicTime() <= start) {
+                it++;
+                break;
+            }
+        }
+    }
+
+    for (; it != mLogElements.end(); ++it) {
         LogBufferElement *element = *it;
 
         if (!privileged && (element->getUid() != uid)) {
diff --git a/logd/LogListener.cpp b/logd/LogListener.cpp
index 8186cea..fc9e30f 100644
--- a/logd/LogListener.cpp
+++ b/logd/LogListener.cpp
@@ -23,6 +23,7 @@
 
 #include <cutils/sockets.h>
 #include <log/logger.h>
+#include <private/android_logger.h>
 
 #include "LogListener.h"
 
@@ -54,7 +55,7 @@
     int socket = cli->getSocket();
 
     ssize_t n = recvmsg(socket, &hdr, 0);
-    if (n <= (ssize_t)(sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time))) {
+    if (n <= (ssize_t)(sizeof(android_log_header_t))) {
         return false;
     }
 
@@ -81,28 +82,19 @@
         return false;
     }
 
-    // First log element is always log_id.
-    log_id_t log_id = (log_id_t) *((typeof_log_id_t *) buffer);
-    if (log_id < 0 || log_id >= LOG_ID_MAX) {
+    android_log_header_t *header = reinterpret_cast<android_log_header_t *>(buffer);
+    if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX) {
         return false;
     }
-    char *msg = ((char *)buffer) + sizeof_log_id_t;
-    n -= sizeof_log_id_t;
 
-    // second element is the thread id of the caller
-    pid_t tid = (pid_t) *((uint16_t *) msg);
-    msg += sizeof(uint16_t);
-    n -= sizeof(uint16_t);
-
-    // third element is the realtime at point of caller
-    log_time realtime(msg);
-    msg += sizeof(log_time);
-    n -= sizeof(log_time);
+    char *msg = ((char *)buffer) + sizeof(android_log_header_t);
+    n -= sizeof(android_log_header_t);
 
     // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
     // truncated message to the logs.
 
-    logbuf->log(log_id, realtime, cred->uid, cred->pid, tid, msg,
+    logbuf->log((log_id_t)header->id, header->realtime,
+        cred->uid, cred->pid, header->tid, msg,
         ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
     reader->notifyNewLog();
 
diff --git a/netcfg/Android.mk b/netcfg/Android.mk
index fc01a54..4796c11 100644
--- a/netcfg/Android.mk
+++ b/netcfg/Android.mk
@@ -1,17 +1,8 @@
-ifneq ($(BUILD_TINY_ANDROID),true)
 LOCAL_PATH:= $(call my-dir)
 
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES:= netcfg.c
 LOCAL_MODULE:= netcfg
-
-#LOCAL_FORCE_STATIC_EXECUTABLE := true
-#LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
-#LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
-#LOCAL_STATIC_LIBRARIES := libcutils libc
-
-LOCAL_SHARED_LIBRARIES := libc libnetutils
+LOCAL_SHARED_LIBRARIES := libnetutils
 LOCAL_CFLAGS := -Werror
-
 include $(BUILD_EXECUTABLE)
-endif
diff --git a/netcfg/netcfg.c b/netcfg/netcfg.c
index 4e83ba4..204bf1d 100644
--- a/netcfg/netcfg.c
+++ b/netcfg/netcfg.c
@@ -22,6 +22,7 @@
 #include <netutils/ifc.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 void die(const char *reason)
 {
diff --git a/toolbox/dynarray.c b/toolbox/dynarray.c
index e9b7b03..47594e0 100644
--- a/toolbox/dynarray.c
+++ b/toolbox/dynarray.c
@@ -1,6 +1,7 @@
 #include "dynarray.h"
-#include <stdlib.h>
 #include <limits.h>
+#include <stdlib.h>
+#include <string.h>
 
 void
 dynarray_init( dynarray_t *a )
diff --git a/toolbox/watchprops.c b/toolbox/watchprops.c
index 0d05aba..cd62922 100644
--- a/toolbox/watchprops.c
+++ b/toolbox/watchprops.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #include <errno.h>