Merge "sdcard: Use AID_ROOT constant"
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index bc88002..7723ec6 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -23,7 +23,6 @@
 LOCAL_C_INCLUDES := \
   $(LOCAL_PATH)/../adb \
   $(LOCAL_PATH)/../mkbootimg \
-  $(LOCAL_PATH)/../../extras/f2fs_utils \
 
 LOCAL_SRC_FILES := \
     bootimg_utils.cpp \
@@ -67,13 +66,7 @@
     libcutils \
     libgtest_host \
 
-# libf2fs_dlutils_host will dlopen("libf2fs_fmt_host_dyn")
 LOCAL_CFLAGS_linux := -DUSE_F2FS
-LOCAL_LDFLAGS_linux := -ldl -rdynamic -Wl,-rpath,.
-LOCAL_REQUIRED_MODULES_linux := libf2fs_fmt_host_dyn
-# The following libf2fs_* are from system/extras/f2fs_utils,
-# and do not use code in external/f2fs-tools.
-LOCAL_STATIC_LIBRARIES_linux += libf2fs_utils_host libf2fs_ioutils_host libf2fs_dlutils_host
 
 LOCAL_CXX_STL := libc++_static
 
@@ -87,9 +80,6 @@
 my_dist_files := $(LOCAL_BUILT_MODULE)
 my_dist_files += $(HOST_OUT_EXECUTABLES)/mke2fs$(HOST_EXECUTABLE_SUFFIX)
 my_dist_files += $(HOST_OUT_EXECUTABLES)/e2fsdroid$(HOST_EXECUTABLE_SUFFIX)
-ifeq ($(HOST_OS),linux)
-my_dist_files += $(HOST_LIBRARY_PATH)/libf2fs_fmt_host_dyn$(HOST_SHLIB_SUFFIX)
-endif
 $(call dist-for-goals,dist_files sdk win_sdk,$(my_dist_files))
 ifdef HOST_CROSS_OS
 # Archive fastboot.exe for win_sdk build.
diff --git a/fastboot/fs.cpp b/fastboot/fs.cpp
index 2d77dd6..8877b09 100644
--- a/fastboot/fs.cpp
+++ b/fastboot/fs.cpp
@@ -1,7 +1,6 @@
 #include "fs.h"
 
 #include "fastboot.h"
-#include "make_f2fs.h"
 
 #include <errno.h>
 #include <fcntl.h>
@@ -23,7 +22,6 @@
 #include <android-base/file.h>
 #include <android-base/stringprintf.h>
 #include <android-base/unique_fd.h>
-#include <sparse/sparse.h>
 
 using android::base::StringPrintf;
 using android::base::unique_fd;
