Merge "Fix -Wdouble-promotion warnings"
diff --git a/cmds/atrace/atrace.cpp b/cmds/atrace/atrace.cpp
index ee2b275..64f7b82 100644
--- a/cmds/atrace/atrace.cpp
+++ b/cmds/atrace/atrace.cpp
@@ -1243,7 +1243,7 @@
             fflush(stdout);
             int outFd = STDOUT_FILENO;
             if (g_outputFile) {
-                outFd = open(g_outputFile, O_WRONLY | O_CREAT);
+                outFd = open(g_outputFile, O_WRONLY | O_CREAT, 0644);
             }
             if (outFd == -1) {
                 printf("Failed to open '%s', err=%d", g_outputFile, errno);
diff --git a/cmds/dumpsys/tests/dumpsys_test.cpp b/cmds/dumpsys/tests/dumpsys_test.cpp
index a66685d..01a2fa3 100644
--- a/cmds/dumpsys/tests/dumpsys_test.cpp
+++ b/cmds/dumpsys/tests/dumpsys_test.cpp
@@ -78,6 +78,7 @@
         R<bool>(const hidl_string&,
                 const hidl_string&,
                 const sp<IServiceNotification>&));
+    MOCK_METHOD1(debugDump, R<void>(debugDump_cb));
 
 };
 
diff --git a/cmds/installd/InstalldNativeService.cpp b/cmds/installd/InstalldNativeService.cpp
index 914eaae..cdef7e1 100644
--- a/cmds/installd/InstalldNativeService.cpp
+++ b/cmds/installd/InstalldNativeService.cpp
@@ -1184,6 +1184,7 @@
         return;
     }
     while ((p = fts_read(fts)) != NULL) {
+        p->fts_number = p->fts_parent->fts_number;
         switch (p->fts_info) {
         case FTS_D:
             if (p->fts_level == 4
@@ -1192,7 +1193,6 @@
                     && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) {
                 p->fts_number = 1;
             }
-            p->fts_number = p->fts_parent->fts_number;
             // Fall through to count the directory
         case FTS_DEFAULT:
         case FTS_F:
@@ -1296,9 +1296,9 @@
             ATRACE_END();
 
             ATRACE_BEGIN("external");
-            auto extPath = create_data_media_package_path(uuid_, userId, pkgname, "data");
+            auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname);
             collectManualStats(extPath, &extStats);
-            auto mediaPath = create_data_media_package_path(uuid_, userId, pkgname, "media");
+            auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname);
             calculate_tree_size(mediaPath, &extStats.dataSize);
             ATRACE_END();
         }
diff --git a/cmds/installd/dexopt.cpp b/cmds/installd/dexopt.cpp
index 1565d0d..5d84157 100644
--- a/cmds/installd/dexopt.cpp
+++ b/cmds/installd/dexopt.cpp
@@ -235,22 +235,24 @@
 
     static const int MAX_INT_LEN = 12;      // '-'+10dig+'\0' -OR- 0x+8dig
 
