Remove hardcoded path for multiuser support
[platform/core/appfw/ui-gadget-1.git] / src / module.c
1 /*
2  *  UI Gadget
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <linux/limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <dlfcn.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30
31 #include <app_manager.h>
32
33 #include "ug-module.h"
34 #include "ug-dbg.h"
35
36 #include <tzplatform_config.h>
37
38 #define UG_MODULE_INIT_SYM "UG_MODULE_INIT"
39 #define UG_MODULE_EXIT_SYM "UG_MODULE_EXIT"
40
41 static int file_exist(const char *filename)
42 {
43         FILE *file;
44         if ((file = fopen(filename, "r"))) {
45                 fclose(file);
46                 return 1;
47         }
48
49         return 0;
50 }
51
52 struct ug_module *ug_module_load(const char *name)
53 {
54         void *handle;
55         struct ug_module *module;
56         char ug_file[PATH_MAX];
57         char *pkg_name = NULL;
58
59         int (*module_init) (struct ug_module_ops *ops);
60
61         module = calloc(1, sizeof(struct ug_module));
62         if (!module) {
63                 errno = ENOMEM;
64                 return NULL;
65         }
66
67         app_manager_get_package(getpid(), &pkg_name);
68
69         do {
70                 if (pkg_name) {
71                         snprintf(ug_file, PATH_MAX, "%s/%s/lib/libug-%s.so", tzplatform_getenv(TZ_SYS_RO_APP), pkg_name, name);
72                         if (file_exist(ug_file))
73                                 break;
74                         snprintf(ug_file, PATH_MAX, "%s/%s/lib/libug-%s.so", tzplatform_getenv(TZ_SYS_RO_APP), pkg_name, name);
75                         if (file_exist(ug_file))
76                                 break;
77                 }
78                 snprintf(ug_file, PATH_MAX, "/usr/ug/lib/libug-%s.so", name);
79                 if (file_exist(ug_file))
80                         break;
81                 snprintf(ug_file, PATH_MAX, "/opt/usr/ug/lib/libug-%s.so", name);
82                 if (file_exist(ug_file))
83                         break;
84         } while (0);
85
86         if(pkg_name) {
87                 free(pkg_name);
88                 pkg_name = NULL;
89         }
90
91         handle = dlopen(ug_file, RTLD_LAZY);
92         if (!handle) {
93                 _ERR("dlopen failed: %s", dlerror());
94                 goto module_free;
95         }
96
97         module_init = dlsym(handle, UG_MODULE_INIT_SYM);
98         if (!module_init) {
99                 _ERR("dlsym failed: %s", dlerror());
100                 goto module_dlclose;
101         }
102
103         if (module_init(&module->ops))
104                 goto module_dlclose;
105
106         module->handle = handle;
107         module->module_name = strdup(name);
108         return module;
109
110  module_dlclose:
111         dlclose(handle);
112
113  module_free:
114         free(module);
115         return NULL;
116 }
117
118 int ug_module_unload(struct ug_module *module)
119 {
120         void (*module_exit) (struct ug_module_ops *ops);
121
122         if (!module) {
123                 errno = EINVAL;
124                 return -1;
125         }
126
127         if (module->handle) {
128                 module_exit = dlsym(module->handle, UG_MODULE_EXIT_SYM);
129                 if (module_exit)
130                         module_exit(&module->ops);
131                 else
132                         _ERR("dlsym failed: %s", dlerror());
133
134                 dlclose(module->handle);
135                 module->handle = NULL;
136         }
137
138         if(module->module_name)
139                 free(module->module_name);
140
141         free(module);
142         return 0;
143 }
144
145 int ug_exist(const char* name)
146 {
147         char ug_file[PATH_MAX] = {0,};
148         int ret = 0;
149
150         do {
151                 snprintf(ug_file, PATH_MAX, "/usr/ug/lib/libug-%s.so", name);
152                 if (file_exist(ug_file)) {
153                         ret = 1;
154                         break;
155                 }
156                 snprintf(ug_file, PATH_MAX, "/opt/ug/lib/libug-%s.so", name);
157                 if (file_exist(ug_file)) {
158                         ret = 1;
159                         break;
160                 }
161                 snprintf(ug_file, PATH_MAX, "/opt/usr/ug/lib/libug-%s.so", name);
162                 if (file_exist(ug_file)) {
163                         ret = 1;
164                         break;
165                 }
166         } while (0);
167
168         return ret;
169 }