Merge "Finally remove ZipString."
diff --git a/adb/daemon/usb.cpp b/adb/daemon/usb.cpp
index 1abae87..f4aa9fb 100644
--- a/adb/daemon/usb.cpp
+++ b/adb/daemon/usb.cpp
@@ -509,14 +509,16 @@
             }
 
             if (id.direction == TransferDirection::READ) {
-                HandleRead(id, event.res);
+                if (!HandleRead(id, event.res)) {
+                    return;
+                }
             } else {
                 HandleWrite(id);
             }
         }
     }
 
-    void HandleRead(TransferId id, int64_t size) {
+    bool HandleRead(TransferId id, int64_t size) {
         uint64_t read_idx = id.id % kUsbReadQueueDepth;
         IoBlock* block = &read_requests_[read_idx];
         block->pending = false;
@@ -526,7 +528,7 @@
         if (block->id().id != needed_read_id_) {
             LOG(VERBOSE) << "read " << block->id().id << " completed while waiting for "
                          << needed_read_id_;
-            return;
+            return true;
         }
 
         for (uint64_t id = needed_read_id_;; ++id) {
@@ -535,15 +537,22 @@
             if (current_block->pending) {
                 break;
             }
-            ProcessRead(current_block);
+            if (!ProcessRead(current_block)) {
+                return false;
+            }
             ++needed_read_id_;
         }
+
+        return true;
     }
 
-    void ProcessRead(IoBlock* block) {
+    bool ProcessRead(IoBlock* block) {
         if (!block->payload->empty()) {
             if (!incoming_header_.has_value()) {
-                CHECK_EQ(sizeof(amessage), block->payload->size());
+                if (block->payload->size() != sizeof(amessage)) {
+                    HandleError("received packet of unexpected length while reading header");
+                    return false;
+                }
                 amessage msg;
                 memcpy(&msg, block->payload->data(), sizeof(amessage));
                 LOG(DEBUG) << "USB read:" << dump_header(&msg);
@@ -551,7 +560,10 @@
             } else {
                 size_t bytes_left = incoming_header_->data_length - incoming_payload_.size();
                 Block payload = std::move(*block->payload);
-                CHECK_LE(payload.size(), bytes_left);
+                if (block->payload->size() > bytes_left) {
+                    HandleError("received too many bytes while waiting for payload");
+                    return false;
+                }
                 incoming_payload_.append(std::make_unique<Block>(std::move(payload)));
             }
 
@@ -570,6 +582,7 @@
 
         PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth);
         SubmitRead(block);
+        return true;
     }
 
     bool SubmitRead(IoBlock* block) {
diff --git a/fs_mgr/Android.bp b/fs_mgr/Android.bp
index ffde114..3d3503c 100644
--- a/fs_mgr/Android.bp
+++ b/fs_mgr/Android.bp
@@ -35,6 +35,7 @@
     export_include_dirs: ["include"],
     include_dirs: ["system/vold"],
     srcs: [
+        "file_wait.cpp",
         "fs_mgr.cpp",
         "fs_mgr_format.cpp",
         "fs_mgr_verity.cpp",
diff --git a/fs_mgr/file_wait.cpp b/fs_mgr/file_wait.cpp
new file mode 100644
index 0000000..cbf6845
--- /dev/null
+++ b/fs_mgr/file_wait.cpp
@@ -0,0 +1,235 @@
+// Copyright (C) 2019 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 <fs_mgr/file_wait.h>
+
+#include <limits.h>
+#if defined(__linux__)
+#include <poll.h>
+#include <sys/inotify.h>
+#endif
+#if defined(WIN32)
+#include <io.h>
+#else
+#include <unistd.h>
+#endif
+
+#include <functional>
+#include <thread>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
+
+namespace android {
+namespace fs_mgr {
+
+using namespace std::literals;
+using android::base::unique_fd;
+
+bool PollForFile(const std::string& path, const std::chrono::milliseconds relative_timeout) {
+    auto start_time = std::chrono::steady_clock::now();
+
+    while (true) {
+        if (!access(path.c_str(), F_OK) || errno != ENOENT) return true;
+
+        std::this_thread::sleep_for(50ms);
+
+        auto now = std::chrono::steady_clock::now();
+        auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
+        if (time_elapsed > relative_timeout) return false;
+    }
+}
+
+bool PollForFileDeleted(const std::string& path, const std::chrono::milliseconds relative_timeout) {
+    auto start_time = std::chrono::steady_clock::now();
+
+    while (true) {
+        if (access(path.c_str(), F_OK) && errno == ENOENT) return true;
+
+        std::this_thread::sleep_for(50ms);
+
+        auto now = std::chrono::steady_clock::now();
+        auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
+        if (time_elapsed > relative_timeout) return false;
+    }
+}
+
+#if defined(__linux__)
+class OneShotInotify {
+  public:
+    OneShotInotify(const std::string& path, uint32_t mask,
+                   const std::chrono::milliseconds relative_timeout);
+
+    bool Wait();
+
+  private:
+    bool CheckCompleted();
+    int64_t RemainingMs() const;
+    bool ConsumeEvents();
+
+    enum class Result { Success, Timeout, Error };
+    Result WaitImpl();
+
+    unique_fd inotify_fd_;
+    std::string path_;
+    uint32_t mask_;
+    std::chrono::time_point<std::chrono::steady_clock> start_time_;
+    std::chrono::milliseconds relative_timeout_;
+    bool finished_;
+};
+
+OneShotInotify::OneShotInotify(const std::string& path, uint32_t mask,
+                               const std::chrono::milliseconds relative_timeout)
+    : path_(path),
+      mask_(mask),
+      start_time_(std::chrono::steady_clock::now()),
+      relative_timeout_(relative_timeout),
+      finished_(false) {
+    // If the condition is already met, don't bother creating an inotify.
+    if (CheckCompleted()) return;
+
+    unique_fd inotify_fd(inotify_init1(IN_CLOEXEC | IN_NONBLOCK));
+    if (inotify_fd < 0) {
+        PLOG(ERROR) << "inotify_init1 failed";
+        return;
+    }
+
+    std::string watch_path;
+    if (mask == IN_CREATE) {
+        watch_path = android::base::Dirname(path);
+    } else {
+        watch_path = path;
+    }
+    if (inotify_add_watch(inotify_fd, watch_path.c_str(), mask) < 0) {
+        PLOG(ERROR) << "inotify_add_watch failed";
+        return;
+    }
+
+    // It's possible the condition was met before the add_watch. Check for
+    // this and abort early if so.
+    if (CheckCompleted()) return;
+
+    inotify_fd_ = std::move(inotify_fd);
+}
+
+bool OneShotInotify::Wait() {
+    Result result = WaitImpl();
+    if (result == Result::Success) return true;
+    if (result == Result::Timeout) return false;
+
+    // Some kind of error with inotify occurred, so fallback to a poll.
+    std::chrono::milliseconds timeout(RemainingMs());
+    if (mask_ == IN_CREATE) {
+        return PollForFile(path_, timeout);
+    } else if (mask_ == IN_DELETE_SELF) {
+        return PollForFileDeleted(path_, timeout);
+    } else {
+        LOG(ERROR) << "Unknown inotify mask: " << mask_;
+        return false;
+    }
+}
+
+OneShotInotify::Result OneShotInotify::WaitImpl() {
+    // If the operation completed super early, we'll never have created an
+    // inotify instance.
+    if (finished_) return Result::Success;
+    if (inotify_fd_ < 0) return Result::Error;
+
+    while (true) {
+        auto remaining_ms = RemainingMs();
+        if (remaining_ms <= 0) return Result::Timeout;
+
+        struct pollfd event = {
+                .fd = inotify_fd_,
+                .events = POLLIN,
+                .revents = 0,
+        };
+        int rv = poll(&event, 1, static_cast<int>(remaining_ms));
+        if (rv <= 0) {
+            if (rv == 0 || errno == EINTR) {
+                continue;
+            }
+            PLOG(ERROR) << "poll for inotify failed";
+            return Result::Error;
+        }
+        if (event.revents & POLLERR) {
+            LOG(ERROR) << "error reading inotify for " << path_;
+            return Result::Error;
+        }
+
+        // Note that we don't bother checking what kind of event it is, since
+        // it's cheap enough to just see if the initial condition is satisified.
+        // If it's not, we consume all the events available and continue.
+        if (CheckCompleted()) return Result::Success;
+        if (!ConsumeEvents()) return Result::Error;
+    }
+}
+
+bool OneShotInotify::CheckCompleted() {
+    if (mask_ == IN_CREATE) {
+        finished_ = !access(path_.c_str(), F_OK) || errno != ENOENT;
+    } else if (mask_ == IN_DELETE_SELF) {
+        finished_ = access(path_.c_str(), F_OK) && errno == ENOENT;
+    } else {
+        LOG(ERROR) << "Unexpected mask: " << mask_;
+    }
+    return finished_;
+}
+
+bool OneShotInotify::ConsumeEvents() {
+    // According to the manpage, this is enough to read at least one event.
+    static constexpr size_t kBufferSize = sizeof(struct inotify_event) + NAME_MAX + 1;
+    char buffer[kBufferSize];
+
+    do {
+        ssize_t rv = TEMP_FAILURE_RETRY(read(inotify_fd_, buffer, sizeof(buffer)));
+        if (rv <= 0) {
+            if (rv == 0 || errno == EAGAIN) {
+                return true;
+            }
+            PLOG(ERROR) << "read inotify failed";
+            return false;
+        }
+    } while (true);
+}
+
+int64_t OneShotInotify::RemainingMs() const {
+    auto remaining = (std::chrono::steady_clock::now() - start_time_);
+    auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(remaining);
+    return (relative_timeout_ - elapsed).count();
+}
+#endif
+
+bool WaitForFile(const std::string& path, const std::chrono::milliseconds relative_timeout) {
+#if defined(__linux__)
+    OneShotInotify inotify(path, IN_CREATE, relative_timeout);
+    return inotify.Wait();
+#else
+    return PollForFile(path, relative_timeout);
+#endif
+}
+
+// Wait at most |relative_timeout| milliseconds for |path| to stop existing.
+bool WaitForFileDeleted(const std::string& path, const std::chrono::milliseconds relative_timeout) {
+#if defined(__linux__)
+    OneShotInotify inotify(path, IN_DELETE_SELF, relative_timeout);
+    return inotify.Wait();
+#else
+    return PollForFileDeleted(path, relative_timeout);
+#endif
+}
+
+}  // namespace fs_mgr
+}  // namespace android
diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp
index 410209b..2a9a9d0 100644
--- a/fs_mgr/fs_mgr.cpp
+++ b/fs_mgr/fs_mgr.cpp
@@ -56,6 +56,7 @@
 #include <ext4_utils/ext4_utils.h>
 #include <ext4_utils/wipe.h>
 #include <fs_avb/fs_avb.h>
+#include <fs_mgr/file_wait.h>
 #include <fs_mgr_overlayfs.h>
 #include <libdm/dm.h>
 #include <liblp/metadata_format.h>
@@ -116,28 +117,6 @@
     FS_STAT_ENABLE_VERITY_FAILED = 0x80000,
 };
 
-// TODO: switch to inotify()
-bool fs_mgr_wait_for_file(const std::string& filename,
-                          const std::chrono::milliseconds relative_timeout,
-                          FileWaitMode file_wait_mode) {
-    auto start_time = std::chrono::steady_clock::now();
-
-    while (true) {
-        int rv = access(filename.c_str(), F_OK);
-        if (file_wait_mode == FileWaitMode::Exists) {
-            if (!rv || errno != ENOENT) return true;
-        } else if (file_wait_mode == FileWaitMode::DoesNotExist) {
-            if (rv && errno == ENOENT) return true;
-        }
-
-        std::this_thread::sleep_for(50ms);
-
-        auto now = std::chrono::steady_clock::now();
-        auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
-        if (time_elapsed > relative_timeout) return false;
-    }
-}
-
 static void log_fs_stat(const std::string& blk_device, int fs_stat) {
     if ((fs_stat & FS_STAT_IS_EXT4) == 0) return; // only log ext4
     std::string msg =
@@ -1103,8 +1082,7 @@
             continue;
         }
 
-        if (current_entry.fs_mgr_flags.wait &&
-            !fs_mgr_wait_for_file(current_entry.blk_device, 20s)) {
+        if (current_entry.fs_mgr_flags.wait && !WaitForFile(current_entry.blk_device, 20s)) {
             LERROR << "Skipping '" << current_entry.blk_device << "' during mount_all";
             continue;
         }
@@ -1373,7 +1351,7 @@
         }
 
         // First check the filesystem if requested.
-        if (fstab_entry.fs_mgr_flags.wait && !fs_mgr_wait_for_file(n_blk_device, 20s)) {
+        if (fstab_entry.fs_mgr_flags.wait && !WaitForFile(n_blk_device, 20s)) {
             LERROR << "Skipping mounting '" << n_blk_device << "'";
             continue;
         }
@@ -1576,7 +1554,7 @@
             fprintf(zram_fp.get(), "%" PRId64 "\n", entry.zram_size);
         }
 
-        if (entry.fs_mgr_flags.wait && !fs_mgr_wait_for_file(entry.blk_device, 20s)) {
+        if (entry.fs_mgr_flags.wait && !WaitForFile(entry.blk_device, 20s)) {
             LERROR << "Skipping mkswap for '" << entry.blk_device << "'";
             ret = false;
             continue;
diff --git a/fs_mgr/fs_mgr_dm_linear.cpp b/fs_mgr/fs_mgr_dm_linear.cpp
index ee6ffdb..1f21a71 100644
--- a/fs_mgr/fs_mgr_dm_linear.cpp
+++ b/fs_mgr/fs_mgr_dm_linear.cpp
@@ -38,6 +38,7 @@
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
+#include <fs_mgr/file_wait.h>
 #include <liblp/reader.h>
 
 #include "fs_mgr_priv.h"
@@ -128,7 +129,7 @@
         return false;
     }
     if (timeout_ms > std::chrono::milliseconds::zero()) {
-        if (!fs_mgr_wait_for_file(*path, timeout_ms, FileWaitMode::Exists)) {
+        if (!WaitForFile(*path, timeout_ms)) {
             DestroyLogicalPartition(name, {});
             LERROR << "Timed out waiting for device path: " << *path;
             return false;
@@ -202,7 +203,7 @@
     if (!dm.DeleteDevice(name)) {
         return false;
     }
-    if (!path.empty() && !fs_mgr_wait_for_file(path, timeout_ms, FileWaitMode::DoesNotExist)) {
+    if (!path.empty() && !WaitForFileDeleted(path, timeout_ms)) {
         LERROR << "Timed out waiting for device path to unlink: " << path;
         return false;
     }
diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp
index ed8cce6..05ca5fc 100644
--- a/fs_mgr/fs_mgr_overlayfs.cpp
+++ b/fs_mgr/fs_mgr_overlayfs.cpp
@@ -44,6 +44,7 @@
 #include <android-base/unique_fd.h>
 #include <ext4_utils/ext4_utils.h>
 #include <fs_mgr.h>
+#include <fs_mgr/file_wait.h>
 #include <fs_mgr_dm_linear.h>
 #include <fs_mgr_overlayfs.h>
 #include <fstab/fstab.h>
@@ -867,7 +868,7 @@
             scratch_can_be_mounted = false;
             auto scratch_device = fs_mgr_overlayfs_scratch_device();
             if (fs_mgr_overlayfs_scratch_can_be_mounted(scratch_device) &&
-                fs_mgr_wait_for_file(scratch_device, 10s)) {
+                WaitForFile(scratch_device, 10s)) {
                 const auto mount_type = fs_mgr_overlayfs_scratch_mount_type();
                 if (fs_mgr_overlayfs_mount_scratch(scratch_device, mount_type,
                                                    true /* readonly */)) {
diff --git a/fs_mgr/fs_mgr_priv.h b/fs_mgr/fs_mgr_priv.h
index c36fd3d..3a33cf3 100644
--- a/fs_mgr/fs_mgr_priv.h
+++ b/fs_mgr/fs_mgr_priv.h
@@ -88,12 +88,6 @@
 
 using namespace std::chrono_literals;
 
-enum class FileWaitMode { Exists, DoesNotExist };
-
-bool fs_mgr_wait_for_file(const std::string& filename,
-                          const std::chrono::milliseconds relative_timeout,
-                          FileWaitMode wait_mode = FileWaitMode::Exists);
-
 bool fs_mgr_set_blk_ro(const std::string& blockdev, bool readonly = true);
 bool fs_mgr_update_for_slotselect(android::fs_mgr::Fstab* fstab);
 bool fs_mgr_is_device_unlocked();
diff --git a/fs_mgr/fs_mgr_verity.cpp b/fs_mgr/fs_mgr_verity.cpp
index 1deb1ac..be8077b 100644
--- a/fs_mgr/fs_mgr_verity.cpp
+++ b/fs_mgr/fs_mgr_verity.cpp
@@ -35,6 +35,7 @@
 #include <android-base/unique_fd.h>
 #include <crypto_utils/android_pubkey.h>
 #include <cutils/properties.h>
+#include <fs_mgr/file_wait.h>
 #include <libdm/dm.h>
 #include <logwrap/logwrap.h>
 #include <openssl/obj_mac.h>
@@ -529,7 +530,7 @@
     }
 
     // make sure we've set everything up properly
-    if (wait_for_verity_dev && !fs_mgr_wait_for_file(entry->blk_device, 1s)) {
+    if (wait_for_verity_dev && !WaitForFile(entry->blk_device, 1s)) {
         goto out;
     }
 
diff --git a/fs_mgr/include/fs_mgr/file_wait.h b/fs_mgr/include/fs_mgr/file_wait.h
new file mode 100644
index 0000000..74d160e
--- /dev/null
+++ b/fs_mgr/include/fs_mgr/file_wait.h
@@ -0,0 +1,34 @@
+// Copyright (C) 2019 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.
+
+#pragma once
+
+#include <chrono>
+#include <string>
+
+namespace android {
+namespace fs_mgr {
+
+// Wait at most |relative_timeout| milliseconds for |path| to exist. dirname(path)
+// must already exist. For example, to wait on /dev/block/dm-6, /dev/block must
+// be a valid directory.
+bool WaitForFile(const std::string& path, const std::chrono::milliseconds relative_timeout);
+
+// Wait at most |relative_timeout| milliseconds for |path| to stop existing.
+// Note that this only returns true if the inode itself no longer exists, i.e.,
+// all outstanding file descriptors have been closed.
+bool WaitForFileDeleted(const std::string& path, const std::chrono::milliseconds relative_timeout);
+
+}  // namespace fs_mgr
+}  // namespace android
diff --git a/fs_mgr/libdm/Android.bp b/fs_mgr/libdm/Android.bp
index c8c2d83..21255df 100644
--- a/fs_mgr/libdm/Android.bp
+++ b/fs_mgr/libdm/Android.bp
@@ -46,6 +46,7 @@
     static_libs: [
         "libdm",
         "libbase",
+        "libfs_mgr",
         "liblog",
     ],
     srcs: [
diff --git a/fs_mgr/libdm/dm.cpp b/fs_mgr/libdm/dm.cpp
index c2917a4..788039d 100644
--- a/fs_mgr/libdm/dm.cpp
+++ b/fs_mgr/libdm/dm.cpp
@@ -302,6 +302,26 @@
     return true;
 }
 
+bool DeviceMapper::GetDeviceNumber(const std::string& name, dev_t* dev) {
+    struct dm_ioctl io;
+    InitIo(&io, name);
+    if (ioctl(fd_, DM_DEV_STATUS, &io) < 0) {
+        PLOG(WARNING) << "DM_DEV_STATUS failed for " << name;
+        return false;
+    }
+    *dev = io.dev;
+    return true;
+}
+
+bool DeviceMapper::GetDeviceString(const std::string& name, std::string* dev) {
+    dev_t num;
+    if (!GetDeviceNumber(name, &num)) {
+        return false;
+    }
+    *dev = std::to_string(major(num)) + ":" + std::to_string(minor(num));
+    return true;
+}
+
 bool DeviceMapper::GetTableStatus(const std::string& name, std::vector<TargetInfo>* table) {
     return GetTable(name, 0, table);
 }
diff --git a/fs_mgr/libdm/dm_test.cpp b/fs_mgr/libdm/dm_test.cpp
index dc47c33..6219923 100644
--- a/fs_mgr/libdm/dm_test.cpp
+++ b/fs_mgr/libdm/dm_test.cpp
@@ -132,8 +132,8 @@
     // Define a 2-sector device, with each sector mapping to the first sector
     // of one of our loop devices.
     DmTable table;
-    ASSERT_TRUE(table.AddTarget(make_unique<DmTargetLinear>(0, 1, loop_a.device(), 0)));
-    ASSERT_TRUE(table.AddTarget(make_unique<DmTargetLinear>(1, 1, loop_b.device(), 0)));
+    ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop_a.device(), 0));
+    ASSERT_TRUE(table.Emplace<DmTargetLinear>(1, 1, loop_b.device(), 0));
     ASSERT_TRUE(table.valid());
 
     TempDevice dev("libdm-test-dm-linear", table);
@@ -141,6 +141,16 @@
     ASSERT_FALSE(dev.path().empty());
     ASSERT_TRUE(dev.WaitForUdev());
 
+    auto& dm = DeviceMapper::Instance();
+
+    dev_t dev_number;
+    ASSERT_TRUE(dm.GetDeviceNumber(dev.name(), &dev_number));
+    ASSERT_NE(dev_number, 0);
+
+    std::string dev_string;
+    ASSERT_TRUE(dm.GetDeviceString(dev.name(), &dev_string));
+    ASSERT_FALSE(dev_string.empty());
+
     // Note: a scope is needed to ensure that there are no open descriptors
     // when we go to close the device.
     {
@@ -157,7 +167,6 @@
     }
 
     // Test GetTableStatus.
-    DeviceMapper& dm = DeviceMapper::Instance();
     vector<DeviceMapper::TargetInfo> targets;
     ASSERT_TRUE(dm.GetTableStatus(dev.name(), &targets));
     ASSERT_EQ(targets.size(), 2);
diff --git a/fs_mgr/libdm/include/libdm/dm.h b/fs_mgr/libdm/include/libdm/dm.h
index d7e8aa9..3d223e3 100644
--- a/fs_mgr/libdm/include/libdm/dm.h
+++ b/fs_mgr/libdm/include/libdm/dm.h
@@ -20,6 +20,7 @@
 #include <fcntl.h>
 #include <linux/dm-ioctl.h>
 #include <linux/kdev_t.h>
+#include <linux/types.h>
 #include <stdint.h>
 #include <sys/sysmacros.h>
 #include <unistd.h>
@@ -111,6 +112,13 @@
     // parameter is not set.
     bool GetDmDevicePathByName(const std::string& name, std::string* path);
 
+    // Returns the dev_t for the named device-mapper node.
+    bool GetDeviceNumber(const std::string& name, dev_t* dev);
+
+    // Returns a major:minor string for the named device-mapper node, that can
+    // be used as inputs to DmTargets that take a block device.
+    bool GetDeviceString(const std::string& name, std::string* dev);
+
     // The only way to create a DeviceMapper object.
     static DeviceMapper& Instance();
 
diff --git a/fs_mgr/libdm/include/libdm/loop_control.h b/fs_mgr/libdm/include/libdm/loop_control.h
index e6e83f4..6b4c2d8 100644
--- a/fs_mgr/libdm/include/libdm/loop_control.h
+++ b/fs_mgr/libdm/include/libdm/loop_control.h
@@ -35,6 +35,9 @@
     // Detach the loop device given by 'loopdev' from the attached backing file.
     bool Detach(const std::string& loopdev) const;
 
+    // Enable Direct I/O on a loop device. This requires kernel 4.9+.
+    static bool EnableDirectIo(int fd);
+
     LoopControl(const LoopControl&) = delete;
     LoopControl& operator=(const LoopControl&) = delete;
     LoopControl& operator=(LoopControl&&) = default;
diff --git a/fs_mgr/libdm/loop_control.cpp b/fs_mgr/libdm/loop_control.cpp
index 0beb1a6..16bf4b0 100644
--- a/fs_mgr/libdm/loop_control.cpp
+++ b/fs_mgr/libdm/loop_control.cpp
@@ -91,6 +91,27 @@
     return true;
 }
 
+bool LoopControl::EnableDirectIo(int fd) {
+#if !defined(LOOP_SET_BLOCK_SIZE)
+    static constexpr int LOOP_SET_BLOCK_SIZE = 0x4C09;
+#endif
+#if !defined(LOOP_SET_DIRECT_IO)
+    static constexpr int LOOP_SET_DIRECT_IO = 0x4C08;
+#endif
+
+    // Note: the block size has to be >= the logical block size of the underlying
+    // block device, *not* the filesystem block size.
+    if (ioctl(fd, LOOP_SET_BLOCK_SIZE, 4096)) {
+        PLOG(ERROR) << "Could not set loop device block size";
+        return false;
+    }
+    if (ioctl(fd, LOOP_SET_DIRECT_IO, 1)) {
+        PLOG(ERROR) << "Could not set loop direct IO";
+        return false;
+    }
+    return true;
+}
+
 LoopDevice::LoopDevice(int fd, bool auto_close) : fd_(fd), owns_fd_(auto_close) {
     Init();
 }
diff --git a/fs_mgr/liblp/partition_opener.cpp b/fs_mgr/liblp/partition_opener.cpp
index 3b12213..cc4a882 100644
--- a/fs_mgr/liblp/partition_opener.cpp
+++ b/fs_mgr/liblp/partition_opener.cpp
@@ -64,6 +64,12 @@
         PERROR << __PRETTY_FUNCTION__ << "BLKALIGNOFF failed on " << block_device;
         return false;
     }
+    // The kernel can return -1 here when misaligned devices are stacked (i.e.
+    // device-mapper).
+    if (alignment_offset == -1) {
+        alignment_offset = 0;
+    }
+
     int logical_block_size;
     if (ioctl(fd, BLKSSZGET, &logical_block_size) < 0) {
         PERROR << __PRETTY_FUNCTION__ << "BLKSSZGET failed on " << block_device;
diff --git a/fs_mgr/tests/Android.bp b/fs_mgr/tests/Android.bp
index eb9f525..83668e9 100644
--- a/fs_mgr/tests/Android.bp
+++ b/fs_mgr/tests/Android.bp
@@ -25,6 +25,7 @@
         "libfstab",
     ],
     srcs: [
+        "file_wait_test.cpp",
         "fs_mgr_test.cpp",
     ],
 
diff --git a/fs_mgr/tests/file_wait_test.cpp b/fs_mgr/tests/file_wait_test.cpp
new file mode 100644
index 0000000..cc8b143
--- /dev/null
+++ b/fs_mgr/tests/file_wait_test.cpp
@@ -0,0 +1,91 @@
+// Copyright (C) 2019 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 <chrono>
+#include <string>
+#include <thread>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+#include <fs_mgr/file_wait.h>
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+using android::base::unique_fd;
+using android::fs_mgr::WaitForFile;
+using android::fs_mgr::WaitForFileDeleted;
+
+class FileWaitTest : public ::testing::Test {
+  protected:
+    void SetUp() override {
+        const ::testing::TestInfo* tinfo = ::testing::UnitTest::GetInstance()->current_test_info();
+        test_file_ = temp_dir_.path + "/"s + tinfo->name();
+    }
+
+    void TearDown() override { unlink(test_file_.c_str()); }
+
+    TemporaryDir temp_dir_;
+    std::string test_file_;
+};
+
+TEST_F(FileWaitTest, FileExists) {
+    unique_fd fd(open(test_file_.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0700));
+    ASSERT_GE(fd, 0);
+
+    ASSERT_TRUE(WaitForFile(test_file_, 500ms));
+    ASSERT_FALSE(WaitForFileDeleted(test_file_, 500ms));
+}
+
+TEST_F(FileWaitTest, FileDoesNotExist) {
+    ASSERT_FALSE(WaitForFile(test_file_, 500ms));
+    ASSERT_TRUE(WaitForFileDeleted(test_file_, 500ms));
+}
+
+TEST_F(FileWaitTest, CreateAsync) {
+    std::thread thread([this] {
+        std::this_thread::sleep_for(std::chrono::seconds(1));
+        unique_fd fd(open(test_file_.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0700));
+    });
+    EXPECT_TRUE(WaitForFile(test_file_, 3s));
+    thread.join();
+}
+
+TEST_F(FileWaitTest, CreateOtherAsync) {
+    std::thread thread([this] {
+        std::this_thread::sleep_for(std::chrono::seconds(1));
+        unique_fd fd(open(test_file_.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0700));
+    });
+    EXPECT_FALSE(WaitForFile(test_file_ + ".wontexist", 2s));
+    thread.join();
+}
+
+TEST_F(FileWaitTest, DeleteAsync) {
+    // Note: need to close the file, otherwise inotify considers it not deleted.
+    {
+        unique_fd fd(open(test_file_.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0700));
+        ASSERT_GE(fd, 0);
+    }
+
+    std::thread thread([this] {
+        std::this_thread::sleep_for(std::chrono::seconds(1));
+        unlink(test_file_.c_str());
+    });
+    EXPECT_TRUE(WaitForFileDeleted(test_file_, 3s));
+    thread.join();
+}
+
+TEST_F(FileWaitTest, BadPath) {
+    ASSERT_FALSE(WaitForFile("/this/path/does/not/exist", 5ms));
+    EXPECT_EQ(errno, ENOENT);
+}
diff --git a/init/Android.bp b/init/Android.bp
index fa0a35c..9c1ed15 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -63,6 +63,7 @@
         "libavb",
         "libc++fs",
         "libcgrouprc_format",
+        "libmodprobe",
         "libprotobuf-cpp-lite",
         "libpropertyinfoserializer",
         "libpropertyinfoparser",
diff --git a/init/Android.mk b/init/Android.mk
index 0a3e8c7..b24f757 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -110,6 +110,7 @@
     libdexfile_support \
     libunwindstack \
     libbacktrace \
+    libmodprobe \
 
 LOCAL_SANITIZE := signed-integer-overflow
 # First stage init is weird: it may start without stdout/stderr, and no /proc.
diff --git a/init/README.md b/init/README.md
index 6868378..8179bff 100644
--- a/init/README.md
+++ b/init/README.md
@@ -196,9 +196,9 @@
 
 `interface <interface name> <instance name>`
 > Associates this service with a list of the HIDL services that it provides. The interface name
-  must be a fully-qualified name and not a value name. This is used to allow hwservicemanager to
-  lazily start services. When multiple interfaces are served, this tag should be used multiple
-  times.
+  must be a fully-qualified name and not a value name. For instance, this is used to allow
+  hwservicemanager to lazily start services. When multiple interfaces are served, this tag should
+  be used multiple times.
   For example: interface vendor.foo.bar@1.0::IBaz default
 
 `ioprio <class> <priority>`
diff --git a/init/first_stage_init.cpp b/init/first_stage_init.cpp
index 5d64f41..17387e2 100644
--- a/init/first_stage_init.cpp
+++ b/init/first_stage_init.cpp
@@ -33,6 +33,7 @@
 #include <android-base/chrono_utils.h>
 #include <android-base/file.h>
 #include <android-base/logging.h>
+#include <modprobe/modprobe.h>
 #include <private/android_filesystem_config.h>
 
 #include "debug_ramdisk.h"
@@ -192,6 +193,11 @@
         old_root_dir.reset();
     }
 
+    Modprobe m({"/lib/modules"});
+    if (!m.LoadListedModules()) {
+        LOG(FATAL) << "Failed to load kernel modules";
+    }
+
     if (ForceNormalBoot()) {
         mkdir("/first_stage_ramdisk", 0755);
         // SwitchRoot() must be called with a mount point as the target, so we bind mount the
diff --git a/init/modalias_handler.cpp b/init/modalias_handler.cpp
index a511156..07b05d8 100644
--- a/init/modalias_handler.cpp
+++ b/init/modalias_handler.cpp
@@ -16,147 +16,20 @@
 
 #include "modalias_handler.h"
 
-#include <fnmatch.h>
-#include <sys/syscall.h>
-
-#include <algorithm>
-#include <functional>
 #include <string>
 #include <vector>
 
-#include <android-base/chrono_utils.h>
-#include <android-base/logging.h>
-#include <android-base/unique_fd.h>
-
-#include "parser.h"
+#include <modprobe/modprobe.h>
 
 namespace android {
 namespace init {
 
-Result<void> ModaliasHandler::ParseDepCallback(std::vector<std::string>&& args) {
-    std::vector<std::string> deps;
-
-    // Set first item as our modules path
-    std::string::size_type pos = args[0].find(':');
-    if (pos != std::string::npos) {
-        deps.emplace_back(args[0].substr(0, pos));
-    } else {
-        return Error() << "dependency lines must start with name followed by ':'";
-    }
-
-    // Remaining items are dependencies of our module
-    for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
-        deps.push_back(*arg);
-    }
-
-    // Key is striped module name to match names in alias file
-    std::size_t start = args[0].find_last_of('/');
-    std::size_t end = args[0].find(".ko:");
-    if ((end - start) <= 1) return Error() << "malformed dependency line";
-    auto mod_name = args[0].substr(start + 1, (end - start) - 1);
-    // module names can have '-', but their file names will have '_'
-    std::replace(mod_name.begin(), mod_name.end(), '-', '_');
-    this->module_deps_[mod_name] = deps;
-
-    return {};
-}
-
-Result<void> ModaliasHandler::ParseAliasCallback(std::vector<std::string>&& args) {
-    auto it = args.begin();
-    const std::string& type = *it++;
-
-    if (type != "alias") {
-        return Error() << "we only handle alias lines, got: " << type;
-    }
-
-    if (args.size() != 3) {
-        return Error() << "alias lines must have 3 entries";
-    }
-
-    std::string& alias = *it++;
-    std::string& module_name = *it++;
-    this->module_aliases_.emplace_back(alias, module_name);
-
-    return {};
-}
-
-ModaliasHandler::ModaliasHandler() {
-    using namespace std::placeholders;
-
-    static const std::string base_paths[] = {
-            "/vendor/lib/modules/",
-            "/lib/modules/",
-            "/odm/lib/modules/",
-    };
-
-    Parser alias_parser;
-    auto alias_callback = std::bind(&ModaliasHandler::ParseAliasCallback, this, _1);
-    alias_parser.AddSingleLineParser("alias", alias_callback);
-    for (const auto& base_path : base_paths) alias_parser.ParseConfig(base_path + "modules.alias");
-
-    Parser dep_parser;
-    auto dep_callback = std::bind(&ModaliasHandler::ParseDepCallback, this, _1);
-    dep_parser.AddSingleLineParser("", dep_callback);
-    for (const auto& base_path : base_paths) dep_parser.ParseConfig(base_path + "modules.dep");
-}
-
-Result<void> ModaliasHandler::Insmod(const std::string& path_name, const std::string& args) {
-    base::unique_fd fd(
-            TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
-    if (fd == -1) return ErrnoError() << "Could not open module '" << path_name << "'";
-
-    int ret = syscall(__NR_finit_module, fd.get(), args.c_str(), 0);
-    if (ret != 0) {
-        if (errno == EEXIST) {
-            // Module already loaded
-            return {};
-        }
-        return ErrnoError() << "Failed to insmod '" << path_name << "' with args '" << args << "'";
-    }
-
-    LOG(INFO) << "Loaded kernel module " << path_name;
-    return {};
-}
-
-Result<void> ModaliasHandler::InsmodWithDeps(const std::string& module_name,
-                                             const std::string& args) {
-    if (module_name.empty()) {
-        return Error() << "Need valid module name";
-    }
-
-    auto it = module_deps_.find(module_name);
-    if (it == module_deps_.end()) {
-        return Error() << "Module '" << module_name << "' not in dependency file";
-    }
-    auto& dependencies = it->second;
-
-    // load module dependencies in reverse order
-    for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) {
-        if (auto result = Insmod(*dep, ""); !result) return result;
-    }
-
-    // load target module itself with args
-    return Insmod(dependencies[0], args);
-}
+ModaliasHandler::ModaliasHandler(const std::vector<std::string>& base_paths)
+    : modprobe_(base_paths) {}
 
 void ModaliasHandler::HandleUevent(const Uevent& uevent) {
     if (uevent.modalias.empty()) return;
-
-    for (const auto& [alias, module] : module_aliases_) {
-        if (fnmatch(alias.c_str(), uevent.modalias.c_str(), 0) != 0) continue;  // Keep looking
-
-        LOG(DEBUG) << "Loading kernel module '" << module << "' for alias '" << uevent.modalias
-                   << "'";
-
-        if (auto result = InsmodWithDeps(module, ""); !result) {
-            LOG(ERROR) << "Cannot load module: " << result.error();
-            // try another one since there may be another match
-            continue;
-        }
-
-        // loading was successful
-        return;
-    }
+    modprobe_.LoadWithAliases(uevent.modalias, true);
 }
 
 }  // namespace init
diff --git a/init/modalias_handler.h b/init/modalias_handler.h
index 7d0afde..ce89a05 100644
--- a/init/modalias_handler.h
+++ b/init/modalias_handler.h
@@ -17,10 +17,10 @@
 #pragma once
 
 #include <string>
-#include <unordered_map>
 #include <vector>
 
-#include "result.h"
+#include <modprobe/modprobe.h>
+
 #include "uevent.h"
 #include "uevent_handler.h"
 
@@ -29,20 +29,13 @@
 
 class ModaliasHandler : public UeventHandler {
   public:
-    ModaliasHandler();
+    ModaliasHandler(const std::vector<std::string>&);
     virtual ~ModaliasHandler() = default;
 
     void HandleUevent(const Uevent& uevent) override;
 
   private:
-    Result<void> InsmodWithDeps(const std::string& module_name, const std::string& args);
-    Result<void> Insmod(const std::string& path_name, const std::string& args);
-
-    Result<void> ParseDepCallback(std::vector<std::string>&& args);
-    Result<void> ParseAliasCallback(std::vector<std::string>&& args);
-
-    std::vector<std::pair<std::string, std::string>> module_aliases_;
-    std::unordered_map<std::string, std::vector<std::string>> module_deps_;
+    Modprobe modprobe_;
 };
 
 }  // namespace init
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index d700c46..f550bc2 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -251,7 +251,8 @@
             std::move(ueventd_configuration.firmware_directories)));
 
     if (ueventd_configuration.enable_modalias_handling) {
-        uevent_handlers.emplace_back(std::make_unique<ModaliasHandler>());
+        std::vector<std::string> base_paths = {"/odm/lib/modules", "/vendor/lib/modules"};
+        uevent_handlers.emplace_back(std::make_unique<ModaliasHandler>(base_paths));
     }
     UeventListener uevent_listener(ueventd_configuration.uevent_socket_rcvbuf_size);
 
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
index 6606030..5b5f2eb 100644
--- a/libcutils/fs_config.cpp
+++ b/libcutils/fs_config.cpp
@@ -46,19 +46,6 @@
 using android::base::EndsWith;
 using android::base::StartsWith;
 
-// My kingdom for <endian.h>
-static inline uint16_t get2LE(const uint8_t* src) {
-    return src[0] | (src[1] << 8);
-}
-
-static inline uint64_t get8LE(const uint8_t* src) {
-    uint32_t low, high;
-
-    low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
-    high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
-    return ((uint64_t)high << 32) | (uint64_t)low;
-}
-
 #define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
 
 // Rules for directories.
@@ -333,7 +320,7 @@
 
         while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
             char* prefix;
-            uint16_t host_len = get2LE((const uint8_t*)&header.len);
+            uint16_t host_len = header.len;
             ssize_t len, remainder = host_len - sizeof(header);
             if (remainder <= 0) {
                 ALOGE("%s len is corrupted", conf[which][dir]);
@@ -358,10 +345,10 @@
             if (fs_config_cmp(dir, prefix, len, path, plen)) {
                 free(prefix);
                 close(fd);
-                *uid = get2LE((const uint8_t*)&(header.uid));
-                *gid = get2LE((const uint8_t*)&(header.gid));
-                *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
-                *capabilities = get8LE((const uint8_t*)&(header.capabilities));
+                *uid = header.uid;
+                *gid = header.gid;
+                *mode = (*mode & (~07777)) | header.mode;
+                *capabilities = header.capabilities;
                 return;
             }
             free(prefix);
@@ -379,21 +366,3 @@
     *mode = (*mode & (~07777)) | pc->mode;
     *capabilities = pc->capabilities;
 }
-
-ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
-    struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
-    size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
-
-    if ((length < len) || (len > UINT16_MAX)) {
-        return -ENOSPC;
-    }
-    memset(p, 0, len);
-    uint16_t host_len = len;
-    p->len = get2LE((const uint8_t*)&host_len);
-    p->mode = get2LE((const uint8_t*)&(pc->mode));
-    p->uid = get2LE((const uint8_t*)&(pc->uid));
-    p->gid = get2LE((const uint8_t*)&(pc->gid));
-    p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
-    strcpy(p->prefix, pc->prefix);
-    return len;
-}
diff --git a/libcutils/include/private/fs_config.h b/libcutils/include/private/fs_config.h
index 8926491..603cf1a 100644
--- a/libcutils/include/private/fs_config.h
+++ b/libcutils/include/private/fs_config.h
@@ -74,8 +74,6 @@
 void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
                unsigned* mode, uint64_t* capabilities);
 
-ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc);
-
 __END_DECLS
 
 #endif /* _LIBS_CUTILS_PRIVATE_FS_CONFIG_H */
