Merge "Pass fault address to GWP-ASan's changed API."
diff --git a/debuggerd/libdebuggerd/scudo.cpp b/debuggerd/libdebuggerd/scudo.cpp
index 37e390b..5a62fe1 100644
--- a/debuggerd/libdebuggerd/scudo.cpp
+++ b/debuggerd/libdebuggerd/scudo.cpp
@@ -44,9 +44,12 @@
                                        __scudo_get_stack_depot_size());
   auto region_info = AllocAndReadFully(process_memory, process_info.scudo_region_info,
                                        __scudo_get_region_info_size());
-  auto ring_buffer = AllocAndReadFully(process_memory, process_info.scudo_ring_buffer,
-                                       process_info.scudo_ring_buffer_size);
-  if (!stack_depot || !region_info || !ring_buffer) {
+  std::unique_ptr<char[]> ring_buffer;
+  if (process_info.scudo_ring_buffer_size != 0) {
+    ring_buffer = AllocAndReadFully(process_memory, process_info.scudo_ring_buffer,
+                                    process_info.scudo_ring_buffer_size);
+  }
+  if (!stack_depot || !region_info) {
     return;
   }
 
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index f1b82e9..9676f87 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -1082,7 +1082,8 @@
     sparse_file** s;
 
     if (partition == "boot" || partition == "boot_a" || partition == "boot_b" ||
-        partition == "init_boot" || partition == "init_boot_a" || partition == "init_boot_b") {
+        partition == "init_boot" || partition == "init_boot_a" || partition == "init_boot_b" ||
+        partition == "recovery" || partition == "recovery_a" || partition == "recovery_b") {
         copy_avb_footer(partition, buf);
     }
 
diff --git a/fs_mgr/TEST_MAPPING b/fs_mgr/TEST_MAPPING
index 432aa4f..2b5e337 100644
--- a/fs_mgr/TEST_MAPPING
+++ b/fs_mgr/TEST_MAPPING
@@ -27,5 +27,16 @@
     {
       "name": "cow_api_test"
     }
+  ],
+  "kernel-presubmit": [
+    {
+      "name": "vts_libdm_test"
+    },
+    {
+      "name": "vts_core_liblp_test"
+    },
+    {
+      "name": "vts_libsnapshot_test"
+    }
   ]
 }
diff --git a/fs_mgr/fs_mgr_remount.cpp b/fs_mgr/fs_mgr_remount.cpp
index 5fddf86..5a9f391 100644
--- a/fs_mgr/fs_mgr_remount.cpp
+++ b/fs_mgr/fs_mgr_remount.cpp
@@ -175,6 +175,8 @@
     }
     if (checkpointing) {
         LOG(ERROR) << "Cannot use remount when a checkpoint is in progress.";
+        LOG(ERROR) << "To force end checkpointing, call 'vdc checkpoint commitChanges'";
+        LOG(ERROR) << "Warning: this can lead to data corruption if rolled back.";
         return false;
     }
     return true;
diff --git a/fs_mgr/libsnapshot/include/libsnapshot/cow_writer.h b/fs_mgr/libsnapshot/include/libsnapshot/cow_writer.h
index 798bc73..c7b83a8 100644
--- a/fs_mgr/libsnapshot/include/libsnapshot/cow_writer.h
+++ b/fs_mgr/libsnapshot/include/libsnapshot/cow_writer.h
@@ -120,6 +120,12 @@
     void EnqueueCompressBlocks(const void* buffer, size_t num_blocks);
     bool GetCompressedBuffers(std::vector<std::basic_string<uint8_t>>* compressed_buf);
     void Finalize();
