Move 2 common utility functions to misc.cpp/misc.h 10/149610/4
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Tue, 12 Sep 2017 13:17:42 +0000 (15:17 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Fri, 15 Sep 2017 13:52:42 +0000 (15:52 +0200)
Change-Id: If29bea3be21bac1cd870bc44250d268b083908b4

server/CMakeLists.txt
server/extension-encryption.cpp
server/external-encryption.cpp
server/internal-encryption.cpp
server/misc.cpp [new file with mode: 0644]
server/misc.h [new file with mode: 0644]

index 1712c6f..66e7a4a 100644 (file)
@@ -16,6 +16,7 @@
 SET(SERVER_SRCS        main.cpp
                                server.cpp
                                launchpad.cpp
+                               misc.cpp
                                ext4-tool.cpp
                                app-bundle.cpp
                                file-footer.cpp
index 7b79ba6..65a7b94 100644 (file)
  */
 #include <mutex>
 
-#include <signal.h>
 #include <unistd.h>
 #include <sys/mount.h>
-#include <stdio.h>
-#include <mntent.h>
 
 #include <klay/file-user.h>
 #include <klay/filesystem.h>
 #include <klay/dbus/variant.h>
 #include <klay/dbus/connection.h>
 
+#include "misc.h"
 #include "logger.h"
 #include "ext4-tool.h"
 #include "engine/encryption/cryptsetup-engine.h"
@@ -61,37 +59,12 @@ std::mutex apiGuard;
 std::mutex stateGuard;
 std::condition_variable storagedCv;
 
-std::string findMntPath(const std::string &devPath)
-{
-       std::string ret;
-
-       FILE* mtab = setmntent("/etc/mtab", "r");
-       struct mntent* entry = NULL;
-       while ((entry = getmntent(mtab)) != NULL) {
-               if (devPath == entry->mnt_fsname) {
-                       ret = entry->mnt_dir;
-                       break;
-               }
-       }
-       endmntent(mtab);
-
-       return ret;
-}
-
-void killDependedApplications(const std::string &mntPath)
-{
-       for (pid_t pid : runtime::FileUser::getList(mntPath, true)) {
-               INFO(SINK, "Close process - " + std::to_string(pid));
-               ::kill(pid, SIGKILL);
-       }
-}
-
 bool findKillAndUmount(const std::string &devPath)
 {
        std::string realMntPath = findMntPath(devPath);
        if (!realMntPath.empty()) {
                INFO(SINK, "Closing all applications using an SD card...");
-               killDependedApplications(realMntPath);
+               killDependentApplications(realMntPath);
 
                int ret = ::umount(realMntPath.c_str());
                if (ret != 0) {
@@ -221,7 +194,7 @@ int ExtensionEncryption::umount()
 
        if (isMounted()) {
                INFO(SINK, "Close all applications using extension storage...");
-               killDependedApplications(info[Device::MAP].mntPath);
+               killDependentApplications(info[Device::MAP].mntPath);
 
                INFO(SINK, "Ask storaged to umount extension storage...");
                if (!storagedUnmount(stateLock)) {
index f448325..bc6a488 100644 (file)
@@ -16,7 +16,6 @@
 #include <fstream>
 #include <sstream>
 
-#include <signal.h>
 #include <unistd.h>
 #include <sys/mount.h>
 
@@ -27,6 +26,7 @@
 #include <klay/dbus/variant.h>
 #include <klay/dbus/connection.h>
 
+#include "misc.h"
 #include "logger.h"
 #include "launchpad.h"
 #include "app-bundle.h"
@@ -53,14 +53,6 @@ std::unique_ptr<EXTERNAL_ENGINE> engine;
 KeyManager::data mountKey;
 bool isBootCompleted = false;
 
-void killDependedApplications()
-{
-       for (pid_t pid : runtime::FileUser::getList(EXTERNAL_PATH, true)) {
-               INFO(SINK, "Close process - " + std::to_string(pid));
-               ::kill(pid, SIGKILL);
-       }
-}
-
 void externalCallback(dbus::Variant parameters)
 {
        int intparams[6];
@@ -280,7 +272,7 @@ int ExternalEncryption::umount()
        }
 
        INFO(SINK, "Close all applications using external storage...");
-       killDependedApplications();
+       killDependentApplications(EXTERNAL_PATH);
        INFO(SINK, "Umount external storage...");
        engine->umount();
 
@@ -304,7 +296,7 @@ int ExternalEncryption::encrypt(const std::string &password, unsigned int option
        auto encryptWorker = [MasterKey, options, this]() {
                try {
                        INFO(SINK, "Close all applications using external storage...");
-                       killDependedApplications();
+                       killDependentApplications(EXTERNAL_PATH);
                        INFO(SINK, "Encryption started...");
                        engine->encrypt(MasterKey, options);
                        setOptions(options & getSupportedOptions());
@@ -342,14 +334,14 @@ int ExternalEncryption::decrypt(const std::string &password)
        auto decryptWorker = [MasterKey, this]() {
                try {
                        INFO(SINK, "Close all applications using external storage...");
-                       killDependedApplications();
+                       killDependentApplications(EXTERNAL_PATH);
                        INFO(SINK, "Umount external storage...");
                        while (1) {
                                try {
                                        engine->umount();
                                        break;
                                } catch (runtime::Exception &e) {
-                                       killDependedApplications();
+                                       killDependentApplications(EXTERNAL_PATH);
                                }
                        }
 
index 5f0d15c..cc5caea 100644 (file)
@@ -22,8 +22,6 @@
 #include <unistd.h>
 #include <sys/mount.h>
 #include <sys/reboot.h>
-#include <stdio.h>
-#include <mntent.h>
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
@@ -35,6 +33,7 @@
 #include <klay/filesystem.h>
 #include <klay/dbus/connection.h>
 
+#include "misc.h"
 #include "logger.h"
 #include "progress-bar.h"
 #include "engine/encryption/dmcrypt-engine.h"
@@ -102,23 +101,6 @@ std::string findDevPath()
        return devPath;
 }
 
-std::string findMntPath(const std::string &devPath)
-{
-       std::string ret;
-
-       FILE* mtab = ::setmntent("/etc/mtab", "r");
-       struct mntent* entry = NULL;
-       while ((entry = ::getmntent(mtab)) != NULL) {
-               if (devPath == entry->mnt_fsname) {
-                       ret = entry->mnt_dir;
-                       break;
-               }
-       }
-       ::endmntent(mtab);
-
-       return ret;
-}
-
 void stopKnownSystemdServices() {
        std::vector<std::string> knownSystemdServices;
        dbus::Connection& systemDBus = dbus::Connection::getSystem();
diff --git a/server/misc.cpp b/server/misc.cpp
new file mode 100644 (file)
index 0000000..c7ba4b3
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+#include <signal.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <mntent.h>
+
+#include <klay/file-user.h>
+
+#include "logger.h"
+#include "misc.h"
+
+namespace ode {
+
+std::string findMntPath(const std::string &devPath)
+{
+       std::string ret;
+
+       FILE* mtab = ::setmntent("/etc/mtab", "r");
+       struct ::mntent* entry = NULL;
+       while ((entry = ::getmntent(mtab)) != NULL) {
+               if (devPath == entry->mnt_fsname) {
+                       ret = entry->mnt_dir;
+                       break;
+               }
+       }
+       ::endmntent(mtab);
+
+       return ret;
+}
+
+void killDependentApplications(const std::string &mntPath)
+{
+       for (pid_t pid : runtime::FileUser::getList(mntPath, true)) {
+               INFO(SINK, "Close process - " + std::to_string(pid));
+               ::kill(pid, SIGKILL);
+       }
+}
+
+} // namespace ode
diff --git a/server/misc.h b/server/misc.h
new file mode 100644 (file)
index 0000000..3b71f49
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __MISC_H__
+#define __MISC_H__
+
+#include <string>
+
+namespace ode {
+
+std::string findMntPath(const std::string &devPath);
+void killDependentApplications(const std::string &mntPath);
+
+} // namespace ode
+#endif // __MISC_H__