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>
40 #include <sys/resource.h>
47 #include "menu_db_util.h"
48 #include "simple_util.h"
49 #include "access_control.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
71 static char *launchpad_cmdline;
72 static char *__appid = NULL;
73 static int initialized = 0;
75 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb);
76 _static_ int __prepare_exec(const char *pkg_name,
77 const char *app_path, app_info_from_db * menu_info,
79 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb);
80 _static_ char **__create_argc_argv(bundle * kb, int *margc);
81 _static_ int __normal_fork_exec(int argc, char **argv);
82 _static_ void __real_launch(const char *app_path, bundle * kb);
83 static inline int __parser(const char *arg, char *out, int out_size);
84 _static_ void __modify_bundle(bundle * kb, int caller_pid,
85 app_info_from_db * menu_info, int cmd);
86 _static_ int __child_raise_win_by_x(int pid, void *priv);
87 _static_ int __raise_win_by_x(int pid);
88 _static_ int __send_to_sigkill(int pid);
89 _static_ int __term_app(int pid);
90 _static_ int __resume_app(int pid);
91 _static_ int __real_send(int clifd, int ret);
92 _static_ void __send_result_to_caller(int clifd, int ret);
93 _static_ void __launchpad_main_loop(int main_fd);
94 _static_ int __launchpad_pre_init(int argc, char **argv);
95 _static_ int __launchpad_post_init();
97 extern ail_error_e ail_db_close(void);
102 _static_ void __set_env(app_info_from_db * menu_info, bundle * kb)
106 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
110 str = bundle_get_val(kb, AUL_K_STARTTIME);
112 setenv("APP_START_TIME", str, 1);
114 if (menu_info->hwacc != NULL)
115 setenv("HWACC", menu_info->hwacc, 1);
116 if (menu_info->taskmanage != NULL)
117 setenv("TASKMANAGE", menu_info->taskmanage, 1);
120 _static_ int __prepare_exec(const char *pkg_name,
121 const char *app_path, app_info_from_db * menu_info,
125 char process_name[AUL_PR_NAME];
128 /* Set new session ID & new process group ID*/
129 /* In linux, child can set new session ID without check permission */
130 /* TODO : should be add to check permission in the kernel*/
133 __preexec_run(menu_info->pkg_type, pkg_name, app_path);
136 SECURE_LOGD("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);
141 /* SET DUMPABLE - for coredump*/
142 prctl(PR_SET_DUMPABLE, 1);
144 /* SET PROCESS NAME*/
145 if (app_path == NULL) {
146 _D("app_path should not be NULL - check menu db");
149 file_name = strrchr(app_path, '/') + 1;
150 if (file_name == NULL) {
151 _D("can't locate file name to execute");
154 memset(process_name, '\0', AUL_PR_NAME);
155 snprintf(process_name, AUL_PR_NAME, "%s", file_name);
156 prctl(PR_SET_NAME, process_name);
159 __set_env(menu_info, kb);
164 _static_ int __fake_launch_app(int cmd, int pid, bundle * kb)
170 bundle_encode(kb, &kb_data, &datalen);
171 if ((ret = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
172 _E("error request fake launch - error code = %d", ret);
177 _static_ char **__create_argc_argv(bundle * kb, int *margc)
182 argc = bundle_export_to_argv(kb, &argv);
188 _static_ int __normal_fork_exec(int argc, char **argv)
190 _D("start real fork and exec\n");
192 #ifdef _APPFW_FEATURE_PRIORITY_CHANGE
193 int res = setpriority(PRIO_PROCESS, 0, 0);
196 SECURE_LOGE("Setting process (%d) priority to 0 failed, errno: %d (%s)",
197 getpid(), errno, strerror(errno));
200 if (execv(argv[0], argv) < 0) { /* Flawfinder: ignore */
202 _E("such a file is no executable - %s", argv[0]);
204 _E("unknown executable error - %s", argv[0]);
211 _static_ void __real_launch(const char *app_path, bundle * kb)
217 app_argv = __create_argc_argv(kb, &app_argc);
218 app_argv[0] = strdup(app_path);
220 for (i = 0; i < app_argc; i++) {
223 SECURE_LOGD("input argument %d : %s##", i, app_argv[i]);
226 PERF("setup argument done");
228 /* Temporary log: launch time checking */
229 SECURE_LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:launchpad:done]", app_path);
231 __preload_exec(app_argc, app_argv);
233 __normal_fork_exec(app_argc, app_argv);
238 * Parsing original app path to retrieve default bundle
240 * -1 : Invalid sequence
241 * -2 : Buffer overflow
244 static inline int __parser(const char *arg, char *out, int out_size)
248 char *start_out = out;
250 if (arg == NULL || out == NULL) {
251 /* Handles null buffer*/
255 for (i = 0; out_size > 1; i++) {
279 case 2: /* escape start*/
294 case 4: /* character escape*/
295 if (arg[i] == '\0') {
305 if (out != start_out) {
314 return -1; /* error*/
315 case 7: /* terminate*/
332 _static_ void __modify_bundle(bundle * kb, int caller_pid,
333 app_info_from_db * menu_info, int cmd)
335 bundle_del(kb, AUL_K_PKG_NAME);
336 bundle_del(kb, AUL_K_EXEC);
337 bundle_del(kb, AUL_K_PACKAGETYPE);
338 bundle_del(kb, AUL_K_HWACC);
339 bundle_del(kb, AUL_K_TASKMANAGE);
341 /* Parse app_path to retrieve default bundle*/
343 || cmd == APP_START_RES
344 || cmd == APP_START_ASYNC
345 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
346 || cmd == APP_START_MULTI_INSTANCE
352 char exe[MAX_PATH_LEN];
355 ptr = _get_original_app_path(menu_info);
357 flag = __parser(ptr, exe, sizeof(exe));
363 SECURE_LOGD("parsing app_path: EXEC - %s\n", exe);
366 flag = __parser(ptr, key, sizeof(key));
371 flag = __parser(ptr, value, sizeof(value));
376 /*bundle_del(kb, key);*/
377 bundle_add(kb, key, value);
379 } else if (flag == 0) {
380 _D("parsing app_path: No arguments\n");
382 _D("parsing app_path: Invalid argument\n");
387 _static_ int __child_raise_win_by_x(int pid, void *priv)
389 return x_util_raise_win(pid);
392 _static_ int __raise_win_by_x(int pid)
395 if (x_util_raise_win(pid) == 0)
398 /* support app launched by shell script*/
400 _D("X raise failed. try to find first child & raise it - c:%d p:%d\n",
405 if (__proc_iter_pgid(pgid, __child_raise_win_by_x, NULL) < 0)
411 _static_ int __send_to_sigkill(int pid)
419 if (killpg(pgid, SIGKILL) < 0)
425 _static_ int __term_app(int pid)
429 (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int)) < 0) {
430 _D("terminate packet send error - use SIGKILL");
431 if (__send_to_sigkill(pid) < 0) {
432 _E("fail to killing - %d\n", pid);
440 _static_ int __resume_app(int pid)
445 __app_send_raw(pid, APP_RESUME_BY_PID, (unsigned char *)&dummy,
448 _E("resume packet timeout error");
450 _D("resume packet send error - use raise win");
451 if (__raise_win_by_x(pid) < 0) {
452 _E("raise failed - %d resume fail\n", pid);
453 _E("we will term the app - %d\n", pid);
454 __send_to_sigkill(pid);
464 static int __get_caller_pid(bundle *kb)
469 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
473 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
485 _static_ int __foward_cmd(int cmd, bundle *kb, int cr_pid)
488 char tmp_pid[MAX_PID_STR_BUFSZ];
493 if ((pid = __get_caller_pid(kb)) < 0)
496 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", cr_pid);
498 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
500 bundle_encode(kb, &kb_data, &datalen);
501 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
509 _static_ int __real_send(int clifd, int ret)
511 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
512 if (errno == EPIPE) {
513 _E("send failed due to EPIPE.\n");
517 _E("send fail to client");
524 _static_ void __send_result_to_caller(int clifd, int ret)
528 int cmdline_changed = 0;
529 int cmdline_exist = 0;
536 __real_send(clifd, ret);
539 /* check normally was launched?*/
542 cmdline = __proc_get_cmdline_bypid(ret);
543 if (cmdline == NULL) {
544 _E("error founded when being launched with %d", ret);
546 } else if (strcmp(cmdline, launchpad_cmdline)) {
555 _D("-- now wait to change cmdline --");
556 usleep(100 * 1000); /* 100ms sleep*/
558 } while (wait_count <= 20); /* max 100*20ms will be sleep*/
560 if ((!cmdline_exist) && (!cmdline_changed)) {
561 __real_send(clifd, -1); /* abnormally launched*/
565 if (!cmdline_changed)
566 _E("process launched, but cmdline not changed");
568 if(__real_send(clifd, ret) < 0) {
569 r = kill(ret, SIGKILL);
571 _E("send SIGKILL: %s", strerror(errno));
577 static app_info_from_db *_get_app_info_from_bundle_by_pkgname(
578 const char *pkgname, bundle *kb)
580 app_info_from_db *menu_info;
582 menu_info = calloc(1, sizeof(app_info_from_db));
583 if (menu_info == NULL) {
587 menu_info->pkg_name = strdup(pkgname);
588 menu_info->app_path = strdup(bundle_get_val(kb, AUL_K_EXEC));
589 if (menu_info->app_path != NULL)
590 menu_info->original_app_path = strdup(menu_info->app_path);
591 menu_info->pkg_type = strdup(bundle_get_val(kb, AUL_K_PACKAGETYPE));
592 menu_info->hwacc = strdup(bundle_get_val(kb, AUL_K_HWACC));
593 menu_info->taskmanage = strdup(bundle_get_val(kb, AUL_K_TASKMANAGE));
595 if (!_get_app_path(menu_info)) {
596 _free_app_info_from_db(menu_info);
603 static void __release_appid_at_exit(void)
605 if (__appid != NULL) {
610 _static_ void __launchpad_main_loop(int main_fd)
613 app_pkt_t *pkt = NULL;
614 app_info_from_db *menu_info = NULL;
616 const char *pkg_name = NULL;
617 const char *app_path = NULL;
621 int is_real_launch = 0;
623 char sock_path[UNIX_PATH_MAX] = {0,};
625 pkt = __app_recv_raw(main_fd, &clifd, &cr);
627 _D("packet is NULL");
631 kb = bundle_decode(pkt->data, pkt->len);
633 _D("bundle decode error");
638 PERF("packet processing start");
640 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
641 SECURE_LOGD("pkg name : %s\n", pkg_name);
643 menu_info = _get_app_info_from_bundle_by_pkgname(pkg_name, kb);
644 if (menu_info == NULL) {
645 _D("such pkg no found");
649 app_path = _get_app_path(menu_info);
650 if(app_path == NULL) {
651 _E("app_path is NULL");
654 if (app_path[0] != '/') {
655 _D("app_path is not absolute path");
659 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
660 pkg_name = _get_pkgname(menu_info);
662 PERF("get package information & modify bundle done");
668 _D("lock up test log(no error) : fork done");
672 __signal_unset_sigchld();
675 snprintf(sock_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX, getpid());
678 PERF("prepare exec - first done");
679 _D("lock up test log(no error) : prepare exec - first done");
681 __appid = strdup(pkg_name);
682 aul_set_preinit_appid(__appid);
683 atexit(__release_appid_at_exit);
685 if (__prepare_exec(pkg_name, app_path,
686 menu_info, kb) < 0) {
687 SECURE_LOGE("preparing work fail to launch - "
688 "can not launch %s\n", pkg_name);
692 PERF("prepare exec - second done");
693 _D("lock up test log(no error) : prepare exec - second done");
695 __real_launch(app_path, kb);
699 SECURE_LOGD("==> real launch pid : %d %s\n", pid, app_path);
704 __send_result_to_caller(clifd, pid);
707 if (is_real_launch) {
709 __signal_block_sigchld();
710 __send_app_launch_signal(pid);
711 __signal_unblock_sigchld();
715 if (menu_info != NULL)
716 _free_app_info_from_db(menu_info);
723 /* Active Flusing for Daemon */
724 if (initialized > AUL_POLL_CNT) {
725 sqlite3_release_memory(SQLITE_FLUSH_MAX);
732 _static_ int __launchpad_pre_init(int argc, char **argv)
739 /* get my(launchpad) command line*/
740 launchpad_cmdline = __proc_get_cmdline_bypid(getpid());
741 if (launchpad_cmdline == NULL) {
742 _E("launchpad cmdline fail to get");
745 _D("launchpad cmdline = %s", launchpad_cmdline);
747 /* create launchpad sock */
748 fd = __create_server_sock(LAUNCHPAD_PID);
750 _E("server sock error");
754 __preload_init(argc, argv);
756 // __preexec_init(argc, argv);
761 _static_ int __launchpad_post_init()
763 /* Setting this as a global variable to keep track
764 of launchpad poll cnt */
765 /* static int initialized = 0;*/
772 if (__signal_set_sigchld() < 0)
780 int main(int argc, char **argv)
783 struct pollfd pfds[POLLFD_MAX];
786 /* init without concerning X & EFL*/
787 main_fd = __launchpad_pre_init(argc, argv);
789 _E("launchpad pre init failed");
793 pfds[0].fd = main_fd;
794 pfds[0].events = POLLIN;
797 #ifdef _APPFW_FEATURE_PRIORITY_CHANGE
798 int res = setpriority(PRIO_PROCESS, 0, -12);
801 SECURE_LOGE("Setting process (%d) priority to -12 failed, errno: %d (%s)",
802 getpid(), errno, strerror(errno));
806 if (poll(pfds, POLLFD_MAX, -1) < 0)
809 /* init with concerning X & EFL (because of booting
811 if (__launchpad_post_init() < 0) {
812 _E("launcpad post init failed");
816 for (i = 0; i < POLLFD_MAX; i++) {
817 if ((pfds[i].revents & POLLIN) != 0) {
818 __launchpad_main_loop(pfds[i].fd);