Wait for hwservicemanager in defaultServiceManager.

Test: internal marlin boots. Nfc/camera/photosphere/sensors/youtube
(audio+video)/gps/wifi/fingerprint all work.
Test: internal angler boots. Binderized class hal hals work.
Test: internal bullhead boots. Binderized class hal hals work.
Test: internal ryu boots. Binderized class hal hals work.
Test: no relevant denials on any of the devices
Test: boottime (perfboot.py) avg 15.8s to 14.2s on marlin
Test: adb shell stop+start works on all 4 devices
Test: all devices boot and work after setting pin which protects boot.
Bug: 36278706
Change-Id: Ib2c7440de43d218dac3e45253ae801aa3c3317bd
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index cc31d9e..34a3c47 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -29,6 +29,7 @@
 #include <hidl/Status.h>
 
 #include <android-base/logging.h>
+#include <android-base/properties.h>
 #include <hidl-util/FQName.h>
 #include <hidl-util/StringHelper.h>
 #include <hwbinder/IPCThreadState.h>
@@ -42,6 +43,8 @@
 #define RE_PATH         RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
 static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
 
+using android::base::WaitForProperty;
+
 using android::hidl::manager::V1_0::IServiceManager;
 using android::hidl::manager::V1_0::IServiceNotification;
 using android::hidl::manager::V1_0::BpHwServiceManager;
@@ -55,6 +58,12 @@
 extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager;
 }  // namespace details
 
+static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
+
+void waitForHwServiceManager() {
+    while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", std::chrono::milliseconds::max())) {}
+}
+
 sp<IServiceManager> defaultServiceManager() {
 
     {
@@ -67,11 +76,15 @@
             // this process.
             return nullptr;
         }
+
+        waitForHwServiceManager();
+
         while (details::gDefaultServiceManager == NULL) {
             details::gDefaultServiceManager =
                     fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
                         ProcessState::self()->getContextObject(NULL));
             if (details::gDefaultServiceManager == NULL) {
+                LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
                 sleep(1);
             }
         }