diff --git a/libmodprobe/Android.bp b/libmodprobe/Android.bp
new file mode 100644
index 0000000..a2824d1
--- /dev/null
+++ b/libmodprobe/Android.bp
@@ -0,0 +1,30 @@
+cc_library_static {
+    name: "libmodprobe",
+    cflags: [
+        "-Werror",
+    ],
+    recovery_available: true,
+    srcs: [
+        "libmodprobe.cpp",
+        "libmodprobe_ext.cpp",
+    ],
+    shared_libs: [
+        "libbase",
+    ],
+    export_include_dirs: ["include/"],
+}
+
+cc_test {
+    name: "libmodprobe_tests",
+    cflags: ["-Werror"],
+    shared_libs: [
+        "libbase",
+    ],
+    local_include_dirs: ["include/"],
+    srcs: [
+        "libmodprobe_test.cpp",
+        "libmodprobe.cpp",
+        "libmodprobe_ext_test.cpp",
+    ],
+    test_suites: ["device-tests"],
+}
diff --git a/libmodprobe/include/modprobe/modprobe.h b/libmodprobe/include/modprobe/modprobe.h
new file mode 100644
index 0000000..0ec766a
--- /dev/null
+++ b/libmodprobe/include/modprobe/modprobe.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+class Modprobe {
+  public:
+    Modprobe(const std::vector<std::string>&);
+
+    bool LoadListedModules();
+    bool LoadWithAliases(const std::string& module_name, bool strict);
+
+  private:
+    std::string MakeCanonical(const std::string& module_path);
+    bool InsmodWithDeps(const std::string& module_name);
+    bool Insmod(const std::string& path_name);
+    std::vector<std::string> GetDependencies(const std::string& module);
+    bool ModuleExists(const std::string& module_name);
+
+    bool ParseDepCallback(const std::string& base_path, const std::vector<std::string>& args);
+    bool ParseAliasCallback(const std::vector<std::string>& args);
+    bool ParseSoftdepCallback(const std::vector<std::string>& args);
+    bool ParseLoadCallback(const std::vector<std::string>& args);
+    bool ParseOptionsCallback(const std::vector<std::string>& args);
+    void ParseCfg(const std::string& cfg, std::function<bool(const std::vector<std::string>&)> f);
+
+    std::vector<std::pair<std::string, std::string>> module_aliases_;
+    std::unordered_map<std::string, std::vector<std::string>> module_deps_;
+    std::vector<std::pair<std::string, std::string>> module_pre_softdep_;
+    std::vector<std::pair<std::string, std::string>> module_post_softdep_;
+    std::vector<std::string> module_load_;
+    std::unordered_map<std::string, std::string> module_options_;
+};
diff --git a/libmodprobe/libmodprobe.cpp b/libmodprobe/libmodprobe.cpp
new file mode 100644
index 0000000..01cf2e3
--- /dev/null
+++ b/libmodprobe/libmodprobe.cpp
@@ -0,0 +1,321 @@
+/*
+ * Copyright (C) 2018 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 <modprobe/modprobe.h>
+
+#include <fnmatch.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <algorithm>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <android-base/chrono_utils.h>
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+
+std::string Modprobe::MakeCanonical(const std::string& module_path) {
+    auto start = module_path.find_last_of('/');
+    if (start == std::string::npos) {
+        start = 0;
+    } else {
+        start += 1;
+    }
+    auto end = module_path.size();
+    if (android::base::EndsWith(module_path, ".ko")) {
+        end -= 3;
+    }
+    if ((end - start) <= 1) {
+        LOG(ERROR) << "malformed module name: " << module_path;
+        return "";
+    }
+    std::string module_name = module_path.substr(start, end - start);
+    // module names can have '-', but their file names will have '_'
+    std::replace(module_name.begin(), module_name.end(), '-', '_');
+    return module_name;
+}
+
+bool Modprobe::ParseDepCallback(const std::string& base_path,
+                                const std::vector<std::string>& args) {
+    std::vector<std::string> deps;
+    std::string prefix = "";
+
+    // Set first item as our modules path
+    std::string::size_type pos = args[0].find(':');
+    if (args[0][0] != '/') {
+        prefix = base_path + "/";
+    }
+    if (pos != std::string::npos) {
+        deps.emplace_back(prefix + args[0].substr(0, pos));
+    } else {
+        LOG(ERROR) << "dependency lines must start with name followed by ':'";
+    }
+
+    // Remaining items are dependencies of our module
+    for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
+        if ((*arg)[0] != '/') {
+            prefix = base_path + "/";
+        } else {
+            prefix = "";
+        }
+        deps.push_back(prefix + *arg);
+    }
+
+    std::string canonical_name = MakeCanonical(args[0].substr(0, pos));
+    if (canonical_name.empty()) {
+        return false;
+    }
+    this->module_deps_[canonical_name] = deps;
+
+    return true;
+}
+
+bool Modprobe::ParseAliasCallback(const std::vector<std::string>& args) {
+    auto it = args.begin();
+    const std::string& type = *it++;
+
+    if (type != "alias") {
+        LOG(ERROR) << "non-alias line encountered in modules.alias, found " << type;
+        return false;
+    }
+
+    if (args.size() != 3) {
+        LOG(ERROR) << "alias lines in modules.alias must have 3 entries, not " << args.size();
+        return false;
+    }
+
+    const std::string& alias = *it++;
+    const std::string& module_name = *it++;
+    this->module_aliases_.emplace_back(alias, module_name);
+
+    return true;
+}
+
+bool Modprobe::ParseSoftdepCallback(const std::vector<std::string>& args) {
+    auto it = args.begin();
+    const std::string& type = *it++;
+    std::string state = "";
+
+    if (type != "softdep") {
+        LOG(ERROR) << "non-softdep line encountered in modules.softdep, found " << type;
+        return false;
+    }
+
+    if (args.size() < 4) {
+        LOG(ERROR) << "softdep lines in modules.softdep must have at least 4 entries";
+        return false;
+    }
+
+    const std::string& module = *it++;
+    while (it != args.end()) {
+        const std::string& token = *it++;
+        if (token == "pre:" || token == "post:") {
+            state = token;
+            continue;
+        }
+        if (state == "") {
+            LOG(ERROR) << "malformed modules.softdep at token " << token;
+            return false;
+        }
+        if (state == "pre:") {
+            this->module_pre_softdep_.emplace_back(module, token);
+        } else {
+            this->module_post_softdep_.emplace_back(module, token);
+        }
+    }
+
+    return true;
+}
+
+bool Modprobe::ParseLoadCallback(const std::vector<std::string>& args) {
+    auto it = args.begin();
+    const std::string& module = *it++;
+
+    const std::string& canonical_name = MakeCanonical(module);
+    if (canonical_name.empty()) {
+        return false;
+    }
+    this->module_load_.emplace_back(canonical_name);
+
+    return true;
+}
+
+bool Modprobe::ParseOptionsCallback(const std::vector<std::string>& args) {
+    auto it = args.begin();
+    const std::string& type = *it++;
+
+    if (type != "options") {
+        LOG(ERROR) << "non-options line encountered in modules.options";
+        return false;
+    }
+
+    if (args.size() < 2) {
+        LOG(ERROR) << "lines in modules.options must have at least 2 entries, not " << args.size();
+        return false;
+    }
+
+    const std::string& module = *it++;
+    std::string options = "";
+
+    const std::string& canonical_name = MakeCanonical(module);
+    if (canonical_name.empty()) {
+        return false;
+    }
+
+    while (it != args.end()) {
+        options += *it++;
+        if (it != args.end()) {
+            options += " ";
+        }
+    }
+
+    auto [unused, inserted] = this->module_options_.emplace(canonical_name, options);
+    if (!inserted) {
+        LOG(ERROR) << "multiple options lines present for module " << module;
+        return false;
+    }
+    return true;
+}
+
+void Modprobe::ParseCfg(const std::string& cfg,
+                        std::function<bool(const std::vector<std::string>&)> f) {
+    std::string cfg_contents;
+    if (!android::base::ReadFileToString(cfg, &cfg_contents, false)) {
+        return;
+    }
+
+    std::vector<std::string> lines = android::base::Split(cfg_contents, "\n");
+    for (const std::string line : lines) {
+        if (line.empty() || line[0] == '#') {
+            continue;
+        }
+        const std::vector<std::string> args = android::base::Split(line, " ");
+        if (args.empty()) continue;
+        f(args);
+    }
+    return;
+}
+
+Modprobe::Modprobe(const std::vector<std::string>& base_paths) {
+    using namespace std::placeholders;
+
+    for (const auto& base_path : base_paths) {
+        auto alias_callback = std::bind(&Modprobe::ParseAliasCallback, this, _1);
+        ParseCfg(base_path + "/modules.alias", alias_callback);
+
+        auto dep_callback = std::bind(&Modprobe::ParseDepCallback, this, base_path, _1);
+        ParseCfg(base_path + "/modules.dep", dep_callback);
+
+        auto softdep_callback = std::bind(&Modprobe::ParseSoftdepCallback, this, _1);
+        ParseCfg(base_path + "/modules.softdep", softdep_callback);
+
+        auto load_callback = std::bind(&Modprobe::ParseLoadCallback, this, _1);
+        ParseCfg(base_path + "/modules.load", load_callback);
+
+        auto options_callback = std::bind(&Modprobe::ParseOptionsCallback, this, _1);
+        ParseCfg(base_path + "/modules.options", options_callback);
+    }
+}
+
+std::vector<std::string> Modprobe::GetDependencies(const std::string& module) {
+    auto it = module_deps_.find(module);
+    if (it == module_deps_.end()) {
+        return {};
+    }
+    return it->second;
+}
+
+bool Modprobe::InsmodWithDeps(const std::string& module_name) {
+    if (module_name.empty()) {
+        LOG(ERROR) << "Need valid module name, given: " << module_name;
+        return false;
+    }
+
+    auto dependencies = GetDependencies(module_name);
+    if (dependencies.empty()) {
+        LOG(ERROR) << "Module " << module_name << " not in dependency file";
+        return false;
+    }
+
+    // load module dependencies in reverse order
+    for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) {
+        const std::string& canonical_name = MakeCanonical(*dep);
+        if (canonical_name.empty()) {
+            return false;
+        }
+        if (!LoadWithAliases(canonical_name, true)) {
+            return false;
+        }
+    }
+
+    // try to load soft pre-dependencies
+    for (const auto& [module, softdep] : module_pre_softdep_) {
+        if (module_name == module) {
+            LoadWithAliases(softdep, false);
+        }
+    }
+
+    // load target module itself with args
+    if (!Insmod(dependencies[0])) {
+        return false;
+    }
+
+    // try to load soft post-dependencies
+    for (const auto& [module, softdep] : module_post_softdep_) {
+        if (module_name == module) {
+            LoadWithAliases(softdep, false);
+        }
+    }
+
+    return true;
+}
+
+bool Modprobe::LoadWithAliases(const std::string& module_name, bool strict) {
+    std::set<std::string> modules_to_load = {module_name};
+    bool module_loaded = false;
+
+    // use aliases to expand list of modules to load (multiple modules
+    // may alias themselves to the requested name)
+    for (const auto& [alias, aliased_module] : module_aliases_) {
+        if (fnmatch(alias.c_str(), module_name.c_str(), 0) != 0) continue;
+        modules_to_load.emplace(aliased_module);
+    }
+
+    // attempt to load all modules aliased to this name
+    for (const auto& module : modules_to_load) {
+        if (!ModuleExists(module)) continue;
+        if (InsmodWithDeps(module)) module_loaded = true;
+    }
+
+    if (strict && !module_loaded) {
+        LOG(ERROR) << "LoadWithAliases did not find a module for " << module_name;
+        return false;
+    }
+    return true;
+}
+
+bool Modprobe::LoadListedModules() {
+    for (const auto& module : module_load_) {
+        if (!LoadWithAliases(module, true)) {
+            return false;
+        }
+    }
+    return true;
+}
diff --git a/libmodprobe/libmodprobe_ext.cpp b/libmodprobe/libmodprobe_ext.cpp
new file mode 100644
index 0000000..5f3a04d
--- /dev/null
+++ b/libmodprobe/libmodprobe_ext.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2018 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 <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
+
+#include <modprobe/modprobe.h>
+
+bool Modprobe::Insmod(const std::string& path_name) {
+    android::base::unique_fd fd(
+            TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
+    if (fd == -1) {
+        LOG(ERROR) << "Could not open module '" << path_name << "'";
+        return false;
+    }
+
+    std::string options = "";
+    auto options_iter = module_options_.find(MakeCanonical(path_name));
+    if (options_iter != module_options_.end()) {
+        options = options_iter->second;
+    }
+
+    LOG(INFO) << "Loading module " << path_name << " with args \"" << options << "\"";
+    int ret = syscall(__NR_finit_module, fd.get(), options.c_str(), 0);
+    if (ret != 0) {
+        if (errno == EEXIST) {
+            // Module already loaded
+            return true;
+        }
+        LOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'";
+        return false;
+    }
+
+    LOG(INFO) << "Loaded kernel module " << path_name;
+    return true;
+}
+
+bool Modprobe::ModuleExists(const std::string& module_name) {
+    struct stat fileStat;
+    auto deps = GetDependencies(module_name);
+    if (deps.empty()) {
+        // missing deps can happen in the case of an alias
+        return false;
+    }
+    if (stat(deps.front().c_str(), &fileStat)) {
+        return false;
+    }
+    if (!S_ISREG(fileStat.st_mode)) {
+        return false;
+    }
+    return true;
+}
diff --git a/libmodprobe/libmodprobe_ext_test.cpp b/libmodprobe/libmodprobe_ext_test.cpp
new file mode 100644
index 0000000..0f073cb
--- /dev/null
+++ b/libmodprobe/libmodprobe_ext_test.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2019 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 <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <string>
+#include <vector>
+
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+#include <gtest/gtest.h>
+
+#include <modprobe/modprobe.h>
+
+#include "libmodprobe_test.h"
+
+bool Modprobe::Insmod(const std::string& path_name) {
+    auto deps = GetDependencies(MakeCanonical(path_name));
+    if (deps.empty()) {
+        return false;
+    }
+    if (std::find(test_modules.begin(), test_modules.end(), deps.front()) == test_modules.end()) {
+        return false;
+    }
+    for (auto it = modules_loaded.begin(); it != modules_loaded.end(); ++it) {
+        if (android::base::StartsWith(*it, path_name)) {
+            return true;
+        }
+    }
+    std::string options;
+    auto options_iter = module_options_.find(MakeCanonical(path_name));
+    if (options_iter != module_options_.end()) {
+        options = " " + options_iter->second;
+    }
+    modules_loaded.emplace_back(path_name + options);
+    return true;
+}
+
+bool Modprobe::ModuleExists(const std::string& module_name) {
+    auto deps = GetDependencies(module_name);
+    if (deps.empty()) {
+        // missing deps can happen in the case of an alias
+        return false;
+    }
+    return std::find(test_modules.begin(), test_modules.end(), deps.front()) != test_modules.end();
+}
diff --git a/libmodprobe/libmodprobe_test.cpp b/libmodprobe/libmodprobe_test.cpp
new file mode 100644
index 0000000..481658d
--- /dev/null
+++ b/libmodprobe/libmodprobe_test.cpp
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2019 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 <functional>
+
+#include <android-base/file.h>
+#include <android-base/macros.h>
+#include <android-base/unique_fd.h>
+#include <gtest/gtest.h>
+
+#include <modprobe/modprobe.h>
+
+#include "libmodprobe_test.h"
+
+// Used by libmodprobe_ext_test to check if requested modules are present.
+std::vector<std::string> test_modules;
+
+// Used by libmodprobe_ext_test to report which modules would have been loaded.
+std::vector<std::string> modules_loaded;
+
+TEST(libmodprobe, Test) {
+    test_modules = {
+            "/test1.ko",  "/test2.ko",  "/test3.ko",  "/test4.ko",  "/test5.ko",
+            "/test6.ko",  "/test7.ko",  "/test8.ko",  "/test9.ko",  "/test10.ko",
+            "/test11.ko", "/test12.ko", "/test13.ko", "/test14.ko", "/test15.ko",
+    };
+
+    std::vector<std::string> expected_modules_loaded = {
+            "/test14.ko",
+            "/test15.ko",
+            "/test3.ko",
+            "/test4.ko",
+            "/test1.ko",
+            "/test6.ko",
+            "/test2.ko",
+            "/test5.ko",
+            "/test8.ko",
+            "/test7.ko param1=4",
+            "/test9.ko param_x=1 param_y=2 param_z=3",
+            "/test10.ko",
+            "/test12.ko",
+            "/test11.ko",
+            "/test13.ko",
+    };
+
+    const std::string modules_dep =
+            "test1.ko:\n"
+            "test2.ko:\n"
+            "test3.ko:\n"
+            "test4.ko: test3.ko\n"
+            "test5.ko: test2.ko test6.ko\n"
+            "test6.ko:\n"
+            "test7.ko:\n"
+            "test8.ko:\n"
+            "test9.ko:\n"
+            "test10.ko:\n"
+            "test11.ko:\n"
+            "test12.ko:\n"
+            "test13.ko:\n"
+            "test14.ko:\n"
+            "test15.ko:\n";
+
+    const std::string modules_softdep =
+            "softdep test7 pre: test8\n"
+            "softdep test9 post: test10\n"
+            "softdep test11 pre: test12 post: test13\n"
+            "softdep test3 pre: test141516\n";
+
+    const std::string modules_alias =
+            "# Aliases extracted from modules themselves.\n"
+            "\n"
+            "alias test141516 test14\n"
+            "alias test141516 test15\n"
+            "alias test141516 test16\n";
+
+    const std::string modules_options =
+            "options test7.ko param1=4\n"
+            "options test9.ko param_x=1 param_y=2 param_z=3\n"
+            "options test100.ko param_1=1\n";
+
+    const std::string modules_load =
+            "test4.ko\n"
+            "test1.ko\n"
+            "test3.ko\n"
+            "test5.ko\n"
+            "test7.ko\n"
+            "test9.ko\n"
+            "test11.ko\n";
+
+    TemporaryDir dir;
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_alias, std::string(dir.path) + "/modules.alias", 0600, getuid(), getgid()));
+
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_dep, std::string(dir.path) + "/modules.dep", 0600, getuid(), getgid()));
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_softdep, std::string(dir.path) + "/modules.softdep", 0600, getuid(), getgid()));
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_options, std::string(dir.path) + "/modules.options", 0600, getuid(), getgid()));
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_load, std::string(dir.path) + "/modules.load", 0600, getuid(), getgid()));
+
+    for (auto i = test_modules.begin(); i != test_modules.end(); ++i) {
+        *i = dir.path + *i;
+    }
+
+    Modprobe m({dir.path});
+    EXPECT_TRUE(m.LoadListedModules());
+
+    GTEST_LOG_(INFO) << "Expected modules loaded (in order):";
+    for (auto i = expected_modules_loaded.begin(); i != expected_modules_loaded.end(); ++i) {
+        *i = dir.path + *i;
+        GTEST_LOG_(INFO) << "\"" << *i << "\"";
+    }
+    GTEST_LOG_(INFO) << "Actual modules loaded (in order):";
+    for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) {
+        GTEST_LOG_(INFO) << "\"" << *i << "\"";
+    }
+
+    EXPECT_TRUE(modules_loaded == expected_modules_loaded);
+}
diff --git a/libmodprobe/libmodprobe_test.h b/libmodprobe/libmodprobe_test.h
new file mode 100644
index 0000000..a001b69
--- /dev/null
+++ b/libmodprobe/libmodprobe_test.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+extern std::vector<std::string> test_modules;
+extern std::vector<std::string> modules_loaded;
diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp
index 1c0f1e6..5b30a4d 100644
--- a/libunwindstack/MapInfo.cpp
+++ b/libunwindstack/MapInfo.cpp
@@ -300,7 +300,7 @@
 
 std::string MapInfo::GetBuildID() {
   uintptr_t id = build_id.load();
-  if (build_id != 0) {
+  if (id != 0) {
     return *reinterpret_cast<std::string*>(id);
   }
 
diff --git a/libziparchive/include/ziparchive/zip_writer.h b/libziparchive/include/ziparchive/zip_writer.h
index bd44fdb..a2a0dbf 100644
--- a/libziparchive/include/ziparchive/zip_writer.h
+++ b/libziparchive/include/ziparchive/zip_writer.h
@@ -21,6 +21,7 @@
 
 #include <memory>
 #include <string>
+#include <string_view>
 #include <vector>
 
 #include "android-base/macros.h"
@@ -101,7 +102,7 @@
    * Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry.
    * Returns 0 on success, and an error value < 0 on failure.
    */