+    static std::basic_string<uint8_t> Compress(CowCompressionAlgorithm compression,
+                                               const void* data, size_t length);
+
+    static bool CompressBlocks(CowCompressionAlgorithm compression, size_t block_size,
+                               const void* buffer, size_t num_blocks,
+                               std::vector<std::basic_string<uint8_t>>* compressed_data);
 
   private:
     struct CompressWork {
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp
index 4d9b748..9b50986 100644
--- a/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp
@@ -32,9 +32,13 @@
 
 namespace android {
 namespace snapshot {
-
 std::basic_string<uint8_t> CompressWorker::Compress(const void* data, size_t length) {
-    switch (compression_) {
+    return Compress(compression_, data, length);
+}
+
+std::basic_string<uint8_t> CompressWorker::Compress(CowCompressionAlgorithm compression,
+                                                    const void* data, size_t length) {
+    switch (compression) {
         case kCowCompressGz: {
             const auto bound = compressBound(length);
             std::basic_string<uint8_t> buffer(bound, '\0');
@@ -94,17 +98,22 @@
             return buffer;
         }
         default:
-            LOG(ERROR) << "unhandled compression type: " << compression_;
+            LOG(ERROR) << "unhandled compression type: " << compression;
             break;
     }
     return {};
 }
-
 bool CompressWorker::CompressBlocks(const void* buffer, size_t num_blocks,
                                     std::vector<std::basic_string<uint8_t>>* compressed_data) {
+    return CompressBlocks(compression_, block_size_, buffer, num_blocks, compressed_data);
+}
+
+bool CompressWorker::CompressBlocks(CowCompressionAlgorithm compression, size_t block_size,
+                                    const void* buffer, size_t num_blocks,
+                                    std::vector<std::basic_string<uint8_t>>* compressed_data) {
     const uint8_t* iter = reinterpret_cast<const uint8_t*>(buffer);
     while (num_blocks) {
-        auto data = Compress(iter, block_size_);
+        auto data = Compress(compression, iter, block_size);
         if (data.empty()) {
             PLOG(ERROR) << "CompressBlocks: Compression failed";
             return false;
@@ -116,7 +125,7 @@
 
         compressed_data->emplace_back(std::move(data));
         num_blocks -= 1;
-        iter += block_size_;
+        iter += block_size;
     }
     return true;
 }
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/cow_writer.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/cow_writer.cpp
index 2d5e4bc..3932fad 100644
--- a/fs_mgr/libsnapshot/libsnapshot_cow/cow_writer.cpp
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/cow_writer.cpp
@@ -275,6 +275,10 @@
 }
 
 void CowWriter::InitWorkers() {
+    if (num_compress_threads_ <= 1) {
+        LOG(INFO) << "Not creating new threads for compression.";
+        return;
+    }
     for (int i = 0; i < num_compress_threads_; i++) {
         auto wt = std::make_unique<CompressWorker>(compression_, header_.block_size);
         threads_.emplace_back(std::async(std::launch::async, &CompressWorker::RunThread, wt.get()));
@@ -447,6 +451,10 @@
     size_t num_blocks_per_thread = num_blocks / num_threads;
     const uint8_t* iter = reinterpret_cast<const uint8_t*>(data);
     compressed_buf_.clear();
+    if (num_threads <= 1) {
+        return CompressWorker::CompressBlocks(compression_, options_.block_size, data, num_blocks,
+                                              &compressed_buf_);
+    }
 
     // Submit the blocks per thread. The retrieval of
     // compressed buffers has to be done in the same order.
@@ -490,13 +498,12 @@
     while (num_blocks) {
         size_t pending_blocks = (std::min(kProcessingBlocks, num_blocks));
 
-        if (compression_) {
+        if (compression_ && num_compress_threads_ > 1) {
             if (!CompressBlocks(pending_blocks, iter)) {
                 return false;
             }
             buf_iter_ = compressed_buf_.begin();
             CHECK(pending_blocks == compressed_buf_.size());
-            iter += (pending_blocks * header_.block_size);
         }
 
         num_blocks -= pending_blocks;
@@ -512,7 +519,17 @@
             }
 
             if (compression_) {
-                auto data = std::move(*buf_iter_);
+                auto data = [&, this]() {
+                    if (num_compress_threads_ > 1) {
+                        auto data = std::move(*buf_iter_);
+                        buf_iter_++;
+                        return data;
+                    } else {
+                        auto data =
+                                CompressWorker::Compress(compression_, iter, header_.block_size);
+                        return data;
+                    }
+                }();
                 op.compression = compression_;
                 op.data_length = static_cast<uint16_t>(data.size());
 
@@ -520,15 +537,14 @@
                     PLOG(ERROR) << "AddRawBlocks: write failed";
                     return false;
                 }
-                buf_iter_++;
             } else {
                 op.data_length = static_cast<uint16_t>(header_.block_size);
                 if (!WriteOperation(op, iter, header_.block_size)) {
                     PLOG(ERROR) << "AddRawBlocks: write failed";
                     return false;
                 }
-                iter += header_.block_size;
             }
+            iter += header_.block_size;
 
             i += 1;
             pending_blocks -= 1;
diff --git a/fs_mgr/libsnapshot/snapshot.cpp b/fs_mgr/libsnapshot/snapshot.cpp
index 10d2f18..961db02 100644
--- a/fs_mgr/libsnapshot/snapshot.cpp
+++ b/fs_mgr/libsnapshot/snapshot.cpp
@@ -1498,6 +1498,7 @@
     if (UpdateUsesUserSnapshots(lock) && !device()->IsTestDevice()) {
         if (snapuserd_client_) {
             snapuserd_client_->DetachSnapuserd();
+            snapuserd_client_->RemoveTransitionedDaemonIndicator();
             snapuserd_client_ = nullptr;
         }
     }
diff --git a/fs_mgr/libsnapshot/snapuserd/Android.bp b/fs_mgr/libsnapshot/snapuserd/Android.bp
index 64e0b8a..a67e37c 100644
--- a/fs_mgr/libsnapshot/snapuserd/Android.bp
+++ b/fs_mgr/libsnapshot/snapuserd/Android.bp
@@ -37,11 +37,13 @@
 cc_library_static {
     name: "libsnapshot_snapuserd",
     defaults: [
+        "fs_mgr_defaults",
         "libsnapshot_snapuserd_defaults",
     ],
     recovery_available: true,
     static_libs: [
         "libcutils_sockets",
+        "libfs_mgr",
     ],
     shared_libs: [
         "libbase",
@@ -49,6 +51,7 @@
     ],
     export_include_dirs: ["include"],
     ramdisk_available: true,
+    vendor_ramdisk_available: true,
 }
 
 cc_defaults {
@@ -86,6 +89,7 @@
         "libgflags",
         "liblog",
         "libsnapshot_cow",
+        "libsnapshot_snapuserd",
         "libz",
         "liblz4",
         "libext4_utils",
diff --git a/fs_mgr/libsnapshot/snapuserd/include/snapuserd/snapuserd_client.h b/fs_mgr/libsnapshot/snapuserd/include/snapuserd/snapuserd_client.h
index 4b62b20..fb2251e 100644
--- a/fs_mgr/libsnapshot/snapuserd/include/snapuserd/snapuserd_client.h
+++ b/fs_mgr/libsnapshot/snapuserd/include/snapuserd/snapuserd_client.h
@@ -32,6 +32,7 @@
 
 static constexpr char kSnapuserdSocket[] = "snapuserd";
 static constexpr char kSnapuserdSocketProxy[] = "snapuserd_proxy";
+static constexpr char kDaemonAliveIndicator[] = "daemon-alive-indicator";
 
 // Ensure that the second-stage daemon for snapuserd is running.
 bool EnsureSnapuserdStarted();
@@ -44,9 +45,11 @@
     std::string Receivemsg();
 
     bool ValidateConnection();
+    std::string GetDaemonAliveIndicatorPath();
 
   public:
     explicit SnapuserdClient(android::base::unique_fd&& sockfd);
+    SnapuserdClient(){};
 
     static std::unique_ptr<SnapuserdClient> Connect(const std::string& socket_name,
                                                     std::chrono::milliseconds timeout_ms);
@@ -91,6 +94,17 @@
     // Check the update verification status - invoked by update_verifier during
     // boot
     bool QueryUpdateVerification();
+
+    // Check if Snapuser daemon is ready post selinux transition after OTA boot
+    // This is invoked only by init as there is no sockets setup yet during
+    // selinux transition
+    bool IsTransitionedDaemonReady();
+
+    // Remove the daemon-alive-indicator path post snapshot merge
+    bool RemoveTransitionedDaemonIndicator();
+
+    // Notify init that snapuserd daemon is ready post selinux transition
+    void NotifyTransitionDaemonIsReady();
 };
 
 }  // namespace snapshot
diff --git a/fs_mgr/libsnapshot/snapuserd/snapuserd_client.cpp b/fs_mgr/libsnapshot/snapuserd/snapuserd_client.cpp
index e08cf9b..695b581 100644
--- a/fs_mgr/libsnapshot/snapuserd/snapuserd_client.cpp
+++ b/fs_mgr/libsnapshot/snapuserd/snapuserd_client.cpp
@@ -29,10 +29,12 @@
 #include <chrono>
 #include <sstream>
 
+#include <android-base/file.h>
 #include <android-base/logging.h>
 #include <android-base/parseint.h>
 #include <android-base/properties.h>
 #include <android-base/strings.h>
+#include <fs_mgr/file_wait.h>
 #include <snapuserd/snapuserd_client.h>
 
 namespace android {
@@ -279,5 +281,42 @@
     return response == "success";
 }
 
+std::string SnapuserdClient::GetDaemonAliveIndicatorPath() {
+    return "/metadata/ota/" + std::string(kDaemonAliveIndicator);
+}
+
+bool SnapuserdClient::IsTransitionedDaemonReady() {
+    if (!android::fs_mgr::WaitForFile(GetDaemonAliveIndicatorPath(), 10s)) {
+        LOG(ERROR) << "Timed out waiting for daemon indicator path: "
+                   << GetDaemonAliveIndicatorPath();
+        return false;
+    }
+
+    return true;
+}
+
+bool SnapuserdClient::RemoveTransitionedDaemonIndicator() {
+    std::string error;
+    std::string filePath = GetDaemonAliveIndicatorPath();
+    if (!android::base::RemoveFileIfExists(filePath, &error)) {
+        LOG(ERROR) << "Failed to remove DaemonAliveIndicatorPath - error: " << error;
+        return false;
+    }
+
+    if (!android::fs_mgr::WaitForFileDeleted(filePath, 5s)) {
+        LOG(ERROR) << "Timed out waiting for " << filePath << " to unlink";
+        return false;
+    }
+
+    return true;
+}
+
+void SnapuserdClient::NotifyTransitionDaemonIsReady() {
+    if (!android::base::WriteStringToFile("1", GetDaemonAliveIndicatorPath())) {
+        PLOG(ERROR) << "Unable to write daemon alive indicator path: "
+                    << GetDaemonAliveIndicatorPath();
+    }
+}
+
 }  // namespace snapshot
 }  // namespace android
diff --git a/fs_mgr/libsnapshot/snapuserd/snapuserd_daemon.cpp b/fs_mgr/libsnapshot/snapuserd/snapuserd_daemon.cpp
index 2f7775c..bfe93eb 100644
--- a/fs_mgr/libsnapshot/snapuserd/snapuserd_daemon.cpp
+++ b/fs_mgr/libsnapshot/snapuserd/snapuserd_daemon.cpp
@@ -119,6 +119,12 @@
         }
     }
 
+    // We reach this point only during selinux transition during device boot.
+    // At this point, all threads are spin up and are ready to serve the I/O
+    // requests for dm-user. Lets inform init.
+    auto client = std::make_unique<SnapuserdClient>();
+    client->NotifyTransitionDaemonIsReady();
+
     // Skip the accept() call to avoid spurious log spam. The server will still
     // run until all handlers have completed.
     return user_server_.WaitForSocket();
diff --git a/init/README.md b/init/README.md
index 957eb9e..b006365 100644
--- a/init/README.md
+++ b/init/README.md
@@ -174,6 +174,17 @@
 be executed. The condition `boot && property:true=true` will be evaluated to
 false because the `boot` trigger is a past event.
 
+Note that when `ro.property_service.async_persist_writes` is `true`, there is no
+defined ordering between persistent setprops and non-persistent setprops. For
+example:
+
+    on boot
+        setprop a 1
+        setprop persist.b 2
+
+When `ro.property_service.async_persist_writes` is `true`, triggers for these
+two properties may execute in any order.
+
 Services
 --------
 Services are programs which init launches and (optionally) restarts
@@ -244,6 +255,10 @@
   "r", "w" or "rw".  For native executables see libcutils
   android\_get\_control\_file().
 
+`gentle_kill`
+> This service will be sent SIGTERM instead of SIGKILL when stopped. After a 200 ms timeout, it will
+  be sent SIGKILL.
+
 `group <groupname> [ <groupname>\* ]`
 > Change to 'groupname' before exec'ing this service.  Additional
   groupnames beyond the (required) first one are used to set the
diff --git a/init/init_test.cpp b/init/init_test.cpp
index 1ab69ac..1e69ede 100644
--- a/init/init_test.cpp
+++ b/init/init_test.cpp
@@ -16,6 +16,7 @@
 
 #include <functional>
 #include <string_view>
+#include <thread>
 #include <type_traits>
 
 #include <android-base/file.h>
@@ -642,6 +643,91 @@
     ASSERT_LE(curr_limit.rlim_max, max_limit);
 }
 
