Remove swap_module.service 30/148730/3
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Fri, 8 Sep 2017 12:33:56 +0000 (15:33 +0300)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Mon, 11 Sep 2017 15:19:32 +0000 (18:19 +0300)
For swap_modules enabling/disabling is used API:
bool kernel_module_init(void);
bool kernel_module_uninit(void);
bool kernel_module_is_init(void);

load/unload modules should be implemented

Change-Id: Ibcd3715562bbb080b437c1f2b76b5cdce878c9cd
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
daemon/Makefile
daemon/cpp/kernel/module.cpp [new file with mode: 0644]
daemon/cpp/kernel/module.h [new file with mode: 0644]
daemon/cpp/kernel/module_c.cpp [new file with mode: 0644]
daemon/cpp/kernel/module_c.h [new file with mode: 0644]
daemon/main.c
packaging/swap-manager.spec
scripts/swap_module.sh
systemd/swap.conf

index 23d412a..4bb4a86 100644 (file)
@@ -124,7 +124,10 @@ SRC_CPP := \
        cpp/auxd/auxd_client_c.cpp \
 \
        cpp/memd/memd_client.cpp \
-       cpp/memd/memd_client_c.cpp
+       cpp/memd/memd_client_c.cpp \
+\
+       cpp/kernel/module.cpp \
+       cpp/kernel/module_c.cpp
 
 
 TARGET = swap_manager
diff --git a/daemon/cpp/kernel/module.cpp b/daemon/cpp/kernel/module.cpp
new file mode 100644 (file)
index 0000000..bc34ac6
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *      Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ * 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.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#include "module.h"
+#include <stdexcept>
+#include <fstream>
+#include <swap_debug.h>
+
+
+// TODO: move read_from_file() and write_to_file() to library
+static void write_to_file(const std::string &path, const std::string &in_data)
+{
+    try {
+        std::ofstream file;
+
+        file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
+        file.open(path, std::ofstream::out | std::ofstream::trunc);
+        file << in_data;
+        file.close();
+    } catch (const std::ofstream::failure &e) {
+        throw std::runtime_error(std::string("Exception opening/writing/closing (") +
+                                 e.what() + ") file=" + path);
+    }
+}
+
+static void read_from_file(const std::string &path, std::string &out_data)
+{
+    try {
+        std::ifstream file;
+
+        file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
+        file.open(path);
+        file >> out_data;
+        file.close();
+    } catch (const std::ifstream::failure &e) {
+        throw std::runtime_error(std::string("Exception opening/reading/closing (") +
+                                 e.what() + ") file=" + path);
+    }
+}
+
+
+namespace Kernel {
+
+static const char enable_path[] = "/sys/kernel/debug/swap/enable";
+
+bool Module::init()
+{
+    if (is_init()) {
+        LOGE("modules are already initialized\n");
+        return false;
+    }
+
+    write_to_file(enable_path, "1");
+    return true;
+}
+
+bool Module::uninit()
+{
+    if (!is_init()) {
+        LOGE("modules are already disabled");
+        return false;
+    }
+
+    write_to_file(enable_path, "0");
+    return true;
+}
+
+bool Module::is_init()
+{
+    std::string val;
+
+    read_from_file(enable_path, val);
+    if (val.at(0) == '0')
+        return false;
+
+    return true;
+}
+
+} // namespace Kernel
diff --git a/daemon/cpp/kernel/module.h b/daemon/cpp/kernel/module.h
new file mode 100644 (file)
index 0000000..16b638f
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *      Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ * 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.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#ifndef KERNEL_MODULE_H
+#define KERNEL_MODULE_H
+
+
+namespace Kernel {
+
+class Module
+{
+public:
+    static bool init();
+    static bool uninit();
+    static bool is_init();
+};
+
+} // namespace Kernel
+
+
+#endif // KERNEL_MODULE_H
diff --git a/daemon/cpp/kernel/module_c.cpp b/daemon/cpp/kernel/module_c.cpp
new file mode 100644 (file)
index 0000000..e69579b
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *      Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ * 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.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#include "module_c.h"
+#include "module.h"
+#include <stdexcept>
+#include <swap_debug.h>
+
+
+bool kernel_module_init(void)
+{
+    std::string err;
+
+    try {
+        return Kernel::Module::init();
+    } catch (const std::runtime_error &e) {
+        err = e.what();
+    } catch (...) {
+        err = "Unknown";
+    }
+
+    LOGE("%s\n", err.c_str());
+    return false;
+}
+
+bool kernel_module_uninit(void)
+{
+    std::string err;
+
+    try {
+        return Kernel::Module::uninit();
+    } catch (const std::runtime_error &e) {
+        err = e.what();
+    } catch (...) {
+        err = "Unknown";
+    }
+
+    LOGE("%s\n", err.c_str());
+    return false;
+}
+
+bool kernel_module_is_init(void)
+{
+    std::string err;
+
+    try {
+        return Kernel::Module::is_init();
+    } catch (const std::runtime_error &e) {
+        err = e.what();
+    } catch (...) {
+        err = "Unknown";
+    }
+
+    LOGE("%s\n", err.c_str());
+    return false;
+}
diff --git a/daemon/cpp/kernel/module_c.h b/daemon/cpp/kernel/module_c.h
new file mode 100644 (file)
index 0000000..56dac80
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *      Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ * 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.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#ifndef KERNEL_MODULE_C_H
+#define KERNEL_MODULE_C_H
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+bool kernel_module_init(void);
+bool kernel_module_uninit(void);
+bool kernel_module_is_init(void);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif // KERNEL_MODULE_C_H
index 242296f..cae9aea 100644 (file)
@@ -54,6 +54,7 @@
 #include "cpp/auxd/auxd_client_c.h"
 #include "cpp/memd/memd_client_c.h"
 #include "cpp/features/feature_manager_c.h"
