Merge "adb: kill adb_mutex_t, adb_cond_t."
diff --git a/adb/Android.mk b/adb/Android.mk
index b2a0dc4..0114ca3 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -62,6 +62,7 @@
     adb_listeners_test.cpp \
     adb_utils_test.cpp \
     fdevent_test.cpp \
+    socket_spec_test.cpp \
     socket_test.cpp \
     sysdeps_test.cpp \
     sysdeps/stat_test.cpp \
diff --git a/adb/socket_spec.cpp b/adb/socket_spec.cpp
index 18e6e6d..14eb16b 100644
--- a/adb/socket_spec.cpp
+++ b/adb/socket_spec.cpp
@@ -20,6 +20,8 @@
 #include <unordered_map>
 #include <vector>
 
+#include <android-base/parseint.h>
+#include <android-base/parsenetaddress.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <cutils/sockets.h>
@@ -62,55 +64,47 @@
     { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
 });
 
-static bool parse_tcp_spec(const std::string& spec, std::string* hostname, int* port,
+bool parse_tcp_socket_spec(const std::string& spec, std::string* hostname, int* port,
                            std::string* error) {
-    std::vector<std::string> fragments = android::base::Split(spec, ":");
-    if (fragments.size() == 1 || fragments.size() > 3) {
-        *error = StringPrintf("invalid tcp specification: '%s'", spec.c_str());
-        return false;
-    }
-
-    if (fragments[0] != "tcp") {
+    if (!StartsWith(spec, "tcp:")) {
         *error = StringPrintf("specification is not tcp: '%s'", spec.c_str());
         return false;
     }
 
-    // strtol accepts leading whitespace.
-    const std::string& port_str = fragments.back();
-    if (port_str.empty() || port_str[0] < '0' || port_str[0] > '9') {
-        *error = StringPrintf("invalid port '%s'", port_str.c_str());
-        return false;
-    }
+    std::string hostname_value;
+    int port_value;
 
-    char* parsed_end;
-    long parsed_port = strtol(port_str.c_str(), &parsed_end, 10);
-    if (*parsed_end != '\0') {
-        *error = StringPrintf("trailing chars in port: '%s'", port_str.c_str());
-        return false;
-    }
-    if (parsed_port > 65535) {
-        *error = StringPrintf("invalid port %ld", parsed_port);
-        return false;
-    }
-
-    // tcp:123 is valid, tcp::123 isn't.
-    if (fragments.size() == 2) {
-        // Empty hostname.
-        if (hostname) {
-            *hostname = "";
-        }
-    } else {
-        if (fragments[1].empty()) {
-            *error = StringPrintf("empty host in '%s'", spec.c_str());
+    // If the spec is tcp:<port>, parse it ourselves.
+    // Otherwise, delegate to android::base::ParseNetAddress.
+    if (android::base::ParseInt(&spec[4], &port_value)) {
+        // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234'
+        // identically.
+        if (port_value < 0 || port_value > 65535) {
+            *error = StringPrintf("bad port number '%d'", port_value);
             return false;
         }
-        if (hostname) {
-            *hostname = fragments[1];
+    } else {
+        std::string addr = spec.substr(4);
+        port_value = -1;
+
+        // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
+        //        on an address that isn't 'localhost' is unsupported.
+        if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, nullptr, error)) {
+            return false;
         }
+
+        if (port_value == -1) {
+            *error = StringPrintf("missing port in specification: '%s'", spec.c_str());
+            return false;
+        }
+    }
+
+    if (hostname) {
+        *hostname = std::move(hostname_value);
     }
 
     if (port) {
-        *port = parsed_port;
+        *port = port_value;
     }
 
     return true;
@@ -141,7 +135,7 @@
 
     std::string error;
     std::string hostname;
-    if (!parse_tcp_spec(spec, &hostname, nullptr, &error)) {
+    if (!parse_tcp_socket_spec(spec, &hostname, nullptr, &error)) {
         return false;
     }
     return tcp_host_is_local(hostname);
@@ -151,7 +145,7 @@
     if (StartsWith(spec, "tcp:")) {
         std::string hostname;
         int port;
-        if (!parse_tcp_spec(spec, &hostname, &port, error)) {
+        if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) {
             return -1;
         }
 
@@ -196,7 +190,7 @@
     if (StartsWith(spec, "tcp:")) {
         std::string hostname;
         int port;
-        if (!parse_tcp_spec(spec, &hostname, &port, error)) {
+        if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) {
             return -1;
         }
 
diff --git a/adb/socket_spec.h b/adb/socket_spec.h
index 6302da5..6920e91 100644
--- a/adb/socket_spec.h
+++ b/adb/socket_spec.h
@@ -25,3 +25,7 @@
 int socket_spec_connect(const std::string& spec, std::string* error);
 int socket_spec_listen(const std::string& spec, std::string* error,
                        int* resolved_tcp_port = nullptr);
+
+// Exposed for testing.
+bool parse_tcp_socket_spec(const std::string& spec, std::string* hostname, int* port,
+                           std::string* error);
diff --git a/adb/socket_spec_test.cpp b/adb/socket_spec_test.cpp
new file mode 100644
index 0000000..40ce21c
--- /dev/null
+++ b/adb/socket_spec_test.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#include "socket_spec.h"
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+TEST(socket_spec, parse_tcp_socket_spec) {
+    std::string hostname, error;
+    int port;
+    EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &error));
+    EXPECT_EQ("", hostname);
+    EXPECT_EQ(5037, port);
+
+    // Bad ports:
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:-1", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:65536", &hostname, &port, &error));
+
+    EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &error));
+    EXPECT_EQ("localhost", hostname);
+    EXPECT_EQ(1234, port);
+
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:-1", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:65536", &hostname, &port, &error));
+
+    // IPv6:
+    EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &error));
+    EXPECT_EQ("::1", hostname);
+    EXPECT_EQ(1234, port);
+
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:-1", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:::1:1234", &hostname, &port, &error));
+}
diff --git a/include/cutils/trace.h b/include/cutils/trace.h
index c9790ad..dc3833f 100644
--- a/include/cutils/trace.h
+++ b/include/cutils/trace.h
@@ -188,8 +188,8 @@
 static inline void atrace_end(uint64_t tag)
 {
     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
-        char c = 'E';
-        write(atrace_marker_fd, &c, 1);
+        void atrace_end_body();
+        atrace_end_body();
     }
 }
 
