From 98062dcd89da6913039da954924ca430b6914c52 Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Mon, 22 Feb 2021 15:10:45 -0500 Subject: [PATCH] vold: Use Wakelock::tryGet() Acquiring a wakelock can fail if the suspend service is unavailable. Explicitly check that wakelock was acquired before performing operations that require the device to stay on. Bug: b/179229598 Test: Boot test on Pixel 4 device Change-Id: If30087223e44098801a31d1bfd239ac22e891abe --- Benchmark.cpp | 5 ++++- IdleMaint.cpp | 15 ++++++++++++--- MoveStorage.cpp | 5 ++++- cryptfs.cpp | 20 +++++++++++--------- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Benchmark.cpp b/Benchmark.cpp index 0770da7..e81cd61 100644 --- a/Benchmark.cpp +++ b/Benchmark.cpp @@ -181,7 +181,10 @@ static status_t benchmarkInternal(const std::string& rootPath, void Benchmark(const std::string& path, const android::sp& listener) { std::lock_guard lock(kBenchmarkLock); - android::wakelock::WakeLock wl{kWakeLock}; + auto wl = android::wakelock::WakeLock::tryGet(kWakeLock); + if (!wl.has_value()) { + return; + } PerformanceBoost boost; android::os::PersistableBundle extras; diff --git a/IdleMaint.cpp b/IdleMaint.cpp index 4c3041b..8005cf4 100644 --- a/IdleMaint.cpp +++ b/IdleMaint.cpp @@ -154,7 +154,10 @@ static void addFromFstab(std::list* paths, PathTypes path_type) { } void Trim(const android::sp& listener) { - android::wakelock::WakeLock wl{kWakeLock}; + auto wl = android::wakelock::WakeLock::tryGet(kWakeLock); + if (!wl.has_value()) { + return; + } // Collect both fstab and vold volumes std::list paths; @@ -414,7 +417,10 @@ int RunIdleMaint(const android::sp& listener) { LOG(DEBUG) << "idle maintenance started"; - android::wakelock::WakeLock wl{kWakeLock}; + auto wl = android::wakelock::WakeLock::tryGet(kWakeLock); + if (!wl.has_value()) { + return android::UNEXPECTED_NULL; + } std::list paths; addFromFstab(&paths, PathTypes::kBlkDevice); @@ -448,7 +454,10 @@ int RunIdleMaint(const android::sp& listener) { } int AbortIdleMaint(const android::sp& listener) { - android::wakelock::WakeLock wl{kWakeLock}; + auto wl = android::wakelock::WakeLock::tryGet(kWakeLock); + if (!wl.has_value()) { + return android::UNEXPECTED_NULL; + } std::unique_lock lk(cv_m); if (idle_maint_stat != IdleMaintStats::kStopped) { diff --git a/MoveStorage.cpp b/MoveStorage.cpp index 3f636a2..54e28a9 100644 --- a/MoveStorage.cpp +++ b/MoveStorage.cpp @@ -256,7 +256,10 @@ fail: void MoveStorage(const std::shared_ptr& from, const std::shared_ptr& to, const android::sp& listener) { - android::wakelock::WakeLock wl{kWakeLock}; + auto wl = android::wakelock::WakeLock::tryGet(kWakeLock); + if (!wl.has_value()) { + return; + } android::os::PersistableBundle extras; status_t res = moveStorageInternal(from, to, listener); diff --git a/cryptfs.cpp b/cryptfs.cpp index faed65b..6203003 100644 --- a/cryptfs.cpp +++ b/cryptfs.cpp @@ -2083,7 +2083,16 @@ int cryptfs_enable_internal(int crypt_type, const char* passwd, int no_ui) { int num_vols; bool rebootEncryption = false; bool onlyCreateHeader = false; - std::unique_ptr wakeLock = nullptr; + + /* Get a wakelock as this may take a while, and we don't want the + * device to sleep on us. We'll grab a partial wakelock, and if the UI + * wants to keep the screen on, it can grab a full wakelock. + */ + snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int)getpid()); + auto wl = android::wakelock::WakeLock::tryGet(lockid); + if (!wl.has_value()) { + return android::UNEXPECTED_NULL; + } if (get_crypt_ftr_and_key(&crypt_ftr) == 0) { if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) { @@ -2132,13 +2141,6 @@ int cryptfs_enable_internal(int crypt_type, const char* passwd, int no_ui) { } } - /* Get a wakelock as this may take a while, and we don't want the - * device to sleep on us. We'll grab a partial wakelock, and if the UI - * wants to keep the screen on, it can grab a full wakelock. - */ - snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int)getpid()); - wakeLock = std::make_unique(lockid); - /* The init files are setup to stop the class main and late start when * vold sets trigger_shutdown_framework. */ @@ -2291,7 +2293,7 @@ int cryptfs_enable_internal(int crypt_type, const char* passwd, int no_ui) { /* default encryption - continue first boot sequence */ property_set("ro.crypto.state", "encrypted"); property_set("ro.crypto.type", "block"); - wakeLock.reset(nullptr); + wl.reset(); if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) { // Bring up cryptkeeper that will check the password and set it property_set("vold.decrypt", "trigger_shutdown_framework");