Add exception handling in threads for encryption/decryption 08/112708/3
authorSungbae Yoo <sungbae.yoo@samsung.com>
Thu, 2 Feb 2017 09:08:24 +0000 (18:08 +0900)
committerSungbae Yoo <sungbae.yoo@samsung.com>
Fri, 3 Feb 2017 09:44:49 +0000 (18:44 +0900)
Signed-off-by: Sungbae Yoo <sungbae.yoo@samsung.com>
Change-Id: Ieb65804da717e4812f9854d73718a9daebb2b10c

server/external-encryption.cpp
server/internal-encryption.cpp

index d476a3c..42d3b04 100644 (file)
@@ -204,7 +204,7 @@ int ExternalEncryption::umount()
 
        INFO("Close all applications using external storage...");
        killDependedApplications();
-       INFO("Umount internal storage...");
+       INFO("Umount external storage...");
        engine.umount();
 
        return 0;
@@ -225,16 +225,20 @@ int ExternalEncryption::encrypt(const std::string &password, unsigned int option
 
        KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
        auto encryptWorker = [&MasterKey, options, this]() {
-               INFO("Close all applications using external storage...");
-               killDependedApplications();
-               INFO("Encryption started...");
-               ::vconf_set_str(EXTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
-               engine.encrypt(MasterKey, options);
-               setOptions(options & getSupportedOptions());
-               INFO("Sync disk...");
-               sync();
-               INFO("Encryption completed");
-               ::vconf_set_str(EXTERNAL_STATE_VCONF_KEY, "encrypted");
+               try {
+                       INFO("Close all applications using external storage...");
+                       killDependedApplications();
+                       INFO("Encryption started...");
+                       ::vconf_set_str(EXTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
+                       engine.encrypt(MasterKey, options);
+                       setOptions(options & getSupportedOptions());
+                       INFO("Sync disk...");
+                       sync();
+                       INFO("Encryption completed");
+                       ::vconf_set_str(EXTERNAL_STATE_VCONF_KEY, "encrypted");
+               } catch (runtime::Exception &e) {
+                       ERROR("Encryption failed - " + std::string(e.what()));
+               }
        };
 
        std::thread asyncWork(encryptWorker);
@@ -258,20 +262,29 @@ int ExternalEncryption::decrypt(const std::string &password)
 
        KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
        auto decryptWorker = [MasterKey, this]() {
-               INFO("Close all applications using external storage...");
-               killDependedApplications();
-               INFO("Umount internal storage...");
                try {
-                       engine.umount();
-               } catch (runtime::Exception &e) {}
-
-               INFO("Decryption started...");
-               ::vconf_set_str(EXTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
-               engine.decrypt(MasterKey, getOptions());
-               INFO("Sync disk...");
-               sync();
-               INFO("Decryption completed");
-               ::vconf_set_str(EXTERNAL_STATE_VCONF_KEY, "unencrypted");
+                       INFO("Close all applications using external storage...");
+                       killDependedApplications();
+                       INFO("Umount external storage...");
+                       while (1) {
+                               try {
+                                       engine.umount();
+                                       break;
+                               } catch (runtime::Exception &e) {
+                                       killDependedApplications();
+                               }
+                       }
+
+                       INFO("Decryption started...");
+                       ::vconf_set_str(EXTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
+                       engine.decrypt(MasterKey, getOptions());
+                       INFO("Sync disk...");
+                       sync();
+                       INFO("Decryption completed");
+                       ::vconf_set_str(EXTERNAL_STATE_VCONF_KEY, "unencrypted");
+               } catch (runtime::Exception &e) {
+                       ERROR("Decryption failed - " + std::string(e.what()));
+               }
        };
 
        std::thread asyncWork(decryptWorker);
@@ -339,8 +352,6 @@ int ExternalEncryption::verifyPassword(const std::string& password)
        return 0;
 }
 
-
-
 int ExternalEncryption::getState()
 {
        char *value = ::vconf_get_str(EXTERNAL_STATE_VCONF_KEY);
index a8f3ad6..01b3ea5 100644 (file)
@@ -179,26 +179,31 @@ int InternalEncryption::encrypt(const std::string& password, unsigned int option
 
        KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
        auto encryptWorker = [&MasterKey, options, this]() {
-               showProgressUI("Encrypting");
-
-               INFO("Close all processes using internal storage...");
-               stopDependedSystemdServices();
-               INFO("Umount internal storage...");
-               while (::umount(INTERNAL_STORAGE_PATH) == -1) {
-                       if (errno != EBUSY) {
-                               break;
+               try {
+                       showProgressUI("Encrypting");
+
+                       INFO("Close all processes using internal storage...");
+                       stopDependedSystemdServices();
+                       INFO("Umount internal storage...");
+                       while (::umount(INTERNAL_STORAGE_PATH) == -1) {
+                               if (errno != EBUSY) {
+                                       throw runtime::Exception("Umount error - " + std::to_string(errno));
+                               }
+                               stopDependedSystemdServices();
                        }
-               }
 
-               INFO("Encryption started...");
-               ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
-               engine.encrypt(MasterKey, options);
-               setOptions(options & getSupportedOptions());
-               INFO("Sync disk...");
-               sync();
-               INFO("Encryption completed");
+                       INFO("Encryption started...");
+                       ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
+                       engine.encrypt(MasterKey, options);
+                       setOptions(options & getSupportedOptions());
+                       INFO("Sync disk...");
+                       sync();
+                       INFO("Encryption completed");
 
-               ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "encrypted");
+                       ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "encrypted");
+               } catch (runtime::Exception &e) {
+                       ERROR("Encryption failed - " + std::string(e.what()));
+               }
                ::reboot(RB_AUTOBOOT);
        };
 
@@ -223,23 +228,32 @@ int InternalEncryption::decrypt(const std::string& password)
 
        KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
        auto decryptWorker = [MasterKey, this]() {
-               showProgressUI("Decrypting");
-
-               INFO("Close all processes using internal storage...");
-               stopDependedSystemdServices();
-               INFO("Umount internal storage...");
                try {
-                       engine.umount();
-               } catch (runtime::Exception& e) {}
+                       showProgressUI("Decrypting");
+
+                       INFO("Close all processes using internal storage...");
+                       stopDependedSystemdServices();
+                       INFO("Umount internal storage...");
+                       while (1) {
+                               try {
+                                       engine.umount();
+                                       break;
+                               } catch (runtime::Exception& e) {
+                                       stopDependedSystemdServices();
+                               }
+                       }
 
-               INFO("Decryption started...");
-               ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
-               engine.decrypt(MasterKey, getOptions());
-               INFO("Sync disk...");
-               sync();
-               INFO("Decryption completed");
+                       INFO("Decryption started...");
+                       ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
+                       engine.decrypt(MasterKey, getOptions());
+                       INFO("Sync disk...");
+                       sync();
+                       INFO("Decryption completed");
 
-               ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "unencrypted");
+                       ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "unencrypted");
+               } catch (runtime::Exception &e) {
+                       ERROR("Decryption failed - " + std::string(e.what()));
+               }
                ::reboot(RB_AUTOBOOT);
        };
 
@@ -308,8 +322,6 @@ int InternalEncryption::verifyPassword(const std::string& password)
        return 0;
 }
 
-
-
 int InternalEncryption::getState()
 {
        char *value = ::vconf_get_str(INTERNAL_STATE_VCONF_KEY);