4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include <linux/limits.h>
29 #include <sys/types.h>
32 #include <pkgmgr-info.h>
34 #include "ug-module.h"
37 #include <tzplatform_config.h>
39 #define UG_MODULE_INIT_SYM "UG_MODULE_INIT"
40 #define UG_MODULE_EXIT_SYM "UG_MODULE_EXIT"
42 #define MEM_ADDR_LEN 8
43 #define MEM_ADDR_TOT_LEN 17
46 #define UG_API __attribute__ ((visibility("default")))
50 static char *__ug_module_get_addr(const char *ug_name)
53 char buf[PATH_MAX] = {0,};
54 char mem[PATH_MAX] = {0,};
56 char *token_param = NULL;
63 snprintf(buf, sizeof(buf), "/proc/%d/maps", getpid());
65 file = fopen(buf, "r");
67 _WRN("proc open fail(%d)", errno);
71 memset(buf, 0x00, PATH_MAX);
73 while (fgets(buf, PATH_MAX, file) != NULL) {
74 if (strstr(buf, ug_name)) {
75 token_param = strtok_r(buf, " ", &saveptr);
76 if ((token_param == NULL) || (strlen(token_param) > MEM_ADDR_TOT_LEN)) {
77 _ERR("proc token param(%s) error", token_param);
82 memcpy((void *)(mem+MEM_ADDR_LEN+1),
83 (const void *)(token_param+MEM_ADDR_LEN+1), MEM_ADDR_LEN);
85 memcpy((void *)mem, (const void *)token_param, strlen(token_param));
93 memset(buf, 0x00, PATH_MAX);
108 static int __file_exist(const char *path)
112 ret = access(path, R_OK);
113 LOGD("ug_file(%s) check %s", path, ret ? "fail" : "ok");
118 static int __get_ug_info(const char *name, char **ug_file_path)
120 char ug_file[PATH_MAX];
121 char app_id[NAME_MAX];
123 char *res_path = NULL;
124 pkgmgrinfo_appinfo_h appinfo = NULL;
126 /* get path using name(file name) */
127 snprintf(ug_file, PATH_MAX, "%s/lib/libug-%s.so",
128 tzplatform_getenv(TZ_SYS_RO_UG), name);
129 if (!__file_exist(ug_file))
131 snprintf(ug_file, PATH_MAX, "%s/lib/lib%s.so",
132 tzplatform_getenv(TZ_SYS_RO_UG), name);
133 if (!__file_exist(ug_file))
136 /* get path using appid */
137 if (aul_app_get_appid_bypid(getpid(), app_id, sizeof(app_id))) {
138 LOGE("failed to get appid");
141 snprintf(ug_file, PATH_MAX, "%s/lib/libug-%s.so",
142 tzplatform_getenv(TZ_SYS_RO_UG), app_id);
143 if (!__file_exist(ug_file))
145 snprintf(ug_file, PATH_MAX, "%s/lib/lib%s.so",
146 tzplatform_getenv(TZ_SYS_RO_UG), app_id);
147 if (!__file_exist(ug_file))
150 /* get app root path */
151 if (pkgmgrinfo_appinfo_get_usr_appinfo(app_id, getuid(), &appinfo)) {
152 LOGE("failed to get app info");
155 if (pkgmgrinfo_appinfo_get_root_path(appinfo, &root_path)) {
156 LOGE("failed to get app root path");
157 pkgmgrinfo_appinfo_destroy_appinfo(appinfo);
160 /* get path using name and root path.
161 * in this case, try to get ug app's library in the same pacakge.
163 snprintf(ug_file, PATH_MAX, "%s/lib/ug/libug-%s.so", root_path, name);
164 if (!__file_exist(ug_file))
166 snprintf(ug_file, PATH_MAX, "%s/lib/ug/lib%s.so", root_path, name);
167 if (!__file_exist(ug_file))
169 /* get path using appid and root path */
170 snprintf(ug_file, PATH_MAX, "%s/lib/ug/libug-%s.so", root_path, app_id);
171 if (!__file_exist(ug_file))
173 snprintf(ug_file, PATH_MAX, "%s/lib/ug/lib%s.so", root_path, app_id);
174 if (!__file_exist(ug_file))
177 /* get path using name and shared resource path.
178 * in this case, we need to get other package's ug library
179 * and the 'name' should be ug's appid.
181 if (aul_get_app_shared_resource_path_by_appid(name, &res_path)) {
182 LOGE("failed to get shared resource path");
183 pkgmgrinfo_appinfo_destroy_appinfo(appinfo);
186 snprintf(ug_file, PATH_MAX, "%s/lib/ug/libug-%s.so", res_path, name);
187 if (!__file_exist(ug_file))
189 snprintf(ug_file, PATH_MAX, "%s/lib/ug/lib-%s.so", res_path, name);
190 if (!__file_exist(ug_file))
194 if ((strlen(ug_file) > 0) && (ug_file_path))
195 *ug_file_path = strdup(ug_file);
199 pkgmgrinfo_appinfo_destroy_appinfo(appinfo);
204 struct ug_module *ug_module_load(const char *name)
207 struct ug_module *module;
208 int (*module_init) (struct ug_module_ops *ops);
209 char *ug_file = NULL;
211 if (__get_ug_info(name, &ug_file) < 0) {
212 _ERR("error in getting ug file path");
216 module = calloc(1, sizeof(struct ug_module));
223 handle = dlopen(ug_file, RTLD_LAZY);
225 _ERR("dlopen failed: %s", dlerror());
229 module_init = dlsym(handle, UG_MODULE_INIT_SYM);
231 _ERR("dlsym failed: %s", dlerror());
235 if (module_init(&module->ops))
238 module->handle = handle;
239 module->module_name = strdup(name);
241 module->addr = __ug_module_get_addr(name);
255 int ug_module_unload(struct ug_module *module)
257 void (*module_exit) (struct ug_module_ops *ops);
264 if (module->handle) {
265 module_exit = dlsym(module->handle, UG_MODULE_EXIT_SYM);
267 module_exit(&module->ops);
269 _ERR("dlsym failed: %s", dlerror());
271 _DBG("dlclose(%s)", module->module_name);
272 dlclose(module->handle);
273 module->handle = NULL;
276 if (module->module_name)
277 free(module->module_name);
286 UG_API int ug_module_get_file_path(const char *name, char **ug_file_path)
288 if (__get_ug_info(name, ug_file_path) < 0) {
289 _ERR("error in getting ug file path");