check rotation lock and set rotation info when ug launch
[framework/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 "ug-module.h"
32 #include "ug-dbg.h"
33
34 #define UG_MODULE_INIT_SYM "UG_MODULE_INIT"
35 #define UG_MODULE_EXIT_SYM "UG_MODULE_EXIT"
36
37 static int file_exist(const char *filename)
38 {
39         FILE *file;
40         if ((file = fopen(filename, "r"))) {
41                 fclose(file);
42                 return 1;
43         }
44
45         return 0;
46 }
47
48 struct ug_module *ug_module_load(const char *name)
49 {
50         void *handle;
51         struct ug_module *module;
52         char ug_file[PATH_MAX];
53
54         char *pkg_name = NULL;
55         uid_t uid;
56
57         int (*module_init) (struct ug_module_ops *ops);
58
59         module = calloc(1, sizeof(struct ug_module));
60
61         if (!module) {
62                 errno = ENOMEM;
63                 return NULL;
64         }
65
66         pkg_name = getenv("PKG_NAME");
67         uid = geteuid();
68
69         do {
70                 if (pkg_name) {
71                         snprintf(ug_file, PATH_MAX, "/usr/apps/%s/lib/libug-%s.so", pkg_name, name);
72                         if (file_exist(ug_file))
73                                 break;
74                         snprintf(ug_file, PATH_MAX, "/opt/apps/%s/lib/libug-%s.so", 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/ug/lib/libug-%s.so", name);
82                 if (file_exist(ug_file))
83                         break;
84         } while (0);
85
86         handle = dlopen(ug_file, RTLD_LAZY);
87         if (!handle) {
88                 _ERR("dlopen failed: %s\n", dlerror());
89                 goto module_free;
90         }
91
92         module_init = dlsym(handle, UG_MODULE_INIT_SYM);
93         if (!module_init) {
94                 _ERR("dlsym failed: %s\n", dlerror());
95                 goto module_dlclose;
96         }
97
98         if (module_init(&module->ops))
99                 goto module_dlclose;
100
101         module->handle = handle;
102         return module;
103
104  module_dlclose:
105         dlclose(handle);
106
107  module_free:
108         free(module);
109         return NULL;
110 }
111
112 int ug_module_unload(struct ug_module *module)
113 {
114         void (*module_exit) (struct ug_module_ops *ops);
115
116         if (!module) {
117                 errno = EINVAL;
118                 return -1;
119         }
120
121         if (module->handle) {
122                 module_exit = dlsym(module->handle, UG_MODULE_EXIT_SYM);
123                 if (module_exit)
124                         module_exit(&module->ops);
125                 else
126                         _ERR("dlsym failed: %s\n", dlerror());
127
128                 dlclose(module->handle);
129         }
130
131         free(module);
132         return 0;
133 }