From 0c3dace9197251b325a77a22dc0d9726b168dc0d Mon Sep 17 00:00:00 2001 From: hyunho Date: Mon, 3 Feb 2020 11:29:49 +0900 Subject: [PATCH] Add a plugin for app-defined loader Change-Id: I73f527421a98bc619266171553280acd9029c61d Signed-off-by: hyunho --- CMakeLists.txt | 3 + packaging/launchpad.spec | 4 + parser/CMakeLists.txt | 39 ++++++ parser/common.hh | 25 ++++ parser/launchpad_parser_plugin.cc | 146 +++++++++++++++++++++ parser/launchpad_parser_plugin.hh | 45 +++++++ parser/launchpad_parser_plugin_pkgmgr_interface.cc | 68 ++++++++++ parser/launchpad_plugins.txt | 1 + parser/loader_info.cc | 44 +++++++ parser/loader_info.hh | 43 ++++++ parser/log_private.hh | 47 +++++++ 11 files changed, 465 insertions(+) create mode 100644 parser/CMakeLists.txt create mode 100644 parser/common.hh create mode 100644 parser/launchpad_parser_plugin.cc create mode 100644 parser/launchpad_parser_plugin.hh create mode 100644 parser/launchpad_parser_plugin_pkgmgr_interface.cc create mode 100644 parser/launchpad_plugins.txt create mode 100644 parser/loader_info.cc create mode 100644 parser/loader_info.hh create mode 100644 parser/log_private.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 5239b49..20d8f0f 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -203,3 +203,6 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/inc/launchpad_hydra.h DESTINATION incl CONFIGURE_FILE(liblaunchpad-hydra.pc.in liblaunchpad-hydra.pc @ONLY) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/liblaunchpad-hydra.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) + +# parser +ADD_SUBDIRECTORY(parser) \ No newline at end of file diff --git a/packaging/launchpad.spec b/packaging/launchpad.spec index 76464ea..babb8b4 100644 --- a/packaging/launchpad.spec +++ b/packaging/launchpad.spec @@ -26,6 +26,7 @@ BuildRequires: pkgconfig(libcap) BuildRequires: pkgconfig(tanchor) BuildRequires: pkgconfig(dbus-1) BuildRequires: pkgconfig(iniparser) +BuildRequires: pkgconfig(libxml-2.0) Requires(post): /sbin/ldconfig Requires(post): /usr/bin/systemctl @@ -119,6 +120,7 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` %install rm -rf %{buildroot} + %make_install mkdir -p %{buildroot}%{_unitdir_user}/basic.target.wants mkdir -p %{buildroot}%{_unitdir_user}/sockets.target.wants @@ -139,6 +141,8 @@ ln -sf ../launchpad-process-pool.service %{buildroot}%{_unitdir_user}/basic.targ %{_unitdir_user}/basic.target.wants/launchpad-process-pool.service %{_bindir}/launchpad-process-pool %{_prefix}/share/aul/launchpad.conf +%{_sysconfdir}/package-manager/parserlib/liblaunchpad-parser.so +%{_datadir}/parser-plugins/* %files devel %{_includedir}/launchpad/*.h diff --git a/parser/CMakeLists.txt b/parser/CMakeLists.txt new file mode 100644 index 0000000..f98579f --- /dev/null +++ b/parser/CMakeLists.txt @@ -0,0 +1,39 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(launchpad-parser CXX) + +INCLUDE(FindPkgConfig) +pkg_check_modules(PKGS REQUIRED + dlog + libxml-2.0 +) + +FOREACH(FLAGS ${PKGS_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${FLAGS}") +ENDFOREACH(FLAGS) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror -Winline -std=c++11") + +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS}") +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") +SET(CMAKE_CXX_FLAGS_RELEASE "-O2") + +ADD_LIBRARY(${PROJECT_NAME} SHARED + launchpad_parser_plugin.cc + loader_info.cc + launchpad_parser_plugin_pkgmgr_interface.cc +) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../) + +SET(PLUGINS_LIST_FILE_NAME launchpad_plugins.txt) +SET(PLUGINS_LIST_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${PLUGINS_LIST_FILE_NAME}) + +SET(PLUGINS_LIST_INSTALL_PATH ${CMAKE_INSTALL_PREFIX}/share/parser-plugins) + +ADD_DEFINITIONS("-DPLUGINS_LIST_INSTALL_PATH=\"${PLUGINS_LIST_INSTALL_PATH}\"") + +INSTALL(FILES ${PLUGINS_LIST_FILE_PATH} DESTINATION ${PLUGINS_LIST_INSTALL_PATH}/) + +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PKGS_LDFLAGS}) + +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${SYSCONF_INSTALL_DIR}/package-manager/parserlib) diff --git a/parser/common.hh b/parser/common.hh new file mode 100644 index 0000000..910caa5 --- /dev/null +++ b/parser/common.hh @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 LAUNCHPAD_PARSER_PLUGIN_COMMON_HH_ +#define LAUNCHPAD_PARSER_PLUGIN_COMMON_HH_ + +#ifdef EXPORT_API +#undef EXPORT_API +#endif +#define EXPORT_API __attribute__((visibility("default"))) + +#endif // LAUNCHPAD_PARSER_PLUGIN_COMMON_HH_ diff --git a/parser/launchpad_parser_plugin.cc b/parser/launchpad_parser_plugin.cc new file mode 100644 index 0000000..a880a6e --- /dev/null +++ b/parser/launchpad_parser_plugin.cc @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 +#include + +#include +#include + +#include "launchpad_parser_plugin.hh" +#include "log_private.hh" + +#define LOADERS_DIRECTORY_PATH "/opt/share/loaders/" + +using namespace std; +namespace launchpad_parser_plugin { + +string LaunchpadParser::GetFilePath(string id) { + return "/opt/share/loaders/" + id + ".loader"; +} + +int LaunchpadParser::WriteToFile(string pkgid) { + if (access(LOADERS_DIRECTORY_PATH, F_OK) != 0) + mkdir(LOADERS_DIRECTORY_PATH, 0644); + + LOGI("write to file (%d)", loader_list_.size()); + for (auto& i : loader_list_) { + std::ofstream out_file; + LOGI("write ID (%s)", i->GetId().c_str()); + out_file.open(GetFilePath(i->GetId())); + out_file << "[LOADER]\n"; + out_file << "NAME " + i->GetId() + "\n"; + out_file << "EXE /usr/bin/launchpad-loader \n"; + out_file << "APP_TYPE capp|c++app \n"; + out_file << "DETECTION_METHOD TIMEOUT|VISIBILITY \n"; + out_file << "TIMEOUT 5000 \n"; + out_file << "TTL " + to_string(i->GetTimeToLive()) + "\n"; + out_file << "OWNER " + pkgid + "\n"; + out_file << "EXTRA loader_type common-loader \n"; + if (i->GetPreloadLib().size() > 0) { + out_file << "EXTRA_ARRAY preload \n"; + for (auto& lib : i->GetPreloadLib()) { + out_file << "EXTRA_ARRAY_VAL /usr/lib/" + lib + "\n"; + } + } + out_file.close(); + } + return 0; +} + +bool LaunchpadParser::IsValidId(string loader_id, string pkgid) { + ifstream in_file(GetFilePath(loader_id).c_str()); + if (!in_file.good()) + return true; + string key, val; + in_file >> key; + while (in_file >> key >> val) { + if (key == "OWNER") { + LOGI("key (%s), val (%s)", key.c_str(), val.c_str()); + if (val == pkgid) + return true; + } + } + return false; +} + +int LaunchpadParser::Install(xmlDocPtr doc, string pkgid) { + xmlNode* root = xmlDocGetRootElement(doc); + for (xmlNode* node = root->children; node; node = node->next) { + if (!node->name) + continue; + + string name = string((char*)node->name); + if (name != "provides-appdefined-loader") + continue; + + xmlChar* val = xmlGetProp(node, (const xmlChar *)"id"); + shared_ptr info = make_shared(string((char*)val)); + if (!IsValidId(info->GetId(), pkgid)) + return -1; + + xmlChar* ttl = xmlGetProp(node, (const xmlChar *)"time-to-live"); + if (ttl) + info->SetTimeToLive(stoi(string((char*)ttl))); + + for (xmlNode* iter = node->children; iter; iter = iter->next) { + if (!iter->name) + continue; + string child_name = string((char*)iter->name); + if (child_name == "preload-library") { + xmlChar* libname = xmlGetProp(iter, (const xmlChar *)"name"); + if (!libname) + continue; + info->AddPreloadLib(string((char*)libname)); + } + } + loader_list_.push_back(info); + } + WriteToFile(pkgid); + + return 0; +} + +int LaunchpadParser::Upgrade(xmlDocPtr doc, std::string pkgid) { + if (UnInstall(doc, pkgid) != 0) + return -1; + + return Install(doc, pkgid); +} + +int LaunchpadParser::UnInstall(xmlDocPtr doc, std::string pkgid) { + xmlNode* root = xmlDocGetRootElement(doc); + for (xmlNode* node = root->children; node; node = node->next) { + if (!node->name) + continue; + + string name = string((char*)node->name); + if (name != "provides-appdefined-loader") + continue; + + xmlChar* val = xmlGetProp(node, (const xmlChar *)"id"); + if (!val) + return -1; + + string id = string((char*)val); + if (!IsValidId(id, pkgid)) + return -1; + remove(GetFilePath(id).c_str()); + } + return 0; +} + +} // namspace launchpad_parser_plugin diff --git a/parser/launchpad_parser_plugin.hh b/parser/launchpad_parser_plugin.hh new file mode 100644 index 0000000..091e50c --- /dev/null +++ b/parser/launchpad_parser_plugin.hh @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 LAUNCHPAD_PARSER_PLUGIN_LAUNCHPAD_PARSER_PLUGIN_HH_ +#define LAUNCHPAD_PARSER_PLUGIN_LAUNCHPAD_PARSER_PLUGIN_HH_ + +#include +#include +#include +#include + +#include "loader_info.hh" + +namespace launchpad_parser_plugin { + +class LaunchpadParser { + public: + std::string GetFilePath(std::string id); + int WriteToFile(std::string pkgid); + int Install(xmlDocPtr doc, std::string pkgid); + int Upgrade(xmlDocPtr doc, std::string pkgid); + int UnInstall(xmlDocPtr doc, std::string pkgid); + bool IsValidId(std::string loader_id, std::string pkgid); + + private: + std::list> loader_list_; +}; + +} // namespace launchpad_parser_plugin + +#endif // LAUNCHPAD_PARSER_PLUGIN_LAUNCHPAD_PARSER_PLUGIN_HH_ + diff --git a/parser/launchpad_parser_plugin_pkgmgr_interface.cc b/parser/launchpad_parser_plugin_pkgmgr_interface.cc new file mode 100644 index 0000000..34d16ba --- /dev/null +++ b/parser/launchpad_parser_plugin_pkgmgr_interface.cc @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019 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 + +#include "launchpad_parser_plugin.hh" +#include "log_private.hh" +#include "common.hh" + +using namespace launchpad_parser_plugin; + +extern "C" EXPORT_API int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr doc, const char *package) +{ + LaunchpadParser parser; + LOGI("install"); + return parser.Install(doc, package); +} + +extern "C" EXPORT_API int PKGMGR_PARSER_PLUGIN_UNINSTALL(xmlDocPtr doc, const char *package) +{ + LOGI("uninstall"); + LaunchpadParser parser; + return parser.UnInstall(doc, package); +} + +extern "C" EXPORT_API int PKGMGR_PARSER_PLUGIN_UPGRADE(xmlDocPtr doc, const char *package) +{ + LOGI("upgrade"); + LaunchpadParser parser; + return parser.Upgrade(doc, package); +} + +extern "C" EXPORT_API int PKGMGR_PARSER_PLUGIN_RECOVERINSTALL(xmlDocPtr doc, + const char *package) +{ + LOGW("recover install"); + LaunchpadParser parser; + return parser.UnInstall(doc, package); +} + +extern "C" EXPORT_API int PKGMGR_PARSER_PLUGIN_RECOVERUNINSTALL(xmlDocPtr doc, + const char *package) +{ + LOGW("recover uninstall"); + LaunchpadParser parser; + return parser.UnInstall(doc, package); +} + +extern "C" EXPORT_API int PKGMGR_PARSER_PLUGIN_RECOVERUPGRADE(xmlDocPtr doc, + const char *package) +{ + LOGW("recover upgrade"); + LaunchpadParser parser; + return parser.Upgrade(doc, package); +} diff --git a/parser/launchpad_plugins.txt b/parser/launchpad_plugins.txt new file mode 100644 index 0000000..75a63d0 --- /dev/null +++ b/parser/launchpad_plugins.txt @@ -0,0 +1 @@ +type="tag";name="provides-appdefined-loader";path="/etc/package-manager/parserlib/liblaunchpad-parser.so";vitalness="true" diff --git a/parser/loader_info.cc b/parser/loader_info.cc new file mode 100644 index 0000000..1f6a2f5 --- /dev/null +++ b/parser/loader_info.cc @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 "loader_info.hh" + +using namespace std; +namespace launchpad_parser_plugin { + +LoaderInfo::LoaderInfo(std::string id) : id_(id), time_to_live_(0) { +} + +string LoaderInfo::GetId() { + return id_; +} + +int LoaderInfo::GetTimeToLive() { + return time_to_live_; +} + +void LoaderInfo::SetTimeToLive(int time) { + time_to_live_ = time; +} + +list LoaderInfo::GetPreloadLib() { + return preload_lib_list_; +} + +void LoaderInfo::AddPreloadLib(string lib_name) { + preload_lib_list_.push_back(lib_name); +} + +} // namspace launchpad_parser_plugin diff --git a/parser/loader_info.hh b/parser/loader_info.hh new file mode 100644 index 0000000..c4ab4f3 --- /dev/null +++ b/parser/loader_info.hh @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 LAUNCHPAD_PARSER_PLUGIN_LOADER_INFO_HH_ +#define LAUNCHPAD_PARSER_PLUGIN_LOADER_INFO_HH_ + +#include +#include + +namespace launchpad_parser_plugin { + +class LoaderInfo { + public: + LoaderInfo(std::string id); + std::string GetId(); + int GetTimeToLive(); + void SetTimeToLive(int time); + std::list GetPreloadLib(); + void AddPreloadLib(std::string lib_name); + + private: + std::string id_; + int time_to_live_; + std::list preload_lib_list_; +}; + +} // namespace launchpad_parser_plugin + +#endif // LAUNCHPAD_PARSER_PLUGIN_LOADER_INFO_HH_ + diff --git a/parser/log_private.hh b/parser/log_private.hh new file mode 100644 index 0000000..80e244b --- /dev/null +++ b/parser/log_private.hh @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020 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 LAUNCHPAD_PARSER_PLUGIN_LOG_PRIVATE_HH_ +#define LAUNCHPAD_PARSER_PLUGIN_LOG_PRIVATE_HH_ + +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "LAUNCHPAD_PARSER_PLUGIN" + +#ifdef _E +#undef _E +#endif +#define _E(fmt, arg...) LOGE(fmt, ##arg) + +#ifdef _D +#undef _D +#endif +#define _D(fmt, arg...) LOGD(fmt, ##arg) + +#ifdef _W +#undef _W +#endif +#define _W(fmt, arg...) LOGW(fmt, ##arg) + +#ifdef _I +#undef _I +#endif +#define _I(fmt, arg...) LOGI(fmt, ##arg) + +#endif /* LAUNCHPAD_PARSER_PLUGIN_LOG_PRIVATE_HH_ */ -- 2.7.4