vold2: Manually bootstrap the ums switch since switch kernel uevents are broken

Signed-off-by: San Mehat <san@google.com>
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 4a24ff7..462029c 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -79,9 +79,15 @@
 }
 
 void VolumeManager::handleSwitchEvent(NetlinkEvent *evt) {
+    const char *devpath = evt->findParam("DEVPATH");
     const char *name = evt->findParam("SWITCH_NAME");
     const char *state = evt->findParam("SWITCH_STATE");
 
+    if (!name || !state) {
+        LOGW("Switch %s event missing name/state info", devpath);
+        return;
+    }
+
     if (!strcmp(name, "usb_mass_storage")) {
 
         if (!strcmp(state, "online"))  {
@@ -228,7 +234,8 @@
              sizeof(nodepath), "/dev/block/vold/%d:%d",
              MAJOR(d), MINOR(d));
 
-    if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0", O_WRONLY)) < 0) {
+    if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file",
+                   O_WRONLY)) < 0) {
         LOGE("Unable to open ums lunfile (%s)", strerror(errno));
         return -1;
     }
@@ -270,7 +277,7 @@
              sizeof(nodepath), "/dev/block/vold/%d:%d",
              MAJOR(d), MINOR(d));
 
-    if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0", O_WRONLY)) < 0) {
+    if ((fd = open("/sys/devices/platform/usb_mass_storage/lun0/file", O_WRONLY)) < 0) {
         LOGE("Unable to open ums lunfile (%s)", strerror(errno));
         return -1;
     }
diff --git a/VolumeManager.h b/VolumeManager.h
index 5f5a249..f686a50 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -56,6 +56,9 @@
     int simulate(const char *cmd, const char *arg);
     int formatVolume(const char *label);
 
+    // XXX: This should be moved private once switch uevents are working
+    void notifyUmsConnected(bool connected);
+
     void setBroadcaster(SocketListener *sl) { mBroadcaster = sl; }
     SocketListener *getBroadcaster() { return mBroadcaster; }
 
@@ -64,6 +67,5 @@
 private:
     VolumeManager();
     Volume *lookupVolume(const char *label);
-    void notifyUmsConnected(bool connected);
 };
 #endif
diff --git a/main.cpp b/main.cpp
index 3f25ab8..1ffbc7b 100644
--- a/main.cpp
+++ b/main.cpp
@@ -78,6 +78,35 @@
     }
 
     coldboot("/sys/block");
+    /*
+     * Switch uevents are broken.
+     * For now we manually bootstrap
+     * the ums switch
+     */
+    {
+        FILE *fp;
+        char state[255];
+
+        if (!(fp = fopen("/sys/devices/virtual/switch/usb_mass_storage/state",
+                         "r"))) {
+            LOGE("Failed to open ums switch (%s)", strerror(errno));
+            exit(1);
+        }
+        if (!fgets(state, sizeof(state), fp)) {
+            LOGE("Failed to read switch state (%s)", strerror(errno));
+            fclose(fp);
+            exit(1);
+        }
+        if (!strncmp(state, "online", 6)) {
+            LOGD("Bootstrapped ums is connected");
+            vm->notifyUmsConnected(true);
+        } else {
+            LOGD("Bootstrapped ums is disconnected");
+            vm->notifyUmsConnected(false);
+        }
+        fclose(fp);
+    }
+//    coldboot("/sys/class/switch");
 
     /*
      * Now that we're up, we can respond to commands