Merge "fix: use EINA_* booleans instread of TRUE/FALSE" into tizen
[platform/framework/web/wrt.git] / src / wrt-launchpad-daemon / include / launchpad_util.h
1 /*
2  * Copyright (c) 2013 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  * @file    launchpad_util.h
18  * @author  Tae-Jeong Lee (taejeong.lee@samsung.com)
19  * @version 0.1
20  * @brief   Api library to support launchpad operation.
21  */
22
23 #ifndef __LAUNCHPAD_UTIL_H_
24 #define __LAUNCHPAD_UTIL_H_
25
26 #include <aul.h>
27 #include <bundle.h>
28 #include <privilege-control.h>
29 #include <sys/prctl.h>
30
31 #include <tzplatform_config.h>
32
33 #include "config.h"
34 #include "gl.h"
35 #include "app_sock.h"
36 #include "menu_db_util.h"
37 #include "simple_util.h"
38 #include "access_control.h"
39
40 #define _static_ static inline
41 #define WRT_AUL_PR_NAME 16
42 #define PKG_ID_LENGTH   11
43 #define SDK_CODE_COVERAGE "CODE_COVERAGE"
44 #define SDK_DYNAMIC_ANALYSIS "DYNAMIC_ANALYSIS"
45 #define PATH_DA_SO tzplatform_mkpath(TZ_SDK_TOOLS,"da/da_probe.so")
46 #define PATH_DATA "/data"
47
48 // Prototype
49 _static_ char** __create_argc_argv(bundle * kb, int *margc);
50 _static_ void   __set_sdk_env(app_info_from_db* menu_info, char* str);
51 _static_ int    __parser(const char *arg, char *out, int out_size);
52 _static_ void   __modify_bundle(bundle * kb, int caller_pid, app_info_from_db * menu_info, int cmd);
53 _static_ void   __set_oom();
54 _static_ void   __set_env(app_info_from_db * menu_info, bundle * kb);
55
56 // Implementation
57 _static_ char** __create_argc_argv(bundle * kb, int *margc)
58 {
59     char **argv;
60     int argc;
61
62     argc = bundle_export_to_argv(kb, &argv);
63
64     *margc = argc;
65     return argv;
66 }
67
68 _static_ void __set_sdk_env(app_info_from_db* menu_info, char* str)
69 {
70     char buf[MAX_LOCAL_BUFSZ];
71     int ret;
72
73     _D("key : %s / value : %s", AUL_K_SDK, str);
74     /* http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html*/
75     /* GCOV_PREFIX contains the prefix to add to the absolute paths in the
76      *object file. */
77     /*          Prefix can be absolute, or relative. The default is no prefix.
78      * */
79     /* GCOV_PREFIX_STRIP indicates the how many initial directory names */
80     /*          to stripoff the hardwired absolute paths. Default value is 0. */
81     if (strncmp(str, SDK_CODE_COVERAGE, strlen(str)) == 0) {
82         snprintf(buf,
83                  MAX_LOCAL_BUFSZ,
84                  "%s/%s" PATH_DATA,
85                  tzplatform_getenv(TZ_USER_APP),_get_pkgname(menu_info));
86         ret = setenv("GCOV_PREFIX", buf, 1);
87         _D("GCOV_PREFIX : %d", ret);
88         ret = setenv("GCOV_PREFIX_STRIP", "4096", 1);
89         _D("GCOV_PREFIX_STRIP : %d", ret);
90     } else if (strncmp(str, SDK_DYNAMIC_ANALYSIS, strlen(str)) == 0) {
91         ret = setenv("LD_PRELOAD", PATH_DA_SO, 1);
92         _D("LD_PRELOAD : %d", ret);
93     }
94 }
95
96
97 /*
98  * Parsing original app path to retrieve default bundle
99  *
100  * -1 : Invalid sequence
101  * -2 : Buffer overflow
102  *
103  */
104 _static_ int __parser(const char *arg, char *out, int out_size)
105 {
106     register int i;
107     int state = 1;
108     char *start_out = out;
109
110     if (arg == NULL || out == NULL) {
111         /* Handles null buffer*/
112         return 0;
113     }
114
115     for (i = 0; out_size > 1; i++) {
116         switch (state) {
117         case 1:
118             switch (arg[i]) {
119             case ' ':
120             case '\t':
121                 state = 5;
122                 break;
123             case '\0':
124                 state = 7;
125                 break;
126             case '\"':
127                 state = 2;
128                 break;
129             case '\\':
130                 state = 4;
131                 break;
132             default:
133                 *out = arg[i];
134                 out++;
135                 out_size--;
136                 break;
137             }
138             break;
139         case 2:         /* escape start*/
140             switch (arg[i]) {
141             case '\0':
142                 state = 6;
143                 break;
144             case '\"':
145                 state = 1;
146                 break;
147             default:
148                 *out = arg[i];
149                 out++;
150                 out_size--;
151                 break;
152             }
153             break;
154         case 4:         /* character escape*/
155             if (arg[i] == '\0') {
156                 state = 6;
157             } else {
158                 *out = arg[i];
159                 out++;
160                 out_size--;
161                 state = 1;
162             }
163             break;
164         case 5:         /* token*/
165             if (out != start_out) {
166                 *out = '\0';
167                 out_size--;
168                 return i;
169             }
170             i--;
171             state = 1;
172             break;
173         case 6:
174             return -1;                  /* error*/
175         case 7:         /* terminate*/
176             *out = '\0';
177             out_size--;
178             return 0;
179         default:
180             state = 6;
181             break;              /* error*/
182         }
183     }
184
185     if (out_size == 1) {
186         *out = '\0';
187     }
188     /* Buffer overflow*/
189     return -2;
190 }
191
192
193 _static_ void __modify_bundle(bundle * kb, int caller_pid,
194                               app_info_from_db * menu_info, int cmd)
195 {
196     // warning: unused parameter
197     (void) caller_pid;
198
199     bundle_del(kb, AUL_K_PKG_NAME);
200     bundle_del(kb, AUL_K_EXEC);
201     bundle_del(kb, AUL_K_PACKAGETYPE);
202     bundle_del(kb, AUL_K_HWACC);
203
204     /* Parse app_path to retrieve default bundle*/
205     if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_OPEN || cmd ==
206         APP_RESUME)
207     {
208         char *ptr;
209         char exe[MAX_PATH_LEN];
210         int flag;
211
212         ptr = _get_original_app_path(menu_info);
213
214         flag = __parser(ptr, exe, sizeof(exe));
215         if (flag > 0) {
216             char key[256];
217             char value[256];
218
219             ptr += flag;
220             _D("parsing app_path: EXEC - %s\n", exe);
221
222             do {
223                 flag = __parser(ptr, key, sizeof(key));
224                 if (flag <= 0) {
225                     break;
226                 }
227                 ptr += flag;
228
229                 flag = __parser(ptr, value, sizeof(value));
230                 if (flag < 0) {
231                     break;
232                 }
233                 ptr += flag;
234
235                 /*bundle_del(kb, key);*/
236                 bundle_add(kb, key, value);
237             } while (flag > 0);
238         } else if (flag == 0) {
239             _D("parsing app_path: No arguments\n");
240         } else {
241             _D("parsing app_path: Invalid argument\n");
242         }
243     }
244 }
245
246
247 _static_ void __set_oom()
248 {
249     char buf[MAX_LOCAL_BUFSZ];
250     FILE *fp;
251
252     /* we should reset oomadj value as default because child
253      * inherits from parent oom_adj*/
254     snprintf(buf, MAX_LOCAL_BUFSZ, "/proc/%d/oom_adj", getpid());
255     fp = fopen(buf, "w");
256     if (fp == NULL) {
257         return;
258     }
259     fprintf(fp, "%d", -16);
260     fclose(fp);
261 }
262
263 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
264 {
265     const char *str;
266     const char **str_array;
267     int len;
268     int i;
269
270     setenv("PKG_NAME", _get_pkgname(menu_info), 1);
271
272     USE_ENGINE("gl")
273
274     str = bundle_get_val(kb, AUL_K_STARTTIME);
275     if (str != NULL) {
276         setenv("APP_START_TIME", str, 1);
277     }
278
279     if (bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
280         str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
281         if (str_array != NULL) {
282             for (i = 0; i < len; i++) {
283                 _D("index : [%d]", i);
284                 __set_sdk_env(menu_info, (char *)str_array[i]);
285             }
286         }
287     } else {
288         str = bundle_get_val(kb, AUL_K_SDK);
289         if (str != NULL) {
290             __set_sdk_env(menu_info, (char *)str);
291         }
292     }
293     if (menu_info->hwacc != NULL) {
294         setenv("HWACC", menu_info->hwacc, 1);
295     }
296 }
297
298 #endif // __LAUNCHPAD_UTIL_H_