From: Mu-Woong Lee Date: Mon, 25 Apr 2016 07:17:30 +0000 (+0900) Subject: Apply launch-time loading routine for providers X-Git-Tag: submit/tizen/20160503.015801^2~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ce6b88d82564d9a9a2c78218f99d42f1068f13ea;p=platform%2Fcore%2Fcontext%2Fcontext-service.git Apply launch-time loading routine for providers It will be modified to load only the necessary providers on demand. Change-Id: I81f622c42dd3ae5d0615feb2aa1592006e67c6f0 Signed-off-by: Mu-Woong Lee --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 29f2f85..9c142e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,8 @@ SET(DEPS cynara-creds-gdbus cynara-client cynara-session - context-provider + libcontext-server + context ) # Common Options @@ -30,21 +31,19 @@ INCLUDE_DIRECTORIES( /usr/include /usr/include/glib-2.0 ) -ADD_DEFINITIONS(-O2 -Wall -fPIC -fdata-sections -ffunction-sections -fvisibility=hidden) -SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -Wl,--as-needed -Wl,--gc-section -Wl,--print-gc-section") +ADD_DEFINITIONS(-O2 -Wall -fPIC -fPIE -fdata-sections -ffunction-sections -fvisibility=hidden) +ADD_DEFINITIONS(-DLOG_TAG="CONTEXT") +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -pie -Wl,--as-needed -Wl,--gc-section -Wl,--print-gc-section") # Building Daemon pkg_check_modules(daemon_pkg REQUIRED ${DEPS}) -SET(DAEMON_EXTRA_CFLAGS -fPIE) FOREACH(flag ${daemon_pkg_CFLAGS}) - SET(DAEMON_EXTRA_CFLAGS "${DAEMON_EXTRA_CFLAGS} ${flag}") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") ENDFOREACH(flag) ADD_EXECUTABLE(${target} ${SRCS}) -TARGET_LINK_LIBRARIES(${target} ${daemon_pkg_LDFLAGS} -pie) -SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_FLAGS ${DAEMON_EXTRA_CFLAGS}) -SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_DEFINITIONS "LOG_TAG=\"CONTEXT\"") +TARGET_LINK_LIBRARIES(${target} ${daemon_pkg_LDFLAGS} -ldl) # Installing Daemon INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/packaging/context-service.spec b/packaging/context-service.spec index ccba421..f9e2e8d 100644 --- a/packaging/context-service.spec +++ b/packaging/context-service.spec @@ -28,8 +28,9 @@ BuildRequires: pkgconfig(cynara-creds-gdbus) BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-session) +BuildRequires: pkgconfig(libcontext-server) BuildRequires: pkgconfig(context) -BuildRequires: pkgconfig(context-provider) +Requires: context-provider Requires(preun): /usr/bin/systemctl Requires(post): /usr/bin/systemctl diff --git a/src/ContextManager.cpp b/src/ContextManager.cpp index 119fcff..39b0ded 100644 --- a/src/ContextManager.cpp +++ b/src/ContextManager.cpp @@ -29,12 +29,6 @@ #include "ContextManager.h" #include "trigger/TemplateManager.h" -/* Context Providers */ -#include -#include -#include -#include - using namespace ctx; struct TriggerItemFormat { @@ -76,21 +70,7 @@ ContextManager::~ContextManager() bool ContextManager::init() { - bool ret; - - ret = initDeviceContextProvider(); - IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: DeviceContextProvider"); - - ret = initStatisticsContextProvider(); - IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: StatisticsContextProvider"); - - ret = initPlaceContextProvider(); - IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: PlaceContextProvider"); - -// ret = initCustomContextProvider(); -// IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: CustomContextProvider"); - - __initialized = true; + __initialized = __providerLoader.loadAll(); return true; } diff --git a/src/ContextManager.h b/src/ContextManager.h index e2518bc..884dd71 100644 --- a/src/ContextManager.h +++ b/src/ContextManager.h @@ -19,6 +19,7 @@ #include #include +#include "ProviderLoader.h" namespace ctx { @@ -60,6 +61,7 @@ namespace ctx { bool __initialized; std::map __providerHandleMap; + ProviderLoader __providerLoader; friend class Server; diff --git a/src/ProviderLoader.cpp b/src/ProviderLoader.cpp new file mode 100644 index 0000000..b8c5bf6 --- /dev/null +++ b/src/ProviderLoader.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2016 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 +#include "ProviderLoader.h" + +#define PROVIDER_SO_PATH "/usr/lib/context" + +using namespace ctx; + +typedef bool (*create_t)(); + +static bool getSharedObjectPaths(const char *dirPath, std::set &soNames) +{ + DIR *dir = NULL; + struct dirent dirEntry; + struct dirent *result; + int error; + + dir = opendir(dirPath); + IF_FAIL_RETURN_TAG(dir, false, _E, "Failed to open '%s'", dirPath); + + while (true) { + error = readdir_r(dir, &dirEntry, &result); + + if (error != 0) + continue; + + if (result == NULL) + break; + + if (dirEntry.d_type != DT_REG) + continue; + + soNames.insert(dirEntry.d_name); + } + + closedir(dir); + return true; +} + +ProviderLoader::ProviderLoader() +{ +} + +ProviderLoader::~ProviderLoader() +{ + __unload(); +} + +ContextProvider* ProviderLoader::load(const char *subject) +{ + /* TODO: Implement */ + return NULL; +} + +ContextProvider* ProviderLoader::__load(const char *soPath, const char *subject) +{ + _I("Load '%s' from '%s'", subject, soPath); + + void *soHandle = dlopen(soPath, RTLD_LAZY | RTLD_GLOBAL); + IF_FAIL_RETURN_TAG(soHandle, NULL, _E, "%s", dlerror()); + + create_t create = reinterpret_cast(dlsym(soHandle, "create")); + if (!create) { + _E("%s", dlerror()); + dlclose(soHandle); + return NULL; + } + + /* TODO: Update this part for dynamic loading */ + create(); + + return NULL; +} + +void ProviderLoader::__unload() +{ + /* TODO: Implement */ +} + +bool ProviderLoader::loadAll() +{ + /* TODO: Remove this function. This is a temporary solution. */ + std::set soNames; + std::string soPath; + + if (!getSharedObjectPaths(PROVIDER_SO_PATH, soNames)) { + return false; + } + + for (std::set::iterator it = soNames.begin(); it != soNames.end(); ++it) { + soPath = PROVIDER_SO_PATH; + soPath = soPath + "/" + (*it); + __load(soPath.c_str(), NULL); + } + + return true; +} diff --git a/src/ProviderLoader.h b/src/ProviderLoader.h new file mode 100644 index 0000000..fedca25 --- /dev/null +++ b/src/ProviderLoader.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2016 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 _CONTEXT_PROVIDER_LOADER_H_ +#define _CONTEXT_PROVIDER_LOADER_H_ + +namespace ctx { + + class ContextProvider; + + class ProviderLoader { + public: + ProviderLoader(); + ~ProviderLoader(); + + ContextProvider* load(const char *subject); + bool loadAll(); + + private: + ContextProvider* __load(const char *soPath, const char *subject); + void __unload(); + }; + +} + +#endif /* _CONTEXT_PROVIDER_LOADER_H_ */