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"
59 #include <app-checker.h>
62 #define _static_ static inline
64 #define SQLITE_FLUSH_MAX (1048576) /* (1024*1024) */
65 #define AUL_POLL_CNT 15
66 #define AUL_PR_NAME 16
67 #define PATH_APP_ROOT "/opt/apps"
68 #define PATH_DATA "/data"
69 #define SDK_CODE_COVERAGE "CODE_COVERAGE"
70 #define SDK_DYNAMIC_ANALYSIS "DYNAMIC_ANALYSIS"
71 #define PATH_DA_SO "/home/developer/sdk_tools/da/da_probe.so"
74 static double root_width;
75 static double root_height;
76 static char *launchpad_cmdline;
77 static int initialized = 0;
81 _static_ void __set_oom();
82 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb);
83 _static_ int __prepare_exec(const char *pkg_name,
84 const char *app_path, app_info_from_db * menu_info,
86 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb);
87 static void __fill_argv(const char *key, const char *value, void *data);
88 _static_ char **__create_argc_argv(bundle * kb, int *margc);
89 _static_ int __normal_fork_exec(int argc, char **argv);
90 _static_ void __real_launch(const char *app_path, bundle * kb);
91 _static_ int __add_history(int caller, int callee, const char *pkgname);
92 static inline int __parser(const char *arg, char *out, int out_size);
93 _static_ void __modify_bundle(bundle * kb, int caller_pid,
94 app_info_from_db * menu_info, int cmd);
95 _static_ int __child_raise_win_by_x(int pid, void *priv);
96 _static_ int __raise_win_by_x(int pid);
97 _static_ int __send_to_sigkill(int pid);
98 _static_ int __term_app(int pid);
99 _static_ int __resume_app(int pid);
100 _static_ int __app_process_by_pid(int cmd,
101 const char *pkg_name, struct ucred *cr);
102 _static_ int __nofork_processing(int cmd, int pid, bundle * kb);
103 _static_ void __real_send(int clifd, int ret);
104 _static_ void __send_result_to_caller(int clifd, int ret);
105 _static_ void __launchpad_main_loop(int main_fd);
106 _static_ int __launchpad_pre_init(int argc, char **argv);
107 _static_ int __launchpad_post_init();
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, str_array[i]);
174 str = bundle_get_val(kb, AUL_K_SDK);
176 __set_sdk_env(menu_info, 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*/
196 /* SET SMACK LABEL */
197 __set_smack(app_path);
200 if (__set_dac(pkg_name) < 0) {
201 _D("fail to set DAC - check your package's credential\n");
204 /* SET DUMPABLE - for coredump*/
205 prctl(PR_SET_DUMPABLE, 1);
207 /* SET PROCESS NAME*/
208 if (app_path == NULL) {
209 _D("app_path should not be NULL - check menu db");
212 file_name = strrchr(app_path, '/') + 1;
213 if (file_name == NULL) {
214 _D("can't locate file name to execute");
217 memset(process_name, '\0', AUL_PR_NAME);
218 snprintf(process_name, AUL_PR_NAME, "%s", file_name);
219 prctl(PR_SET_NAME, process_name);
222 __set_env(menu_info, kb);
227 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb)
233 bundle_encode(kb, &kb_data, &datalen);
234 if ((ret = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
235 _E("error request fake launch - error code = %d", ret);
240 static int __g_argv_pos = 0;
241 static void __fill_argv(const char *key, const char *value, void *data)
244 argv = (char **)data;
246 argv[__g_argv_pos] = (char *)key;
247 argv[__g_argv_pos + 1] = (char *)value;
252 _static_ char **__create_argc_argv(bundle * kb, int *margc)
257 argc = bundle_export_to_argv(kb, &argv);
263 _static_ int __normal_fork_exec(int argc, char **argv)
265 _D("start real fork and exec\n");
267 if (execv(argv[0], argv) < 0) { /* Flawfinder: ignore */
269 _E("such a file is no executable - %s", argv[0]);
271 _E("unknown executable error - %s", argv[0]);
278 _static_ void __real_launch(const char *app_path, bundle * kb)
284 app_argv = __create_argc_argv(kb, &app_argc);
285 app_argv[0] = strdup(app_path);
287 for (i = 0; i < app_argc; i++)
288 _D("input argument %d : %s##", i, app_argv[i]);
290 PERF("setup argument done");
291 _E("lock up test log(no error) : setup argument done");
293 /* Temporary log: launch time checking */
294 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:launchpad:done]", app_path);
296 __preload_exec(app_argc, app_argv);
298 __normal_fork_exec(app_argc, app_argv);
301 _static_ int __add_history(int caller, int callee, const char *pkgname)
303 /* TODO - make real history */
304 _D("***** HISTORY *****\n");
305 _D("%d ==> %d(%s) \n", caller, callee, pkgname);
306 _D("*******************\n");
312 * Parsing original app path to retrieve default bundle
314 * -1 : Invalid sequence
315 * -2 : Buffer overflow
318 static inline int __parser(const char *arg, char *out, int out_size)
322 char *start_out = out;
324 if (arg == NULL || out == NULL) {
325 /* Handles null buffer*/
329 for (i = 0; out_size > 1; i++) {
353 case 2: /* escape start*/
368 case 4: /* character escape*/
369 if (arg[i] == '\0') {
379 if (out != start_out) {
388 return -1; /* error*/
389 case 7: /* terminate*/
406 _static_ void __modify_bundle(bundle * kb, int caller_pid,
407 app_info_from_db * menu_info, int cmd)
409 char tmp_pid[MAX_PID_STR_BUFSZ];
411 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", caller_pid);
412 bundle_add(kb, AUL_K_CALLER_PID, tmp_pid);
413 bundle_del(kb, AUL_K_PKG_NAME);
414 if (cmd == APP_START_RES)
415 bundle_add(kb, AUL_K_WAIT_RESULT, "1");
417 /* Parse app_path to retrieve default bundle*/
418 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_RESUME) {
420 char exe[MAX_PATH_LEN];
423 ptr = _get_original_app_path(menu_info);
425 flag = __parser(ptr, exe, sizeof(exe));
431 _D("parsing app_path: EXEC - %s\n", exe);
434 flag = __parser(ptr, key, sizeof(key));
439 flag = __parser(ptr, value, sizeof(value));
444 /*bundle_del(kb, key);*/
445 bundle_add(kb, key, value);
447 } else if (flag == 0) {
448 _D("parsing app_path: No arguments\n");
450 _D("parsing app_path: Invalid argument\n");
455 _static_ int __child_raise_win_by_x(int pid, void *priv)
457 return x_util_raise_win(pid);
460 _static_ int __raise_win_by_x(int pid)
463 if (x_util_raise_win(pid) == 0)
466 /* support app launched by shell script*/
468 _D("X raise failed. try to find first child & raise it - c:%d p:%d\n",
473 if (__proc_iter_pgid(pgid, __child_raise_win_by_x, NULL) < 0)
479 _static_ int __send_to_sigkill(int pid)
487 if (killpg(pgid, SIGKILL) < 0)
493 _static_ int __term_app(int pid)
497 (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int)) < 0) {
498 _D("terminate packet send error - use SIGKILL");
499 if (__send_to_sigkill(pid) < 0) {
500 _E("fail to killing - %d\n", pid);
508 _static_ int __resume_app(int pid)
513 __app_send_raw(pid, APP_RESUME_BY_PID, (unsigned char *)&dummy,
516 _E("resume packet timeout error");
518 _D("resume packet send error - use raise win");
519 if (__raise_win_by_x(pid) < 0) {
520 _E("raise failed - %d resume fail\n", pid);
521 _E("we will term the app - %d\n", pid);
522 __send_to_sigkill(pid);
532 static int __get_caller_pid(bundle *kb)
537 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
541 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
553 _static_ int __foward_cmd(int cmd, bundle *kb, int cr_pid)
556 char tmp_pid[MAX_PID_STR_BUFSZ];
561 if ((pid = __get_caller_pid(kb)) < 0)
564 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", cr_pid);
566 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
568 bundle_encode(kb, &kb_data, &datalen);
569 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
577 _static_ int __app_process_by_pid(int cmd,
578 const char *pkg_name, struct ucred *cr)
583 if (pkg_name == NULL)
586 if ((cr->uid != 0) && (cr->uid != INHOUSE_UID)) {
587 _E("reject by security rule, your uid is %u\n", cr->uid);
591 pid = atoi(pkg_name);
598 case APP_RESUME_BY_PID:
599 ret = __resume_app(pid);
601 case APP_TERM_BY_PID:
602 ret = __term_app(pid);
604 case APP_KILL_BY_PID:
605 if ((ret = __send_to_sigkill(pid)) < 0)
606 _E("fail to killing - %d\n", pid);
612 _static_ int __nofork_processing(int cmd, int pid, bundle * kb)
617 _D("resume app's pid : %d\n", pid);
618 if ((ret = __resume_app(pid)) < 0)
619 _E("__resume_app failed. error code = %d", ret);
620 PERF("resume app done");
625 _D("fake launch pid : %d\n", pid);
626 if ((ret = __fake_launch_app(cmd, pid, kb)) < 0)
627 _E("fake_launch failed. error code = %d", ret);
628 PERF("fake launch done");
634 _static_ void __real_send(int clifd, int ret)
636 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
637 if (errno == EPIPE) {
638 _E("send failed due to EPIPE.\n");
640 _E("send fail to client");
646 _static_ void __send_result_to_caller(int clifd, int ret)
650 int cmdline_changed = 0;
651 int cmdline_exist = 0;
657 __real_send(clifd, ret);
660 /* check normally was launched?*/
663 cmdline = __proc_get_cmdline_bypid(ret);
664 if (cmdline == NULL) {
665 _E("error founded when being launched with %d", ret);
667 } else if (strcmp(cmdline, launchpad_cmdline)) {
676 _D("-- now wait to change cmdline --");
677 usleep(50 * 1000); /* 50ms sleep*/
679 } while (wait_count <= 10); /* max 50*10ms will be sleep*/
681 if ((!cmdline_exist) && (!cmdline_changed)) {
682 __real_send(clifd, -1); /* abnormally launched*/
686 if (!cmdline_changed)
687 _E("process launched, but cmdline not changed");
689 __real_send(clifd, ret);
693 _static_ void __launchpad_main_loop(int main_fd)
696 app_pkt_t *pkt = NULL;
697 app_info_from_db *menu_info = NULL;
699 const char *pkg_name;
700 const char *app_path;
705 char sock_path[UNIX_PATH_MAX] = {0,};
707 pkt = __app_recv_raw(main_fd, &clifd, &cr);
709 _D("packet is NULL");
713 kb = bundle_decode(pkt->data, pkt->len);
715 _D("bundle decode error");
720 PERF("packet processing start");
722 if (pkt->cmd == APP_RESULT || pkt->cmd == APP_CANCEL) {
723 pid = __foward_cmd(pkt->cmd, kb, cr.pid);
727 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
728 _D("pkg name : %s\n", pkg_name);
730 if (pkt->cmd == APP_TERM_BY_PID || pkt->cmd == APP_RESUME_BY_PID ||
731 pkt->cmd == APP_KILL_BY_PID) {
732 pid = __app_process_by_pid(pkt->cmd, pkg_name, &cr);
736 menu_info = _get_app_info_from_db_by_pkgname(pkg_name);
737 if (menu_info == NULL) {
738 _D("such pkg no found");
742 app_path = _get_app_path(menu_info);
743 if(app_path == NULL) {
744 _E("app_path is NULL");
747 if (app_path[0] != '/') {
748 _D("app_path is not absolute path");
752 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
753 pkg_name = _get_pkgname(menu_info);
755 PERF("get package information & modify bundle done");
757 if (_is_app_multi_inst(menu_info) == 0)
758 pid = __proc_iter_cmdline(NULL, (void *)app_path);
760 PERF("find pid by searching proc file system done");
766 _D("caller process & callee process is same.[%s:%d]",
768 pid = -ELOCALLAUNCH_ID;
769 } else if ((ret = __nofork_processing(pkt->cmd, pid, kb)) < 0)
775 _E("lock up test log(no error) : fork done");
780 __signal_unset_sigchld();
783 snprintf(sock_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, getpid());
786 PERF("prepare exec - first done");
787 _E("lock up test log(no error) : prepare exec - first done");
789 if (__prepare_exec(pkg_name, app_path,
790 menu_info, kb) < 0) {
791 _E("preparing work fail to launch - "
792 "can not launch %s\n", pkg_name);
796 PERF("prepare exec - second done");
797 _E("lock up test log(no error) : prepare exec - second done");
799 __real_launch(app_path, kb);
803 _D("==> real launch pid : %d %s\n", pid, app_path);
808 __add_history(cr.pid, pid, pkg_name);
810 __send_result_to_caller(clifd, pid);
814 ret = ac_check_launch_privilege(pkg_name, menu_info->pkg_type, pid);
815 _D("ac_check_launch_privilege : %d", ret);
818 if (menu_info != NULL)
819 _free_app_info_from_db(menu_info);
826 /* Active Flusing for Daemon */
827 if (initialized > AUL_POLL_CNT) {
828 sqlite3_release_memory(SQLITE_FLUSH_MAX);
835 _static_ int __launchpad_pre_init(int argc, char **argv)
845 /* get my(launchpad) command line*/
846 launchpad_cmdline = __proc_get_cmdline_bypid(getpid());
847 if (launchpad_cmdline == NULL) {
848 _E("launchpad cmdline fail to get");
851 _D("launchpad cmdline = %s", launchpad_cmdline);
853 /* create launchpad sock */
854 fd = __create_server_sock(LAUNCHPAD_PID);
856 _E("server sock error");
860 __preload_init(argc, argv);
865 _static_ int __launchpad_post_init()
867 /* Setting this as a global variable to keep track
868 of launchpad poll cnt */
869 /* static int initialized = 0;*/
877 if (__signal_set_sigchld() < 0)
885 int main(int argc, char **argv)
888 struct pollfd pfds[POLLFD_MAX];
891 /* init without concerning X & EFL*/
892 main_fd = __launchpad_pre_init(argc, argv);
894 _E("launchpad pre init failed");
898 pfds[0].fd = main_fd;
899 pfds[0].events = POLLIN;
903 if (poll(pfds, POLLFD_MAX, -1) < 0)
906 /* init with concerning X & EFL (because of booting
908 if (__launchpad_post_init() < 0) {
909 _E("launcpad post init failed");
913 for (i = 0; i < POLLFD_MAX; i++) {
914 if ((pfds[i].revents & POLLIN) != 0) {
915 __launchpad_main_loop(pfds[i].fd);