tizen 2.3.1 release
[framework/appfw/aul-1.git] / feature / preexec.h
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@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 #ifdef PREEXEC_ACTIVATE
23
24 #include <dlfcn.h>
25 #include <glib.h>
26 #define PREEXEC_FILE SHARE_PREFIX"/preexec_list.txt"
27
28 static int preexec_initialized = 0;
29
30 GSList *preexec_list = NULL;
31
32 typedef struct _preexec_list_t {
33         char *pkg_type;
34         char *so_path;
35         int (*dl_do_pre_exe) (char *, char *);
36 } preexec_list_t;
37
38 static void __preexec_list_free()
39 {
40         GSList *iter = NULL;
41         preexec_list_t *type_t;
42
43         for (iter = preexec_list; iter != NULL; iter = g_slist_next(iter)) {
44                 type_t = iter->data;
45                 if (type_t) {
46                         if (type_t->pkg_type)
47                                 free(type_t->pkg_type);
48                         if (type_t->so_path)
49                                 free(type_t->so_path);
50                         free(type_t);
51                 }
52         }
53         g_slist_free(preexec_list);
54         preexec_initialized = 0;
55         return;
56 }
57
58 static inline void __preexec_init(int argc, char **argv)
59 {
60         void *handle = NULL;
61         FILE *preexec_file;
62         char *saveptr = NULL;
63         char line[MAX_LOCAL_BUFSZ];
64         char *type = NULL;
65         char *sopath = NULL;
66         char *symbol = NULL;
67         int (*func) (char *, char *) = NULL;
68         preexec_list_t *type_t = NULL;
69
70         preexec_file = fopen(PREEXEC_FILE, "rt");
71         if (preexec_file == NULL) {
72                 _E("no preexec\n");
73                 return;
74         }
75
76         _D("preexec start\n");
77
78         while (fgets(line, MAX_LOCAL_BUFSZ, preexec_file) > 0) {
79                 /* Parse each line */
80                 if (line[0] == '#' || line[0] == '\0')
81                         continue;
82
83                 type = strtok_r(line, ":\f\n\r\t\v ", &saveptr);
84                 if (type == NULL)
85                         continue;
86                 sopath = strtok_r(NULL, ",\f\n\r\t\v ", &saveptr);
87                 if (sopath == NULL)
88                         continue;
89                 symbol = strtok_r(NULL, ",\f\n\r\t\v ", &saveptr);
90                 if (symbol == NULL)
91                         continue;
92
93                 type_t = (preexec_list_t *) calloc(1, sizeof(preexec_list_t));
94                 if (type_t == NULL) {
95                         _E("no available memory\n");
96                         __preexec_list_free();
97                         fclose(preexec_file);
98                         return;
99                 }
100
101                 handle = dlopen(sopath, RTLD_NOW);
102                 if (handle == NULL) {
103                         free(type_t);
104                         continue;
105                 }
106                 _D("preexec %s %s# - handle : %x\n", type, sopath, handle);
107
108                 func = dlsym(handle, symbol);
109                 if (func == NULL) {
110                         _E("failed to get symbol type:%s path:%s\n",
111                            type, sopath);
112                         free(type_t);
113                         dlclose(handle);
114                         handle = NULL;
115                         continue;
116                 }
117
118                 type_t->pkg_type = strdup(type);
119                 if (type_t->pkg_type == NULL) {
120                         _E("no available memory\n");
121                         free(type_t);
122                         __preexec_list_free();
123                         fclose(preexec_file);
124                         return;
125                 }
126                 type_t->so_path = strdup(sopath);
127                 if (type_t->so_path == NULL) {
128                         _E("no available memory\n");
129                         free(type_t->pkg_type);
130                         free(type_t);
131                         __preexec_list_free();
132                         fclose(preexec_file);
133                         return;
134                 }
135                 type_t->dl_do_pre_exe = func;
136
137                 preexec_list = g_slist_append(preexec_list, (void *)type_t);
138         }
139
140         fclose(preexec_file);
141         preexec_initialized = 1;
142 }
143
144 static inline void __preexec_run(const char *pkg_type, const char *pkg_name,
145                                  const char *app_path)
146 {
147         GSList *iter = NULL;
148         preexec_list_t *type_t;
149
150         if (!preexec_initialized)
151                 return;
152
153         for (iter = preexec_list; iter != NULL; iter = g_slist_next(iter)) {
154                 type_t = iter->data;
155                 if (type_t) {
156                         if (!strcmp(pkg_type, type_t->pkg_type)) {
157                                 if (type_t->dl_do_pre_exe != NULL) {
158                                         type_t->dl_do_pre_exe((char *)pkg_name,
159                                                               (char *)app_path);
160                                         _D("called dl_do_pre_exe() type: %s",
161                                            pkg_type);
162                                 } else {
163                                         _E("no symbol for this type: %s",
164                                            pkg_type);
165                                 }
166                         }
167                 }
168         }
169
170 }
171
172 #else
173
174 static void __preexec_list_free()
175 {
176 }
177
178 static inline void __preexec_init(int argc, char **argv)
179 {
180 }
181
182 static inline void __preexec_run(const char *pkg_type, const char *pkg_name,
183                                  const char *app_path)
184 {
185 }
186
187 #endif