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);
54 _static_ int __wrt_prepare_exec(const char *pkg_name, const char *app_path, app_info_from_db * menu_info, bundle * kb);
55 _static_ void __wrt_launchpad_main_loop(app_pkt_t* pkt, char* out_app_path, int* out_argc, char ***out_argv);
59 _static_ char** __create_argc_argv(bundle * kb, int *margc)
64 argc = bundle_export_to_argv(kb, &argv);
70 _static_ void __set_sdk_env(app_info_from_db* menu_info, char* str)
72 char buf[MAX_LOCAL_BUFSZ];
75 _D("key : %s / value : %s", AUL_K_SDK, str);
76 /* http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html*/
77 /* GCOV_PREFIX contains the prefix to add to the absolute paths in the
79 /* Prefix can be absolute, or relative. The default is no prefix.
81 /* GCOV_PREFIX_STRIP indicates the how many initial directory names */
82 /* to stripoff the hardwired absolute paths. Default value is 0. */
83 if (strncmp(str, SDK_CODE_COVERAGE, strlen(str)) == 0) {
86 PATH_APP_ROOT "/%s"PATH_DATA,
87 _get_pkgname(menu_info));
88 ret = setenv("GCOV_PREFIX", buf, 1);
89 _D("GCOV_PREFIX : %d", ret);
90 ret = setenv("GCOV_PREFIX_STRIP", "4096", 1);
91 _D("GCOV_PREFIX_STRIP : %d", ret);
92 } else if (strncmp(str, SDK_DYNAMIC_ANALYSIS, strlen(str)) == 0) {
93 ret = setenv("LD_PRELOAD", PATH_DA_SO, 1);
94 _D("LD_PRELOAD : %d", ret);
100 * Parsing original app path to retrieve default bundle
102 * -1 : Invalid sequence
103 * -2 : Buffer overflow
106 _static_ int __parser(const char *arg, char *out, int out_size)
110 char *start_out = out;
112 if (arg == NULL || out == NULL) {
113 /* Handles null buffer*/
117 for (i = 0; out_size > 1; i++) {
141 case 2: /* escape start*/
156 case 4: /* character escape*/
157 if (arg[i] == '\0') {
167 if (out != start_out) {
176 return -1; /* error*/
177 case 7: /* terminate*/
195 _static_ void __modify_bundle(bundle * kb, int caller_pid,
196 app_info_from_db * menu_info, int cmd)
198 // warning: unused parameter
199 caller_pid = caller_pid;
201 bundle_del(kb, AUL_K_PKG_NAME);
202 bundle_del(kb, AUL_K_EXEC);
203 bundle_del(kb, AUL_K_PACKAGETYPE);
204 bundle_del(kb, AUL_K_HWACC);
206 /* Parse app_path to retrieve default bundle*/
207 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_OPEN || cmd ==
211 char exe[MAX_PATH_LEN];
214 ptr = _get_original_app_path(menu_info);
216 flag = __parser(ptr, exe, sizeof(exe));
222 _D("parsing app_path: EXEC - %s\n", exe);
225 flag = __parser(ptr, key, sizeof(key));
231 flag = __parser(ptr, value, sizeof(value));
237 /*bundle_del(kb, key);*/
238 bundle_add(kb, key, value);
240 } else if (flag == 0) {
241 _D("parsing app_path: No arguments\n");
243 _D("parsing app_path: Invalid argument\n");
249 _static_ void __set_oom()
251 char buf[MAX_LOCAL_BUFSZ];
254 /* we should reset oomadj value as default because child
255 * inherits from parent oom_adj*/
256 snprintf(buf, MAX_LOCAL_BUFSZ, "/proc/%d/oom_adj", getpid());
257 fp = fopen(buf, "w");
261 fprintf(fp, "%d", -16);
265 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
268 const char **str_array;
272 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
276 str = bundle_get_val(kb, AUL_K_STARTTIME);
278 setenv("APP_START_TIME", str, 1);
281 if (bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
282 str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
283 if (str_array != NULL) {
284 for (i = 0; i < len; i++) {
285 _D("index : [%d]", i);
286 __set_sdk_env(menu_info, (char *)str_array[i]);
290 str = bundle_get_val(kb, AUL_K_SDK);
292 __set_sdk_env(menu_info, (char *)str);
295 if (menu_info->hwacc != NULL) {
296 setenv("HWACC", menu_info->hwacc, 1);
300 _static_ int __wrt_prepare_exec(const char *pkg_name,
301 const char *app_path, app_info_from_db * menu_info,
304 const char *file_name;
305 char process_name[WRT_AUL_PR_NAME];
308 char pkg_id[PKG_ID_LENGTH];
309 memset(pkg_id, '\0', PKG_ID_LENGTH);
310 snprintf(pkg_id, PKG_ID_LENGTH, "%s", pkg_name);
312 if (__set_access(pkg_id, menu_info->pkg_type, app_path) < 0) {
313 _D("fail to set privileges - check your package's credential\n");
317 /* SET PROCESS NAME*/
318 if (app_path == NULL) {
319 _D("app_path should not be NULL - check menu db");
322 file_name = strrchr(app_path, '/') + 1;
323 if (file_name == NULL) {
324 _D("can't locate file name to execute");
327 memset(process_name, '\0', WRT_AUL_PR_NAME);
328 snprintf(process_name, WRT_AUL_PR_NAME, "%s", file_name);
329 prctl(PR_SET_NAME, process_name);
332 __set_env(menu_info, kb);
337 _static_ void __wrt_launchpad_main_loop(app_pkt_t* pkt, char* out_app_path, int* out_argc, char ***out_argv)
341 app_info_from_db *menu_info = NULL;
343 const char *pkg_name = NULL;
344 const char *app_path = NULL;
347 kb = bundle_decode(pkt->data, pkt->len);
349 _E("bundle decode error");
353 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
354 _D("pkg name : %s", pkg_name);
356 menu_info = _get_app_info_from_bundle_by_pkgname(pkg_name, kb);
357 if (menu_info == NULL) {
358 _D("such pkg no found");
362 app_path = _get_app_path(menu_info);
364 if (app_path == NULL) {
365 _E("app_path is NULL");
369 if (app_path[0] != '/') {
370 _E("app_path is not absolute path");
374 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
375 pkg_name = _get_pkgname(menu_info);
376 _D("pkg name : %s", pkg_name);
378 __wrt_prepare_exec(pkg_name, app_path, menu_info, kb);
380 if (out_app_path != NULL && out_argc != NULL && out_argv != NULL)
384 sprintf(out_app_path, "%s", app_path);
386 *out_argv = __create_argc_argv(kb, out_argc);
387 (*out_argv)[0] = out_app_path;
389 for (i = 0; i < *out_argc; i++)
391 _D("input argument %d : %s##", i, (*out_argv)[i]);
401 if (menu_info != NULL) {
402 _free_app_info_from_db(menu_info);
411 #endif // __LAUNCHPAD_UTIL_H_