+static std::vector<const char*> ConvertToArgv(const std::vector<std::string>& args) {
+    std::vector<const char*> argv;
+    argv.reserve(args.size() + 1);
+    for (const auto& arg : args) {
+        if (argv.empty()) {
+            LOG(DEBUG) << arg;
+        } else {
+            LOG(DEBUG) << "    " << arg;
+        }
+        argv.emplace_back(arg.data());
+    }
+    argv.emplace_back(nullptr);
+    return argv;
+}
+
+pid_t ForkExecvpAsync(const std::vector<std::string>& args) {
+    auto argv = ConvertToArgv(args);
+
+    pid_t pid = fork();
+    if (pid == 0) {
+        close(STDIN_FILENO);
+        close(STDOUT_FILENO);
+        close(STDERR_FILENO);
+
+        execvp(argv[0], const_cast<char**>(argv.data()));
+        PLOG(ERROR) << "exec in ForkExecvpAsync init test";
+        _exit(EXIT_FAILURE);
+    }
+    if (pid == -1) {
+        PLOG(ERROR) << "fork in ForkExecvpAsync init test";
+        return -1;
+    }
+    return pid;
+}
+
+TEST(init, GentleKill) {
+    std::string init_script = R"init(
+service test_gentle_kill /system/bin/sleep 1000
+    disabled
+    oneshot
+    gentle_kill
+    user root
+    group root
+    seclabel u:r:toolbox:s0
+)init";
+
+    ActionManager action_manager;
+    ServiceList service_list;
+    TestInitText(init_script, BuiltinFunctionMap(), {}, &action_manager, &service_list);
+    ASSERT_EQ(std::distance(service_list.begin(), service_list.end()), 1);
+
+    auto service = service_list.begin()->get();
+    ASSERT_NE(service, nullptr);
+    ASSERT_RESULT_OK(service->Start());
+    const pid_t pid = service->pid();
+    ASSERT_GT(pid, 0);
+    EXPECT_NE(getsid(pid), 0);
+
+    TemporaryFile logfile;
+    logfile.DoNotRemove();
+    ASSERT_TRUE(logfile.fd != -1);
+
+    std::vector<std::string> cmd;
+    cmd.push_back("system/bin/strace");
+    cmd.push_back("-o");
+    cmd.push_back(logfile.path);
+    cmd.push_back("-e");
+    cmd.push_back("signal");
+    cmd.push_back("-p");
+    cmd.push_back(std::to_string(pid));
+    pid_t strace_pid = ForkExecvpAsync(cmd);
+
+    // Give strace a moment to connect
+    std::this_thread::sleep_for(1s);
+    service->Stop();
+
+    int status;
+    waitpid(strace_pid, &status, 0);
+
+    std::string logs;
+    android::base::ReadFdToString(logfile.fd, &logs);
+    int pos = logs.find("killed by SIGTERM");
+    ASSERT_NE(pos, (int)std::string::npos);
+}
+
 class TestCaseLogger : public ::testing::EmptyTestEventListener {
     void OnTestStart(const ::testing::TestInfo& test_info) override {
 #ifdef __ANDROID__
diff --git a/init/property_service.cpp b/init/property_service.cpp
index 9df9828..87ffdb9 100644
--- a/init/property_service.cpp
+++ b/init/property_service.cpp
@@ -98,6 +98,9 @@
 
 namespace android {
 namespace init {
+
+class PersistWriteThread;
+
 constexpr auto FINGERPRINT_PROP = "ro.build.fingerprint";
 constexpr auto LEGACY_FINGERPRINT_PROP = "ro.build.legacy.fingerprint";
 constexpr auto ID_PROP = "ro.build.id";
@@ -115,6 +118,8 @@
 static std::mutex accept_messages_lock;
 static std::thread property_service_thread;
 
+static std::unique_ptr<PersistWriteThread> persist_write_thread;
+
 static PropertyInfoAreaFile property_info_area;
 
 struct PropertyAuditData {
@@ -177,48 +182,13 @@
     return has_access;
 }
 
-static uint32_t PropertySet(const std::string& name, const std::string& value, std::string* error) {
-    size_t valuelen = value.size();
-
-    if (!IsLegalPropertyName(name)) {
-        *error = "Illegal property name";
-        return PROP_ERROR_INVALID_NAME;
-    }
-
-    if (auto result = IsLegalPropertyValue(name, value); !result.ok()) {
-        *error = result.error().message();
-        return PROP_ERROR_INVALID_VALUE;
-    }
-
-    prop_info* pi = (prop_info*) __system_property_find(name.c_str());
-    if (pi != nullptr) {
-        // ro.* properties are actually "write-once".
-        if (StartsWith(name, "ro.")) {
-            *error = "Read-only property was already set";
-            return PROP_ERROR_READ_ONLY_PROPERTY;
-        }
-
-        __system_property_update(pi, value.c_str(), valuelen);
-    } else {
-        int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
-        if (rc < 0) {
-            *error = "__system_property_add failed";
-            return PROP_ERROR_SET_FAILED;
-        }
-    }
-
-    // Don't write properties to disk until after we have read all default
-    // properties to prevent them from being overwritten by default values.
-    if (persistent_properties_loaded && StartsWith(name, "persist.")) {
-        WritePersistentProperty(name, value);
-    }
+void NotifyPropertyChange(const std::string& name, const std::string& value) {
     // If init hasn't started its main loop, then it won't be handling property changed messages
     // anyway, so there's no need to try to send them.
     auto lock = std::lock_guard{accept_messages_lock};
     if (accept_messages) {
         PropertyChanged(name, value);
     }
-    return PROP_SUCCESS;
 }
 
 class AsyncRestorecon {
@@ -259,7 +229,9 @@
 
 class SocketConnection {
   public:
+    SocketConnection() = default;
     SocketConnection(int socket, const ucred& cred) : socket_(socket), cred_(cred) {}
+    SocketConnection(SocketConnection&&) = default;
 
     bool RecvUint32(uint32_t* value, uint32_t* timeout_ms) {
         return RecvFully(value, sizeof(*value), timeout_ms);
@@ -318,6 +290,8 @@
 
     const ucred& cred() { return cred_; }
 
+    SocketConnection& operator=(SocketConnection&&) = default;
+
   private:
     bool PollIn(uint32_t* timeout_ms) {
         struct pollfd ufd = {
@@ -388,9 +362,78 @@
     unique_fd socket_;
     ucred cred_;
 
-    DISALLOW_IMPLICIT_CONSTRUCTORS(SocketConnection);
+    DISALLOW_COPY_AND_ASSIGN(SocketConnection);
 };
 
+class PersistWriteThread {
+  public:
+    PersistWriteThread();
+    void Write(std::string name, std::string value, SocketConnection socket);
+
+  private:
+    void Work();
+
+  private:
+    std::thread thread_;
+    std::mutex mutex_;
+    std::condition_variable cv_;
+    std::deque<std::tuple<std::string, std::string, SocketConnection>> work_;
+};
+
+static std::optional<uint32_t> PropertySet(const std::string& name, const std::string& value,
+                                           SocketConnection* socket, std::string* error) {
+    size_t valuelen = value.size();
+
+    if (!IsLegalPropertyName(name)) {
+        *error = "Illegal property name";
+        return {PROP_ERROR_INVALID_NAME};
+    }
+
+    if (auto result = IsLegalPropertyValue(name, value); !result.ok()) {
+        *error = result.error().message();
+        return {PROP_ERROR_INVALID_VALUE};
+    }
+
+    prop_info* pi = (prop_info*)__system_property_find(name.c_str());
+    if (pi != nullptr) {
+        // ro.* properties are actually "write-once".
+        if (StartsWith(name, "ro.")) {
+            *error = "Read-only property was already set";
+            return {PROP_ERROR_READ_ONLY_PROPERTY};
+        }
+
+        __system_property_update(pi, value.c_str(), valuelen);
+    } else {
+        int rc = __system_property_add(name.c_str(), name.size(), value.c_str(), valuelen);
+        if (rc < 0) {
+            *error = "__system_property_add failed";
+            return {PROP_ERROR_SET_FAILED};
+        }
+    }
+
+    // Don't write properties to disk until after we have read all default
+    // properties to prevent them from being overwritten by default values.
+    if (socket && persistent_properties_loaded && StartsWith(name, "persist.")) {
+        if (persist_write_thread) {
+            persist_write_thread->Write(name, value, std::move(*socket));
+            return {};
+        }
+        WritePersistentProperty(name, value);
+    }
+
+    NotifyPropertyChange(name, value);
+    return {PROP_SUCCESS};
+}
+
+// Helper for PropertySet, for the case where no socket is used, and therefore an asynchronous
+// return is not possible.
+static uint32_t PropertySetNoSocket(const std::string& name, const std::string& value,
+                                    std::string* error) {
+    auto ret = PropertySet(name, value, nullptr, error);
+    CHECK(ret.has_value());
+    return *ret;
+}
+
 static uint32_t SendControlMessage(const std::string& msg, const std::string& name, pid_t pid,
                                    SocketConnection* socket, std::string* error) {
     auto lock = std::lock_guard{accept_messages_lock};
@@ -481,16 +524,17 @@
     return PROP_SUCCESS;
 }
 
-// This returns one of the enum of PROP_SUCCESS or PROP_ERROR*.
-uint32_t HandlePropertySet(const std::string& name, const std::string& value,
-                           const std::string& source_context, const ucred& cr,
-                           SocketConnection* socket, std::string* error) {
+// This returns one of the enum of PROP_SUCCESS or PROP_ERROR*, or std::nullopt
+// if asynchronous.
+std::optional<uint32_t> HandlePropertySet(const std::string& name, const std::string& value,
+                                          const std::string& source_context, const ucred& cr,
+                                          SocketConnection* socket, std::string* error) {
     if (auto ret = CheckPermissions(name, value, source_context, cr, error); ret != PROP_SUCCESS) {
-        return ret;
+        return {ret};
     }
 
     if (StartsWith(name, "ctl.")) {
-        return SendControlMessage(name.c_str() + 4, value, cr.pid, socket, error);
+        return {SendControlMessage(name.c_str() + 4, value, cr.pid, socket, error)};
     }
 
     // sys.powerctl is a special property that is used to make the device reboot.  We want to log
@@ -511,7 +555,7 @@
         }
         if (value == "reboot,userspace" && !is_userspace_reboot_supported().value_or(false)) {
             *error = "Userspace reboot is not supported by this device";
-            return PROP_ERROR_INVALID_VALUE;
+            return {PROP_ERROR_INVALID_VALUE};
         }
     }
 
@@ -522,10 +566,20 @@
     if (name == kRestoreconProperty && cr.pid != 1 && !value.empty()) {
         static AsyncRestorecon async_restorecon;
         async_restorecon.TriggerRestorecon(value);
-        return PROP_SUCCESS;
+        return {PROP_SUCCESS};
     }
 
-    return PropertySet(name, value, error);
+    return PropertySet(name, value, socket, error);
+}
+
+// Helper for HandlePropertySet, for the case where no socket is used, and
+// therefore an asynchronous return is not possible.
+uint32_t HandlePropertySetNoSocket(const std::string& name, const std::string& value,
+                                   const std::string& source_context, const ucred& cr,
+                                   std::string* error) {
+    auto ret = HandlePropertySet(name, value, source_context, cr, nullptr, error);
+    CHECK(ret.has_value());
+    return *ret;
 }
 
 static void handle_property_set_fd() {
@@ -576,8 +630,7 @@
 
         const auto& cr = socket.cred();
         std::string error;
-        uint32_t result =
-                HandlePropertySet(prop_name, prop_value, source_context, cr, nullptr, &error);
+        auto result = HandlePropertySetNoSocket(prop_name, prop_value, source_context, cr, &error);
         if (result != PROP_SUCCESS) {
             LOG(ERROR) << "Unable to set property '" << prop_name << "' from uid:" << cr.uid
                        << " gid:" << cr.gid << " pid:" << cr.pid << ": " << error;
@@ -603,14 +656,19 @@
             return;
         }
 
+        // HandlePropertySet takes ownership of the socket if the set is handled asynchronously.
         const auto& cr = socket.cred();
         std::string error;
-        uint32_t result = HandlePropertySet(name, value, source_context, cr, &socket, &error);
-        if (result != PROP_SUCCESS) {
+        auto result = HandlePropertySet(name, value, source_context, cr, &socket, &error);
+        if (!result) {
+            // Result will be sent after completion.
+            return;
+        }
+        if (*result != PROP_SUCCESS) {
             LOG(ERROR) << "Unable to set property '" << name << "' from uid:" << cr.uid
                        << " gid:" << cr.gid << " pid:" << cr.pid << ": " << error;
         }
-        socket.SendUint32(result);
+        socket.SendUint32(*result);
         break;
       }
 
@@ -622,10 +680,9 @@
 }
 
 uint32_t InitPropertySet(const std::string& name, const std::string& value) {
-    uint32_t result = 0;
     ucred cr = {.pid = 1, .uid = 0, .gid = 0};
     std::string error;
-    result = HandlePropertySet(name, value, kInitContext, cr, nullptr, &error);
+    auto result = HandlePropertySetNoSocket(name, value, kInitContext, cr, &error);
     if (result != PROP_SUCCESS) {
         LOG(ERROR) << "Init cannot set '" << name << "' to '" << value << "': " << error;
     }
@@ -795,7 +852,7 @@
         load_properties_from_file("/data/local.prop", nullptr, &properties);
         for (const auto& [name, value] : properties) {
             std::string error;
-            if (PropertySet(name, value, &error) != PROP_SUCCESS) {
+            if (PropertySetNoSocket(name, value, &error) != PROP_SUCCESS) {
                 LOG(ERROR) << "Could not set '" << name << "' to '" << value
                            << "' in /data/local.prop: " << error;
             }
@@ -861,7 +918,7 @@
                 LOG(INFO) << "Setting product property " << base_prop << " to '" << target_prop_val
                           << "' (from " << target_prop << ")";
                 std::string error;
-                uint32_t res = PropertySet(base_prop, target_prop_val, &error);
+                auto res = PropertySetNoSocket(base_prop, target_prop_val, &error);
                 if (res != PROP_SUCCESS) {
                     LOG(ERROR) << "Error setting product property " << base_prop << ": err=" << res
                                << " (" << error << ")";
@@ -890,7 +947,7 @@
     }
 
     std::string error;
-    auto res = PropertySet(ID_PROP, build_id, &error);
+    auto res = PropertySetNoSocket(ID_PROP, build_id, &error);
     if (res != PROP_SUCCESS) {
         LOG(ERROR) << "Failed to set " << ID_PROP << " to " << build_id;
     }
@@ -938,7 +995,7 @@
               << legacy_build_fingerprint << "'";
 
     std::string error;
-    uint32_t res = PropertySet(LEGACY_FINGERPRINT_PROP, legacy_build_fingerprint, &error);
+    auto res = PropertySetNoSocket(LEGACY_FINGERPRINT_PROP, legacy_build_fingerprint, &error);
     if (res != PROP_SUCCESS) {
         LOG(ERROR) << "Error setting property '" << LEGACY_FINGERPRINT_PROP << "': err=" << res
                    << " (" << error << ")";
@@ -956,7 +1013,7 @@
     LOG(INFO) << "Setting property '" << FINGERPRINT_PROP << "' to '" << build_fingerprint << "'";
 
     std::string error;
-    uint32_t res = PropertySet(FINGERPRINT_PROP, build_fingerprint, &error);
+    auto res = PropertySetNoSocket(FINGERPRINT_PROP, build_fingerprint, &error);
     if (res != PROP_SUCCESS) {
         LOG(ERROR) << "Error setting property '" << FINGERPRINT_PROP << "': err=" << res << " ("
                    << error << ")";
@@ -1018,7 +1075,7 @@
         LOG(INFO) << "Setting property '" << prop << "' to '" << prop_val << "'";
 
         std::string error;
-        uint32_t res = PropertySet(prop, prop_val, &error);
+        auto res = PropertySetNoSocket(prop, prop_val, &error);
         if (res != PROP_SUCCESS) {
             LOG(ERROR) << "Error setting property '" << prop << "': err=" << res << " (" << error
                        << ")";
@@ -1052,7 +1109,7 @@
     int api_level = std::min(read_api_level_props(BOARD_API_LEVEL_PROPS),
                              read_api_level_props(DEVICE_API_LEVEL_PROPS));
     std::string error;
-    uint32_t res = PropertySet(VENDOR_API_LEVEL_PROP, std::to_string(api_level), &error);
+    auto res = PropertySetNoSocket(VENDOR_API_LEVEL_PROP, std::to_string(api_level), &error);
     if (res != PROP_SUCCESS) {
         LOG(ERROR) << "Failed to set " << VENDOR_API_LEVEL_PROP << " with " << api_level << ": "
                    << error << "(" << res << ")";
@@ -1146,7 +1203,7 @@
 
     for (const auto& [name, value] : properties) {
         std::string error;
-        if (PropertySet(name, value, &error) != PROP_SUCCESS) {
+        if (PropertySetNoSocket(name, value, &error) != PROP_SUCCESS) {
             LOG(ERROR) << "Could not set '" << name << "' to '" << value
                        << "' while loading .prop files" << error;
         }
@@ -1388,6 +1445,46 @@
     }
 }
 
+PersistWriteThread::PersistWriteThread() {
+    auto new_thread = std::thread([this]() -> void { Work(); });
+    thread_.swap(new_thread);
+}
+
+void PersistWriteThread::Work() {
+    while (true) {
+        std::tuple<std::string, std::string, SocketConnection> item;
+
+        // Grab the next item within the lock.
+        {
+            std::unique_lock<std::mutex> lock(mutex_);
+
+            while (work_.empty()) {
+                cv_.wait(lock);
+            }
+
+            item = std::move(work_.front());
+            work_.pop_front();
+        }
+
+        std::this_thread::sleep_for(1s);
+
+        // Perform write/fsync outside the lock.
+        WritePersistentProperty(std::get<0>(item), std::get<1>(item));
+        NotifyPropertyChange(std::get<0>(item), std::get<1>(item));
+
+        SocketConnection& socket = std::get<2>(item);
+        socket.SendUint32(PROP_SUCCESS);
+    }
+}
+
+void PersistWriteThread::Write(std::string name, std::string value, SocketConnection socket) {
+    {
+        std::unique_lock<std::mutex> lock(mutex_);
+        work_.emplace_back(std::move(name), std::move(value), std::move(socket));
+    }
+    cv_.notify_all();
+}
+
 void StartPropertyService(int* epoll_socket) {
     InitPropertySet("ro.property_service.version", "2");
 
@@ -1412,6 +1509,13 @@
 
     auto new_thread = std::thread{PropertyServiceThread};
     property_service_thread.swap(new_thread);
+
+    auto async_persist_writes =
+            android::base::GetBoolProperty("ro.property_service.async_persist_writes", false);
+
+    if (async_persist_writes) {
+        persist_write_thread = std::make_unique<PersistWriteThread>();
+    }
 }
 
 }  // namespace init
diff --git a/init/property_service.h b/init/property_service.h
index 2d49a36..71a609c 100644
--- a/init/property_service.h
+++ b/init/property_service.h
@@ -18,7 +18,11 @@
 
 #include <sys/socket.h>
 
+#include <condition_variable>
+#include <deque>
+#include <mutex>
 #include <string>
+#include <thread>
 
 #include "epoll.h"
 
diff --git a/init/service.cpp b/init/service.cpp
index b9b3309..87d9c3a 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -26,6 +26,7 @@
 #include <sys/time.h>
 #include <termios.h>
 #include <unistd.h>
+#include <thread>
 
 #include <android-base/file.h>
 #include <android-base/logging.h>
@@ -887,6 +888,10 @@
     }
 
     if (pid_) {
+        if (flags_ & SVC_GENTLE_KILL) {
+            KillProcessGroup(SIGTERM);
+            if (!process_cgroup_empty()) std::this_thread::sleep_for(200ms);
+        }
         KillProcessGroup(SIGKILL);
         NotifyStateChange("stopping");
     } else {
diff --git a/init/service.h b/init/service.h
index f9749d2..3ef8902 100644
--- a/init/service.h
+++ b/init/service.h
@@ -56,6 +56,8 @@
                                      // should not be killed during shutdown
 #define SVC_TEMPORARY 0x1000  // This service was started by 'exec' and should be removed from the
                               // service list once it is reaped.
+#define SVC_GENTLE_KILL 0x2000  // This service should be stopped with SIGTERM instead of SIGKILL
+                                // Will still be SIGKILLed after timeout period of 200 ms
 
 #define NR_SVC_SUPP_GIDS 12    // twelve supplementary groups
 
diff --git a/init/service_parser.cpp b/init/service_parser.cpp
index 24a2024..3563084 100644
--- a/init/service_parser.cpp
+++ b/init/service_parser.cpp
@@ -151,6 +151,11 @@
     return {};
 }
 
+Result<void> ServiceParser::ParseGentleKill(std::vector<std::string>&& args) {
+    service_->flags_ |= SVC_GENTLE_KILL;
+    return {};
+}
+
 Result<void> ServiceParser::ParseGroup(std::vector<std::string>&& args) {
     auto gid = DecodeUid(args[1]);
     if (!gid.ok()) {
@@ -584,6 +589,7 @@
         {"disabled",                {0,     0,    &ServiceParser::ParseDisabled}},
         {"enter_namespace",         {2,     2,    &ServiceParser::ParseEnterNamespace}},
         {"file",                    {2,     2,    &ServiceParser::ParseFile}},
+        {"gentle_kill",             {0,     0,    &ServiceParser::ParseGentleKill}},
         {"group",                   {1,     NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}},
         {"interface",               {2,     2,    &ServiceParser::ParseInterface}},
         {"ioprio",                  {2,     2,    &ServiceParser::ParseIoprio}},
diff --git a/init/service_parser.h b/init/service_parser.h
index 54503dd..670a5c6 100644
--- a/init/service_parser.h
+++ b/init/service_parser.h
@@ -53,6 +53,7 @@
     Result<void> ParseDisabled(std::vector<std::string>&& args);
     Result<void> ParseEnterNamespace(std::vector<std::string>&& args);
     Result<void> ParseGroup(std::vector<std::string>&& args);
+    Result<void> ParseGentleKill(std::vector<std::string>&& args);
     Result<void> ParsePriority(std::vector<std::string>&& args);
     Result<void> ParseInterface(std::vector<std::string>&& args);
     Result<void> ParseIoprio(std::vector<std::string>&& args);
diff --git a/init/snapuserd_transition.cpp b/init/snapuserd_transition.cpp
index 6972f30..3a9ff5b 100644
--- a/init/snapuserd_transition.cpp
+++ b/init/snapuserd_transition.cpp
@@ -112,6 +112,10 @@
 
     setenv(kSnapuserdFirstStagePidVar, std::to_string(pid).c_str(), 1);
 
+    if (!client->RemoveTransitionedDaemonIndicator()) {
+        LOG(ERROR) << "RemoveTransitionedDaemonIndicator failed";
+    }
+
     LOG(INFO) << "Relaunched snapuserd with pid: " << pid;
 }
 
@@ -263,6 +267,19 @@
  * we may see audit logs.
  */
 bool SnapuserdSelinuxHelper::TestSnapuserdIsReady() {
+    // Wait for the daemon to be fully up. Daemon will write to path
+    // /metadata/ota/daemon-alive-indicator only when all the threads
+    // are ready and attached to dm-user.
+    //
+    // This check will fail for GRF devices with vendor on Android S.
+    // snapuserd binary from Android S won't be able to communicate
+    // and hence, we will fallback and issue I/O to verify
+    // the presence of daemon.
+    auto client = std::make_unique<SnapuserdClient>();
+    if (!client->IsTransitionedDaemonReady()) {
+        LOG(ERROR) << "IsTransitionedDaemonReady failed";
+    }
+
     std::string dev = "/dev/block/mapper/system"s + fs_mgr_get_slot_suffix();
     android::base::unique_fd fd(open(dev.c_str(), O_RDONLY | O_DIRECT));
     if (fd < 0) {
diff --git a/libcutils/TEST_MAPPING b/libcutils/TEST_MAPPING
index 6477502..eb63aa5 100644
--- a/libcutils/TEST_MAPPING
+++ b/libcutils/TEST_MAPPING
@@ -8,5 +8,10 @@
     {
       "name": "libcutils_test"
     }
+  ],
+  "kernel-presubmit": [
+    {
+      "name": "libcutils_test"
+    }
   ]
 }
diff --git a/mkbootfs/mkbootfs.c b/mkbootfs/mkbootfs.c
index 64e5a8c..d3922bf 100644
--- a/mkbootfs/mkbootfs.c
+++ b/mkbootfs/mkbootfs.c
@@ -1,20 +1,18 @@
 
 #include <ctype.h>
+#include <dirent.h>
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <getopt.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/sysmacros.h>
-#include <dirent.h>
-
-#include <stdarg.h>
-#include <fcntl.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include <linux/kdev_t.h>
 
@@ -23,23 +21,12 @@
 
 /* NOTES
 **
-** - see buffer-format.txt from the linux kernel docs for
-**   an explanation of this file format
+** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
+**   for an explanation of this file format
 ** - dotfiles are ignored
 ** - directories named 'root' are ignored
 */
 
-static void die(const char* why, ...) {
-    va_list ap;
-
-    va_start(ap, why);
-    fprintf(stderr,"error: ");
-    vfprintf(stderr, why, ap);
-    fprintf(stderr,"\n");
-    va_end(ap);
-    exit(1);
-}
-
 struct fs_config_entry {
     char* name;
     int uid, gid, mode;
@@ -48,17 +35,8 @@
 static struct fs_config_entry* canned_config = NULL;
 static const char* target_out_path = NULL;
 
-/* Each line in the canned file should be a path plus three ints (uid,
- * gid, mode). */
-#ifdef PATH_MAX
-#define CANNED_LINE_LENGTH  (PATH_MAX+100)
-#else
-#define CANNED_LINE_LENGTH  (1024)
-#endif
-
 #define TRAILER "TRAILER!!!"
 
-static int verbose = 0;
 static int total_size = 0;
 
 static void fix_stat(const char *path, struct stat *s)
@@ -134,7 +112,7 @@
 
     total_size += 6 + 8*13 + olen + 1;
 
-    if(strlen(out) != (unsigned int)olen) die("ACK!");
+    if(strlen(out) != (unsigned int)olen) errx(1, "ACK!");
 
     while(total_size & 3) {
         total_size++;
@@ -168,23 +146,16 @@
 static void _archive_dir(char *in, char *out, int ilen, int olen)
 {
     int i, t;
-    DIR *d;
     struct dirent *de;
 
-    if(verbose) {
-        fprintf(stderr,"_archive_dir('%s','%s',%d,%d)\n",
-                in, out, ilen, olen);
-    }
-
-    d = opendir(in);
-    if(d == 0) die("cannot open directory '%s'", in);
+    DIR* d = opendir(in);
+    if (d == NULL) err(1, "cannot open directory '%s'", in);
 
     int size = 32;
     int entries = 0;
     char** names = malloc(size * sizeof(char*));
     if (names == NULL) {
-      fprintf(stderr, "failed to allocate dir names array (size %d)\n", size);
-      exit(1);
+      errx(1, "failed to allocate dir names array (size %d)", size);
     }
 
     while((de = readdir(d)) != 0){
@@ -198,16 +169,12 @@
           size *= 2;
           names = realloc(names, size * sizeof(char*));
           if (names == NULL) {
-            fprintf(stderr, "failed to reallocate dir names array (size %d)\n",
-                    size);
-            exit(1);
+            errx(1, "failed to reallocate dir names array (size %d)", size);
           }
         }
         names[entries] = strdup(de->d_name);
         if (names[entries] == NULL) {
-          fprintf(stderr, "failed to strdup name \"%s\"\n",
-                  de->d_name);
-          exit(1);
+          errx(1, "failed to strdup name \"%s\"", de->d_name);
         }
         ++entries;
     }
@@ -241,26 +208,17 @@
 static void _archive(char *in, char *out, int ilen, int olen)
 {
     struct stat s;
-
-    if(verbose) {
-        fprintf(stderr,"_archive('%s','%s',%d,%d)\n",
-                in, out, ilen, olen);
-    }
-
-    if(lstat(in, &s)) die("could not stat '%s'\n", in);
+    if(lstat(in, &s)) err(1, "could not stat '%s'", in);
 
     if(S_ISREG(s.st_mode)){
-        char *tmp;
-        int fd;
+        int fd = open(in, O_RDONLY);
+        if(fd < 0) err(1, "cannot open '%s' for read", in);
 
-        fd = open(in, O_RDONLY);
-        if(fd < 0) die("cannot open '%s' for read", in);
-
-        tmp = (char*) malloc(s.st_size);
-        if(tmp == 0) die("cannot allocate %d bytes", s.st_size);
+        char* tmp = (char*) malloc(s.st_size);
+        if(tmp == 0) errx(1, "cannot allocate %zd bytes", s.st_size);
 
         if(read(fd, tmp, s.st_size) != s.st_size) {
-            die("cannot read %d bytes", s.st_size);
+            err(1, "cannot read %zd bytes", s.st_size);
         }
 
         _eject(&s, out, olen, tmp, s.st_size);
@@ -274,13 +232,13 @@
         char buf[1024];
         int size;
         size = readlink(in, buf, 1024);
-        if(size < 0) die("cannot read symlink '%s'", in);
+        if(size < 0) err(1, "cannot read symlink '%s'", in);
         _eject(&s, out, olen, buf, size);
     } else if(S_ISBLK(s.st_mode) || S_ISCHR(s.st_mode) ||
               S_ISFIFO(s.st_mode) || S_ISSOCK(s.st_mode)) {
         _eject(&s, out, olen, NULL, 0);
     } else {
-        die("Unknown '%s' (mode %d)?\n", in, s.st_mode);
+        errx(1, "Unknown '%s' (mode %d)?", in, s.st_mode);
     }
 }
 
@@ -302,17 +260,18 @@
     canned_config =
         (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry));
 
-    char line[CANNED_LINE_LENGTH];
-    FILE* f = fopen(filename, "r");
-    if (f == NULL) die("failed to open canned file '%s'", filename);
+    FILE* fp = fopen(filename, "r");
+    if (fp == NULL) err(1, "failed to open canned file '%s'", filename);
 
-    while (fgets(line, CANNED_LINE_LENGTH, f) != NULL) {
+    char* line = NULL;
+    size_t allocated_len;
+    while (getline(&line, &allocated_len, fp) != -1) {
         if (!line[0]) break;
         if (used >= allocated) {
             allocated *= 2;
             canned_config = (struct fs_config_entry*)realloc(
                 canned_config, allocated * sizeof(struct fs_config_entry));
-            if (canned_config == NULL) die("failed to reallocate memory");
+            if (canned_config == NULL) errx(1, "failed to reallocate memory");
         }
 
         struct fs_config_entry* cc = canned_config + used;
@@ -332,17 +291,18 @@
         ++allocated;
         canned_config = (struct fs_config_entry*)realloc(
             canned_config, allocated * sizeof(struct fs_config_entry));
-        if (canned_config == NULL) die("failed to reallocate memory");
+        if (canned_config == NULL) errx(1, "failed to reallocate memory");
     }
     canned_config[used].name = NULL;
 
-    fclose(f);
+    free(line);
+    fclose(fp);
 }
 
 static void devnodes_desc_error(const char* filename, unsigned long line_num,
                               const char* msg)
 {
-    errx(EXIT_FAILURE, "failed to read nodes desc file '%s' line %lu: %s", filename, line_num, msg);
+    errx(1, "failed to read nodes desc file '%s' line %lu: %s", filename, line_num, msg);
 }
 
 static int append_devnodes_desc_dir(char* path, char* args)
@@ -386,15 +346,15 @@
 
 static void append_devnodes_desc(const char* filename)
 {
-    FILE* f = fopen(filename, "re");
-    if (!f) err(EXIT_FAILURE, "failed to open nodes description file '%s'", filename);
+    FILE* fp = fopen(filename, "re");
+    if (!fp) err(1, "failed to open nodes description file '%s'", filename);
 
-    char *line, *args, *type, *path;
     unsigned long line_num = 0;
-    size_t allocated_len;
 
-    while (getline(&line, &allocated_len, f) != -1) {
-        char* type;
+    char* line = NULL;
+    size_t allocated_len;
+    while (getline(&line, &allocated_len, fp) != -1) {
+        char *type, *path, *args;
 
         line_num++;
 
@@ -428,7 +388,7 @@
     }
 
     free(line);
-    fclose(f);
+    fclose(fp);
 }
 
 static const struct option long_options[] = {
@@ -448,7 +408,8 @@
             "\t-f, --file=FILE: Canned configuration file\n"
             "\t-h, --help: Print this help\n"
             "\t-n, --nodes=FILE: Dev nodes description file\n"
-            "\nDev nodes description:\n"
+            "\n"
+            "Dev nodes description:\n"
             "\t[dir|nod] [perms] [uid] [gid] [c|b] [minor] [major]\n"
             "\tExample:\n"
             "\t\t# My device nodes\n"
@@ -477,7 +438,7 @@
             break;
         default:
             usage();
-            die("Unknown option %s", argv[optind - 1]);
+            errx(1, "Unknown option %s", argv[optind - 1]);
         }
     }
 
@@ -486,7 +447,7 @@
 
     if (num_dirs <= 0) {
         usage();
-        die("no directories to process?!");
+        errx(1, "no directories to process?!");
     }
 
     while(num_dirs-- > 0){
diff --git a/rootdir/init.rc b/rootdir/init.rc
index efad37c..a2fb88a 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -1286,6 +1286,7 @@
     group shell log readproc
     seclabel u:r:shell:s0
     setenv HOSTNAME console
+    shutdown critical
 
 on property:ro.debuggable=1
     # Give writes to anyone for the trace folder on debug builds.
diff --git a/storaged/tests/storaged_test.cpp b/storaged/tests/storaged_test.cpp
index bb71bf3..9a281c2 100644
--- a/storaged/tests/storaged_test.cpp
+++ b/storaged/tests/storaged_test.cpp
@@ -284,6 +284,8 @@
     dsm_detect.update_mean();
     dsm_detect.update_std();
 
+    // FixLater: avoid floating point loop counters
+    // NOLINTNEXTLINE(clang-analyzer-security.FloatLoopCounter,cert-flp30-c)
     for (double i = 0; i < 2 * dsm_detect.mSigma; i += 0.5) {
         struct disk_perf test_perf;
         struct disk_perf test_mean = dsm_detect.mMean;
diff --git a/trusty/keymaster/Android.bp b/trusty/keymaster/Android.bp
index 31f0a72..b249013 100644
--- a/trusty/keymaster/Android.bp
+++ b/trusty/keymaster/Android.bp
@@ -181,3 +181,30 @@
         "-Werror",
     ],
 }
+
+cc_binary {
+    name: "trusty_keymaster_set_attestation_ids",
+    vendor: true,
+
+    srcs: [
+        "set_attestation_ids/set_attestation_ids.cpp",
+        "ipc/trusty_keymaster_ipc.cpp",
+    ],
+
+    local_include_dirs: ["include"],
+
+    shared_libs: [
+        "libbase",
+        "libc",
+        "libcrypto",
+        "liblog",
+        "libtrusty",
+        "libhardware",
+        "libkeymaster_messages",
+        "libutils",
+    ],
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+}
diff --git a/trusty/keymaster/set_attestation_ids/set_attestation_ids.cpp b/trusty/keymaster/set_attestation_ids/set_attestation_ids.cpp
new file mode 100644
index 0000000..e944167
--- /dev/null
+++ b/trusty/keymaster/set_attestation_ids/set_attestation_ids.cpp
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2020 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 <string>
+
+#include <android-base/properties.h>
+#include <trusty_keymaster/ipc/trusty_keymaster_ipc.h>
+
+namespace {
+
+const char* sopts = "hb:d:p:s:M:m:i:c:";
+const struct option lopts[] = {
+        {"help", no_argument, nullptr, 'h'},
+        {"brand", required_argument, nullptr, 'b'},
+        {"device", required_argument, nullptr, 'd'},
+        {"product", required_argument, nullptr, 'p'},
+        {"serial", required_argument, nullptr, 's'},
+        {"manufacturer", required_argument, nullptr, 'M'},
+        {"model", required_argument, nullptr, 'm'},
+        {"imei", required_argument, nullptr, 'i'},
+        {"meid", required_argument, nullptr, 'c'},
+        {0, 0, 0, 0},
+};
+
+std::string buf2string(const keymaster::Buffer& buf) {
+    return std::string(reinterpret_cast<const char*>(buf.peek_read()), buf.available_read());
+}
+
+void print_usage(const char* prog, const keymaster::SetAttestationIdsRequest& req) {
+    fprintf(stderr,
+            "Usage: %s [options]\n"
+            "\n"
+            "options:\n"
+            "  -h, --help                 prints this message and exit\n"
+            "  -b, --brand <val>          set brand (default '%s')\n"
+            "  -d, --device <val>         set device (default '%s')\n"
+            "  -p, --product <val>        set product (default '%s')\n"
+            "  -s, --serial <val>         set serial (default '%s')\n"
+            "  -M, --manufacturer <val>   set manufacturer (default '%s')\n"
+            "  -m, --model <val>          set model (default '%s')\n"
+            "  -i, --imei <val>           set IMEI (default '%s')\n"
+            "  -c, --meid <val>           set MEID (default '%s')\n"
+            "\n",
+            prog, buf2string(req.brand).c_str(), buf2string(req.device).c_str(),
+            buf2string(req.product).c_str(), buf2string(req.serial).c_str(),
+            buf2string(req.manufacturer).c_str(), buf2string(req.model).c_str(),
+            buf2string(req.imei).c_str(), buf2string(req.meid).c_str());
+}
+
+void set_from_prop(keymaster::Buffer* buf, const std::string& prop) {
+    std::string prop_value = ::android::base::GetProperty(prop, /* default_value = */ "");
+    if (!prop_value.empty()) {
+        buf->Reinitialize(prop_value.data(), prop_value.size());
+    }
+}
+
+void populate_ids(keymaster::SetAttestationIdsRequest* req) {
+    set_from_prop(&req->brand, "ro.product.brand");
+    set_from_prop(&req->device, "ro.product.device");
+    set_from_prop(&req->product, "ro.product.name");
+    set_from_prop(&req->serial, "ro.serialno");
+    set_from_prop(&req->manufacturer, "ro.product.manufacturer");
+    set_from_prop(&req->model, "ro.product.model");
+}
+
+}  // namespace
+
+int main(int argc, char** argv) {
+    // By default, set attestation IDs to the values in userspace properties.
+    keymaster::SetAttestationIdsRequest req(/* ver = */ 4);
+    populate_ids(&req);
+
+    while (true) {
+        int oidx = 0;
+        int c = getopt_long(argc, argv, sopts, lopts, &oidx);
+        if (c == -1) {
+            break; /* done */
+        }
+
+        switch (c) {
+            case 'b':
+                req.brand.Reinitialize(optarg, strlen(optarg));
+                break;
+            case 'd':
+                req.device.Reinitialize(optarg, strlen(optarg));
+                break;
+            case 'p':
+                req.product.Reinitialize(optarg, strlen(optarg));
+                break;
+            case 's':
+                req.serial.Reinitialize(optarg, strlen(optarg));
+                break;
+            case 'M':
+                req.manufacturer.Reinitialize(optarg, strlen(optarg));
+                break;
+            case 'm':
+                req.model.Reinitialize(optarg, strlen(optarg));
+                break;
+            case 'i':
+                req.imei.Reinitialize(optarg, strlen(optarg));
+                break;
+            case 'c':
+                req.meid.Reinitialize(optarg, strlen(optarg));
+                break;
+            case 'h':
+                print_usage(argv[0], req);
+                exit(EXIT_SUCCESS);
+            default:
+                print_usage(argv[0], req);
+                exit(EXIT_FAILURE);
+        }
+    }
+    if (optind != argc) {
+        print_usage(argv[0], req);
+        exit(EXIT_FAILURE);
+    }
+
+    int ret = trusty_keymaster_connect();
+    if (ret) {
+        fprintf(stderr, "trusty_keymaster_connect failed: %d\n", ret);
+        return EXIT_FAILURE;
+    }
+
+    printf("Setting:\n"
+           "  brand:        %s\n"
+           "  device:       %s\n"
+           "  product:      %s\n"
+           "  serial:       %s\n"
+           "  manufacturer: %s\n"
+           "  model:        %s\n"
+           "  IMEI:         %s\n"
+           "  MEID:         %s\n",
+           buf2string(req.brand).c_str(), buf2string(req.device).c_str(),
+           buf2string(req.product).c_str(), buf2string(req.serial).c_str(),
+           buf2string(req.manufacturer).c_str(), buf2string(req.model).c_str(),
+           buf2string(req.imei).c_str(), buf2string(req.meid).c_str());
+
+    keymaster::EmptyKeymasterResponse rsp(/* ver = */ 4);
+    ret = trusty_keymaster_send(KM_SET_ATTESTATION_IDS, req, &rsp);
+    if (ret) {
+        fprintf(stderr, "SET_ATTESTATION_IDS failed: %d\n", ret);
+        trusty_keymaster_disconnect();
+        return EXIT_FAILURE;
+    }
+
+    return EXIT_SUCCESS;
+}
diff --git a/usbd/Android.bp b/usbd/Android.bp
index 27db0fa..e67759c 100644
--- a/usbd/Android.bp
+++ b/usbd/Android.bp
@@ -8,10 +8,12 @@
     srcs: ["usbd.cpp"],
     shared_libs: [
         "libbase",
+        "libbinder_ndk",
         "libhidlbase",
         "liblog",
         "libutils",
         "libhardware",
         "android.hardware.usb.gadget@1.0",
+        "android.hardware.usb.gadget-V1-ndk",
     ],
 }
diff --git a/usbd/usbd.cpp b/usbd/usbd.cpp
index 6e24d8e..0616cfb 100644
--- a/usbd/usbd.cpp
+++ b/usbd/usbd.cpp
@@ -18,43 +18,78 @@
 
 #include <string>
 
+#include <aidl/android/hardware/usb/gadget/GadgetFunction.h>
+#include <aidl/android/hardware/usb/gadget/IUsbGadget.h>
 #include <android-base/logging.h>
 #include <android-base/properties.h>
+#include <android/binder_manager.h>
+#include <android/binder_process.h>
 #include <android/hardware/usb/gadget/1.0/IUsbGadget.h>
 
-#include <hidl/HidlTransportSupport.h>
-
+using aidl::android::hardware::usb::gadget::GadgetFunction;
 using android::base::GetProperty;
 using android::base::SetProperty;
-using android::hardware::configureRpcThreadpool;
-using android::hardware::usb::gadget::V1_0::GadgetFunction;
-using android::hardware::usb::gadget::V1_0::IUsbGadget;
 using android::hardware::Return;
+using ndk::ScopedAStatus;
+using std::shared_ptr;
+
+std::atomic<int> sUsbOperationCount{};
 
 int main(int /*argc*/, char** /*argv*/) {
     if (GetProperty("ro.bootmode", "") == "charger") exit(0);
+    int operationId = sUsbOperationCount++;
 
-    configureRpcThreadpool(1, true /*callerWillJoin*/);
-    android::sp<IUsbGadget> gadget = IUsbGadget::getService();
-    Return<void> ret;
+    ABinderProcess_setThreadPoolMaxThreadCount(1);
+    ABinderProcess_startThreadPool();
+    const std::string service_name =
+            std::string(aidl::android::hardware::usb::gadget::IUsbGadget::descriptor)
+                    .append("/default");
 
-    if (gadget != nullptr) {
-        LOG(INFO) << "Usb HAL found.";
-        std::string function = GetProperty("persist.sys.usb.config", "");
-        if (function == "adb") {
-            LOG(INFO) << "peristent prop is adb";
-            SetProperty("ctl.start", "adbd");
-            ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::ADB),
-                                                 nullptr, 0);
+    std::string function = GetProperty("persist.sys.usb.config", "");
+    if (function == "adb") {
+        LOG(INFO) << "persistent prop is adb";
+        SetProperty("ctl.start", "adbd");
+    }
+
+    if (AServiceManager_isDeclared(service_name.c_str())) {
+        shared_ptr<aidl::android::hardware::usb::gadget::IUsbGadget> gadget_aidl =
+                aidl::android::hardware::usb::gadget::IUsbGadget::fromBinder(
+                        ndk::SpAIBinder(AServiceManager_waitForService(service_name.c_str())));
+        ScopedAStatus ret;
+        if (gadget_aidl != nullptr) {
+            LOG(INFO) << "Usb AIDL HAL found.";
+            if (function == "adb") {
+                ret = gadget_aidl->setCurrentUsbFunctions(
+                        static_cast<uint64_t>(GadgetFunction::ADB), nullptr, 0, operationId);
+            } else {
+                LOG(INFO) << "Signal MTP to enable default functions";
+                ret = gadget_aidl->setCurrentUsbFunctions(
+                        static_cast<uint64_t>(GadgetFunction::MTP), nullptr, 0, operationId);
+            }
+
+            if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal";
         } else {
-            LOG(INFO) << "Signal MTP to enable default functions";
-            ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::MTP),
-                                                 nullptr, 0);
+            LOG(INFO) << "Usb AIDL HAL not found";
         }
-
-        if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal";
     } else {
-        LOG(INFO) << "Usb HAL not found";
+        android::sp<android::hardware::usb::gadget::V1_0::IUsbGadget> gadget =
+                android::hardware::usb::gadget::V1_0::IUsbGadget::getService();
+        Return<void> ret;
+        if (gadget != nullptr) {
+            LOG(INFO) << "Usb HAL found.";
+            if (function == "adb") {
+                ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::ADB),
+                                                     nullptr, 0);
+            } else {
+                LOG(INFO) << "Signal MTP to enable default functions";
+                ret = gadget->setCurrentUsbFunctions(static_cast<uint64_t>(GadgetFunction::MTP),
+                                                     nullptr, 0);
+            }
+
+            if (!ret.isOk()) LOG(ERROR) << "Error while invoking usb hal";
+        } else {
+            LOG(INFO) << "Usb HAL not found";
+        }
     }
     exit(0);
 }