-    char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
-    char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
-    char input_vdex_fd_arg[strlen("--input-vdex-fd=") + MAX_INT_LEN];
-    char output_vdex_fd_arg[strlen("--output-vdex-fd=") + MAX_INT_LEN];
-    char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
-    char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
-    char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
-    char instruction_set_variant_arg[strlen("--instruction-set-variant=") + kPropertyValueMax];
-    char instruction_set_features_arg[strlen("--instruction-set-features=") + kPropertyValueMax];
-    char dex2oat_Xms_arg[strlen("-Xms") + kPropertyValueMax];
-    char dex2oat_Xmx_arg[strlen("-Xmx") + kPropertyValueMax];
-    char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + kPropertyValueMax];
+    // clang FORTIFY doesn't let us use strlen in constant array bounds, so we
+    // use arraysize instead.
+    char zip_fd_arg[arraysize("--zip-fd=") + MAX_INT_LEN];
+    char zip_location_arg[arraysize("--zip-location=") + PKG_PATH_MAX];
+    char input_vdex_fd_arg[arraysize("--input-vdex-fd=") + MAX_INT_LEN];
+    char output_vdex_fd_arg[arraysize("--output-vdex-fd=") + MAX_INT_LEN];
+    char oat_fd_arg[arraysize("--oat-fd=") + MAX_INT_LEN];
+    char oat_location_arg[arraysize("--oat-location=") + PKG_PATH_MAX];
+    char instruction_set_arg[arraysize("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
+    char instruction_set_variant_arg[arraysize("--instruction-set-variant=") + kPropertyValueMax];
+    char instruction_set_features_arg[arraysize("--instruction-set-features=") + kPropertyValueMax];
+    char dex2oat_Xms_arg[arraysize("-Xms") + kPropertyValueMax];
+    char dex2oat_Xmx_arg[arraysize("-Xmx") + kPropertyValueMax];
+    char dex2oat_compiler_filter_arg[arraysize("--compiler-filter=") + kPropertyValueMax];
     bool have_dex2oat_swap_fd = false;
-    char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
+    char dex2oat_swap_fd[arraysize("--swap-fd=") + MAX_INT_LEN];
     bool have_dex2oat_image_fd = false;
-    char dex2oat_image_fd[strlen("--app-image-fd=") + MAX_INT_LEN];
+    char dex2oat_image_fd[arraysize("--app-image-fd=") + MAX_INT_LEN];
 
     sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
     sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
@@ -495,7 +497,7 @@
     fd_t profile_fd = -1;
     std::string profile_file = create_primary_profile(profile_dir);
 
-    profile_fd = TEMP_FAILURE_RETRY(open(profile_file.c_str(), open_mode | O_NOFOLLOW));
+    profile_fd = TEMP_FAILURE_RETRY(open(profile_file.c_str(), open_mode | O_NOFOLLOW, 0600));
     if (profile_fd == -1) {
         // It's not an error if the profile file does not exist.
         if (errno != ENOENT) {
@@ -754,7 +756,7 @@
         return false;
     }
 
-    fd_t output_fd = open(out_file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW);
+    fd_t output_fd = open(out_file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644);
     if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
         ALOGE("installd cannot chmod '%s' dump_profile\n", out_file_name.c_str());
         return false;
diff --git a/cmds/lshal/Android.bp b/cmds/lshal/Android.bp
new file mode 100644
index 0000000..dd8104d
--- /dev/null
+++ b/cmds/lshal/Android.bp
@@ -0,0 +1,26 @@
+// 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.
+
+cc_binary {
+    name: "lshal",
+    shared_libs: [
+        "libutils",
+        "libhidlbase",
+        "android.hidl.manager@1.0",
+        "libhidltransport",
+    ],
+    srcs: [
+        "lshal.cpp"
+    ],
+}
diff --git a/cmds/lshal/lshal.cpp b/cmds/lshal/lshal.cpp
new file mode 100644
index 0000000..bc8bf39
--- /dev/null
+++ b/cmds/lshal/lshal.cpp
@@ -0,0 +1,117 @@
+/*
+ * 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 <getopt.h>
+
+#include <map>
+#include <iomanip>
+#include <iostream>
+#include <sstream>
+
+#include <android/hidl/manager/1.0/IServiceManager.h>
+#include <hidl/ServiceManagement.h>
+
+template <typename A, typename B, typename C, typename D>
+void printColumn(std::stringstream &stream,
+        const A &a, const B &b, const C &c, const D &d) {
+    using namespace ::std;
+    stream << left
+           << setw(70) << a << "\t"
+           << setw(20) << b << "\t"
+           << setw(10) << c << "\t"
+           << setw(5)  << d << "\t"
+           << endl;
+}
+
+int dump() {
+    using namespace ::std;
+    using namespace ::android::hardware;
+    using namespace ::android::hidl::manager::V1_0;
+
+    std::map<std::string, ::android::sp<IServiceManager>> mapping = {
+            {"hwbinder", defaultServiceManager()},
+            {"passthrough", getPassthroughServiceManager()}
+    };
+
+    std::stringstream stream;
+
+    stream << "All services:" << endl;
+    stream << left;
+    printColumn(stream, "Interface", "Instance", "Transport", "Ref");
+
+    for (const auto &pair : mapping) {
+        const std::string &mode = pair.first;
+        const ::android::sp<IServiceManager> &manager = pair.second;
+
+        if (manager == nullptr) {
+            cerr << "Failed to get IServiceManager for " << mode << "!" << endl;
+            continue;
+        }
+
+        auto ret = manager->debugDump([&](const auto &registered) {
+            for (const auto &info : registered) {
+                printColumn(stream,
+                    info.interfaceName,
+                    info.instanceName.empty() ? "N/A" : info.instanceName,
+                    mode,
+                    info.refCount == 0 ? "N/A" : std::to_string(info.refCount - 1));
+            }
+        });
+        if (!ret.isOk()) {
+            cerr << "Failed to list services for " << mode << ": "
+                 << ret.description() << endl;
+        }
+    }
+    cout << stream.rdbuf();
+    return 0;
+}
+
+int usage() {
+    using namespace ::std;
+    cerr
+        << "usage: lshal" << endl
+        << "           To dump all hals." << endl
+        << "or:" << endl
+        << "       lshal [-h|--help]" << endl
+        << "           -h, --help: show this help information." << endl;
+    return -1;
+}
+
+int main(int argc, char **argv) {
+    static struct option longOptions[] = {
+        {"help", no_argument, 0, 'h' },
+        { 0,               0, 0,  0  }
+    };
+
+    int optionIndex;
+    int c;
+    optind = 1;
+    for (;;) {
+        // using getopt_long in case we want to add other options in the future
+        c = getopt_long(argc, argv, "h", longOptions, &optionIndex);
+        if (c == -1) {
+            break;
+        }
+        switch (c) {
+        case 'h': // falls through
+        default: // see unrecognized options
+            return usage();
+        }
+    }
+    return dump();
+
+}