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 #include <app-checker.h>
64 #define _static_ static inline
66 #define SQLITE_FLUSH_MAX (1048576) /* (1024*1024) */
67 #define AUL_POLL_CNT 15
68 #define AUL_PR_NAME 16
69 #define PATH_APP_ROOT "/opt/apps"
70 #define PATH_DATA "/data"
71 #define SDK_CODE_COVERAGE "CODE_COVERAGE"
72 #define SDK_DYNAMIC_ANALYSIS "DYNAMIC_ANALYSIS"
73 #define PATH_DA_SO "/home/developer/sdk_tools/da/da_probe.so"
76 static char *launchpad_cmdline;
77 static int initialized = 0;
80 _static_ void __set_oom();
81 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb);
82 _static_ int __prepare_exec(const char *pkg_name,
83 const char *app_path, app_info_from_db * menu_info,
85 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb);
86 _static_ char **__create_argc_argv(bundle * kb, int *margc);
87 _static_ int __normal_fork_exec(int argc, char **argv);
88 _static_ void __real_launch(const char *app_path, bundle * kb);
89 _static_ void __add_history(int caller, int callee, const char *pkgname,
90 bundle *b, const char *app_path);
91 static inline int __parser(const char *arg, char *out, int out_size);
92 _static_ void __modify_bundle(bundle * kb, int caller_pid,
93 app_info_from_db * menu_info, int cmd);
94 _static_ int __child_raise_win_by_x(int pid, void *priv);
95 _static_ int __raise_win_by_x(int pid);
96 _static_ int __send_to_sigkill(int pid);
97 _static_ int __term_app(int pid);
98 _static_ int __resume_app(int pid);
99 _static_ int __app_process_by_pid(int cmd,
100 const char *pkg_name, struct ucred *cr);
101 _static_ int __nofork_processing(int cmd, int pid, bundle * kb);
102 _static_ void __real_send(int clifd, int ret);
103 _static_ void __send_result_to_caller(int clifd, int ret);
104 _static_ void __launchpad_main_loop(int main_fd);
105 _static_ int __launchpad_pre_init(int argc, char **argv);
106 _static_ int __launchpad_post_init();
108 extern ail_error_e ail_db_close(void);
112 _static_ void __set_oom()
114 char buf[MAX_LOCAL_BUFSZ];
117 /* we should reset oomadj value as default because child
118 inherits from parent oom_adj*/
119 snprintf(buf, MAX_LOCAL_BUFSZ, "/proc/%d/oom_adj", getpid());
120 fp = fopen(buf, "w");
123 fprintf(fp, "%d", -16);
127 _static_ void __set_sdk_env(app_info_from_db* menu_info, char* str) {
128 char buf[MAX_LOCAL_BUFSZ];
131 _D("key : %s / value : %s", AUL_K_SDK, str);
132 /* http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html*/
133 /* GCOV_PREFIX contains the prefix to add to the absolute paths in the object file. */
134 /* Prefix can be absolute, or relative. The default is no prefix. */
135 /* GCOV_PREFIX_STRIP indicates the how many initial directory names */
136 /* to stripoff the hardwired absolute paths. Default value is 0. */
137 if (strncmp(str, SDK_CODE_COVERAGE, strlen(str)) == 0) {
138 snprintf(buf, MAX_LOCAL_BUFSZ, PATH_APP_ROOT"/%s"PATH_DATA, _get_pkgname(menu_info));
139 ret = setenv("GCOV_PREFIX", buf, 1);
140 _D("GCOV_PREFIX : %d", ret);
141 ret = setenv("GCOV_PREFIX_STRIP", "4096", 1);
142 _D("GCOV_PREFIX_STRIP : %d", ret);
143 } else if (strncmp(str, SDK_DYNAMIC_ANALYSIS, strlen(str)) == 0) {
144 ret = setenv("LD_PRELOAD", PATH_DA_SO, 1);
145 _D("LD_PRELOAD : %d", ret);
150 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
153 const char **str_array;
157 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
161 str = bundle_get_val(kb, AUL_K_STARTTIME);
163 setenv("APP_START_TIME", str, 1);
165 if(bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
166 str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
167 if(str_array != NULL) {
168 for (i = 0; i < len; i++) {
169 _D("index : [%d]", i);
170 __set_sdk_env(menu_info, (char *)str_array[i]);
174 str = bundle_get_val(kb, AUL_K_SDK);
176 __set_sdk_env(menu_info, (char *)str);
181 _static_ int __prepare_exec(const char *pkg_name,
182 const char *app_path, app_info_from_db * menu_info,
186 char process_name[AUL_PR_NAME];
188 /* Set new session ID & new process group ID*/
189 /* In linux, child can set new session ID without check permission */
190 /* TODO : should be add to check permission in the kernel*/
193 __preexec_run(menu_info->pkg_type, pkg_name, app_path);
198 /* SET SMACK LABEL */
199 __set_smack((char *)app_path);
202 if (__set_dac(pkg_name) < 0) {
203 _D("fail to set DAC - check your package's credential\n");
206 /* SET DUMPABLE - for coredump*/
207 prctl(PR_SET_DUMPABLE, 1);
209 /* SET PROCESS NAME*/
210 if (app_path == NULL) {
211 _D("app_path should not be NULL - check menu db");
214 file_name = strrchr(app_path, '/') + 1;
215 if (file_name == NULL) {
216 _D("can't locate file name to execute");
219 memset(process_name, '\0', AUL_PR_NAME);
220 snprintf(process_name, AUL_PR_NAME, "%s", file_name);
221 prctl(PR_SET_NAME, process_name);
224 __set_env(menu_info, kb);
229 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb)
235 bundle_encode(kb, &kb_data, &datalen);
236 if ((ret = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
237 _E("error request fake launch - error code = %d", ret);
242 _static_ char **__create_argc_argv(bundle * kb, int *margc)
247 argc = bundle_export_to_argv(kb, &argv);
253 _static_ int __normal_fork_exec(int argc, char **argv)
255 _D("start real fork and exec\n");
257 if (execv(argv[0], argv) < 0) { /* Flawfinder: ignore */
259 _E("such a file is no executable - %s", argv[0]);
261 _E("unknown executable error - %s", argv[0]);
268 _static_ void __real_launch(const char *app_path, bundle * kb)
274 app_argv = __create_argc_argv(kb, &app_argc);
275 app_argv[0] = strdup(app_path);
277 for (i = 0; i < app_argc; i++)
278 _D("input argument %d : %s##", i, app_argv[i]);
280 PERF("setup argument done");
281 _E("lock up test log(no error) : setup argument done");
283 /* Temporary log: launch time checking */
284 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:launchpad:done]", app_path);
286 __preload_exec(app_argc, app_argv);
288 __normal_fork_exec(app_argc, app_argv);
291 _static_ void __add_history(int caller, int callee, const char *pkgname,
292 bundle *b, const char *app_path)
294 struct history_data *hd;
298 _D("***** HISTORY *****\n");
299 _D("%d ==> %d(%s) \n", caller, callee, pkgname);
300 _D("*******************\n");
303 bundle_encode(b, (bundle_raw **)&kb_data, &len);
304 hd = (struct history_data *)malloc(sizeof(char) * (len+1029));
306 strncpy(hd->pkg_name, pkgname, MAX_PACKAGE_STR_SIZE-1);
307 strncpy(hd->app_path, app_path, MAX_PACKAGE_APP_PATH_SIZE-1);
309 memcpy(hd->data, kb_data, len);
311 __app_send_raw(AUL_UTIL_PID, APP_ADD_HISTORY, (unsigned char *)hd,
316 hd = (struct history_data *)malloc(sizeof(char) * 1029);
318 strncpy(hd->pkg_name, pkgname, MAX_PACKAGE_STR_SIZE-1);
319 strncpy(hd->app_path, app_path, MAX_PACKAGE_APP_PATH_SIZE-1);
322 __app_send_raw(AUL_UTIL_PID, APP_ADD_HISTORY, (unsigned char *)hd,
332 * Parsing original app path to retrieve default bundle
334 * -1 : Invalid sequence
335 * -2 : Buffer overflow
338 static inline int __parser(const char *arg, char *out, int out_size)
342 char *start_out = out;
344 if (arg == NULL || out == NULL) {
345 /* Handles null buffer*/
349 for (i = 0; out_size > 1; i++) {
373 case 2: /* escape start*/
388 case 4: /* character escape*/
389 if (arg[i] == '\0') {
399 if (out != start_out) {
408 return -1; /* error*/
409 case 7: /* terminate*/
426 _static_ void __modify_bundle(bundle * kb, int caller_pid,
427 app_info_from_db * menu_info, int cmd)
429 char tmp_pid[MAX_PID_STR_BUFSZ];
431 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", caller_pid);
432 bundle_add(kb, AUL_K_CALLER_PID, tmp_pid);
433 bundle_del(kb, AUL_K_PKG_NAME);
434 if (cmd == APP_START_RES)
435 bundle_add(kb, AUL_K_WAIT_RESULT, "1");
437 /* Parse app_path to retrieve default bundle*/
438 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_OPEN || cmd == APP_RESUME) {
440 char exe[MAX_PATH_LEN];
443 ptr = _get_original_app_path(menu_info);
445 flag = __parser(ptr, exe, sizeof(exe));
451 _D("parsing app_path: EXEC - %s\n", exe);
454 flag = __parser(ptr, key, sizeof(key));
459 flag = __parser(ptr, value, sizeof(value));
464 /*bundle_del(kb, key);*/
465 bundle_add(kb, key, value);
467 } else if (flag == 0) {
468 _D("parsing app_path: No arguments\n");
470 _D("parsing app_path: Invalid argument\n");
475 _static_ int __child_raise_win_by_x(int pid, void *priv)
477 return x_util_raise_win(pid);
480 _static_ int __raise_win_by_x(int pid)
483 if (x_util_raise_win(pid) == 0)
486 /* support app launched by shell script*/
488 _D("X raise failed. try to find first child & raise it - c:%d p:%d\n",
493 if (__proc_iter_pgid(pgid, __child_raise_win_by_x, NULL) < 0)
499 _static_ int __send_to_sigkill(int pid)
507 if (killpg(pgid, SIGKILL) < 0)
513 _static_ int __term_app(int pid)
517 (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int)) < 0) {
518 _D("terminate packet send error - use SIGKILL");
519 if (__send_to_sigkill(pid) < 0) {
520 _E("fail to killing - %d\n", pid);
528 _static_ int __resume_app(int pid)
533 __app_send_raw(pid, APP_RESUME_BY_PID, (unsigned char *)&dummy,
536 _E("resume packet timeout error");
538 _D("resume packet send error - use raise win");
539 if (__raise_win_by_x(pid) < 0) {
540 _E("raise failed - %d resume fail\n", pid);
541 _E("we will term the app - %d\n", pid);
542 __send_to_sigkill(pid);
552 static int __get_caller_pid(bundle *kb)
557 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
561 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
573 _static_ int __foward_cmd(int cmd, bundle *kb, int cr_pid)
576 char tmp_pid[MAX_PID_STR_BUFSZ];
581 if ((pid = __get_caller_pid(kb)) < 0)
584 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", cr_pid);
586 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
588 bundle_encode(kb, &kb_data, &datalen);
589 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
597 _static_ int __app_process_by_pid(int cmd,
598 const char *pkg_name, struct ucred *cr)
603 if (pkg_name == NULL)
606 if ((cr->uid != 0) && (cr->uid != INHOUSE_UID)) {
607 _E("reject by security rule, your uid is %u\n", cr->uid);
611 pid = atoi(pkg_name);
618 case APP_RESUME_BY_PID:
619 ret = __resume_app(pid);
621 case APP_TERM_BY_PID:
622 ret = __term_app(pid);
624 case APP_KILL_BY_PID:
625 if ((ret = __send_to_sigkill(pid)) < 0)
626 _E("fail to killing - %d\n", pid);
632 _static_ int __nofork_processing(int cmd, int pid, bundle * kb)
638 _D("resume app's pid : %d\n", pid);
639 if ((ret = __resume_app(pid)) < 0)
640 _E("__resume_app failed. error code = %d", ret);
641 PERF("resume app done");
646 _D("fake launch pid : %d\n", pid);
647 if ((ret = __fake_launch_app(cmd, pid, kb)) < 0)
648 _E("fake_launch failed. error code = %d", ret);
649 PERF("fake launch done");
655 _static_ void __real_send(int clifd, int ret)
657 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
658 if (errno == EPIPE) {
659 _E("send failed due to EPIPE.\n");
661 _E("send fail to client");
667 _static_ void __send_result_to_caller(int clifd, int ret)
671 int cmdline_changed = 0;
672 int cmdline_exist = 0;
678 __real_send(clifd, ret);
681 /* check normally was launched?*/
684 cmdline = __proc_get_cmdline_bypid(ret);
685 if (cmdline == NULL) {
686 _E("error founded when being launched with %d", ret);
688 } else if (strcmp(cmdline, launchpad_cmdline)) {
697 _D("-- now wait to change cmdline --");
698 usleep(50 * 1000); /* 50ms sleep*/
700 } while (wait_count <= 20); /* max 50*20ms will be sleep*/
702 if ((!cmdline_exist) && (!cmdline_changed)) {
703 __real_send(clifd, -1); /* abnormally launched*/
707 if (!cmdline_changed)
708 _E("process launched, but cmdline not changed");
710 __real_send(clifd, ret);
714 _static_ void __launchpad_main_loop(int main_fd)
717 app_pkt_t *pkt = NULL;
718 app_info_from_db *menu_info = NULL;
720 const char *pkg_name = NULL;
721 const char *app_path = NULL;
725 int is_real_launch = 0;
727 char sock_path[UNIX_PATH_MAX] = {0,};
729 pkt = __app_recv_raw(main_fd, &clifd, &cr);
731 _D("packet is NULL");
735 kb = bundle_decode(pkt->data, pkt->len);
737 _D("bundle decode error");
742 PERF("packet processing start");
744 if (pkt->cmd == APP_RESULT || pkt->cmd == APP_CANCEL) {
745 pid = __foward_cmd(pkt->cmd, kb, cr.pid);
749 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
750 _D("pkg name : %s\n", pkg_name);
752 if (pkt->cmd == APP_TERM_BY_PID || pkt->cmd == APP_RESUME_BY_PID ||
753 pkt->cmd == APP_KILL_BY_PID) {
754 pid = __app_process_by_pid(pkt->cmd, pkg_name, &cr);
758 menu_info = _get_app_info_from_db_by_pkgname(pkg_name);
759 if (menu_info == NULL) {
760 _D("such pkg no found");
764 app_path = _get_app_path(menu_info);
765 if(app_path == NULL) {
766 _E("app_path is NULL");
769 if (app_path[0] != '/') {
770 _D("app_path is not absolute path");
774 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
775 pkg_name = _get_pkgname(menu_info);
777 PERF("get package information & modify bundle done");
779 if (_is_app_multi_inst(menu_info) == 0)
780 pid = __proc_iter_cmdline(NULL, (void *)app_path);
782 PERF("find pid by searching proc file system done");
788 _D("caller process & callee process is same.[%s:%d]",
790 pid = -ELOCALLAUNCH_ID;
791 } else if ((ret = __nofork_processing(pkt->cmd, pid, kb)) < 0)
793 } else if (pkt->cmd != APP_RESUME) {
797 _E("lock up test log(no error) : fork done");
802 __signal_unset_sigchld();
805 snprintf(sock_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, getpid());
808 PERF("prepare exec - first done");
809 _E("lock up test log(no error) : prepare exec - first done");
811 if (__prepare_exec(pkg_name, app_path,
812 menu_info, kb) < 0) {
813 _E("preparing work fail to launch - "
814 "can not launch %s\n", pkg_name);
818 PERF("prepare exec - second done");
819 _E("lock up test log(no error) : prepare exec - second done");
821 __real_launch(app_path, kb);
825 _D("==> real launch pid : %d %s\n", pid, app_path);
830 __send_result_to_caller(clifd, pid);
834 ret = ac_check_launch_privilege(pkg_name, menu_info->pkg_type, pid);
835 _D("ac_check_launch_privilege : %d", ret);
839 __add_history(cr.pid, pid, pkg_name, NULL, app_path);
843 __add_history(cr.pid, pid, pkg_name, kb, app_path);
846 _D("no launch case");
848 if (is_real_launch) {
850 __signal_block_sigchld();
851 __send_app_launch_signal(pid);
852 __signal_unblock_sigchld();
856 if (menu_info != NULL)
857 _free_app_info_from_db(menu_info);
864 /* Active Flusing for Daemon */
865 if (initialized > AUL_POLL_CNT) {
866 sqlite3_release_memory(SQLITE_FLUSH_MAX);
873 _static_ int __launchpad_pre_init(int argc, char **argv)
883 /* get my(launchpad) command line*/
884 launchpad_cmdline = __proc_get_cmdline_bypid(getpid());
885 if (launchpad_cmdline == NULL) {
886 _E("launchpad cmdline fail to get");
889 _D("launchpad cmdline = %s", launchpad_cmdline);
891 /* create launchpad sock */
892 fd = __create_server_sock(LAUNCHPAD_PID);
894 _E("server sock error");
898 __preload_init(argc, argv);
900 __preexec_init(argc, argv);
905 _static_ int __launchpad_post_init()
907 /* Setting this as a global variable to keep track
908 of launchpad poll cnt */
909 /* static int initialized = 0;*/
916 if (__signal_set_sigchld() < 0)
924 int main(int argc, char **argv)
927 struct pollfd pfds[POLLFD_MAX];
930 /* init without concerning X & EFL*/
931 main_fd = __launchpad_pre_init(argc, argv);
933 _E("launchpad pre init failed");
937 pfds[0].fd = main_fd;
938 pfds[0].events = POLLIN;
942 if (poll(pfds, POLLFD_MAX, -1) < 0)
945 /* init with concerning X & EFL (because of booting
947 if (__launchpad_post_init() < 0) {
948 _E("launcpad post init failed");
952 for (i = 0; i < POLLFD_MAX; i++) {
953 if ((pfds[i].revents & POLLIN) != 0) {
954 __launchpad_main_loop(pfds[i].fd);