Source code formating unification
[platform/framework/web/wrt.git] / src / wrt-launchpad-daemon / feature / preexec.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #ifdef PREEXEC_ACTIVATE
18
19 #include <dlfcn.h>
20 #include <glib.h>
21 #define PREEXEC_FILE SHARE_PREFIX "/preexec_list.txt"
22
23 static int preexec_initialized = 0;
24
25 GSList *preexec_list = NULL;
26
27 typedef struct _preexec_list_t {
28     char *pkg_type;
29     char *so_path;
30     int (*dl_do_pre_exe)(char *, char *);
31 } preexec_list_t;
32
33 static void __preexec_list_free()
34 {
35     GSList *iter = NULL;
36     preexec_list_t *type_t;
37
38     for (iter = preexec_list; iter != NULL; iter = g_slist_next(iter)) {
39         type_t = iter->data;
40         if (type_t) {
41             if (type_t->pkg_type) {
42                 free(type_t->pkg_type);
43             }
44             if (type_t->so_path) {
45                 free(type_t->so_path);
46             }
47             free(type_t);
48         }
49     }
50     g_slist_free(preexec_list);
51     return;
52 }
53
54 static inline void __preexec_init(int argc, char **argv)
55 {
56     void *handle = NULL;
57     FILE *preexec_file;
58     char *saveptr = NULL;
59     char line[MAX_LOCAL_BUFSZ] = { 0, };
60     char *type = NULL;
61     char *sopath = NULL;
62     char *symbol = NULL;
63     int (*func)(char *, char *) = NULL;
64     preexec_list_t *type_t = NULL;
65
66     // warning: unused parameter
67     argc = argc;
68     argv = argv;
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) > (char*)0) {
79         /* Parse each line */
80         if (line[0] == '#' || line[0] == '\0') {
81             continue;
82         }
83
84         type = strtok_r(line, ":\f\n\r\t\v ", &saveptr);
85         if (type == NULL) {
86             continue;
87         }
88         sopath = strtok_r(NULL, ",\f\n\r\t\v ", &saveptr);
89         if (sopath == NULL) {
90             continue;
91         }
92         symbol = strtok_r(NULL, ",\f\n\r\t\v ", &saveptr);
93         if (symbol == NULL) {
94             continue;
95         }
96
97         type_t = (preexec_list_t *) calloc(1, sizeof(preexec_list_t));
98         if (type_t == NULL) {
99             _E("no available memory\n");
100             __preexec_list_free();
101             fclose(preexec_file);
102             return;
103         }
104
105         handle = dlopen(sopath, RTLD_NOW);
106         if (handle == NULL) {
107             free(type_t);
108             continue;
109         }
110         _D("preexec %s %s# - handle : %x\n", type, sopath, handle);
111
112         func = dlsym(handle, symbol);
113         if (func == NULL) {
114             _E("failed to get symbol type:%s path:%s\n",
115                type, sopath);
116             free(type_t);
117             dlclose(handle);
118             handle = NULL;
119             continue;
120         }
121
122         type_t->pkg_type = strdup(type);
123         if (type_t->pkg_type == NULL) {
124             _E("no available memory\n");
125             free(type_t);
126             __preexec_list_free();
127             fclose(preexec_file);
128             return;
129         }
130         type_t->so_path = strdup(sopath);
131         if (type_t->so_path == NULL) {
132             _E("no available memory\n");
133             free(type_t->pkg_type);
134             free(type_t);
135             __preexec_list_free();
136             fclose(preexec_file);
137             return;
138         }
139         type_t->dl_do_pre_exe = func;
140
141         preexec_list = g_slist_append(preexec_list, (void *)type_t);
142     }
143
144     fclose(preexec_file);
145     preexec_initialized = 1;
146 }
147
148 static inline void __preexec_run(const char *pkg_type, const char *pkg_name,
149                                  const char *app_path)
150 {
151     GSList *iter = NULL;
152     preexec_list_t *type_t;
153
154     if (!preexec_initialized) {
155         return;
156     }
157
158     for (iter = preexec_list; iter != NULL; iter = g_slist_next(iter)) {
159         type_t = iter->data;
160         if (type_t) {
161             if (!strcmp(pkg_type, type_t->pkg_type)) {
162                 if (type_t->dl_do_pre_exe != NULL) {
163                     type_t->dl_do_pre_exe((char *)pkg_name,
164                                           (char *)app_path);
165                     _D("called dl_do_pre_exe() type: %s",
166                        pkg_type);
167                 } else {
168                     _E("no symbol for this type: %s",
169                        pkg_type);
170                 }
171             }
172         }
173     }
174 }
175
176 #else
177
178 static inline void __preexec_init(int argc, char **argv)
179 {}
180
181 static inline void __preexec_run(const char *pkg_type, const char *pkg_name,
182                                  const char *app_path)
183 {}
184
185 #endif