-  int32_t StartEntry(const char* path, size_t flags);
+  int32_t StartEntry(std::string_view path, size_t flags);
 
   /**
    * Starts a new zip entry with the given path and flags, where the
@@ -111,17 +112,17 @@
    * Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry.
    * Returns 0 on success, and an error value < 0 on failure.
    */
-  int32_t StartAlignedEntry(const char* path, size_t flags, uint32_t alignment);
+  int32_t StartAlignedEntry(std::string_view path, size_t flags, uint32_t alignment);
 
   /**
    * Same as StartEntry(const char*, size_t), but sets a last modified time for the entry.
    */
-  int32_t StartEntryWithTime(const char* path, size_t flags, time_t time);
+  int32_t StartEntryWithTime(std::string_view path, size_t flags, time_t time);
 
   /**
    * Same as StartAlignedEntry(const char*, size_t), but sets a last modified time for the entry.
    */
-  int32_t StartAlignedEntryWithTime(const char* path, size_t flags, time_t time, uint32_t alignment);
+  int32_t StartAlignedEntryWithTime(std::string_view path, size_t flags, time_t time, uint32_t alignment);
 
   /**
    * Writes bytes to the zip file for the previously started zip entry.
diff --git a/libziparchive/zip_archive_benchmark.cpp b/libziparchive/zip_archive_benchmark.cpp
index 23ed408..09d3b8a 100644
--- a/libziparchive/zip_archive_benchmark.cpp
+++ b/libziparchive/zip_archive_benchmark.cpp
@@ -58,7 +58,7 @@
   std::string_view name("thisFileNameDoesNotExist");
 
   // Start the benchmark.
-  while (state.KeepRunning()) {
+  for (auto _ : state) {
     OpenArchive(temp_file->path, &handle);
     FindEntry(handle, name, &data);
     CloseArchive(handle);
@@ -73,7 +73,7 @@
   ZipEntry data;
   std::string name;
 
-  while (state.KeepRunning()) {
+  for (auto _ : state) {
     OpenArchive(temp_file->path, &handle);
     StartIteration(handle, &iteration_cookie);
     while (Next(iteration_cookie, &data, &name) == 0) {
@@ -84,4 +84,27 @@
 }
 BENCHMARK(Iterate_all_files);
 
+static void StartAlignedEntry(benchmark::State& state) {
+  TemporaryFile file;
+  FILE* fp = fdopen(file.fd, "w");
+
+  ZipWriter writer(fp);
+
+  auto alignment = uint32_t(state.range(0));
+  std::string name = "name";
+  int counter = 0;
+  for (auto _ : state) {
+    writer.StartAlignedEntry(name + std::to_string(counter++), 0, alignment);
+    state.PauseTiming();
+    writer.WriteBytes("hola", 4);
+    writer.FinishEntry();
+    state.ResumeTiming();
+  }
+
+  writer.Finish();
+  fclose(fp);
+}
+BENCHMARK(StartAlignedEntry)->Arg(2)->Arg(16)->Arg(1024)->Arg(4096);
+
+
 BENCHMARK_MAIN();
diff --git a/libziparchive/zip_writer.cc b/libziparchive/zip_writer.cc
index ae9d145..198154b 100644
--- a/libziparchive/zip_writer.cc
+++ b/libziparchive/zip_writer.cc
@@ -130,7 +130,7 @@
   return error_code;
 }
 
-int32_t ZipWriter::StartEntry(const char* path, size_t flags) {
+int32_t ZipWriter::StartEntry(std::string_view path, size_t flags) {
   uint32_t alignment = 0;
   if (flags & kAlign32) {
     flags &= ~kAlign32;
@@ -139,11 +139,11 @@
   return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
 }
 
-int32_t ZipWriter::StartAlignedEntry(const char* path, size_t flags, uint32_t alignment) {
+int32_t ZipWriter::StartAlignedEntry(std::string_view path, size_t flags, uint32_t alignment) {
   return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
 }
 
-int32_t ZipWriter::StartEntryWithTime(const char* path, size_t flags, time_t time) {
+int32_t ZipWriter::StartEntryWithTime(std::string_view path, size_t flags, time_t time) {
   uint32_t alignment = 0;
   if (flags & kAlign32) {
     flags &= ~kAlign32;
@@ -198,7 +198,7 @@
   dst->extra_field_length = src.padding_length;
 }
 
-int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, time_t time,
+int32_t ZipWriter::StartAlignedEntryWithTime(std::string_view path, size_t flags, time_t time,
                                              uint32_t alignment) {
   if (state_ != State::kWritingZip) {
     return kInvalidState;
@@ -247,13 +247,24 @@
   ExtractTimeAndDate(time, &file_entry.last_mod_time, &file_entry.last_mod_date);
 
   off_t offset = current_offset_ + sizeof(LocalFileHeader) + file_entry.path.size();
-  std::vector<char> zero_padding;
+  // prepare a pre-zeroed memory page in case when we need to pad some aligned data.
+  static constexpr auto kPageSize = 4096;
+  static constexpr char kSmallZeroPadding[kPageSize] = {};
+  // use this buffer if our preallocated one is too small
+  std::vector<char> zero_padding_big;
+  const char* zero_padding = nullptr;
+
   if (alignment != 0 && (offset & (alignment - 1))) {
     // Pad the extra field so the data will be aligned.
     uint16_t padding = static_cast<uint16_t>(alignment - (offset % alignment));
     file_entry.padding_length = padding;
     offset += padding;
-    zero_padding.resize(padding, 0);
+    if (padding <= std::size(kSmallZeroPadding)) {
+        zero_padding = kSmallZeroPadding;
+    } else {
+        zero_padding_big.resize(padding, 0);
+        zero_padding = zero_padding_big.data();
+    }
   }
 
   LocalFileHeader header = {};
@@ -265,11 +276,11 @@
     return HandleError(kIoError);
   }
 
-  if (fwrite(path, sizeof(*path), file_entry.path.size(), file_) != file_entry.path.size()) {
+  if (fwrite(path.data(), 1, path.size(), file_) != path.size()) {
     return HandleError(kIoError);
   }
 
-  if (file_entry.padding_length != 0 && fwrite(zero_padding.data(), 1, file_entry.padding_length,
+  if (file_entry.padding_length != 0 && fwrite(zero_padding, 1, file_entry.padding_length,
                                                file_) != file_entry.padding_length) {
     return HandleError(kIoError);
   }
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 7cb0f66..3acf301 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -345,8 +345,11 @@
     trigger early-boot
     trigger boot
 
-on post-fs
+on early-fs
+    # Once metadata has been mounted, we'll need vold to deal with userdata checkpointing
     start vold
+
+on post-fs
     exec - system system -- /system/bin/vdc checkpoint markBootAttempt
 
     # Once everything is setup, no need to modify /.
@@ -442,6 +445,7 @@
     mkdir /data/apex 0750 root system
     mkdir /data/apex/active 0750 root system
     mkdir /data/apex/backup 0700 root system
+    mkdir /data/apex/hashtree 0700 root system
     mkdir /data/apex/sessions 0700 root system
     mkdir /data/app-staging 0750 system system
     start apexd