diff --git a/include/log/log.h b/include/log/log.h
index 045feca..24c77fc 100644
--- a/include/log/log.h
+++ b/include/log/log.h
@@ -41,6 +41,15 @@
 extern "C" {
 #endif
 
+// This file uses ", ## __VA_ARGS__" zero-argument token pasting to
+// work around issues with debug-only syntax errors in assertions
+// that are missing format strings.  See commit
+// 19299904343daf191267564fe32e6cd5c165cd42
+#if defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
+#endif
+
 // ---------------------------------------------------------------------
 
 /*
@@ -692,6 +701,10 @@
 #endif
     ;
 
+#if defined(__clang__)
+#pragma clang diagnostic pop
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index bc1c0ca..167a6d9 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -115,7 +115,6 @@
 #define AID_NET_ADMIN     3005  /* can configure interfaces and routing tables. */
 #define AID_NET_BW_STATS  3006  /* read bandwidth statistics */
 #define AID_NET_BW_ACCT   3007  /* change bandwidth statistics accounting */
-#define AID_NET_BT_STACK  3008  /* bluetooth: access config files */
 #define AID_READPROC      3009  /* Allow /proc read access */
 #define AID_WAKELOCK      3010  /* Allow system wakelock read/write access */
 
@@ -221,7 +220,6 @@
     { "net_admin",     AID_NET_ADMIN, },
     { "net_bw_stats",  AID_NET_BW_STATS, },
     { "net_bw_acct",   AID_NET_BW_ACCT, },
-    { "net_bt_stack",  AID_NET_BT_STACK, },
     { "readproc",      AID_READPROC, },
     { "wakelock",      AID_WAKELOCK, },
 
diff --git a/libcutils/trace-dev.c b/libcutils/trace-dev.c
index 778e4f0..099ab45 100644
--- a/libcutils/trace-dev.c
+++ b/libcutils/trace-dev.c
@@ -196,6 +196,12 @@
     write(atrace_marker_fd, buf, len);
 }
 
+void atrace_end_body()
+{
+    char c = 'E';
+    write(atrace_marker_fd, &c, 1);
+}
+
 #define WRITE_MSG(format_begin, format_end, pid, name, value) { \
     char buf[ATRACE_MESSAGE_LENGTH]; \
     int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \
diff --git a/libcutils/trace-host.c b/libcutils/trace-host.c
index 6478e3e..05842cd 100644
--- a/libcutils/trace-host.c
+++ b/libcutils/trace-host.c
@@ -29,6 +29,7 @@
 void atrace_update_tags() { }
 void atrace_setup() { }
 void atrace_begin_body(const char* name __unused) { }
+void atrace_end_body() { }
 void atrace_async_begin_body(const char* name __unused, int32_t cookie __unused) { }
 void atrace_async_end_body(const char* name __unused, int32_t cookie __unused) { }
 void atrace_int_body(const char* name __unused, int32_t value __unused) { }
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 1a601f0..6315cf4 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -355,12 +355,12 @@
 
     # create basic filesystem structure
     mkdir /data/misc 01771 system misc
-    mkdir /data/misc/bluedroid 02770 bluetooth net_bt_stack
+    mkdir /data/misc/bluedroid 02770 bluetooth bluetooth
     # Fix the access permissions and group ownership for 'bt_config.conf'
     chmod 0660 /data/misc/bluedroid/bt_config.conf
-    chown bluetooth net_bt_stack /data/misc/bluedroid/bt_config.conf
-    mkdir /data/misc/bluetooth 0770 bluetooth net_bt_stack
-    mkdir /data/misc/bluetooth/logs 0770 bluetooth net_bt_stack
+    chown bluetooth bluetooth /data/misc/bluedroid/bt_config.conf
+    mkdir /data/misc/bluetooth 0770 bluetooth bluetooth
+    mkdir /data/misc/bluetooth/logs 0770 bluetooth bluetooth
     mkdir /data/misc/keystore 0700 keystore keystore
     mkdir /data/misc/gatekeeper 0700 system system
     mkdir /data/misc/keychain 0771 system system
diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index 6ef491c..7e13df9 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -40,8 +40,8 @@
 /dev/android_adb          0660   adb        adb
 /dev/android_adb_enable   0660   adb        adb
 /dev/ttyMSM0              0600   bluetooth  bluetooth
-/dev/uhid                 0660   system     net_bt_stack
-/dev/uinput               0660   system     net_bt_stack
+/dev/uhid                 0660   system     bluetooth
+/dev/uinput               0660   system     bluetooth
 /dev/alarm                0664   system     radio
 /dev/rtc0                 0640   system     system
 /dev/tty0                 0660   root       system