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"
71 static double root_width;
72 static double root_height;
73 static int scale_factor_init = 0;
74 static char *launchpad_cmdline;
75 static int initialized = 0;
79 _static_ void __set_oom();
80 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb);
81 _static_ int __prepare_exec(const char *pkg_name,
82 const char *app_path, app_info_from_db * menu_info, bundle * kb);
83 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb);
84 static void __fill_argv(const char *key, const char *value, void *data);
85 _static_ char **__create_argc_argv(bundle * kb, int *margc);
86 _static_ int __normal_fork_exec(int argc, char **argv);
87 _static_ void __real_launch(const char *app_path, bundle * kb);
88 _static_ int __add_history(int caller, int callee, const char *pkgname);
89 static inline int __parser(const char *arg, char *out, int out_size);
90 _static_ void __modify_bundle(bundle * kb, int caller_pid,
91 app_info_from_db * menu_info, int cmd);
92 _static_ int __child_raise_win_by_x(int pid, void *priv);
93 _static_ int __raise_win_by_x(int pid);
94 _static_ int __send_to_sigkill(int pid);
95 _static_ int __term_app(int pid);
96 _static_ int __resume_app(int pid);
97 _static_ int __app_process_by_pid(int cmd,
98 const char *pkg_name, struct ucred *cr);
99 _static_ int __nofork_processing(int cmd, int pid, bundle * kb);
100 _static_ void __real_send(int clifd, int ret);
101 _static_ void __send_result_to_caller(int clifd, int ret);
102 _static_ void __launchpad_main_loop(int main_fd);
103 _static_ int __launchpad_pre_init(int argc, char **argv);
104 _static_ int __launchpad_post_init();
109 _static_ void __set_oom()
111 char buf[MAX_LOCAL_BUFSZ];
114 /* we should reset oomadj value as default because child
115 inherits from parent oom_adj*/
116 snprintf(buf, MAX_LOCAL_BUFSZ, "/proc/%d/oom_adj", getpid());
117 fp = fopen(buf, "w");
120 fprintf(fp, "%d", -16);
124 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
126 char buf[MAX_LOCAL_BUFSZ];
130 if (scale_factor_init) {
131 if (_is_app_scalable_with_height(menu_info)) {
132 if (_get_app_height(menu_info) == 0)
133 snprintf(buf, MAX_LOCAL_BUFSZ, "%lf", 1.0);
135 snprintf(buf, MAX_LOCAL_BUFSZ, "%lf",
137 _get_app_height(menu_info));
139 if (_get_app_width(menu_info) == 0)
140 snprintf(buf, MAX_LOCAL_BUFSZ, "%lf", 1.0);
142 snprintf(buf, MAX_LOCAL_BUFSZ, "%lf",
144 _get_app_width(menu_info));
147 _D("changed scale factor = %s", buf);
149 setenv("ELM_SCALE", buf, 1);
150 setenv("SCALE_FACTOR", buf, 1);
153 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
157 str = bundle_get_val(kb, AUL_K_STARTTIME);
159 setenv("APP_START_TIME", str, 1);
161 str = bundle_get_val(kb, AUL_K_SDK);
163 _D("key : %s / value : %s",AUL_K_SDK,str);
164 /* http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html*/
165 /* GCOV_PREFIX contains the prefix to add to the absolute paths in the object file. */
166 /* Prefix can be absolute, or relative. The default is no prefix. */
167 /* GCOV_PREFIX_STRIP indicates the how many initial directory names */
168 /* to stripoff the hardwired absolute paths. Default value is 0. */
169 if (strncmp(str, SDK_CODE_COVERAGE, strlen(str)) == 0) {
170 snprintf(buf, MAX_LOCAL_BUFSZ, PATH_APP_ROOT"/%s"PATH_DATA, _get_pkgname(menu_info));
171 ret = setenv("GCOV_PREFIX", buf, 1);
172 _D("GCOV_PREFIX : %d", ret);
173 ret = setenv("GCOV_PREFIX_STRIP", "4096", 1);
174 _D("GCOV_PREFIX_STRIP : %d", ret);
180 _static_ int __prepare_exec(const char *pkg_name,
181 const char *app_path, app_info_from_db * menu_info, bundle * kb)
184 char process_name[AUL_PR_NAME];
186 /* Set new session ID & new process group ID*/
187 /* In linux, child can set new session ID without check permission */
188 /* TODO : should be add to check permission in the kernel*/
194 /* SET SMACK LABEL */
195 __set_smack(app_path);
198 if (__set_dac(pkg_name) < 0) {
199 _D("fail to set DAC - check your package's credential\n");
202 /* SET DUMPABLE - for coredump*/
203 prctl(PR_SET_DUMPABLE, 1);
205 /* SET PROCESS NAME*/
206 if (app_path == NULL) {
207 _D("app_path should not be NULL - check menu db");
210 file_name = strrchr(app_path, '/') + 1;
211 if (file_name == NULL) {
212 _D("can't locate file name to execute");
215 memset(process_name, '\0', AUL_PR_NAME);
216 snprintf(process_name, AUL_PR_NAME, "%s", file_name);
217 prctl(PR_SET_NAME, process_name);
220 __set_env(menu_info, kb);
225 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb)
231 bundle_encode(kb, &kb_data, &datalen);
232 if ((ret = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
233 _E("error request fake launch - error code = %d", ret);
238 static int __g_argv_pos = 0;
239 static void __fill_argv(const char *key, const char *value, void *data)
242 argv = (char **)data;
244 argv[__g_argv_pos] = (char *)key;
245 argv[__g_argv_pos + 1] = (char *)value;
250 _static_ char **__create_argc_argv(bundle * kb, int *margc)
255 argc = bundle_export_to_argv(kb, &argv);
261 _static_ int __normal_fork_exec(int argc, char **argv)
263 _D("start real fork and exec\n");
265 if (execv(argv[0], argv) < 0) { /* Flawfinder: ignore */
267 _E("such a file is no executable - %s", argv[0]);
269 _E("unknown executable error - %s", argv[0]);
276 _static_ void __real_launch(const char *app_path, bundle * kb)
282 app_argv = __create_argc_argv(kb, &app_argc);
283 app_argv[0] = strdup(app_path);
285 for (i = 0; i < app_argc; i++)
286 _D("input argument %d : %s##", i, app_argv[i]);
288 PERF("setup argument done");
289 _E("lock up test log(no error) : setup argument done");
291 /* Temporary log: launch time checking */
292 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:launchpad:done]", app_path);
294 __preload_exec(app_argc, app_argv);
296 __normal_fork_exec(app_argc, app_argv);
299 _static_ int __add_history(int caller, int callee, const char *pkgname)
301 /* TODO - make real history */
302 _D("***** HISTORY *****\n");
303 _D("%d ==> %d(%s) \n", caller, callee, pkgname);
304 _D("*******************\n");
310 * Parsing original app path to retrieve default bundle
312 * -1 : Invalid sequence
313 * -2 : Buffer overflow
316 static inline int __parser(const char *arg, char *out, int out_size)
320 char *start_out = out;
322 if (arg == NULL || out == NULL) {
323 /* Handles null buffer*/
327 for (i = 0; out_size > 1; i++) {
351 case 2: /* escape start*/
366 case 4: /* character escape*/
367 if (arg[i] == '\0') {
377 if (out != start_out) {
386 return -1; /* error*/
387 case 7: /* terminate*/
404 _static_ void __modify_bundle(bundle * kb, int caller_pid,
405 app_info_from_db * menu_info, int cmd)
407 char tmp_pid[MAX_PID_STR_BUFSZ];
409 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", caller_pid);
410 bundle_add(kb, AUL_K_CALLER_PID, tmp_pid);
411 bundle_del(kb, AUL_K_PKG_NAME);
412 if (cmd == APP_START_RES)
413 bundle_add(kb, AUL_K_WAIT_RESULT, "1");
415 /* Parse app_path to retrieve default bundle*/
416 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_RESUME) {
418 char exe[MAX_PATH_LEN];
421 ptr = _get_original_app_path(menu_info);
423 flag = __parser(ptr, exe, sizeof(exe));
429 _D("parsing app_path: EXEC - %s\n", exe);
432 flag = __parser(ptr, key, sizeof(key));
437 flag = __parser(ptr, value, sizeof(value));
442 /*bundle_del(kb, key);*/
443 bundle_add(kb, key, value);
445 } else if (flag == 0) {
446 _D("parsing app_path: No arguments\n");
448 _D("parsing app_path: Invalid argument\n");
453 _static_ int __child_raise_win_by_x(int pid, void *priv)
455 return x_util_raise_win(pid);
458 _static_ int __raise_win_by_x(int pid)
461 if (x_util_raise_win(pid) == 0)
464 /* support app launched by shell script*/
466 _D("X raise failed. try to find first child & raise it - c:%d p:%d\n",
471 if (__proc_iter_pgid(pgid, __child_raise_win_by_x, NULL) < 0)
477 _static_ int __send_to_sigkill(int pid)
485 if (killpg(pgid, SIGKILL) < 0)
491 _static_ int __term_app(int pid)
495 (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int)) < 0) {
496 _D("terminate packet send error - use SIGKILL");
497 if (__send_to_sigkill(pid) < 0) {
498 _E("fail to killing - %d\n", pid);
506 _static_ int __resume_app(int pid)
511 __app_send_raw(pid, APP_RESUME_BY_PID, (unsigned char *)&dummy,
514 _E("resume packet timeout error");
516 _D("resume packet send error - use raise win");
517 if (__raise_win_by_x(pid) < 0) {
518 _E("raise failed - %d resume fail\n", pid);
519 _E("we will term the app - %d\n", pid);
520 __send_to_sigkill(pid);
530 static int __get_caller_pid(bundle *kb)
535 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
539 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
551 _static_ int __foward_cmd(int cmd, bundle *kb, int cr_pid)
554 char tmp_pid[MAX_PID_STR_BUFSZ];
559 if ((pid = __get_caller_pid(kb)) < 0)
562 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", cr_pid);
564 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
566 bundle_encode(kb, &kb_data, &datalen);
567 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
575 _static_ int __app_process_by_pid(int cmd,
576 const char *pkg_name, struct ucred *cr)
581 if (pkg_name == NULL)
584 if ((cr->uid != 0) && (cr->uid != INHOUSE_UID)) {
585 _E("reject by security rule, your uid is %u\n", cr->uid);
589 pid = atoi(pkg_name);
596 case APP_RESUME_BY_PID:
597 ret = __resume_app(pid);
599 case APP_TERM_BY_PID:
600 ret = __term_app(pid);
602 case APP_KILL_BY_PID:
603 if ((ret = __send_to_sigkill(pid)) < 0)
604 _E("fail to killing - %d\n", pid);
610 _static_ int __nofork_processing(int cmd, int pid, bundle * kb)
615 _D("resume app's pid : %d\n", pid);
616 if ((ret = __resume_app(pid)) < 0)
617 _E("__resume_app failed. error code = %d", ret);
618 PERF("resume app done");
623 _D("fake launch pid : %d\n", pid);
624 if ((ret = __fake_launch_app(cmd, pid, kb)) < 0)
625 _E("fake_launch failed. error code = %d", ret);
626 PERF("fake launch done");
632 _static_ void __real_send(int clifd, int ret)
634 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
635 if (errno == EPIPE) {
636 _E("send failed due to EPIPE.\n");
638 _E("send fail to client");
644 _static_ void __send_result_to_caller(int clifd, int ret)
648 int cmdline_changed = 0;
649 int cmdline_exist = 0;
655 __real_send(clifd, ret);
658 /* check normally was launched?*/
661 cmdline = __proc_get_cmdline_bypid(ret);
662 if (cmdline == NULL) {
663 _E("error founded when being launched with %d", ret);
665 } else if (strcmp(cmdline, launchpad_cmdline)) {
674 _D("-- now wait to change cmdline --");
675 usleep(50 * 1000); /* 50ms sleep*/
677 } while (wait_count <= 10); /* max 50*10ms will be sleep*/
679 if ((!cmdline_exist) && (!cmdline_changed)) {
680 __real_send(clifd, -1); /* abnormally launched*/
684 if (!cmdline_changed)
685 _E("process launched, but cmdline not changed");
687 __real_send(clifd, ret);
691 _static_ void __launchpad_main_loop(int main_fd)
694 app_pkt_t *pkt = NULL;
695 app_info_from_db *menu_info = NULL;
697 const char *pkg_name;
698 const char *app_path;
703 char sock_path[UNIX_PATH_MAX] = {0,};
705 pkt = __app_recv_raw(main_fd, &clifd, &cr);
707 _D("packet is NULL");
711 kb = bundle_decode(pkt->data, pkt->len);
713 _D("bundle decode error");
718 PERF("packet processing start");
720 if (pkt->cmd == APP_RESULT || pkt->cmd == APP_CANCEL) {
721 pid = __foward_cmd(pkt->cmd, kb, cr.pid);
725 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
726 _D("pkg name : %s\n", pkg_name);
728 if (pkt->cmd == APP_TERM_BY_PID || pkt->cmd == APP_RESUME_BY_PID ||
729 pkt->cmd == APP_KILL_BY_PID) {
730 pid = __app_process_by_pid(pkt->cmd, pkg_name, &cr);
734 menu_info = _get_app_info_from_db_by_pkgname(pkg_name);
735 if (menu_info == NULL) {
736 _D("such pkg no found");
740 app_path = _get_app_path(menu_info);
741 if(app_path == NULL) {
742 _E("app_path is NULL");
745 if (app_path[0] != '/') {
746 _D("app_path is not absolute path");
750 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
751 pkg_name = _get_pkgname(menu_info);
753 PERF("get package information & modify bundle done");
755 if (_is_app_multi_inst(menu_info) == 0)
756 pid = __proc_iter_cmdline(NULL, (void *)app_path);
758 PERF("find pid by searching proc file system done");
764 _D("caller process & callee process is same.[%s:%d]",
766 pid = -ELOCALLAUNCH_ID;
767 } else if ((ret = __nofork_processing(pkt->cmd, pid, kb)) < 0)
773 _E("lock up test log(no error) : fork done");
778 __signal_unset_sigchld();
781 snprintf(sock_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, getpid());
784 PERF("prepare exec - first done");
785 _E("lock up test log(no error) : prepare exec - first done");
787 if (__prepare_exec(pkg_name, app_path,
788 menu_info, kb) < 0) {
789 _E("preparing work fail to launch - "
790 "can not launch %s\n", pkg_name);
794 PERF("prepare exec - second done");
795 _E("lock up test log(no error) : prepare exec - second done");
797 __real_launch(app_path, kb);
801 _D("==> real launch pid : %d %s\n", pid, app_path);
806 __add_history(cr.pid, pid, pkg_name);
808 __send_result_to_caller(clifd, pid);
812 ret = ac_check_launch_privilege(pkg_name, menu_info->pkg_type, pid);
813 _D("ac_check_launch_privilege : %d", ret);
816 if (menu_info != NULL)
817 _free_app_info_from_db(menu_info);
824 /* Active Flusing for Daemon */
825 if (initialized > AUL_POLL_CNT) {
826 sqlite3_release_memory(SQLITE_FLUSH_MAX);
833 _static_ int __launchpad_pre_init(int argc, char **argv)
843 /* get my(launchpad) command line*/
844 launchpad_cmdline = __proc_get_cmdline_bypid(getpid());
845 if (launchpad_cmdline == NULL) {
846 _E("launchpad cmdline fail to get");
849 _D("launchpad cmdline = %s", launchpad_cmdline);
851 /* create launchpad sock */
852 fd = __create_server_sock(LAUNCHPAD_PID);
854 _E("server sock error");
858 __preload_init(argc, argv);
863 _static_ int __launchpad_post_init()
865 /* Setting this as a global variable to keep track
866 of launchpad poll cnt */
867 /* static int initialized = 0;*/
876 /* get root window size */
877 x_util_get_default_size(&root_width, &root_height);
878 scale_factor_init = 1;
881 if (__signal_set_sigchld() < 0)
889 int main(int argc, char **argv)
892 struct pollfd pfds[POLLFD_MAX];
895 /* init without concerning X & EFL*/
896 main_fd = __launchpad_pre_init(argc, argv);
898 _E("launchpad pre init failed");
902 pfds[0].fd = main_fd;
903 pfds[0].events = POLLIN;
907 if (poll(pfds, POLLFD_MAX, -1) < 0)
910 /* init with concerning X & EFL (because of booting
912 if (__launchpad_post_init() < 0) {
913 _E("launcpad post init failed");
917 for (i = 0; i < POLLFD_MAX; i++) {
918 if ((pfds[i].revents & POLLIN) != 0) {
919 __launchpad_main_loop(pfds[i].fd);