Merge "Speed up StepIfSignalHandler path."
diff --git a/fs_mgr/fs_mgr_verity.cpp b/fs_mgr/fs_mgr_verity.cpp
index 5de0903..7f8e1e2 100644
--- a/fs_mgr/fs_mgr_verity.cpp
+++ b/fs_mgr/fs_mgr_verity.cpp
@@ -841,9 +841,15 @@
 
     // verify the signature on the table
     if (verify_verity_signature(verity) < 0) {
+        // Allow signature verification error when the device is unlocked
+        if (fs_mgr_is_device_unlocked()) {
+            retval = FS_MGR_SETUP_VERITY_SKIPPED;
+            LWARNING << "Allow signature verification error when the device is unlocked";
+            goto out;
+        }
         if (params.mode == VERITY_MODE_LOGGING) {
             // the user has been warned, allow mounting without dm-verity
-            retval = FS_MGR_SETUP_VERITY_SUCCESS;
+            retval = FS_MGR_SETUP_VERITY_SKIPPED;
             goto out;
         }
 
diff --git a/init/reboot.cpp b/init/reboot.cpp
index 8196d58..d096b0d 100644
--- a/init/reboot.cpp
+++ b/init/reboot.cpp
@@ -323,11 +323,11 @@
     UmountStat stat = UmountPartitions(timeout - t.duration());
     if (stat != UMOUNT_STAT_SUCCESS) {
         LOG(INFO) << "umount timeout, last resort, kill all and try";
-        if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
+        if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
         KillAllProcesses();
         // even if it succeeds, still it is timeout and do not run fsck with all processes killed
-        UmountPartitions(0ms);
-        if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
+        UmountStat st = UmountPartitions(0ms);
+        if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
     }
 
     if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
@@ -340,13 +340,6 @@
     return stat;
 }
 
-static void __attribute__((noreturn)) DoThermalOff() {
-    LOG(WARNING) << "Thermal system shutdown";
-    sync();
-    RebootSystem(ANDROID_RB_THERMOFF, "");
-    abort();
-}
-
 void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
               bool runFsck) {
     Timer t;
@@ -355,19 +348,25 @@
     android::base::WriteStringToFile(StringPrintf("%s\n", reason.c_str()), LAST_REBOOT_REASON_FILE,
                                      S_IRUSR | S_IWUSR, AID_SYSTEM, AID_SYSTEM);
 
-    if (cmd == ANDROID_RB_THERMOFF) {  // do not wait if it is thermal
-        DoThermalOff();
-        abort();
+    bool is_thermal_shutdown = false;
+    if (cmd == ANDROID_RB_THERMOFF) {
+        is_thermal_shutdown = true;
+        runFsck = false;
     }
 
-    auto shutdown_timeout = 0s;
+    auto shutdown_timeout = 0ms;
     if (!SHUTDOWN_ZERO_TIMEOUT) {
-        constexpr unsigned int shutdown_timeout_default = 6;
-        auto shutdown_timeout_property =
-            android::base::GetUintProperty("ro.build.shutdown_timeout", shutdown_timeout_default);
-        shutdown_timeout = std::chrono::seconds(shutdown_timeout_property);
+        if (is_thermal_shutdown) {
+            constexpr unsigned int thermal_shutdown_timeout = 1;
+            shutdown_timeout = std::chrono::seconds(thermal_shutdown_timeout);
+        } else {
+            constexpr unsigned int shutdown_timeout_default = 6;
+            auto shutdown_timeout_property = android::base::GetUintProperty(
+                "ro.build.shutdown_timeout", shutdown_timeout_default);
+            shutdown_timeout = std::chrono::seconds(shutdown_timeout_property);
+        }
     }
-    LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " seconds";
+    LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
 
     // keep debugging tools until non critical ones are all gone.
     const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
@@ -394,7 +393,7 @@
 
     // optional shutdown step
     // 1. terminate all services except shutdown critical ones. wait for delay to finish
-    if (shutdown_timeout > 0s) {
+    if (shutdown_timeout > 0ms) {
         LOG(INFO) << "terminating init services";
 
         // Ask all services to terminate except shutdown critical ones.
@@ -403,8 +402,8 @@
         });
 
         int service_count = 0;
-        // Up to half as long as shutdown_timeout or 3 seconds, whichever is lower.
-        auto termination_wait_timeout = std::min((shutdown_timeout + 1s) / 2, 3s);
+        // Only wait up to half of timeout here
+        auto termination_wait_timeout = shutdown_timeout / 2;
         while (t.duration() < termination_wait_timeout) {
             ServiceManager::GetInstance().ReapAnyOutstandingChildren();
 
@@ -456,7 +455,7 @@
     UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration());
     // Follow what linux shutdown is doing: one more sync with little bit delay
     sync();
-    std::this_thread::sleep_for(100ms);
+    if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
     LogShutdownTime(stat, &t);
     // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
     RebootSystem(cmd, rebootTarget);
diff --git a/init/ueventd_test.cpp b/init/ueventd_test.cpp
index 86d7055..4d9a1fa 100644
--- a/init/ueventd_test.cpp
+++ b/init/ueventd_test.cpp
@@ -63,7 +63,10 @@
 }
 
 TEST(ueventd, setegid_IsPerThread) {
-    if (getuid() != 0) return;
+    if (getuid() != 0) {
+        GTEST_LOG_(INFO) << "Skipping test, must be run as root.";
+        return;
+    }
 
     TemporaryDir dir;
 
@@ -78,13 +81,20 @@
 
     for (const auto& [file, expected_gid] : files_and_gids) {
         struct stat info;
-        EXPECT_EQ(0, stat(file.c_str(), &info));
+        ASSERT_EQ(0, stat(file.c_str(), &info));
         EXPECT_EQ(expected_gid, info.st_gid);
     }
 }
 
 TEST(ueventd, setfscreatecon_IsPerThread) {
-    if (getuid() != 0) return;
+    if (getuid() != 0) {
+        GTEST_LOG_(INFO) << "Skipping test, must be run as root.";
+        return;
+    }
+    if (!is_selinux_enabled() || security_getenforce() == 1) {
+        GTEST_LOG_(INFO) << "Skipping test, SELinux must be enabled and in permissive mode.";
+        return;
+    }
 
     const char* const contexts[] = {
         "u:object_r:audio_device:s0",
@@ -105,7 +115,7 @@
 
     for (const auto& [file, expected_context] : files_and_contexts) {
         char* file_context;
-        EXPECT_GT(getfilecon(file.c_str(), &file_context), 0);
+        ASSERT_GT(getfilecon(file.c_str(), &file_context), 0);
         EXPECT_EQ(expected_context, file_context);
         freecon(file_context);
     }