@@ -160,16 +158,32 @@
 static int generate_f2fs_image(const char* fileName, long long partSize, const std::string& initial_dir,
                                unsigned /* unused */, unsigned /* unused */)
 {
+    const std::string exec_dir = android::base::GetExecutableDirectory();
+    const std::string mkf2fs_path = exec_dir + "/make_f2fs";
+    std::vector<const char*> mkf2fs_args = {mkf2fs_path.c_str()};
+
+    mkf2fs_args.push_back("-S");
+    std::string size_str = std::to_string(partSize);
+    mkf2fs_args.push_back(size_str.c_str());
+    mkf2fs_args.push_back("-f");
+    mkf2fs_args.push_back("-O");
+    mkf2fs_args.push_back("encrypt");
+    mkf2fs_args.push_back("-O");
+    mkf2fs_args.push_back("quota");
+    mkf2fs_args.push_back(fileName);
+    mkf2fs_args.push_back(nullptr);
+
+    int ret = exec_e2fs_cmd(mkf2fs_args[0], const_cast<char**>(mkf2fs_args.data()));
+    if (ret != 0) {
+        fprintf(stderr, "mkf2fs failed: %d\n", ret);
+        return -1;
+    }
+
     if (!initial_dir.empty()) {
         fprintf(stderr, "Unable to set initial directory on F2FS filesystem: %s\n", strerror(errno));
         return -1;
     }
-    unique_fd fd(open(fileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
-    if (fd == -1) {
-        fprintf(stderr, "Unable to open output file for F2FS filesystem: %s\n", strerror(errno));
-        return -1;
-    }
-    return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
+    return 0;
 }
 #endif
 
diff --git a/healthd/Health.cpp b/healthd/Health.cpp
index 74f3eec..d271811 100644
--- a/healthd/Health.cpp
+++ b/healthd/Health.cpp
@@ -13,6 +13,8 @@
 namespace V2_0 {
 namespace implementation {
 
+sp<Health> Health::instance_;
+
 Health::Health(struct healthd_config* c) {
     battery_monitor_ = std::make_unique<BatteryMonitor>();
     battery_monitor_->init(c);
@@ -154,7 +156,17 @@
     (void)unregisterCallbackInternal(who.promote());
 }
 
-// Methods from ::android::hidl::base::V1_0::IBase follow.
+sp<IHealth> Health::initInstance(struct healthd_config* c) {
+    if (instance_ == nullptr) {
+        instance_ = new Health(c);
+    }
+    return instance_;
+}
+
+sp<Health> Health::getImplementation() {
+    CHECK(instance_ != nullptr);
+    return instance_;
+}
 
 }  // namespace implementation
 }  // namespace V2_0
diff --git a/healthd/HealthServiceCommon.cpp b/healthd/HealthServiceCommon.cpp
index 260ca78..68ff526 100644
--- a/healthd/HealthServiceCommon.cpp
+++ b/healthd/HealthServiceCommon.cpp
@@ -33,9 +33,6 @@
 using android::hardware::health::V2_0::IHealth;
 using android::hardware::health::V2_0::implementation::Health;
 
-// see healthd_common.cpp
-android::sp<IHealth> gHealth;
-
 extern int healthd_main(void);
 
 static void binder_event(uint32_t /*epevents*/) {
@@ -63,8 +60,8 @@
     // TODO(b/68724651): healthd_board_* functions should be removed in health@2.0
     healthd_board_init(config);
 
-    gHealth = new ::android::hardware::health::V2_0::implementation::Health(config);
-    CHECK_EQ(gHealth->registerAsService(HEALTH_INSTANCE_NAME), android::OK)
+    android::sp<IHealth> service = Health::initInstance(config);
+    CHECK_EQ(service->registerAsService(HEALTH_INSTANCE_NAME), android::OK)
         << LOG_TAG << ": Failed to register HAL";
 
     LOG(INFO) << LOG_TAG << ": Hal init done";
@@ -85,7 +82,7 @@
 
     HealthInfo info;
     convertToHealthInfo(prop, info);
-    static_cast<Health*>(gHealth.get())->notifyListeners(info);
+    Health::getImplementation()->notifyListeners(info);
 }
 
 static struct healthd_mode_ops healthd_mode_service_2_0_ops = {
diff --git a/healthd/healthd_common.cpp b/healthd/healthd_common.cpp
index 19e600f..140c49d 100644
--- a/healthd/healthd_common.cpp
+++ b/healthd/healthd_common.cpp
@@ -91,7 +91,7 @@
 #ifndef HEALTHD_USE_HEALTH_2_0
 static BatteryMonitor* gBatteryMonitor = nullptr;
 #else
-extern sp<::android::hardware::health::V2_0::IHealth> gHealth;
+using ::android::hardware::health::V2_0::implementation::Health;
 #endif
 
 struct healthd_mode_ops *healthd_mode_ops = nullptr;
@@ -160,42 +160,42 @@
     status_t err = UNKNOWN_ERROR;
     switch (id) {
         case BATTERY_PROP_CHARGE_COUNTER: {
-            gHealth->getChargeCounter([&](Result r, int32_t v) {
+            Health::getImplementation()->getChargeCounter([&](Result r, int32_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_CURRENT_NOW: {
-            gHealth->getCurrentNow([&](Result r, int32_t v) {
+            Health::getImplementation()->getCurrentNow([&](Result r, int32_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_CURRENT_AVG: {
-            gHealth->getCurrentAverage([&](Result r, int32_t v) {
+            Health::getImplementation()->getCurrentAverage([&](Result r, int32_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_CAPACITY: {
-            gHealth->getCapacity([&](Result r, int32_t v) {
+            Health::getImplementation()->getCapacity([&](Result r, int32_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_ENERGY_COUNTER: {
-            gHealth->getEnergyCounter([&](Result r, int64_t v) {
+            Health::getImplementation()->getEnergyCounter([&](Result r, int64_t v) {
                 err = convertStatus(r);
                 val->valueInt64 = v;
             });
             break;
         }
         case BATTERY_PROP_BATTERY_STATUS: {
-            gHealth->getChargeStatus([&](Result r, BatteryStatus v) {
+            Health::getImplementation()->getChargeStatus([&](Result r, BatteryStatus v) {
                 err = convertStatus(r);
                 val->valueInt64 = static_cast<int64_t>(v);
             });
@@ -237,7 +237,7 @@
 #ifndef HEALTHD_USE_HEALTH_2_0
     healthd_battery_update_internal(gBatteryMonitor->update());
 #else
-    gHealth->update();
+    Health::getImplementation()->update();
 #endif
 }
 
@@ -249,7 +249,7 @@
     nativeHandle->data[0] = fd;
     ::android::hardware::hidl_handle handle;
     handle.setTo(nativeHandle, true /* shouldOwn */);
-    gHealth->debug(handle, {} /* options */);
+    Health::getImplementation()->debug(handle, {} /* options */);
 #endif
 
     fsync(fd);
diff --git a/healthd/include/health2/Health.h b/healthd/include/health2/Health.h
index 4e78380..012b95b 100644
--- a/healthd/include/health2/Health.h
+++ b/healthd/include/health2/Health.h
@@ -22,9 +22,16 @@
 
 struct Health : public IHealth, hidl_death_recipient {
   public:
+    static sp<IHealth> initInstance(struct healthd_config* c);
+    // Should only be called by implementation itself (-impl, -service).
+    // Clients should not call this function. Instead, initInstance() initializes and returns the
+    // global instance that has fewer functions.
+    // TODO(b/62229583): clean up and hide these functions after update() logic is simplified.
+    static sp<Health> getImplementation();
+
     Health(struct healthd_config* c);
 
-    // TODO(b/62229583): clean up and hide these functions.
+    // TODO(b/62229583): clean up and hide these functions after update() logic is simplified.
     void notifyListeners(const HealthInfo& info);
 
     // Methods from IHealth follow.
@@ -44,6 +51,8 @@
     void serviceDied(uint64_t cookie, const wp<IBase>& /* who */) override;
 
   private:
+    static sp<Health> instance_;
+
     std::mutex callbacks_lock_;
     std::vector<sp<IHealthInfoCallback>> callbacks_;
     std::unique_ptr<BatteryMonitor> battery_monitor_;
diff --git a/libcutils/Android.bp b/libcutils/Android.bp
index a21d1ca..d7413d1 100644
--- a/libcutils/Android.bp
+++ b/libcutils/Android.bp
@@ -74,12 +74,8 @@
     ],
 
     target: {
-        host: {
-            srcs: ["dlmalloc_stubs.c"],
-        },
         linux_bionic: {
             enabled: true,
-            exclude_srcs: ["dlmalloc_stubs.c"],
         },
         not_windows: {
             srcs: libcutils_nonwindows_sources + [
diff --git a/libcutils/dlmalloc_stubs.c b/libcutils/dlmalloc_stubs.c
deleted file mode 100644
index 2cff9dd..0000000
--- a/libcutils/dlmalloc_stubs.c
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (C) 2007 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 LOG_TAG "dlmalloc-stubs"
-
-#include "log/log.h"
-
-#define UNUSED __attribute__((__unused__))
-
-/*
- * Stubs for functions defined in bionic/libc/bionic/dlmalloc.c. These
- * are used in host builds, as the host libc will not contain these
- * functions.
- */
-void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*) UNUSED,
-                          void* arg UNUSED)
-{
-  ALOGW("Called host unimplemented stub: dlmalloc_inspect_all");
-}
-
-int dlmalloc_trim(size_t unused UNUSED)
-{
-  ALOGW("Called host unimplemented stub: dlmalloc_trim");
-  return 0;
-}
diff --git a/logcat/Android.bp b/logcat/Android.bp
index 729c8ff..afc7a01 100644
--- a/logcat/Android.bp
+++ b/logcat/Android.bp
@@ -67,6 +67,7 @@
     name: "logpersist.start",
     srcs: ["logpersist"],
     init_rc: ["logcatd.rc"],
+    required: ["logcatd"],
     symlinks: ["logpersist.stop", "logpersist.cat"],
     strip: {
         none: true,
diff --git a/rootdir/Android.mk b/rootdir/Android.mk
index 6aadf9e..560092e 100644
--- a/rootdir/Android.mk
+++ b/rootdir/Android.mk
@@ -241,7 +241,7 @@
 else # if _enforce_vndk_at_runtime is not true
 
 LOCAL_MODULE := ld.config.txt
-ifeq ($(PRODUCT_FULL_TREBLE)|$(SANITIZE_TARGET),true|)
+ifeq ($(PRODUCT_TREBLE_LINKER_NAMESPACES)|$(SANITIZE_TARGET),true|)
 LOCAL_SRC_FILES := etc/ld.config.txt
 else
 LOCAL_SRC_FILES := etc/ld.config.legacy.txt