WRT Launchpad Daemon Feature
[platform/framework/web/wrt.git] / src / wrt-launchpad-daemon / legacy / preload.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
23 #ifdef PRELOAD_ACTIVATE 
24
25 #include <dlfcn.h>
26 #define PRELOAD_FILE        SHARE_PREFIX"/preload_list.txt"
27 #define PRELOAD_FILE_WRT    SHARE_PREFIX"/preload_list_wrt.txt"
28
29 #define EFL_PREINIT_FUNC        "elm_quicklaunch_init"
30 #define EFL_SHUTDOWN_FUNC       "elm_quicklaunch_shutdown"
31
32 static int preload_initialized = 0;
33 static int g_argc;
34 static char **g_argv;
35 static size_t max_cmdline_size = 0;
36
37 static int (*dl_einit) () = NULL;
38 static int (*dl_efini) () = NULL;
39
40 static inline void __preload_init(int argc, char **argv)
41 {
42         void *handle = NULL;
43         char soname[MAX_LOCAL_BUFSZ];
44         FILE *preload_list;
45         int (*func)() = NULL;
46         int i;
47
48         g_argc = argc;
49         g_argv = argv;
50         for (i = 0; i < argc; i++) {
51                 max_cmdline_size += (strlen(argv[i]) + 1);
52         }
53         _D("max_cmdline_size = %d", max_cmdline_size);
54
55         preload_list = fopen(PRELOAD_FILE, "rt");
56         if (preload_list == NULL) {
57                 _E("no preload\n");
58                 return;
59         }
60
61         while (fgets(soname, MAX_LOCAL_BUFSZ, preload_list) > (char*)0) {
62                 soname[strlen(soname) - 1] = 0;
63                 handle = dlopen(soname, RTLD_NOW);
64                 if (handle == NULL)
65                         continue;
66                 _D("preload %s# - handle : %x\n", soname, handle);
67
68                 func = dlsym(handle, EFL_PREINIT_FUNC);
69                 if (func != NULL) {
70                         _D("get pre-initialization function\n");
71                         dl_einit = func;
72                         func = dlsym(handle, EFL_SHUTDOWN_FUNC);
73                         if (func != NULL) {
74                                 _D("get shutdown function\n");
75                                 dl_efini = func;
76                         }
77                 }
78         }
79
80         fclose(preload_list);
81         preload_initialized = 1;
82 }
83
84 static inline int preinit_init()
85 {
86         if (dl_einit != NULL)
87                 dl_einit(0, NULL);
88         _D("pre-initialzation on");
89         return 0;
90 }
91
92 static inline int preinit_fini()
93 {
94         if (dl_efini != NULL)
95                 dl_efini();
96         _D("pre-initialization off");
97         return 0;
98 }
99
100 /* TODO : how to set cmdline gracefully ?? */
101 static inline int __change_cmdline(char *cmdline)
102 {
103         if (strlen(cmdline) > max_cmdline_size + 1) {
104                 _E("cmdline exceed max size : %d", max_cmdline_size);
105                 return -1;
106         }
107
108         memset(g_argv[0], '\0', max_cmdline_size);
109         snprintf(g_argv[0], max_cmdline_size, "%s", cmdline);
110
111         return 0;
112 }
113
114 static inline void __preload_exec(int argc, char **argv)
115 {
116         void *handle = NULL;
117         int (*dl_main) (int, char **);
118
119         if (!preload_initialized)
120                 return;
121
122         handle = dlopen(argv[0], RTLD_LAZY | RTLD_GLOBAL);
123         if (handle == NULL) {
124                 return;
125         }
126
127         dl_main = dlsym(handle, "main");
128         if (dl_main != NULL) {
129                 if (__change_cmdline(argv[0]) < 0) {
130                         _E("change cmdline fail");
131                         return;
132                 }
133                 dl_main(argc, argv);
134         } else {
135                 _E("dlsym not founded. bad preloaded app - check fpie pie");
136         }
137
138         exit(0);
139 }
140
141 static inline void __preload_init_for_wrt()
142 {
143         void *handle = NULL;
144         char soname[MAX_LOCAL_BUFSZ];
145         FILE *preload_list;
146
147         preload_list = fopen(PRELOAD_FILE_WRT, "rt");
148         if (preload_list == NULL) {
149                 _E("no wrt preload\n");
150                 return;
151         }
152
153         while (fgets(soname, MAX_LOCAL_BUFSZ, preload_list) > (char*)0) {
154                 soname[strlen(soname) - 1] = 0;
155                 handle = dlopen(soname, RTLD_NOW|RTLD_GLOBAL);
156                 if (handle == NULL)
157                         continue;
158                 _D("preload %s# - handle : %x\n", soname, handle);
159         }
160
161         fclose(preload_list);
162 }
163
164 #else
165
166 static inline void __preload_init(int argc, char **argv);
167 static inline void __preload_exec(int argc, char **argv);
168 static inline void __preload_init_for_wrt();
169
170 #endif
171