4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * simple AUL daemon - launchpad
31 #include <sys/types.h>
38 #include <sys/prctl.h>
46 #include "menu_db_util.h"
47 #include "simple_util.h"
48 #include "access_control.h"
61 #define _static_ static inline
63 #define SQLITE_FLUSH_MAX (1048576) /* (1024*1024) */
64 #define AUL_POLL_CNT 15
65 #define AUL_PR_NAME 16
66 #define PATH_APP_ROOT "/opt/usr/apps"
67 #define PATH_DATA "/data"
68 #define SDK_CODE_COVERAGE "CODE_COVERAGE"
69 #define SDK_DYNAMIC_ANALYSIS "DYNAMIC_ANALYSIS"
70 #define PATH_DA_SO "/usr/lib/da_probe_osp.so"
73 static char *launchpad_cmdline;
74 static int initialized = 0;
77 _static_ void __set_oom();
78 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb);
79 _static_ int __prepare_exec(const char *pkg_name,
80 const char *app_path, app_info_from_db * menu_info,
82 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb);
83 _static_ char **__create_argc_argv(bundle * kb, int *margc);
84 _static_ int __normal_fork_exec(int argc, char **argv);
85 _static_ void __real_launch(const char *app_path, bundle * kb);
86 static inline int __parser(const char *arg, char *out, int out_size);
87 _static_ void __modify_bundle(bundle * kb, int caller_pid,
88 app_info_from_db * menu_info, int cmd);
89 _static_ int __child_raise_win_by_x(int pid, void *priv);
90 _static_ int __raise_win_by_x(int pid);
91 _static_ int __send_to_sigkill(int pid);
92 _static_ int __term_app(int pid);
93 _static_ int __resume_app(int pid);
94 _static_ void __real_send(int clifd, int ret);
95 _static_ void __send_result_to_caller(int clifd, int ret);
96 _static_ void __launchpad_main_loop(int main_fd);
97 _static_ int __launchpad_pre_init(int argc, char **argv);
98 _static_ int __launchpad_post_init();
100 extern ail_error_e ail_db_close(void);
104 _static_ void __set_oom()
106 char buf[MAX_LOCAL_BUFSZ];
109 /* we should reset oomadj value as default because child
110 inherits from parent oom_adj*/
111 snprintf(buf, MAX_LOCAL_BUFSZ, "/proc/%d/oom_adj", getpid());
112 fp = fopen(buf, "w");
115 fprintf(fp, "%d", -16);
119 _static_ void __set_sdk_env(app_info_from_db* menu_info, char* str) {
120 char buf[MAX_LOCAL_BUFSZ];
123 _D("key : %s / value : %s", AUL_K_SDK, str);
124 /* http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html*/
125 /* GCOV_PREFIX contains the prefix to add to the absolute paths in the object file. */
126 /* Prefix can be absolute, or relative. The default is no prefix. */
127 /* GCOV_PREFIX_STRIP indicates the how many initial directory names */
128 /* to stripoff the hardwired absolute paths. Default value is 0. */
129 if (strncmp(str, SDK_CODE_COVERAGE, strlen(str)) == 0) {
130 snprintf(buf, MAX_LOCAL_BUFSZ, PATH_APP_ROOT"/%s"PATH_DATA, _get_pkgname(menu_info));
131 ret = setenv("GCOV_PREFIX", buf, 1);
132 _D("GCOV_PREFIX : %d", ret);
133 ret = setenv("GCOV_PREFIX_STRIP", "4096", 1);
134 _D("GCOV_PREFIX_STRIP : %d", ret);
135 } else if (strncmp(str, SDK_DYNAMIC_ANALYSIS, strlen(str)) == 0) {
136 ret = setenv("LD_PRELOAD", PATH_DA_SO, 1);
137 _D("LD_PRELOAD : %d", ret);
142 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
145 const char **str_array;
149 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
153 str = bundle_get_val(kb, AUL_K_STARTTIME);
155 setenv("APP_START_TIME", str, 1);
157 if(bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
158 str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
159 if(str_array != NULL) {
160 for (i = 0; i < len; i++) {
161 _D("index : [%d]", i);
162 __set_sdk_env(menu_info, (char *)str_array[i]);
166 str = bundle_get_val(kb, AUL_K_SDK);
168 __set_sdk_env(menu_info, (char *)str);
171 if (menu_info->hwacc != NULL)
172 setenv("HWACC", menu_info->hwacc, 1);
175 _static_ int __prepare_exec(const char *pkg_name,
176 const char *app_path, app_info_from_db * menu_info,
180 char process_name[AUL_PR_NAME];
183 /* Set new session ID & new process group ID*/
184 /* In linux, child can set new session ID without check permission */
185 /* TODO : should be add to check permission in the kernel*/
188 __preexec_run(menu_info->pkg_type, pkg_name, app_path);
194 if(bundle_get_val(kb, AUL_K_PRIVACY_APPID) == NULL) {
195 _D("pkg_name : %s / pkg_type : %s / app_path : %s ", pkg_name, menu_info->pkg_type, app_path);
196 if ((ret = __set_access(pkg_name, menu_info->pkg_type, app_path)) < 0) {
197 _D("fail to set privileges - check your package's credential : %d\n", ret);
201 /* SET DUMPABLE - for coredump*/
202 prctl(PR_SET_DUMPABLE, 1);
204 /* SET PROCESS NAME*/
205 if (app_path == NULL) {
206 _D("app_path should not be NULL - check menu db");
209 file_name = strrchr(app_path, '/') + 1;
210 if (file_name == NULL) {
211 _D("can't locate file name to execute");
214 memset(process_name, '\0', AUL_PR_NAME);
215 snprintf(process_name, AUL_PR_NAME, "%s", file_name);
216 prctl(PR_SET_NAME, process_name);
219 __set_env(menu_info, kb);
224 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb)
230 bundle_encode(kb, &kb_data, &datalen);
231 if ((ret = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
232 _E("error request fake launch - error code = %d", ret);
237 _static_ char **__create_argc_argv(bundle * kb, int *margc)
242 argc = bundle_export_to_argv(kb, &argv);
248 _static_ int __normal_fork_exec(int argc, char **argv)
250 _D("start real fork and exec\n");
252 if (execv(argv[0], argv) < 0) { /* Flawfinder: ignore */
254 _E("such a file is no executable - %s", argv[0]);
256 _E("unknown executable error - %s", argv[0]);
263 _static_ void __real_launch(const char *app_path, bundle * kb)
270 app_argv = __create_argc_argv(kb, &app_argc);
271 app_argv[0] = strdup(app_path);
273 for (i = 0; i < app_argc; i++)
274 _D("input argument %d : %s##", i, app_argv[i]);
276 PERF("setup argument done");
277 _D("lock up test log(no error) : setup argument done");
279 /* Temporary log: launch time checking */
280 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:launchpad:done]", app_path);
282 str = bundle_get_val(kb, AUL_K_SDK);
283 if ( !(str && strncmp(str, SDK_DYNAMIC_ANALYSIS, strlen(str))==0) ) {
284 __preload_exec(app_argc, app_argv);
287 __normal_fork_exec(app_argc, app_argv);
292 * Parsing original app path to retrieve default bundle
294 * -1 : Invalid sequence
295 * -2 : Buffer overflow
298 static inline int __parser(const char *arg, char *out, int out_size)
302 char *start_out = out;
304 if (arg == NULL || out == NULL) {
305 /* Handles null buffer*/
309 for (i = 0; out_size > 1; i++) {
333 case 2: /* escape start*/
348 case 4: /* character escape*/
349 if (arg[i] == '\0') {
359 if (out != start_out) {
368 return -1; /* error*/
369 case 7: /* terminate*/
386 _static_ void __modify_bundle(bundle * kb, int caller_pid,
387 app_info_from_db * menu_info, int cmd)
389 bundle_del(kb, AUL_K_PKG_NAME);
390 bundle_del(kb, AUL_K_EXEC);
391 bundle_del(kb, AUL_K_PACKAGETYPE);
392 bundle_del(kb, AUL_K_HWACC);
394 /* Parse app_path to retrieve default bundle*/
395 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_OPEN || cmd == APP_RESUME) {
397 char exe[MAX_PATH_LEN];
400 ptr = _get_original_app_path(menu_info);
402 flag = __parser(ptr, exe, sizeof(exe));
408 _D("parsing app_path: EXEC - %s\n", exe);
411 flag = __parser(ptr, key, sizeof(key));
416 flag = __parser(ptr, value, sizeof(value));
421 /*bundle_del(kb, key);*/
422 bundle_add(kb, key, value);
424 } else if (flag == 0) {
425 _D("parsing app_path: No arguments\n");
427 _D("parsing app_path: Invalid argument\n");
432 _static_ int __child_raise_win_by_x(int pid, void *priv)
434 return x_util_raise_win(pid);
437 _static_ int __raise_win_by_x(int pid)
440 if (x_util_raise_win(pid) == 0)
443 /* support app launched by shell script*/
445 _D("X raise failed. try to find first child & raise it - c:%d p:%d\n",
450 if (__proc_iter_pgid(pgid, __child_raise_win_by_x, NULL) < 0)
456 _static_ int __send_to_sigkill(int pid)
464 if (killpg(pgid, SIGKILL) < 0)
470 _static_ int __term_app(int pid)
474 (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int)) < 0) {
475 _D("terminate packet send error - use SIGKILL");
476 if (__send_to_sigkill(pid) < 0) {
477 _E("fail to killing - %d\n", pid);
485 _static_ int __resume_app(int pid)
490 __app_send_raw(pid, APP_RESUME_BY_PID, (unsigned char *)&dummy,
493 _E("resume packet timeout error");
495 _D("resume packet send error - use raise win");
496 if (__raise_win_by_x(pid) < 0) {
497 _E("raise failed - %d resume fail\n", pid);
498 _E("we will term the app - %d\n", pid);
499 __send_to_sigkill(pid);
509 static int __get_caller_pid(bundle *kb)
514 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
518 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
530 _static_ int __foward_cmd(int cmd, bundle *kb, int cr_pid)
533 char tmp_pid[MAX_PID_STR_BUFSZ];
538 if ((pid = __get_caller_pid(kb)) < 0)
541 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", cr_pid);
543 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
545 bundle_encode(kb, &kb_data, &datalen);
546 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
554 _static_ void __real_send(int clifd, int ret)
556 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
557 if (errno == EPIPE) {
558 _E("send failed due to EPIPE.\n");
560 _E("send fail to client");
566 _static_ void __send_result_to_caller(int clifd, int ret)
570 int cmdline_changed = 0;
571 int cmdline_exist = 0;
577 __real_send(clifd, ret);
580 /* check normally was launched?*/
583 cmdline = __proc_get_cmdline_bypid(ret);
584 if (cmdline == NULL) {
585 _E("error founded when being launched with %d", ret);
587 } else if (strcmp(cmdline, launchpad_cmdline)) {
596 _D("-- now wait to change cmdline --");
597 usleep(50 * 1000); /* 50ms sleep*/
599 } while (wait_count <= 20); /* max 50*20ms will be sleep*/
601 if ((!cmdline_exist) && (!cmdline_changed)) {
602 __real_send(clifd, -1); /* abnormally launched*/
606 if (!cmdline_changed)
607 _E("process launched, but cmdline not changed");
609 __real_send(clifd, ret);
613 static app_info_from_db *_get_app_info_from_bundle_by_pkgname(
614 const char *pkgname, bundle *kb)
616 app_info_from_db *menu_info;
618 menu_info = calloc(1, sizeof(app_info_from_db));
619 if (menu_info == NULL) {
623 menu_info->pkg_name = strdup(pkgname);
624 menu_info->app_path = strdup(bundle_get_val(kb, AUL_K_EXEC));
625 if (menu_info->app_path != NULL)
626 menu_info->original_app_path = strdup(menu_info->app_path);
627 menu_info->pkg_type = strdup(bundle_get_val(kb, AUL_K_PACKAGETYPE));
628 menu_info->hwacc = strdup(bundle_get_val(kb, AUL_K_HWACC));
630 if (!_get_app_path(menu_info)) {
631 _free_app_info_from_db(menu_info);
638 _static_ void __launchpad_main_loop(int main_fd)
641 app_pkt_t *pkt = NULL;
642 app_info_from_db *menu_info = NULL;
644 const char *pkg_name = NULL;
645 const char *app_path = NULL;
649 int is_real_launch = 0;
651 char sock_path[UNIX_PATH_MAX] = {0,};
653 pkt = __app_recv_raw(main_fd, &clifd, &cr);
655 _D("packet is NULL");
659 kb = bundle_decode(pkt->data, pkt->len);
661 _D("bundle decode error");
666 PERF("packet processing start");
668 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
669 _D("pkg name : %s\n", pkg_name);
671 menu_info = _get_app_info_from_bundle_by_pkgname(pkg_name, kb);
672 if (menu_info == NULL) {
673 _D("such pkg no found");
677 app_path = _get_app_path(menu_info);
678 if(app_path == NULL) {
679 _E("app_path is NULL");
682 if (app_path[0] != '/') {
683 _D("app_path is not absolute path");
687 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
688 pkg_name = _get_pkgname(menu_info);
690 PERF("get package information & modify bundle done");
696 _D("lock up test log(no error) : fork done");
700 __signal_unset_sigchld();
703 snprintf(sock_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, getpid());
706 PERF("prepare exec - first done");
707 _D("lock up test log(no error) : prepare exec - first done");
709 if (__prepare_exec(pkg_name, app_path,
710 menu_info, kb) < 0) {
711 _E("preparing work fail to launch - "
712 "can not launch %s\n", pkg_name);
716 PERF("prepare exec - second done");
717 _D("lock up test log(no error) : prepare exec - second done");
719 __real_launch(app_path, kb);
723 _D("==> real launch pid : %d %s\n", pid, app_path);
728 __send_result_to_caller(clifd, pid);
731 if (is_real_launch) {
733 __signal_block_sigchld();
734 __send_app_launch_signal(pid);
735 __signal_unblock_sigchld();
739 if (menu_info != NULL)
740 _free_app_info_from_db(menu_info);
747 /* Active Flusing for Daemon */
748 if (initialized > AUL_POLL_CNT) {
749 sqlite3_release_memory(SQLITE_FLUSH_MAX);
756 _static_ int __launchpad_pre_init(int argc, char **argv)
763 /* get my(launchpad) command line*/
764 launchpad_cmdline = __proc_get_cmdline_bypid(getpid());
765 if (launchpad_cmdline == NULL) {
766 _E("launchpad cmdline fail to get");
769 _D("launchpad cmdline = %s", launchpad_cmdline);
771 /* create launchpad sock */
772 fd = __create_server_sock(DEBUG_LAUNCHPAD_PID);
774 _E("server sock error");
778 __preload_init(argc, argv);
780 __preexec_init(argc, argv);
785 _static_ int __launchpad_post_init()
787 /* Setting this as a global variable to keep track
788 of launchpad poll cnt */
789 /* static int initialized = 0;*/
796 if (__signal_set_sigchld() < 0)
804 int main(int argc, char **argv)
807 struct pollfd pfds[POLLFD_MAX];
810 /* init without concerning X & EFL*/
811 main_fd = __launchpad_pre_init(argc, argv);
813 _E("launchpad pre init failed");
817 pfds[0].fd = main_fd;
818 pfds[0].events = POLLIN;
822 if (poll(pfds, POLLFD_MAX, -1) < 0)
825 /* init with concerning X & EFL (because of booting
827 if (__launchpad_post_init() < 0) {
828 _E("launcpad post init failed");
832 for (i = 0; i < POLLFD_MAX; i++) {
833 if ((pfds[i].revents & POLLIN) != 0) {
834 __launchpad_main_loop(pfds[i].fd);