2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <livebox-service.h>
25 #include <livebox-errno.h>
28 #include "livebox-cpp.h"
29 #include "livebox-impl.h"
33 const char *CModule::m_sModulePath = "/opt/usr/live/%s/libexec/liblive-%s.so";
34 struct dlist *CModule::m_pModuleList = NULL;
36 CModule *CModule::Load(const char *pkgname)
43 tmp = strdup(pkgname);
45 ErrPrint("Memory: %s\n", strerror(errno));
50 inst = new CModule(tmp);
57 module_path = livebox_service_libexec(pkgname);
64 inst->m_pHandle = dlopen(module_path, RTLD_LOCAL | RTLD_NOW | RTLD_DEEPBIND);
67 if (!inst->m_pHandle) {
68 ErrPrint("dlopen: %s\n", dlerror());
74 inst->m_fNew = (void *(*)(void))dlsym(inst->m_pHandle, "livebox_new");
76 ErrPrint("symbol: livebox_new is not found: %s\n", dlerror());
77 dlclose(inst->m_pHandle);
83 inst->m_fNeedToCreate = (int (*)(const char *, const char *))dlsym(inst->m_pHandle, "livebox_need_to_create");
84 if (!inst->m_fNeedToCreate)
85 DbgPrint("%s has no livebox_need_to_create\n", pkgname);
87 inst->m_fInitialize = (int (*)(const char *))dlsym(inst->m_pHandle, "livebox_initialize");
88 if (!inst->m_fInitialize) {
89 DbgPrint("%s has no livebox_initialize\n", pkgname);
91 ret = inst->m_fInitialize(pkgname);
93 ErrPrint("livebox_finalize(%s) returns %d\n", pkgname, ret);
94 dlclose(inst->m_pHandle);
101 inst->m_fFinalize = (int (*)(void))dlsym(inst->m_pHandle, "livebox_finalize");
102 if (!inst->m_fFinalize)
103 DbgPrint("%s has no livebox_finalize\n", pkgname);
105 m_pModuleList = dlist_append(m_pModuleList, inst);
109 int CModule::Unload(void)
113 l = dlist_find_data(m_pModuleList, this);
114 m_pModuleList = dlist_remove(m_pModuleList, l);
119 DbgPrint("livebox_finalize of %s returns %d\n", m_sPkgname, ret);
125 return LB_STATUS_SUCCESS;
128 int CModule::Create(const char *filename, const char *content, const char *cluster, const char *category)
132 inst = (CLiveBoxImpl *)m_fNew();
135 ret = inst->Create(filename, content, cluster, category);
141 m_pList = dlist_append(m_pList, inst);
145 return LB_STATUS_ERROR_FAULT;
148 int CModule::Destroy(CLiveBoxImpl *inst)
152 l = dlist_find_data(m_pList, inst);
154 return LB_STATUS_ERROR_NOT_EXIST;
156 m_pList = dlist_remove(m_pList, l);
158 return LB_STATUS_SUCCESS;
161 CLiveBoxImpl *CModule::FindLiveBox(const char *filename)
167 dlist_foreach(m_pList, l, item) {
168 box = (CLiveBoxImpl *)item;
169 if (!strcmp(filename, box->Filename()))
176 int CModule::NeedToCreate(const char *cluster, const char *category)
178 if (!m_fNeedToCreate)
181 return m_fNeedToCreate(cluster, category);
184 CModule *CModule::FindModule(const char *pkgname)
190 dlist_foreach(m_pModuleList, l, item) {
191 module = (CModule *)item;
192 if (!strcmp(pkgname, module->PackageName())) {