Snap for 7901677 from f6a1d8ef0be044060d8ae7e18e8f8c77308ec356 to sc-platform-release

Change-Id: Ifa4bd0aae0d28e9c251a2075adb242fb5df943db
diff --git a/suspend/1.0/default/SystemSuspend.cpp b/suspend/1.0/default/SystemSuspend.cpp
index 81f3d45..e9bb646 100644
--- a/suspend/1.0/default/SystemSuspend.cpp
+++ b/suspend/1.0/default/SystemSuspend.cpp
@@ -18,6 +18,7 @@
 
 #include <android-base/file.h>
 #include <android-base/logging.h>
+#include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <fcntl.h>
 #include <hidl/Status.h>
@@ -70,8 +71,8 @@
     std::string reasonlines;
 
     lseek(fd, 0, SEEK_SET);
-    if (!ReadFdToString(fd, &reasonlines)) {
-        LOG(ERROR) << "failed to read wakeup reasons";
+    if (!ReadFdToString(fd, &reasonlines) || reasonlines.empty()) {
+        PLOG(ERROR) << "failed to read wakeup reasons";
         // Return unknown wakeup reason if we fail to read
         return {kUnknownWakeup};
     }
@@ -236,6 +237,17 @@
     }
 }
 
+unique_fd SystemSuspend::reopenFileUsingFd(const int fd, const int permission) {
+    string filePath = android::base::StringPrintf("/proc/self/fd/%d", fd);
+
+    unique_fd tempFd{TEMP_FAILURE_RETRY(open(filePath.c_str(), permission))};
+    if (tempFd < 0) {
+        PLOG(ERROR) << "SystemSuspend: Error opening file, using path: " << filePath;
+        return unique_fd(-1);
+    }
+    return tempFd;
+}
+
 void SystemSuspend::initAutosuspend() {
     std::thread autosuspendThread([this] {
         while (true) {
@@ -268,6 +280,12 @@
             updateSleepTime(success, suspendTime);
 
             std::vector<std::string> wakeupReasons = readWakeupReasons(mWakeupReasonsFd);
+            if (wakeupReasons == std::vector<std::string>({kUnknownWakeup})) {
+                LOG(INFO) << "Unknown/empty wakeup reason. Re-opening wakeup_reason file.";
+
+                mWakeupReasonsFd =
+                    std::move(reopenFileUsingFd(mWakeupReasonsFd.get(), O_CLOEXEC | O_RDONLY));
+            }
             mWakeupList.update(wakeupReasons);
 
             mControlService->notifyWakeup(success, wakeupReasons);
diff --git a/suspend/1.0/default/SystemSuspend.h b/suspend/1.0/default/SystemSuspend.h
index 5632325..b84b1c9 100644
--- a/suspend/1.0/default/SystemSuspend.h
+++ b/suspend/1.0/default/SystemSuspend.h
@@ -114,6 +114,7 @@
     Result<SuspendStats> getSuspendStats();
     void getSuspendInfo(SuspendInfo* info);
     std::chrono::milliseconds getSleepTime() const;
+    unique_fd reopenFileUsingFd(const int fd, int permission);
 
    private:
     void initAutosuspend();