Remove hardcoded path for multiuser support
[platform/framework/web/livebox-cpp.git] / src / CModule.cpp
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <dlfcn.h>
22
23 #include <dlog.h>
24 #include <livebox-service.h>
25 #include <livebox-errno.h>
26 #include <tzplatform_config.h>
27
28 #include "debug.h"
29 #include "livebox-cpp.h"
30 #include "livebox-impl.h"
31 #include "CModule.h"
32 #include "dlist.h"
33
34 const char *CModule::m_sModulePath = tzplatform_mkpath(TZ_USER_LIVE,"%s/libexec/liblive-%s.so");
35 struct dlist *CModule::m_pModuleList = NULL;
36
37 CModule *CModule::Load(const char *pkgname)
38 {
39         CModule *inst;
40         char *tmp;
41         char *module_path;
42         int ret;
43
44         tmp = strdup(pkgname);
45         if (!tmp) {
46                 ErrPrint("Memory: %s\n", strerror(errno));
47                 return NULL;
48         }
49
50         try {
51                 inst = new CModule(tmp);
52         } catch (...) {
53                 ErrPrint("Memory\n");
54                 free(tmp);
55                 return NULL;
56         }
57
58         module_path = livebox_service_libexec(pkgname);
59         if (!module_path) {
60                 delete inst;
61                 free(tmp);
62                 return NULL;
63         }
64
65         inst->m_pHandle = dlopen(module_path, RTLD_LOCAL | RTLD_NOW | RTLD_DEEPBIND);
66         free(module_path);
67
68         if (!inst->m_pHandle) {
69                 ErrPrint("dlopen: %s\n", dlerror());
70                 delete inst;
71                 free(tmp);
72                 return NULL;
73         }
74
75         inst->m_fNew = (void *(*)(void))dlsym(inst->m_pHandle, "livebox_new");
76         if (!inst->m_fNew) {
77                 ErrPrint("symbol: livebox_new is not found: %s\n", dlerror());
78                 dlclose(inst->m_pHandle);
79                 delete inst;
80                 free(tmp);
81                 return NULL;
82         }
83
84         inst->m_fNeedToCreate = (int (*)(const char *, const char *))dlsym(inst->m_pHandle, "livebox_need_to_create");
85         if (!inst->m_fNeedToCreate) {
86                 DbgPrint("%s has no livebox_need_to_create\n", pkgname);
87         }
88
89         inst->m_fInitialize = (int (*)(const char *))dlsym(inst->m_pHandle, "livebox_initialize");
90         if (!inst->m_fInitialize) {
91                 DbgPrint("%s has no livebox_initialize\n", pkgname);
92         } else {
93                 ret = inst->m_fInitialize(pkgname);
94                 if (ret < 0) {
95                         ErrPrint("livebox_finalize(%s) returns %d\n", pkgname, ret);
96                         dlclose(inst->m_pHandle);
97                         delete inst;
98                         free(tmp);
99                         return NULL;
100                 }
101         }
102
103         inst->m_fFinalize = (int (*)(void))dlsym(inst->m_pHandle, "livebox_finalize");
104         if (!inst->m_fFinalize) {
105                 DbgPrint("%s has no livebox_finalize\n", pkgname);
106         }
107
108         m_pModuleList = dlist_append(m_pModuleList, inst);
109         return inst;
110 }
111
112 int CModule::Unload(void)
113 {
114         struct dlist *l;
115
116         l = dlist_find_data(m_pModuleList, this);
117         m_pModuleList = dlist_remove(m_pModuleList, l);
118
119         if (m_fFinalize) {
120                 int ret;
121                 ret = m_fFinalize();
122                 DbgPrint("livebox_finalize of %s returns %d\n", m_sPkgname, ret);
123         }
124
125         dlclose(m_pHandle);
126         free(m_sPkgname);
127         delete this;
128         return LB_STATUS_SUCCESS;
129 }
130
131 int CModule::Create(const char *filename, const char *content, const char *cluster, const char *category)
132 {
133         CLiveBoxImpl *inst;
134
135         inst = (CLiveBoxImpl *)m_fNew();
136         if (inst) {
137                 int ret;
138                 ret = inst->Create(filename, content, cluster, category);
139                 if (ret < 0) {
140                         delete inst;
141                         return ret;
142                 }
143
144                 m_pList = dlist_append(m_pList, inst);
145                 return 0;
146         }
147
148         return LB_STATUS_ERROR_FAULT;
149 }
150
151 int CModule::Destroy(CLiveBoxImpl *inst)
152 {
153         struct dlist *l;
154
155         l = dlist_find_data(m_pList, inst);
156         if (!l) {
157                 return LB_STATUS_ERROR_NOT_EXIST;
158         }
159
160         m_pList = dlist_remove(m_pList, l);
161         delete inst;
162         return LB_STATUS_SUCCESS;
163 }
164
165 CLiveBoxImpl *CModule::FindLiveBox(const char *filename)
166 {
167         struct dlist *l;
168         void *item;
169         CLiveBoxImpl *box;
170
171         dlist_foreach(m_pList, l, item) {
172                 box = (CLiveBoxImpl *)item;
173                 if (!strcmp(filename, box->Filename())) {
174                         return box;
175                 }
176         }
177
178         return NULL;
179 }
180
181 int CModule::NeedToCreate(const char *cluster, const char *category)
182 {
183         if (!m_fNeedToCreate) {
184                 return 0;
185         }
186
187         return m_fNeedToCreate(cluster, category);
188 }
189
190 CModule *CModule::FindModule(const char *pkgname)
191 {
192         struct dlist *l;
193         void *item;
194         CModule *module;
195
196         dlist_foreach(m_pModuleList, l, item) {
197                 module = (CModule *)item;
198                 if (!strcmp(pkgname, module->PackageName())) {
199                         return module;
200                 }
201         }
202
203         return NULL;
204 }
205
206 /* End of a file */