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