2 * Copyright (c) 2011 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.
19 * simple AUL daemon - launchpad
26 #include <sys/types.h>
33 #include <sys/prctl.h>
41 #include "menu_db_util.h"
42 #include "simple_util.h"
43 #include "access_control.h"
54 #include <app-checker.h>
57 #define _static_ static inline
59 #define SQLITE_FLUSH_MAX (1048576) /* (1024*1024) */
60 #define AUL_POLL_CNT 15
61 #define AUL_PR_NAME 16
62 #define PATH_APP_ROOT "/opt/usr/apps"
63 #define PATH_DATA "/data"
64 #define SDK_CODE_COVERAGE "CODE_COVERAGE"
65 #define SDK_DYNAMIC_ANALYSIS "DYNAMIC_ANALYSIS"
66 #define PATH_DA_SO "/home/developer/sdk_tools/da/da_probe.so"
69 static char *launchpad_cmdline;
70 static int initialized = 0;
73 _static_ void __set_oom();
74 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb);
75 _static_ int __prepare_exec(const char *pkg_name,
76 const char *app_path, app_info_from_db * menu_info,
78 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb);
79 _static_ char **__create_argc_argv(bundle * kb, int *margc);
80 _static_ int __normal_fork_exec(int argc, char **argv);
81 _static_ void __real_launch(const char *app_path, bundle * kb);
82 _static_ void __add_history(int caller, int callee, const char *pkgname,
83 bundle *b, const char *app_path);
84 static inline int __parser(const char *arg, char *out, int out_size);
85 _static_ void __modify_bundle(bundle * kb, int caller_pid,
86 app_info_from_db * menu_info, int cmd);
87 _static_ int __child_raise_win_by_x(int pid, void *priv);
88 _static_ int __raise_win_by_x(int pid);
89 _static_ int __send_to_sigkill(int pid);
90 _static_ int __term_app(int pid);
91 _static_ int __resume_app(int pid);
92 _static_ void __real_send(int clifd, int ret);
93 _static_ void __send_result_to_caller(int clifd, int ret);
94 _static_ void __launchpad_main_loop(int main_fd);
95 _static_ int __launchpad_pre_init(int argc, char **argv);
96 _static_ int __launchpad_post_init();
98 extern ail_error_e ail_db_close(void);
102 _static_ void __set_oom()
104 char buf[MAX_LOCAL_BUFSZ];
107 /* we should reset oomadj value as default because child
108 inherits from parent oom_adj*/
109 snprintf(buf, MAX_LOCAL_BUFSZ, "/proc/%d/oom_adj", getpid());
110 fp = fopen(buf, "w");
113 fprintf(fp, "%d", -16);
117 _static_ void __set_sdk_env(app_info_from_db* menu_info, char* str) {
118 char buf[MAX_LOCAL_BUFSZ];
121 _D("key : %s / value : %s", AUL_K_SDK, str);
122 /* http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html*/
123 /* GCOV_PREFIX contains the prefix to add to the absolute paths in the object file. */
124 /* Prefix can be absolute, or relative. The default is no prefix. */
125 /* GCOV_PREFIX_STRIP indicates the how many initial directory names */
126 /* to stripoff the hardwired absolute paths. Default value is 0. */
127 if (strncmp(str, SDK_CODE_COVERAGE, strlen(str)) == 0) {
128 snprintf(buf, MAX_LOCAL_BUFSZ, PATH_APP_ROOT"/%s"PATH_DATA, _get_pkgname(menu_info));
129 ret = setenv("GCOV_PREFIX", buf, 1);
130 _D("GCOV_PREFIX : %d", ret);
131 ret = setenv("GCOV_PREFIX_STRIP", "4096", 1);
132 _D("GCOV_PREFIX_STRIP : %d", ret);
133 } else if (strncmp(str, SDK_DYNAMIC_ANALYSIS, strlen(str)) == 0) {
134 ret = setenv("LD_PRELOAD", PATH_DA_SO, 1);
135 _D("LD_PRELOAD : %d", ret);
140 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
143 const char **str_array;
147 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
151 str = bundle_get_val(kb, AUL_K_STARTTIME);
153 setenv("APP_START_TIME", str, 1);
155 if(bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
156 str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
157 if(str_array != NULL) {
158 for (i = 0; i < len; i++) {
159 _D("index : [%d]", i);
160 __set_sdk_env(menu_info, (char *)str_array[i]);
164 str = bundle_get_val(kb, AUL_K_SDK);
166 __set_sdk_env(menu_info, (char *)str);
169 if (menu_info->hwacc != NULL)
170 setenv("HWACC", menu_info->hwacc, 1);
173 _static_ int __prepare_exec(const char *pkg_name,
174 const char *app_path, app_info_from_db * menu_info,
178 char process_name[AUL_PR_NAME];
180 /* Set new session ID & new process group ID*/
181 /* In linux, child can set new session ID without check permission */
182 /* TODO : should be add to check permission in the kernel*/
185 __preexec_run(menu_info->pkg_type, pkg_name, app_path);
191 if (__set_access(pkg_name, menu_info->pkg_type, app_path) < 0) {
192 _D("fail to set privileges - check your package's credential\n");
195 /* SET DUMPABLE - for coredump*/
196 prctl(PR_SET_DUMPABLE, 1);
198 /* SET PROCESS NAME*/
199 if (app_path == NULL) {
200 _D("app_path should not be NULL - check menu db");
203 file_name = strrchr(app_path, '/') + 1;
204 if (file_name == NULL) {
205 _D("can't locate file name to execute");
208 memset(process_name, '\0', AUL_PR_NAME);
209 snprintf(process_name, AUL_PR_NAME, "%s", file_name);
210 prctl(PR_SET_NAME, process_name);
213 __set_env(menu_info, kb);
218 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb)
224 bundle_encode(kb, &kb_data, &datalen);
225 if ((ret = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
226 _E("error request fake launch - error code = %d", ret);
231 _static_ char **__create_argc_argv(bundle * kb, int *margc)
236 argc = bundle_export_to_argv(kb, &argv);
242 _static_ int __normal_fork_exec(int argc, char **argv)
244 _D("start real fork and exec\n");
246 // warning: unused parameter
249 if (execv(argv[0], argv) < 0) { /* Flawfinder: ignore */
251 _E("such a file is no executable - %s", argv[0]);
253 _E("unknown executable error - %s", argv[0]);
260 _static_ void __real_launch(const char *app_path, bundle * kb)
266 app_argv = __create_argc_argv(kb, &app_argc);
268 #ifndef NATIVE_LAUNCHPAD
269 if (__change_cmdline(app_path) < 0) {
270 _E("change cmdline fail");
274 app_argv[0] = g_argv[0];
276 app_argv[0] = strdup(app_path);
279 for (i = 0; i < app_argc; i++)
280 _D("input argument %d : %s##", i, app_argv[i]);
282 PERF("setup argument done");
283 _E("lock up test log(no error) : setup argument done");
285 /* Temporary log: launch time checking */
286 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:launchpad:done]", app_path);
288 __preload_exec(app_argc, app_argv);
290 __normal_fork_exec(app_argc, app_argv);
293 _static_ void __add_history(int caller, int callee, const char *pkgname,
294 bundle *b, const char *app_path)
296 struct history_data *hd;
300 _D("***** HISTORY *****\n");
301 _D("%d ==> %d(%s) \n", caller, callee, pkgname);
302 _D("*******************\n");
305 bundle_encode(b, (bundle_raw **)&kb_data, &len);
306 hd = (struct history_data *)malloc(sizeof(char) * (len+1033));
308 strncpy(hd->pkg_name, pkgname, MAX_PACKAGE_STR_SIZE-1);
309 strncpy(hd->app_path, app_path, MAX_PACKAGE_APP_PATH_SIZE-1);
312 memcpy(hd->data, kb_data, len);
314 __app_send_raw(AUL_UTIL_PID, APP_ADD_HISTORY, (unsigned char *)hd,
319 hd = (struct history_data *)malloc(sizeof(struct history_data));
321 strncpy(hd->pkg_name, pkgname, MAX_PACKAGE_STR_SIZE-1);
322 strncpy(hd->app_path, app_path, MAX_PACKAGE_APP_PATH_SIZE-1);
326 __app_send_raw(AUL_UTIL_PID, APP_ADD_HISTORY, (unsigned char *)hd,
336 * Parsing original app path to retrieve default bundle
338 * -1 : Invalid sequence
339 * -2 : Buffer overflow
342 static inline int __parser(const char *arg, char *out, int out_size)
346 char *start_out = out;
348 if (arg == NULL || out == NULL) {
349 /* Handles null buffer*/
353 for (i = 0; out_size > 1; i++) {
377 case 2: /* escape start*/
392 case 4: /* character escape*/
393 if (arg[i] == '\0') {
403 if (out != start_out) {
412 return -1; /* error*/
413 case 7: /* terminate*/
430 _static_ void __modify_bundle(bundle * kb, int caller_pid,
431 app_info_from_db * menu_info, int cmd)
433 // warning: unused parameter
434 caller_pid = caller_pid;
436 bundle_del(kb, AUL_K_PKG_NAME);
437 bundle_del(kb, AUL_K_EXEC);
438 bundle_del(kb, AUL_K_PACKAGETYPE);
439 bundle_del(kb, AUL_K_HWACC);
441 /* Parse app_path to retrieve default bundle*/
442 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_OPEN || cmd == APP_RESUME) {
444 char exe[MAX_PATH_LEN];
447 ptr = _get_original_app_path(menu_info);
449 flag = __parser(ptr, exe, sizeof(exe));
455 _D("parsing app_path: EXEC - %s\n", exe);
458 flag = __parser(ptr, key, sizeof(key));
463 flag = __parser(ptr, value, sizeof(value));
468 /*bundle_del(kb, key);*/
469 bundle_add(kb, key, value);
471 } else if (flag == 0) {
472 _D("parsing app_path: No arguments\n");
474 _D("parsing app_path: Invalid argument\n");
479 _static_ int __child_raise_win_by_x(int pid, void *priv)
481 // warning: unused parameter
484 return x_util_raise_win(pid);
487 _static_ int __raise_win_by_x(int pid)
490 if (x_util_raise_win(pid) == 0)
493 /* support app launched by shell script*/
495 _D("X raise failed. try to find first child & raise it - c:%d p:%d\n",
500 if (__proc_iter_pgid(pgid, __child_raise_win_by_x, NULL) < 0)
506 _static_ int __send_to_sigkill(int pid)
514 if (killpg(pgid, SIGKILL) < 0)
520 _static_ int __term_app(int pid)
524 (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int)) < 0) {
525 _D("terminate packet send error - use SIGKILL");
526 if (__send_to_sigkill(pid) < 0) {
527 _E("fail to killing - %d\n", pid);
535 _static_ int __resume_app(int pid)
540 __app_send_raw(pid, APP_RESUME_BY_PID, (unsigned char *)&dummy,
543 _E("resume packet timeout error");
545 _D("resume packet send error - use raise win");
546 if (__raise_win_by_x(pid) < 0) {
547 _E("raise failed - %d resume fail\n", pid);
548 _E("we will term the app - %d\n", pid);
549 __send_to_sigkill(pid);
559 static int __get_caller_pid(bundle *kb)
564 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
568 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
580 _static_ int __foward_cmd(int cmd, bundle *kb, int cr_pid)
583 char tmp_pid[MAX_PID_STR_BUFSZ];
588 if ((pid = __get_caller_pid(kb)) < 0)
591 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", cr_pid);
593 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
595 bundle_encode(kb, &kb_data, &datalen);
596 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
604 _static_ void __real_send(int clifd, int ret)
606 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
607 if (errno == EPIPE) {
608 _E("send failed due to EPIPE.\n");
610 _E("send fail to client");
616 _static_ void __send_result_to_caller(int clifd, int ret)
620 int cmdline_changed = 0;
621 int cmdline_exist = 0;
627 __real_send(clifd, ret);
630 /* check normally was launched?*/
633 cmdline = __proc_get_cmdline_bypid(ret);
634 if (cmdline == NULL) {
635 _E("error founded when being launched with %d", ret);
637 } else if (strcmp(cmdline, launchpad_cmdline)) {
646 _D("-- now wait to change cmdline --");
647 usleep(50 * 1000); /* 50ms sleep*/
649 } while (wait_count <= 20); /* max 50*20ms will be sleep*/
651 if ((!cmdline_exist) && (!cmdline_changed)) {
652 __real_send(clifd, -1); /* abnormally launched*/
656 if (!cmdline_changed)
657 _E("process launched, but cmdline not changed");
659 __real_send(clifd, ret);
663 static app_info_from_db *_get_app_info_from_bundle_by_pkgname(
664 const char *pkgname, bundle *kb)
666 app_info_from_db *menu_info;
668 menu_info = calloc(1, sizeof(app_info_from_db));
669 if (menu_info == NULL) {
673 menu_info->pkg_name = strdup(pkgname);
674 menu_info->app_path = strdup(bundle_get_val(kb, AUL_K_EXEC));
675 if (menu_info->app_path != NULL)
676 menu_info->original_app_path = strdup(menu_info->app_path);
677 menu_info->pkg_type = strdup(bundle_get_val(kb, AUL_K_PACKAGETYPE));
678 menu_info->hwacc = strdup(bundle_get_val(kb, AUL_K_HWACC));
680 if (!_get_app_path(menu_info)) {
681 _free_app_info_from_db(menu_info);
688 _static_ void __launchpad_main_loop(int main_fd)
691 app_pkt_t *pkt = NULL;
692 app_info_from_db *menu_info = NULL;
694 const char *pkg_name = NULL;
695 const char *app_path = NULL;
699 int is_real_launch = 0;
701 char sock_path[UNIX_PATH_MAX] = {0,};
703 pkt = __app_recv_raw(main_fd, &clifd, &cr);
705 _D("packet is NULL");
709 kb = bundle_decode(pkt->data, pkt->len);
711 _D("bundle decode error");
716 PERF("packet processing start");
718 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
719 _D("pkg name : %s\n", pkg_name);
721 menu_info = _get_app_info_from_bundle_by_pkgname(pkg_name, kb);
722 if (menu_info == NULL) {
723 _D("such pkg no found");
727 app_path = _get_app_path(menu_info);
728 if(app_path == NULL) {
729 _E("app_path is NULL");
732 if (app_path[0] != '/') {
733 _D("app_path is not absolute path");
737 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
738 pkg_name = _get_pkgname(menu_info);
740 PERF("get package information & modify bundle done");
746 _E("lock up test log(no error) : fork done");
750 __signal_unset_sigchld();
753 snprintf(sock_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, getpid());
756 PERF("prepare exec - first done");
757 _E("lock up test log(no error) : prepare exec - first done");
759 if (__prepare_exec(pkg_name, app_path,
760 menu_info, kb) < 0) {
761 _E("preparing work fail to launch - "
762 "can not launch %s\n", pkg_name);
766 PERF("prepare exec - second done");
767 _E("lock up test log(no error) : prepare exec - second done");
769 __real_launch(app_path, kb);
773 _D("==> real launch pid : %d %s\n", pid, app_path);
778 __send_result_to_caller(clifd, pid);
781 if (is_real_launch) {
783 __signal_block_sigchld();
784 __send_app_launch_signal(pid);
785 __signal_unblock_sigchld();
789 if (menu_info != NULL)
790 _free_app_info_from_db(menu_info);
797 /* Active Flusing for Daemon */
798 if (initialized > AUL_POLL_CNT) {
799 sqlite3_release_memory(SQLITE_FLUSH_MAX);
806 _static_ int __launchpad_pre_init(int argc, char **argv)
813 /* get my(launchpad) command line*/
814 launchpad_cmdline = __proc_get_cmdline_bypid(getpid());
815 if (launchpad_cmdline == NULL) {
816 _E("launchpad cmdline fail to get");
819 _D("launchpad cmdline = %s", launchpad_cmdline);
821 /* create launchpad sock */
822 fd = __create_server_sock(WRT_LAUNCHPAD_PID);
824 _E("server sock error");
828 __preload_init(argc, argv);
830 __preload_init_for_wrt();
832 __preexec_init(argc, argv);
837 _static_ int __launchpad_post_init()
839 /* Setting this as a global variable to keep track
840 of launchpad poll cnt */
841 /* static int initialized = 0;*/
848 if (__signal_set_sigchld() < 0)
856 int main(int argc, char **argv)
859 struct pollfd pfds[POLLFD_MAX];
862 /* init without concerning X & EFL*/
863 main_fd = __launchpad_pre_init(argc, argv);
865 _E("launchpad pre init failed");
869 pfds[0].fd = main_fd;
870 pfds[0].events = POLLIN;
874 if (poll(pfds, POLLFD_MAX, -1) < 0)
877 /* init with concerning X & EFL (because of booting
879 if (__launchpad_post_init() < 0) {
880 _E("launcpad post init failed");
884 for (i = 0; i < POLLFD_MAX; i++) {
885 if ((pfds[i].revents & POLLIN) != 0) {
886 __launchpad_main_loop(pfds[i].fd);