4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jungmin Cho <chivalry.cho@samsung.com>, Gwangho Hwang <gwang.hwang@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.
23 * simple AUL daemon - launchpad
30 #include <sys/types.h>
37 #include <sys/prctl.h>
45 #include "menu_db_util.h"
46 #include "simple_util.h"
47 #include "access_control.h"
59 #include <sys/smack.h>
60 #include "fileutils.h"
61 #include <sys/capability.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
69 #define PATH_TMP "/tmp"
70 #define PATH_DATA "/data"
72 #define SDK_CODE_COVERAGE "CODE_COVERAGE"
73 #define SDK_DEBUG "DEBUG"
74 #define SDK_DYNAMIC_ANALYSIS "DYNAMIC_ANALYSIS"
75 #define SDK_UNIT_TEST "UNIT_TEST"
76 #define SDK_VALGRIND "VALGRIND"
78 /* DLP is short for debug-launchpad */
79 #define DLP_K_DEBUG_ARG "__DLP_DEBUG_ARG__"
80 #define DLP_K_UNIT_TEST_ARG "__DLP_UNIT_TEST_ARG__"
81 #define DLP_K_VALGRIND_ARG "__DLP_VALGRIND_ARG__"
83 #define PATH_GDBSERVER "/home/developer/sdk_tools/gdbserver/gdbserver"
84 #define PATH_VALGRIND "/home/developer/sdk_tools/valgrind/usr/bin/valgrind"
85 #define PATH_DA_SO "/usr/lib/da_probe_osp.so"
86 #define PATH_NATIVE_APP "/opt/apps/"
88 #define OPT_VALGRIND_LOGFILE "--log-file="
89 #define OPT_VALGRIND_LOGFILE_FIXED "--log-file=/tmp/valgrind_result.txt"
90 #define PATH_VALGRIND_LOGFILE "/tmp/valgrind_result.txt"
91 #define OPT_VALGRIND_XMLFILE "--xml-file="
92 #define OPT_VALGRIND_XMLFILE_FIXED "--xml-file=/tmp/valgrind_result.xml"
93 #define PATH_VALGRIND_XMLFILE "/tmp/valgrind_result.xml"
96 #define PATH_MEMCHECK "/opt/home/developer/sdk_tools/valgrind/usr/lib/valgrind/memcheck-arm-linux"
98 #define PATH_MEMCHECK "/opt/home/developer/sdk_tools/valgrind/usr/lib/valgrind/memcheck-x86-linux"
101 #define POLL_VALGRIND_LOGFILE 0x00000001
102 #define POLL_VALGRIND_XMLFILE 0x00000002
104 #define CAPABILITY_SET_ORIGINAL 0
105 #define CAPABILITY_SET_INHERITABLE 1
107 static int need_to_set_inh_cap_after_fork = 0;
108 static char *launchpad_cmdline;
109 static int initialized = 0;
111 static int poll_outputfile = 0;
114 void __set_env(app_info_from_db * menu_info, bundle * kb);
115 int __prepare_exec(const char *pkg_name,
116 const char *app_path, app_info_from_db * menu_info,
118 int __fake_launch_app(int cmd, int pid, bundle * kb);
119 char **__create_argc_argv(bundle * kb, int *margc, const char *app_path);
120 int __normal_fork_exec(int argc, char **argv);
121 void __real_launch(const char *app_path, bundle * kb);
122 static inline int __parser(const char *arg, char *out, int out_size);
123 void __modify_bundle(bundle * kb, int caller_pid,
124 app_info_from_db * menu_info, int cmd);
125 int __send_to_sigkill(int pid);
126 int __term_app(int pid);
127 void __real_send(int clifd, int ret);
128 void __send_result_to_caller(int clifd, int ret);
129 void __launchpad_main_loop(int main_fd);
130 int __launchpad_pre_init(int argc, char **argv);
131 int __launchpad_post_init();
133 extern ail_error_e ail_db_close(void);
139 char buf[MAX_LOCAL_BUFSZ];
142 /* we should reset oomadj value as default because child
143 inherits from parent oom_adj*/
144 snprintf(buf, MAX_LOCAL_BUFSZ, "/proc/%d/oom_adj", getpid());
145 fp = fopen(buf, "w");
148 fprintf(fp, "%d", -16);
152 void __set_sdk_env(app_info_from_db* menu_info, char* str) {
153 char buf_pkgname[MAX_LOCAL_BUFSZ];
154 char buf[MAX_LOCAL_BUFSZ];
157 _D("key : %s / value : %s", AUL_K_SDK, str);
158 /* http://gcc.gnu.org/onlinedocs/gcc/Cross_002dprofiling.html*/
159 /* GCOV_PREFIX contains the prefix to add to the absolute paths */
160 /* in the object file. Prefix can be absolute, or relative.*/
161 /* The default is no prefix. */
162 /* GCOV_PREFIX_STRIP indicates the how many initial directory names */
163 /* to stripoff the hardwired absolute paths. Default value is 0. */
164 if (strncmp(str, SDK_CODE_COVERAGE, strlen(str)) == 0) {
165 strncpy(buf_pkgname,_get_pkgname(menu_info),MAX_LOCAL_BUFSZ-1);
166 buf_pkgname[MAX_LOCAL_BUFSZ-1]='\0';
167 snprintf(buf, MAX_LOCAL_BUFSZ, PATH_TMP"/%s"PATH_DATA
168 , strtok(buf_pkgname,"."));
169 ret = setenv("GCOV_PREFIX", buf, 1);
170 _D("GCOV_PREFIX : %d", ret);
171 ret = setenv("GCOV_PREFIX_STRIP", "0", 1);
172 _D("GCOV_PREFIX_STRIP : %d", ret);
173 } else if (strncmp(str, SDK_DYNAMIC_ANALYSIS, strlen(str)) == 0) {
174 ret = setenv("LD_PRELOAD", PATH_DA_SO, 1);
175 _D("LD_PRELOAD : %d", ret);
180 void __set_env(app_info_from_db * menu_info, bundle * kb)
183 const char **str_array;
187 setenv("PKG_NAME", _get_pkgname(menu_info), 1);
191 str = bundle_get_val(kb, AUL_K_STARTTIME);
193 setenv("APP_START_TIME", str, 1);
195 if(bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
196 str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
197 if(str_array != NULL) {
198 for (i = 0; i < len; i++) {
199 _D("index : [%d]", i);
200 __set_sdk_env(menu_info, (char *)str_array[i]);
204 str = bundle_get_val(kb, AUL_K_SDK);
206 __set_sdk_env(menu_info, (char *)str);
209 if (menu_info->hwacc != NULL)
210 setenv("HWACC", menu_info->hwacc, 1);
213 int __prepare_exec(const char *pkg_name,
214 const char *app_path, app_info_from_db * menu_info,
218 char process_name[AUL_PR_NAME];
221 /* Set new session ID & new process group ID*/
222 /* In linux, child can set new session ID without check permission */
223 /* TODO : should be add to check permission in the kernel*/
226 __preexec_run(menu_info->pkg_type, pkg_name, app_path);
232 if(bundle_get_val(kb, AUL_K_PRIVACY_APPID) == NULL) {
233 _D("pkg_name : %s / pkg_type : %s / app_path : %s ", pkg_name
234 , menu_info->pkg_type, app_path);
235 if ((ret = __set_access(pkg_name, menu_info->pkg_type
238 _D("fail to set privileges - check your package's credential : %d\n"
243 /* SET DUMPABLE - for coredump*/
244 prctl(PR_SET_DUMPABLE, 1);
246 /* SET PROCESS NAME*/
247 if (app_path == NULL) {
248 _D("app_path should not be NULL - check menu db");
251 file_name = strrchr(app_path, '/') + 1;
252 if (file_name == NULL) {
253 _D("can't locate file name to execute");
256 memset(process_name, '\0', AUL_PR_NAME);
257 snprintf(process_name, AUL_PR_NAME, "%s", file_name);
258 prctl(PR_SET_NAME, process_name);
261 __set_env(menu_info, kb);
266 int __fake_launch_app(int cmd, int pid, bundle * kb)
272 bundle_encode(kb, &kb_data, &datalen);
273 if ((ret = __app_send_raw(pid, cmd, kb_data, datalen)) < 0)
274 _E("error request fake launch - error code = %d", ret);
279 char** __add_arg(bundle * kb, char **argv, int *margc, const char *key)
281 const char *str = NULL;
282 const char **str_array = NULL;
285 char ** new_argv = NULL;
287 if(bundle_get_type(kb, key) & BUNDLE_TYPE_ARRAY) {
288 str_array = bundle_get_str_array(kb, key, &len);
290 str = bundle_get_val(kb, key);
296 if(str_array != NULL) {
297 if(strncmp(key, DLP_K_DEBUG_ARG, strlen(key)) == 0
298 || strncmp(key, DLP_K_VALGRIND_ARG, strlen(key)) == 0)
300 new_argv = (char **) realloc(argv
301 , sizeof(char *) * (*margc+len+2));
303 _E("realloc fail (key = %s)", key);
306 for(i=*margc+len+1; i-(len+1)>=0; i--) {
307 new_argv[i] = new_argv[i-(len+1)];
309 // need to add new_argv[0]
310 for(i=0; i<len; i++) {
311 new_argv[1+i] = strdup(str_array[i]);
313 len++; /* gdbserver or valgrind */
314 _D("uid : %d", getuid());
315 _D("euid : %d", geteuid());
316 _D("gid : %d", getgid());
317 _D("egid : %d", getegid());
319 new_argv = (char **) realloc(argv
320 , sizeof(char *) * (*margc+len+1));
322 _E("realloc fail (key = %s)", key);
325 for(i=0; i<len; i++) {
326 new_argv[*margc+i] = strdup(str_array[i]);
329 new_argv[*margc+len] = NULL;
332 if(strncmp(key, DLP_K_DEBUG_ARG, strlen(key)) == 0
333 || strncmp(key, DLP_K_VALGRIND_ARG, strlen(key)) == 0)
335 new_argv = (char **) realloc(argv
336 , sizeof(char *) * (*margc+2));
338 _E("realloc fail (key = %s)", key);
341 for(i=*margc+1; i-1>=0; i--) {
342 new_argv[i] = new_argv[i-1];
344 // need to add new_argv[0]
349 if(new_argv==NULL) return argv;
353 char **__create_argc_argv(bundle * kb, int *margc, const char *app_path)
356 char **new_argv = NULL;
359 const char *str = NULL;
360 const char **str_array = NULL;
364 argc = bundle_export_to_argv(kb, &argv);
366 for(i=1; i<argc; i++) {
367 argv[i] = strdup(argv[i]);
369 argv[0] = strdup(app_path);
371 _E("bundle_export_to_argv error");
375 if(bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
376 str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
378 str = bundle_get_val(kb, AUL_K_SDK);
384 if(str_array == NULL) {
389 for (i = 0; i < len; i++) {
390 if(str_array[i] == NULL) break;
391 _D("index : [%d]", i);
393 if (strncmp(str_array[i], SDK_DEBUG, strlen(str_array[i])) == 0)
395 char buf[MAX_LOCAL_BUFSZ];
396 if (argv[0]) free(argv[0]);
397 snprintf(buf,MAX_LOCAL_BUFSZ,"%s.exe",app_path);
398 argv[0] = strdup(buf);
399 new_argv = __add_arg(kb, argv, &argc, DLP_K_DEBUG_ARG);
400 new_argv[0] = strdup(PATH_GDBSERVER);
403 else if (strncmp(str_array[i], SDK_VALGRIND
404 , strlen(str_array[i])) == 0)
406 new_argv = __add_arg(kb, argv, &argc
407 , DLP_K_VALGRIND_ARG);
408 new_argv[0] = strdup(PATH_VALGRIND);
411 else if (strncmp(str_array[i], SDK_UNIT_TEST
412 , strlen(str_array[i])) == 0)
414 new_argv = __add_arg(kb, argv, &argc
415 , DLP_K_UNIT_TEST_ARG);
420 if(new_argv==NULL) return argv;
424 int __normal_fork_exec(int argc, char **argv)
426 _D("start real fork and exec\n");
428 if (execv(argv[0], argv) < 0) { /* Flawfinder: ignore */
429 if (errno == EACCES) {
430 _E("such a file is no executable - %s", argv[0]);
432 _E("unknown executable error - %s", argv[0]);
440 void __real_launch(const char *app_path, bundle * kb)
446 app_argv = __create_argc_argv(kb, &app_argc, app_path);
448 for (i = 0; i < app_argc; i++)
449 _D("input argument %d : %s##", i, app_argv[i]);
451 PERF("setup argument done");
452 _D("lock up test log(no error) : setup argument done");
454 /* Temporary log: launch time checking */
455 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:launchpad:done]", app_path);
457 __normal_fork_exec(app_argc, app_argv);
459 for(i=0; i<app_argc; i++) {
460 if(app_argv[i]) free(app_argv[i]);
467 * Parsing original app path to retrieve default bundle
469 * -1 : Invalid sequence
470 * -2 : Buffer overflow
473 static inline int __parser(const char *arg, char *out, int out_size)
477 char *start_out = out;
479 if (arg == NULL || out == NULL) {
480 /* Handles null buffer*/
484 for (i = 0; out_size > 1; i++) {
508 case 2: /* escape start*/
523 case 4: /* character escape*/
524 if (arg[i] == '\0') {
534 if (out != start_out) {
543 return -1; /* error*/
544 case 7: /* terminate*/
561 void __modify_bundle(bundle * kb, int caller_pid,
562 app_info_from_db * menu_info, int cmd)
564 bundle_del(kb, AUL_K_PKG_NAME);
565 bundle_del(kb, AUL_K_EXEC);
566 bundle_del(kb, AUL_K_PACKAGETYPE);
567 bundle_del(kb, AUL_K_HWACC);
569 /* Parse app_path to retrieve default bundle*/
570 if (cmd == APP_START || cmd == APP_START_RES || cmd == APP_OPEN
571 || cmd == APP_RESUME)
574 char exe[MAX_PATH_LEN];
577 ptr = _get_original_app_path(menu_info);
579 flag = __parser(ptr, exe, sizeof(exe));
585 _D("parsing app_path: EXEC - %s\n", exe);
588 flag = __parser(ptr, key, sizeof(key));
593 flag = __parser(ptr, value, sizeof(value));
598 /*bundle_del(kb, key);*/
599 bundle_add(kb, key, value);
601 } else if (flag == 0) {
602 _D("parsing app_path: No arguments\n");
604 _D("parsing app_path: Invalid argument\n");
609 int __send_to_sigkill(int pid)
617 if (killpg(pgid, SIGKILL) < 0)
623 int __term_app(int pid)
627 (pid, APP_TERM_BY_PID, (unsigned char *)&dummy, sizeof(int)) < 0) {
628 _D("terminate packet send error - use SIGKILL");
629 if (__send_to_sigkill(pid) < 0) {
630 _E("fail to killing - %d\n", pid);
638 static int __get_caller_pid(bundle *kb)
643 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
647 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
659 int __foward_cmd(int cmd, bundle *kb, int cr_pid)
662 char tmp_pid[MAX_PID_STR_BUFSZ];
667 if ((pid = __get_caller_pid(kb)) < 0)
670 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", cr_pid);
672 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
674 bundle_encode(kb, &kb_data, &datalen);
675 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
683 void __real_send(int clifd, int ret)
685 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
686 if (errno == EPIPE) {
687 _E("send failed due to EPIPE.\n");
689 _E("send fail to client");
695 void __send_result_to_caller(int clifd, int ret)
699 int cmdline_changed = 0;
700 int cmdline_exist = 0;
706 __real_send(clifd, ret);
709 /* check normally was launched?*/
712 cmdline = __proc_get_cmdline_bypid(ret);
713 if (cmdline == NULL) {
714 _E("error founded when being launched with %d", ret);
716 } else if (strcmp(cmdline, launchpad_cmdline)) {
725 _D("-- now wait to change cmdline --");
726 usleep(50 * 1000); /* 50ms sleep*/
728 } while (wait_count <= 20); /* max 50*20ms will be sleep*/
730 if ((!cmdline_exist) && (!cmdline_changed)) {
731 _E("abnormally launched");
732 __real_send(clifd, -1); /* abnormally launched*/
736 if (!cmdline_changed)
737 _E("process launched, but cmdline not changed");
739 __real_send(clifd, ret);
743 static app_info_from_db *_get_app_info_from_bundle_by_pkgname(
744 const char *pkgname, bundle *kb)
746 app_info_from_db *menu_info;
748 menu_info = calloc(1, sizeof(app_info_from_db));
749 if (menu_info == NULL) {
753 menu_info->pkg_name = strdup(pkgname);
754 menu_info->app_path = strdup(bundle_get_val(kb, AUL_K_EXEC));
755 if (menu_info->app_path != NULL)
756 menu_info->original_app_path = strdup(menu_info->app_path);
757 menu_info->pkg_type = strdup(bundle_get_val(kb, AUL_K_PACKAGETYPE));
758 menu_info->hwacc = strdup(bundle_get_val(kb, AUL_K_HWACC));
760 if (!_get_app_path(menu_info)) {
761 _free_app_info_from_db(menu_info);
771 int get_native_appid(const char* app_path, char** appid) {
772 int rc = smack_lgetlabel(app_path, appid, SMACK_LABEL_ACCESS);
774 if (rc != 0 || *appid == NULL) {
775 _E("smack_lgetlabel fail");
779 if (strlen(*appid) != APPID_LEN) {
780 _E("wrong native appid : %s", *appid);
784 if (strlen(app_path) < sizeof(PATH_NATIVE_APP)+APPID_LEN-1) {
785 _E("wrong native app_path : %s", app_path);
788 else if ( strncmp(app_path, PATH_NATIVE_APP, sizeof(PATH_NATIVE_APP)-1)
789 || strncmp(&app_path[sizeof(PATH_NATIVE_APP)-1]
790 , *appid,APPID_LEN) )
792 _E("wrong native app_path : %s", app_path);
796 _D("get_appid return : %s", *appid);
800 int apply_smack_rules(const char* subject, const char* object
801 , const char* access_type)
803 struct smack_accesses *rules = NULL;
805 _D("apply_smack_rules : %s %s %s", subject, object, access_type);
807 if (smack_accesses_new(&rules)) {
808 _E("smack_accesses_new fail");
812 if (smack_accesses_add(rules, subject, object, access_type)) {
813 smack_accesses_free(rules);
814 _E("smack_accesses_add fail");
818 if (smack_accesses_apply(rules)) {
819 smack_accesses_free(rules);
820 _E("smack_accesses_apply fail");
824 smack_accesses_free(rules);
829 int __prepare_valgrind_outputfile(bundle *kb)
831 const char *str = NULL;
832 const char **str_array = NULL;
836 if(bundle_get_type(kb, DLP_K_VALGRIND_ARG) & BUNDLE_TYPE_ARRAY) {
837 str_array = bundle_get_str_array(kb, DLP_K_VALGRIND_ARG, &len);
839 str = bundle_get_val(kb, DLP_K_VALGRIND_ARG);
845 if(str_array == NULL) return 0;
847 for (i = 0; i < len; i++) {
848 if(str_array[i] == NULL) break;
849 /* valgrind log file option */
850 if (strncmp(str_array[i], OPT_VALGRIND_LOGFILE
851 , strlen(OPT_VALGRIND_LOGFILE)) == 0)
853 if(strncmp(str_array[i], OPT_VALGRIND_LOGFILE_FIXED
854 , strlen(str_array[i])))
856 _E("wrong valgrind option(%s). It should be %s"
858 , OPT_VALGRIND_LOGFILE_FIXED);
861 poll_outputfile |= POLL_VALGRIND_LOGFILE;
862 if(remove(PATH_VALGRIND_LOGFILE)){
863 _D("cannot remove %s"
864 , PATH_VALGRIND_LOGFILE);
868 /* valgrind xml file option */
869 else if (strncmp(str_array[i], OPT_VALGRIND_XMLFILE
870 , strlen(OPT_VALGRIND_XMLFILE)) == 0)
872 if(strncmp(str_array[i], OPT_VALGRIND_XMLFILE_FIXED
873 , strlen(str_array[i])))
875 _E("wrong valgrind option(%s). It should be %s"
877 , OPT_VALGRIND_XMLFILE_FIXED);
880 poll_outputfile |= POLL_VALGRIND_XMLFILE;
881 if(remove(PATH_VALGRIND_XMLFILE)){
882 _D("cannot remove %s"
883 , PATH_VALGRIND_XMLFILE);
891 extern int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
893 int __adjust_process_capability(int sv)
895 static struct __user_cap_header_struct h;
896 static struct __user_cap_data_struct ori_d[_LINUX_CAPABILITY_U32S_2];
897 static struct __user_cap_data_struct inh_d[_LINUX_CAPABILITY_U32S_2];
898 static int isinit = 0;
901 h.version = _LINUX_CAPABILITY_VERSION_2;
907 inh_d[CAP_TO_INDEX(CAP_NET_RAW)].inheritable |=
908 CAP_TO_MASK(CAP_NET_RAW);
909 inh_d[CAP_TO_INDEX(CAP_SYS_CHROOT)].inheritable |=
910 CAP_TO_MASK(CAP_SYS_CHROOT);
914 if(sv == CAPABILITY_SET_ORIGINAL) return 0;
918 _E("__adjust_process_capability init failed");
922 if(sv == CAPABILITY_SET_ORIGINAL) {
924 if (capset(&h, ori_d) < 0) {
925 _E("Capability setting error");
929 else if (sv == CAPABILITY_SET_INHERITABLE) {
931 if (capset(&h, inh_d) < 0) {
932 _E("Capability setting error");
940 int __adjust_file_capability(const char * path)
942 if(cap_set_file(path,cap_from_text("CAP_NET_RAW,CAP_SYS_CHROOT+i"))) {
943 _E("cap_set_file failed : %s", path);
949 int __prepare_fork(bundle *kb, char *appid)
951 const char *str = NULL;
952 const char **str_array = NULL;
957 if(bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
958 str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
960 str = bundle_get_val(kb, AUL_K_SDK);
966 if(str_array == NULL) return 0;
968 for (i = 0; i < len; i++) {
969 if(str_array[i] == NULL) break;
971 if (strncmp(str_array[i], SDK_DEBUG, strlen(str_array[i])) == 0)
973 if(apply_smack_rules("sdbd",appid,"w")) {
974 _E("unable to set sdbd rules");
978 // FIXME: set gdbfolder to 755 also
979 if(dlp_chmod(PATH_GDBSERVER
980 , S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP
984 _D("unable to set 755 to %s", PATH_GDBSERVER);
986 __adjust_file_capability(PATH_GDBSERVER);
987 need_to_set_inh_cap_after_fork++;
990 else if (strncmp(str_array[i], SDK_VALGRIND
991 , strlen(str_array[i])) == 0)
993 if (__prepare_valgrind_outputfile(kb)) return 1;
994 __adjust_file_capability(PATH_MEMCHECK);
1000 /* chmod and chsmack to read file without root privilege */
1001 void __chmod_chsmack_toread(const char * path)
1004 if(dlp_chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 0))
1006 _E("unable to set 644 to %s", path);
1008 _D("set 644 to %s", path);
1012 if(smack_setlabel(path, "*", SMACK_LABEL_ACCESS))
1014 _E("failed chsmack -a \"*\" %s", path);
1016 _D("chsmack -a \"*\" %s", path);
1022 /* waiting for creating outputfile by child process */
1023 void __waiting_outputfile()
1026 while(poll_outputfile && wait_count<10) {
1027 /* valgrind log file */
1028 if( (poll_outputfile & POLL_VALGRIND_LOGFILE)
1029 && (access(PATH_VALGRIND_LOGFILE,F_OK)==0) )
1031 __chmod_chsmack_toread(PATH_VALGRIND_LOGFILE);
1032 poll_outputfile &= ~POLL_VALGRIND_LOGFILE;
1035 /* valgrind xml file */
1036 if( (poll_outputfile & POLL_VALGRIND_XMLFILE)
1037 && (access(PATH_VALGRIND_XMLFILE,F_OK)==0) )
1039 __chmod_chsmack_toread(PATH_VALGRIND_XMLFILE);
1040 poll_outputfile &= ~POLL_VALGRIND_XMLFILE;
1043 if(poll_outputfile) {
1044 _D("-- now wait for creating the file --");
1045 usleep(50 * 1000); /* 50ms sleep*/
1050 if(wait_count==10) _E("faild to waiting");
1054 int __stdout_stderr_redirection(int defpid)
1056 char defpath[UNIX_PATH_MAX];
1057 int deffd, result=0;
1060 snprintf(defpath, UNIX_PATH_MAX, "/proc/%d/fd/1", defpid);
1061 deffd = open(defpath,O_WRONLY);
1063 _E("opening caller(%d) stdout failed due to %s"
1064 , defpid, strerror(errno));
1072 snprintf(defpath, UNIX_PATH_MAX, "/proc/%d/fd/2", defpid);
1073 deffd = open(defpath,O_WRONLY);
1075 _E("opening caller(%d) stderr failed due to %s"
1076 , defpid,strerror(errno));
1086 void __launchpad_main_loop(int main_fd)
1089 app_pkt_t *pkt = NULL;
1090 app_info_from_db *menu_info = NULL;
1092 const char *pkg_name = NULL;
1093 const char *app_path = NULL;
1097 int is_real_launch = 0;
1099 char sock_path[UNIX_PATH_MAX] = {0,};
1100 char * appid = NULL;
1102 pkt = __app_recv_raw(main_fd, &clifd, &cr);
1104 _D("packet is NULL");
1108 kb = bundle_decode(pkt->data, pkt->len);
1110 _D("bundle decode error");
1115 PERF("packet processing start");
1117 pkg_name = bundle_get_val(kb, AUL_K_PKG_NAME);
1118 _D("pkg name : %s\n", pkg_name);
1120 menu_info = _get_app_info_from_bundle_by_pkgname(pkg_name, kb);
1121 if (menu_info == NULL) {
1122 _D("such pkg no found");
1126 app_path = _get_app_path(menu_info);
1127 if(app_path == NULL) {
1128 _E("app_path is NULL");
1131 if (app_path[0] != '/') {
1132 _D("app_path is not absolute path");
1137 int rc = get_native_appid(app_path,&appid);
1138 if(rc!=0 || appid==NULL) {
1139 _E("unable to get native appid");
1140 if(appid) free(appid);
1145 __modify_bundle(kb, cr.pid, menu_info, pkt->cmd);
1146 pkg_name = _get_pkgname(menu_info);
1148 PERF("get package information & modify bundle done");
1150 if(__prepare_fork(kb,appid)) goto end;
1154 if(need_to_set_inh_cap_after_fork) {
1155 __adjust_process_capability(CAPABILITY_SET_INHERITABLE);
1156 need_to_set_inh_cap_after_fork = 0;
1159 _D("lock up test log(no error) : fork done");
1163 __signal_unset_sigchld();
1166 snprintf(sock_path, UNIX_PATH_MAX, "%s/%d", AUL_SOCK_PREFIX
1170 if(__stdout_stderr_redirection(__get_caller_pid(kb))) {
1171 _E("__stdout_stderr_redirection fail");
1174 PERF("prepare exec - first done");
1175 _D("lock up test log(no error) : prepare exec - first done");
1177 if (__prepare_exec(pkg_name, app_path,
1178 menu_info, kb) < 0) {
1179 _E("preparing work fail to launch - "
1180 "can not launch %s\n", pkg_name);
1184 PERF("prepare exec - second done");
1185 _D("lock up test log(no error) : prepare exec - second done");
1187 __real_launch(app_path, kb);
1192 _D("==> real launch pid : %d %s\n", pid, app_path);
1196 __send_result_to_caller(clifd, pid);
1199 if (is_real_launch) {
1201 __signal_block_sigchld();
1202 __send_app_launch_signal(pid);
1203 __signal_unblock_sigchld();
1207 if (menu_info != NULL)
1208 _free_app_info_from_db(menu_info);
1217 /* Active Flusing for Daemon */
1218 if (initialized > AUL_POLL_CNT) {
1219 sqlite3_release_memory(SQLITE_FLUSH_MAX);
1224 if(poll_outputfile) __waiting_outputfile();
1227 int __launchpad_pre_init(int argc, char **argv)
1234 /* get my(launchpad) command line*/
1235 launchpad_cmdline = __proc_get_cmdline_bypid(getpid());
1236 if (launchpad_cmdline == NULL) {
1237 _E("launchpad cmdline fail to get");
1240 _D("launchpad cmdline = %s", launchpad_cmdline);
1242 /* create launchpad sock */
1243 fd = __create_server_sock(DEBUG_LAUNCHPAD_PID);
1245 _E("server sock error");
1249 __preload_init(argc, argv);
1251 __preexec_init(argc, argv);
1256 int __launchpad_post_init()
1258 /* Setting this as a global variable to keep track
1259 of launchpad poll cnt */
1260 /* static int initialized = 0;*/
1267 if (__signal_set_sigchld() < 0)
1275 int main(int argc, char **argv)
1278 struct pollfd pfds[POLLFD_MAX];
1281 __adjust_process_capability(CAPABILITY_SET_ORIGINAL);
1283 /* init without concerning X & EFL*/
1284 main_fd = __launchpad_pre_init(argc, argv);
1286 _E("launchpad pre init failed");
1290 pfds[0].fd = main_fd;
1291 pfds[0].events = POLLIN;
1292 pfds[0].revents = 0;
1295 if (poll(pfds, POLLFD_MAX, -1) < 0)
1298 /* init with concerning X & EFL (because of booting
1300 if (__launchpad_post_init() < 0) {
1301 _E("launcpad post init failed");
1305 for (i = 0; i < POLLFD_MAX; i++) {
1306 if ((pfds[i].revents & POLLIN) != 0) {
1307 __launchpad_main_loop(pfds[i].fd);