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"
63 #define _static_ static inline
65 #define SQLITE_FLUSH_MAX (1048576) /* (1024*1024) */
66 #define AUL_POLL_CNT 15
67 #define AUL_PR_NAME 16
70 static char *launchpad_cmdline;
71 static char *__appid = NULL;
72 static int initialized = 0;
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 inline int __parser(const char *arg, char *out, int out_size);
83 _static_ void __modify_bundle(bundle * kb, int caller_pid,
84 app_info_from_db * menu_info, int cmd);
85 _static_ int __child_raise_win_by_x(int pid, void *priv);
86 _static_ int __raise_win_by_x(int pid);
87 _static_ int __send_to_sigkill(int pid);
88 _static_ int __term_app(int pid);
89 _static_ int __resume_app(int pid);
90 _static_ int __real_send(int clifd, int ret);
91 _static_ void __send_result_to_caller(int clifd, int ret);
92 _static_ void __launchpad_main_loop(int main_fd);
93 _static_ int __launchpad_pre_init(int argc, char **argv);
94 _static_ int __launchpad_post_init();
96 extern ail_error_e ail_db_close(void);
101 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
105 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
109 str = bundle_get_val(kb, AUL_K_STARTTIME);
111 setenv("APP_START_TIME", str, 1);
113 if (menu_info->hwacc != NULL)
114 setenv("HWACC", menu_info->hwacc, 1);
115 if (menu_info->taskmanage != NULL)
116 setenv("TASKMANAGE", menu_info->taskmanage, 1);
119 _static_ int __prepare_exec(const char *pkg_name,
120 const char *app_path, app_info_from_db * menu_info,
124 char process_name[AUL_PR_NAME];
127 /* Set new session ID & new process group ID*/
128 /* In linux, child can set new session ID without check permission */
129 /* TODO : should be add to check permission in the kernel*/
132 __preexec_run(menu_info->pkg_type, pkg_name, app_path);
135 if(bundle_get_val(kb, AUL_K_PRIVACY_APPID) == NULL) {
136 _D("pkg_name : %s / pkg_type : %s / app_path : %s ", pkg_name, menu_info->pkg_type, app_path);
137 if ((ret = __set_access(pkg_name, menu_info->pkg_type, app_path)) < 0) {
138 _D("fail to set privileges - check your package's credential : %d\n", ret);
143 /* SET DUMPABLE - for coredump*/
144 prctl(PR_SET_DUMPABLE, 1);
146 /* SET PROCESS NAME*/
147 if (app_path == NULL) {
148 _D("app_path should not be NULL - check menu db");
151 file_name = strrchr(app_path, '/') + 1;
152 if (file_name == NULL) {
153 _D("can't locate file name to execute");
156 memset(process_name, '\0', AUL_PR_NAME);
157 snprintf(process_name, AUL_PR_NAME, "%s", file_name);
158 prctl(PR_SET_NAME, process_name);
161 __set_env(menu_info, kb);
166 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb)
172 bundle_encode(kb, &kb_data, &datalen);
173 if ((ret = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
174 _E("error request fake launch - error code = %d", ret);
179 _static_ char **__create_argc_argv(bundle * kb, int *margc)
184 argc = bundle_export_to_argv(kb, &argv);
190 _static_ int __normal_fork_exec(int argc, char **argv)
192 _D("start real fork and exec\n");
194 if (execv(argv[0], argv) < 0) { /* Flawfinder: ignore */
196 _E("such a file is no executable - %s", argv[0]);
198 _E("unknown executable error - %s", argv[0]);
205 _static_ void __real_launch(const char *app_path, bundle * kb)
211 app_argv = __create_argc_argv(kb, &app_argc);
212 app_argv[0] = strdup(app_path);
214 for (i = 0; i < app_argc; i++) {
217 SECURE_LOGD("input argument %d : %s##", i, app_argv[i]);
220 PERF("setup argument done");
222 /* Temporary log: launch time checking */
223 SECURE_LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:launchpad:done]", app_path);
225 __preload_exec(app_argc, app_argv);
227 __normal_fork_exec(app_argc, app_argv);
232 * Parsing original app path to retrieve default bundle
234 * -1 : Invalid sequence
235 * -2 : Buffer overflow
238 static inline int __parser(const char *arg, char *out, int out_size)
242 char *start_out = out;
244 if (arg == NULL || out == NULL) {
245 /* Handles null buffer*/
249 for (i = 0; out_size > 1; i++) {
273 case 2: /* escape start*/
288 case 4: /* character escape*/
289 if (arg[i] == '\0') {
299 if (out != start_out) {
308 return -1; /* error*/
309 case 7: /* terminate*/
326 _static_ void __modify_bundle(bundle * kb, int caller_pid,
327 app_info_from_db * menu_info, int cmd)
329 bundle_del(kb, AUL_K_PKG_NAME);
330 bundle_del(kb, AUL_K_EXEC);
331 bundle_del(kb, AUL_K_PACKAGETYPE);
332 bundle_del(kb, AUL_K_HWACC);
333 bundle_del(kb, AUL_K_TASKMANAGE);
335 /* Parse app_path to retrieve default bundle*/
336 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_START_ASYNC || cmd == APP_OPEN || cmd == APP_RESUME) {
338 char exe[MAX_PATH_LEN];
341 ptr = _get_original_app_path(menu_info);
343 flag = __parser(ptr, exe, sizeof(exe));
349 SECURE_LOGD("parsing app_path: EXEC - %s\n", exe);
352 flag = __parser(ptr, key, sizeof(key));
357 flag = __parser(ptr, value, sizeof(value));
362 /*bundle_del(kb, key);*/
363 bundle_add(kb, key, value);
365 } else if (flag == 0) {
366 _D("parsing app_path: No arguments\n");
368 _D("parsing app_path: Invalid argument\n");
373 _static_ int __child_raise_win_by_x(int pid, void *priv)
375 return x_util_raise_win(pid);
378 _static_ int __raise_win_by_x(int pid)
381 if (x_util_raise_win(pid) == 0)
384 /* support app launched by shell script*/
386 _D("X raise failed. try to find first child & raise it - c:%d p:%d\n",
391 if (__proc_iter_pgid(pgid, __child_raise_win_by_x, NULL) < 0)
397 _static_ int __send_to_sigkill(int pid)
405 if (killpg(pgid, SIGKILL) < 0)
411 _static_ int __term_app(int pid)
415 (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int)) < 0) {
416 _D("terminate packet send error - use SIGKILL");
417 if (__send_to_sigkill(pid) < 0) {
418 _E("fail to killing - %d\n", pid);
426 _static_ int __resume_app(int pid)
431 __app_send_raw(pid, APP_RESUME_BY_PID, (unsigned char *)&dummy,
434 _E("resume packet timeout error");
436 _D("resume packet send error - use raise win");
437 if (__raise_win_by_x(pid) < 0) {
438 _E("raise failed - %d resume fail\n", pid);
439 _E("we will term the app - %d\n", pid);
440 __send_to_sigkill(pid);
450 static int __get_caller_pid(bundle *kb)
455 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
459 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
471 _static_ int __foward_cmd(int cmd, bundle *kb, int cr_pid)
474 char tmp_pid[MAX_PID_STR_BUFSZ];
479 if ((pid = __get_caller_pid(kb)) < 0)
482 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", cr_pid);
484 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
486 bundle_encode(kb, &kb_data, &datalen);
487 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
495 _static_ int __real_send(int clifd, int ret)
497 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
498 if (errno == EPIPE) {
499 _E("send failed due to EPIPE.\n");
503 _E("send fail to client");
510 _static_ void __send_result_to_caller(int clifd, int ret)
514 int cmdline_changed = 0;
515 int cmdline_exist = 0;
522 __real_send(clifd, ret);
525 /* check normally was launched?*/
528 cmdline = __proc_get_cmdline_bypid(ret);
529 if (cmdline == NULL) {
530 _E("error founded when being launched with %d", ret);
532 } else if (strcmp(cmdline, launchpad_cmdline)) {
541 _D("-- now wait to change cmdline --");
542 usleep(100 * 1000); /* 100ms sleep*/
544 } while (wait_count <= 20); /* max 100*20ms will be sleep*/
546 if ((!cmdline_exist) && (!cmdline_changed)) {
547 __real_send(clifd, -1); /* abnormally launched*/
551 if (!cmdline_changed)
552 _E("process launched, but cmdline not changed");
554 if(__real_send(clifd, ret) < 0) {
555 r = kill(ret, SIGKILL);
557 _E("send SIGKILL: %s", strerror(errno));
563 static app_info_from_db *_get_app_info_from_bundle_by_pkgname(
564 const char *pkgname, bundle *kb)
566 app_info_from_db *menu_info;
568 menu_info = calloc(1, sizeof(app_info_from_db));
569 if (menu_info == NULL) {
573 menu_info->pkg_name = strdup(pkgname);
574 menu_info->app_path = strdup(bundle_get_val(kb, AUL_K_EXEC));
575 if (menu_info->app_path != NULL)
576 menu_info->original_app_path = strdup(menu_info->app_path);
577 menu_info->pkg_type = strdup(bundle_get_val(kb, AUL_K_PACKAGETYPE));
578 menu_info->hwacc = strdup(bundle_get_val(kb, AUL_K_HWACC));
579 menu_info->taskmanage = strdup(bundle_get_val(kb, AUL_K_TASKMANAGE));
581 if (!_get_app_path(menu_info)) {
582 _free_app_info_from_db(menu_info);
589 static void __release_appid_at_exit(void)
591 if (__appid != NULL) {
596 _static_ void __launchpad_main_loop(int main_fd)
599 app_pkt_t *pkt = NULL;
600 app_info_from_db *menu_info = NULL;
602 const char *pkg_name = NULL;
603 const char *app_path = NULL;
607 int is_real_launch = 0;
609 char sock_path[UNIX_PATH_MAX] = {0,};
611 pkt = __app_recv_raw(main_fd, &clifd, &cr);
613 _D("packet is NULL");
617 kb = bundle_decode(pkt->data, pkt->len);
619 _D("bundle decode error");
624 PERF("packet processing start");
626 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
627 SECURE_LOGD("pkg name : %s\n", pkg_name);
629 menu_info = _get_app_info_from_bundle_by_pkgname(pkg_name, kb);
630 if (menu_info == NULL) {
631 _D("such pkg no found");
635 app_path = _get_app_path(menu_info);
636 if(app_path == NULL) {
637 _E("app_path is NULL");
640 if (app_path[0] != '/') {
641 _D("app_path is not absolute path");
645 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
646 pkg_name = _get_pkgname(menu_info);
648 PERF("get package information & modify bundle done");
654 _D("lock up test log(no error) : fork done");
658 __signal_unset_sigchld();
661 snprintf(sock_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, getpid());
664 PERF("prepare exec - first done");
665 _D("lock up test log(no error) : prepare exec - first done");
667 __appid = strdup(pkg_name);
668 aul_set_preinit_appid(__appid);
669 atexit(__release_appid_at_exit);
671 if (__prepare_exec(pkg_name, app_path,
672 menu_info, kb) < 0) {
673 SECURE_LOGE("preparing work fail to launch - "
674 "can not launch %s\n", pkg_name);
678 PERF("prepare exec - second done");
679 _D("lock up test log(no error) : prepare exec - second done");
681 __real_launch(app_path, kb);
685 SECURE_LOGD("==> real launch pid : %d %s\n", pid, app_path);
690 __send_result_to_caller(clifd, pid);
693 if (is_real_launch) {
695 __signal_block_sigchld();
696 __send_app_launch_signal(pid);
697 __signal_unblock_sigchld();
701 if (menu_info != NULL)
702 _free_app_info_from_db(menu_info);
709 /* Active Flusing for Daemon */
710 if (initialized > AUL_POLL_CNT) {
711 sqlite3_release_memory(SQLITE_FLUSH_MAX);
718 _static_ int __launchpad_pre_init(int argc, char **argv)
725 /* get my(launchpad) command line*/
726 launchpad_cmdline = __proc_get_cmdline_bypid(getpid());
727 if (launchpad_cmdline == NULL) {
728 _E("launchpad cmdline fail to get");
731 _D("launchpad cmdline = %s", launchpad_cmdline);
733 /* create launchpad sock */
734 fd = __create_server_sock(LAUNCHPAD_PID);
736 _E("server sock error");
740 __preload_init(argc, argv);
742 /* preexec is not used */
743 //__preexec_init(argc, argv);
748 _static_ int __launchpad_post_init()
750 /* Setting this as a global variable to keep track
751 of launchpad poll cnt */
752 /* static int initialized = 0;*/
759 if (__signal_set_sigchld() < 0)
767 int main(int argc, char **argv)
770 struct pollfd pfds[POLLFD_MAX];
773 /* init without concerning X & EFL*/
774 main_fd = __launchpad_pre_init(argc, argv);
776 _E("launchpad pre init failed");
780 pfds[0].fd = main_fd;
781 pfds[0].events = POLLIN;
785 if (poll(pfds, POLLFD_MAX, -1) < 0)
788 /* init with concerning X & EFL (because of booting
790 if (__launchpad_post_init() < 0) {
791 _E("launcpad post init failed");
795 for (i = 0; i < POLLFD_MAX; i++) {
796 if ((pfds[i].revents & POLLIN) != 0) {
797 __launchpad_main_loop(pfds[i].fd);