vold: Unmount secure containers when the underlying media is removed.

Signed-off-by: San Mehat <san@google.com>
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 596590d..4b3342e 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -51,12 +51,15 @@
 VolumeManager::VolumeManager() {
     mBlockDevices = new BlockDeviceCollection();
     mVolumes = new VolumeCollection();
+    mActiveContainers = new AsecIdCollection();
     mBroadcaster = NULL;
     mUsbMassStorageConnected = false;
 }
 
 VolumeManager::~VolumeManager() {
     delete mBlockDevices;
+    delete mVolumes;
+    delete mActiveContainers;
 }
 
 int VolumeManager::start() {
@@ -257,7 +260,8 @@
         unlink(asecFileName);
         return -1;
     }
-    
+
+    mActiveContainers->push_back(strdup(id));
     return 0;
 }
 
@@ -324,6 +328,8 @@
         return -1;
     }
 
+    unlink(mountPoint);
+
     if (Devmapper::destroy(id) && errno != ENXIO) {
         LOGE("Failed to destroy devmapper instance (%s)", strerror(errno));
     }
@@ -332,6 +338,18 @@
     if (!Loop::lookupActive(asecFileName, loopDevice, sizeof(loopDevice))) {
         Loop::destroyByDevice(loopDevice);
     }
+
+    AsecIdCollection::iterator it;
+    for (it = mActiveContainers->begin(); it != mActiveContainers->end(); ++it) {
+        if (!strcmp(*it, id)) {
+            free(*it);
+            mActiveContainers->erase(it);
+            break;
+        }
+    }
+    if (it == mActiveContainers->end()) {
+        LOGW("mActiveContainers is inconsistent!");
+    }
     return 0;
 }
 
@@ -441,6 +459,7 @@
         return -1;
     }
 
+    mActiveContainers->push_back(strdup(id));
     LOGD("ASEC %s mounted", id);
     return 0;
 }
@@ -610,6 +629,15 @@
         return -1;
     }
 
+    while(mActiveContainers->size()) {
+        AsecIdCollection::iterator it = mActiveContainers->begin();
+        LOGI("Unmounting ASEC %s (dependant on %s)", *it, v->getMountpoint());
+        if (unmountAsec(*it)) {
+            LOGE("Failed to unmount ASEC %s (%s) - unmount of %s may fail!", *it,
+                 strerror(errno), v->getMountpoint());
+        }
+    }
+
     return v->unmountVol();
 }