apply FSL license
[pkgs/u/ui-gadget.git] / src / module.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *     http://www.tizenopensource.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
18
19
20
21 #include <linux/limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <dlfcn.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29
30 #include "ug-module.h"
31 #include "ug-dbg.h"
32
33 #define UG_MODULE_INIT_SYM "UG_MODULE_INIT"
34 #define UG_MODULE_EXIT_SYM "UG_MODULE_EXIT"
35
36 static int file_exist(const char *filename)
37 {
38         FILE *file;
39         if ((file = fopen(filename, "r"))) {
40                 fclose(file);
41                 return 0;
42         }
43
44         return -1;
45 }
46
47 struct ug_module *ug_module_load(const char *name)
48 {
49         void *handle;
50         struct ug_module *module;
51         char ug_file[PATH_MAX];
52
53         char *pkg_name = NULL;
54         uid_t uid;
55
56         int (*module_init) (struct ug_module_ops *ops);
57
58         module = calloc(1, sizeof(struct ug_module));
59
60         if (!module) {
61                 errno = ENOMEM;
62                 return NULL;
63         }
64
65         pkg_name = getenv("PKG_NAME");
66         uid = geteuid();
67
68         if (pkg_name) {
69                 if (snprintf
70                     (ug_file, PATH_MAX, "/opt/apps/%s/lib/libug-%s.so",
71                      pkg_name, name) < 0) {
72                         goto module_free;
73                 } else if (file_exist(ug_file) < 0) {
74                         if (snprintf
75                             (ug_file, PATH_MAX, "/usr/ug/lib/libug-%s.so",
76                              name) < 0) {
77                                 goto module_free;
78                         } else if (file_exist(ug_file) < 0) {
79                                 if (snprintf
80                                     (ug_file, PATH_MAX,
81                                      "/opt/ug/lib/libug-%s.so", name) < 0) {
82                                         goto module_free;
83                                 } else if (file_exist(ug_file) < 0) {
84                                         goto module_free;
85                                 }
86                         }
87                 }
88         } else {
89                 if (snprintf(ug_file, PATH_MAX, "/usr/ug/lib/libug-%s.so", name)
90                     < 0) {
91                         goto module_free;
92                 } else if (file_exist(ug_file) < 0) {
93                         if (snprintf
94                             (ug_file, PATH_MAX, "/opt/ug/lib/libug-%s.so",
95                              name) < 0) {
96                                 goto module_free;
97                         } else if (file_exist(ug_file) < 0) {
98                                 goto module_free;
99                         }
100                 }
101         }
102
103         handle = dlopen(ug_file, RTLD_LAZY);
104         if (!handle) {
105                 _ERR("dlopen failed: %s\n", dlerror());
106                 goto module_free;
107         }
108
109         module_init = dlsym(handle, UG_MODULE_INIT_SYM);
110         if (!module_init) {
111                 _ERR("dlsym failed: %s\n", dlerror());
112                 goto module_dlclose;
113         }
114
115         if (module_init(&module->ops))
116                 goto module_dlclose;
117
118         module->handle = handle;
119         return module;
120
121  module_dlclose:
122         dlclose(handle);
123
124  module_free:
125         free(module);
126         return NULL;
127 }
128
129 int ug_module_unload(struct ug_module *module)
130 {
131         void (*module_exit) (struct ug_module_ops *ops);
132
133         if (!module) {
134                 errno = EINVAL;
135                 return -1;
136         }
137
138         if (module->handle) {
139                 module_exit = dlsym(module->handle, UG_MODULE_EXIT_SYM);
140                 if (module_exit)
141                         module_exit(&module->ops);
142                 else
143                         _ERR("dlsym failed: %s\n", dlerror());
144
145                 dlclose(module->handle);
146         }
147
148         free(module);
149         return 0;
150 }