ExtensionEncryption: handle encrypted external sd while formatting 11/149611/4
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Tue, 12 Sep 2017 12:13:55 +0000 (14:13 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Fri, 15 Sep 2017 13:52:42 +0000 (15:52 +0200)
There will be two different things mounted as /opt/media/SDCardA1
while external sd card is encrypted and mounted. Handle this case.

TODO for the findKillAndUmount() usage is still valid, this is just an
immediate workaround for the issue.

Change-Id: If0209165401e9fb88895c417b127aad2fcb75828

server/extension-encryption.cpp

index 65a7b94..aa5e176 100644 (file)
@@ -61,16 +61,23 @@ std::condition_variable storagedCv;
 
 bool findKillAndUmount(const std::string &devPath)
 {
-       std::string realMntPath = findMntPath(devPath);
-       if (!realMntPath.empty()) {
+       for(;;) {
+               std::string mntPath = findMntPath(devPath);
+
+               if (mntPath.empty()) {
+                       break;
+               }
+
                INFO(SINK, "Closing all applications using an SD card...");
-               killDependentApplications(realMntPath);
+               killDependentApplications(mntPath);
 
-               int ret = ::umount(realMntPath.c_str());
-               if (ret != 0) {
-                       ERROR(SINK, "The card is still mounted, umount failed with: "
-                                 + std::to_string(ret));
-                       return false;
+               while (::umount(mntPath.c_str()) == -1) {
+                       if (errno != EBUSY) {
+                               ERROR(SINK, "The card is still mounted, umount failed with: "
+                                         + std::to_string(errno));
+                               return false;
+                       }
+                       killDependentApplications(mntPath);
                }
        }
 
@@ -262,9 +269,10 @@ int ExtensionEncryption::format(const std::string &password)
                        // TODO: this probably needs a rework, some sort of communication
                        // and/or merge with External. The use case for it might be:
                        // 1. The mmc is mounted by something else, somewhere else. Do we care?
-                       // 2. We take over the cd card from External.
-                       if (!findKillAndUmount(EXTENSION_DEV_PATH))
+                       // 2. We take over the (possibly mounted and/or encrypted) cd card from External.
+                       if (!findKillAndUmount(EXTENSION_DEV_PATH)) {
                                return;
+                       }
 
                        INFO(SINK, "Creating LUKS...");
                        engine->format(CryptsetupEngine::DeviceType::LUKS, masterKey);