Improve internal memory unmounting 59/168759/5
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 30 Jan 2018 12:26:24 +0000 (13:26 +0100)
committerDariusz Michaluk <d.michaluk@samsung.com>
Wed, 7 Feb 2018 08:42:04 +0000 (08:42 +0000)
On some devices there are multiple mounts under /opt/usr. We have to unmount
all the others to unlock the /opt/usr unmounting.

With this commit ode will iterate over all matching entries and try to unmount
all of them. Some of them are unmounted externally and may disappear before the
call to umount() in ode causing EINVAL error. Ode will ignore it.

Change-Id: I306cc61436e4c151a8396a6d26fefc32a9f93826

server/internal-encryption.cpp
server/misc.cpp
server/misc.h

index c542ddb..4ff4539 100644 (file)
@@ -193,6 +193,29 @@ void setOptions(unsigned int options)
        ::vconf_set_bool(VCONFKEY_ODE_FAST_ENCRYPTION, value);
 }
 
+void unmountInternalStorage(const std::string& source)
+{
+       while(true) {
+               auto mntPaths = findMountPointsByDevice(source);
+               if(mntPaths.empty())
+                       break;
+
+               bool unmounted = true;
+               for(const auto& path : mntPaths) {
+                       INFO(SINK, "Unmounting " + path);
+                       if (::umount(path.c_str()) == -1) {
+                               // If it's busy or was already unmounted ignore the error
+                               if (errno != EBUSY && errno != EINVAL) {
+                                       throw runtime::Exception("umount() error: " + runtime::GetSystemErrorMessage());
+                               }
+                               unmounted = false;
+                       }
+               }
+               if (!unmounted)
+                       stopDependedSystemdServices();
+       }
+}
+
 }
 
 InternalEncryptionServer::InternalEncryptionServer(ServerContext& srv,
@@ -312,24 +335,16 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
        auto encryptWorker = [masterKey, options, this]() {
                try {
                        std::string source = engine->getSource();
-                       std::string mntPath = findMountPointByDevice(source);
+                       auto mntPaths = findMountPointsByDevice(source);
 
-                       if (!mntPath.empty()) {
+                       if (!mntPaths.empty()) {
                                INFO(SINK, "Closing all known systemd services that might be using internal storage.");
                                stopKnownSystemdServices();
                                INFO(SINK, "Closing all processes using internal storage.");
                                stopDependedSystemdServices();
-                       }
 
-                       while (!mntPath.empty()) {
-                               INFO(SINK, "Umounting internal storage.");
-                               while (::umount(mntPath.c_str()) == -1) {
-                                       if (errno != EBUSY) {
-                                               throw runtime::Exception("Umount error: " + runtime::GetSystemErrorMessage());
-                                       }
-                                       stopDependedSystemdServices();
-                               }
-                               mntPath = findMountPointByDevice(source);
+                               INFO(SINK, "Unmounting internal storage.");
+                               unmountInternalStorage(source);
                        }
 
                        showProgressUI("Encrypting");
index 5533432..51a250f 100644 (file)
@@ -40,17 +40,15 @@ struct ::mntent* Mtab::next() {
 }
 
 
-std::string findMountPointByDevice(const std::string &devPath)
+std::vector<std::string> findMountPointsByDevice(const std::string &devPath)
 {
-       std::string ret;
+       std::vector<std::string> ret;
 
        Mtab mtab;
        struct ::mntent* entry = NULL;
        while ((entry = mtab.next()) != NULL) {
-               if (devPath == entry->mnt_fsname) {
-                       ret = entry->mnt_dir;
-                       break;
-               }
+               if (devPath == entry->mnt_fsname)
+                       ret.push_back(entry->mnt_dir);
        }
 
        return ret;
index 13c380e..04ec0ba 100644 (file)
@@ -21,6 +21,8 @@
 #include <stdio.h>
 #include <mntent.h>
 
+#include <vector>
+
 namespace ode {
 
 class Mtab {
@@ -34,7 +36,7 @@ private:
        FILE* mtab;
 };
 
-std::string findMountPointByDevice(const std::string &devPath);
+std::vector<std::string> findMountPointsByDevice(const std::string &devPath);
 void killDependentApplications(const std::string &mntPath);
 
 } // namespace ode