2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file launchpad_util.h
18 * @author Tae-Jeong Lee (taejeong.lee@samsung.com)
20 * @brief Api library to support launchpad operation.
23 #ifndef __LAUNCHPAD_UTIL_H_
24 #define __LAUNCHPAD_UTIL_H_
28 #include <privilege-control.h>
29 #include <sys/prctl.h>
34 #include "menu_db_util.h"
35 #include "simple_util.h"
36 #include "access_control.h"
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"
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);
56 _static_ char** __create_argc_argv(bundle * kb, int *margc)
61 argc = bundle_export_to_argv(kb, &argv);
67 _static_ void __set_sdk_env(app_info_from_db* menu_info, char* str)
69 char buf[MAX_LOCAL_BUFSZ];
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
76 /* Prefix can be absolute, or relative. The default is no prefix.
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) {
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);
97 * Parsing original app path to retrieve default bundle
99 * -1 : Invalid sequence
100 * -2 : Buffer overflow
103 _static_ int __parser(const char *arg, char *out, int out_size)
107 char *start_out = out;
109 if (arg == NULL || out == NULL) {
110 /* Handles null buffer*/
114 for (i = 0; out_size > 1; i++) {
138 case 2: /* escape start*/
153 case 4: /* character escape*/
154 if (arg[i] == '\0') {
164 if (out != start_out) {
173 return -1; /* error*/
174 case 7: /* terminate*/
192 _static_ void __modify_bundle(bundle * kb, int caller_pid,
193 app_info_from_db * menu_info, int cmd)
195 // warning: unused parameter
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);
203 /* Parse app_path to retrieve default bundle*/
204 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_OPEN || cmd ==
208 char exe[MAX_PATH_LEN];
211 ptr = _get_original_app_path(menu_info);
213 flag = __parser(ptr, exe, sizeof(exe));
219 _D("parsing app_path: EXEC - %s\n", exe);
222 flag = __parser(ptr, key, sizeof(key));
228 flag = __parser(ptr, value, sizeof(value));
234 /*bundle_del(kb, key);*/
235 bundle_add(kb, key, value);
237 } else if (flag == 0) {
238 _D("parsing app_path: No arguments\n");
240 _D("parsing app_path: Invalid argument\n");
246 _static_ void __set_oom()
248 char buf[MAX_LOCAL_BUFSZ];
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");
258 fprintf(fp, "%d", -16);
262 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
265 const char **str_array;
269 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
273 str = bundle_get_val(kb, AUL_K_STARTTIME);
275 setenv("APP_START_TIME", str, 1);
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]);
287 str = bundle_get_val(kb, AUL_K_SDK);
289 __set_sdk_env(menu_info, (char *)str);
292 if (menu_info->hwacc != NULL) {
293 setenv("HWACC", menu_info->hwacc, 1);
297 #endif // __LAUNCHPAD_UTIL_H_