WRT Launchpad Daemon Feature
[platform/framework/web/wrt.git] / src / wrt-launchpad-daemon / 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         return;
55 }
56
57 static inline void __preexec_init(int argc, char **argv)
58 {
59         void *handle = NULL;
60         FILE *preexec_file;
61         char *saveptr = NULL;
62         char line[MAX_LOCAL_BUFSZ];
63         char *type = NULL;
64         char *sopath = NULL;
65         char *symbol = NULL;
66         int (*func) (char *, char *) = NULL;
67         preexec_list_t *type_t = NULL;
68
69         // warning: unused parameter
70         argc = argc;
71         argv = argv;
72
73         preexec_file = fopen(PREEXEC_FILE, "rt");
74         if (preexec_file == NULL) {
75                 _E("no preexec\n");
76                 return;
77         }
78
79         _D("preexec start\n");
80
81         while (fgets(line, MAX_LOCAL_BUFSZ, preexec_file) > (char*)0) {
82                 /* Parse each line */
83                 if (line[0] == '#' || line[0] == '\0')
84                         continue;
85
86                 type = strtok_r(line, ":\f\n\r\t\v ", &saveptr);
87                 if (type == NULL)
88                         continue;
89                 sopath = strtok_r(NULL, ",\f\n\r\t\v ", &saveptr);
90                 if (sopath == NULL)
91                         continue;
92                 symbol = strtok_r(NULL, ",\f\n\r\t\v ", &saveptr);
93                 if (symbol == NULL)
94                         continue;
95
96                 type_t = (preexec_list_t *) calloc(1, sizeof(preexec_list_t));
97                 if (type_t == NULL) {
98                         _E("no available memory\n");
99                         __preexec_list_free();
100                         return;
101                 }
102
103                 handle = dlopen(sopath, RTLD_NOW);
104                 if (handle == NULL) {
105                         free(type_t);
106                         continue;
107                 }
108                 _D("preexec %s %s# - handle : %x\n", type, sopath, handle);
109
110                 func = dlsym(handle, symbol);
111                 if (func == NULL) {
112                         _E("failed to get symbol type:%s path:%s\n",
113                            type, sopath);
114                         free(type_t);
115                         dlclose(handle);
116                         continue;
117                 }
118
119                 type_t->pkg_type = strdup(type);
120                 if (type_t->pkg_type == NULL) {
121                         _E("no available memory\n");
122                         free(type_t);
123                         __preexec_list_free();
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                         return;
133                 }
134                 type_t->dl_do_pre_exe = func;
135
136                 preexec_list = g_slist_append(preexec_list, (void *)type_t);
137         }
138
139         fclose(preexec_file);
140         preexec_initialized = 1;
141 }
142
143 static inline void __preexec_run(const char *pkg_type, const char *pkg_name,
144                                  const char *app_path)
145 {
146         GSList *iter = NULL;
147         preexec_list_t *type_t;
148
149         if (!preexec_initialized)
150                 return;
151
152         for (iter = preexec_list; iter != NULL; iter = g_slist_next(iter)) {
153                 type_t = iter->data;
154                 if (type_t) {
155                         if (!strcmp(pkg_type, type_t->pkg_type)) {
156                                 if (type_t->dl_do_pre_exe != NULL) {
157                                         type_t->dl_do_pre_exe((char *)pkg_name,
158                                                               (char *)app_path);
159                                         _D("called dl_do_pre_exe() type: %s",
160                                            pkg_type);
161                                 } else {
162                                         _E("no symbol for this type: %s",
163                                            pkg_type);
164                                 }
165                         }
166                 }
167         }
168
169 }
170
171 #else
172
173 static inline void __preexec_init(int argc, char **argv)
174 {
175 }
176
177 static inline void __preexec_run(const char *pkg_type, const char *pkg_name,
178                                  const char *app_path)
179 {
180 }
181
182 #endif