Merge "Lose bootmode and console globals."
diff --git a/adb/adb.cpp b/adb/adb.cpp
index ffa93f4..d37ca36 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -14,21 +14,24 @@
  * limitations under the License.
  */
 
-#define  TRACE_TAG   TRACE_ADB
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <stdarg.h>
-#include <errno.h>
-#include <stddef.h>
-#include <string.h>
-#include <time.h>
-#include <sys/time.h>
-#include <stdint.h>
+#define TRACE_TAG TRACE_ADB
 
 #include "sysdeps.h"
 #include "adb.h"
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <time.h>
+
+#include <string>
+
 #include "adb_auth.h"
 #include "adb_io.h"
 #include "adb_listeners.h"
@@ -74,17 +77,71 @@
     exit(-1);
 }
 
-int   adb_trace_mask;
+#if !ADB_HOST
+void start_device_log(void) {
+    adb_mkdir("/data/adb", 0775);
 
-/* read a comma/space/colum/semi-column separated list of tags
- * from the ADB_TRACE environment variable and build the trace
- * mask from it. note that '1' and 'all' are special cases to
- * enable all tracing
- */
-void  adb_trace_init(void)
-{
-    const char*  p = getenv("ADB_TRACE");
-    const char*  q;
+    struct tm now;
+    time_t t;
+    tzset();
+    time(&t);
+    localtime_r(&t, &now);
+
+    char path[PATH_MAX];
+    strftime(path, sizeof(path), "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt", &now);
+
+    int fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
+    if (fd == -1) {
+        return;
+    }
+
+    // redirect stdout and stderr to the log file
+    dup2(fd, STDOUT_FILENO);
+    dup2(fd, STDERR_FILENO);
+    fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
+    adb_close(fd);
+
+    fd = unix_open("/dev/null", O_RDONLY);
+    dup2(fd, 0);
+    adb_close(fd);
+}
+#endif
+
+int adb_trace_mask;
+
+std::string get_trace_setting_from_env() {
+    const char* setting = getenv("ADB_TRACE");
+    if (setting == nullptr) {
+        setting = "";
+    }
+
+    return std::string(setting);
+}
+
+#if !ADB_HOST
+std::string get_trace_setting_from_prop() {
+    char buf[PROPERTY_VALUE_MAX];
+    property_get("persist.adb.trace_mask", buf, "");
+    return std::string(buf);
+}
+#endif
+
+std::string get_trace_setting() {
+#if ADB_HOST
+    return get_trace_setting_from_env();
+#else
+    return get_trace_setting_from_prop();
+#endif
+}
+
+// Split the comma/space/colum/semi-column separated list of tags from the trace
+// setting and build the trace mask from it. note that '1' and 'all' are special
+// cases to enable all tracing.
+//
+// adb's trace setting comes from the ADB_TRACE environment variable, whereas
+// adbd's comes from the system property persist.adb.trace_mask.
+void adb_trace_init() {
+    const std::string trace_setting = get_trace_setting();
 
     static const struct {
         const char*  tag;
@@ -106,25 +163,25 @@
         { NULL, 0 }
     };
 
-    if (p == NULL)
-            return;
+    if (trace_setting.empty()) {
+        return;
+    }
 
-    /* use a comma/column/semi-colum/space separated list */
+    // Use a comma/colon/semi-colon/space separated list
+    const char* p = trace_setting.c_str();
     while (*p) {
         int  len, tagn;
 
-        q = strpbrk(p, " ,:;");
+        const char* q = strpbrk(p, " ,:;");
         if (q == NULL) {
             q = p + strlen(p);
         }
         len = q - p;
 
-        for (tagn = 0; tags[tagn].tag != NULL; tagn++)
-        {
+        for (tagn = 0; tags[tagn].tag != NULL; tagn++) {
             int  taglen = strlen(tags[tagn].tag);
 
-            if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
-            {
+            if (len == taglen && !memcmp(tags[tagn].tag, p, len)) {
                 int  flag = tags[tagn].flag;
                 if (flag == 0) {
                     adb_trace_mask = ~0;
@@ -138,6 +195,10 @@
         if (*p)
             p++;
     }
+
+#if !ADB_HOST
+    start_device_log();
+#endif
 }
 
 apacket* get_apacket(void)
diff --git a/adb/adb_auth.cpp b/adb/adb_auth.cpp
index c236b64..dc01825 100644
--- a/adb/adb_auth.cpp
+++ b/adb/adb_auth.cpp
@@ -14,7 +14,10 @@
  * limitations under the License.
  */
 
-#define  TRACE_TAG   TRACE_ADB
+#define TRACE_TAG TRACE_ADB
+
+#include "sysdeps.h"
+#include "adb_auth.h"
 
 #include <errno.h>
 #include <stdio.h>
@@ -23,9 +26,7 @@
 #include <unistd.h>
 
 #include "adb.h"
-#include "adb_auth.h"
 #include "transport.h"
-#include "sysdeps.h"
 
 int auth_enabled = 0;
 
diff --git a/adb/adb_auth.h b/adb/adb_auth.h
index e0425ad..635556e 100644
--- a/adb/adb_auth.h
+++ b/adb/adb_auth.h
@@ -17,6 +17,8 @@
 #ifndef __ADB_AUTH_H
 #define __ADB_AUTH_H
 
+#include "adb.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/adb/adb_auth_client.cpp b/adb/adb_auth_client.cpp
index 5dadcd9..8e7d38b 100644
--- a/adb/adb_auth_client.cpp
+++ b/adb/adb_auth_client.cpp
@@ -14,24 +14,24 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_AUTH
+
+#include "sysdeps.h"
+#include "adb_auth.h"
+
 #include <resolv.h>
 #include <stdio.h>
 #include <string.h>
 
-#include "sysdeps.h"
-
-#include "adb.h"
-#include "adb_auth.h"
 #include "cutils/list.h"
 #include "cutils/sockets.h"
-#include "fdevent.h"
 #include "mincrypt/rsa.h"
 #include "mincrypt/sha.h"
+
+#include "adb.h"
+#include "fdevent.h"
 #include "transport.h"
 
-#define TRACE_TAG TRACE_AUTH
-
-
 struct adb_public_key {
     struct listnode node;
     RSAPublicKey key;
diff --git a/adb/adb_auth_host.cpp b/adb/adb_auth_host.cpp
index aba23d4..7c2bcfb 100644
--- a/adb/adb_auth_host.cpp
+++ b/adb/adb_auth_host.cpp
@@ -14,8 +14,14 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_AUTH
+
+#include "sysdeps.h"
+#include "adb_auth.h"
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #ifdef _WIN32
 #  ifndef WIN32_LEAN_AND_MEAN
@@ -28,11 +34,8 @@
 #  include <sys/stat.h>
 #  include <unistd.h>
 #endif
-#include <string.h>
 
-#include "sysdeps.h"
 #include "adb.h"
-#include "adb_auth.h"
 
 /* HACK: we need the RSAPublicKey struct
  * but RSA_verify conflits with openssl */
@@ -52,12 +55,9 @@
 #include <openssl/base64.h>
 #endif
 
-#define TRACE_TAG TRACE_AUTH
-
 #define ANDROID_PATH   ".android"
 #define ADB_KEY_FILE   "adbkey"
 
-
 struct adb_private_key {
     struct listnode node;
     RSA *rsa;
diff --git a/adb/adb_client.cpp b/adb/adb_client.cpp
index a485aa2..4751bff 100644
--- a/adb/adb_client.cpp
+++ b/adb/adb_client.cpp
@@ -14,6 +14,11 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_ADB
+
+#include "sysdeps.h"
+#include "adb_client.h"
+
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
@@ -23,10 +28,6 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include "sysdeps.h"
-
-#define  TRACE_TAG  TRACE_ADB
-#include "adb_client.h"
 #include "adb_io.h"
 
 static transport_type __adb_transport = kTransportAny;
diff --git a/adb/adb_io.cpp b/adb/adb_io.cpp
index 4dd9f4d..d89f304 100644
--- a/adb/adb_io.cpp
+++ b/adb/adb_io.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#define TRACE_TAG  TRACE_RWX
+#define TRACE_TAG TRACE_RWX
 
 #include "sysdeps.h"
 #include "adb_io.h"
@@ -79,6 +79,8 @@
                 D("writex: fd=%d disconnected\n", fd);
                 errno = 0;
                 return false;
+            } else {
+                return false;
             }
         } else {
             len -= r;
diff --git a/adb/adb_io_test.cpp b/adb/adb_io_test.cpp
index 0c69bc9..da340b2 100644
--- a/adb/adb_io_test.cpp
+++ b/adb/adb_io_test.cpp
@@ -18,8 +18,11 @@
 
 #include <gtest/gtest.h>
 
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include <string>
@@ -127,6 +130,15 @@
   EXPECT_EQ(expected, s);
 }
 
+TEST(io, WriteFdExactly_ENOSPC) {
+    int fd = open("/dev/full", O_WRONLY);
+    ASSERT_NE(-1, fd);
+
+    char buf[] = "foo";
+    ASSERT_FALSE(WriteFdExactly(fd, buf, sizeof(buf)));
+    ASSERT_EQ(ENOSPC, errno);
+}
+
 TEST(io, WriteStringFully) {
   const char str[] = "Foobar";
   TemporaryFile tf;
diff --git a/adb/adb_main.cpp b/adb/adb_main.cpp
index 1d9cc3b..c1e4b13 100644
--- a/adb/adb_main.cpp
+++ b/adb/adb_main.cpp
@@ -14,15 +14,15 @@
  * limitations under the License.
  */
 
-#define  TRACE_TAG   TRACE_ADB
+#define TRACE_TAG TRACE_ADB
+
+#include "sysdeps.h"
 
 #include <errno.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "sysdeps.h"
-
 #include "adb.h"
 #include "adb_auth.h"
 #include "adb_listeners.h"
@@ -148,42 +148,6 @@
     return true; // "adb root" not allowed, always drop privileges.
 #endif /* ALLOW_ADBD_ROOT */
 }
-
-void start_device_log(void)
-{
-    int fd;
-    char    path[PATH_MAX];
-    struct tm now;
-    time_t t;
-    char value[PROPERTY_VALUE_MAX];
-
-    // read the trace mask from persistent property persist.adb.trace_mask
-    // give up if the property is not set or cannot be parsed
-    property_get("persist.adb.trace_mask", value, "");
-    if (sscanf(value, "%x", &adb_trace_mask) != 1)
-        return;
-
-    adb_mkdir("/data/adb", 0775);
-    tzset();
-    time(&t);
-    localtime_r(&t, &now);
-    strftime(path, sizeof(path),
-                "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
-                &now);
-    fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
-    if (fd < 0)
-        return;
-
-    // redirect stdout and stderr to the log file
-    dup2(fd, 1);
-    dup2(fd, 2);
-    fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
-    adb_close(fd);
-
-    fd = unix_open("/dev/null", O_RDONLY);
-    dup2(fd, 0);
-    adb_close(fd);
-}
 #endif /* ADB_HOST */
 
 /* Constructs a local name of form tcp:port.
@@ -385,18 +349,20 @@
     return 0;
 }
 
-int main(int argc, char **argv)
-{
+int main(int argc, char **argv) {
 #if ADB_HOST
     adb_sysdeps_init();
+#endif
     adb_trace_init();
+
+#if ADB_HOST
     D("Handling commandline()\n");
     return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
 #else
     /* If adbd runs inside the emulator this will enable adb tracing via
      * adb-debug qemud service in the emulator. */
     adb_qemu_trace_init();
-    while(1) {
+    while (1) {
         int c;
         int option_index = 0;
         static struct option opts[] = {
@@ -418,7 +384,6 @@
         }
     }
 
-    start_device_log();
     D("Handling main()\n");
     return adb_main(0, DEFAULT_ADB_PORT);
 #endif
diff --git a/adb/commandline.cpp b/adb/commandline.cpp
index 4538b04..c9b1eab 100644
--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_ADB
+
+#include "sysdeps.h"
+
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
@@ -31,9 +35,6 @@
 #include <unistd.h>
 #endif
 
-#include "sysdeps.h"
-
-#define  TRACE_TAG  TRACE_ADB
 #include "adb.h"
 #include "adb_auth.h"
 #include "adb_client.h"
diff --git a/adb/fdevent.cpp b/adb/fdevent.cpp
index eeb2a9c..0c43c5e 100644
--- a/adb/fdevent.cpp
+++ b/adb/fdevent.cpp
@@ -15,25 +15,23 @@
 ** limitations under the License.
 */
 
-#include <sys/ioctl.h>
+#define TRACE_TAG TRACE_FDEVENT
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
+#include "sysdeps.h"
+#include "fdevent.h"
+
 #include <errno.h>
-
 #include <fcntl.h>
-
 #include <stdarg.h>
 #include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
 
 #include "adb_io.h"
 #include "adb_trace.h"
-#include "fdevent.h"
-#include "sysdeps.h"
-
-#define TRACE_TAG  TRACE_FDEVENT
 
 /* !!! Do not enable DEBUG for the adb that will run as the server:
 ** both stdout and stderr are used to communicate between the client
diff --git a/adb/file_sync_service.cpp b/adb/file_sync_service.cpp
index ac01678..e8e9a0f 100644
--- a/adb/file_sync_service.cpp
+++ b/adb/file_sync_service.cpp
@@ -14,6 +14,11 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_SYNC
+
+#include "sysdeps.h"
+#include "file_sync_service.h"
+
 #include <dirent.h>
 #include <errno.h>
 #include <selinux/android.h>
@@ -25,12 +30,8 @@
 #include <unistd.h>
 #include <utime.h>
 
-#include "sysdeps.h"
-
-#define TRACE_TAG  TRACE_SYNC
 #include "adb.h"
 #include "adb_io.h"
-#include "file_sync_service.h"
 #include "private/android_filesystem_config.h"
 
 static bool should_use_fs_config(const char* path) {
diff --git a/adb/jdwp_service.cpp b/adb/jdwp_service.cpp
index f0b4ba7..9cf084e 100644
--- a/adb/jdwp_service.cpp
+++ b/adb/jdwp_service.cpp
@@ -1,12 +1,32 @@
+/*
+ * 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.
+ */
+
 /* implement the "debug-ports" and "track-debug-ports" device services */
+
+#define TRACE_TAG TRACE_JDWP
+
 #include "sysdeps.h"
-#define  TRACE_TAG   TRACE_JDWP
-#include "adb.h"
+
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 
+#include "adb.h"
+
 /* here's how these things work.
 
    when adbd starts, it creates a unix server socket
diff --git a/adb/remount_service.cpp b/adb/remount_service.cpp
index a83d5b1..483ca3d 100644
--- a/adb/remount_service.cpp
+++ b/adb/remount_service.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_ADB
+
+#include "sysdeps.h"
+
 #include <errno.h>
 #include <fcntl.h>
 #include <mntent.h>
@@ -25,9 +29,6 @@
 
 #include <string>
 
-#include "sysdeps.h"
-
-#define  TRACE_TAG  TRACE_ADB
 #include "adb.h"
 #include "adb_io.h"
 #include "cutils/properties.h"
diff --git a/adb/services.cpp b/adb/services.cpp
index e7bf6b0..abf8ea5 100644
--- a/adb/services.cpp
+++ b/adb/services.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_SERVICES
+
+#include "sysdeps.h"
+
 #include <errno.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -32,9 +36,6 @@
 #include "cutils/properties.h"
 #endif
 
-#include "sysdeps.h"
-
-#define  TRACE_TAG  TRACE_SERVICES
 #include "adb.h"
 #include "adb_io.h"
 #include "file_sync_service.h"
diff --git a/adb/set_verity_enable_state_service.cpp b/adb/set_verity_enable_state_service.cpp
index 139b074..b75ed4c 100644
--- a/adb/set_verity_enable_state_service.cpp
+++ b/adb/set_verity_enable_state_service.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_ADB
+
+#include "sysdeps.h"
+
 #include <fcntl.h>
 #include <inttypes.h>
 #include <stdarg.h>
@@ -21,13 +25,12 @@
 #include <stdio.h>
 #include <sys/stat.h>
 
-#define  TRACE_TAG  TRACE_ADB
-#include "adb.h"
 #include "cutils/properties.h"
+
+#include "adb.h"
 #include "ext4_sb.h"
 #include "fs_mgr.h"
 #include "remount_service.h"
-#include "sysdeps.h"
 
 #define FSTAB_PREFIX "/fstab."
 struct fstab *fstab;
diff --git a/adb/sockets.cpp b/adb/sockets.cpp
index 12bc8d8..48d02d6 100644
--- a/adb/sockets.cpp
+++ b/adb/sockets.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_SOCKETS
+
+#include "sysdeps.h"
+
 #include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
@@ -21,14 +25,12 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "sysdeps.h"
-
-#define  TRACE_TAG  TRACE_SOCKETS
-#include "adb.h"
-#include "adb_io.h"
 #if !ADB_HOST
 #include "cutils/properties.h"
 #endif
+
+#include "adb.h"
+#include "adb_io.h"
 #include "transport.h"
 
 ADB_MUTEX_DEFINE( socket_list_lock );
diff --git a/adb/sysdeps_win32.c b/adb/sysdeps_win32.c
index f132b8c..c28e031 100644
--- a/adb/sysdeps_win32.c
+++ b/adb/sysdeps_win32.c
@@ -1,10 +1,30 @@
+/*
+ * 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.
+ */
+
+#define TRACE_TAG TRACE_SYSDEPS
+
 #include "sysdeps.h"
-#include <winsock2.h>
+
+#include <winsock2.h> /* winsock.h *must* be included before windows.h. */
 #include <windows.h>
+
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
-#define  TRACE_TAG  TRACE_SYSDEPS
+
 #include "adb.h"
 
 extern void fatal(const char *fmt, ...);
diff --git a/adb/transport.cpp b/adb/transport.cpp
index 1f8ac03..0a960ff 100644
--- a/adb/transport.cpp
+++ b/adb/transport.cpp
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
-#include "sysdeps.h"
+#define TRACE_TAG TRACE_TRANSPORT
 
+#include "sysdeps.h"
 #include "transport.h"
 
 #include <ctype.h>
@@ -25,7 +26,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#define   TRACE_TAG  TRACE_TRANSPORT
 #include "adb.h"
 
 static void transport_unref(atransport *t);
diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp
index 440d4c5..fe3c87f 100644
--- a/adb/transport_local.cpp
+++ b/adb/transport_local.cpp
@@ -14,21 +14,23 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_TRANSPORT
+
+#include "sysdeps.h"
+#include "transport.h"
+
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 
-#include "sysdeps.h"
-
-#define  TRACE_TAG  TRACE_TRANSPORT
-#include "adb.h"
-#include "adb_io.h"
 #if !ADB_HOST
 #include "cutils/properties.h"
 #endif
-#include "transport.h"
+
+#include "adb.h"
+#include "adb_io.h"
 
 #if ADB_HOST
 /* we keep a list of opened transports. The atransport struct knows to which
diff --git a/adb/transport_usb.cpp b/adb/transport_usb.cpp
index 37a8219..cdabffe 100644
--- a/adb/transport_usb.cpp
+++ b/adb/transport_usb.cpp
@@ -14,15 +14,16 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_TRANSPORT
+
+#include "sysdeps.h"
+#include "transport.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <sysdeps.h>
-
-#define  TRACE_TAG  TRACE_TRANSPORT
 #include "adb.h"
-#include "transport.h"
 
 static int remote_read(apacket *p, atransport *t)
 {
diff --git a/adb/usb_linux.cpp b/adb/usb_linux.cpp
index c01ec8c..6fd2b40 100644
--- a/adb/usb_linux.cpp
+++ b/adb/usb_linux.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_USB
+
+#include "sysdeps.h"
+
 #include <ctype.h>
 #include <dirent.h>
 #include <errno.h>
@@ -33,9 +37,6 @@
 #include <linux/usb_ch9.h>
 #endif
 
-#include "sysdeps.h"
-
-#define   TRACE_TAG  TRACE_USB
 #include "adb.h"
 #include "transport.h"
 
diff --git a/adb/usb_linux_client.c b/adb/usb_linux_client.c
index c88b258..434451c 100644
--- a/adb/usb_linux_client.c
+++ b/adb/usb_linux_client.c
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_USB
+
+#include "sysdeps.h"
+
 #include <dirent.h>
 #include <errno.h>
 #include <linux/usb/ch9.h>
@@ -25,9 +29,6 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "sysdeps.h"
-
-#define   TRACE_TAG  TRACE_USB
 #include "adb.h"
 #include "transport.h"
 
diff --git a/adb/usb_osx.c b/adb/usb_osx.c
index aa7e1ea..94c8cfe 100644
--- a/adb/usb_osx.c
+++ b/adb/usb_osx.c
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_USB
+
+#include "sysdeps.h"
+
 #include <CoreFoundation/CoreFoundation.h>
 
 #include <IOKit/IOKitLib.h>
@@ -24,9 +28,6 @@
 
 #include <stdio.h>
 
-#include "sysdeps.h"
-
-#define TRACE_TAG   TRACE_USB
 #include "adb.h"
 #include "transport.h"
 
diff --git a/adb/usb_windows.cpp b/adb/usb_windows.cpp
index 3c5533b..d2bd58c 100644
--- a/adb/usb_windows.cpp
+++ b/adb/usb_windows.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#define TRACE_TAG TRACE_USB
+
+#include "sysdeps.h"
+
 #include <winsock2.h>  // winsock.h *must* be included before windows.h.
 #include <adb_api.h>
 #include <errno.h>
@@ -23,9 +27,6 @@
 #include <windows.h>
 #include <winerror.h>
 
-#include "sysdeps.h"
-
-#define   TRACE_TAG  TRACE_USB
 #include "adb.h"
 #include "transport.h"
 
diff --git a/base/Android.mk b/base/Android.mk
index 0e1a9b6..17d6ece 100644
--- a/base/Android.mk
+++ b/base/Android.mk
@@ -56,7 +56,6 @@
 # ------------------------------------------------------------------------------
 include $(CLEAR_VARS)
 LOCAL_MODULE := libbase
-LOCAL_CLANG := true
 LOCAL_SRC_FILES := $(libbase_src_files)
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
 LOCAL_CPPFLAGS := $(libbase_cppflags)
@@ -66,7 +65,6 @@
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := libbase
-LOCAL_CLANG := true
 LOCAL_WHOLE_STATIC_LIBRARIES := libbase
 LOCAL_SHARED_LIBRARIES := liblog
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
@@ -88,7 +86,6 @@
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := libbase_test
-LOCAL_CLANG := true
 LOCAL_SRC_FILES := $(libbase_test_src_files)
 LOCAL_CPPFLAGS := $(libbase_cppflags)
 LOCAL_SHARED_LIBRARIES := libbase
diff --git a/fs_mgr/fs_mgr_verity.c b/fs_mgr/fs_mgr_verity.c
index feb3c19..5c67333 100644
--- a/fs_mgr/fs_mgr_verity.c
+++ b/fs_mgr/fs_mgr_verity.c
@@ -591,7 +591,7 @@
     return rc;
 }
 
-int fs_mgr_update_verity_state()
+int fs_mgr_update_verity_state(fs_mgr_verity_state_callback callback)
 {
     _Alignas(struct dm_ioctl) char buffer[DM_BUF_SIZE];
     char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
@@ -645,7 +645,14 @@
 
         if (*status == 'C') {
             rc = write_verity_state(state_loc, offset, VERITY_MODE_LOGGING);
-            goto out;
+
+            if (rc == -1) {
+                goto out;
+            }
+        }
+
+        if (callback) {
+            callback(&fstab->recs[i], mount_point, *status);
         }
     }
 
@@ -729,6 +736,8 @@
         mode = VERITY_MODE_RESTART; /* default dm-verity mode */
     }
 
+    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,
             mode) < 0) {
diff --git a/fs_mgr/include/fs_mgr.h b/fs_mgr/include/fs_mgr.h
index 0437d45..d2c8fff 100644
--- a/fs_mgr/include/fs_mgr.h
+++ b/fs_mgr/include/fs_mgr.h
@@ -27,6 +27,10 @@
 // turn verity off in userdebug builds.
 #define VERITY_METADATA_MAGIC_DISABLE 0x46464f56 // "VOFF"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 // Verity modes
 enum verity_mode {
     VERITY_MODE_EIO = 0,
@@ -35,10 +39,6 @@
     VERITY_MODE_LAST = VERITY_MODE_RESTART
 };
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 /*
  * The entries must be kept in the same order as they were seen in the fstab.
  * Unless explicitly requested, a lookup on mount point should always
@@ -66,6 +66,10 @@
     unsigned int zram_size;
 };
 
+// Callback function for verity status
+typedef void (*fs_mgr_verity_state_callback)(struct fstab_rec *fstab,
+        const char *mount_point, int status);
+
 struct fstab *fs_mgr_read_fstab(const char *fstab_path);
 void fs_mgr_free_fstab(struct fstab *fstab);
 
@@ -84,7 +88,7 @@
 int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc,
                           char *real_blk_device, int size);
 int fs_mgr_load_verity_state(int *mode);
-int fs_mgr_update_verity_state();
+int fs_mgr_update_verity_state(fs_mgr_verity_state_callback callback);
 int fs_mgr_add_entry(struct fstab *fstab,
                      const char *mount_point, const char *fs_type,
                      const char *blk_device);
diff --git a/init/builtins.cpp b/init/builtins.cpp
index fb1aa7c..01217c1 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -687,6 +687,30 @@
     return -1;
 }
 
+static void verity_update_property(struct fstab_rec *fstab,
+                    const char *mount_point, int status) {
+    char key[PROP_NAME_MAX];
+    int ret;
+
+    ret = snprintf(key, PROP_NAME_MAX, "partition.%s.verified", mount_point);
+    if (ret >= PROP_NAME_MAX) {
+        ERROR("Error setting verified property for %s: name too long\n",
+            mount_point);
+        return;
+    }
+
+    ret = property_set(key, "1");
+    if (ret < 0)
+        ERROR("Error setting verified property %s: %d\n", key, ret);
+}
+
+int do_verity_update_state(int nargs, char **args) {
+    if (nargs == 1) {
+        return fs_mgr_update_verity_state(verity_update_property);
+    }
+    return -1;
+}
+
 int do_write(int nargs, char **args)
 {
     const char *path = args[1];
diff --git a/init/init_parser.cpp b/init/init_parser.cpp
index f3d34b2..7db203f 100644
--- a/init/init_parser.cpp
+++ b/init/init_parser.cpp
@@ -202,6 +202,7 @@
         break;
     case 'v':
         if (!strcmp(s, "erity_load_state")) return K_verity_load_state;
+        if (!strcmp(s, "erity_update_state")) return K_verity_update_state;
         break;
     case 'w':
         if (!strcmp(s, "rite")) return K_write;
diff --git a/init/keywords.h b/init/keywords.h
index c8327c3..09f645b 100644
--- a/init/keywords.h
+++ b/init/keywords.h
@@ -37,6 +37,7 @@
 int do_load_persist_props(int nargs, char **args);
 int do_load_all_props(int nargs, char **args);
 int do_verity_load_state(int nargs, char **args);
+int do_verity_update_state(int nargs, char **args);
 int do_wait(int nargs, char **args);
 #define __MAKE_KEYWORD_ENUM__
 #define KEYWORD(symbol, flags, nargs, func) K_##symbol,
@@ -89,6 +90,7 @@
     KEYWORD(sysclktz,    COMMAND, 1, do_sysclktz)
     KEYWORD(user,        OPTION,  0, 0)
     KEYWORD(verity_load_state,      COMMAND, 0, do_verity_load_state)
+    KEYWORD(verity_update_state,    COMMAND, 0, do_verity_update_state)
     KEYWORD(wait,        COMMAND, 1, do_wait)
     KEYWORD(write,       COMMAND, 2, do_write)
     KEYWORD(copy,        COMMAND, 2, do_copy)