Fix deadlock between vold and init

By setting property after listeners are initialized
we avoid deadlock between vold and init where
vold is waiting on property_service while init is blocked
(and therefore is not able to accept connections) on vdc
which is attempting to communicate with vold.

(This also speeds up boot by 250ms)

Test: Boot a device, check locks and make sure there is no timeout
      on property_set(.)
Test: Successfully boot a device with new property service protocol.
Bug: http://b/34278978
Change-Id: I9547d2f19cb35aa452bf01fbff0eb4b32a4824a4
diff --git a/main.cpp b/main.cpp
index 9cbcf88..68477ac 100644
--- a/main.cpp
+++ b/main.cpp
@@ -39,7 +39,7 @@
 #include <dirent.h>
 #include <fs_mgr.h>
 
-static int process_config(VolumeManager *vm);
+static int process_config(VolumeManager *vm, bool* has_adoptable);
 static void coldboot(const char *path);
 static void parse_args(int argc, char** argv);
 
@@ -106,7 +106,9 @@
         exit(1);
     }
 
-    if (process_config(vm)) {
+    bool has_adoptable;
+
+    if (process_config(vm, &has_adoptable)) {
         PLOG(ERROR) << "Error reading configuration... continuing anyways";
     }
 
@@ -131,6 +133,10 @@
         exit(1);
     }
 
+    // This call should go after listeners are started to avoid
+    // a deadlock between vold and init (see b/34278978 for details)
+    property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
+
     // Eventually we'll become the monitoring thread
     while(1) {
         sleep(1000);
@@ -207,7 +213,7 @@
     }
 }
 
-static int process_config(VolumeManager *vm) {
+static int process_config(VolumeManager *vm, bool* has_adoptable) {
     std::string path(android::vold::DefaultFstabPath());
     fstab = fs_mgr_read_fstab(path.c_str());
     if (!fstab) {
@@ -216,7 +222,7 @@
     }
 
     /* Loop through entries looking for ones that vold manages */
-    bool has_adoptable = false;
+    *has_adoptable = false;
     for (int i = 0; i < fstab->num_entries; i++) {
         if (fs_mgr_is_voldmanaged(&fstab->recs[i])) {
             if (fs_mgr_is_nonremovable(&fstab->recs[i])) {
@@ -230,7 +236,7 @@
 
             if (fs_mgr_is_encryptable(&fstab->recs[i])) {
                 flags |= android::vold::Disk::Flags::kAdoptable;
-                has_adoptable = true;
+                *has_adoptable = true;
             }
             if (fs_mgr_is_noemulatedsd(&fstab->recs[i])
                     || property_get_bool("vold.debug.default_primary", false)) {
@@ -241,6 +247,5 @@
                     new VolumeManager::DiskSource(sysPattern, nickname, flags)));
         }
     }
-    property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
     return 0;
 }