Apply launch-time loading routine for providers 92/67092/5
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 25 Apr 2016 07:17:30 +0000 (16:17 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 25 Apr 2016 10:22:43 +0000 (19:22 +0900)
It will be modified to load only the necessary providers on demand.

Change-Id: I81f622c42dd3ae5d0615feb2aa1592006e67c6f0
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
CMakeLists.txt
packaging/context-service.spec
src/ContextManager.cpp
src/ContextManager.h
src/ProviderLoader.cpp [new file with mode: 0644]
src/ProviderLoader.h [new file with mode: 0644]

index 29f2f85911aa3a47aa6facc68ff3f3e1fcfe3bd0..9c142e75bd9adf9bf5ab05750245b72358fd78d0 100644 (file)
@@ -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})
index ccba421c167e3e513188b4819a5a3891899f4821..f9e2e8d7f83ace67b36ca0fa3982aac456a699e1 100644 (file)
@@ -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
index 119fcffa8c6b1a7cc8a88a8a25f13c9261d0cb30..39b0ded8e4dd962b520602f82c4e41d431ae0b2e 100644 (file)
 #include "ContextManager.h"
 #include "trigger/TemplateManager.h"
 
-/* Context Providers */
-#include <internal/DeviceContextProvider.h>
-#include <internal/StatisticsContextProvider.h>
-#include <internal/PlaceContextProvider.h>
-#include <internal/CustomContextProvider.h>
-
 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;
 }
 
index e2518bcaee217ca5ef6b15f1c0da970fc0ff1772..884dd714d2f6414302a3420b6de894d3c50330a4 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <map>
 #include <IContextManager.h>
+#include "ProviderLoader.h"
 
 namespace ctx {
 
@@ -60,6 +61,7 @@ namespace ctx {
 
                bool __initialized;
                std::map<std::string, ProviderHandler*> __providerHandleMap;
+               ProviderLoader __providerLoader;
 
                friend class Server;
 
diff --git a/src/ProviderLoader.cpp b/src/ProviderLoader.cpp
new file mode 100644 (file)
index 0000000..b8c5bf6
--- /dev/null
@@ -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 <dlfcn.h>
+#include <dirent.h>
+#include <set>
+#include <Types.h>
+#include <ContextProvider.h>
+#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<std::string> &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<create_t>(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<std::string> soNames;
+       std::string soPath;
+
+       if (!getSharedObjectPaths(PROVIDER_SO_PATH, soNames)) {
+               return false;
+       }
+
+       for (std::set<std::string>::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 (file)
index 0000000..fedca25
--- /dev/null
@@ -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_ */