vold: use RAII wake locks

Prevents wake lock leaks, e.g. b/133175847

Bug: 133175847
Test: boot blueline
Change-Id: I62fd1c6c3abbfd35aebe11343abd717a7cf4eef7
diff --git a/Benchmark.cpp b/Benchmark.cpp
index b0a3b85..9548956 100644
--- a/Benchmark.cpp
+++ b/Benchmark.cpp
@@ -181,7 +181,7 @@
 void Benchmark(const std::string& path,
                const android::sp<android::os::IVoldTaskListener>& listener) {
     std::lock_guard<std::mutex> lock(kBenchmarkLock);
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::power::WakeLock wl{kWakeLock};
 
     PerformanceBoost boost;
     android::os::PersistableBundle extras;
@@ -190,8 +190,6 @@
     if (listener) {
         listener->onFinished(res, extras);
     }
-
-    release_wake_lock(kWakeLock);
 }
 
 }  // namespace vold
diff --git a/IdleMaint.cpp b/IdleMaint.cpp
index a2d0f91..164986c 100644
--- a/IdleMaint.cpp
+++ b/IdleMaint.cpp
@@ -145,7 +145,7 @@
 }
 
 void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::power::WakeLock wl{kWakeLock};
 
     // Collect both fstab and vold volumes
     std::list<std::string> paths;
@@ -195,7 +195,6 @@
         listener->onFinished(0, extras);
     }
 
-    release_wake_lock(kWakeLock);
 }
 
 static bool waitForGc(const std::list<std::string>& paths) {
@@ -370,7 +369,7 @@
 
     LOG(DEBUG) << "idle maintenance started";
 
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::power::WakeLock wl{kWakeLock};
 
     std::list<std::string> paths;
     addFromFstab(&paths, PathTypes::kBlkDevice);
@@ -400,13 +399,11 @@
 
     LOG(DEBUG) << "idle maintenance completed";
 
-    release_wake_lock(kWakeLock);
-
     return android::OK;
 }
 
 int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::power::WakeLock wl{kWakeLock};
 
     std::unique_lock<std::mutex> lk(cv_m);
     if (idle_maint_stat != IdleMaintStats::kStopped) {
@@ -424,8 +421,6 @@
         listener->onFinished(0, extras);
     }
 
-    release_wake_lock(kWakeLock);
-
     LOG(DEBUG) << "idle maintenance stopped";
 
     return android::OK;
diff --git a/MoveStorage.cpp b/MoveStorage.cpp
index 79a47ae..9b806ec 100644
--- a/MoveStorage.cpp
+++ b/MoveStorage.cpp
@@ -258,15 +258,13 @@
 
 void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
                  const android::sp<android::os::IVoldTaskListener>& listener) {
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::power::WakeLock wl{kWakeLock};
 
     android::os::PersistableBundle extras;
     status_t res = moveStorageInternal(from, to, listener);
     if (listener) {
         listener->onFinished(res, extras);
     }
-
-    release_wake_lock(kWakeLock);
 }
 
 }  // namespace vold
diff --git a/cryptfs.cpp b/cryptfs.cpp
index c5d0307..7d59b2d 100644
--- a/cryptfs.cpp
+++ b/cryptfs.cpp
@@ -2007,6 +2007,7 @@
     off64_t previously_encrypted_upto = 0;
     bool rebootEncryption = false;
     bool onlyCreateHeader = false;
+    std::unique_ptr<android::power::WakeLock> wakeLock = nullptr;
 
     if (get_crypt_ftr_and_key(&crypt_ftr) == 0) {
         if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
@@ -2073,7 +2074,7 @@
      * wants to keep the screen on, it can grab a full wakelock.
      */
     snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int)getpid());
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
+    wakeLock = std::make_unique<android::power::WakeLock>(lockid);
 
     /* The init files are setup to stop the class main and late start when
      * vold sets trigger_shutdown_framework.
@@ -2254,7 +2255,6 @@
                 /* default encryption - continue first boot sequence */
                 property_set("ro.crypto.state", "encrypted");
                 property_set("ro.crypto.type", "block");
-                release_wake_lock(lockid);
                 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");
@@ -2291,7 +2291,6 @@
         } else {
             /* set property to trigger dialog */
             property_set("vold.encrypt_progress", "error_partially_encrypted");
-            release_wake_lock(lockid);
         }
         return -1;
     }
@@ -2301,14 +2300,10 @@
      * Set the property and return.  Hope the framework can deal with it.
      */
     property_set("vold.encrypt_progress", "error_reboot_failed");
-    release_wake_lock(lockid);
     return rc;
 
 error_unencrypted:
     property_set("vold.encrypt_progress", "error_not_encrypted");
-    if (lockid[0]) {
-        release_wake_lock(lockid);
-    }
     return -1;
 
 error_shutting_down:
@@ -2323,9 +2318,6 @@
 
     /* shouldn't get here */
     property_set("vold.encrypt_progress", "error_shutting_down");
-    if (lockid[0]) {
-        release_wake_lock(lockid);
-    }
     return -1;
 }