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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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
#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"
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");
}
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}/
%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
case "$CMD" in
load)
load_modules
- enable_modules
;;
unload)
- disable_modules
unload_modules
;;
enable)
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=*