+#include "cpp/kernel/module_c.h"
 
 #define SINGLETON_LOCKFILE                     "/run/swap/manager.lock"
 #define PORTFILE                               "/run/swap/manager/port"
@@ -396,18 +397,21 @@ static bool initialize_pthread_sigmask()
 
 static int ks_init(void)
 {
-       int ret = es_modules_load();
-       if (ret) {
+       /* TODO:
+        *    add load/unload modules on demand,
+        *    use es_modules_load()/es_modules_unload()
+        */
+       if (!kernel_module_init()) {
                LOGE("Cannot insert swap modules\n");
-               return ret;
+               return -1;
        }
 
-       return ret;
+       return 0;
 }
 
 static void ks_uninit(void)
 {
-       if (es_modules_unload())
+       if (!kernel_module_uninit())
                LOGE("Cannot remove swap modules\n");
 }
 
index f214140..8475910 100644 (file)
@@ -159,11 +159,6 @@ mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants
 mkdir -p %{buildroot}%{_unitdir_user}
 mkdir -p %{buildroot}%{_unitdir_user}/sockets.target.wants
 
-#   swap_module
-install -m 0644 systemd/swap_module.service %{buildroot}%{_unitdir}/
-install -m 0644 systemd/swap_module.path %{buildroot}%{_unitdir}/
-ln -s ../swap_module.path %{buildroot}%{_unitdir}/multi-user.target.wants/
-
 #   swap_manager
 install -m 0644 systemd/swap_manager.service %{buildroot}%{_unitdir}/
 install -m 0644 systemd/swap_manager.socket %{buildroot}%{_unitdir}/
@@ -223,11 +218,6 @@ su - owner -c "systemctl --user daemon-reload"
 %defattr(-,root,root,-)
 
 %if %{SYSTEMD_SUPPORT}
-# swap_module
-%{_unitdir}/swap_module.service
-%{_unitdir}/swap_module.path
-%{_unitdir}/multi-user.target.wants/swap_module.path
-
 # swap_manager
 %{_unitdir}/swap_manager.socket
 %{_unitdir}/swap_manager.service
index fab636d..9c280ab 100755 (executable)
@@ -86,10 +86,8 @@ disable_modules() {
 case "$CMD" in
        load)
                load_modules
-               enable_modules
                ;;
        unload)
-               disable_modules
                unload_modules
                ;;
        enable)
index 68e2a40..bcb4a59 100644 (file)
@@ -22,10 +22,6 @@ t /run/swap/manager/port - - - - security.SMACK64=System::Shared
 f /run/swap/manager/pid 0644 system_fw system_fw -
 t /run/swap/manager/pid - - - - security.SMACK64=System::Shared
 
-# setup /run/swap/trigger
-d /run/swap/trigger 0777 system_fw system_fw -
-t /run/swap/trigger - - - - security.SMACK64=System::Privileged
-
 # setup /run/swap/trigger_shell
 d /run/swap/trigger_shell 0777 system_fw system_fw -
 t /run/swap/trigger_shell - - - - security.SMACK64=*